interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / cut4b / FB1-CUT4B-710d438c / omega_gpu_session.c

542 lines · 25863 bytes Raw Blame History
1/*
2 * omega_gpu_session.c: FB-1 cut 4b, made portable in native v1 P07 cut C (session part). See the header.
3 *
4 * The launch words are the ones omega_gpu_matmul_api.c (cut 1b, chip PASS FB1-CUT1B-4345406) and
5 * omega_gpu_elementwise_api.c (cut 4, FB1-CUT4-0d0241a) both emitted; cut C did not change one of them. What
6 * changed is where the machine comes from: every device, buffer, queue, clock, lock and host-memory call goes
7 * through the installed OmegaGpuPlatform (ABI 1.1), so this file builds freestanding (OMEGA_GPU_NO_POSIX) with no
8 * Linux header and no libc call. The hosted build installs the Linux reference (src/omega_gpu_platform_linux.c)
9 * on first use; it makes the same driver calls in the same order as this file did before.
10 *
11 * No read-modify-write atomics: the freestanding target must not pull libgcc outline atomics, so shared words are
12 * plain atomic loads and stores, and counters are only written with the session lock held.
13 */
14#include "omega_gpu_session.h"
15#include "omega_gpu_code_alloc.h"
16#include "omega_gpu_fmt.h"
17#include "omega_blackwell_qmd.h"
18#include "omega_blackwell_submit.h" /* launch constants only (macros); the vector executor itself stays Linux-private */
19#include <string.h>
20
21#ifndef OMEGA_GPU_NO_POSIX
22#include "omega_gpu_platform_linux.h"
23#include <pthread.h>
24#endif
25
26#define PAGE 0x1000ull
27
28/* Push-buffer method header (the nvrm_mthd encoding, physics nvrm.h): increasing, count words, subchannel. */
29static inline uint32_t bw_mthd(uint32_t subc, uint32_t mthd, uint32_t count) {
30 return (2u << 28) | (count << 16) | (subc << 13) | (mthd >> 2);
31}
32
33/* Same words as src/omega_blackwell_submit.c SETUP_WORDS (compute class setup). */
34static const uint32_t SETUP_WORDS[18] = {
35 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
36 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
37 0x20012559, 0x00000000,
38};
39
40static const OmegaGpuPlatform *g_p; /* installed platform (atomic load / store) */
41static OmegaGpuPlatformCaps g_caps;
42static OmegaGpuSession g_s;
43static bool g_open;
44static uint32_t g_blocked; /* latched (atomic) */
45static uint32_t g_locked; /* session lock held (atomic) */
46static uint32_t g_cancel; /* cancel requested (atomic) */
47static uint32_t g_spin_us; /* omega_gpu_session_set_spin_us; 0 = poll_word only */
48static char g_err[320] = "";
49static void (*g_hooks[8])(void);
50static unsigned g_nhooks;
51static OmegaGpuAllocStats g_stats; /* written with the lock held, read with atomic loads */
52static OmegaGpuBufferHandle g_parked[OMEGA_GPU_SESSION_PARK_MAX];
53static OmegaGpuParkStats g_park;
54
55#define LOAD(x) __atomic_load_n(&(x), __ATOMIC_ACQUIRE)
56#define STORE(x, v) __atomic_store_n(&(x), (v), __ATOMIC_RELEASE)
57#define BUMP(x, by) STORE((x), LOAD(x) + (by)) /* lock held: one writer */
58
59/* ---- platform ---- */
60
61static int install(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps) {
62 if (!p || !caps) return OMEGA_GPU_PLAT_REFUSED_ARGS;
63 OmegaGpuPlatformCaps req = {
64 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR,
65 .services = OMEGA_GPU_SVC_REQUIRED_SESSION,
66 .max_devices = 1, .max_buffers = 8, .max_queues = 1, .max_inflight = 1,
67 .max_buffer_bytes = 0x10000, .max_total_bytes = 0x10000 + 3 * 0x10000,
68 };
69 int rc = omega_gpu_platform_negotiate(caps, &req);
70 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_platform_check_for(p, caps, OMEGA_GPU_SVC_REQUIRED_SESSION);
71 if (rc != OMEGA_GPU_PLAT_OK) return rc;
72 g_caps = *caps;
73 STORE(g_p, p);
74 return OMEGA_GPU_PLAT_OK;
75}
76
77int omega_gpu_session_set_platform(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps) {
78 if (g_open || LOAD(g_locked) || LOAD(g_blocked)) return OMEGA_GPU_PLAT_REFUSED_BUSY;
79 if (!p) { STORE(g_p, (const OmegaGpuPlatform *)0); return OMEGA_GPU_PLAT_OK; }
80 return install(p, caps);
81}
82
83#ifndef OMEGA_GPU_NO_POSIX
84static pthread_once_t g_default_once = PTHREAD_ONCE_INIT;
85static void install_default(void) {
86 if (LOAD(g_p)) return; /* one was installed explicitly */
87 OmegaGpuPlatformCaps c;
88 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&c);
89 (void)install(p, &c);
90}
91#endif
92
93static const OmegaGpuPlatform *platform(void) {
94 const OmegaGpuPlatform *p = LOAD(g_p);
95#ifndef OMEGA_GPU_NO_POSIX
96 if (!p) { pthread_once(&g_default_once, install_default); p = LOAD(g_p); }
97#endif
98 return p;
99}
100
101const OmegaGpuPlatform *omega_gpu_session_platform(void) { return platform(); }
102OmegaGpuCompletionMode omega_gpu_session_completion_mode(void) { return OMEGA_GPU_COMPLETION_POLLING; }
103
104uint64_t omega_gpu_session_now_ns(void) {
105 const OmegaGpuPlatform *p = platform();
106 return p ? p->time_now_ns(p->ctx) : 0u;
107}
108
109void *omega_gpu_host_alloc(size_t bytes) {
110 const OmegaGpuPlatform *p = platform();
111 return p ? p->host_alloc(p->ctx, bytes ? bytes : 1) : (void *)0; /* 0 bytes: a unique 1-byte block, as malloc(0) gives */
112}
113void *omega_gpu_host_calloc(size_t n, size_t size) {
114 size_t bytes;
115 if (__builtin_mul_overflow(n, size, &bytes)) return (void *)0;
116 void *q = omega_gpu_host_alloc(bytes);
117 if (q) memset(q, 0, bytes);
118 return q;
119}
120void omega_gpu_host_free(void *q) {
121 const OmegaGpuPlatform *p = platform();
122 if (p && q) p->host_free(p->ctx, q);
123}
124
125/* ---- errors ---- */
126
127const char *omega_gpu_session_last_error(void) { return g_err; }
128void omega_gpu_session_set_error(const char *stage) { omega_gpu_fmt(g_err, sizeof g_err, "%s", stage ? stage : ""); }
129
130/* A platform refusal: the driver text when the device refused (the Linux reference writes "<driver call>: <Nvrm.err>",
131 * the stage names the session ever reported), else the stage and the refusal name. */
132static void fail_plat(const char *stage, int rc) {
133 const OmegaGpuPlatform *p = LOAD(g_p);
134 const char *diag = p ? p->diag_text(p->ctx) : "";
135 if (rc == OMEGA_GPU_PLAT_REFUSED_DEVICE && diag && diag[0])
136 omega_gpu_fmt(g_err, sizeof g_err, "%s", diag);
137 else
138 omega_gpu_fmt(g_err, sizeof g_err, "%s: %s", stage, omega_gpu_platform_refusal_name(rc));
139}
140
141/* ---- lock, state ---- */
142
143void omega_gpu_session_lock(void) {
144 const OmegaGpuPlatform *p = platform();
145 if (!p) return;
146 (void)p->lock(p->ctx, OMEGA_GPU_SESSION_LOCK_ID);
147 STORE(g_locked, 1u);
148}
149void omega_gpu_session_unlock(void) {
150 const OmegaGpuPlatform *p = LOAD(g_p);
151 if (!p) return;
152 STORE(g_locked, 0u);
153 (void)p->unlock(p->ctx, OMEGA_GPU_SESSION_LOCK_ID);
154}
155bool omega_gpu_session_is_open(void) { return g_open; }
156int omega_gpu_session_is_blocked(void) { return LOAD(g_blocked) ? 1 : 0; }
157int omega_gpu_session_set_spin_us(uint32_t us) {
158 if (us > OMEGA_GPU_SESSION_MAX_SPIN_US) return -1;
159 STORE(g_spin_us, us);
160 return 0;
161}
162uint32_t omega_gpu_session_spin_us(void) { return LOAD(g_spin_us); }
163void omega_gpu_session_cancel(void) { STORE(g_cancel, 1u); }
164
165/* Consumes a pending cancel request. */
166static bool take_cancel(void) {
167 if (!LOAD(g_cancel)) return false;
168 STORE(g_cancel, 0u);
169 return true;
170}
171
172void omega_gpu_session_alloc_stats(OmegaGpuAllocStats *out) {
173 if (!out) return;
174 out->allocs = LOAD(g_stats.allocs);
175 out->alloc_bytes = LOAD(g_stats.alloc_bytes);
176 out->alloc_failures = LOAD(g_stats.alloc_failures);
177 out->frees = LOAD(g_stats.frees);
178 out->stale_refusals = LOAD(g_stats.stale_refusals);
179}
180
181/* ---- parking ---- */
182
183static void park(const OmegaGpuMem *m) {
184 uint32_t n = LOAD(g_park.buffers) - LOAD(g_park.untracked);
185 for (uint32_t i = 0; i < n; i++)
186 if (g_parked[i].index == m->handle.index && g_parked[i].generation == m->handle.generation) return;
187 if (n < OMEGA_GPU_SESSION_PARK_MAX) g_parked[n] = m->handle;
188 else BUMP(g_park.untracked, 1u);
189 BUMP(g_park.bytes, m->size);
190 BUMP(g_park.buffers, 1u);
191}
192
193void omega_gpu_session_parked(OmegaGpuParkStats *out) {
194 if (!out) return;
195 out->buffers = LOAD(g_park.buffers);
196 out->untracked = LOAD(g_park.untracked);
197 out->bytes = LOAD(g_park.bytes);
198}
199
200int omega_gpu_session_is_parked(OmegaGpuBufferHandle h) {
201 uint32_t n = LOAD(g_park.buffers) - LOAD(g_park.untracked);
202 if (h.generation == 0u) return 0;
203 for (uint32_t i = 0; i < n && i < OMEGA_GPU_SESSION_PARK_MAX; i++)
204 if (g_parked[i].index == h.index && g_parked[i].generation == h.generation) return 1;
205 return 0;
206}
207
208/* Uncertain completion: the device may still touch any session buffer. Keep everything, refuse everything. */
209static void latch(const char *why) {
210 STORE(g_blocked, 1u);
211 if (g_s.pb.cpu) park(&g_s.pb);
212 if (g_s.cbank.cpu) park(&g_s.cbank);
213 if (g_s.marker.cpu) park(&g_s.marker);
214 if (g_s.qmd.cpu) park(&g_s.qmd);
215 omega_gpu_session_set_error(why);
216}
217
218/* ---- buffers ---- */
219
220static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; }
221
222static int mem_alloc(uint32_t kind, uint32_t rights, size_t bytes, OmegaGpuMem *out) {
223 const OmegaGpuPlatform *p = LOAD(g_p);
224 memset(out, 0, sizeof *out);
225 if (!g_open || LOAD(g_blocked) || !p) { omega_gpu_session_set_error("alloc without open device"); return -1; }
226 const size_t want = page_up(bytes);
227 OmegaGpuBufferDesc d = {.kind = kind, .rights = rights, .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .align = (uint32_t)PAGE, .bytes = want};
228 OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; /* the bus address never leaves this function */
229 int rc = p->dma_alloc(p->ctx, g_s.dev, &d, &out->handle, &cpu, &va, &bus);
230 if (rc != OMEGA_GPU_PLAT_OK || !cpu.ptr || cpu.len < want) {
231 if (rc == OMEGA_GPU_PLAT_OK) { (void)p->dma_free(p->ctx, out->handle); rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; }
232 BUMP(g_stats.alloc_failures, 1u);
233 fail_plat("dma_alloc", rc);
234 memset(out, 0, sizeof *out);
235 return -1;
236 }
237 out->kind = kind; out->cpu = cpu.ptr; out->va = va.va; out->size = cpu.len;
238 BUMP(g_stats.allocs, 1u);
239 BUMP(g_stats.alloc_bytes, (uint64_t)want);
240 return 0;
241}
242
243#define RIGHTS_DATA (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE)
244#define RIGHTS_CODE (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_EXEC)
245#define RIGHTS_RING (OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ)
246#define RIGHTS_MARKER (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_WRITE | OMEGA_GPU_RIGHT_HOST_POLLED)
247#define RIGHTS_CBANK (OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ)
248
249int omega_gpu_session_alloc(size_t bytes, OmegaGpuMem *out) {
250 return mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_DATA, bytes, out);
251}
252
253int omega_gpu_session_alloc_code(size_t code_size, OmegaGpuMem *out) {
254 size_t bytes = omega_gpu_code_alloc_bytes(code_size);
255 if (bytes == 0) { omega_gpu_session_set_error("code size overflow"); memset(out, 0, sizeof *out); return -1; }
256 if (mem_alloc(OMEGA_GPU_HANDLE_BUF_CODE, RIGHTS_CODE, bytes, out) != 0) return -1;
257 memset((uint8_t *)out->cpu + code_size, 0, bytes - code_size);
258 return 0;
259}
260
261void omega_gpu_session_free(OmegaGpuMem *m) {
262 if (!m) return;
263 if (m->cpu) {
264 const OmegaGpuPlatform *p = LOAD(g_p);
265 if (LOAD(g_blocked)) {
266 park(m); /* the device may still use it */
267 } else if (!g_open || !p) {
268 BUMP(g_stats.stale_refusals, 1u); /* its device is closed: the handle is stale, nothing to release */
269 } else {
270 int rc = p->dma_free(p->ctx, m->handle);
271 if (rc == OMEGA_GPU_PLAT_OK) BUMP(g_stats.frees, 1u);
272 else { BUMP(g_stats.stale_refusals, 1u); fail_plat("dma_free", rc); }
273 }
274 }
275 memset(m, 0, sizeof *m);
276}
277
278static int sync_mem(const OmegaGpuMem *m, int to_gpu) {
279 const OmegaGpuPlatform *p = LOAD(g_p);
280 if (!m || !m->cpu) return 0;
281 if (!p || !g_open) { omega_gpu_session_set_error("sync without open device"); return -1; }
282 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);
283 if (rc != OMEGA_GPU_PLAT_OK) { fail_plat(to_gpu ? "sync_cpu_to_gpu" : "sync_gpu_to_cpu", rc); return -1; }
284 return 0;
285}
286int omega_gpu_session_sync_to_gpu(const OmegaGpuMem *m) { return sync_mem(m, 1); }
287int omega_gpu_session_sync_to_cpu(const OmegaGpuMem *m) { return sync_mem(m, 0); }
288
289int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) {
290 if (s->reserved && (bytes > s->cap || !s->mem.cpu)) {
291 omega_gpu_fmt(g_err, sizeof g_err, "serving reservation exceeded: need %zu bytes, reserved %zu", bytes, s->cap);
292 return OMEGA_GPU_SESSION_SCRATCH_RESERVED;
293 }
294 if (bytes > s->high_water) s->high_water = bytes;
295 if (s->cap >= bytes && s->mem.cpu) return 0;
296 omega_gpu_session_free(&s->mem);
297 s->cap = 0;
298 if (omega_gpu_session_alloc(s->high_water, &s->mem) != 0) return -1;
299 s->cap = (size_t)s->mem.size;
300 return 0;
301}
302
303void omega_gpu_session_scratch_free(OmegaGpuScratch *s) {
304 omega_gpu_session_free(&s->mem);
305 s->cap = 0;
306 s->reserved = false;
307}
308
309int omega_gpu_session_scratch_reserve(OmegaGpuScratch *s, size_t bytes) {
310 s->reserved = false; /* grow normally first */
311 if (omega_gpu_session_scratch(s, bytes) != 0) return -1;
312 s->reserved = true;
313 return 0;
314}
315
316void omega_gpu_session_scratch_unreserve(OmegaGpuScratch *s) { s->reserved = false; }
317
318static void free_session_buffers(void) {
319 omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker);
320 omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb);
321}
322
323/* ---- open, close ---- */
324
325OmegaGpuSession *omega_gpu_session_open(void) {
326 if (LOAD(g_blocked)) { omega_gpu_session_set_error("process latched"); return NULL; }
327 if (g_open) return &g_s;
328 const OmegaGpuPlatform *p = platform();
329 if (!p) { omega_gpu_session_set_error("no GPU platform installed (omega_gpu_session_set_platform)"); return NULL; }
330 uint64_t t0 = p->time_now_ns(p->ctx);
331 const uint32_t opens = g_s.opens;
332 memset(&g_s, 0, sizeof g_s);
333 g_s.opens = opens;
334 int rc = p->device_open(p->ctx, 0, &g_s.dev);
335 if (rc != OMEGA_GPU_PLAT_OK) { fail_plat("device_open", rc); memset(&g_s.dev, 0, sizeof g_s.dev); return NULL; }
336 g_open = true; /* so alloc / free work during the rest of the open */
337 /* same four buffers, same sizes, same order as before cut C; every one GPU-uncached */
338 if (mem_alloc(OMEGA_GPU_HANDLE_BUF_QUEUE, RIGHTS_RING, 0x10000, &g_s.pb) ||
339 mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_CBANK, PAGE, &g_s.cbank) ||
340 mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_MARKER, PAGE, &g_s.marker) ||
341 mem_alloc(OMEGA_GPU_HANDLE_BUF_DATA, RIGHTS_DATA, 0x10000, &g_s.qmd) ||
342 (rc = p->queue_open(p->ctx, g_s.dev, g_s.pb.handle, &g_s.queue)) != OMEGA_GPU_PLAT_OK) {
343 char keep[sizeof g_err];
344 if (rc != OMEGA_GPU_PLAT_OK) fail_plat("queue_open", rc);
345 memcpy(keep, g_err, sizeof keep);
346 free_session_buffers();
347 g_open = false;
348 (void)p->device_close(p->ctx, g_s.dev);
349 memset(&g_s.dev, 0, sizeof g_s.dev);
350 omega_gpu_fmt(g_err, sizeof g_err, "session scratch alloc (%s)", keep);
351 return NULL;
352 }
353 g_s.opens++;
354 g_s.last_open_ns = p->time_now_ns(p->ctx) - t0;
355 return &g_s;
356}
357
358int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */
359 if (!hook) return -1;
360 int rc = 0;
361 bool seen = false;
362 for (unsigned i = 0; i < g_nhooks; i++) if (g_hooks[i] == hook) seen = true;
363 if (!seen) { if (g_nhooks < sizeof g_hooks / sizeof g_hooks[0]) g_hooks[g_nhooks++] = hook; else rc = -1; }
364 return rc;
365}
366
367void omega_gpu_session_close(void) {
368 omega_gpu_session_lock();
369 const OmegaGpuPlatform *p = LOAD(g_p);
370 if (g_open && !LOAD(g_blocked) && p) {
371 for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i]();
372 if (!LOAD(g_blocked)) { /* a hook's launch may have latched: then nothing below runs */
373 (void)p->queue_close(p->ctx, g_s.queue);
374 memset(&g_s.queue, 0, sizeof g_s.queue);
375 free_session_buffers();
376 (void)p->device_close(p->ctx, g_s.dev); /* every handle of this device is stale from here */
377 memset(&g_s.dev, 0, sizeof g_s.dev);
378 g_open = false;
379 }
380 }
381 omega_gpu_session_unlock();
382}
383
384/* ---- launch ---- */
385
386static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; }
387
388static inline void cpu_relax(void) {
389#if defined(__aarch64__)
390 __asm__ volatile("yield" ::: "memory");
391#else
392 __asm__ volatile("" ::: "memory");
393#endif
394}
395
396#define WAIT_CANCELLED 1
397#define WAIT_TIMED_OUT 2
398#define WAIT_REFUSED 3
399
400/* Marker wait by POLLING. With spin_us, the host first re-reads the uncached marker word back to back (with the
401 * ARM yield hint) so a launch that finishes inside the window is seen within about one read. Then the platform's
402 * poll_word in slices of OMEGA_GPU_SESSION_POLL_SLICE_MS (Linux reference: the m16 50 us sleep-poll) until
403 * timeout_ms, checking for a cancel request between slices. */
404static int session_wait(const OmegaGpuPlatform *p, uint64_t off, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) {
405 volatile uint32_t *m = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + off);
406 if (spin_us) {
407 const uint64_t end = p->time_now_ns(p->ctx) + (uint64_t)spin_us * 1000u;
408 do {
409 if ((int32_t)(*m - want) >= 0) return 0;
410 if (LOAD(g_cancel)) return WAIT_CANCELLED;
411 cpu_relax();
412 } while (p->time_now_ns(p->ctx) < end);
413 }
414 uint64_t waited = 0;
415 for (;;) {
416 if (LOAD(g_cancel)) return WAIT_CANCELLED;
417 const uint64_t left = timeout_ms - waited;
418 const uint64_t slice = left < OMEGA_GPU_SESSION_POLL_SLICE_MS ? left : OMEGA_GPU_SESSION_POLL_SLICE_MS;
419 uint32_t seen = 0;
420 int rc = p->poll_word(p->ctx, g_s.marker.handle, off, want, slice, &seen);
421 if (rc == OMEGA_GPU_PLAT_OK) return 0;
422 if (rc != OMEGA_GPU_POLL_TIMED_OUT) return WAIT_REFUSED;
423 waited += slice;
424 if (waited >= timeout_ms) return WAIT_TIMED_OUT;
425 }
426}
427
428int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) {
429 const OmegaGpuPlatform *p = LOAD(g_p);
430 if (!g_open || LOAD(g_blocked) || !p) {
431 omega_gpu_session_set_error(LOAD(g_blocked) ? "process latched" : "launch without open device"); return -1;
432 }
433 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 ||
434 L->spin_us > OMEGA_GPU_SESSION_MAX_SPIN_US) {
435 omega_gpu_session_set_error("launch arguments"); return -1;
436 }
437 if (take_cancel()) { omega_gpu_session_set_error("launch cancelled before submit"); return OMEGA_GPU_SESSION_CANCELLED; }
438 const uint32_t threads_y = L->threads_y ? L->threads_y : 1;
439 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
440 omega_blackwell_build_cbank_driver_2d(cbank_data, g_s.cbank.va, L->threads_x, threads_y, L->grid_x, L->grid_y);
441 memcpy(g_s.cbank.cpu, cbank_data, sizeof cbank_data);
442 memcpy((uint8_t *)g_s.cbank.cpu + 0x380, L->args, (size_t)L->n_args * 4u);
443
444 const uint64_t qmd0_va = g_s.qmd.va, qmd1_va = g_s.qmd.va + 0x1000, sem_va = g_s.qmd.va + 0x2000, scratch_va = g_s.qmd.va + 0x4000;
445 uint32_t gpr = L->gpr_count > 64u ? round_up(L->gpr_count, 8u) : 64u;
446 OmegaBlackwellQmdConfig cfg = {
447 .code_va = L->code_va, .cbank_va = g_s.cbank.va, .scratch_va = scratch_va, .sem_va = sem_va,
448 .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements,
449 .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y,
450 .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr,
451 .shared_bytes = L->shared_bytes,
452 };
453 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS];
454 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va);
455 if (omega_blackwell_build_qmd1(qmd1_words, &cfg) != 0) { omega_gpu_session_set_error("qmd build (shared_bytes)"); return -1; }
456 if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; }
457 memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words);
458 memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words);
459
460 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)g_s.qmd.cpu + 0x2000);
461 volatile uint32_t *hmarker = (volatile uint32_t *)g_s.marker.cpu;
462 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x10);
463 *hsem = 0; *hmarker = 0; *hmarker2 = 0;
464 /* explicit visibility of everything the CPU just wrote (Linux reference: dsb sy, the barrier issued here before) */
465 int rc = p->sync_cpu_to_gpu(p->ctx, g_s.cbank.handle, 0, PAGE);
466 if (rc == OMEGA_GPU_PLAT_OK) rc = p->sync_cpu_to_gpu(p->ctx, g_s.qmd.handle, 0, 0x3000);
467 if (rc == OMEGA_GPU_PLAT_OK) rc = p->sync_cpu_to_gpu(p->ctx, g_s.marker.handle, 0, 0x20);
468 if (rc != OMEGA_GPU_PLAT_OK) { fail_plat("sync_cpu_to_gpu", rc); return -1; }
469
470 uint32_t pb[1024];
471 size_t n = 0;
472 memcpy(&pb[n], SETUP_WORDS, sizeof SETUP_WORDS); n += sizeof SETUP_WORDS / 4;
473 /* Invalidate the SM instruction, constant and data caches before this launch
474 * (cut 1b finding: a reused code address served stale instructions; clcec0.h
475 * NVCEC0_INVALIDATE_SHADER_CACHES 0x021c, bits INSTRUCTION 0, DATA 4, CONSTANT 12). */
476 pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL;
477 /* constant bank 0: driver words, then our arguments at 0x380 (inline upload) */
478 pb[n++] = bw_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(g_s.cbank.va >> 32); pb[n++] = (uint32_t)g_s.cbank.va;
479 pb[n++] = bw_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001;
480 pb[n++] = bw_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
481 pb[n++] = (224u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28);
482 memcpy(&pb[n], cbank_data, 224 * 4); n += 224;
483 pb[n++] = bw_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((g_s.cbank.va + 0x380) >> 32); pb[n++] = (uint32_t)(g_s.cbank.va + 0x380);
484 pb[n++] = bw_mthd(1, 0x0180, 2); pb[n++] = L->n_args * 4u; pb[n++] = 0x00000001;
485 pb[n++] = bw_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
486 pb[n++] = (L->n_args << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28);
487 memcpy(&pb[n], L->args, (size_t)L->n_args * 4u); n += L->n_args;
488 /* QMD0 (null grid, dependence 2) */
489 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28);
490 pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd0_va >> 8);
491 memcpy(&pb[n], qmd0_words, 96 * 4); n += 96;
492 /* intermediate semaphore init */
493 pb[n++] = bw_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va;
494 pb[n++] = bw_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001;
495 pb[n++] = bw_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
496 pb[n++] = (1u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28);
497 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
498 /* QMD1 (the kernel) */
499 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28);
500 pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd1_va >> 8);
501 memcpy(&pb[n], qmd1_words, 96 * 4); n += 96;
502 /* first marker */
503 pb[n++] = bw_mthd(0, 0x005c, 5);
504 pb[n++] = (uint32_t)g_s.marker.va; pb[n++] = (uint32_t)(g_s.marker.va >> 32);
505 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
506 /* C3 tail: L2_FLUSH_DIRTY, then the second release on the uncached marker page */
507 pb[n++] = bw_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27);
508 pb[n++] = bw_mthd(0, 0x005c, 5);
509 pb[n++] = (uint32_t)(g_s.marker.va + 0x10); pb[n++] = (uint32_t)((g_s.marker.va + 0x10) >> 32);
510 pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
511
512 const uint32_t spin_us = L->spin_us ? L->spin_us : omega_gpu_session_spin_us();
513 uint64_t t0 = p->time_now_ns(p->ctx);
514 rc = p->queue_submit(p->ctx, g_s.queue, pb, (uint32_t)n);
515 if (rc != OMEGA_GPU_PLAT_OK) {
516 fail_plat("submit_methods", rc); /* contract: a refused submit rang no doorbell, nothing is in flight */
517 return -1;
518 }
519 int w = session_wait(p, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms, spin_us);
520 if (w == 0) w = session_wait(p, 0x10, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms, spin_us);
521 if (w != 0) {
522 /* uncertain completion: keep everything (parked), refuse every later call */
523 if (w == WAIT_CANCELLED) {
524 (void)take_cancel();
525 latch("launch cancelled after submit (completion uncertain, latched)");
526 return OMEGA_GPU_SESSION_CANCELLED;
527 }
528 latch(w == WAIT_TIMED_OUT ? "marker wait timed out (latched)" : "marker poll refused (latched)");
529 return -1;
530 }
531 (void)p->sync_gpu_to_cpu(p->ctx, g_s.marker.handle, 0, 0x20);
532 (void)p->sync_gpu_to_cpu(p->ctx, g_s.qmd.handle, 0x2000, 4);
533 uint64_t t1 = p->time_now_ns(p->ctx);
534 (void)p->queue_retire(p->ctx, g_s.queue); /* both releases landed: the GPFIFO entry is consumed */
535 if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { omega_gpu_session_set_error("intermediate semaphore not DONE"); return -1; }
536 if (elapsed_ns) *elapsed_ns += t1 - t0;
537 if (marker_out) *marker_out = *hmarker;
538 (void)hmarker2;
539 return 0;
540}
541
542uint32_t omega_gpu_session_open_count(void) { return g_s.opens; }