Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/test_omega_gpu_platform_aien.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / test_omega_gpu_platform_aien.c` 551 lines · 29453 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* test_omega_gpu_platform_aien: the aien-gpu platform (src/omega_gpu_platform_aien.c) on a simulated /dev/aien-gpu. 3 * Host program, opens no device: every case either installs the simulated system calls (OmegaGpuAienSys) or points 4 * CUDAPP_AIEN_GPU_DEV at a path that does not exist. 5 * 6 * What it checks: the same caps as the Linux reference, bit for bit, and the session contract check; a missing device 7 * node, a module that is not RM_READY and an ABI mismatch are named REFUSED_DEVICE refusals; every buffer is reserved, 8 * allocated at that VA (GPU VA == CPU VA) and mapped MAP_FIXED|MAP_SHARED over it, GPU-uncached unless a device-private 9 * DATA buffer asked for GPU_CACHED; the GPFIFO entry is nvrm_gp_entry's, USERD GPPut sits at Nvc96fControl.GPPut after 10 * a 1024-entry ring, and the doorbell gets the work-submit token at +0x90; a simulated GPU that executes semaphore 11 * releases from the ring completes a submission that poll_word then observes; refusals ring no doorbell; the ring 12 * refuses its 1024th unretired entry; the omega #457 prefix follows a host sync into a GPU-cached buffer; the session 13 * opens and closes on it; closing the device releases every object. Each case runs in its own child process. 14 * 15 * Differential (CUDApp#128): with AIEN_TRACE= every simulated system call (request bytes in and out, map and 16 * unmap arguments) and the final ring, USERD GPPut, pushbuffer and doorbell bytes are appended to . The make 17 * target builds this test against native/aiengpu/context.om (default) and against the C oracle 18 * (CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN=1) and requires the two traces to be byte-identical. 19 */ 20 #include "omega_gpu_platform_aien.h" 21 #include "omega_gpu_platform_linux.h" 22 #include "omega_gpu_session.h" 23 #include "nvrm.h" 24 #include "aien_gpu_uapi.h" 25 #include "nvtypes.h" 26 #include "class/clc96f.h" 27 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 40 static int case_ok; 41 #define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) 42 43 /* ---- simulated /dev/aien-gpu ---------------------------------------------------------------------------------- */ 44 45 #define FAKE_FD 900 46 #define FAKE_PATH "/fake/aien-gpu" 47 #define FAKE_TOKEN 0x00010007u 48 #define FAKE_DOORBELL_OFF (1ull << 40) 49 #define FAKE_OBJS 64 50 51 typedef struct { uint64_t va, size; uint32_t flags; int live, mapped; } FakeObj; 52 static struct { 53 uint32_t state, abi; 54 int fail_chan, fail_free, opens, closes, chan_live, chan_creates, chan_destroys, frees, maps_fixed; 55 FakeObj obj[FAKE_OBJS]; 56 uint32_t nobj; 57 struct agpu_chan_create chan; 58 volatile uint8_t *doorbell; 59 int bad_fixed_map; /* an object mapped somewhere other than its VA, or not FIXED|SHARED */ 60 } fk; 61 62 /* ---- trace (AIEN_TRACE) ---- */ 63 static int g_tfd = -1; 64 static void trace_hex(const char *tag, const void *p, size_t n) { 65 if (g_tfd < 0) return; 66 static const char hx[] = "`0123456789abcdef`"; 67 const uint8_t *b = p; 68 char line[2 * 8192 + 64]; 69 size_t k = (size_t)snprintf(line, 64, "%s %zu ", tag, n); 70 for (size_t i = 0; i < n && k + 3 < sizeof line; i++) { line[k++] = hx[b[i] >> 4]; line[k++] = hx[b[i] & 15u]; } 71 line[k++] = '\n'; 72 (void)!write(g_tfd, line, k); 73 } 74 75 static int f_open(const char *path, int flags) { 76 (void)flags; 77 if (g_tfd >= 0) dprintf(g_tfd, "open %s\n", path); 78 if (strcmp(path, FAKE_PATH) != 0) { errno = ENOENT; return -1; } 79 fk.opens++; 80 return FAKE_FD; 81 } 82 static int f_close(int fd) { if (g_tfd >= 0) dprintf(g_tfd, "close %d\n", fd); if (fd == FAKE_FD) fk.closes++; return 0; } 83 static FakeObj *obj_of(uint32_t h) { return (h >= 1 && h <= fk.nobj && fk.obj[h - 1].live) ? &fk.obj[h - 1] : NULL; } 84 static int f_ioctl_dev(int fd, unsigned long req, void *arg); 85 static int f_ioctl(int fd, unsigned long req, void *arg) { 86 char tag[48]; 87 snprintf(tag, sizeof tag, "ioctl 0x%lx in", req); 88 trace_hex(tag, arg, _IOC_SIZE(req)); 89 const int rc = f_ioctl_dev(fd, req, arg); 90 snprintf(tag, sizeof tag, "ioctl rc %d out", rc); 91 trace_hex(tag, arg, _IOC_SIZE(req)); 92 return rc; 93 } 94 static int f_ioctl_dev(int fd, unsigned long req, void *arg) { 95 if (fd != FAKE_FD) { errno = EBADF; return -1; } 96 if (req == AGPU_INFO) { 97 struct agpu_info *i = arg; memset(i, 0, sizeof *i); i->abi = fk.abi; i->state = fk.state; return 0; 98 } 99 if (req == AGPU_MEM_ALLOC) { 100 struct agpu_mem_alloc *a = arg; 101 if (a->size == 0 || (a->size & 4095u) || (a->va & 4095u) || fk.nobj >= FAKE_OBJS) { a->st.verdict = AGPU_RM_E_ARG; return 0; } 102 FakeObj *o = &fk.obj[fk.nobj++]; 103 *o = (FakeObj){ .va = a->va, .size = a->size, .flags = a->flags, .live = 1 }; 104 a->handle = fk.nobj; a->mmap_off = (2ull << 40) + ((uint64_t)a->handle << 36); a->st.verdict = AGPU_RM_OK; 105 return 0; 106 } 107 if (req == AGPU_MEM_FREE) { 108 struct agpu_mem_free *f = arg; FakeObj *o = obj_of(f->handle); 109 if (fk.fail_free || !o) { f->st.verdict = AGPU_RM_E_ARG; f->st.code = 9; return 0; } 110 o->live = 0; fk.frees++; f->st.verdict = AGPU_RM_OK; return 0; 111 } 112 if (req == AGPU_CHAN_CREATE) { 113 struct agpu_chan_create *c = arg; 114 FakeObj *ring = obj_of(c->ring_handle), *ud = obj_of(c->userd_handle), *er = obj_of(c->err_handle); 115 if (fk.fail_chan) { c->st.verdict = AGPU_RM_E_RESULT; c->st.code = 0x1f; c->st.fn = 103; return 0; } 116 if (!ring || !ud || !er || fk.chan_live || !(ring->flags & AGPU_MEM_GPU_UNCACHED) || !(ud->flags & AGPU_MEM_GPU_UNCACHED) || 117 c->entries < 8 || (c->entries & (c->entries - 1)) || c->ring_off + (uint64_t)c->entries * 8 > ring->size || 118 (c->userd_off & 511) || c->userd_off + 512 > ud->size) { c->st.verdict = AGPU_RM_E_ARG; return 0; } 119 c->chid = 7; c->token = FAKE_TOKEN; c->doorbell_off = FAKE_DOORBELL_OFF; c->channel = 77; c->st.verdict = AGPU_RM_OK; 120 fk.chan = *c; fk.chan_live = 1; fk.chan_creates++; 121 return 0; 122 } 123 if (req == AGPU_CHAN_DESTROY) { 124 struct agpu_chan_destroy *d = arg; 125 if (!fk.chan_live || d->channel != 77) { d->st.verdict = AGPU_RM_E_ARG; return 0; } 126 fk.chan_live = 0; fk.chan_destroys++; d->st.verdict = AGPU_RM_OK; return 0; 127 } 128 errno = ENOTTY; 129 return -1; 130 } 131 static void *f_map(void *addr, size_t len, int prot, int flags, int fd, uint64_t off) { 132 if (g_tfd >= 0) dprintf(g_tfd, "map 0x%llx %zu %d 0x%x %d 0x%llx\n", (unsigned long long)(uintptr_t)addr, len, prot, flags, fd, 133 (unsigned long long)off); 134 if (fd == -1) return mmap(addr, len, prot, flags, -1, 0); 135 if (fd != FAKE_FD) { errno = EBADF; return MAP_FAILED; } 136 if (off == FAKE_DOORBELL_OFF) { 137 void *p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 138 if (p != MAP_FAILED) fk.doorbell = p; 139 return p; 140 } 141 uint32_t h = (uint32_t)((off - (2ull << 40)) >> 36); 142 FakeObj *o = obj_of(h); 143 if (!o || (uint64_t)(uintptr_t)addr != o->va || len != o->size || (flags & (MAP_FIXED | MAP_SHARED)) != (MAP_FIXED | MAP_SHARED)) { 144 fk.bad_fixed_map = 1; errno = EINVAL; return MAP_FAILED; 145 } 146 fk.maps_fixed++; o->mapped = 1; 147 return mmap(addr, len, prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 148 } 149 static int f_unmap(void *addr, size_t len) { 150 if (g_tfd >= 0) { 151 if ((volatile uint8_t *)addr == fk.doorbell) dprintf(g_tfd, "unmap doorbell %zu\n", len); 152 else dprintf(g_tfd, "unmap 0x%llx %zu\n", (unsigned long long)(uintptr_t)addr, len); 153 } 154 if ((volatile uint8_t *)addr == fk.doorbell) fk.doorbell = NULL; 155 return munmap(addr, len); 156 } 157 static const OmegaGpuAienSys FAKE_SYS = { f_open, f_close, f_ioctl, f_map, f_unmap }; 158 159 static void fake_install(void) { 160 memset(&fk, 0, sizeof fk); 161 fk.state = AGPU_STATE_RM_READY; fk.abi = AGPU_ABI_VERSION; 162 setenv("CUDAPP_AIEN_GPU_DEV", FAKE_PATH, 1); 163 setenv("CUDAPP_MEM_RESERVE_MIB", "0", 1); 164 (void)omega_gpu_platform_aien_set_sys(&FAKE_SYS); 165 } 166 167 static FakeObj *obj_at(uint64_t va) { 168 for (uint32_t i = 0; i < fk.nobj; i++) if (fk.obj[i].live && fk.obj[i].va == va) return &fk.obj[i]; 169 return NULL; 170 } 171 172 /* ---- simulated GPU: on a doorbell write of the token it runs every GP entry up to USERD GPPut and executes the 173 * semaphore releases (method 0x5c, subchannel 0, five words) it finds; every other method is skipped by count. ---- */ 174 static volatile int g_gpu_stop; 175 static uint32_t g_gpu_get, g_gpu_entries_run, g_gpu_prefixes; 176 static uint64_t g_fifo_va; 177 static void run_methods(const uint32_t *m, uint32_t n) { 178 for (uint32_t i = 0; i < n;) { 179 const uint32_t h = m[i++], count = (h >> 16) & 0x1fffu, subc = (h >> 13) & 7u, mthd = (h & 0xfffu) << 2; 180 if (h == ((2u << 28) | (4u << 16) | (0x0028u >> 2)) && i + 3 < n && m[i + 3] == (0x11u << 27)) g_gpu_prefixes++; 181 if (mthd == 0x005c && subc == 0 && count == 5 && i + 5 <= n) { 182 volatile uint32_t *dst = (volatile uint32_t *)(uintptr_t)(((uint64_t)m[i + 1] << 32) | m[i]); 183 *dst = m[i + 2]; 184 __sync_synchronize(); 185 } 186 i += count; 187 } 188 } 189 static void *gpu_thread(void *arg) { 190 (void)arg; 191 while (!g_gpu_stop) { 192 volatile uint8_t *db = fk.doorbell; 193 if (db && *(volatile uint32_t *)(db + OMEGA_GPU_AIEN_DOORBELL_WORD) == FAKE_TOKEN) { 194 *(volatile uint32_t *)(db + OMEGA_GPU_AIEN_DOORBELL_WORD) = 0; 195 __sync_synchronize(); 196 volatile uint64_t *ring = (volatile uint64_t *)(uintptr_t)g_fifo_va; 197 const uint32_t put = *(volatile uint32_t *)(uintptr_t)(g_fifo_va + OMEGA_GPU_AIEN_USERD_OFF + OMEGA_GPU_AIEN_USERD_GPPUT); 198 while (g_gpu_get != put) { 199 const uint64_t e = ring[g_gpu_get]; 200 run_methods((const uint32_t *)(uintptr_t)(e & ((1ull << 41) - 4u)), (uint32_t)((e >> 42) & 0x1fffffu)); 201 g_gpu_get = (g_gpu_get + 1u) % fk.chan.entries; 202 g_gpu_entries_run++; 203 } 204 } 205 usleep(20); 206 } 207 return NULL; 208 } 209 210 /* ---- helpers -------------------------------------------------------------------------------------------------- */ 211 212 static const OmegaGpuPlatform *P(void) { return omega_gpu_platform_aien(NULL); } 213 static OmegaGpuBufferDesc desc(uint32_t kind, uint32_t rights, uint32_t cache, uint64_t bytes) { 214 OmegaGpuBufferDesc d = { .kind = kind, .rights = rights, .gpu_cache = cache, .align = 0, .bytes = bytes }; 215 return d; 216 } 217 #define RW (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE) 218 #define POLLED (RW | OMEGA_GPU_RIGHT_HOST_POLLED) 219 #define RING_RIGHTS (OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ) 220 221 typedef struct { OmegaGpuDeviceHandle dev; OmegaGpuBufferHandle ring, marker; OmegaCpuView ring_cpu, marker_cpu; 222 OmegaGpuVa ring_va, marker_va; OmegaGpuQueueHandle q; } Rig; 223 224 static int rig_open(Rig *g) { 225 const OmegaGpuPlatform *p = P(); 226 memset(g, 0, sizeof *g); 227 OmegaGpuBufferDesc dr = desc(OMEGA_GPU_HANDLE_BUF_QUEUE, RING_RIGHTS, OMEGA_GPU_CACHE_GPU_UNCACHED, 0x10000); 228 OmegaGpuBufferDesc dm = desc(OMEGA_GPU_HANDLE_BUF_DATA, POLLED, OMEGA_GPU_CACHE_GPU_UNCACHED, 4096); 229 if (p->device_open(p->ctx, 0, &g->dev) != OMEGA_GPU_PLAT_OK) return -1; 230 if (p->dma_alloc(p->ctx, g->dev, &dr, &g->ring, &g->ring_cpu, &g->ring_va, NULL) != OMEGA_GPU_PLAT_OK) return -1; 231 if (p->dma_alloc(p->ctx, g->dev, &dm, &g->marker, &g->marker_cpu, &g->marker_va, NULL) != OMEGA_GPU_PLAT_OK) return -1; 232 if (p->queue_open(p->ctx, g->dev, g->ring, &g->q) != OMEGA_GPU_PLAT_OK) return -1; 233 g_fifo_va = fk.chan.ring_handle ? fk.obj[fk.chan.ring_handle - 1].va : 0; 234 return 0; 235 } 236 static uint32_t release_words(uint32_t *w, uint64_t va, uint32_t payload) { 237 uint32_t n = 0; 238 w[n++] = nvrm_mthd(0, 0x005c, 5); 239 w[n++] = (uint32_t)va; w[n++] = (uint32_t)(va >> 32); w[n++] = payload; w[n++] = 0; w[n++] = 0x1 | (1u << 20); 240 return n; 241 } 242 static uint32_t doorbell_word(void) { return fk.doorbell ? *(volatile uint32_t *)(fk.doorbell + OMEGA_GPU_AIEN_DOORBELL_WORD) : 0xdeadu; } 243 static uint32_t gpput_word(void) { return *(volatile uint32_t *)(uintptr_t)(g_fifo_va + OMEGA_GPU_AIEN_USERD_OFF + OMEGA_GPU_AIEN_USERD_GPPUT); } 244 /* The bytes the platform stored for the GPU: the whole GPFIFO ring, USERD GPPut, the pushbuffer head, the doorbell. */ 245 static void trace_state(const Rig *g) { 246 if (g_tfd < 0 || !g_fifo_va) return; 247 trace_hex("ring", (const void *)(uintptr_t)g_fifo_va, (size_t)OMEGA_GPU_AIEN_GP_ENTRIES * 8u); 248 const uint32_t put = gpput_word(), db = doorbell_word(); 249 trace_hex("gpput", &put, sizeof put); 250 trace_hex("pushbuffer", g->ring_cpu.ptr, 256); 251 trace_hex("doorbell", &db, sizeof db); 252 } 253 254 /* ---- cases ---------------------------------------------------------------------------------------------------- */ 255 256 static void c_caps(void) { 257 OmegaGpuPlatformCaps caps, lcaps; 258 const OmegaGpuPlatform *p = omega_gpu_platform_aien(&caps); 259 (void)omega_gpu_platform_linux(&lcaps); 260 /* The same contract as the Linux reference except the queue count: the reference opens extra channels 261 * (max_queues = 1 + NVRM_MAX_EXTRA_CHANNELS), aien-gpu ABI 1 drives one queue. */ 262 EXPECT(caps.max_queues == 1 && lcaps.max_queues >= 1); 263 lcaps.max_queues = caps.max_queues; 264 /* Field by field: OmegaGpuPlatformCaps has padding, which struct assignment need not copy (memcmp failed on x86-64). */ 265 EXPECT(caps.abi_major == lcaps.abi_major && caps.abi_minor == lcaps.abi_minor && caps.services == lcaps.services && 266 caps.max_devices == lcaps.max_devices && caps.max_buffers == lcaps.max_buffers && 267 caps.max_queues == lcaps.max_queues && caps.max_inflight == lcaps.max_inflight && 268 caps.max_buffer_bytes == lcaps.max_buffer_bytes && caps.max_total_bytes == lcaps.max_total_bytes && 269 caps.submit_prefix_words == lcaps.submit_prefix_words); 270 EXPECT(caps.services == OMEGA_GPU_SVC_REQUIRED_SESSION); 271 EXPECT(p->irq_wait == NULL && p->mmio_read32 == NULL && p->mmio_write32 == NULL); 272 EXPECT(omega_gpu_platform_check_for(p, &caps, OMEGA_GPU_SVC_REQUIRED_SESSION) == OMEGA_GPU_PLAT_OK); 273 EXPECT(omega_gpu_platform_negotiate(&caps, &lcaps) == OMEGA_GPU_PLAT_OK); 274 OmegaGpuPlatformCaps want = caps; want.services |= OMEGA_GPU_SVC_IRQ; 275 EXPECT(omega_gpu_platform_negotiate(&caps, &want) == OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE); 276 want = caps; want.abi_minor = caps.abi_minor + 1; 277 EXPECT(omega_gpu_platform_negotiate(&caps, &want) == OMEGA_GPU_PLAT_REFUSED_ABI); 278 } 279 280 static void c_absent_device(void) { 281 /* The real system calls, a device node that does not exist: a named refusal, nothing opened. */ 282 setenv("CUDAPP_AIEN_GPU_DEV", "/nonexistent/aien-gpu", 1); 283 EXPECT(omega_gpu_platform_aien_set_sys(NULL) == OMEGA_GPU_PLAT_OK); 284 const OmegaGpuPlatform *p = P(); 285 OmegaGpuDeviceHandle d = {9, 9}; 286 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_REFUSED_DEVICE); 287 EXPECT(d.index == 0 && d.generation == 0); 288 const char *t = p->diag_text(p->ctx); 289 printf(" diag: %s\n", t); 290 EXPECT(strstr(t, "aien_gpu_open: open /nonexistent/aien-gpu: No such file or directory") == t); 291 OmegaGpuBufferDesc dd = desc(OMEGA_GPU_HANDLE_BUF_DATA, RW, 0, 4096); 292 OmegaGpuBufferHandle b; OmegaCpuView c; OmegaGpuVa v; 293 EXPECT(p->dma_alloc(p->ctx, d, &dd, &b, &c, &v, NULL) == OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE); 294 EXPECT(p->device_open(p->ctx, 1, &d) == OMEGA_GPU_PLAT_REFUSED_ARGS); 295 /* the session reports it too, and stays usable */ 296 EXPECT(omega_gpu_session_set_platform(omega_gpu_platform_aien(NULL), &(OmegaGpuPlatformCaps){0}) != OMEGA_GPU_PLAT_OK); 297 OmegaGpuPlatformCaps caps; const OmegaGpuPlatform *ap = omega_gpu_platform_aien(&caps); 298 EXPECT(omega_gpu_session_set_platform(ap, &caps) == OMEGA_GPU_PLAT_OK); 299 omega_gpu_session_lock(); 300 EXPECT(omega_gpu_session_open() == NULL); 301 omega_gpu_session_unlock(); 302 } 303 304 static void c_not_ready(void) { 305 fake_install(); 306 const OmegaGpuPlatform *p = P(); 307 OmegaGpuDeviceHandle d; 308 fk.state = AGPU_STATE_GSP_UP; 309 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_REFUSED_DEVICE); 310 printf(" diag: %s\n", p->diag_text(p->ctx)); 311 EXPECT(strstr(p->diag_text(p->ctx), "state GSP_UP (3), not RM_READY") != NULL); 312 EXPECT(fk.closes == fk.opens); 313 fk.state = AGPU_STATE_RM_READY; fk.abi = 2; 314 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_REFUSED_DEVICE); 315 EXPECT(strstr(p->diag_text(p->ctx), "speaks ABI 2") != NULL); 316 fk.abi = AGPU_ABI_VERSION; 317 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK); 318 EXPECT(p->diag_text(p->ctx)[0] == '\0'); 319 OmegaGpuDeviceHandle d2; 320 EXPECT(p->device_open(p->ctx, 0, &d2) == OMEGA_GPU_PLAT_REFUSED_LIMIT); 321 EXPECT(omega_gpu_platform_aien_set_sys(NULL) == OMEGA_GPU_PLAT_REFUSED_BUSY); 322 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK); 323 EXPECT(fk.closes == fk.opens); 324 } 325 326 static void c_layout_matches_nvrm(void) { 327 static const uint64_t vas[] = { 0x1000, 0x7f12345000ull, 0xfffffffff000ull, 0x200000004ull, 0x123 }; 328 static const uint32_t ns[] = { 1, 6, 255, 0x1fffff, 0x200000 }; 329 for (unsigned i = 0; i < sizeof vas / sizeof vas[0]; i++) 330 for (unsigned j = 0; j < sizeof ns / sizeof ns[0]; j++) 331 EXPECT(omega_gpu_aien_gp_entry(vas[i], ns[j]) == nvrm_gp_entry(vas[i], ns[j])); 332 EXPECT(OMEGA_GPU_AIEN_USERD_GPPUT == offsetof(Nvc96fControl, GPPut)); 333 EXPECT(OMEGA_GPU_AIEN_USERD_OFF == 1024u * 8u); /* nvrm_channel: entries 1024, USERD after the ring */ 334 EXPECT(OMEGA_GPU_AIEN_USERD_OFF + sizeof(Nvc96fControl) <= OMEGA_GPU_AIEN_FIFO_BYTES); 335 } 336 337 static void c_alloc(void) { 338 fake_install(); 339 const OmegaGpuPlatform *p = P(); 340 OmegaGpuDeviceHandle d; 341 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK); 342 OmegaGpuBufferDesc dd = desc(OMEGA_GPU_HANDLE_BUF_DATA, POLLED, OMEGA_GPU_CACHE_GPU_UNCACHED, 5000); 343 dd.align = 1u << 16; 344 OmegaGpuBufferHandle b; OmegaCpuView c; OmegaGpuVa v; OmegaGpuBusAddr bus = {1}; 345 EXPECT(p->dma_alloc(p->ctx, d, &dd, &b, &c, &v, &bus) == OMEGA_GPU_PLAT_OK); 346 EXPECT((uint64_t)(uintptr_t)c.ptr == v.va && c.len == 8192 && (v.va & 0xffffu) == 0 && bus.iova == 0); 347 EXPECT(v.va >= (64ull << 30) && v.va + c.len <= (1ull << 40)); /* a GP entry carries 40 address bits */ 348 FakeObj *o = obj_at(v.va); 349 EXPECT(o && o->mapped && o->size == 8192 && o->flags == AGPU_MEM_GPU_UNCACHED); 350 EXPECT(!fk.bad_fixed_map); 351 memset(c.ptr, 0xa5, c.len); /* the mapping is real memory */ 352 /* GPU-cached: only device-private DATA */ 353 OmegaGpuBufferDesc dc = desc(OMEGA_GPU_HANDLE_BUF_DATA, OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ, OMEGA_GPU_CACHE_GPU_CACHED, 4096); 354 OmegaGpuBufferHandle bc; OmegaCpuView cc; OmegaGpuVa vc; 355 EXPECT(p->dma_alloc(p->ctx, d, &dc, &bc, &cc, &vc, NULL) == OMEGA_GPU_PLAT_OK); 356 EXPECT(obj_at(vc.va) && obj_at(vc.va)->flags == 0u); 357 dc.rights |= OMEGA_GPU_RIGHT_CPU_READ; 358 EXPECT(p->dma_alloc(p->ctx, d, &dc, &bc, &cc, &vc, NULL) == OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY); 359 OmegaGpuBufferDesc dk = desc(OMEGA_GPU_HANDLE_BUF_CODE, OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_EXEC, OMEGA_GPU_CACHE_GPU_CACHED, 4096); 360 EXPECT(p->dma_alloc(p->ctx, d, &dk, &bc, &cc, &vc, NULL) == OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY); 361 /* free: MEM_FREE, unmapped, stale */ 362 const int frees = fk.frees; 363 fk.fail_free = 1; 364 EXPECT(p->dma_free(p->ctx, b) == OMEGA_GPU_PLAT_REFUSED_DEVICE); 365 EXPECT(strstr(p->diag_text(p->ctx), "AGPU_MEM_FREE: AGPU_RM_E_ARG code 9") != NULL); 366 EXPECT(p->sync_cpu_to_gpu(p->ctx, b, 0, 4) == OMEGA_GPU_PLAT_OK); /* a refused free released nothing */ 367 fk.fail_free = 0; 368 EXPECT(p->dma_free(p->ctx, b) == OMEGA_GPU_PLAT_OK); 369 EXPECT(fk.frees == frees + 1 && obj_at(v.va) == NULL); 370 EXPECT(p->dma_free(p->ctx, b) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE); 371 uint32_t seen; 372 EXPECT(p->poll_word(p->ctx, b, 0, 1, 0, &seen) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE); 373 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK); 374 } 375 376 static void c_submit_poll(void) { 377 fake_install(); 378 const OmegaGpuPlatform *p = P(); 379 Rig g; 380 EXPECT(rig_open(&g) == 0); 381 EXPECT(fk.chan_creates == 1 && fk.chan.entries == 1024 && fk.chan.ring_off == 0 && fk.chan.userd_off == 8192 && 382 fk.chan.ring_handle == fk.chan.userd_handle && fk.chan.err_handle != fk.chan.ring_handle && fk.doorbell != NULL); 383 pthread_t th; g_gpu_stop = 0; 384 pthread_create(&th, NULL, gpu_thread, NULL); 385 uint32_t w[16], seen = 0; 386 volatile uint32_t *marker = g.marker_cpu.ptr; 387 *marker = 0; 388 for (uint32_t k = 1; k <= 3; k++) { 389 const uint32_t n = release_words(w, g.marker_va.va, 0x16c0ffe0u + k); 390 EXPECT(p->queue_submit(p->ctx, g.q, w, n) == OMEGA_GPU_PLAT_OK); 391 EXPECT(p->poll_word(p->ctx, g.marker, 0, 0x16c0ffe0u + k, 2000, &seen) == OMEGA_GPU_PLAT_OK); 392 EXPECT(seen == 0x16c0ffe0u + k); 393 EXPECT(gpput_word() == k); 394 const uint64_t e = ((volatile uint64_t *)(uintptr_t)g_fifo_va)[k - 1]; 395 EXPECT(e == nvrm_gp_entry(g.ring_va.va, n)); 396 EXPECT(p->queue_retire(p->ctx, g.q) == OMEGA_GPU_PLAT_OK); 397 } 398 EXPECT(g_gpu_entries_run == 3); 399 /* timeout: the word never reaches the value */ 400 EXPECT(p->poll_word(p->ctx, g.marker, 0, 0x7fffffffu, 5, &seen) == OMEGA_GPU_POLL_TIMED_OUT); 401 EXPECT(p->poll_word(p->ctx, g.ring, 0, 1, 0, &seen) == OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS); 402 EXPECT(p->poll_word(p->ctx, g.marker, 2, 1, 0, &seen) == OMEGA_GPU_PLAT_REFUSED_ARGS); 403 EXPECT(p->poll_word(p->ctx, g.marker, 4096, 1, 0, &seen) == OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH); 404 g_gpu_stop = 1; 405 pthread_join(th, NULL); 406 /* refusals ring no doorbell and change no ring state */ 407 *(volatile uint32_t *)(fk.doorbell + OMEGA_GPU_AIEN_DOORBELL_WORD) = 0; 408 EXPECT(p->queue_submit(p->ctx, g.q, w, 0x10000 / 4 + 1) == OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH); 409 EXPECT(p->queue_submit(p->ctx, g.q, NULL, 4) == OMEGA_GPU_PLAT_REFUSED_ARGS); 410 EXPECT(doorbell_word() == 0 && gpput_word() == 3); 411 /* the ring refuses its 1024th unretired entry, and a retire frees it */ 412 uint32_t ok = 0; 413 for (uint32_t k = 0; k < 1023; k++) ok += p->queue_submit(p->ctx, g.q, w, 6) == OMEGA_GPU_PLAT_OK; 414 EXPECT(ok == 1023 && doorbell_word() == FAKE_TOKEN); 415 EXPECT(gpput_word() == (3u + 1023u) % 1024u); 416 *(volatile uint32_t *)(fk.doorbell + OMEGA_GPU_AIEN_DOORBELL_WORD) = 0; 417 EXPECT(p->queue_submit(p->ctx, g.q, w, 6) == OMEGA_GPU_PLAT_REFUSED_DEVICE); 418 EXPECT(strstr(p->diag_text(p->ctx), "GPFIFO full: 1023 entries unretired") != NULL); 419 EXPECT(doorbell_word() == 0); 420 EXPECT(p->queue_retire(p->ctx, g.q) == OMEGA_GPU_PLAT_OK); 421 EXPECT(p->queue_submit(p->ctx, g.q, w, 6) == OMEGA_GPU_PLAT_OK && doorbell_word() == FAKE_TOKEN); 422 /* the queue's ring cannot be freed while the queue is open; a second queue is refused */ 423 EXPECT(p->dma_free(p->ctx, g.ring) == OMEGA_GPU_PLAT_REFUSED_BUSY); 424 OmegaGpuQueueHandle q2; 425 EXPECT(p->queue_open(p->ctx, g.dev, g.ring, &q2) == OMEGA_GPU_PLAT_REFUSED_LIMIT); 426 EXPECT(p->queue_open(p->ctx, g.dev, g.marker, &q2) == OMEGA_GPU_PLAT_REFUSED_BAD_KIND); 427 trace_state(&g); 428 EXPECT(p->queue_close(p->ctx, g.q) == OMEGA_GPU_PLAT_OK); 429 EXPECT(fk.chan_destroys == 1 && fk.doorbell == NULL); 430 EXPECT(p->queue_submit(p->ctx, g.q, w, 6) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE); 431 EXPECT(p->dma_free(p->ctx, g.ring) == OMEGA_GPU_PLAT_OK); 432 EXPECT(p->device_close(p->ctx, g.dev) == OMEGA_GPU_PLAT_OK); 433 } 434 435 static void c_chan_fail(void) { 436 fake_install(); 437 const OmegaGpuPlatform *p = P(); 438 Rig g; 439 fk.fail_chan = 1; 440 EXPECT(rig_open(&g) == -1); 441 printf(" diag: %s\n", p->diag_text(p->ctx)); 442 EXPECT(strstr(p->diag_text(p->ctx), "aien_gpu_chan_create: AGPU_CHAN_CREATE: AGPU_RM_E_RESULT code 31 fn 103") != NULL); 443 EXPECT(g.q.generation == 0); 444 uint32_t live = 0; 445 for (uint32_t i = 0; i < fk.nobj; i++) live += fk.obj[i].live; 446 EXPECT(live == 2); /* the ring and the marker; the fifo and notifier went back */ 447 fk.fail_chan = 0; 448 EXPECT(p->queue_open(p->ctx, g.dev, g.ring, &g.q) == OMEGA_GPU_PLAT_OK); 449 EXPECT(p->device_close(p->ctx, g.dev) == OMEGA_GPU_PLAT_OK); 450 } 451 452 static void c_l2_prefix(void) { 453 fake_install(); 454 const OmegaGpuPlatform *p = P(); 455 Rig g; 456 EXPECT(rig_open(&g) == 0); 457 OmegaGpuBufferDesc dc = desc(OMEGA_GPU_HANDLE_BUF_DATA, OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ, OMEGA_GPU_CACHE_GPU_CACHED, 4096); 458 OmegaGpuBufferHandle bc; OmegaCpuView cc; OmegaGpuVa vc; 459 EXPECT(p->dma_alloc(p->ctx, g.dev, &dc, &bc, &cc, &vc, NULL) == OMEGA_GPU_PLAT_OK); 460 uint32_t w[8]; const uint32_t n = release_words(w, g.marker_va.va, 5); 461 const uint32_t *pb = g.ring_cpu.ptr; 462 EXPECT(p->sync_cpu_to_gpu(p->ctx, g.marker, 0, 4) == OMEGA_GPU_PLAT_OK); /* uncached: no prefix */ 463 EXPECT(p->queue_submit(p->ctx, g.q, w, n) == OMEGA_GPU_PLAT_OK && pb[0] == w[0]); 464 EXPECT(p->sync_cpu_to_gpu(p->ctx, bc, 0, 4096) == OMEGA_GPU_PLAT_OK); 465 EXPECT(p->queue_submit(p->ctx, g.q, w, n) == OMEGA_GPU_PLAT_OK); 466 /* The whole omega #457 MEM_OP prefix (L2_SYSMEM_NCOH_INVALIDATE, as the Linux reference), then the words; traced 467 * here because the next submit reuses the pushbuffer head. */ 468 EXPECT(pb[0] == ((2u << 28) | (4u << 16) | (0x0028u >> 2)) && pb[1] == 0 && pb[2] == 0 && pb[3] == 0); 469 EXPECT(pb[4] == (0x11u << 27) && pb[5] == w[0]); 470 if (g_tfd >= 0) trace_hex("pushbuffer-prefixed", pb, 64); 471 EXPECT(((volatile uint64_t *)(uintptr_t)g_fifo_va)[1] == nvrm_gp_entry(g.ring_va.va, n + 5)); 472 EXPECT(p->queue_submit(p->ctx, g.q, w, n) == OMEGA_GPU_PLAT_OK && pb[0] == w[0]); /* once only */ 473 trace_state(&g); 474 EXPECT(p->device_close(p->ctx, g.dev) == OMEGA_GPU_PLAT_OK); 475 } 476 477 static void c_session(void) { 478 fake_install(); 479 OmegaGpuPlatformCaps caps; 480 const OmegaGpuPlatform *p = omega_gpu_platform_aien(&caps); 481 EXPECT(omega_gpu_session_set_platform(p, &caps) == OMEGA_GPU_PLAT_OK); 482 omega_gpu_session_lock(); 483 EXPECT(omega_gpu_session_open() != NULL); 484 EXPECT(fk.chan_creates == 1 && fk.maps_fixed >= 6 && !fk.bad_fixed_map); 485 OmegaGpuMem m; 486 EXPECT(omega_gpu_session_alloc(10000, &m) == 0); 487 EXPECT(m.cpu && (uint64_t)(uintptr_t)m.cpu == m.va && obj_at(m.va) && obj_at(m.va)->flags == AGPU_MEM_GPU_UNCACHED); 488 omega_gpu_session_free(&m); 489 omega_gpu_session_unlock(); 490 omega_gpu_session_close(); /* takes the lock itself (docs/runtime/API.md) */ 491 uint32_t live = 0; 492 for (uint32_t i = 0; i < fk.nobj; i++) live += fk.obj[i].live; 493 EXPECT(live == 0 && fk.chan_live == 0 && fk.closes == fk.opens); 494 } 495 496 static void c_close_releases(void) { 497 fake_install(); 498 const OmegaGpuPlatform *p = P(); 499 Rig g; 500 EXPECT(rig_open(&g) == 0); 501 EXPECT(p->device_close(p->ctx, g.dev) == OMEGA_GPU_PLAT_OK); 502 uint32_t live = 0; 503 for (uint32_t i = 0; i < fk.nobj; i++) live += fk.obj[i].live; 504 EXPECT(live == 0 && fk.chan_destroys == 1 && fk.closes == 1 && fk.doorbell == NULL); 505 uint32_t seen; 506 EXPECT(p->poll_word(p->ctx, g.marker, 0, 0, 0, &seen) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE); 507 EXPECT(p->queue_retire(p->ctx, g.q) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE); 508 EXPECT(p->device_close(p->ctx, g.dev) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE); 509 EXPECT(rig_open(&g) == 0); /* reopens cleanly */ 510 EXPECT(p->device_close(p->ctx, g.dev) == OMEGA_GPU_PLAT_OK); 511 } 512 513 typedef struct { const char *name; void (*fn)(void); } Case; 514 static const Case CASES[] = { 515 {"AIEN_CAPS_EQUAL_LINUX_REF", c_caps}, 516 {"AIEN_ABSENT_DEVICE_REFUSED", c_absent_device}, 517 {"AIEN_NOT_RM_READY_REFUSED", c_not_ready}, 518 {"AIEN_GP_ENTRY_USERD_MATCH_NVRM", c_layout_matches_nvrm}, 519 {"AIEN_ALLOC_VA_IDENTITY_CACHE_FREE", c_alloc}, 520 {"AIEN_SUBMIT_DOORBELL_POLL_RING", c_submit_poll}, 521 {"AIEN_CHAN_CREATE_FAIL_UNWINDS", c_chan_fail}, 522 {"AIEN_L2_INVAL_PREFIX", c_l2_prefix}, 523 {"AIEN_SESSION_OPEN_CLOSE", c_session}, 524 {"AIEN_DEVICE_CLOSE_RELEASES", c_close_releases}, 525 }; 526 527 int main(void) { 528 int fails = 0; 529 const unsigned n = sizeof CASES / sizeof CASES[0]; 530 const char *only = getenv("AIEN_TEST_CASE"); 531 for (unsigned i = 0; i < n; i++) { 532 if (only && strcmp(only, CASES[i].name) != 0) continue; 533 fflush(stdout); 534 pid_t pid = fork(); 535 if (pid == 0) { 536 const char *tp = getenv("AIEN_TRACE"); 537 if (tp && *tp) { 538 g_tfd = open(tp, O_WRONLY | O_CREAT | O_APPEND, 0644); 539 if (g_tfd >= 0) dprintf(g_tfd, "case %s\n", CASES[i].name); 540 } 541 alarm(30); case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); 542 } 543 int st = 0; 544 waitpid(pid, &st, 0); 545 const int ok = WIFEXITED(st) && WEXITSTATUS(st) == 0; 546 printf("%s %s\n", ok ? "PASS" : "FAIL", CASES[i].name); 547 fails += !ok; 548 } 549 printf("test_omega_gpu_platform_aien: %u cases, %d failed\n", n, fails); 550 return fails ? 1 : 0; 551 }