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 / x86 / ahci_x86.c

420 lines · 15165 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/* ahci_x86.c -- AIENOS-IMAC-KERNEL-1: the SATA disks behind an AHCI
18 * controller for the 2017 iMac (Intel 8086:a102, the 1 TB hard disk half of
19 * the Fusion Drive) and QEMU q35 (ICH9). Read-only unless a write range is
20 * opened.
21 *
22 * Each controller is driven polled,
23 * with interrupts off: AHCI mode on, no controller or port reset (the links
24 * the firmware brought up are used as they are), per port the command engine
25 * stopped, the command list and received-FIS area moved into frames the
26 * kernel owns, the engine started again. The PCI walk that finds the
27 * controllers is pci_x86.c.
28 *
29 * Four ATA commands exist in this file: IDENTIFY DEVICE (0xEC), READ DMA
30 * EXT (0x25), WRITE DMA EXT (0x35) and FLUSH CACHE EXT (0xEA); issue()
31 * refuses every other opcode. Writes and flushes are refused unless the
32 * transfer lies wholly inside a write range opened with ck_x86_ahci_allow,
33 * and only x86/memory_x86.c opens ranges (the Makefile checks): the GPT
34 * blocks of the approved hard disk while Allen's partition is added, then
35 * the two memory copies inside that partition. A disk with no open range
36 * cannot be changed. The IDENTIFY serial number words are never read. QEMU
37 * runs of this path qualify nothing physical. */
38#include "../core/ck_internal.h"
39#include "../mm/frames.h"
40#include "disk.h"
41#include "disk_part.h"
42#include "x86.h"
43
44#define AHCI_MAX_CTRL 4u
45#define AHCI_MAX_DISKS 8u
46#define BOUNCE_BYTES (64u << 10)
47
48/* HBA registers */
49#define HBA_CAP 0x00
50#define HBA_GHC 0x04
51#define HBA_PI 0x0c
52#define HBA_VS 0x10
53#define HBA_CAP2 0x24
54#define HBA_BOHC 0x28
55#define CAP_S64A (1u << 31)
56#define GHC_AE (1u << 31)
57#define GHC_IE (1u << 1)
58#define BOHC_BOS (1u << 0)
59#define BOHC_OOS (1u << 1)
60/* port registers, at 0x100 + 0x80 * port */
61#define P_CLB 0x00
62#define P_CLBU 0x04
63#define P_FB 0x08
64#define P_FBU 0x0c
65#define P_IS 0x10
66#define P_IE 0x14
67#define P_CMD 0x18
68#define P_TFD 0x20
69#define P_SIG 0x24
70#define P_SSTS 0x28
71#define P_SERR 0x30
72#define P_CI 0x38
73#define CMD_ST (1u << 0)
74#define CMD_FRE (1u << 4)
75#define CMD_FR (1u << 14)
76#define CMD_CR (1u << 15)
77#define IS_TFES (1u << 30)
78#define TFD_ERR 0x01u
79#define TFD_DRQ 0x08u
80#define TFD_BSY 0x80u
81#define SIG_ATA 0x00000101u
82
83#define ATA_IDENTIFY 0xec
84#define ATA_READ_DMA_EXT 0x25
85#define ATA_WRITE_DMA_EXT 0x35
86#define ATA_FLUSH_CACHE_EXT 0xea
87#define AHCI_MAX_ALLOW 3u
88
89/* pci_x86.c: sector 0 and the GPT check of one disk, printed. */
90void ck_x86_disk_report(const char *tag, unsigned ctrl, unsigned index, const disk_dev *dev);
91
92struct ahci_disk {
93 volatile uint8_t *port;
94 uint8_t *cl, *fis, *ct, *bounce; /* kernel frames the controller reads and writes */
95 uint64_t tsc_hz;
96 unsigned ctrl, index;
97 char model[41];
98 uint64_t allow_first[AHCI_MAX_ALLOW], allow_last[AHCI_MAX_ALLOW]; /* open write ranges, inclusive */
99 unsigned nallow;
100 disk_dev dev;
101};
102
103static struct ahci_disk disks[AHCI_MAX_DISKS];
104static unsigned ndisks;
105static uint64_t g_tsc_hz;
106
107static uint32_t mr(volatile uint8_t *b, unsigned off) { return *(volatile uint32_t *)(b + off); }
108static void mw(volatile uint8_t *b, unsigned off, uint32_t v) { *(volatile uint32_t *)(b + off) = v; }
109
110/* Waits until (reg & mask) == want, at most ms milliseconds of TSC. */
111static int wait_reg(volatile uint8_t *b, unsigned off, uint32_t mask, uint32_t want, unsigned ms)
112{
113 uint64_t t0 = ck_rdtsc(), n = g_tsc_hz / 1000 * ms;
114 while ((mr(b, off) & mask) != want) {
115 if (ck_rdtsc() - t0 > n)
116 return -1;
117 ck_pause();
118 }
119 return 0;
120}
121
122static int alloc_low(struct ck_frames *f, uint64_t pages, int low4g, uint8_t **out)
123{
124 uint64_t pa;
125 if (ck_frames_alloc(f, pages, CK_PAGE, &pa))
126 return -1;
127 if (low4g && pa + pages * CK_PAGE > (1ull << 32)) {
128 ck_frames_free(f, pa, pages);
129 return -1;
130 }
131 memset((void *)(uintptr_t)pa, 0, pages * CK_PAGE);
132 *out = (uint8_t *)(uintptr_t)pa;
133 return 0;
134}
135
136static uint64_t pa_of(const void *p) { return (uint64_t)(uintptr_t)p; }
137
138/* One command in slot 0, polled. Only IDENTIFY DEVICE, READ DMA EXT, WRITE
139 * DMA EXT (the data already in the bounce buffer) and FLUSH CACHE EXT (no
140 * data, bytes 0). */
141static int issue(struct ahci_disk *d, uint8_t cmd, uint64_t lba, uint32_t count, uint32_t bytes)
142{
143 int rw = cmd == ATA_READ_DMA_EXT || cmd == ATA_WRITE_DMA_EXT;
144 if (cmd != ATA_IDENTIFY && !rw && cmd != ATA_FLUSH_CACHE_EXT)
145 return DISK_EARG;
146 if ((cmd == ATA_FLUSH_CACHE_EXT) != !bytes || bytes > BOUNCE_BYTES)
147 return DISK_EARG;
148 volatile uint8_t *p = d->port;
149 if (wait_reg(p, P_TFD, TFD_BSY | TFD_DRQ, 0, 1000))
150 return DISK_ESTATE;
151 uint32_t *hdr = (uint32_t *)d->cl;
152 /* CFL 5 dwords; W (bit 6) for a write; one PRD, none for a flush */
153 hdr[0] = 5u | (cmd == ATA_WRITE_DMA_EXT ? 1u << 6 : 0u) | (bytes ? 1u << 16 : 0u);
154 hdr[1] = 0;
155 hdr[2] = (uint32_t)pa_of(d->ct);
156 hdr[3] = (uint32_t)(pa_of(d->ct) >> 32);
157 memset(d->ct, 0, 128 + 16);
158 uint8_t *f = d->ct;
159 f[0] = 0x27; /* host to device register FIS */
160 f[1] = 0x80; /* command */
161 f[2] = cmd;
162 if (rw) {
163 f[4] = (uint8_t)lba;
164 f[5] = (uint8_t)(lba >> 8);
165 f[6] = (uint8_t)(lba >> 16);
166 f[7] = 0x40; /* LBA mode */
167 f[8] = (uint8_t)(lba >> 24);
168 f[9] = (uint8_t)(lba >> 32);
169 f[10] = (uint8_t)(lba >> 40);
170 f[12] = (uint8_t)count;
171 f[13] = (uint8_t)(count >> 8);
172 }
173 uint32_t *prd = (uint32_t *)(d->ct + 128);
174 prd[0] = (uint32_t)pa_of(d->bounce);
175 prd[1] = (uint32_t)(pa_of(d->bounce) >> 32);
176 prd[2] = 0;
177 prd[3] = bytes ? bytes - 1u : 0u;
178 __asm__ volatile("mfence" ::: "memory");
179 mw(p, P_IS, 0xffffffffu);
180 mw(p, P_CI, 1);
181 uint64_t t0 = ck_rdtsc(), lim = d->tsc_hz * 30; /* a spun-down hard disk spins up first; a flush
182 * may also wait for the cache to reach the platters */
183 for (;;) {
184 if (mr(p, P_IS) & IS_TFES)
185 return DISK_EIO;
186 if (!(mr(p, P_CI) & 1))
187 break;
188 if (ck_rdtsc() - t0 > lim)
189 return DISK_EIO;
190 ck_pause();
191 }
192 if (mr(p, P_TFD) & TFD_ERR)
193 return DISK_EIO;
194 __asm__ volatile("mfence" ::: "memory");
195 return hdr[1] == bytes ? DISK_OK : DISK_EIO;
196}
197
198static int ahci_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf)
199{
200 struct ahci_disk *d = ctx;
201 uint32_t bytes = count * d->dev.block_size;
202 int rc = issue(d, ATA_READ_DMA_EXT, lba, count, bytes);
203 if (rc == DISK_OK)
204 memcpy(buf, d->bounce, bytes);
205 return rc;
206}
207
208/* Inside one open write range, as a whole. */
209static int allowed(const struct ahci_disk *d, uint64_t lba, uint32_t count)
210{
211 uint64_t last = lba + count - 1u;
212 if (!count || last < lba)
213 return 0;
214 for (unsigned i = 0; i < d->nallow; i++)
215 if (lba >= d->allow_first[i] && last <= d->allow_last[i])
216 return 1;
217 return 0;
218}
219
220static int ahci_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf)
221{
222 struct ahci_disk *d = ctx;
223 if (!allowed(d, lba, count)) {
224 ck_printf("ahci_write: ctrl=%u disk=%u lba=%llu count=%u refused: outside the open write ranges\n", d->ctrl,
225 d->index, (unsigned long long)lba, count);
226 return DISK_ERANGE;
227 }
228 uint32_t bytes = count * d->dev.block_size;
229 memcpy(d->bounce, buf, bytes);
230 return issue(d, ATA_WRITE_DMA_EXT, lba, count, bytes);
231}
232
233static int ahci_flush(void *ctx)
234{
235 struct ahci_disk *d = ctx;
236 if (!d->nallow)
237 return DISK_ESTATE; /* nothing could have been written */
238 return issue(d, ATA_FLUSH_CACHE_EXT, 0, 0, 0);
239}
240
241static void id_string(const uint16_t *id, unsigned w0, unsigned words, char *out)
242{
243 unsigned n = 0;
244 for (unsigned i = 0; i < words; i++) {
245 char a = (char)(id[w0 + i] >> 8), b = (char)id[w0 + i];
246 out[n++] = a >= 0x20 && a < 0x7f ? a : '?';
247 out[n++] = b >= 0x20 && b < 0x7f ? b : '?';
248 }
249 while (n && out[n - 1] == ' ')
250 n--;
251 out[n] = 0;
252}
253
254static void port_stop(volatile uint8_t *p)
255{
256 mw(p, P_CMD, mr(p, P_CMD) & ~CMD_ST);
257 wait_reg(p, P_CMD, CMD_CR, 0, 500);
258 mw(p, P_CMD, mr(p, P_CMD) & ~CMD_FRE);
259 wait_reg(p, P_CMD, CMD_FR, 0, 500);
260}
261
262static void port_bringup(unsigned ctrl, volatile uint8_t *hba, unsigned pn, struct ck_frames *fr, int low4g)
263{
264 volatile uint8_t *p = hba + 0x100 + 0x80 * pn;
265 uint32_t ssts = mr(p, P_SSTS), sig = mr(p, P_SIG);
266 unsigned det = ssts & 0xf, spd = (ssts >> 4) & 0xf;
267 if (det != 3) {
268 ck_printf("ahci_port: ctrl=%u port=%u link=none det=%u\n", ctrl, pn, det);
269 return;
270 }
271 if (sig != SIG_ATA) {
272 ck_printf("ahci_port: ctrl=%u port=%u link=gen%u sig=0x%08x not an ATA disk, left alone\n", ctrl, pn, spd,
273 sig);
274 return;
275 }
276 if (ndisks == AHCI_MAX_DISKS) {
277 ck_printf("ahci_port: ctrl=%u port=%u skipped: more than %u disks\n", ctrl, pn, AHCI_MAX_DISKS);
278 return;
279 }
280 struct ahci_disk *d = &disks[ndisks];
281 uint8_t *page, *bounce;
282 if (alloc_low(fr, 1, low4g, &page) || alloc_low(fr, BOUNCE_BYTES / CK_PAGE, low4g, &bounce)) {
283 ck_printf("ahci_port: ctrl=%u port=%u no DMA frames\n", ctrl, pn);
284 return;
285 }
286 d->port = p;
287 d->cl = page; /* 1 KiB command list */
288 d->fis = page + 1024; /* 256 B received FIS */
289 d->ct = page + 2048; /* command table: 128 B header + one PRD */
290 d->bounce = bounce;
291 d->tsc_hz = g_tsc_hz;
292 d->ctrl = ctrl;
293 d->index = ndisks;
294
295 port_stop(p);
296 if (mr(p, P_CMD) & (CMD_CR | CMD_FR)) {
297 ck_printf("ahci_port: ctrl=%u port=%u engine did not stop cmd=0x%x\n", ctrl, pn, mr(p, P_CMD));
298 return;
299 }
300 mw(p, P_CLB, (uint32_t)pa_of(d->cl));
301 mw(p, P_CLBU, (uint32_t)(pa_of(d->cl) >> 32));
302 mw(p, P_FB, (uint32_t)pa_of(d->fis));
303 mw(p, P_FBU, (uint32_t)(pa_of(d->fis) >> 32));
304 mw(p, P_IE, 0);
305 mw(p, P_SERR, 0xffffffffu);
306 mw(p, P_IS, 0xffffffffu);
307 mw(p, P_CMD, mr(p, P_CMD) | CMD_FRE);
308 if (wait_reg(p, P_TFD, TFD_BSY | TFD_DRQ, 0, 1000)) {
309 ck_printf("ahci_port: ctrl=%u port=%u device busy tfd=0x%x\n", ctrl, pn, mr(p, P_TFD));
310 return;
311 }
312 mw(p, P_CMD, mr(p, P_CMD) | CMD_ST);
313
314 if (issue(d, ATA_IDENTIFY, 0, 0, 512) != DISK_OK) {
315 ck_printf("ahci_port: ctrl=%u port=%u IDENTIFY failed tfd=0x%x is=0x%x\n", ctrl, pn, mr(p, P_TFD),
316 mr(p, P_IS));
317 port_stop(p);
318 return;
319 }
320 const uint16_t *id = (const uint16_t *)d->bounce;
321 char model[41];
322 id_string(id, 27, 20, model); /* words 10..19 (serial number) are never read */
323 memcpy(d->model, model, sizeof d->model);
324 d->nallow = 0;
325 uint64_t sectors = 0;
326 int lba48 = (id[83] & (1u << 10)) != 0;
327 if (lba48)
328 for (int i = 3; i >= 0; i--)
329 sectors = sectors << 16 | id[100 + i];
330 uint32_t bs = 512;
331 if ((id[106] & 0xc000) == 0x4000 && (id[106] & (1u << 12)))
332 bs = ((uint32_t)id[118] << 16 | id[117]) * 2u;
333 if (!lba48 || !sectors || (bs != 512 && bs != 4096)) {
334 ck_printf("ahci_port: ctrl=%u port=%u model=\"%s\" unsupported lba48=%d sectors=%llu block=%u\n", ctrl, pn,
335 model, lba48, (unsigned long long)sectors, bs);
336 port_stop(p);
337 return;
338 }
339 d->dev.ctx = d;
340 d->dev.block_size = bs;
341 d->dev.block_count = sectors;
342 d->dev.read = ahci_read;
343 d->dev.write = ahci_write;
344 d->dev.flush = ahci_flush;
345 d->dev.max_blocks_per_io = BOUNCE_BYTES / bs;
346 ndisks++;
347 ck_printf("ahci_disk: ctrl=%u disk=%u port=%u link=gen%u model=\"%s\" blocks=%llu block=%u mib=%llu "
348 "access=read-only\n", ctrl, d->index, pn, spd, model, (unsigned long long)sectors, bs,
349 (unsigned long long)(sectors * bs >> 20));
350 ck_x86_disk_report("ahci", d->ctrl, d->index, &d->dev);
351}
352
353void ck_x86_ahci_start(unsigned ctrl, uint64_t abar, struct ck_frames *fr, uint64_t tsc_hz)
354{
355 g_tsc_hz = tsc_hz;
356 volatile uint8_t *hba = (volatile uint8_t *)(uintptr_t)abar;
357 uint32_t cap = mr(hba, HBA_CAP);
358 /* Taking the controller from the firmware (AHCI 1.3 section 10.6): ask,
359 * then wait up to 2 s for the firmware to let go. */
360 if (mr(hba, HBA_CAP2) & 1u) {
361 mw(hba, HBA_BOHC, mr(hba, HBA_BOHC) | BOHC_OOS);
362 wait_reg(hba, HBA_BOHC, BOHC_BOS, 0, 2000);
363 }
364 mw(hba, HBA_GHC, (mr(hba, HBA_GHC) | GHC_AE) & ~GHC_IE);
365 uint32_t pi = mr(hba, HBA_PI), vs = mr(hba, HBA_VS);
366 ck_printf("ahci: ctrl=%u abar=0x%llx version=%x.%x%x ports=%u implemented=0x%x s64a=%u polled=yes\n", ctrl,
367 (unsigned long long)abar, vs >> 16, (vs >> 8) & 0xff, vs & 0xff, (cap & 0x1f) + 1, pi,
368 (cap & CAP_S64A) ? 1u : 0u);
369 for (unsigned pn = 0; pn < 32; pn++)
370 if (pi & (1u << pn))
371 port_bringup(ctrl, hba, pn, fr, !(cap & CAP_S64A));
372}
373
374unsigned ck_x86_ahci_disks(void) { return ndisks; }
375
376const disk_dev *ck_x86_ahci_dev(unsigned index, const char **model)
377{
378 if (index >= ndisks)
379 return 0;
380 if (model)
381 *model = disks[index].model;
382 return &disks[index].dev;
383}
384
385int ck_x86_ahci_allow(unsigned index, uint64_t first, uint64_t last)
386{
387 if (index >= ndisks)
388 return -1;
389 struct ahci_disk *d = &disks[index];
390 if (d->nallow == AHCI_MAX_ALLOW || first == 0 || first > last || last >= d->dev.block_count)
391 return -1; /* LBA 0 (the protective MBR) is never opened */
392 d->allow_first[d->nallow] = first;
393 d->allow_last[d->nallow] = last;
394 d->nallow++;
395 ck_printf("ahci_disk: ctrl=%u disk=%u writes allowed lba=%llu..%llu\n", d->ctrl, index,
396 (unsigned long long)first, (unsigned long long)last);
397 return 0;
398}
399
400void ck_x86_ahci_allow_none(unsigned index)
401{
402 if (index < ndisks && disks[index].nallow) {
403 disks[index].nallow = 0;
404 ck_printf("ahci_disk: ctrl=%u disk=%u write ranges closed\n", disks[index].ctrl, index);
405 }
406}
407
408#ifdef CK_X86_SWAP_TEST_DISK
409/* Read LBAs directly for the swap inbox (QEMU/lab test only, not production). */
410int ck_x86_ahci_read_lba(unsigned ctrl, unsigned disk_idx, uint64_t lba,
411 unsigned count, uint8_t *buf)
412{
413 for (unsigned i = 0; i < ndisks; i++) {
414 if (disks[i].ctrl == ctrl && i == disk_idx) {
415 return disk_read(&disks[i].dev, lba, count, buf);
416 }
417 }
418 return -1;
419}
420#endif /* CK_X86_SWAP_TEST_DISK */