git: e23c13a0c967 - main - pci_dw: Honor the max-link-speed property

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Sun, 15 Nov 2026 08:19:05 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=e23c13a0c96772efe8fb3d1a88b219fb4ef189a2

commit e23c13a0c96772efe8fb3d1a88b219fb4ef189a2
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-27 07:24:57 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-15 08:18:46 +0000

    pci_dw: Honor the max-link-speed property
    
    The root port reports in LNKCAP the highest link speed the core was
    synthesized for, which can be more than the PHY behind it supports.  On
    the RK3568 pcie2x1 the root port advertises 8GT/s while the PHY only
    does 5GT/s.  The link stays at 2.5GT/s after the directed speed change.
    
    Read max-link-speed from the device tree and write it to the Max Link
    Speed field of LNKCAP and to the Target Link Speed field of LNKCTL2
    before requesting the speed change.  Nothing changes when the property
    is absent.
    
    Tested on a Quartz64 Model A with a Samsung 970 EVO Plus NVMe drive: the
    link now trains at 5GT/s and the sequential throughput goes from 200MB/s
    to 400MB/s.
    
    Differential Revision:  https://reviews.freebsd.org/D61014
---
 sys/dev/pci/pci_dw.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/pci/pci_dw.h |  1 +
 2 files changed, 55 insertions(+)

diff --git a/sys/dev/pci/pci_dw.c b/sys/dev/pci/pci_dw.c
index 7c9448a5593e..2ca1fc60c5b1 100644
--- a/sys/dev/pci/pci_dw.c
+++ b/sys/dev/pci/pci_dw.c
@@ -324,6 +324,52 @@ pci_dw_map_out_atu(struct pci_dw_softc *sc, int idx, int type,
 		    pci_addr, size));
 }
 
+#define	DW_LINK_CTL2_TARGET_SPEED	0x000f
+
+static void
+pci_dw_set_max_link_speed(struct pci_dw_softc *sc)
+{
+	uint32_t reg;
+	uint16_t ctl2;
+	u_int cap, ptr, n;
+
+	if (sc->max_link_speed == 0)
+		return;
+	if (sc->max_link_speed > 4) {
+		device_printf(sc->dev,
+		    "'max-link-speed' property have invalid value: %u\n",
+		    sc->max_link_speed);
+		return;
+	}
+
+	/* Find the PCI Express capability of the root port. */
+	cap = 0;
+	ptr = DBI_RD1(sc, PCIR_CAP_PTR) & ~3;
+	for (n = 0; ptr != 0 && n < 48; n++) {
+		if (DBI_RD1(sc, ptr + PCICAP_ID) == PCIY_EXPRESS) {
+			cap = ptr;
+			break;
+		}
+		ptr = DBI_RD1(sc, ptr + PCICAP_NEXTPTR) & ~3;
+	}
+	if (cap == 0) {
+		device_printf(sc->dev,
+		    "Cannot find the PCI Express capability\n");
+		return;
+	}
+
+	pci_dw_dbi_protect(sc, false);
+	reg = DBI_RD4(sc, cap + PCIER_LINK_CAP);
+	reg &= ~PCIEM_LINK_CAP_MAX_SPEED;
+	reg |= sc->max_link_speed;
+	DBI_WR4(sc, cap + PCIER_LINK_CAP, reg);
+	ctl2 = DBI_RD2(sc, cap + PCIER_LINK_CTL2);
+	ctl2 &= ~DW_LINK_CTL2_TARGET_SPEED;
+	ctl2 |= sc->max_link_speed;
+	DBI_WR2(sc, cap + PCIER_LINK_CTL2, ctl2);
+	pci_dw_dbi_protect(sc, true);
+}
+
 static int
 pci_dw_setup_hw(struct pci_dw_softc *sc)
 {
@@ -421,6 +467,8 @@ pci_dw_setup_hw(struct pci_dw_softc *sc)
 	}
 	DBI_WR4(sc, DW_GEN2_CTRL, reg);
 
+	pci_dw_set_max_link_speed(sc);
+
 	reg = DBI_RD4(sc, DW_GEN2_CTRL);
 	reg |= DIRECT_SPEED_CHANGE;
 	DBI_WR4(sc, DW_GEN2_CTRL, reg);
@@ -762,6 +810,12 @@ pci_dw_init(device_t dev)
 		sc->num_lanes = 1;
 	dprintf("%s: num lanes: %d\n", __func__, sc->num_lanes);
 
+	rv = OF_getencprop(sc->node, "max-link-speed", &sc->max_link_speed,
+	    sizeof(sc->max_link_speed));
+	if (rv != sizeof(sc->max_link_speed))
+		sc->max_link_speed = 0;
+	dprintf("%s: max link speed: %u\n", __func__, sc->max_link_speed);
+
 	if (sc->num_lanes != 1 && sc->num_lanes != 2 &&
 	    sc->num_lanes != 4 && sc->num_lanes != 8) {
 		device_printf(dev,
diff --git a/sys/dev/pci/pci_dw.h b/sys/dev/pci/pci_dw.h
index 5b6f62ad6c84..15a7e705ac9e 100644
--- a/sys/dev/pci/pci_dw.h
+++ b/sys/dev/pci/pci_dw.h
@@ -116,6 +116,7 @@ struct pci_dw_softc {
 	bus_dma_tag_t		dmat;
 
 	int			num_lanes;
+	u_int			max_link_speed;
 	int			num_out_regions;
 	struct resource		*iatu_ur_res;	/* NB: May be dbi_res */
 	bus_addr_t		iatu_ur_offset;