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/patches/293705509f028f4c # 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 ## GitHub aien-dev/aienux#371 (code only) closed derived `293705509f028f4c` #371 Imported from GitHub aien-dev/aienux#371 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `15c19d499c8ce9ed99cdb5584f30b662437cae8e` · Head `1230c7446a14b1638ecc78bce8e618e98c7dbf8f` ### Apply it ``` git checkout -b patch-293705509f028f4c 15c19d499c8ce9ed99cdb5584f30b662437cae8e curl -fsSL https://interplane.io/drakestapleton/aienux/patches/293705509f028f4c.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aienux 371 1230c7446a14b1638ecc78bce8e618e98c7dbf8f ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":371,"url":"https://github.com/aien-dev/aienux/pull/371","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:17Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index 9e508ae4..337a6b64 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "25020cdb700e0f5b821988ff7a3c5a7e5bd9e9019ae4d26a79ce1367e4ebfce1", - "last_commit": "0fab4bc", + "digest": "56d07fe5889c5254e4734f3c06056d6fdaf75e06c057952b0c4d7f076246ae43", + "last_commit": "23c8bcd", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "aaf01af1c67ace2137a6c3343225682262b3208d7b98dedac1410bc18e642c36", - "last_commit": "53de55c", + "digest": "e2a3164c1405eca5c2ce65baa8d0acae588e8d3c3f00f788af0a4f90746f593a", + "last_commit": "cb3bba7", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "95951d5a52b7ade0f66bb972e92ebfd3fb14433557f1ee827bef14d6481a17a7", - "last_commit": "53de55c", + "digest": "48c4fa69e56fcade3e5fa0d959501d89269856026f10eec71fe9818e6ca9a9de", + "last_commit": "cb3bba7", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:481462d0ba8e29d9f1456b9d42606f404e3c505e9c32e02ea072962e8bbb0d6b", - "compiled_at": "2026-10-13T23:07:22Z", + "children_root": "sha256:8c6cbe9eacc90d8b46a492c891dfc785e66687431d9b0cf96df3bb57b57a16c3", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:59de48de86c5964b2d8db84d8be0df96f5de3547d6dd74f81d46d5860d0257ba", + "digest": "sha256:45e06b1a1efe485ac6ee5ea982e629abd36109e5f86e14c731f832f87f921cf9", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3197, - "generated_at_commit": "53de55c5dd3485948cd34734caf0f646e28a68c4", - "source_tree": "sha256:3510409465a633f52564105bf22f73acb73ac5ce9098a449d254275dcd1eacab" + "files": 3201, + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:851da4029d8b603841af4f012f932469e021ba857aa3a75ac7ceb48c95e03b67" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 0b1e29b2..5da483f6 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:c5d7ca9ce0145309c90a439711adcb6dd8e8e40ff48b90455f545e63c05f04c8", - "compiled_at": "2026-10-13T22:05:34Z", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:25020cdb700e0f5b821988ff7a3c5a7e5bd9e9019ae4d26a79ce1367e4ebfce1", + "digest": "sha256:56d07fe5889c5254e4734f3c06056d6fdaf75e06c057952b0c4d7f076246ae43", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 80, - "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", - "source_tree": "sha256:6b9981604fb64759f58b202f3905e6cb1ec0a3c4cdea416ffe8a9b1fa34d923c" + "files": 81, + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:c22db8af13a56be027321240ae6f619a10aec867e1b0df5516b3243aeb5dfbaf" }, "owner_lane": "", "provenance": { diff --git a/docs/native/PI5-NET-3-RUNS.md b/docs/native/PI5-NET-3-RUNS.md new file mode 100644 index 00000000..0b9dd68f --- /dev/null +++ b/docs/native/PI5-NET-3-RUNS.md @@ -0,0 +1,99 @@ +# PI5-NET-3 runs: one Ethernet frame through the RP1 GEM, descriptors inside the bounce window only + +Image: `make pi5-net` (`CK_PI5_NET=1`). Code: `native/kernel/dev/pi5_gem.c`, `pi5_gem.h` (library plus image glue), called from `pi5_net.c`. Host tests: `tests/test_pi5_gem.c` (fake GEM that flags any DMA outside the window, DMA without bus-master, bus-master dropped while the transmitter runs, malformed rings; 281 checks, 23 mutants, all killed by `make pi5-gem-mutants`). + +Register sources (cited in the code comments): Linux `macb.h` and `macb_main.c` [M1, M2], Linux `broadcom.c`, `bcm-phy-lib.c`, `mii.h`, `phy_device.c` [P1], RP1 datasheet ethernet chapter [D1], IEEE 802.3 clause 22 [I1]. + +## Run pi5-net-3-r1 (real Raspberry Pi 5) + +Evidence file: `~/workspace/pi5-runs/pi5-net-3-r1/kernel.log` (image sha256 7693ccde4ca62422dcd60e50efbe515d3992ad052baa36f32cccb54b1bb9eaf5). `\EFI\AIENOS\` had no MODEL.MAP. KLOG=1, tryboot soft reset. Exact lines (all `pi5_` lines of the log): + +``` +pi5_watchdog: armed 15 s (PM block 0x107d200000), petted at every stage +pi5_sd: host 0x1000fff000 version=0x1002 caps=0x15eac832 caps_base_mhz=200 using 200 MHz (clk_emmc2) +pi5_sd: card ready spec=2 card_hz=25000000 high_capacity=1 blocks=250347520 (122240 MiB) rca=0x59b4 bus=4bit +pi5_sd: read sector 0 rc=0 signature=55aa (MBR ok) +pi5_part: AIENOS partition first_lba=1050624 blocks=8388608 (4096 MiB) entries_used=3 index=1 via GPT (CRC32 primary+backup ok) +pi5_store_view: lba 0..8388543 (1048568 units) for the Store; marker reserved at 8388544..8388607 +pi5_store_layer: reopened (payload identical to the pattern, generation bumped and read back) generation=22 region=64 sectors at partition block 8388544 +pi5_net: cut 3: hardware DMA confinement, RP1 Ethernet up to the PHY, then one frame through the GEM (descriptors inside the bounce window only) +pi5_net: software-only DMA confinement (Pi 5, weaker than the Spark) plus the root-complex window below +pi5_net: root 00:00.0 14e4:2712 class=060400 sec=1 sub=1 +pi5_net: rp1 01:00.0 1de4:0001 MATCH cmd=0x0006 +pi5_dma: bounce cpu=0x4400000 size=0x200000 (ADR 0020 rule 1) +pi5_net: inbound before win0 bus=0x0 size=0x1000000000 cpu=0x0 remap=1 valid=1 +pi5_dma: hw_window before=NOT_CONFINED [NOT_CONFINED] reach=0x1000000000 inside=0x200000 outside=0xfffe00000 +pi5_dma: plan bus=0x4400000 cpu=0x4400000 size=0x200000 code=6 extra_beyond_bounce=0x0 +pi5_dma: rp1 bus-master off for the window change: rc=0 cmd_was=0x0006 cmd_now=0x00100002 +pi5_dma: apply rc=0 (0 = window now matches the plan); other_windows_disabled=0 +pi5_net: inbound after win0 bus=0x4400000 size=0x200000 cpu=0x4400000 remap=1 valid=1 +pi5_dma: hw_window after=CONFINED [CONFINED] reach=0x200000 inside=0x200000 outside=0x0 +pi5_dma: rp1 bus-master is LEFT OFF (cmd=0x00100002); the GEM driver turns it on when it needs it +pi5_net: outbound win0 bus=0xc0000000 cpu=0x1f00000000 size=0x40000000 +pi5_net: rp1 bar1 bus=0xc0000000 -> cpu=0x1f00000000 chip_id=0x20001927 platform=0x00000002 +pi5_net: gem before: MID=0x00070109 NCR=0x00000000 NCFGR=0x00540000 NSR=0x00000006 eth_cfg CONTROL=0x00000000 CLKGEN=0x00000080 +pi5_net: rp1 clock regs: PLL cs=0x80000001 fbint=0x00000014 frac=0x00000000 prim=0x00051010 sec=0x80000800 | SYS ctrl=0x00000002 div=1 sel=0x4 | ETH ctrl=0x10000800 div=1 sel=0x1 | TSU ctrl=0x10000800 div=1 sel=0x1 +pi5_net: rp1 clocks before: pll_core=1000000000 pll_sec=125000000 clk_sys=200000000 clk_eth=125000000 clk_eth_tsu=50000000 Hz (0 = off/unknown) +pi5_net: clk_eth: ALREADY_ON (already enabled: left as found) | clk_eth_tsu: ALREADY_ON (already enabled: left as found) +pi5_net: rp1 clocks after: pll_core=1000000000 pll_sec=125000000 clk_sys=200000000 clk_eth=125000000 clk_eth_tsu=50000000 Hz (0 = off/unknown) +pi5_net: clock apply rc=0 +pi5_net: phy reset gpio32 rc=0 ctrl 0000009f->00000085 pad 000000d6->00000056 oe_before=00000000 out_before=00000000 in: before=00000000 low=00000000 high=00000012 +pi5_net: mdio enable rc=0 NCR=0x00000010 NCFGR=0x00540000 (MDC divider code 5 for pclk 200000000 Hz) +pi5_net: phy addr=1 id=0x600d84a2 oui=18-c0-86 model=0x0a rev=2 class=BCM54213PE attempts=1 +pi5_net: phy found; handing over to the GEM frame test (hw window confined=1) +pi5_gem: cut 3: one frame through the RP1 GEM with descriptors only inside the bounce window; software-only DMA confinement (Pi 5, weaker than the Spark) +pi5_gem: caps rc=0 MID=0x00070109 DCFG1=0x08508513 DCFG2=0x6eb13fff DCFG6=0x08820000 gem=1 daw64=1 queue_mask=0x1 queues=1 desc_bytes=16 dbw_code=2 +pi5_gem: state before: NCR=0x00000010 NCFGR=0x00540000 USRIO=0x00000000 DMACFG=0x00020704 AMP=0x00000101 TBQP=0x00000000 RBQP=0x00000000 eth_cfg STATUS=0x00000000 CLKGEN=0x00000080 rp1 pci cmd=0x00100002 +pi5_gem: link (first read, read-only): BMCR=0x1140 BMSR=0x7949/0x7949 ADV=0x01e1 LPA=0x0000 CTRL1000=0x0200 STAT1000=0x0000 link=0 aneg_done=0 speed=0 duplex=half | eth_cfg STATUS=0x00000000 link=0 speed=10 duplex=half +pi5_gem: link UP: BMCR=0x1140 BMSR=0x7969/0x796d ADV=0x01e1 LPA=0xcde1 CTRL1000=0x0200 STAT1000=0x0800 link=1 aneg_done=1 speed=1000 duplex=full | eth_cfg STATUS=0x00000000 link=0 speed=10 duplex=half +pi5_gem: LINK FOUND +pi5_gem: phy rgmii-id delays rc=0 AUXCTL_MISC before=0x71e7 after=0x71e7 CLK_CTL before=0x200 after=0x200 +pi5_gem: loopback gem-local(NCR.LLB) seq=1 rc=0 tx_done=1 rx_done=1 rx_frames=1 tx_ctrl=0x8000803c rx_word0=0x04400181 rx_ctrl=0x8000c03c rx_len=60 frame_diff=0 +pi5_gem: loopback gem-local(NCR.LLB) seq=1 regs: tsr=0x00000021 rsr=0x00000002 isr=0x00000000 ncr_run=0x0000001e ncfgr=0x00560402 dmacfg=0x40180710 ncr_end=0x00000010 tsr_end=0x00000000 bme_on_for_us=1 bme_off_at_end=1 tx_bus=0x4400140 rx_bus=0x4400180 +pi5_gem: LOOPBACK gem-local PASS (sent seq=1 60 bytes, received 60 bytes, byte compare identical) +pi5_gem: phy loopback enter rc=0 BMCR_was=0x1140 BMSR_now=0x794d (link in loopback=1) +pi5_gem: loopback phy(BMCR.LOOPBACK) seq=2 rc=0 tx_done=1 rx_done=1 rx_frames=1 tx_ctrl=0x8000803c rx_word0=0x04400181 rx_ctrl=0x8000c03c rx_len=60 frame_diff=0 +pi5_gem: loopback phy(BMCR.LOOPBACK) seq=2 regs: tsr=0x00000021 rsr=0x00000002 isr=0x00000000 ncr_run=0x0000001c ncfgr=0x00560402 dmacfg=0x40180710 ncr_end=0x00000010 tsr_end=0x00000000 bme_on_for_us=1 bme_off_at_end=1 tx_bus=0x4400140 rx_bus=0x4400180 +pi5_gem: LOOPBACK phy PASS (sent seq=2 60 bytes, received 60 bytes, byte compare identical) +pi5_gem: phy loopback exit rc=0 BMCR restored to 0x1140 +pi5_gem: link after loopback (UP): BMCR=0x1140 BMSR=0x7969/0x796d ADV=0x01e1 LPA=0xcde1 CTRL1000=0x0200 STAT1000=0x3800 link=1 aneg_done=1 speed=1000 duplex=full | eth_cfg STATUS=0x00000000 link=0 speed=10 duplex=half +pi5_gem: wire arp-probe 192.168.1.1 seq=3 rc=0 tx_done=1 rx_done=0 rx_frames=0 tx_ctrl=0x8000803c rx_word0=0x00000000 rx_ctrl=0x00000000 rx_len=0 frame_diff=4294967295 +pi5_gem: wire arp-probe 192.168.1.1 seq=3 regs: tsr=0x00000021 rsr=0x00000000 isr=0x00000000 ncr_run=0x0000001c ncfgr=0x00560402 dmacfg=0x40180710 ncr_end=0x00000010 tsr_end=0x00000000 bme_on_for_us=1 bme_off_at_end=1 tx_bus=0x4400140 rx_bus=0x4400180 +pi5_gem: WIRE tx PASS (ARP probe 60 bytes at 1000 Mbit full duplex, TX complete=1, tx_ctrl error bits=0x00000000) +pi5_gem: WIRE rx (bonus) none within 400 ms +pi5_gem: confinement setup: canary page cpu=0x4600000 (outside window 0x4400000..+0x200000); validator on a scratch copy of the window says 2 (2 = E_RANGE, refused) for a 64 byte buffer there +pi5_gem: CONFINEMENT A: the RX descriptor points at 0x4600000 outside the window, buffer size 64 bytes (the device can write at most 64 bytes there) +pi5_gem: confinement A (rx write outside) seq=4 rc=0 tx_done=1 rx_done=1 rx_frames=1 tx_ctrl=0x8000803c rx_word0=0x04600001 rx_ctrl=0x8000c03c rx_len=60 frame_diff=1 +pi5_gem: confinement A (rx write outside) seq=4 regs: tsr=0x00000021 rsr=0x00000003 isr=0x00000000 ncr_run=0x0000001e ncfgr=0x00560402 dmacfg=0x40010710 ncr_end=0x00000010 tsr_end=0x00000000 bme_on_for_us=1 bme_off_at_end=1 tx_bus=0x4400140 rx_bus=0x4400180 +pi5_gem: CONFINEMENT A result: canary bytes changed=0 first_changed_offset=-1 device_says_rx_done=1 hresp=0 +pi5_gem: CONFINEMENT A BLOCKED: the write did not reach the outside page +pi5_gem: CONFINEMENT B: the TX descriptor points at 0x4601000 outside the window, which holds a different frame (marker seq=0xdead0005) +pi5_gem: confinement B (tx read outside) seq=5 rc=0 tx_done=1 rx_done=0 rx_frames=0 tx_ctrl=0x8000803c rx_word0=0x00000000 rx_ctrl=0x00000000 rx_len=0 frame_diff=4294967295 +pi5_gem: confinement B (tx read outside) seq=5 regs: tsr=0x00000021 rsr=0x00000000 isr=0x00000000 ncr_run=0x0000001e ncfgr=0x00560402 dmacfg=0x40180710 ncr_end=0x00000010 tsr_end=0x00000000 bme_on_for_us=1 bme_off_at_end=1 tx_bus=0x4400140 rx_bus=0x4400180 +pi5_gem: CONFINEMENT B result: rx_done=0 diff_vs_outside_marker=4294967295 diff_vs_inside_frame=4294967295 tsr_bex=0 hresp=0 +pi5_gem: CONFINEMENT B BLOCKED: the outside page was not delivered as the frame +pi5_gem: done: GEM RE/TE/LLB off, halted=0 bme_on=0 rp1 pci cmd=0x00100002 NCR=0x00000010 +pi5_net: done: rp1 bus-master off unless the line above says otherwise +``` + +## What the run shows + +- Link: FOUND. The PHY (BCM54213PE at addr 1) reports link up, autoneg done, 1000 Mbit full duplex, partner LPA 0xcde1. A cable and a partner exist. +- Loopback, GEM local (NCR.LLB): PASS. Frame seq=1, 60 bytes sent, 60 received, byte compare identical. +- Loopback, PHY (BMCR bit 14): PASS. Frame seq=2, same result. The PHY was put back to its original BMCR afterwards. +- Wire: TX PASS. One ARP probe (60 bytes, sender IP 0.0.0.0, target 192.168.1.1) at 1000 Mbit full duplex, TX complete, no error bits. RX of an incoming frame: none within 400 ms (bonus, not required). +- Every descriptor went through the ADR 0020 validator before bus-master was turned on. Bus-master was on only during each transfer and was off again at the end of each (`bme_off_at_end=1`, final `rp1 pci cmd=0x00100002`). The GEM was left with RE/TE/LLB off. +- Confinement A (device WRITE outside the window): BLOCKED by the readback. The RX descriptor pointed at a private page at cpu 0x4600000 (outside the window), buffer 64 bytes. The canary page (filled with 0xa5) had 0 changed bytes. Note: the GEM itself reported the frame as received (rx_word0 used bit set, rx_len=60), and the in-window buffer stayed empty, so the device believed it wrote; the root complex dropped the write. No fault, no hang, `hresp=0`. +- Confinement B (device READ outside the window): the frame at the outside page (marker seq=0xdead0005) was not delivered (rx_done=0). The TX descriptor was consumed (TX complete, no error bits). This shows the marker frame did not come back, which is consistent with a blocked read. + +## UNVERIFIED + +- Confinement B is inferred from "marker frame not received". The run does not prove what the read returned (zeros, all ones, or an error response were not captured), so it does not prove the device never saw our bytes, only that they were not delivered as the frame. A wire-side capture would settle it. +- Confinement A and B each ran once on one board. +- What GEM NCR.LLB does at the RGMII pins was not measured; the result only shows the frame returned. +- Whether the TX frame in the wire test reached the partner (no capture on the partner side). TX complete only means the MAC finished sending. +- RX of an incoming frame from the network: not seen. +- The local MAC 02:41:49:45:4e:03 is a locally administered address, not the board's OTP MAC (not read). +- ETH_CFG STATUS read 0x00000000 (link=0) while the PHY reported link up. The register was not interpreted further; its decode is unverified on this board. +- RGMII-ID delay config is copied from Linux; before and after read back identical (AUXCTL_MISC 0x71e7, CLK_CTL 0x200), so the PHY already had the delays set and the write was not shown to change anything. The data path worked in loopback and on the wire at TX only. +- No interrupts or MSI used; polling only. diff --git a/native/.crumb b/native/.crumb index 93da5c03..ef1f3117 100644 --- a/native/.crumb +++ b/native/.crumb @@ -56,7 +56,7 @@ }, { "digest": "441bb7a06c78208da659e10e8ff3c50998f4aac3fe8446bcc6259521d040b466", - "last_commit": "53de55c", + "last_commit": "15c19d4", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "e105bc4fbeb01dd053f104b85e784ba7f62cd52bd5574bcbd50bc3ddc150cf01", - "last_commit": "53de55c", + "digest": "a055397a8db7d75eb8eeeb0e1e105b53ddb0e93ba13f9d0bb265749377bfae0b", + "last_commit": "cb3bba7", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:99e5fdfd7c1323ebb984593edfe0c9683d3cf7a79192c1d0f95acb6c25558886", - "compiled_at": "2026-10-13T23:07:22Z", + "children_root": "sha256:abb35e3d9ebfb14eba10be5460456dc15283465b14f2d7adf736c36517af9e26", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:aaf01af1c67ace2137a6c3343225682262b3208d7b98dedac1410bc18e642c36", + "digest": "sha256:e2a3164c1405eca5c2ce65baa8d0acae588e8d3c3f00f788af0a4f90746f593a", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 553, - "generated_at_commit": "53de55c5dd3485948cd34734caf0f646e28a68c4", - "source_tree": "sha256:7f21c1caa14ee51248375281b89ed8737eb398d28e758a0c66cd170013837d8a" + "files": 556, + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:b0c6458b356dbabaf48eb5c4b5aa7ea7d3e54569bf19a18f99b0356714598001" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index ec804c9b..f6faa607 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -58,13 +58,13 @@ }, { "digest": "4838185961eedd4d2053c8d8a6804065d1f8f450461ce74a819d5f7d4d8988f1", - "last_commit": "53de55c", + "last_commit": "15c19d4", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "9302f530853614d0541c387ba09fca4364bc195eef335a24bbe205dfd69f1ecd", - "last_commit": "0fab4bc", + "digest": "ebdd66c0966fba664f36dc167836b98d26ffae97f9502b4409e9c22da687b473", + "last_commit": "cb3bba7", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, @@ -82,13 +82,13 @@ }, { "digest": "2a98333d826d94a7131119aaeb5a15d8bc543baa5531fa930e6f0afc1fccb83d", - "last_commit": "53de55c", + "last_commit": "15c19d4", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "b6bc9b845fa67108c0bdac73dc640d2a634f8606e833703096f3746df1c23257", - "last_commit": "53de55c", + "digest": "e3169d29c0846819ae597b2674f496724638f1456c3c36d7f72d736240211262", + "last_commit": "cb3bba7", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:5b2f348bd7f112ca485a960f4de36a0ff131efa37d62877f42853fe129d31473", - "compiled_at": "2026-10-13T23:07:22Z", + "children_root": "sha256:4ec0cd6a5814361000abd7e7b1e07514febd2aec9df33e53b30edf786a04b7f3", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:e105bc4fbeb01dd053f104b85e784ba7f62cd52bd5574bcbd50bc3ddc150cf01", + "digest": "sha256:a055397a8db7d75eb8eeeb0e1e105b53ddb0e93ba13f9d0bb265749377bfae0b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 373, - "generated_at_commit": "53de55c5dd3485948cd34734caf0f646e28a68c4", - "source_tree": "sha256:47a343bea2cb8478dbdbc4e04cd67c4665daba3acb8e12998148f3db92c45197" + "files": 376, + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:7f1752f64e6c6c1c4f4f12c0c02af9f1759715949356149197250479556d0065" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 2ff6a7f4..87f0f62f 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -829,7 +829,7 @@ ifeq ($(strip $(CK_PI5_PCIE)),1) CORE_C += $(KDIR)/dev/pi5_pcie.c $(KDIR)/dev/dma_window.c endif ifeq ($(strip $(CK_PI5_NET)),1) -CORE_C += $(KDIR)/dev/pi5_pcie.c $(KDIR)/dev/dma_window.c $(KDIR)/dev/pi5_net.c +CORE_C += $(KDIR)/dev/pi5_pcie.c $(KDIR)/dev/dma_window.c $(KDIR)/dev/pi5_net.c $(KDIR)/dev/pi5_gem.c endif ifneq ($(strip $(CK_PI5_UNLOCK_CARD)),) CORE_C += $(BOOT)/efi_unlock.c $(KDIR)/core/unlock_req.c @@ -1269,7 +1269,7 @@ HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/ $(KDIR)/core/line_rx.c $(KDIR)/core/klog.c $(KDIR)/core/klog_slot.c $(KDIR)/core/cpufeat.c $(KDIR)/core/unlock_req.c $(KDIR)/core/agent_cmd.c TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_osc_unit test_osc_launch test_osc_admit test_entropy test_handoff test_fbcon test_osh_op \ test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ - test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx test_klog test_klog_slot test_pi5 test_pi5_marker test_pi5_rng test_cpufeat test_dma_window test_pi5_pcie test_pi5_net test_unlock_req test_context test_agent_cmd + test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx test_klog test_klog_slot test_pi5 test_pi5_marker test_pi5_rng test_cpufeat test_dma_window test_pi5_pcie test_pi5_net test_pi5_gem test_unlock_req test_context test_agent_cmd HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer @@ -2199,3 +2199,24 @@ pi5-net-mutants: if $(OUT)/host/test_pi5_net_mut_$$m >/dev/null; then echo "MUTANT SURVIVED: $$m"; fail=1; else echo "mutant killed: $$m"; fi; \ done; test $$fail -eq 0 .PHONY: test-pi5-net pi5-net-mutants pi5-net + +# AIENOS-PI5 network cut 3 (GEM descriptors, transfer order, bus-master window, PHY loopback) over a fake GEM that checks every DMA it makes. +PI5GEM_SRCS := $(KDIR)/dev/pi5_gem.c $(KDIR)/dev/pi5_net.c $(KDIR)/dev/pi5_pcie.c $(KDIR)/dev/dma_window.c +PI5GEM_DEPS := $(KDIR)/tests/test_pi5_gem.c $(PI5GEM_SRCS) $(KDIR)/tests/ck_test.h $(KDIR)/dev/pi5_gem.h $(KDIR)/dev/pi5_net.h $(KDIR)/dev/pi5_pcie.h $(KDIR)/dev/dma_window.h +$(OUT)/host/test_pi5_gem: $(PI5GEM_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) -I$(KDIR)/dev -o $@ $< $(PI5GEM_SRCS) +$(OUT)/host-san/test_pi5_gem: $(PI5GEM_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) -I$(KDIR)/dev -o $@ $< $(PI5GEM_SRCS) +test-pi5-gem: $(OUT)/host/test_pi5_gem + $< +# Each mutant removes one decision; the test must FAIL for every one. +PI5GEM_MUTANTS := NO_VALIDATE BME_EARLY BME_LEFT_ON NO_QUIESCE_WAIT NO_RETE_OFF NO_BARRIER NO_TIEOFF NO_RXLEN_CHECK NO_TX_LAST NO_SENTINEL_USED NO_ADDR64 NO_HWWIN NOT_STICKY NO_FRAMECAP NO_SEQ NO_DIFF MDIO_OP PHYLB_BIT NO_WRAP NO_GBE NO_DRFCS NO_BME_VERIFY NO_LLB_CLEAR +pi5-gem-mutants: + @mkdir -p $(OUT)/host + @fail=0; for m in $(PI5GEM_MUTANTS); do \ + $(HOSTCC) $(HCFLAGS) -DCK_GEM_MUTANT_$$m -I$(KDIR)/dev -o $(OUT)/host/test_pi5_gem_mut_$$m $(KDIR)/tests/test_pi5_gem.c $(PI5GEM_SRCS) || exit 1; \ + if $(OUT)/host/test_pi5_gem_mut_$$m >/dev/null; then echo "MUTANT SURVIVED: $$m"; fail=1; else echo "mutant killed: $$m"; fi; \ + done; test $$fail -eq 0 +.PHONY: test-pi5-gem pi5-gem-mutants diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 27c03043..722e1a6b 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T22:05:34Z", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:9302f530853614d0541c387ba09fca4364bc195eef335a24bbe205dfd69f1ecd", + "digest": "sha256:ebdd66c0966fba664f36dc167836b98d26ffae97f9502b4409e9c22da687b473", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 41, - "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", - "source_tree": "sha256:5cc13d7f7294ea12b0564904b87dd7c6bea8611da9f63e928351e8387a7d3e83" + "files": 43, + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:802face91e606e3063e35e7499217980f488aa11797961decc4c17e21bf4c22f" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/pi5_gem.c b/native/kernel/dev/pi5_gem.c new file mode 100644 index 00000000..844fdcb1 --- /dev/null +++ b/native/kernel/dev/pi5_gem.c @@ -0,0 +1,1052 @@ +/* pi5_gem.c -- see pi5_gem.h. Pi 5 network cut 3: one frame through the RP1 GEM with descriptors only in the + * ADR 0020 bounce window. software-only DMA confinement (Pi 5, weaker than the Spark). */ +#include "pi5_gem.h" + +/* TEST-only mutants (make pi5-gem-mutants): each removes one decision and must make test_pi5_gem FAIL. + * Host builds only; never part of an image. */ +#if defined(CK_GEM_MUTANT_NO_VALIDATE) || defined(CK_GEM_MUTANT_BME_EARLY) || defined(CK_GEM_MUTANT_BME_LEFT_ON) || \ + defined(CK_GEM_MUTANT_NO_QUIESCE_WAIT) || defined(CK_GEM_MUTANT_NO_RETE_OFF) || defined(CK_GEM_MUTANT_NO_BARRIER) || \ + defined(CK_GEM_MUTANT_NO_TIEOFF) || defined(CK_GEM_MUTANT_NO_RXLEN_CHECK) || defined(CK_GEM_MUTANT_NO_TX_LAST) || \ + defined(CK_GEM_MUTANT_NO_SENTINEL_USED) || defined(CK_GEM_MUTANT_NO_ADDR64) || defined(CK_GEM_MUTANT_NO_HWWIN) || \ + defined(CK_GEM_MUTANT_NOT_STICKY) || defined(CK_GEM_MUTANT_NO_FRAMECAP) || defined(CK_GEM_MUTANT_NO_SEQ) || \ + defined(CK_GEM_MUTANT_NO_DIFF) || defined(CK_GEM_MUTANT_MDIO_OP) || defined(CK_GEM_MUTANT_PHYLB_BIT) || \ + defined(CK_GEM_MUTANT_NO_WRAP) || defined(CK_GEM_MUTANT_NO_GBE) || defined(CK_GEM_MUTANT_NO_DRFCS) || \ + defined(CK_GEM_MUTANT_NO_BME_VERIFY) || defined(CK_GEM_MUTANT_NO_LLB_CLEAR) +#if !__STDC_HOSTED__ +#error "CK_GEM_MUTANT_* are host test mutants only" +#endif +#ifdef CK_HARDWARE_STAGING +#error "CK_GEM_MUTANT_* cannot be combined with CK_HARDWARE_STAGING" +#endif +#endif + +#define GEM(off) (CK_RP1_ETH + (off)) +static uint32_t grd(const struct ck_gem *g, uint32_t off) { return g->rio->rd(g->rio->ctx, GEM(off)); } +static void gwr(const struct ck_gem *g, uint32_t off, uint32_t v) { g->rio->wr(g->rio->ctx, GEM(off), v); } +static void gdelay(const struct ck_gem *g, uint32_t us) +{ + if (g->rio->delay_us && us) + g->rio->delay_us(g->rio->ctx, us); +} +static void gbarrier(const struct ck_gem *g) +{ +#ifndef CK_GEM_MUTANT_NO_BARRIER + if (g->barrier) + g->barrier(); +#else + (void)g; +#endif +} + +/* ---------------- probe ---------------- */ + +int ck_gem_probe(const struct ck_rp1_io *io, struct ck_gem_caps *c) +{ + uint32_t dbwdef, i; + if (!io || !c) + return CK_GEM_E_ARG; + *c = (struct ck_gem_caps){0}; + c->mid = io->rd(io->ctx, GEM(CK_GEM_MID)); + c->is_gem = ((c->mid >> CK_GEM_MID_IDNUM_SHIFT) & 0xfffu) >= 2u; /* hw_is_gem [M2] */ + if (!c->is_gem) + return CK_GEM_E_NOGEM; + c->dcfg1 = io->rd(io->ctx, GEM(CK_GEM_DCFG1)); + c->dcfg2 = io->rd(io->ctx, GEM(CK_GEM_DCFG2)); + c->dcfg6 = io->rd(io->ctx, GEM(CK_GEM_DCFG6)); + c->daw64 = (c->dcfg6 & CK_GEM_DCFG6_DAW64) != 0; + c->queue_mask = 1u | (c->dcfg6 & 0xffu); /* macb_probe_queues: bit 0 is never set but queue 0 always exists */ + for (i = 0; i < CK_GEM_MAX_QUEUES; i++) + if (c->queue_mask & (1u << i)) + c->nqueues++; + c->desc_bytes = c->daw64 ? 16u : 8u; + dbwdef = (c->dcfg1 >> CK_GEM_DCFG1_DBWDEF_SHIFT) & 7u; + c->dbw = dbwdef == 4 ? 2u : dbwdef == 2 ? 1u : 0u; /* macb_dbw: 128 / 64 / 32 bit */ + return 0; +} + +void ck_gem_init(struct ck_gem *g, const struct ck_rp1_io *rio, struct ck_dmaw *w, volatile uint8_t *va, + int (*bme)(void *, int), void *bme_ctx, void (*barrier)(void), int hw_window_ok) +{ + *g = (struct ck_gem){0}; + g->rio = rio; + g->w = w; + g->va = va; + g->bme = bme; + g->bme_ctx = bme_ctx; + g->barrier = barrier; + g->hw_window_ok = hw_window_ok; +} + +/* ---------------- window memory (CPU side) ---------------- */ + +static int in_win(const struct ck_gem *g, uint64_t bus, uint64_t len) +{ + return bus >= g->w->base && len <= g->w->size && (bus - g->w->base) <= (g->w->size - len); +} +static uint32_t m_rd(const struct ck_gem *g, uint64_t bus) +{ + return in_win(g, bus, 4) ? *(volatile uint32_t *)(g->va + (bus - g->w->base)) : 0xdeadbeefu; +} +static void m_wr(const struct ck_gem *g, uint64_t bus, uint32_t v) +{ + if (in_win(g, bus, 4)) + *(volatile uint32_t *)(g->va + (bus - g->w->base)) = v; +} +static void m_zero(const struct ck_gem *g, uint64_t bus, uint64_t len) +{ + uint64_t i; + if (!in_win(g, bus, len)) + return; + for (i = 0; i < len; i++) + g->va[(bus - g->w->base) + i] = 0; +} + +/* ---------------- frames ---------------- */ + +static const char k_magic[] = "AIENOS-PI5-NET3"; /* 15 chars + NUL = 16 bytes */ + +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) +{ + uint32_t i; + if (!mac || !out || cap < 60u) + return 0; + for (i = 0; i < 60u; i++) + out[i] = 0; + for (i = 0; i < 6; i++) { + out[i] = 0xff; + out[6 + i] = mac[i]; + } + if (kind == CK_GEMF_RAW) { + out[12] = (uint8_t)(CK_GEMF_ETHERTYPE_EXP >> 8); + out[13] = (uint8_t)CK_GEMF_ETHERTYPE_EXP; + for (i = 0; i < 16; i++) + out[14 + i] = (uint8_t)k_magic[i]; +#ifndef CK_GEM_MUTANT_NO_SEQ + out[30] = (uint8_t)(seq >> 24); + out[31] = (uint8_t)(seq >> 16); + out[32] = (uint8_t)(seq >> 8); + out[33] = (uint8_t)seq; +#endif + for (i = 34; i < 60u; i++) + out[i] = (uint8_t)(seq + i * 7u + 1u); + return 60u; + } + if (kind == CK_GEMF_ARP) { + static const uint8_t hdr[8] = {0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01}; /* htype 1, ptype IPv4, hlen 6, plen 4, request */ + out[12] = 0x08; + out[13] = 0x06; + for (i = 0; i < 8; i++) + out[14 + i] = hdr[i]; + for (i = 0; i < 6; i++) + out[22 + i] = mac[i]; /* sender hardware address; sender IP (28..31) and target hardware (32..37) stay zero: a probe */ + if (tpa) + for (i = 0; i < 4; i++) + out[38 + i] = tpa[i]; + out[42] = 'A'; out[43] = 'I'; out[44] = 'E'; out[45] = 'N'; out[46] = '3'; +#ifndef CK_GEM_MUTANT_NO_SEQ + out[47] = (uint8_t)(seq >> 24); + out[48] = (uint8_t)(seq >> 16); + out[49] = (uint8_t)(seq >> 8); + out[50] = (uint8_t)seq; +#endif + for (i = 51; i < 60u; i++) + out[i] = (uint8_t)(seq + i * 7u + 1u); + return 60u; + } + return 0; +} + +uint32_t ck_gem_frame_diff(const uint8_t *a, uint32_t alen, const uint8_t *b, uint32_t blen) +{ + uint32_t i, n = alen < blen ? alen : blen; +#ifdef CK_GEM_MUTANT_NO_DIFF + if (n) + n--; /* mutant: ignores the last byte */ + if (alen != blen && n == 0) + return 1; + for (i = 0; i < n; i++) + if (a[i] != b[i]) + return i + 1; + return 0; +#else + for (i = 0; i < n; i++) + if (a[i] != b[i]) + return i + 1; + return alen == blen ? 0 : n + 1; +#endif +} + +/* ---------------- bus-master ---------------- */ + +int ck_pi5gem_bme_set(const struct ck_pi5pcie_io *io, int on) +{ + uint32_t v = 0, nv; + if (!io || ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v)) + return CK_GEM_E_ARG; + nv = (v & 0xffffu); /* status half written as zero: RW1C, nothing cleared */ + nv = on ? (nv | CK_PI5NET_CMD_BME) : (nv & ~(uint32_t)CK_PI5NET_CMD_BME); + if (ck_pi5net_cfg_write32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, nv)) + return CK_GEM_E_ARG; +#ifndef CK_GEM_MUTANT_NO_BME_VERIFY + if (ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v)) + return CK_GEM_E_ARG; + if (((v & CK_PI5NET_CMD_BME) != 0) != (on != 0)) + return CK_GEM_E_BME; +#endif + return 0; +} + +/* ---------------- descriptors (CPU writes into the window) ---------------- */ + +/* Hand a TX slot to the device: address words first, ctrl (USED clear) last [M2 macb_tx_map]. */ +static void tx_desc_write(const struct ck_gem *g, uint64_t slot_bus, uint64_t buf, uint32_t len, uint32_t flags_ctrl) +{ + uint32_t dsz = g->caps.desc_bytes; + m_wr(g, slot_bus + 0, (uint32_t)buf); + if (dsz == 16) { + m_wr(g, slot_bus + 8, (uint32_t)(buf >> 32)); + m_wr(g, slot_bus + 12, 0); + } + gbarrier(g); + m_wr(g, slot_bus + 4, (len & CK_GEMD_TX_FRMLEN_MASK) | flags_ctrl); +} + +/* RX slot: ctrl 0 and the high address first, the low address word (USED clear) last [M2 gem_rx_refill]. */ +static void rx_desc_write(const struct ck_gem *g, uint64_t slot_bus, uint64_t buf, uint32_t addr_flags) +{ + uint32_t dsz = g->caps.desc_bytes; + m_wr(g, slot_bus + 4, 0); + if (dsz == 16) { + m_wr(g, slot_bus + 8, (uint32_t)(buf >> 32)); + m_wr(g, slot_bus + 12, 0); + } + gbarrier(g); + m_wr(g, slot_bus + 0, ((uint32_t)buf & ~3u) | addr_flags); +} + +static int post(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint64_t addr, uint32_t len, uint32_t flags, uint32_t *idx, int caller_chosen) +{ +#ifdef CK_GEM_MUTANT_NO_VALIDATE + if (caller_chosen) { /* mutant: a caller-chosen address is trusted */ + struct ck_dmaw_slot *s = &r->slots[r->head]; + s->d.addr = addr; s->d.len = len; s->d.flags = flags; s->state = CK_DMAW_SLOT_INFLIGHT; + *idx = r->head; r->head = (r->head + 1) & (r->n - 1); r->inflight++; + return CK_DMAW_OK; + } +#else + (void)caller_chosen; +#endif + return ck_dmaw_ring_post(r, w, addr, len, flags, idx); +} + +#ifdef CK_GEM_MUTANT_NO_VALIDATE +static int emit(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t i, struct ck_dmaw_desc *d) +{ + (void)w; + *d = r->slots[i].d; /* mutant: shadow trusted without re-validation */ + return 0; +} +#else +#define emit ck_dmaw_ring_emit +#endif + +/* ---------------- hardware programming ---------------- */ + +static void gem_reset_hw(const struct ck_gem *g) +{ + uint32_t ncr = grd(g, CK_GEM_NCR), q; + ncr &= ~(CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_TSTART); /* macb_reset_hw [M2] */ +#ifndef CK_GEM_MUTANT_NO_LLB_CLEAR + ncr &= ~CK_GEM_NCR_LLB; +#endif + ncr |= CK_GEM_NCR_CLRSTAT; + gwr(g, CK_GEM_NCR, ncr); + gwr(g, CK_GEM_TSR, 0xffffffffu); + gwr(g, CK_GEM_RSR, 0xffffffffu); + gwr(g, CK_GEM_PBUFRXCUT, 0); + gwr(g, CK_GEM_IDR, 0xffffffffu); + (void)grd(g, CK_GEM_ISR); /* read clears (no MACB_CAPS_ISR_CLEAR_ON_WRITE for RP1) */ + for (q = 1; q < CK_GEM_MAX_QUEUES; q++) { + if (!(g->caps.queue_mask & (1u << q))) + continue; + gwr(g, CK_GEM_QN_IDR(q), 0xffffffffu); + (void)grd(g, CK_GEM_QN_ISR(q)); + } +} + +static void gem_set_mac(const struct ck_gem *g, const uint8_t m[6]) +{ + gwr(g, CK_GEM_SA1B, (uint32_t)m[0] | ((uint32_t)m[1] << 8) | ((uint32_t)m[2] << 16) | ((uint32_t)m[3] << 24)); + gwr(g, CK_GEM_SA1T, (uint32_t)m[4] | ((uint32_t)m[5] << 8)); + gwr(g, CK_GEM_SA2B, 0); gwr(g, CK_GEM_SA2B + 4, 0); + gwr(g, CK_GEM_SA2B + 8, 0); gwr(g, CK_GEM_SA2B + 12, 0); + gwr(g, CK_GEM_SA2B + 16, 0); gwr(g, CK_GEM_SA2B + 20, 0); + gwr(g, CK_GEM_HRB, 0); + gwr(g, CK_GEM_HRT, 0); +} + +static void gem_config(const struct ck_gem *g, const struct ck_gem_xfer *x) +{ + uint32_t cfg = grd(g, CK_GEM_NCFGR), dma, amp; + /* NCFGR [M2 macb_init_hw + macb_mac_link_up]: keep the MDC divider the MDIO code set; data bus width from DCFG1; + * discard the FCS on receive; broadcast accepted (NBC clear); no copy-all; standard frame size (no BIG/JFRAME). */ + cfg &= (uint32_t)CK_GEM_NCFGR_CLK_MASK; + cfg |= g->caps.dbw << CK_GEM_NCFGR_DBW_SHIFT; +#ifndef CK_GEM_MUTANT_NO_DRFCS + cfg |= CK_GEM_NCFGR_DRFCS; +#endif + if (x->full_duplex) + cfg |= CK_GEM_NCFGR_FD; + if (x->speed == 100) + cfg |= CK_GEM_NCFGR_SPD; +#ifndef CK_GEM_MUTANT_NO_GBE + if (x->speed == 1000) + cfg |= CK_GEM_NCFGR_GBE; +#endif + gwr(g, CK_GEM_NCFGR, cfg); + /* gem_init_axi [M2]: AXI pipelines as rp1.dtsi sets them. */ + amp = grd(g, CK_GEM_AMP); + amp |= CK_GEM_AMP_AW2B_FILL; + amp = (amp & ~(uint32_t)CK_GEM_AMP_AW2W_MASK) | (CK_GEM_AXI_PIPE << CK_GEM_AMP_AW2W_SHIFT); + amp = (amp & ~(uint32_t)CK_GEM_AMP_AR2R_MASK) | CK_GEM_AXI_PIPE; + gwr(g, CK_GEM_AMP, amp); + /* macb_configure_dma [M2] */ + dma = grd(g, CK_GEM_DMACFG); + dma &= ~(uint32_t)(CK_GEM_DMACFG_RXBS_MASK | CK_GEM_DMACFG_FBLDO_MASK | CK_GEM_DMACFG_ENDIA_PKT | + CK_GEM_DMACFG_ENDIA_DESC | CK_GEM_DMACFG_TXCOEN | CK_GEM_DMACFG_RXEXT | CK_GEM_DMACFG_TXEXT | + CK_GEM_DMACFG_ADDR64); + dma |= ((x->rx_buf_len ? x->rx_buf_len : 64u) / CK_GEM_RXBS_UNIT) << CK_GEM_DMACFG_RXBS_SHIFT; + dma |= CK_GEM_BURST; + dma |= CK_GEM_DMACFG_TXPBMS | CK_GEM_DMACFG_RXBMS_MASK; +#ifndef CK_GEM_MUTANT_NO_ADDR64 + if (g->caps.daw64) + dma |= CK_GEM_DMACFG_ADDR64; +#endif + gwr(g, CK_GEM_DMACFG, dma); + /* macb_init: USRIO for an RGMII phy-mode (rp1.dtsi: rgmii-id). */ + gwr(g, CK_GEM_USRIO, CK_GEM_USRIO_RGMII); +} + +/* ---------------- one transfer ---------------- */ + +static int refuse(struct ck_gem *g, int rc) +{ + (void)g; +#ifndef CK_GEM_MUTANT_NOT_STICKY + g->halted = 1; +#endif + return rc; +} + +int ck_gem_xfer(struct ck_gem *g, struct ck_gem_xfer *x) +{ + struct ck_dmaw *w; + struct ck_dmaw_slot txs[CK_GEM_TXRING], rxs[CK_GEM_RXRING]; + struct ck_dmaw_ring txr, rxr; + struct ck_dmaw_desc d; + uint64_t next0, txring, rxring, tie_tx, tie_rx, txbuf, sentbuf, rxbuf[3] = {0, 0, 0}; + uint32_t dsz, i, idx, polls, q, ncr, flags; + int rc = 0, qrc = 0, prc; + + if (!g || !g->w || !g->rio || !x || !x->frame) + return CK_GEM_E_ARG; + w = g->w; + x->tx_done = x->rx_done = 0; + x->tx_ctrl = x->rx_word0 = x->rx_ctrl = x->rx_len = 0; + x->tsr = x->rsr = x->isr = x->ncr_run = x->ncfgr_run = x->dmacfg_run = x->ncr_end = x->tsr_end = 0; + x->tx_bus = x->rx_bus = 0; + x->rx_copied = x->rx_frames = x->rx_slot = 0; + x->bme_enabled_for_us = 0; + x->bme_off_at_end = 0; +#ifndef CK_GEM_MUTANT_NOT_STICKY + if (g->halted || ck_dmaw_halted(w)) + return CK_GEM_E_HALTED; +#endif + if (x->mode < CK_GEMX_LLB || x->mode > CK_GEMX_WIRE || (x->speed != 10 && x->speed != 100 && x->speed != 1000) || + x->rx_count > 3 || (x->rx_count && (x->rx_buf_len < 64u || x->rx_buf_len > CK_DMAW_MAX_FRAME || (x->rx_buf_len % 64u)))) + return CK_GEM_E_ARG; +#ifdef CK_GEM_MUTANT_NO_FRAMECAP + if (x->frame_len < 60u) +#else + if (x->frame_len < 60u || x->frame_len > 1514u) +#endif + return CK_GEM_E_ARG; + if (!g->caps.is_gem) + return CK_GEM_E_NOGEM; +#ifndef CK_GEM_MUTANT_NO_HWWIN + if (!g->hw_window_ok) + return CK_GEM_E_HWWIN; /* no root-complex backstop confirmed: bus-master stays off, nothing touched */ +#endif +#ifdef CK_GEM_MUTANT_BME_EARLY + if (g->bme) + (void)g->bme(g->bme_ctx, 1); /* mutant: bus-master before anything is validated */ +#endif + dsz = g->caps.desc_bytes; + next0 = w->next; + + /* ---- 1. layout and validation: memory only, no register is touched, bus-master stays off ---- */ + if (ck_dmaw_alloc(w, CK_GEM_TXRING * dsz, 64, &txring) || ck_dmaw_alloc(w, CK_GEM_RXRING * dsz, 64, &rxring) || + ck_dmaw_alloc(w, dsz, 64, &tie_tx) || ck_dmaw_alloc(w, dsz, 64, &tie_rx) || + ck_dmaw_alloc(w, 64, 64, &sentbuf) || ck_dmaw_alloc(w, (uint64_t)((x->frame_len + 63u) & ~63u), 64, &txbuf)) { + w->next = next0; + return CK_GEM_E_WINDOW; + } + for (i = 0; i < x->rx_count; i++) + if (ck_dmaw_alloc(w, x->rx_buf_len, 64, &rxbuf[i])) { + w->next = next0; + return CK_GEM_E_WINDOW; + } + if (x->tx_addr) + txbuf = x->tx_addr; + if (x->rx_count && x->rx_addr) + rxbuf[0] = x->rx_addr; + if (!g->caps.daw64 && ((w->base + w->size - 1) >> 32)) + return refuse(g, CK_GEM_E_WINDOW); /* 32-bit descriptors cannot name this window */ + /* the four tables the device reads or writes, and every buffer, through the ADR 0020 validator */ + if (ck_dmaw_validate_aligned(w, txring, CK_GEM_TXRING * dsz, 64) || ck_dmaw_validate_aligned(w, rxring, CK_GEM_RXRING * dsz, 64) || + ck_dmaw_validate_aligned(w, tie_tx, dsz, 64) || ck_dmaw_validate_aligned(w, tie_rx, dsz, 64)) + return refuse(g, CK_GEM_E_WINDOW); + if (ck_dmaw_ring_init(&txr, txs, CK_GEM_TXRING) || ck_dmaw_ring_init(&rxr, rxs, CK_GEM_RXRING)) + return refuse(g, CK_GEM_E_ARG); + prc = post(&txr, w, txbuf, x->frame_len, CK_GEMD_TX_LAST, &idx, x->tx_addr != 0); + if (prc) + return refuse(g, CK_GEM_E_WINDOW); + prc = post(&txr, w, sentbuf, 64, CK_GEMD_TX_USED | CK_GEMD_TX_WRAP, &idx, 0); /* sentinel: never executed (USED) */ + if (prc) + return refuse(g, CK_GEM_E_WINDOW); + for (i = 0; i < CK_GEM_RXRING; i++) { + if (i < x->rx_count) + prc = post(&rxr, w, rxbuf[i], x->rx_buf_len, 0, &idx, i == 0 && x->rx_addr != 0); + else + prc = post(&rxr, w, sentbuf, 64, CK_GEMD_RX_USED, &idx, 0); + if (prc) + return refuse(g, CK_GEM_E_WINDOW); + } + + /* ---- 2. write the descriptors from the shadow only ---- */ + m_zero(g, txring, CK_GEM_TXRING * dsz); + m_zero(g, rxring, CK_GEM_RXRING * dsz); + m_zero(g, tie_tx, dsz); + m_zero(g, tie_rx, dsz); + m_zero(g, sentbuf, 64); + m_zero(g, txbuf, (uint64_t)((x->frame_len + 63u) & ~63u)); + for (i = 0; i < x->rx_count; i++) + m_zero(g, rxbuf[i], x->rx_buf_len); + if (in_win(g, txbuf, x->frame_len)) + for (i = 0; i < x->frame_len; i++) + g->va[(txbuf - w->base) + i] = x->frame[i]; /* copy only (ADR 0020 rule 5) */ + x->tx_bus = txbuf; + x->rx_bus = x->rx_count ? rxbuf[0] : 0; + + for (i = 0; i < CK_GEM_TXRING; i++) { + if (emit(&txr, w, i, &d)) + return refuse(g, CK_GEM_E_WINDOW); + flags = i == 0 ? 0 : (CK_GEMD_TX_USED | CK_GEMD_TX_WRAP); +#ifdef CK_GEM_MUTANT_NO_SENTINEL_USED + flags = i == 0 ? 0 : CK_GEMD_TX_WRAP; +#endif + if (i == 0) { +#ifndef CK_GEM_MUTANT_NO_TX_LAST + flags |= CK_GEMD_TX_LAST; +#endif + } + if (i == 0 && x->raw_tx_outside) + d.addr = x->raw_tx_outside; /* confinement test only */ + tx_desc_write(g, txring + (uint64_t)i * dsz, d.addr, d.len, flags); + } + for (i = 0; i < CK_GEM_RXRING; i++) { + if (emit(&rxr, w, i, &d)) + return refuse(g, CK_GEM_E_WINDOW); + flags = (i >= x->rx_count) ? CK_GEMD_RX_USED : 0; +#ifndef CK_GEM_MUTANT_NO_WRAP + if (i == CK_GEM_RXRING - 1) + flags |= CK_GEMD_RX_WRAP; +#endif + if (i == 0 && x->raw_rx_outside) + d.addr = x->raw_rx_outside; /* confinement test only */ + rx_desc_write(g, rxring + (uint64_t)i * dsz, d.addr, flags); + } + /* tie-off descriptors for every other hardware queue [M2 macb_init_tieoff]: USED so the device never uses them */ + tx_desc_write(g, tie_tx, sentbuf, 64, CK_GEMD_TX_USED | CK_GEMD_TX_WRAP); + rx_desc_write(g, tie_rx, sentbuf, CK_GEMD_RX_USED | CK_GEMD_RX_WRAP); + gbarrier(g); + + /* ---- 3. program the MAC (bus-master still off) ---- */ + gem_reset_hw(g); + gem_set_mac(g, x->mac); + gem_config(g, x); + gwr(g, CK_GEM_RBQP, (uint32_t)rxring); + gwr(g, CK_GEM_TBQP, (uint32_t)txring); + if (g->caps.daw64) { + gwr(g, CK_GEM_RBQPH, (uint32_t)(rxring >> 32)); + gwr(g, CK_GEM_TBQPH, (uint32_t)(txring >> 32)); + } +#ifndef CK_GEM_MUTANT_NO_TIEOFF + for (q = 1; q < CK_GEM_MAX_QUEUES; q++) { + if (!(g->caps.queue_mask & (1u << q))) + continue; + gwr(g, CK_GEM_QN_TBQP(q), (uint32_t)tie_tx); + gwr(g, CK_GEM_QN_RBQP(q), (uint32_t)tie_rx); + gwr(g, CK_GEM_QN_RBQS(q), (x->rx_buf_len ? x->rx_buf_len : 64u) / CK_GEM_RXBS_UNIT); + } +#else + (void)q; (void)tie_tx; (void)tie_rx; +#endif + x->dmacfg_run = grd(g, CK_GEM_DMACFG); + x->ncfgr_run = grd(g, CK_GEM_NCFGR); + gbarrier(g); + + /* ---- 4. bus-master on ONLY now, after every descriptor passed the validator ---- */ + if (!g->bme) + return refuse(g, CK_GEM_E_BME); +#ifndef CK_GEM_MUTANT_BME_EARLY + if (g->bme(g->bme_ctx, 1)) { + (void)g->bme(g->bme_ctx, 0); + return refuse(g, CK_GEM_E_BME); + } +#endif + g->bme_on = 1; + x->bme_enabled_for_us = 1; + ncr = grd(g, CK_GEM_NCR); + ncr |= CK_GEM_NCR_TE; + if (x->rx_count) + ncr |= CK_GEM_NCR_RE; + if (x->mode == CK_GEMX_LLB) + ncr |= CK_GEM_NCR_LLB; + gwr(g, CK_GEM_NCR, ncr); + gbarrier(g); + gwr(g, CK_GEM_NCR, ncr | CK_GEM_NCR_TSTART); + x->ncr_run = grd(g, CK_GEM_NCR); + g->xfers++; + + /* ---- 5. poll (bounded) ---- */ + for (polls = 0; polls <= x->tx_wait_us / 10u; polls++) { + if (m_rd(g, txring + 4) & CK_GEMD_TX_USED) { + x->tx_done = 1; + break; + } + gdelay(g, 10); + } + if (x->rx_count) { + for (polls = 0; polls <= x->rx_wait_us / 10u; polls++) { + if ((m_rd(g, rxring) & CK_GEMD_RX_USED) && (m_rd(g, rxring + 4) & (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) == + (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) + break; + gdelay(g, 10); + } + } + x->tsr = grd(g, CK_GEM_TSR); + x->rsr = grd(g, CK_GEM_RSR); + x->isr = grd(g, CK_GEM_ISR); + + /* ---- 6. quiesce: stop RE/TE (and loopback), wait for the transmitter, settle, THEN bus-master off ---- */ + ncr = grd(g, CK_GEM_NCR); +#ifndef CK_GEM_MUTANT_NO_RETE_OFF + ncr &= ~(CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_LLB | CK_GEM_NCR_TSTART); + gwr(g, CK_GEM_NCR, ncr); +#endif +#ifndef CK_GEM_MUTANT_NO_QUIESCE_WAIT + for (polls = 0; polls < 1000u; polls++) { + if (!(grd(g, CK_GEM_TSR) & CK_GEM_TSR_TGO)) + break; + gdelay(g, 10); + } + if (grd(g, CK_GEM_TSR) & CK_GEM_TSR_TGO) + qrc = CK_GEM_E_QUIESCE; +#endif + gdelay(g, 2000); /* let anything already on the bus finish (same margin as the window change) */ + gwr(g, CK_GEM_TSR, 0xffffffffu); + gwr(g, CK_GEM_RSR, 0xffffffffu); + x->ncr_end = grd(g, CK_GEM_NCR); + x->tsr_end = grd(g, CK_GEM_TSR); +#ifndef CK_GEM_MUTANT_BME_LEFT_ON + if (g->bme(g->bme_ctx, 0)) { + g->halted = 1; + return CK_GEM_E_BME; + } + g->bme_on = 0; + x->bme_off_at_end = 1; +#endif + if (qrc) { + g->halted = 1; + rc = qrc; + } + + /* ---- 7. results: device-written words are read as data only; lengths are checked against the shadow ---- */ + x->tx_ctrl = m_rd(g, txring + 4); + x->tx_done = (x->tx_ctrl & CK_GEMD_TX_USED) != 0; + for (i = 0; i < x->rx_count; i++) { + uint32_t w0 = m_rd(g, rxring + (uint64_t)i * dsz), c = m_rd(g, rxring + (uint64_t)i * dsz + 4); + if ((w0 & CK_GEMD_RX_USED) && (c & (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) == (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) { + uint32_t len = c & CK_GEMD_RX_FRMLEN_MASK, n; +#ifndef CK_GEM_MUTANT_NO_RXLEN_CHECK + if (ck_dmaw_ring_complete(&rxr, w, i, len)) { + g->halted = 1; /* the device claims more bytes than the buffer holds */ + return CK_GEM_E_DEVICE; + } +#endif + if (x->rx_frames++ == 0) { + x->rx_slot = i; + x->rx_done = 1; + x->rx_word0 = w0; + x->rx_ctrl = c; + x->rx_len = len; + n = len < CK_GEM_RXCOPY ? len : CK_GEM_RXCOPY; + if (n > x->rx_buf_len) + n = x->rx_buf_len; + if (in_win(g, rxbuf[i], n)) { + uint32_t k; + for (k = 0; k < n; k++) + x->rx_data[k] = g->va[(rxbuf[i] - w->base) + k]; + } else { + n = 0; + } + x->rx_copied = n; + } + } + } + if (x->tx_done && ck_dmaw_ring_complete(&txr, w, 0, x->frame_len)) { + g->halted = 1; + return CK_GEM_E_DEVICE; + } + /* give the window space back, zeroed, so nothing the device left is read later as if it were new */ + m_zero(g, w->base + next0, w->next - next0); + w->next = next0; + return rc; +} + +void ck_gem_park(struct ck_gem *g) +{ + uint32_t ncr = grd(g, CK_GEM_NCR); + ncr &= ~(CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_LLB | CK_GEM_NCR_TSTART); + gwr(g, CK_GEM_NCR, ncr); +} + +/* ---------------- PHY ---------------- */ + +uint32_t ck_gem_mdio_frame_write(uint32_t phy, uint32_t reg, uint16_t val) +{ +#ifdef CK_GEM_MUTANT_MDIO_OP + uint32_t op = 2u; /* mutant: the READ op code */ +#else + uint32_t op = 1u; /* MACB_MAN_C22_WRITE */ +#endif + return (1u << 30) | (op << 28) | ((phy & 31u) << 23) | ((reg & 31u) << 18) | (2u << 16) | val; +} + +int ck_gem_mdio_write(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t val) +{ + uint32_t i; + if (!io || phy > 31u || reg > 31u) + return CK_GEM_E_ARG; + for (i = 0; i < CK_GEM_MDIO_POLLS; i++) { + if (io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NSR) & CK_GEM_NSR_IDLE) + break; + if (io->delay_us) + io->delay_us(io->ctx, CK_GEM_MDIO_POLL_US); + } + if (i == CK_GEM_MDIO_POLLS) + return CK_GEM_E_TIMEOUT; + io->wr(io->ctx, CK_RP1_ETH + CK_GEM_MAN, ck_gem_mdio_frame_write(phy, reg, val)); + for (i = 0; i < CK_GEM_MDIO_POLLS; i++) { + if (io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NSR) & CK_GEM_NSR_IDLE) + return 0; + if (io->delay_us) + io->delay_us(io->ctx, CK_GEM_MDIO_POLL_US); + } + return CK_GEM_E_TIMEOUT; +} + +void ck_eth_cfg_status_decode(uint32_t st, int *link, uint32_t *speed, int *full_duplex) +{ + uint32_t s = (st >> 1) & 3u; + if (link) + *link = (st & 1u) != 0; + if (speed) + *speed = s == 0 ? 10u : s == 1 ? 100u : s == 2 ? 1000u : 0u; + if (full_duplex) + *full_duplex = (st & 8u) != 0; +} + +int ck_phy_link_read(const struct ck_rp1_io *io, uint32_t phy, struct ck_phy_link *l) +{ + int e = 0; + if (!io || !l) + return CK_GEM_E_ARG; + *l = (struct ck_phy_link){0}; + e |= ck_gem_mdio_read(io, phy, CK_MII_BMCR, &l->bmcr); + e |= ck_gem_mdio_read(io, phy, CK_MII_BMSR, &l->bmsr_first); /* latched low: the first read reports a past drop */ + e |= ck_gem_mdio_read(io, phy, CK_MII_BMSR, &l->bmsr); + e |= ck_gem_mdio_read(io, phy, CK_MII_ADVERTISE, &l->adv); + e |= ck_gem_mdio_read(io, phy, CK_MII_LPA, &l->lpa); + e |= ck_gem_mdio_read(io, phy, CK_MII_CTRL1000, &l->ctrl1000); + e |= ck_gem_mdio_read(io, phy, CK_MII_STAT1000, &l->stat1000); + if (e) + return CK_GEM_E_TIMEOUT; + l->link = (l->bmsr & CK_MII_BMSR_LSTATUS) != 0; + l->aneg_done = (l->bmsr & CK_MII_BMSR_ANEGCOMPLETE) != 0; + if (!l->link) + return 0; + if (l->bmcr & CK_MII_BMCR_ANENABLE) { + uint16_t gb, com; + if (!l->aneg_done) + return 0; + gb = (uint16_t)(l->stat1000 & (l->ctrl1000 << 2)); /* LPA_1000FULL = ADVERTISE_1000FULL << 2 */ + com = (uint16_t)(l->adv & l->lpa); + if (gb & 0x0800u) { l->speed = 1000; l->full_duplex = 1; } + else if (gb & 0x0400u) { l->speed = 1000; l->full_duplex = 0; } + else if (com & 0x0100u) { l->speed = 100; l->full_duplex = 1; } + else if (com & 0x0080u) { l->speed = 100; l->full_duplex = 0; } + else if (com & 0x0040u) { l->speed = 10; l->full_duplex = 1; } + else if (com & 0x0020u) { l->speed = 10; l->full_duplex = 0; } + } else { + l->speed = (l->bmcr & CK_MII_BMCR_SPEED1000) ? 1000u : (l->bmcr & CK_MII_BMCR_SPEED100) ? 100u : 10u; + l->full_duplex = (l->bmcr & CK_MII_BMCR_FULLDPLX) != 0; + } + return 0; +} + +int ck_phy_rgmii_id_config(const struct ck_rp1_io *io, uint32_t phy, uint16_t *auxctl_before, uint16_t *clk_before) +{ + /* Linux bcm54xx_config_clock_delay for PHY_INTERFACE_MODE_RGMII_ID [P1]: + * RX skew: AUXCTL shadow MISC (0x07) read-modify-write with WREN (0x8000) and RGMII_SKEW_EN (0x0100). + * TX delay: shadow CLK_CTL (0x03) |= GTXCLK_EN (1 << 9). */ + uint16_t v = 0, s = 0; + int e; + if (!io) + return CK_GEM_E_ARG; + e = ck_gem_mdio_write(io, phy, 0x18u, (uint16_t)(0x0007u | (0x0007u << 12))); /* select + read-select MISC */ + e |= ck_gem_mdio_read(io, phy, 0x18u, &v); + if (e) + return CK_GEM_E_TIMEOUT; + if (auxctl_before) + *auxctl_before = v; + v = (uint16_t)(v | 0x8000u | 0x0100u); + e = ck_gem_mdio_write(io, phy, 0x18u, (uint16_t)(0x0007u | v)); + e |= ck_gem_mdio_write(io, phy, 0x1cu, (uint16_t)(0x03u << 10)); /* shadow read select CLK_CTL */ + e |= ck_gem_mdio_read(io, phy, 0x1cu, &s); + if (e) + return CK_GEM_E_TIMEOUT; + s &= 0x3ffu; + if (clk_before) + *clk_before = s; + s |= (uint16_t)(1u << 9); + e = ck_gem_mdio_write(io, phy, 0x1cu, (uint16_t)(0x8000u | (0x03u << 10) | s)); + return e ? CK_GEM_E_TIMEOUT : 0; +} + +int ck_phy_loopback_enter(const struct ck_rp1_io *io, uint32_t phy, uint32_t speed, uint16_t *bmcr_saved, uint16_t *bmsr_out) +{ + uint16_t cur = 0, bmsr = 0; + uint32_t i; + uint16_t want; + if (!io || (speed != 10 && speed != 100 && speed != 1000)) + return CK_GEM_E_ARG; + if (ck_gem_mdio_read(io, phy, CK_MII_BMCR, &cur)) + return CK_GEM_E_TIMEOUT; + if (bmcr_saved) + *bmcr_saved = cur; +#ifdef CK_GEM_MUTANT_PHYLB_BIT + want = CK_MII_BMCR_FULLDPLX | 0x2000u; +#else + want = (uint16_t)(CK_MII_BMCR_LOOPBACK | CK_MII_BMCR_FULLDPLX); +#endif + if (speed == 1000) + want |= CK_MII_BMCR_SPEED1000; + else if (speed == 100) + want |= CK_MII_BMCR_SPEED100; + if (ck_gem_mdio_write(io, phy, CK_MII_BMCR, want)) + return CK_GEM_E_TIMEOUT; + for (i = 0; i < 100u; i++) { /* genphy_loopback waits up to 500 ms for link, 5 ms apart */ + (void)ck_gem_mdio_read(io, phy, CK_MII_BMSR, &bmsr); + (void)ck_gem_mdio_read(io, phy, CK_MII_BMSR, &bmsr); + if (bmsr & CK_MII_BMSR_LSTATUS) + break; + if (io->delay_us) + io->delay_us(io->ctx, 5000u); + } + if (bmsr_out) + *bmsr_out = bmsr; + return (bmsr & CK_MII_BMSR_LSTATUS) ? 0 : CK_GEM_E_TIMEOUT; +} + +int ck_phy_loopback_exit(const struct ck_rp1_io *io, uint32_t phy, uint16_t bmcr_saved) +{ + uint16_t v = (uint16_t)(bmcr_saved & ~CK_MII_BMCR_LOOPBACK); + if (!io) + return CK_GEM_E_ARG; + if (v & CK_MII_BMCR_ANENABLE) + v |= CK_MII_BMCR_ANRESTART; + return ck_gem_mdio_write(io, phy, CK_MII_BMCR, v) ? CK_GEM_E_TIMEOUT : 0; +} + +/* ================= image glue (CK_PI5_NET only) ================= */ +#ifdef CK_PI5_NET +#include "ck.h" + +static int glue_bme(void *ctx, int on) { return ck_pi5gem_bme_set((const struct ck_pi5pcie_io *)ctx, on); } +static void glue_barrier(void) { ck_mb(); } + +/* Locally administered unicast (bit 1 of the first octet set), not the board's own address: this cut does not read + * the OTP MAC (UNVERIFIED which one the board owns). */ +static const uint8_t k_mac[6] = {0x02, 0x41, 0x49, 0x45, 0x4e, 0x03}; + +static const char *verdict(int ok) { return ok ? "PASS" : "FAIL"; } + +static void print_xfer(const char *tag, int rc, const struct ck_gem_xfer *x, const uint8_t *tx) +{ + uint32_t diff = x->rx_done ? ck_gem_frame_diff(tx, x->frame_len, x->rx_data, x->rx_copied) : 0xffffffffu; + ck_printf("pi5_gem: %s rc=%d tx_done=%u rx_done=%u rx_frames=%u tx_ctrl=0x%08x rx_word0=0x%08x rx_ctrl=0x%08x rx_len=%u frame_diff=%u\n", + tag, rc, x->tx_done, x->rx_done, x->rx_frames, x->tx_ctrl, x->rx_word0, x->rx_ctrl, x->rx_len, + x->rx_done ? diff : 0xffffffffu); + ck_printf("pi5_gem: %s regs: tsr=0x%08x rsr=0x%08x isr=0x%08x ncr_run=0x%08x ncfgr=0x%08x dmacfg=0x%08x ncr_end=0x%08x tsr_end=0x%08x bme_on_for_us=%u bme_off_at_end=%u tx_bus=0x%llx rx_bus=0x%llx\n", + tag, x->tsr, x->rsr, x->isr, x->ncr_run, x->ncfgr_run, x->dmacfg_run, x->ncr_end, x->tsr_end, + x->bme_enabled_for_us, x->bme_off_at_end, (unsigned long long)x->tx_bus, (unsigned long long)x->rx_bus); +} + +static void print_link(const char *tag, const struct ck_phy_link *l, uint32_t eth_status) +{ + int el = 0, efd = 0; + uint32_t es = 0; + ck_eth_cfg_status_decode(eth_status, &el, &es, &efd); + ck_printf("pi5_gem: %s: BMCR=0x%04x BMSR=0x%04x/0x%04x ADV=0x%04x LPA=0x%04x CTRL1000=0x%04x STAT1000=0x%04x link=%u aneg_done=%u speed=%u duplex=%s | eth_cfg STATUS=0x%08x link=%u speed=%u duplex=%s\n", + tag, l->bmcr, l->bmsr_first, l->bmsr, l->adv, l->lpa, l->ctrl1000, l->stat1000, l->link, l->aneg_done, l->speed, + l->full_duplex ? "full" : "half", eth_status, el, es, efd ? "full" : "half"); +} + +static uint32_t pci_cmd(const struct ck_pi5gem_ctx *c) +{ + uint32_t v = 0; + (void)ck_pi5pcie_cfg_read32(c->rc, 1, 0, 0, 4, &v); + return v; +} + +static uint32_t eth_status(const struct ck_rp1_io *rio) { return rio->rd(rio->ctx, CK_RP1_ETH_CFG + 0x04u); } + +/* Wait (bounded) for the PHY to report link with autonegotiation complete. */ +static int wait_link(const struct ck_pi5gem_ctx *c, uint32_t max_ms, struct ck_phy_link *l) +{ + uint32_t t; + for (t = 0; t <= max_ms; t += 100) { + if (ck_phy_link_read(c->rio, c->phy_addr, l) == 0 && l->link && l->aneg_done) + return 1; + if (t < max_ms) + c->rio->delay_us(c->rio->ctx, 100000u); + } + return 0; +} + +static uint32_t canary_changed(volatile const uint8_t *p, uint32_t n, uint8_t pat, uint32_t *first) +{ + uint32_t i, c = 0; + *first = 0xffffffffu; + for (i = 0; i < n; i++) + if (p[i] != pat) { + if (c == 0) + *first = i; + c++; + } + return c; +} + +void ck_pi5gem_run(const struct ck_pi5gem_ctx *c) +{ + struct ck_gem g; + struct ck_gem_xfer x; + struct ck_phy_link lk; + uint8_t txf[64], tpa[4] = {192, 168, 1, 1}; + uint16_t aux0 = 0, clk0 = 0, bmcr0 = 0, bmsr_lb = 0; + int rc, link_up, llb_ok = 0, phylb_ok = 0, ok; + uint32_t n; + volatile uint8_t *va = (volatile uint8_t *)c->bounce; + + ck_printf("pi5_gem: cut 3: one frame through the RP1 GEM with descriptors only inside the bounce window; software-only DMA confinement (Pi 5, weaker than the Spark)\n"); + if (!c->hw_window_confined) { + ck_printf("pi5_gem: STOP root-complex inbound window is not CONFINED; bus-master stays off, no GEM DMA\n"); + return; + } + ck_gem_init(&g, c->rio, c->dw, va, glue_bme, (void *)c->rc, glue_barrier, 1); + rc = ck_gem_probe(c->rio, &g.caps); + ck_printf("pi5_gem: caps rc=%d MID=0x%08x DCFG1=0x%08x DCFG2=0x%08x DCFG6=0x%08x gem=%u daw64=%u queue_mask=0x%x queues=%u desc_bytes=%u dbw_code=%u\n", + rc, g.caps.mid, g.caps.dcfg1, g.caps.dcfg2, g.caps.dcfg6, g.caps.is_gem, g.caps.daw64, g.caps.queue_mask, + g.caps.nqueues, g.caps.desc_bytes, g.caps.dbw); + if (rc) { + ck_printf("pi5_gem: STOP not a GEM; nothing programmed\n"); + return; + } + ck_printf("pi5_gem: state before: NCR=0x%08x NCFGR=0x%08x USRIO=0x%08x DMACFG=0x%08x AMP=0x%08x TBQP=0x%08x RBQP=0x%08x eth_cfg STATUS=0x%08x CLKGEN=0x%08x rp1 pci cmd=0x%08x\n", + c->rio->rd(0, CK_RP1_ETH + CK_GEM_NCR), c->rio->rd(0, CK_RP1_ETH + CK_GEM_NCFGR), + c->rio->rd(0, CK_RP1_ETH + CK_GEM_USRIO), c->rio->rd(0, CK_RP1_ETH + CK_GEM_DMACFG), + c->rio->rd(0, CK_RP1_ETH + CK_GEM_AMP), c->rio->rd(0, CK_RP1_ETH + CK_GEM_TBQP), + c->rio->rd(0, CK_RP1_ETH + CK_GEM_RBQP), eth_status(c->rio), c->rio->rd(0, CK_RP1_ETH_CFG + 0x14u), + pci_cmd(c)); + + /* ---- 1. link status first, nothing assumed ---- */ + rc = ck_phy_link_read(c->rio, c->phy_addr, &lk); + print_link("link (first read, read-only)", &lk, eth_status(c->rio)); + link_up = wait_link(c, 6000, &lk); + print_link(link_up ? "link UP" : "link NOT UP after 6000 ms (no cable or no partner, or autoneg not complete)", &lk, eth_status(c->rio)); + ck_printf("pi5_gem: LINK %s\n", link_up ? "FOUND" : "ABSENT"); + + /* ---- PHY RGMII-ID delays as Linux sets them (broadcom.c), then read back ---- */ + rc = ck_phy_rgmii_id_config(c->rio, c->phy_addr, &aux0, &clk0); + { + uint16_t a1 = 0, s1 = 0; + (void)ck_gem_mdio_write(c->rio, c->phy_addr, 0x18u, (uint16_t)(0x0007u | (0x0007u << 12))); + (void)ck_gem_mdio_read(c->rio, c->phy_addr, 0x18u, &a1); + (void)ck_gem_mdio_write(c->rio, c->phy_addr, 0x1cu, (uint16_t)(0x03u << 10)); + (void)ck_gem_mdio_read(c->rio, c->phy_addr, 0x1cu, &s1); + ck_printf("pi5_gem: phy rgmii-id delays rc=%d AUXCTL_MISC before=0x%04x after=0x%04x CLK_CTL before=0x%03x after=0x%03x\n", + rc, aux0, a1, clk0, s1 & 0x3ff); + } + + /* ---- 2. GEM local loopback ---- */ + ck_gem_frame_build(CK_GEMF_RAW, k_mac, 1, tpa, txf, sizeof txf); + x = (struct ck_gem_xfer){0}; + x.mode = CK_GEMX_LLB; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 2; x.rx_buf_len = 1536; + x.tx_wait_us = 20000; x.rx_wait_us = 20000; + for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; + rc = ck_gem_xfer(&g, &x); + print_xfer("loopback gem-local(NCR.LLB) seq=1", rc, &x, txf); + llb_ok = rc == 0 && x.tx_done && x.rx_done && x.rx_frames == 1 && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 && + x.rx_len == 60; + ck_printf("pi5_gem: LOOPBACK gem-local %s (sent seq=1 60 bytes, received %u bytes, byte compare %s)\n", verdict(llb_ok), x.rx_len, + x.rx_done && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 ? "identical" : "different or none"); + + /* ---- 3. PHY loopback (BMCR bit 14) ---- */ + ck_gem_frame_build(CK_GEMF_RAW, k_mac, 2, tpa, txf, sizeof txf); + rc = ck_phy_loopback_enter(c->rio, c->phy_addr, 1000, &bmcr0, &bmsr_lb); + ck_printf("pi5_gem: phy loopback enter rc=%d BMCR_was=0x%04x BMSR_now=0x%04x (link in loopback=%u)\n", rc, bmcr0, bmsr_lb, + (bmsr_lb & CK_MII_BMSR_LSTATUS) != 0); + x = (struct ck_gem_xfer){0}; + x.mode = CK_GEMX_PHYLB; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 2; x.rx_buf_len = 1536; + x.tx_wait_us = 20000; x.rx_wait_us = 50000; + for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; + rc = ck_gem_xfer(&g, &x); + print_xfer("loopback phy(BMCR.LOOPBACK) seq=2", rc, &x, txf); + phylb_ok = rc == 0 && x.tx_done && x.rx_done && x.rx_frames == 1 && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 && + x.rx_len == 60; + ck_printf("pi5_gem: LOOPBACK phy %s (sent seq=2 60 bytes, received %u bytes, byte compare %s)\n", verdict(phylb_ok), x.rx_len, + x.rx_done && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 ? "identical" : "different or none"); + rc = ck_phy_loopback_exit(c->rio, c->phy_addr, bmcr0); + ck_printf("pi5_gem: phy loopback exit rc=%d BMCR restored to 0x%04x\n", rc, (uint16_t)(bmcr0 & ~CK_MII_BMCR_LOOPBACK)); + + /* ---- 4. one frame on the wire, only if a link exists ---- */ + if (link_up) { + struct ck_phy_link l2; + uint32_t speed, fd, es = 0; + int el = 0, efd = 0; + link_up = wait_link(c, 8000, &l2); + print_link(link_up ? "link after loopback (UP)" : "link after loopback (NOT UP)", &l2, eth_status(c->rio)); + if (link_up) { + speed = l2.speed; + fd = (uint32_t)l2.full_duplex; + ck_eth_cfg_status_decode(eth_status(c->rio), &el, &es, &efd); + if (!speed) { speed = es; fd = (uint32_t)efd; } + ck_gem_frame_build(CK_GEMF_ARP, k_mac, 3, tpa, txf, sizeof txf); + x = (struct ck_gem_xfer){0}; + x.mode = CK_GEMX_WIRE; x.speed = speed ? speed : 100; x.full_duplex = (int)fd; x.frame = txf; x.frame_len = 60; + x.rx_count = 3; x.rx_buf_len = 1536; x.tx_wait_us = 50000; x.rx_wait_us = 400000; + for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; + rc = ck_gem_xfer(&g, &x); + print_xfer("wire arp-probe 192.168.1.1 seq=3", rc, &x, txf); + ck_printf("pi5_gem: WIRE tx %s (ARP probe 60 bytes at %u Mbit %s duplex, TX complete=%u, tx_ctrl error bits=0x%08x)\n", + verdict(rc == 0 && x.tx_done && !(x.tx_ctrl & (CK_GEMD_TX_ERROR | CK_GEMD_TX_UNDERRUN | CK_GEMD_TX_BUF_EXHAUSTED))), + x.speed, x.full_duplex ? "full" : "half", x.tx_done, x.tx_ctrl & (CK_GEMD_TX_ERROR | CK_GEMD_TX_UNDERRUN | CK_GEMD_TX_BUF_EXHAUSTED)); + if (x.rx_done) { + ck_printf("pi5_gem: WIRE rx (bonus) frames=%u first: len=%u dst=%02x:%02x:%02x:%02x:%02x:%02x src=%02x:%02x:%02x:%02x:%02x:%02x type=%02x%02x\n", + x.rx_frames, x.rx_len, x.rx_data[0], x.rx_data[1], x.rx_data[2], x.rx_data[3], x.rx_data[4], x.rx_data[5], + x.rx_data[6], x.rx_data[7], x.rx_data[8], x.rx_data[9], x.rx_data[10], x.rx_data[11], x.rx_data[12], x.rx_data[13]); + } else { + ck_printf("pi5_gem: WIRE rx (bonus) none within 400 ms\n"); + } + } + } else { + ck_printf("pi5_gem: WIRE SKIPPED (no link partner found)\n"); + } + + /* ---- 5. the confinement proof (ADR 0020 test 5): descriptors deliberately OUTSIDE the window ---- */ + { + int mode = llb_ok ? CK_GEMX_LLB : phylb_ok ? CK_GEMX_PHYLB : 0; + uint64_t cphys = 0, cphys2 = 0; + volatile uint8_t *can = 0, *can2 = 0; + struct ck_dmaw scratch = *c->dw; + int vr; + if (!mode) { + ck_printf("pi5_gem: CONFINEMENT SKIPPED: neither loopback delivered a frame, so there is no frame to push at an outside address; UNVERIFIED\n"); + } else { + if (mode == CK_GEMX_PHYLB) { + rc = ck_phy_loopback_enter(c->rio, c->phy_addr, 1000, &bmcr0, &bmsr_lb); + ck_printf("pi5_gem: confinement: phy loopback re-entered rc=%d\n", rc); + } + can = (volatile uint8_t *)ck_dma_alloc(4096, 4096, &cphys); + can2 = (volatile uint8_t *)ck_dma_alloc(4096, 4096, &cphys2); + ok = can && can2 && cphys && cphys2 && cphys != cphys2 && + (cphys + 4096 <= c->bounce_phys || cphys >= c->bounce_phys + c->dw->size) && + (cphys2 + 4096 <= c->bounce_phys || cphys2 >= c->bounce_phys + c->dw->size); + if (!ok) { + ck_printf("pi5_gem: CONFINEMENT SKIPPED: could not get two private pages outside the window (can=%llx can2=%llx); UNVERIFIED\n", + (unsigned long long)cphys, (unsigned long long)cphys2); + } else { + uint32_t i, changed, first; + for (i = 0; i < 4096; i++) { can[i] = 0xa5; can2[i] = 0; } + vr = ck_dmaw_validate(&scratch, cphys, 64); + ck_printf("pi5_gem: confinement setup: canary page cpu=0x%llx (outside window 0x%llx..+0x%llx); validator on a scratch copy of the window says %d (2 = E_RANGE, refused) for a 64 byte buffer there\n", + (unsigned long long)cphys, (unsigned long long)c->bounce_phys, (unsigned long long)c->dw->size, vr); + if (vr != CK_DMAW_E_RANGE) { + ck_printf("pi5_gem: CONFINEMENT STOP validator did not refuse the outside address; nothing run\n"); + } else { + /* A. device WRITE outside: RX buffer 64 bytes, one slot, the page is ours and bounded at 64 bytes */ + ck_gem_frame_build(CK_GEMF_RAW, k_mac, 4, tpa, txf, sizeof txf); + x = (struct ck_gem_xfer){0}; + x.mode = mode; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 1; x.rx_buf_len = 64; + x.tx_wait_us = 20000; x.rx_wait_us = 50000; x.raw_rx_outside = cphys; + for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; + ck_printf("pi5_gem: CONFINEMENT A: the RX descriptor points at 0x%llx outside the window, buffer size 64 bytes (the device can write at most 64 bytes there)\n", + (unsigned long long)cphys); + rc = ck_gem_xfer(&g, &x); + print_xfer("confinement A (rx write outside) seq=4", rc, &x, txf); + changed = canary_changed(can, 4096, 0xa5, &first); + ck_printf("pi5_gem: CONFINEMENT A result: canary bytes changed=%u first_changed_offset=%d device_says_rx_done=%u hresp=%u\n", + changed, (int)first, x.rx_done, (x.isr & CK_GEM_ISR_HRESP) != 0); + ck_printf("pi5_gem: CONFINEMENT A %s\n", changed == 0 ? "BLOCKED: the write did not reach the outside page" : + "NOT BLOCKED: the device wrote outside the window (hardware backstop NOT effective)"); + /* B. device READ outside: TX descriptor points at a page holding a different frame */ + ck_gem_frame_build(CK_GEMF_RAW, k_mac, 0xdead0005u, tpa, (uint8_t *)can2, 64); + ck_gem_frame_build(CK_GEMF_RAW, k_mac, 5, tpa, txf, sizeof txf); + x = (struct ck_gem_xfer){0}; + x.mode = mode; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 1; x.rx_buf_len = 1536; + x.tx_wait_us = 20000; x.rx_wait_us = 50000; x.raw_tx_outside = cphys2; + for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; + ck_printf("pi5_gem: CONFINEMENT B: the TX descriptor points at 0x%llx outside the window, which holds a different frame (marker seq=0xdead0005)\n", + (unsigned long long)cphys2); + rc = ck_gem_xfer(&g, &x); + print_xfer("confinement B (tx read outside) seq=5", rc, &x, txf); + { + uint8_t outf[64]; + uint32_t dm = 1, dt = 1; + ck_gem_frame_build(CK_GEMF_RAW, k_mac, 0xdead0005u, tpa, outf, sizeof outf); + if (x.rx_done) { + dm = ck_gem_frame_diff(outf, 60, x.rx_data, x.rx_copied); + dt = ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied); + } + ck_printf("pi5_gem: CONFINEMENT B result: rx_done=%u diff_vs_outside_marker=%u diff_vs_inside_frame=%u tsr_bex=%u hresp=%u\n", + x.rx_done, x.rx_done ? dm : 0xffffffffu, x.rx_done ? dt : 0xffffffffu, (x.tsr & CK_GEM_TSR_BEX) != 0, + (x.isr & CK_GEM_ISR_HRESP) != 0); + ck_printf("pi5_gem: CONFINEMENT B %s\n", (x.rx_done && dm == 0) ? "NOT BLOCKED: the device read the outside page (hardware backstop NOT effective for reads)" : + "BLOCKED: the outside page was not delivered as the frame"); + } + } + } + } + if (mode == CK_GEMX_PHYLB) { + rc = ck_phy_loopback_exit(c->rio, c->phy_addr, bmcr0); + ck_printf("pi5_gem: phy loopback exit rc=%d\n", rc); + } + } + + ck_gem_park(&g); + ck_printf("pi5_gem: done: GEM RE/TE/LLB off, halted=%u bme_on=%u rp1 pci cmd=0x%08x NCR=0x%08x\n", g.halted, g.bme_on, + pci_cmd(c), c->rio->rd(0, CK_RP1_ETH + CK_GEM_NCR)); +} +#endif diff --git a/native/kernel/dev/pi5_gem.h b/native/kernel/dev/pi5_gem.h new file mode 100644 index 00000000..c997e9fa --- /dev/null +++ b/native/kernel/dev/pi5_gem.h @@ -0,0 +1,288 @@ +/* pi5_gem.h -- Raspberry Pi 5 network lane, cut 3: the RP1 Ethernet MAC (Cadence GEM_GXL) sends and + * receives ONE frame through DMA descriptors that live only inside the ADR 0020 bounce window and pass the + * dma_window.c validator, plus the PHY link/loopback helpers the proof needs. + * + * LABEL: software-only DMA confinement (Pi 5, weaker than the Spark). Pi-only; not linked into the Spark or QEMU. + * + * Hosted-testable over fake registers and a fake GEM (tests/test_pi5_gem.c, with mutants). A host test proves + * the decisions and the ORDER of register writes, NOT the hardware. The MMIO glue at the bottom of pi5_gem.c + * is the CK_PI5_NET image only. Every number below cites its source; anything not read is UNVERIFIED. + * + * SOURCES (all read in this cut; Linux = raspberrypi/linux branch rpi-6.12.y): + * [M1] drivers/net/ethernet/cadence/macb.h: register offsets (MACB_NCR/NCFGR/NSR/TSR/RBQP/TBQP/RSR/ISR/IDR, + * GEM_DMACFG/USRIO/PBUFRXCUT/AMP/HRB/HRT/SA1B..SA4T/DCFG1/DCFG2/DCFG6, GEM_ISR/TBQP/RBQP/RBQS/IDR(hw_q), + * GEM_TBQPH/RBQPH), bit fields (NCR LLB/RE/TE/MPE/CLRSTAT/TSTART, NCFGR SPD/FD/GBE/DBW/DRFCS/CLK, DMACFG + * FBLDO/RXBMS/TXPBMS/TXCOEN/RXBS/ADDR64/RXEXT/TXEXT, TSR UBR/TGO/BEX/COMP/UND, RSR BNA/REC/OVR, DCFG6 DAW64), + * descriptor layout (struct macb_dma_desc addr/ctrl, macb_dma_desc_64 addrh/resvd; RX USED/WRAP/FRMLEN/SOF/EOF, + * TX FRMLEN(14 bits on GEM)/LAST/NOCRC/BUF_EXHAUSTED/UNDERRUN/ERROR/WRAP/USED). + * [M2] drivers/net/ethernet/cadence/macb_main.c: macb_reset_hw, macb_init_hw, macb_configure_dma, gem_init_axi, + * macb_set_hwaddr, macb_init_buffers (per-queue RBQP/TBQP, shared RBQPH/TBQPH), macb_probe_queues (queue mask + * from DCFG6), macb_dbw (DCFG1.DBWDEF), macb_init (USRIO rgmii), macb_mac_link_up (SPD/FD/GBE, then RE|TE), + * macb_set_addr / gem_rx_refill (RX address word: USED clears last, WRAP on the last slot), macb_tx_map (TX ctrl + * written last, USED clear hands the slot over), raspberrypi_rp1_config (dma_burst_length 16, jumbo caps), + * macb_init_tieoff (unused RX queue: WRAP|USED descriptor), rp1.dtsi rp1_eth (phy-mode rgmii-id, + * cdns,aw2w-max-pipe/ar2r-max-pipe 8, use-aw2b-fill), bcm2712-rpi-5-b.dts (phy reg 1). + * [P1] drivers/net/phy/broadcom.c bcm54xx_config_clock_delay (RGMII-ID: AUXCTL misc shadow skew enable, shadow 0x03 + * GTXCLK_EN), drivers/net/phy/bcm-phy-lib.c bcm54xx_auxctl_read/write, bcm_phy_read/write_shadow, + * include/linux/brcmphy.h shadow numbers, include/uapi/linux/mii.h BMCR/BMSR/ADVERTISE/CTRL1000/STAT1000 bits, + * drivers/net/phy/phy_device.c genphy_loopback (BMCR LOOPBACK + fixed speed/duplex, then wait BMSR link). + * [D1] RP1 peripherals datasheet RP-008370-DS-1 chapter 7: GEM_GXL (64-bit address DMA, internal loopback, RGMII), + * ETH_CFG STATUS (RGMII link/speed/duplex), CLKGEN (tx clock follows the MAC speed). + * [I1] IEEE 802.3 clause 22 (MII registers, BMCR loopback bit 14), IEEE 802 Local Experimental Ethertype 1 = 0x88b5. + * Not read: the Cadence GEM_GXL user guide and the BCM54213PE datasheet (not public). Anything only they would + * say (what exactly local loopback does at the RGMII pins) is UNVERIFIED and is measured by the Pi run. */ +#ifndef AIENOS_CK_PI5_GEM_H +#define AIENOS_CK_PI5_GEM_H +#include +#include +#include "pi5_net.h" +#include "dma_window.h" + +enum { + CK_GEM_OK = 0, + CK_GEM_E_ARG = -1, + CK_GEM_E_HALTED = -2, /* latched earlier: no more DMA until reboot */ + CK_GEM_E_WINDOW = -3, /* the validator refused a descriptor or buffer (window latched, BME never enabled) */ + CK_GEM_E_HWWIN = -4, /* the root-complex window is not confirmed confined: refuse to enable bus-master */ + CK_GEM_E_BME = -5, /* bus-master enable/disable did not read back */ + CK_GEM_E_QUIESCE = -6, /* the transmitter did not stop (halted) */ + CK_GEM_E_DEVICE = -7, /* the device reported an impossible index or length (window latched) */ + CK_GEM_E_NOGEM = -8, /* MID says this is not a GEM */ + CK_GEM_E_TIMEOUT = -9, + CK_GEM_E_READBACK = -10 +}; + +/* ============ GEM registers: offset from the GEM base (CK_RP1_ETH in BAR1) [M1] ============ */ +#define CK_GEM_USRIO 0x00cu +#define CK_GEM_DMACFG 0x010u +#define CK_GEM_TSR 0x014u +#define CK_GEM_RBQP 0x018u +#define CK_GEM_TBQP 0x01cu +#define CK_GEM_RSR 0x020u +#define CK_GEM_ISR 0x024u +#define CK_GEM_IDR 0x02cu +#define CK_GEM_PBUFRXCUT 0x044u +#define CK_GEM_AMP 0x054u +#define CK_GEM_HRB 0x080u +#define CK_GEM_HRT 0x084u +#define CK_GEM_SA1B 0x088u +#define CK_GEM_SA1T 0x08cu +#define CK_GEM_SA2B 0x090u /* SA2B..SA4T = 0x90..0xa4 */ +#define CK_GEM_DCFG1 0x280u +#define CK_GEM_DCFG2 0x284u +#define CK_GEM_DCFG6 0x294u +#define CK_GEM_TBQPH 0x4c8u /* shared by every queue */ +#define CK_GEM_RBQPH 0x4d4u +/* 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 */ +#define CK_GEM_QN_TBQP(hwq) (0x440u + (((hwq) - 1u) << 2)) +#define CK_GEM_QN_RBQP(hwq) (0x480u + (((hwq) - 1u) << 2)) +#define CK_GEM_QN_RBQS(hwq) (0x4a0u + (((hwq) - 1u) << 2)) +#define CK_GEM_QN_IDR(hwq) (0x620u + (((hwq) - 1u) << 2)) +#define CK_GEM_QN_ISR(hwq) (0x400u + (((hwq) - 1u) << 2)) + +/* NCR (CK_GEM_NCR_MPE is in pi5_net.h) */ +#define CK_GEM_NCR_LLB (1u << 1) +#define CK_GEM_NCR_RE (1u << 2) +#define CK_GEM_NCR_TE (1u << 3) +#define CK_GEM_NCR_CLRSTAT (1u << 5) +#define CK_GEM_NCR_TSTART (1u << 9) +/* NCFGR (the MDC divider CK_GEM_NCFGR_CLK_* is in pi5_net.h) */ +#define CK_GEM_NCFGR_SPD (1u << 0) /* 100 Mbit when set (GBE clear) */ +#define CK_GEM_NCFGR_FD (1u << 1) +#define CK_GEM_NCFGR_CAF (1u << 4) +#define CK_GEM_NCFGR_NBC (1u << 5) +#define CK_GEM_NCFGR_GBE (1u << 10) +#define CK_GEM_NCFGR_DRFCS (1u << 17) +#define CK_GEM_NCFGR_DBW_SHIFT 21 +#define CK_GEM_NCFGR_DBW_MASK (3u << 21) +/* DMACFG */ +#define CK_GEM_DMACFG_FBLDO_MASK 0x1fu +#define CK_GEM_DMACFG_ENDIA_DESC (1u << 6) +#define CK_GEM_DMACFG_ENDIA_PKT (1u << 7) +#define CK_GEM_DMACFG_RXBMS_MASK (3u << 8) +#define CK_GEM_DMACFG_TXPBMS (1u << 10) +#define CK_GEM_DMACFG_TXCOEN (1u << 11) +#define CK_GEM_DMACFG_RXBS_SHIFT 16 +#define CK_GEM_DMACFG_RXBS_MASK (0xffu << 16) +#define CK_GEM_DMACFG_RXEXT (1u << 28) +#define CK_GEM_DMACFG_TXEXT (1u << 29) +#define CK_GEM_DMACFG_ADDR64 (1u << 30) +#define CK_GEM_BURST 16u /* macb dma_burst_length for raspberrypi_rp1_config */ +#define CK_GEM_RXBS_UNIT 64u /* RX_BUFFER_MULTIPLE */ +/* TSR / RSR / ISR */ +#define CK_GEM_TSR_UBR (1u << 0) +#define CK_GEM_TSR_RLE (1u << 2) +#define CK_GEM_TSR_TGO (1u << 3) +#define CK_GEM_TSR_BEX (1u << 4) +#define CK_GEM_TSR_COMP (1u << 5) +#define CK_GEM_TSR_UND (1u << 6) +#define CK_GEM_RSR_BNA (1u << 0) +#define CK_GEM_RSR_REC (1u << 1) +#define CK_GEM_RSR_OVR (1u << 2) +#define CK_GEM_ISR_HRESP (1u << 11) +/* USRIO */ +#define CK_GEM_USRIO_RGMII (1u << 0) +/* AMP */ +#define CK_GEM_AMP_AR2R_MASK 0xffu +#define CK_GEM_AMP_AW2W_SHIFT 8 +#define CK_GEM_AMP_AW2W_MASK (0xffu << 8) +#define CK_GEM_AMP_AW2B_FILL (1u << 16) +#define CK_GEM_AXI_PIPE 8u /* rp1.dtsi cdns,aw2w-max-pipe = <8>, cdns,ar2r-max-pipe = <8> */ +/* DCFG */ +#define CK_GEM_DCFG1_DBWDEF_SHIFT 25 +#define CK_GEM_DCFG6_DAW64 (1u << 23) +#define CK_GEM_MID_IDNUM_SHIFT 16 + +/* Descriptors [M1]: word0 = address (RX: bits 31:2, bit1 WRAP, bit0 USED), word1 = ctrl, then (64-bit address + * mode) word2 = address high, word3 reserved. */ +#define CK_GEMD_RX_USED (1u << 0) +#define CK_GEMD_RX_WRAP (1u << 1) +#define CK_GEMD_RX_FRMLEN_MASK 0xfffu +#define CK_GEMD_RX_SOF (1u << 14) +#define CK_GEMD_RX_EOF (1u << 15) +#define CK_GEMD_TX_FRMLEN_MASK 0x3fffu /* GEM_TX_FRMLEN_SIZE 14 */ +#define CK_GEMD_TX_LAST (1u << 15) +#define CK_GEMD_TX_NOCRC (1u << 16) +#define CK_GEMD_TX_BUF_EXHAUSTED (1u << 27) +#define CK_GEMD_TX_UNDERRUN (1u << 28) +#define CK_GEMD_TX_ERROR (1u << 29) +#define CK_GEMD_TX_WRAP (1u << 30) +#define CK_GEMD_TX_USED (1u << 31) + +#define CK_GEM_MAX_QUEUES 8u +#define CK_GEM_TXRING 2u /* slot 0 = the frame, slot 1 = a USED|WRAP sentinel */ +#define CK_GEM_RXRING 4u /* power of two for the ring helper; unused slots are USED sentinels */ +#define CK_GEM_RXCOPY 256u + +/* What the hardware says it is (read from the registers, never assumed). */ +struct ck_gem_caps { + uint32_t mid, dcfg1, dcfg2, dcfg6; + int is_gem; /* MID IDNUM >= 2 (hw_is_gem) */ + int daw64; /* DCFG6 bit 23 */ + uint32_t queue_mask; /* bit q set = hardware queue q exists; bit 0 always */ + uint32_t nqueues; + uint32_t desc_bytes; /* 16 with 64-bit descriptors, else 8 */ + uint32_t dbw; /* NCFGR DBW field value (macb_dbw) */ +}; +int ck_gem_probe(const struct ck_rp1_io *io, struct ck_gem_caps *c); + +/* The driver context. All memory the device may touch is the bounce window `w`, mapped at `va`. */ +struct ck_gem { + const struct ck_rp1_io *rio; + struct ck_gem_caps caps; + struct ck_dmaw *w; /* the ADR 0020 validator and allocator */ + volatile uint8_t *va; /* CPU mapping of the window; bus address w->base */ + int (*bme)(void *ctx, int on); /* RP1 PCI bus-master enable: 0 = done AND read back */ + void *bme_ctx; + void (*barrier)(void); /* data barrier between descriptor stores and the register kick */ + int hw_window_ok; /* the root-complex window read back CONFINED (cut 2 verdict); else no bus-master */ + int halted; /* latched until reboot */ + int bme_on; + uint32_t xfers; +}; +void ck_gem_init(struct ck_gem *g, const struct ck_rp1_io *rio, struct ck_dmaw *w, volatile uint8_t *va, + int (*bme)(void *, int), void *bme_ctx, void (*barrier)(void), int hw_window_ok); + +enum { CK_GEMX_LLB = 1, CK_GEMX_PHYLB = 2, CK_GEMX_WIRE = 3 }; + +struct ck_gem_xfer { + /* in */ + int mode; /* LLB: NCR.LLB; PHYLB/WIRE: GEM normal (the PHY loops or the wire does) */ + uint32_t speed; /* 1000, 100 or 10 */ + int full_duplex; + uint8_t mac[6]; + const uint8_t *frame; /* sent as given (the MAC appends the FCS) */ + uint32_t frame_len; /* 60..1514 */ + uint32_t rx_count; /* real RX buffers, 0..3 (0 = TX only); the rest are USED sentinels */ + uint32_t rx_buf_len; /* multiple of 64, 64..1536 */ + uint32_t tx_wait_us, rx_wait_us; + uint64_t tx_addr, rx_addr; /* 0 = allocate inside the window; non-zero = caller-chosen, STILL validated */ + /* Confinement test ONLY (ADR 0020 test 5). After the validated slot is written, the hardware-visible address + * word of TX slot 0 / RX slot 0 is replaced by this raw address. The shadow still holds the valid one. + * Callers must have proven the target is a page they own and that a write there is harmless. */ + uint64_t raw_tx_outside, raw_rx_outside; + /* out */ + int tx_done, rx_done; + uint32_t tx_ctrl, rx_word0, rx_ctrl, rx_len; + uint32_t tsr, rsr, isr, ncr_run, ncfgr_run, dmacfg_run, ncr_end, tsr_end; + uint64_t tx_bus, rx_bus; /* where the buffers were */ + uint8_t rx_data[CK_GEM_RXCOPY]; + uint32_t rx_copied; + uint32_t rx_frames; /* RX slots the device handed back (USED, SOF and EOF) */ + uint32_t rx_slot; /* the first such slot; rx_data/rx_len/rx_ctrl/rx_word0 are its */ + int bme_enabled_for_us; /* 1 once bus-master was turned on for this transfer */ + int bme_off_at_end; +}; +/* One transfer: validate everything, THEN program the GEM, THEN enable RP1 bus-master, kick, poll, + * quiesce (RE/TE off, wait TGO clear, 2 ms), THEN disable bus-master and read the results. 0 = the procedure + * ran and the device is stopped (tx_done/rx_done say what happened); negative = refused or failed. */ +int ck_gem_xfer(struct ck_gem *g, struct ck_gem_xfer *x); +/* Stop the MAC (RE/TE/LLB off, MPE kept) with bus-master already off; for the end of a run. */ +void ck_gem_park(struct ck_gem *g); + +/* ============ frames ============ */ +enum { CK_GEMF_RAW = 1, CK_GEMF_ARP = 2 }; +#define CK_GEMF_ETHERTYPE_EXP 0x88b5u +/* A 60 byte frame (MAC pads nothing, appends FCS). Raw: broadcast, EtherType 0x88b5, magic, sequence number, + * pattern. ARP: broadcast ARP probe (sender IP 0.0.0.0) for tpa, trailer carries magic + sequence number. */ +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); +/* 0 = identical; else 1 + index of the first difference (a length difference counts at min(len)). */ +uint32_t ck_gem_frame_diff(const uint8_t *a, uint32_t alen, const uint8_t *b, uint32_t blen); + +/* ============ PHY (clause 22) ============ */ +int ck_gem_mdio_write(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t val); +uint32_t ck_gem_mdio_frame_write(uint32_t phy, uint32_t reg, uint16_t val); +#define CK_MII_BMCR 0u +#define CK_MII_BMSR 1u +#define CK_MII_ADVERTISE 4u +#define CK_MII_LPA 5u +#define CK_MII_CTRL1000 9u +#define CK_MII_STAT1000 10u +#define CK_MII_BMCR_SPEED1000 0x0040u +#define CK_MII_BMCR_FULLDPLX 0x0100u +#define CK_MII_BMCR_ANRESTART 0x0200u +#define CK_MII_BMCR_ANENABLE 0x1000u +#define CK_MII_BMCR_SPEED100 0x2000u +#define CK_MII_BMCR_LOOPBACK 0x4000u +#define CK_MII_BMSR_LSTATUS 0x0004u +#define CK_MII_BMSR_ANEGCOMPLETE 0x0020u + +struct ck_phy_link { + uint16_t bmcr, bmsr_first, bmsr, adv, lpa, ctrl1000, stat1000; + int link; /* BMSR link status (read twice: the bit is latched low) */ + int aneg_done; + uint32_t speed; /* resolved 1000/100/10; 0 = unresolved */ + int full_duplex; +}; +/* Read the registers and resolve speed/duplex from the advertisement and partner ability (IEEE 802.3 clause 28/40; + * the same resolution as Linux genphy_read_status). Does not write. */ +int ck_phy_link_read(const struct ck_rp1_io *io, uint32_t phy, struct ck_phy_link *l); +/* ETH_CFG STATUS decode [D1]: bit0 link, bits2:1 speed (0=10,1=100,2=1000), bit3 duplex. speed 0 if reserved. */ +void ck_eth_cfg_status_decode(uint32_t st, int *link, uint32_t *speed, int *full_duplex); +/* Linux bcm54xx_config_clock_delay for RGMII-ID [P1]. before/after = the two registers read before the write. */ +int ck_phy_rgmii_id_config(const struct ck_rp1_io *io, uint32_t phy, uint16_t *auxctl_before, uint16_t *clk_before); +/* genphy_loopback [P1]: BMCR = LOOPBACK | duplex | speed, then wait (bounded) for BMSR link. *bmcr_saved = BMCR found. */ +int ck_phy_loopback_enter(const struct ck_rp1_io *io, uint32_t phy, uint32_t speed, uint16_t *bmcr_saved, + uint16_t *bmsr_out); +/* Leave loopback: BMCR = saved (autoneg restarted if it was enabled). */ +int ck_phy_loopback_exit(const struct ck_rp1_io *io, uint32_t phy, uint16_t bmcr_saved); + +/* RP1 PCI bus-master through the root complex config window (needs a read back). */ +int ck_pi5gem_bme_set(const struct ck_pi5pcie_io *io, int on); + +#ifdef CK_PI5_NET +/* Image glue called by ck_pi5net_run after the PHY id is read. */ +struct ck_pi5gem_ctx { + const struct ck_pi5pcie_io *rc; + const struct ck_rp1_io *rio; + struct ck_dmaw *dw; + void *bounce; + uint64_t bounce_phys; + int hw_window_confined; + uint32_t phy_addr; + uint32_t pclk_hz; +}; +void ck_pi5gem_run(const struct ck_pi5gem_ctx *c); +#endif + +#endif diff --git a/native/kernel/dev/pi5_net.c b/native/kernel/dev/pi5_net.c index 840994fc..4ac4a0ac 100644 --- a/native/kernel/dev/pi5_net.c +++ b/native/kernel/dev/pi5_net.c @@ -676,6 +676,7 @@ int ck_phy_find(const struct ck_rp1_io *io, uint32_t addr, uint32_t tries, uint3 #ifdef CK_PI5_NET #include "ck.h" #include "dma_window.h" +#include "pi5_gem.h" static volatile uint8_t *rc; static volatile uint8_t *rp1; @@ -725,14 +726,17 @@ static uint32_t unmodelled_windows(void) return u; } -static void print_verdict(const char *tag, const struct ck_pi5pcie_ibwin *win, uint32_t unmod, uint64_t b_phys, +static int print_verdict(const char *tag, const struct ck_pi5pcie_ibwin *win, uint32_t unmod, uint64_t b_phys, uint64_t b_size, const struct ck_pi5net_ibprog *plan) { struct ck_pi5pcie_dmav dv; + int label; ck_pi5pcie_ib_verdict(win, CK_PI5PCIE_IB_WINDOWS, unmod, b_phys, b_size, &dv); + label = ck_pi5net_confine_label(&dv, plan); ck_printf("pi5_dma: hw_window %s=%s [%s] reach=0x%llx inside=0x%llx outside=0x%llx\n", tag, - ck_pi5net_confine_name(ck_pi5net_confine_label(&dv, plan)), ck_pi5pcie_verdict_name(dv.verdict), (unsigned long long)dv.reach_bytes, + ck_pi5net_confine_name(label), ck_pi5pcie_verdict_name(dv.verdict), (unsigned long long)dv.reach_bytes, (unsigned long long)dv.inside_bytes, (unsigned long long)dv.outside_bytes); + return label; } static void print_clk_rates(const char *tag, const struct ck_rp1_clk_regs *r) @@ -760,10 +764,10 @@ void ck_pi5net_run(void) uint64_t b_phys = 0, bar1_cpu; uint32_t unmod, ndis = 0, attempts = 0; uint16_t cmd_saved = 0; - int rrc, rrp1, ver, rc2, scan_hit = -1; + int rrc, rrp1, ver, rc2, scan_hit = -1, confined = 0; void *bounce; - ck_printf("pi5_net: cut 2: hardware DMA confinement + RP1 Ethernet up to the PHY; no frames, no DMA descriptors\n"); + ck_printf("pi5_net: cut 3: hardware DMA confinement, RP1 Ethernet up to the PHY, then one frame through the GEM (descriptors inside the bounce window only)\n"); ck_printf("pi5_net: software-only DMA confinement (Pi 5, weaker than the Spark) plus the root-complex window below\n"); rc = (volatile uint8_t *)ck_mmio_try_map(CK_PI5PCIE_RC2_PHYS, CK_PI5PCIE_RC_BYTES); if (!rc) { @@ -818,7 +822,7 @@ void ck_pi5net_run(void) } unmod = unmodelled_windows(); print_wins("after", &io, win); - print_verdict("after", win, unmod, b_phys, dw.size, &plan); + confined = print_verdict("after", win, unmod, b_phys, dw.size, &plan) == CK_PI5NET_CONF_CONFINED && rc2 == 0; ck_printf("pi5_dma: rp1 bus-master is LEFT OFF (cmd=0x%08x); the GEM driver turns it on when it needs it\n", cmd_dword(&io)); } @@ -898,6 +902,13 @@ void ck_pi5net_run(void) ck_phy_class_name(pid.cls), attempts); else ck_printf("pi5_net: phy addr=1 NOT FOUND rc=%d attempts=%u scan_lowest_answering=%d\n", rc2, attempts, scan_hit); - ck_printf("pi5_net: done: no frames sent; GEM tx/rx not enabled; rp1 bus-master left off\n"); + if (rc2 == 0 && pid.cls == CK_PHY_BCM54213PE) { + struct ck_pi5gem_ctx gc = {&io, &rio, &dw, bounce, b_phys, confined, 1, ck_rp1_clk_sys_hz(&cr)}; + ck_printf("pi5_net: phy found; handing over to the GEM frame test (hw window confined=%d)\n", confined); + ck_pi5gem_run(&gc); + } else { + ck_printf("pi5_net: no BCM54213PE at addr 1; GEM frame test not run\n"); + } + ck_printf("pi5_net: done: rp1 bus-master off unless the line above says otherwise\n"); } #endif diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 236abf53..1b8b4d88 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T23:07:22Z", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:b6bc9b845fa67108c0bdac73dc640d2a634f8606e833703096f3746df1c23257", + "digest": "sha256:e3169d29c0846819ae597b2674f496724638f1456c3c36d7f72d736240211262", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 163, - "generated_at_commit": "53de55c5dd3485948cd34734caf0f646e28a68c4", - "source_tree": "sha256:23e3a72107864253e896e995535687b6c314beb5420084129c66effb66d97b6b" + "files": 164, + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:a97fb66aea5aeb8b78d181e3220140dba721bc386f66283124e88ca1ebc1bfcd" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_pi5_gem.c b/native/kernel/tests/test_pi5_gem.c new file mode 100644 index 00000000..0c0234f5 --- /dev/null +++ b/native/kernel/tests/test_pi5_gem.c @@ -0,0 +1,745 @@ +/* test_pi5_gem.c -- Pi 5 network lane cut 3 (dev/pi5_gem.c) over a FAKE GEM that behaves like the Cadence + * descriptor DMA and checks every access it makes: any DMA address outside the bounce window, any DMA while RP1 + * bus-master is off, bus-master dropped while the transmitter still runs, malformed rings, missing tie-offs. + * Hosted: this proves the decisions and the ORDER of register writes, NOT the hardware. Run under ASan/UBSan + * and with each CK_GEM_MUTANT_* (make pi5-gem-mutants); every mutant must make it FAIL. */ +#include +#include +#include +#include "ck_test.h" +#include "pi5_gem.h" + +#define WBASE 0x4400000ull +#define WSIZE 0x200000ull +#define OUTBASE 0x6000000ull /* a pretend page outside the window */ + +struct fk { + uint8_t *mem; + uint8_t outside[4096]; + uint32_t r[0x800 / 4]; + uint32_t tbqp_q[8], rbqp_q[8]; + uint16_t phy[32], shadow[32]; + uint32_t shd_sel; + uint32_t dcfg1, dcfg6; + int daw64; + int tgo_delay_cfg, tgo_hold, tgo_cleared_seen; + int tx_completes, rx_len_lie, expect_gbe; + /* observations */ + int bme, bme_on_n, bme_off_n, bme_fail_on, bme_fail_off; + int barrier_n, barrier_at_kick, kicks; + int dma_without_bme, outside_access, outside_writes; + int frames_sent, frames_rcvd; + uint8_t last_tx[1600]; + uint32_t last_tx_len; + int bad_desc_mode, queue_ptr_outside, rx_nowrap, tx_runaway, tx_no_last, bad_speed; + int bme_off_while_tgo, bme_off_while_te; + int gem_writes; + uint32_t cap_w0, cap_ctrl0, cap_ctrl1, cap_w2; + char ev[64]; + int nev; +}; +static struct fk f; + +static void ev(char c) +{ + if (f.nev < 63) + f.ev[f.nev++] = c; + f.ev[f.nev] = 0; +} +static int inwin(uint64_t a, uint64_t n) { return a >= WBASE && n <= WSIZE && a - WBASE <= WSIZE - n; } +static uint32_t d_rd(uint64_t a) { uint32_t v; memcpy(&v, f.mem + (a - WBASE), 4); return v; } +static void d_wr(uint64_t a, uint32_t v) { memcpy(f.mem + (a - WBASE), &v, 4); } + +static void deliver(const uint8_t *data, uint32_t len) +{ + uint64_t a = ((uint64_t)f.r[CK_GEM_RBQPH / 4] << 32) | f.r[CK_GEM_RBQP / 4]; + uint32_t dsz = f.daw64 ? 16 : 8, i; + if (!(f.r[CK_GEM_NCR / 4] & CK_GEM_NCR_RE)) + return; + if (!f.daw64) + a &= 0xffffffffull; + for (i = 0; i < 8; i++) { + uint32_t w0, ctrl, L; + uint64_t buf; + if (!inwin(a, dsz)) { + f.outside_access++; + return; + } + w0 = d_rd(a); + if (!(w0 & CK_GEMD_RX_USED)) { + buf = (uint64_t)(w0 & ~3u) | (f.daw64 ? (uint64_t)d_rd(a + 8) << 32 : 0); + L = len + ((f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_DRFCS) ? 0u : 4u); + if (inwin(buf, len)) { + memcpy(f.mem + (buf - WBASE), data, len); + } else { + f.outside_access++; + f.outside_writes++; + if (buf >= OUTBASE && buf - OUTBASE + len <= sizeof f.outside) + memcpy(f.outside + (buf - OUTBASE), data, len); + } + ctrl = L | CK_GEMD_RX_SOF | CK_GEMD_RX_EOF | (1u << 31); + if (f.rx_len_lie) + ctrl = 2000u | CK_GEMD_RX_SOF | CK_GEMD_RX_EOF; + d_wr(a + 4, ctrl); + d_wr(a, w0 | CK_GEMD_RX_USED); + f.frames_rcvd++; + f.r[CK_GEM_RSR / 4] |= CK_GEM_RSR_REC; + return; + } + if (w0 & CK_GEMD_RX_WRAP) + return; + a += dsz; + } +} + +static void kick(uint32_t ncr) +{ + uint64_t a; + uint32_t dsz = f.daw64 ? 16 : 8, i, q; + ev('K'); + f.kicks++; + f.barrier_at_kick = f.barrier_n; + if (!f.bme) { + f.dma_without_bme++; + return; + } + if (f.daw64 != !!(f.r[CK_GEM_DMACFG / 4] & CK_GEM_DMACFG_ADDR64)) + f.bad_desc_mode++; + if (f.expect_gbe && !(f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_GBE)) + f.bad_speed++; + for (q = 1; q < 8; q++) { + if (!((f.dcfg6 >> q) & 1u)) + continue; + if (!inwin(f.tbqp_q[q], dsz) || !inwin(f.rbqp_q[q], dsz) || !(d_rd(f.tbqp_q[q] + 4) & CK_GEMD_TX_USED) || + !(d_rd(f.rbqp_q[q]) & CK_GEMD_RX_USED)) + f.queue_ptr_outside++; + } + /* RX ring must wrap within 8 slots */ + a = ((uint64_t)f.r[CK_GEM_RBQPH / 4] << 32) | f.r[CK_GEM_RBQP / 4]; + for (i = 0; i < 8; i++) { + if (!inwin(a, dsz)) + break; + if (d_rd(a) & CK_GEMD_RX_WRAP) + break; + a += dsz; + } + if (i == 8 || !inwin(a, dsz)) + f.rx_nowrap++; + a = ((uint64_t)f.r[CK_GEM_TBQPH / 4] << 32) | f.r[CK_GEM_TBQP / 4]; + for (i = 0; i < 8; i++) { + uint32_t ctrl, len; + uint64_t buf; + uint8_t data[1600]; + if (!inwin(a, dsz)) { + f.outside_access++; + return; + } + ctrl = d_rd(a + 4); + if (i == 0) { + f.cap_w0 = d_rd(a); + f.cap_ctrl0 = ctrl; + f.cap_w2 = f.daw64 ? d_rd(a + 8) : 0; + f.cap_ctrl1 = d_rd(a + dsz + 4); + } + if (ctrl & CK_GEMD_TX_USED) { + f.r[CK_GEM_TSR / 4] |= CK_GEM_TSR_UBR; + return; + } + if (!(ctrl & CK_GEMD_TX_LAST)) { + f.tx_no_last++; + return; + } + len = ctrl & CK_GEMD_TX_FRMLEN_MASK; + buf = d_rd(a) | (f.daw64 ? (uint64_t)d_rd(a + 8) << 32 : 0); + if (len > sizeof data) + len = sizeof data; + if (inwin(buf, len)) { + memcpy(data, f.mem + (buf - WBASE), len); + } else { + f.outside_access++; + memset(data, 0xee, len); + } + f.frames_sent++; + memcpy(f.last_tx, data, len); + f.last_tx_len = len; + if (f.tx_completes) { + d_wr(a + 4, ctrl | CK_GEMD_TX_USED); + f.r[CK_GEM_TSR / 4] |= CK_GEM_TSR_COMP; + } + f.tgo_hold = f.tgo_delay_cfg; + if ((ncr & CK_GEM_NCR_LLB) || (f.phy[0] & CK_MII_BMCR_LOOPBACK)) + deliver(data, len); + if (ctrl & CK_GEMD_TX_WRAP) + a = ((uint64_t)f.r[CK_GEM_TBQPH / 4] << 32) | f.r[CK_GEM_TBQP / 4]; + else + a += dsz; + } + f.tx_runaway++; +} + +static void phy_man(uint32_t v) +{ + uint32_t reg = (v >> 18) & 31u, op = (v >> 28) & 3u, data = v & 0xffffu; + if (op == 2) { /* read */ + if (reg == 0x1c) + f.phy[reg] = (uint16_t)(f.shadow[f.shd_sel] & 0x3ff); + f.r[CK_GEM_MAN / 4] = (v & 0xffff0000u) | f.phy[reg]; + } else if (op == 1) { /* write */ + if (reg == 0x1c) { + if (data & 0x8000u) + f.shadow[(data >> 10) & 31u] = (uint16_t)(data & 0x3ff); + else + f.shd_sel = (data >> 10) & 31u; + } else if (reg == 0x18) { + if ((data & 7u) == 7u && ((data >> 12) & 7u) == 7u) { + /* read-select of MISC: nothing stored */ + } else { + f.phy[reg] = (uint16_t)data; + } + } else { + f.phy[reg] = (uint16_t)data; + } + f.r[CK_GEM_MAN / 4] = v; + } +} + +static uint32_t fk_rd(void *c, uint32_t off) +{ + uint32_t o; + (void)c; + if (off < CK_RP1_ETH || off >= CK_RP1_ETH + 0x800u) + return 0; + o = off - CK_RP1_ETH; + if (o == CK_GEM_NSR) + return CK_GEM_NSR_IDLE; + if (o == CK_GEM_TSR) { + uint32_t v = f.r[CK_GEM_TSR / 4]; + if (f.tgo_hold > 0) { + v |= CK_GEM_TSR_TGO; + f.tgo_hold--; + } else { + f.tgo_cleared_seen = 1; + ev('g'); + } + return v; + } + if (o == CK_GEM_ISR) { + uint32_t v = f.r[o / 4]; + f.r[o / 4] = 0; + return v; + } + if (o == CK_GEM_MID) + return 0x00070109u; + if (o == CK_GEM_DCFG1) + return f.dcfg1; + if (o == CK_GEM_DCFG6) + return f.dcfg6; + return f.r[o / 4]; +} + +static void fk_wr(void *c, uint32_t off, uint32_t v) +{ + uint32_t o; + (void)c; + if (off < CK_RP1_ETH || off >= CK_RP1_ETH + 0x800u) + return; + o = off - CK_RP1_ETH; + if (o == CK_GEM_MAN) { + phy_man(v); + return; + } + f.gem_writes++; + if (o == CK_GEM_NCR) { + uint32_t old = f.r[o / 4], nv = v; + nv &= ~(uint32_t)CK_GEM_NCR_CLRSTAT; + nv &= ~(uint32_t)CK_GEM_NCR_TSTART; + if ((old & (CK_GEM_NCR_RE | CK_GEM_NCR_TE)) && !(nv & (CK_GEM_NCR_RE | CK_GEM_NCR_TE))) + ev('E'); + f.r[o / 4] = nv; + if (v & CK_GEM_NCR_TSTART) + kick(nv); + return; + } + if (o == CK_GEM_TSR || o == CK_GEM_RSR) { + f.r[o / 4] &= ~v; + return; + } + if (o >= 0x440 && o < 0x460) { + f.tbqp_q[((o - 0x440) >> 2) + 1] = v; + return; + } + if (o >= 0x480 && o < 0x4a0) { + f.rbqp_q[((o - 0x480) >> 2) + 1] = v; + return; + } + f.r[o / 4] = v; +} +static struct ck_rp1_io fio = {fk_rd, fk_wr, 0, 0}; + +static int fk_bme(void *c, int on) +{ + (void)c; + if (on) { + if (f.bme_fail_on) + return -1; + f.bme = 1; + f.bme_on_n++; + ev('B'); + } else { + if (f.bme_fail_off) + return -1; + if (f.tgo_hold > 0) + f.bme_off_while_tgo++; + if (f.r[CK_GEM_NCR / 4] & (CK_GEM_NCR_TE | CK_GEM_NCR_RE)) + f.bme_off_while_te++; + f.bme = 0; + f.bme_off_n++; + ev('b'); + } + return 0; +} +static void fk_barrier(void) { f.barrier_n++; } + +static struct ck_dmaw dw; +static struct ck_gem g; + +static void fresh(void) +{ + uint8_t *keep = f.mem; + memset(&f, 0, sizeof f); + f.mem = keep ? keep : calloc(1, WSIZE); + memset(f.mem, 0, WSIZE); + f.dcfg1 = 4u << CK_GEM_DCFG1_DBWDEF_SHIFT; + f.daw64 = 1; + f.dcfg6 = CK_GEM_DCFG6_DAW64 | 0x0eu; /* queues 1..3 exist besides 0 */ + f.tx_completes = 1; + f.tgo_delay_cfg = 50; + f.expect_gbe = 1; + f.r[CK_GEM_NCR / 4] = CK_GEM_NCR_MPE; + f.r[CK_GEM_NCFGR / 4] = 0x00540000u; + f.phy[0] = 0x1140u; + f.phy[1] = 0x796du; + f.phy[2] = 0x600du; + f.phy[3] = 0x84a2u; + f.phy[4] = 0x01e1u; + f.phy[5] = 0x45e1u; + f.phy[9] = 0x0300u; + f.phy[10] = 0x3800u; + f.shadow[3] = 0x0010; + CHECK(ck_dmaw_init(&dw, f.mem, WBASE, WSIZE) == CK_DMAW_OK); + ck_gem_init(&g, &fio, &dw, f.mem, fk_bme, 0, fk_barrier, 1); + CHECK(ck_gem_probe(&fio, &g.caps) == 0); +} + +static const uint8_t MAC[6] = {0x02, 0x41, 0x49, 0x45, 0x4e, 0x03}; + +static void mkx(struct ck_gem_xfer *x, int mode, uint8_t *frame, uint32_t seq) +{ + *x = (struct ck_gem_xfer){0}; + ck_gem_frame_build(CK_GEMF_RAW, MAC, seq, 0, frame, 64); + x->mode = mode; + x->speed = 1000; + x->full_duplex = 1; + memcpy(x->mac, MAC, 6); + x->frame = frame; + x->frame_len = 60; + x->rx_count = 2; + x->rx_buf_len = 1536; + x->tx_wait_us = 20000; + x->rx_wait_us = 20000; +} + +static void clean_flags(void) +{ + CHECK(f.dma_without_bme == 0); + CHECK(f.outside_access == 0); + CHECK(f.bad_desc_mode == 0); + CHECK(f.queue_ptr_outside == 0); + CHECK(f.rx_nowrap == 0); + CHECK(f.tx_runaway == 0); + CHECK(f.tx_no_last == 0); + CHECK(f.bad_speed == 0); + CHECK(f.bme_off_while_tgo == 0); + CHECK(f.bme_off_while_te == 0); +} + +static void t_probe(void) +{ + struct ck_gem_caps c; + fresh(); + CHECK(ck_gem_probe(&fio, &c) == 0); + CHECK(c.is_gem == 1 && c.daw64 == 1 && c.queue_mask == 0x0fu && c.nqueues == 4 && c.desc_bytes == 16 && c.dbw == 2); + f.dcfg6 = 0; /* 32-bit descriptors, one queue, 64-bit bus */ + f.dcfg1 = 2u << CK_GEM_DCFG1_DBWDEF_SHIFT; + CHECK(ck_gem_probe(&fio, &c) == 0); + CHECK(c.daw64 == 0 && c.queue_mask == 1 && c.nqueues == 1 && c.desc_bytes == 8 && c.dbw == 1); +} + +static void t_loopback(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + uint64_t tx_buf; + fresh(); + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == 0); + CHECK(x.tx_done == 1 && x.rx_done == 1 && x.rx_frames == 1 && x.rx_len == 60); + CHECK(ck_gem_frame_diff(fr, 60, x.rx_data, x.rx_copied) == 0); + CHECK(f.frames_sent == 1 && f.frames_rcvd == 1); + CHECK(f.bme_on_n == 1 && f.bme_off_n == 1 && f.bme == 0 && g.bme_on == 0); + CHECK(x.bme_enabled_for_us == 1 && x.bme_off_at_end == 1); + CHECK(strcmp(f.ev, "BKEgb") == 0 || strcmp(f.ev, "BKEgggb") == 0 || (f.ev[0] == 'B' && f.ev[1] == 'K' && f.ev[2] == 'E')); + /* bus-master on first, then the kick, then the engine off, THEN bus-master off */ + CHECK(f.ev[0] == 'B' && f.ev[1] == 'K' && f.ev[2] == 'E' && f.ev[f.nev - 1] == 'b'); + CHECK(f.barrier_at_kick >= 1); + CHECK(!(f.r[CK_GEM_NCR / 4] & (CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_LLB))); + CHECK(f.r[CK_GEM_NCR / 4] & CK_GEM_NCR_MPE); /* MDIO port stays enabled */ + CHECK(!(x.ncr_end & (CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_LLB))); + CHECK(x.ncr_run & CK_GEM_NCR_LLB); + /* the descriptors the device saw: address inside the window, length 60, LAST, owned by the device; sentinel USED|WRAP */ + tx_buf = x.tx_bus; + CHECK(inwin(tx_buf, 64) && f.cap_w0 == (uint32_t)tx_buf && f.cap_w2 == (uint32_t)(tx_buf >> 32)); + CHECK((f.cap_ctrl0 & CK_GEMD_TX_FRMLEN_MASK) == 60 && (f.cap_ctrl0 & CK_GEMD_TX_LAST) && !(f.cap_ctrl0 & CK_GEMD_TX_USED)); + CHECK((f.cap_ctrl1 & CK_GEMD_TX_USED) && (f.cap_ctrl1 & CK_GEMD_TX_WRAP)); + /* DMA configuration: 64-bit descriptors, 1536 byte buffers, burst 16, TXCOEN off */ + CHECK((f.r[CK_GEM_DMACFG / 4] & CK_GEM_DMACFG_ADDR64) && ((f.r[CK_GEM_DMACFG / 4] >> 16) & 0xff) == 24); + CHECK((f.r[CK_GEM_DMACFG / 4] & 0x1f) == 16 && !(f.r[CK_GEM_DMACFG / 4] & CK_GEM_DMACFG_TXCOEN)); + CHECK((f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_GBE) && (f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_FD) && + (f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_DRFCS)); + CHECK(((f.r[CK_GEM_NCFGR / 4] >> 18) & 7u) == 5u); /* MDC divider kept */ + CHECK(f.r[CK_GEM_USRIO / 4] == CK_GEM_USRIO_RGMII); + CHECK(f.r[CK_GEM_SA1B / 4] == 0x45494102u && f.r[CK_GEM_SA1T / 4] == 0x0000034eu); + clean_flags(); + /* a second transfer reuses the window cleanly and still sends exactly one more frame */ + mkx(&x, CK_GEMX_LLB, fr, 2); + CHECK(ck_gem_xfer(&g, &x) == 0 && x.rx_done && ck_gem_frame_diff(fr, 60, x.rx_data, x.rx_copied) == 0); + CHECK(f.frames_sent == 2); + CHECK(g.w->next < 0x10000); + clean_flags(); +} + +static void t_phy_loopback(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + uint16_t saved = 0, bmsr = 0; + fresh(); + CHECK(ck_phy_loopback_enter(&fio, 1, 1000, &saved, &bmsr) == 0); + CHECK(saved == 0x1140); + CHECK(f.phy[0] == (CK_MII_BMCR_LOOPBACK | CK_MII_BMCR_FULLDPLX | CK_MII_BMCR_SPEED1000)); /* 0x4140, no autoneg */ + mkx(&x, CK_GEMX_PHYLB, fr, 7); + CHECK(ck_gem_xfer(&g, &x) == 0 && x.rx_done && ck_gem_frame_diff(fr, 60, x.rx_data, x.rx_copied) == 0); + CHECK(!(x.ncr_run & CK_GEM_NCR_LLB)); /* the PHY loops, not the MAC */ + CHECK(ck_phy_loopback_exit(&fio, 1, saved) == 0); + CHECK(f.phy[0] == (0x1140 | CK_MII_BMCR_ANRESTART)); + clean_flags(); + f.phy[1] = 0x7969u; /* link down in loopback: enter reports a timeout, does not claim success */ + CHECK(ck_phy_loopback_enter(&fio, 1, 100, &saved, &bmsr) != 0); + CHECK(f.phy[0] == (CK_MII_BMCR_LOOPBACK | CK_MII_BMCR_FULLDPLX | CK_MII_BMCR_SPEED100)); +} + +static void t_wire_no_loop(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + f.r[CK_GEM_NCR / 4] |= CK_GEM_NCR_LLB; /* left over from an earlier test: the reset must clear it */ + mkx(&x, CK_GEMX_WIRE, fr, 3); + x.speed = 100; + f.expect_gbe = 0; + CHECK(ck_gem_xfer(&g, &x) == 0); + CHECK(x.tx_done == 1 && x.rx_done == 0 && f.frames_sent == 1 && f.frames_rcvd == 0); + CHECK(f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_SPD); + CHECK(!(f.r[CK_GEM_NCFGR / 4] & CK_GEM_NCFGR_GBE)); + clean_flags(); +} + +static void hostile(uint64_t tx_addr, uint64_t rx_addr) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + int rc; + fresh(); + mkx(&x, CK_GEMX_LLB, fr, 1); + x.tx_addr = tx_addr; + x.rx_addr = rx_addr; + rc = ck_gem_xfer(&g, &x); + CHECK(rc == CK_GEM_E_WINDOW); + CHECK(f.bme_on_n == 0 && f.bme == 0); /* bus-master never enabled */ + CHECK(f.gem_writes == 0 && f.kicks == 0); /* the MAC was not even programmed */ + CHECK(f.frames_sent == 0); + CHECK(g.halted == 1 && ck_dmaw_halted(&dw)); /* latched */ + mkx(&x, CK_GEMX_LLB, fr, 2); /* a good transfer afterwards is refused: sticky until reboot */ + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_HALTED); + CHECK(f.bme_on_n == 0 && f.kicks == 0); +} + +static void t_refuse_outside(void) +{ + hostile(WBASE - 64, 0); /* just below the window */ + hostile(WBASE + WSIZE, 0); /* first byte after */ + hostile(WBASE + WSIZE - 32, 0); /* straddles the end (60 byte frame) */ + hostile(0xfffffffffffffff0ull, 0); /* wraps when added to a length */ + hostile(OUTBASE, 0); + hostile(0, WBASE + WSIZE); /* RX buffer outside */ + hostile(0, WBASE + WSIZE - 1536 + 64); /* RX buffer overhangs the end */ + hostile(0, 0xfffffffffffffff8ull); +} + +static void t_hwwin_gate(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + g.hw_window_ok = 0; + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_HWWIN); + CHECK(f.bme_on_n == 0 && f.gem_writes == 0 && f.kicks == 0); + CHECK(g.halted == 0); /* a refusal here is not a violation: it can be retried once the window is confirmed */ +} + +static void t_arg_checks(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + mkx(&x, CK_GEMX_LLB, fr, 1); + x.frame_len = 1515; + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_ARG); + x.frame_len = 59; + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_ARG); + mkx(&x, CK_GEMX_LLB, fr, 1); + x.speed = 777; + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_ARG); + mkx(&x, CK_GEMX_LLB, fr, 1); + x.rx_buf_len = 1537; + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_ARG); + x.rx_buf_len = 4096; + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_ARG); + CHECK(f.bme_on_n == 0 && f.gem_writes == 0 && f.kicks == 0 && g.halted == 0); +} + +static void t_device_lies(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + f.rx_len_lie = 1; /* the device claims 2000 bytes in a 1536 byte buffer */ + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_DEVICE); + CHECK(g.halted == 1 && ck_dmaw_halted(&dw)); + CHECK(f.bme == 0 && f.bme_off_n == 1); /* bus-master was already off when the lie was read */ +} + +static void t_bme_failures(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + f.bme_fail_on = 1; + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_BME); + CHECK(f.kicks == 0 && f.frames_sent == 0 && g.halted == 1); + fresh(); + f.bme_fail_off = 1; + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_BME); + CHECK(g.halted == 1); + CHECK(!(f.r[CK_GEM_NCR / 4] & (CK_GEM_NCR_RE | CK_GEM_NCR_TE))); /* the MAC was stopped even though bus-master would not drop */ +} + +static void t_tieoff_and_queues(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + uint32_t q; + fresh(); + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == 0); + for (q = 1; q <= 3; q++) { + CHECK(inwin(f.tbqp_q[q], 16) && inwin(f.rbqp_q[q], 16)); + CHECK(f.tbqp_q[q] != 0 && f.rbqp_q[q] != 0); + } + clean_flags(); +} + +static void t_quiesce_order(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + f.tgo_delay_cfg = 200; /* the transmitter stays busy for 200 status reads */ + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == 0); + CHECK(f.bme_off_while_tgo == 0 && f.bme_off_while_te == 0); + CHECK(f.tgo_cleared_seen == 1); + clean_flags(); + /* a transmitter that never stops: halted, bus-master still dropped, error reported */ + fresh(); + f.tgo_delay_cfg = 1000000; + mkx(&x, CK_GEMX_LLB, fr, 1); + CHECK(ck_gem_xfer(&g, &x) == CK_GEM_E_QUIESCE); + CHECK(g.halted == 1 && f.bme == 0); +} + +static void t_raw_outside_for_hw_test(void) +{ + struct ck_gem_xfer x; + uint8_t fr[64]; + fresh(); + /* confinement test path: the hardware-visible RX address is OUTSIDE; the shadow stays valid and nothing latches */ + mkx(&x, CK_GEMX_LLB, fr, 4); + x.rx_count = 1; + x.rx_buf_len = 64; + x.raw_rx_outside = OUTBASE; + memset(f.outside, 0xa5, sizeof f.outside); + CHECK(ck_gem_xfer(&g, &x) == 0); + CHECK(g.halted == 0 && !ck_dmaw_halted(&dw)); + CHECK(f.outside_writes == 1); /* the fake (no RC window) let it through */ + CHECK(memcmp(f.outside, fr, 60) == 0 && f.outside[60] == 0xa5); + CHECK(x.rx_len == 60 && x.rx_done); + CHECK(f.bme_on_n == 1 && f.bme_off_n == 1); + /* the in-window RX buffer stayed empty: the data went where the raw descriptor said */ + { + uint32_t i, nz = 0; + for (i = 0; i < 60; i++) nz += x.rx_data[i] != 0; + CHECK(nz == 0); + } + fresh(); + mkx(&x, CK_GEMX_LLB, fr, 5); + x.rx_count = 1; + x.raw_tx_outside = OUTBASE; + CHECK(ck_gem_xfer(&g, &x) == 0); + CHECK(f.outside_access >= 1); /* the fake saw a read outside; the real test reads whether the hardware blocked it */ + CHECK(g.halted == 0); +} + +static void t_frames(void) +{ + uint8_t a[64], b[64], c[64]; + uint8_t tpa[4] = {192, 168, 1, 1}; + CHECK(ck_gem_frame_build(CK_GEMF_RAW, MAC, 0x01020304u, 0, a, sizeof a) == 60); + CHECK(ck_gem_frame_build(CK_GEMF_RAW, MAC, 0x01020305u, 0, b, sizeof b) == 60); + CHECK(a[0] == 0xff && a[5] == 0xff && memcmp(a + 6, MAC, 6) == 0 && a[12] == 0x88 && a[13] == 0xb5); + CHECK(memcmp(a + 14, "AIENOS-PI5-NET3", 16) == 0); + CHECK(a[30] == 1 && a[31] == 2 && a[32] == 3 && a[33] == 4); /* sequence number, big endian */ + CHECK(ck_gem_frame_diff(a, 60, b, 60) != 0); /* different sequence = different frame */ + CHECK(ck_gem_frame_build(CK_GEMF_ARP, MAC, 9, tpa, c, sizeof c) == 60); + CHECK(c[12] == 0x08 && c[13] == 0x06 && c[14] == 0 && c[15] == 1 && c[16] == 8 && c[17] == 0 && c[18] == 6 && c[19] == 4 && c[21] == 1); + CHECK(memcmp(c + 22, MAC, 6) == 0 && c[28] == 0 && c[31] == 0 && memcmp(c + 38, tpa, 4) == 0); /* probe: sender IP 0.0.0.0 */ + CHECK(c[50] == 9); + CHECK(ck_gem_frame_build(99, MAC, 1, 0, a, sizeof a) == 0 && ck_gem_frame_build(CK_GEMF_RAW, MAC, 1, 0, a, 59) == 0); + /* diff: identical, last byte, length */ + memcpy(b, a, 60); + CHECK(ck_gem_frame_diff(a, 60, b, 60) == 0); + b[59] ^= 1; + CHECK(ck_gem_frame_diff(a, 60, b, 60) == 60); + b[59] ^= 1; + b[0] ^= 1; + CHECK(ck_gem_frame_diff(a, 60, b, 60) == 1); + b[0] ^= 1; + CHECK(ck_gem_frame_diff(a, 60, b, 59) == 60); +} + +static void t_mdio_phy(void) +{ + struct ck_phy_link l; + uint16_t aux = 0, clk = 0; + int link = 0, fd = 0; + uint32_t sp = 0; + fresh(); + /* IEEE 802.3 clause 22 write frame: ST=01 OP=01 PHYAD REGAD TA=10 DATA */ + CHECK(ck_gem_mdio_frame_write(1, 0, 0x4140) == (0x40000000u | (1u << 28) | (1u << 23) | (2u << 16) | 0x4140u)); + CHECK(ck_gem_mdio_write(&fio, 1, 0, 0x4140) == 0 && f.phy[0] == 0x4140); + CHECK(ck_gem_mdio_write(&fio, 32, 0, 0) != 0 && ck_gem_mdio_write(&fio, 1, 32, 0) != 0); + /* link resolution: 1000 full from the partner's STAT1000 */ + fresh(); + f.phy[0] = 0x1140; f.phy[1] = 0x796d; f.phy[9] = 0x0300; f.phy[10] = 0x0800; + CHECK(ck_phy_link_read(&fio, 1, &l) == 0 && l.link && l.aneg_done && l.speed == 1000 && l.full_duplex); + f.phy[10] = 0; f.phy[4] = 0x01e1; f.phy[5] = 0x0100 | 0x0001; + CHECK(ck_phy_link_read(&fio, 1, &l) == 0 && l.speed == 100 && l.full_duplex); + f.phy[5] = 0x0020 | 0x0001; + CHECK(ck_phy_link_read(&fio, 1, &l) == 0 && l.speed == 10 && !l.full_duplex); + f.phy[1] = 0x7969; /* no link */ + CHECK(ck_phy_link_read(&fio, 1, &l) == 0 && !l.link && l.speed == 0); + f.phy[1] = 0x794d; /* link, autoneg not complete: unresolved */ + CHECK(ck_phy_link_read(&fio, 1, &l) == 0 && l.link && !l.aneg_done && l.speed == 0); + /* ETH_CFG STATUS decode */ + ck_eth_cfg_status_decode(0x00000005u, &link, &sp, &fd); + CHECK(link == 0 || link == 1); + ck_eth_cfg_status_decode(0x0000000bu, &link, &sp, &fd); + CHECK(link == 1 && sp == 100 && fd == 1); + ck_eth_cfg_status_decode(0x00000004u, &link, &sp, &fd); + CHECK(link == 0 && sp == 1000 && fd == 0); + ck_eth_cfg_status_decode(0x00000000u, &link, &sp, &fd); + CHECK(sp == 10); + /* RGMII-ID clock delays as Linux sets them */ + fresh(); + CHECK(ck_phy_rgmii_id_config(&fio, 1, &aux, &clk) == 0); + CHECK((f.phy[0x18] & 0x8000u) && (f.phy[0x18] & 0x0100u) && (f.phy[0x18] & 7u) == 7u); + CHECK(clk == 0x0010 && (f.shadow[3] & (1u << 9)) && (f.shadow[3] & 0x0010)); +} + +/* ---- bus-master through the root complex config window ---- */ +static uint32_t cmd_status, cfg_index, stuck_bme; +static int cfg_writes_status_ones; +static uint32_t rc_rd(void *c, uint32_t off) +{ + (void)c; + if (off == CK_PI5PCIE_EXT_CFG_INDEX) + return cfg_index; + if (off >= 0x8000 && off < 0x9000) + return (cfg_index == 0x00100000u && off - 0x8000 == CK_PI5NET_PCI_COMMAND) ? cmd_status : 0; + return 0; +} +static void rc_wr(void *c, uint32_t off, uint32_t v) +{ + (void)c; + if (off == CK_PI5PCIE_EXT_CFG_INDEX) { + cfg_index = v; + return; + } + if (off >= 0x8000 && off < 0x9000 && cfg_index == 0x00100000u && off - 0x8000 == CK_PI5NET_PCI_COMMAND) { + if (v >> 16) + cfg_writes_status_ones++; + cmd_status = (cmd_status & 0xffff0000u & ~(v & 0xffff0000u)) | (v & 0xffffu); + if (stuck_bme) + cmd_status |= CK_PI5NET_CMD_BME; + } +} +static struct ck_pi5pcie_io rcio = {rc_rd, rc_wr, 0}; + +static void t_bme_set(void) +{ + cmd_status = (0x0010u << 16) | 0x0002u; + cfg_index = 0; + stuck_bme = 0; + cfg_writes_status_ones = 0; + CHECK(ck_pi5gem_bme_set(&rcio, 1) == 0 && (cmd_status & 0xffffu) == 0x0006u); + CHECK((cmd_status >> 16) == 0x0010u && cfg_writes_status_ones == 0); /* RW1C status never written back as ones */ + CHECK(ck_pi5gem_bme_set(&rcio, 0) == 0 && (cmd_status & 0xffffu) == 0x0002u); + stuck_bme = 1; /* a device that ignores the clear must be reported, not trusted */ + cmd_status = (0x0010u << 16) | 0x0006u; + CHECK(ck_pi5gem_bme_set(&rcio, 0) == CK_GEM_E_BME); +} + +int main(void) +{ + t_probe(); + t_loopback(); + t_phy_loopback(); + t_wire_no_loop(); + t_refuse_outside(); + t_hwwin_gate(); + t_arg_checks(); + t_device_lies(); + t_bme_failures(); + t_tieoff_and_queues(); + t_quiesce_order(); + t_raw_outside_for_hw_test(); + t_frames(); + t_mdio_phy(); + t_bme_set(); + free(f.mem); + return ck_t_verdict("test_pi5_gem"); +} diff --git a/scripts/.crumb b/scripts/.crumb index 9b0c1168..4588979a 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T23:07:22Z", + "compiled_at": "2026-10-13T23:37:04Z", "compiler": "crumb-compile/1", - "digest": "sha256:95951d5a52b7ade0f66bb972e92ebfd3fb14433557f1ee827bef14d6481a17a7", + "digest": "sha256:48c4fa69e56fcade3e5fa0d959501d89269856026f10eec71fe9818e6ca9a9de", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 96, - "generated_at_commit": "53de55c5dd3485948cd34734caf0f646e28a68c4", - "source_tree": "sha256:ac8f9aa5b5a238cb102f7a679b798ed7ac5ff7059bcf8d7a2456279c7c6acae6" + "generated_at_commit": "23c8bcd5490dd2ec9ded661ffefd5ecb5533dcec", + "source_tree": "sha256:4af4194f1f4a23a3ab45707c3407c3cb00b5311d33c95f530e80d90731456dc0" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_pi5_build_check.sh b/scripts/ck_pi5_build_check.sh index 6d1d5e6b..a44938b4 100755 --- a/scripts/ck_pi5_build_check.sh +++ b/scripts/ck_pi5_build_check.sh @@ -79,6 +79,11 @@ for s in ck_pi5net_run ck_pi5net_ib_plan ck_pi5net_ib_apply ck_rp1_phy_reset ck_ done if grep -q "pi5_dma: hw_window %s=%s" <<<"$(strings -a "${pnet}")"; then echo "PASS pi5-net image carries the verdict line"; else echo "FAIL pi5-net image lacks the verdict line"; fail=1; fi test -s "${root}/target/native-kernel-pi5-net/BOOTAA64.EFI" || { echo "FAIL no pi5-net BOOTAA64.EFI"; fail=1; } +# Cut 3: the GEM frame test (ck_gem_xfer, ck_pi5gem_run) lives only in the pi5-net image. +for s in ck_gem_xfer ck_pi5gem_run ck_pi5gem_bme_set; do + if defined "${pnet}" "${s}"; then echo "PASS pi5-net image defines ${s}"; else echo "FAIL pi5-net image lacks ${s}"; fail=1; fi + if defined "${pi5}" "${s}" || defined "${ps}" "${s}" || defined "${pinf}" "${s}" || defined "${def}" "${s}" || defined "${ppc}" "${s}"; then echo "FAIL a non-net image defines ${s}"; fail=1; else echo "PASS no other image defines ${s}"; fi +done for other in CK_PI5_STORE=1 CK_PI5_INFER=1 CK_PI5_PCIE=1 CK_PI5_UNLOCK=1 CK_PI5_AGENT=1 CK_PI5_FACT=1 CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_PI5_NET=2; do if make -n -C "${root}/native/kernel" CK_PI5_NET=1 ${other} full >/dev/null 2>&1; then echo "FAIL Makefile accepts CK_PI5_NET=1 with ${other}"; fail=1; else echo "PASS Makefile refuses CK_PI5_NET=1 with ${other}"; fi done ```