interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tests / test_gpu_sim.c

558 lines · 30826 bytes Raw Blame History
1/* test_gpu_sim.c -- cut D host tests: GB10 GPU page tables, allocator and
2 * channel over the simulated device in gpu/gpu_sim.c. Proves the logic and
3 * the encodings against the driver-source layouts, NOT the chip. Run under
4 * ASan/UBSan and with each CK_GPU_MUTANT_* (make gpu-sim-mutants), where
5 * every mutant must make this test FAIL. */
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include "ck_test.h"
10#include "gmmu.h"
11#include "gpu_mem.h"
12#include "gpu_chan.h"
13#include "gpu_sim.h"
14
15#define MEM_BYTES (8u << 20) /* 8 MiB simulated unified memory */
16#define PA_BASE 0x80000000ull
17#define POOL_PA PA_BASE /* 64 page-table pages */
18#define DATA_PA (PA_BASE + 64ull * 4096u) /* data pages after the pool */
19#define VA_BASE 0x0000000200000000ull /* 8 GiB, below 2^40 */
20
21static uint8_t *mem;
22static struct ck_gsim sim;
23static struct ck_gpu_regs regs;
24static struct ck_gmmu_as as;
25static struct ck_gmem gm;
26
27static void fresh(uint32_t pt_pages)
28{
29 memset(mem, 0xA5, MEM_BYTES); /* garbage: gmmu must zero what it uses */
30 ck_gsim_init(&sim, mem, PA_BASE, MEM_BYTES);
31 regs = ck_gsim_regs(&sim);
32 CHECK(ck_gmmu_as_init(&as, &sim.mem, POOL_PA, pt_pages) == CK_GMMU_OK);
33}
34
35static uint32_t gread(uint64_t va, uint32_t *v) { return ck_gsim_gpu_read32(&sim, as.root_pa, va, v); }
36static uint32_t gwrite(uint64_t va, uint32_t v) { return ck_gsim_gpu_write32(&sim, as.root_pa, va, v); }
37
38/* 1. Entry encodings against hand-computed dev_mmu.h bit positions. */
39static void test_encodings(void)
40{
41 /* PTE: VALID 0, APERTURE 2:1 = SYS_COH(2), PCF 7:3 = 1, KIND 11:8 = 0, ADDR 51:12. */
42 CHECK(ck_gmmu_pte(0x123456789000ull, CK_GMMU_PTE_AP_SYS_COH, CK_GMMU_PCF_RW_UNCACHED, 0) ==
43 (0x123456789000ull | (1u << 3) | (2u << 1) | 1u));
44 CHECK(ck_gmmu_pte(0xFFF0000000001FFFull, 3, 0x1F, 0xF) == (0x0000000000001000ull | 0xFFFull)); /* masks */
45 /* PDE: IS_PTE 0 = 0, APERTURE 2:1, PCF 5:3 = 2 (cached, no ATS), ADDR 51:12. */
46 CHECK(ck_gmmu_pde(0xABCDE000ull, CK_GMMU_PDE_AP_SYS_COH) == (0xABCDE000ull | (2u << 3) | (2u << 1)));
47 CHECK((ck_gmmu_pde(0x1000, CK_GMMU_PDE_AP_SYS_COH) & 1u) == 0);
48 /* Dual PDE: small half lands in bits 66:65 (aperture), 69:67 (PCF), 115:76 (addr >> 12). */
49 uint64_t d[2];
50 ck_gmmu_dual_pde(d, 0, CK_GMMU_PDE_AP_INVALID, 0x77777000ull, CK_GMMU_PDE_AP_SYS_COH);
51 CHECK(d[0] == 0);
52 CHECK(d[1] == (0x77777000ull | (2u << 3) | (2u << 1)));
53 ck_gmmu_dual_pde(d, 0x55555500ull, CK_GMMU_PDE_AP_SYS_NC, 0, CK_GMMU_PDE_AP_INVALID);
54 CHECK(d[0] == (0x55555500ull | (2u << 3) | (3u << 1))); /* ADDRESS_BIG 51:8, shift 8 */
55 CHECK(d[1] == 0);
56 /* Work-submit token: RUNLIST_ID 22:16, VECTOR 11:0. */
57 CHECK(ck_gpu_submit_token(3, 0x2A) == 0x0003002Au);
58 CHECK(ck_gpu_submit_token(0x7E, 0xFFF) == 0x007E0FFFu);
59 /* Runlist USERD pointer: LO 31:8 of word 0, HI_HW 19:0 of word 1. */
60 uint32_t rl[2] = {0, 0};
61 CHECK(ck_gpu_rl_userd(rl, 0x000ABCDE12345600ull) == CK_GCHAN_OK);
62 CHECK(rl[0] == 0x12345600u && rl[1] == 0xABCDEu);
63 CHECK(ck_gpu_rl_userd(rl, 0x1000000000080ull) == CK_GCHAN_EINVAL); /* not 256-byte aligned */
64 CHECK(ck_gpu_rl_userd(rl, 1ull << 52) == CK_GCHAN_EINVAL); /* does not fit 20+32 bits */
65 /* Carried encoders, bit-identical to physics nvrm_mthd / nvrm_gp_entry. */
66 CHECK(ck_gpu_mthd(1, 0x0100, 1) == ((2u << 28) | (1u << 16) | (1u << 13) | 0x40u));
67 CHECK(ck_gpu_gp_entry(0x12345678ull, 10) == (0x12345678ull | (10ull << 42) | (1ull << 41)));
68 /* clc96f view of the same entry: GET 31:2 / GET_HI 7:0 / LENGTH 30:10 of word 1. */
69 uint64_t e = ck_gpu_gp_entry(0xAB12345674ull, 0x1FFFFF);
70 CHECK((((uint32_t)e) & ~3u) == 0x12345674u);
71 CHECK(((uint32_t)(e >> 32) & 0xFFu) == 0xABu);
72 CHECK((((uint32_t)(e >> 32)) >> 10 & 0x1FFFFFu) == 0x1FFFFFu);
73 /* LEVEL 9:9 of word 1 is set (SUBROUTINE): what physics emits on the chip today. */
74 CHECK(((uint32_t)(e >> 32) >> 9 & 1u) == 1u);
75}
76
77/* 2. Map, walk through the simulator, unmap, refusals. */
78static void test_map_unmap(void)
79{
80 fresh(64);
81 uint64_t va = 0x0100000000000000ull + 0x5000; /* bit 56 set: PD4 entry 1 */
82 uint64_t pa = DATA_PA + 0x3000;
83 CHECK(ck_gmmu_map(&as, va, pa, 0x2000, CK_GMMU_SZ_4K, CK_GMMU_PTE_AP_SYS_COH, CK_GMMU_PCF_RW_CACHED) == CK_GMMU_OK);
84 uint64_t wpa = 0, page = 0;
85 uint32_t lo = 0;
86 CHECK(ck_gsim_walk(&sim, as.root_pa, va + 0x1234, 0, &wpa, &page, &lo) == 0);
87 CHECK(wpa == pa + 0x1234 && page == CK_GMMU_SZ_4K);
88 CHECK(gwrite(va + 0x10, 0xC0FFEE01u) == 0);
89 CHECK(memcmp(mem + (pa - PA_BASE) + 0x10, "\x01\xEE\xFF\xC0", 4) == 0); /* landed at the PA */
90 uint32_t v = 0;
91 CHECK(gread(va + 0x10, &v) == 0 && v == 0xC0FFEE01u);
92 /* Unmapped neighbour faults and the access does not touch memory. */
93 uint32_t nf = sim.nfaults;
94 CHECK(gread(va + 0x2000, &v) == CK_GSIM_F_UNMAPPED);
95 CHECK(sim.nfaults == nf + 1 && sim.faults[nf].va == va + 0x2000);
96 /* Refusals leave everything unchanged. */
97 CHECK(ck_gmmu_map(&as, va + 0x1000, pa, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EBUSY); /* overlap */
98 CHECK(ck_gmmu_map(&as, va + 0x800, pa, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EALIGN); /* VA */
99 CHECK(ck_gmmu_map(&as, va + 0x4000, pa + 1, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EALIGN); /* PA */
100 CHECK(ck_gmmu_map(&as, va + 0x4000, pa, 0x1800, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EALIGN); /* size */
101 CHECK(ck_gmmu_map(&as, va + 0x4000, pa, 0, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EINVAL); /* empty */
102 CHECK(ck_gmmu_map(&as, va + 0x4000, pa, 0x1000, 0x2000, 2, 0) == CK_GMMU_EINVAL); /* page size */
103 CHECK(ck_gmmu_map(&as, 1ull << 57, pa, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* VA bits */
104 CHECK(ck_gmmu_map(&as, (1ull << 57) - 0x1000, pa, 0x2000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* VA end */
105 CHECK(ck_gmmu_map(&as, va + 0x4000, PA_BASE - 0x1000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* PA below */
106 CHECK(ck_gmmu_map(&as, va + 0x4000, PA_BASE + MEM_BYTES, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* PA above */
107 CHECK(ck_gmmu_map(&as, va + 0x4000, PA_BASE + MEM_BYTES - 0x1000, 0x2000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* PA end */
108 CHECK(ck_gmmu_map(&as, va + 0x4000, pa, 0x1000, CK_GMMU_SZ_4K, 4, 0) == CK_GMMU_EINVAL); /* aperture */
109 CHECK(ck_gmmu_map(&as, va + 0x4000, pa, 0x1000, CK_GMMU_SZ_4K, 2, 0x20) == CK_GMMU_EINVAL); /* PCF */
110 /* A 64 KiB page in a 2 MiB region that already has a 4 KiB table: refused. */
111 CHECK(ck_gmmu_map(&as, va & ~0xFFFFull, DATA_PA + 0x10000, 0x10000, CK_GMMU_SZ_64K, 2, 0) == CK_GMMU_EBUSY);
112 /* A 2 MiB page over that region: refused. */
113 CHECK(ck_gmmu_map(&as, va & ~0x1FFFFFull, PA_BASE + 0x200000, CK_GMMU_SZ_2M, CK_GMMU_SZ_2M, 2, 0) == CK_GMMU_EBUSY);
114 CHECK(gread(va + 0x10, &v) == 0 && v == 0xC0FFEE01u);
115 /* Unmap: partial or wrong-size requests refused with no change. */
116 CHECK(ck_gmmu_unmap(&as, va, 0x3000, CK_GMMU_SZ_4K) == CK_GMMU_ENOENT);
117 CHECK(ck_gmmu_unmap(&as, va & ~0xFFFFull, 0x10000, CK_GMMU_SZ_64K) == CK_GMMU_ENOENT);
118 CHECK(ck_gsim_walk(&sim, as.root_pa, va + 0x1000, 0, &wpa, &page, &lo) == 0);
119 CHECK(ck_gmmu_unmap(&as, va, 0x2000, CK_GMMU_SZ_4K) == CK_GMMU_OK);
120 CHECK(ck_gsim_walk(&sim, as.root_pa, va, 0, &wpa, &page, &lo) == CK_GSIM_F_UNMAPPED);
121 CHECK(ck_gmmu_unmap(&as, va, 0x1000, CK_GMMU_SZ_4K) == CK_GMMU_ENOENT); /* double unmap */
122}
123
124/* 3. Page sizes 64 KiB and 2 MiB, and the read-only PCF. */
125static void test_page_sizes(void)
126{
127 fresh(64);
128 uint64_t v64 = 0x40000000ull, v2m = 0x80000000ull;
129 CHECK(ck_gmmu_map(&as, v64, DATA_PA + 0x10000 - (DATA_PA & 0xFFFF), 0x20000, CK_GMMU_SZ_64K, 2,
130 CK_GMMU_PCF_RO_CACHED) == CK_GMMU_OK);
131 uint64_t pa2m = (PA_BASE + 0x200000ull);
132 CHECK(ck_gmmu_map(&as, v2m, pa2m, CK_GMMU_SZ_2M, CK_GMMU_SZ_2M, 2, CK_GMMU_PCF_RW_CACHED) == CK_GMMU_OK);
133 uint64_t wpa = 0, page = 0;
134 uint32_t lo = 0, v = 0;
135 CHECK(ck_gsim_walk(&sim, as.root_pa, v64 + 0x1ABCD, 0, &wpa, &page, &lo) == 0);
136 CHECK(page == CK_GMMU_SZ_64K && wpa == DATA_PA + 0x10000 - (DATA_PA & 0xFFFF) + 0x1ABCD);
137 CHECK(ck_gsim_walk(&sim, as.root_pa, v2m + 0x1F0004, 0, &wpa, &page, &lo) == 0);
138 CHECK(page == CK_GMMU_SZ_2M && wpa == pa2m + 0x1F0004);
139 CHECK(gwrite(v64 + 8, 1) == CK_GSIM_F_RO_WRITE); /* read-only page */
140 CHECK(gread(v64 + 8, &v) == 0);
141 CHECK(gwrite(v2m + 8, 7) == 0);
142 /* Small page inside the 2 MiB page: refused. */
143 CHECK(ck_gmmu_map(&as, v2m + 0x1000, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EBUSY);
144 CHECK(ck_gmmu_map(&as, v2m, pa2m, CK_GMMU_SZ_2M, CK_GMMU_SZ_2M, 2, 0) == CK_GMMU_EBUSY);
145 CHECK(ck_gmmu_unmap(&as, v2m, CK_GMMU_SZ_2M, CK_GMMU_SZ_4K) == CK_GMMU_ENOENT);
146 CHECK(ck_gmmu_unmap(&as, v2m, CK_GMMU_SZ_2M, CK_GMMU_SZ_2M) == CK_GMMU_OK);
147 CHECK(ck_gmmu_unmap(&as, v64, 0x20000, CK_GMMU_SZ_64K) == CK_GMMU_OK);
148}
149
150/* 4. Page-table pool exhaustion: the map is refused whole, nothing written. */
151static void test_pool_exhaustion(void)
152{
153 fresh(5); /* root + 4: exactly one 4K path (PD3, PD2, PD1, PD0) and no PT */
154 uint32_t before = 0;
155 for (uint32_t i = 0; i < as.pt_count; i++)
156 before += as.pt_used[i];
157 CHECK(ck_gmmu_map(&as, 0x1000, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ENOMEM);
158 uint32_t after = 0;
159 for (uint32_t i = 0; i < as.pt_count; i++)
160 after += as.pt_used[i];
161 CHECK(before == after);
162 uint64_t wpa, page;
163 uint32_t lo;
164 CHECK(ck_gsim_walk(&sim, as.root_pa, 0x1000, 0, &wpa, &page, &lo) == CK_GSIM_F_UNMAPPED);
165 /* 2 MiB needs no PT: the same pool is enough. */
166 CHECK(ck_gmmu_map(&as, 0x200000, PA_BASE + 0x200000, CK_GMMU_SZ_2M, CK_GMMU_SZ_2M, 2, 0) == CK_GMMU_OK);
167 /* Exact count across a 2 MiB boundary: two PTs needed, pool has none left. */
168 fresh(7); /* root + PD3 + PD2 + PD1 + PD0 + 2 PT */
169 CHECK(ck_gmmu_map(&as, 0x1FF000, DATA_PA, 0x2000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK);
170 fresh(6);
171 CHECK(ck_gmmu_map(&as, 0x1FF000, DATA_PA, 0x2000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ENOMEM);
172 CHECK(ck_gmmu_as_init(&as, &sim.mem, POOL_PA + 1, 8) == CK_GMMU_EINVAL);
173 CHECK(ck_gmmu_as_init(&as, &sim.mem, PA_BASE + MEM_BYTES - 4096, 2) == CK_GMMU_ERANGE);
174 CHECK(ck_gmmu_as_init(&as, &sim.mem, POOL_PA, CK_GMMU_MAX_PT + 1) == CK_GMMU_EINVAL);
175}
176
177/* 5. The invalidate sequence, the stale TLB without it, and the timeout latch. */
178static void test_invalidate(void)
179{
180 fresh(64);
181 uint64_t va = 0x10000;
182 CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK);
183 uint32_t v = 0;
184 CHECK(gwrite(va, 0x11) == 0); /* fills the TLB */
185 CHECK(ck_gmmu_unmap(&as, va, 0x1000, CK_GMMU_SZ_4K) == CK_GMMU_OK);
186 CHECK(gread(va, &v) == 0 && v == 0x11); /* stale: no invalidate yet */
187 /* Prove it is the old translation: change the bytes at the old PA from the CPU. */
188 *(volatile uint32_t *)(mem + (DATA_PA - PA_BASE)) = 0x5E5E5E5Eu;
189 CHECK(gread(va, &v) == 0 && v == 0x5E5E5E5Eu);
190 sim.nlog = 0;
191 CHECK(ck_gmmu_invalidate(&as, &regs, 10) == CK_GMMU_OK);
192 /* Register order: PDB, UPPER_PDB, INVALIDATE(ALL_VA|TRIGGER), then polls. */
193 CHECK(sim.nlog >= 4);
194 uint64_t pdb = as.root_pa >> 12;
195 CHECK(sim.log[0].write && sim.log[0].off == 0x00B830A0u && sim.log[0].val == ((uint32_t)((pdb & 0xFFFFFFF) << 4) | 2u));
196 CHECK(sim.log[1].write && sim.log[1].off == 0x00B830A4u && sim.log[1].val == (uint32_t)(pdb >> 28));
197 CHECK(sim.log[2].write && sim.log[2].off == 0x00B830B0u && sim.log[2].val == 0x80000001u);
198 CHECK(!sim.log[3].write && sim.log[3].off == 0x00B830B0u);
199 CHECK(sim.inval_done == 1);
200 CHECK(gread(va, &v) == CK_GSIM_F_UNMAPPED); /* translation gone */
201 /* Budget smaller than the device latency: timeout, then the AS refuses everything. */
202 fresh(64);
203 sim.inval_latency = 5;
204 CHECK(ck_gmmu_invalidate(&as, &regs, 3) == CK_GMMU_ETIMEDOUT);
205 CHECK(as.dead == 1);
206 CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EDEAD);
207 CHECK(ck_gmmu_unmap(&as, va, 0x1000, CK_GMMU_SZ_4K) == CK_GMMU_EDEAD);
208 CHECK(ck_gmmu_invalidate(&as, &regs, 100) == CK_GMMU_EDEAD);
209 /* Exact poll budget: latency 3 needs 4 reads (3 with TRIGGER set, then clear). */
210 fresh(64);
211 CHECK(ck_gmmu_invalidate(&as, &regs, 3) == CK_GMMU_ETIMEDOUT);
212 fresh(64);
213 CHECK(ck_gmmu_invalidate(&as, &regs, 4) == CK_GMMU_OK);
214 /* Invalidate-all drops every PDB; one-PDB leaves other PDBs cached. */
215 CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK);
216 CHECK(gread(va, &v) == 0);
217 sim.tlb[0].pdb ^= 0x1000; /* pretend the entry belongs to another address space */
218 CHECK(ck_gmmu_invalidate(&as, &regs, 4) == CK_GMMU_OK && sim.tlb[0].valid);
219 regs.wr(regs.ctx, CK_GPU_REG_INVAL, CK_GPU_INVAL_ALL_PDB | CK_GPU_INVAL_ALL_VA | CK_GPU_INVAL_TRIGGER);
220 for (int i = 0; i < 4; i++)
221 (void)regs.rd(regs.ctx, CK_GPU_REG_INVAL);
222 CHECK(!sim.tlb[0].valid);
223}
224
225/* 6. Allocator: distinct addresses, cache policy, reclaim, bounds, quarantine. */
226static void test_allocator(void)
227{
228 fresh(64);
229 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 16, VA_BASE) == CK_GMEM_OK);
230 CHECK(ck_gmem_init(&gm, &as, &regs, 10, POOL_PA, 16, VA_BASE) == CK_GMEM_EINVAL); /* overlaps the PT pool */
231 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 16, VA_BASE + 0x1000) == CK_GMEM_EINVAL); /* VA alignment */
232 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 16, VA_BASE) == CK_GMEM_OK);
233 struct ck_gmem_buf *a = 0, *b = 0, *flag = 0, *x = 0;
234 CHECK(ck_gmem_alloc(&gm, 5000, CK_GMEM_CACHED, 0, &a) == CK_GMEM_OK);
235 CHECK(a && a->size == 8192 && a->pa == DATA_PA && a->gpu_va == VA_BASE);
236 CHECK((uint64_t)(uintptr_t)a->cpu != a->gpu_va && a->cpu == mem + (DATA_PA - PA_BASE));
237 {
238 int dirty = 0;
239 for (uint64_t i = 0; i < a->size; i++)
240 dirty |= a->cpu[i];
241 CHECK(!dirty); /* alloc zeros, including a window poisoned with 0xA5 */
242 }
243 /* Host-polled must be uncached: refused when asked cached. */
244 CHECK(ck_gmem_alloc(&gm, 4, CK_GMEM_CACHED, CK_GMEM_HOST_POLLED, &flag) == CK_GMEM_EINVAL);
245 CHECK(ck_gmem_alloc(&gm, 4, CK_GMEM_UNCACHED, CK_GMEM_HOST_POLLED, &flag) == CK_GMEM_OK);
246 CHECK(ck_gmem_alloc(&gm, 4, 7, 0, &x) == CK_GMEM_EINVAL);
247 CHECK(ck_gmem_alloc(&gm, 4, CK_GMEM_CACHED, 0x80, &x) == CK_GMEM_EINVAL);
248 CHECK(ck_gmem_alloc(&gm, 0, CK_GMEM_CACHED, 0, &x) == CK_GMEM_EINVAL);
249 uint64_t wpa, page;
250 uint32_t lo = 0;
251 CHECK(ck_gsim_walk(&sim, as.root_pa, flag->gpu_va, 0, &wpa, &page, &lo) == 0);
252 CHECK(((lo >> 3) & 0x1F) == CK_GMMU_PCF_RW_UNCACHED && ((lo >> 1) & 3) == CK_GMMU_PTE_AP_SYS_COH);
253 CHECK(ck_gsim_walk(&sim, as.root_pa, a->gpu_va + 4096, 0, &wpa, &page, &lo) == 0);
254 CHECK(((lo >> 3) & 0x1F) == CK_GMMU_PCF_RW_CACHED);
255 /* GPU writes the flag through its VA; the CPU sees it through its own pointer. */
256 CHECK(gwrite(flag->gpu_va, 0xD0E) == 0);
257 CHECK(*(volatile uint32_t *)flag->cpu == 0xD0E);
258 /* Bounds: 16 pages total, 3 used. 14 more is too many; 13 fits exactly. */
259 CHECK(ck_gmem_alloc(&gm, 14 * 4096, CK_GMEM_CACHED, 0, &b) == CK_GMEM_ENOMEM);
260 CHECK(ck_gmem_alloc(&gm, 13 * 4096, CK_GMEM_CACHED, 0, &b) == CK_GMEM_OK);
261 CHECK(ck_gmem_count(&gm, CK_GMEM_FREE) == 0);
262 /* Free reclaims after a completed invalidate; GPU access then faults. */
263 uint32_t inv = sim.inval_done;
264 CHECK(gwrite(a->gpu_va, 1) == 0);
265 uint64_t a_va = a->gpu_va;
266 CHECK(ck_gmem_free(&gm, a) == CK_GMEM_OK);
267 CHECK(sim.inval_done == inv + 1);
268 CHECK(gwrite(a_va, 2) == CK_GSIM_F_UNMAPPED);
269 CHECK(ck_gmem_free(&gm, a) == CK_GMEM_ESTATE); /* double free */
270 CHECK(ck_gmem_free(&gm, (struct ck_gmem_buf *)((uint8_t *)&gm.buf[0] + 1)) == CK_GMEM_ESTATE); /* not a slot */
271 CHECK(ck_gmem_count(&gm, CK_GMEM_FREE) == 2);
272 CHECK(ck_gmem_alloc(&gm, 8192, CK_GMEM_CACHED, 0, &a) == CK_GMEM_OK && a->pa == DATA_PA); /* reused */
273 {
274 int dirty = 0;
275 for (uint64_t i = 0; i < a->size; i++)
276 dirty |= a->cpu[i];
277 CHECK(!dirty); /* reused pages do not keep the previous buffer */
278 }
279 /* Invalidate that never completes: the buffer is quarantined, not reused. */
280 sim.inval_stuck = 1;
281 CHECK(ck_gmem_free(&gm, b) == CK_GMEM_ETIMEDOUT);
282 CHECK(ck_gmem_count(&gm, CK_GMEM_QUARANTINED) == 13);
283 sim.inval_stuck = 0;
284 CHECK(ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, 0, &x) != CK_GMEM_OK); /* AS dead + no free pages */
285 CHECK(ck_gmem_count(&gm, CK_GMEM_QUARANTINED) == 13);
286 /* Slot table bound. */
287 fresh(64);
288 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 128, VA_BASE) == CK_GMEM_OK);
289 int ok = 1;
290 for (uint32_t i = 0; i < CK_GMEM_MAX_BUF; i++)
291 ok &= ck_gmem_alloc(&gm, 1, CK_GMEM_CACHED, 0, &x) == CK_GMEM_OK;
292 CHECK(ok);
293 CHECK(ck_gmem_alloc(&gm, 1, CK_GMEM_CACHED, 0, &x) == CK_GMEM_ENOMEM);
294}
295
296/* 7. Channel: layout, token, submit through the GMMU, refusals, teardown. */
297static void test_channel(void)
298{
299 fresh(64);
300 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 64, VA_BASE) == CK_GMEM_OK);
301 struct ck_gpu_chan_ops ops = ck_gsim_chan_ops();
302 struct ck_gpu_chan ch;
303 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 0x1000, 0, 8) == CK_GCHAN_EINVAL); /* chid > 11 bits */
304 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 1, 0x7F, 8) == CK_GCHAN_EINVAL); /* INVALID_RUNLIST */
305 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 1, 0, 6) == CK_GCHAN_EINVAL); /* not a power of 2 */
306 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 1, 0, 1) == CK_GCHAN_EINVAL);
307 CHECK(ck_gmem_count(&gm, CK_GMEM_LIVE) == 0);
308 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 5, 2, 8) == CK_GCHAN_OK);
309 if (!ch.open)
310 return; /* the rest needs a live channel */
311 CHECK(ch.d.token == 0x00020005u);
312 CHECK(ch.d.rl_entry[0] == ((uint32_t)ch.userd->pa & 0xFFFFFF00u) && ch.d.rl_entry[1] == (uint32_t)(ch.userd->pa >> 32));
313 CHECK(ch.d.pdb == as.root_pa && ch.d.gpfifo_va == ch.gpfifo->gpu_va);
314 CHECK(ch.gpfifo->cache == CK_GMEM_UNCACHED && ch.userd->cache == CK_GMEM_UNCACHED);
315 /* Instance block word 128/129 as the driver writes it: TARGET SYS_COH, BIG_PAGE 64K, NEW_PT_FORMAT 0. */
316 const uint32_t *ramin = (const uint32_t *)ch.inst->cpu;
317 CHECK(ramin[128] == (((uint32_t)as.root_pa & 0xFFFFF000u) | (1u << 11) | 2u));
318 CHECK(ramin[129] == (uint32_t)(as.root_pa >> 32));
319 struct ck_gpu_chan dup;
320 CHECK(ck_gpu_chan_create(&dup, &gm, &regs, &ops, &sim, 5, 2, 8) == CK_GCHAN_EBIND); /* same chid live */
321 CHECK(ck_gmem_count(&gm, CK_GMEM_LIVE) == 3); /* dup's memory returned */
322
323 struct ck_gmem_buf *pb = 0, *sem = 0;
324 CHECK(ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, CK_GMEM_GPU_RO, &pb) == CK_GMEM_OK);
325 CHECK(ck_gmem_alloc(&gm, 16, CK_GMEM_UNCACHED, CK_GMEM_HOST_POLLED, &sem) == CK_GMEM_OK);
326 uint32_t *w = (uint32_t *)pb->cpu;
327 w[0] = ck_gpu_mthd(0, 0x5c, 2);
328 w[1] = (uint32_t)sem->gpu_va;
329 w[2] = (uint32_t)(sem->gpu_va >> 32);
330 sim.nlog = 0;
331 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 3) == CK_GCHAN_OK);
332 /* Ring entry (CPU view), GPPut in USERD at 0x8c, doorbell write with the token. */
333 uint64_t ent = ((uint64_t *)ch.gpfifo->cpu)[0];
334 CHECK(ent == ck_gpu_gp_entry(pb->gpu_va, 3));
335 CHECK(*(uint32_t *)(ch.userd->cpu + 0x8c) == 1);
336 CHECK(sim.nlog == 1 && sim.log[0].write && sim.log[0].off == 0x00BB0090u && sim.log[0].val == 0x00020005u);
337 const struct ck_gsim_chan *sc = ck_gsim_chan(&sim, 5);
338 CHECK(sc && sc->gp_get == 1 && sc->words_read == 3 && !sc->faulted);
339 /* Refusals. */
340 CHECK(ck_gpu_chan_submit(&ch, pb, 2, 1) == CK_GCHAN_EINVAL); /* misaligned */
341 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 0) == CK_GCHAN_EINVAL); /* empty */
342 CHECK(ck_gpu_chan_submit(&ch, pb, 4092, 2) == CK_GCHAN_EINVAL); /* past the buffer */
343 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 0x200000) == CK_GCHAN_EINVAL); /* LENGTH is 21 bits */
344 struct ck_gmem_buf fake = *pb;
345 CHECK(ck_gpu_chan_submit(&ch, &fake, 0, 1) == CK_GCHAN_EINVAL); /* not an allocator buffer */
346 /* Ring full: entries - 1 outstanding at most. */
347 int okc = 1;
348 for (uint32_t i = 1; i < 7; i++)
349 okc &= ck_gpu_chan_submit(&ch, pb, 0, 1) == CK_GCHAN_OK;
350 CHECK(okc);
351 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 1) == CK_GCHAN_EFULL);
352 ck_gpu_chan_retire(&ch, 100); /* beyond put: ignored */
353 CHECK(ch.retired == 0);
354 ck_gpu_chan_retire(&ch, 7);
355 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 1) == CK_GCHAN_OK); /* wraps the ring */
356 CHECK(sc->gp_get == 0 && !sc->faulted);
357 /* A pushbuffer that was freed: the GPU faults on it and the channel stops. */
358 struct ck_gmem_buf *gone = 0;
359 CHECK(ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, 0, &gone) == CK_GMEM_OK);
360 ck_gpu_chan_retire(&ch, 8);
361 uint64_t gone_va = gone->gpu_va;
362 /* Queue it by hand after freeing (submit itself would refuse a dead buffer). */
363 CHECK(ck_gmem_free(&gm, gone) == CK_GMEM_OK);
364 CHECK(ck_gpu_chan_submit(&ch, gone, 0, 1) == CK_GCHAN_EINVAL);
365 ((uint64_t *)ch.gpfifo->cpu)[ch.put % 8] = ck_gpu_gp_entry(gone_va, 1);
366 ch.put++;
367 *(uint32_t *)(ch.userd->cpu + 0x8c) = ch.put % 8;
368 uint32_t nf = sim.nfaults;
369 regs.wr(regs.ctx, CK_GPU_REG_DOORBELL, ch.d.token);
370 CHECK(sc->faulted && sim.nfaults == nf + 1 && sim.faults[nf].va == gone_va);
371 /* Unknown token is counted, not acted on. */
372 regs.wr(regs.ctx, CK_GPU_REG_DOORBELL, 0x00010001u);
373 CHECK(sim.doorbell_unknown == 1);
374 /* Clean teardown: everything returns to FREE except pb/sem (caller's). */
375 CHECK(ck_gpu_chan_teardown(&ch, 4, 0, 0) == CK_GCHAN_OK);
376 CHECK(ck_gmem_count(&gm, CK_GMEM_QUARANTINED) == 0);
377 CHECK(ck_gmem_count(&gm, CK_GMEM_LIVE) == 2);
378 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 1) == CK_GCHAN_ESTATE);
379 CHECK(ck_gpu_chan_teardown(&ch, 4, 0, 0) == CK_GCHAN_ESTATE);
380 /* The channel id is free again: reopen, then a hung teardown quarantines. */
381 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 5, 2, 8) == CK_GCHAN_OK);
382 sim.hang = 1;
383 struct ck_gmem_buf *extra[2] = {pb, sem};
384 uint64_t pb_pa = pb->pa, sem_pa = sem->pa; /* slots are reused after quarantine; the pages are not */
385 uint32_t polls = sim.idle_polls;
386 CHECK(ck_gpu_chan_teardown(&ch, 6, extra, 2) == CK_GCHAN_ETIMEDOUT);
387 CHECK(sim.idle_polls == polls + 6); /* bounded wait */
388 CHECK(ck_gmem_count(&gm, CK_GMEM_LIVE) == 0);
389 CHECK(ck_gmem_count(&gm, CK_GMEM_QUARANTINED) == 3 + 1 + 1); /* ring 1 page, USERD 1, inst 1, pb 1, sem 1 */
390 uint32_t freep = ck_gmem_count(&gm, CK_GMEM_FREE);
391 struct ck_gmem_buf *y = 0;
392 int never = 1;
393 while (ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, 0, &y) == CK_GMEM_OK)
394 never &= gm.page_state[(y->pa - DATA_PA) / 4096] == CK_GMEM_LIVE && y->pa != pb_pa && y->pa != sem_pa;
395 CHECK(never);
396 CHECK(ck_gmem_count(&gm, CK_GMEM_QUARANTINED) == 5 && ck_gmem_count(&gm, CK_GMEM_LIVE) <= freep);
397}
398
399/* 8. Reopen loop: repeated create/submit/teardown leaks nothing. */
400static void test_reopen(void)
401{
402 fresh(64);
403 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 32, VA_BASE) == CK_GMEM_OK);
404 struct ck_gpu_chan_ops ops = ck_gsim_chan_ops();
405 struct ck_gpu_chan ch;
406 struct ck_gmem_buf *pb = 0;
407 CHECK(ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, CK_GMEM_GPU_RO, &pb) == CK_GMEM_OK);
408 int ok = 1;
409 for (uint32_t i = 0; i < 200; i++) {
410 ok &= ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, i & 0xFFF, 1, 4) == CK_GCHAN_OK;
411 ok &= ck_gpu_chan_submit(&ch, pb, 0, 4) == CK_GCHAN_OK;
412 ok &= ck_gpu_chan_teardown(&ch, 2, 0, 0) == CK_GCHAN_OK;
413 }
414 CHECK(ok);
415 CHECK(ck_gmem_count(&gm, CK_GMEM_LIVE) == 1 && ck_gmem_count(&gm, CK_GMEM_FREE) == 31);
416 CHECK(sim.nfaults == 0 && sim.log_dropped > 0); /* the log is bounded too */
417}
418
419/* 9. Levels with a PTE the mapper never writes: PD1 (512 MiB) and PD2 (256 GiB)
420 * decode as huge pages (kgmmuFmtInitLevels_GH10X / _GB10X bPageTable), PD3 and
421 * PD4 have no PTE form (BAD_PDE). Entries are planted by hand. */
422static uint64_t table_at(uint64_t tbl, uint32_t idx)
423{
424 uint64_t e;
425 memcpy(&e, mem + (tbl - PA_BASE) + idx * 8u, 8);
426 return e & 0x000FFFFFFFFFF000ull;
427}
428
429static void plant(uint64_t tbl, uint32_t idx, uint64_t val)
430{
431 memcpy(mem + (tbl - PA_BASE) + idx * 8u, &val, 8);
432}
433
434static void test_huge_levels(void)
435{
436 fresh(64);
437 CHECK(ck_gmmu_map(&as, 0x1000, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK);
438 uint64_t pd3 = table_at(as.root_pa, 0), pd2 = table_at(pd3, 0), pd1 = table_at(pd2, 0);
439 uint64_t wpa = 0, page = 0;
440 uint32_t lo = 0;
441 plant(pd1, 1, ck_gmmu_pte(PA_BASE, CK_GMMU_PTE_AP_SYS_COH, 0, 0)); /* VA [512 MiB, 1 GiB) */
442 CHECK(ck_gsim_walk(&sim, as.root_pa, (1ull << 29) + 0x4321, 0, &wpa, &page, &lo) == 0);
443 CHECK(page == (1ull << 29) && wpa == PA_BASE + 0x4321);
444 plant(pd2, 1, ck_gmmu_pte(0, CK_GMMU_PTE_AP_SYS_COH, 0, 0)); /* VA [256 GiB, 512 GiB) */
445 CHECK(ck_gsim_walk(&sim, as.root_pa, (1ull << 38) + PA_BASE + 8, 0, &wpa, &page, &lo) == 0);
446 CHECK(page == (1ull << 38) && wpa == PA_BASE + 8);
447 plant(pd3, 1, ck_gmmu_pte(PA_BASE, CK_GMMU_PTE_AP_SYS_COH, 0, 0));
448 CHECK(ck_gsim_walk(&sim, as.root_pa, 1ull << 47, 0, &wpa, &page, &lo) == CK_GSIM_F_BAD_PDE);
449 plant(as.root_pa, 1, ck_gmmu_pte(PA_BASE, CK_GMMU_PTE_AP_SYS_COH, 0, 0));
450 CHECK(ck_gsim_walk(&sim, as.root_pa, 1ull << 56, 0, &wpa, &page, &lo) == CK_GSIM_F_BAD_PDE);
451 /* The mapper refuses to map under a huge PTE instead of overwriting it. */
452 CHECK(ck_gmmu_map(&as, 1ull << 29, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EBUSY);
453 CHECK(ck_gmmu_map(&as, 1ull << 38, PA_BASE + 0x200000, CK_GMMU_SZ_2M, CK_GMMU_SZ_2M, 2, 0) == CK_GMMU_EBUSY);
454 /* Video and peer apertures do not exist on GB10: fault, never translate. */
455 plant(pd1, 2, ck_gmmu_pte(PA_BASE, CK_GMMU_PTE_AP_VIDMEM, 0, 0));
456 CHECK(ck_gsim_walk(&sim, as.root_pa, 2ull << 29, 0, &wpa, &page, &lo) == CK_GSIM_F_APERTURE);
457 plant(pd1, 3, ck_gmmu_pde(PA_BASE, CK_GMMU_PDE_AP_VIDMEM));
458 CHECK(ck_gsim_walk(&sim, as.root_pa, 3ull << 29, 0, &wpa, &page, &lo) == CK_GSIM_F_APERTURE);
459}
460
461/* 10. The 40-bit pushbuffer address limit of the GPFIFO entry. */
462static void test_gp_va40(void)
463{
464 struct ck_gpu_chan_ops ops = ck_gsim_chan_ops();
465 struct ck_gpu_chan ch;
466 struct ck_gmem_buf *big = 0, *pb = 0;
467 /* Last page ends exactly at 2^40: allowed. */
468 fresh(64);
469 CHECK(ck_gmem_init(&gm, &as, &regs, 10, DATA_PA, 520, (1ull << 40) - 0x200000) == CK_GMEM_OK);
470 CHECK(ck_gpu_chan_create(&ch, &gm, &regs, &ops, &sim, 9, 0, 4) == CK_GCHAN_OK);
471 CHECK(ck_gmem_alloc(&gm, 0x200000 - 4u * 4096u, CK_GMEM_CACHED, 0, &big) == CK_GMEM_OK);
472 CHECK(ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, CK_GMEM_GPU_RO, &pb) == CK_GMEM_OK);
473 CHECK(pb->gpu_va == (1ull << 40) - 4096);
474 CHECK(ck_gpu_chan_submit(&ch, pb, 0, 1024) == CK_GCHAN_OK);
475 const struct ck_gsim_chan *sc = ck_gsim_chan(&sim, 9);
476 CHECK(sc && sc->words_read == 1024 && !sc->faulted);
477 /* At or beyond 2^40: refused before anything reaches the ring. */
478 struct ck_gmem_buf *hi = 0;
479 CHECK(ck_gmem_alloc(&gm, 4096, CK_GMEM_CACHED, CK_GMEM_GPU_RO, &hi) == CK_GMEM_OK);
480 CHECK(hi->gpu_va == (1ull << 40));
481 uint32_t put = ch.put, bells = sim.doorbells;
482 CHECK(ck_gpu_chan_submit(&ch, hi, 0, 1) == CK_GCHAN_EINVAL);
483 CHECK(ch.put == put && sim.doorbells == bells);
484 CHECK(ck_gpu_chan_teardown(&ch, 2, 0, 0) == CK_GCHAN_OK);
485}
486
487/* Entry slot of `va` at level lv (0 = PD4 root .. 4 = PD0, 16-byte dual
488 * entries), found by following the PDEs gmmu wrote. */
489static uint64_t *pd_slot(uint64_t va, unsigned lv)
490{
491 static const uint8_t lo[5] = {56, 47, 38, 29, 21}, bits[5] = {1, 9, 9, 9, 8};
492 uint64_t tbl = as.root_pa;
493 for (unsigned l = 0;; l++) {
494 uint64_t idx = (va >> lo[l]) & ((1ull << bits[l]) - 1);
495 uint64_t *p = (uint64_t *)(mem + (tbl - PA_BASE) + idx * (l == 4 ? 16u : 8u));
496 if (l == lv)
497 return p;
498 tbl = *p & 0x000FFFFFFFFFF000ull;
499 }
500}
501
502/* 11. #493 N2/N3: a PDE read back from table memory must name a live page
503 * of the pool (else latch EDEAD, touch nothing), and no mapping may cover
504 * the page-table pool itself. */
505static void test_pde_and_pool(void)
506{
507 uint64_t va = VA_BASE, junk = DATA_PA + 0x10000;
508 uint8_t before[4096];
509
510 /* Map path: a PD3 entry redirected into data memory (0xA5 garbage). */
511 fresh(64);
512 CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK);
513 *pd_slot(va, 1) = ck_gmmu_pde(junk, CK_GMMU_PDE_AP_SYS_COH);
514 memcpy(before, mem + (junk - PA_BASE), sizeof before);
515 CHECK(ck_gmmu_map(&as, va + 0x1000, DATA_PA + 0x1000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EDEAD);
516 CHECK(as.dead == 1);
517 CHECK(memcmp(before, mem + (junk - PA_BASE), sizeof before) == 0);
518 CHECK(ck_gmmu_map(&as, va + 0x200000, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EDEAD); /* latched */
519
520 /* Unmap path: the PD0 small-table half redirected into data memory. */
521 fresh(64);
522 CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK);
523 uint64_t nd[2];
524 ck_gmmu_dual_pde(nd, 0, CK_GMMU_PDE_AP_INVALID, junk, CK_GMMU_PDE_AP_SYS_COH);
525 pd_slot(va, 4)[1] = nd[1];
526 memcpy(before, mem + (junk - PA_BASE), sizeof before);
527 CHECK(ck_gmmu_unmap(&as, va, 0x1000, CK_GMMU_SZ_4K) == CK_GMMU_EDEAD);
528 CHECK(as.dead == 1);
529 CHECK(memcmp(before, mem + (junk - PA_BASE), sizeof before) == 0); /* no PTE cleared in data */
530
531 /* Pool overlap: 8 page-table pages at POOL_PA. */
532 fresh(8);
533 CHECK(ck_gmmu_map(&as, va, POOL_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* the root */
534 CHECK(ck_gmmu_map(&as, va, POOL_PA + 0x7000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* last page */
535 CHECK(ck_gmmu_map(&as, va, POOL_PA + 0x6000, 0x4000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* straddles end */
536 CHECK(ck_gmmu_map(&as, va, POOL_PA + 0x8000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK); /* first page after */
537 CHECK(as.dead == 0);
538}
539
540int main(void)
541{
542 mem = aligned_alloc(4096, MEM_BYTES);
543 if (!mem)
544 return 2;
545 test_encodings();
546 test_map_unmap();
547 test_page_sizes();
548 test_pool_exhaustion();
549 test_invalidate();
550 test_allocator();
551 test_channel();
552 test_reopen();
553 test_huge_levels();
554 test_gp_va40();
555 test_pde_and_pool();
556 free(mem);
557 return ck_t_verdict("test_gpu_sim");
558}