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
omega-mv / src / omega_gpu_platform_linux.c
| 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 | /* omega_gpu_platform_linux.c -- Linux REFERENCE implementation of OmegaGpuPlatform 1.1. REFERENCE ONLY: see the |
| 18 | * header. Every driver call here is the one src/omega_gpu_session.c made directly before P07 cut C, in the same |
| 19 | * order, so the launch path on GB10 under Linux is unchanged: |
| 20 | * device_open = nvrm_open + m16_native_create_channel (session open, before the four session buffers) |
| 21 | * dma_alloc = nvrm_alloc_gpu_uncached (every session / API buffer) |
| 22 | * dma_free = nvrm_free |
| 23 | * queue_open = ctx.pb_mem = the ring buffer (m16_native_enqueue_methods copies methods there) |
| 24 | * queue_submit = m16_native_submit_methods |
| 25 | * queue_retire = nvrm_retire(&rm, rm.put) (after both markers landed) |
| 26 | * poll_word = m16_native_wait_marker (>= serial compare, 50 us sleep-poll) |
| 27 | * sync_* = dsb sy (the barrier the session issued itself) |
| 28 | * device_close = m16_native_close (nvrm_close) |
| 29 | */ |
| 30 | #include "omega_gpu_platform_linux.h" |
| 31 | |
| 32 | #include "m16_native.h" |
| 33 | #include "nvrm.h" |
| 34 | |
| 35 | #include <pthread.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <time.h> |
| 40 | |
| 41 | #define LINUX_REF_BUFFERS (OMEGA_GPU_HANDLE_SLOTS - 2u) /* one slot each for the device and the queue */ |
| 42 | |
| 43 | typedef struct { |
| 44 | pthread_mutex_t mu; /* table, device and diagnostics */ |
| 45 | pthread_mutex_t locks[OMEGA_GPU_PLATFORM_LINUX_LOCKS]; |
| 46 | int ready; |
| 47 | OmegaGpuHandleTable t; |
| 48 | NvrmMem mem[OMEGA_GPU_HANDLE_SLOTS]; |
| 49 | uint32_t rights[OMEGA_GPU_HANDLE_SLOTS]; |
| 50 | uint8_t gpu_cached[OMEGA_GPU_HANDLE_SLOTS]; /* allocated GPU-cached (nvrm_alloc), omega #457 */ |
| 51 | int l2_inval_pending; /* a host sync into a GPU-cached buffer since the last submission, omega #457 */ |
| 52 | M16NativeContext m16; |
| 53 | OmegaGpuDeviceHandle dev; /* generation 0 = closed */ |
| 54 | OmegaGpuQueueHandle queue; |
| 55 | /* L136: extra queues on extra channels of the same channel group (nvrm_channel_n), opened only when |
| 56 | * AIEN_OMEGA_GPU_CHANNELS > 1 at device_open; queue 0 above is unchanged. */ |
| 57 | uint32_t nchan; |
| 58 | OmegaGpuQueueHandle xq[NVRM_MAX_EXTRA_CHANNELS]; |
| 59 | NvrmMem xring[NVRM_MAX_EXTRA_CHANNELS]; |
| 60 | uint8_t x_l2_pending[NVRM_MAX_EXTRA_CHANNELS]; |
| 61 | char diag[320]; |
| 62 | } LinuxRef; |
| 63 | |
| 64 | static LinuxRef g_ref = { |
| 65 | .mu = PTHREAD_MUTEX_INITIALIZER, |
| 66 | .locks = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER}, |
| 67 | }; |
| 68 | |
| 69 | /* 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 |
| 70 | * same method form as the C3 tail's L2_FLUSH_DIRTY. ref_queue_submit puts it ahead of the next submission after a host |
| 71 | * sync into a GPU-cached buffer; g_caps.submit_prefix_words is its size, so callers budget exactly what is written. */ |
| 72 | static const uint32_t L2_INVAL_PREFIX[] = { (2u << 28) | (4u << 16) | (0x0028u >> 2), 0, 0, 0, 0x11u << 27 }; |
| 73 | #define L2_INVAL_PREFIX_WORDS ((uint32_t)(sizeof L2_INVAL_PREFIX / sizeof L2_INVAL_PREFIX[0])) |
| 74 | |
| 75 | static const OmegaGpuPlatformCaps g_caps = { |
| 76 | .submit_prefix_words = L2_INVAL_PREFIX_WORDS, |
| 77 | .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, |
| 78 | .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, |
| 79 | .services = OMEGA_GPU_SVC_REQUIRED_SESSION, /* no MMIO (RM owns the registers), no IRQ (polling only) */ |
| 80 | .max_devices = 1, |
| 81 | .max_buffers = LINUX_REF_BUFFERS, |
| 82 | .max_queues = 1u + NVRM_MAX_EXTRA_CHANNELS, /* queues past the first need AIEN_OMEGA_GPU_CHANNELS (L136) */ |
| 83 | .max_inflight = 1, |
| 84 | .max_buffer_bytes = 1ull << 37, /* 128 GiB: the whole unified pool */ |
| 85 | .max_total_bytes = 1ull << 37, |
| 86 | }; |
| 87 | |
| 88 | static void ref_lock(LinuxRef *r) { |
| 89 | pthread_mutex_lock(&r->mu); |
| 90 | if (!r->ready) { omega_gpu_handles_init(&r->t); r->ready = 1; } |
| 91 | } |
| 92 | static void ref_unlock(LinuxRef *r) { pthread_mutex_unlock(&r->mu); } |
| 93 | |
| 94 | /* Records why the call failed; REFUSED_DEVICE carries the driver text (Nvrm.err). Called with r->mu held. */ |
| 95 | static int refuse(LinuxRef *r, int rc, const char *stage) { |
| 96 | if (rc == OMEGA_GPU_PLAT_REFUSED_DEVICE) |
| 97 | snprintf(r->diag, sizeof r->diag, "%s: %s", stage, r->m16.rm.err[0] ? r->m16.rm.err : "(no driver text)"); |
| 98 | else |
| 99 | snprintf(r->diag, sizeof r->diag, "%s: %s", stage, omega_gpu_platform_refusal_name(rc)); |
| 100 | return rc; |
| 101 | } |
| 102 | |
| 103 | static int dev_ok(LinuxRef *r, OmegaGpuDeviceHandle d) { |
| 104 | return omega_gpu_handles_check(&r->t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 105 | } |
| 106 | |
| 107 | /* A live buffer handle of any buffer kind; *kind gets it. */ |
| 108 | static int buf_ok(LinuxRef *r, OmegaGpuBufferHandle b, uint32_t *kind) { |
| 109 | if (b.index >= OMEGA_GPU_HANDLE_SLOTS || b.generation == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; |
| 110 | if (!r->t.live[b.index] || r->t.generation[b.index] != b.generation) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; |
| 111 | const uint32_t k = r->t.kind[b.index]; |
| 112 | if (k != OMEGA_GPU_HANDLE_BUF_DATA && k != OMEGA_GPU_HANDLE_BUF_CODE && k != OMEGA_GPU_HANDLE_BUF_QUEUE) |
| 113 | return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; |
| 114 | if (kind) *kind = k; |
| 115 | return OMEGA_GPU_PLAT_OK; |
| 116 | } |
| 117 | |
| 118 | static int range_ok(LinuxRef *r, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { |
| 119 | const uint64_t size = r->mem[b.index].size; |
| 120 | return (len == 0u || off > size || len > size - off) ? OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH : OMEGA_GPU_PLAT_OK; |
| 121 | } |
| 122 | |
| 123 | static uint64_t ref_now_ns(void *ctx) { |
| 124 | (void)ctx; |
| 125 | struct timespec t; |
| 126 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 127 | return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; |
| 128 | } |
| 129 | static uint64_t ref_now_ms(void *ctx) { return ref_now_ns(ctx) / 1000000ull; } |
| 130 | |
| 131 | static int ref_wait_ms(void *ctx, uint64_t ms) { |
| 132 | (void)ctx; |
| 133 | struct timespec t = {.tv_sec = (time_t)(ms / 1000u), .tv_nsec = (long)(ms % 1000u) * 1000000L}; |
| 134 | while (nanosleep(&t, &t) != 0) {} |
| 135 | return OMEGA_GPU_PLAT_OK; |
| 136 | } |
| 137 | |
| 138 | static int ref_lock_id(void *ctx, uint32_t id) { |
| 139 | LinuxRef *r = ctx; |
| 140 | if (id >= OMEGA_GPU_PLATFORM_LINUX_LOCKS) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 141 | return pthread_mutex_lock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_BUSY; |
| 142 | } |
| 143 | static int ref_unlock_id(void *ctx, uint32_t id) { |
| 144 | LinuxRef *r = ctx; |
| 145 | if (id >= OMEGA_GPU_PLATFORM_LINUX_LOCKS) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 146 | return pthread_mutex_unlock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 147 | } |
| 148 | |
| 149 | uint32_t omega_gpu_platform_linux_channels_env(void) { |
| 150 | const char *e = getenv("AIEN_OMEGA_GPU_CHANNELS"); |
| 151 | if (!e || e[1] != '\0' || e[0] < '2' || e[0] > (char)('1' + NVRM_MAX_EXTRA_CHANNELS)) return 1u; |
| 152 | return (uint32_t)(e[0] - '0'); |
| 153 | } |
| 154 | |
| 155 | /* Extra queue slot of q (0..nchan-2), or -1 when q is not an open extra queue. Caller holds r->mu. */ |
| 156 | static int xq_slot(const LinuxRef *r, OmegaGpuQueueHandle q) { |
| 157 | for (uint32_t i = 0; i + 1u < r->nchan; i++) |
| 158 | if (r->xq[i].generation != 0u && r->xq[i].index == q.index && r->xq[i].generation == q.generation) return (int)i; |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | static int ref_device_open(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *out) { |
| 163 | LinuxRef *r = ctx; |
| 164 | int rc; |
| 165 | if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 166 | memset(out, 0, sizeof *out); |
| 167 | ref_lock(r); |
| 168 | if (ordinal != 0u) { rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_ARGS, "device_open"); goto out; } |
| 169 | if (r->dev.generation != 0u) { rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_LIMIT, "device_open (already open)"); goto out; } |
| 170 | memset(&r->m16, 0, sizeof r->m16); |
| 171 | /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on failure) so the driver's own |
| 172 | * failure text survives into the stage name, as the session did before cut C. */ |
| 173 | if (nvrm_open(&r->m16.rm) != 0) { |
| 174 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "nvrm_open"); |
| 175 | (void)nvrm_close(&r->m16.rm); |
| 176 | goto out; |
| 177 | } |
| 178 | r->nchan = omega_gpu_platform_linux_channels_env(); |
| 179 | if (r->nchan == 1u ? m16_native_create_channel(&r->m16) != 0 : nvrm_channel_n(&r->m16.rm, r->nchan - 1u) != 0) { |
| 180 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, r->nchan == 1u ? "m16_native_create_channel" : "nvrm_channel_n"); |
| 181 | (void)m16_native_close(&r->m16); |
| 182 | goto out; |
| 183 | } |
| 184 | rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); |
| 185 | if (rc != OMEGA_GPU_PLAT_OK) { (void)m16_native_close(&r->m16); rc = refuse(r, rc, "device_open"); goto out; } |
| 186 | r->dev = *out; |
| 187 | r->diag[0] = '\0'; |
| 188 | out: |
| 189 | ref_unlock(r); |
| 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | static int ref_device_close(void *ctx, OmegaGpuDeviceHandle dev) { |
| 194 | LinuxRef *r = ctx; |
| 195 | ref_lock(r); |
| 196 | int rc = dev_ok(r, dev); |
| 197 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "device_close"); ref_unlock(r); return rc; } |
| 198 | /* Every handle of the device goes stale. The RM client close below releases the driver objects; buffers the |
| 199 | * caller did not free are not unmapped here, exactly as before cut C. */ |
| 200 | for (uint32_t i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) { |
| 201 | if (!r->t.live[i] || i == dev.index) continue; |
| 202 | (void)omega_gpu_handles_close(&r->t, i, r->t.generation[i], r->t.kind[i]); |
| 203 | memset(&r->mem[i], 0, sizeof r->mem[i]); |
| 204 | r->rights[i] = 0; |
| 205 | r->gpu_cached[i] = 0; |
| 206 | } |
| 207 | (void)omega_gpu_handles_close(&r->t, dev.index, dev.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 208 | memset(&r->dev, 0, sizeof r->dev); |
| 209 | r->l2_inval_pending = 0; /* omega #457: nothing pending survives the device */ |
| 210 | memset(&r->queue, 0, sizeof r->queue); |
| 211 | 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); |
| 212 | r->nchan = 0; |
| 213 | /* C5: the driver's close result is the proof that this process's channels (and any grid still on them) are gone. |
| 214 | * The state above is closed either way; a teardown fault is reported so omega_gpu_session_recover stays latched. */ |
| 215 | rc = m16_native_close(&r->m16) == 0 ? OMEGA_GPU_PLAT_OK : refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "device_close (driver teardown fault)"); |
| 216 | ref_unlock(r); |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | static const char *ref_diag_text(void *ctx) { return ((LinuxRef *)ctx)->diag; } |
| 221 | |
| 222 | static int ref_dma_alloc(void *ctx, OmegaGpuDeviceHandle dev, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *out, |
| 223 | OmegaCpuView *cpu, OmegaGpuVa *gpu, OmegaGpuBusAddr *bus) { |
| 224 | LinuxRef *r = ctx; |
| 225 | if (!desc || !out || !cpu || !gpu) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 226 | memset(out, 0, sizeof *out); memset(cpu, 0, sizeof *cpu); memset(gpu, 0, sizeof *gpu); |
| 227 | if (bus) memset(bus, 0, sizeof *bus); |
| 228 | ref_lock(r); |
| 229 | int rc = dev_ok(r, dev); |
| 230 | if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_platform_check_buffer(desc, &g_caps); |
| 231 | /* GPU-uncached by default. GPU-cached only for DEVICE-PRIVATE data buffers (OWN-30B T3b, 2026-10-13): no CPU_READ, no |
| 232 | * 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 |
| 233 | * coherent with direct CPU mappings"). omega_gpu_mem_cached.c (OWN-30B L2) writes such a buffer from the CPU exactly once, |
| 234 | * before any GPU read, then syncs; omega_gpu_devmem.h (T3b) fills them with GPU writes only. Either way no stale L2 line exists. */ |
| 235 | const int cached = desc->gpu_cache == OMEGA_GPU_CACHE_GPU_CACHED; |
| 236 | if (rc == OMEGA_GPU_PLAT_OK && cached && |
| 237 | (desc->kind != OMEGA_GPU_HANDLE_BUF_DATA || (desc->rights & (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_HOST_POLLED)))) |
| 238 | rc = OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY; |
| 239 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; } |
| 240 | uint32_t idx = 0, gen = 0; |
| 241 | rc = omega_gpu_handles_open(&r->t, desc->kind, desc->bytes, LINUX_REF_BUFFERS, g_caps.max_total_bytes, &idx, &gen); |
| 242 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; } |
| 243 | NvrmMem m; |
| 244 | if ((cached ? nvrm_alloc(&r->m16.rm, desc->bytes, &m) : nvrm_alloc_gpu_uncached(&r->m16.rm, desc->bytes, &m)) != 0) { |
| 245 | (void)omega_gpu_handles_close(&r->t, idx, gen, desc->kind); /* this slot never got memory: its generation moves on */ |
| 246 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, cached ? "nvrm_alloc" : "nvrm_alloc_gpu_uncached"); |
| 247 | ref_unlock(r); |
| 248 | return rc; |
| 249 | } |
| 250 | r->mem[idx] = m; |
| 251 | r->rights[idx] = desc->rights; |
| 252 | r->gpu_cached[idx] = (uint8_t)cached; |
| 253 | out->index = idx; out->generation = gen; |
| 254 | cpu->ptr = m.cpu; cpu->len = (size_t)m.size; |
| 255 | gpu->va = m.va; |
| 256 | ref_unlock(r); |
| 257 | return OMEGA_GPU_PLAT_OK; |
| 258 | } |
| 259 | |
| 260 | static int ref_dma_free(void *ctx, OmegaGpuBufferHandle b) { |
| 261 | LinuxRef *r = ctx; |
| 262 | uint32_t kind = 0; |
| 263 | ref_lock(r); |
| 264 | int rc = buf_ok(r, b, &kind); |
| 265 | if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u && r->m16.pb_mem.handle == r->mem[b.index].handle && |
| 266 | r->m16.pb_mem.cpu == r->mem[b.index].cpu) |
| 267 | rc = OMEGA_GPU_PLAT_REFUSED_BUSY; /* the open queue still uses it as its ring */ |
| 268 | for (uint32_t i = 0; rc == OMEGA_GPU_PLAT_OK && i + 1u < r->nchan; i++) |
| 269 | if (r->xq[i].generation != 0u && r->xring[i].handle == r->mem[b.index].handle) rc = OMEGA_GPU_PLAT_REFUSED_BUSY; |
| 270 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_free"); ref_unlock(r); return rc; } |
| 271 | (void)nvrm_free(&r->m16.rm, &r->mem[b.index]); |
| 272 | (void)omega_gpu_handles_close(&r->t, b.index, b.generation, kind); |
| 273 | memset(&r->mem[b.index], 0, sizeof r->mem[b.index]); |
| 274 | r->rights[b.index] = 0; |
| 275 | r->gpu_cached[b.index] = 0; |
| 276 | ref_unlock(r); |
| 277 | return OMEGA_GPU_PLAT_OK; |
| 278 | } |
| 279 | |
| 280 | static void barrier(void) { |
| 281 | #if defined(__aarch64__) |
| 282 | __asm__ volatile("dsb sy" ::: "memory"); |
| 283 | #else |
| 284 | __sync_synchronize(); |
| 285 | #endif |
| 286 | } |
| 287 | |
| 288 | static int ref_sync(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len, const char *stage, int to_gpu) { |
| 289 | LinuxRef *r = ctx; |
| 290 | ref_lock(r); |
| 291 | int rc = buf_ok(r, b, NULL); |
| 292 | if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, len); |
| 293 | if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, stage); |
| 294 | /* omega #457 (chip, 2026-10-15): after the host rewrites a GPU-cached buffer the GPU already read, every later launch |
| 295 | * read the old bytes from L2 (nvos.h:1117 "For system memory this will not be coherent with direct CPU mappings"). |
| 296 | * The next submission therefore starts with L2_SYSMEM_NCOH_INVALIDATE (ref_queue_submit). */ |
| 297 | else if (to_gpu && r->gpu_cached[b.index]) { r->l2_inval_pending = 1; memset(r->x_l2_pending, 1, sizeof r->x_l2_pending); } |
| 298 | ref_unlock(r); |
| 299 | if (rc == OMEGA_GPU_PLAT_OK) barrier(); /* uncached system memory: a full barrier is the whole sync */ |
| 300 | return rc; |
| 301 | } |
| 302 | 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); } |
| 303 | 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); } |
| 304 | |
| 305 | static int ref_poll_word(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint32_t expected, uint64_t timeout_ms, uint32_t *observed) { |
| 306 | LinuxRef *r = ctx; |
| 307 | ref_lock(r); |
| 308 | int rc = buf_ok(r, b, NULL); |
| 309 | if (rc == OMEGA_GPU_PLAT_OK && !(r->rights[b.index] & OMEGA_GPU_RIGHT_HOST_POLLED)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 310 | if (rc == OMEGA_GPU_PLAT_OK && (off & 3u)) rc = OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 311 | if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, 4u); |
| 312 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "poll_word"); ref_unlock(r); return rc; } |
| 313 | volatile uint32_t *w = (volatile uint32_t *)((uint8_t *)r->mem[b.index].cpu + off); |
| 314 | ref_unlock(r); /* never hold the table lock while polling */ |
| 315 | rc = m16_native_wait_marker(w, expected, timeout_ms) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_POLL_TIMED_OUT; |
| 316 | if (observed) *observed = *w; |
| 317 | return rc; |
| 318 | } |
| 319 | |
| 320 | static int queue_ok(LinuxRef *r, OmegaGpuQueueHandle q) { |
| 321 | return omega_gpu_handles_check(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); |
| 322 | } |
| 323 | |
| 324 | static int ref_queue_open(void *ctx, OmegaGpuDeviceHandle dev, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *out) { |
| 325 | LinuxRef *r = ctx; |
| 326 | uint32_t kind = 0; |
| 327 | if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 328 | memset(out, 0, sizeof *out); |
| 329 | ref_lock(r); |
| 330 | int rc = dev_ok(r, dev); |
| 331 | if (rc == OMEGA_GPU_PLAT_OK) rc = buf_ok(r, ring, &kind); |
| 332 | if (rc == OMEGA_GPU_PLAT_OK && kind != OMEGA_GPU_HANDLE_BUF_QUEUE) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; |
| 333 | /* queue 0 first (channel 0); then, with AIEN_OMEGA_GPU_CHANNELS > 1, one queue per extra channel (L136) */ |
| 334 | int slot = -1; |
| 335 | if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u) { |
| 336 | for (uint32_t i = 0; i + 1u < r->nchan && slot < 0; i++) if (r->xq[i].generation == 0u) slot = (int)i; |
| 337 | if (slot < 0) rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 338 | } |
| 339 | 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) */ |
| 340 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_open"); ref_unlock(r); return rc; } |
| 341 | 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; ref_unlock(r); return OMEGA_GPU_PLAT_OK; } |
| 342 | r->m16.pb_mem = r->mem[ring.index]; /* m16_native_enqueue_methods copies the method stream here */ |
| 343 | r->queue = *out; |
| 344 | ref_unlock(r); |
| 345 | return OMEGA_GPU_PLAT_OK; |
| 346 | } |
| 347 | |
| 348 | static int ref_queue_close(void *ctx, OmegaGpuQueueHandle q) { |
| 349 | LinuxRef *r = ctx; |
| 350 | ref_lock(r); |
| 351 | int rc = queue_ok(r, q); |
| 352 | if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_close(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); |
| 353 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_close"); ref_unlock(r); return rc; } |
| 354 | const int slot = xq_slot(r, q); |
| 355 | if (slot >= 0) { memset(&r->xq[slot], 0, sizeof r->xq[slot]); memset(&r->xring[slot], 0, sizeof r->xring[slot]); ref_unlock(r); return OMEGA_GPU_PLAT_OK; } |
| 356 | memset(&r->queue, 0, sizeof r->queue); |
| 357 | memset(&r->m16.pb_mem, 0, sizeof r->m16.pb_mem); |
| 358 | ref_unlock(r); |
| 359 | return OMEGA_GPU_PLAT_OK; |
| 360 | } |
| 361 | |
| 362 | static int ref_queue_submit(void *ctx, OmegaGpuQueueHandle q, const uint32_t *words, uint32_t nwords) { |
| 363 | LinuxRef *r = ctx; |
| 364 | ref_lock(r); |
| 365 | int rc = queue_ok(r, q); |
| 366 | if (rc == OMEGA_GPU_PLAT_OK && (!words || nwords == 0u)) rc = OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 367 | const int slot = rc == OMEGA_GPU_PLAT_OK ? xq_slot(r, q) : -1; |
| 368 | if (slot >= 0) { |
| 369 | /* an extra channel: the same #457 prefix rule (its own pending flag), the same copy-to-ring-offset-0 + one GP |
| 370 | * entry + doorbell as m16_native_submit_methods, on channel slot + 1 */ |
| 371 | NvrmMem *ring = &r->xring[slot]; |
| 372 | const uint32_t xpre = r->x_l2_pending[slot] ? L2_INVAL_PREFIX_WORDS : 0u; |
| 373 | if (((uint64_t)nwords + xpre) * 4u > ring->size) rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 374 | else { |
| 375 | uint32_t *dst = ring->cpu; |
| 376 | if (xpre) memcpy(dst, L2_INVAL_PREFIX, sizeof L2_INVAL_PREFIX); |
| 377 | memcpy(dst + xpre, words, (size_t)nwords * 4u); |
| 378 | __asm__ volatile("dsb sy" ::: "memory"); |
| 379 | if (nvrm_chan_submit(&r->m16.rm, (uint32_t)slot + 1u, ring, 0, nwords + xpre) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; |
| 380 | else r->x_l2_pending[slot] = 0; |
| 381 | } |
| 382 | if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, "submit_methods (extra channel)"); |
| 383 | ref_unlock(r); |
| 384 | return rc; |
| 385 | } |
| 386 | /* omega #457: a host sync into a GPU-cached buffer is pending, so this submission starts with L2_INVAL_PREFIX. |
| 387 | * No dirty line is dropped: every earlier submission ended with L2_FLUSH_DIRTY and the host waited for it. |
| 388 | * DOCS SILENT on ordering between this MEM_OP and the launch that follows (no WFI is inserted); the chip gate |
| 389 | * tools/run_gpu_l2_stale_chip.sh (20/20 FRESH) is the only proof. The prefix counts against the ring like any |
| 390 | * word; callers budget it from g_caps.submit_prefix_words (omega_gpu_cmdlist.c). */ |
| 391 | const uint32_t pre = r->l2_inval_pending ? L2_INVAL_PREFIX_WORDS : 0u; |
| 392 | if (rc == OMEGA_GPU_PLAT_OK && ((uint64_t)nwords + pre) * 4u > r->m16.pb_mem.size) rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 393 | uint32_t *buf = NULL; const char *stage = "submit_methods"; |
| 394 | if (rc == OMEGA_GPU_PLAT_OK && pre) { |
| 395 | buf = malloc(((size_t)nwords + pre) * 4u); |
| 396 | if (!buf) { rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; stage = "submit_methods (host copy for the #457 L2 invalidate prefix)"; } |
| 397 | else { |
| 398 | memcpy(buf, L2_INVAL_PREFIX, sizeof L2_INVAL_PREFIX); |
| 399 | memcpy(buf + pre, words, (size_t)nwords * 4u); |
| 400 | } |
| 401 | } |
| 402 | if (rc == OMEGA_GPU_PLAT_OK && m16_native_submit_methods(&r->m16, buf ? buf : words, nwords + pre) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; |
| 403 | if (rc == OMEGA_GPU_PLAT_OK && pre) r->l2_inval_pending = 0; |
| 404 | free(buf); |
| 405 | if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, stage); |
| 406 | ref_unlock(r); |
| 407 | return rc; |
| 408 | } |
| 409 | |
| 410 | static int ref_queue_retire(void *ctx, OmegaGpuQueueHandle q) { |
| 411 | LinuxRef *r = ctx; |
| 412 | ref_lock(r); |
| 413 | int rc = queue_ok(r, q); |
| 414 | const int slot = rc == OMEGA_GPU_PLAT_OK ? xq_slot(r, q) : -1; |
| 415 | if (slot >= 0) nvrm_chan_retire(&r->m16.rm, (uint32_t)slot + 1u, nvrm_chan_put(&r->m16.rm, (uint32_t)slot + 1u)); |
| 416 | else if (rc == OMEGA_GPU_PLAT_OK) nvrm_retire(&r->m16.rm, r->m16.rm.put); /* both releases landed: the entry is consumed */ |
| 417 | else rc = refuse(r, rc, "queue_retire"); |
| 418 | ref_unlock(r); |
| 419 | return rc; |
| 420 | } |
| 421 | |
| 422 | static void *ref_host_alloc(void *ctx, size_t bytes) { (void)ctx; return bytes ? malloc(bytes) : NULL; } |
| 423 | static void ref_host_free(void *ctx, void *p) { (void)ctx; free(p); } |
| 424 | |
| 425 | static const OmegaGpuPlatform g_platform = { |
| 426 | .ctx = &g_ref, |
| 427 | .time_now_ms = ref_now_ms, |
| 428 | .wait_ms = ref_wait_ms, |
| 429 | .lock = ref_lock_id, |
| 430 | .unlock = ref_unlock_id, |
| 431 | .mmio_read32 = NULL, /* not provided: the RM driver owns the registers */ |
| 432 | .mmio_write32 = NULL, |
| 433 | .dma_alloc = ref_dma_alloc, |
| 434 | .dma_free = ref_dma_free, |
| 435 | .sync_cpu_to_gpu = ref_sync_to_gpu, |
| 436 | .sync_gpu_to_cpu = ref_sync_to_cpu, |
| 437 | .poll_word = ref_poll_word, |
| 438 | .irq_wait = NULL, /* polling is not interrupt support */ |
| 439 | .device_open = ref_device_open, |
| 440 | .device_close = ref_device_close, |
| 441 | .diag_text = ref_diag_text, |
| 442 | .queue_open = ref_queue_open, |
| 443 | .queue_close = ref_queue_close, |
| 444 | .queue_submit = ref_queue_submit, |
| 445 | .queue_retire = ref_queue_retire, |
| 446 | .time_now_ns = ref_now_ns, |
| 447 | .host_alloc = ref_host_alloc, |
| 448 | .host_free = ref_host_free, |
| 449 | }; |
| 450 | |
| 451 | const OmegaGpuPlatform *omega_gpu_platform_linux(OmegaGpuPlatformCaps *caps) { |
| 452 | if (caps) *caps = g_caps; |
| 453 | return &g_platform; |
| 454 | } |