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 / disk / qemu / sdhci_payload.c

215 lines · 7885 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/*
18 * sdhci_payload.c -- bare-metal AArch64 test of native/disk/disk_sdhci.c
19 * against QEMU's emulated PCI SD Host Controller (sdhci-pci) and SD card on
20 * the "virt" machine (highmem=off: PCIe ECAM at 0x3f000000, 32-bit MMIO
21 * window at 0x10000000). MMU off, so every access is Device-nGnRnE. The
22 * driver is PIO only: no DMA, no bus mastering.
23 *
24 * QEMU is an emulator. A PASS here says the C driver works against QEMU's
25 * SDHCI and SD card models; it says nothing about the Pi 5's controller.
26 */
27#include <stdint.h>
28#include "../disk.h"
29#include "../disk_sdhci.h"
30
31#define UART 0x09000000ul
32#define ECAM 0x3f000000ul
33#define MMIO_BASE 0x10000000ul
34
35static void putc_(char c)
36{
37 volatile uint32_t *dr = (volatile uint32_t *)UART;
38 volatile uint32_t *fr = (volatile uint32_t *)(UART + 0x18);
39 while (*fr & (1u << 5))
40 ;
41 *dr = (uint32_t)(uint8_t)c;
42}
43static void puts_(const char *s) { while (*s) { if (*s == '\n') putc_('\r'); putc_(*s++); } }
44static void putdec(int64_t v)
45{
46 char b[24];
47 int n = 0;
48 uint64_t u = v < 0 ? (uint64_t)(-v) : (uint64_t)v;
49 if (v < 0) putc_('-');
50 do { b[n++] = (char)('0' + u % 10); u /= 10; } while (u);
51 while (n) putc_(b[--n]);
52}
53static void puthex(uint64_t v)
54{
55 puts_("0x");
56 for (int i = 60; i >= 0; i -= 4) putc_("0123456789abcdef"[(v >> i) & 15]);
57}
58
59static int fails, checks;
60static void check(int ok, const char *what, int64_t got)
61{
62 checks++;
63 if (!ok) {
64 fails++;
65 puts_("FAIL: ");
66 puts_(what);
67 puts_(" (got ");
68 putdec(got);
69 puts_(")\n");
70 }
71}
72
73/* ---- platform ops ---- */
74static volatile uint8_t *bar0;
75static uint32_t r32(void *c, uint32_t o) { (void)c; return *(volatile uint32_t *)(bar0 + o); }
76static void w32(void *c, uint32_t o, uint32_t v) { (void)c; *(volatile uint32_t *)(bar0 + o) = v; }
77static uint64_t cntfrq(void) { uint64_t v; __asm__ volatile("mrs %0, cntfrq_el0" : "=r"(v)); return v; }
78static uint64_t cntvct(void) { uint64_t v; __asm__ volatile("isb; mrs %0, cntvct_el0" : "=r"(v)); return v; }
79static void delay(void *c, uint32_t us)
80{
81 (void)c;
82 uint64_t f = cntfrq(), t0 = cntvct(), ticks = (f * us) / 1000000u + 1;
83 while (cntvct() - t0 < ticks)
84 ;
85}
86
87/* ---- PCIe: find the SD host controller (class 08 05), assign BAR0, enable MEM ---- */
88static volatile uint32_t *cfg(uint32_t bus, uint32_t dev, uint32_t fn, uint32_t off)
89{
90 return (volatile uint32_t *)(ECAM + (bus << 20) + (dev << 15) + (fn << 12) + off);
91}
92static int pci_find_sdhci(void)
93{
94 for (uint32_t d = 0; d < 32; d++) {
95 uint32_t id = *cfg(0, d, 0, 0);
96 if ((id & 0xffff) == 0xffff) continue;
97 uint32_t cls = *cfg(0, d, 0, 8) >> 8; /* class/subclass/progif */
98 if ((cls >> 8) != 0x0805) continue;
99 puts_("pci: SDHCI at 00:");
100 putdec(d);
101 puts_(".0 id ");
102 puthex(id);
103 puts_("\n");
104 *cfg(0, d, 0, 4) = 0; /* disable decode while sizing */
105 *cfg(0, d, 0, 0x10) = 0xffffffffu;
106 uint32_t lo = *cfg(0, d, 0, 0x10);
107 int is64 = ((lo >> 1) & 3) == 2;
108 uint32_t size = ~(lo & ~0xfu) + 1;
109 if (size < 0x100 || size > 0x1000000) { puts_("pci: bad BAR0 size\n"); return -1; }
110 *cfg(0, d, 0, 0x10) = (uint32_t)MMIO_BASE;
111 if (is64) *cfg(0, d, 0, 0x14) = 0;
112 *cfg(0, d, 0, 4) = 1u << 1; /* memory space only: PIO driver, no bus master */
113 bar0 = (volatile uint8_t *)MMIO_BASE;
114 puts_("pci: BAR0 size ");
115 puthex(size);
116 puts_("\n");
117 return 0;
118 }
119 return -1;
120}
121
122static uint8_t wbuf[256u * 512u], rbuf[256u * 512u];
123
124static void fill(uint8_t *b, uint32_t n, uint32_t seed)
125{
126 for (uint32_t i = 0; i < n; i++) b[i] = (uint8_t)(seed * 37u + i * 13u + (i >> 8));
127}
128static void zero(uint8_t *b, uint32_t n) { for (uint32_t i = 0; i < n; i++) ((volatile uint8_t *)b)[i] = 0; }
129static int same(const uint8_t *a, const uint8_t *b, uint32_t n)
130{
131 for (uint32_t i = 0; i < n; i++) if (a[i] != b[i]) return 0;
132 return 1;
133}
134
135/* The host script reads this block back out of the card image file. */
136static const char MARK[] = "AIENOS-SDHCI-QEMU block 5 written by disk_sdhci.c";
137
138static sdhci_card C;
139static const sdhci_ops OPS = {0, r32, w32, delay};
140
141void payload_main(void)
142{
143 puts_("AIENOS native SDHCI payload (C driver, QEMU virt)\n");
144 if (pci_find_sdhci()) {
145 puts_("no SD host controller found\nSDHCI_QEMU_PAYLOAD: FAIL\n");
146 return;
147 }
148 sdhci_cfg scfg = {0, 0, 1, 64}; /* 4-bit bus, at most 64 blocks per command */
149 int rc = sdhci_init(&C, &OPS, &scfg);
150 check(rc == SDHCI_OK, "sdhci_init", rc);
151 if (rc) {
152 puts_("last_cmd ");
153 putdec(C.last_cmd);
154 puts_(" last_int ");
155 puthex(C.last_int);
156 puts_("\nSDHCI_QEMU_PAYLOAD: FAIL\n");
157 return;
158 }
159 puts_("card: spec ");
160 putdec(C.spec);
161 puts_(" base_hz ");
162 putdec(C.base_hz);
163 puts_(" card_hz ");
164 putdec(C.card_hz);
165 puts_(" high_capacity ");
166 putdec(C.high_capacity);
167 puts_(" blocks ");
168 putdec((int64_t)C.blocks);
169 puts_(" rca ");
170 puthex(C.rca);
171 puts_("\n");
172 check(C.card_hz <= 25000000u && C.card_hz > 0, "card clock at or below 25 MHz", C.card_hz);
173 disk_dev *d = &C.dev;
174 check(d->block_size == 512, "block size", d->block_size);
175
176 /* 1, 8, 64 (one command), 200 (split into 64-block commands) blocks */
177 uint32_t sizes[] = {1, 8, 64, 200};
178 uint64_t lba = 16;
179 for (uint32_t i = 0; i < 4; i++) {
180 uint32_t n = sizes[i], bytes = n * 512u;
181 fill(wbuf, bytes, i + 1);
182 check(disk_write(d, lba, n, wbuf) == DISK_OK, "disk_write", n);
183 zero(rbuf, bytes);
184 check(disk_read(d, lba, n, rbuf) == DISK_OK, "disk_read", n);
185 check(same(wbuf, rbuf, bytes), "read-back matches write", n);
186 lba += n + 1;
187 }
188 zero(wbuf, 512);
189 for (uint32_t i = 0; MARK[i]; i++) wbuf[i] = (uint8_t)MARK[i];
190 check(disk_write(d, 5, 1, wbuf) == DISK_OK, "write marker block", 0);
191 check(disk_flush(d) == DISK_OK, "flush", 0);
192 /* out of range: refused by the driver, never sent */
193 check(sdhci_read(&C, C.blocks, 1, rbuf) == DISK_ERANGE, "range refusal", 0);
194 check(disk_read(d, d->block_count - 1, 2, rbuf) == DISK_ERANGE, "disk range refusal", 0);
195 check(C.ready, "still ready after refusals", 0);
196 /* last block */
197 fill(wbuf, 512, 99);
198 check(disk_write(d, d->block_count - 1, 1, wbuf) == DISK_OK, "write last block", 0);
199 /* full re-init (controller reset, card re-identified), data still there */
200 rc = sdhci_init(&C, &OPS, &scfg);
201 check(rc == SDHCI_OK, "re-init", rc);
202 zero(rbuf, 512);
203 check(disk_read(d, d->block_count - 1, 1, rbuf) == DISK_OK && same(wbuf, rbuf, 512),
204 "last block survives re-init", 0);
205 fill(wbuf, 200u * 512u, 4);
206 zero(rbuf, 200u * 512u);
207 check(disk_read(d, lba - 201, 200, rbuf) == DISK_OK && same(wbuf, rbuf, 200u * 512u),
208 "200-block extent survives re-init", 0);
209 puts_("payload checks ");
210 putdec(checks);
211 puts_(", failures ");
212 putdec(fails);
213 puts_("\n");
214 puts_(fails == 0 && checks > 0 ? "SDHCI_QEMU_PAYLOAD: PASS\n" : "SDHCI_QEMU_PAYLOAD: FAIL\n");
215}