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/src/omega_blackwell_engine.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 / src / omega_blackwell_engine.c` 763 lines · 29184 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "omega_fixture_rest.h" 3 /* 4 * Omega GPU Engine, Blackwell GB10 backend (cut A3b1). See 5 * src/omega_blackwell_engine.h and docs/numeric/OMEGA_GPU_ENGINE.md. 6 * 7 * This file is the backend function table (OmegaGpuBackend) that the engine 8 * core in src/omega_gpu_engine.c drives. The core owns the state machine, the 9 * poison comparison, the uncertain-completion block and the cleanup order. 10 * This file owns only the device calls. Each callback below says which lines of 11 * the launcher it was lifted from. "SUB@`676f16f`" means 12 * omega_blackwell_execute_vector in src/omega_blackwell_submit.c at commit 13 * `676f16f` (the file before this cut). "ASTRA" means the first cut by GPT-6 14 * Astra, src/omega_blackwell_engine.c at commit `21392ee` on branch 15 * fix/omega-gpu-engine-first-cut. 16 * 17 * Roles. SCRATCH is the backend's own and holds four device buffers: the large 18 * pushbuffer (0x10000), the driver/argument constant bank (0x1000), the marker 19 * page (0x1000) and the QMD page (0x10000), the same sizes as SUB@`676f16f` 20 * lines 50, 59, 63, 64. PROGRAM, INPUT_A, INPUT_B and OUTPUT are page rounded 21 * with a 0x1000 minimum (SUB@`676f16f` lines 55-56, 58-62). 22 * 23 * L2 flush. The core has no separate callback for it, so build() emits the L2 24 * flush and the second release marker (payload 0x46464646 at marker page + 0x10) 25 * inside the pushbuffer unless the job sets NO_C3, exactly as divsqrt does in 26 * src/omega_numeric_divsqrt_gb10.c:1145-1152. The wait_marker2 callback waits 27 * for that marker. The CPU side of the visibility rule (dsb sy before readback) 28 * is the barrier callback (divsqrt line 1171). 29 * 30 * One run at a time. The context is one static object because a retained run 31 * (uncertain completion, nothing freed, nothing closed) must keep its device 32 * memory reachable for the life of the process, and because the native context 33 * is about 225 KiB (Nvrm in physics/nvrm/nvrm.h). A second open while a run is 34 * active fails with DEVICE_OPEN. 35 * 36 * NOT_RUN: this file has not been compiled or executed by its author. 37 */ 38 #include "omega_blackwell_engine.h" 39 #include "omega_blackwell_qmd.h" 40 #include "omega_blackwell_submit.h" 41 #include "m16_native.h" 42 #include "cpu_barrier.h" 43 /* Page rounding, the VECTOR_1D shape, the QMD1 release check and the device 44 * persistence decision come from om/engine/eng.om (op BWE, src/omega_eng_gates.h); 45 * the C oracle is kept behind CUDAPP_MIGRATION_PENDING_C_BWENGINE. */ 46 #if defined(CUDAPP_MIGRATION_PENDING_C_BWENGINE) 47 #define ENG_C_PART 1 48 #endif 49 #include "omega_eng_gates.h" 50 _Static_assert(OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE == 6u, "eng.om bwe k2 expects SEMAPHORE_INTERMEDIATE_DONE 6"); 51 52 #include 53 #include 54 #include 55 #include 56 #include 57 #include 58 #include 59 #include 60 61 #define BE_PB_CAP 1024u /* pushbuffer words, SUB@`676f16f` line 131 (uint32_t pb[1024]) */ 62 #define BE_PAGE 0x1000u 63 #define BE_PB_BYTES 0x10000u /* SUB@`676f16f` line 50 */ 64 #define BE_QMD_BYTES 0x10000u /* SUB@`676f16f` line 64 */ 65 #define BE_SEM_OFFSET 0x2000u /* sem_va = qmd page + 0x2000, SUB@`676f16f` line 99 */ 66 #define BE_MARKER2_OFFSET 0x10u /* divsqrt line 1113 and 1152 */ 67 #define BE_MARKER2_PAYLOAD 0x46464646u /* divsqrt line 1152 */ 68 69 typedef struct { 70 M16NativeContext m; 71 NvrmMem pb; /* SCRATCH part 1: large pushbuffer */ 72 NvrmMem cbank; /* SCRATCH part 2 */ 73 NvrmMem marker; /* SCRATCH part 3 */ 74 NvrmMem qmd; /* SCRATCH part 4 (QMD0, QMD1, semaphore) */ 75 NvrmMem buf[OMEGA_GPU_BUF_ROLE_COUNT]; /* PROGRAM, INPUT_A, INPUT_B, OUTPUT (SCRATCH slot unused) */ 76 int native_open; /* m16_native_open was attempted this run */ 77 int pb_replaced; /* m.pb_mem now points at the large pushbuffer */ 78 int c3; /* L2 flush and second marker are in the pushbuffer */ 79 volatile uint32_t *hmarker; /* NULL until build zeroes it; NULL again once SCRATCH is freed */ 80 volatile uint32_t *hmarker2; 81 volatile uint32_t *hsem; 82 uint32_t pb_words[BE_PB_CAP]; 83 size_t pb_len; 84 int pb_overflow; 85 int last_rc; /* raw driver code of the most recent callback */ 86 char text[OMEGA_GPU_ENGINE_DRV_TEXT_BYTES]; /* backend's own message, "" when none */ 87 OmegaBlackwellEngineRunInfo info; 88 } BeCtx; 89 90 static BeCtx g_be; 91 static atomic_flag g_busy = ATOMIC_FLAG_INIT; 92 93 /* Persistent device (omega-mv#575, measured 2026-10-19 on the Spark): of 38.8 ms per 94 * omega_gpu_execute job, open_device took 7.5 ms, create_channel 19.2 ms and close 95 * 11.3 ms; the work itself (alloc, copy, submit, wait, readback, free) under 1 ms. 96 * So the RM client, device and channel stay open between jobs. Per-job buffers and 97 * the scratch are still allocated and freed per job (the engine's state machine is 98 * unchanged). A full close happens when the driver reports a fault, when 99 * omega_blackwell_engine_shutdown() is called, or when OMEGA_GPU_ENGINE_NO_PERSIST=1 100 * is set in the environment (the old one-open-per-job behaviour, for A/B runs). 101 * A retained run (uncertain completion) never reaches be_close, as before. */ 102 /* nvrm_retire lives in physics/nvrm/nvrm.c; the simulated-driver host build 103 * (tests/fake_m16_native.c) links no nvrm, so the reference is weak there. */ 104 extern void nvrm_retire(Nvrm *rm, uint32_t upto) __attribute__((weak)); 105 static int g_dev_open; /* m16_native_open succeeded and was not closed */ 106 static int g_chan_open; /* m16_native_create_channel succeeded on that device */ 107 108 /* Reuse the open device: it is open, persistence is on (OMEGA_GPU_ENGINE_NO_PERSIST 109 * is not "1...") and the driver has not faulted. The decision is eng.om BWE k3. */ 110 static int be_reuse(int faulted) 111 { 112 const char *e = getenv("OMEGA_GPU_ENGINE_NO_PERSIST"); 113 int reuse = 0; 114 (void)omega_gate_persist(e != NULL, e ? (unsigned char)e[0] : 0u, g_dev_open, faulted, &reuse); 115 return reuse; 116 } 117 118 static uint64_t now_ns(void) 119 { 120 struct timespec t; 121 if (clock_gettime(CLOCK_MONOTONIC, &t) != 0) return 0u; 122 return omega_fixture_calc(FR_NS, (uint64_t)t.tv_sec, (uint64_t)t.tv_nsec, 0); 123 } 124 125 /* Bounded copy that never reads past a NUL and always terminates. */ 126 static void copy_text(char *dst, size_t cap, const char *src) 127 { 128 size_t i = 0; 129 if (cap == 0) return; 130 while (i + 1u < cap && src[i] != '\0') { 131 dst[i] = src[i]; 132 i++; 133 } 134 dst[i] = '\0'; 135 } 136 137 static void note(BeCtx *c, const char *msg) 138 { 139 copy_text(c->text, sizeof c->text, msg); 140 } 141 142 /* Start of every callback that can fail: forget the previous callback's code and text. */ 143 static void begin(BeCtx *c) 144 { 145 c->last_rc = 0; 146 c->text[0] = '\0'; 147 } 148 149 /* ---- allocation helpers ------------------------------------------------ */ 150 151 /* Allocates and checks one device buffer. The post-allocation checks (CPU 152 * mapping present, size at least what was asked, 256-byte aligned GPU address) 153 * come from ASTRA checked_alloc. Returns 0 or non-zero. */ 154 static int alloc_checked(BeCtx *c, uint64_t bytes, NvrmMem *m) 155 { 156 int rc; 157 memset(m, 0, sizeof *m); 158 /* GB10: the CPU rewrites pushbuffers and reads markers/outputs; GPU-cacheable system memory is not 159 * coherent with CPU mappings (nvos.h:1117-1118). All engine buffers are GPU-uncached until the 160 * allocator default is flipped (hive idea I36). */ 161 rc = nvrm_alloc_gpu_uncached(&c->m.rm, bytes, m); 162 if (rc != 0) { 163 c->last_rc = rc; 164 memset(m, 0, sizeof *m); 165 return rc; 166 } 167 if (!m->cpu || m->size < bytes || (m->va & 255u) != 0) { 168 note(c, "allocation failed its own checks (CPU mapping, size or 256-byte alignment)"); 169 (void)nvrm_free(&c->m.rm, m); 170 memset(m, 0, sizeof *m); 171 return -1; 172 } 173 return 0; 174 } 175 176 /* Page rounding with a 0x1000 minimum, SUB@`676f16f` lines 55-56. Non-zero if bytes is 0 or overflows. */ 177 static int round_pages(size_t bytes, uint64_t *out) 178 { 179 return omega_gate_round_pages((uint64_t)bytes, out); 180 } 181 182 static NvrmMem *role_mem(BeCtx *c, int role) 183 { 184 if (role < (int)OMEGA_GPU_BUF_PROGRAM || role >= (int)OMEGA_GPU_BUF_ROLE_COUNT) return NULL; 185 return &c->buf[role]; 186 } 187 188 /* Frees the four SCRATCH buffers, every one even if an earlier one fails. 189 * Returns 0, or the first non-zero nvrm_free code. */ 190 static int release_scratch(BeCtx *c) 191 { 192 NvrmMem *parts[4]; 193 int first = 0; 194 size_t i; 195 parts[0] = &c->qmd; 196 parts[1] = &c->marker; 197 parts[2] = &c->cbank; 198 parts[3] = &c->pb; 199 /* The marker and semaphore words live in these buffers: stop reading them first. */ 200 c->hmarker = NULL; 201 c->hmarker2 = NULL; 202 c->hsem = NULL; 203 for (i = 0; i < 4u; i++) { 204 int rc = nvrm_free(&c->m.rm, parts[i]); /* a zeroed NvrmMem is a no-op (nvrm.h) */ 205 if (rc != 0 && first == 0) first = rc; 206 } 207 if (c->pb_replaced) { 208 memset(&c->m.pb_mem, 0, sizeof c->m.pb_mem); 209 c->pb_replaced = 0; 210 } 211 return first; 212 } 213 214 /* ---- pushbuffer helpers ------------------------------------------------ */ 215 216 static void pb_put(BeCtx *c, uint32_t w) 217 { 218 if (c->pb_len < BE_PB_CAP) c->pb_words[c->pb_len++] = w; 219 else c->pb_overflow = 1; 220 } 221 222 static void pb_copy(BeCtx *c, const uint32_t *w, size_t n) 223 { 224 size_t i; 225 for (i = 0; i < n; i++) pb_put(c, w[i]); 226 } 227 228 /* Compute class and engine setup methods, SUB@`676f16f` lines 12-16 (also in divsqrt as SETUP). */ 229 static const uint32_t BE_SETUP_WORDS[18] = { 230 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, 231 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, 232 0x20012559, 0x00000000, 233 }; 234 235 /* ---- callbacks --------------------------------------------------------- */ 236 237 /* Opens the RM client. Resets every bit of the context first so an open 238 * failure never reports values from an earlier run (ASTRA did memset(&ctx) at 239 * the same point). The busy flag stays set until be_close. */ 240 static int be_open_device(void *ctx) 241 { 242 BeCtx *c = (BeCtx *)ctx; 243 M16NativeContext keep; 244 int reuse; 245 int rc; 246 #if !defined(__aarch64__) && !defined(OMEGA_BLACKWELL_HOST_SIMULATOR) 247 /* The production device is the GB10 on AArch64 Linux. A portable host 248 * compiles this file and refuses to open it. The fake-driver tests define 249 * OMEGA_BLACKWELL_HOST_SIMULATOR. That define does not create an x86 GPU. */ 250 c->last_rc = -1; 251 note(c, "Blackwell GPU backend requires AArch64 Linux"); 252 errno = ENOTSUP; 253 return -1; 254 #endif 255 if (atomic_flag_test_and_set(&g_busy)) return -1; /* another run is active; touch nothing */ 256 reuse = be_reuse(c->m.rm.faulted); 257 if (g_dev_open && !reuse) { 258 /* persistence off, or the driver faulted: close the old device first */ 259 (void)m16_native_close(&c->m); 260 g_dev_open = 0; 261 g_chan_open = 0; 262 } 263 keep = c->m; 264 memset(c, 0, sizeof *c); 265 c->native_open = 1; 266 if (reuse) { 267 c->m = keep; /* the open device and channel carry over; every per-job field is fresh */ 268 return 0; 269 } 270 rc = m16_native_open(&c->m); /* SUB@`676f16f` line 45 */ 271 if (rc != 0) { 272 c->last_rc = rc; 273 atomic_flag_clear(&g_busy); /* native open already cleaned up after itself (m16_native.c:15-21) */ 274 return rc; 275 } 276 g_dev_open = 1; 277 return 0; 278 } 279 280 static int be_create_channel(void *ctx) 281 { 282 BeCtx *c = (BeCtx *)ctx; 283 int rc; 284 begin(c); 285 if (g_chan_open) return 0; /* persistent channel from an earlier job */ 286 rc = m16_native_create_channel(&c->m); /* SUB@`676f16f` line 46 */ 287 c->last_rc = rc; 288 if (rc == 0) g_chan_open = 1; 289 return rc; 290 } 291 292 static int be_alloc(void *ctx, int role, size_t bytes) 293 { 294 BeCtx *c = (BeCtx *)ctx; 295 NvrmMem *m; 296 uint64_t sz; 297 begin(c); 298 if (role == (int)OMEGA_GPU_BUF_SCRATCH) { 299 if (alloc_checked(c, BE_PB_BYTES, &c->pb) != 0 || 300 alloc_checked(c, BE_PAGE, &c->cbank) != 0 || 301 alloc_checked(c, BE_PAGE, &c->marker) != 0 || 302 alloc_checked(c, BE_QMD_BYTES, &c->qmd) != 0) { 303 int rc = c->last_rc ? c->last_rc : -1; 304 int saved_errno = errno; /* the core reads errno right after this callback */ 305 char keep[OMEGA_GPU_ENGINE_DRV_TEXT_BYTES]; 306 copy_text(keep, sizeof keep, c->text); 307 (void)release_scratch(c); /* the core does not free a role whose alloc failed */ 308 copy_text(c->text, sizeof c->text, keep); 309 c->last_rc = rc; 310 errno = saved_errno; 311 return rc; 312 } 313 c->m.pb_mem = c->pb; /* the unified method stream needs the large ring, SUB@`676f16f` lines 49-51 */ 314 c->pb_replaced = 1; 315 return 0; 316 } 317 m = role_mem(c, role); 318 if (!m) { 319 note(c, "alloc: unknown buffer role"); 320 return -1; 321 } 322 if (round_pages(bytes, &sz) != 0) { 323 note(c, "alloc: byte count is zero or too large"); 324 return -1; 325 } 326 return alloc_checked(c, sz, m); 327 } 328 329 static int be_copy_in(void *ctx, int role, const void *src, size_t bytes) 330 { 331 BeCtx *c = (BeCtx *)ctx; 332 NvrmMem *m; 333 begin(c); 334 m = role_mem(c, role); 335 if (!m || !m->cpu || !src || (uint64_t)bytes > m->size) { 336 note(c, "copy_in: bad role, buffer or length"); 337 return -1; 338 } 339 memcpy(m->cpu, src, bytes); /* SUB@`676f16f` lines 74-76 (code via the encoder, inputs via memcpy) */ 340 return 0; 341 } 342 343 static int be_fill_poison(void *ctx, const uint32_t *words, size_t count) 344 { 345 BeCtx *c = (BeCtx *)ctx; 346 NvrmMem *out = &c->buf[OMEGA_GPU_BUF_OUTPUT]; 347 begin(c); 348 if (!out->cpu || !words || (uint64_t)count * 4u > out->size) { 349 note(c, "fill_poison: no output buffer or length too large"); 350 return -1; 351 } 352 memcpy(out->cpu, words, count * 4u); /* SUB@`676f16f` lines 77-80: pre-fill the device output */ 353 return 0; 354 } 355 356 /* Descriptors, QMDs and the whole pushbuffer. Lifted from SUB@`676f16f` lines 357 * 83-194 and, for the C3 tail, divsqrt lines 1145-1152. Zeroes the marker and 358 * semaphore words and issues dsb sy (SUB@`676f16f` lines 124-128). Does not submit. */ 359 static int be_build(void *ctx, const OmegaGpuJob *job) 360 { 361 BeCtx *c = (BeCtx *)ctx; 362 const NvrmMem *code = &c->buf[OMEGA_GPU_BUF_PROGRAM]; 363 const NvrmMem *a = &c->buf[OMEGA_GPU_BUF_INPUT_A]; 364 const NvrmMem *b = &c->buf[OMEGA_GPU_BUF_INPUT_B]; 365 const NvrmMem *out = &c->buf[OMEGA_GPU_BUF_OUTPUT]; 366 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; 367 uint32_t cbank_args[OMEGA_BW_CBANK_ARGS_WORDS]; 368 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS]; 369 uint32_t qmd1_words[OMEGA_BW_QMD_WORDS]; 370 OmegaBlackwellQmdConfig cfg; 371 uint64_t qmd0_va, qmd1_va, sem_va, marker_va, marker2_va, need; 372 uint32_t n, cfg_grid = 0, cfg_tpb = 0; 373 int rc; 374 375 begin(c); 376 if (!job || job->layout != (int)OMEGA_GPU_LAYOUT_VECTOR_1D) { 377 note(c, "build: only the VECTOR_1D layout is supported"); 378 return -1; 379 } 380 n = job->element_count; 381 /* (n + 63) / 64 must not overflow; ASTRA refused n above UINT32_MAX - 63 as well. */ 382 { 383 uint32_t gw, tpb; 384 if (omega_gate_vec_shape(n, &gw, &tpb, &need) != 0) { 385 note(c, "build: element count is zero or too large"); 386 return -1; 387 } 388 cfg_grid = gw; 389 cfg_tpb = tpb; 390 } 391 if (!code->cpu || !a->cpu || !b->cpu || !out->cpu || !c->pb.cpu || !c->cbank.cpu || 392 !c->marker.cpu || !c->qmd.cpu || a->size < need || b->size < need || out->size < need) { 393 note(c, "build: a buffer is missing or too small"); 394 return -1; 395 } 396 c->c3 = (job->flags & OMEGA_GPU_ENGINE_FLAG_NO_C3) == 0; 397 398 qmd0_va = c->qmd.va; 399 qmd1_va = c->qmd.va + 0x1000; 400 sem_va = c->qmd.va + BE_SEM_OFFSET; 401 marker_va = c->marker.va; 402 marker2_va = c->marker.va + BE_MARKER2_OFFSET; 403 404 memset(&cfg, 0, sizeof cfg); 405 cfg.code_va = code->va; 406 cfg.cbank_va = c->cbank.va; 407 cfg.scratch_va = c->qmd.va + 0x4000; /* SUB@`676f16f` line 100 */ 408 cfg.sem_va = sem_va; 409 cfg.qmd0_va = qmd0_va; 410 cfg.qmd1_va = qmd1_va; 411 cfg.num_elements = n; 412 cfg.threads_per_block = cfg_tpb; 413 cfg.grid_width = cfg_grid; 414 415 /* The builders return 0 on success (src/omega_blackwell_qmd.c); SUB@`676f16f` 416 * ignored their codes, ASTRA checked them. Checked here. */ 417 rc = omega_blackwell_build_cbank_driver(cbank_data, c->cbank.va); 418 if (rc == 0) rc = omega_blackwell_build_cbank_args(cbank_args, a->va, b->va, out->va, n); 419 if (rc == 0) rc = omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); 420 if (rc == 0) rc = omega_blackwell_build_qmd1(qmd1_words, &cfg); 421 if (rc == 0) rc = omega_blackwell_verify_qmd_invariants(qmd1_words); /* SUB@`676f16f` line 118 */ 422 if (rc != 0) { 423 c->last_rc = rc; 424 note(c, "build: a descriptor builder or the QMD invariant check refused"); 425 return rc; 426 } 427 /* Extra QMD1 checks from ASTRA: release enable (word 9), the release 428 * semaphore address and the DONE value (words 15-17), and the memory 429 * barrier flags (word 19). The words come from src/omega_blackwell_qmd.c 430 * lines 81, 93-98. */ 431 if (!omega_gate_qmd1_release(qmd1_words[9], qmd1_words[15], qmd1_words[16], qmd1_words[17], qmd1_words[19], 432 sem_va)) { 433 note(c, "build: QMD1 release words do not match the expected layout"); 434 return -1; 435 } 436 437 memcpy(c->cbank.cpu, cbank_data, sizeof cbank_data); /* SUB@`676f16f` line 112 */ 438 memcpy((uint8_t *)c->cbank.cpu + 0x380, cbank_args, sizeof cbank_args); /* line 113 */ 439 memcpy(c->qmd.cpu, qmd0_words, sizeof qmd0_words); /* line 120 */ 440 memcpy((uint8_t *)c->qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words); /* line 121 */ 441 442 /* Initialise the synchronisation words in coherent memory (lines 124-128; 443 * divsqrt also zeroes the second marker, line 1113). Then dsb sy so the 444 * zeros are visible to the chip before the doorbell. */ 445 c->hsem = (volatile uint32_t *)((uint8_t *)c->qmd.cpu + BE_SEM_OFFSET); 446 c->hmarker = (volatile uint32_t *)c->marker.cpu; 447 c->hmarker2 = (volatile uint32_t *)((uint8_t *)c->marker.cpu + BE_MARKER2_OFFSET); 448 *c->hsem = 0; 449 *c->hmarker = 0; 450 *c->hmarker2 = 0; 451 if (!c->c3) c->hmarker2 = NULL; /* never waited for, never reported */ 452 nvrm_cpu_barrier(); /* AArch64: dsb sy. See nvrm/cpu_barrier.h. */ 453 454 /* ---- unified pushbuffer, SUB@`676f16f` lines 130-195 ---- */ 455 c->pb_len = 0; 456 c->pb_overflow = 0; 457 458 /* 1. setup words (18) */ 459 pb_copy(c, BE_SETUP_WORDS, 18u); 460 461 /* 1b. invalidate the shader instruction, data and constant caches (clcec0.h 462 * NVCEC0_INVALIDATE_SHADER_CACHES 0x021c). With the device kept open between 463 * jobs (omega-mv#575) the PROGRAM buffer of the next job can land at the 464 * address of the last one, and the chip then ran the OLD code from its 465 * instruction cache: the no-store control in tests/test_omega_gpu_engine_gb10.c 466 * caught it on the Spark 2026-10-19 (unchanged=0). The session path has done 467 * this every launch since cut 1b (src/omega_gpu_session.c). */ 468 pb_put(c, nvrm_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1)); 469 pb_put(c, OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL); 470 471 /* 2. driver constant bank upload (224 words by DMA) */ 472 pb_put(c, nvrm_mthd(1, 0x0188, 2)); 473 pb_put(c, (uint32_t)(c->cbank.va >> 32)); 474 pb_put(c, (uint32_t)c->cbank.va); 475 pb_put(c, nvrm_mthd(1, 0x0180, 2)); 476 pb_put(c, 0x00000380); 477 pb_put(c, 0x00000001); 478 pb_put(c, nvrm_mthd(1, 0x01b0, 1)); 479 pb_put(c, 0x00000041); 480 pb_put(c, (uint32_t)((224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28))); 481 pb_copy(c, cbank_data, 224u); 482 483 /* 3. kernel argument upload (7 words by DMA) */ 484 pb_put(c, nvrm_mthd(1, 0x0188, 2)); 485 pb_put(c, (uint32_t)((c->cbank.va + 0x380) >> 32)); 486 pb_put(c, (uint32_t)(c->cbank.va + 0x380)); 487 pb_put(c, nvrm_mthd(1, 0x0180, 2)); 488 pb_put(c, 0x0000001c); 489 pb_put(c, 0x00000001); 490 pb_put(c, nvrm_mthd(1, 0x01b0, 1)); 491 pb_put(c, 0x00000041); 492 pb_put(c, (uint32_t)((7 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28))); 493 pb_copy(c, cbank_args, 7u); 494 495 /* 4. inline QMD 0 (2 address words + 96 QMD words) */ 496 pb_put(c, (uint32_t)((98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28))); 497 pb_put(c, (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff)); 498 pb_put(c, (uint32_t)(qmd0_va >> 8)); 499 pb_copy(c, qmd0_words, 96u); 500 501 /* 5. intermediate semaphore initial value (one word by DMA) */ 502 pb_put(c, nvrm_mthd(1, 0x0188, 2)); 503 pb_put(c, (uint32_t)(sem_va >> 32)); 504 pb_put(c, (uint32_t)sem_va); 505 pb_put(c, nvrm_mthd(1, 0x0180, 2)); 506 pb_put(c, 0x00000004); 507 pb_put(c, 0x00000001); 508 pb_put(c, nvrm_mthd(1, 0x01b0, 1)); 509 pb_put(c, 0x00000041); 510 pb_put(c, (uint32_t)((1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28))); 511 pb_put(c, OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT); 512 513 /* 6. inline QMD 1 */ 514 pb_put(c, (uint32_t)((98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28))); 515 pb_put(c, (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff)); 516 pb_put(c, (uint32_t)(qmd1_va >> 8)); 517 pb_copy(c, qmd1_words, 96u); 518 519 /* 7. subchannel 0 completion release (RELEASE | WFI) */ 520 pb_put(c, nvrm_mthd(0, 0x005c, 5)); 521 pb_put(c, (uint32_t)marker_va); 522 pb_put(c, (uint32_t)(marker_va >> 32)); 523 pb_put(c, OMEGA_BW_MARKER_COMPLETION_PAYLOAD); 524 pb_put(c, 0); 525 pb_put(c, 0x1u | (1u << 20)); 526 527 /* 8. C3, divsqrt lines 1145-1152 (NVC96F_MEM_OP_A..D = 0x28..0x34, operation 528 * L2_FLUSH_DIRTY 0x10 in bits 31:27; third_party/nvidia-open-580.173.02/src/ 529 * common/sdk/nvidia/inc/class/clc96f.h:36-73): flush the L2, then a second 530 * WFI release at marker page + 0x10 that the host waits for before readback. */ 531 if (c->c3) { 532 pb_put(c, nvrm_mthd(0, 0x0028, 4)); 533 pb_put(c, 0); 534 pb_put(c, 0); 535 pb_put(c, 0); 536 pb_put(c, 0x10u << 27); 537 pb_put(c, nvrm_mthd(0, 0x005c, 5)); 538 pb_put(c, (uint32_t)marker2_va); 539 pb_put(c, (uint32_t)(marker2_va >> 32)); 540 pb_put(c, BE_MARKER2_PAYLOAD); 541 pb_put(c, 0); 542 pb_put(c, 0x1u | (1u << 20)); 543 } 544 545 if (c->pb_overflow) { 546 note(c, "build: pushbuffer exceeded its word capacity"); 547 return -1; 548 } 549 return 0; 550 } 551 552 /* Submit methods to the GPFIFO ring and ring the doorbell, SUB@`676f16f` lines 553 * 198-200. m16_native_submit_methods rings only after the enqueue succeeded 554 * (physics/m16/m16_native.c:76-84), so a non-zero return means the doorbell 555 * was not rung: a known failure, not an uncertain completion. */ 556 static int be_submit(void *ctx) 557 { 558 BeCtx *c = (BeCtx *)ctx; 559 int rc; 560 begin(c); 561 if (c->pb_len == 0) { 562 note(c, "submit: nothing was built"); 563 return -1; 564 } 565 c->info.launch_ns = now_ns(); /* SUB@`676f16f` line 199 */ 566 rc = m16_native_submit_methods(&c->m, c->pb_words, c->pb_len); 567 c->last_rc = rc; 568 return rc; 569 } 570 571 /* Waits for one coherent word. m16_native_wait_marker is a ">=" serial compare 572 * (physics/m16/m16_native.c:93-108), so an overshoot also returns 0; ASTRA 573 * checked_alloc/checked_wait therefore re-checked equality, kept here and 574 * reported as UNKNOWN_STATE (the device wrote a value we cannot classify). 575 * Returns 0, OMEGA_GPU_BACKEND_TIMEOUT, OMEGA_GPU_BACKEND_UNKNOWN_STATE or a 576 * driver fault. *snap always receives the last value read. */ 577 static int wait_word(BeCtx *c, volatile uint32_t *w, uint32_t want, uint64_t ms, uint32_t *snap) 578 { 579 int rc; 580 begin(c); 581 if (!w) { 582 note(c, "wait: the word is not mapped (wait before build or after free)"); 583 return -2; 584 } 585 rc = m16_native_wait_marker(w, want, ms); 586 *snap = *w; 587 if (rc != 0) { 588 c->last_rc = rc; 589 return rc == -1 ? OMEGA_GPU_BACKEND_TIMEOUT : -2; 590 } 591 if (*snap != want) { 592 (void)snprintf(c->text, sizeof c->text, "observed 0x%08x, wanted 0x%08x", *snap, want); 593 return OMEGA_GPU_BACKEND_UNKNOWN_STATE; 594 } 595 return 0; 596 } 597 598 static int be_wait_marker(void *ctx, uint64_t timeout_ms) 599 { 600 BeCtx *c = (BeCtx *)ctx; 601 int rc = wait_word(c, c->hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout_ms, &c->info.marker); 602 if (rc == 0) c->info.marker_done_ns = now_ns(); /* SUB@`676f16f` line 204 */ 603 return rc; 604 } 605 606 static int be_wait_marker2(void *ctx, uint64_t timeout_ms) 607 { 608 BeCtx *c = (BeCtx *)ctx; 609 return wait_word(c, c->hmarker2, BE_MARKER2_PAYLOAD, timeout_ms, &c->info.marker2); 610 } 611 612 /* The QMD's own release semaphore, written after the grid finishes (divsqrt 613 * lines 1163-1169). SUB@`676f16f` only compared it once (line 207); now it is waited for. */ 614 static int be_wait_release_semaphore(void *ctx, uint64_t timeout_ms) 615 { 616 BeCtx *c = (BeCtx *)ctx; 617 return wait_word(c, c->hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE, timeout_ms, 618 &c->info.semaphore); 619 } 620 621 /* dsb sy before readback, divsqrt line 1171. Completion is already known here. */ 622 static int be_barrier(void *ctx, uint64_t timeout_ms) 623 { 624 BeCtx *c = (BeCtx *)ctx; 625 (void)timeout_ms; 626 begin(c); 627 nvrm_cpu_barrier(); /* AArch64: dsb sy before readback. */ 628 return 0; 629 } 630 631 static int be_copy_out(void *ctx, void *dst, size_t bytes) 632 { 633 BeCtx *c = (BeCtx *)ctx; 634 const NvrmMem *out = &c->buf[OMEGA_GPU_BUF_OUTPUT]; 635 begin(c); 636 if (!out->cpu || !dst || (uint64_t)bytes > out->size) { 637 note(c, "copy_out: no output buffer or length too large"); 638 return -1; 639 } 640 memcpy(dst, out->cpu, bytes); /* SUB@`676f16f` line 210 */ 641 return 0; 642 } 643 644 /* Driver code, driver text and the sync words as they are right now. Words are 645 * read only while SCRATCH is still mapped (hmarker and friends are NULL before 646 * build and after SCRATCH is freed), so this is safe during cleanup. */ 647 static void be_diagnostics(void *ctx, OmegaGpuBackendDiag *d) 648 { 649 BeCtx *c = (BeCtx *)ctx; 650 d->drv_rc = c->last_rc; 651 if (c->text[0] != '\0') copy_text(d->drv_text, sizeof d->drv_text, c->text); 652 else if (c->native_open) copy_text(d->drv_text, sizeof d->drv_text, c->m.rm.err); 653 if (c->hmarker) { 654 d->marker = *c->hmarker; 655 d->sync_valid |= OMEGA_GPU_SYNC_MARKER; 656 } 657 if (c->hmarker2) { 658 d->marker2 = *c->hmarker2; 659 d->sync_valid |= OMEGA_GPU_SYNC_MARKER2; 660 } 661 if (c->hsem) { 662 d->semaphore[0] = *c->hsem; 663 d->sync_valid |= OMEGA_GPU_SYNC_SEMAPHORE; 664 } 665 } 666 667 /* Real nvrm_free per buffer. SUB@`676f16f` never freed (m16_native_close frees 668 * everything left, nvrm.c:757-766); nvrm_free before close is already used by 669 * src/omega_accelerator_world.c (A1b scout, section 4). DIVERGENCE, chip 670 * behaviour of free-then-close for this launcher is UNVERIFIED (confidence 70%). */ 671 static int be_free_buf(void *ctx, int role) 672 { 673 BeCtx *c = (BeCtx *)ctx; 674 NvrmMem *m; 675 int rc; 676 begin(c); 677 if (role == (int)OMEGA_GPU_BUF_SCRATCH) { 678 rc = release_scratch(c); 679 c->last_rc = rc; 680 return rc; 681 } 682 m = role_mem(c, role); 683 if (!m) { 684 note(c, "free: unknown buffer role"); 685 return -1; 686 } 687 rc = nvrm_free(&c->m.rm, m); 688 c->last_rc = rc; 689 return rc; 690 } 691 692 static int be_close(void *ctx) 693 { 694 BeCtx *c = (BeCtx *)ctx; 695 int rc = 0; 696 begin(c); 697 c->hmarker = NULL; 698 c->hmarker2 = NULL; 699 c->hsem = NULL; 700 if (be_reuse(c->m.rm.faulted)) { 701 /* Keep the device and channel. The job's GPFIFO entries are complete (the 702 * engine only reaches close after the completion waits or on a failure before 703 * submit), so retire them; the ring never fills across jobs. */ 704 if (nvrm_retire) nvrm_retire(&c->m.rm, c->m.rm.put); 705 atomic_flag_clear(&g_busy); 706 return 0; 707 } 708 rc = m16_native_close(&c->m); /* SUB@`676f16f` line 225 */ 709 c->last_rc = rc; 710 g_dev_open = 0; 711 g_chan_open = 0; 712 atomic_flag_clear(&g_busy); 713 return rc; 714 } 715 716 int omega_blackwell_engine_shutdown(void) 717 { 718 BeCtx *c = &g_be; 719 int rc; 720 if (atomic_flag_test_and_set(&g_busy)) 721 return -1; /* a run is active (or retained): leave it */ 722 if (!g_dev_open) { atomic_flag_clear(&g_busy); return 0; } 723 rc = m16_native_close(&g_be.m); 724 g_dev_open = 0; 725 g_chan_open = 0; 726 memset(c, 0, sizeof(BeCtx)); 727 atomic_flag_clear(&g_busy); 728 return rc; 729 } 730 731 int omega_blackwell_engine_device_is_open(void) 732 { 733 return g_dev_open; 734 } 735 736 static const OmegaGpuBackend g_backend = { 737 &g_be, 738 be_open_device, 739 be_create_channel, 740 be_alloc, 741 be_copy_in, 742 be_fill_poison, 743 be_build, 744 be_submit, 745 be_wait_marker, 746 be_wait_marker2, 747 be_wait_release_semaphore, 748 be_barrier, 749 be_copy_out, 750 be_diagnostics, 751 be_free_buf, 752 be_close, 753 }; 754 755 const OmegaGpuBackend *omega_blackwell_engine_backend(void) 756 { 757 return &g_backend; 758 } 759 760 void omega_blackwell_engine_run_info(OmegaBlackwellEngineRunInfo *out) 761 { 762 if (out) *out = g_be.info; 763 }