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/OWN30B-REQUAL-MAIN-710d438c-20261015/gates/cut4b/FB1-CUT4B-710d438c/omega_gpu_session.h # 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 / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / cut4b / FB1-CUT4B-710d438c / omega_gpu_session.h` 253 lines ยท 14990 bytes Raw Blame History 1 #ifndef OMEGA_GPU_SESSION_H 2 #define OMEGA_GPU_SESSION_H 3 /* 4 * omega_gpu_session: the ONE persistent device for the native GPU APIs 5 * (FB-1 cut 4b, 2026-10-04). Shared by omega_gpu_matmul_api, 6 * omega_gpu_elementwise_api and omega_gpu_attention_api. 7 * 8 * Before this cut the matmul API kept its own device open for the process while 9 * the elementwise and attention APIs opened a client, device, VA space and 10 * channel for EVERY call and closed them again (~40 ms per call against ~0.1 ms 11 * of chip time; TinyLlama decode makes ~3500 elementwise calls per 32 tokens, 12 * sovereign-core cut 3c: 0.207 tok/s). Now: 13 * - one RM client / device / channel per process, opened on the first launch of 14 * any API and kept until omega_gpu_session_close(); 15 * - one pushbuffer, constant bank, marker page and QMD page, reused per launch; 16 * - staging buffers owned by the callers grow to a high-water mark and are reused 17 * (omega_gpu_session_scratch); 18 * - every buffer is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE, read 19 * 2026-10-04: "For system memory this will not be coherent with direct CPU 20 * mappings"; the default for system memory is uncached), because the host 21 * writes inputs the chip then reads, and polls the markers the chip writes; 22 * - every launch keeps the chip-proven sequence: shader-cache invalidate 23 * (clcec0.h NVCEC0_INVALIDATE_SHADER_CACHES, cut 1b finding), constant bank 24 * upload, QMD0 / QMD1, first marker, L2_FLUSH_DIRTY, second marker on the 25 * uncached page (C3 tail, omega#225/#230), GPFIFO retire after completion. 26 * - an uncertain completion (marker wait timed out) latches the process: nothing 27 * is freed or closed (the chip may still be running) and every later launch 28 * of every API is refused, as the engine does (omega_gpu_engine.h). 29 * 30 * Threading: one process-wide mutex. A caller takes omega_gpu_session_lock(), 31 * does its whole call (kernel lookup, staging, launches, readback) and unlocks. 32 * Calls from several threads therefore serialise on the device; that is the 33 * documented model (one channel, one GPFIFO). 34 * 35 * Diagnostics (cut 4b flake investigation): every failure names its stage 36 * (omega_gpu_session_last_error) and an open failure carries the driver text 37 * (Nvrm.err: RM class, ioctl errno, RM status) so a first-call failure is never 38 * reported as a bare CHIP_FAIL again. 39 * 40 * Portability (native v1 P07 cut C, session part; aienos#286, docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md): 41 * this file and omega_gpu_session.c no longer name a Linux driver type or call one. Everything the session 42 * needs from the machine goes through the installed OmegaGpuPlatform (src/omega_gpu_platform.h, ABI 1.1, 43 * service set OMEGA_GPU_SVC_REQUIRED_SESSION): device open/close, typed DMA buffers with explicit rights, 44 * kind and GPU-uncached policy, the submission queue, POLLING completion (poll_word; the session never 45 * claims or waits on an interrupt), explicit CPU<->GPU sync, the nanosecond clock, the session lock and 46 * CPU-only host memory. omega_gpu_session_set_platform installs one; the hosted build installs the Linux 47 * reference (src/omega_gpu_platform_linux.c, reference only) on first use when none was installed; the 48 * freestanding build (OMEGA_GPU_NO_POSIX) has no default and refuses until a platform is installed. 49 * Buffers are OmegaGpuMem: a typed handle (index + generation) plus the CPU view and the GPU VA as two 50 * distinct fields; no bus or physical address reaches the APIs. A handle from a closed device is stale and 51 * every use of it is refused. 52 * 53 * Uncertain completion parks, it never releases: after a marker poll timed out or a launch was cancelled 54 * after submit, the device may still be reading or writing any session buffer, so every buffer the session 55 * or an API frees from then on is PARKED (kept allocated, counted in omega_gpu_session_parked) and never 56 * returned to the platform; close and reopen are refused; the latch holds for the life of the process. 57 */ 58 #include 59 #include 60 #include 61 #include "omega_gpu_platform.h" 62 63 #ifdef __cplusplus 64 extern "C" { 65 #endif 66 67 /* A device buffer: typed handle, kind, CPU view (cpu, size bytes) and GPU VA (va). cpu NULL = none. */ 68 typedef struct { 69 OmegaGpuBufferHandle handle; 70 uint32_t kind; /* OMEGA_GPU_HANDLE_BUF_DATA, _BUF_CODE or _BUF_QUEUE */ 71 void *cpu; /* CPU view (OmegaCpuView.ptr) */ 72 uint64_t va; /* GPU virtual address (OmegaGpuVa.va) */ 73 uint64_t size; /* bytes of both views */ 74 } OmegaGpuMem; 75 76 typedef struct { 77 OmegaGpuDeviceHandle dev; 78 OmegaGpuQueueHandle queue; 79 OmegaGpuMem pb, cbank, marker, qmd; /* per-launch scratch, owned by the session (pb is the queue ring) */ 80 uint32_t opens; /* device opens in this process */ 81 uint64_t last_open_ns; /* wall time of the last successful open */ 82 } OmegaGpuSession; 83 84 /* A caller-owned staging buffer that grows to its high-water mark. */ 85 typedef struct { 86 OmegaGpuMem mem; 87 size_t cap; /* usable bytes */ 88 size_t high_water; /* largest request so far */ 89 bool reserved; /* serving reservation active: a request past cap is refused, never allocated (omega_gpu_session_scratch_reserve) */ 90 } OmegaGpuScratch; 91 92 /* One launch: a kernel already resident at code_va, the argument words for 93 * constant bank 0 at 0x380 (buffer addresses then parameters), the grid. */ 94 typedef struct { 95 uint64_t code_va; 96 uint32_t gpr_count; /* registers the QMD declares (multiple of 8, >= 64 in practice) */ 97 uint32_t threads_x, threads_y; 98 uint32_t grid_x, grid_y; 99 size_t num_elements; /* QMD config num_elements (kept per API for identical words) */ 100 const uint32_t *args; /* n_args words written at cbank + 0x380 */ 101 uint32_t n_args; /* <= OMEGA_GPU_SESSION_MAX_ARGS */ 102 uint64_t timeout_ms; /* marker poll; past it the completion is uncertain and the process latches */ 103 uint32_t shared_bytes; /* shared memory per CTA; 0 = the QMD default (1024) */ 104 uint32_t spin_us; /* poll the marker without sleeping for up to this long, then the platform's 105 poll_word (Linux reference: 50 us sleep-poll) until timeout_ms; 0 = poll_word only */ 106 } OmegaGpuLaunch; 107 #define OMEGA_GPU_SESSION_MAX_SPIN_US 1000000u 108 #define OMEGA_GPU_SESSION_MAX_ARGS 32u 109 /* poll_word is called in slices of at most this many ms so a cancel request is seen within one slice */ 110 #define OMEGA_GPU_SESSION_POLL_SLICE_MS 1u 111 112 /* ---- platform (ABI 1.1) ---- */ 113 114 /* Installs the platform the session runs on. Negotiates caps against OMEGA_GPU_SVC_REQUIRED_SESSION and checks the 115 * table (omega_gpu_platform_check_for). OMEGA_GPU_PLAT_OK, or the refusal; REFUSED_BUSY while the device is open, 116 * the lock is held or the process is latched. NULL p uninstalls (refused the same way). Install once, at start. */ 117 int omega_gpu_session_set_platform(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps); 118 /* The installed platform (the hosted build installs the Linux reference here when none was installed); NULL in the 119 * freestanding build until omega_gpu_session_set_platform. */ 120 const OmegaGpuPlatform *omega_gpu_session_platform(void); 121 122 /* Completion is observed by polling a GPU-uncached word; the session never claims interrupt support. */ 123 typedef enum { OMEGA_GPU_COMPLETION_POLLING = 1 } OmegaGpuCompletionMode; 124 OmegaGpuCompletionMode omega_gpu_session_completion_mode(void); 125 126 /* Platform clock in ns (CLOCK_NS); 0 without a platform. */ 127 uint64_t omega_gpu_session_now_ns(void); 128 129 /* CPU-only memory for API tables and staging, from the platform HOST_MEM service (Linux reference: malloc/free). 130 * NULL without a platform or on refusal; 0 bytes gives a unique 1-byte block (malloc(0) behaviour); calloc also 131 * refuses n * size overflow. free(NULL) is a no-op. */ 132 void *omega_gpu_host_alloc(size_t bytes); 133 void *omega_gpu_host_calloc(size_t n, size_t size); 134 void omega_gpu_host_free(void *p); 135 136 /* ---- lock, open, state ---- */ 137 138 /* Take the session lock (platform LOCK service, lock id OMEGA_GPU_SESSION_LOCK_ID). Does not touch the device. 139 * Without a platform it is a no-op and the open that follows refuses. */ 140 #define OMEGA_GPU_SESSION_LOCK_ID 0u 141 void omega_gpu_session_lock(void); 142 void omega_gpu_session_unlock(void); 143 144 /* Caller holds the lock. Opens the device on first use. NULL when the process is 145 * latched, no platform is installed or the open failed (stage in omega_gpu_session_last_error). */ 146 OmegaGpuSession *omega_gpu_session_open(void); 147 bool omega_gpu_session_is_open(void); 148 149 /* 1 once an uncertain completion has latched this process. */ 150 int omega_gpu_session_is_blocked(void); 151 152 /* Process-wide marker-poll spin window, used by every launch whose own spin_us is 0 153 * (the matmul, elementwise and attention APIs leave it 0). 0 (the default) keeps the 154 * poll_word sleep-poll (Linux reference: 50 us, which Linux timer slack rounds to about 102 us 155 * per launch on GB10, omega#328). 0 on success, -1 above OMEGA_GPU_SESSION_MAX_SPIN_US (setting unchanged). */ 156 int omega_gpu_session_set_spin_us(uint32_t us); 157 uint32_t omega_gpu_session_spin_us(void); 158 159 /* ---- buffers ---- */ 160 161 /* Caller holds the lock, device open. GPU-uncached DATA buffer (CPU and GPU read/write), page rounded. */ 162 int omega_gpu_session_alloc(size_t bytes, OmegaGpuMem *out); 163 /* Caller holds the lock, device open. A CODE buffer (CPU writes, GPU executes, GPU never writes) for code_size 164 * bytes with the 2 KB instruction prefetch tail (omega_gpu_code_alloc.h), the tail zero-filled; the caller writes 165 * the code to out->cpu. */ 166 int omega_gpu_session_alloc_code(size_t code_size, OmegaGpuMem *out); 167 /* Releases m to the platform and zeroes *m. Latched: m is PARKED instead (never released). A handle from a closed 168 * device (stale) or a closed session is refused: nothing is released, the refusal is counted 169 * (OmegaGpuAllocStats.stale_refusals) and *m is zeroed. */ 170 void omega_gpu_session_free(OmegaGpuMem *m); 171 172 /* Explicit visibility of a whole buffer (platform SYNC): CPU writes before the device reads them, device writes 173 * before the CPU reads them (Linux reference: dsb sy, the barrier the APIs issued themselves before cut C). 174 * 0 also for a buffer with no CPU view (nothing to sync); -1 with the stage named when refused or no device. */ 175 int omega_gpu_session_sync_to_gpu(const OmegaGpuMem *m); 176 int omega_gpu_session_sync_to_cpu(const OmegaGpuMem *m); 177 178 /* Caller holds the lock, device open. Makes s->mem at least `bytes` long 179 * (reallocated when it grows; contents are not kept). */ 180 int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes); 181 /* Caller holds the lock, device open. Allocates s->mem for `bytes` now and marks it reserved: from then on 182 * omega_gpu_session_scratch(s, n) with n <= the reserved capacity never touches the driver, and with n past it 183 * returns OMEGA_GPU_SESSION_SCRATCH_RESERVED (-2) with a clear error and no allocation. 0 on success, -1 on 184 * allocation failure (not reserved then). Cleared by omega_gpu_session_scratch_free (device close) or _unreserve. */ 185 #define OMEGA_GPU_SESSION_SCRATCH_RESERVED (-2) 186 int omega_gpu_session_scratch_reserve(OmegaGpuScratch *s, size_t bytes); 187 void omega_gpu_session_scratch_unreserve(OmegaGpuScratch *s); 188 void omega_gpu_session_scratch_free(OmegaGpuScratch *s); 189 190 /* ---- launch, cancel ---- */ 191 192 /* Caller holds the lock, device open, not latched. Submits one launch and polls 193 * for both markers. 0 on success; -1 with the stage named on failure. A marker 194 * timeout latches the process (uncertain completion). elapsed_ns (submit to second 195 * marker) and the observed first marker word are added / stored when the pointers are given. 196 * OMEGA_GPU_SESSION_CANCELLED: a cancel request was seen. Before submit nothing was submitted and the 197 * session stays usable; after submit the completion is uncertain and the process latches. */ 198 #define OMEGA_GPU_SESSION_CANCELLED (-3) 199 int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out); 200 201 /* Asks the launch in progress (or the next one) to stop. Any thread, no lock. Polling cannot stop the device: a 202 * launch already submitted is not cancelled on the chip, its completion becomes uncertain (latch + park). The 203 * request is consumed by the launch that sees it. */ 204 void omega_gpu_session_cancel(void); 205 206 /* Stage of the most recent failure ("" if none). Shared by every API. */ 207 const char *omega_gpu_session_last_error(void); 208 void omega_gpu_session_set_error(const char *stage); 209 210 /* Caller holds the lock. Register a function the session calls (lock held, device 211 * still open) right before it closes, so an API can drop its resident kernels and 212 * scratch. Up to 8; the same function is registered once. (Taking the lock inside 213 * deadlocked the first call of every API on 2026-10-04: the APIs register from 214 * their open path, which already holds it.) */ 215 int omega_gpu_session_on_close(void (*hook)(void)); 216 217 /* Close the device and free every session buffer. Takes the lock itself. 218 * Refused (no-op) after an uncertain completion. The next open reopens; every handle of the 219 * closed device is stale from then on. */ 220 void omega_gpu_session_close(void); 221 222 /* Device opens so far in this process (diagnostics; a persistent session shows 1). */ 223 uint32_t omega_gpu_session_open_count(void); 224 225 /* Driver allocations made through omega_gpu_session_alloc (every GPU API buffer, tensor, scratch growth and kernel 226 * code buffer) since the process started, for measurement: a serving daemon reads it after warm-up and per request 227 * to show that serving asks the driver for nothing. Covers this session layer only, not other engines. */ 228 typedef struct { 229 uint64_t allocs; /* successful driver allocations */ 230 uint64_t alloc_bytes; /* their page-rounded bytes */ 231 uint64_t alloc_failures; /* driver refusals (e.g. NV_ERR_NO_MEMORY) */ 232 uint64_t frees; /* driver frees */ 233 uint64_t stale_refusals; /* frees refused because the handle was stale (closed device or double free) */ 234 } OmegaGpuAllocStats; 235 void omega_gpu_session_alloc_stats(OmegaGpuAllocStats *out); 236 237 /* Buffers parked after an uncertain completion: kept allocated, never returned to the platform. The session's own 238 * launch buffers (ring, constant bank, marker, QMD page) are parked at the latch; every later free parks too. 239 * The first OMEGA_GPU_SESSION_PARK_MAX are listed by handle; past that they are still kept, counted in untracked. */ 240 #define OMEGA_GPU_SESSION_PARK_MAX 512u 241 typedef struct { 242 uint32_t buffers; /* parked buffers (listed + untracked) */ 243 uint32_t untracked; /* parked past the list (still never released) */ 244 uint64_t bytes; /* their bytes */ 245 } OmegaGpuParkStats; 246 void omega_gpu_session_parked(OmegaGpuParkStats *out); 247 /* 1 if this handle is parked (listed). */ 248 int omega_gpu_session_is_parked(OmegaGpuBufferHandle h); 249 250 #ifdef __cplusplus 251 } 252 #endif 253 #endif