diff --git a/.crumb b/.crumb index 0a741f50..967df9ce 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "b623dcff39819d5cee38a02dcca6bf81b8808a5113b4b5115d9efabde1f7cea8", - "last_commit": "253f032", + "digest": "25020cdb700e0f5b821988ff7a3c5a7e5bd9e9019ae4d26a79ce1367e4ebfce1", + "last_commit": "e7c361a", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "68ee591ce3a703ab3b2de971fa0109212ba0ccdc0ee5bf6388757f6116c0a4f4", - "last_commit": "03de868", + "digest": "a0bcd4afd87949d9d7bb92fefb489693d9035ee01fcd22cf418b630eb4127e28", + "last_commit": "5b0b9da", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "14efd43f6438e72f0045a0b2388de579210569ba4514e70fab32e317c8813e02", - "last_commit": "2d3b591", + "digest": "17ca56cca1e53f3d5ef7813006097037d3e3bbf8b4882fc907ea6c6979756d08", + "last_commit": "5b0b9da", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:0e410b17c3b25acc3cc14ae5952690aa16f1efeddeee1bd29305ebdd91ab623e", - "compiled_at": "2026-10-13T21:27:22Z", + "children_root": "sha256:46a1dd17cc083812966f373720f2fcba99c5a2186b6caa85d9e3e40d80dc94fa", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:da62089689742848b9e7b02e9aed67005140540f7ae1162e958a47a403634015", + "digest": "sha256:7056633ab259accedf28870417868bba98e3d2f3172c5e2fee5e7754e44fc4dd", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3188, - "generated_at_commit": "03de8681ae58720bd01a45ccb5311c792f1d517b", - "source_tree": "sha256:4316ca32be87e0848d3b730a8708f9e24f0c0ef4ea61e7654bed43cb04720930" + "files": 3192, + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:d1d8f50283fa1e97f75a3267b0c82eb6d06268847eab7f838e993c0e052ac832" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 57d74ca2..0b1e29b2 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:c5d7ca9ce0145309c90a439711adcb6dd8e8e40ff48b90455f545e63c05f04c8", - "compiled_at": "2026-10-13T21:03:58Z", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:b623dcff39819d5cee38a02dcca6bf81b8808a5113b4b5115d9efabde1f7cea8", + "digest": "sha256:25020cdb700e0f5b821988ff7a3c5a7e5bd9e9019ae4d26a79ce1367e4ebfce1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 79, - "generated_at_commit": "e0390838eacf984a66dfdf4fdf9e8d5dc88bdd70", - "source_tree": "sha256:e59e5cb87ae38f2afc23020a84349606f61bd0b33877db6e2e6f4283d32d2cae" + "files": 80, + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:6b9981604fb64759f58b202f3905e6cb1ec0a3c4cdea416ffe8a9b1fa34d923c" }, "owner_lane": "", "provenance": { diff --git a/docs/native/PI5-NET-2-RUNS.md b/docs/native/PI5-NET-2-RUNS.md new file mode 100644 index 00000000..d2a2e1c5 --- /dev/null +++ b/docs/native/PI5-NET-2-RUNS.md @@ -0,0 +1,53 @@ +# PI5-NET-2 runs: inbound DMA window confinement and RP1 Ethernet up to the PHY + +Image: `make pi5-net` (flag `CK_PI5_NET=1`, refuses every other Pi image flag). Code: `native/kernel/dev/pi5_net.c`, `pi5_net.h`. Host tests: `tests/test_pi5_net.c` (fake registers, 23 mutants, all killed by `make pi5-net-mutants`). Base: main 5429602 (cut 1, #363). A host test proves the decisions and the register write order, not the hardware. + +## Run pi5-net-1 (real Raspberry Pi 5) + +Evidence file: `~/workspace/pi5-runs/pi5-net-1/kernel.log` (image sha256 f42298cb40d39d20375a4659d18b76a5971ca7c2816c49cbf93b13189d27181b). `\EFI\AIENOS\` had no MODEL.MAP. Exact lines: + +``` +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 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: phy reset gpio32 rc=0 ctrl 0000009f->00000085 pad 000000d6->00000056 oe_before=00000000 out_before=00000000 in: before=00000002 low=00000002 high=00000010 +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: done: no frames sent; GEM tx/rx not enabled; rp1 bus-master left off +``` + +## Results + +- **A. Confinement: CONFINED, exact.** Window 0 went from the firmware's 64 GiB (bus 0 to cpu 0) to exactly the 2 MiB bounce block (0x4400000, size code 6). Extra bytes beyond the bounce region: 0. Readback of all four registers matched the plan; no other inbound window was enabled (0 disabled). +- **B. PHY ID: 0x600d84a2**, OUI 18-C0-86 (Broadcom in the IEEE registry), model 0x0a, revision 2, matches Linux `PHY_ID_BCM54213PE` exactly. Found at MDIO address 1 on the first attempt. +- **Clocks.** The firmware had already enabled `clk_eth` (125 MHz) and `clk_eth_tsu` (50 MHz). They were left exactly as found (no write). The decide/apply path that would enable them is tested on host only (UNVERIFIED on hardware, since not needed). +- **PHY reset.** GPIO32 was driven low then released; the pin input read low during the pulse (bit 4 clear) and high afterwards. This shows the line moved; a PHY ID answer after it shows the PHY came out of reset. + +## Order and safety + +RP1 bus mastering (PCI command BME on 01:00.0) was cleared first and read back off, then 2 ms wait, then window 0 was switched off, HI/REMAP changed, LO written last with the size code. On any failed readback the firmware window is restored. BME is left off afterwards (the later GEM driver must turn it on). + +## UNVERIFIED + +- **Blocking was not exercised.** The register readback proves the window is programmed; no RP1 DMA was issued at an address outside it, because there is no safe DMA master yet (no GEM descriptors in this cut). The safe test belongs to the cut that has the GEM: with BME on, point one descriptor just outside the bounce and expect an abort or error status, then one inside and expect success. +- **"No DMA in flight"** is established by BME off plus a 2 ms wait, not by observing RP1 internal queues. +- **MSI.** MSI writes also pass an inbound window (Linux uses a window to the MSI target). A 2 MiB window excludes it. This kernel polls and uses no MSI, so nothing breaks now; an MSI-using driver must add a window for it (ADR 0020 rule 6 already says the MSI path is UNVERIFIED). +- **Controller-wide.** The window covers all devices behind this root complex (RP1 DMA and USB), as ADR 0020 states. +- Clock enable path, MDC divider at other rates, and a stuck-PHY reset are host-tested only. + +## Sources read (see header of `dev/pi5_net.h`) + +edk2-platforms Bcm2712PciHostBridge.c and Bcm2712Pcie.h (inbound/outbound window registers), Rp1.h, Linux rpi-6.12.y pcie-brcmstb.c, clk-rp1.c, pinctrl-rp1.c, macb_main.c, broadcom.c, brcmphy.h, rp1.dtsi, and the RP1 datasheet (outbound direct mapped space, GEM, ETH_CFG). The Pi run is the final authority for what the firmware actually left in place. diff --git a/native/.crumb b/native/.crumb index 6bff04aa..58963cbe 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "2c8c54f2bf9bd722362b7d0c9508dd733f48440454c0d7e8225d9d1849c85306", - "last_commit": "03de868", + "digest": "dad7c2c41e5b3ee793d9dd4cb57644b09e9987269506b04ea01352bc83ea2cbf", + "last_commit": "5b0b9da", "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:68a091842d689ca741a1ba90ce7335a5055feff6c5583651a17361c2d2c39a15", - "compiled_at": "2026-10-13T21:27:22Z", + "children_root": "sha256:17931a0ea027fb5fdd5992c3ff9e9c5a394907c8e5915699c060acca3480ff4a", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:68ee591ce3a703ab3b2de971fa0109212ba0ccdc0ee5bf6388757f6116c0a4f4", + "digest": "sha256:a0bcd4afd87949d9d7bb92fefb489693d9035ee01fcd22cf418b630eb4127e28", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 545, - "generated_at_commit": "03de8681ae58720bd01a45ccb5311c792f1d517b", - "source_tree": "sha256:b2626c23e00865c9928b68dd2234173a3c3dbdfffa114fdb43443b3bd985a8aa" + "files": 548, + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:d04fb5884b942ca9fc7a24fc96a808fadd7a4a2becb1308f90accea48202a64b" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index d2603716..44fc774b 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,20 +57,20 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "7dfb14f28dff4f2b7bb5f1941f433d5f89ce83703643c256816475337cf3386e", - "last_commit": "03de868", + "digest": "957bfad5ac7bb52cc3447b4520f3b029c421a8891b90b5c00dad842339294575", + "last_commit": "5b0b9da", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "c33332d860fe8c2c50a901891772289271c5bf0e7a442fe46dd9b881033c7966", - "last_commit": "5429602", + "digest": "9302f530853614d0541c387ba09fca4364bc195eef335a24bbe205dfd69f1ecd", + "last_commit": "5b0b9da", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "8198bb9a0fbd634281a251b012eca8874c97e469a45eec8b5a6bf06a7e828f9a", - "last_commit": "cd8d6a2", + "last_commit": "0735543", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, @@ -82,32 +82,32 @@ }, { "digest": "ef2c5b80defe4f152278954ff8a5b256a1898dd0f77d8220c5fbe6637e99bed0", - "last_commit": "cd8d6a2", + "last_commit": "0735543", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "774873904015b85c13b33a02b7f2ebcc41a815306ad7a09973cd3c201a9edf49", - "last_commit": "cd8d6a2", + "digest": "17f3ab5f5e8314d29db1d2ee908a01848745520cc61b8de1d95982f6657e065c", + "last_commit": "5b0b9da", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { "digest": "0edc3f7461724a676c9514bce12eb8fa4bd9eabb45a4802192aca7c50aaf8e20", - "last_commit": "cd8d6a2", + "last_commit": "0735543", "name": "tools", "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:cbdad3f1026f074f3192cf1ef19b1a5010f6e46b6439c2906f9ba5a7e69ea463", - "compiled_at": "2026-10-13T21:27:22Z", + "children_root": "sha256:f9f3a89da6f80d7778e39ff641df570c22ffaafa59862543d2bd5ad934617de3", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:2c8c54f2bf9bd722362b7d0c9508dd733f48440454c0d7e8225d9d1849c85306", + "digest": "sha256:dad7c2c41e5b3ee793d9dd4cb57644b09e9987269506b04ea01352bc83ea2cbf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 366, - "generated_at_commit": "03de8681ae58720bd01a45ccb5311c792f1d517b", - "source_tree": "sha256:3cb96f47a4edb199b9122f996e3a50071287db309cf89bd1dcb74eace1b893ed" + "files": 369, + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:ec50a94fd5ad0afc6e61b8553c4cfff7a9cc62480b509322a50c1ab74c83b518" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 531fd75e..0f1ad8e9 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -178,14 +178,30 @@ ifneq ($(strip $(CK_PI5_PCIE)),) ifneq ($(strip $(CK_PI5_PCIE)),1) $(error CK_PI5_PCIE must be empty or 1) endif -ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_UNLOCK)$(CK_PI5_AGENT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)),) -$(error CK_PI5_PCIE is the Pi PCIe/RP1 enumerator image: it cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_UNLOCK, CK_PI5_AGENT, CK_SERVICE or CK_HARDWARE_STAGING) +ifneq ($(strip $(CK_PI5_NET)$(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_UNLOCK)$(CK_PI5_AGENT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)),) +$(error CK_PI5_PCIE is the Pi PCIe/RP1 enumerator image: it cannot be combined with CK_PI5_NET, CK_PI5_STORE, CK_PI5_INFER, CK_PI5_UNLOCK, CK_PI5_AGENT, CK_SERVICE or CK_HARDWARE_STAGING) endif CK_PI5 := 1 OUT ?= $(REPO)/target/native-kernel-pi5-pcie CK_EXTRA_CFLAGS += -DCK_PI5_PCIE=1 endif +# Pi 5 network bring-up image (CK_PI5_NET=1, "make pi5-net", AIENOS-PI5 network lane cut 2): the plain Pi image plus +# dev/pi5_net.c (narrows the BCM2712 PCIe2 inbound DMA window to the ADR 0020 bounce region, then brings RP1's +# Ethernet up to the PHY and reads the PHY ID; no frames). Own OUT; no other image carries this code. +CK_PI5_NET ?= +ifneq ($(strip $(CK_PI5_NET)),) +ifneq ($(strip $(CK_PI5_NET)),1) +$(error CK_PI5_NET must be empty or 1) +endif +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_UNLOCK)$(CK_PI5_AGENT)$(CK_PI5_FACT)$(CK_PI5_PCIE)$(CK_SERVICE)$(CK_HARDWARE_STAGING)),) +$(error CK_PI5_NET is the Pi network bring-up image: it cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_UNLOCK, CK_PI5_AGENT, CK_PI5_FACT, CK_PI5_PCIE, CK_SERVICE or CK_HARDWARE_STAGING) +endif +CK_PI5 := 1 +OUT ?= $(REPO)/target/native-kernel-pi5-net +CK_EXTRA_CFLAGS += -DCK_PI5_NET=1 +endif + # Pi unlock image (CK_PI5_UNLOCK=1, "make pi5-unlock", PI5-4 cut 1): the same Store stage, but the Store opens # ONLY through the ADR 0017 Slot 1 recovery-secret path (ck_store_unlock_boot, PRODUCTION-class keys). # ck_store_test_keys and ck_store_test_uuid are not compiled, so no TEST key or TEST uuid can reach it. @@ -239,8 +255,8 @@ endif ifneq ($(strip $(CK_PI5_UNLOCK))$(strip $(CK_PI5_UNLOCK_CARD)),11) $(error CK_PI5_FACT (Cortex fact stage, QUALIFICATION ONLY) needs CK_PI5_UNLOCK=1 and CK_PI5_UNLOCK_CARD=1) endif -ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)$(CK_PI5_AGENT)),) -$(error CK_PI5_FACT cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE or CK_PI5_AGENT) +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)$(CK_PI5_NET)$(CK_PI5_AGENT)),) +$(error CK_PI5_FACT cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE, CK_PI5_NET or CK_PI5_AGENT) endif CK_EXTRA_CFLAGS += -DCK_PI5_FACT=1 endif @@ -258,8 +274,8 @@ endif ifneq ($(strip $(CK_PI5_UNLOCK))$(strip $(CK_PI5_UNLOCK_CARD)),11) $(error CK_PI5_AGENT (Store plus inference, QUALIFICATION ONLY) needs CK_PI5_UNLOCK=1 and CK_PI5_UNLOCK_CARD=1) endif -ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)$(CK_PI5_FACT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)$(CK_TEST_FWCFG_ARTIFACTS)),) -$(error CK_PI5_AGENT cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE, CK_PI5_FACT, CK_SERVICE, CK_HARDWARE_STAGING or CK_TEST_FWCFG_ARTIFACTS) +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)$(CK_PI5_NET)$(CK_PI5_FACT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)$(CK_TEST_FWCFG_ARTIFACTS)),) +$(error CK_PI5_AGENT cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE, CK_PI5_NET, CK_PI5_FACT, CK_SERVICE, CK_HARDWARE_STAGING or CK_TEST_FWCFG_ARTIFACTS) endif ifeq ($(strip $(CK_INFER_LIB)),) $(error CK_PI5_AGENT needs CK_INFER_LIB= (use "make pi5-agent", which builds it)) @@ -787,6 +803,9 @@ endif 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 +endif ifneq ($(strip $(CK_PI5_UNLOCK_CARD)),) CORE_C += $(BOOT)/efi_unlock.c $(KDIR)/core/unlock_req.c endif @@ -1215,7 +1234,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 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_unlock_req test_context + 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 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 @@ -2000,6 +2019,9 @@ pi5: $(MAKE) CK_PI5=1 all # Raspberry Pi 5 PCIe/RP1 enumerator image (see CK_PI5_PCIE above): target/native-kernel-pi5-pcie/BOOTAA64.EFI +pi5-net: + $(MAKE) CK_PI5_NET=1 all + pi5-pcie: $(MAKE) CK_PI5_PCIE=1 all @@ -2107,3 +2129,24 @@ pi5-pcie-mutants: if $(OUT)/host/test_pi5_pcie_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-pcie pi5-pcie-mutants pi5-pcie + +# AIENOS-PI5 network cut 2 (inbound window plan, RP1 quiesce, clocks, PHY reset, MDIO, PHY ID) over fake registers. +PI5NET_SRCS := $(KDIR)/dev/pi5_net.c $(KDIR)/dev/pi5_pcie.c +PI5NET_DEPS := $(KDIR)/tests/test_pi5_net.c $(PI5NET_SRCS) $(KDIR)/tests/ck_test.h $(KDIR)/dev/pi5_net.h $(KDIR)/dev/pi5_pcie.h +$(OUT)/host/test_pi5_net: $(PI5NET_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) -I$(KDIR)/dev -o $@ $< $(PI5NET_SRCS) +$(OUT)/host-san/test_pi5_net: $(PI5NET_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) -I$(KDIR)/dev -o $@ $< $(PI5NET_SRCS) +test-pi5-net: $(OUT)/host/test_pi5_net + $< +# Each mutant removes one decision; the test must FAIL for every one. +PI5NET_MUTANTS := NO_STRADDLE NO_REMAP_EN NO_DISABLE_FIRST NO_READBACK NO_RESTORE NO_OTHERS NO_STATUS_MASK NO_QUIESCE_CHECK OB_NO_LIMIT NO_ASSERT NO_HOLD NO_RELEASE NO_HIGH_FIRST NO_IDLE_AFTER NO_MPE TOUCH_ON_CLOCK NO_EXACT_DIV NO_CLK_VERIFY OUI_ORDER BAD_CLASS NO_BOUNDS NO_MDC_DIV PARTIAL_ANY +pi5-net-mutants: + @mkdir -p $(OUT)/host + @fail=0; for m in $(PI5NET_MUTANTS); do \ + $(HOSTCC) $(HCFLAGS) -DCK_NET_MUTANT_$$m -I$(KDIR)/dev -o $(OUT)/host/test_pi5_net_mut_$$m $(KDIR)/tests/test_pi5_net.c $(PI5NET_SRCS) || exit 1; \ + 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 diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 0700d1c0..75775b65 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T21:27:22Z", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:7dfb14f28dff4f2b7bb5f1941f433d5f89ce83703643c256816475337cf3386e", + "digest": "sha256:957bfad5ac7bb52cc3447b4520f3b029c421a8891b90b5c00dad842339294575", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 64, - "generated_at_commit": "03de8681ae58720bd01a45ccb5311c792f1d517b", - "source_tree": "sha256:caade2fa31fc8cf8afda54a9df8bc223342dfe3b1ef6262b940f861259ac21f9" + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:cd057fbeedff2ee679d4166845d463c42bd8bc00e0486cf3955c78cb2f5a2a30" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c index 120f00ac..d4a788c9 100644 --- a/native/kernel/core/kmain.c +++ b/native/kernel/core/kmain.c @@ -14,6 +14,9 @@ #include "cpufeat.h" #include "handoff_check.h" #include "frames.h" +#ifdef CK_PI5_NET +#include "../dev/pi5_net.h" +#endif #ifdef CK_PI5_PCIE #include "../dev/pi5_pcie.h" #endif @@ -248,6 +251,11 @@ static __attribute__((noreturn)) void ck_el1_main(void *arg) ck_set_stage("pi5_pcie"); ck_pi5pcie_report(); #endif +#ifdef CK_PI5_NET + /* Pi 5 network cut 2 (dev/pi5_net.c): narrow the PCIe inbound DMA window, bring RP1 Ethernet up to the PHY. CK_PI5_NET image only. */ + ck_set_stage("pi5_net"); + ck_pi5net_run(); +#endif #ifdef CK_B7B_PROBE /* B7b preparation probe (core/smmu_svc.c), probe build only. */ diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index de4780d3..27c03043 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-13T18:08:10Z", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:c33332d860fe8c2c50a901891772289271c5bf0e7a442fe46dd9b881033c7966", + "digest": "sha256:9302f530853614d0541c387ba09fca4364bc195eef335a24bbe205dfd69f1ecd", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 39, - "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", - "source_tree": "sha256:0640ebbac7fc4b25989fcdc81917064756003f93f9fd2c4863a9076286850fdc" + "files": 41, + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:5cc13d7f7294ea12b0564904b87dd7c6bea8611da9f63e928351e8387a7d3e83" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/pi5_net.c b/native/kernel/dev/pi5_net.c new file mode 100644 index 00000000..840994fc --- /dev/null +++ b/native/kernel/dev/pi5_net.c @@ -0,0 +1,903 @@ +/* pi5_net.c -- see pi5_net.h. The decision and register-sequence logic is hosted-testable + * (tests/test_pi5_net.c); the MMIO glue at the bottom is the CK_PI5_NET image only. */ +#include "pi5_net.h" + +/* TEST-only mutants (make pi5-net-mutants): each removes one decision or one step and must + * make test_pi5_net FAIL. Host builds only; never part of an image. */ +#if defined(CK_NET_MUTANT_NO_STRADDLE) || defined(CK_NET_MUTANT_NO_REMAP_EN) || \ + defined(CK_NET_MUTANT_NO_DISABLE_FIRST) || defined(CK_NET_MUTANT_NO_READBACK) || \ + defined(CK_NET_MUTANT_NO_RESTORE) || defined(CK_NET_MUTANT_NO_OTHERS) || \ + defined(CK_NET_MUTANT_NO_STATUS_MASK) || defined(CK_NET_MUTANT_NO_QUIESCE_CHECK) || \ + defined(CK_NET_MUTANT_OB_NO_LIMIT) || defined(CK_NET_MUTANT_NO_ASSERT) || defined(CK_NET_MUTANT_NO_HOLD) || \ + defined(CK_NET_MUTANT_NO_RELEASE) || defined(CK_NET_MUTANT_NO_HIGH_FIRST) || \ + defined(CK_NET_MUTANT_NO_IDLE_AFTER) || defined(CK_NET_MUTANT_NO_MPE) || \ + defined(CK_NET_MUTANT_TOUCH_ON_CLOCK) || defined(CK_NET_MUTANT_NO_EXACT_DIV) || \ + defined(CK_NET_MUTANT_NO_CLK_VERIFY) || defined(CK_NET_MUTANT_OUI_ORDER) || \ + defined(CK_NET_MUTANT_BAD_CLASS) || defined(CK_NET_MUTANT_NO_BOUNDS) || defined(CK_NET_MUTANT_NO_MDC_DIV) || \ + defined(CK_NET_MUTANT_PARTIAL_ANY) +#if !__STDC_HOSTED__ +#error "CK_NET_MUTANT_* are host test mutants only" +#endif +#endif + +#define IB_SIZE_MASK 0x1fu +#define IB_LO_ADDR_MASK 0xfffff000u +#define GiB64 (1ull << 36) + +/* ---------------- A. inbound window ---------------- */ + +uint32_t ck_pi5net_ib_size_code(uint64_t size) +{ + uint32_t lg = 0; + if (size == 0 || (size & (size - 1)) != 0) + return 0; + while ((1ull << lg) < size) + lg++; + if (lg >= 12 && lg <= 15) + return (lg - 12) + 0x1cu; /* 4 KiB..32 KiB [E1] */ + if (lg >= 16 && lg <= 36) + return lg - 15; /* 64 KiB..64 GiB [E1] */ + return 0; +} + +int ck_pi5net_ib_plan(uint64_t b_cpu, uint64_t b_size, struct ck_pi5net_ibprog *p) +{ + uint64_t blk = 4096, end, base; + if (!p) + return CK_PI5NET_E_ARG; + *p = (struct ck_pi5net_ibprog){0}; + if (b_size == 0 || b_cpu > UINT64_MAX - b_size) + return CK_PI5NET_E_ARG; + end = b_cpu + b_size; + if (end > (1ull << 40)) /* REMAP_HI holds CPU address bits 39:32 */ + return CK_PI5NET_E_RANGE; + while (blk < b_size && blk <= GiB64) + blk <<= 1; + for (;;) { + if (blk > GiB64) + return CK_PI5NET_E_RANGE; + base = b_cpu & ~(blk - 1); +#ifndef CK_NET_MUTANT_NO_STRADDLE + if (base + blk < end) { /* aligned block does not reach the end: next size up */ + blk <<= 1; + continue; + } +#endif + break; + } + p->size_code = ck_pi5net_ib_size_code(blk); + if (p->size_code == 0) + return CK_PI5NET_E_RANGE; + p->bus_base = p->cpu_base = base; + p->size = blk; + p->extra_bytes = blk - b_size; + p->lo = ((uint32_t)base & ~IB_SIZE_MASK) | p->size_code; + p->hi = (uint32_t)(base >> 32); + p->remap_lo = (uint32_t)base & CK_PI5NET_REMAP_LO_MASK; +#ifndef CK_NET_MUTANT_NO_REMAP_EN + p->remap_lo |= CK_PI5NET_REMAP_ENABLE; +#endif + p->remap_hi = (uint32_t)(base >> 32) & CK_PI5NET_REMAP_HI_MASK; + return CK_PI5NET_OK; +} + +int ck_pi5net_cfg_write32(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, uint32_t reg, + uint32_t v) +{ + if (!io || !io->rd || !io->wr) + return CK_PI5NET_E_ARG; + if (reg >= 4096u || (reg & 3u) != 0 || fn > 7u) + return CK_PI5NET_E_ARG; + if (bus == 0) { + if (dev != 0 || fn != 0) + return CK_PI5NET_E_ARG; + io->wr(io->ctx, reg, v); + return CK_PI5NET_OK; + } + if (bus != 1 || dev != 0) + return CK_PI5NET_E_ARG; + io->wr(io->ctx, CK_PI5PCIE_EXT_CFG_INDEX, ((uint32_t)bus << 20) | ((uint32_t)dev << 15) | ((uint32_t)fn << 12)); + io->wr(io->ctx, CK_PI5PCIE_EXT_CFG_DATA + reg, v); + return CK_PI5NET_OK; +} + +int ck_pi5net_rp1_quiesce(const struct ck_pi5pcie_io *io, uint16_t *saved) +{ + uint32_t v, nv; + if (!saved) + return CK_PI5NET_E_ARG; + if (ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v)) + return CK_PI5NET_E_ARG; + *saved = (uint16_t)(v & 0xffffu); +#ifdef CK_NET_MUTANT_NO_STATUS_MASK + nv = v & ~(uint32_t)CK_PI5NET_CMD_BME; /* would write the RW1C status bits back as ones */ +#else + nv = (v & 0xffffu) & ~(uint32_t)CK_PI5NET_CMD_BME; /* status half written as zero: RW1C, nothing cleared */ +#endif + if (ck_pi5net_cfg_write32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, nv)) + return CK_PI5NET_E_ARG; +#ifndef CK_NET_MUTANT_NO_QUIESCE_CHECK + if (ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v)) + return CK_PI5NET_E_ARG; + if (v & CK_PI5NET_CMD_BME) + return CK_PI5NET_E_QUIESCE; +#endif + return CK_PI5NET_OK; +} + +static uint32_t oth_lo(uint32_t i) { return 0x40d4u + (i - 1) * 8u; } /* windows 1..7 [E2] */ + +static int ib_matches(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibprog *p) +{ +#ifdef CK_NET_MUTANT_NO_READBACK + (void)io; + (void)p; + return 1; +#else + uint32_t lo = io->rd(io->ctx, CK_PI5NET_IB0_LO), hi = io->rd(io->ctx, CK_PI5NET_IB0_HI); + uint32_t rlo = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_LO), rhi = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_HI); + /* bits 11:5 of LO are not part of the window (size code is 4:0, address from bit 12) */ + return (lo & (IB_LO_ADDR_MASK | IB_SIZE_MASK)) == (p->lo & (IB_LO_ADDR_MASK | IB_SIZE_MASK)) && hi == p->hi && + (rlo & (CK_PI5NET_REMAP_LO_MASK | CK_PI5NET_REMAP_ENABLE)) == p->remap_lo && + (rhi & CK_PI5NET_REMAP_HI_MASK) == p->remap_hi; +#endif +} + +static void ib_restore(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibsaved *s) +{ +#ifndef CK_NET_MUTANT_NO_RESTORE + uint32_t i; + io->wr(io->ctx, CK_PI5NET_IB0_LO, s->lo & ~IB_SIZE_MASK); + io->wr(io->ctx, CK_PI5NET_IB0_HI, s->hi); + io->wr(io->ctx, CK_PI5NET_IB0_REMAP_LO, s->remap_lo); + io->wr(io->ctx, CK_PI5NET_IB0_REMAP_HI, s->remap_hi); + io->wr(io->ctx, CK_PI5NET_IB0_LO, s->lo); + for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++) + io->wr(io->ctx, oth_lo(i), s->other_lo[i]); +#else + (void)io; + (void)s; +#endif +} + +int ck_pi5net_ib_apply(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibprog *p, + struct ck_pi5net_ibsaved *saved, uint32_t *n_disabled) +{ + uint32_t i, n = 0; + if (!io || !io->rd || !io->wr || !p || !saved || p->size_code == 0) + return CK_PI5NET_E_ARG; + if (n_disabled) + *n_disabled = 0; + saved->lo = io->rd(io->ctx, CK_PI5NET_IB0_LO); + saved->hi = io->rd(io->ctx, CK_PI5NET_IB0_HI); + saved->remap_lo = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_LO); + saved->remap_hi = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_HI); + saved->other_lo[0] = 0; + for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++) + saved->other_lo[i] = io->rd(io->ctx, oth_lo(i)); +#ifndef CK_NET_MUTANT_NO_DISABLE_FIRST + io->wr(io->ctx, CK_PI5NET_IB0_LO, saved->lo & ~IB_SIZE_MASK); /* window off while its other halves change */ +#endif + io->wr(io->ctx, CK_PI5NET_IB0_HI, p->hi); + io->wr(io->ctx, CK_PI5NET_IB0_REMAP_LO, p->remap_lo); + io->wr(io->ctx, CK_PI5NET_IB0_REMAP_HI, p->remap_hi); + io->wr(io->ctx, CK_PI5NET_IB0_LO, p->lo); /* size code last: the window turns on complete */ +#ifndef CK_NET_MUTANT_NO_OTHERS + for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++) { + if (saved->other_lo[i] & IB_SIZE_MASK) { + io->wr(io->ctx, oth_lo(i), saved->other_lo[i] & ~IB_SIZE_MASK); + n++; + } + } +#endif + if (n_disabled) + *n_disabled = n; + if (!ib_matches(io, p)) { + ib_restore(io, saved); + return CK_PI5NET_E_READBACK; + } + for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++) { + if (io->rd(io->ctx, oth_lo(i)) & IB_SIZE_MASK) { +#ifndef CK_NET_MUTANT_NO_READBACK + ib_restore(io, saved); + return CK_PI5NET_E_READBACK; +#endif + } + } + return CK_PI5NET_OK; +} + +int ck_pi5net_confine_label(const struct ck_pi5pcie_dmav *dv, const struct ck_pi5net_ibprog *plan) +{ + if (!dv) + return CK_PI5NET_CONF_UNKNOWN; + switch (dv->verdict) { + case CK_PI5PCIE_DMA_CONFINED: + return CK_PI5NET_CONF_CONFINED; + case CK_PI5PCIE_DMA_WIDER: +#ifdef CK_NET_MUTANT_PARTIAL_ANY + (void)plan; + return CK_PI5NET_CONF_PARTIAL; /* would call a 64 GiB window "partial" */ +#else + if (plan && plan->extra_bytes != 0 && dv->outside_bytes == plan->extra_bytes && + dv->inside_bytes + dv->outside_bytes == plan->size) + return CK_PI5NET_CONF_PARTIAL; + return CK_PI5NET_CONF_NOT_CONFINED; +#endif + case CK_PI5PCIE_DMA_NO_WINDOW: + case CK_PI5PCIE_DMA_MISS: + case CK_PI5PCIE_DMA_PARTIAL: + return CK_PI5NET_CONF_BROKEN; + default: + return CK_PI5NET_CONF_UNKNOWN; + } +} + +const char *ck_pi5net_confine_name(int label) +{ + switch (label) { + case CK_PI5NET_CONF_CONFINED: return "CONFINED"; + case CK_PI5NET_CONF_PARTIAL: return "PARTIAL"; + case CK_PI5NET_CONF_NOT_CONFINED: return "NOT_CONFINED"; + case CK_PI5NET_CONF_BROKEN: return "BROKEN"; + default: return "UNKNOWN"; + } +} + +/* ---------------- B0. outbound window ---------------- */ + +int ck_pi5net_ob_read(const struct ck_pi5pcie_io *io, struct ck_pi5net_obwin *w) +{ + uint32_t bl, bh, lh, bus_lo, bus_hi; + uint64_t base_mb, limit_mb; + if (!io || !io->rd || !w) + return CK_PI5NET_E_ARG; + *w = (struct ck_pi5net_obwin){0}; + bl = io->rd(io->ctx, CK_PI5NET_OB0_BASE_LIMIT); + bh = io->rd(io->ctx, CK_PI5NET_OB0_BASE_HI) & 0xffu; + lh = io->rd(io->ctx, CK_PI5NET_OB0_LIMIT_HI) & 0xffu; + bus_lo = io->rd(io->ctx, CK_PI5NET_OB0_BUS_LO); + bus_hi = io->rd(io->ctx, CK_PI5NET_OB0_BUS_HI); + base_mb = ((uint64_t)bh << 12) | ((bl >> 4) & 0xfffu); + limit_mb = ((uint64_t)lh << 12) | ((bl >> 20) & 0xfffu); + w->bus_base = ((uint64_t)bus_hi << 32) | bus_lo; + w->cpu_base = base_mb << 20; +#ifdef CK_NET_MUTANT_OB_NO_LIMIT + w->size = (limit_mb - base_mb) << 20; /* off by one granule */ + w->valid = limit_mb >= base_mb; +#else + w->valid = limit_mb >= base_mb; + w->size = w->valid ? (limit_mb - base_mb + 1) << 20 : 0; +#endif + return CK_PI5NET_OK; +} + +uint64_t ck_pi5net_bus_to_cpu(const struct ck_pi5net_obwin *w, uint64_t bus, uint64_t len) +{ + if (!w || !w->valid || len == 0) + return 0; +#ifndef CK_NET_MUTANT_NO_BOUNDS + if (bus < w->bus_base || len > w->size || bus - w->bus_base > w->size - len) + return 0; +#endif + return w->cpu_base + (bus - w->bus_base); +} + +/* ---------------- B1. clocks ---------------- */ + +static uint32_t div_round_closest(uint64_t a, uint64_t b) { return b ? (uint32_t)((a + (b >> 1)) / b) : 0; } + +uint32_t ck_rp1_pll_core_hz(const struct ck_rp1_clk_regs *r) +{ + uint64_t v; + if (!(r->pll_cs & 0x80000000u)) /* PLL_CS_LOCK [L2] */ + return 0; + v = ((uint64_t)CK_RP1_XOSC_HZ * (((uint64_t)r->pll_fbdiv_int << 24) + r->pll_fbdiv_frac) + (1u << 23)) >> 24; + return v > 0xffffffffull ? 0 : (uint32_t)v; +} + +static const uint8_t sec_div_tab[32] = {19, 19, 19, 19, 19, 19, 19, 19, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19}; + +uint32_t ck_rp1_pll_sec_hz(const struct ck_rp1_clk_regs *r) +{ + uint32_t core = ck_rp1_pll_core_hz(r), d; + if (!core || (r->pll_sec & (1u << 16)) || !(r->pll_sec & 0x80000000u)) /* RST set, or divider not implemented */ + return 0; + d = sec_div_tab[(r->pll_sec >> 8) & 0x1fu]; + return (uint32_t)(((uint64_t)core + d - 1) / d); /* DIV_ROUND_UP like clk_divider_ops */ +} + +uint32_t ck_rp1_pll_pri_hz(const struct ck_rp1_clk_regs *r) +{ + uint32_t core = ck_rp1_pll_core_hz(r), d1 = (r->pll_prim >> 16) & 7u, d2 = (r->pll_prim >> 12) & 7u; + if (!core || !d1 || !d2) + return 0; + return div_round_closest(core, (uint64_t)d1 * d2); +} + +static uint32_t clk_div(uint32_t div_reg) { return div_reg ? div_reg : 1u << 16; } /* clk-rp1.c recalc: 0 = 2^16 */ + +uint32_t ck_rp1_clk_sys_hz(const struct ck_rp1_clk_regs *r) +{ + /* SEL is one-hot over the standard parents {xosc, -, pll_sys}; 0 = not switched yet: use CTRL.SRC (clk-rp1.c:1101-1114) */ + uint32_t sel = r->sys_sel, src, parent; + if (sel) + src = (uint32_t)__builtin_ctz(sel); + else + src = r->sys_ctrl & 0x3u; + if (src == 0) + parent = CK_RP1_XOSC_HZ; + else if (src == 2) + parent = ck_rp1_pll_pri_hz(r); + else + return 0; /* aux source: not modelled */ + return parent / clk_div(r->sys_div); +} + +static uint32_t aux_parent_hz(const struct ck_rp1_clk_regs *r, uint32_t aux, int eth) +{ + /* RP1_CLK_ETH aux parents: 0 pll_sys_sec, 1 pll_sys; RP1_CLK_ETH_TSU aux parents: 0 xosc [L2] */ + if (eth) + return aux == 0 ? ck_rp1_pll_sec_hz(r) : aux == 1 ? ck_rp1_pll_pri_hz(r) : 0; + return aux == 0 ? CK_RP1_XOSC_HZ : 0; +} + +uint32_t ck_rp1_clk_eth_hz(const struct ck_rp1_clk_regs *r) +{ + uint32_t p; + if (!(r->eth_ctrl & CK_RP1_CLK_ENABLE)) + return 0; + p = aux_parent_hz(r, (r->eth_ctrl & CK_RP1_CLK_AUXSRC_MASK) >> CK_RP1_CLK_AUXSRC_SHIFT, 1); + return p / clk_div(r->eth_div); +} + +uint32_t ck_rp1_clk_tsu_hz(const struct ck_rp1_clk_regs *r) +{ + uint32_t p; + if (!(r->tsu_ctrl & CK_RP1_CLK_ENABLE)) + return 0; + p = aux_parent_hz(r, (r->tsu_ctrl & CK_RP1_CLK_AUXSRC_MASK) >> CK_RP1_CLK_AUXSRC_SHIFT, 0); + return p / clk_div(r->tsu_div); +} + +void ck_rp1_clk_decide(const struct ck_rp1_clk_regs *r, struct ck_rp1_clk_plan *p) +{ + uint32_t sec; + *p = (struct ck_rp1_clk_plan){0}; + /* ETH (tx_clk, 125 MHz from pll_sys_sec) */ +#ifndef CK_NET_MUTANT_TOUCH_ON_CLOCK + if (r->eth_ctrl & CK_RP1_CLK_ENABLE) { + p->eth = CK_RP1_CLK_ALREADY_ON; + p->eth_why = "already enabled: left as found"; + } else +#endif + { + sec = ck_rp1_pll_sec_hz(r); + if (!sec) { + p->eth = CK_RP1_CLK_BLOCKED; + p->eth_why = "pll_sys_sec not running (core unlocked, divider in reset or not implemented)"; +#ifndef CK_NET_MUTANT_NO_EXACT_DIV + } else if (sec % CK_RP1_ETH_HZ != 0 || sec / CK_RP1_ETH_HZ == 0 || sec / CK_RP1_ETH_HZ > 255u) { +#else + } else if (sec / CK_RP1_ETH_HZ == 0 || sec / CK_RP1_ETH_HZ > 255u) { +#endif + p->eth = CK_RP1_CLK_BLOCKED; + p->eth_why = "pll_sys_sec is not an exact multiple (1..255) of 125 MHz"; + } else { + p->eth = CK_RP1_CLK_TO_ENABLE; + p->eth_div = sec / CK_RP1_ETH_HZ; + p->eth_ctrl = (r->eth_ctrl & ~(uint32_t)CK_RP1_CLK_AUXSRC_MASK) | CK_RP1_CLK_ENABLE; /* aux 0 = pll_sys_sec */ + p->eth_why = "disabled: enable from pll_sys_sec at 125 MHz"; + } + } + /* ETH_TSU (tsu_clk, 50 MHz from xosc) */ +#ifndef CK_NET_MUTANT_TOUCH_ON_CLOCK + if (r->tsu_ctrl & CK_RP1_CLK_ENABLE) { + p->tsu = CK_RP1_CLK_ALREADY_ON; + p->tsu_why = "already enabled: left as found"; + } else +#endif + { + p->tsu = CK_RP1_CLK_TO_ENABLE; + p->tsu_div = CK_RP1_XOSC_HZ / CK_RP1_TSU_HZ; + p->tsu_ctrl = (r->tsu_ctrl & ~(uint32_t)CK_RP1_CLK_AUXSRC_MASK) | CK_RP1_CLK_ENABLE; /* aux 0 = xosc */ + p->tsu_why = "disabled: enable from xosc at 50 MHz"; + } +} + +static uint32_t clk_rd(const struct ck_rp1_io *io, uint32_t off) { return io->rd(io->ctx, CK_RP1_CLOCKS + off); } +static void clk_wr(const struct ck_rp1_io *io, uint32_t off, uint32_t v) { io->wr(io->ctx, CK_RP1_CLOCKS + off, v); } + +void ck_rp1_clk_read(const struct ck_rp1_io *io, struct ck_rp1_clk_regs *r) +{ + r->pll_cs = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_CS); + r->pll_fbdiv_int = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_FBDIV_INT); + r->pll_fbdiv_frac = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_FBDIV_FRAC); + r->pll_prim = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_PRIM); + r->pll_sec = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_SEC); + r->sys_ctrl = clk_rd(io, CK_RP1_CLK_SYS_CTRL); + r->sys_div = clk_rd(io, CK_RP1_CLK_SYS_DIV); + r->sys_sel = clk_rd(io, CK_RP1_CLK_SYS_SEL); + r->eth_ctrl = clk_rd(io, CK_RP1_CLK_ETH_CTRL); + r->eth_div = clk_rd(io, CK_RP1_CLK_ETH_DIV); + r->eth_sel = clk_rd(io, CK_RP1_CLK_ETH_SEL); + r->tsu_ctrl = clk_rd(io, CK_RP1_CLK_TSU_CTRL); + r->tsu_div = clk_rd(io, CK_RP1_CLK_TSU_DIV); + r->tsu_sel = clk_rd(io, CK_RP1_CLK_TSU_SEL); +} + +static int clk_enable_one(const struct ck_rp1_io *io, uint32_t ctrl_off, uint32_t div_off, uint32_t div, uint32_t ctrl) +{ + clk_wr(io, div_off, div); /* divider first (clk-rp1.c set_rate_and_parent) */ + clk_wr(io, ctrl_off, ctrl & ~(uint32_t)CK_RP1_CLK_ENABLE); /* source (AUXSRC) while still off */ + clk_wr(io, ctrl_off, ctrl); /* then clock on (rp1_clock_on) */ +#ifdef CK_NET_MUTANT_NO_CLK_VERIFY + return 0; +#else + if (!(clk_rd(io, ctrl_off) & CK_RP1_CLK_ENABLE) || clk_rd(io, div_off) != div) + return CK_PI5NET_E_READBACK; + return 0; +#endif +} + +int ck_rp1_clk_apply(const struct ck_rp1_io *io, const struct ck_rp1_clk_plan *p) +{ + int rc = 0, e; + if (p->eth == CK_RP1_CLK_TO_ENABLE) { + e = clk_enable_one(io, CK_RP1_CLK_ETH_CTRL, CK_RP1_CLK_ETH_DIV, p->eth_div, p->eth_ctrl); + if (e) + rc = e; + } + if (p->tsu == CK_RP1_CLK_TO_ENABLE) { + e = clk_enable_one(io, CK_RP1_CLK_TSU_CTRL, CK_RP1_CLK_TSU_DIV, p->tsu_div, p->tsu_ctrl); + if (e) + rc = e; + } + return rc; +} + +/* ---------------- B2. PHY reset ---------------- */ + +static void delay_ms(const struct ck_rp1_io *io, uint32_t ms) +{ + if (io->delay_us && ms) + io->delay_us(io->ctx, ms * 1000u); +} + +int ck_rp1_phy_reset(const struct ck_rp1_io *io, struct ck_rp1_phyreset *out) +{ + uint32_t ctrl, pad; + struct ck_rp1_phyreset o = {0}; + int rc = 0; + o.ctrl_before = io->rd(io->ctx, CK_RP1_GPIO32_CTRL); + o.pad_before = io->rd(io->ctx, CK_RP1_GPIO32_PAD); + o.oe_before = io->rd(io->ctx, CK_RP1_RIO1_OE); + o.out_before = io->rd(io->ctx, CK_RP1_RIO1_OUT); + o.in_before = io->rd(io->ctx, CK_RP1_RIO1_IN); + /* 1. level high (reset released) before the pin is ours: no glitch low (pinctrl-rp1.c gpio_direction_output order) */ +#ifndef CK_NET_MUTANT_NO_HIGH_FIRST + io->wr(io->ctx, CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET, CK_RP1_GPIO_PIN_BIT); +#endif + /* 2. pad: input enabled (so we can read the pin), output not disabled */ + pad = io->rd(io->ctx, CK_RP1_GPIO32_PAD); + pad = (pad | CK_RP1_PAD_IN_ENABLE) & ~(uint32_t)CK_RP1_PAD_OUT_DISABLE; + io->wr(io->ctx, CK_RP1_GPIO32_PAD, pad); + /* 3. output enable */ + io->wr(io->ctx, CK_RP1_RIO1_OE + CK_RP1_ATOMIC_SET, CK_RP1_GPIO_PIN_BIT); + /* 4. function = gpio (RIO), output/oe overrides = peripheral (0) */ + ctrl = io->rd(io->ctx, CK_RP1_GPIO32_CTRL); + ctrl &= ~(0x1fu | 0x3000u | 0xc000u | 0x30000u); + ctrl |= CK_RP1_FSEL_GPIO; + io->wr(io->ctx, CK_RP1_GPIO32_CTRL, ctrl); + o.ctrl_after = io->rd(io->ctx, CK_RP1_GPIO32_CTRL); + o.pad_after = io->rd(io->ctx, CK_RP1_GPIO32_PAD); + if (!(io->rd(io->ctx, CK_RP1_RIO1_IN) & CK_RP1_GPIO_PIN_BIT)) + rc = CK_PI5NET_E_READBACK; /* we drive it high and it does not read high */ + /* 5. assert reset (line low, active low) */ +#ifndef CK_NET_MUTANT_NO_ASSERT + io->wr(io->ctx, CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_CLR, CK_RP1_GPIO_PIN_BIT); +#endif +#ifndef CK_NET_MUTANT_NO_HOLD + delay_ms(io, CK_RP1_PHY_RESET_MS); +#endif + o.in_low = io->rd(io->ctx, CK_RP1_RIO1_IN); + if (o.in_low & CK_RP1_GPIO_PIN_BIT) + rc = CK_PI5NET_E_READBACK; /* driven low but reads high */ + /* 6. release and let the PHY come up */ +#ifndef CK_NET_MUTANT_NO_RELEASE + io->wr(io->ctx, CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET, CK_RP1_GPIO_PIN_BIT); +#endif + delay_ms(io, CK_RP1_PHY_SETTLE_MS); + o.in_high = io->rd(io->ctx, CK_RP1_RIO1_IN); + if (!(o.in_high & CK_RP1_GPIO_PIN_BIT)) + rc = CK_PI5NET_E_READBACK; + if (out) + *out = o; + return rc; +} + +/* ---------------- B3. MDIO ---------------- */ + +uint32_t ck_gem_mdc_div_code(uint32_t pclk_hz) +{ + if (pclk_hz == 0) + return 7; /* unknown rate: slowest MDC */ +#ifdef CK_NET_MUTANT_NO_MDC_DIV + return 7; +#else + if (pclk_hz <= 20000000u) return 0; + if (pclk_hz <= 40000000u) return 1; + if (pclk_hz <= 80000000u) return 2; + if (pclk_hz <= 120000000u) return 3; + if (pclk_hz <= 160000000u) return 4; + if (pclk_hz <= 240000000u) return 5; + if (pclk_hz <= 320000000u) return 6; + return 7; +#endif +} + +uint32_t ck_gem_mdio_frame_read(uint32_t phy, uint32_t reg) +{ + return (1u << 30) | (2u << 28) | ((phy & 31u) << 23) | ((reg & 31u) << 18) | (2u << 16); +} + +int ck_gem_mdio_enable(const struct ck_rp1_io *io, uint32_t pclk_hz) +{ + uint32_t ncr = io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCR), cfg = io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCFGR); + uint32_t code = ck_gem_mdc_div_code(pclk_hz); + cfg = (cfg & ~(uint32_t)CK_GEM_NCFGR_CLK_MASK) | (code << CK_GEM_NCFGR_CLK_SHIFT); + io->wr(io->ctx, CK_RP1_ETH + CK_GEM_NCFGR, cfg); /* divider before the port is enabled */ +#ifndef CK_NET_MUTANT_NO_MPE + io->wr(io->ctx, CK_RP1_ETH + CK_GEM_NCR, ncr | CK_GEM_NCR_MPE); +#else + (void)ncr; +#endif + if (!(io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCR) & CK_GEM_NCR_MPE) || + ((io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCFGR) & CK_GEM_NCFGR_CLK_MASK) >> CK_GEM_NCFGR_CLK_SHIFT) != code) + return CK_PI5NET_E_READBACK; + return 0; +} + +static int mdio_wait_idle(const struct ck_rp1_io *io) +{ + uint32_t i; + 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_PI5NET_E_TIMEOUT; +} + +int ck_gem_mdio_read(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t *val) +{ + if (!io || !val || phy > 31u || reg > 31u) + return CK_PI5NET_E_ARG; + if (mdio_wait_idle(io)) + return CK_PI5NET_E_TIMEOUT; + io->wr(io->ctx, CK_RP1_ETH + CK_GEM_MAN, ck_gem_mdio_frame_read(phy, reg)); +#ifndef CK_NET_MUTANT_NO_IDLE_AFTER + if (mdio_wait_idle(io)) + return CK_PI5NET_E_TIMEOUT; +#endif + *val = (uint16_t)(io->rd(io->ctx, CK_RP1_ETH + CK_GEM_MAN) & 0xffffu); + return 0; +} + +void ck_phy_id_decode(uint16_t r2, uint16_t r3, struct ck_phy_id *o) +{ + uint8_t b[25] = {0}; /* b[1]..b[24] = OUI bits 1..24 (IEEE 802.3 22.2.4.3.1) */ + uint32_t i, k, oui = 0; + *o = (struct ck_phy_id){0}; + o->id = ((uint32_t)r2 << 16) | r3; + /* reg 2 bit 15..0 = OUI bit 3..18; reg 3 bit 15..10 = OUI bit 19..24 */ + for (i = 0; i < 16; i++) + b[3 + i] = (uint8_t)((r2 >> (15 - i)) & 1u); + for (i = 0; i < 6; i++) + b[19 + i] = (uint8_t)((r3 >> (15 - i)) & 1u); + for (k = 0; k < 3; k++) { + uint32_t oct = 0; + for (i = 0; i < 8; i++) +#ifdef CK_NET_MUTANT_OUI_ORDER + oct |= (uint32_t)b[8 * k + 1 + i] << (7 - i); /* wrong bit order within the octet */ +#else + oct |= (uint32_t)b[8 * k + 1 + i] << i; /* octet's bit 0 is the first transmitted */ +#endif + oui = (oui << 8) | oct; + } + o->oui = oui; + o->model = (uint8_t)((r3 >> 4) & 0x3fu); + o->rev = (uint8_t)(r3 & 0xfu); + if (o->id == 0xffffffffu || o->id == 0 || r2 == 0xffffu) + o->cls = CK_PHY_ABSENT; +#ifndef CK_NET_MUTANT_BAD_CLASS + else if (o->id == CK_PHY_ID_BCM54213PE) + o->cls = CK_PHY_BCM54213PE; +#else + else if ((o->id & 0xfffffff0u) == (CK_PHY_ID_BCM54213PE & 0xfffffff0u)) + o->cls = CK_PHY_BCM54213PE; /* would also accept BCM54210E and any revision: Linux matches exactly */ +#endif + else if (o->oui == 0x18c086u || o->oui == 0x001018u) /* both are "Broadcom" in the IEEE registry */ + o->cls = CK_PHY_BROADCOM_OTHER; + else + o->cls = CK_PHY_OTHER; +} + +const char *ck_phy_class_name(int cls) +{ + switch (cls) { + case CK_PHY_ABSENT: return "ABSENT"; + case CK_PHY_BCM54213PE: return "BCM54213PE"; + case CK_PHY_BROADCOM_OTHER: return "BROADCOM_OTHER"; + default: return "OTHER"; + } +} + +int ck_phy_find(const struct ck_rp1_io *io, uint32_t addr, uint32_t tries, uint32_t gap_ms, struct ck_phy_id *id, + uint32_t *attempts, int *scan_hit) +{ + uint32_t t, a; + uint16_t r2 = 0xffff, r3 = 0xffff; + if (attempts) + *attempts = 0; + if (scan_hit) + *scan_hit = -1; + if (!io || !id || addr > 31u || tries == 0) + return CK_PI5NET_E_ARG; + ck_phy_id_decode(0xffff, 0xffff, id); + for (t = 0; t < tries; t++) { + int e2, e3; + if (attempts) + *attempts = t + 1; + e2 = ck_gem_mdio_read(io, addr, 2, &r2); + e3 = ck_gem_mdio_read(io, addr, 3, &r3); + if (e2 || e3) + return e2 ? e2 : e3; + if (r2 != 0xffffu && !(r2 == 0 && r3 == 0)) { + ck_phy_id_decode(r2, r3, id); + return 0; + } + delay_ms(io, gap_ms); + } + if (scan_hit) { + for (a = 0; a < 32u; a++) { + if (ck_gem_mdio_read(io, a, 2, &r2) == 0 && r2 != 0xffffu && r2 != 0) { + *scan_hit = (int)a; + break; + } + } + } + return CK_PI5NET_E_TIMEOUT; +} + +/* ================= image glue (CK_PI5_NET only) ================= */ +#ifdef CK_PI5_NET +#include "ck.h" +#include "dma_window.h" + +static volatile uint8_t *rc; +static volatile uint8_t *rp1; + +static uint32_t rc_rd(void *c, uint32_t off) { (void)c; return *(volatile uint32_t *)(rc + off); } +static void rc_wr(void *c, uint32_t off, uint32_t v) { (void)c; *(volatile uint32_t *)(rc + off) = v; ck_mb(); } +static uint32_t rp_rd(void *c, uint32_t off) { (void)c; return *(volatile uint32_t *)(rp1 + off); } +static void rp_wr(void *c, uint32_t off, uint32_t v) { (void)c; *(volatile uint32_t *)(rp1 + off) = v; ck_mb(); } +static void rp_delay(void *c, uint32_t us) +{ + (void)c; + while (us > 1000u) { + ck_udelay(1000u); + us -= 1000u; + } + if (us) + ck_udelay(us); +} + +static uint32_t cmd_dword(const struct ck_pi5pcie_io *io) +{ + uint32_t v = 0; + (void)ck_pi5pcie_cfg_read32(io, 1, 0, 0, 4, &v); + return v; +} + +static void print_wins(const char *tag, const struct ck_pi5pcie_io *io, struct ck_pi5pcie_ibwin *win) +{ + uint32_t i; + for (i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++) { + if (ck_pi5pcie_ib_read(io, i, &win[i])) + continue; + if (win[i].enabled) + ck_printf("pi5_net: inbound %s win%u bus=0x%llx size=0x%llx cpu=0x%llx remap=%u valid=%u\n", tag, i, + (unsigned long long)win[i].bus_base, (unsigned long long)win[i].size, + (unsigned long long)win[i].cpu_base, win[i].remap_en, win[i].size_valid); + } +} + +static uint32_t unmodelled_windows(void) +{ + uint32_t u = 0; + if (rc_rd(0, CK_PI5PCIE_RC_BAR1_LO) & CK_PI5PCIE_BAR_SIZE_MASK) + u++; + if (rc_rd(0, CK_PI5PCIE_RC_BAR3_LO) & CK_PI5PCIE_BAR_SIZE_MASK) + u++; + return u; +} + +static void 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; + ck_pi5pcie_ib_verdict(win, CK_PI5PCIE_IB_WINDOWS, unmod, b_phys, b_size, &dv); + 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, + (unsigned long long)dv.inside_bytes, (unsigned long long)dv.outside_bytes); +} + +static void print_clk_rates(const char *tag, const struct ck_rp1_clk_regs *r) +{ + ck_printf("pi5_net: rp1 clocks %s: pll_core=%u pll_sec=%u clk_sys=%u clk_eth=%u clk_eth_tsu=%u Hz (0 = off/unknown)\n", + tag, ck_rp1_pll_core_hz(r), ck_rp1_pll_sec_hz(r), ck_rp1_clk_sys_hz(r), ck_rp1_clk_eth_hz(r), + ck_rp1_clk_tsu_hz(r)); +} + +void ck_pi5net_run(void) +{ + static const struct ck_pi5pcie_io io = {rc_rd, rc_wr, 0}; + static const struct ck_rp1_io rio = {rp_rd, rp_wr, rp_delay, 0}; + struct ck_pi5pcie_func root, rp1f; + struct ck_pi5pcie_ibwin win[CK_PI5PCIE_IB_WINDOWS]; + struct ck_pi5net_ibprog plan; + struct ck_pi5net_ibsaved saved; + struct ck_pi5net_obwin ob; + struct ck_pi5pcie_bar bar1; + struct ck_rp1_clk_regs cr; + struct ck_rp1_clk_plan cp; + struct ck_rp1_phyreset pr; + struct ck_phy_id pid; + struct ck_dmaw dw; + 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; + 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: 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) { + ck_printf("pi5_net: FAIL map root complex 0x%llx (refused); nothing done\n", + (unsigned long long)CK_PI5PCIE_RC2_PHYS); + return; + } + rrc = ck_pi5pcie_read_func(&io, 0, 0, 0, &root); + rrp1 = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1f); + ver = ck_pi5pcie_rp1_check(rrp1, &rp1f); + if (rrc == 0) + ck_printf("pi5_net: root 00:00.0 %04x:%04x class=%06x sec=%u sub=%u\n", root.vendor, root.device, + root.class_code, root.secondary, root.subordinate); + ck_printf("pi5_net: rp1 01:00.0 %04x:%04x %s cmd=0x%04x\n", rp1f.vendor, rp1f.device, + ver == CK_PI5PCIE_RP1_OK ? "MATCH" : "NOT_RP1", rp1f.command); + if (ver != CK_PI5PCIE_RP1_OK) { + ck_printf("pi5_net: STOP rp1 not present at 01:00.0; nothing programmed\n"); + return; + } + + /* ---------------- A. confine the inbound window ---------------- */ + bounce = ck_dma_alloc(CK_DMAW_MAX_BYTES, CK_DMAW_MAX_BYTES, &b_phys); + if (!bounce || ck_dmaw_init(&dw, bounce, b_phys, CK_DMAW_MAX_BYTES) != CK_DMAW_OK) { + ck_printf("pi5_dma: FAIL no bounce window (alloc/init); window left as firmware set it\n"); + } else { + ck_printf("pi5_dma: bounce cpu=0x%llx size=0x%llx (ADR 0020 rule 1)\n", (unsigned long long)b_phys, + (unsigned long long)dw.size); + unmod = unmodelled_windows(); + print_wins("before", &io, win); + print_verdict("before", win, unmod, b_phys, dw.size, 0); + rc2 = ck_pi5net_ib_plan(b_phys, dw.size, &plan); + if (rc2) { + ck_printf("pi5_dma: FAIL plan rc=%d; window left as firmware set it\n", rc2); + } else { + ck_printf("pi5_dma: plan bus=0x%llx cpu=0x%llx size=0x%llx code=%u extra_beyond_bounce=0x%llx\n", + (unsigned long long)plan.bus_base, (unsigned long long)plan.cpu_base, + (unsigned long long)plan.size, plan.size_code, (unsigned long long)plan.extra_bytes); + rc2 = ck_pi5net_rp1_quiesce(&io, &cmd_saved); + ck_printf("pi5_dma: rp1 bus-master off for the window change: rc=%d cmd_was=0x%04x cmd_now=0x%08x\n", rc2, + cmd_saved, cmd_dword(&io)); + if (rc2 == 0) { + ck_udelay(2000); /* let anything already on the wire finish */ + rc2 = ck_pi5net_ib_apply(&io, &plan, &saved, &ndis); + ck_printf("pi5_dma: apply rc=%d (0 = window now matches the plan); other_windows_disabled=%u\n", rc2, + ndis); + if (rc2) + ck_printf("pi5_dma: readback LO=0x%08x HI=0x%08x REMAP_LO=0x%08x REMAP_HI=0x%08x (firmware window restored)\n", + rc_rd(0, CK_PI5NET_IB0_LO), rc_rd(0, CK_PI5NET_IB0_HI), + rc_rd(0, CK_PI5NET_IB0_REMAP_LO), rc_rd(0, CK_PI5NET_IB0_REMAP_HI)); + } else { + ck_printf("pi5_dma: STOP rp1 would not stop bus mastering; window left as firmware set it\n"); + } + unmod = unmodelled_windows(); + print_wins("after", &io, win); + print_verdict("after", win, unmod, b_phys, dw.size, &plan); + 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)); + } + } + + /* ---------------- B. RP1 Ethernet up to the PHY ---------------- */ + if (ck_pi5net_ob_read(&io, &ob) != 0 || !ob.valid) { + ck_printf("pi5_net: STOP outbound window 0 unreadable; RP1 not touched\n"); + return; + } + ck_printf("pi5_net: outbound win0 bus=0x%llx cpu=0x%llx size=0x%llx\n", (unsigned long long)ob.bus_base, + (unsigned long long)ob.cpu_base, (unsigned long long)ob.size); + (void)ck_pi5pcie_bar_decode(rp1f.bar_raw, 1, rp1f.nbars, &bar1); + if (bar1.kind != CK_PI5PCIE_BAR_MEM32 && bar1.kind != CK_PI5PCIE_BAR_MEM64) { + ck_printf("pi5_net: STOP rp1 bar1 is not a memory BAR; RP1 not touched\n"); + return; + } + bar1_cpu = ck_pi5net_bus_to_cpu(&ob, bar1.addr, CK_RP1_BAR1_MAP_BYTES); + if (!bar1_cpu) { + ck_printf("pi5_net: STOP rp1 bar1 bus 0x%llx is outside outbound win0; RP1 not touched\n", + (unsigned long long)bar1.addr); + return; + } + rp1 = (volatile uint8_t *)ck_mmio_try_map(bar1_cpu, CK_RP1_BAR1_MAP_BYTES); + if (!rp1) { + ck_printf("pi5_net: STOP map rp1 bar1 cpu 0x%llx refused; RP1 not touched\n", (unsigned long long)bar1_cpu); + return; + } + ck_printf("pi5_net: rp1 bar1 bus=0x%llx -> cpu=0x%llx chip_id=0x%08x platform=0x%08x\n", + (unsigned long long)bar1.addr, (unsigned long long)bar1_cpu, rp_rd(0, CK_RP1_SYSINFO), + rp_rd(0, CK_RP1_SYSINFO + 4)); + if (rp_rd(0, CK_RP1_SYSINFO) == 0xffffffffu) { + ck_printf("pi5_net: STOP rp1 reads all ones (link or window problem); nothing programmed\n"); + return; + } + ck_printf("pi5_net: gem before: MID=0x%08x NCR=0x%08x NCFGR=0x%08x NSR=0x%08x eth_cfg CONTROL=0x%08x CLKGEN=0x%08x\n", + rp_rd(0, CK_RP1_ETH + CK_GEM_MID), rp_rd(0, CK_RP1_ETH + CK_GEM_NCR), + rp_rd(0, CK_RP1_ETH + CK_GEM_NCFGR), rp_rd(0, CK_RP1_ETH + CK_GEM_NSR), + rp_rd(0, CK_RP1_ETH_CFG + 0x00), rp_rd(0, CK_RP1_ETH_CFG + 0x14)); + ck_rp1_clk_read(&rio, &cr); + ck_printf("pi5_net: rp1 clock regs: PLL cs=0x%08x fbint=0x%08x frac=0x%08x prim=0x%08x sec=0x%08x | SYS ctrl=0x%08x div=%u sel=0x%x | ETH ctrl=0x%08x div=%u sel=0x%x | TSU ctrl=0x%08x div=%u sel=0x%x\n", + cr.pll_cs, cr.pll_fbdiv_int, cr.pll_fbdiv_frac, cr.pll_prim, cr.pll_sec, cr.sys_ctrl, cr.sys_div, + cr.sys_sel, cr.eth_ctrl, cr.eth_div, cr.eth_sel, cr.tsu_ctrl, cr.tsu_div, cr.tsu_sel); + print_clk_rates("before", &cr); + ck_rp1_clk_decide(&cr, &cp); + ck_printf("pi5_net: clk_eth: %s (%s) | clk_eth_tsu: %s (%s)\n", + cp.eth == CK_RP1_CLK_ALREADY_ON ? "ALREADY_ON" : cp.eth == CK_RP1_CLK_TO_ENABLE ? "ENABLE" : "BLOCKED", + cp.eth_why, cp.tsu == CK_RP1_CLK_ALREADY_ON ? "ALREADY_ON" : cp.tsu == CK_RP1_CLK_TO_ENABLE ? "ENABLE" : "BLOCKED", + cp.tsu_why); + rc2 = ck_rp1_clk_apply(&rio, &cp); + ck_rp1_clk_read(&rio, &cr); + print_clk_rates("after", &cr); + ck_printf("pi5_net: clock apply rc=%d\n", rc2); + if (cp.eth == CK_RP1_CLK_BLOCKED) { + ck_printf("pi5_net: STOP clk_eth blocked; PHY not touched\n"); + return; + } + + rc2 = ck_rp1_phy_reset(&rio, &pr); + ck_printf("pi5_net: phy reset gpio32 rc=%d ctrl %08x->%08x pad %08x->%08x oe_before=%08x out_before=%08x in: before=%08x low=%08x high=%08x\n", + rc2, pr.ctrl_before, pr.ctrl_after, pr.pad_before, pr.pad_after, pr.oe_before, pr.out_before, + pr.in_before, pr.in_low, pr.in_high); + if (rc2) { + ck_printf("pi5_net: STOP gpio32 did not follow the reset sequence; PHY not read\n"); + return; + } + rc2 = ck_gem_mdio_enable(&rio, ck_rp1_clk_sys_hz(&cr)); + ck_printf("pi5_net: mdio enable rc=%d NCR=0x%08x NCFGR=0x%08x (MDC divider code %u for pclk %u Hz)\n", rc2, + rp_rd(0, CK_RP1_ETH + CK_GEM_NCR), rp_rd(0, CK_RP1_ETH + CK_GEM_NCFGR), + ck_gem_mdc_div_code(ck_rp1_clk_sys_hz(&cr)), ck_rp1_clk_sys_hz(&cr)); + if (rc2) + return; + rc2 = ck_phy_find(&rio, 1, 20, 25, &pid, &attempts, &scan_hit); + if (rc2 == 0) + ck_printf("pi5_net: phy addr=1 id=0x%08x oui=%02x-%02x-%02x model=0x%02x rev=%u class=%s attempts=%u\n", pid.id, + (pid.oui >> 16) & 0xff, (pid.oui >> 8) & 0xff, pid.oui & 0xff, pid.model, pid.rev, + 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"); +} +#endif diff --git a/native/kernel/dev/pi5_net.h b/native/kernel/dev/pi5_net.h new file mode 100644 index 00000000..29f14b9e --- /dev/null +++ b/native/kernel/dev/pi5_net.h @@ -0,0 +1,270 @@ +/* pi5_net.h -- Raspberry Pi 5 network lane, cut 2: (A) hardware confinement of the + * BCM2712 PCIe2 inbound DMA window to the ADR 0020 bounce region, (B) RP1 Ethernet + * bring-up as far as the PHY: RP1 clocks the GEM needs, PHY reset on RP1 GPIO32, + * MDIO read of the PHY ID. NO frames, NO DMA descriptors, NO GEM transmit/receive. + * + * The decision and register-sequence logic here is hosted-testable over fake + * registers (tests/test_pi5_net.c, with mutants). The MMIO glue at the bottom of + * pi5_net.c is the CK_PI5_NET image only. A host test proves the decisions, NOT the + * hardware. Every number below cites its source; anything not read is UNVERIFIED. + * + * SOURCES (all read in this cut): + * [E1] edk2-platforms 8e1779b (aien-dev/rpi5-uefi v0.3 c1ca184 pins it), + * Silicon/Broadcom/Bcm27xx/Library/Bcm2712PciHostBridgeLib/Bcm2712PciHostBridge.c + * PcieEncodeInboundSize + PcieSetupInboundWindow (size code, LO/HI, REMAP_LO/HI), + * PcieSetupOutboundWindow (MEM_WIN_LO/HI, BASE_LIMIT, BASE_HI, LIMIT_HI). + * [E2] Silicon/Broadcom/Bcm27xx/Include/IndustryStandard/Bcm2712Pcie.h register offsets + * and Include/IndustryStandard/Bcm2712.h BCM2712_BRCMSTB_PCIE2_CPU_MEM_BASE 0x1f00000000; + * RpiPlatformDxe/ConfigTable.h PCI_RESERVED_MEM32_BASE 0xC0000000 (bus) size 1 GiB. + * [E3] Silicon/RaspberryPi/RpiSiliconPkg/Include/Rp1.h block offsets inside RP1 BAR1. + * [L1] raspberrypi/linux rpi-6.12.y drivers/pci/controller/pcie-brcmstb.c + * set_inbound_win_registers (LO write with size code, HI, remap with ACCESS_EN bit 0). + * [L2] drivers/clk/clk-rp1.c (register offsets, CLK_CTRL_ENABLE bit 11, AUXSRC bits 9:5, + * PLL fields, PLL_SEC divider table, RP1_CLK_ETH / RP1_CLK_ETH_TSU / RP1_CLK_SYS). + * [L3] arch/arm/boot/dts/broadcom (rp1.dtsi, bcm2712-rpi-5-b.dts): rp1_eth clocks pclk/hclk + * = RP1_CLK_SYS, tsu_clk = RP1_CLK_ETH_TSU 50 MHz, tx_clk = RP1_CLK_ETH 125 MHz, + * phy-reset-gpios = rp1_gpio 32 GPIO_ACTIVE_LOW, phy-reset-duration 5 ms, phy reg 1. + * [L4] drivers/pinctrl/pinctrl-rp1.c (GPIO CTRL/PAD/RIO layout, bank table, FSEL gpio = 5). + * [L5] drivers/net/ethernet/cadence/macb_main.c + macb.h (MAN frame, NSR.IDLE, NCR.MPE, + * GEM MDC divider table, MID register). + * [L6] drivers/net/phy/broadcom.c + include/linux/brcmphy.h PHY_ID_BCM54213PE 0x600d84a2, + * phy_id_mask 0xffffffff. + * [D1] RP1 peripherals datasheet RP-008370-DS-1: section 2.2 (PCIe outbound direct mapped + * space), chapter 7 (GEM_GXL, RGMII, ETH_CFG registers). */ +#ifndef AIENOS_CK_PI5_NET_H +#define AIENOS_CK_PI5_NET_H +#include +#include +#include "pi5_pcie.h" + +enum { + CK_PI5NET_OK = 0, + CK_PI5NET_E_ARG = -1, + CK_PI5NET_E_RANGE = -2, /* window cannot be expressed by the hardware */ + CK_PI5NET_E_QUIESCE = -3, /* RP1 bus mastering did not read back as off */ + CK_PI5NET_E_READBACK = -4, /* a register did not read back what was written */ + CK_PI5NET_E_TIMEOUT = -5, + CK_PI5NET_E_NOMAP = -6 /* BAR/window address could not be derived */ +}; + +/* ============ A. inbound window confinement ============ */ + +/* Registers (offset from the RC base) [E2] Bcm2712Pcie.h:90-185. Window 0 is BAR2. */ +#define CK_PI5NET_IB0_LO 0x4034u +#define CK_PI5NET_IB0_HI 0x4038u +#define CK_PI5NET_IB0_REMAP_LO 0x40b4u +#define CK_PI5NET_IB0_REMAP_HI 0x40b8u +#define CK_PI5NET_REMAP_ENABLE 0x1u +#define CK_PI5NET_REMAP_LO_MASK 0xfffff000u +#define CK_PI5NET_REMAP_HI_MASK 0xffu /* CPU address is 40 bits */ +#define CK_PI5NET_PCI_COMMAND 0x04u /* config dword 1: status[31:16] | command[15:0] */ +#define CK_PI5NET_CMD_BME 0x0004u /* bus master enable */ + +/* The smallest hardware-expressible window that contains [b_cpu, b_cpu+b_size). + * Bus address == CPU address (identity), as the firmware's own window 0 is + * (bus 0 -> cpu 0) and as ck_dma_alloc documents for the bounce region. */ +struct ck_pi5net_ibprog { + uint64_t bus_base, cpu_base, size; /* window; bus_base == cpu_base */ + uint32_t size_code; /* the 5-bit code [E1] PcieEncodeInboundSize */ + uint32_t lo, hi, remap_lo, remap_hi; /* register values to write */ + uint64_t extra_bytes; /* size - b_size: reach beyond the bounce region */ +}; +/* Pure. E_ARG for size 0 / overflow; E_RANGE if the window would exceed 64 GiB, + * the CPU address passes 40 bits, or the size has no code. */ +int ck_pi5net_ib_plan(uint64_t b_cpu, uint64_t b_size, struct ck_pi5net_ibprog *p); +/* bytes -> 5-bit size code ([E1]: 4K..32K = 0x1c.., 64K..64G = log2-15); 0 = not expressible */ +uint32_t ck_pi5net_ib_size_code(uint64_t size); + +/* Write a config dword of 01:00.0 through the RC window (INDEX then DATA+reg). */ +int ck_pi5net_cfg_write32(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, uint32_t reg, + uint32_t v); +/* Clear RP1's PCI bus-master-enable so no new upstream DMA can start, and verify it + * read back off. *saved = the command register as found. Status bits (RW1C) are never + * written back. E_QUIESCE if the readback still shows BME. */ +int ck_pi5net_rp1_quiesce(const struct ck_pi5pcie_io *io, uint16_t *saved); + +/* Saved raw registers of window 0, to put the firmware's window back on failure. */ +struct ck_pi5net_ibsaved { + uint32_t lo, hi, remap_lo, remap_hi; + uint32_t other_lo[CK_PI5PCIE_IB_WINDOWS]; /* index 1.. used */ +}; +/* Reprogram window 0 to `p` and disable every other enabled inbound window. Order: + * window off (size code 0), HI, REMAP, LO (size code, enables last). Everything is read + * back; on any mismatch the saved values are written back and E_READBACK returned. + * `*n_disabled` = how many other windows were switched off. */ +int ck_pi5net_ib_apply(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibprog *p, + struct ck_pi5net_ibsaved *saved, uint32_t *n_disabled); + +/* Plain label for the boot line, from the verdict computed over what the registers read back as. CONFINED = + * reach is exactly the bounce region. PARTIAL = reach is the smallest window the hardware can express (it + * equals `plan`, so the extra bytes are unavoidable) and nothing else. NOT_CONFINED = anything wider. + * BROKEN = the window misses or does not fully cover the bounce region (or no window at all). */ +enum { CK_PI5NET_CONF_CONFINED = 0, CK_PI5NET_CONF_PARTIAL = 1, CK_PI5NET_CONF_NOT_CONFINED = 2, CK_PI5NET_CONF_BROKEN = 3, + CK_PI5NET_CONF_UNKNOWN = 4 }; +int ck_pi5net_confine_label(const struct ck_pi5pcie_dmav *dv, const struct ck_pi5net_ibprog *plan); +const char *ck_pi5net_confine_name(int label); + +/* ============ B0. where RP1 BAR1 is, as the CPU sees it ============ */ + +/* Outbound CPU->PCIe window 0 as programmed by firmware [E1] PcieSetupOutboundWindow. */ +#define CK_PI5NET_OB0_BUS_LO 0x400cu +#define CK_PI5NET_OB0_BUS_HI 0x4010u +#define CK_PI5NET_OB0_BASE_LIMIT 0x4070u +#define CK_PI5NET_OB0_BASE_HI 0x4080u +#define CK_PI5NET_OB0_LIMIT_HI 0x4084u +struct ck_pi5net_obwin { + uint64_t bus_base, cpu_base, size; /* size = limit - base + 1MB granule */ + uint8_t valid; +}; +int ck_pi5net_ob_read(const struct ck_pi5pcie_io *io, struct ck_pi5net_obwin *w); +/* CPU address of a PCI bus address inside the outbound window; 0 when outside. */ +uint64_t ck_pi5net_bus_to_cpu(const struct ck_pi5net_obwin *w, uint64_t bus, uint64_t len); + +/* ============ B1. RP1 blocks and the clocks the GEM needs ============ */ + +/* Offsets inside RP1 BAR1 [E3] Rp1.h. */ +#define CK_RP1_SYSINFO 0x00000u +#define CK_RP1_CLOCKS 0x18000u +#define CK_RP1_PLL_SYS 0x20000u +#define CK_RP1_IO_BANK1 0xd4000u +#define CK_RP1_RIO1 0xe4000u +#define CK_RP1_PADS_BANK1 0xf4000u +#define CK_RP1_ETH 0x100000u +#define CK_RP1_ETH_CFG 0x104000u +#define CK_RP1_BAR1_MAP_BYTES 0x108000u /* through ETH_CFG + 0x4000 */ + +/* Clock registers: offset from CK_RP1_CLOCKS (clk-rp1.c:32-67,119-121) and from CK_RP1_PLL_SYS + * for the PLL (clk-rp1.c:28-33; PLL_SYS_CS is 0x08000 from the clocks base = 0x20000 from BAR1). */ +#define CK_RP1_CLK_SYS_CTRL 0x014u +#define CK_RP1_CLK_SYS_DIV 0x018u +#define CK_RP1_CLK_SYS_SEL 0x020u +#define CK_RP1_CLK_ETH_CTRL 0x064u +#define CK_RP1_CLK_ETH_DIV 0x068u +#define CK_RP1_CLK_ETH_SEL 0x070u +#define CK_RP1_CLK_TSU_CTRL 0x134u +#define CK_RP1_CLK_TSU_DIV 0x138u +#define CK_RP1_CLK_TSU_SEL 0x140u +#define CK_RP1_PLL_CS 0x00u +#define CK_RP1_PLL_FBDIV_INT 0x08u +#define CK_RP1_PLL_FBDIV_FRAC 0x0cu +#define CK_RP1_PLL_PRIM 0x10u +#define CK_RP1_PLL_SEC 0x14u +#define CK_RP1_XOSC_HZ 50000000u /* clk-rp1 DT: XOSC 50 MHz ("Must match the XOSC frequency") */ +#define CK_RP1_CLK_ENABLE (1u << 11) +#define CK_RP1_CLK_AUXSRC_MASK 0x3e0u +#define CK_RP1_CLK_AUXSRC_SHIFT 5 +#define CK_RP1_ETH_HZ 125000000u /* assigned-clock-rates RP1_CLK_ETH */ +#define CK_RP1_TSU_HZ 50000000u /* assigned-clock-rates RP1_CLK_ETH_TSU */ + +/* Raw register snapshot the plan works from. */ +struct ck_rp1_clk_regs { + uint32_t pll_cs, pll_fbdiv_int, pll_fbdiv_frac, pll_prim, pll_sec; + uint32_t sys_ctrl, sys_div, sys_sel; + uint32_t eth_ctrl, eth_div, eth_sel; + uint32_t tsu_ctrl, tsu_div, tsu_sel; +}; +/* Rates in Hz from the snapshot; 0 = off / unlocked / not computable. */ +uint32_t ck_rp1_pll_core_hz(const struct ck_rp1_clk_regs *r); +uint32_t ck_rp1_pll_sec_hz(const struct ck_rp1_clk_regs *r); +uint32_t ck_rp1_pll_pri_hz(const struct ck_rp1_clk_regs *r); +uint32_t ck_rp1_clk_sys_hz(const struct ck_rp1_clk_regs *r); +uint32_t ck_rp1_clk_eth_hz(const struct ck_rp1_clk_regs *r); /* 0 if disabled */ +uint32_t ck_rp1_clk_tsu_hz(const struct ck_rp1_clk_regs *r); + +enum { CK_RP1_CLK_ALREADY_ON = 0, CK_RP1_CLK_TO_ENABLE = 1, CK_RP1_CLK_BLOCKED = 2 }; +struct ck_rp1_clk_plan { + int eth, tsu; /* the three states above */ + uint32_t eth_div, tsu_div; /* DIV_INT to write when TO_ENABLE */ + uint32_t eth_ctrl, tsu_ctrl; /* CTRL value to write (ENABLE set) when TO_ENABLE */ + const char *eth_why, *tsu_why; /* why BLOCKED / how decided */ +}; +/* A clock that is already enabled is left exactly as found (its rate is reported, never + * changed). A disabled clock is programmed to the DT assigned rate only if its source is + * running and gives an exact integer divide; the PLLs are never touched. */ +void ck_rp1_clk_decide(const struct ck_rp1_clk_regs *r, struct ck_rp1_clk_plan *p); + +/* Register access to RP1 BAR1 (offset from BAR1 base). */ +struct ck_rp1_io { + uint32_t (*rd)(void *ctx, uint32_t off); + void (*wr)(void *ctx, uint32_t off, uint32_t v); + void (*delay_us)(void *ctx, uint32_t us); + void *ctx; +}; +void ck_rp1_clk_read(const struct ck_rp1_io *io, struct ck_rp1_clk_regs *r); +/* Program the clocks the plan marks TO_ENABLE (DIV_INT, then CTRL with AUXSRC, then ENABLE) and + * read back. 0 = ok; E_READBACK if ENABLE does not stick. */ +int ck_rp1_clk_apply(const struct ck_rp1_io *io, const struct ck_rp1_clk_plan *p); + +/* ============ B2. PHY reset on RP1 GPIO32 ============ */ + +#define CK_RP1_PHY_RESET_GPIO 32u +#define CK_RP1_PHY_RESET_MS 5u /* [L3] phy-reset-duration */ +#define CK_RP1_PHY_SETTLE_MS 30u /* our own margin after release (PHY datasheet NOT read: UNVERIFIED) */ +/* GPIO32 = bank 1 pin 4 (clk: pinctrl-rp1.c:292-296 bank table { 28, 6, ...}). */ +#define CK_RP1_GPIO32_CTRL (CK_RP1_IO_BANK1 + 4u * 8u + 4u) /* gpio base + j*8 + RP1_GPIO_CTRL */ +#define CK_RP1_GPIO32_PAD (CK_RP1_PADS_BANK1 + 4u + 4u * 4u) /* pads bank offset 0x4004 + j*4 (from 0xf0000) */ +#define CK_RP1_RIO1_OUT (CK_RP1_RIO1 + 0x0u) +#define CK_RP1_RIO1_OE (CK_RP1_RIO1 + 0x4u) +#define CK_RP1_RIO1_IN (CK_RP1_RIO1 + 0x8u) +#define CK_RP1_ATOMIC_SET 0x2000u +#define CK_RP1_ATOMIC_CLR 0x3000u +#define CK_RP1_GPIO_PIN_BIT (1u << 4) /* bit j inside the bank's RIO word */ +#define CK_RP1_FSEL_GPIO 0x05u +#define CK_RP1_PAD_IN_ENABLE 0x40u +#define CK_RP1_PAD_OUT_DISABLE 0x80u + +struct ck_rp1_phyreset { + uint32_t ctrl_before, pad_before, oe_before, out_before, in_before; + uint32_t ctrl_after, pad_after, in_low, in_high; /* in_low: pin read while we drive low */ +}; +/* Drive GPIO32: line high first (no glitch low), pad+function, output enable, then low for + * CK_RP1_PHY_RESET_MS, then high and settle. Reads every register back. 0 = ok; + * E_READBACK if the pin does not read low while driven low or high after release. */ +int ck_rp1_phy_reset(const struct ck_rp1_io *io, struct ck_rp1_phyreset *out); + +/* ============ B3. MDIO read of the PHY ID ============ */ + +#define CK_GEM_NCR 0x000u +#define CK_GEM_NCFGR 0x004u +#define CK_GEM_NSR 0x008u +#define CK_GEM_MAN 0x034u +#define CK_GEM_MID 0x0fcu +#define CK_GEM_NCR_MPE (1u << 4) +#define CK_GEM_NSR_IDLE (1u << 2) +#define CK_GEM_NCFGR_CLK_SHIFT 18 +#define CK_GEM_NCFGR_CLK_MASK (7u << 18) +#define CK_GEM_MDIO_POLLS 2000u +#define CK_GEM_MDIO_POLL_US 10u + +/* GEM MDC divider code from the pclk rate (macb_main.c:2826-2849); 0 pclk -> the slowest. */ +uint32_t ck_gem_mdc_div_code(uint32_t pclk_hz); +/* The C22 read frame the MAN register wants (macb.h MACB_MAN_C22_*). phy, reg < 32. */ +uint32_t ck_gem_mdio_frame_read(uint32_t phy, uint32_t reg); +/* Set NCR.MPE and the MDC divider (read-modify-write, other bits kept). */ +int ck_gem_mdio_enable(const struct ck_rp1_io *io, uint32_t pclk_hz); +/* One clause 22 read. Waits for idle before and after (bounded). 0 = ok, E_TIMEOUT, E_ARG. */ +int ck_gem_mdio_read(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t *val); + +enum { CK_PHY_ABSENT = 0, CK_PHY_BCM54213PE = 1, CK_PHY_BROADCOM_OTHER = 2, CK_PHY_OTHER = 3 }; +#define CK_PHY_ID_BCM54213PE 0x600d84a2u +struct ck_phy_id { + uint32_t id; /* reg2 << 16 | reg3 */ + uint32_t oui; /* 24-bit OUI in IEEE octet order (e.g. 0x18c086), bits 1,2 taken as 0 */ + uint8_t model, rev; + int cls; /* CK_PHY_* */ +}; +void ck_phy_id_decode(uint16_t r2, uint16_t r3, struct ck_phy_id *out); +const char *ck_phy_class_name(int cls); +/* Find the PHY: poll address `addr` (dts: 1) up to `tries` times with `gap_ms` between; the + * first non-0xffff/0x0000 pair wins. If it never answers, scan 0..31 once for diagnostics + * (`*scan_hit` = lowest answering address, -1 if none). *attempts = polls used at `addr`. + * 0 = answered at addr, E_TIMEOUT otherwise. */ +int ck_phy_find(const struct ck_rp1_io *io, uint32_t addr, uint32_t tries, uint32_t gap_ms, struct ck_phy_id *id, + uint32_t *attempts, int *scan_hit); + +#ifdef CK_PI5_NET +/* Boot stage (CK_PI5_NET image only): see pi5_net.c. */ +void ck_pi5net_run(void); +#endif + +#endif diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 188f43ea..d6c1cc21 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-13T21:27:22Z", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:774873904015b85c13b33a02b7f2ebcc41a815306ad7a09973cd3c201a9edf49", + "digest": "sha256:17f3ab5f5e8314d29db1d2ee908a01848745520cc61b8de1d95982f6657e065c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 161, - "generated_at_commit": "03de8681ae58720bd01a45ccb5311c792f1d517b", - "source_tree": "sha256:9341b7b9ccc8554b542ab894b41a1db16a764105faef3e65f6acdae45a61afd7" + "files": 162, + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:8578bae60fd303317f5f7a831a60d8fa6e8d065c2351f86710c16ee6726ac8a9" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_pi5_net.c b/native/kernel/tests/test_pi5_net.c new file mode 100644 index 00000000..ab1d2cfe --- /dev/null +++ b/native/kernel/tests/test_pi5_net.c @@ -0,0 +1,699 @@ +/* test_pi5_net.c -- Pi 5 network lane cut 2 (dev/pi5_net.c) over FAKE registers: the inbound + * window plan and its register sequence, the RP1 bus-master quiesce, the outbound window decode, + * the RP1 clock decisions, the PHY reset line sequence, the GEM MDIO read and the PHY ID decode. + * Hosted: this proves the decisions and the order of register writes, NOT the hardware. Run under + * ASan/UBSan and with each CK_NET_MUTANT_* (make pi5-net-mutants); every mutant must make it FAIL. */ +#include +#include +#include +#include "ck_test.h" +#include "pi5_net.h" + +#define MiB (1ull << 20) +#define GiB (1ull << 30) + +/* ===================== fake root complex ===================== */ +struct frc { + uint32_t regs[0xa000 / 4]; + uint32_t cmd_status; /* RP1 config dword 1: status[31:16] | command[15:0] */ + uint32_t cmd_stuck_bme; /* 1: BME bit ignores clears */ + uint32_t hi_stuck; /* 1: window 0 HI register ignores writes */ + uint32_t w3_stuck; /* 1: window 3 cannot be switched off */ + uint32_t index; + /* write log for window 0 */ + uint32_t nwr, order_violations; + uint32_t lo_writes[16], nlo; + uint32_t cfg_writes[8], ncfg; +}; +static struct frc fr; + +static uint32_t fr_rd(void *c, uint32_t off) +{ + struct frc *f = c; + if (off == CK_PI5PCIE_EXT_CFG_INDEX) + return f->index; + if (off >= 0x8000 && off < 0x9000) { + if (f->index != 0x00100000u) + return 0xffffffffu; + if (off - 0x8000 == CK_PI5NET_PCI_COMMAND) + return f->cmd_status; + return 0; + } + return f->regs[off / 4]; +} + +static void fr_wr(void *c, uint32_t off, uint32_t v) +{ + struct frc *f = c; + f->nwr++; + if (off == CK_PI5PCIE_EXT_CFG_INDEX) { + f->index = v; + return; + } + if (off >= 0x8000 && off < 0x9000) { + if (f->index == 0x00100000u && off - 0x8000 == CK_PI5NET_PCI_COMMAND) { + uint32_t status = f->cmd_status >> 16; + if (f->ncfg < 8) + f->cfg_writes[f->ncfg] = v; + f->ncfg++; + status &= ~(v >> 16); /* RW1C */ + f->cmd_status = (status << 16) | (v & 0xffffu); + if (f->cmd_stuck_bme) + f->cmd_status |= CK_PI5NET_CMD_BME; + } + return; + } + if (off == CK_PI5NET_IB0_LO) { + if (f->nlo < 16) + f->lo_writes[f->nlo] = v; + f->nlo++; + } + if (off == CK_PI5NET_IB0_HI || off == CK_PI5NET_IB0_REMAP_LO || off == CK_PI5NET_IB0_REMAP_HI) { + if (f->regs[CK_PI5NET_IB0_LO / 4] & 0x1fu) + f->order_violations++; /* a half of an ENABLED window changed under traffic */ + if (off == CK_PI5NET_IB0_HI && f->hi_stuck) + return; + if (off == CK_PI5NET_IB0_REMAP_LO) + v &= 0xfffff001u; + if (off == CK_PI5NET_IB0_REMAP_HI) + v &= 0xffu; + } + if (off == 0x40d4u + 2 * 8u && f->w3_stuck) /* window 3 */ + return; + f->regs[off / 4] = v; +} +static struct ck_pi5pcie_io fio = {fr_rd, fr_wr, &fr}; + +static void fr_fresh(void) +{ + memset(&fr, 0, sizeof fr); + fr.cmd_status = (0x0010u << 16) | 0x0006u; /* status: capability list; command: MEM + BME */ + /* the firmware's window 0: bus 0 -> cpu 0, 64 GiB (size code 21), remap on */ + fr.regs[CK_PI5NET_IB0_LO / 4] = 21; + fr.regs[CK_PI5NET_IB0_HI / 4] = 0; + fr.regs[CK_PI5NET_IB0_REMAP_LO / 4] = 1; + fr.regs[CK_PI5NET_IB0_REMAP_HI / 4] = 0; +} + +static void t_size_code(void) +{ + CHECK(ck_pi5net_ib_size_code(4096) == 0x1c); + CHECK(ck_pi5net_ib_size_code(32768) == 0x1f); + CHECK(ck_pi5net_ib_size_code(65536) == 1); + CHECK(ck_pi5net_ib_size_code(2 * MiB) == 6); + CHECK(ck_pi5net_ib_size_code(64 * GiB) == 21); + CHECK(ck_pi5net_ib_size_code(0) == 0); + CHECK(ck_pi5net_ib_size_code(3 * MiB) == 0); /* not a power of two */ + CHECK(ck_pi5net_ib_size_code(2048) == 0); /* below 4 KiB */ + CHECK(ck_pi5net_ib_size_code(128 * GiB) == 0); /* above 64 GiB */ + /* agrees with the decoder cut 1 uses to read the registers back */ + for (uint32_t lg = 12; lg <= 36; lg++) { + uint64_t sz = 0; + CHECK(ck_pi5pcie_ib_size_decode(ck_pi5net_ib_size_code(1ull << lg), &sz) == 1 && sz == (1ull << lg)); + } +} + +static void t_plan(void) +{ + struct ck_pi5net_ibprog p; + /* the real Pi run: bounce cpu 0x4400000 size 0x200000 */ + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0); + CHECK(p.bus_base == 0x4400000ull && p.cpu_base == 0x4400000ull && p.size == 0x200000ull); + CHECK(p.size_code == 6 && p.extra_bytes == 0); + CHECK(p.lo == 0x04400006u && p.hi == 0 && p.remap_lo == 0x04400001u && p.remap_hi == 0); + /* not a power of two: rounds up, the extra is reported */ + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x180000ull, &p) == 0 && p.size == 0x200000ull && p.extra_bytes == 0x80000ull); + /* a region that straddles an aligned block of its own size must get the next size up */ + CHECK(ck_pi5net_ib_plan(0x4500000ull, 0x200000ull, &p) == 0); + CHECK(p.size == 0x400000ull && p.cpu_base == 0x4400000ull && p.extra_bytes == 0x200000ull); + CHECK(p.cpu_base <= 0x4500000ull && p.cpu_base + p.size >= 0x4700000ull); + /* high address: the upper halves carry the bits above 32 */ + CHECK(ck_pi5net_ib_plan(0x880000000ull, 0x200000ull, &p) == 0); + CHECK(p.hi == 8 && p.remap_hi == 8 && p.lo == (0x80000000u | 6) && p.remap_lo == 0x80000001u); + /* small region: 4 KiB window */ + CHECK(ck_pi5net_ib_plan(0x10000ull, 0x1000ull, &p) == 0 && p.size == 0x1000ull && p.size_code == 0x1c); + /* refusals */ + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0, &p) == CK_PI5NET_E_ARG); + CHECK(ck_pi5net_ib_plan(UINT64_MAX - 10, 0x1000, &p) == CK_PI5NET_E_ARG); + CHECK(ck_pi5net_ib_plan(0, 128 * GiB, &p) == CK_PI5NET_E_RANGE); + CHECK(ck_pi5net_ib_plan((1ull << 40) - 0x1000, 0x2000, &p) == CK_PI5NET_E_RANGE); /* passes 40 bits */ + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, NULL) == CK_PI5NET_E_ARG); + /* property: the window always holds the region, is power-of-two sized, aligned, and minimal */ + srand(12345); + for (int i = 0; i < 20000; i++) { + uint64_t b = ((uint64_t)rand() << 12) % (1ull << 38); + uint64_t s = ((uint64_t)(rand() % 0x400) + 1) << (12 + rand() % 14); + if (ck_pi5net_ib_plan(b, s, &p) != 0) + continue; + CHECK(p.size && (p.size & (p.size - 1)) == 0 && (p.cpu_base & (p.size - 1)) == 0); + CHECK(p.cpu_base <= b && p.cpu_base + p.size >= b + s); + CHECK(p.extra_bytes == p.size - s); + if (p.size > 4096) { /* minimality: the half-size aligned block around b does not hold the region */ + uint64_t h = p.size / 2, hb = b & ~(h - 1); + CHECK(hb + h < b + s); + } + CHECK(p.bus_base == p.cpu_base); + } +} + +static void t_plan_then_registers_then_verdict(void) +{ + struct ck_pi5net_ibprog p; + struct ck_pi5net_ibsaved sv; + struct ck_pi5pcie_ibwin w[CK_PI5PCIE_IB_WINDOWS]; + struct ck_pi5pcie_dmav dv; + uint32_t nd = 99; + fr_fresh(); + for (uint32_t i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++) + CHECK(ck_pi5pcie_ib_read(&fio, i, &w[i]) == 0); + CHECK(ck_pi5pcie_ib_verdict(w, CK_PI5PCIE_IB_WINDOWS, 0, 0x4400000ull, 0x200000ull, &dv) == 0); + CHECK(dv.verdict == CK_PI5PCIE_DMA_WIDER && dv.reach_bytes == 64 * GiB); /* the cut 1 result */ + CHECK(ck_pi5net_confine_label(&dv, NULL) == CK_PI5NET_CONF_NOT_CONFINED); + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0); + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0 && nd == 0); + for (uint32_t i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++) + CHECK(ck_pi5pcie_ib_read(&fio, i, &w[i]) == 0); + CHECK(w[0].enabled && w[0].size_valid && w[0].remap_en && w[0].size == 0x200000ull); + CHECK(w[0].bus_base == 0x4400000ull && w[0].cpu_base == 0x4400000ull); + CHECK(ck_pi5pcie_ib_verdict(w, CK_PI5PCIE_IB_WINDOWS, 0, 0x4400000ull, 0x200000ull, &dv) == 0); + CHECK(dv.verdict == CK_PI5PCIE_DMA_CONFINED && dv.reach_bytes == 0x200000ull && dv.outside_bytes == 0); + CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_CONFINED); + /* a region that needed a bigger block: PARTIAL with exactly the plan's extra, not CONFINED */ + fr_fresh(); + CHECK(ck_pi5net_ib_plan(0x4500000ull, 0x200000ull, &p) == 0); + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0); + for (uint32_t i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++) + CHECK(ck_pi5pcie_ib_read(&fio, i, &w[i]) == 0); + CHECK(ck_pi5pcie_ib_verdict(w, CK_PI5PCIE_IB_WINDOWS, 0, 0x4500000ull, 0x200000ull, &dv) == 0); + CHECK(dv.verdict == CK_PI5PCIE_DMA_WIDER && dv.outside_bytes == 0x200000ull && dv.inside_bytes == 0x200000ull); + CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_PARTIAL); + CHECK(ck_pi5net_confine_label(&dv, NULL) == CK_PI5NET_CONF_NOT_CONFINED); /* without the plan it is just wide */ + /* the other verdicts map to BROKEN / UNKNOWN */ + dv.verdict = CK_PI5PCIE_DMA_MISS; + CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_BROKEN); + dv.verdict = CK_PI5PCIE_DMA_UNKNOWN; + CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_UNKNOWN); + CHECK(strcmp(ck_pi5net_confine_name(CK_PI5NET_CONF_PARTIAL), "PARTIAL") == 0); + CHECK(strcmp(ck_pi5net_confine_name(CK_PI5NET_CONF_CONFINED), "CONFINED") == 0); +} + +static void t_apply_order(void) +{ + struct ck_pi5net_ibprog p; + struct ck_pi5net_ibsaved sv; + uint32_t nd = 0; + fr_fresh(); + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0); + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0); + CHECK(fr.nlo == 2); /* off, then on */ + CHECK((fr.lo_writes[0] & 0x1f) == 0); /* window is OFF before its halves change */ + CHECK(fr.lo_writes[fr.nlo - 1] == p.lo); /* the size code (enable) is the last write to LO */ + CHECK(fr.order_violations == 0); + CHECK(sv.lo == 21 && sv.hi == 0 && sv.remap_lo == 1); /* firmware window saved for the restore */ + /* other enabled windows are switched off */ + fr_fresh(); + fr.regs[(0x40d4u) / 4] = 0x10000000u | 6; /* window 1 */ + fr.regs[(0x40d4u + 3 * 8u) / 4] = 0x20000000u | 1; /* window 4 */ + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0 && nd == 2); + CHECK((fr.regs[0x40d4u / 4] & 0x1f) == 0 && (fr.regs[(0x40d4u + 24u) / 4] & 0x1f) == 0); + CHECK(fr.regs[0x40d4u / 4] >> 5 == 0x10000000u >> 5); /* base bits kept, only the size code cleared */ +} + +static void t_apply_failure_restores(void) +{ + struct ck_pi5net_ibprog p; + struct ck_pi5net_ibsaved sv; + uint32_t nd = 0; + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0); + /* hardware ignores the HI write: readback does not match, firmware window is put back */ + fr_fresh(); + fr.hi_stuck = 1; + /* make the plan need a non-zero HI so the stuck register matters */ + CHECK(ck_pi5net_ib_plan(0x880000000ull, 0x200000ull, &p) == 0); + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == CK_PI5NET_E_READBACK); + CHECK(fr.regs[CK_PI5NET_IB0_LO / 4] == 21 && fr.regs[CK_PI5NET_IB0_HI / 4] == 0 && + fr.regs[CK_PI5NET_IB0_REMAP_LO / 4] == 1 && fr.regs[CK_PI5NET_IB0_REMAP_HI / 4] == 0); + /* another window that refuses to switch off: not confined, so refuse and restore */ + fr_fresh(); + fr.regs[(0x40d4u + 2 * 8u) / 4] = 0x30000000u | 7; /* window 3 */ + fr.w3_stuck = 1; + CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0); + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == CK_PI5NET_E_READBACK); + CHECK(fr.regs[CK_PI5NET_IB0_LO / 4] == 21); + /* arguments */ + CHECK(ck_pi5net_ib_apply(NULL, &p, &sv, &nd) == CK_PI5NET_E_ARG); + memset(&p, 0, sizeof p); + CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == CK_PI5NET_E_ARG); /* a plan with no size code */ +} + +static void t_quiesce(void) +{ + uint16_t saved = 0xdead; + fr_fresh(); + CHECK(ck_pi5net_rp1_quiesce(&fio, &saved) == 0 && saved == 0x0006); + CHECK((fr.cmd_status & 0xffffu) == 0x0002); /* BME off, MEM kept */ + CHECK((fr.cmd_status >> 16) == 0x0010); /* status bits not cleared by writing zeros */ + CHECK(fr.ncfg == 1 && (fr.cfg_writes[0] >> 16) == 0); /* the write carried no status ones */ + CHECK(fr.index == 0x00100000u); + fr_fresh(); + fr.cmd_stuck_bme = 1; + CHECK(ck_pi5net_rp1_quiesce(&fio, &saved) == CK_PI5NET_E_QUIESCE); + CHECK(ck_pi5net_rp1_quiesce(&fio, NULL) == CK_PI5NET_E_ARG); + /* config write addressing */ + fr_fresh(); + CHECK(ck_pi5net_cfg_write32(&fio, 1, 0, 0, 4, 0x2) == 0); + CHECK(ck_pi5net_cfg_write32(&fio, 2, 0, 0, 4, 0) == CK_PI5NET_E_ARG); + CHECK(ck_pi5net_cfg_write32(&fio, 1, 1, 0, 4, 0) == CK_PI5NET_E_ARG); + CHECK(ck_pi5net_cfg_write32(&fio, 1, 0, 0, 5, 0) == CK_PI5NET_E_ARG); + CHECK(ck_pi5net_cfg_write32(&fio, 1, 0, 0, 4096, 0) == CK_PI5NET_E_ARG); + CHECK(ck_pi5net_cfg_write32(&fio, 0, 1, 0, 4, 0) == CK_PI5NET_E_ARG); +} + +static void t_outbound(void) +{ + struct ck_pi5net_obwin w; + /* the values edk2 PcieSetupOutboundWindow writes for CPU 0x1f00000000, 1 GiB, bus 0xc0000000 */ + uint64_t cpu_mb = 0x1f00000000ull / MiB, lim_mb = (0x1f00000000ull + GiB - 1) / MiB; + fr_fresh(); + fr.regs[CK_PI5NET_OB0_BASE_LIMIT / 4] = (uint32_t)((cpu_mb << 4) | (lim_mb << 20)); + fr.regs[CK_PI5NET_OB0_BASE_HI / 4] = (uint32_t)(cpu_mb >> 12); + fr.regs[CK_PI5NET_OB0_LIMIT_HI / 4] = (uint32_t)(lim_mb >> 12); + fr.regs[CK_PI5NET_OB0_BUS_LO / 4] = 0xc0000000u; + fr.regs[CK_PI5NET_OB0_BUS_HI / 4] = 0; + CHECK(ck_pi5net_ob_read(&fio, &w) == 0 && w.valid); + CHECK(w.cpu_base == 0x1f00000000ull && w.bus_base == 0xc0000000ull && w.size == GiB); + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 0x108000) == 0x1f00000000ull); + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0400000ull, 0x10000) == 0x1f00400000ull); + CHECK(ck_pi5net_bus_to_cpu(&w, 0xbfffffffull, 0x10) == 0); /* below the window */ + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull + GiB - 0x10, 0x20) == 0); /* runs past the end */ + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull + GiB - 0x10, 0x10) != 0); /* exactly to the end */ + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 2 * GiB) == 0); /* longer than the window */ + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 0) == 0); + CHECK(ck_pi5net_bus_to_cpu(NULL, 0xc0000000ull, 0x10) == 0); + /* limit below base: not valid */ + fr.regs[CK_PI5NET_OB0_BASE_LIMIT / 4] = (uint32_t)((cpu_mb << 4) | ((lim_mb - 0x1000) << 20)); + fr.regs[CK_PI5NET_OB0_LIMIT_HI / 4] = 0x1e; + CHECK(ck_pi5net_ob_read(&fio, &w) == 0 && !w.valid); + CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 0x10) == 0); +} + +/* ===================== fake RP1 BAR1 ===================== */ +#define NOFF 0x108000u +struct frp { + uint32_t r[NOFF / 4]; + uint32_t wr_count[NOFF / 4]; + uint64_t t_us; /* fake time */ + uint32_t order[256], nord; + int enable_sticks; /* clock ENABLE bit holds */ + /* GPIO32 pin model */ + int pin_forced; /* 1: ext fault, pin reads 0 whatever we drive */ + uint64_t low_us; /* total time the pin was driven low */ + int driven_low_before_oe, drove_low; + uint64_t low_since; + int low_now; + /* GEM / MDIO model */ + uint32_t gem_busy_polls; /* idle stays 0 for this many NSR reads after a MAN write */ + uint32_t busy_left; + int phy_addr; /* -1 none */ + uint32_t phy_ready_polls; /* PHY returns 0xffff until this many reads happened */ + uint32_t phy_reads; + uint16_t phy_regs[32]; + int never_idle; + int bad_frame_seen; + uint32_t last_man; + int stuck_ncfg; +}; +static struct frp fp; + +static int pin_level(void) +{ + uint32_t oe = fp.r[CK_RP1_RIO1_OE / 4] & CK_RP1_GPIO_PIN_BIT, out = fp.r[CK_RP1_RIO1_OUT / 4] & CK_RP1_GPIO_PIN_BIT; + if (fp.pin_forced) + return 0; + if (oe) + return out ? 1 : 0; + return 1; /* pulled up */ +} + +static void pin_track(void) +{ + int lv = pin_level(); + if (!lv && !fp.low_now) { + fp.low_now = 1; + fp.low_since = fp.t_us; + fp.drove_low = 1; + if (!(fp.r[CK_RP1_RIO1_OE / 4] & CK_RP1_GPIO_PIN_BIT)) + fp.driven_low_before_oe = 1; + } else if (lv && fp.low_now) { + fp.low_now = 0; + fp.low_us += fp.t_us - fp.low_since; + } +} + +static uint32_t rp_rd(void *c, uint32_t off) +{ + (void)c; + if (off == CK_RP1_RIO1_IN) + return pin_level() ? CK_RP1_GPIO_PIN_BIT : 0; + if (off == CK_RP1_ETH + CK_GEM_NSR) { + if (fp.never_idle) + return 0; + if (fp.busy_left) { + fp.busy_left--; + return 0; + } + return CK_GEM_NSR_IDLE; + } + if (off == CK_RP1_ETH + CK_GEM_MAN) { + /* stale until the transfer finished; the model keeps the data only for a correct read frame */ + if (fp.busy_left) + return (fp.r[off / 4] & 0xffff0000u) | 0xdeadu; + return fp.r[off / 4]; + } + return fp.r[off / 4]; +} + +static void rp_wr(void *c, uint32_t off, uint32_t v) +{ + (void)c; + if (fp.nord < 256) + fp.order[fp.nord++] = off; + fp.wr_count[off / 4]++; + if (off >= CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET && off < CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET + 0x10) { + uint32_t base = off - CK_RP1_ATOMIC_SET; + fp.r[base / 4] |= v; + pin_track(); + return; + } + if (off >= CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_CLR && off < CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_CLR + 0x10) { + uint32_t base = off - CK_RP1_ATOMIC_CLR; + fp.r[base / 4] &= ~v; + pin_track(); + return; + } + if (off == CK_RP1_CLOCKS + CK_RP1_CLK_ETH_CTRL || off == CK_RP1_CLOCKS + CK_RP1_CLK_TSU_CTRL) { + if (!fp.enable_sticks) + v &= ~(uint32_t)CK_RP1_CLK_ENABLE; + } + if (off == CK_RP1_ETH + CK_GEM_NCFGR && fp.stuck_ncfg) + return; + if (off == CK_RP1_ETH + CK_GEM_MAN) { + uint32_t phy = (v >> 23) & 31u, reg = (v >> 18) & 31u; + fp.last_man = v; + if ((v >> 30) != 1 || ((v >> 28) & 3u) != 2 || ((v >> 16) & 3u) != 2) { + fp.bad_frame_seen = 1; /* not a clause 22 read: the model returns nothing useful */ + fp.r[off / 4] = v & 0xffff0000u; + } else { + uint16_t d = 0xffff; + if ((int)phy == fp.phy_addr) { + fp.phy_reads++; + if (fp.phy_reads > fp.phy_ready_polls) + d = fp.phy_regs[reg]; + } + fp.r[off / 4] = (v & 0xffff0000u) | d; + } + fp.busy_left = fp.gem_busy_polls; + return; + } + fp.r[off / 4] = v; + if (off == CK_RP1_GPIO32_PAD || off == CK_RP1_GPIO32_CTRL) + pin_track(); +} + +static void rp_delay(void *c, uint32_t us) +{ + (void)c; + fp.t_us += us; +} +static struct ck_rp1_io rio = {rp_rd, rp_wr, rp_delay, &fp}; + +static void rp_fresh(void) +{ + memset(&fp, 0, sizeof fp); + fp.phy_addr = -1; + /* clock block as left by a cold boot: PLL locked, secondary running, ETH/TSU off */ + fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_CS) / 4] = 0x80000001u; + fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_FBDIV_INT) / 4] = 20; + fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_PRIM) / 4] = (5u << 16) | (1u << 12); + fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_SEC) / 4] = 0x80000000u | (8u << 8); + fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_SYS_CTRL) / 4] = 2; + fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_SYS_DIV) / 4] = 1; + fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_SYS_SEL) / 4] = 4; + fp.enable_sticks = 1; +} + +static void t_clock_rates(void) +{ + struct ck_rp1_clk_regs r; + rp_fresh(); + ck_rp1_clk_read(&rio, &r); + CHECK(ck_rp1_pll_core_hz(&r) == 1000000000u); + CHECK(ck_rp1_pll_sec_hz(&r) == 125000000u); + CHECK(ck_rp1_pll_pri_hz(&r) == 200000000u); + CHECK(ck_rp1_clk_sys_hz(&r) == 200000000u); + CHECK(ck_rp1_clk_eth_hz(&r) == 0 && ck_rp1_clk_tsu_hz(&r) == 0); + r.pll_cs = 1; /* unlocked */ + CHECK(ck_rp1_pll_core_hz(&r) == 0 && ck_rp1_pll_sec_hz(&r) == 0 && ck_rp1_pll_pri_hz(&r) == 0); + rp_fresh(); + ck_rp1_clk_read(&rio, &r); + r.pll_sec |= 1u << 16; /* divider held in reset */ + CHECK(ck_rp1_pll_sec_hz(&r) == 0); + r.pll_sec = 0x80000000u | (12u << 8); /* divide by 12 */ + CHECK(ck_rp1_pll_sec_hz(&r) == 83333334u); + r.pll_sec = 0x80000000u | (3u << 8); /* invalid field value defaults to 19 */ + CHECK(ck_rp1_pll_sec_hz(&r) == 52631579u); + r.pll_sec = (8u << 8); /* divider not implemented (IMPL bit clear) */ + CHECK(ck_rp1_pll_sec_hz(&r) == 0); + /* clk_sys on xosc: sel bit 0 */ + ck_rp1_clk_read(&rio, &r); + r.sys_sel = 1; + CHECK(ck_rp1_clk_sys_hz(&r) == 50000000u); +} + +static void t_clock_decide(void) +{ + struct ck_rp1_clk_regs r; + struct ck_rp1_clk_plan p; + rp_fresh(); + ck_rp1_clk_read(&rio, &r); + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_TO_ENABLE && p.eth_div == 1 && (p.eth_ctrl & CK_RP1_CLK_ENABLE) && + (p.eth_ctrl & CK_RP1_CLK_AUXSRC_MASK) == 0); + CHECK(p.tsu == CK_RP1_CLK_TO_ENABLE && p.tsu_div == 1 && (p.tsu_ctrl & CK_RP1_CLK_ENABLE)); + /* a clock that is already on is left exactly as found, even at an odd rate */ + r.eth_ctrl = CK_RP1_CLK_ENABLE | (1u << CK_RP1_CLK_AUXSRC_SHIFT); + r.eth_div = 2; + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_ALREADY_ON && p.eth_div == 0 && p.eth_ctrl == 0); + CHECK(ck_rp1_clk_eth_hz(&r) == 100000000u); /* pll_sys (200 MHz) / 2: reported, not changed */ + r.tsu_ctrl = CK_RP1_CLK_ENABLE; + r.tsu_div = 1; + ck_rp1_clk_decide(&r, &p); + CHECK(p.tsu == CK_RP1_CLK_ALREADY_ON && ck_rp1_clk_tsu_hz(&r) == 50000000u); + /* PLL not locked: ETH blocked, TSU (from xosc) is not */ + ck_rp1_clk_read(&rio, &r); + r.pll_cs = 1; + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_BLOCKED && p.tsu == CK_RP1_CLK_TO_ENABLE); + /* secondary in reset */ + ck_rp1_clk_read(&rio, &r); + r.pll_sec |= 1u << 16; + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_BLOCKED); + /* secondary not an exact multiple of 125 MHz (1000/9): refuse rather than run ETH at a wrong rate */ + ck_rp1_clk_read(&rio, &r); + r.pll_sec = 0x80000000u | (9u << 8); + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_BLOCKED); + ck_rp1_clk_read(&rio, &r); + r.pll_fbdiv_int = 30; /* core 1.5 GHz, sec 187.5 MHz: ratio 1 but not exact */ + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_BLOCKED); + /* a faster secondary that divides exactly: divider = ratio (1000 MHz core / 8 = 125; make core 2000/8 = 250 -> 2) */ + ck_rp1_clk_read(&rio, &r); + r.pll_fbdiv_int = 40; + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_TO_ENABLE && p.eth_div == 2); + /* and a secondary slower than 125 MHz is refused (ratio 0) */ + ck_rp1_clk_read(&rio, &r); + r.pll_sec = 0x80000000u | (16u << 8); /* 1000/16 = 62.5 MHz */ + ck_rp1_clk_decide(&r, &p); + CHECK(p.eth == CK_RP1_CLK_BLOCKED); +} + +static void t_clock_apply(void) +{ + struct ck_rp1_clk_regs r; + struct ck_rp1_clk_plan p; + uint32_t eth_ctrl = (CK_RP1_CLOCKS + CK_RP1_CLK_ETH_CTRL) / 4, eth_div = (CK_RP1_CLOCKS + CK_RP1_CLK_ETH_DIV) / 4; + uint32_t tsu_ctrl = (CK_RP1_CLOCKS + CK_RP1_CLK_TSU_CTRL) / 4; + rp_fresh(); + ck_rp1_clk_read(&rio, &r); + ck_rp1_clk_decide(&r, &p); + CHECK(ck_rp1_clk_apply(&rio, &p) == 0); + ck_rp1_clk_read(&rio, &r); + CHECK(ck_rp1_clk_eth_hz(&r) == 125000000u && ck_rp1_clk_tsu_hz(&r) == 50000000u); + /* order: divider, then source with the clock still off, then the enable */ + CHECK(fp.wr_count[eth_div] == 1 && fp.wr_count[eth_ctrl] == 2); + { + int i_div = -1, i_c1 = -1, i_c2 = -1; + for (uint32_t i = 0; i < fp.nord; i++) { + if (fp.order[i] == CK_RP1_CLOCKS + CK_RP1_CLK_ETH_DIV) + i_div = (int)i; + if (fp.order[i] == CK_RP1_CLOCKS + CK_RP1_CLK_ETH_CTRL) { + if (i_c1 < 0) + i_c1 = (int)i; + else + i_c2 = (int)i; + } + } + CHECK(i_div >= 0 && i_div < i_c1 && i_c1 < i_c2); + } + /* already-on clocks get no write at all */ + rp_fresh(); + fp.r[eth_ctrl] = CK_RP1_CLK_ENABLE; + fp.r[eth_div] = 1; + fp.r[tsu_ctrl] = CK_RP1_CLK_ENABLE | 0x20; + fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_TSU_DIV) / 4] = 1; + ck_rp1_clk_read(&rio, &r); + ck_rp1_clk_decide(&r, &p); + fp.nord = 0; + memset(fp.wr_count, 0, sizeof fp.wr_count); + CHECK(ck_rp1_clk_apply(&rio, &p) == 0); + CHECK(fp.nord == 0 && fp.r[eth_ctrl] == CK_RP1_CLK_ENABLE); + /* enable does not stick: reported */ + rp_fresh(); + fp.enable_sticks = 0; + ck_rp1_clk_read(&rio, &r); + ck_rp1_clk_decide(&r, &p); + CHECK(ck_rp1_clk_apply(&rio, &p) == CK_PI5NET_E_READBACK); +} + +static void t_phy_reset(void) +{ + struct ck_rp1_phyreset pr; + rp_fresh(); + fp.r[CK_RP1_GPIO32_CTRL / 4] = 0x1f | (1u << 20); /* NONE, with an interrupt-enable bit set */ + fp.r[CK_RP1_GPIO32_PAD / 4] = CK_RP1_PAD_OUT_DISABLE | 0x1; + CHECK(ck_rp1_phy_reset(&rio, &pr) == 0); + CHECK(fp.drove_low == 1); /* reset was really asserted */ + CHECK(fp.driven_low_before_oe == 0); /* the pin never went low before we owned it */ + CHECK(fp.low_us >= (uint64_t)CK_RP1_PHY_RESET_MS * 1000u); /* held for the dts reset time */ + CHECK(fp.low_us < 1000u * 1000u); + CHECK(fp.t_us >= (uint64_t)(CK_RP1_PHY_RESET_MS + CK_RP1_PHY_SETTLE_MS) * 1000u); + CHECK(pin_level() == 1); /* released at the end */ + CHECK((fp.r[CK_RP1_RIO1_OE / 4] & CK_RP1_GPIO_PIN_BIT) != 0); + CHECK((fp.r[CK_RP1_GPIO32_CTRL / 4] & 0x1f) == CK_RP1_FSEL_GPIO && (fp.r[CK_RP1_GPIO32_CTRL / 4] & (1u << 20))); + CHECK((fp.r[CK_RP1_GPIO32_PAD / 4] & CK_RP1_PAD_IN_ENABLE) && !(fp.r[CK_RP1_GPIO32_PAD / 4] & CK_RP1_PAD_OUT_DISABLE)); + CHECK((pr.in_low & CK_RP1_GPIO_PIN_BIT) == 0 && (pr.in_high & CK_RP1_GPIO_PIN_BIT) != 0); + CHECK(pr.ctrl_before == (0x1f | (1u << 20)) && (pr.ctrl_after & 0x1f) == 5); + /* the register addresses (bank 1 pin 4): see pinctrl-rp1.c bank table */ + CHECK(CK_RP1_GPIO32_CTRL == 0xd4024u && CK_RP1_GPIO32_PAD == 0xf4014u && CK_RP1_RIO1_OUT == 0xe4000u); + /* a pin stuck low (external fault) is reported, not ignored */ + rp_fresh(); + fp.pin_forced = 1; + CHECK(ck_rp1_phy_reset(&rio, &pr) == CK_PI5NET_E_READBACK); +} + +static void gem_phy(uint32_t polls_busy, int addr, uint32_t ready_polls) +{ + rp_fresh(); + fp.gem_busy_polls = polls_busy; + fp.phy_addr = addr; + fp.phy_ready_polls = ready_polls; + fp.phy_regs[2] = 0x600d; + fp.phy_regs[3] = 0x84a2; + fp.r[(CK_RP1_ETH + CK_GEM_NCR) / 4] = 0x00000003u; + fp.r[(CK_RP1_ETH + CK_GEM_NCFGR) / 4] = 0x00000800u | (1u << 18); +} + +static void t_mdio(void) +{ + uint16_t v = 0; + struct ck_phy_id id; + uint32_t att = 0; + int hit = 0; + CHECK(ck_gem_mdio_frame_read(1, 2) == 0x608a0000u); + CHECK(ck_gem_mdio_frame_read(31, 31) == (0x60000000u | (31u << 23) | (31u << 18) | 0x20000u)); + CHECK(ck_gem_mdc_div_code(0) == 7 && ck_gem_mdc_div_code(20000000) == 0 && ck_gem_mdc_div_code(20000001) == 1 && + ck_gem_mdc_div_code(80000000) == 2 && ck_gem_mdc_div_code(120000000) == 3 && + ck_gem_mdc_div_code(160000000) == 4 && ck_gem_mdc_div_code(200000000) == 5 && + ck_gem_mdc_div_code(240000000) == 5 && ck_gem_mdc_div_code(320000000) == 6 && + ck_gem_mdc_div_code(400000000) == 7); + /* 200 MHz pclk / 96 = 2.08 MHz MDC, under the 2.5 MHz limit */ + CHECK(200000000u / 96u <= 2500000u); + gem_phy(3, 1, 0); + CHECK(ck_gem_mdio_read(&rio, 1, 2, &v) == 0 && v == 0x600d); + CHECK(ck_gem_mdio_read(&rio, 1, 3, &v) == 0 && v == 0x84a2); + CHECK(fp.bad_frame_seen == 0 && fp.last_man == ck_gem_mdio_frame_read(1, 3)); + CHECK(ck_gem_mdio_read(&rio, 5, 2, &v) == 0 && v == 0xffff); /* nothing at address 5 */ + CHECK(ck_gem_mdio_read(&rio, 32, 2, &v) == CK_PI5NET_E_ARG && ck_gem_mdio_read(&rio, 1, 32, &v) == CK_PI5NET_E_ARG); + CHECK(ck_gem_mdio_read(&rio, 1, 2, NULL) == CK_PI5NET_E_ARG); + /* the management logic never goes idle: bounded wait, then timeout (no hang) */ + gem_phy(0, 1, 0); + fp.never_idle = 1; + CHECK(ck_gem_mdio_read(&rio, 1, 2, &v) == CK_PI5NET_E_TIMEOUT); + CHECK(fp.t_us >= (uint64_t)CK_GEM_MDIO_POLLS * CK_GEM_MDIO_POLL_US); + /* enable: MPE set, other NCR bits kept, divider replaced and other NCFGR bits kept */ + gem_phy(0, 1, 0); + fp.r[(CK_RP1_ETH + CK_GEM_NCR) / 4] = 0x00000003u; + CHECK(ck_gem_mdio_enable(&rio, 200000000u) == 0); + CHECK(fp.r[(CK_RP1_ETH + CK_GEM_NCR) / 4] == (0x3u | CK_GEM_NCR_MPE)); + CHECK(fp.r[(CK_RP1_ETH + CK_GEM_NCFGR) / 4] == (0x800u | (5u << 18))); + gem_phy(0, 1, 0); + fp.stuck_ncfg = 1; + CHECK(ck_gem_mdio_enable(&rio, 200000000u) == CK_PI5NET_E_READBACK); + /* find: the PHY answers only after a few polls (still in reset): retries, then reports attempts */ + gem_phy(1, 1, 6); /* the first 6 register reads return 0xffff: 3 attempts of 2 reads fail, the 4th works */ + CHECK(ck_phy_find(&rio, 1, 20, 25, &id, &att, &hit) == 0); + CHECK(att == 4 && id.id == 0x600d84a2u && id.cls == CK_PHY_BCM54213PE); + /* never answers at 1, but something answers at 7: reported for the diagnosis */ + gem_phy(0, 7, 0); + CHECK(ck_phy_find(&rio, 1, 3, 1, &id, &att, &hit) == CK_PI5NET_E_TIMEOUT && att == 3 && hit == 7); + CHECK(id.cls == CK_PHY_ABSENT); + gem_phy(0, -1, 0); + CHECK(ck_phy_find(&rio, 1, 2, 1, &id, &att, &hit) == CK_PI5NET_E_TIMEOUT && hit == -1); + CHECK(ck_phy_find(&rio, 1, 0, 1, &id, &att, &hit) == CK_PI5NET_E_ARG); +} + +static void t_phy_id(void) +{ + struct ck_phy_id d; + /* BCM54213PE: Linux PHY_ID_BCM54213PE 0x600d84a2; OUI 18-C0-86 is "Broadcom" in the IEEE registry */ + ck_phy_id_decode(0x600d, 0x84a2, &d); + CHECK(d.id == 0x600d84a2u && d.oui == 0x18c086u && d.model == 0x0a && d.rev == 2 && d.cls == CK_PHY_BCM54213PE); + /* BCM54210E (0x600d84a0) is Broadcom but NOT this chip: Linux matches the whole 32 bits */ + ck_phy_id_decode(0x600d, 0x84a0, &d); + CHECK(d.cls == CK_PHY_BROADCOM_OTHER && d.oui == 0x18c086u); + ck_phy_id_decode(0x600d, 0x84a3, &d); /* a later revision of 54213PE is not exact either */ + CHECK(d.cls == CK_PHY_BROADCOM_OTHER); + /* independent vector: Realtek RTL8211 0x001cc916, OUI 00-E0-4C */ + ck_phy_id_decode(0x001c, 0xc916, &d); + CHECK(d.oui == 0x00e04cu && d.cls == CK_PHY_OTHER); + /* independent vector: Broadcom BCM5421 0x002060e0, OUI 00-10-18 */ + ck_phy_id_decode(0x0020, 0x60e0, &d); + CHECK(d.oui == 0x001018u && d.cls == CK_PHY_BROADCOM_OTHER); + ck_phy_id_decode(0xffff, 0xffff, &d); + CHECK(d.cls == CK_PHY_ABSENT); + ck_phy_id_decode(0x0000, 0x0000, &d); + CHECK(d.cls == CK_PHY_ABSENT); + CHECK(strcmp(ck_phy_class_name(CK_PHY_BCM54213PE), "BCM54213PE") == 0); +} + +int main(void) +{ + t_size_code(); + t_plan(); + t_plan_then_registers_then_verdict(); + t_apply_order(); + t_apply_failure_restores(); + t_quiesce(); + t_outbound(); + t_clock_rates(); + t_clock_decide(); + t_clock_apply(); + t_phy_reset(); + t_mdio(); + t_phy_id(); + return ck_t_verdict("test_pi5_net"); +} diff --git a/scripts/.crumb b/scripts/.crumb index 9f19fac7..2846bcff 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T21:27:22Z", + "compiled_at": "2026-10-13T22:05:34Z", "compiler": "crumb-compile/1", - "digest": "sha256:14efd43f6438e72f0045a0b2388de579210569ba4514e70fab32e317c8813e02", + "digest": "sha256:17ca56cca1e53f3d5ef7813006097037d3e3bbf8b4882fc907ea6c6979756d08", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 96, - "generated_at_commit": "03de8681ae58720bd01a45ccb5311c792f1d517b", - "source_tree": "sha256:7746885001c4f2ea92ca51e2ba12712ac2b65c86bc087c57a49ca8b90613cc82" + "generated_at_commit": "e7c361ad2f58c74e10e2e6dcb1d3210ea5c1c47a", + "source_tree": "sha256:fb2edc26100d338b6605de42a98d6832fa96927c73e4d0591529a999fb22b6e1" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_pi5_build_check.sh b/scripts/ck_pi5_build_check.sh index 1b6c3d81..4d27ec51 100755 --- a/scripts/ck_pi5_build_check.sh +++ b/scripts/ck_pi5_build_check.sh @@ -67,6 +67,22 @@ done if grep -q "software-only DMA confinement (Pi 5, weaker than the Spark)" <<<"$(strings -a "${ppc}")"; then echo "PASS pi5-pcie image carries the ADR 0020 label"; else echo "FAIL pi5-pcie image lacks the ADR 0020 label"; fail=1; fi test -s "${root}/target/native-kernel-pi5-pcie/BOOTAA64.EFI" || { echo "FAIL no pi5-pcie BOOTAA64.EFI"; fail=1; } +# AIENOS-PI5 network lane cut 2: the Pi image that narrows the inbound DMA window and reads the PHY ID (make pi5-net). +# No other image may carry it, and it must refuse every other Pi image flag. +make -C "${root}/native/kernel" pi5-net >/dev/null +pnet="${root}/target/native-kernel-pi5-net/aienos-ck.elf" +for s in ck_pi5net_run ck_pi5net_ib_plan ck_pi5net_ib_apply ck_rp1_phy_reset ck_gem_mdio_read ck_pi5pcie_ib_verdict ck_pi5_start; do + if defined "${pnet}" "${s}"; then echo "PASS pi5-net image defines ${s}"; else echo "FAIL pi5-net image lacks ${s}"; fail=1; fi +done +for s in ck_pi5net_run ck_pi5net_ib_plan ck_pi5net_ib_apply ck_rp1_phy_reset ck_gem_mdio_read; do + 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 plain pi5, pi5-store, pi5-infer, pi5-pcie and default images have no ${s}"; fi +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; } +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 + # PI5-4 cut 1: the Pi unlock image opens the Store only through the Slot 1 recovery-secret path. make -C "${root}/native/kernel" pi5-unlock >/dev/null uo="${root}/target/native-kernel-pi5-unlock/full-pi5-unlock/aienos-ck.elf"