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.c
| 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.c -- see smmu.h. Port of crates/aienos-kernel/src/smmu.rs. */ |
| 18 | #include "smmu.h" |
| 19 | |
| 20 | /* TEST-only two-level stream table mutants (make smmu-l2-mutants): each |
| 21 | * must make test_smmu FAIL. Host builds only; never part of an image. */ |
| 22 | #if defined(CK_SMMU_MUTANT_SPAN_OVERFLOW) || defined(CK_SMMU_MUTANT_UNGRANTED_VALID) || \ |
| 23 | defined(CK_SMMU_MUTANT_BAD_L1_PTR) || defined(CK_SMMU_MUTANT_L2_NO_FILL) || \ |
| 24 | defined(CK_SMMU_MUTANT_DROP_EVENT) |
| 25 | #if !__STDC_HOSTED__ |
| 26 | #error "CK_SMMU_MUTANT_* are host test mutants only" |
| 27 | #endif |
| 28 | #ifdef CK_HARDWARE_STAGING |
| 29 | #error "CK_SMMU_MUTANT_* cannot be combined with CK_HARDWARE_STAGING" |
| 30 | #endif |
| 31 | #endif |
| 32 | |
| 33 | #define IDR1_SIDSIZE_MASK 0x3fu |
| 34 | #define IDR1_EVENTQS_SHIFT 16u |
| 35 | #define IDR1_CMDQS_SHIFT 21u |
| 36 | #define IDR1_QS_MASK 0x1fu |
| 37 | #define CR0_ACK_MASK 0x0fu |
| 38 | #define CMDQ_CONS_ERR_MASK (0x7fu << 24) |
| 39 | #define EVENTQ_OVERFLOW (1u << 31) |
| 40 | |
| 41 | static uint32_t rd(const struct ck_smmu_regs *r, uint32_t off) { return r->rd32(r->ctx, off); } |
| 42 | static void wr(const struct ck_smmu_regs *r, uint32_t off, uint32_t v) { r->wr32(r->ctx, off, v); } |
| 43 | static void wr64(const struct ck_smmu_regs *r, uint32_t off, uint64_t v) { r->wr64(r->ctx, off, v); } |
| 44 | static void bar(const struct ck_smmu_regs *r) { r->barrier(r->ctx); } |
| 45 | |
| 46 | static int pow2(uint32_t n, uint32_t min) { return n >= min && (n & (n - 1)) == 0; } |
| 47 | static uint32_t log2u(uint32_t n) |
| 48 | { |
| 49 | uint32_t s = 0; |
| 50 | while (n > 1u) { |
| 51 | n >>= 1; |
| 52 | s++; |
| 53 | } |
| 54 | return s; |
| 55 | } |
| 56 | static int aligned(const void *p, uint64_t bytes) |
| 57 | { |
| 58 | uint64_t a = (uint64_t)(uintptr_t)p; |
| 59 | if (bytes < 32u) bytes = 32u; |
| 60 | return a != 0 && (a & (bytes - 1u)) == 0; |
| 61 | } |
| 62 | |
| 63 | static uint32_t l1_n(const struct ck_smmu_tables *t) { return 1u << (t->sid_bits - CK_SMMU_SPLIT); } |
| 64 | |
| 65 | int ck_smmu_tables_check(const struct ck_smmu_tables *t) |
| 66 | { |
| 67 | if (!pow2(t->cmd_n, 2) || !pow2(t->evt_n, 2) || t->cmd_n > (1u << 19) || t->evt_n > (1u << 19) || |
| 68 | !aligned(t->cmdq, (uint64_t)t->cmd_n * CK_SMMU_CMD_BYTES) || |
| 69 | !aligned(t->evtq, (uint64_t)t->evt_n * CK_SMMU_EVT_BYTES)) |
| 70 | return CK_SMMU_EINVAL; |
| 71 | if (t->l1) { |
| 72 | if (t->sid_bits < CK_SMMU_L2_MIN_BITS || t->sid_bits > CK_SMMU_L2_MAX_BITS || !t->l2_alloc) |
| 73 | return CK_SMMU_EINVAL; |
| 74 | /* IHI 0070 6.3.24: 2-level base aligned to max(64 bytes, L1 size). */ |
| 75 | uint64_t l1_bytes = (uint64_t)l1_n(t) * 8u; |
| 76 | #ifndef CK_SMMU_MUTANT_BAD_L1_PTR |
| 77 | if (!aligned(t->l1, l1_bytes < 64u ? 64u : l1_bytes) || |
| 78 | ((uint64_t)(uintptr_t)t->l1 & ~CK_SMMU_L1STD_L2PTR_MASK)) |
| 79 | return CK_SMMU_EINVAL; |
| 80 | #else |
| 81 | (void)l1_bytes; /* TEST mutant: any L1 pointer accepted */ |
| 82 | #endif |
| 83 | return 0; |
| 84 | } |
| 85 | if (!pow2(t->ste_n, 1) || t->ste_n > (1u << 16) || !aligned(t->strtab, (uint64_t)t->ste_n * CK_SMMU_STE_BYTES)) |
| 86 | return CK_SMMU_EINVAL; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int ck_smmu_sid_in_range(const struct ck_smmu_tables *t, uint32_t sid) |
| 91 | { |
| 92 | if (t->l1) |
| 93 | return t->sid_bits <= CK_SMMU_L2_MAX_BITS && (sid >> t->sid_bits) == 0; |
| 94 | return sid < t->ste_n; |
| 95 | } |
| 96 | |
| 97 | uint64_t ck_smmu_l1std(const struct ck_smmu_tables *t, uint32_t sid) |
| 98 | { |
| 99 | if (!t->l1 || !ck_smmu_sid_in_range(t, sid)) |
| 100 | return 0; |
| 101 | return ((volatile const uint64_t *)t->l1)[sid >> CK_SMMU_SPLIT]; |
| 102 | } |
| 103 | |
| 104 | /* The STE of `sid`, or NULL when out of range or (two-level) its span has |
| 105 | * no L2 table yet (or a descriptor this code did not write: wrong Span or a |
| 106 | * zero L2Ptr, which the callers refuse). */ |
| 107 | static volatile uint64_t *ste_at(const struct ck_smmu_tables *t, uint32_t sid) |
| 108 | { |
| 109 | if (!ck_smmu_sid_in_range(t, sid)) |
| 110 | return 0; |
| 111 | if (!t->l1) |
| 112 | return (volatile uint64_t *)t->strtab + (uint64_t)sid * 8u; |
| 113 | uint64_t d = ck_smmu_l1std(t, sid); |
| 114 | if ((d & CK_SMMU_L1STD_SPAN_MASK) != CK_SMMU_L1STD_SPAN || !(d & CK_SMMU_L1STD_L2PTR_MASK)) |
| 115 | return 0; |
| 116 | return (volatile uint64_t *)(uintptr_t)(d & CK_SMMU_L1STD_L2PTR_MASK) + |
| 117 | (uint64_t)(sid & (CK_SMMU_L2_STES - 1u)) * 8u; |
| 118 | } |
| 119 | |
| 120 | void ck_smmu_ste_abort(uint64_t ste[8]) |
| 121 | { |
| 122 | /* V = 1, Config = 0b000: abort every transaction. No bypass builder. */ |
| 123 | ste[0] = 1; |
| 124 | for (int i = 1; i < 8; i++) |
| 125 | ste[i] = 0; |
| 126 | } |
| 127 | |
| 128 | void ck_smmu_ste_stage1(uint64_t ste[8], uint64_t cd_addr) |
| 129 | { |
| 130 | /* V (bit 0), Config 0b101 (S1 translate, S2 bypass) in bits 3:1, |
| 131 | * S1Fmt 0 (linear, one CD), S1ContextPtr bits 51:6. */ |
| 132 | ste[0] = 1u | (0x5ull << 1) | (cd_addr & 0x000fffffffffffc0ull); |
| 133 | for (int i = 1; i < 8; i++) |
| 134 | ste[i] = 0; |
| 135 | } |
| 136 | |
| 137 | int ck_smmu_cd_stage1(uint64_t cd[8], uint64_t ttb0, uint16_t asid, uint64_t mair, uint8_t t0sz) |
| 138 | { |
| 139 | if (t0sz > 39u || (ttb0 & 0xfffu)) |
| 140 | return CK_SMMU_EINVAL; |
| 141 | /* T0SZ, TG0 = 0 (4 KiB), IRGN0/ORGN0 = 0 and SH0 = 0 (Non-cacheable |
| 142 | * walks: the tables are in Non-cacheable memory; the Rust reference |
| 143 | * uses write-back walks over cleaned write-back memory), EPD1, V, |
| 144 | * IPS = 5 (48-bit), AA64, R (record faults), A (abort faulting |
| 145 | * transactions), ASID. */ |
| 146 | cd[0] = (uint64_t)t0sz | (1ull << 30) | (1ull << 31) | (0x5ull << 32) | (1ull << 41) | (1ull << 45) | |
| 147 | (1ull << 46) | ((uint64_t)asid << 48); |
| 148 | cd[1] = ttb0 & 0x0000fffffffffff0ull; |
| 149 | cd[2] = 0; |
| 150 | cd[3] = mair; |
| 151 | for (int i = 4; i < 8; i++) |
| 152 | cd[i] = 0; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | void ck_smmu_cmd(uint64_t w[2], uint8_t opcode, uint32_t sid, int leaf) |
| 157 | { |
| 158 | w[0] = (uint64_t)opcode | ((uint64_t)sid << 32); |
| 159 | w[1] = opcode == CK_SMMU_CMD_CFGI_ALL ? 31u : (leaf ? 1u : 0u); |
| 160 | } |
| 161 | |
| 162 | uint32_t ck_smmu_q_encode(uint32_t index, int wrap, uint32_t entries) |
| 163 | { |
| 164 | return (index & (entries - 1u)) | ((wrap ? 1u : 0u) << log2u(entries)); |
| 165 | } |
| 166 | |
| 167 | struct qc { |
| 168 | uint32_t index; |
| 169 | int wrap; |
| 170 | }; |
| 171 | static struct qc q_decode(uint32_t v, uint32_t entries) |
| 172 | { |
| 173 | struct qc c = { v & (entries - 1u), (v & entries) != 0 }; |
| 174 | return c; |
| 175 | } |
| 176 | static void q_advance(struct qc *c, uint32_t entries) |
| 177 | { |
| 178 | if (++c->index == entries) { |
| 179 | c->index = 0; |
| 180 | c->wrap = !c->wrap; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static int cmdq_cons(const struct ck_smmu_regs *r, uint32_t entries, struct qc *out) |
| 185 | { |
| 186 | uint32_t v = rd(r, CK_SMMU_CMDQ_CONS); |
| 187 | if (v & CMDQ_CONS_ERR_MASK) |
| 188 | return CK_SMMU_ECMDQ; |
| 189 | *out = q_decode(v, entries); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int ck_smmu_submit(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, const uint64_t (*cmds)[2], |
| 194 | uint32_t n, uint32_t spins) |
| 195 | { |
| 196 | uint32_t entries = t->cmd_n; |
| 197 | struct qc prod = q_decode(rd(r, CK_SMMU_CMDQ_PROD), entries), cons; |
| 198 | for (uint32_t k = 0; k < n; k++) { |
| 199 | /* Full when the indexes match and the wrap flags differ. */ |
| 200 | int space = 0; |
| 201 | for (uint32_t i = 0; i < spins; i++) { |
| 202 | int rc = cmdq_cons(r, entries, &cons); |
| 203 | if (rc) |
| 204 | return rc; |
| 205 | if (!(cons.index == prod.index && cons.wrap != prod.wrap)) { |
| 206 | space = 1; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | if (!space) |
| 211 | return CK_SMMU_ETIMEOUT; |
| 212 | volatile uint64_t *slot = t->cmdq + (uint64_t)prod.index * 2u; |
| 213 | slot[0] = cmds[k][0]; |
| 214 | slot[1] = cmds[k][1]; |
| 215 | bar(r); |
| 216 | q_advance(&prod, entries); |
| 217 | wr(r, CK_SMMU_CMDQ_PROD, ck_smmu_q_encode(prod.index, prod.wrap, entries)); |
| 218 | } |
| 219 | for (uint32_t i = 0; i < spins; i++) { |
| 220 | int rc = cmdq_cons(r, entries, &cons); |
| 221 | if (rc) |
| 222 | return rc; |
| 223 | if (cons.index == prod.index && cons.wrap == prod.wrap) |
| 224 | return 0; |
| 225 | } |
| 226 | return CK_SMMU_ETIMEOUT; |
| 227 | } |
| 228 | |
| 229 | static int poll_gbpa_idle(const struct ck_smmu_regs *r, uint32_t spins, uint32_t *out) |
| 230 | { |
| 231 | for (uint32_t i = 0; i < spins; i++) { |
| 232 | uint32_t g = rd(r, CK_SMMU_GBPA); |
| 233 | if (!(g & CK_SMMU_GBPA_UPDATE)) { |
| 234 | *out = g; |
| 235 | return 0; |
| 236 | } |
| 237 | } |
| 238 | return CK_SMMU_ETIMEOUT; |
| 239 | } |
| 240 | |
| 241 | static int set_global_abort(const struct ck_smmu_regs *r, uint32_t spins) |
| 242 | { |
| 243 | uint32_t g; |
| 244 | int rc = poll_gbpa_idle(r, spins, &g); |
| 245 | if (rc) |
| 246 | return rc; |
| 247 | g = (g & ~CK_SMMU_GBPA_UPDATE) | CK_SMMU_GBPA_ABORT; |
| 248 | wr(r, CK_SMMU_GBPA, g | CK_SMMU_GBPA_UPDATE); |
| 249 | rc = poll_gbpa_idle(r, spins, &g); |
| 250 | if (rc) |
| 251 | return rc; |
| 252 | return (g & CK_SMMU_GBPA_ABORT) ? 0 : CK_SMMU_ENOABORT; |
| 253 | } |
| 254 | |
| 255 | static int wait_cr0(const struct ck_smmu_regs *r, uint32_t want, uint32_t spins) |
| 256 | { |
| 257 | for (uint32_t i = 0; i < spins; i++) |
| 258 | if ((rd(r, CK_SMMU_CR0ACK) & CR0_ACK_MASK) == (want & CR0_ACK_MASK)) |
| 259 | return 0; |
| 260 | return CK_SMMU_ETIMEOUT; |
| 261 | } |
| 262 | |
| 263 | static void write_ste_word(volatile uint64_t *ste, int word, uint64_t v) |
| 264 | { |
| 265 | /* A 64-bit aligned store is single-copy atomic: no torn word 0. */ |
| 266 | ste[word] = v; |
| 267 | } |
| 268 | |
| 269 | static int invalidate_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, |
| 270 | uint32_t spins) |
| 271 | { |
| 272 | /* STE, every cached CD of this stream (the CD page is reused when a |
| 273 | * stream is re-confined), then the TLBs. */ |
| 274 | uint64_t c[4][2]; |
| 275 | ck_smmu_cmd(c[0], CK_SMMU_CMD_CFGI_STE, sid, 1); |
| 276 | ck_smmu_cmd(c[1], CK_SMMU_CMD_CFGI_CD_ALL, sid, 0); |
| 277 | ck_smmu_cmd(c[2], CK_SMMU_CMD_TLBI_NSNH_ALL, 0, 0); |
| 278 | ck_smmu_cmd(c[3], CK_SMMU_CMD_SYNC, 0, 0); |
| 279 | bar(r); |
| 280 | return ck_smmu_submit(r, t, (const uint64_t (*)[2])c, 4, spins); |
| 281 | } |
| 282 | |
| 283 | static int program(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t spins) |
| 284 | { |
| 285 | uint32_t sid_bits = t->l1 ? t->sid_bits : log2u(t->ste_n); |
| 286 | uint32_t idr0 = rd(r, CK_SMMU_IDR0); |
| 287 | if (!(idr0 & CK_SMMU_IDR0_S1P)) |
| 288 | return CK_SMMU_EUNSUP; |
| 289 | if (t->l1 && ((idr0 >> CK_SMMU_IDR0_ST_LEVEL_SHIFT) & CK_SMMU_IDR0_ST_LEVEL_MASK) != 1u) |
| 290 | return CK_SMMU_EUNSUP; |
| 291 | uint32_t idr1 = rd(r, CK_SMMU_IDR1); |
| 292 | if ((idr1 & IDR1_SIDSIZE_MASK) < sid_bits || |
| 293 | ((idr1 >> IDR1_CMDQS_SHIFT) & IDR1_QS_MASK) < log2u(t->cmd_n) || |
| 294 | ((idr1 >> IDR1_EVENTQS_SHIFT) & IDR1_QS_MASK) < log2u(t->evt_n)) |
| 295 | return CK_SMMU_EUNSUP; |
| 296 | if (t->l1) { |
| 297 | /* Every span invalid (Span 0): its StreamIDs are terminated. */ |
| 298 | volatile uint64_t *l1 = t->l1; |
| 299 | for (uint32_t i = 0; i < l1_n(t); i++) |
| 300 | #ifndef CK_SMMU_MUTANT_UNGRANTED_VALID |
| 301 | l1[i] = 0; |
| 302 | #else |
| 303 | l1[i] = CK_SMMU_L1STD_SPAN; /* TEST mutant: valid span, L2Ptr 0 */ |
| 304 | #endif |
| 305 | } else { |
| 306 | uint64_t abort[8]; |
| 307 | ck_smmu_ste_abort(abort); |
| 308 | for (uint32_t sid = 0; sid < t->ste_n; sid++) |
| 309 | for (int w = 0; w < 8; w++) |
| 310 | write_ste_word((volatile uint64_t *)t->strtab + (uint64_t)sid * 8u, w, abort[w]); |
| 311 | } |
| 312 | volatile uint64_t *cq = t->cmdq, *eq = t->evtq; |
| 313 | for (uint64_t i = 0; i < (uint64_t)t->cmd_n * 2u; i++) |
| 314 | cq[i] = 0; |
| 315 | for (uint64_t i = 0; i < (uint64_t)t->evt_n * 4u; i++) |
| 316 | eq[i] = 0; |
| 317 | bar(r); /* everything the SMMU will fetch is written */ |
| 318 | |
| 319 | wr(r, CK_SMMU_CR0, 0); |
| 320 | int rc = wait_cr0(r, 0, spins); |
| 321 | if (rc) |
| 322 | return rc; |
| 323 | /* CR1 = 0: queue and table fetches Non-cacheable, matching the |
| 324 | * Non-cacheable DMA pool (the Rust reference uses 0x0d75, write-back). */ |
| 325 | wr(r, CK_SMMU_CR1, 0); |
| 326 | wr64(r, CK_SMMU_CMDQ_BASE, (uint64_t)(uintptr_t)t->cmdq | log2u(t->cmd_n)); |
| 327 | wr(r, CK_SMMU_CMDQ_CONS, 0); |
| 328 | wr(r, CK_SMMU_CMDQ_PROD, 0); |
| 329 | wr64(r, CK_SMMU_EVENTQ_BASE, (uint64_t)(uintptr_t)t->evtq | log2u(t->evt_n)); |
| 330 | wr(r, CK_SMMU_EVENTQ_CONS, 0); |
| 331 | wr(r, CK_SMMU_EVENTQ_PROD, 0); |
| 332 | if (t->l1) { |
| 333 | wr(r, CK_SMMU_STRTAB_BASE_CFG, CK_SMMU_STRTAB_FMT_2LVL | (CK_SMMU_SPLIT << 6) | sid_bits); |
| 334 | wr64(r, CK_SMMU_STRTAB_BASE, (uint64_t)(uintptr_t)t->l1); |
| 335 | } else { |
| 336 | wr(r, CK_SMMU_STRTAB_BASE_CFG, sid_bits); /* FMT = 0: linear */ |
| 337 | wr64(r, CK_SMMU_STRTAB_BASE, (uint64_t)(uintptr_t)t->strtab); |
| 338 | } |
| 339 | wr(r, CK_SMMU_CR0, CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN); |
| 340 | rc = wait_cr0(r, CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN, spins); |
| 341 | if (rc) |
| 342 | return rc; |
| 343 | uint64_t c[3][2]; |
| 344 | ck_smmu_cmd(c[0], CK_SMMU_CMD_CFGI_ALL, 0, 0); |
| 345 | ck_smmu_cmd(c[1], CK_SMMU_CMD_TLBI_NSNH_ALL, 0, 0); |
| 346 | ck_smmu_cmd(c[2], CK_SMMU_CMD_SYNC, 0, 0); |
| 347 | rc = ck_smmu_submit(r, t, (const uint64_t (*)[2])c, 3, spins); |
| 348 | if (rc) |
| 349 | return rc; |
| 350 | uint32_t en = CK_SMMU_CR0_SMMUEN | CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN; |
| 351 | wr(r, CK_SMMU_CR0, en); |
| 352 | return wait_cr0(r, en, spins); |
| 353 | } |
| 354 | |
| 355 | int ck_smmu_enable(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t spins) |
| 356 | { |
| 357 | if (ck_smmu_tables_check(t)) |
| 358 | return CK_SMMU_EINVAL; |
| 359 | int rc = set_global_abort(r, spins); |
| 360 | if (rc) |
| 361 | return rc; /* ABORT may not have latched: caller treats the SMMU as unusable */ |
| 362 | rc = program(r, t, spins); |
| 363 | if (rc) { |
| 364 | /* SMMU off behind GBPA.ABORT: every stream aborts. Best effort ack. */ |
| 365 | wr(r, CK_SMMU_CR0, 0); |
| 366 | (void)wait_cr0(r, 0, spins); |
| 367 | } |
| 368 | return rc; |
| 369 | } |
| 370 | |
| 371 | /* Two-level: give the span of `sid` its L2 table. The page is checked |
| 372 | * (non-zero, aligned to CK_SMMU_L2_BYTES, inside L2Ptr bits 55:6), filled |
| 373 | * with abort STEs, made visible, and only then published in the L1STD |
| 374 | * (Span = SPLIT + 1). IHI 0070 5.1.1: an L1STD going from Span 0 to active |
| 375 | * needs the non-leaf CMD_CFGI_STE only. */ |
| 376 | static int attach_span(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, |
| 377 | uint32_t spins) |
| 378 | { |
| 379 | if (ck_smmu_l1std(t, sid) != 0) |
| 380 | return CK_SMMU_EINVAL; /* a descriptor we did not write: refuse, do not overwrite */ |
| 381 | uint64_t *page = 0; |
| 382 | if (t->l2_alloc(t->l2_ctx, &page) || !page) |
| 383 | return CK_SMMU_EINVAL; |
| 384 | uint64_t a = (uint64_t)(uintptr_t)page; |
| 385 | if ((a & (CK_SMMU_L2_BYTES - 1u)) || (a & ~CK_SMMU_L1STD_L2PTR_MASK)) |
| 386 | return CK_SMMU_EINVAL; |
| 387 | #ifndef CK_SMMU_MUTANT_L2_NO_FILL |
| 388 | uint64_t abort[8]; |
| 389 | ck_smmu_ste_abort(abort); |
| 390 | for (uint32_t i = 0; i < CK_SMMU_L2_STES; i++) |
| 391 | for (int w = 0; w < 8; w++) |
| 392 | write_ste_word((volatile uint64_t *)page + (uint64_t)i * 8u, w, abort[w]); |
| 393 | #endif |
| 394 | bar(r); /* every STE of the span is abort before the span goes live */ |
| 395 | #ifndef CK_SMMU_MUTANT_SPAN_OVERFLOW |
| 396 | uint64_t d = a | CK_SMMU_L1STD_SPAN; |
| 397 | #else |
| 398 | uint64_t d = a | (CK_SMMU_L1STD_SPAN + 1u); /* TEST mutant: Span past SPLIT + 1 */ |
| 399 | #endif |
| 400 | ((volatile uint64_t *)t->l1)[sid >> CK_SMMU_SPLIT] = d; |
| 401 | uint64_t c[2][2]; |
| 402 | ck_smmu_cmd(c[0], CK_SMMU_CMD_CFGI_STE, sid, 0); /* non-leaf: the L1STD */ |
| 403 | ck_smmu_cmd(c[1], CK_SMMU_CMD_SYNC, 0, 0); |
| 404 | bar(r); |
| 405 | return ck_smmu_submit(r, t, (const uint64_t (*)[2])c, 2, spins); |
| 406 | } |
| 407 | |
| 408 | int ck_smmu_abort_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, |
| 409 | uint32_t spins) |
| 410 | { |
| 411 | if (!ck_smmu_sid_in_range(t, sid)) |
| 412 | return CK_SMMU_EINVAL; |
| 413 | volatile uint64_t *e = ste_at(t, sid); |
| 414 | if (!e) { |
| 415 | /* Two-level span without an L2 table: Span 0, every StreamID of it |
| 416 | * is already terminated. Anything else is not ours: refuse. */ |
| 417 | return ck_smmu_l1std(t, sid) == 0 ? 0 : CK_SMMU_EINVAL; |
| 418 | } |
| 419 | uint64_t abort[8]; |
| 420 | ck_smmu_ste_abort(abort); |
| 421 | /* Word 0 first: once V = 1 and Config = abort the rest is ignored. */ |
| 422 | for (int w = 0; w < 8; w++) |
| 423 | write_ste_word(e, w, abort[w]); |
| 424 | return invalidate_ste(r, t, sid, spins); |
| 425 | } |
| 426 | |
| 427 | int ck_smmu_install_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, |
| 428 | const uint64_t ste[8], uint32_t spins) |
| 429 | { |
| 430 | if (!ck_smmu_sid_in_range(t, sid)) |
| 431 | return CK_SMMU_EINVAL; |
| 432 | int rc; |
| 433 | if (t->l1 && !ste_at(t, sid) && (rc = attach_span(r, t, sid, spins))) |
| 434 | return rc; |
| 435 | volatile uint64_t *e = ste_at(t, sid); |
| 436 | if (!e) |
| 437 | return CK_SMMU_EINVAL; |
| 438 | rc = ck_smmu_abort_ste(r, t, sid, spins); |
| 439 | if (rc) |
| 440 | return rc; |
| 441 | for (int w = 1; w < 8; w++) |
| 442 | write_ste_word(e, w, ste[w]); |
| 443 | bar(r); /* words 1..7 visible before word 0 makes the entry live */ |
| 444 | write_ste_word(e, 0, ste[0]); |
| 445 | return invalidate_ste(r, t, sid, spins); |
| 446 | } |
| 447 | |
| 448 | int ck_smmu_read_events(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, |
| 449 | struct ck_smmu_event *out, int n, int *overflowed) |
| 450 | { |
| 451 | uint32_t entries = t->evt_n; |
| 452 | uint32_t prod_raw = rd(r, CK_SMMU_EVENTQ_PROD), cons_raw = rd(r, CK_SMMU_EVENTQ_CONS); |
| 453 | struct qc prod = q_decode(prod_raw, entries), cons = q_decode(cons_raw, entries); |
| 454 | uint32_t ovf = prod_raw & EVENTQ_OVERFLOW; |
| 455 | int count = 0; |
| 456 | bar(r); |
| 457 | while ((cons.index != prod.index || cons.wrap != prod.wrap) && count < n) { |
| 458 | volatile const uint64_t *slot = t->evtq + (uint64_t)cons.index * 4u; |
| 459 | struct ck_smmu_event *e = &out[count++]; |
| 460 | for (int w = 0; w < 4; w++) |
| 461 | e->raw[w] = slot[w]; |
| 462 | e->type = (uint8_t)e->raw[0]; |
| 463 | e->sid = (uint32_t)(e->raw[0] >> 32); |
| 464 | e->addr = e->raw[2]; |
| 465 | q_advance(&cons, entries); |
| 466 | } |
| 467 | /* Copying OVFLG into OVACKFLG acknowledges an overflow. */ |
| 468 | wr(r, CK_SMMU_EVENTQ_CONS, ck_smmu_q_encode(cons.index, cons.wrap, entries) | ovf); |
| 469 | if (overflowed) |
| 470 | *overflowed = ovf != (cons_raw & EVENTQ_OVERFLOW); |
| 471 | return count; |
| 472 | } |
| 473 | |
| 474 | const char *ck_smmu_event_name(uint8_t type) |
| 475 | { |
| 476 | /* Arm IHI 0070 event IDs; unknown events keep their raw payload. */ |
| 477 | switch (type) { |
| 478 | case 0x02: return "C_BAD_STREAMID"; |
| 479 | case 0x03: return "F_STE_FETCH"; |
| 480 | case 0x04: return "C_BAD_STE"; |
| 481 | case 0x06: return "F_STREAM_DISABLED"; |
| 482 | case 0x08: return "C_BAD_SUBSTREAMID"; |
| 483 | case 0x09: return "F_CD_FETCH"; |
| 484 | case 0x0a: return "C_BAD_CD"; |
| 485 | case 0x10: return "F_TRANSLATION"; |
| 486 | case 0x11: return "F_ADDR_SIZE"; |
| 487 | case 0x12: return "F_ACCESS"; |
| 488 | case 0x13: return "F_PERMISSION"; |
| 489 | case 0x25: return "F_VMS_FETCH"; |
| 490 | default: return "UNKNOWN"; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | struct ck_smmu_observation ck_smmu_observe_events( |
| 495 | const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, |
| 496 | void (*visit)(void *ctx, const struct ck_smmu_event *e), void *ctx) |
| 497 | { |
| 498 | struct ck_smmu_observation result = {0, 0, 0}; |
| 499 | #ifdef CK_SMMU_MUTANT_DROP_EVENT |
| 500 | (void)visit; |
| 501 | (void)ctx; |
| 502 | #endif |
| 503 | while (result.events < t->evt_n) { |
| 504 | struct ck_smmu_event events[4]; |
| 505 | uint32_t remaining = t->evt_n - result.events; |
| 506 | int overflow = 0; |
| 507 | int n = ck_smmu_read_events(r, t, events, remaining < 4 ? (int)remaining : 4, |
| 508 | &overflow); |
| 509 | result.overflow |= overflow; |
| 510 | for (int i = 0; i < n; i++) { |
| 511 | #ifndef CK_SMMU_MUTANT_DROP_EVENT |
| 512 | visit(ctx, &events[i]); |
| 513 | #endif |
| 514 | } |
| 515 | result.events += (uint32_t)n; |
| 516 | if (n < 4) |
| 517 | break; |
| 518 | } |
| 519 | struct qc prod = q_decode(rd(r, CK_SMMU_EVENTQ_PROD), t->evt_n); |
| 520 | struct qc cons = q_decode(rd(r, CK_SMMU_EVENTQ_CONS), t->evt_n); |
| 521 | result.pending = prod.index != cons.index || prod.wrap != cons.wrap; |
| 522 | return result; |
| 523 | } |