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 / devices.c

254 lines · 11649 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/* devices.c -- ck_stage_devices: PCI (with the post-exit bus-master sweep),
18 * NVMe, virtio-net, xHCI DMA fence. Freestanding. */
19#include "devices.h"
20#include "ck.h"
21#include "nvme_bind.h"
22#include "pci.h"
23#include "mmio_window.h"
24#include "virtio_net.h"
25#include "net_bind.h"
26#include "rtl8127_bind.h"
27#if CK_RTL8127
28#include "aienos_report.h"
29#include "../core/klog.h"
30#endif
31#include "xhci_fence.h"
32#include "usb_kbd.h"
33#include "../../boot/slot_format.h"
34
35static pci_system g_pci;
36#if !CK_SPARK_INPUT
37static ck_nvme g_nvme;
38
39/* Later stages get only the AIENOS partition view (dev/disk_part.h), never the
40 * whole namespace: every access they make goes through ck_part_xlate. */
41const disk_dev *ck_dev_boot_disk(void) { return g_nvme.bound && g_nvme.part.valid ? &g_nvme.part.dev : 0; }
42const pci_system *ck_dev_pci(void) { return &g_pci; }
43
44/* The AIENOS image slots partition (native/boot/slot_format.h, P11 cut 2b) and
45 * the update inbox partition (P11 cut 3) on the same bound NVMe disk: further
46 * bounded partition views, found with the same strict GPT validation as the
47 * AIENOS partition; never the whole namespace. Each is looked up once, on
48 * first use. *rc_out: 0, a CK_GPT_* code, or CK_DEV_NO_BOOT_DISK when no disk
49 * is bound (or it was released). */
50struct typed_part {
51 ck_part part;
52 int rc; /* 1: not looked up yet */
53};
54static struct typed_part g_slots = {.rc = 1}, g_inbox = {.rc = 1};
55static const disk_dev *typed_disk(struct typed_part *tp, const uint8_t type[16], int *rc_out)
56{
57 int rc;
58 if (!g_nvme.bound || !g_nvme.part.valid) {
59 rc = CK_DEV_NO_BOOT_DISK;
60 } else {
61 if (tp->rc == 1) {
62 ck_gpt_info gi;
63 tp->rc = ck_gpt_find_type(&g_nvme.disk, type, &tp->part, &gi);
64 }
65 rc = tp->rc;
66 }
67 if (rc_out) *rc_out = rc;
68 return rc == 0 && tp->part.valid ? &tp->part.dev : 0;
69}
70const disk_dev *ck_dev_slots_disk(int *rc_out)
71{
72 static const uint8_t type[16] = CK_SLOT_PART_TYPE_BYTES;
73 return typed_disk(&g_slots, type, rc_out);
74}
75const disk_dev *ck_dev_inbox_disk(int *rc_out)
76{
77 static const uint8_t type[16] = CK_SLOT_INBOX_TYPE_BYTES;
78 return typed_disk(&g_inbox, type, rc_out);
79}
80
81void ck_dev_nvme_release(void)
82{
83 if (!g_nvme.pf) { /* bind stopped before any device access: nothing to quiesce */
84 g_nvme.claimed = 0;
85 return;
86 }
87 g_nvme.bound = 0;
88 if (g_nvme.bm_on) {
89 /* Halt order: NVMe normal shutdown (CC.SHN, wait CSTS.SHST) while the
90 * controller can still DMA, then bus master off, then the SMMU
91 * stream back to abort. */
92 ck_nvme_shutdown_bound(&g_nvme);
93 if (pci_bus_master_off(g_nvme.pf) == 0) {
94 g_nvme.bm_on = 0;
95 ck_printf("dma_gate: nvme bus master revoked\n");
96 } else /* bm_on stays set: the reset quiesce hook retries the revoke */
97 ck_printf("dma_gate: nvme bus master revoke FAILED (command register still has BME)\n");
98 }
99 if (g_nvme.confined) {
100 /* Bus mastering is off (or never came on); return the stream to
101 * abort as well, independently of the bus master state. A failed
102 * abort leaves the stream live in the SMMU service, so it stays
103 * confined here too: ownership is kept and a later release retries. */
104 int urc = ck_dma_unconfine(g_nvme.stream_id);
105 if (urc == 0) g_nvme.confined = 0;
106 ck_printf("smmu: nvme stream 0x%x %s (rc=%d%s%s)\n", g_nvme.stream_id,
107 urc == 0 ? "returned to abort" : "abort FAILED, stays confined", urc, urc ? ": " : "",
108 urc ? ck_dma_unconfine_why(urc) : "");
109 if (urc != 0) ck_dma_fault_report("nvme", g_nvme.stream_id);
110 }
111 /* Ownership is given back only once DMA is fully off (bus master and stream). */
112 if (!g_nvme.bm_on && !g_nvme.confined && !g_nvme.stuck) g_nvme.claimed = 0;
113}
114#endif
115
116int ck_stage_devices(void)
117{
118 int rc = pci_stage_probe(&g_pci);
119 if (rc) return rc;
120 /* Report-only multi-segment discovery for the boot log (aienos#286); the
121 * result is ignored, nothing below depends on it. */
122 (void)pci_stage_discover_report();
123#if !CK_SPARK_INPUT
124 /* Report-only read-only MMIO window on a discovered NVMe BAR0 (aienos#286, cut B3); result ignored. */
125 (void)ck_mmio_stage_report();
126#endif
127 /* Post-exit bus-master sweep (Rust dma_gate::sweep_bus_master): before any
128 * device is given DMA, clear BME on every endpoint firmware left with it
129 * set, so each device starts with DMA off. */
130 if (ck_xhci_sweep_enabled()) {
131 pci_sweep sw;
132 pci_sweep_bus_master(&g_pci.acc, &sw);
133 pci_sweep_report(&g_pci.ecam, &sw);
134 }
135 ck_xhci_after_sweep(&g_pci); /* TEST-ONLY bm-left-on mutation hook; no-op otherwise */
136 /* NVMe is bound from the multi-segment discovery result (aienos#31), any segment; the segment-0
137 * enumeration above is only used for the other devices and for log wording. */
138 uint32_t dn = 0;
139 const pci_found *df = pci_stage_disc_found(&dn);
140#if !CK_SPARK_INPUT
141 int was_owned = g_nvme.claimed; /* a refusal as "already owned" must not tear the owner down */
142 int nrc = ck_nvme_bind(&g_nvme, df, dn, &g_pci);
143 if (nrc && !was_owned) ck_dev_nvme_release(); /* a failed bind never keeps DMA */
144 (void)pci_nvme_disc_report(df, dn, g_nvme.claimed ? g_nvme.pf : 0);
145 virtio_pci_caps caps;
146 const pci_func *vf = 0;
147 int vrc = ck_virtio_net_probe(&g_pci, &caps, &vf);
148 /* virtio-net: SMMU-confined attach plus one bounded UDP round trip; the
149 * device is always released again before this returns (net_bind.h). A
150 * net failure is reported, never a devices-stage failure. */
151 int netrc = (vrc == 0 && vf) ? ck_net_bind_selftest(vf, &caps) : 1;
152 ck_printf("devices: pci=ok nvme=%s virtio_net=%s\n", g_nvme.bound ? "bound" : "unbound",
153 vrc != 0 ? "caps-refused" : !vf ? "absent" : netrc == 0 ? "selftest-ok" : "selftest-failed");
154#else
155 ck_printf("devices: storage-free Spark input candidate (NVMe/Store/Allen not linked)\n");
156#endif
157 /* xHCI: DMA fence (bus master off before the gate, SMMU-confined grant or
158 * fail-closed deny, halt, revoke, stream back to abort) with the operator
159 * keyboard phase inside the grant (usb_kbd.c). Reported, never a
160 * devices-stage failure. */
161 int xrc = ck_xhci_fence(&g_pci);
162 ck_printf("devices: xhci=%s (rc=%d)\n", ck_xhci_state(), xrc);
163 ck_kbd_recovery_report(ck_xhci_state());
164 if (ck_kbd_recovery_requested()) {
165 /* Recovery access chosen: the xHCI is already revoked; release every
166 * other device's DMA before the stub halts (no Store stage runs). */
167#if !CK_SPARK_INPUT
168 if (ck_net_live()) ck_net_release();
169 ck_dev_nvme_release();
170#endif
171#if CK_SPARK_INPUT
172 ck_printf("devices: recovery halt nvme=not-linked virtio_net=not-linked xhci=%s\n",
173 ck_xhci_live() ? "LIVE" : "released");
174 if (!ck_xhci_live()) ck_recovery_console_stub();
175#else
176 ck_printf("devices: recovery halt nvme=%s virtio_net=%s xhci=%s\n", g_nvme.bm_on || g_nvme.confined ? "LIVE" : "released",
177 ck_net_live() ? "LIVE" : "released", ck_xhci_live() ? "LIVE" : "released");
178 if (!g_nvme.bm_on && !g_nvme.confined && !ck_net_live() && !ck_xhci_live()) ck_recovery_console_stub();
179#endif
180 ck_panic("recovery access: device DMA still live, refusing to halt with it");
181 }
182#if CK_RTL8127
183 /* Spark attended boot (L198): the RTL8127 report. Reported, never a
184 * devices-stage failure; the chip is released again before this returns. */
185 {
186 static pci_func rf;
187 static char rlog[NET_REPORT_MAX];
188 int rrc = ck_rtl8127_find(df, dn, &rf);
189 if (rrc == CK_RTL_OK) {
190 size_t rn = ck_klog_read(rlog, sizeof rlog);
191 rrc = ck_rtl8127_report(&rf, rlog, rn, 20);
192 }
193 ck_printf("devices: rtl8127=%s (rc=%d)\n", rrc == CK_RTL_OK ? "report-sent" : rrc == CK_RTL_ABSENT ? "absent" : "failed", rrc);
194 }
195#endif
196#if CK_SPARK_INPUT
197 return ck_xhci_live() || ck_rtl8127_live() ? -1 : 0;
198#else
199 if (nrc == -1) return 0; /* no NVMe present: not a devices failure; Store reports it */
200 return nrc;
201#endif
202}
203
204/* Called by the core's ck_reset (PSCI reset/off after the final report, a
205 * panic or a fault report): no device may keep DMA across a reset. A normal
206 * boot has already released the NVMe controller in the Store stage, so this
207 * only reports that; after a panic mid-stage it does the full halt order. */
208void ck_stage_quiesce(void)
209{
210#if !CK_SPARK_INPUT
211 int net_live = ck_net_live();
212 if (net_live) ck_net_release();
213#endif
214 int rtl_live = ck_rtl8127_live();
215 if (rtl_live) ck_rtl8127_release();
216 int xhci_live = ck_xhci_live();
217 if (xhci_live) ck_xhci_release();
218#if !CK_SPARK_INPUT
219 int was_live = g_nvme.pf && (g_nvme.bm_on || g_nvme.confined);
220 if (was_live)
221 ck_dev_nvme_release();
222 /* A refused abort leaves the stream confined (or bus mastering on): that is LIVE, never released (#489). */
223 ck_printf("devices: quiesce before reset nvme=%s\n",
224 !g_nvme.pf ? "none"
225 : (g_nvme.bm_on || g_nvme.confined) ? "LIVE"
226 : was_live ? "released-now"
227 : "already-released");
228 if (net_live) ck_printf("devices: quiesce before reset virtio_net=%s\n", ck_net_live() ? "LIVE" : "released-now");
229#endif
230 if (rtl_live) ck_printf("devices: quiesce before reset rtl8127=%s\n", ck_rtl8127_live() ? "LIVE" : "released-now");
231 if (xhci_live) ck_printf("devices: quiesce before reset xhci=%s\n", ck_xhci_live() ? "LIVE" : "released-now");
232}
233
234#if defined(CK_CONSOLE_SESSION) && CK_CONSOLE_SESSION
235/* Final boot stage "console" (C3-1a, TEST-ONLY QEMU image built with
236 * CK_CONSOLE_SESSION=1): runs after every other boot step. Serial (PL011) and
237 * the USB keyboard feed one line editor and the existing shell until "exit".
238 * The USB keyboard runs inside the same xHCI DMA fence as the boot shell
239 * (SMMU window first, no SMMU means no DMA); the fence is re-entered here and
240 * revoked exactly as in the devices stage. Nothing here grants capabilities. */
241int ck_stage_console_session(void)
242{
243#if defined(CK_TEST_CONSOLE_MUTATION)
244 ck_printf("WARNING: TEST-ONLY console session MUTATION BUILD (CK_TEST_CONSOLE_MUTATION=bm-left-on-after-exit, "
245 "QEMU gate self-test only, never a PASS)\n");
246#endif
247 ck_printf("console_session: begin (TEST-ONLY QEMU image, CK_CONSOLE_SESSION=1; QEMU only, hardware NOT_RUN)\n");
248 int xrc = ck_xhci_session(&g_pci);
249 ck_printf("console_session: xhci fence returned rc=%d state=%s\n", xrc, ck_xhci_state());
250 ck_console_session_serial_only(); /* no-op once the exit came inside the fence */
251 ck_printf("console_session: end (exit) xhci=%s\n", ck_xhci_live() ? "LIVE" : "released");
252 return 0;
253}
254#endif