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_gpu_session.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_gpu_session.c` 886 lines · 42312 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * omega_gpu_session.c: FB-1 cut 4b, made portable in native v1 P07 cut C (session part). See the header. 4 * 5 * The launch words are the ones omega_gpu_matmul_api.c (cut 1b, chip PASS FB1-CUT1B-`4345406`) and 6 * omega_gpu_elementwise_api.c (cut 4, FB1-CUT4-`0d0241a`) both emitted; cut C did not change one of them. What 7 * changed is where the machine comes from: every device, buffer, queue, clock, lock and host-memory call goes 8 * through the installed OmegaGpuPlatform (ABI 1.1), so this file builds freestanding (OMEGA_GPU_NO_POSIX) with no 9 * Linux header and no libc call. The hosted build installs the Linux reference (src/omega_gpu_platform_linux.c) 10 * on first use; it makes the same driver calls in the same order as this file did before. 11 * 12 * No read-modify-write atomics: the freestanding target must not pull libgcc outline atomics, so shared words are 13 * plain atomic loads and stores, and counters are only written with the session lock held. 14 */ 15 #include "omega_gpu_session.h" 16 #include "omega_gpu_code_alloc.h" 17 #include "cudapp_native.h" 18 #include "omega_no_posix_session.h" 19 #include "omega_gpu_fmt.h" 20 #include "omega_blackwell_qmd.h" 21 #include "omega_ampere_qmd.h" 22 #include "omega_blackwell_submit.h" /* launch constants only (macros); the vector executor itself stays Linux-private */ 23 #include 24 25 #ifndef OMEGA_GPU_NO_POSIX 26 #include "omega_gpu_platform_linux.h" 27 #include 28 #endif 29 #ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF 30 void cudapp_streams_diff_note(int same, const char *line); /* diff builds: tests/streams_diff_test.c */ 31 #endif 32 #ifdef CUDAPP_MIGRATION_PENDING_SESSION_DIFF 33 void cudapp_session_diff_note(int same, const char *line); /* diff builds: tests/session_diff_test.c */ 34 #endif 35 36 /* Session admission, bookkeeping and method-word decisions are Omega 37 * (om/session/admit.om, om/session/park.om, om/session/words.om). The C copies 38 * are the default-OFF differential oracle. */ 39 #if (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION) || defined(CUDAPP_MIGRATION_PENDING_C_SESSION) || defined(CUDAPP_MIGRATION_PENDING_SESSION_DIFF) 40 #define SESSION_C_ORACLE 1 41 #endif 42 43 static uint64_t ses(uint64_t op, uint64_t i0, uint64_t i1, uint64_t i2, uint64_t i3, uint64_t i4, uint64_t *out0); 44 45 46 /* Differential oracle only. Production uses the checked Omega C5 decisions. */ 47 #ifndef CUDAPP_MIGRATION_PENDING_C_SESSION_TIMEOUT 48 #define CUDAPP_MIGRATION_PENDING_C_SESSION_TIMEOUT 0 49 #endif 50 #define PAGE 0x1000ull 51 52 /* Push-buffer method header (the nvrm_mthd encoding, physics nvrm.h): increasing, count words, subchannel. */ 53 #ifdef SESSION_C_ORACLE 54 static inline uint32_t bw_mthd(uint32_t subc, uint32_t mthd, uint32_t count) { 55 return (2u << 28) | (count << 16) | (subc << 13) | (mthd >> 2); 56 } 57 #endif 58 59 #ifdef SESSION_C_ORACLE 60 /* Same words as src/omega_blackwell_submit.c SETUP_WORDS (compute class setup). */ 61 static const uint32_t SETUP_WORDS[18] = { 62 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, 63 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, 64 0x20012559, 0x00000000, 65 }; 66 #endif 67 68 static const OmegaGpuPlatform *g_p; /* installed platform (atomic load / store) */ 69 static OmegaGpuPlatformCaps g_caps; 70 static OmegaGpuSession g_s; 71 static bool g_open; 72 static uint32_t g_target_sm; 73 static uint64_t g_epoch = 1; /* changes only after confirmed device teardown */ 74 static uint32_t g_blocked; /* latched (atomic) */ 75 static uint32_t g_locked; /* session lock held (atomic) */ 76 static uint32_t g_cancel; /* cancel requested (atomic) */ 77 static uint32_t g_spin_us; /* omega_gpu_session_set_spin_us; 0 = poll_word only */ 78 static char g_err[320] = ""; 79 static void (*g_hooks[8])(void); 80 static unsigned g_nhooks; 81 static OmegaGpuAllocStats g_stats; /* written with the lock held, read with atomic loads */ 82 static OmegaGpuBufferHandle g_parked[OMEGA_GPU_SESSION_PARK_MAX]; 83 static OmegaGpuParkStats g_park; 84 85 #define LOAD(x) __atomic_load_n(&(x), __ATOMIC_ACQUIRE) 86 #define STORE(x, v) __atomic_store_n(&(x), (v), __ATOMIC_RELEASE) 87 #define BUMP(x, by) STORE((x), LOAD(x) + (by)) /* lock held: one writer */ 88 89 /* The generated pure-scalar root owns saturation. Called only after the 90 * platform confirms teardown; refusal preserves the exhausted sentinel. */ 91 static void advance_device_epoch(void) { 92 uint64_t next = UINT64_MAX; 93 (void)omega_native_epoch(&next, LOAD(g_epoch)); 94 STORE(g_epoch, next); 95 } 96 97 /* ---- platform ---- */ 98 99 static int install(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps) { 100 if (!p || !caps) return OMEGA_GPU_PLAT_REFUSED_ARGS; 101 OmegaGpuPlatformCaps req = { 102 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, 103 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, 104 .max_devices = 1, .max_buffers = 8, .max_queues = 1, .max_inflight = 1, 105 .max_buffer_bytes = 0x10000, .max_total_bytes = 0x10000 + 3 * 0x10000, 106 }; 107 int rc = omega_gpu_platform_negotiate(caps, &req); 108 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_platform_check_for(p, caps, OMEGA_GPU_SVC_REQUIRED_SESSION); 109 if (rc != OMEGA_GPU_PLAT_OK) return rc; 110 g_caps = *caps; 111 STORE(g_p, p); 112 return OMEGA_GPU_PLAT_OK; 113 } 114 115 int omega_gpu_session_set_platform(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps) { 116 if (ses(9, g_open ? 1u : 0u, LOAD(g_locked) ? 1u : 0u, LOAD(g_blocked) ? 1u : 0u, 0, 0, 0) != 0) return OMEGA_GPU_PLAT_REFUSED_BUSY; 117 if (!p) { STORE(g_p, (const OmegaGpuPlatform *)0); return OMEGA_GPU_PLAT_OK; } 118 return install(p, caps); 119 } 120 121 #ifndef OMEGA_GPU_NO_POSIX 122 static pthread_once_t g_default_once = PTHREAD_ONCE_INIT; 123 static void install_default(void) { 124 if (LOAD(g_p)) return; /* one was installed explicitly */ 125 OmegaGpuPlatformCaps c; 126 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&c); 127 (void)install(p, &c); 128 } 129 #endif 130 131 static const OmegaGpuPlatform *platform(void) { 132 const OmegaGpuPlatform *p = LOAD(g_p); 133 #ifndef OMEGA_GPU_NO_POSIX 134 if (!p) { pthread_once(&g_default_once, install_default); p = LOAD(g_p); } 135 #endif 136 return p; 137 } 138 139 const OmegaGpuPlatform *omega_gpu_session_platform(void) { return platform(); } 140 uint32_t omega_gpu_session_submit_prefix_words(void) { return platform() ? g_caps.submit_prefix_words : 0u; } /* g_caps is written before g_p is published */ 141 OmegaGpuCompletionMode omega_gpu_session_completion_mode(void) { return OMEGA_GPU_COMPLETION_POLLING; } 142 143 uint64_t omega_gpu_session_now_ns(void) { 144 const OmegaGpuPlatform *p = platform(); 145 return p ? p->time_now_ns(p->ctx) : 0u; 146 } 147 148 void *omega_gpu_host_alloc(size_t bytes) { 149 const OmegaGpuPlatform *p = platform(); 150 return p ? p->host_alloc(p->ctx, bytes ? bytes : 1) : (void *)0; /* 0 bytes: a unique 1-byte block, as malloc(0) gives */ 151 } 152 void *omega_gpu_host_calloc(size_t n, size_t size) { 153 size_t bytes; 154 if (__builtin_mul_overflow(n, size, &bytes)) return (void *)0; 155 void *q = omega_gpu_host_alloc(bytes); 156 if (q) memset(q, 0, bytes); 157 return q; 158 } 159 void omega_gpu_host_free(void *q) { 160 const OmegaGpuPlatform *p = platform(); 161 if (p && q) p->host_free(p->ctx, q); 162 } 163 164 /* ---- errors ---- */ 165 166 const char *omega_gpu_session_last_error(void) { return g_err; } 167 void omega_gpu_session_set_error(const char *stage) { omega_gpu_fmt(g_err, sizeof g_err, "%s", stage ? stage : ""); } 168 169 /* A platform refusal: the driver text when the device refused (the Linux reference writes ": ", 170 * the stage names the session ever reported), else the stage and the refusal name. */ 171 /* The platform refused and wrote a full sentence for this stage (the Linux reference's memory reserve, developer bar D1, 172 * starts "dma_alloc: refused "): pass it on as it is. Any other refusal keeps the short form below. */ 173 static void fail_plat(const char *stage, int rc); 174 static void fail_plat_said(const char *stage, int rc) { 175 const OmegaGpuPlatform *p = LOAD(g_p); 176 const char *diag = p ? p->diag_text(p->ctx) : ""; 177 static const char said[] = "dma_alloc: refused "; 178 if (rc == OMEGA_GPU_PLAT_REFUSED_LIMIT && diag && stage && memcmp(stage, "dma_alloc", 10) == 0 && 179 strlen(diag) >= sizeof said - 1 && memcmp(diag, said, sizeof said - 1) == 0) { 180 omega_gpu_fmt(g_err, sizeof g_err, "%s", diag); 181 return; 182 } 183 fail_plat(stage, rc); 184 } 185 static void fail_plat(const char *stage, int rc) { 186 const OmegaGpuPlatform *p = LOAD(g_p); 187 const char *diag = p ? p->diag_text(p->ctx) : ""; 188 if (rc == OMEGA_GPU_PLAT_REFUSED_DEVICE && diag && diag[0]) 189 omega_gpu_fmt(g_err, sizeof g_err, "%s", diag); 190 else 191 omega_gpu_fmt(g_err, sizeof g_err, "%s: %s", stage, omega_gpu_platform_refusal_name(rc)); 192 } 193 194 /* ---- lock, state ---- */ 195 196 void omega_gpu_session_lock(void) { 197 const OmegaGpuPlatform *p = platform(); 198 if (!p) return; 199 (void)p->lock(p->ctx, OMEGA_GPU_SESSION_LOCK_ID); 200 STORE(g_locked, 1u); 201 } 202 void omega_gpu_session_unlock(void) { 203 const OmegaGpuPlatform *p = LOAD(g_p); 204 if (!p) return; 205 STORE(g_locked, 0u); 206 (void)p->unlock(p->ctx, OMEGA_GPU_SESSION_LOCK_ID); 207 } 208 bool omega_gpu_session_is_open(void) { return g_open; } 209 int omega_gpu_session_is_blocked(void) { return LOAD(g_blocked) ? 1 : 0; } 210 int omega_gpu_session_set_spin_us(uint32_t us) { 211 if (ses(2, us, 1000000u, 0, 0, 0, 0) != 0) return -1; 212 STORE(g_spin_us, us); 213 return 0; 214 } 215 uint32_t omega_gpu_session_spin_us(void) { return LOAD(g_spin_us); } 216 void omega_gpu_session_cancel(void) { STORE(g_cancel, 1u); } 217 218 /* Consumes a pending cancel request. */ 219 static bool take_cancel(void) { 220 if (!LOAD(g_cancel)) return false; 221 STORE(g_cancel, 0u); 222 return true; 223 } 224 225 void omega_gpu_session_alloc_stats(OmegaGpuAllocStats *out) { 226 if (!out) return; 227 out->allocs = LOAD(g_stats.allocs); 228 out->alloc_bytes = LOAD(g_stats.alloc_bytes); 229 out->alloc_failures = LOAD(g_stats.alloc_failures); 230 out->frees = LOAD(g_stats.frees); 231 out->stale_refusals = LOAD(g_stats.stale_refusals); 232 } 233 234 /* ---- parking ---- */ 235 236 static void park(const OmegaGpuMem *m) { 237 uint32_t n = LOAD(g_park.buffers) - LOAD(g_park.untracked); 238 for (uint32_t i = 0; i < n; i++) 239 if (g_parked[i].index == m->handle.index && g_parked[i].generation == m->handle.generation) return; 240 if (n < OMEGA_GPU_SESSION_PARK_MAX) g_parked[n] = m->handle; 241 else BUMP(g_park.untracked, 1u); 242 BUMP(g_park.bytes, m->size); 243 BUMP(g_park.buffers, 1u); 244 } 245 246 void omega_gpu_session_parked(OmegaGpuParkStats *out) { 247 if (!out) return; 248 out->buffers = LOAD(g_park.buffers); 249 out->untracked = LOAD(g_park.untracked); 250 out->bytes = LOAD(g_park.bytes); 251 } 252 253 int omega_gpu_session_is_parked(OmegaGpuBufferHandle h) { 254 uint32_t n = LOAD(g_park.buffers) - LOAD(g_park.untracked); 255 if (h.generation == 0u) return 0; 256 if (n > OMEGA_GPU_SESSION_PARK_MAX) n = OMEGA_GPU_SESSION_PARK_MAX; 257 #if (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION) || defined(CUDAPP_MIGRATION_PENDING_C_SESSION) 258 for (uint32_t i = 0; i < n; i++) 259 if (g_parked[i].index == h.index && g_parked[i].generation == h.generation) return 1; 260 return 0; 261 #else 262 uint64_t idx[OMEGA_GPU_SESSION_PARK_MAX] = {0}, gen[OMEGA_GPU_SESSION_PARK_MAX] = {0}, r = 0; 263 for (uint32_t i = 0; i < n; i++) { idx[i] = g_parked[i].index; gen[i] = g_parked[i].generation; } 264 if (omega_scan(&r, idx, OMEGA_GPU_SESSION_PARK_MAX, gen, OMEGA_GPU_SESSION_PARK_MAX, h.index, h.generation) != 0) return 0; 265 return r ? 1 : 0; 266 #endif 267 } 268 269 /* L136: queues past the first (omega_gpu_session_extra_queue), each with its own 64 KB ring; opened on demand, closed 270 * and freed before queue 0 at close, parked at the latch like the session ring. */ 271 #define XQ_MAX 3u 272 static OmegaGpuQueueHandle g_xq[XQ_MAX]; 273 static OmegaGpuMem g_xpb[XQ_MAX]; 274 275 /* Uncertain completion: the device may still touch any session buffer. Keep everything, refuse everything. */ 276 static void latch(const char *why) { 277 STORE(g_blocked, 1u); 278 for (uint32_t i = 0; i < XQ_MAX; i++) if (g_xpb[i].cpu) park(&g_xpb[i]); 279 if (g_s.pb.cpu) park(&g_s.pb); 280 if (g_s.cbank.cpu) park(&g_s.cbank); 281 if (g_s.marker.cpu) park(&g_s.marker); 282 if (g_s.qmd.cpu) park(&g_s.qmd); 283 omega_gpu_session_set_error(why); 284 } 285 286 void omega_gpu_session_latch_uncertain(const char *why) { latch(why ? why : "uncertain completion (latched by a caller)"); } 287 288 /* ---- buffers ---- */ 289 290 #ifdef SESSION_C_ORACLE 291 static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; } 292 #endif 293 294 static int mem_alloc(uint32_t kind, uint32_t rights, size_t bytes, OmegaGpuMem *out) { 295 const OmegaGpuPlatform *p = LOAD(g_p); 296 memset(out, 0, sizeof *out); 297 if (!g_open || LOAD(g_blocked) || !p) { omega_gpu_session_set_error("alloc without open device"); return -1; } 298 const size_t want = (size_t)ses(1, bytes, 0, 0, 0, 0, 0); 299 OmegaGpuBufferDesc d = {.kind = kind, .rights = rights, .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .align = (uint32_t)PAGE, .bytes = want}; 300 OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; /* the bus address never leaves this function */ 301 int rc = p->dma_alloc(p->ctx, g_s.dev, &d, &out->handle, &cpu, &va, &bus); 302 if (rc != OMEGA_GPU_PLAT_OK || !cpu.ptr || cpu.len < want) { 303 const int platform_refused = rc != OMEGA_GPU_PLAT_OK; /* only then is the platform's text about this call */ 304 if (rc == OMEGA_GPU_PLAT_OK) { (void)p->dma_free(p->ctx, out->handle); rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; } 305 BUMP(g_stats.alloc_failures, 1u); 306 if (platform_refused) fail_plat_said("dma_alloc", rc); else fail_plat("dma_alloc", rc); 307 memset(out, 0, sizeof *out); 308 return -1; 309 } 310 out->kind = kind; out->cpu = cpu.ptr; out->va = va.va; out->size = cpu.len; 311 BUMP(g_stats.allocs, 1u); 312 BUMP(g_stats.alloc_bytes, (uint64_t)want); 313 return 0; 314 } 315 316 #define RIGHTS_DATA (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE) 317 #define RIGHTS_CODE (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_EXEC) 318 #define RIGHTS_RING (OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ) 319 #define RIGHTS_MARKER (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_WRITE | OMEGA_GPU_RIGHT_HOST_POLLED) 320 #define RIGHTS_CBANK (OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ) 321 322 int omega_gpu_session_alloc(size_t bytes, OmegaGpuMem *out) { 323 return mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_DATA, bytes, out); 324 } 325 326 int omega_gpu_session_alloc_code(size_t code_size, OmegaGpuMem *out) { 327 size_t bytes = omega_gpu_code_alloc_bytes(code_size); 328 if (bytes == 0) { omega_gpu_session_set_error("code size overflow"); memset(out, 0, sizeof *out); return -1; } 329 if (mem_alloc(OMEGA_GPU_HANDLE_BUF_CODE, RIGHTS_CODE, bytes, out) != 0) return -1; 330 memset((uint8_t *)out->cpu + code_size, 0, bytes - code_size); 331 return 0; 332 } 333 334 void omega_gpu_session_free(OmegaGpuMem *m) { 335 if (!m) return; 336 if (m->cpu) { 337 const OmegaGpuPlatform *p = LOAD(g_p); 338 uint64_t code = ses(7, LOAD(g_blocked) ? 1u : 0u, g_open ? 1u : 0u, p ? 1u : 0u, 0, 0, 0); 339 if (code == 0) { 340 park(m); /* the device may still use it */ 341 } else if (code == 3) { 342 BUMP(g_stats.stale_refusals, 1u); /* its device is closed: the handle is stale, nothing to release */ 343 } else { 344 int rc = p->dma_free(p->ctx, m->handle); 345 if (rc == OMEGA_GPU_PLAT_OK) BUMP(g_stats.frees, 1u); 346 else { BUMP(g_stats.stale_refusals, 1u); fail_plat("dma_free", rc); } 347 } 348 } 349 memset(m, 0, sizeof *m); 350 } 351 352 static int sync_mem(const OmegaGpuMem *m, int to_gpu) { 353 const OmegaGpuPlatform *p = LOAD(g_p); 354 if (!m || !m->cpu) return 0; 355 if (!p || !g_open) { omega_gpu_session_set_error("sync without open device"); return -1; } 356 int rc = to_gpu ? p->sync_cpu_to_gpu(p->ctx, m->handle, 0, m->size) : p->sync_gpu_to_cpu(p->ctx, m->handle, 0, m->size); 357 if (rc != OMEGA_GPU_PLAT_OK) { fail_plat(to_gpu ? "sync_cpu_to_gpu" : "sync_gpu_to_cpu", rc); return -1; } 358 return 0; 359 } 360 int omega_gpu_session_sync_to_gpu(const OmegaGpuMem *m) { return sync_mem(m, 1); } 361 int omega_gpu_session_sync_to_cpu(const OmegaGpuMem *m) { return sync_mem(m, 0); } 362 363 int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) { 364 uint64_t hw = 0; 365 uint64_t code = ses(6, bytes, s->cap, s->reserved ? 1u : 0u, s->mem.cpu ? 1u : 0u, s->high_water, &hw); 366 if (code == 2) { 367 omega_gpu_fmt(g_err, sizeof g_err, "serving reservation exceeded: need %zu bytes, reserved %zu", bytes, s->cap); 368 return OMEGA_GPU_SESSION_SCRATCH_RESERVED; 369 } 370 s->high_water = (size_t)hw; 371 if (code == 0) return 0; 372 omega_gpu_session_free(&s->mem); 373 s->cap = 0; 374 if (omega_gpu_session_alloc(s->high_water, &s->mem) != 0) return -1; 375 s->cap = (size_t)s->mem.size; 376 return 0; 377 } 378 379 void omega_gpu_session_scratch_free(OmegaGpuScratch *s) { 380 omega_gpu_session_free(&s->mem); 381 s->cap = 0; 382 s->reserved = false; 383 } 384 385 int omega_gpu_session_scratch_reserve(OmegaGpuScratch *s, size_t bytes) { 386 s->reserved = false; /* grow normally first */ 387 if (omega_gpu_session_scratch(s, bytes) != 0) return -1; 388 s->reserved = true; 389 return 0; 390 } 391 392 void omega_gpu_session_scratch_unreserve(OmegaGpuScratch *s) { s->reserved = false; } 393 394 static void free_session_buffers(void) { 395 omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker); 396 omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb); 397 } 398 399 /* ---- open, close ---- */ 400 401 OmegaGpuSession *omega_gpu_session_open(void) { 402 const OmegaGpuPlatform *p = platform(); 403 uint64_t code = ses(8, LOAD(g_blocked) ? 1u : 0u, LOAD(g_epoch), p ? 1u : 0u, g_open ? 1u : 0u, 0, 0); 404 if (code == 1) { omega_gpu_session_set_error("process latched"); return NULL; } 405 if (code == 2) return &g_s; 406 if (code == 3) { omega_gpu_session_set_error("device epoch exhausted"); return NULL; } 407 if (code == 4) { omega_gpu_session_set_error("no GPU platform installed (omega_gpu_session_set_platform)"); return NULL; } 408 uint64_t t0 = p->time_now_ns(p->ctx); 409 const uint32_t opens = g_s.opens; 410 memset(&g_s, 0, sizeof g_s); 411 g_s.opens = opens; 412 int rc = p->device_open(p->ctx, 0, &g_s.dev); 413 if (rc != OMEGA_GPU_PLAT_OK) { fail_plat("device_open", rc); memset(&g_s.dev, 0, sizeof g_s.dev); return NULL; } 414 g_target_sm = 121; /* custom/freestanding platforms retain the SM121 contract */ 415 #ifndef OMEGA_GPU_NO_POSIX 416 if (p == omega_gpu_platform_linux(NULL)) g_target_sm = omega_gpu_platform_linux_target_sm(); 417 #endif 418 g_open = true; /* so alloc / free work during the rest of the open */ 419 /* same four buffers, same sizes, same order as before cut C; every one GPU-uncached */ 420 if (mem_alloc(OMEGA_GPU_HANDLE_BUF_QUEUE, RIGHTS_RING, 0x10000, &g_s.pb) || 421 mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_CBANK, PAGE, &g_s.cbank) || 422 mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_MARKER, PAGE, &g_s.marker) || 423 mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_DATA, 0x10000, &g_s.qmd) || 424 (rc = p->queue_open(p->ctx, g_s.dev, g_s.pb.handle, &g_s.queue)) != OMEGA_GPU_PLAT_OK) { 425 char keep[sizeof g_err]; 426 if (rc != OMEGA_GPU_PLAT_OK) fail_plat("queue_open", rc); 427 memcpy(keep, g_err, sizeof keep); 428 free_session_buffers(); 429 rc = p->device_close(p->ctx, g_s.dev); 430 if (rc == OMEGA_GPU_PLAT_OK) { 431 advance_device_epoch(); 432 g_open = false; 433 memset(&g_s.dev, 0, sizeof g_s.dev); 434 } else { 435 STORE(g_blocked, 1u); /* preserve the device handle until recovery confirms teardown */ 436 } 437 omega_gpu_fmt(g_err, sizeof g_err, "session scratch alloc (%s)", keep); 438 return NULL; 439 } 440 g_s.opens++; 441 g_s.last_open_ns = p->time_now_ns(p->ctx) - t0; 442 return &g_s; 443 } 444 445 /* Extra queue i against the open set (CUDApp#115): 0 index outside 1..XQ_MAX, 1 already open, 2 an earlier one is not 446 * open yet, 3 open it now. Every build, freestanding included, asks seeded Omega (om/streams/xq.om, a bridge-freest 447 * unit); CUDAPP_MIGRATION_PENDING_C_STREAMS selects the C rule, which _STREAMS_DIFF keeps as oracle. */ 448 #if defined(CUDAPP_MIGRATION_PENDING_C_STREAMS) || defined(CUDAPP_MIGRATION_PENDING_STREAMS_DIFF) 449 static uint32_t xq_admit_c(uint32_t i) { 450 if (i == 0u || i > XQ_MAX) return 0u; 451 if (g_xq[i - 1u].generation != 0u) return 1u; 452 for (uint32_t j = 0; j + 1u < i; j++) if (g_xq[j].generation == 0u) return 2u; 453 return 3u; 454 } 455 #endif 456 static uint32_t xq_admit(uint32_t i) { 457 #ifdef CUDAPP_MIGRATION_PENDING_C_STREAMS 458 return xq_admit_c(i); 459 #else 460 uint64_t open = 0, r = 0; 461 for (uint32_t j = 0; j < XQ_MAX; j++) if (g_xq[j].generation != 0u) open |= 1ull << (j + 1u); 462 if (omega_streams_xq(&r, i, open) != 0 || r > 3u) return 0u; 463 #ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF 464 if (r != xq_admit_c(i)) { 465 char line[96]; 466 omega_gpu_fmt(line, sizeof line, "streams diff: extra_queue %u Omega %llu C %u", i, (unsigned long long)r, xq_admit_c(i)); 467 cudapp_streams_diff_note(0, line); 468 } 469 #endif 470 return (uint32_t)r; 471 #endif 472 } 473 474 int omega_gpu_session_extra_queue(uint32_t i, OmegaGpuQueueHandle *out) { 475 const OmegaGpuPlatform *p = platform(); 476 const uint32_t adm = out ? xq_admit(i) : 0u; 477 if (adm == 0u) { omega_gpu_session_set_error("extra_queue: index outside 1..3"); return -1; } 478 if (LOAD(g_blocked) || !g_open || !p) { omega_gpu_session_set_error("extra_queue: device not open or latched"); return -1; } 479 if (adm == 1u) { *out = g_xq[i - 1u]; return 0; } 480 if (adm == 2u) { omega_gpu_session_set_error("extra_queue: open queues in order (1, then 2, ...)"); return -1; } 481 if (mem_alloc(OMEGA_GPU_HANDLE_BUF_QUEUE, RIGHTS_RING, 0x10000, &g_xpb[i - 1u])) return -1; 482 const int rc = p->queue_open(p->ctx, g_s.dev, g_xpb[i - 1u].handle, &g_xq[i - 1u]); 483 if (rc != OMEGA_GPU_PLAT_OK) { 484 fail_plat("queue_open (extra queue)", rc); 485 memset(&g_xq[i - 1u], 0, sizeof g_xq[i - 1u]); 486 omega_gpu_session_free(&g_xpb[i - 1u]); 487 return -1; 488 } 489 *out = g_xq[i - 1u]; 490 return 0; 491 } 492 493 int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */ 494 if (!hook) return -1; 495 int rc = 0; 496 bool seen = false; 497 for (unsigned i = 0; i < g_nhooks; i++) if (g_hooks[i] == hook) seen = true; 498 if (!seen) { if (ses(11, g_nhooks, hook ? 1u : 0u, 0, 0, 0, 0) == 0) g_hooks[g_nhooks++] = hook; else rc = -1; } 499 return rc; 500 } 501 502 void omega_gpu_session_close(void) { 503 omega_gpu_session_lock(); 504 const OmegaGpuPlatform *p = LOAD(g_p); 505 if (g_open && !LOAD(g_blocked) && p) { 506 for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); 507 if (!LOAD(g_blocked)) { /* a hook's launch may have latched: then nothing below runs */ 508 for (uint32_t i = XQ_MAX; i-- > 0;) { /* L136: extra queues first, then queue 0 */ 509 if (g_xq[i].generation != 0u) (void)p->queue_close(p->ctx, g_xq[i]); 510 memset(&g_xq[i], 0, sizeof g_xq[i]); 511 omega_gpu_session_free(&g_xpb[i]); 512 } 513 (void)p->queue_close(p->ctx, g_s.queue); 514 memset(&g_s.queue, 0, sizeof g_s.queue); 515 free_session_buffers(); 516 int rc = p->device_close(p->ctx, g_s.dev); 517 if (rc == OMEGA_GPU_PLAT_OK) { 518 advance_device_epoch(); 519 memset(&g_s.dev, 0, sizeof g_s.dev); 520 g_open = false; 521 } else { 522 fail_plat("close: device teardown fault (still latched)", rc); 523 STORE(g_blocked, 1u); 524 } 525 } 526 } 527 omega_gpu_session_unlock(); 528 } 529 530 531 /* C5: the way back from a latch without restarting the process. The on-close hooks run first, still latched, so the APIs 532 * drop their resident kernels and scratch (their frees park: nothing is released while the old device may run). Then 533 * the device closes: the Linux reference closes the RM client, which destroys every channel of this process (extra 534 * queues first) and with it every grid still running on them, then frees and unmaps every buffer, parked ones 535 * included. Only after the close returned OK is the latch lifted; the next open starts a new device. */ 536 static uint32_t g_recoveries; 537 static uint32_t g_module_latched; /* a module (the command lists) latched itself while the session stayed usable */ 538 539 void omega_gpu_session_note_module_latch(void) { STORE(g_module_latched, 1u); } 540 541 int omega_gpu_session_recover(void) { 542 omega_gpu_session_lock(); 543 const OmegaGpuPlatform *p = LOAD(g_p); 544 int rc = -1; 545 const int latched = LOAD(g_blocked) || LOAD(g_module_latched); 546 uint64_t code = ses(10, latched ? 1u : 0u, g_open ? 1u : 0u, p ? 1u : 0u, 0, 0, 0); 547 if (code != 0) { 548 omega_gpu_session_set_error(code == 1 ? "recover: latched but no device is open" : "recover: not latched (nothing to recover)"); 549 goto out; 550 } 551 for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); 552 const int prc = p->device_close(p->ctx, g_s.dev); 553 if (prc != OMEGA_GPU_PLAT_OK) { fail_plat("recover: the device close reported a teardown fault (still latched: the old grid may live on, exit the process)", prc); goto out; } 554 #if !CUDAPP_MIGRATION_PENDING_C_SESSION_TIMEOUT 555 /* device_close acknowledges the platform's bundled queue/channel/context 556 * destruction. Never release resources or lift admission on a failed ack. */ 557 uint64_t phase = 0; 558 if (omega_native_c5(&phase, 4, 1, 0, 0, 0, 0) != OMEGA_OK || phase != 7) { 559 omega_gpu_session_set_error("recover: Omega teardown decision refused (still latched)"); 560 goto out; 561 } 562 #endif 563 memset(g_xq, 0, sizeof g_xq); 564 memset(g_xpb, 0, sizeof g_xpb); 565 const uint32_t opens = g_s.opens; 566 memset(&g_s, 0, sizeof g_s); 567 g_s.opens = opens; 568 g_open = false; 569 memset(g_parked, 0, sizeof g_parked); 570 memset(&g_park, 0, sizeof g_park); 571 BUMP(g_recoveries, 1u); 572 advance_device_epoch(); 573 STORE(g_module_latched, 0u); 574 STORE(g_blocked, 0u); 575 omega_gpu_session_set_error(""); 576 rc = 0; 577 out: 578 omega_gpu_session_unlock(); 579 return rc; 580 } 581 582 uint32_t omega_gpu_session_recoveries(void) { return LOAD(g_recoveries); } 583 uint64_t omega_gpu_session_epoch(void) { return LOAD(g_epoch); } 584 /* ---- launch ---- */ 585 586 #ifdef SESSION_C_ORACLE 587 static uint32_t round_up(uint32_t v, uint32_t q) { return q ? (uint32_t)(((uint64_t)v + q - 1u) / q * q) : 0u; } 588 #endif 589 590 #ifdef SESSION_C_ORACLE 591 /* Default-OFF C oracle for the session decisions. Mirrors om/session/admit.om 592 * and the park/words units so the differential can compare both paths. */ 593 static uint64_t ses_c(uint64_t op, uint64_t i0, uint64_t i1, uint64_t i2, uint64_t i3, uint64_t i4, uint64_t *out0) { 594 uint64_t high = i4; 595 uint64_t b, c; 596 switch (op) { 597 case 0: return round_up((uint32_t)i0, (uint32_t)i1); 598 case 1: return page_up(i0); 599 case 2: return (uint32_t)i0 > (uint32_t)i1 ? 1u : 0u; 600 case 3: return (uint32_t)i0 > 64u ? round_up((uint32_t)i0, 8u) : 64u; 601 case 4: return (uint32_t)i0 ? 352u : 896u; 602 case 5: return (uint32_t)i0 ? i1 + 32u : i2 + 8192u; 603 case 6: 604 b = i0; c = i1; 605 if (i2) { if (b > c || !i3) { if (out0) *out0 = high; return 2; } } 606 if (b > high) high = b; 607 if (out0) *out0 = high; 608 return (c >= b && i3) ? 0u : 1u; 609 case 7: if (i0) return 0; if (!i1 || !i2) return 3; return 2; 610 case 8: 611 if (i0) return 1; 612 if (i3) return 2; 613 if (i1 == UINT64_MAX) return 3; 614 return !i2 ? 4u : 0u; 615 case 9: return (i0 || i1 || i2) ? 1u : 0u; 616 case 10: if (!i0) return 2; if (!i1 || !i2) return 1; return 0; 617 case 11: return (i0 < 8u && i1) ? 0u : 1u; 618 case 12: return (uint32_t)i0 < 1u ? (uint32_t)i0 : 1u; 619 case 13: return ((int32_t)(i0 - i1) >= 0) ? 0u : 1u; 620 default: return UINT64_MAX; 621 } 622 } 623 #endif 624 625 static uint64_t ses(uint64_t op, uint64_t i0, uint64_t i1, uint64_t i2, uint64_t i3, uint64_t i4, uint64_t *out0) { 626 uint64_t r = 0; 627 #if (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION) || defined(CUDAPP_MIGRATION_PENDING_C_SESSION) 628 r = ses_c(op, i0, i1, i2, i3, i4, out0); 629 #else 630 uint64_t a[16] = {i0, i1, i2, i3, i4}, o[4] = {0}; 631 if (omega_decide(&r, op, a, 16, o, 4) != 0) r = UINT64_MAX; 632 if (out0) *out0 = o[0]; 633 #ifdef CUDAPP_MIGRATION_PENDING_SESSION_DIFF 634 { 635 uint64_t cr, co = 0; 636 cr = ses_c(op, i0, i1, i2, i3, i4, &co); 637 if (cr != r || (out0 && co != o[0])) cudapp_session_diff_note(0, "session decide"); 638 else cudapp_session_diff_note(1, "session decide"); 639 } 640 #endif 641 #endif 642 return r; 643 } 644 645 /* Append one method packet from om/session/words.om. The blob copies stay in C. */ 646 static size_t ses_pkt(uint32_t *pb, size_t n, uint64_t op, uint64_t a0, uint64_t a1, uint64_t a2) { 647 uint64_t r = 0, a[16] = {a0, a1, a2}, o[32] = {0}; 648 #if (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION) || defined(CUDAPP_MIGRATION_PENDING_C_SESSION) 649 switch (op) { 650 case 0: 651 o[0] = bw_mthd(1, 0, 1); o[1] = OMEGA_AMPERE_COMPUTE_B; o[2] = bw_mthd(1, 0x021c, 1); o[3] = (1u << 0) | (1u << 4) | (1u << 12); 652 o[4] = bw_mthd(1, 0x02b4, 1); o[5] = (uint32_t)(a0 >> 8); o[6] = bw_mthd(1, 0x02c0, 1); o[7] = 3; r = 8; break; 653 case 1: r = sizeof SETUP_WORDS / sizeof SETUP_WORDS[0]; for (uint64_t i = 0; i < r; i++) o[i] = SETUP_WORDS[i]; break; 654 case 2: o[0] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); o[1] = (uint32_t)a0; r = 2; break; 655 case 3: 656 o[0] = bw_mthd(1, 0x0188, 2); o[1] = (uint32_t)(a0 >> 32); o[2] = (uint32_t)a0; 657 o[3] = bw_mthd(1, 0x0180, 2); o[4] = (uint32_t)a1; o[5] = 1; 658 o[6] = bw_mthd(1, 0x01b0, 1); o[7] = 0x41; 659 o[8] = ((uint32_t)a2 << 16) | (1u << 13) | (0x01b4u >> 2) | (6u << 28); r = 9; break; 660 case 4: 661 o[0] = (98u << 16) | (1u << 13) | (0x0318u >> 2) | (2u << 28); 662 o[1] = (1u << 30) | (uint32_t)((a0 >> 40) & 0x1ff); o[2] = (uint32_t)(a0 >> 8); r = 3; break; 663 case 5: 664 o[0] = bw_mthd(1, 0x0188, 2); o[1] = (uint32_t)(a0 >> 32); o[2] = (uint32_t)a0; 665 o[3] = bw_mthd(1, 0x0180, 2); o[4] = 4; o[5] = 1; 666 o[6] = bw_mthd(1, 0x01b0, 1); o[7] = 0x41; 667 o[8] = (1u << 16) | (1u << 13) | (0x01b4u >> 2) | (6u << 28); o[9] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; r = 10; break; 668 case 6: 669 o[0] = bw_mthd(0, 0x005c, 5); o[1] = (uint32_t)a0; o[2] = (uint32_t)(a0 >> 32); 670 o[3] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; o[4] = 0; o[5] = 0x1u | (1u << 20); 671 o[6] = bw_mthd(0, 0x0028, 4); o[7] = 0; o[8] = 0; o[9] = 0; o[10] = (0x10u << 27); 672 o[11] = bw_mthd(0, 0x005c, 5); o[12] = (uint32_t)(a0 + 0x10); o[13] = (uint32_t)((a0 + 0x10) >> 32); 673 o[14] = OMEGA_BW_MARKER2_PAYLOAD; o[15] = 0; o[16] = 0x1u | (1u << 20); r = 17; break; 674 default: r = 0; break; 675 } 676 (void)a; 677 #else 678 if (omega_emit(&r, op, a, 16, o, 32) != 0) return n; 679 #endif 680 for (uint64_t i = 0; i < r && n < 1024; i++) pb[n++] = (uint32_t)o[i]; 681 return n; 682 } 683 684 static inline void cpu_relax(void) { 685 #if defined(__aarch64__) 686 __asm__ volatile("yield" ::: "memory"); 687 #else 688 __asm__ volatile("" ::: "memory"); 689 #endif 690 } 691 692 #define WAIT_CANCELLED 1 693 #define WAIT_TIMED_OUT 2 694 #define WAIT_REFUSED 3 695 696 /* Marker wait by POLLING. With spin_us, the host first re-reads the uncached marker word back to back (with the 697 * ARM yield hint) so a launch that finishes inside the window is seen within about one read. Then the platform's 698 * poll_word in slices of OMEGA_GPU_SESSION_POLL_SLICE_MS (Linux reference: the m16 50 us sleep-poll) until 699 * timeout_ms, checking for a cancel request between slices. */ 700 static int session_wait(const OmegaGpuPlatform *p, uint64_t off, uint32_t want, uint64_t timeout_ms, uint32_t spin_us, 701 uint64_t start_ns, uint64_t deadline_ns) { 702 #if CUDAPP_MIGRATION_PENDING_C_SESSION_TIMEOUT 703 (void)start_ns; (void)deadline_ns; 704 volatile uint32_t *m = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + off); 705 if (spin_us) { 706 const uint64_t end = p->time_now_ns(p->ctx) + (uint64_t)spin_us * 1000u; 707 do { 708 if (ses(13, *m, want, 0, 0, 0, 0) == 0) return 0; 709 if (LOAD(g_cancel)) return WAIT_CANCELLED; 710 cpu_relax(); 711 } while (p->time_now_ns(p->ctx) < end); 712 } 713 uint64_t waited = 0; 714 for (;;) { 715 if (LOAD(g_cancel)) return WAIT_CANCELLED; 716 const uint64_t left = timeout_ms - waited; 717 const uint64_t slice = left < OMEGA_GPU_SESSION_POLL_SLICE_MS ? left : OMEGA_GPU_SESSION_POLL_SLICE_MS; 718 uint32_t seen = 0; 719 int rc = p->poll_word(p->ctx, g_s.marker.handle, off, want, slice, &seen); 720 if (rc == OMEGA_GPU_PLAT_OK) return 0; 721 if (rc != OMEGA_GPU_POLL_TIMED_OUT) return WAIT_REFUSED; 722 waited += slice; 723 if (waited >= timeout_ms) return WAIT_TIMED_OUT; 724 } 725 #else 726 (void)timeout_ms; 727 volatile uint32_t *m = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + off); 728 for (;;) { 729 if (LOAD(g_cancel)) return WAIT_CANCELLED; 730 const uint64_t now = p->time_now_ns(p->ctx); 731 uint64_t decision = 0; 732 const int done = (int32_t)(*m - want) >= 0; 733 if (omega_native_c5(&decision, 1, now, start_ns, deadline_ns, done, 0) != OMEGA_OK) 734 return WAIT_REFUSED; 735 if (decision == 3) return WAIT_TIMED_OUT; 736 if (decision == 1) return 0; 737 if (now - start_ns < (uint64_t)spin_us * 1000u) { cpu_relax(); continue; } 738 uint64_t slice = 0; 739 if (omega_native_c5(&slice, 2, now, deadline_ns, OMEGA_GPU_SESSION_POLL_SLICE_MS, 0, 0) != OMEGA_OK) 740 return WAIT_REFUSED; 741 uint32_t seen = 0; 742 int rc = p->poll_word(p->ctx, g_s.marker.handle, off, want, slice, &seen); 743 if (rc != OMEGA_GPU_PLAT_OK && rc != OMEGA_GPU_POLL_TIMED_OUT) return WAIT_REFUSED; 744 /* Re-observe the clock even on success: late completion is not timely 745 * completion. Floor slices and zero-ms polls cover the sub-ms tail. */ 746 if (!slice) cpu_relax(); 747 } 748 #endif 749 } 750 751 int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) { 752 const OmegaGpuPlatform *p = LOAD(g_p); 753 if (!g_open || LOAD(g_blocked) || !p) { 754 omega_gpu_session_set_error(LOAD(g_blocked) ? "process latched" : "launch without open device"); return -1; 755 } 756 /* timeout_ms 0 is refused (CudaAuditFable F15): it would submit and then latch the process after one poll. */ 757 if (!L || !L->args || L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0 || 758 L->spin_us > OMEGA_GPU_SESSION_MAX_SPIN_US || L->timeout_ms == 0) { 759 omega_gpu_session_set_error("launch arguments"); return -1; 760 } 761 const uint32_t requested = L->target_sm ? L->target_sm : 121u; 762 if (requested != g_target_sm || (requested != 86 && requested != 121)) { 763 omega_gpu_session_set_error("launch target does not match selected GPU; SM86 requires target_sm=86"); 764 return -1; 765 } 766 const int ampere = requested == 86; 767 if (take_cancel()) { omega_gpu_session_set_error("launch cancelled before submit"); return OMEGA_GPU_SESSION_CANCELLED; } 768 const uint32_t threads_y = L->threads_y ? L->threads_y : 1; 769 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; 770 if (ampere) memset(cbank_data, 0, sizeof cbank_data); 771 else omega_blackwell_build_cbank_driver_2d(cbank_data, g_s.cbank.va, L->threads_x, threads_y, L->grid_x, L->grid_y); 772 memcpy(g_s.cbank.cpu, cbank_data, sizeof cbank_data); 773 const uint32_t args_offset = ampere ? 0x160u : 0x380u; 774 memcpy((uint8_t *)g_s.cbank.cpu + args_offset, L->args, (size_t)L->n_args * 4u); 775 776 const uint64_t qmd0_va = g_s.qmd.va, qmd1_va = g_s.qmd.va + 0x1000, 777 sem_va = ampere ? g_s.marker.va + 0x20 : g_s.qmd.va + 0x2000, scratch_va = g_s.qmd.va + 0x4000; 778 uint32_t gpr = L->gpr_count > 64u ? (uint32_t)ses(0, L->gpr_count, 8u, 0, 0, 0, 0) : 64u; 779 OmegaBlackwellQmdConfig cfg = { 780 .code_va = L->code_va, .cbank_va = g_s.cbank.va, .scratch_va = scratch_va, .sem_va = sem_va, 781 .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements, 782 .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y, 783 .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, 784 .shared_bytes = L->shared_bytes, 785 }; 786 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; 787 if (ampere) { 788 #ifndef OMEGA_GPU_NO_POSIX 789 if (qmd1_va >> 40) { 790 omega_gpu_session_set_error("Ampere PCAS QMD address exceeds 40-bit launch aperture"); 791 return -1; 792 } 793 if (omega_ampere_build_qmd(qmd1_words, L->code_va, g_s.cbank.va, sem_va, 794 L->threads_x, threads_y, L->grid_x, L->grid_y, gpr, L->shared_bytes)) { 795 omega_gpu_session_set_error("Ampere QMD3 build: unsupported dimensions, registers, shared memory or VA"); 796 return -1; 797 } 798 memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, OMEGA_AMPERE_QMD_WORDS * sizeof(uint32_t)); 799 #else 800 omega_gpu_session_set_error("Ampere requires the Linux native Omega runtime"); 801 return -1; 802 #endif 803 } else { 804 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); 805 if (omega_blackwell_build_qmd1(qmd1_words, &cfg) != 0) { omega_gpu_session_set_error("qmd build (shared_bytes)"); return -1; } 806 if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; } 807 memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words); 808 memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words); 809 } 810 811 volatile uint32_t *hsem = ampere ? (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x20) : 812 (volatile uint32_t *)((uint8_t *)g_s.qmd.cpu + 0x2000); 813 volatile uint32_t *hmarker = (volatile uint32_t *)g_s.marker.cpu; 814 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x10); 815 *hsem = 0; *hmarker = 0; *hmarker2 = 0; 816 /* explicit visibility of everything the CPU just wrote (Linux reference: dsb sy, the barrier issued here before) */ 817 int rc = p->sync_cpu_to_gpu(p->ctx, g_s.cbank.handle, 0, PAGE); 818 if (rc == OMEGA_GPU_PLAT_OK) rc = p->sync_cpu_to_gpu(p->ctx, g_s.qmd.handle, 0, 0x3000); 819 if (rc == OMEGA_GPU_PLAT_OK) rc = p->sync_cpu_to_gpu(p->ctx, g_s.marker.handle, 0, ampere ? 0x40 : 0x20); 820 if (rc != OMEGA_GPU_PLAT_OK) { fail_plat("sync_cpu_to_gpu", rc); return -1; } 821 822 uint32_t pb[1024]; 823 size_t n = 0; 824 if (ampere) { 825 n = ses_pkt(pb, n, 0, qmd1_va, 0, 0); 826 } else { 827 n = ses_pkt(pb, n, 1, 0, 0, 0); 828 n = ses_pkt(pb, n, 2, OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL, 0, 0); 829 n = ses_pkt(pb, n, 3, g_s.cbank.va, 0x380u, 224u); 830 memcpy(&pb[n], cbank_data, 224 * 4); n += 224; 831 n = ses_pkt(pb, n, 3, g_s.cbank.va + 0x380u, (uint64_t)L->n_args * 4u, L->n_args); 832 memcpy(&pb[n], L->args, (size_t)L->n_args * 4u); n += L->n_args; 833 n = ses_pkt(pb, n, 4, qmd0_va, 0, 0); 834 memcpy(&pb[n], qmd0_words, 96 * 4); n += 96; 835 n = ses_pkt(pb, n, 5, sem_va, 0, 0); 836 n = ses_pkt(pb, n, 4, qmd1_va, 0, 0); 837 memcpy(&pb[n], qmd1_words, 96 * 4); n += 96; 838 } 839 n = ses_pkt(pb, n, 6, g_s.marker.va, 0, 0); 840 841 const uint32_t spin_us = L->spin_us ? L->spin_us : omega_gpu_session_spin_us(); 842 uint64_t t0 = p->time_now_ns(p->ctx); 843 uint64_t deadline_ns = 0; 844 #if !CUDAPP_MIGRATION_PENDING_C_SESSION_TIMEOUT 845 if (omega_native_c5(&deadline_ns, 0, t0, L->timeout_ms, 0, 0, 0) != OMEGA_OK || !deadline_ns) { 846 omega_gpu_session_set_error("launch watchdog deadline overflow"); 847 return -1; /* refused before ringing the doorbell */ 848 } 849 #endif 850 rc = p->queue_submit(p->ctx, g_s.queue, pb, (uint32_t)n); 851 if (rc == OMEGA_GPU_PLAT_REFUSED_BUSY) { 852 /* an earlier submission is unretired, so its completion is uncertain (a command list that timed out queues 853 * this launch on purpose): nothing new was written to the ring, latch like a marker timeout */ 854 latch("submit refused: an earlier submission is still in flight (latched)"); 855 return -1; 856 } 857 if (rc != OMEGA_GPU_PLAT_OK) { 858 fail_plat("submit_methods", rc); /* contract: a refused submit rang no doorbell, nothing is in flight */ 859 return -1; 860 } 861 int w = session_wait(p, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms, spin_us, t0, deadline_ns); 862 if (w == 0) w = session_wait(p, 0x10, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms, spin_us, t0, deadline_ns); 863 if (w != 0) { 864 /* uncertain completion: keep everything (parked), refuse every later call */ 865 if (w == WAIT_CANCELLED) { 866 (void)take_cancel(); 867 latch("launch cancelled after submit (completion uncertain, latched)"); 868 return OMEGA_GPU_SESSION_CANCELLED; 869 } 870 latch(w == WAIT_TIMED_OUT ? "marker wait timed out (latched)" : "marker poll refused (latched)"); 871 return -1; 872 } 873 (void)p->sync_gpu_to_cpu(p->ctx, g_s.marker.handle, 0, 0x20); 874 if (ampere) (void)p->sync_gpu_to_cpu(p->ctx, g_s.marker.handle, 0x20, 4); 875 else (void)p->sync_gpu_to_cpu(p->ctx, g_s.qmd.handle, 0x2000, 4); 876 uint64_t t1 = p->time_now_ns(p->ctx); 877 (void)p->queue_retire(p->ctx, g_s.queue); /* both releases landed: the GPFIFO entry is consumed */ 878 if (*hsem != (ampere ? 1u : OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE)) { omega_gpu_session_set_error("intermediate semaphore not DONE"); return -1; } 879 if (elapsed_ns) *elapsed_ns += t1 - t0; 880 if (marker_out) *marker_out = *hmarker; 881 (void)hmarker2; 882 return 0; 883 } 884 885 uint32_t omega_gpu_session_open_count(void) { return g_s.opens; } 886 uint32_t omega_gpu_session_target_sm(void) { return g_open ? g_target_sm : 0; }