Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/evidence/FB1-CUT4B-0396844/omega_gpu_session.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / evidence / FB1-CUT4B-0396844 / omega_gpu_session.c` 224 lines ยท 11628 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 14 #include 15 #include 16 #include 17 18 #define PAGE 0x1000ull 19 20 /* Same words as src/omega_blackwell_submit.c SETUP_WORDS (compute class setup). */ 21 static 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 27 static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; 28 static OmegaGpuSession g_s; 29 static bool g_open, g_blocked; 30 static char g_err[320] = ""; 31 static void (*g_hooks[8])(void); 32 static unsigned g_nhooks; 33 34 static 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 static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; } 40 41 const char *omega_gpu_session_last_error(void) { return g_err; } 42 void omega_gpu_session_set_error(const char *stage) { 43 snprintf(g_err, sizeof g_err, "%s", stage ? stage : ""); 44 } 45 static void fail_rm(const char *stage, const Nvrm *rm) { 46 /* the driver text (RM class / ioctl errno / RM status, nvrm.c fail()) travels with the stage */ 47 snprintf(g_err, sizeof g_err, "%s: %s", stage, rm && rm->err[0] ? rm->err : "(no driver text)"); 48 } 49 50 void omega_gpu_session_lock(void) { pthread_mutex_lock(&g_mu); } 51 void omega_gpu_session_unlock(void) { pthread_mutex_unlock(&g_mu); } 52 bool omega_gpu_session_is_open(void) { return g_open; } 53 int omega_gpu_session_is_blocked(void) { return g_blocked ? 1 : 0; } 54 55 int omega_gpu_session_alloc(size_t bytes, NvrmMem *out) { 56 if (!g_open || g_blocked) { omega_gpu_session_set_error("alloc without open device"); return -1; } 57 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; } 58 return 0; 59 } 60 61 void omega_gpu_session_free(NvrmMem *m) { 62 if (m->cpu && g_open && !g_blocked) (void)nvrm_free(&g_s.ctx.rm, m); 63 memset(m, 0, sizeof *m); 64 } 65 66 int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) { 67 if (bytes > s->high_water) s->high_water = bytes; 68 if (s->cap >= bytes && s->mem.cpu) return 0; 69 omega_gpu_session_free(&s->mem); 70 s->cap = 0; 71 if (omega_gpu_session_alloc(s->high_water, &s->mem) != 0) return -1; 72 s->cap = (size_t)s->mem.size; 73 return 0; 74 } 75 76 void omega_gpu_session_scratch_free(OmegaGpuScratch *s) { 77 omega_gpu_session_free(&s->mem); 78 s->cap = 0; 79 } 80 81 static void free_session_buffers(void) { 82 omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker); 83 omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb); 84 } 85 86 OmegaGpuSession *omega_gpu_session_open(void) { 87 if (g_blocked) { omega_gpu_session_set_error("process latched"); return NULL; } 88 if (g_open) return &g_s; 89 uint64_t t0 = now_ns(); 90 memset(&g_s.ctx, 0, sizeof g_s.ctx); 91 /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on 92 * failure) so the driver's own failure text survives into the stage name. */ 93 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; } 94 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; } 95 g_open = true; /* so alloc / free work during the rest of the open */ 96 if (omega_gpu_session_alloc(0x10000, &g_s.pb) || omega_gpu_session_alloc(PAGE, &g_s.cbank) || 97 omega_gpu_session_alloc(PAGE, &g_s.marker) || omega_gpu_session_alloc(0x10000, &g_s.qmd)) { 98 char keep[sizeof g_err]; memcpy(keep, g_err, sizeof keep); 99 free_session_buffers(); 100 g_open = false; 101 (void)m16_native_close(&g_s.ctx); 102 snprintf(g_err, sizeof g_err, "session scratch alloc (%s)", keep); 103 return NULL; 104 } 105 g_s.ctx.pb_mem = g_s.pb; /* m16_native_enqueue_methods copies the method stream here */ 106 g_s.opens++; 107 g_s.last_open_ns = now_ns() - t0; 108 return &g_s; 109 } 110 111 int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */ 112 if (!hook) return -1; 113 int rc = 0; 114 bool seen = false; 115 for (unsigned i = 0; i < g_nhooks; i++) if (g_hooks[i] == hook) seen = true; 116 if (!seen) { if (g_nhooks < sizeof g_hooks / sizeof g_hooks[0]) g_hooks[g_nhooks++] = hook; else rc = -1; } 117 return rc; 118 } 119 120 void omega_gpu_session_close(void) { 121 pthread_mutex_lock(&g_mu); 122 if (g_open && !g_blocked) { 123 for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); 124 free_session_buffers(); 125 (void)m16_native_close(&g_s.ctx); 126 g_open = false; 127 } 128 pthread_mutex_unlock(&g_mu); 129 } 130 131 static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } 132 133 int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) { 134 if (!g_open || g_blocked) { omega_gpu_session_set_error(g_blocked ? "process latched" : "launch without open device"); return -1; } 135 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) { 136 omega_gpu_session_set_error("launch arguments"); return -1; 137 } 138 const uint32_t threads_y = L->threads_y ? L->threads_y : 1; 139 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; 140 omega_blackwell_build_cbank_driver_2d(cbank_data, g_s.cbank.va, L->threads_x, threads_y, L->grid_x, L->grid_y); 141 memcpy(g_s.cbank.cpu, cbank_data, sizeof cbank_data); 142 memcpy((uint8_t *)g_s.cbank.cpu + 0x380, L->args, (size_t)L->n_args * 4u); 143 144 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; 145 uint32_t gpr = L->gpr_count > 64u ? round_up(L->gpr_count, 8u) : 64u; 146 OmegaBlackwellQmdConfig cfg = { 147 .code_va = L->code_va, .cbank_va = g_s.cbank.va, .scratch_va = scratch_va, .sem_va = sem_va, 148 .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements, 149 .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y, 150 .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, 151 }; 152 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; 153 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); 154 omega_blackwell_build_qmd1(qmd1_words, &cfg); 155 if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; } 156 memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words); 157 memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words); 158 159 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)g_s.qmd.cpu + 0x2000); 160 volatile uint32_t *hmarker = (volatile uint32_t *)g_s.marker.cpu; 161 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x10); 162 *hsem = 0; *hmarker = 0; *hmarker2 = 0; 163 __asm__ volatile("dsb sy" ::: "memory"); 164 165 uint32_t pb[1024]; 166 size_t n = 0; 167 memcpy(&pb[n], SETUP_WORDS, sizeof SETUP_WORDS); n += sizeof SETUP_WORDS / 4; 168 /* Invalidate the SM instruction, constant and data caches before this launch 169 * (cut 1b finding: a reused code address served stale instructions; clcec0.h 170 * NVCEC0_INVALIDATE_SHADER_CACHES 0x021c, bits INSTRUCTION 0, DATA 4, CONSTANT 12). */ 171 pb[n++] = nvrm_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; 172 /* constant bank 0: driver words, then our arguments at 0x380 (inline upload) */ 173 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(g_s.cbank.va >> 32); pb[n++] = (uint32_t)g_s.cbank.va; 174 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001; 175 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; 176 pb[n++] = (224u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); 177 memcpy(&pb[n], cbank_data, 224 * 4); n += 224; 178 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); 179 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = L->n_args * 4u; pb[n++] = 0x00000001; 180 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; 181 pb[n++] = (L->n_args << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); 182 memcpy(&pb[n], L->args, (size_t)L->n_args * 4u); n += L->n_args; 183 /* QMD0 (null grid, dependence 2) */ 184 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); 185 pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd0_va >> 8); 186 memcpy(&pb[n], qmd0_words, 96 * 4); n += 96; 187 /* intermediate semaphore init */ 188 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va; 189 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001; 190 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; 191 pb[n++] = (1u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); 192 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; 193 /* QMD1 (the kernel) */ 194 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); 195 pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd1_va >> 8); 196 memcpy(&pb[n], qmd1_words, 96 * 4); n += 96; 197 /* first marker */ 198 pb[n++] = nvrm_mthd(0, 0x005c, 5); 199 pb[n++] = (uint32_t)g_s.marker.va; pb[n++] = (uint32_t)(g_s.marker.va >> 32); 200 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); 201 /* C3 tail: L2_FLUSH_DIRTY, then the second release on the uncached marker page */ 202 pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27); 203 pb[n++] = nvrm_mthd(0, 0x005c, 5); 204 pb[n++] = (uint32_t)(g_s.marker.va + 0x10); pb[n++] = (uint32_t)((g_s.marker.va + 0x10) >> 32); 205 pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); 206 207 uint64_t t0 = now_ns(); 208 if (m16_native_submit_methods(&g_s.ctx, pb, n) != 0) { fail_rm("submit_methods", &g_s.ctx.rm); return -1; } 209 if (m16_native_wait_marker(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms) != 0 || 210 m16_native_wait_marker(hmarker2, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms) != 0) { 211 g_blocked = true; /* uncertain completion: keep everything, refuse every later call */ 212 omega_gpu_session_set_error("marker wait timed out (latched)"); 213 return -1; 214 } 215 __asm__ volatile("dsb sy" ::: "memory"); 216 uint64_t t1 = now_ns(); 217 nvrm_retire(&g_s.ctx.rm, g_s.ctx.rm.put); /* both releases landed: the GPFIFO entry is consumed */ 218 if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { omega_gpu_session_set_error("intermediate semaphore not DONE"); return -1; } 219 if (elapsed_ns) *elapsed_ns += t1 - t0; 220 if (marker_out) *marker_out = *hmarker; 221 return 0; 222 } 223 224 uint32_t omega_gpu_session_open_count(void) { return g_s.opens; }