interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / cut4b / FB1-CUT4B-710d438c / omega_gpu_platform_linux.c

349 lines · 16092 bytes Raw Blame History
1/* omega_gpu_platform_linux.c -- Linux REFERENCE implementation of OmegaGpuPlatform 1.1. REFERENCE ONLY: see the
2 * header. Every driver call here is the one src/omega_gpu_session.c made directly before P07 cut C, in the same
3 * order, so the launch path on GB10 under Linux is unchanged:
4 * device_open = nvrm_open + m16_native_create_channel (session open, before the four session buffers)
5 * dma_alloc = nvrm_alloc_gpu_uncached (every session / API buffer)
6 * dma_free = nvrm_free
7 * queue_open = ctx.pb_mem = the ring buffer (m16_native_enqueue_methods copies methods there)
8 * queue_submit = m16_native_submit_methods
9 * queue_retire = nvrm_retire(&rm, rm.put) (after both markers landed)
10 * poll_word = m16_native_wait_marker (>= serial compare, 50 us sleep-poll)
11 * sync_* = dsb sy (the barrier the session issued itself)
12 * device_close = m16_native_close (nvrm_close)
13 */
14#include "omega_gpu_platform_linux.h"
15
16#include "m16_native.h"
17#include "nvrm.h"
18
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <time.h>
24
25#define LINUX_REF_BUFFERS (OMEGA_GPU_HANDLE_SLOTS - 2u) /* one slot each for the device and the queue */
26
27typedef struct {
28 pthread_mutex_t mu; /* table, device and diagnostics */
29 pthread_mutex_t locks[OMEGA_GPU_PLATFORM_LINUX_LOCKS];
30 int ready;
31 OmegaGpuHandleTable t;
32 NvrmMem mem[OMEGA_GPU_HANDLE_SLOTS];
33 uint32_t rights[OMEGA_GPU_HANDLE_SLOTS];
34 M16NativeContext m16;
35 OmegaGpuDeviceHandle dev; /* generation 0 = closed */
36 OmegaGpuQueueHandle queue;
37 char diag[320];
38} LinuxRef;
39
40static LinuxRef g_ref = {
41 .mu = PTHREAD_MUTEX_INITIALIZER,
42 .locks = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER},
43};
44
45static const OmegaGpuPlatformCaps g_caps = {
46 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR,
47 .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR,
48 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, /* no MMIO (RM owns the registers), no IRQ (polling only) */
49 .max_devices = 1,
50 .max_buffers = LINUX_REF_BUFFERS,
51 .max_queues = 1,
52 .max_inflight = 1,
53 .max_buffer_bytes = 1ull << 37, /* 128 GiB: the whole unified pool */
54 .max_total_bytes = 1ull << 37,
55};
56
57static void ref_lock(LinuxRef *r) {
58 pthread_mutex_lock(&r->mu);
59 if (!r->ready) { omega_gpu_handles_init(&r->t); r->ready = 1; }
60}
61static void ref_unlock(LinuxRef *r) { pthread_mutex_unlock(&r->mu); }
62
63/* Records why the call failed; REFUSED_DEVICE carries the driver text (Nvrm.err). Called with r->mu held. */
64static int refuse(LinuxRef *r, int rc, const char *stage) {
65 if (rc == OMEGA_GPU_PLAT_REFUSED_DEVICE)
66 snprintf(r->diag, sizeof r->diag, "%s: %s", stage, r->m16.rm.err[0] ? r->m16.rm.err : "(no driver text)");
67 else
68 snprintf(r->diag, sizeof r->diag, "%s: %s", stage, omega_gpu_platform_refusal_name(rc));
69 return rc;
70}
71
72static int dev_ok(LinuxRef *r, OmegaGpuDeviceHandle d) {
73 return omega_gpu_handles_check(&r->t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE);
74}
75
76/* A live buffer handle of any buffer kind; *kind gets it. */
77static int buf_ok(LinuxRef *r, OmegaGpuBufferHandle b, uint32_t *kind) {
78 if (b.index >= OMEGA_GPU_HANDLE_SLOTS || b.generation == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE;
79 if (!r->t.live[b.index] || r->t.generation[b.index] != b.generation) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE;
80 const uint32_t k = r->t.kind[b.index];
81 if (k != OMEGA_GPU_HANDLE_BUF_DATA && k != OMEGA_GPU_HANDLE_BUF_CODE && k != OMEGA_GPU_HANDLE_BUF_QUEUE)
82 return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE;
83 if (kind) *kind = k;
84 return OMEGA_GPU_PLAT_OK;
85}
86
87static int range_ok(LinuxRef *r, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) {
88 const uint64_t size = r->mem[b.index].size;
89 return (len == 0u || off > size || len > size - off) ? OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH : OMEGA_GPU_PLAT_OK;
90}
91
92static uint64_t ref_now_ns(void *ctx) {
93 (void)ctx;
94 struct timespec t;
95 clock_gettime(CLOCK_MONOTONIC, &t);
96 return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
97}
98static uint64_t ref_now_ms(void *ctx) { return ref_now_ns(ctx) / 1000000ull; }
99
100static int ref_wait_ms(void *ctx, uint64_t ms) {
101 (void)ctx;
102 struct timespec t = {.tv_sec = (time_t)(ms / 1000u), .tv_nsec = (long)(ms % 1000u) * 1000000L};
103 while (nanosleep(&t, &t) != 0) {}
104 return OMEGA_GPU_PLAT_OK;
105}
106
107static int ref_lock_id(void *ctx, uint32_t id) {
108 LinuxRef *r = ctx;
109 if (id >= OMEGA_GPU_PLATFORM_LINUX_LOCKS) return OMEGA_GPU_PLAT_REFUSED_ARGS;
110 return pthread_mutex_lock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_BUSY;
111}
112static int ref_unlock_id(void *ctx, uint32_t id) {
113 LinuxRef *r = ctx;
114 if (id >= OMEGA_GPU_PLATFORM_LINUX_LOCKS) return OMEGA_GPU_PLAT_REFUSED_ARGS;
115 return pthread_mutex_unlock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_ARGS;
116}
117
118static int ref_device_open(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *out) {
119 LinuxRef *r = ctx;
120 int rc;
121 if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS;
122 memset(out, 0, sizeof *out);
123 ref_lock(r);
124 if (ordinal != 0u) { rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_ARGS, "device_open"); goto out; }
125 if (r->dev.generation != 0u) { rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_LIMIT, "device_open (already open)"); goto out; }
126 memset(&r->m16, 0, sizeof r->m16);
127 /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on failure) so the driver's own
128 * failure text survives into the stage name, as the session did before cut C. */
129 if (nvrm_open(&r->m16.rm) != 0) {
130 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "nvrm_open");
131 (void)nvrm_close(&r->m16.rm);
132 goto out;
133 }
134 if (m16_native_create_channel(&r->m16) != 0) {
135 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "m16_native_create_channel");
136 (void)m16_native_close(&r->m16);
137 goto out;
138 }
139 rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation);
140 if (rc != OMEGA_GPU_PLAT_OK) { (void)m16_native_close(&r->m16); rc = refuse(r, rc, "device_open"); goto out; }
141 r->dev = *out;
142 r->diag[0] = '\0';
143out:
144 ref_unlock(r);
145 return rc;
146}
147
148static int ref_device_close(void *ctx, OmegaGpuDeviceHandle dev) {
149 LinuxRef *r = ctx;
150 ref_lock(r);
151 int rc = dev_ok(r, dev);
152 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "device_close"); ref_unlock(r); return rc; }
153 /* Every handle of the device goes stale. The RM client close below releases the driver objects; buffers the
154 * caller did not free are not unmapped here, exactly as before cut C. */
155 for (uint32_t i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) {
156 if (!r->t.live[i] || i == dev.index) continue;
157 (void)omega_gpu_handles_close(&r->t, i, r->t.generation[i], r->t.kind[i]);
158 memset(&r->mem[i], 0, sizeof r->mem[i]);
159 r->rights[i] = 0;
160 }
161 (void)omega_gpu_handles_close(&r->t, dev.index, dev.generation, OMEGA_GPU_HANDLE_DEVICE);
162 memset(&r->dev, 0, sizeof r->dev);
163 memset(&r->queue, 0, sizeof r->queue);
164 (void)m16_native_close(&r->m16);
165 ref_unlock(r);
166 return OMEGA_GPU_PLAT_OK;
167}
168
169static const char *ref_diag_text(void *ctx) { return ((LinuxRef *)ctx)->diag; }
170
171static int ref_dma_alloc(void *ctx, OmegaGpuDeviceHandle dev, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *out,
172 OmegaCpuView *cpu, OmegaGpuVa *gpu, OmegaGpuBusAddr *bus) {
173 LinuxRef *r = ctx;
174 if (!desc || !out || !cpu || !gpu) return OMEGA_GPU_PLAT_REFUSED_ARGS;
175 memset(out, 0, sizeof *out); memset(cpu, 0, sizeof *cpu); memset(gpu, 0, sizeof *gpu);
176 if (bus) memset(bus, 0, sizeof *bus);
177 ref_lock(r);
178 int rc = dev_ok(r, dev);
179 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_platform_check_buffer(desc, &g_caps);
180 /* GPU-uncached by default. GPU-cached only for DEVICE-PRIVATE data buffers (OWN-30B T3b, 2026-10-13): no CPU_READ, no
181 * 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
182 * coherent with direct CPU mappings"). omega_gpu_mem_cached.c (OWN-30B L2) writes such a buffer from the CPU exactly once,
183 * before any GPU read, then syncs; omega_gpu_devmem.h (T3b) fills them with GPU writes only. Either way no stale L2 line exists. */
184 const int cached = desc->gpu_cache == OMEGA_GPU_CACHE_GPU_CACHED;
185 if (rc == OMEGA_GPU_PLAT_OK && cached &&
186 (desc->kind != OMEGA_GPU_HANDLE_BUF_DATA || (desc->rights & (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_HOST_POLLED))))
187 rc = OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY;
188 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; }
189 uint32_t idx = 0, gen = 0;
190 rc = omega_gpu_handles_open(&r->t, desc->kind, desc->bytes, LINUX_REF_BUFFERS, g_caps.max_total_bytes, &idx, &gen);
191 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; }
192 NvrmMem m;
193 if ((cached ? nvrm_alloc(&r->m16.rm, desc->bytes, &m) : nvrm_alloc_gpu_uncached(&r->m16.rm, desc->bytes, &m)) != 0) {
194 (void)omega_gpu_handles_close(&r->t, idx, gen, desc->kind); /* this slot never got memory: its generation moves on */
195 rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "nvrm_alloc_gpu_uncached");
196 ref_unlock(r);
197 return rc;
198 }
199 r->mem[idx] = m;
200 r->rights[idx] = desc->rights;
201 out->index = idx; out->generation = gen;
202 cpu->ptr = m.cpu; cpu->len = (size_t)m.size;
203 gpu->va = m.va;
204 ref_unlock(r);
205 return OMEGA_GPU_PLAT_OK;
206}
207
208static int ref_dma_free(void *ctx, OmegaGpuBufferHandle b) {
209 LinuxRef *r = ctx;
210 uint32_t kind = 0;
211 ref_lock(r);
212 int rc = buf_ok(r, b, &kind);
213 if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u && r->m16.pb_mem.handle == r->mem[b.index].handle &&
214 r->m16.pb_mem.cpu == r->mem[b.index].cpu)
215 rc = OMEGA_GPU_PLAT_REFUSED_BUSY; /* the open queue still uses it as its ring */
216 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_free"); ref_unlock(r); return rc; }
217 (void)nvrm_free(&r->m16.rm, &r->mem[b.index]);
218 (void)omega_gpu_handles_close(&r->t, b.index, b.generation, kind);
219 memset(&r->mem[b.index], 0, sizeof r->mem[b.index]);
220 r->rights[b.index] = 0;
221 ref_unlock(r);
222 return OMEGA_GPU_PLAT_OK;
223}
224
225static void barrier(void) {
226#if defined(__aarch64__)
227 __asm__ volatile("dsb sy" ::: "memory");
228#else
229 __sync_synchronize();
230#endif
231}
232
233static int ref_sync(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len, const char *stage) {
234 LinuxRef *r = ctx;
235 ref_lock(r);
236 int rc = buf_ok(r, b, NULL);
237 if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, len);
238 if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, stage);
239 ref_unlock(r);
240 if (rc == OMEGA_GPU_PLAT_OK) barrier(); /* uncached system memory: a full barrier is the whole sync */
241 return rc;
242}
243static 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"); }
244static 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"); }
245
246static int ref_poll_word(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint32_t expected, uint64_t timeout_ms, uint32_t *observed) {
247 LinuxRef *r = ctx;
248 ref_lock(r);
249 int rc = buf_ok(r, b, NULL);
250 if (rc == OMEGA_GPU_PLAT_OK && !(r->rights[b.index] & OMEGA_GPU_RIGHT_HOST_POLLED)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS;
251 if (rc == OMEGA_GPU_PLAT_OK && (off & 3u)) rc = OMEGA_GPU_PLAT_REFUSED_ARGS;
252 if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, 4u);
253 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "poll_word"); ref_unlock(r); return rc; }
254 volatile uint32_t *w = (volatile uint32_t *)((uint8_t *)r->mem[b.index].cpu + off);
255 ref_unlock(r); /* never hold the table lock while polling */
256 rc = m16_native_wait_marker(w, expected, timeout_ms) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_POLL_TIMED_OUT;
257 if (observed) *observed = *w;
258 return rc;
259}
260
261static int queue_ok(LinuxRef *r, OmegaGpuQueueHandle q) {
262 return omega_gpu_handles_check(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE);
263}
264
265static int ref_queue_open(void *ctx, OmegaGpuDeviceHandle dev, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *out) {
266 LinuxRef *r = ctx;
267 uint32_t kind = 0;
268 if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS;
269 memset(out, 0, sizeof *out);
270 ref_lock(r);
271 int rc = dev_ok(r, dev);
272 if (rc == OMEGA_GPU_PLAT_OK) rc = buf_ok(r, ring, &kind);
273 if (rc == OMEGA_GPU_PLAT_OK && kind != OMEGA_GPU_HANDLE_BUF_QUEUE) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND;
274 if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u) rc = OMEGA_GPU_PLAT_REFUSED_LIMIT;
275 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &out->index, &out->generation);
276 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_open"); ref_unlock(r); return rc; }
277 r->m16.pb_mem = r->mem[ring.index]; /* m16_native_enqueue_methods copies the method stream here */
278 r->queue = *out;
279 ref_unlock(r);
280 return OMEGA_GPU_PLAT_OK;
281}
282
283static int ref_queue_close(void *ctx, OmegaGpuQueueHandle q) {
284 LinuxRef *r = ctx;
285 ref_lock(r);
286 int rc = queue_ok(r, q);
287 if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_close(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE);
288 if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_close"); ref_unlock(r); return rc; }
289 memset(&r->queue, 0, sizeof r->queue);
290 memset(&r->m16.pb_mem, 0, sizeof r->m16.pb_mem);
291 ref_unlock(r);
292 return OMEGA_GPU_PLAT_OK;
293}
294
295static int ref_queue_submit(void *ctx, OmegaGpuQueueHandle q, const uint32_t *words, uint32_t nwords) {
296 LinuxRef *r = ctx;
297 ref_lock(r);
298 int rc = queue_ok(r, q);
299 if (rc == OMEGA_GPU_PLAT_OK && (!words || nwords == 0u)) rc = OMEGA_GPU_PLAT_REFUSED_ARGS;
300 if (rc == OMEGA_GPU_PLAT_OK && (uint64_t)nwords * 4u > r->m16.pb_mem.size) rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH;
301 if (rc == OMEGA_GPU_PLAT_OK && m16_native_submit_methods(&r->m16, words, nwords) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE;
302 if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, "submit_methods");
303 ref_unlock(r);
304 return rc;
305}
306
307static int ref_queue_retire(void *ctx, OmegaGpuQueueHandle q) {
308 LinuxRef *r = ctx;
309 ref_lock(r);
310 int rc = queue_ok(r, q);
311 if (rc == OMEGA_GPU_PLAT_OK) nvrm_retire(&r->m16.rm, r->m16.rm.put); /* both releases landed: the entry is consumed */
312 else rc = refuse(r, rc, "queue_retire");
313 ref_unlock(r);
314 return rc;
315}
316
317static void *ref_host_alloc(void *ctx, size_t bytes) { (void)ctx; return bytes ? malloc(bytes) : NULL; }
318static void ref_host_free(void *ctx, void *p) { (void)ctx; free(p); }
319
320static const OmegaGpuPlatform g_platform = {
321 .ctx = &g_ref,
322 .time_now_ms = ref_now_ms,
323 .wait_ms = ref_wait_ms,
324 .lock = ref_lock_id,
325 .unlock = ref_unlock_id,
326 .mmio_read32 = NULL, /* not provided: the RM driver owns the registers */
327 .mmio_write32 = NULL,
328 .dma_alloc = ref_dma_alloc,
329 .dma_free = ref_dma_free,
330 .sync_cpu_to_gpu = ref_sync_to_gpu,
331 .sync_gpu_to_cpu = ref_sync_to_cpu,
332 .poll_word = ref_poll_word,
333 .irq_wait = NULL, /* polling is not interrupt support */
334 .device_open = ref_device_open,
335 .device_close = ref_device_close,
336 .diag_text = ref_diag_text,
337 .queue_open = ref_queue_open,
338 .queue_close = ref_queue_close,
339 .queue_submit = ref_queue_submit,
340 .queue_retire = ref_queue_retire,
341 .time_now_ns = ref_now_ns,
342 .host_alloc = ref_host_alloc,
343 .host_free = ref_host_free,
344};
345
346const OmegaGpuPlatform *omega_gpu_platform_linux(OmegaGpuPlatformCaps *caps) {
347 if (caps) *caps = g_caps;
348 return &g_platform;
349}