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 / gpu / gmmu.c
| 1 | /* gmmu.c -- GB10 GPU page tables over a CPU view of the GPU-visible window. |
| 2 | * Format and register sources: see gmmu.h. Freestanding: no libc. */ |
| 3 | #include "gmmu.h" |
| 4 | |
| 5 | #define PDE_ADDR_MASK 0x000FFFFFFFFFF000ull /* NV_MMU_VER3_PDE_ADDRESS 51:12 */ |
| 6 | #define DUAL_BIG_ADDR_MASK 0x000FFFFFFFFFFF00ull /* NV_MMU_VER3_DUAL_PDE_ADDRESS_BIG 51:8 */ |
| 7 | #define PTE_ADDR_MASK 0x000FFFFFFFFFF000ull /* NV_MMU_VER3_PTE_ADDRESS 51:12 */ |
| 8 | |
| 9 | /* Level layout (kgmmuFmtInitLevels_GH10X / _GB10X). Index 5 is PT_BIG, |
| 10 | * index 6 PT_SMALL; both hang off the PD0 dual PDE. */ |
| 11 | static const uint8_t lv_lo[7] = {56, 47, 38, 29, 21, 16, 12}; |
| 12 | static const uint8_t lv_hi[7] = {56, 55, 46, 37, 28, 20, 20}; |
| 13 | #define LV_PD0 4u |
| 14 | #define LV_BIG 5u |
| 15 | #define LV_SMALL 6u |
| 16 | |
| 17 | uint64_t ck_gmmu_pde(uint64_t pa, uint32_t aperture) |
| 18 | { |
| 19 | return (pa & PDE_ADDR_MASK) | ((uint64_t)CK_GMMU_PDE_PCF_CACHED_NO_ATS << 3) | ((uint64_t)(aperture & 3u) << 1); |
| 20 | } |
| 21 | |
| 22 | void ck_gmmu_dual_pde(uint64_t out[2], uint64_t big_pa, uint32_t big_ap, uint64_t small_pa, uint32_t small_ap) |
| 23 | { |
| 24 | /* Big half: APERTURE_BIG 2:1, PCF_BIG 5:3, ADDRESS_BIG 51:8 (shift 8). |
| 25 | * Small half: APERTURE_SMALL 66:65, PCF_SMALL 69:67, ADDRESS_SMALL 115:76 (shift 12). */ |
| 26 | out[0] = 0; |
| 27 | out[1] = 0; |
| 28 | if (big_ap != CK_GMMU_PDE_AP_INVALID) |
| 29 | out[0] = (big_pa & DUAL_BIG_ADDR_MASK) | ((uint64_t)CK_GMMU_PDE_PCF_CACHED_NO_ATS << 3) | ((uint64_t)(big_ap & 3u) << 1); |
| 30 | if (small_ap != CK_GMMU_PDE_AP_INVALID) |
| 31 | out[1] = (small_pa & PTE_ADDR_MASK) | ((uint64_t)CK_GMMU_PDE_PCF_CACHED_NO_ATS << 3) | ((uint64_t)(small_ap & 3u) << 1); |
| 32 | } |
| 33 | |
| 34 | uint64_t ck_gmmu_pte(uint64_t pa, uint32_t aperture, uint32_t pcf, uint32_t kind) |
| 35 | { |
| 36 | /* VALID 0:0, APERTURE 2:1, PCF 7:3, KIND 11:8, ADDRESS 51:12, PEER_ID 63:61 = 0. */ |
| 37 | return 1ull | ((uint64_t)(aperture & 3u) << 1) | ((uint64_t)(pcf & 0x1Fu) << 3) | ((uint64_t)(kind & 0xFu) << 8) | |
| 38 | (pa & PTE_ADDR_MASK); |
| 39 | } |
| 40 | |
| 41 | static uint32_t lv_index(uint32_t lv, uint64_t va) |
| 42 | { |
| 43 | uint32_t bits = (uint32_t)(lv_hi[lv] - lv_lo[lv] + 1u); |
| 44 | return (uint32_t)((va >> lv_lo[lv]) & ((1ull << bits) - 1ull)); |
| 45 | } |
| 46 | |
| 47 | static volatile uint64_t *ent(const struct ck_gmmu_as *as, uint64_t table_pa, uint32_t idx, uint32_t esize) |
| 48 | { |
| 49 | return (volatile uint64_t *)(as->win->cpu + (table_pa - as->win->pa_base) + (uint64_t)idx * esize); |
| 50 | } |
| 51 | |
| 52 | static int pa_in_window(const struct ck_gpu_window *w, uint64_t pa, uint64_t size) |
| 53 | { |
| 54 | return size != 0 && pa >= w->pa_base && pa - w->pa_base <= w->size && size <= w->size - (pa - w->pa_base); |
| 55 | } |
| 56 | |
| 57 | static void zero_page(const struct ck_gmmu_as *as, uint64_t pa) |
| 58 | { |
| 59 | volatile uint64_t *p = ent(as, pa, 0, 8); |
| 60 | for (uint32_t i = 0; i < CK_GMMU_PT_BYTES / 8u; i++) |
| 61 | p[i] = 0; |
| 62 | } |
| 63 | |
| 64 | static int pt_alloc(struct ck_gmmu_as *as, uint64_t *pa) |
| 65 | { |
| 66 | for (uint32_t i = 0; i < as->pt_count; i++) { |
| 67 | if (!as->pt_used[i]) { |
| 68 | as->pt_used[i] = 1; |
| 69 | zero_page(as, as->pt_pa[i]); |
| 70 | *pa = as->pt_pa[i]; |
| 71 | return CK_GMMU_OK; |
| 72 | } |
| 73 | } |
| 74 | return CK_GMMU_ENOMEM; |
| 75 | } |
| 76 | |
| 77 | /* A table address read back from table memory must name a page of this |
| 78 | * address space's pool that is in use. Anything else (a stray or corrupted |
| 79 | * PDE) would make the walk read and write outside the page tables. */ |
| 80 | static int pt_live(const struct ck_gmmu_as *as, uint64_t pa) |
| 81 | { |
| 82 | #ifdef CK_GPU_MUTANT_NO_PDE_CHECK |
| 83 | (void)as; |
| 84 | (void)pa; |
| 85 | return 1; |
| 86 | #else |
| 87 | for (uint32_t i = 0; i < as->pt_count; i++) |
| 88 | if (as->pt_pa[i] == pa) |
| 89 | return as->pt_used[i] != 0; |
| 90 | return 0; |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | static int overlaps_pool(const struct ck_gmmu_as *as, uint64_t pa, uint64_t size) |
| 95 | { |
| 96 | #ifdef CK_GPU_MUTANT_NO_POOL_OVERLAP |
| 97 | (void)as; |
| 98 | (void)pa; |
| 99 | (void)size; |
| 100 | return 0; |
| 101 | #else |
| 102 | for (uint32_t i = 0; i < as->pt_count; i++) |
| 103 | if (as->pt_pa[i] < pa + size && pa < as->pt_pa[i] + CK_GMMU_PT_BYTES) |
| 104 | return 1; |
| 105 | return 0; |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | static uint32_t pt_free_count(const struct ck_gmmu_as *as) |
| 110 | { |
| 111 | uint32_t n = 0; |
| 112 | for (uint32_t i = 0; i < as->pt_count; i++) |
| 113 | n += as->pt_used[i] ? 0u : 1u; |
| 114 | return n; |
| 115 | } |
| 116 | |
| 117 | int ck_gmmu_as_init(struct ck_gmmu_as *as, const struct ck_gpu_window *win, uint64_t pool_pa, uint32_t pool_pages) |
| 118 | { |
| 119 | if (!as || !win || !win->cpu || pool_pages < 2 || pool_pages > CK_GMMU_MAX_PT || (pool_pa & 0xFFFu)) |
| 120 | return CK_GMMU_EINVAL; |
| 121 | if (!pa_in_window(win, pool_pa, (uint64_t)pool_pages * CK_GMMU_PT_BYTES)) |
| 122 | return CK_GMMU_ERANGE; |
| 123 | as->win = win; |
| 124 | as->pt_count = pool_pages; |
| 125 | as->dead = 0; |
| 126 | for (uint32_t i = 0; i < CK_GMMU_MAX_PT; i++) { |
| 127 | as->pt_pa[i] = i < pool_pages ? pool_pa + (uint64_t)i * CK_GMMU_PT_BYTES : 0; |
| 128 | as->pt_used[i] = 0; |
| 129 | } |
| 130 | return pt_alloc(as, &as->root_pa); |
| 131 | } |
| 132 | |
| 133 | enum walk_r { W_FREE, W_MAPPED, W_MISSING, W_CONFLICT, W_CORRUPT }; |
| 134 | |
| 135 | /* Walk to the leaf slot for `va` at page size `page`. With create=0 nothing |
| 136 | * is written; *miss_lv gets the first level whose table does not exist yet |
| 137 | * (W_MISSING). With create=1 missing tables are allocated (the caller has |
| 138 | * already checked the pool). *leaf is the PTE slot on W_FREE / W_MAPPED. |
| 139 | * W_CORRUPT: a PDE read back from table memory does not point at a live page |
| 140 | * of this pool; nothing was dereferenced through it. */ |
| 141 | static enum walk_r walk(struct ck_gmmu_as *as, uint64_t va, uint64_t page, int create, uint32_t *miss_lv, |
| 142 | volatile uint64_t **leaf) |
| 143 | { |
| 144 | uint64_t tbl = as->root_pa; |
| 145 | for (uint32_t lv = 0; lv < LV_PD0; lv++) { |
| 146 | volatile uint64_t *e = ent(as, tbl, lv_index(lv, va), 8); |
| 147 | uint64_t v = *e; |
| 148 | if (v & 1u) |
| 149 | return W_CONFLICT; /* a 512 MiB / 256 GiB PTE covers this VA */ |
| 150 | if (((v >> 1) & 3u) == CK_GMMU_PDE_AP_INVALID) { |
| 151 | if (!create) { |
| 152 | *miss_lv = lv + 1u; |
| 153 | return W_MISSING; |
| 154 | } |
| 155 | uint64_t npa; |
| 156 | if (pt_alloc(as, &npa) != CK_GMMU_OK) |
| 157 | return W_CONFLICT; /* unreachable: pool checked by the caller */ |
| 158 | *e = ck_gmmu_pde(npa, CK_GMMU_PDE_AP_SYS_COH); |
| 159 | v = *e; |
| 160 | } |
| 161 | tbl = v & PDE_ADDR_MASK; |
| 162 | if (!pt_live(as, tbl)) |
| 163 | return W_CORRUPT; |
| 164 | } |
| 165 | volatile uint64_t *d = ent(as, tbl, lv_index(LV_PD0, va), 16); |
| 166 | uint64_t w0 = d[0], w1 = d[1]; |
| 167 | uint32_t big_ap = (uint32_t)((w0 >> 1) & 3u), small_ap = (uint32_t)((w1 >> 1) & 3u); |
| 168 | if (page == CK_GMMU_SZ_2M) { |
| 169 | if (w0 & 1u) { |
| 170 | *leaf = &d[0]; |
| 171 | return W_MAPPED; |
| 172 | } |
| 173 | if (big_ap || small_ap) |
| 174 | return W_CONFLICT; /* page tables already hang here */ |
| 175 | *leaf = &d[0]; |
| 176 | return W_FREE; |
| 177 | } |
| 178 | if (w0 & 1u) |
| 179 | return W_CONFLICT; /* covered by a 2 MiB PTE */ |
| 180 | int big = page == CK_GMMU_SZ_64K; |
| 181 | /* One page size per 2 MiB region: the dual PDE lets hardware walk both |
| 182 | * halves in parallel, and which one wins on overlap is not something |
| 183 | * this code wants to depend on (DOCS SILENT on overlap priority). */ |
| 184 | #ifndef CK_GPU_MUTANT_NO_MIXED_PAGES |
| 185 | if ((big && small_ap) || (!big && big_ap)) |
| 186 | return W_CONFLICT; |
| 187 | #endif |
| 188 | uint32_t ap = big ? big_ap : small_ap; |
| 189 | uint64_t pt; |
| 190 | if (ap == CK_GMMU_PDE_AP_INVALID) { |
| 191 | if (!create) { |
| 192 | *miss_lv = big ? LV_BIG : LV_SMALL; |
| 193 | return W_MISSING; |
| 194 | } |
| 195 | if (pt_alloc(as, &pt) != CK_GMMU_OK) |
| 196 | return W_CONFLICT; |
| 197 | uint64_t nd[2]; |
| 198 | if (big) |
| 199 | ck_gmmu_dual_pde(nd, pt, CK_GMMU_PDE_AP_SYS_COH, 0, CK_GMMU_PDE_AP_INVALID); |
| 200 | else |
| 201 | ck_gmmu_dual_pde(nd, 0, CK_GMMU_PDE_AP_INVALID, pt, CK_GMMU_PDE_AP_SYS_COH); |
| 202 | d[1] = nd[1]; |
| 203 | d[0] = nd[0]; |
| 204 | } else { |
| 205 | pt = big ? (w0 & DUAL_BIG_ADDR_MASK) : (w1 & PTE_ADDR_MASK); |
| 206 | if (!pt_live(as, pt)) |
| 207 | return W_CORRUPT; |
| 208 | } |
| 209 | volatile uint64_t *p = ent(as, pt, lv_index(big ? LV_BIG : LV_SMALL, va), 8); |
| 210 | *leaf = p; |
| 211 | return (*p & 1u) ? W_MAPPED : W_FREE; |
| 212 | } |
| 213 | |
| 214 | static int check_args(const struct ck_gmmu_as *as, uint64_t va, uint64_t size, uint64_t page) |
| 215 | { |
| 216 | if (!as) |
| 217 | return CK_GMMU_EINVAL; |
| 218 | if (as->dead) |
| 219 | return CK_GMMU_EDEAD; |
| 220 | if (page != CK_GMMU_SZ_4K && page != CK_GMMU_SZ_64K && page != CK_GMMU_SZ_2M) |
| 221 | return CK_GMMU_EINVAL; |
| 222 | if (size == 0) |
| 223 | return CK_GMMU_EINVAL; |
| 224 | if ((va | size) & (page - 1u)) |
| 225 | return CK_GMMU_EALIGN; |
| 226 | #ifndef CK_GPU_MUTANT_NO_VA_BITS |
| 227 | if (va >= (1ull << CK_GMMU_VA_BITS) || size > (1ull << CK_GMMU_VA_BITS) - va) |
| 228 | return CK_GMMU_ERANGE; |
| 229 | #endif |
| 230 | return CK_GMMU_OK; |
| 231 | } |
| 232 | |
| 233 | int ck_gmmu_map(struct ck_gmmu_as *as, uint64_t va, uint64_t pa, uint64_t size, uint64_t page, uint32_t aperture, |
| 234 | uint32_t pcf) |
| 235 | { |
| 236 | int rc = check_args(as, va, size, page); |
| 237 | if (rc != CK_GMMU_OK) |
| 238 | return rc; |
| 239 | if (aperture > 3u || pcf > 0x1Fu) |
| 240 | return CK_GMMU_EINVAL; |
| 241 | if (pa & (page - 1u)) |
| 242 | return CK_GMMU_EALIGN; |
| 243 | #ifndef CK_GPU_MUTANT_NO_PA_RANGE |
| 244 | if (pa >= (1ull << CK_GMMU_PA_BITS) || !pa_in_window(as->win, pa, size)) |
| 245 | return CK_GMMU_ERANGE; |
| 246 | #endif |
| 247 | if (overlaps_pool(as, pa, size)) |
| 248 | return CK_GMMU_ERANGE; /* a GPU mapping never covers its own page tables */ |
| 249 | |
| 250 | /* Dry run: every page must be free, and count the page-table pages the |
| 251 | * map would create. Pages run in VA order, so a table instance at level |
| 252 | * j (key va >> (hi_j + 1)) is counted once. */ |
| 253 | uint64_t last_key[7]; |
| 254 | for (uint32_t j = 0; j < 7; j++) |
| 255 | last_key[j] = ~0ull; |
| 256 | uint32_t need = 0; |
| 257 | for (uint64_t off = 0; off < size; off += page) { |
| 258 | uint32_t miss = 0; |
| 259 | volatile uint64_t *leaf = 0; |
| 260 | enum walk_r w = walk(as, va + off, page, 0, &miss, &leaf); |
| 261 | if (w == W_CORRUPT) { |
| 262 | as->dead = 1; |
| 263 | return CK_GMMU_EDEAD; |
| 264 | } |
| 265 | #ifndef CK_GPU_MUTANT_NO_OVERLAP |
| 266 | if (w == W_MAPPED || w == W_CONFLICT) |
| 267 | #else |
| 268 | if (w == W_CONFLICT) |
| 269 | #endif |
| 270 | return CK_GMMU_EBUSY; |
| 271 | if (w == W_MISSING) { |
| 272 | uint32_t last = page == CK_GMMU_SZ_2M ? LV_PD0 : (page == CK_GMMU_SZ_64K ? LV_BIG : LV_SMALL); |
| 273 | for (uint32_t j = miss; j <= last; j++) { |
| 274 | uint64_t key = (va + off) >> (lv_hi[j] + 1u); |
| 275 | if (key != last_key[j]) { |
| 276 | last_key[j] = key; |
| 277 | need++; |
| 278 | } |
| 279 | if (j == LV_PD0 && last == LV_SMALL) |
| 280 | j = LV_BIG; /* skip PT_BIG on the small path */ |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | #ifndef CK_GPU_MUTANT_NO_POOL_COUNT |
| 285 | if (need > pt_free_count(as)) |
| 286 | return CK_GMMU_ENOMEM; |
| 287 | #else |
| 288 | (void)pt_free_count(as); |
| 289 | #endif |
| 290 | |
| 291 | for (uint64_t off = 0; off < size; off += page) { |
| 292 | uint32_t miss = 0; |
| 293 | volatile uint64_t *leaf = 0; |
| 294 | enum walk_r w = walk(as, va + off, page, 1, &miss, &leaf); |
| 295 | #ifdef CK_GPU_MUTANT_NO_OVERLAP |
| 296 | if (w == W_MAPPED) |
| 297 | w = W_FREE; |
| 298 | #endif |
| 299 | if (w != W_FREE) |
| 300 | return CK_GMMU_EBUSY; /* unreachable after the dry run */ |
| 301 | *leaf = ck_gmmu_pte(pa + off, aperture, pcf, 0); |
| 302 | } |
| 303 | return CK_GMMU_OK; |
| 304 | } |
| 305 | |
| 306 | int ck_gmmu_unmap(struct ck_gmmu_as *as, uint64_t va, uint64_t size, uint64_t page) |
| 307 | { |
| 308 | int rc = check_args(as, va, size, page); |
| 309 | if (rc != CK_GMMU_OK) |
| 310 | return rc; |
| 311 | for (uint64_t off = 0; off < size; off += page) { |
| 312 | uint32_t miss = 0; |
| 313 | volatile uint64_t *leaf = 0; |
| 314 | enum walk_r w = walk(as, va + off, page, 0, &miss, &leaf); |
| 315 | if (w == W_CORRUPT) { |
| 316 | as->dead = 1; |
| 317 | return CK_GMMU_EDEAD; |
| 318 | } |
| 319 | if (w != W_MAPPED) |
| 320 | return CK_GMMU_ENOENT; |
| 321 | } |
| 322 | for (uint64_t off = 0; off < size; off += page) { |
| 323 | uint32_t miss = 0; |
| 324 | volatile uint64_t *leaf = 0; |
| 325 | (void)walk(as, va + off, page, 0, &miss, &leaf); |
| 326 | *leaf = 0; /* VALID=0; page-table pages stay until the AS is torn down */ |
| 327 | } |
| 328 | return CK_GMMU_OK; |
| 329 | } |
| 330 | |
| 331 | int ck_gmmu_invalidate(struct ck_gmmu_as *as, const struct ck_gpu_regs *r, uint32_t poll_budget) |
| 332 | { |
| 333 | if (!as || !r || !r->rd || !r->wr) |
| 334 | return CK_GMMU_EINVAL; |
| 335 | if (as->dead) |
| 336 | return CK_GMMU_EDEAD; |
| 337 | uint64_t pdb = as->root_pa >> 12; /* MMU_INVALIDATE_PDB_ADDR_ALIGNMENT 12 */ |
| 338 | r->wr(r->ctx, CK_GPU_REG_INVAL_PDB, (uint32_t)((pdb & 0x0FFFFFFFull) << 4) | CK_GPU_INVAL_PDB_AP_SYS); |
| 339 | r->wr(r->ctx, CK_GPU_REG_INVAL_UPPER, (uint32_t)((pdb >> 28) & 0xFFFFFull)); |
| 340 | r->wr(r->ctx, CK_GPU_REG_INVAL, CK_GPU_INVAL_ALL_VA | CK_GPU_INVAL_TRIGGER); |
| 341 | #ifdef CK_GPU_MUTANT_NO_INVAL_POLL |
| 342 | return CK_GMMU_OK; |
| 343 | #endif |
| 344 | for (uint32_t i = 0; i < poll_budget; i++) { |
| 345 | if (!(r->rd(r->ctx, CK_GPU_REG_INVAL) & CK_GPU_INVAL_TRIGGER)) |
| 346 | return CK_GMMU_OK; |
| 347 | } |
| 348 | #ifndef CK_GPU_MUTANT_NO_TIMEOUT_LATCH |
| 349 | as->dead = 1; |
| 350 | #endif |
| 351 | return CK_GMMU_ETIMEDOUT; |
| 352 | } |