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_amd_gpu_ring.c

276 lines · 11382 bytes Raw Blame History
1/* native/kernel/tests/test_amd_gpu_ring.c -- host test of the real GPU ring code.
2 *
3 * Compiles the driver's own gpu/amd_ucode.c (amd_gfx_ring_init, ck_gpu_ring_submit,
4 * ck_gpu_fence_wait, amd_gfx_ring_test) and the DMA pool (amd_dma_pool_take in
5 * gpu/amd_gpu.h). Replaced for the host: the x86 rdtsc/pause (amd_host_shim.h),
6 * the register file (a plain array) and the GPU itself.
7 *
8 * The GPU is a model thread. It watches CP_RB0_WPTR, decodes the PM4 packets in
9 * the ring (type 2 fillers, type 3 headers with a count of body dwords), executes
10 * WRITE_DATA by writing to the physical address, and writes its read pointer back.
11 * It checks the packets against the layout gfx_v8_0.c emits; the test asserts on
12 * what the model executed. Host run only: nothing physical is qualified here. */
13#define AMD_GPU_TEST_HOST 1
14#define _POSIX_C_SOURCE 200809L
15#include <pthread.h>
16#include <sched.h>
17#include <stdarg.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include "amd_gpu.h"
22
23#define POOL_PAGES 1024u /* same size as the driver's DMA pool */
24
25/* ---- stand-ins for the kernel's console (ck.h) -------------------------*/
26void ck_puts(const char *s) { fputs(s, stdout); }
27void ck_printf(const char *fmt, ...)
28{
29 va_list ap;
30 va_start(ap, fmt);
31 vprintf(fmt, ap);
32 va_end(ap);
33}
34
35/* ---- the driver's global state and register file -----------------------*/
36struct amd_gpu_state g_amd_gpu; /* gpu_x86.c is not linked into this test */
37static volatile uint32_t fake_regs[1u << 16];
38
39/* ---- physical pool: the host buffer that stands in for its frames -------*/
40static uint8_t *pool_host;
41static uint64_t pool_pa, pool_bytes;
42
43static int in_pool(uint64_t pa, uint64_t n)
44{
45 return pa >= pool_pa && pa + n <= pool_pa + pool_bytes;
46}
47
48static volatile uint32_t *pa_to_host(uint64_t pa)
49{
50 return in_pool(pa, 4) ? (volatile uint32_t *)(pool_host + (pa - pool_pa)) : NULL;
51}
52
53/* ---- the GPU model (CP) ------------------------------------------------*/
54struct cp_log {
55 unsigned fillers; /* type-2 filler dwords executed */
56 unsigned writes; /* WRITE_DATA packets executed */
57 unsigned unknown; /* anything else: must stay 0 */
58 unsigned faults; /* WRITE_DATA to an address outside the pool */
59 uint32_t hdr; /* first type-3 header seen */
60 uint32_t info; /* WRITE_DATA info dword of the last one */
61 uint64_t addr; /* its address */
62 uint32_t data; /* its data */
63 uint32_t rptr; /* read pointer the CP last reported */
64};
65static struct cp_log cpl;
66static uint32_t cp_rptr; /* the GPU's read pointer, kept across runs */
67static volatile int cp_stop;
68static pthread_t cp_thread;
69
70static void *cp_main(void *arg)
71{
72 (void)arg;
73 struct amd_one_ring *r = &g_amd_gpu.ring_gfx;
74 uint32_t mask = r->ring_dw - 1u, rptr = cp_rptr;
75 while (!cp_stop) {
76 uint32_t wptr = fake_regs[mmCP_RB0_WPTR] & mask;
77 while (rptr != wptr) {
78 uint32_t dw = r->ring[rptr];
79 if ((dw >> 30) == 2u) { /* type 2: one dword, no body */
80 cpl.fillers++;
81 rptr = (rptr + 1u) & mask;
82 continue;
83 }
84 if ((dw >> 30) != 3u) { /* not a packet this model knows */
85 cpl.unknown++;
86 rptr = wptr;
87 break;
88 }
89 uint32_t count = ((dw >> 16) & 0x3fffu) + 1u; /* body dwords */
90 uint32_t op = (dw >> 8) & 0xffu;
91 if (!cpl.hdr)
92 cpl.hdr = dw;
93 if (op == 0x37u && count == 4u) { /* WRITE_DATA */
94 uint32_t info = r->ring[(rptr + 1u) & mask];
95 uint32_t lo = r->ring[(rptr + 2u) & mask];
96 uint32_t hi = r->ring[(rptr + 3u) & mask];
97 uint32_t data = r->ring[(rptr + 4u) & mask];
98 uint64_t addr = ((uint64_t)hi << 32) | lo;
99 cpl.info = info;
100 cpl.addr = addr;
101 cpl.data = data;
102 cpl.writes++;
103 if (((info >> 8) & 0xfu) == 5u) { /* DST_SEL 5: memory */
104 volatile uint32_t *p = pa_to_host(addr);
105 __atomic_thread_fence(__ATOMIC_SEQ_CST);
106 if (p) *p = data; else cpl.faults++;
107 }
108 } else {
109 cpl.unknown++;
110 rptr = wptr;
111 break;
112 }
113 rptr = (rptr + 1u + count) & mask;
114 }
115 cpl.rptr = rptr;
116 cp_rptr = rptr;
117 *r->rptr_va = rptr;
118 sched_yield();
119 }
120 return NULL;
121}
122
123static void cp_start(void)
124{
125 memset(&cpl, 0, sizeof cpl);
126 cp_stop = 0;
127 if (pthread_create(&cp_thread, NULL, cp_main, NULL)) {
128 printf("cannot start the GPU model\n");
129 exit(2);
130 }
131}
132
133static void cp_stop_join(void)
134{
135 cp_stop = 1;
136 pthread_join(cp_thread, NULL);
137}
138
139/* ---- test helpers ------------------------------------------------------*/
140static int pass_count, fail_count;
141#define CHECK(cond, label) \
142 do { \
143 if (cond) { printf(" PASS %s\n", (label)); pass_count++; } \
144 else { printf(" FAIL %s\n", (label)); fail_count++; } \
145 } while (0)
146
147/* A pool at 2 GiB: frames 2 GiB .. 4 GiB + 1 MiB, the pool is the first 4 MiB. */
148static void setup_pool(void)
149{
150 struct ck_frames fr;
151 ck_frames_init(&fr);
152 ck_frames_add(&fr, 0x80000000ull, 0x100100000ull);
153 memset(&g_amd_gpu, 0, sizeof g_amd_gpu);
154 if (amd_dma_pool_take(&g_amd_gpu, &fr, POOL_PAGES, AMD_DMA_LIMIT)) {
155 printf("setup: no pool\n");
156 exit(2);
157 }
158 pool_host = aligned_alloc(4096, (size_t)POOL_PAGES * 4096u);
159 if (!pool_host) { printf("setup: no host memory\n"); exit(2); }
160 memset(pool_host, 0, (size_t)POOL_PAGES * 4096u);
161 pool_pa = g_amd_gpu.dma_pa;
162 pool_bytes = g_amd_gpu.dma_total;
163 g_amd_gpu.dma_va = pool_host;
164 g_amd_gpu.regs = fake_regs;
165 g_amd_gpu.tsc_hz = 1000000000ull; /* one tick per nanosecond (amd_host_shim.h) */
166 /* The firmware load is not under test here: mark the ucode step as done. */
167 g_amd_gpu.steps_done = AMD_STEP_UCODE;
168 memset((void *)fake_regs, 0, sizeof fake_regs);
169}
170
171/* ---- R1-R3: the pool comes from frames, below 4 GiB --------------------*/
172static void test_pool_from_frames(void)
173{
174 printf("R1: the DMA pool is taken from frames below 4 GiB\n");
175 struct ck_frames fr;
176 ck_frames_init(&fr);
177 ck_frames_add(&fr, 0x80000000ull, 0x100100000ull);
178 memset(&g_amd_gpu, 0, sizeof g_amd_gpu);
179 int rc = amd_dma_pool_take(&g_amd_gpu, &fr, POOL_PAGES, AMD_DMA_LIMIT);
180 CHECK(rc == 0 && g_amd_gpu.dma_pa == 0x80000000ull, "pool starts at the first free frame");
181 CHECK(g_amd_gpu.dma_total == POOL_PAGES * 4096u && g_amd_gpu.dma_off == 0,
182 "pool size is 4 MiB with the cursor at zero");
183 CHECK(g_amd_gpu.dma_pa + g_amd_gpu.dma_total <= AMD_DMA_LIMIT, "pool ends at or below 4 GiB");
184
185 printf("R2: frames only above 4 GiB give no pool, and nothing is taken\n");
186 ck_frames_init(&fr);
187 ck_frames_add(&fr, 0x100000000ull, 0x110000000ull);
188 uint64_t before = ck_frames_free_bytes(&fr);
189 memset(&g_amd_gpu, 0, sizeof g_amd_gpu);
190 rc = amd_dma_pool_take(&g_amd_gpu, &fr, POOL_PAGES, AMD_DMA_LIMIT);
191 CHECK(rc == -1 && g_amd_gpu.dma_total == 0, "take refuses: no frame below the limit");
192 CHECK(ck_frames_free_bytes(&fr) == before, "a refused take leaves the frames free");
193
194 printf("R3: no frame allocator (fr == NULL) gives no pool and no crash\n");
195 memset(&g_amd_gpu, 0, sizeof g_amd_gpu);
196 CHECK(amd_dma_pool_take(&g_amd_gpu, NULL, POOL_PAGES, AMD_DMA_LIMIT) == -1 &&
197 g_amd_gpu.dma_total == 0, "NULL frames: take returns -1");
198}
199
200/* ---- R4: the gfx ring allocates from the pool -------------------------*/
201static void test_ring_init(void)
202{
203 printf("R4: amd_gfx_ring_init allocates ring, rptr and fence from the pool\n");
204 int rc = amd_gfx_ring_init();
205 struct amd_one_ring *r = &g_amd_gpu.ring_gfx;
206 CHECK(rc == 0 && r->ready, "gfx ring init returns 0 and marks the ring ready");
207 CHECK(in_pool(r->ring_pa, AMD_RING_BYTES), "ring buffer lies inside the pool");
208 CHECK(in_pool(r->rptr_pa, 4) && in_pool(r->fence_pa, 4), "rptr and fence slots lie inside the pool");
209 CHECK(r->ring_pa != r->rptr_pa && r->rptr_pa != r->fence_pa && r->ring_pa != r->fence_pa,
210 "ring, rptr and fence are three different allocations");
211 CHECK(((r->ring_pa - pool_pa) % 64u) == 0 && ((r->fence_pa - pool_pa) % 64u) == 0,
212 "allocations are 64-byte aligned (amd_dma_alloc)");
213 CHECK((volatile uint8_t *)r->ring == pool_host + (r->ring_pa - pool_pa),
214 "host pointer and physical address of the ring agree");
215 CHECK(fake_regs[mmCP_RB0_BASE] == (uint32_t)(r->ring_pa >> 8) &&
216 fake_regs[mmCP_RB0_BASE_HI] == (uint32_t)(r->ring_pa >> 40),
217 "CP_RB0_BASE and _HI carry ring_pa >> 8 (gfx_v8_0.c:4278-4280)");
218}
219
220/* ---- R5-R6: NOP + WRITE_DATA fence round trip --------------------------*/
221static void test_fence_round_trip(void)
222{
223 struct amd_one_ring *r = &g_amd_gpu.ring_gfx;
224
225 printf("R5: NOP + WRITE_DATA fence round-trips through the GPU model\n");
226 cp_start();
227 int rc = amd_gfx_ring_test();
228 cp_stop_join();
229 CHECK(rc == 0, "amd_gfx_ring_test returns 0");
230 CHECK(*r->fence_va == 1, "fence slot reads 1 (the GPU wrote it)");
231 CHECK(cpl.fillers == 1 && cpl.writes == 1 && cpl.unknown == 0 && cpl.faults == 0,
232 "the GPU executed one filler and one WRITE_DATA, nothing else");
233 CHECK(cpl.hdr == 0xc0033700u, "WRITE_DATA header is PACKET3(0x37, 3) (gfx_v8_0.c:891)");
234 CHECK(cpl.info == 0x00100500u, "WRITE_DATA info is DST_SEL 5 (memory) with WR_CONFIRM (gfx_v8_0.c:892)");
235 CHECK(cpl.addr == r->fence_pa && cpl.data == 1, "WRITE_DATA targets the fence PA with seq 1");
236 CHECK(cpl.rptr == 6 && r->wptr == 6, "the GPU consumed all six dwords (rptr == wptr == 6)");
237
238 printf("R6: a second round trip advances the ring and the fence\n");
239 cp_start();
240 rc = amd_gfx_ring_test();
241 cp_stop_join();
242 CHECK(rc == 0 && *r->fence_va == 2, "second fence lands with seq 2");
243 CHECK(cpl.writes == 1 && cpl.rptr == 12 && r->wptr == 12,
244 "the second packet pair starts at dword 6 and ends at dword 12");
245}
246
247/* ---- R7: a fence nobody writes times out -----------------------------*/
248static void test_fence_timeout(void)
249{
250 printf("R7: ck_gpu_fence_wait times out when nothing writes the fence\n");
251 volatile uint32_t slot = 0;
252 uint64_t t0 = ck_rdtsc();
253 int rc = ck_gpu_fence_wait(&slot, 5u, 20u);
254 uint64_t dt = ck_rdtsc() - t0;
255 CHECK(rc == AMD_RING_ETIMEOUT, "returns AMD_RING_ETIMEOUT");
256 CHECK(dt < 2000ull * 1000000ull, "gives up within the TSC bound (about 20 ms, under 2 s)");
257}
258
259int main(void)
260{
261 printf("AMD GPU ring host test\n");
262 printf("======================\n");
263 test_pool_from_frames();
264 setup_pool();
265 test_ring_init();
266 test_fence_round_trip();
267 test_fence_timeout();
268 printf("\n======================\n");
269 printf("PASS: %d FAIL: %d\n", pass_count, fail_count);
270 if (fail_count > 0) {
271 printf("AMD_GPU_RING_TEST: FAIL\n");
272 return 1;
273 }
274 printf("AMD_GPU_RING_TEST: PASS\n");
275 return 0;
276}