Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/dma_window.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / dma_window.c` 213 lines ยท 6889 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 /* dma_window.c -- see dma_window.h and ADR 0020. 18 * software-only DMA confinement (Pi 5, weaker than the Spark). */ 19 #include "dma_window.h" 20 21 /* TEST-only mutants (make dma-window-mutants): each removes one check and 22 * must make test_dma_window FAIL. Host builds only; never part of an image. */ 23 #if defined(CK_DMAW_MUTANT_NO_END) || defined(CK_DMAW_MUTANT_WRAP) || \ 24 defined(CK_DMAW_MUTANT_NO_ZERO) || defined(CK_DMAW_MUTANT_NO_LATCH) || defined(CK_DMAW_MUTANT_NO_STALE) || \ 25 defined(CK_DMAW_MUTANT_NO_ALIGN) || defined(CK_DMAW_MUTANT_NO_FRAMECAP) || defined(CK_DMAW_MUTANT_NO_GOT) || \ 26 defined(CK_DMAW_MUTANT_NO_INDEX) 27 #if !__STDC_HOSTED__ 28 #error "CK_DMAW_MUTANT_* are host test mutants only" 29 #endif 30 #ifdef CK_HARDWARE_STAGING 31 #error "CK_DMAW_MUTANT_* cannot be combined with CK_HARDWARE_STAGING" 32 #endif 33 #endif 34 35 static int pow2(uint64_t n) { return n != 0 && (n & (n - 1)) == 0; } 36 37 int ck_dmaw_init(struct ck_dmaw *w, void *mem, uint64_t bus_base, uint64_t size) 38 { 39 if (!w) 40 return CK_DMAW_E_PARAM; 41 w->latched = 1; /* stay halted unless init fully succeeds */ 42 w->last_err = CK_DMAW_E_PARAM; 43 w->base = 0; 44 w->size = 0; 45 w->next = 0; 46 if (!mem || !pow2(size) || size < CK_DMAW_MIN_BYTES || size > CK_DMAW_MAX_BYTES) 47 return CK_DMAW_E_PARAM; 48 if ((bus_base & (size - 1)) != 0 || bus_base > UINT64_MAX - size) 49 return CK_DMAW_E_PARAM; /* base must be aligned to size and not wrap */ 50 volatile uint8_t *p = (volatile uint8_t *)mem; 51 for (uint64_t i = 0; i < size; i++) 52 p[i] = 0; 53 w->base = bus_base; 54 w->size = size; 55 w->latched = 0; 56 w->last_err = CK_DMAW_OK; 57 return CK_DMAW_OK; 58 } 59 60 static int latch(struct ck_dmaw *w, int err) 61 { 62 #ifndef CK_DMAW_MUTANT_NO_LATCH 63 w->latched = 1; 64 #endif 65 w->last_err = err; 66 return err; 67 } 68 69 int ck_dmaw_halted(const struct ck_dmaw *w) { return !w || w->latched; } 70 71 int ck_dmaw_validate_aligned(struct ck_dmaw *w, uint64_t addr, uint64_t len, uint64_t align) 72 { 73 (void)align; /* unused only in the NO_ALIGN mutant */ 74 if (!w) 75 return CK_DMAW_E_PARAM; 76 if (w->latched) 77 return CK_DMAW_E_LATCHED; 78 #ifndef CK_DMAW_MUTANT_NO_ZERO 79 if (len == 0) 80 return latch(w, CK_DMAW_E_ZERO); 81 #endif 82 #ifndef CK_DMAW_MUTANT_NO_ALIGN 83 if (!pow2(align) || (addr & (align - 1)) != 0) 84 return latch(w, CK_DMAW_E_ALIGN); 85 #endif 86 #ifdef CK_DMAW_MUTANT_WRAP 87 /* mutant: the naive end computation that wraps */ 88 if (addr < w->base || addr + len > w->base + w->size) 89 return latch(w, CK_DMAW_E_RANGE); 90 #else 91 int bad = 0; 92 bad |= addr < w->base; 93 bad |= len > w->size; /* also guarantees size - len cannot underflow */ 94 #ifndef CK_DMAW_MUTANT_NO_END 95 if (!bad) 96 bad |= (addr - w->base) > (w->size - len); 97 #endif 98 if (bad) 99 return latch(w, CK_DMAW_E_RANGE); 100 #endif 101 return CK_DMAW_OK; 102 } 103 104 int ck_dmaw_validate(struct ck_dmaw *w, uint64_t addr, uint64_t len) 105 { 106 return ck_dmaw_validate_aligned(w, addr, len, 1); 107 } 108 109 int ck_dmaw_alloc(struct ck_dmaw *w, uint64_t len, uint64_t align, uint64_t *addr) 110 { 111 if (!w || !addr || len == 0 || !pow2(align)) 112 return CK_DMAW_E_PARAM; 113 if (w->latched) 114 return CK_DMAW_E_LATCHED; 115 uint64_t off = (w->next + (align - 1)) & ~(align - 1); 116 if (off < w->next || off > w->size || len > w->size - off) 117 return CK_DMAW_E_NOSPACE; 118 w->next = off + len; 119 *addr = w->base + off; 120 return CK_DMAW_OK; 121 } 122 123 int ck_dmaw_ring_init(struct ck_dmaw_ring *r, struct ck_dmaw_slot *slots, uint32_t n) 124 { 125 if (!r || !slots || n == 0 || n > CK_DMAW_RING_MAX || (n & (n - 1)) != 0) 126 return CK_DMAW_E_PARAM; 127 r->slots = slots; 128 r->n = n; 129 r->head = 0; 130 r->inflight = 0; 131 for (uint32_t i = 0; i < n; i++) { 132 slots[i].d.addr = 0; 133 slots[i].d.len = 0; 134 slots[i].d.flags = 0; 135 slots[i].state = CK_DMAW_SLOT_FREE; 136 } 137 return CK_DMAW_OK; 138 } 139 140 int ck_dmaw_ring_post(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint64_t addr, uint32_t len, 141 uint32_t flags, uint32_t *idx) 142 { 143 if (!r || !w || !idx) 144 return CK_DMAW_E_PARAM; 145 if (w->latched) 146 return CK_DMAW_E_LATCHED; 147 #ifndef CK_DMAW_MUTANT_NO_FRAMECAP 148 if (len > CK_DMAW_MAX_FRAME) 149 return latch(w, CK_DMAW_E_BIG); 150 #endif 151 int rc = ck_dmaw_validate_aligned(w, addr, len, CK_DMAW_BUF_ALIGN); 152 if (rc != CK_DMAW_OK) 153 return rc; 154 struct ck_dmaw_slot *s = &r->slots[r->head]; 155 if (s->state != CK_DMAW_SLOT_FREE) 156 return CK_DMAW_E_NOSPACE; 157 s->d.addr = addr; 158 s->d.len = len; 159 s->d.flags = flags; 160 s->state = CK_DMAW_SLOT_INFLIGHT; 161 *idx = r->head; 162 r->head = (r->head + 1) & (r->n - 1); 163 r->inflight++; 164 return CK_DMAW_OK; 165 } 166 167 int ck_dmaw_ring_emit(const struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, 168 struct ck_dmaw_desc *out) 169 { 170 if (!r || !w || !out) 171 return CK_DMAW_E_PARAM; 172 if (w->latched) 173 return CK_DMAW_E_LATCHED; 174 if (idx >= r->n) 175 return latch(w, CK_DMAW_E_INDEX); 176 const struct ck_dmaw_slot *s = &r->slots[idx]; 177 #ifndef CK_DMAW_MUTANT_NO_STALE 178 if (s->state != CK_DMAW_SLOT_INFLIGHT) 179 return latch(w, CK_DMAW_E_STALE); 180 #endif 181 /* defence in depth: the shadow itself is checked again before it leaves */ 182 int rc = ck_dmaw_validate_aligned(w, s->d.addr, s->d.len, CK_DMAW_BUF_ALIGN); 183 if (rc != CK_DMAW_OK) 184 return rc; 185 *out = s->d; 186 return CK_DMAW_OK; 187 } 188 189 int ck_dmaw_ring_complete(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, uint32_t got) 190 { 191 (void)got; /* unused only in the NO_GOT mutant */ 192 if (!r || !w) 193 return CK_DMAW_E_PARAM; 194 if (w->latched) 195 return CK_DMAW_E_LATCHED; 196 #ifndef CK_DMAW_MUTANT_NO_INDEX 197 if (idx >= r->n) 198 return latch(w, CK_DMAW_E_INDEX); 199 #endif 200 struct ck_dmaw_slot *s = &r->slots[idx & (r->n - 1)]; 201 #ifndef CK_DMAW_MUTANT_NO_STALE 202 if (s->state != CK_DMAW_SLOT_INFLIGHT) 203 return latch(w, CK_DMAW_E_STALE); 204 #endif 205 #ifndef CK_DMAW_MUTANT_NO_GOT 206 if (got > s->d.len) 207 return latch(w, CK_DMAW_E_RANGE); 208 #endif 209 s->state = CK_DMAW_SLOT_FREE; 210 if (r->inflight) 211 r->inflight--; 212 return CK_DMAW_OK; 213 }