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