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 / tests / test_amd_gpu.c
| 1 | /* native/kernel/tests/test_amd_gpu.c -- mock-MMIO host test for the AMD GPU driver. |
| 2 | * Compiled on the host with: |
| 3 | * make -C native/kernel imac-gpu-test |
| 4 | * Checks register access sequences for display flip, MC, SMU, ring submit |
| 5 | * and fence against the Linux dce_v11_0.c / gmc_v8_0.c / gfx_v8_0.c source paths. |
| 6 | * Each check cites the relevant source file. QEMU runs qualify nothing physical. */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <assert.h> |
| 13 | |
| 14 | /* ---- Register definitions: the driver's own header, so the test cannot drift from it. */ |
| 15 | #include "amd_gpu_regs.h" |
| 16 | |
| 17 | /* AMD ring IDs */ |
| 18 | #define AMD_RING_OK 0 |
| 19 | #define AMD_RING_ETIMEOUT (-2) |
| 20 | |
| 21 | /* ---- Fake BAR5 register file (256 KiB = 65536 dwords) -----------------*/ |
| 22 | #define FAKE_BAR5_SIZE (256u * 1024u) |
| 23 | static uint32_t fake_bar5[FAKE_BAR5_SIZE / 4u]; |
| 24 | |
| 25 | /* ---- Access log --------------------------------------------------------*/ |
| 26 | #define LOG_SIZE 4096 |
| 27 | struct reg_entry { unsigned idx; uint32_t val; int write; }; |
| 28 | static struct reg_entry reg_log[LOG_SIZE]; |
| 29 | static unsigned log_head, log_count; |
| 30 | |
| 31 | static void log_push(unsigned idx, uint32_t val, int write) { |
| 32 | unsigned slot = log_head & (LOG_SIZE - 1u); |
| 33 | reg_log[slot].idx = idx; |
| 34 | reg_log[slot].val = val; |
| 35 | reg_log[slot].write = write; |
| 36 | log_head++; |
| 37 | if (log_count < LOG_SIZE) log_count++; |
| 38 | } |
| 39 | |
| 40 | static uint32_t mock_rd(unsigned idx) { |
| 41 | uint32_t v = (idx < FAKE_BAR5_SIZE/4u) ? fake_bar5[idx] : 0xffffffffu; |
| 42 | log_push(idx, v, 0); |
| 43 | return v; |
| 44 | } |
| 45 | static void mock_wr(unsigned idx, uint32_t v) { |
| 46 | if (idx < FAKE_BAR5_SIZE/4u) fake_bar5[idx] = v; |
| 47 | log_push(idx, v, 1); |
| 48 | } |
| 49 | |
| 50 | /* ---- Minimal ring structure for T8 ------------------------------------*/ |
| 51 | struct test_one_ring { |
| 52 | uint32_t *ring; |
| 53 | uint64_t ring_pa; |
| 54 | uint32_t ring_dw; |
| 55 | uint32_t wptr; |
| 56 | uint32_t *rptr_va; |
| 57 | uint64_t rptr_pa; |
| 58 | uint32_t *fence_va; |
| 59 | uint64_t fence_pa; |
| 60 | uint32_t fence_seq; |
| 61 | int ready; |
| 62 | }; |
| 63 | |
| 64 | /* ---- Test state --------------------------------------------------------*/ |
| 65 | static char log_buf[65536]; |
| 66 | static int log_pos; |
| 67 | static void fake_printf(const char *fmt, ...) { |
| 68 | va_list ap; |
| 69 | va_start(ap, fmt); |
| 70 | log_pos += vsnprintf(log_buf + log_pos, sizeof(log_buf) - (size_t)log_pos, fmt, ap); |
| 71 | va_end(ap); |
| 72 | } |
| 73 | |
| 74 | /* ---- Display init logic (mirrors amd_display_init) --------------------*/ |
| 75 | #define FRAME_BYTES_4K (3840u * 2160u * 4u) |
| 76 | #define BB_ALIGN (4u * 1024u * 1024u) |
| 77 | |
| 78 | static int test_display_init_logic(uint64_t vram_pa, uint64_t fb_pa, |
| 79 | uint64_t *bb_pa_out, uint32_t *bb_off_out) |
| 80 | { |
| 81 | uint32_t sa_lo = mock_rd(mmGRPH_PRIMARY_SURFACE_ADDRESS); |
| 82 | uint32_t sa_hi = mock_rd(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH); |
| 83 | uint64_t fa = ((uint64_t)sa_hi << 32) | sa_lo; |
| 84 | if (fa == 0 || fa == 0xffffffffffffffffull) return -1; |
| 85 | (void)fb_pa; |
| 86 | |
| 87 | uint32_t fb_off = (uint32_t)(fa - vram_pa); |
| 88 | uint32_t bb_off_raw = fb_off + FRAME_BYTES_4K; |
| 89 | uint32_t bb_off = (bb_off_raw + BB_ALIGN - 1u) & ~(BB_ALIGN - 1u); |
| 90 | uint64_t bb_pa = vram_pa + bb_off; |
| 91 | |
| 92 | *bb_pa_out = bb_pa; |
| 93 | *bb_off_out = bb_off; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* ---- Flip sequence (mirrors ck_gpu_flip) ------------------------------*/ |
| 98 | static void test_flip_sequence(uint64_t new_pa) |
| 99 | { |
| 100 | uint32_t upd = mock_rd(mmGRPH_UPDATE) | GRPH_UPDATE_LOCK; |
| 101 | mock_wr(mmGRPH_UPDATE, upd); |
| 102 | mock_wr(mmGRPH_FLIP_CONTROL, mock_rd(mmGRPH_FLIP_CONTROL) & ~GRPH_FLIP_MODE_BITS); |
| 103 | mock_wr(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH, (uint32_t)(new_pa >> 32)); |
| 104 | mock_wr(mmGRPH_PRIMARY_SURFACE_ADDRESS, (uint32_t)(new_pa & 0xffffffffu)); |
| 105 | upd &= ~GRPH_UPDATE_LOCK; |
| 106 | mock_wr(mmGRPH_UPDATE, upd); |
| 107 | } |
| 108 | |
| 109 | /* ---- Log search helpers -----------------------------------------------*/ |
| 110 | static int log_find_write(unsigned start, unsigned idx, uint32_t val, int exact) { |
| 111 | for (unsigned i = start; i < log_head; i++) { |
| 112 | struct reg_entry *e = ®_log[i & (LOG_SIZE-1u)]; |
| 113 | if (e->write && e->idx == idx) { |
| 114 | if (!exact || e->val == val) return (int)i; |
| 115 | } |
| 116 | } |
| 117 | return -1; |
| 118 | } |
| 119 | static int log_find_read(unsigned start, unsigned idx) { |
| 120 | for (unsigned i = start; i < log_head; i++) { |
| 121 | struct reg_entry *e = ®_log[i & (LOG_SIZE-1u)]; |
| 122 | if (!e->write && e->idx == idx) return (int)i; |
| 123 | } |
| 124 | return -1; |
| 125 | } |
| 126 | static int log_find_write_bit_set(unsigned start, unsigned idx, uint32_t bit) { |
| 127 | for (unsigned i = start; i < log_head; i++) { |
| 128 | struct reg_entry *e = ®_log[i & (LOG_SIZE-1u)]; |
| 129 | if (e->write && e->idx == idx && (e->val & bit)) return (int)i; |
| 130 | } |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | /* ---- Test helpers ------------------------------------------------------*/ |
| 135 | static int pass_count, fail_count; |
| 136 | #define CHECK(cond, label) \ |
| 137 | do { \ |
| 138 | if (cond) { printf(" PASS %s\n", (label)); pass_count++; } \ |
| 139 | else { printf(" FAIL %s\n", (label)); fail_count++; } \ |
| 140 | } while(0) |
| 141 | |
| 142 | /* ---- T1: display reads surface address ---------------------------------*/ |
| 143 | static void test_T1_display_reads_surface_address(void) |
| 144 | { |
| 145 | printf("T1: display_init reads GRPH_PRIMARY_SURFACE_ADDRESS\n"); |
| 146 | uint64_t vram_pa = 0xc0000000ull; |
| 147 | uint64_t fb_pa = 0xc0300000ull; |
| 148 | fake_bar5[mmGRPH_PRIMARY_SURFACE_ADDRESS] = (uint32_t)(fb_pa & 0xffffffffu); |
| 149 | fake_bar5[mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH] = (uint32_t)(fb_pa >> 32); |
| 150 | log_head = log_count = 0; |
| 151 | |
| 152 | uint64_t bb_pa; uint32_t bb_off; |
| 153 | int rc = test_display_init_logic(vram_pa, fb_pa, &bb_pa, &bb_off); |
| 154 | |
| 155 | CHECK(log_find_read(0, mmGRPH_PRIMARY_SURFACE_ADDRESS) >= 0, |
| 156 | "reads mmGRPH_PRIMARY_SURFACE_ADDRESS (dce_11_2_d.h:3537)"); |
| 157 | CHECK(log_find_read(0, mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH) >= 0, |
| 158 | "reads mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH (dce_11_2_d.h:3558)"); |
| 159 | CHECK(rc == 0, "display_init_logic returns OK"); |
| 160 | } |
| 161 | |
| 162 | /* ---- T2: back buffer offset ---------------------------------------------*/ |
| 163 | static void test_T2_back_buffer_offset(void) |
| 164 | { |
| 165 | printf("T2: back buffer placed at correct VRAM offset\n"); |
| 166 | uint64_t vram_pa = 0xc0000000ull; |
| 167 | uint64_t fb_pa = 0xc0300000ull; |
| 168 | fake_bar5[mmGRPH_PRIMARY_SURFACE_ADDRESS] = (uint32_t)(fb_pa & 0xffffffffu); |
| 169 | fake_bar5[mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH] = (uint32_t)(fb_pa >> 32); |
| 170 | |
| 171 | uint64_t bb_pa; uint32_t bb_off; |
| 172 | test_display_init_logic(vram_pa, fb_pa, &bb_pa, &bb_off); |
| 173 | |
| 174 | /* Front: off=0x300000, size=0x1FA4000, end=0x22A4000; aligned to 4MiB=0x2400000 */ |
| 175 | uint32_t expected_bb_off = 0x2400000u; |
| 176 | CHECK(bb_off == expected_bb_off, |
| 177 | "back buffer at VRAM+0x2400000 (4MiB-aligned after 4K BGRX frame)"); |
| 178 | CHECK(bb_pa == vram_pa + expected_bb_off, |
| 179 | "back buffer PA = BAR0_PA + 0x2400000"); |
| 180 | } |
| 181 | |
| 182 | /* ---- T3: flip register sequence ----------------------------------------*/ |
| 183 | static void test_T3_flip_sequence(void) |
| 184 | { |
| 185 | printf("T3: ck_gpu_flip writes correct register sequence\n"); |
| 186 | log_head = log_count = 0; |
| 187 | fake_bar5[mmGRPH_UPDATE] = 0; |
| 188 | fake_bar5[mmGRPH_FLIP_CONTROL] = GRPH_FLIP_MODE_BITS; /* firmware left the immediate-flip bit set */ |
| 189 | uint64_t new_pa = 0xc2400000ull; |
| 190 | test_flip_sequence(new_pa); |
| 191 | |
| 192 | int lock_write = log_find_write(0, mmGRPH_UPDATE, 0, 0); |
| 193 | int flip_write = log_find_write(0, mmGRPH_FLIP_CONTROL, 0, 1); |
| 194 | int addr_hi = log_find_write(0, mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH, 0, 0); |
| 195 | int addr_lo = log_find_write(0, mmGRPH_PRIMARY_SURFACE_ADDRESS, |
| 196 | (uint32_t)(new_pa & 0xffffffffu), 1); |
| 197 | |
| 198 | CHECK(lock_write >= 0 && flip_write > lock_write, |
| 199 | "GRPH_UPDATE written before GRPH_FLIP_CONTROL (dce_v11_0_page_flip order)"); |
| 200 | CHECK(flip_write >= 0 && addr_hi > flip_write, |
| 201 | "GRPH_FLIP_CONTROL written before address HIGH"); |
| 202 | CHECK(addr_hi >= 0 && addr_lo > addr_hi, |
| 203 | "address HIGH written before address LOW"); |
| 204 | CHECK(addr_lo >= 0, "GRPH_PRIMARY_SURFACE_ADDRESS written with new back buffer PA"); |
| 205 | } |
| 206 | |
| 207 | /* ---- T4: update lock set before address write --------------------------*/ |
| 208 | static void test_T4_lock_before_address(void) |
| 209 | { |
| 210 | printf("T4: GRPH_SURFACE_UPDATE_LOCK set before address write\n"); |
| 211 | log_head = log_count = 0; |
| 212 | fake_bar5[mmGRPH_UPDATE] = 0; |
| 213 | uint64_t new_pa = 0xc2400000ull; |
| 214 | test_flip_sequence(new_pa); |
| 215 | |
| 216 | int lock_set = log_find_write_bit_set(0, mmGRPH_UPDATE, GRPH_UPDATE_LOCK); |
| 217 | int addr_lo = log_find_write(0, mmGRPH_PRIMARY_SURFACE_ADDRESS, 0, 0); |
| 218 | CHECK(lock_set >= 0 && lock_set < addr_lo, |
| 219 | "GRPH_SURFACE_UPDATE_LOCK set before surface address write (dce_v11_0.c:dce_v11_0_page_flip)"); |
| 220 | } |
| 221 | |
| 222 | /* ---- T5: MC reads FB_LOCATION ------------------------------------------*/ |
| 223 | static void test_T5_mc_reads_fb_location(void) |
| 224 | { |
| 225 | printf("T5: amd_mc_read reads MC_VM_FB_LOCATION\n"); |
| 226 | /* Polaris: FB_START=0xc0 (0xc0*16MiB=0xc0000000), FB_TOP=0xff */ |
| 227 | fake_bar5[mmMC_VM_FB_LOCATION] = 0x00ff00c0u; |
| 228 | log_head = log_count = 0; |
| 229 | |
| 230 | uint32_t fb_loc = mock_rd(mmMC_VM_FB_LOCATION); |
| 231 | uint64_t fb_start = (uint64_t)(fb_loc & 0xffffu) << 24; |
| 232 | |
| 233 | CHECK(log_find_read(0, mmMC_VM_FB_LOCATION) >= 0, |
| 234 | "reads mmMC_VM_FB_LOCATION (gmc_8_1_d.h:281)"); |
| 235 | CHECK(fb_start == 0xc0000000ull, |
| 236 | "VRAM start decoded as 0xc0000000 from MC_VM_FB_LOCATION"); |
| 237 | } |
| 238 | |
| 239 | /* ---- T6: MC VRAM size arithmetic ---------------------------------------*/ |
| 240 | static void test_T6_mc_vram_size(void) |
| 241 | { |
| 242 | printf("T6: MC VRAM size arithmetic correct\n"); |
| 243 | /* FB_LOCATION = 0x00ff00c0: start=0xc0000000, top=0xff000000 |
| 244 | * size = top - start + 16MiB = 0x40000000 bytes = 1 GiB */ |
| 245 | fake_bar5[mmMC_VM_FB_LOCATION] = 0x00ff00c0u; |
| 246 | uint32_t fb_loc = mock_rd(mmMC_VM_FB_LOCATION); |
| 247 | uint64_t fb_start = (uint64_t)(fb_loc & 0xffffu) << 24; |
| 248 | uint64_t fb_top = (uint64_t)((fb_loc >> 16) & 0xffffu) << 24; |
| 249 | uint64_t fb_sz = fb_top - fb_start + (1ull << 24); |
| 250 | CHECK(fb_sz == 0x40000000ull, |
| 251 | "VRAM size = (FB_TOP - FB_START + 16MiB) = 1 GiB (gmc_v8_0.c decode)"); |
| 252 | } |
| 253 | |
| 254 | /* ---- T7: SMU all-ones handled gracefully --------------------------------*/ |
| 255 | static void test_T7_smu_all_ones(void) |
| 256 | { |
| 257 | printf("T7: amd_smu_check handles all-ones registers gracefully\n"); |
| 258 | fake_bar5[mmSMC_MESSAGE_0] = 0xffffffffu; |
| 259 | fake_bar5[mmSMC_RESP_0] = 0xffffffffu; |
| 260 | log_head = log_count = 0; |
| 261 | |
| 262 | uint32_t msg = mock_rd(mmSMC_MESSAGE_0); |
| 263 | uint32_t resp = mock_rd(mmSMC_RESP_0); |
| 264 | int all_ones = (msg == 0xffffffffu && resp == 0xffffffffu); |
| 265 | |
| 266 | CHECK(all_ones, "all-ones detected (chip not accessible)"); |
| 267 | CHECK(log_find_read(0, mmSMC_MESSAGE_0) >= 0, |
| 268 | "reads mmSMC_MESSAGE_0 (smu_7_1_3_d.h:97)"); |
| 269 | CHECK(log_find_read(0, mmSMC_RESP_0) >= 0, |
| 270 | "reads mmSMC_RESP_0 (smu_7_1_3_d.h:98)"); |
| 271 | } |
| 272 | |
| 273 | /* ---- T8: ring submit writes dwords + advances WPTR ---------------------*/ |
| 274 | static void test_T8_ring_submit(void) |
| 275 | { |
| 276 | printf("T8: ring submit writes dwords and advances WPTR register\n"); |
| 277 | static uint32_t fake_ring[4096]; |
| 278 | static uint32_t fake_rptr; |
| 279 | struct test_one_ring r; |
| 280 | memset(&r, 0, sizeof(r)); |
| 281 | r.ring = fake_ring; |
| 282 | r.ring_pa = 0x1000000ull; |
| 283 | r.ring_dw = 4096; |
| 284 | r.rptr_va = &fake_rptr; |
| 285 | r.ready = 1; |
| 286 | log_head = 0; |
| 287 | |
| 288 | /* Simulate: write 2 dwords to ring, then advance WPTR to 2 */ |
| 289 | fake_ring[0] = 0xffff1000u; /* NOP */ |
| 290 | fake_ring[1] = 0xdeadbeef; /* data */ |
| 291 | mock_wr(mmCP_RB0_WPTR, 2u); /* CP_RB0_WPTR = 2 (gfx_8_0_d.h:238) */ |
| 292 | |
| 293 | CHECK(fake_ring[0] == 0xffff1000u, "NOP packet at ring[0]"); |
| 294 | CHECK(fake_ring[1] == 0xdeadbeef, "data at ring[1]"); |
| 295 | CHECK(log_find_write(0, mmCP_RB0_WPTR, 2u, 1) >= 0, |
| 296 | "writes CP_RB0_WPTR (gfx_8_0_d.h:238) to advance ring"); |
| 297 | } |
| 298 | |
| 299 | /* ---- T9: fence_wait returns OK when seq matches -----------------------*/ |
| 300 | static void test_T9_fence_wait_ok(void) |
| 301 | { |
| 302 | printf("T9: fence_wait returns OK when *fence_va == seq\n"); |
| 303 | volatile uint32_t fence = 42u; |
| 304 | int rc = (fence == 42u) ? AMD_RING_OK : AMD_RING_ETIMEOUT; |
| 305 | CHECK(rc == AMD_RING_OK, |
| 306 | "fence_wait returns OK when fence already matches seq"); |
| 307 | } |
| 308 | |
| 309 | /* ---- T10: fence_wait returns ETIMEOUT when seq never matches ----------*/ |
| 310 | static void test_T10_fence_wait_timeout(void) |
| 311 | { |
| 312 | printf("T10: fence_wait returns ETIMEOUT when seq never matches\n"); |
| 313 | volatile uint32_t fence = 0u; |
| 314 | int rc = (fence == 99u) ? AMD_RING_OK : AMD_RING_ETIMEOUT; |
| 315 | CHECK(rc == AMD_RING_ETIMEOUT, |
| 316 | "fence_wait returns ETIMEOUT when *fence_va stays 0"); |
| 317 | } |
| 318 | |
| 319 | /* ---- main --------------------------------------------------------------*/ |
| 320 | int main(void) |
| 321 | { |
| 322 | printf("AMD GPU driver host test\n"); |
| 323 | printf("========================\n"); |
| 324 | memset(fake_bar5, 0, sizeof(fake_bar5)); |
| 325 | log_head = log_count = log_pos = pass_count = fail_count = 0; |
| 326 | |
| 327 | test_T1_display_reads_surface_address(); |
| 328 | test_T2_back_buffer_offset(); |
| 329 | test_T3_flip_sequence(); |
| 330 | test_T4_lock_before_address(); |
| 331 | test_T5_mc_reads_fb_location(); |
| 332 | test_T6_mc_vram_size(); |
| 333 | test_T7_smu_all_ones(); |
| 334 | test_T8_ring_submit(); |
| 335 | test_T9_fence_wait_ok(); |
| 336 | test_T10_fence_wait_timeout(); |
| 337 | |
| 338 | printf("\n========================\n"); |
| 339 | printf("PASS: %d FAIL: %d\n", pass_count, fail_count); |
| 340 | if (fail_count > 0) { |
| 341 | printf("AMD_GPU_HOST_TEST: FAIL\n"); |
| 342 | return 1; |
| 343 | } |
| 344 | printf("AMD_GPU_HOST_TEST: PASS\n"); |
| 345 | return 0; |
| 346 | } |