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 / src / omega_gpu_session.c

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