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/src/omega_gpu_cmdlist.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 / src / omega_gpu_cmdlist.c` 560 lines ยท 34564 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_cmdlist.c: many launches in one submission (see the header). 19 * 20 * The per-launch words reuse the session's chip-proven pieces (omega_gpu_session.c, read 2026-10-13): SETUP_WORDS, 21 * INVALIDATE_SHADER_CACHES_ALL, the QMD0/QMD1 builders, the C3 tail (first marker with RELEASE_WFI, L2_FLUSH_DIRTY 22 * mem-op, second marker on the uncached page) and the poll loop. What is new: banks and QMDs live in per-launch slots 23 * written by the CPU, WAIT_FOR_IDLE sits between launches, and the PCAS launch method. 24 */ 25 #include "omega_gpu_cmdlist.h" 26 #include "omega_blackwell_codegen.h" 27 #include "omega_blackwell_qmd.h" 28 #include "omega_native.h" 29 #include "omega_blackwell_submit.h" 30 #include 31 32 #define PAGE 0x1000ull 33 #define RING_WORDS (0x10000u / 4u) /* the session ring: 64 KB */ 34 #define DEFAULT_TIMEOUT_MS 20000ull 35 #define MTHD_WAIT_FOR_IDLE 0x0110u /* clcec0.h NVCEC0_WAIT_FOR_IDLE */ 36 /* Completion markers live on the SESSION marker page (OmegaGpuSession.marker, allocated by the session with 37 * OMEGA_GPU_RIGHT_HOST_POLLED, GPU-uncached): the platform refuses poll_word on any buffer without that right 38 * (omega_gpu_platform_linux.c ref_poll_word, chip run cmdlist-r1 2026-10-13: "marker poll refused"), and the session 39 * exposes no polled allocation. The session uses bytes 0..0x1f of the page; this module uses 0x100..0x11f. */ 40 #define MARKER_OFF 0x100ull 41 #define MTHD_SEND_PCAS_A 0x02b4u /* clcec0.h NVCEC0_SEND_PCAS_A, QMD_ADDRESS_SHIFTED8 */ 42 #define MTHD_SEND_SIGNALING_PCAS2_B 0x02c0u /* clcec0.h, PCAS_ACTION 3:0 */ 43 #define PCAS_ACTION_INVALIDATE_COPY_SCHEDULE 3u 44 45 static 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 /* Same words as omega_gpu_session.c / omega_blackwell_submit.c SETUP_WORDS (compute class setup). */ 49 static const uint32_t SETUP_WORDS[18] = { 50 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, 51 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, 52 0x20012559, 0x00000000, 53 }; 54 55 struct OmegaGpuCmdList { 56 uint32_t cap, n; 57 OmegaGpuMem banks, qmds, sems, scratch; 58 OmegaGpuMem stamps; /* (cap + 1) x 16 B: compute-class REPORT_SEMAPHORE four-word reports {u64 payload, u64 timestamp} 59 (mesa clcec0.h 0x0158..0x0168, nvk_query.h), slot 0 = submit start, slot i + 1 = launch i done */ 60 uint32_t *words; /* host method stream */ 61 uint32_t words_cap; 62 OmegaGpuMem nop_code; /* EXIT kernel used to force the session latch after a timeout */ 63 uint32_t nop_gpr; 64 uint8_t *indep; /* per launch: 1 = shares no data with the launch before it (mark_independent): no barrier before it */ 65 int stamped; /* the last submit had opts.stamps (the accessor refuses stale stamps from an earlier submit) */ 66 uint64_t *wait_va; /* per launch: semaphore word of another list's launch to wait for before it (wait_for), 0 = none */ 67 uint64_t pending_wait;/* wait_for recorded, taken by the next add */ 68 uint32_t nwaits; /* launches with a wait (6 more words each) */ 69 const OmegaGpuCmdList *wait_other; /* the one list this list waits on (it must be the other list of the pair) */ 70 uint32_t wait_max; /* highest launch index of wait_other waited on */ 71 uint32_t gen; /* omega_gpu_session_recoveries() at create: a list from before a recovery is stale (C5) */ 72 }; 73 74 static uint32_t g_latched; /* 1 + omega_gpu_session_recoveries() at the latch; 0 never latched (C5: a recovery lifts it) */ 75 76 int omega_gpu_cmdlist_is_latched(void) { return __atomic_load_n(&g_latched, __ATOMIC_ACQUIRE) == omega_gpu_session_recoveries() + 1u ? 1 : 0; } 77 uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl) { return cl ? cl->n : 0; } 78 void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl) { if (cl) { cl->n = 0; cl->pending_wait = 0; cl->nwaits = 0; cl->wait_other = NULL; cl->wait_max = 0; } } 79 /* C5: every handle of a list made before the latest session recovery belongs to the closed device */ 80 static int stale(const OmegaGpuCmdList *cl) { 81 if (cl->gen == omega_gpu_session_recoveries()) return 0; 82 omega_gpu_session_set_error("cmdlist: made before a session recovery (its buffers are gone): create a new list"); 83 return 1; 84 } 85 int omega_gpu_cmdlist_wait_for(OmegaGpuCmdList *cl, const OmegaGpuCmdList *other, uint32_t index) { 86 if (!cl || !other || cl == other || index >= other->n) { omega_gpu_session_set_error("cmdlist wait_for: another list and one of its launches"); return -1; } 87 if (stale(cl) || stale(other)) return -1; 88 if (cl->wait_other && cl->wait_other != other) { omega_gpu_session_set_error("cmdlist wait_for: a list waits on one other list only"); return -1; } 89 cl->wait_other = other; 90 if (index > cl->wait_max) cl->wait_max = index; 91 cl->pending_wait = other->sems.va + (uint64_t)index * 16u; 92 return 0; 93 } 94 int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl) { 95 if (!cl || cl->n == 0) { omega_gpu_session_set_error("cmdlist mark_independent: no launch recorded"); return -1; } 96 cl->indep[cl->n - 1] = 1; 97 return 0; 98 } 99 100 #include "omega_bw_om_ir.h" 101 #include "omega_native.h" 102 103 static int build_nop(OmegaGpuCmdList *cl) { 104 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 105 if (!prog) return -1; 106 uint64_t ins[8192], meta[512]; 107 memset(ins, 0, sizeof(ins)); 108 memset(meta, 0, sizeof(meta)); 109 uint64_t om_rc = 1; 110 int rc = (omega2_bw_cmdlist_nop(&om_rc, ins, 8192, meta, 512) == OMEGA_OK && om_rc == 0) 111 ? omega_bw_ir_from_om(ins, meta, prog) 112 : -1; 113 size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; 114 if (need == 0 || omega_gpu_session_alloc_code(need, &cl->nop_code) != 0) { omega_gpu_host_free(prog); return -1; } 115 OmegaBlackwellKernel k; 116 rc = omega_blackwell_kernel_encode_into(prog, cl->nop_code.cpu, need, &k); 117 omega_gpu_host_free(prog); 118 if (rc != 0 || omega_gpu_session_sync_to_gpu(&cl->nop_code) != 0) return -1; 119 cl->nop_gpr = omega_bw_gpr_declared(k.gpr_count); 120 if (cl->nop_gpr < 64u) cl->nop_gpr = 64u; 121 return 0; 122 } 123 124 int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out) { 125 if (!out || cap == 0 || cap > OMEGA_GPU_CMDLIST_MAX_LAUNCHES) { omega_gpu_session_set_error("cmdlist create: arguments"); return -1; } 126 *out = NULL; 127 OmegaGpuCmdList *cl = omega_gpu_host_calloc(1, sizeof *cl); 128 if (!cl) return -1; 129 cl->cap = cap; 130 cl->gen = omega_gpu_session_recoveries(); 131 cl->words_cap = RING_WORDS; 132 cl->words = omega_gpu_host_alloc((size_t)cl->words_cap * 4u); 133 cl->indep = omega_gpu_host_calloc(cap, 1); 134 cl->wait_va = omega_gpu_host_calloc(cap, sizeof *cl->wait_va); 135 int rc = cl->words && cl->indep && cl->wait_va ? 0 : -1; 136 omega_gpu_session_lock(); 137 if (rc == 0 && !omega_gpu_session_open()) rc = -1; 138 if (rc == 0 && (omega_gpu_session_alloc((size_t)cap * OMEGA_GPU_CMDLIST_BANK_BYTES, &cl->banks) != 0 || 139 omega_gpu_session_alloc((size_t)cap * OMEGA_GPU_CMDLIST_QMD_BYTES, &cl->qmds) != 0 || 140 omega_gpu_session_alloc((size_t)cap * 16u, &cl->sems) != 0 || 141 omega_gpu_session_alloc((size_t)(cap + 1u) * 16u, &cl->stamps) != 0 || 142 omega_gpu_session_alloc(0x4000, &cl->scratch) != 0)) 143 rc = -1; 144 if (rc == 0 && build_nop(cl) != 0) { omega_gpu_session_set_error("cmdlist create: nop kernel"); rc = -1; } 145 if (rc == 0) { 146 memset(cl->scratch.cpu, 0, 0x4000); 147 if (omega_gpu_session_sync_to_gpu(&cl->scratch) != 0) rc = -1; 148 } 149 omega_gpu_session_unlock(); 150 if (rc != 0) { omega_gpu_cmdlist_free(cl); return -1; } 151 *out = cl; 152 return 0; 153 } 154 155 void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl) { 156 if (!cl) return; 157 omega_gpu_session_lock(); 158 OmegaGpuMem *m[] = { &cl->banks, &cl->qmds, &cl->sems, &cl->stamps, &cl->scratch, &cl->nop_code }; 159 for (unsigned i = 0; i < sizeof m / sizeof m[0]; i++) if (m[i]->cpu) omega_gpu_session_free(m[i]); 160 omega_gpu_session_unlock(); 161 omega_gpu_host_free(cl->words); 162 omega_gpu_host_free(cl->indep); 163 omega_gpu_host_free(cl->wait_va); 164 omega_gpu_host_free(cl); 165 } 166 167 static uint32_t round8(uint32_t v) { return (v + 7u) / 8u * 8u; } 168 169 static int publish_slot(OmegaGpuCmdList *cl, uint32_t i) { 170 cl->indep[i] = 0; 171 cl->wait_va[i] = cl->pending_wait; 172 if (cl->pending_wait) cl->nwaits++; 173 cl->pending_wait = 0; 174 cl->n++; 175 return (int)i; 176 } 177 178 int omega_gpu_cmdlist_record_attn256(OmegaGpuCmdList *cl, const uint64_t spec[16], uint64_t code_va, 179 uint32_t gprs, uint32_t grid_y, uint32_t output_words) { 180 if (!cl || !spec || stale(cl)) return -1; 181 if (cl->n >= cl->cap) { omega_gpu_session_set_error("cmdlist add: list full"); return -1; } 182 const uint32_t i = cl->n; 183 const size_t bank_off = (size_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES; 184 const size_t qmd_off = (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; 185 uint32_t *bank = (uint32_t *)((uint8_t *)cl->banks.cpu + bank_off); 186 uint32_t *q1 = (uint32_t *)((uint8_t *)cl->qmds.cpu + qmd_off); 187 const uint64_t slot[8] = {code_va, cl->banks.va + bank_off, cl->scratch.va, cl->sems.va + (uint64_t)i * 16u, 188 cl->qmds.va + qmd_off, gprs, grid_y, output_words}; 189 uint64_t result = 1; 190 if (omega2_bw_attn256_record(&result, spec, 16, slot, 8, omega_bw_cbank_driver_template, OMEGA_BW_CBANK_DRIVER_WORDS, 191 bank, 249, q1, OMEGA_BW_QMD_WORDS, q1 + 0x200 / sizeof(*q1), OMEGA_BW_QMD_WORDS) 192 != OMEGA_OK || result != 0) { 193 omega_gpu_session_set_error("cmdlist record: hd256 attention"); 194 return -1; 195 } 196 return publish_slot(cl, i); 197 } 198 199 int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) { 200 if (!cl || !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 || 201 L->code_va == 0) { omega_gpu_session_set_error("cmdlist add: arguments"); return -1; } 202 if (stale(cl)) return -1; 203 if (cl->n >= cl->cap) { omega_gpu_session_set_error("cmdlist add: list full"); return -1; } 204 const uint32_t i = cl->n; 205 const uint32_t ty = L->threads_y ? L->threads_y : 1; 206 const uint64_t bank_va = cl->banks.va + (uint64_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES; 207 uint8_t *bank = (uint8_t *)cl->banks.cpu + (size_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES; 208 uint32_t drv[OMEGA_BW_CBANK_DRIVER_WORDS]; 209 omega_blackwell_build_cbank_driver_2d(drv, bank_va, L->threads_x, ty, L->grid_x, L->grid_y); 210 memcpy(bank, drv, sizeof drv); 211 memset(bank + sizeof drv, 0, 0x380 - sizeof drv); 212 memcpy(bank + 0x380, L->args, (size_t)L->n_args * 4u); 213 214 const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES, q0_va = q1_va + 0x200; 215 const uint64_t sem_va = cl->sems.va + (uint64_t)i * 16u; 216 uint32_t gpr = L->gpr_count > 64u ? round8(L->gpr_count) : 64u; 217 OmegaBlackwellQmdConfig cfg = { 218 .code_va = L->code_va, .cbank_va = bank_va, .scratch_va = cl->scratch.va, .sem_va = sem_va, 219 .qmd0_va = q0_va, .qmd1_va = q1_va, .num_elements = (uint32_t)L->num_elements, 220 .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = ty, 221 .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, .shared_bytes = L->shared_bytes, 222 }; 223 uint32_t q1[OMEGA_BW_QMD_WORDS], q0[OMEGA_BW_QMD_WORDS]; 224 if (omega_blackwell_build_qmd1(q1, &cfg) != 0 || omega_blackwell_verify_qmd_invariants(q1) != 0) { omega_gpu_session_set_error("cmdlist add: qmd"); return -1; } 225 omega_blackwell_build_qmd0(q0, q0_va, q1_va); 226 uint8_t *qs = (uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; 227 memcpy(qs, q1, sizeof q1); 228 memcpy(qs + 0x200, q0, sizeof q0); 229 return publish_slot(cl, i); 230 } 231 232 #define WAIT_OK 0 233 #define WAIT_TIMED_OUT 2 234 #define WAIT_REFUSED 3 235 236 static inline void cpu_relax(void) { 237 #if defined(__aarch64__) 238 __asm__ volatile("yield" ::: "memory"); 239 #else 240 __asm__ volatile("" ::: "memory"); 241 #endif 242 } 243 244 static int wait_word(const OmegaGpuPlatform *p, const OmegaGpuMem *marker, uint64_t off, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) { 245 off += MARKER_OFF; 246 volatile uint32_t *m = (volatile uint32_t *)((uint8_t *)marker->cpu + off); 247 if (spin_us) { 248 const uint64_t end = p->time_now_ns(p->ctx) + (uint64_t)spin_us * 1000u; 249 do { if ((int32_t)(*m - want) >= 0) return WAIT_OK; cpu_relax(); } while (p->time_now_ns(p->ctx) < end); 250 } 251 uint64_t waited = 0; 252 for (;;) { 253 uint32_t seen = 0; 254 int rc = p->poll_word(p->ctx, marker->handle, off, want, 1, &seen); 255 if (rc == OMEGA_GPU_PLAT_OK) return WAIT_OK; 256 if (rc != OMEGA_GPU_POLL_TIMED_OUT) return WAIT_REFUSED; 257 if (++waited >= timeout_ms) return WAIT_TIMED_OUT; 258 } 259 } 260 261 /* Uncertain completion: latch this module, then queue a 1 ms session launch behind the stuck list. If the channel is 262 * still busy it times out and the session latches the whole process (parks every buffer, refuses every API). */ 263 static void force_latch(OmegaGpuCmdList *cl, const char *why) { 264 __atomic_store_n(&g_latched, omega_gpu_session_recoveries() + 1u, __ATOMIC_RELEASE); 265 uint32_t arg = 0; 266 OmegaGpuLaunch L = { .code_va = cl->nop_code.va, .gpr_count = cl->nop_gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1, 267 .num_elements = 1, .args = &arg, .n_args = 1, .timeout_ms = 1 }; 268 (void)omega_gpu_session_launch(&L, NULL, NULL); 269 omega_gpu_session_note_module_latch(); /* C5: so omega_gpu_session_recover lifts it even when the check launch completed */ 270 omega_gpu_session_set_error(why); 271 } 272 273 static size_t emit_tail(uint32_t *pb, size_t n, uint64_t marker_va) { 274 pb[n++] = bw_mthd(0, 0x005c, 5); 275 pb[n++] = (uint32_t)marker_va; pb[n++] = (uint32_t)(marker_va >> 32); 276 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); 277 pb[n++] = bw_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27); 278 pb[n++] = bw_mthd(0, 0x005c, 5); 279 pb[n++] = (uint32_t)(marker_va + 0x10); pb[n++] = (uint32_t)((marker_va + 0x10) >> 32); 280 pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); 281 return n; 282 } 283 #define TAIL_WORDS 17u 284 285 /* Compute-class four-word report {u64 payload = slot, u64 GPU timestamp} to stamps slot `slot` (mesa clcec0.h: PAYLOAD_LOWER 0x0158, 286 * PAYLOAD_UPPER 0x015c, ADDRESS_LOWER 0x0160, ADDRESS_UPPER 0x0164, EXECUTE 0x0168 = OPERATION_RELEASE 0 | STRUCTURE_SIZE_FOUR_WORDS 0). 287 * Ordered after the preceding WAIT_FOR_IDLE, like nvk_query_pool.c timestamp queries. */ 288 #define MTHD_REPORT_SEMAPHORE_PAYLOAD_LOWER 0x0158u 289 #define STAMP_WORDS 6u 290 /* INVALIDATE_SHADER_CACHES (NVCEC0 0x021c) fields, clc6c0.h:222-236 (same layout on Blackwell): INSTRUCTION 0:0, FLUSH_DATA 2:2, DATA 4:4, CONSTANT 12:12 */ 291 #define INVALIDATE_DATA (1u << 4) 292 #define INVALIDATE_FLUSH_DATA (1u << 2) 293 static size_t emit_stamp(uint32_t *pb, size_t n, uint64_t stamps_va, uint32_t slot) { 294 const uint64_t va = stamps_va + (uint64_t)slot * 16u; 295 pb[n++] = bw_mthd(1, MTHD_REPORT_SEMAPHORE_PAYLOAD_LOWER, 5); 296 pb[n++] = slot; pb[n++] = 0; pb[n++] = (uint32_t)va; pb[n++] = (uint32_t)(va >> 32); pb[n++] = 0; 297 return n; 298 } 299 300 static size_t emit_inline_qmd(uint32_t *pb, size_t n, uint64_t va, const uint32_t *words) { 301 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); 302 pb[n++] = (1u << 30) | (uint32_t)((va >> 40) & 0x1ff); pb[n++] = (uint32_t)(va >> 8); 303 memcpy(&pb[n], words, 96 * 4); return n + 96; 304 } 305 306 /* One submission's method stream for launches [done, end) into cl->words, ending with the completion tail at marker_va. 307 * Shared by submit and submit_pair (L136) so both emit the same words. Returns the word count. */ 308 static size_t emit_chunk(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *op, uint32_t done, uint32_t end, uint64_t marker_va) { 309 const OmegaGpuCmdListOpts o = *op; 310 uint32_t *pb = cl->words; 311 size_t n = 0; 312 memcpy(pb, SETUP_WORDS, sizeof SETUP_WORDS); n += 18; 313 for (uint32_t i = done; i < end; i++) { 314 if (cl->wait_va[i]) { 315 /* wait_for: host semaphore ACQUIRE (clc96f.h SEM_ADDR_LO 0x5c .. SEM_EXECUTE 0x6c, OPERATION_ACQUIRE 0, 32-bit 316 * payload; the omega-mv#580 ORDER_SEM words, chip-proven): this queue stops here until the other list's launch 317 * released DONE. Before the barrier, so the launch's cache invalidate comes after the wait. */ 318 const uint64_t va = cl->wait_va[i]; 319 pb[n++] = bw_mthd(0, 0x005c, 5); pb[n++] = (uint32_t)va; pb[n++] = (uint32_t)(va >> 32); 320 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; pb[n++] = 0; pb[n++] = 0; 321 } 322 if (o.skip_launch && i == (uint32_t)(o.skip_launch - 1)) continue; 323 /* the first launch of a submission gets the full barrier (CPU-written QMDs, code and banks before it), unless 324 * opts.first_barrier_none (C4 streams measurement: a WAIT_FOR_IDLE may also wait for another queue's grids) */ 325 const int indep = i != done && cl->indep[i]; /* marked independent: no barrier, overlaps the launch before it */ 326 /* a launch that waited (wait_for) always gets the full barrier: its invalidate must come after the wait */ 327 const OmegaGpuCmdListBarrier b = cl->wait_va[i] ? OMEGA_GPU_CMDLIST_BARRIER_WFI 328 : i == done ? (i == 0 && o.first_barrier_none ? OMEGA_GPU_CMDLIST_BARRIER_NONE : OMEGA_GPU_CMDLIST_BARRIER_WFI) 329 : indep ? OMEGA_GPU_CMDLIST_BARRIER_NONE : o.barrier; 330 if (indep && o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* no WFI: slot i is the front-end time, launch i - 1 may still run */ 331 if (b != OMEGA_GPU_CMDLIST_BARRIER_NONE) { 332 if (b != OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA) { pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; } 333 else { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA | INVALIDATE_FLUSH_DATA; } /* implicit WFI (NVK) */ 334 if (o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* slot i: launch i - 1 done (slot `done`: submit start) */ 335 if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; } 336 else if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA; } 337 } 338 const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; 339 if (o.mode == OMEGA_GPU_CMDLIST_INLINE) { 340 const uint8_t *qs = (const uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; 341 uint32_t q1[OMEGA_BW_QMD_WORDS]; 342 memcpy(q1, qs, sizeof q1); 343 q1[10] = 0x00190000u; q1[12] = (uint32_t)((q1_va + 0x200) >> 8); /* the session pair: QMD1 decrements QMD0 */ 344 n = emit_inline_qmd(pb, n, q1_va + 0x200, (const uint32_t *)(qs + 0x200)); 345 n = emit_inline_qmd(pb, n, q1_va, q1); 346 } else { 347 /* standalone grid: no dependent QMD (NVK builds Blackwell QMDs without one, nak/qmd.rs Qmd5_0) */ 348 uint32_t *q1 = (uint32_t *)((uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES); 349 q1[10] = 0; q1[12] = 0; 350 pb[n++] = bw_mthd(1, MTHD_SEND_PCAS_A, 1); pb[n++] = (uint32_t)(q1_va >> 8); 351 pb[n++] = bw_mthd(1, MTHD_SEND_SIGNALING_PCAS2_B, 1); pb[n++] = PCAS_ACTION_INVALIDATE_COPY_SCHEDULE; 352 } 353 } 354 if (o.stamps) { 355 pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; 356 n = emit_stamp(pb, n, cl->stamps.va, end); /* slot end: the last launch done */ 357 } 358 n = emit_tail(pb, n, marker_va); 359 return n; 360 } 361 362 int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st) { 363 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); 364 if (opts) o = *opts; 365 if (st) { memset(st, 0, sizeof *st); st->first_missing = -1; } 366 const OmegaGpuPlatform *p = omega_gpu_session_platform(); 367 if (!cl || cl->n == 0 || !p) { omega_gpu_session_set_error("cmdlist submit: empty list or no platform"); return -1; } 368 if (stale(cl)) return -1; 369 if (cl->nwaits) { omega_gpu_session_set_error("cmdlist submit: a list with waits (wait_for) runs only in a pair (submit_pair)"); return -1; } 370 if (omega_gpu_cmdlist_is_latched() || omega_gpu_session_is_blocked()) { omega_gpu_session_set_error("cmdlist submit: latched"); return -1; } 371 OmegaGpuSession *s = omega_gpu_session_open(); 372 if (!s) return -1; 373 if (!s->marker.cpu || s->marker.size < MARKER_OFF + 0x20) { omega_gpu_session_set_error("cmdlist submit: session marker page"); return -1; } 374 const OmegaGpuMem *marker = &s->marker; 375 const uint64_t marker_va = marker->va + MARKER_OFF; 376 const uint64_t timeout = o.timeout_ms ? o.timeout_ms : DEFAULT_TIMEOUT_MS; 377 const uint32_t spin = o.spin_us ? o.spin_us : omega_gpu_session_spin_us(); 378 const uint64_t th0 = p->time_now_ns(p->ctx); 379 380 /* semaphores INIT, markers 0, everything the CPU wrote made visible */ 381 uint32_t *sem = cl->sems.cpu; 382 for (uint32_t i = 0; i < cl->n; i++) sem[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; 383 volatile uint32_t *mk = (volatile uint32_t *)((uint8_t *)marker->cpu + MARKER_OFF); 384 mk[0] = 0; mk[4] = 0; 385 if (p->sync_cpu_to_gpu(p->ctx, cl->banks.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_BANK_BYTES) != OMEGA_GPU_PLAT_OK || 386 p->sync_cpu_to_gpu(p->ctx, cl->qmds.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK || 387 p->sync_cpu_to_gpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u) != OMEGA_GPU_PLAT_OK || 388 p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, 0x20) != OMEGA_GPU_PLAT_OK) { 389 omega_gpu_session_set_error("cmdlist submit: sync_cpu_to_gpu"); return -1; 390 } 391 392 const uint32_t per_launch = (o.mode == OMEGA_GPU_CMDLIST_INLINE ? 4u + 2u * 99u : 8u) + (o.stamps ? STAMP_WORDS : 0u) + (cl->nwaits ? 6u : 0u); 393 /* The platform may put its own words ahead of ours in the ring (the Linux reference: the omega #457 L2 invalidate 394 * after a host write to a GPU-cached buffer). Budget what it publishes, or a near-full submission is refused. */ 395 const uint32_t prefix = omega_gpu_session_submit_prefix_words(); 396 const uint32_t avail = cl->words_cap - 18u - TAIL_WORDS - 2u - STAMP_WORDS; 397 if (prefix > avail || avail - prefix < per_launch) { omega_gpu_session_set_error("cmdlist submit: the platform submit prefix leaves no room for a launch"); return -1; } 398 uint64_t *stamp_cpu = cl->stamps.cpu; 399 cl->stamped = o.stamps ? 1 : 0; 400 if (o.stamps) { 401 memset(stamp_cpu, 0xff, (size_t)(cl->n + 1u) * 16u); /* poisoned: a slot the chip never wrote reads all-ones */ 402 if (p->sync_cpu_to_gpu(p->ctx, cl->stamps.handle, 0, (uint64_t)(cl->n + 1u) * 16u) != OMEGA_GPU_PLAT_OK) { 403 omega_gpu_session_set_error("cmdlist submit: sync stamps"); return -1; 404 } 405 } 406 const uint32_t room = (avail - prefix) / per_launch; 407 uint64_t elapsed = 0, host = p->time_now_ns(p->ctx) - th0; 408 uint32_t done = 0, submits = 0, words = 0; 409 while (done < cl->n) { 410 const uint64_t tb = p->time_now_ns(p->ctx); 411 const uint32_t end = (cl->n - done > room) ? done + room : cl->n; 412 uint32_t *pb = cl->words; 413 size_t n = emit_chunk(cl, &o, done, end, marker_va); 414 if (o.mode != OMEGA_GPU_CMDLIST_INLINE && 415 p->sync_cpu_to_gpu(p->ctx, cl->qmds.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK) { 416 omega_gpu_session_set_error("cmdlist submit: sync qmd"); return -1; 417 } 418 mk[0] = 0; mk[4] = 0; 419 (void)p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, 0x20); 420 const uint64_t t0 = p->time_now_ns(p->ctx); 421 host += t0 - tb; 422 if (p->queue_submit(p->ctx, s->queue, pb, (uint32_t)n) != OMEGA_GPU_PLAT_OK) { 423 omega_gpu_session_set_error("cmdlist submit: queue_submit refused"); return -1; /* refused: nothing in flight */ 424 } 425 int w = wait_word(p, marker, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin); 426 if (w == WAIT_OK) w = wait_word(p, marker, 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin); 427 if (w != WAIT_OK) { force_latch(cl, w == WAIT_TIMED_OUT ? "cmdlist: marker wait timed out (latched)" : "cmdlist: marker poll refused (latched)"); return -1; } 428 (void)p->sync_gpu_to_cpu(p->ctx, marker->handle, MARKER_OFF, 0x20); 429 elapsed += p->time_now_ns(p->ctx) - t0; 430 (void)p->queue_retire(p->ctx, s->queue); 431 submits++; words += (uint32_t)n; done = end; 432 } 433 (void)p->sync_gpu_to_cpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u); 434 if (o.stamps) (void)p->sync_gpu_to_cpu(p->ctx, cl->stamps.handle, 0, (uint64_t)(cl->n + 1u) * 16u); 435 uint32_t ok = 0; int32_t missing = -1; 436 for (uint32_t i = 0; i < cl->n; i++) { 437 if (sem[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) ok++; 438 else if (missing < 0) missing = (int32_t)i; 439 } 440 if (st) { st->launches = cl->n; st->submits = submits; st->words = words; st->elapsed_ns = elapsed; st->host_ns = host; st->sems_done = ok; st->first_missing = missing; } 441 if (missing >= 0) { omega_gpu_session_set_error("cmdlist: a launch semaphore is not DONE (launch did not run)"); return -1; } 442 return 0; 443 } 444 445 /* L136: list a on the session queue and list b on extra queue 1, both in flight before either is waited. Each list gets 446 * the same prefix as submit (semaphores INIT, banks / QMDs / semaphores synced) and the same words (emit_chunk); b's 447 * completion markers sit at MARKER_OFF + PAIR_B_MARKER on the same uncached page. */ 448 #define PAIR_B_MARKER 0x80ull 449 static int pair_prep(const OmegaGpuPlatform *p, OmegaGpuCmdList *cl) { 450 uint32_t *sem = cl->sems.cpu; 451 for (uint32_t i = 0; i < cl->n; i++) sem[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; 452 cl->stamped = 0; 453 return p->sync_cpu_to_gpu(p->ctx, cl->banks.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_BANK_BYTES) == OMEGA_GPU_PLAT_OK && 454 p->sync_cpu_to_gpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u) == OMEGA_GPU_PLAT_OK ? 0 : -1; 455 } 456 457 static int pair_check(const OmegaGpuPlatform *p, OmegaGpuCmdList *cl, uint32_t nwords, uint64_t elapsed, uint64_t host, OmegaGpuCmdListStats *st) { 458 (void)p->sync_gpu_to_cpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u); 459 const uint32_t *sem = cl->sems.cpu; 460 uint32_t ok = 0; int32_t missing = -1; 461 for (uint32_t i = 0; i < cl->n; i++) { 462 if (sem[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) ok++; 463 else if (missing < 0) missing = (int32_t)i; 464 } 465 if (st) { st->launches = cl->n; st->submits = 1; st->words = nwords; st->elapsed_ns = elapsed; st->host_ns = host; st->sems_done = ok; st->first_missing = missing; } 466 if (missing >= 0) { omega_gpu_session_set_error("cmdlist pair: a launch semaphore is not DONE (launch did not run)"); return -1; } 467 return 0; 468 } 469 470 /* Uncertain completion of a pair: queue 1 may still run, so a nop on queue 0 proves nothing; latch the session directly. */ 471 static void pair_latch(const char *why) { 472 __atomic_store_n(&g_latched, omega_gpu_session_recoveries() + 1u, __ATOMIC_RELEASE); 473 omega_gpu_session_latch_uncertain(why); 474 } 475 476 int omega_gpu_cmdlist_submit_pair(OmegaGpuCmdList *a, OmegaGpuCmdList *b, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *sa, OmegaGpuCmdListStats *sb) { 477 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); 478 if (opts) o = *opts; 479 if (sa) { memset(sa, 0, sizeof *sa); sa->first_missing = -1; } 480 if (sb) { memset(sb, 0, sizeof *sb); sb->first_missing = -1; } 481 const OmegaGpuPlatform *p = omega_gpu_session_platform(); 482 if (!a || !b || a == b || a->n == 0 || b->n == 0 || !p) { omega_gpu_session_set_error("cmdlist pair: two distinct non-empty lists and a platform"); return -1; } 483 if (stale(a) || stale(b)) return -1; 484 for (int k = 0; k < 2; k++) { /* a wait must point into the other list of this pair, at a launch it still has */ 485 const OmegaGpuCmdList *me = k ? b : a, *peer = k ? a : b; 486 if (me->nwaits && (me->wait_other != peer || me->wait_max >= peer->n)) { 487 omega_gpu_session_set_error("cmdlist pair: a wait_for points outside the other list of this pair"); return -1; 488 } 489 } 490 if (o.mode != OMEGA_GPU_CMDLIST_PCAS || o.stamps || o.skip_launch) { omega_gpu_session_set_error("cmdlist pair: PCAS mode only, no stamps, no skip"); return -1; } 491 if (omega_gpu_cmdlist_is_latched() || omega_gpu_session_is_blocked()) { omega_gpu_session_set_error("cmdlist pair: latched"); return -1; } 492 OmegaGpuSession *s = omega_gpu_session_open(); 493 if (!s) return -1; 494 if (!s->marker.cpu || s->marker.size < MARKER_OFF + PAIR_B_MARKER + 0x20) { omega_gpu_session_set_error("cmdlist pair: session marker page"); return -1; } 495 OmegaGpuQueueHandle q1; 496 if (omega_gpu_session_extra_queue(1, &q1) != 0) return -1; /* names the refusing stage */ 497 const OmegaGpuMem *marker = &s->marker; 498 const uint64_t marker_va = marker->va + MARKER_OFF; 499 const uint64_t timeout = o.timeout_ms ? o.timeout_ms : DEFAULT_TIMEOUT_MS; 500 const uint32_t spin = o.spin_us ? o.spin_us : omega_gpu_session_spin_us(); 501 const uint64_t th0 = p->time_now_ns(p->ctx); 502 /* one submission per list: both must fit their ring with the platform prefix (a q38 token is ~650 launches x 8 words) */ 503 const uint32_t prefix = omega_gpu_session_submit_prefix_words(); 504 const OmegaGpuCmdList *L[2] = { a, b }; 505 for (int k = 0; k < 2; k++) { 506 const uint32_t avail = L[k]->words_cap - 18u - TAIL_WORDS - 2u - STAMP_WORDS; 507 if (prefix > avail || (avail - prefix) / (L[k]->nwaits ? 14u : 8u) < L[k]->n) { omega_gpu_session_set_error("cmdlist pair: a list does not fit one submission"); return -1; } 508 } 509 if (pair_prep(p, a) || pair_prep(p, b)) { omega_gpu_session_set_error("cmdlist pair: sync_cpu_to_gpu"); return -1; } 510 const size_t na = emit_chunk(a, &o, 0, a->n, marker_va), nb = emit_chunk(b, &o, 0, b->n, marker_va + PAIR_B_MARKER); 511 volatile uint32_t *mk = (volatile uint32_t *)((uint8_t *)marker->cpu + MARKER_OFF); 512 mk[0] = 0; mk[4] = 0; mk[PAIR_B_MARKER / 4] = 0; mk[PAIR_B_MARKER / 4 + 4] = 0; 513 if (p->sync_cpu_to_gpu(p->ctx, a->qmds.handle, 0, (uint64_t)a->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK || 514 p->sync_cpu_to_gpu(p->ctx, b->qmds.handle, 0, (uint64_t)b->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK || 515 p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, PAIR_B_MARKER + 0x20) != OMEGA_GPU_PLAT_OK) { 516 omega_gpu_session_set_error("cmdlist pair: sync qmd / marker"); return -1; 517 } 518 const uint64_t t0 = p->time_now_ns(p->ctx); 519 const uint64_t host = t0 - th0; 520 if (p->queue_submit(p->ctx, s->queue, a->words, (uint32_t)na) != OMEGA_GPU_PLAT_OK) { 521 omega_gpu_session_set_error("cmdlist pair: queue_submit (queue 0) refused"); return -1; /* nothing in flight */ 522 } 523 const int b_ok = p->queue_submit(p->ctx, q1, b->words, (uint32_t)nb) == OMEGA_GPU_PLAT_OK; 524 int w = wait_word(p, marker, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin); 525 if (w == WAIT_OK) w = wait_word(p, marker, 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin); 526 if (w == WAIT_OK && b_ok) w = wait_word(p, marker, PAIR_B_MARKER, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin); 527 if (w == WAIT_OK && b_ok) w = wait_word(p, marker, PAIR_B_MARKER + 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin); 528 if (w != WAIT_OK) { pair_latch(w == WAIT_TIMED_OUT ? "cmdlist pair: marker wait timed out (latched)" : "cmdlist pair: marker poll refused (latched)"); return -1; } 529 (void)p->sync_gpu_to_cpu(p->ctx, marker->handle, MARKER_OFF, PAIR_B_MARKER + 0x20); 530 const uint64_t elapsed = p->time_now_ns(p->ctx) - t0; 531 (void)p->queue_retire(p->ctx, s->queue); 532 if (!b_ok) { omega_gpu_session_set_error("cmdlist pair: queue_submit (queue 1) refused"); return -1; } /* a completed and is retired */ 533 (void)p->queue_retire(p->ctx, q1); 534 const int ra = pair_check(p, a, (uint32_t)na, elapsed, host, sa), rb = pair_check(p, b, (uint32_t)nb, elapsed, host, sb); 535 return ra == 0 && rb == 0 ? 0 : -1; 536 } 537 538 int omega_gpu_cmdlist_stamps(const OmegaGpuCmdList *cl, uint64_t *out, uint32_t cap) { 539 if (!cl || !out || !cl->stamps.cpu) { omega_gpu_session_set_error("cmdlist stamps: arguments"); return -1; } 540 if (!cl->stamped) { omega_gpu_session_set_error("cmdlist stamps: the last submit had stamps off"); return -1; } 541 const uint32_t want = cl->n + 1u; 542 if (cap < want) { omega_gpu_session_set_error("cmdlist stamps: out too small"); return -1; } 543 const uint64_t *s = cl->stamps.cpu; 544 for (uint32_t i = 0; i < want; i++) { 545 if (s[2 * i] != (uint64_t)i || s[2 * i + 1] == UINT64_MAX) { omega_gpu_session_set_error("cmdlist stamps: a slot was not written (stamps off, or the launch did not run)"); return -1; } 546 out[i] = s[2 * i + 1]; 547 } 548 return (int)want; 549 } 550 551 const char *omega_gpu_cmdlist_barrier_name(OmegaGpuCmdListBarrier b) { 552 switch (b) { 553 case OMEGA_GPU_CMDLIST_BARRIER_WFI: return "wfi+inval_all"; 554 case OMEGA_GPU_CMDLIST_BARRIER_NONE: return "none"; 555 case OMEGA_GPU_CMDLIST_BARRIER_WFI_ONLY: return "wfi_only"; 556 case OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA: return "wfi+inval_data"; 557 case OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA: return "flush_data"; 558 } 559 return "?"; 560 }