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_platform_linux.c # 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_platform_linux.c` 798 lines · 38420 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_gpu_platform_linux.c -- Linux REFERENCE implementation of OmegaGpuPlatform 1.1. REFERENCE ONLY: see the 3 * header. Every driver call here is the one src/omega_gpu_session.c made directly before P07 cut C, in the same 4 * order, so the launch path on GB10 under Linux is unchanged: 5 * device_open = nvrm_open + m16_native_create_channel (session open, before the four session buffers) 6 * dma_alloc = nvrm_alloc_gpu_uncached (every session / API buffer) 7 * dma_free = nvrm_free 8 * queue_open = ctx.pb_mem = the ring buffer (m16_native_enqueue_methods copies methods there) 9 * queue_submit = m16_native_submit_methods (refused BUSY while the last one is unretired) 10 * queue_retire = nvrm_retire(&rm, rm.put) (after both markers landed; no-op when nothing is in flight) 11 * poll_word = omega plat_wait_ge on a mapbind alias of the shared mapping (50 us slots) 12 * sync_* = dsb sy (the barrier the session issued itself) 13 * device_close = m16_native_close (nvrm_close) 14 * Every submission copies its methods to offset 0 of the one ring buffer, and Blackwell USERD has no GPGet the 15 * client could read (nvrm/nvrm.c, nvrm_submit), so nothing tells the reference which entry the GPU has fetched. 16 * caps.max_inflight is therefore 1 and enforced: a submit while the previous one is unretired is refused 17 * REFUSED_BUSY before the ring is written or the doorbell rung (CudaAuditFable REPORT.md Q6), and one retire after 18 * the caller observed completion covers everything submitted. 19 */ 20 #include "omega_gpu_platform_linux.h" 21 #include "cpu_barrier.h" 22 #include "cudapp_native.h" 23 24 #include "m16_native.h" 25 #include "nvrm.h" 26 #include "omega_ampere_qmd.h" 27 #include "omega_gpu_mem_reserve.h" 28 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #if defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) 39 #include "omega_gpu_platform_linux_cpolicy.h" 40 #else 41 uint32_t omega_plat_policy(uint64_t *result, const void *p0, uint64_t p0_n, void *p1, uint64_t p1_n); 42 #endif 43 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) 44 #include "cudapp_plat_effects.h" 45 #endif 46 47 #ifndef CUDAPP_MIGRATION_PENDING_C_PLATFORM_REST 48 #define CUDAPP_MIGRATION_PENDING_C_PLATFORM_REST 0 49 #endif 50 #if CUDAPP_MIGRATION_PENDING_C_PLATFORM_REST 51 #include "omega_gpu_platform_linux_rest_oracle.h" 52 #else 53 uint32_t omega_plat_rest(uint64_t *, uint64_t, const void *, uint64_t, void *, uint64_t); 54 #endif 55 static uint64_t plat_rest(uint64_t mode, const uint64_t m[16], uint64_t *out) { 56 uint64_t q[16] = {0}, result; 57 #if CUDAPP_MIGRATION_PENDING_C_PLATFORM_REST 58 result = plat_rest_c(mode, m, q); 59 #else 60 if (omega_plat_rest(&result, mode, m, 16, q, 16) != 0) abort(); 61 #endif 62 if (out) memcpy(out, q, sizeof q); 63 return result; 64 } 65 66 static uint64_t plat_scalar(uint64_t mode, uint64_t a, uint64_t b) { 67 const uint64_t m[16] = {a,b}; 68 return plat_rest(mode, m, NULL); 69 } 70 71 /* CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX defaults OFF. The .om unit is the path; C is the oracle. */ 72 static uint64_t plat_decide(const uint64_t m_in[16], uint64_t q_out[16]) { 73 uint64_t q[16]; 74 memset(q, 0, sizeof q); 75 #if defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) 76 uint64_t result = plat_c(m_in, q); 77 #else 78 uint64_t m[16], result = 0; 79 memcpy(m, m_in, sizeof m); 80 if (omega_plat_policy(&result, m, 16, q, 16) != 0) return 0; 81 #endif 82 if (q_out) memcpy(q_out, q, sizeof q); 83 return result; 84 } 85 86 static void plat_l2_words(uint32_t out[5]) { 87 #if defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) 88 const uint32_t words[5] = { (2u << 28) | (4u << 16) | (0x0028u >> 2), 0, 0, 0, 0x11u << 27 }; 89 memcpy(out, words, sizeof words); 90 #else 91 uint64_t m[16], q[16]; 92 memset(m, 0, sizeof m); 93 m[0] = 1; 94 (void)plat_decide(m, q); 95 for (int i = 0; i < 5; i++) out[i] = (uint32_t)q[i]; 96 #endif 97 } 98 99 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 100 #include 101 102 #include 103 uint32_t plat_wt_context_create(uint64_t capacity, omega_native_context **out); 104 uint32_t plat_wt_context_destroy(omega_native_context *context); 105 uint32_t omega2_ctx_plat_wait_ge(omega_native_context *context, uint64_t *result, uint64_t o_handle, uint64_t o_off, uint64_t o_want, uint64_t o_polls); 106 uint32_t omega2_ctx_plat_wait_budget(omega_native_context *context, uint64_t *result, uint64_t o_ms); 107 uint32_t omega2_ctx_plat_wait_hold(omega_native_context *context, uint64_t *result, uint64_t o_addr, uint64_t o_n); 108 uint32_t omega2_ctx_plat_wait_bind(omega_native_context *context, uint64_t *result, uint64_t o_handle, uint64_t o_fd); 109 uint32_t omega2_ctx_plat_wait_drop(omega_native_context *context, uint64_t *result, uint64_t o_handle); 110 static omega_native_context *plat_wait_ctx; 111 static uint64_t plat_alias_cursor = 268435456ull; /* WolframLanguageEvaluator: 256*1024*1024 -> `268435456` */ 112 static __thread int plat_capture_mmap; 113 static __thread int plat_cap_fd = -1; 114 115 static int plat_fd_is_memfd(int fd) { 116 char path[64], link[64] = {0}; 117 snprintf(path, sizeof path, "/proc/self/fd/%d", fd); 118 ssize_t n = readlink(path, link, sizeof link - 1); 119 uint64_t m[16] = {(uint64_t)n}; 120 for (unsigned i = 0; i < 7; ++i) m[i+1] = (unsigned char)link[i]; 121 return (int)plat_rest(1, m, NULL); 122 } 123 void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) { 124 void *p = (void *)syscall(SYS_mmap, addr, len, prot, flags, fd, off); 125 const uint64_t capture[16] = {plat_capture_mmap, p != MAP_FAILED, (uint64_t)(int64_t)fd, 126 (uint64_t)flags, MAP_SHARED, MAP_ANONYMOUS}; 127 if (plat_rest(12, capture, NULL) && plat_fd_is_memfd(fd)) { 128 int d = dup(fd); 129 if (d >= 0) { 130 if (plat_cap_fd >= 0) close(plat_cap_fd); 131 plat_cap_fd = d; 132 } 133 } 134 return p; 135 } 136 static int plat_wait_ready(void) { 137 if (plat_wait_ctx) return 0; 138 return plat_wt_context_create(8, &plat_wait_ctx) == 0 && plat_wait_ctx ? 0 : -1; 139 140 } 141 142 #endif 143 144 #define LINUX_REF_BUFFERS (OMEGA_GPU_HANDLE_SLOTS - 2u) /* one slot each for the device and the queue */ 145 146 typedef struct { 147 pthread_mutex_t mu; /* table, device and diagnostics */ 148 pthread_mutex_t locks[OMEGA_GPU_PLATFORM_LINUX_LOCKS]; 149 int ready; 150 OmegaGpuHandleTable t; 151 NvrmMem mem[OMEGA_GPU_HANDLE_SLOTS]; 152 uint32_t rights[OMEGA_GPU_HANDLE_SLOTS]; 153 uint8_t gpu_cached[OMEGA_GPU_HANDLE_SLOTS]; /* allocated GPU-cached (nvrm_alloc), omega #457 */ 154 int l2_inval_pending; /* a host sync into a GPU-cached buffer since the last submission, omega #457 */ 155 uint32_t inflight; /* submissions not yet retired: 0 or 1 (caps.max_inflight) */ 156 M16NativeContext m16; 157 OmegaGpuDeviceHandle dev; /* generation 0 = closed */ 158 OmegaGpuQueueHandle queue; 159 /* L136: extra queues on extra channels of the same channel group (nvrm_channel_n), opened only when 160 * AIEN_OMEGA_GPU_CHANNELS > 1 at device_open; queue 0 above is unchanged. */ 161 uint32_t nchan; 162 OmegaGpuQueueHandle xq[NVRM_MAX_EXTRA_CHANNELS]; 163 NvrmMem xring[NVRM_MAX_EXTRA_CHANNELS]; 164 uint8_t x_l2_pending[NVRM_MAX_EXTRA_CHANNELS]; 165 uint32_t xinflight[NVRM_MAX_EXTRA_CHANNELS]; /* per extra queue, as inflight above (caps.max_inflight is per queue) */ 166 uint64_t wait_token[OMEGA_GPU_HANDLE_SLOTS]; /* mapbind alias; 0 = not bound */ 167 int wait_fd[OMEGA_GPU_HANDLE_SLOTS]; /* dup of the shared mapping; 0 = none */ 168 char diag[320]; 169 } LinuxRef; 170 171 static LinuxRef g_ref = { 172 .mu = PTHREAD_MUTEX_INITIALIZER, 173 .locks = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER}, 174 }; 175 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 176 static void plat_drop_token(LinuxRef *r, uint32_t idx) { 177 if (r->wait_token[idx] != 0 && plat_wait_ctx) { 178 uint64_t result = 0; 179 (void)omega2_ctx_plat_wait_drop(plat_wait_ctx, &result, r->wait_token[idx]); 180 } 181 if (r->wait_fd[idx] > 0) close(r->wait_fd[idx]); 182 r->wait_token[idx] = 0; 183 r->wait_fd[idx] = 0; 184 } 185 static int plat_bind_fd(LinuxRef *r, uint32_t idx, int fd, uint64_t bytes) { 186 uint64_t held = 0, token = 0; 187 if (plat_wait_ready() != 0) return -1; 188 for (int attempt = 0; attempt < 8; attempt++) { 189 const uint64_t m[16] = {bytes, plat_alias_cursor}; 190 uint64_t q[16]; 191 if (!plat_rest(0, m, q)) return -1; 192 const uint64_t pages = q[0], addr = q[1]; 193 plat_alias_cursor = q[2]; 194 if (omega2_ctx_plat_wait_hold(plat_wait_ctx, &held, addr, pages) != 0 || (int64_t)held < 0) continue; 195 if (omega2_ctx_plat_wait_bind(plat_wait_ctx, &token, held, (uint64_t)fd) != 0 || (int64_t)token < 0) { 196 uint64_t drop = 0; 197 (void)omega2_ctx_plat_wait_drop(plat_wait_ctx, &drop, held); 198 continue; 199 } 200 r->wait_token[idx] = held; 201 r->wait_fd[idx] = fd; 202 return 0; 203 } 204 return -1; 205 } 206 #endif 207 208 /* omega #457: MEM_OP_A..D = 0, 0, 0, OPERATION L2_SYSMEM_NCOH_INVALIDATE 0x11 in D bits 31:27 (clc96f.h:30-73), the 209 * same method form as the C3 tail's L2_FLUSH_DIRTY. ref_queue_submit puts it ahead of the next submission after a host 210 * sync into a GPU-cached buffer; g_caps.submit_prefix_words is its size, so callers budget exactly what is written. */ 211 static const uint32_t L2_INVAL_PREFIX[] = { (2u << 28) | (4u << 16) | (0x0028u >> 2), 0, 0, 0, 0x11u << 27 }; 212 #define L2_INVAL_PREFIX_WORDS ((uint32_t)(sizeof L2_INVAL_PREFIX / sizeof L2_INVAL_PREFIX[0])) 213 214 static const OmegaGpuPlatformCaps g_caps = { 215 .submit_prefix_words = L2_INVAL_PREFIX_WORDS, 216 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, 217 .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, 218 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, /* no MMIO (RM owns the registers), no IRQ (polling only) */ 219 .max_devices = 1, 220 .max_buffers = LINUX_REF_BUFFERS, 221 .max_queues = 1u + NVRM_MAX_EXTRA_CHANNELS, /* queues past the first need AIEN_OMEGA_GPU_CHANNELS (L136) */ 222 .max_inflight = 1, 223 .max_buffer_bytes = 1ull << 37, /* 128 GiB: the whole unified pool */ 224 .max_total_bytes = 1ull << 37, 225 }; 226 227 static void ref_lock(LinuxRef *r) { 228 pthread_mutex_lock(&r->mu); 229 if (!r->ready) { omega_gpu_handles_init(&r->t); r->ready = 1; } 230 } 231 static void ref_unlock(LinuxRef *r) { pthread_mutex_unlock(&r->mu); } 232 233 /* Records why the call failed; REFUSED_DEVICE carries the driver text (Nvrm.err). Called with r->mu held. */ 234 static int refuse(LinuxRef *r, int rc, const char *stage) { 235 if (rc == OMEGA_GPU_PLAT_REFUSED_DEVICE) 236 snprintf(r->diag, sizeof r->diag, "%s: %s", stage, r->m16.rm.err[0] ? r->m16.rm.err : "(no driver text)"); 237 else 238 snprintf(r->diag, sizeof r->diag, "%s: %s", stage, omega_gpu_platform_refusal_name(rc)); 239 return rc; 240 } 241 242 static int dev_ok(LinuxRef *r, OmegaGpuDeviceHandle d) { 243 return omega_gpu_handles_check(&r->t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 244 } 245 246 /* A live buffer handle of any buffer kind; *kind gets it. */ 247 static int buf_ok(LinuxRef *r, OmegaGpuBufferHandle b, uint32_t *kind) { 248 uint64_t m[16] = {b.index, b.generation, 0, 0, 0, OMEGA_GPU_HANDLE_SLOTS}; 249 /* Only this transport bound guards a foreign C array; policy owns the verdict. */ 250 if (b.index < OMEGA_GPU_HANDLE_SLOTS) { 251 m[2] = r->t.live[b.index]; m[3] = r->t.generation[b.index]; m[4] = r->t.kind[b.index]; 252 } 253 int rc = (int)plat_rest(2, m, NULL); 254 if (rc == OMEGA_GPU_PLAT_OK && kind) *kind = (uint32_t)m[4]; 255 return rc; 256 } 257 258 static int range_ok(LinuxRef *r, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { 259 const uint64_t m[16] = {3, r->mem[b.index].size, off, len}; 260 return (int)plat_decide(m, NULL); 261 } 262 263 static uint64_t ref_now_ns(void *ctx) { 264 (void)ctx; 265 struct timespec t; 266 clock_gettime(CLOCK_MONOTONIC, &t); 267 return plat_scalar(3, (uint64_t)t.tv_sec, (uint64_t)t.tv_nsec); 268 } 269 static uint64_t ref_now_ms(void *ctx) { return plat_scalar(9, ref_now_ns(ctx), 0); } 270 271 static int ref_wait_ms(void *ctx, uint64_t ms) { 272 (void)ctx; 273 const uint64_t m[16] = {ms}; 274 uint64_t q[16]; 275 (void)plat_rest(4, m, q); 276 struct timespec t = {.tv_sec = (time_t)q[0], .tv_nsec = (long)q[1]}; 277 while (nanosleep(&t, &t) != 0) {} 278 return OMEGA_GPU_PLAT_OK; 279 } 280 281 static int ref_lock_id(void *ctx, uint32_t id) { 282 LinuxRef *r = ctx; 283 if (plat_scalar(5, id, OMEGA_GPU_PLATFORM_LINUX_LOCKS)) return OMEGA_GPU_PLAT_REFUSED_ARGS; 284 return pthread_mutex_lock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_BUSY; 285 } 286 static int ref_unlock_id(void *ctx, uint32_t id) { 287 LinuxRef *r = ctx; 288 if (plat_scalar(5, id, OMEGA_GPU_PLATFORM_LINUX_LOCKS)) return OMEGA_GPU_PLAT_REFUSED_ARGS; 289 return pthread_mutex_unlock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_ARGS; 290 } 291 292 /* AIEN_OMEGA_GPU_CHANNELS: one digit 2..1+NVRM_MAX_EXTRA_CHANNELS, else 1 (CUDApp#115: the rule is seeded Omega, 293 * om/streams/streams.om op 4; the C rule stays as the CUDAPP_MIGRATION_PENDING_C_STREAMS / _STREAMS_DIFF oracle). */ 294 #if defined(CUDAPP_MIGRATION_PENDING_C_STREAMS) || defined(CUDAPP_MIGRATION_PENDING_STREAMS_DIFF) 295 static uint32_t channels_c(const char *e) { 296 if (!e || e[0] == '\0' || e[1] != '\0' || e[0] < '2' || e[0] > (char)('1' + NVRM_MAX_EXTRA_CHANNELS)) return 1u; 297 return (uint32_t)(e[0] - '0'); 298 } 299 #endif 300 uint32_t omega_gpu_platform_linux_channels_env(void) { 301 const char *e = getenv("AIEN_OMEGA_GPU_CHANNELS"); 302 #ifdef CUDAPP_MIGRATION_PENDING_C_STREAMS 303 return channels_c(e); 304 #else 305 uint64_t in[16] = { e != NULL, e ? strlen(e) : 0u, e ? (uint64_t)(unsigned char)e[0] : 0u, NVRM_MAX_EXTRA_CHANNELS }; 306 uint64_t out[32], r = 0; 307 if (omega_streams_words(&r, 4u, in, 16u, out, 32u) != 0 || r < 1u || r > 1u + NVRM_MAX_EXTRA_CHANNELS) r = 1u; 308 #ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF 309 if (r != channels_c(e)) { fprintf(stderr, "streams diff: AIEN_OMEGA_GPU_CHANNELS Omega %llu C %u\n", (unsigned long long)r, channels_c(e)); abort(); } 310 #endif 311 return (uint32_t)r; 312 #endif 313 } 314 315 /* Extra queue slot of q (0..nchan-2), or -1 when q is not an open extra queue. Caller holds r->mu. */ 316 static int xq_slot(const LinuxRef *r, OmegaGpuQueueHandle q) { 317 uint64_t m[16] = {12, r->nchan, q.index, q.generation}; 318 for (uint32_t i = 0; i < NVRM_MAX_EXTRA_CHANNELS; i++) { 319 m[4 + i * 2] = r->xq[i].index; 320 m[5 + i * 2] = r->xq[i].generation; 321 } 322 uint64_t slot = plat_decide(m, NULL); 323 return slot ? (int)slot - 1 : -1; 324 } 325 326 static int ref_device_open(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *out) { 327 LinuxRef *r = ctx; 328 int rc; 329 if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS; 330 memset(out, 0, sizeof *out); 331 ref_lock(r); 332 rc = (int)plat_scalar(7, ordinal, r->dev.generation); 333 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, ordinal ? "device_open" : "device_open (already open)"); goto out; } 334 memset(&r->m16, 0, sizeof r->m16); 335 /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on failure) so the driver's own 336 * failure text survives into the stage name, as the session did before cut C. */ 337 if (nvrm_open(&r->m16.rm) != 0) { 338 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "nvrm_open"); 339 (void)nvrm_close(&r->m16.rm); 340 goto out; 341 } 342 r->nchan = omega_gpu_platform_linux_channels_env(); 343 if (r->nchan == 1u ? m16_native_create_channel(&r->m16) != 0 : nvrm_channel_n(&r->m16.rm, r->nchan - 1u) != 0) { 344 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, r->nchan == 1u ? "m16_native_create_channel" : "nvrm_channel_n"); 345 (void)m16_native_close(&r->m16); 346 goto out; 347 } 348 rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); 349 if (rc != OMEGA_GPU_PLAT_OK) { (void)m16_native_close(&r->m16); rc = refuse(r, rc, "device_open"); goto out; } 350 r->dev = *out; 351 r->diag[0] = '\0'; 352 out: 353 ref_unlock(r); 354 return rc; 355 } 356 357 static int ref_device_close(void *ctx, OmegaGpuDeviceHandle dev) { 358 LinuxRef *r = ctx; 359 ref_lock(r); 360 int rc = dev_ok(r, dev); 361 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "device_close"); ref_unlock(r); return rc; } 362 /* Every handle of the device goes stale. The RM client close below releases the driver objects; buffers the 363 * caller did not free are not unmapped here, exactly as before cut C. */ 364 for (uint32_t i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) { 365 if (!r->t.live[i] || i == dev.index) continue; 366 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 367 plat_drop_token(r, i); 368 #endif 369 (void)omega_gpu_handles_close(&r->t, i, r->t.generation[i], r->t.kind[i]); 370 memset(&r->mem[i], 0, sizeof r->mem[i]); 371 r->rights[i] = 0; 372 r->gpu_cached[i] = 0; 373 } 374 (void)omega_gpu_handles_close(&r->t, dev.index, dev.generation, OMEGA_GPU_HANDLE_DEVICE); 375 memset(&r->dev, 0, sizeof r->dev); 376 r->l2_inval_pending = 0; /* omega #457: nothing pending survives the device */ 377 r->inflight = 0; 378 memset(&r->queue, 0, sizeof r->queue); 379 memset(r->xq, 0, sizeof r->xq); memset(r->xring, 0, sizeof r->xring); memset(r->x_l2_pending, 0, sizeof r->x_l2_pending); 380 memset(r->xinflight, 0, sizeof r->xinflight); 381 r->nchan = 0; 382 /* C5: the driver's close result is the proof that this process's channels (and any grid still on them) are gone. 383 * The state above is closed either way; a teardown fault is reported so omega_gpu_session_recover stays latched. */ 384 rc = m16_native_close(&r->m16) == 0 ? OMEGA_GPU_PLAT_OK : refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "device_close (driver teardown fault)"); 385 ref_unlock(r); 386 return rc; 387 } 388 389 static const char *ref_diag_text(void *ctx) { return ((LinuxRef *)ctx)->diag; } 390 391 /* Developer bar D1 (docs/PAIN_POINTS.md). Shared host memory goes through src/omega_gpu_mem_reserve.c, shared with the 392 * aien-gpu platform: it runs under ref_lock, and every meminfo open there is O_NOFOLLOW|O_NONBLOCK and must reach a 393 * regular file, so it cannot block on a FIFO or act on a device. Local VRAM on a discrete GPU is checked here against 394 * the selected framebuffer's free memory, with CUDAPP_GPU_MIN_FREE_BYTES (bytes) replacing the reserve when set. */ 395 static int mem_reserve_check(LinuxRef *r, uint64_t bytes, int local) { 396 if (!local) return omega_gpu_mem_reserve_check(bytes, r->diag, sizeof r->diag); 397 uint64_t reserve = omega_gpu_mem_reserve_bytes(); 398 const char *minimum = getenv("CUDAPP_GPU_MIN_FREE_BYTES"); 399 if (minimum && *minimum) { 400 uint64_t pm[16] = {7}; 401 size_t n = strlen(minimum); 402 pm[1] = n > 32 ? 33 : n; 403 if (n <= 32) memcpy(&pm[2], minimum, n); 404 uint64_t pq[16]; 405 if (n > 32 || plat_decide(pm, pq) != 1) { 406 snprintf(r->diag, sizeof r->diag, "dma_alloc: invalid CUDAPP_GPU_MIN_FREE_BYTES"); 407 return OMEGA_GPU_PLAT_REFUSED_LIMIT; 408 } 409 reserve = pq[0]; 410 } 411 if (!reserve) return OMEGA_GPU_PLAT_OK; 412 uint64_t avail = 0, total = 0; 413 uint64_t page_m[16] = {5, bytes}; 414 uint64_t page_q[16]; 415 if (plat_decide(page_m, page_q) != 1 || 416 nvrm_vram_info(&r->m16.rm, &avail, &total) != 0) { 417 snprintf(r->diag, sizeof r->diag, "dma_alloc: refused: selected GPU framebuffer free-memory query failed"); 418 return OMEGA_GPU_PLAT_REFUSED_LIMIT; 419 } 420 bytes = page_q[0]; 421 const uint64_t m[16] = {2, avail, bytes, reserve}; 422 if (omega_ampere_policy(m, NULL) == 0) return OMEGA_GPU_PLAT_OK; 423 snprintf(r->diag, sizeof r->diag, 424 "dma_alloc: refused local VRAM allocation: free=%llu bytes request=%llu bytes reserve=%llu bytes; host RAM is not VRAM", 425 (unsigned long long)avail, (unsigned long long)bytes, (unsigned long long)reserve); 426 return OMEGA_GPU_PLAT_REFUSED_LIMIT; 427 } 428 429 static int ref_dma_alloc(void *ctx, OmegaGpuDeviceHandle dev, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *out, 430 OmegaCpuView *cpu, OmegaGpuVa *gpu, OmegaGpuBusAddr *bus) { 431 LinuxRef *r = ctx; 432 if (!desc || !out || !cpu || !gpu) return OMEGA_GPU_PLAT_REFUSED_ARGS; 433 memset(out, 0, sizeof *out); memset(cpu, 0, sizeof *cpu); memset(gpu, 0, sizeof *gpu); 434 if (bus) memset(bus, 0, sizeof *bus); 435 ref_lock(r); 436 int rc = dev_ok(r, dev); 437 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_platform_check_buffer(desc, &g_caps); 438 /* GPU-uncached by default. GPU-cached only for DEVICE-PRIVATE data buffers (OWN-30B T3b, 2026-10-13): no CPU_READ, no 439 * HOST_POLLED, so the CPU never reads a line the GPU may still hold in L2 (nvos.h:1116 "For system memory this will not be 440 * coherent with direct CPU mappings"). omega_gpu_mem_cached.c (OWN-30B L2) writes such a buffer from the CPU exactly once, 441 * before any GPU read, then syncs; omega_gpu_devmem.h (T3b) fills them with GPU writes only. Either way no stale L2 line exists. */ 442 uint64_t am[16] = {4, (uint64_t)r->m16.rm.discrete, desc->kind, desc->rights, 443 desc->gpu_cache == OMEGA_GPU_CACHE_GPU_CACHED, (uint64_t)(rc < 0 ? 0 : rc)}; 444 uint64_t aq[16]; 445 (void)plat_decide(am, aq); 446 rc = (int)aq[0]; 447 const int local = (int)aq[1]; 448 const int cached = (int)aq[2]; 449 const int alloc_class = (int)aq[3]; 450 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; } 451 rc = mem_reserve_check(r, desc->bytes, local); /* D1 before handle/driver allocation; the sentence is in r->diag */ 452 if (rc != OMEGA_GPU_PLAT_OK) { ref_unlock(r); return rc; } 453 uint32_t idx = 0, gen = 0; 454 rc = omega_gpu_handles_open(&r->t, desc->kind, desc->bytes, LINUX_REF_BUFFERS, g_caps.max_total_bytes, &idx, &gen); 455 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; } 456 NvrmMem m; 457 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 458 plat_capture_mmap = 1; 459 if (plat_cap_fd >= 0) { close(plat_cap_fd); plat_cap_fd = -1; } 460 #endif 461 if ((alloc_class == 1 ? nvrm_alloc_vram(&r->m16.rm, desc->bytes, &m) : 462 alloc_class == 2 ? nvrm_alloc(&r->m16.rm, desc->bytes, &m) : nvrm_alloc_gpu_uncached(&r->m16.rm, desc->bytes, &m)) != 0) { 463 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 464 plat_capture_mmap = 0; 465 if (plat_cap_fd >= 0) { close(plat_cap_fd); plat_cap_fd = -1; } 466 #endif 467 (void)omega_gpu_handles_close(&r->t, idx, gen, desc->kind); /* this slot never got memory: its generation moves on */ 468 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, cached ? "nvrm_alloc" : "nvrm_alloc_gpu_uncached"); 469 ref_unlock(r); 470 return rc; 471 } 472 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 473 plat_capture_mmap = 0; 474 if (plat_cap_fd >= 0) { 475 if (plat_bind_fd(r, idx, plat_cap_fd, m.size) != 0) close(plat_cap_fd); 476 plat_cap_fd = -1; 477 } 478 #endif 479 r->mem[idx] = m; 480 r->rights[idx] = desc->rights; 481 r->gpu_cached[idx] = (uint8_t)cached; 482 out->index = idx; out->generation = gen; 483 cpu->ptr = m.cpu; cpu->len = (size_t)m.size; 484 gpu->va = m.va; 485 ref_unlock(r); 486 return OMEGA_GPU_PLAT_OK; 487 } 488 489 static int ref_dma_free(void *ctx, OmegaGpuBufferHandle b) { 490 LinuxRef *r = ctx; 491 uint32_t kind = 0; 492 ref_lock(r); 493 int rc = buf_ok(r, b, &kind); 494 if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u && r->m16.pb_mem.handle == r->mem[b.index].handle && 495 r->m16.pb_mem.cpu == r->mem[b.index].cpu) 496 rc = OMEGA_GPU_PLAT_REFUSED_BUSY; /* the open queue still uses it as its ring */ 497 for (uint32_t i = 0; rc == OMEGA_GPU_PLAT_OK && i + 1u < r->nchan; i++) 498 if (r->xq[i].generation != 0u && r->xring[i].handle == r->mem[b.index].handle) rc = OMEGA_GPU_PLAT_REFUSED_BUSY; 499 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_free"); ref_unlock(r); return rc; } 500 if (nvrm_free(&r->m16.rm, &r->mem[b.index]) != 0) { 501 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "nvrm_free (allocation retained for cleanup)"); 502 ref_unlock(r); 503 return rc; 504 } 505 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 506 plat_drop_token(r, b.index); 507 #endif 508 (void)omega_gpu_handles_close(&r->t, b.index, b.generation, kind); 509 memset(&r->mem[b.index], 0, sizeof r->mem[b.index]); 510 r->rights[b.index] = 0; 511 r->gpu_cached[b.index] = 0; 512 ref_unlock(r); 513 return OMEGA_GPU_PLAT_OK; 514 } 515 516 #if defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) 517 static int plat_publish(void *dst, const void *src, uint64_t n) { 518 memcpy(dst, src, (size_t)n); 519 return 0; 520 } 521 static int plat_fence(void) { 522 nvrm_cpu_barrier(); 523 return 0; 524 } 525 #else 526 static int plat_publish(void *dst, const void *src, uint64_t n) { 527 uint64_t wrote = 0; 528 if (n == 0) return 0; 529 if (omega2_plat_ring_store(&wrote, dst, n, src, n, n) != 0 || wrote != n) return -1; 530 return 0; 531 } 532 static int plat_fence(void) { 533 uint64_t result = 0; 534 return omega2_plat_fence(&result) == 0 && result == 1 ? 0 : -1; 535 } 536 #endif 537 538 static int ref_sync(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len, const char *stage, int to_gpu) { 539 LinuxRef *r = ctx; 540 ref_lock(r); 541 int rc = buf_ok(r, b, NULL); 542 if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, len); 543 if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, stage); 544 /* omega #457 (chip, 2026-10-15): after the host rewrites a GPU-cached buffer the GPU already read, every later launch 545 * read the old bytes from L2 (nvos.h:1117 "For system memory this will not be coherent with direct CPU mappings"). 546 * The next submission therefore starts with L2_SYSMEM_NCOH_INVALIDATE (ref_queue_submit). */ 547 else { 548 uint64_t pm[16] = {13, (uint64_t)to_gpu, r->gpu_cached[b.index]}; 549 if (plat_decide(pm, NULL)) { r->l2_inval_pending = 1; memset(r->x_l2_pending, 1, sizeof r->x_l2_pending); } 550 } 551 ref_unlock(r); 552 if (rc == OMEGA_GPU_PLAT_OK && plat_fence() != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 553 return rc; 554 } 555 static int ref_sync_to_gpu(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { return ref_sync(ctx, b, off, len, "sync_cpu_to_gpu", 1); } 556 static int ref_sync_to_cpu(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { return ref_sync(ctx, b, off, len, "sync_gpu_to_cpu", 0); } 557 558 static int ref_poll_word(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint32_t expected, uint64_t timeout_ms, uint32_t *observed) { 559 LinuxRef *r = ctx; 560 ref_lock(r); 561 int rc = buf_ok(r, b, NULL); 562 if (rc == OMEGA_GPU_PLAT_OK) { 563 uint64_t pm[16] = {10, r->rights[b.index], off}; 564 rc = (int)plat_decide(pm, NULL); 565 } 566 if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, 4u); 567 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "poll_word"); ref_unlock(r); return rc; } 568 volatile uint32_t *w = (volatile uint32_t *)((uint8_t *)r->mem[b.index].cpu + off); 569 #if !defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX) && !defined(CUDAPP_MIGRATION_PENDING_C_WAIT_MARKER) 570 uint64_t token = r->wait_token[b.index]; 571 uint64_t map_off = off; 572 omega_native_context *pctx = plat_wait_ctx; 573 if (token == 0) { 574 uint64_t mapping = 0, byte_off = 0; 575 omega_native_context *live = NULL; 576 if (omega_gpu_live_marker(&r->m16.rm, (const void *)w, &mapping, &byte_off, &live) == 0 && mapping != 0 && live) { 577 token = mapping; 578 map_off = byte_off; 579 pctx = live; 580 } 581 } 582 if (token == 0) { 583 ref_unlock(r); 584 rc = m16_native_wait_marker(w, expected, timeout_ms) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_POLL_TIMED_OUT; 585 } else { 586 uint64_t slots = 0; 587 int have = pctx && omega2_ctx_plat_wait_budget(pctx, &slots, timeout_ms) == 0; 588 ref_unlock(r); 589 if (!have) { 590 rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 591 } else { 592 slots = plat_scalar(13, slots, 0); 593 rc = OMEGA_GPU_POLL_TIMED_OUT; 594 for (uint64_t i = 0; i < slots; i++) { 595 uint64_t result = 0; 596 uint32_t st = omega2_ctx_plat_wait_ge(pctx, &result, token, map_off, expected, 1); 597 if (st == 0 && (int64_t)result == 0) { rc = OMEGA_GPU_PLAT_OK; break; } 598 if (i + 1u < slots) { 599 struct timespec ts = {0, 50000}; 600 nanosleep(&ts, NULL); 601 } 602 } 603 } 604 } 605 #else 606 ref_unlock(r); /* never hold the table lock while polling */ 607 rc = m16_native_wait_marker(w, expected, timeout_ms) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_POLL_TIMED_OUT; 608 #endif 609 if (observed) *observed = *w; 610 return rc; 611 } 612 613 static int queue_ok(LinuxRef *r, OmegaGpuQueueHandle q) { 614 return omega_gpu_handles_check(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 615 } 616 617 static int ref_queue_open(void *ctx, OmegaGpuDeviceHandle dev, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *out) { 618 LinuxRef *r = ctx; 619 uint32_t kind = 0; 620 if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS; 621 memset(out, 0, sizeof *out); 622 ref_lock(r); 623 int rc = dev_ok(r, dev); 624 if (rc == OMEGA_GPU_PLAT_OK) rc = buf_ok(r, ring, &kind); 625 if (rc == OMEGA_GPU_PLAT_OK && kind != OMEGA_GPU_HANDLE_BUF_QUEUE) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; 626 /* queue 0 first (channel 0); then, with AIEN_OMEGA_GPU_CHANNELS > 1, one queue per extra channel (L136) */ 627 int slot = -1; 628 if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u) { 629 const uint64_t m[16] = {r->nchan, r->xq[0].generation, r->xq[1].generation, r->xq[2].generation}; 630 const uint64_t chosen = plat_rest(6, m, NULL); 631 if (chosen) slot = (int)chosen - 1; 632 if (slot < 0) rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; 633 } 634 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_QUEUE, 0, r->nchan, 0, &out->index, &out->generation); /* one queue per channel (L136: nchan is 1 unless AIEN_OMEGA_GPU_CHANNELS) */ 635 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_open"); ref_unlock(r); return rc; } 636 if (slot >= 0) { r->xring[slot] = r->mem[ring.index]; r->xq[slot] = *out; r->x_l2_pending[slot] = (uint8_t)r->l2_inval_pending; r->xinflight[slot] = 0; ref_unlock(r); return OMEGA_GPU_PLAT_OK; } 637 r->m16.pb_mem = r->mem[ring.index]; /* m16_native_enqueue_methods copies the method stream here */ 638 r->queue = *out; 639 r->inflight = 0; 640 ref_unlock(r); 641 return OMEGA_GPU_PLAT_OK; 642 } 643 644 static int ref_queue_close(void *ctx, OmegaGpuQueueHandle q) { 645 LinuxRef *r = ctx; 646 ref_lock(r); 647 int rc = queue_ok(r, q); 648 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_close(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 649 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_close"); ref_unlock(r); return rc; } 650 const int slot = xq_slot(r, q); 651 if (slot >= 0) { memset(&r->xq[slot], 0, sizeof r->xq[slot]); memset(&r->xring[slot], 0, sizeof r->xring[slot]); r->xinflight[slot] = 0; ref_unlock(r); return OMEGA_GPU_PLAT_OK; } 652 memset(&r->queue, 0, sizeof r->queue); 653 r->inflight = 0; 654 memset(&r->m16.pb_mem, 0, sizeof r->m16.pb_mem); 655 ref_unlock(r); 656 return OMEGA_GPU_PLAT_OK; 657 } 658 659 static int ref_queue_submit(void *ctx, OmegaGpuQueueHandle q, const uint32_t *words, uint32_t nwords) { 660 LinuxRef *r = ctx; 661 ref_lock(r); 662 int rc = queue_ok(r, q); 663 if (rc == OMEGA_GPU_PLAT_OK && (!words || nwords == 0u)) rc = OMEGA_GPU_PLAT_REFUSED_ARGS; 664 /* CudaAuditFable Q6: every ring (channel 0 and each L136 extra channel) is rewritten from offset 0 by every 665 * submission, so a second one while the first is unretired would overwrite methods the GPU may still be fetching. 666 * caps.max_inflight is per queue; refused before any copy or doorbell. */ 667 const char *stage = "submit_methods"; 668 const int slot = rc == OMEGA_GPU_PLAT_OK ? xq_slot(r, q) : -1; 669 if (rc == OMEGA_GPU_PLAT_OK) { 670 uint64_t im[16] = {8, slot >= 0 ? r->xinflight[slot] : r->inflight, g_caps.max_inflight}; 671 if (plat_decide(im, NULL) != 0) { 672 rc = OMEGA_GPU_PLAT_REFUSED_BUSY; 673 stage = "queue_submit (the previous submission is not retired; caps.max_inflight is 1)"; 674 } 675 } 676 if (rc == OMEGA_GPU_PLAT_OK && slot >= 0) { 677 /* an extra channel: the same #457 prefix rule (its own pending flag), the same copy-to-ring-offset-0 + one GP 678 * entry + doorbell as m16_native_submit_methods, on channel slot + 1 */ 679 NvrmMem *ring = &r->xring[slot]; 680 const uint32_t xpre = (uint32_t)plat_scalar(8, r->x_l2_pending[slot], 0); 681 const uint64_t sm[16] = {nwords, xpre}; 682 uint64_t sq[16]; 683 (void)plat_rest(10, sm, sq); 684 uint32_t prefix[5]; 685 plat_l2_words(prefix); 686 uint64_t fm[16] = {9, nwords, xpre, ring->size}; 687 if (plat_decide(fm, NULL) != 0) rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 688 else { 689 uint32_t *dst = ring->cpu; 690 if (xpre && plat_publish(dst, prefix, sizeof prefix) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 691 else if (plat_publish(dst + xpre, words, sq[0]) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 692 else if (plat_fence() != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 693 if (rc == OMEGA_GPU_PLAT_OK && nvrm_chan_submit(&r->m16.rm, (uint32_t)slot + 1u, ring, 0, (uint32_t)sq[2]) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 694 if (rc == OMEGA_GPU_PLAT_OK) { r->x_l2_pending[slot] = 0; r->xinflight[slot] = (uint32_t)plat_scalar(11, r->xinflight[slot], 0); } 695 } 696 if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, "submit_methods (extra channel)"); 697 ref_unlock(r); 698 return rc; 699 } 700 /* omega #457: a host sync into a GPU-cached buffer is pending, so this submission starts with L2_INVAL_PREFIX. 701 * No dirty line is dropped: every earlier submission ended with L2_FLUSH_DIRTY and the host waited for it. 702 * DOCS SILENT on ordering between this MEM_OP and the launch that follows (no WFI is inserted); the chip gate 703 * tools/run_gpu_l2_stale_chip.sh (20/20 FRESH) is the only proof. The prefix counts against the ring like any 704 * word; callers budget it from g_caps.submit_prefix_words (omega_gpu_cmdlist.c). */ 705 const uint32_t pre = (uint32_t)plat_scalar(8, r->l2_inval_pending, 0); 706 const uint64_t sm[16] = {nwords, pre}; 707 uint64_t sq[16]; 708 (void)plat_rest(10, sm, sq); 709 uint64_t fm[16] = {9, nwords, pre, r->m16.pb_mem.size}; 710 if (rc == OMEGA_GPU_PLAT_OK && plat_decide(fm, NULL) != 0) rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 711 uint32_t *buf = NULL; 712 if (rc == OMEGA_GPU_PLAT_OK && pre) { 713 buf = malloc((size_t)sq[1]); 714 if (!buf) { rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; stage = "submit_methods (host copy for the #457 L2 invalidate prefix)"; } 715 else { 716 uint32_t prefix[5]; 717 plat_l2_words(prefix); 718 if (plat_publish(buf, prefix, sizeof prefix) != 0 || plat_publish(buf + pre, words, sq[0]) != 0) 719 rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 720 } 721 } 722 if (rc == OMEGA_GPU_PLAT_OK && m16_native_submit_methods(&r->m16, buf ? buf : words, (uint32_t)sq[2]) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; 723 if (rc == OMEGA_GPU_PLAT_OK && pre) r->l2_inval_pending = 0; 724 if (rc == OMEGA_GPU_PLAT_OK) r->inflight = (uint32_t)plat_scalar(11, r->inflight, 0); 725 free(buf); 726 if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, stage); 727 ref_unlock(r); 728 return rc; 729 } 730 731 static int ref_queue_retire(void *ctx, OmegaGpuQueueHandle q) { 732 LinuxRef *r = ctx; 733 ref_lock(r); 734 int rc = queue_ok(r, q); 735 const int slot = rc == OMEGA_GPU_PLAT_OK ? xq_slot(r, q) : -1; 736 if (slot >= 0) { 737 if (r->xinflight[slot]) { /* no-op when nothing is in flight on this extra channel */ 738 nvrm_chan_retire(&r->m16.rm, (uint32_t)slot + 1u, nvrm_chan_put(&r->m16.rm, (uint32_t)slot + 1u)); 739 r->xinflight[slot] = 0; 740 } 741 } 742 else if (rc == OMEGA_GPU_PLAT_OK && r->inflight) { 743 nvrm_retire(&r->m16.rm, r->m16.rm.put); /* both releases landed: the entry is consumed */ 744 r->inflight = 0; 745 } 746 else if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, "queue_retire"); 747 ref_unlock(r); 748 return rc; 749 } 750 751 static void *ref_host_alloc(void *ctx, size_t bytes) { (void)ctx; return bytes ? malloc(bytes) : NULL; } 752 static void ref_host_free(void *ctx, void *p) { (void)ctx; free(p); } 753 754 static const OmegaGpuPlatform g_platform = { 755 .ctx = &g_ref, 756 .time_now_ms = ref_now_ms, 757 .wait_ms = ref_wait_ms, 758 .lock = ref_lock_id, 759 .unlock = ref_unlock_id, 760 .mmio_read32 = NULL, /* not provided: the RM driver owns the registers */ 761 .mmio_write32 = NULL, 762 .dma_alloc = ref_dma_alloc, 763 .dma_free = ref_dma_free, 764 .sync_cpu_to_gpu = ref_sync_to_gpu, 765 .sync_gpu_to_cpu = ref_sync_to_cpu, 766 .poll_word = ref_poll_word, 767 .irq_wait = NULL, /* polling is not interrupt support */ 768 .device_open = ref_device_open, 769 .device_close = ref_device_close, 770 .diag_text = ref_diag_text, 771 .queue_open = ref_queue_open, 772 .queue_close = ref_queue_close, 773 .queue_submit = ref_queue_submit, 774 .queue_retire = ref_queue_retire, 775 .time_now_ns = ref_now_ns, 776 .host_alloc = ref_host_alloc, 777 .host_free = ref_host_free, 778 }; 779 780 const OmegaGpuPlatform *omega_gpu_platform_linux(OmegaGpuPlatformCaps *caps) { 781 if (caps) *caps = g_caps; 782 return &g_platform; 783 } 784 785 uint32_t omega_gpu_platform_linux_target_sm(void) { 786 ref_lock(&g_ref); 787 uint64_t sm[16] = {14, g_ref.dev.generation, g_ref.m16.rm.target_sm}; 788 const uint32_t target = (uint32_t)plat_decide(sm, NULL); 789 ref_unlock(&g_ref); 790 return target; 791 } 792 793 int omega_gpu_platform_linux_vram_info(uint64_t *free_bytes, uint64_t *total_bytes) { 794 ref_lock(&g_ref); 795 int rc = g_ref.dev.generation ? nvrm_vram_info(&g_ref.m16.rm, free_bytes, total_bytes) : -1; 796 ref_unlock(&g_ref); 797 return rc; 798 }