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 / gpu-reconv-308 / T3 / FB1-CUT4B-0a1812f / omega_gpu_session.c

241 lines · 12604 bytes Raw Blame History
1/*
2 * omega_gpu_session.c: FB-1 cut 4b. See the header for the contract.
3 *
4 * The launch words are the ones omega_gpu_matmul_api.c (cut 1b, chip PASS
5 * FB1-CUT1B-4345406) and omega_gpu_elementwise_api.c (cut 4, FB1-CUT4-0d0241a)
6 * both emitted; the two were identical except for the argument word count, which
7 * is now a parameter. Nothing about the sequence changed in this cut.
8 */
9#include "omega_gpu_session.h"
10#include "omega_blackwell_qmd.h"
11#include "omega_blackwell_submit.h"
12#include "nvrm.h"
13#include <pthread.h>
14#include <stdio.h>
15#include <string.h>
16#include <time.h>
17
18#define PAGE 0x1000ull
19
20/* Same words as src/omega_blackwell_submit.c SETUP_WORDS (compute class setup). */
21static const uint32_t SETUP_WORDS[18] = {
22 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
23 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
24 0x20012559, 0x00000000,
25};
26
27static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;
28static OmegaGpuSession g_s;
29static bool g_open, g_blocked;
30static char g_err[320] = "";
31static void (*g_hooks[8])(void);
32static unsigned g_nhooks;
33
34static uint64_t now_ns(void) {
35 struct timespec t;
36 clock_gettime(CLOCK_MONOTONIC, &t);
37 return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
38}
39
40/* Marker wait. With spin_us, the host first re-reads the uncached marker word back to back (with
41 * the ARM yield hint) so a launch that finishes inside the window is seen within about one read,
42 * instead of after a 50 us sleep that Linux timer slack stretches further. Past the window it is
43 * the same m16 sleep-poll as before, with the full timeout, so a stuck launch still latches. */
44static int session_wait(volatile uint32_t *m, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) {
45 if (spin_us) {
46 const uint64_t end = now_ns() + (uint64_t)spin_us * 1000u;
47 do {
48 if ((int32_t)(*m - want) >= 0) return 0;
49 __asm__ volatile("yield" ::: "memory");
50 } while (now_ns() < end);
51 }
52 return m16_native_wait_marker(m, want, timeout_ms);
53}
54static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; }
55
56const char *omega_gpu_session_last_error(void) { return g_err; }
57void omega_gpu_session_set_error(const char *stage) {
58 snprintf(g_err, sizeof g_err, "%s", stage ? stage : "");
59}
60static void fail_rm(const char *stage, const Nvrm *rm) {
61 /* the driver text (RM class / ioctl errno / RM status, nvrm.c fail()) travels with the stage */
62 snprintf(g_err, sizeof g_err, "%s: %s", stage, rm && rm->err[0] ? rm->err : "(no driver text)");
63}
64
65void omega_gpu_session_lock(void) { pthread_mutex_lock(&g_mu); }
66void omega_gpu_session_unlock(void) { pthread_mutex_unlock(&g_mu); }
67bool omega_gpu_session_is_open(void) { return g_open; }
68int omega_gpu_session_is_blocked(void) { return g_blocked ? 1 : 0; }
69
70int omega_gpu_session_alloc(size_t bytes, NvrmMem *out) {
71 if (!g_open || g_blocked) { omega_gpu_session_set_error("alloc without open device"); return -1; }
72 if (nvrm_alloc_gpu_uncached(&g_s.ctx.rm, page_up(bytes), out) != 0) { fail_rm("nvrm_alloc_gpu_uncached", &g_s.ctx.rm); memset(out, 0, sizeof *out); return -1; }
73 return 0;
74}
75
76void omega_gpu_session_free(NvrmMem *m) {
77 if (m->cpu && g_open && !g_blocked) (void)nvrm_free(&g_s.ctx.rm, m);
78 memset(m, 0, sizeof *m);
79}
80
81int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) {
82 if (bytes > s->high_water) s->high_water = bytes;
83 if (s->cap >= bytes && s->mem.cpu) return 0;
84 omega_gpu_session_free(&s->mem);
85 s->cap = 0;
86 if (omega_gpu_session_alloc(s->high_water, &s->mem) != 0) return -1;
87 s->cap = (size_t)s->mem.size;
88 return 0;
89}
90
91void omega_gpu_session_scratch_free(OmegaGpuScratch *s) {
92 omega_gpu_session_free(&s->mem);
93 s->cap = 0;
94}
95
96static void free_session_buffers(void) {
97 omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker);
98 omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb);
99}
100
101OmegaGpuSession *omega_gpu_session_open(void) {
102 if (g_blocked) { omega_gpu_session_set_error("process latched"); return NULL; }
103 if (g_open) return &g_s;
104 uint64_t t0 = now_ns();
105 memset(&g_s.ctx, 0, sizeof g_s.ctx);
106 /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on
107 * failure) so the driver's own failure text survives into the stage name. */
108 if (nvrm_open(&g_s.ctx.rm) != 0) { fail_rm("nvrm_open", &g_s.ctx.rm); (void)nvrm_close(&g_s.ctx.rm); return NULL; }
109 if (m16_native_create_channel(&g_s.ctx) != 0) { fail_rm("m16_native_create_channel", &g_s.ctx.rm); (void)m16_native_close(&g_s.ctx); return NULL; }
110 g_open = true; /* so alloc / free work during the rest of the open */
111 if (omega_gpu_session_alloc(0x10000, &g_s.pb) || omega_gpu_session_alloc(PAGE, &g_s.cbank) ||
112 omega_gpu_session_alloc(PAGE, &g_s.marker) || omega_gpu_session_alloc(0x10000, &g_s.qmd)) {
113 char keep[sizeof g_err]; memcpy(keep, g_err, sizeof keep);
114 free_session_buffers();
115 g_open = false;
116 (void)m16_native_close(&g_s.ctx);
117 snprintf(g_err, sizeof g_err, "session scratch alloc (%s)", keep);
118 return NULL;
119 }
120 g_s.ctx.pb_mem = g_s.pb; /* m16_native_enqueue_methods copies the method stream here */
121 g_s.opens++;
122 g_s.last_open_ns = now_ns() - t0;
123 return &g_s;
124}
125
126int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */
127 if (!hook) return -1;
128 int rc = 0;
129 bool seen = false;
130 for (unsigned i = 0; i < g_nhooks; i++) if (g_hooks[i] == hook) seen = true;
131 if (!seen) { if (g_nhooks < sizeof g_hooks / sizeof g_hooks[0]) g_hooks[g_nhooks++] = hook; else rc = -1; }
132 return rc;
133}
134
135void omega_gpu_session_close(void) {
136 pthread_mutex_lock(&g_mu);
137 if (g_open && !g_blocked) {
138 for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i]();
139 free_session_buffers();
140 (void)m16_native_close(&g_s.ctx);
141 g_open = false;
142 }
143 pthread_mutex_unlock(&g_mu);
144}
145
146static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; }
147
148int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) {
149 if (!g_open || g_blocked) { omega_gpu_session_set_error(g_blocked ? "process latched" : "launch without open device"); return -1; }
150 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 ||
151 L->spin_us > OMEGA_GPU_SESSION_MAX_SPIN_US) {
152 omega_gpu_session_set_error("launch arguments"); return -1;
153 }
154 const uint32_t threads_y = L->threads_y ? L->threads_y : 1;
155 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
156 omega_blackwell_build_cbank_driver_2d(cbank_data, g_s.cbank.va, L->threads_x, threads_y, L->grid_x, L->grid_y);
157 memcpy(g_s.cbank.cpu, cbank_data, sizeof cbank_data);
158 memcpy((uint8_t *)g_s.cbank.cpu + 0x380, L->args, (size_t)L->n_args * 4u);
159
160 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;
161 uint32_t gpr = L->gpr_count > 64u ? round_up(L->gpr_count, 8u) : 64u;
162 OmegaBlackwellQmdConfig cfg = {
163 .code_va = L->code_va, .cbank_va = g_s.cbank.va, .scratch_va = scratch_va, .sem_va = sem_va,
164 .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements,
165 .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y,
166 .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr,
167 .shared_bytes = L->shared_bytes,
168 };
169 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS];
170 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va);
171 if (omega_blackwell_build_qmd1(qmd1_words, &cfg) != 0) { omega_gpu_session_set_error("qmd build (shared_bytes)"); return -1; }
172 if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; }
173 memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words);
174 memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words);
175
176 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)g_s.qmd.cpu + 0x2000);
177 volatile uint32_t *hmarker = (volatile uint32_t *)g_s.marker.cpu;
178 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x10);
179 *hsem = 0; *hmarker = 0; *hmarker2 = 0;
180 __asm__ volatile("dsb sy" ::: "memory");
181
182 uint32_t pb[1024];
183 size_t n = 0;
184 memcpy(&pb[n], SETUP_WORDS, sizeof SETUP_WORDS); n += sizeof SETUP_WORDS / 4;
185 /* Invalidate the SM instruction, constant and data caches before this launch
186 * (cut 1b finding: a reused code address served stale instructions; clcec0.h
187 * NVCEC0_INVALIDATE_SHADER_CACHES 0x021c, bits INSTRUCTION 0, DATA 4, CONSTANT 12). */
188 pb[n++] = nvrm_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL;
189 /* constant bank 0: driver words, then our arguments at 0x380 (inline upload) */
190 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(g_s.cbank.va >> 32); pb[n++] = (uint32_t)g_s.cbank.va;
191 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001;
192 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
193 pb[n++] = (224u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28);
194 memcpy(&pb[n], cbank_data, 224 * 4); n += 224;
195 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((g_s.cbank.va + 0x380) >> 32); pb[n++] = (uint32_t)(g_s.cbank.va + 0x380);
196 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = L->n_args * 4u; pb[n++] = 0x00000001;
197 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
198 pb[n++] = (L->n_args << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28);
199 memcpy(&pb[n], L->args, (size_t)L->n_args * 4u); n += L->n_args;
200 /* QMD0 (null grid, dependence 2) */
201 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28);
202 pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd0_va >> 8);
203 memcpy(&pb[n], qmd0_words, 96 * 4); n += 96;
204 /* intermediate semaphore init */
205 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va;
206 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001;
207 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
208 pb[n++] = (1u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28);
209 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
210 /* QMD1 (the kernel) */
211 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28);
212 pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd1_va >> 8);
213 memcpy(&pb[n], qmd1_words, 96 * 4); n += 96;
214 /* first marker */
215 pb[n++] = nvrm_mthd(0, 0x005c, 5);
216 pb[n++] = (uint32_t)g_s.marker.va; pb[n++] = (uint32_t)(g_s.marker.va >> 32);
217 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
218 /* C3 tail: L2_FLUSH_DIRTY, then the second release on the uncached marker page */
219 pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27);
220 pb[n++] = nvrm_mthd(0, 0x005c, 5);
221 pb[n++] = (uint32_t)(g_s.marker.va + 0x10); pb[n++] = (uint32_t)((g_s.marker.va + 0x10) >> 32);
222 pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
223
224 uint64_t t0 = now_ns();
225 if (m16_native_submit_methods(&g_s.ctx, pb, n) != 0) { fail_rm("submit_methods", &g_s.ctx.rm); return -1; }
226 if (session_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms, L->spin_us) != 0 ||
227 session_wait(hmarker2, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms, L->spin_us) != 0) {
228 g_blocked = true; /* uncertain completion: keep everything, refuse every later call */
229 omega_gpu_session_set_error("marker wait timed out (latched)");
230 return -1;
231 }
232 __asm__ volatile("dsb sy" ::: "memory");
233 uint64_t t1 = now_ns();
234 nvrm_retire(&g_s.ctx.rm, g_s.ctx.rm.put); /* both releases landed: the GPFIFO entry is consumed */
235 if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { omega_gpu_session_set_error("intermediate semaphore not DONE"); return -1; }
236 if (elapsed_ns) *elapsed_ns += t1 - t0;
237 if (marker_out) *marker_out = *hmarker;
238 return 0;
239}
240
241uint32_t omega_gpu_session_open_count(void) { return g_s.opens; }