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.h # 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.h` 113 lines ยท 5023 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.h -- software-only DMA confinement for the Raspberry Pi 5 18 * (ADR 0020). One bounce window, an overflow-safe validator, and descriptor 19 * ring helpers that only ever emit addresses inside the window. 20 * 21 * LABEL: software-only DMA confinement (Pi 5, weaker than the Spark). 22 * This is an explicit Pi-only exception to ADR 0012 rule 5. The Spark's DMA 23 * is fenced by SMMU stream windows (core/smmu.c); this file is not part of 24 * that and is not linked into the Spark, QEMU or any default image. 25 * 26 * Pure C, freestanding (stdint only), no allocation, no I/O. The GEM 27 * descriptor bit layout is NOT modelled here (UNVERIFIED, driver cut): a 28 * descriptor is just (addr, len, flags) from the driver's own record. */ 29 #ifndef AIENOS_CK_DMA_WINDOW_H 30 #define AIENOS_CK_DMA_WINDOW_H 31 #include 32 33 #define CK_DMAW_MAX_BYTES (2u * 1024u * 1024u) /* ADR 0020 rule 1: <= 2 MiB */ 34 #define CK_DMAW_MIN_BYTES 4096u 35 #define CK_DMAW_MAX_FRAME 1536u /* ADR 0020 rule 7: no jumbo frames */ 36 #define CK_DMAW_BUF_ALIGN 8u /* driver-chosen, not taken from the GEM manual */ 37 #define CK_DMAW_RING_MAX 256u 38 39 enum { 40 CK_DMAW_OK = 0, 41 CK_DMAW_E_LATCHED = 1, /* the NIC is halted until reboot */ 42 CK_DMAW_E_RANGE = 2, /* not fully inside the window */ 43 CK_DMAW_E_ZERO = 3, /* zero length */ 44 CK_DMAW_E_ALIGN = 4, 45 CK_DMAW_E_PARAM = 5, /* bad init or call arguments */ 46 CK_DMAW_E_NOSPACE = 6, /* window or ring full (does not latch) */ 47 CK_DMAW_E_STALE = 7, /* slot not in flight: reuse or double completion */ 48 CK_DMAW_E_BIG = 8, /* frame larger than CK_DMAW_MAX_FRAME */ 49 CK_DMAW_E_INDEX = 9 /* device-supplied slot index out of range */ 50 }; 51 52 struct ck_dmaw { 53 uint64_t base; /* bus address of the window */ 54 uint64_t size; /* power of two, 4 KiB..2 MiB, base aligned to size */ 55 uint64_t next; /* bump offset */ 56 int latched; 57 int last_err; 58 }; 59 60 /* Take a power-of-two, size-aligned block (bus address `bus_base`, CPU 61 * mapping `mem`) and zero it. The block is used for nothing else. */ 62 int ck_dmaw_init(struct ck_dmaw *w, void *mem, uint64_t bus_base, uint64_t size); 63 64 /* Is [addr, addr+len) fully inside the window? Never computes addr+len. 65 * Any violation latches the window; a latched window refuses everything. */ 66 int ck_dmaw_validate(struct ck_dmaw *w, uint64_t addr, uint64_t len); 67 int ck_dmaw_validate_aligned(struct ck_dmaw *w, uint64_t addr, uint64_t len, uint64_t align); 68 69 /* Carve `len` bytes at `align` (power of two) out of the window; returns the 70 * bus address through *addr. Running out of space is E_NOSPACE (no latch). */ 71 int ck_dmaw_alloc(struct ck_dmaw *w, uint64_t len, uint64_t align, uint64_t *addr); 72 73 /* Operator-visible halt: set when anything went wrong; cleared only by init. */ 74 int ck_dmaw_halted(const struct ck_dmaw *w); 75 76 /* The descriptor handed to the device. Built only from the driver's record. */ 77 struct ck_dmaw_desc { 78 uint64_t addr; 79 uint32_t len; 80 uint32_t flags; 81 }; 82 83 enum { CK_DMAW_SLOT_FREE = 0, CK_DMAW_SLOT_INFLIGHT = 1 }; 84 85 struct ck_dmaw_slot { /* the driver's shadow of one descriptor */ 86 struct ck_dmaw_desc d; 87 uint32_t state; 88 }; 89 90 struct ck_dmaw_ring { 91 struct ck_dmaw_slot *slots; /* caller storage, kernel memory, NOT in the window */ 92 uint32_t n; /* power of two, <= CK_DMAW_RING_MAX */ 93 uint32_t head; /* next slot to post */ 94 uint32_t inflight; 95 }; 96 97 int ck_dmaw_ring_init(struct ck_dmaw_ring *r, struct ck_dmaw_slot *slots, uint32_t n); 98 99 /* Post one buffer: validated (range, alignment, frame cap) against the window, 100 * recorded in the shadow. *idx is the slot used. A full ring is E_NOSPACE. */ 101 int ck_dmaw_ring_post(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint64_t addr, uint32_t len, 102 uint32_t flags, uint32_t *idx); 103 104 /* Produce the descriptor for slot idx FROM THE SHADOW ONLY (never from ring 105 * memory the device can write), re-validated. Slot not in flight: E_STALE. */ 106 int ck_dmaw_ring_emit(const struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, 107 struct ck_dmaw_desc *out); 108 109 /* Device says slot idx finished with `got` bytes. Index out of range, slot not 110 * in flight, or got > posted length: latch. Otherwise the slot is freed. */ 111 int ck_dmaw_ring_complete(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, uint32_t got); 112 113 #endif