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 / xhci_fence.h

114 lines · 6141 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/* xhci_fence.h -- DMA fence for the xHCI (USB) controller: the DMA-safety
18 * half of the SEED-0A keyboard gate (GATES.md "SEED-0A keyboard", rows 25-28,
19 * 31, 32). Behaviour follows crates/aienos-boot/src/usb_keyboard.rs (run,
20 * revoke_dma) and the Rust dma_gate; observables use the Rust line texts
21 * where the Rust gate (scripts/qemu_keyboard_test.sh) greps them.
22 *
23 * What it does, after ExitBootServices, in the devices stage:
24 * 1. find the first class 0x0c0330 (xHCI) function on the ECAM bus walk
25 * ("keyboard: xhci SSSS:BB:DD.F mmio 0x..");
26 * 2. the post-exit bus-master sweep (pci_sweep_bus_master) already ran at
27 * the start of the devices stage; read the controller's PCI COMMAND
28 * register back ("xhci_pci: command=0x.... bus_master=off") and stop if
29 * bus mastering is still on;
30 * 3. DMA gate, same rule as NVMe and virtio-net: no SMMU confinement means
31 * no DMA. The controller's DMA region is allocated first and becomes its
32 * stream's only SMMU window (ck_dma_confine); only then are memory
33 * decode and bus mastering enabled. Without an SMMU (or an SMMU that did
34 * not come up) the controller stays off: "dma_gate: xhci denied
35 * (NoSmmu), bus master stays off" + "keyboard: unavailable (SMMU DMA
36 * isolation not active)". There is no unconfined bypass for the xHCI,
37 * not even in the TEST-ONLY CK_QEMU_UNSAFE_DMA=1 build;
38 * 4. with the grant: map BAR0 and make sure the controller is halted
39 * (USBCMD.R/S = 0, USBSTS.HCH = 1, bounded wait); then, only if it
40 * halted, the operator phase of usb_kbd.c runs on this DMA region: HID
41 * boot keyboard, recovery-access hook and console shell (rows 29-30);
42 * 5. release, in the NVMe halt order: controller halted while it can
43 * still DMA, bus master off with read-back ("dma_gate: xhci bus master
44 * revoked"), the controller's COMMAND register read back again
45 * ("xhci_pci: after phase command=0x.... bus_master=off"), stream back
46 * to abort ("smmu: xhci stream 0x.. returned to abort (rc=0)").
47 *
48 * TEST-ONLY mutations (CK_TEST_XHCI_MUTATION, refused with
49 * CK_HARDWARE_STAGING by the Makefile and by an #error here); each must
50 * make scripts/qemu_ck_keyboard_test.sh --mutation see its rows FAIL:
51 * 1 bm-left-on the xHCI's bus mastering is switched back on after the
52 * sweep and left on before the gate (row 27)
53 * 2 no-revoke the release skips the bus-master clear (row 31)
54 * 3 grant-no-smmu with no SMMU the xHCI is granted DMA anyway (row 28)
55 * 4 no-sweep the post-exit bus-master sweep is skipped (row 26)
56 * A mutation image announces itself on the console. QEMU only. */
57#ifndef AIENOS_CK_XHCI_FENCE_H
58#define AIENOS_CK_XHCI_FENCE_H
59#include "pci.h"
60
61/* DMA region the controller is confined to (its only SMMU window): the 16
62 * pages of usb_kbd.c (DCBAA, device and input contexts, command and event
63 * rings, ERST, EP0 and interrupt-IN rings, buffer, up to 6 scratchpads). */
64#define CK_XHCI_DMA_BYTES (64u * 1024u)
65#define CK_XHCI_CLASS 0x0c0330u
66/* xHCI 1.2 section 5.4.1: the controller halts within 16 ms of R/S = 0. */
67#define CK_XHCI_HALT_WAIT_US 16000u
68
69enum {
70 CK_XHCI_OK = 0,
71 CK_XHCI_ABSENT = 1, /* no xHCI function: nothing to fence */
72 CK_XHCI_E_ARG = -601, /* BAR0 unusable */
73 CK_XHCI_E_BME = -602, /* bus mastering still on before the gate */
74 CK_XHCI_E_DENIED = -603, /* no SMMU confinement: controller left off */
75 CK_XHCI_E_DMA = -604, /* DMA region allocation failed */
76 CK_XHCI_E_HALT = -605, /* controller would not halt (still released) */
77 CK_XHCI_E_RELEASE = -606, /* stream could not be returned to abort: stays confined and live */
78};
79
80/* TEST-ONLY mutation numbers (CK_TEST_XHCI_MUTATION). */
81#define CK_XHCI_MUT_BM_LEFT_ON 1
82#define CK_XHCI_MUT_NO_REVOKE 2
83#define CK_XHCI_MUT_GRANT_NO_SMMU 3
84#define CK_XHCI_MUT_NO_SWEEP 4
85
86/* Steps 1-5 above. Leaves the controller released (bus master off, stream
87 * back to abort) before returning, except under the TEST-ONLY no-revoke
88 * mutation or when the SMMU refuses the abort (CK_XHCI_E_RELEASE: the stream
89 * stays confined, ck_xhci_live stays 1, no other controller is tried).
90 * Returns CK_XHCI_OK, CK_XHCI_ABSENT or a CK_XHCI_E_*. */
91int ck_xhci_fence(const pci_system *pci);
92#if defined(CK_CONSOLE_SESSION) && CK_CONSOLE_SESSION
93/* Console session (TEST-ONLY QEMU image): the same fence as ck_xhci_fence, with
94 * ck_kbd_session_phase as the operator phase. Same release, same rows. */
95int ck_xhci_session(const pci_system *pci);
96#endif
97/* Halt, bus master off, stream back to abort if still live. Idempotent. A
98 * refused abort keeps the stream confined; calling again retries it. */
99void ck_xhci_release(void);
100/* 1 while the controller may still DMA (bus master on or stream confined). */
101int ck_xhci_live(void);
102/* 1 when the post-exit bus-master sweep should run (0 only under the
103 * TEST-ONLY no-sweep mutation); prints the mutation banner once. */
104int ck_xhci_sweep_enabled(void);
105/* Called by the devices stage right after the sweep (TEST-ONLY bm-left-on
106 * mutation hook; does nothing in every other build). */
107void ck_xhci_after_sweep(const pci_system *pci);
108/* Short state for the devices summary line: absent, fenced, denied, ... */
109const char *ck_xhci_state(void);
110#ifdef CK_HOST_TEST
111/* Host tests only: forget the one-shot ACPI scan. -1 while a stream is live. */
112int ck_xhci_host_forget(void);
113#endif
114#endif