aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / smmu.h
| 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 | /* smmu.h -- Arm SMMUv3 programming for the AIENOS C kernel, ported from the |
| 18 | * Rust reference crates/aienos-kernel/src/smmu.rs (linear stream table, |
| 19 | * stage-1 translation only, one linear context descriptor per stream). The |
| 20 | * two-level stream table (FMT = 2-level) is C only, from Arm IHI 0070 H.a. |
| 21 | * |
| 22 | * Pure logic over a register-access callback and table memory, so the host |
| 23 | * tests drive it against a model SMMU. The kernel glue (core/smmu_svc.c) |
| 24 | * supplies MMIO accessors and Normal Non-cacheable table memory from the |
| 25 | * DMA pool (ck_dma_alloc), so no cache maintenance is needed; the barrier |
| 26 | * callback orders table writes before the register write that publishes |
| 27 | * them. Addresses are identity mapped: a table pointer is its bus address. |
| 28 | * |
| 29 | * Fail-closed rules (same as the Rust reference): |
| 30 | * - GBPA.ABORT is set first and never cleared: a disabled SMMU aborts all |
| 31 | * DMA. There is no bypass STE constructor. |
| 32 | * - Every stream starts as an abort STE; only an explicit install makes a |
| 33 | * stream translate, and only through a stage-1 table. |
| 34 | * - Any error after GBPA.ABORT latched writes CR0 = 0 (SMMU off, so ABORT |
| 35 | * governs every stream). Every poll is bounded by `spins`. |
| 36 | * QEMU is not hardware: nothing built on this is physically qualified. */ |
| 37 | #ifndef AIENOS_CK_SMMU_H |
| 38 | #define AIENOS_CK_SMMU_H |
| 39 | #include <stdint.h> |
| 40 | |
| 41 | #define CK_SMMU_STE_BYTES 64u |
| 42 | #define CK_SMMU_CD_BYTES 64u |
| 43 | #define CK_SMMU_CMD_BYTES 16u |
| 44 | #define CK_SMMU_EVT_BYTES 32u |
| 45 | #define CK_SMMU_MMIO_BYTES 0x20000u /* register pages 0 and 1 */ |
| 46 | |
| 47 | /* Register offsets (SMMUv3 architecture). */ |
| 48 | #define CK_SMMU_IDR0 0x00u |
| 49 | #define CK_SMMU_IDR1 0x04u |
| 50 | #define CK_SMMU_CR0 0x20u |
| 51 | #define CK_SMMU_CR0ACK 0x24u |
| 52 | #define CK_SMMU_CR1 0x28u |
| 53 | #define CK_SMMU_CR2 0x2cu |
| 54 | #define CK_SMMU_GBPA 0x44u |
| 55 | #define CK_SMMU_STRTAB_BASE 0x80u |
| 56 | #define CK_SMMU_STRTAB_BASE_CFG 0x88u |
| 57 | #define CK_SMMU_CMDQ_BASE 0x90u |
| 58 | #define CK_SMMU_CMDQ_PROD 0x98u |
| 59 | #define CK_SMMU_CMDQ_CONS 0x9cu |
| 60 | #define CK_SMMU_EVENTQ_BASE 0xa0u |
| 61 | #define CK_SMMU_EVENTQ_PROD 0x100a8u /* page 1 */ |
| 62 | #define CK_SMMU_EVENTQ_CONS 0x100acu /* page 1 */ |
| 63 | |
| 64 | #define CK_SMMU_IDR0_S1P (1u << 1) |
| 65 | #define CK_SMMU_GBPA_ABORT (1u << 20) |
| 66 | #define CK_SMMU_GBPA_UPDATE (1u << 31) |
| 67 | #define CK_SMMU_CR0_SMMUEN (1u << 0) |
| 68 | #define CK_SMMU_CR0_EVENTQEN (1u << 2) |
| 69 | #define CK_SMMU_CR0_CMDQEN (1u << 3) |
| 70 | |
| 71 | #define CK_SMMU_CMD_CFGI_STE 0x03u |
| 72 | #define CK_SMMU_CMD_CFGI_ALL 0x04u |
| 73 | #define CK_SMMU_CMD_CFGI_CD_ALL 0x06u |
| 74 | #define CK_SMMU_CMD_TLBI_NSNH_ALL 0x30u |
| 75 | #define CK_SMMU_CMD_SYNC 0x46u |
| 76 | |
| 77 | /* Two-level stream table (Arm IHI 0070 H.a: SMMU_STRTAB_BASE_CFG 6.3.25, |
| 78 | * L1STD 5.1, SMMU_IDR0.ST_LEVEL). FMT = 0b01 in bits 17:16; SPLIT in bits |
| 79 | * 10:6 (6 = "4KB leaf tables"); LOG2SIZE in bits 5:0. The L1 table holds |
| 80 | * 2^(LOG2SIZE - SPLIT) 8-byte L1STDs indexed by StreamID[LOG2SIZE-1:SPLIT]; |
| 81 | * each L2 table is indexed by StreamID[SPLIT-1:0]. L1STD: Span bits 4:0 |
| 82 | * (0 = invalid: every StreamID of that span is terminated; 1..11 = 2^(Span-1) |
| 83 | * STEs; at most SPLIT + 1), L2Ptr bits 55:6, the L2 table aligned to its |
| 84 | * size. Here SPLIT is fixed at 6: one 4 KiB page of 64 STEs per span, Span |
| 85 | * = 7. LOG2SIZE is bounded to CK_SMMU_L2_MIN_BITS..CK_SMMU_L2_MAX_BITS so |
| 86 | * the L1 table is at most 128 KiB. */ |
| 87 | #define CK_SMMU_IDR0_ST_LEVEL_SHIFT 27u |
| 88 | #define CK_SMMU_IDR0_ST_LEVEL_MASK 0x3u |
| 89 | #define CK_SMMU_IDR1_SIDSIZE_MASK 0x3fu |
| 90 | #define CK_SMMU_STRTAB_FMT_2LVL (1u << 16) |
| 91 | #define CK_SMMU_SPLIT 6u |
| 92 | #define CK_SMMU_L2_STES (1u << CK_SMMU_SPLIT) /* 64 */ |
| 93 | #define CK_SMMU_L2_BYTES (CK_SMMU_L2_STES * CK_SMMU_STE_BYTES) /* 4096 */ |
| 94 | #define CK_SMMU_L1STD_SPAN_MASK 0x1full |
| 95 | #define CK_SMMU_L1STD_L2PTR_MASK 0x00ffffffffffffc0ull /* bits 55:6 */ |
| 96 | #define CK_SMMU_L1STD_SPAN (CK_SMMU_SPLIT + 1u) /* 2^(7-1) = 64 STEs */ |
| 97 | #define CK_SMMU_L2_MIN_BITS (CK_SMMU_SPLIT + 1u) |
| 98 | #define CK_SMMU_L2_MAX_BITS 20u |
| 99 | |
| 100 | /* Event record types (word 0 bits 7:0). */ |
| 101 | #define CK_SMMU_EVT_F_TRANSLATION 0x10u |
| 102 | #define CK_SMMU_EVT_F_ADDR_SIZE 0x11u |
| 103 | #define CK_SMMU_EVT_F_ACCESS 0x12u |
| 104 | #define CK_SMMU_EVT_F_PERMISSION 0x13u |
| 105 | #define CK_SMMU_EVT_C_BAD_STE 0x04u |
| 106 | #define CK_SMMU_EVT_F_STREAM_DISABLED 0x06u |
| 107 | |
| 108 | #define CK_SMMU_DEFAULT_SPINS 1000000u |
| 109 | |
| 110 | /* Errors (negative). */ |
| 111 | #define CK_SMMU_ETIMEOUT (-10) /* a bounded poll ran out */ |
| 112 | #define CK_SMMU_ECMDQ (-11) /* CMDQ_CONS.ERR set */ |
| 113 | #define CK_SMMU_EUNSUP (-12) /* IDR0/IDR1 cannot run this geometry */ |
| 114 | #define CK_SMMU_EINVAL (-13) /* bad stream id, geometry or alignment */ |
| 115 | #define CK_SMMU_ENOABORT (-14) /* GBPA.ABORT did not latch */ |
| 116 | |
| 117 | struct ck_smmu_regs { |
| 118 | void *ctx; |
| 119 | uint32_t (*rd32)(void *ctx, uint32_t off); |
| 120 | void (*wr32)(void *ctx, uint32_t off, uint32_t v); |
| 121 | void (*wr64)(void *ctx, uint32_t off, uint64_t v); |
| 122 | void (*barrier)(void *ctx); /* table writes visible before the next register write */ |
| 123 | }; |
| 124 | |
| 125 | /* Stream table + queues. Entry counts are powers of two; every base is |
| 126 | * aligned to its own size (min 32 bytes). Pointers are bus addresses. |
| 127 | * Linear (l1 == NULL): strtab holds ste_n STEs. |
| 128 | * Two-level (l1 != NULL): strtab and ste_n are unused; l1 holds |
| 129 | * 2^(sid_bits - CK_SMMU_SPLIT) L1STDs, aligned to the L1 size (min 64 |
| 130 | * bytes). Every L1STD starts invalid (Span 0); the first install into a span |
| 131 | * takes one CK_SMMU_L2_BYTES page from l2_alloc (aligned to its size), |
| 132 | * fills it with abort STEs and only then publishes the L1STD. */ |
| 133 | struct ck_smmu_tables { |
| 134 | uint64_t *strtab; /* ste_n x 8 words */ |
| 135 | uint32_t ste_n; |
| 136 | uint64_t *cmdq; /* cmd_n x 2 words */ |
| 137 | uint32_t cmd_n; |
| 138 | uint64_t *evtq; /* evt_n x 4 words */ |
| 139 | uint32_t evt_n; |
| 140 | uint64_t *l1; /* two-level: 2^(sid_bits - CK_SMMU_SPLIT) L1STDs */ |
| 141 | uint32_t sid_bits; |
| 142 | int (*l2_alloc)(void *ctx, uint64_t **page); /* 0 ok; one zeroed or dirty page */ |
| 143 | void *l2_ctx; |
| 144 | }; |
| 145 | |
| 146 | struct ck_smmu_event { |
| 147 | uint8_t type; |
| 148 | uint32_t sid; |
| 149 | uint64_t addr; /* word 2: input address for translation-class faults */ |
| 150 | uint64_t raw[4]; |
| 151 | }; |
| 152 | |
| 153 | /* 0 if the geometry and alignment are valid, else CK_SMMU_EINVAL. */ |
| 154 | int ck_smmu_tables_check(const struct ck_smmu_tables *t); |
| 155 | |
| 156 | /* 1 if `sid` can be given an STE in these tables (linear: sid < ste_n; |
| 157 | * two-level: sid < 2^sid_bits), else 0. */ |
| 158 | int ck_smmu_sid_in_range(const struct ck_smmu_tables *t, uint32_t sid); |
| 159 | /* Two-level only: the L1STD covering `sid`, or 0 when out of range or |
| 160 | * linear. */ |
| 161 | uint64_t ck_smmu_l1std(const struct ck_smmu_tables *t, uint32_t sid); |
| 162 | |
| 163 | /* Descriptor builders. */ |
| 164 | void ck_smmu_ste_abort(uint64_t ste[8]); |
| 165 | void ck_smmu_ste_stage1(uint64_t ste[8], uint64_t cd_addr); |
| 166 | /* CD for a stage-1 walk: 4 KiB granule, TTBR0 only (EPD1), 48-bit IPS, |
| 167 | * walks Non-cacheable (the kernel's tables live in Non-cacheable memory), |
| 168 | * faults recorded (R) and aborted (A). CK_SMMU_EINVAL if t0sz > 39 or ttb0 |
| 169 | * is not 4 KiB aligned. */ |
| 170 | int ck_smmu_cd_stage1(uint64_t cd[8], uint64_t ttb0, uint16_t asid, uint64_t mair, uint8_t t0sz); |
| 171 | void ck_smmu_cmd(uint64_t w[2], uint8_t opcode, uint32_t sid, int leaf); |
| 172 | |
| 173 | /* Queue index encoding: index in bits n-1:0, wrap flag in bit n. */ |
| 174 | uint32_t ck_smmu_q_encode(uint32_t index, int wrap, uint32_t entries); |
| 175 | |
| 176 | /* Append commands and wait until the SMMU consumed them all. */ |
| 177 | int ck_smmu_submit(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, const uint64_t (*cmds)[2], |
| 178 | uint32_t n, uint32_t spins); |
| 179 | |
| 180 | /* Bring-up with every stream in abort: |
| 181 | * 1. GBPA.ABORT set and latched (else return without touching CR0); |
| 182 | * 2. IDR0.S1P, IDR1 SIDSIZE / CMDQS / EVENTQS checked against the tables |
| 183 | * (two-level also needs IDR0.ST_LEVEL == 0b01); |
| 184 | * 3. all STEs abort (two-level: every L1STD Span 0), queues zeroed, barrier; |
| 185 | * 4. CR0 = 0 (ack), queues + stream table programmed, queues enabled (ack), |
| 186 | * CFGI_ALL + TLBI_NSNH_ALL + SYNC, SMMUEN (ack). |
| 187 | * Any error after step 1 writes CR0 = 0 and returns it. */ |
| 188 | int ck_smmu_enable(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t spins); |
| 189 | |
| 190 | /* Point stream `sid` at `ste`: abort + invalidate first, write words 1..7, |
| 191 | * (two-level: a span with no L2 table first gets one, see ck_smmu_tables), |
| 192 | * barrier, then word 0, then CFGI_STE + TLBI_NSNH_ALL + SYNC. */ |
| 193 | int ck_smmu_install_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, |
| 194 | const uint64_t ste[8], uint32_t spins); |
| 195 | /* Return `sid` to abort and invalidate it. On 0 no further DMA translates. */ |
| 196 | int ck_smmu_abort_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, |
| 197 | uint32_t spins); |
| 198 | |
| 199 | /* Consume up to n event records, oldest first; acknowledges an overflow. |
| 200 | * Returns the number of records written to out. */ |
| 201 | int ck_smmu_read_events(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, |
| 202 | struct ck_smmu_event *out, int n, int *overflowed); |
| 203 | |
| 204 | /* Probe observer: visit every SID, at most one queue capacity per call. |
| 205 | * A busy producer cannot keep the final report from running. pending marks an |
| 206 | * incomplete drain; overflow marks loss even if no records remain. */ |
| 207 | struct ck_smmu_observation { |
| 208 | uint32_t events; |
| 209 | int overflow, pending; |
| 210 | }; |
| 211 | struct ck_smmu_observation ck_smmu_observe_events( |
| 212 | const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, |
| 213 | void (*visit)(void *ctx, const struct ck_smmu_event *e), void *ctx); |
| 214 | const char *ck_smmu_event_name(uint8_t type); |
| 215 | #endif |