interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / pci.h

232 lines · 11031 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* pci.h -- AIENOS C kernel stage: PCIe ECAM discovery from ACPI MCFG.
18 *
19 * Freestanding. The only outside services are ck.h ones (ck_printf,
20 * ck_mmio_map, ck_acpi_find). The pure parts (MCFG parse, BAR sizing math)
21 * take plain memory and are tested on the host against crafted tables.
22 *
23 * Scope: segment 0 from the first MCFG allocation, bus start_bus, plus any
24 * bridge whose secondary bus firmware already programmed (bridges are never
25 * reprogrammed here). Memory BARs firmware left unassigned are assigned only
26 * inside a window the caller names (pci_window); with no window the device
27 * is reported and left disabled. QEMU is not hardware.
28 */
29#ifndef AIENOS_CK_PCI_H
30#define AIENOS_CK_PCI_H
31#include <stddef.h>
32#include <stdint.h>
33
34#define PCI_MAX_FUNCS 64u
35#define PCI_MAX_BARS 6u
36
37enum {
38 PCI_OK = 0,
39 PCI_E_NO_MCFG = -401, /* ACPI has no MCFG table */
40 PCI_E_MCFG = -402, /* MCFG malformed (length, no allocation, bus range) */
41 PCI_E_FULL = -403, /* more than PCI_MAX_FUNCS functions */
42 PCI_E_BAR = -404, /* BAR sizing / assignment failed */
43 PCI_E_ARG = -405,
44};
45
46/* One MCFG allocation (ACPI PCI Firmware spec, 16 bytes). */
47typedef struct {
48 uint64_t base; /* ECAM base of bus 0 of this segment */
49 uint16_t segment;
50 uint8_t start_bus, end_bus;
51} pci_ecam;
52
53/* Parse an MCFG table (header included, `len` = table length field).
54 * Picks the first allocation for segment 0. Validates: length >= 44 + 16,
55 * (length - 44) a multiple of 16, start_bus <= end_bus, base != 0, base
56 * 1 MiB aligned. */
57int pci_mcfg_parse(const uint8_t *table, size_t len, pci_ecam *out);
58
59/* Config space access. `cfg` returns the 4 KiB config page of bus/dev/fn
60 * or NULL when out of the ECAM bus range. */
61typedef struct {
62 volatile uint8_t *ecam; /* mapped ECAM for bus start_bus */
63 uint8_t start_bus, end_bus;
64} pci_bus_access;
65volatile uint8_t *pci_cfg(const pci_bus_access *a, uint8_t bus, uint8_t dev, uint8_t fn);
66uint32_t pci_r32(volatile uint8_t *cfg, uint32_t off);
67void pci_w32(volatile uint8_t *cfg, uint32_t off, uint32_t v);
68uint16_t pci_r16(volatile uint8_t *cfg, uint32_t off);
69void pci_w16(volatile uint8_t *cfg, uint32_t off, uint16_t v);
70
71/* ---- Read-only discovery (multi-segment) ----
72 *
73 * pci_enumerate sizes BARs, which writes config space (all-ones BAR writes,
74 * COMMAND changes). The functions below never do: pci_discover reads config
75 * space through a read-only helper (const volatile pointers) and nothing
76 * else, so it is safe to run against a device the kernel must not disturb.
77 *
78 * Discovery never sizes BARs and never writes. bar_raw holds exactly what
79 * firmware left in each BAR register, unsized: the size is unknown, and the
80 * low bits are type flags. The values are bus addresses, not assumed to be
81 * CPU addresses. Header type 0 has 6 BAR registers, type 1 has 2; the
82 * unused entries are 0. */
83
84/* Every MCFG allocation, in table order, up to cap entries. Same header
85 * checks as pci_mcfg_parse. Each entry needs base != 0, 1 MiB aligned,
86 * start_bus <= end_bus and base + ((end_bus + 1) << 20) not overflowing
87 * u64. Two allocations for one segment with overlapping bus ranges (exact
88 * duplicates included) are PCI_E_MCFG. More entries than cap: PCI_E_FULL
89 * (never truncated). `base` is the ECAM address of bus 0 of the segment, so
90 * bus b sits at base + (b << 20). */
91int pci_mcfg_parse_all(const uint8_t *table, size_t len, pci_ecam *out, uint32_t cap, uint32_t *n_out);
92
93typedef struct {
94 uint16_t segment;
95 uint8_t bus, dev, fn;
96 uint16_t vendor, device;
97 uint32_t class_code; /* class << 16 | subclass << 8 | progif */
98 uint8_t revision, header_type;
99 uint16_t subsys_vendor, subsys_id; /* header type 0 only, else 0 */
100 uint32_t bar_raw[PCI_MAX_BARS]; /* as read, unsized */
101 uint8_t secondary, subordinate; /* header type 1 only, else 0 */
102 uint16_t command; /* COMMAND register as read (bit 1: memory decode) */
103} pci_found;
104
105/* Scan segment `segment` through `a`: bus start_bus, then every type-1 bridge
106 * whose secondary > its own bus, secondary <= end_bus, subordinate >=
107 * secondary, depth < 8 and whose bus was not scanned already. Vendor 0xffff
108 * or 0 is absent; an absent function 0 skips the device; the multi-function
109 * bit is honored. Fills out[0..*n_out). *bridges_followed (may be NULL)
110 * counts bridges whose secondary bus was scanned. Capacity exhausted:
111 * PCI_E_FULL. */
112int pci_discover(const pci_bus_access *a, uint16_t segment, pci_found *out, uint32_t cap, uint32_t *n_out,
113 uint32_t *bridges_followed);
114
115/* Window for BARs firmware did not assign. [base, limit) and next. */
116typedef struct {
117 uint64_t base, limit, next;
118} pci_window;
119
120typedef struct {
121 uint64_t addr; /* assigned bus address (identity: CPU address) */
122 uint64_t size; /* 0: BAR not implemented */
123 uint8_t is64, prefetch, io, assigned_here;
124} pci_bar;
125
126typedef struct {
127 uint8_t bus, dev, fn;
128 uint16_t segment; /* PCI segment of the ECAM it was found in (IORT lookup key) */
129 uint16_t vendor, device;
130 uint32_t class_code; /* class << 16 | subclass << 8 | progif */
131 uint8_t header_type;
132 uint16_t subsys_vendor, subsys_id;
133 volatile uint8_t *cfg;
134 pci_bar bar[PCI_MAX_BARS];
135 int bars_ok; /* every memory BAR assigned (by firmware or here) */
136} pci_func;
137
138typedef struct {
139 pci_ecam ecam;
140 pci_bus_access acc;
141 pci_func f[PCI_MAX_FUNCS];
142 uint32_t n;
143 uint32_t bridges_followed;
144} pci_system;
145
146/* Size of a memory BAR from the value read back after writing all ones
147 * (lo/hi halves; hi used only when is64). 0 when not implemented. */
148uint64_t pci_bar_size(uint32_t lo_readback, uint32_t hi_readback, int is64);
149
150/* Allocate size (power of two) aligned to size from the window. 0 on
151 * exhaustion or overflow. */
152uint64_t pci_window_alloc(pci_window *w, uint64_t size);
153
154/* Enumerate: bus start_bus, then programmed bridges. Sizes every BAR; a
155 * memory BAR reading 0 is assigned from `win` when win != NULL. Leaves
156 * COMMAND as firmware set it except while sizing (decode is turned off
157 * around the sizing writes and restored). */
158int pci_enumerate(pci_system *s, pci_window *win);
159
160/* Turn on memory decode (+ bus master when bm != 0). */
161void pci_enable(const pci_func *f, int bm);
162/* Clear Bus Master Enable and read it back: 0 when it reads clear. */
163int pci_bus_master_off(const pci_func *f);
164
165
166/* Post-exit bus-master sweep (port of crates/aienos-kernel/src/dma_gate.rs
167 * sweep_bus_master): walk every bus in the ECAM window, every device and
168 * (when marked multi-function) every function, and clear Bus Master Enable
169 * on every endpoint (header type 0) that has it set, reading it back.
170 * Bridges (header type 1) are counted, never changed: a bridge's BME only
171 * forwards DMA from the endpoints below it, which are cleared one by one.
172 * Runs before any device is given DMA, so every device starts with DMA off. */
173#define PCI_SWEEP_FINDINGS 8u
174typedef struct {
175 uint8_t bus, dev, fn;
176 uint16_t command_before, command_after; /* after: read back after the clear */
177} pci_bme_finding;
178typedef struct {
179 uint16_t functions; /* functions that answered a config read */
180 uint16_t bridges; /* header type 1 among them (left untouched) */
181 uint16_t bridges_bme; /* bridges with BME set */
182 uint16_t endpoints_bme; /* endpoints found with BME set (and cleared) */
183 uint16_t still_enabled; /* endpoints whose BME still reads set after the clear */
184 pci_bme_finding f[PCI_SWEEP_FINDINGS]; /* first PCI_SWEEP_FINDINGS endpoints */
185} pci_sweep;
186void pci_sweep_bus_master(const pci_bus_access *a, pci_sweep *out);
187/* Print the sweep in the Rust kernel's format: one "dma_sweep: seg ..." line
188 * plus one "dma_sweep: bme cleared|STUCK ..." line per recorded finding. */
189void pci_sweep_report(const pci_ecam *e, const pci_sweep *s);
190
191/* First function with this class (class/subclass/progif masked by mask). */
192const pci_func *pci_find_class(const pci_system *s, uint32_t class_code, uint32_t mask);
193const pci_func *pci_find_id(const pci_system *s, uint16_t vendor, uint16_t device);
194
195const char *pci_class_name(uint32_t class_code);
196
197/* Stage glue (ck.h): find MCFG, map ECAM, enumerate, print one line per
198 * function. Uses the QEMU virt 32-bit window only when the ECAM base is a
199 * known QEMU virt base (0x4010000000 or 0x3f000000). */
200int pci_stage_probe(pci_system *s);
201
202/* Report-only, read-only multi-segment discovery for the boot log (see pci.c). */
203int pci_stage_discover_report(void);
204/* Every function the last report found, across segments (capped), for the
205 * read-only MMIO window report (mmio_window.h). */
206const pci_found *pci_stage_disc_found(uint32_t *n);
207
208/* ECAM accessor the last pci_stage_discover_report mapped for `segment` whose
209 * bus range holds `bus`. PCI_E_ARG when that report mapped none. */
210int pci_stage_disc_access(uint16_t segment, uint8_t bus, pci_bus_access *out);
211
212/* Turn one discovered endpoint (header type 0) into a pci_func through its
213 * segment's accessor, so a driver can bind a function outside segment 0
214 * (aienos#31, discovery-to-binding cut). Re-reads vendor/device and refuses
215 * (PCI_E_ARG) if they differ from `f`. Sizes the BARs like pci_enumerate
216 * (decode turned off around the all-ones writes and restored), so unlike
217 * pci_discover this WRITES config space of that one function; it never assigns
218 * a BAR (bars_ok = 0 when a sized BAR is unassigned). */
219int pci_func_from_found(pci_func *out, const pci_found *f, const pci_bus_access *a);
220
221/* NVMe through multi-segment discovery (aienos#31, cut S1). Pure selector over
222 * a discovery result: the first function (lowest segment, then bus, device,
223 * function) whose class_code & mask == class_code & mask of the argument, or
224 * NULL. *count (may be NULL) receives how many functions match. Reads only. */
225const pci_found *pci_disc_find_class(const pci_found *f, uint32_t n, uint32_t class_code, uint32_t mask,
226 uint32_t *count);
227/* Report-only: print the discovery's NVMe (class 0x010802) candidate and
228 * whether the function the NVMe bind owns (`bound`, NULL when none)
229 * is the same. Changes no state; returns the number of candidates. */
230int pci_nvme_disc_report(const pci_found *f, uint32_t n, const pci_func *bound);
231
232#endif