Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/pi5_gem.h # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / pi5_gem.h` 304 lines ยท 15829 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* pi5_gem.h -- Raspberry Pi 5 network lane, cut 3: the RP1 Ethernet MAC (Cadence GEM_GXL) sends and 18 * receives ONE frame through DMA descriptors that live only inside the ADR 0020 bounce window and pass the 19 * dma_window.c validator, plus the PHY link/loopback helpers the proof needs. 20 * 21 * LABEL: software-only DMA confinement (Pi 5, weaker than the Spark). Pi-only; not linked into the Spark or QEMU. 22 * 23 * Hosted-testable over fake registers and a fake GEM (tests/test_pi5_gem.c, with mutants). A host test proves 24 * the decisions and the ORDER of register writes, NOT the hardware. The MMIO glue at the bottom of pi5_gem.c 25 * is the CK_PI5_NET image only. Every number below cites its source; anything not read is UNVERIFIED. 26 * 27 * SOURCES (all read in this cut; Linux = raspberrypi/linux branch rpi-6.12.y): 28 * [M1] drivers/net/ethernet/cadence/macb.h: register offsets (MACB_NCR/NCFGR/NSR/TSR/RBQP/TBQP/RSR/ISR/IDR, 29 * GEM_DMACFG/USRIO/PBUFRXCUT/AMP/HRB/HRT/SA1B..SA4T/DCFG1/DCFG2/DCFG6, GEM_ISR/TBQP/RBQP/RBQS/IDR(hw_q), 30 * GEM_TBQPH/RBQPH), bit fields (NCR LLB/RE/TE/MPE/CLRSTAT/TSTART, NCFGR SPD/FD/GBE/DBW/DRFCS/CLK, DMACFG 31 * FBLDO/RXBMS/TXPBMS/TXCOEN/RXBS/ADDR64/RXEXT/TXEXT, TSR UBR/TGO/BEX/COMP/UND, RSR BNA/REC/OVR, DCFG6 DAW64), 32 * descriptor layout (struct macb_dma_desc addr/ctrl, macb_dma_desc_64 addrh/resvd; RX USED/WRAP/FRMLEN/SOF/EOF, 33 * TX FRMLEN(14 bits on GEM)/LAST/NOCRC/BUF_EXHAUSTED/UNDERRUN/ERROR/WRAP/USED). 34 * [M2] drivers/net/ethernet/cadence/macb_main.c: macb_reset_hw, macb_init_hw, macb_configure_dma, gem_init_axi, 35 * macb_set_hwaddr, macb_init_buffers (per-queue RBQP/TBQP, shared RBQPH/TBQPH), macb_probe_queues (queue mask 36 * from DCFG6), macb_dbw (DCFG1.DBWDEF), macb_init (USRIO rgmii), macb_mac_link_up (SPD/FD/GBE, then RE|TE), 37 * macb_set_addr / gem_rx_refill (RX address word: USED clears last, WRAP on the last slot), macb_tx_map (TX ctrl 38 * written last, USED clear hands the slot over), raspberrypi_rp1_config (dma_burst_length 16, jumbo caps), 39 * macb_init_tieoff (unused RX queue: WRAP|USED descriptor), rp1.dtsi rp1_eth (phy-mode rgmii-id, 40 * cdns,aw2w-max-pipe/ar2r-max-pipe 8, use-aw2b-fill), bcm2712-rpi-5-b.dts (phy reg 1). 41 * [P1] drivers/net/phy/broadcom.c bcm54xx_config_clock_delay (RGMII-ID: AUXCTL misc shadow skew enable, shadow 0x03 42 * GTXCLK_EN), drivers/net/phy/bcm-phy-lib.c bcm54xx_auxctl_read/write, bcm_phy_read/write_shadow, 43 * include/linux/brcmphy.h shadow numbers, include/uapi/linux/mii.h BMCR/BMSR/ADVERTISE/CTRL1000/STAT1000 bits, 44 * drivers/net/phy/phy_device.c genphy_loopback (BMCR LOOPBACK + fixed speed/duplex, then wait BMSR link). 45 * [D1] RP1 peripherals datasheet RP-008370-DS-1 chapter 7: GEM_GXL (64-bit address DMA, internal loopback, RGMII), 46 * ETH_CFG STATUS (RGMII link/speed/duplex), CLKGEN (tx clock follows the MAC speed). 47 * [I1] IEEE 802.3 clause 22 (MII registers, BMCR loopback bit 14), IEEE 802 Local Experimental Ethertype 1 = 0x88b5. 48 * Not read: the Cadence GEM_GXL user guide and the BCM54213PE datasheet (not public). Anything only they would 49 * say (what exactly local loopback does at the RGMII pins) is UNVERIFIED and is measured by the Pi run. */ 50 #ifndef AIENOS_CK_PI5_GEM_H 51 #define AIENOS_CK_PI5_GEM_H 52 #include 53 #include 54 #include "pi5_net.h" 55 #include "dma_window.h" 56 57 enum { 58 CK_GEM_OK = 0, 59 CK_GEM_E_ARG = -1, 60 CK_GEM_E_HALTED = -2, /* latched earlier: no more DMA until reboot */ 61 CK_GEM_E_WINDOW = -3, /* the validator refused a descriptor or buffer (window latched, BME never enabled) */ 62 CK_GEM_E_HWWIN = -4, /* the root-complex window is not confirmed confined: refuse to enable bus-master */ 63 CK_GEM_E_BME = -5, /* bus-master enable/disable did not read back */ 64 CK_GEM_E_QUIESCE = -6, /* the transmitter did not stop (halted) */ 65 CK_GEM_E_DEVICE = -7, /* the device reported an impossible index or length (window latched) */ 66 CK_GEM_E_NOGEM = -8, /* MID says this is not a GEM */ 67 CK_GEM_E_TIMEOUT = -9, 68 CK_GEM_E_READBACK = -10 69 }; 70 71 /* ============ GEM registers: offset from the GEM base (CK_RP1_ETH in BAR1) [M1] ============ */ 72 #define CK_GEM_USRIO 0x00cu 73 #define CK_GEM_DMACFG 0x010u 74 #define CK_GEM_TSR 0x014u 75 #define CK_GEM_RBQP 0x018u 76 #define CK_GEM_TBQP 0x01cu 77 #define CK_GEM_RSR 0x020u 78 #define CK_GEM_ISR 0x024u 79 #define CK_GEM_IDR 0x02cu 80 #define CK_GEM_PBUFRXCUT 0x044u 81 #define CK_GEM_AMP 0x054u 82 #define CK_GEM_HRB 0x080u 83 #define CK_GEM_HRT 0x084u 84 #define CK_GEM_SA1B 0x088u 85 #define CK_GEM_SA1T 0x08cu 86 #define CK_GEM_SA2B 0x090u /* SA2B..SA4T = 0x90..0xa4 */ 87 #define CK_GEM_DCFG1 0x280u 88 #define CK_GEM_DCFG2 0x284u 89 #define CK_GEM_DCFG6 0x294u 90 #define CK_GEM_TBQPH 0x4c8u /* shared by every queue */ 91 #define CK_GEM_RBQPH 0x4d4u 92 /* queues 1.. (hw_q - 1 = n): GEM_ISR(n) 0x400, GEM_TBQP(n) 0x440, GEM_RBQP(n) 0x480, GEM_RBQS(n) 0x4a0, GEM_IDR(n) 0x620 */ 93 #define CK_GEM_QN_TBQP(hwq) (0x440u + (((hwq) - 1u) << 2)) 94 #define CK_GEM_QN_RBQP(hwq) (0x480u + (((hwq) - 1u) << 2)) 95 #define CK_GEM_QN_RBQS(hwq) (0x4a0u + (((hwq) - 1u) << 2)) 96 #define CK_GEM_QN_IDR(hwq) (0x620u + (((hwq) - 1u) << 2)) 97 #define CK_GEM_QN_ISR(hwq) (0x400u + (((hwq) - 1u) << 2)) 98 99 /* NCR (CK_GEM_NCR_MPE is in pi5_net.h) */ 100 #define CK_GEM_NCR_LLB (1u << 1) 101 #define CK_GEM_NCR_RE (1u << 2) 102 #define CK_GEM_NCR_TE (1u << 3) 103 #define CK_GEM_NCR_CLRSTAT (1u << 5) 104 #define CK_GEM_NCR_TSTART (1u << 9) 105 /* NCFGR (the MDC divider CK_GEM_NCFGR_CLK_* is in pi5_net.h) */ 106 #define CK_GEM_NCFGR_SPD (1u << 0) /* 100 Mbit when set (GBE clear) */ 107 #define CK_GEM_NCFGR_FD (1u << 1) 108 #define CK_GEM_NCFGR_CAF (1u << 4) 109 #define CK_GEM_NCFGR_NBC (1u << 5) 110 #define CK_GEM_NCFGR_GBE (1u << 10) 111 #define CK_GEM_NCFGR_DRFCS (1u << 17) 112 #define CK_GEM_NCFGR_DBW_SHIFT 21 113 #define CK_GEM_NCFGR_DBW_MASK (3u << 21) 114 /* DMACFG */ 115 #define CK_GEM_DMACFG_FBLDO_MASK 0x1fu 116 #define CK_GEM_DMACFG_ENDIA_DESC (1u << 6) 117 #define CK_GEM_DMACFG_ENDIA_PKT (1u << 7) 118 #define CK_GEM_DMACFG_RXBMS_MASK (3u << 8) 119 #define CK_GEM_DMACFG_TXPBMS (1u << 10) 120 #define CK_GEM_DMACFG_TXCOEN (1u << 11) 121 #define CK_GEM_DMACFG_RXBS_SHIFT 16 122 #define CK_GEM_DMACFG_RXBS_MASK (0xffu << 16) 123 #define CK_GEM_DMACFG_RXEXT (1u << 28) 124 #define CK_GEM_DMACFG_TXEXT (1u << 29) 125 #define CK_GEM_DMACFG_ADDR64 (1u << 30) 126 #define CK_GEM_BURST 16u /* macb dma_burst_length for raspberrypi_rp1_config */ 127 #define CK_GEM_RXBS_UNIT 64u /* RX_BUFFER_MULTIPLE */ 128 /* TSR / RSR / ISR */ 129 #define CK_GEM_TSR_UBR (1u << 0) 130 #define CK_GEM_TSR_RLE (1u << 2) 131 #define CK_GEM_TSR_TGO (1u << 3) 132 #define CK_GEM_TSR_BEX (1u << 4) 133 #define CK_GEM_TSR_COMP (1u << 5) 134 #define CK_GEM_TSR_UND (1u << 6) 135 #define CK_GEM_RSR_BNA (1u << 0) 136 #define CK_GEM_RSR_REC (1u << 1) 137 #define CK_GEM_RSR_OVR (1u << 2) 138 #define CK_GEM_ISR_HRESP (1u << 11) 139 /* USRIO */ 140 #define CK_GEM_USRIO_RGMII (1u << 0) 141 /* AMP */ 142 #define CK_GEM_AMP_AR2R_MASK 0xffu 143 #define CK_GEM_AMP_AW2W_SHIFT 8 144 #define CK_GEM_AMP_AW2W_MASK (0xffu << 8) 145 #define CK_GEM_AMP_AW2B_FILL (1u << 16) 146 #define CK_GEM_AXI_PIPE 8u /* rp1.dtsi cdns,aw2w-max-pipe = <8>, cdns,ar2r-max-pipe = <8> */ 147 /* DCFG */ 148 #define CK_GEM_DCFG1_DBWDEF_SHIFT 25 149 #define CK_GEM_DCFG6_DAW64 (1u << 23) 150 #define CK_GEM_MID_IDNUM_SHIFT 16 151 152 /* Descriptors [M1]: word0 = address (RX: bits 31:2, bit1 WRAP, bit0 USED), word1 = ctrl, then (64-bit address 153 * mode) word2 = address high, word3 reserved. */ 154 #define CK_GEMD_RX_USED (1u << 0) 155 #define CK_GEMD_RX_WRAP (1u << 1) 156 #define CK_GEMD_RX_FRMLEN_MASK 0xfffu 157 #define CK_GEMD_RX_SOF (1u << 14) 158 #define CK_GEMD_RX_EOF (1u << 15) 159 #define CK_GEMD_TX_FRMLEN_MASK 0x3fffu /* GEM_TX_FRMLEN_SIZE 14 */ 160 #define CK_GEMD_TX_LAST (1u << 15) 161 #define CK_GEMD_TX_NOCRC (1u << 16) 162 #define CK_GEMD_TX_BUF_EXHAUSTED (1u << 27) 163 #define CK_GEMD_TX_UNDERRUN (1u << 28) 164 #define CK_GEMD_TX_ERROR (1u << 29) 165 #define CK_GEMD_TX_WRAP (1u << 30) 166 #define CK_GEMD_TX_USED (1u << 31) 167 168 #define CK_GEM_MAX_QUEUES 8u 169 #define CK_GEM_TXRING 2u /* slot 0 = the frame, slot 1 = a USED|WRAP sentinel */ 170 #define CK_GEM_RXRING 4u /* power of two for the ring helper; unused slots are USED sentinels */ 171 #define CK_GEM_RXCOPY 256u 172 173 /* What the hardware says it is (read from the registers, never assumed). */ 174 struct ck_gem_caps { 175 uint32_t mid, dcfg1, dcfg2, dcfg6; 176 int is_gem; /* MID IDNUM >= 2 (hw_is_gem) */ 177 int daw64; /* DCFG6 bit 23 */ 178 uint32_t queue_mask; /* bit q set = hardware queue q exists; bit 0 always */ 179 uint32_t nqueues; 180 uint32_t desc_bytes; /* 16 with 64-bit descriptors, else 8 */ 181 uint32_t dbw; /* NCFGR DBW field value (macb_dbw) */ 182 }; 183 int ck_gem_probe(const struct ck_rp1_io *io, struct ck_gem_caps *c); 184 185 /* The driver context. All memory the device may touch is the bounce window `w`, mapped at `va`. */ 186 struct ck_gem { 187 const struct ck_rp1_io *rio; 188 struct ck_gem_caps caps; 189 struct ck_dmaw *w; /* the ADR 0020 validator and allocator */ 190 volatile uint8_t *va; /* CPU mapping of the window; bus address w->base */ 191 int (*bme)(void *ctx, int on); /* RP1 PCI bus-master enable: 0 = done AND read back */ 192 void *bme_ctx; 193 void (*barrier)(void); /* data barrier between descriptor stores and the register kick */ 194 int hw_window_ok; /* the root-complex window read back CONFINED (cut 2 verdict); else no bus-master */ 195 int halted; /* latched until reboot */ 196 int bme_on; 197 uint32_t xfers; 198 }; 199 void ck_gem_init(struct ck_gem *g, const struct ck_rp1_io *rio, struct ck_dmaw *w, volatile uint8_t *va, 200 int (*bme)(void *, int), void *bme_ctx, void (*barrier)(void), int hw_window_ok); 201 202 enum { CK_GEMX_LLB = 1, CK_GEMX_PHYLB = 2, CK_GEMX_WIRE = 3 }; 203 204 struct ck_gem_xfer { 205 /* in */ 206 int mode; /* LLB: NCR.LLB; PHYLB/WIRE: GEM normal (the PHY loops or the wire does) */ 207 uint32_t speed; /* 1000, 100 or 10 */ 208 int full_duplex; 209 uint8_t mac[6]; 210 const uint8_t *frame; /* sent as given (the MAC appends the FCS) */ 211 uint32_t frame_len; /* 60..1514 */ 212 uint32_t rx_count; /* real RX buffers, 0..3 (0 = TX only); the rest are USED sentinels */ 213 uint32_t rx_buf_len; /* multiple of 64, 64..1536 */ 214 uint32_t tx_wait_us, rx_wait_us; 215 uint64_t tx_addr, rx_addr; /* 0 = allocate inside the window; non-zero = caller-chosen, STILL validated */ 216 /* Confinement test ONLY (ADR 0020 test 5). After the validated slot is written, the hardware-visible address 217 * word of TX slot 0 / RX slot 0 is replaced by this raw address. The shadow still holds the valid one. 218 * Callers must have proven the target is a page they own and that a write there is harmless. */ 219 uint64_t raw_tx_outside, raw_rx_outside; 220 /* out */ 221 int tx_done, rx_done; 222 uint32_t tx_ctrl, rx_word0, rx_ctrl, rx_len; 223 uint32_t tsr, rsr, isr, ncr_run, ncfgr_run, dmacfg_run, ncr_end, tsr_end; 224 uint64_t tx_bus, rx_bus; /* where the buffers were */ 225 uint8_t rx_data[CK_GEM_RXCOPY]; 226 uint32_t rx_copied; 227 uint32_t rx_frames; /* RX slots the device handed back (USED, SOF and EOF) */ 228 uint32_t rx_slot; /* the first such slot; rx_data/rx_len/rx_ctrl/rx_word0 are its */ 229 int bme_enabled_for_us; /* 1 once bus-master was turned on for this transfer */ 230 int bme_off_at_end; 231 }; 232 /* One transfer: validate everything, THEN program the GEM, THEN enable RP1 bus-master, kick, poll, 233 * quiesce (RE/TE off, wait TGO clear, 2 ms), THEN disable bus-master and read the results. 0 = the procedure 234 * ran and the device is stopped (tx_done/rx_done say what happened); negative = refused or failed. */ 235 int ck_gem_xfer(struct ck_gem *g, struct ck_gem_xfer *x); 236 /* Stop the MAC (RE/TE/LLB off, MPE kept) with bus-master already off; for the end of a run. */ 237 void ck_gem_park(struct ck_gem *g); 238 239 /* ============ frames ============ */ 240 enum { CK_GEMF_RAW = 1, CK_GEMF_ARP = 2 }; 241 #define CK_GEMF_ETHERTYPE_EXP 0x88b5u 242 /* A 60 byte frame (MAC pads nothing, appends FCS). Raw: broadcast, EtherType 0x88b5, magic, sequence number, 243 * pattern. ARP: broadcast ARP probe (sender IP 0.0.0.0) for tpa, trailer carries magic + sequence number. */ 244 uint32_t ck_gem_frame_build(int kind, const uint8_t mac[6], uint32_t seq, const uint8_t tpa[4], uint8_t *out, uint32_t cap); 245 /* 0 = identical; else 1 + index of the first difference (a length difference counts at min(len)). */ 246 uint32_t ck_gem_frame_diff(const uint8_t *a, uint32_t alen, const uint8_t *b, uint32_t blen); 247 248 /* ============ PHY (clause 22) ============ */ 249 int ck_gem_mdio_write(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t val); 250 uint32_t ck_gem_mdio_frame_write(uint32_t phy, uint32_t reg, uint16_t val); 251 #define CK_MII_BMCR 0u 252 #define CK_MII_BMSR 1u 253 #define CK_MII_ADVERTISE 4u 254 #define CK_MII_LPA 5u 255 #define CK_MII_CTRL1000 9u 256 #define CK_MII_STAT1000 10u 257 #define CK_MII_BMCR_SPEED1000 0x0040u 258 #define CK_MII_BMCR_FULLDPLX 0x0100u 259 #define CK_MII_BMCR_ANRESTART 0x0200u 260 #define CK_MII_BMCR_ANENABLE 0x1000u 261 #define CK_MII_BMCR_SPEED100 0x2000u 262 #define CK_MII_BMCR_LOOPBACK 0x4000u 263 #define CK_MII_BMSR_LSTATUS 0x0004u 264 #define CK_MII_BMSR_ANEGCOMPLETE 0x0020u 265 266 struct ck_phy_link { 267 uint16_t bmcr, bmsr_first, bmsr, adv, lpa, ctrl1000, stat1000; 268 int link; /* BMSR link status (read twice: the bit is latched low) */ 269 int aneg_done; 270 uint32_t speed; /* resolved 1000/100/10; 0 = unresolved */ 271 int full_duplex; 272 }; 273 /* Read the registers and resolve speed/duplex from the advertisement and partner ability (IEEE 802.3 clause 28/40; 274 * the same resolution as Linux genphy_read_status). Does not write. */ 275 int ck_phy_link_read(const struct ck_rp1_io *io, uint32_t phy, struct ck_phy_link *l); 276 /* ETH_CFG STATUS decode [D1]: bit0 link, bits2:1 speed (0=10,1=100,2=1000), bit3 duplex. speed 0 if reserved. */ 277 void ck_eth_cfg_status_decode(uint32_t st, int *link, uint32_t *speed, int *full_duplex); 278 /* Linux bcm54xx_config_clock_delay for RGMII-ID [P1]. before/after = the two registers read before the write. */ 279 int ck_phy_rgmii_id_config(const struct ck_rp1_io *io, uint32_t phy, uint16_t *auxctl_before, uint16_t *clk_before); 280 /* genphy_loopback [P1]: BMCR = LOOPBACK | duplex | speed, then wait (bounded) for BMSR link. *bmcr_saved = BMCR found. */ 281 int ck_phy_loopback_enter(const struct ck_rp1_io *io, uint32_t phy, uint32_t speed, uint16_t *bmcr_saved, 282 uint16_t *bmsr_out); 283 /* Leave loopback: BMCR = saved (autoneg restarted if it was enabled). */ 284 int ck_phy_loopback_exit(const struct ck_rp1_io *io, uint32_t phy, uint16_t bmcr_saved); 285 286 /* RP1 PCI bus-master through the root complex config window (needs a read back). */ 287 int ck_pi5gem_bme_set(const struct ck_pi5pcie_io *io, int on); 288 289 #ifdef CK_PI5_NET 290 /* Image glue called by ck_pi5net_run after the PHY id is read. */ 291 struct ck_pi5gem_ctx { 292 const struct ck_pi5pcie_io *rc; 293 const struct ck_rp1_io *rio; 294 struct ck_dmaw *dw; 295 void *bounce; 296 uint64_t bounce_phys; 297 int hw_window_confined; 298 uint32_t phy_addr; 299 uint32_t pclk_hz; 300 }; 301 void ck_pi5gem_run(const struct ck_pi5gem_ctx *c); 302 #endif 303 304 #endif