aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / pi5_pcie.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* pi5_pcie.h -- Raspberry Pi 5: how the kernel reaches the BCM2712 PCIe root |
| 18 | * complex that hosts the RP1 south bridge, plus a read-only enumerator. |
| 19 | * AIENOS-PI5 network lane, GEM-first plan step 1. NO GEM register code here. |
| 20 | * |
| 21 | * Everything below is READ-ONLY toward the hardware except one write: the |
| 22 | * root complex's config window index (CK_PI5PCIE_EXT_CFG_INDEX), which only |
| 23 | * selects which function the 4 KiB config window shows. No BAR sizing, no |
| 24 | * COMMAND change, no inbound window change. |
| 25 | * |
| 26 | * WHY THE ROOT COMPLEX'S OWN CONFIG WINDOW, NOT ECAM (decision, cited): |
| 27 | * - RP1 sits behind PCIe controller 2 (segment 2, RC registers at |
| 28 | * 0x1000120000). Frozen firmware = aien-dev/rpi5-uefi tag v0.3 (c1ca184), which |
| 29 | * pins worproject/edk2-platforms 8e1779b. In that tree: |
| 30 | * Platform/RaspberryPi/RPi5/AcpiTables/Mcfg.aslc:26-48 the MCFG entry for |
| 31 | * each segment has base = RC base + PCIE_EXT_CFG_DATA (0x8000) and bus |
| 32 | * range 0..0: a 4 KiB window, not an ECAM that can address bus 1. |
| 33 | * Platform/RaspberryPi/RPi5/Drivers/RpiPlatformDxe/ConfigTable.c:350-354 |
| 34 | * in the default (DEN0115) mode the MCFG signature is zeroed ("MCFG |
| 35 | * must be hidden"); ConfigTable.h:23 makes NXPMX6_DEN0115 the default |
| 36 | * and ConfigTable.c:301-308 clears the NXPMX6 half for non-Windows OSes. |
| 37 | * Silicon/Broadcom/Bcm27xx/Library/Bcm2712PciSegmentLib/PciSegmentLib.c: |
| 38 | * 89-115 is the firmware's own access rule: bus 0 = the root port at the |
| 39 | * RC base; bus > 0 = write bus/dev/fn to RC+0x9000 (INDEX), read the |
| 40 | * function at RC+0x8000+reg (DATA). 96-104: only dev 0 fn 0 exists on |
| 41 | * bus 0 and only dev 0 on bus 1. |
| 42 | * Platform/RaspberryPi/RPi5/AcpiTables/PcieCommon.asi:30-38 (_INI) points |
| 43 | * INDEX at 01:00.0 for OSes that cannot do more. The kernel does not |
| 44 | * run AML (core/pi5.c:15-21), so it writes INDEX itself. |
| 45 | * - So ECAM via MCFG is unusable here and the RC window is used always. The |
| 46 | * decision function reports WHY, from the actual MCFG if the firmware |
| 47 | * exposes one. Which MCFG the running firmware shows is UNVERIFIED until |
| 48 | * the hardware run (the stored UEFI variable AcpiPcieEcamCompatMode can |
| 49 | * change it). |
| 50 | * - Linux reference reading only: drivers/pci/controller/pcie-brcmstb.c uses |
| 51 | * the same INDEX/DATA pair; drivers/misc/rp1 is the RP1 platform driver. |
| 52 | * Not re-read in this cut (UNVERIFIED against the Linux tree). |
| 53 | * |
| 54 | * Inbound window registers: edk2-platforms |
| 55 | * Silicon/Broadcom/Bcm27xx/Include/IndustryStandard/Bcm2712Pcie.h:97-104, |
| 56 | * 133-170, 196-201; encode rules in Bcm2712PciHostBridge.c:113-170 (size code |
| 57 | * and remap). The firmware programs one window per enabled RC: bus 0 -> CPU 0, |
| 58 | * 64 GiB (Bcm2712PciHostBridgeLib.c:157-159). That is the whole of RAM, so the |
| 59 | * hardware backstop of ADR 0020 rule 6 is NOT in place unless a later cut |
| 60 | * reprograms it. This file only reports; it never programs. */ |
| 61 | #ifndef AIENOS_CK_PI5_PCIE_H |
| 62 | #define AIENOS_CK_PI5_PCIE_H |
| 63 | #include <stddef.h> |
| 64 | #include <stdint.h> |
| 65 | |
| 66 | #define CK_PI5PCIE_RC2_PHYS 0x1000120000ull /* BCM2712_BRCMSTB_PCIE2_BASE */ |
| 67 | #define CK_PI5PCIE_RC_BYTES 0xa000u /* through INDEX (0x9000) + 0x1000 */ |
| 68 | #define CK_PI5PCIE_SEGMENT 2u |
| 69 | #define CK_PI5PCIE_EXT_CFG_DATA 0x8000u |
| 70 | #define CK_PI5PCIE_EXT_CFG_INDEX 0x9000u |
| 71 | |
| 72 | #define CK_PI5PCIE_RP1_VENDOR 0x1de4u /* Rp1.h:12 */ |
| 73 | #define CK_PI5PCIE_RP1_DEVICE 0x0001u /* Rp1.h:13 */ |
| 74 | |
| 75 | enum { |
| 76 | CK_PI5PCIE_OK = 0, |
| 77 | CK_PI5PCIE_E_ADDR = -1, /* bus/dev/fn/reg the root complex cannot address */ |
| 78 | CK_PI5PCIE_E_ARG = -2 |
| 79 | }; |
| 80 | |
| 81 | /* Register access to the root complex block (offset from the RC base). A |
| 82 | * host test stands a fake register file behind it. */ |
| 83 | struct ck_pi5pcie_io { |
| 84 | uint32_t (*rd)(void *ctx, uint32_t off); |
| 85 | void (*wr)(void *ctx, uint32_t off, uint32_t v); |
| 86 | void *ctx; |
| 87 | }; |
| 88 | |
| 89 | /* One 32-bit config read. Addressable: bus 0 dev 0 fn 0 (the root port, at the |
| 90 | * RC base) and bus 1 dev 0 fn 0..7 (through INDEX/DATA). reg is dword aligned |
| 91 | * and < 4096. Anything else is E_ADDR and touches no register. */ |
| 92 | int ck_pi5pcie_cfg_read32(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, uint32_t reg, |
| 93 | uint32_t *out); |
| 94 | |
| 95 | struct ck_pi5pcie_func { |
| 96 | uint8_t bus, dev, fn; |
| 97 | uint16_t vendor, device; |
| 98 | uint32_t class_code; /* class << 16 | subclass << 8 | progif */ |
| 99 | uint8_t revision, header_type, nbars; |
| 100 | uint16_t command; |
| 101 | uint32_t bar_raw[6]; /* as read, never sized */ |
| 102 | uint8_t primary, secondary, subordinate; /* header type 1 only */ |
| 103 | }; |
| 104 | |
| 105 | /* 0 = present, 1 = absent (vendor 0xffff or 0), < 0 = E_ADDR. */ |
| 106 | int ck_pi5pcie_read_func(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, |
| 107 | struct ck_pi5pcie_func *f); |
| 108 | |
| 109 | enum { CK_PI5PCIE_RP1_OK = 0, CK_PI5PCIE_RP1_ABSENT = 1, CK_PI5PCIE_RP1_MISMATCH = 2 }; |
| 110 | /* rc = return of ck_pi5pcie_read_func for 01:00.0. */ |
| 111 | int ck_pi5pcie_rp1_check(int read_rc, const struct ck_pi5pcie_func *f); |
| 112 | |
| 113 | enum { CK_PI5PCIE_BAR_EMPTY = 0, CK_PI5PCIE_BAR_MEM32 = 1, CK_PI5PCIE_BAR_MEM64 = 2, CK_PI5PCIE_BAR_IO = 3, |
| 114 | CK_PI5PCIE_BAR_BAD = 4 }; |
| 115 | struct ck_pi5pcie_bar { |
| 116 | uint8_t kind, prefetch; |
| 117 | uint64_t addr; /* as firmware left it; size unknown (not sized) */ |
| 118 | }; |
| 119 | /* Decode BAR register `idx` of `nbars`. Returns how many registers it used |
| 120 | * (1, or 2 for a 64-bit memory BAR). EMPTY = raw 0: unimplemented or |
| 121 | * unassigned, which unsized reading cannot tell apart. */ |
| 122 | uint32_t ck_pi5pcie_bar_decode(const uint32_t *raw, uint32_t idx, uint32_t nbars, struct ck_pi5pcie_bar *b); |
| 123 | |
| 124 | /* ---- inbound windows (PCIe -> memory) ---- */ |
| 125 | #define CK_PI5PCIE_IB_WINDOWS 8u /* PCIE_NUM_INBOUND_WINDOWS */ |
| 126 | #define CK_PI5PCIE_MISC_CTRL 0x4008u |
| 127 | #define CK_PI5PCIE_RC_BAR1_LO 0x402cu /* GISB window: firmware disables it */ |
| 128 | #define CK_PI5PCIE_RC_BAR3_LO 0x403cu /* SCB window: firmware disables it */ |
| 129 | #define CK_PI5PCIE_BAR_SIZE_MASK 0x1fu |
| 130 | |
| 131 | struct ck_pi5pcie_ibwin { |
| 132 | uint32_t idx; |
| 133 | uint8_t enabled; /* size code != 0 */ |
| 134 | uint8_t size_valid; /* code decodes, base aligned to size */ |
| 135 | uint8_t remap_en; /* UBUS remap enable (bit 0) */ |
| 136 | uint64_t bus_base, size, cpu_base; |
| 137 | }; |
| 138 | /* Size code -> bytes. 1 = valid (*size set), 0 = disabled (code 0), -1 = a |
| 139 | * code the firmware never writes. 1..21 = 64 KiB..64 GiB, 0x1c..0x1f = |
| 140 | * 4 KiB..32 KiB (Bcm2712PciHostBridge.c:119-127). */ |
| 141 | int ck_pi5pcie_ib_size_decode(uint32_t code, uint64_t *size); |
| 142 | int ck_pi5pcie_ib_read(const struct ck_pi5pcie_io *io, uint32_t idx, struct ck_pi5pcie_ibwin *w); |
| 143 | |
| 144 | enum { |
| 145 | CK_PI5PCIE_DMA_UNKNOWN = 0, /* cannot tell (bad window, no remap, extra BAR windows) */ |
| 146 | CK_PI5PCIE_DMA_NO_WINDOW = 1, /* no inbound window enabled: DMA cannot reach memory at all */ |
| 147 | CK_PI5PCIE_DMA_MISS = 2, /* windows exist but none reaches the bounce region */ |
| 148 | CK_PI5PCIE_DMA_PARTIAL = 3, /* only part of the bounce region is reachable */ |
| 149 | CK_PI5PCIE_DMA_WIDER = 4, /* bounce region reachable AND memory outside it: NOT confined */ |
| 150 | CK_PI5PCIE_DMA_CONFINED = 5 /* reach == bounce region exactly */ |
| 151 | }; |
| 152 | struct ck_pi5pcie_dmav { |
| 153 | int verdict; |
| 154 | uint32_t windows; /* enabled windows */ |
| 155 | uint64_t reach_bytes; /* union of CPU ranges the windows reach */ |
| 156 | uint64_t inside_bytes; /* of which inside the bounce region */ |
| 157 | uint64_t outside_bytes; /* of which outside it */ |
| 158 | }; |
| 159 | /* ADR 0020 rule 6: does the hardware window confine inbound DMA to the bounce |
| 160 | * region [b_cpu, b_cpu+b_size)? Works in CPU address space (bus -> CPU through |
| 161 | * each window's remap). `unmodelled` = enabled windows this model does not |
| 162 | * cover (RC_BAR1/BAR3); they can only turn CONFINED into UNKNOWN. */ |
| 163 | int ck_pi5pcie_ib_verdict(const struct ck_pi5pcie_ibwin *w, uint32_t n, uint32_t unmodelled, uint64_t b_cpu, |
| 164 | uint64_t b_size, struct ck_pi5pcie_dmav *out); |
| 165 | const char *ck_pi5pcie_verdict_name(int v); |
| 166 | |
| 167 | /* ---- which door: ECAM via MCFG, or the RC's own window ---- */ |
| 168 | enum { |
| 169 | CK_PI5PCIE_WHY_NO_MCFG = 0, /* firmware exposes no MCFG (hidden or absent) */ |
| 170 | CK_PI5PCIE_WHY_MALFORMED = 1, |
| 171 | CK_PI5PCIE_WHY_NO_SEGMENT = 2, /* MCFG has no entry for segment 2 */ |
| 172 | CK_PI5PCIE_WHY_DATA_WINDOW = 3, /* entry base is RC+0x8000: the 4 KiB window, not an ECAM */ |
| 173 | CK_PI5PCIE_WHY_NARROW = 4, /* entry bus range stops before bus 1 */ |
| 174 | CK_PI5PCIE_WHY_ECAM_CANDIDATE = 5 /* looks like a real ECAM: still NOT used here (UNVERIFIED) */ |
| 175 | }; |
| 176 | struct ck_pi5pcie_door { |
| 177 | int why; |
| 178 | uint64_t base; |
| 179 | uint8_t bus_min, bus_max; |
| 180 | }; |
| 181 | /* The kernel always uses the RC window (the only door implemented and |
| 182 | * proven by the firmware source); this explains why ECAM is not used. `mcfg` |
| 183 | * may be NULL; `len` is the table length field. */ |
| 184 | void ck_pi5pcie_door_decide(const uint8_t *mcfg, size_t len, uint16_t segment, uint64_t rc_phys, |
| 185 | struct ck_pi5pcie_door *out); |
| 186 | const char *ck_pi5pcie_why_name(int why); |
| 187 | |
| 188 | /* Boot report (CK_PI5_PCIE image only): maps RC2, prints one line per fact, |
| 189 | * allocates and zeroes the ADR 0020 bounce window and judges the hardware |
| 190 | * inbound window against it. Read-only apart from the INDEX select. */ |
| 191 | #ifdef CK_PI5_PCIE |
| 192 | void ck_pi5pcie_report(void); |
| 193 | #endif |
| 194 | |
| 195 | #endif |