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