git: 200b2faf4819 - main - dwc: Limit the DMA of the QoS core to the address width of the hardware

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 13:55:55 UTC
The branch main has been updated by manu:

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

commit 200b2faf481930ae66984743f04efccebde920d3
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-27 11:07:48 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-20 13:55:33 +0000

    dwc: Limit the DMA of the QoS core to the address width of the hardware
    
    The QoS core is used with the 64 bits descriptor address scheme, TDES1
    and the *DLAR_HI registers holding the upper 32 bits.  This only works
    when the core was synthesized with a wider than 32 bits address bus.  The
    RK3568 GMAC for example has a 32 bits DMA: DMASBMR.EAME cannot be set and
    the *DLAR_HI registers read back as zero.  The DMA tags allowed any
    address, so on systems with memory above 4GB the descriptor rings and the
    mbufs could be allocated there and the core fetched descriptors and data
    from the truncated addresses.  Nothing could be sent or received.
    
    Read the address width from the ADDR64 field of the MAC hardware feature 1
    register, like Linux does, and limit the DMA tags to it.  Memory that
    cannot be reached is bounced by busdma.
    
    Tested on a Quartz64 Model A with 8GB of RAM and on a NanoPi R5S.
    
    Reviewed by:    br
    Differential Revision:  https://reviews.freebsd.org/D61018
---
 sys/dev/dwc/dwc4_dma.c | 26 ++++++++++++++++++++++----
 sys/dev/dwc/dwc4_reg.h |  2 ++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/sys/dev/dwc/dwc4_dma.c b/sys/dev/dwc/dwc4_dma.c
index 15a454dcee15..c42fb1e5e8c5 100644
--- a/sys/dev/dwc/dwc4_dma.c
+++ b/sys/dev/dwc/dwc4_dma.c
@@ -621,10 +621,28 @@ int
 dma4_init(struct dwc_softc *sc)
 {
 	struct mbuf *m;
+	bus_addr_t lowaddr;
 	uint32_t reg;
+	u_int addr_bits;
 	int error;
 	int idx;
 
+	/* The address width of the DMA is a synthesis option of the core. */
+	reg = READ4(sc, ETH_MACHWF1R);
+	switch ((reg & MACHWF1R_ADDR64_M) >> MACHWF1R_ADDR64_S) {
+	case 1:
+		addr_bits = 40;
+		break;
+	case 2:
+		addr_bits = 48;
+		break;
+	default:
+		addr_bits = 32;
+		break;
+	}
+	lowaddr = (bus_addr_t)MIN(((uint64_t)1 << addr_bits) - 1,
+	    (uint64_t)BUS_SPACE_MAXADDR);
+
 	WRITE4(sc, ETH_MACQ0TXFCR, 0xffff << 16 | 1);
 	WRITE4(sc, ETH_MACRXFCR, 1);
 
@@ -692,7 +710,7 @@ dma4_init(struct dwc_softc *sc)
 	error = bus_dma_tag_create(
 	    bus_get_dma_tag(sc->dev),	/* Parent tag. */
 	    DWC_DESC_RING_ALIGN, 0,	/* alignment, boundary */
-	    BUS_SPACE_MAXADDR,		/* lowaddr */
+	    lowaddr,			/* lowaddr */
 	    BUS_SPACE_MAXADDR,		/* highaddr */
 	    NULL, NULL,			/* filter, filterarg */
 	    TX_DESC_SIZE, 1, 		/* maxsize, nsegments */
@@ -727,7 +745,7 @@ dma4_init(struct dwc_softc *sc)
 	error = bus_dma_tag_create(
 	    bus_get_dma_tag(sc->dev),	/* Parent tag. */
 	    1, 0,			/* alignment, boundary */
-	    BUS_SPACE_MAXADDR,		/* lowaddr */
+	    lowaddr,			/* lowaddr */
 	    BUS_SPACE_MAXADDR,		/* highaddr */
 	    NULL, NULL,			/* filter, filterarg */
 	    MCLBYTES * TX_MAP_MAX_SEGS,	/* maxsize */
@@ -766,7 +784,7 @@ dma4_init(struct dwc_softc *sc)
 	error = bus_dma_tag_create(
 	    bus_get_dma_tag(sc->dev),	/* Parent tag. */
 	    DWC_DESC_RING_ALIGN, 0,	/* alignment, boundary */
-	    BUS_SPACE_MAXADDR,		/* lowaddr */
+	    lowaddr,			/* lowaddr */
 	    BUS_SPACE_MAXADDR,		/* highaddr */
 	    NULL, NULL,			/* filter, filterarg */
 	    RX_DESC_SIZE, 1, 		/* maxsize, nsegments */
@@ -801,7 +819,7 @@ dma4_init(struct dwc_softc *sc)
 	error = bus_dma_tag_create(
 	    bus_get_dma_tag(sc->dev),	/* Parent tag. */
 	    1, 0,			/* alignment, boundary */
-	    BUS_SPACE_MAXADDR,		/* lowaddr */
+	    lowaddr,			/* lowaddr */
 	    BUS_SPACE_MAXADDR,		/* highaddr */
 	    NULL, NULL,			/* filter, filterarg */
 	    MCLBYTES, 1, 		/* maxsize, nsegments */
diff --git a/sys/dev/dwc/dwc4_reg.h b/sys/dev/dwc/dwc4_reg.h
index 87d234274159..b7c96433cb55 100644
--- a/sys/dev/dwc/dwc4_reg.h
+++ b/sys/dev/dwc/dwc4_reg.h
@@ -76,6 +76,8 @@
 #define	ETH_MACDR		0x0114
 #define	ETH_MACHWF0R		0x011C
 #define	ETH_MACHWF1R		0x0120
+#define	 MACHWF1R_ADDR64_S	14 /* Address Width */
+#define	 MACHWF1R_ADDR64_M	(0x3 << MACHWF1R_ADDR64_S)
 #define	ETH_MACHWF2R		0x0124
 #define	ETH_MACHWF3R		0x0128
 #define	ETH_MACMDIOAR		0x0200