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 / src / omega_accelerator_world.c

1338 lines · 58060 bytes Raw Blame History
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#include "omega_accelerator_world.h"
18#include "omega_gpu_code_alloc.h"
19#include "omega_blackwell_qmd.h"
20#include "omega_blackwell_encoder.h"
21#include "omega_blackwell_codegen.h"
22#include "omega_blackwell_submit.h"
23#include "omega_gpu_wait.h"
24#include "sha256.h"
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
29#include <unistd.h>
30#include <sys/mman.h>
31#include <stdatomic.h>
32
33/* Capability epochs are process unique; the local epoch remains available for
34 * deterministic rolling provenance across independent world instances. */
35static _Atomic uint32_t next_handle_epoch = 1;
36
37static uint32_t allocate_handle_epoch(void) {
38 uint32_t old = atomic_load_explicit(&next_handle_epoch, memory_order_relaxed);
39 for (;;) {
40 if (old == UINT32_MAX) return 0;
41 if (atomic_compare_exchange_weak_explicit(&next_handle_epoch, &old, old + 1,
42 memory_order_relaxed, memory_order_relaxed))
43 return old;
44 }
45}
46
47
48static const uint32_t WORLD_SETUP_WORDS[18] = {
49 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
50 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
51 0x20012559, 0x00000000,
52};
53
54/* A submission counts as complete once the hardware marker has reached or
55 * passed its payload (serial-number arithmetic, correct across 2^32 wrap). */
56static bool payload_reached(uint32_t payload, uint32_t upto) {
57 return (int32_t)(payload - upto) <= 0;
58}
59
60/* CHIPWAIT-2: completion memory and waits for the world's completion page.
61 * The page is GPU-uncached. Each synchronous dispatch (execute and the matmul
62 * variant) releases its payload at +0 and, after an L2_FLUSH_DIRTY mem-op
63 * (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27 OPERATION = 0x10, as in
64 * omega_numeric_gb10.c), the same payload at +0x10; the host waits for both with
65 * the wrap-safe sequence wait. The same payload sequence is used so the check is
66 * "reached or passed", never exact equality. */
67#define WORLD_MARKER2_OFFSET 0x10
68
69static volatile uint32_t *world_marker2(const OmegaAcceleratorWorld *world) {
70 return (volatile uint32_t *)((uint8_t *)world->completion.cpu_marker + WORLD_MARKER2_OFFSET);
71}
72
73
74/* Wait until word has reached `payload` (sequence wait). Returns 0 on PASS;
75 * otherwise prints one tagged line with the report and returns -1. The caller
76 * keeps its own fault handling. The hard total is *budget_ms; time spent is
77 * deducted so chained waits share one budget. */
78static int world_wait_seq(const char *site, const char *what, volatile uint32_t *word,
79 uint32_t payload, uint32_t progress_ms, uint32_t *budget_ms) {
80 uint32_t total_ms = *budget_ms;
81 omega_gpu_wait_cfg_t cfg;
82 memset(&cfg, 0, sizeof(cfg));
83 cfg.progress_timeout_ms = progress_ms;
84 cfg.total_timeout_ms = total_ms;
85 omega_gpu_wait_report_t r;
86 if (omega_gpu_wait_sequence(word, payload, &r, &cfg)) {
87 uint64_t spent_ms = r.elapsed_ns / 1000000ull;
88 *budget_ms = spent_ms < total_ms ? total_ms - (uint32_t)spent_ms : 1u;
89 return 0;
90 }
91 fprintf(stderr,
92 "WORLD_WAIT_FAIL site=%s wait=%s kind=%s result=%s expected=%u observed=%u "
93 "elapsed_ns=%llu progress_count=%llu polls=%llu\n",
94 site, what, omega_gpu_wait_kind_name(r.wait_kind), omega_gpu_wait_result_name(r.result),
95 r.expected, r.last_observed, (unsigned long long)r.elapsed_ns,
96 (unsigned long long)r.progress_count, (unsigned long long)r.polls);
97 return -1;
98}
99
100/* Synchronous dispatch: marker, then marker2 (progress stall 5000 ms, one hard
101 * total of 600000 ms shared by both waits). */
102static int world_wait_dispatch(const OmegaAcceleratorWorld *world, const char *site, uint32_t payload) {
103 uint32_t budget_ms = 600000;
104 if (world_wait_seq(site, "marker", world->completion.cpu_marker, payload, 5000, &budget_ms) != 0) return -1;
105 return world_wait_seq(site, "marker2", world_marker2(world), payload, 5000, &budget_ms);
106}
107
108static bool buffer_referenced_in_flight(const OmegaAcceleratorWorld *world, uint32_t id) {
109 for (uint32_t i = 0; i < world->in_flight_count; i++) {
110 const OmegaWorldSubmission *s = &world->in_flight[i];
111 if (s->a_object_id == id || s->b_object_id == id || s->c_object_id == id) return true;
112 }
113 return false;
114}
115
116static bool code_referenced_in_flight(const OmegaAcceleratorWorld *world, uint32_t id) {
117 for (uint32_t i = 0; i < world->in_flight_count; i++) {
118 if (world->in_flight[i].code_object_id == id) return true;
119 }
120 return false;
121}
122
123/* Return a revoked slot's memory to PHYSICS. The slot becomes reusable only
124 * after this succeeds; a PHYSICS refusal is an accounting failure. */
125static int release_buffer_memory(OmegaAcceleratorWorld *world, OmegaBufferEntry *entry) {
126 if (nvrm_free(&world->m16.rm, &entry->mem) != 0) {
127 world->faulted = true;
128 return OMEGA_WORLD_ERR_FAULT;
129 }
130 entry->retiring = false;
131 entry->cpu_addr = NULL;
132 entry->gpu_va = 0;
133 entry->size_bytes = 0;
134 return OMEGA_WORLD_OK;
135}
136
137static int release_code_memory(OmegaAcceleratorWorld *world, OmegaCodeEntry *entry) {
138 if (nvrm_free(&world->m16.rm, &entry->mem) != 0) {
139 world->faulted = true;
140 return OMEGA_WORLD_ERR_FAULT;
141 }
142 entry->retiring = false;
143 entry->cpu_addr = NULL;
144 entry->gpu_va = 0;
145 entry->size_bytes = 0;
146 return OMEGA_WORLD_OK;
147}
148
149/* Release every retiring slot that no remaining in-flight submission binds. */
150static void release_unreferenced_retiring(OmegaAcceleratorWorld *world) {
151 for (uint32_t i = 0; i < OMEGA_WORLD_MAX_BUFFERS; i++) {
152 OmegaBufferEntry *e = &world->buffers[i];
153 if (e->retiring && !buffer_referenced_in_flight(world, i)) (void)release_buffer_memory(world, e);
154 }
155 for (uint32_t i = 0; i < OMEGA_WORLD_MAX_CODE_ENTRIES; i++) {
156 OmegaCodeEntry *e = &world->code_entries[i];
157 if (e->retiring && !code_referenced_in_flight(world, i)) (void)release_code_memory(world, e);
158 }
159}
160
161int omega_world_init(OmegaAcceleratorWorld *world) {
162 if (!world) return OMEGA_WORLD_ERR_INVALID_ARG;
163 memset(world, 0, sizeof(*world));
164
165 world->current_epoch = 1;
166 world->handle_epoch = allocate_handle_epoch();
167 if (!world->handle_epoch) return OMEGA_WORLD_ERR_FAULT;
168 world->channel_generation = 1;
169
170 if (m16_native_open(&world->m16) != 0) {
171 return OMEGA_WORLD_ERR_HARDWARE;
172 }
173 if (m16_native_create_channel(&world->m16) != 0) {
174 m16_native_close(&world->m16);
175 return OMEGA_WORLD_ERR_HARDWARE;
176 }
177
178 world->ring_capacity = world->m16.rm.entries;
179 world->channel_active = true;
180
181 /* The CPU rewrites retired command slots. GPU-cached slots can replay
182 * earlier commands despite CPU store barriers, so bypass that cache. */
183 if (nvrm_alloc_gpu_uncached(&world->m16.rm, 0x10000, &world->pb_mem) != 0) {
184 m16_native_close(&world->m16);
185 return OMEGA_WORLD_ERR_NO_MEM;
186 }
187 world->m16.pb_mem = world->pb_mem;
188
189 /* Allocate persistent scratch arena */
190 /* GB10: GPU-cacheable system memory is not coherent with CPU mappings (nvos.h:1115-1118, hive-phases I37). */
191 if (nvrm_alloc_gpu_uncached(&world->m16.rm, OMEGA_WORLD_SCRATCH_SIZE, &world->scratch.mem) != 0) {
192 m16_native_close(&world->m16);
193 return OMEGA_WORLD_ERR_NO_MEM;
194 }
195 world->scratch.capacity_bytes = OMEGA_WORLD_SCRATCH_SIZE;
196 world->scratch.allocated_bytes = 0;
197
198 /* Allocate persistent completion marker */
199 /* GB10: GPU-cacheable system memory is not coherent with CPU mappings (nvos.h:1115-1118, hive-phases I37). */
200 if (nvrm_alloc_gpu_uncached(&world->m16.rm, 0x1000, &world->completion.mem) != 0) {
201 m16_native_close(&world->m16);
202 return OMEGA_WORLD_ERR_NO_MEM;
203 }
204 world->completion.cpu_marker = (volatile uint32_t *)world->completion.mem.cpu;
205 world->completion.gpu_va = world->completion.mem.va;
206 *world->completion.cpu_marker = 0;
207 world->completion.cpu_marker2 = (volatile uint32_t *)((uint8_t *)world->completion.mem.cpu + 0x10);
208 world->completion.gpu_va2 = world->completion.mem.va + 0x10;
209 *world->completion.cpu_marker2 = 0;
210 __asm__ volatile("dsb sy" ::: "memory");
211 world->completion.last_payload = 0;
212
213 /* Initialize rolling state digest D_0 */
214 memset(world->rolling_state_digest, 0, sizeof(world->rolling_state_digest));
215 world->sequence_number = 0;
216
217 world->initialized = true;
218 return OMEGA_WORLD_OK;
219}
220
221int omega_world_destroy(OmegaAcceleratorWorld *world) {
222 if (!world || !world->initialized) return OMEGA_WORLD_ERR_INVALID_ARG;
223
224 for (uint32_t i = 0; i < OMEGA_WORLD_MAX_BUFFERS; i++) {
225 world->buffers[i].active = false;
226 }
227 for (uint32_t i = 0; i < OMEGA_WORLD_MAX_CODE_ENTRIES; i++) {
228 world->code_entries[i].active = false;
229 }
230
231 int close_rc = m16_native_close(&world->m16);
232 if (world->m16.rm.faulted) world->faulted = true;
233 world->buffer_count = 0;
234 world->code_count = 0;
235 world->in_flight_count = 0;
236 world->scratch.allocated_bytes = 0;
237 world->completion.cpu_marker = NULL;
238 world->completion.gpu_va = 0;
239 world->completion.cpu_marker2 = NULL;
240 world->completion.gpu_va2 = 0;
241 memset(&world->pb_mem, 0, sizeof world->pb_mem);
242 world->channel_active = false;
243 world->initialized = false;
244 return close_rc == 0 ? OMEGA_WORLD_OK : OMEGA_WORLD_ERR_HARDWARE;
245}
246
247int omega_world_rebuild(OmegaAcceleratorWorld *world) {
248 if (!world || !world->initialized) return OMEGA_WORLD_ERR_INVALID_ARG;
249 if (world->current_epoch == UINT32_MAX) return OMEGA_WORLD_ERR_FAULT;
250 uint32_t next_epoch = world->current_epoch + 1;
251 if (world->initialized && omega_world_destroy(world) != OMEGA_WORLD_OK)
252 return OMEGA_WORLD_ERR_HARDWARE;
253
254 int rc = omega_world_init(world);
255 if (rc == OMEGA_WORLD_OK) {
256 world->current_epoch = next_epoch;
257 }
258 return rc;
259}
260
261int omega_world_register_buffer(OmegaAcceleratorWorld *world,
262 size_t size_bytes,
263 uint32_t permissions,
264 OmegaHandle *out_handle) {
265 if (!world || !world->initialized || size_bytes == 0 || !out_handle) {
266 return OMEGA_WORLD_ERR_INVALID_ARG;
267 }
268 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
269
270 int free_slot = -1;
271 for (uint32_t i = 0; i < OMEGA_WORLD_MAX_BUFFERS; i++) {
272 if (!world->buffers[i].active && !world->buffers[i].retiring &&
273 world->buffers[i].generation != UINT32_MAX) {
274 free_slot = (int)i;
275 break;
276 }
277 }
278 if (free_slot < 0) return OMEGA_WORLD_ERR_NO_MEM;
279
280 OmegaBufferEntry *entry = &world->buffers[free_slot];
281 if (size_bytes > SIZE_MAX - 0xfffULL) return OMEGA_WORLD_ERR_BOUNDS;
282 size_t alloc_bytes = (size_bytes + 0xfffULL) & ~0xfffULL;
283 if (alloc_bytes < 0x1000) alloc_bytes = 0x1000;
284
285 /* GB10: GPU-cacheable system memory is not coherent with CPU mappings (nvos.h:1115-1118, hive-phases I37). */
286 if (nvrm_alloc_gpu_uncached(&world->m16.rm, alloc_bytes, &entry->mem) != 0) {
287 return OMEGA_WORLD_ERR_NO_MEM;
288 }
289
290 entry->object_id = (uint32_t)free_slot;
291 if (entry->generation == 0) {
292 entry->generation = 1;
293 }
294 entry->permissions = permissions;
295 entry->size_bytes = size_bytes;
296 entry->cpu_addr = entry->mem.cpu;
297 entry->gpu_va = entry->mem.va;
298 entry->active = true;
299
300 out_handle->world_epoch = world->handle_epoch;
301 out_handle->object_id = (uint32_t)free_slot;
302 out_handle->object_generation = entry->generation;
303 out_handle->object_type = OMEGA_OBJ_BUFFER;
304 out_handle->permissions = permissions;
305
306 world->buffer_count++;
307 return OMEGA_WORLD_OK;
308}
309
310int omega_world_revoke_buffer(OmegaAcceleratorWorld *world,
311 const OmegaHandle *handle) {
312 if (!world || !world->initialized || !handle) return OMEGA_WORLD_ERR_INVALID_ARG;
313 if (handle->world_epoch != world->handle_epoch) return OMEGA_WORLD_ERR_STALE_EPOCH;
314 if (handle->object_id >= OMEGA_WORLD_MAX_BUFFERS) return OMEGA_WORLD_ERR_INVALID_ARG;
315
316 OmegaBufferEntry *entry = &world->buffers[handle->object_id];
317 if (!entry->active) return OMEGA_WORLD_ERR_NOT_FOUND;
318 if (handle->object_generation != entry->generation) return OMEGA_WORLD_ERR_STALE_GEN;
319 if (handle->object_type != OMEGA_OBJ_BUFFER) return OMEGA_WORLD_ERR_INVALID_ARG;
320
321 /* The handle is refused from this instant; the memory is returned now,
322 * or after drain if an in-flight submission still binds it. */
323 entry->active = false;
324 if (entry->generation != UINT32_MAX) entry->generation++;
325 if (world->buffer_count > 0) world->buffer_count--;
326 if (buffer_referenced_in_flight(world, handle->object_id)) {
327 entry->retiring = true;
328 return OMEGA_WORLD_OK;
329 }
330 return release_buffer_memory(world, entry);
331}
332
333int omega_world_resolve_buffer(OmegaAcceleratorWorld *world,
334 const OmegaHandle *handle,
335 uint32_t required_perms,
336 size_t req_offset,
337 size_t req_size,
338 void **out_cpu,
339 uint64_t *out_gpu_va) {
340 if (!world || !world->initialized || !handle) return OMEGA_WORLD_ERR_INVALID_ARG;
341 if (handle->world_epoch != world->handle_epoch) return OMEGA_WORLD_ERR_STALE_EPOCH;
342 if (handle->object_id >= OMEGA_WORLD_MAX_BUFFERS) return OMEGA_WORLD_ERR_INVALID_ARG;
343
344 OmegaBufferEntry *entry = &world->buffers[handle->object_id];
345 if (!entry->active) return OMEGA_WORLD_ERR_NOT_FOUND;
346 if (handle->object_generation != entry->generation) return OMEGA_WORLD_ERR_STALE_GEN;
347 if (handle->object_type != OMEGA_OBJ_BUFFER) return OMEGA_WORLD_ERR_INVALID_ARG;
348
349 if ((entry->permissions & required_perms) != required_perms) return OMEGA_WORLD_ERR_PERM_DENIED;
350 if ((handle->permissions & required_perms) != required_perms) return OMEGA_WORLD_ERR_PERM_DENIED;
351 if ((required_perms & OMEGA_PERM_WRITE) &&
352 buffer_referenced_in_flight(world, handle->object_id))
353 return OMEGA_WORLD_ERR_FAULT;
354
355 if (req_offset > entry->size_bytes || req_size > entry->size_bytes - req_offset ||
356 entry->gpu_va > UINT64_MAX - req_offset) return OMEGA_WORLD_ERR_BOUNDS;
357
358 if (out_cpu) {
359 *out_cpu = (uint8_t *)entry->cpu_addr + req_offset;
360 }
361 if (out_gpu_va) {
362 *out_gpu_va = entry->gpu_va + req_offset;
363 }
364 return OMEGA_WORLD_OK;
365}
366
367int omega_world_register_code(OmegaAcceleratorWorld *world,
368 const void *code_bytes,
369 size_t code_size,
370 const uint8_t realization_id[32],
371 OmegaHandle *out_handle) {
372 if (!world || !world->initialized || !code_bytes || code_size == 0 || !out_handle) {
373 return OMEGA_WORLD_ERR_INVALID_ARG;
374 }
375 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
376
377 int free_slot = -1;
378 for (uint32_t i = 0; i < OMEGA_WORLD_MAX_CODE_ENTRIES; i++) {
379 if (!world->code_entries[i].active && !world->code_entries[i].retiring &&
380 world->code_entries[i].generation != UINT32_MAX) {
381 free_slot = (int)i;
382 break;
383 }
384 }
385 if (free_slot < 0) return OMEGA_WORLD_ERR_NO_MEM;
386
387 OmegaCodeEntry *entry = &world->code_entries[free_slot];
388 size_t alloc_bytes = omega_gpu_code_alloc_bytes(code_size); /* code + 2 KB prefetch tail, page rounded */
389 if (alloc_bytes == 0) return OMEGA_WORLD_ERR_BOUNDS;
390
391 /* GB10: GPU-cacheable system memory is not coherent with CPU mappings (nvos.h:1115-1118, hive-phases I37). */
392 if (nvrm_alloc_gpu_uncached(&world->m16.rm, alloc_bytes, &entry->mem) != 0) {
393 return OMEGA_WORLD_ERR_NO_MEM;
394 }
395
396 memcpy(entry->mem.cpu, code_bytes, code_size);
397 memset((uint8_t *)entry->mem.cpu + code_size, 0, alloc_bytes - code_size);
398
399 entry->object_id = (uint32_t)free_slot;
400 if (entry->generation == 0) {
401 entry->generation = 1;
402 }
403 entry->size_bytes = code_size;
404 entry->cpu_addr = entry->mem.cpu;
405 entry->gpu_va = entry->mem.va;
406
407 sha256_hash(code_bytes, code_size, entry->code_digest);
408 if (realization_id) {
409 memcpy(entry->realization_id, realization_id, 32);
410 } else {
411 /* Derive a real realization identity from the realized machine code when
412 * the registrant does not supply one, so digest provenance never falls
413 * back to a placeholder. */
414 static const uint8_t REALIZATION_DOMAIN[32] = "OMEGA_CODE_REALIZATION_V1";
415 uint8_t rbuf[64];
416 memcpy(rbuf, REALIZATION_DOMAIN, 32);
417 memcpy(rbuf + 32, entry->code_digest, 32);
418 sha256_hash(rbuf, sizeof(rbuf), entry->realization_id);
419 }
420 entry->active = true;
421
422 out_handle->world_epoch = world->handle_epoch;
423 out_handle->object_id = (uint32_t)free_slot;
424 out_handle->object_generation = entry->generation;
425 out_handle->object_type = OMEGA_OBJ_CODE;
426 out_handle->permissions = OMEGA_PERM_EXECUTE;
427
428 world->code_count++;
429 return OMEGA_WORLD_OK;
430}
431
432int omega_world_revoke_code(OmegaAcceleratorWorld *world,
433 const OmegaHandle *handle) {
434 if (!world || !world->initialized || !handle) return OMEGA_WORLD_ERR_INVALID_ARG;
435 if (handle->world_epoch != world->handle_epoch) return OMEGA_WORLD_ERR_STALE_EPOCH;
436 if (handle->object_id >= OMEGA_WORLD_MAX_CODE_ENTRIES) return OMEGA_WORLD_ERR_INVALID_ARG;
437
438 OmegaCodeEntry *entry = &world->code_entries[handle->object_id];
439 if (!entry->active) return OMEGA_WORLD_ERR_NOT_FOUND;
440 if (handle->object_generation != entry->generation) return OMEGA_WORLD_ERR_STALE_GEN;
441 if (handle->object_type != OMEGA_OBJ_CODE) return OMEGA_WORLD_ERR_INVALID_ARG;
442
443 entry->active = false;
444 if (entry->generation != UINT32_MAX) entry->generation++;
445 if (world->code_count > 0) world->code_count--;
446 if (code_referenced_in_flight(world, handle->object_id)) {
447 entry->retiring = true;
448 return OMEGA_WORLD_OK;
449 }
450 return release_code_memory(world, entry);
451}
452
453int omega_world_resolve_code(OmegaAcceleratorWorld *world,
454 const OmegaHandle *handle,
455 const void **out_cpu,
456 uint64_t *out_gpu_va,
457 size_t *out_size) {
458 if (!world || !world->initialized || !handle) return OMEGA_WORLD_ERR_INVALID_ARG;
459 if (handle->world_epoch != world->handle_epoch) return OMEGA_WORLD_ERR_STALE_EPOCH;
460 if (handle->object_id >= OMEGA_WORLD_MAX_CODE_ENTRIES) return OMEGA_WORLD_ERR_INVALID_ARG;
461
462 OmegaCodeEntry *entry = &world->code_entries[handle->object_id];
463 if (!entry->active) return OMEGA_WORLD_ERR_NOT_FOUND;
464 if (handle->object_generation != entry->generation) return OMEGA_WORLD_ERR_STALE_GEN;
465 if (handle->object_type != OMEGA_OBJ_CODE) return OMEGA_WORLD_ERR_INVALID_ARG;
466 if (!(handle->permissions & OMEGA_PERM_EXECUTE)) return OMEGA_WORLD_ERR_PERM_DENIED;
467
468 if (out_cpu) *out_cpu = entry->cpu_addr;
469 if (out_gpu_va) *out_gpu_va = entry->gpu_va;
470 if (out_size) *out_size = entry->size_bytes;
471 return OMEGA_WORLD_OK;
472}
473
474int omega_world_validate_handle(const OmegaAcceleratorWorld *world,
475 const OmegaHandle *handle,
476 uint32_t expected_type,
477 uint32_t required_perms) {
478 if (!world || !world->initialized || !handle) return OMEGA_WORLD_ERR_INVALID_ARG;
479 if (handle->world_epoch != world->handle_epoch) return OMEGA_WORLD_ERR_STALE_EPOCH;
480 if (handle->object_type != expected_type) return OMEGA_WORLD_ERR_INVALID_ARG;
481
482 if (expected_type == OMEGA_OBJ_BUFFER) {
483 if (handle->object_id >= OMEGA_WORLD_MAX_BUFFERS) return OMEGA_WORLD_ERR_INVALID_ARG;
484 const OmegaBufferEntry *entry = &world->buffers[handle->object_id];
485 if (!entry->active) return OMEGA_WORLD_ERR_NOT_FOUND;
486 if (handle->object_generation != entry->generation) return OMEGA_WORLD_ERR_STALE_GEN;
487 if ((entry->permissions & required_perms) != required_perms) return OMEGA_WORLD_ERR_PERM_DENIED;
488 } else if (expected_type == OMEGA_OBJ_CODE) {
489 if (handle->object_id >= OMEGA_WORLD_MAX_CODE_ENTRIES) return OMEGA_WORLD_ERR_INVALID_ARG;
490 const OmegaCodeEntry *entry = &world->code_entries[handle->object_id];
491 if (!entry->active) return OMEGA_WORLD_ERR_NOT_FOUND;
492 if (handle->object_generation != entry->generation) return OMEGA_WORLD_ERR_STALE_GEN;
493 } else {
494 return OMEGA_WORLD_ERR_INVALID_ARG;
495 }
496
497 if ((handle->permissions & required_perms) != required_perms) return OMEGA_WORLD_ERR_PERM_DENIED;
498 return OMEGA_WORLD_OK;
499}
500
501int omega_world_scratch_acquire(OmegaAcceleratorWorld *world,
502 size_t size_bytes,
503 void **out_cpu,
504 uint64_t *out_gpu_va,
505 size_t *out_offset) {
506 if (!world || !world->initialized || size_bytes == 0 || !out_cpu || !out_gpu_va) {
507 return OMEGA_WORLD_ERR_INVALID_ARG;
508 }
509 if (size_bytes > SIZE_MAX - 255ULL) return OMEGA_WORLD_ERR_BOUNDS;
510 size_t aligned = (size_bytes + 255ULL) & ~255ULL;
511 if (world->scratch.allocated_bytes > world->scratch.capacity_bytes ||
512 aligned > world->scratch.capacity_bytes - world->scratch.allocated_bytes) {
513 return OMEGA_WORLD_ERR_NO_MEM;
514 }
515
516 size_t off = world->scratch.allocated_bytes;
517 *out_cpu = (uint8_t *)world->scratch.mem.cpu + off;
518 *out_gpu_va = world->scratch.mem.va + off;
519 if (out_offset) *out_offset = off;
520
521 world->scratch.allocated_bytes += aligned;
522 return OMEGA_WORLD_OK;
523}
524
525void omega_world_scratch_reset(OmegaAcceleratorWorld *world) {
526 if (world) {
527 world->scratch.allocated_bytes = 0;
528 }
529}
530
531int omega_world_discover_ring_capacity(OmegaAcceleratorWorld *world,
532 uint32_t *out_capacity) {
533 if (!world || !world->initialized || !out_capacity) return OMEGA_WORLD_ERR_INVALID_ARG;
534 *out_capacity = world->ring_capacity;
535 return OMEGA_WORLD_OK;
536}
537
538int omega_world_recover_channel_fault(OmegaAcceleratorWorld *world) {
539 if (!world || !world->initialized) return OMEGA_WORLD_ERR_INVALID_ARG;
540
541 Nvrm *rm = &world->m16.rm;
542 /* Reconstruct channel within existing VAS and device root */
543 if (nvrm_channel_destroy(rm) != 0) {
544 world->faulted = true;
545 world->channel_active = false;
546 return OMEGA_WORLD_ERR_HARDWARE;
547 }
548
549 if (nvrm_channel(rm) != 0) {
550 world->faulted = true;
551 world->channel_active = false;
552 return OMEGA_WORLD_ERR_HARDWARE;
553 }
554
555 if (world->channel_generation == UINT32_MAX || world->channel_reconstructions == UINT64_MAX) {
556 world->faulted = true;
557 world->channel_active = false;
558 return OMEGA_WORLD_ERR_FAULT;
559 }
560 world->channel_generation++;
561 world->channel_reconstructions++;
562 world->channel_active = true;
563
564 /* Submissions queued on the torn-down channel will never complete and
565 * are never committed. */
566 for (uint32_t i = 0; i < world->in_flight_count; i++) {
567 OmegaWorldSubmission *s = &world->in_flight[i];
568 if (!s->committed) {
569 if (world->abandoned_dispatches == UINT64_MAX) {
570 world->faulted = true;
571 return OMEGA_WORLD_ERR_FAULT;
572 }
573 world->abandoned_dispatches++;
574 }
575 if (s->output_cpu_protected && s->c_object_id < OMEGA_WORLD_MAX_BUFFERS) {
576 OmegaBufferEntry *c = &world->buffers[s->c_object_id];
577 if (c->mem.handle && mprotect(c->cpu_addr, c->mem.size, PROT_READ | PROT_WRITE) != 0) {
578 world->faulted = true;
579 return OMEGA_WORLD_ERR_FAULT;
580 }
581 }
582 }
583 world->in_flight_count = 0;
584 release_unreferenced_retiring(world);
585 if (world->faulted && rm->faulted) return OMEGA_WORLD_ERR_FAULT;
586 world->faulted = false;
587 return OMEGA_WORLD_OK;
588}
589
590int omega_world_update_digest(OmegaAcceleratorWorld *world,
591 const uint8_t *semantic_id,
592 const uint8_t *realization_id,
593 const uint8_t *code_digest,
594 const uint8_t *input_bindings_digest,
595 const uint32_t *obj_id_gen_pairs,
596 uint32_t obj_count,
597 uint32_t capability_scope,
598 uint32_t completion_val,
599 const uint8_t *result_digest,
600 uint32_t outcome_code) {
601 if (!world || !world->initialized) return OMEGA_WORLD_ERR_INVALID_ARG;
602
603 /* Canonical binary serialization per spec section 6 */
604 uint8_t buf[512];
605 size_t off = 0;
606
607 static const uint8_t DOMAIN_TAG[32] = "OMEGA_WORLD_ROLLING_STATE_V1";
608 memcpy(&buf[off], DOMAIN_TAG, 32); off += 32;
609 memcpy(&buf[off], world->rolling_state_digest, 32); off += 32;
610
611 uint32_t epoch = world->current_epoch;
612 memcpy(&buf[off], &epoch, 4); off += 4;
613
614 if (world->sequence_number == UINT64_MAX) return OMEGA_WORLD_ERR_FAULT;
615 uint64_t seq = world->sequence_number + 1;
616 memcpy(&buf[off], &seq, 8); off += 8;
617
618 uint8_t zero32[32] = {0};
619 memcpy(&buf[off], semantic_id ? semantic_id : zero32, 32); off += 32;
620 memcpy(&buf[off], realization_id ? realization_id : zero32, 32); off += 32;
621 memcpy(&buf[off], code_digest ? code_digest : zero32, 32); off += 32;
622 memcpy(&buf[off], input_bindings_digest ? input_bindings_digest : zero32, 32); off += 32;
623
624 uint32_t cnt = obj_count;
625 memcpy(&buf[off], &cnt, 4); off += 4;
626
627 size_t pairs_bytes = obj_count * 8;
628 if (pairs_bytes > 64) pairs_bytes = 64;
629 if (obj_id_gen_pairs && pairs_bytes > 0) {
630 memcpy(&buf[off], obj_id_gen_pairs, pairs_bytes);
631 } else {
632 memset(&buf[off], 0, pairs_bytes);
633 }
634 off += pairs_bytes;
635
636 memcpy(&buf[off], &capability_scope, 4); off += 4;
637 memcpy(&buf[off], &completion_val, 4); off += 4;
638 memcpy(&buf[off], result_digest ? result_digest : zero32, 32); off += 32;
639 memcpy(&buf[off], &outcome_code, 4); off += 4;
640
641 sha256_hash(buf, off, world->rolling_state_digest);
642 world->sequence_number = seq;
643 return OMEGA_WORLD_OK;
644}
645
646/* Commit a successful physical dispatch to the world trace. The semantic
647 * words and handle generations are fixed-width; host addresses and time are
648 * deliberately absent from the serialized state. */
649static int record_completed_dispatch(OmegaAcceleratorWorld *world,
650 const uint32_t semantic_words[5],
651 const OmegaHandle *code,
652 const OmegaHandle *a,
653 const OmegaHandle *b,
654 const OmegaHandle *c,
655 const void *a_cpu, size_t a_bytes,
656 const void *b_cpu, size_t b_bytes,
657 const void *c_cpu, size_t c_bytes,
658 uint32_t completion,
659 const uint8_t *submitted_semantic_id,
660 const uint8_t *submitted_input_digest) {
661 if (world->total_dispatches == UINT64_MAX) return OMEGA_WORLD_ERR_FAULT;
662 uint8_t semantic_id[32], a_digest[32], b_digest[32];
663 uint8_t bindings[64], bindings_digest[32], result_digest[32];
664 uint32_t pairs[8] = {
665 code->object_id, code->object_generation,
666 a->object_id, a->object_generation,
667 b->object_id, b->object_generation,
668 c->object_id, c->object_generation
669 };
670 const OmegaCodeEntry *entry = &world->code_entries[code->object_id];
671 if (submitted_semantic_id) memcpy(semantic_id, submitted_semantic_id, 32);
672 else sha256_hash((const uint8_t *)semantic_words, 5 * sizeof(uint32_t), semantic_id);
673 if (submitted_input_digest) memcpy(bindings_digest, submitted_input_digest, 32);
674 else {
675 sha256_hash((const uint8_t *)a_cpu, a_bytes, a_digest);
676 sha256_hash((const uint8_t *)b_cpu, b_bytes, b_digest);
677 memcpy(bindings, a_digest, 32);
678 memcpy(bindings + 32, b_digest, 32);
679 sha256_hash(bindings, sizeof(bindings), bindings_digest);
680 }
681 sha256_hash((const uint8_t *)c_cpu, c_bytes, result_digest);
682 return omega_world_update_digest(world, semantic_id, entry->realization_id,
683 entry->code_digest, bindings_digest,
684 pairs, 4, code->permissions | a->permissions |
685 b->permissions | c->permissions,
686 completion, result_digest, 0);
687}
688
689int omega_world_submit(OmegaAcceleratorWorld *world,
690 const NvrmMem *pb_mem,
691 uint32_t off_bytes,
692 uint32_t nwords,
693 const OmegaWorldSubmission *submission) {
694 if (!world || !world->initialized || !pb_mem || !submission) {
695 return OMEGA_WORLD_ERR_INVALID_ARG;
696 }
697 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
698 if (!world->channel_active) return OMEGA_WORLD_ERR_HARDWARE;
699 if (world->in_flight_count >= OMEGA_WORLD_MAX_IN_FLIGHT) {
700 return OMEGA_WORLD_ERR_NO_MEM;
701 }
702 if (submission->c_object_id >= OMEGA_WORLD_MAX_BUFFERS ||
703 submission->a_object_id >= OMEGA_WORLD_MAX_BUFFERS ||
704 submission->b_object_id >= OMEGA_WORLD_MAX_BUFFERS ||
705 submission->code_object_id >= OMEGA_WORLD_MAX_CODE_ENTRIES)
706 return OMEGA_WORLD_ERR_INVALID_ARG;
707 const OmegaBufferEntry *a = &world->buffers[submission->a_object_id];
708 const OmegaBufferEntry *b = &world->buffers[submission->b_object_id];
709 const OmegaBufferEntry *c = &world->buffers[submission->c_object_id];
710 const OmegaCodeEntry *code = &world->code_entries[submission->code_object_id];
711 if (!a->active || !b->active || !c->active || !code->active ||
712 a->generation != submission->a_generation ||
713 b->generation != submission->b_generation ||
714 c->generation != submission->c_generation ||
715 code->generation != submission->code_generation ||
716 submission->a_bytes > a->size_bytes || submission->b_bytes > b->size_bytes ||
717 submission->c_bytes > c->size_bytes || !(c->permissions & OMEGA_PERM_WRITE))
718 return OMEGA_WORLD_ERR_INVALID_ARG;
719 /* A later submission cannot claim an input digest for an earlier output
720 * that the GPU may not have written yet, nor reuse that output as a writer. */
721 for (uint32_t i = 0; i < world->in_flight_count; i++) {
722 const OmegaWorldSubmission *prior = &world->in_flight[i];
723 if (prior->c_bytes &&
724 ((submission->c_bytes && prior->c_object_id == submission->c_object_id) ||
725 (submission->a_bytes && prior->c_object_id == submission->a_object_id) ||
726 (submission->b_bytes && prior->c_object_id == submission->b_object_id)))
727 return OMEGA_WORLD_ERR_FAULT;
728 }
729 /* One payload sequence for the whole world: the pushbuffer's terminal
730 * release (built by the caller) must carry a payload strictly after every
731 * payload already issued, synchronous or batched. */
732 if (!(submission->completion_val != world->completion.last_payload &&
733 !payload_reached(submission->completion_val, world->completion.last_payload))) {
734 return OMEGA_WORLD_ERR_INVALID_ARG;
735 }
736 if (submission->c_bytes && mprotect(c->cpu_addr, c->mem.size, PROT_READ) != 0) {
737 world->faulted = true;
738 return OMEGA_WORLD_ERR_FAULT;
739 }
740 if (nvrm_enqueue(&world->m16.rm, pb_mem, off_bytes, nwords) != 0) {
741 if (submission->c_bytes && mprotect(c->cpu_addr, c->mem.size, PROT_READ | PROT_WRITE) != 0)
742 world->faulted = true;
743 return OMEGA_WORLD_ERR_HARDWARE;
744 }
745 world->completion.last_payload = submission->completion_val;
746 world->in_flight[world->in_flight_count] = *submission;
747 world->in_flight[world->in_flight_count].gp_seq = world->m16.rm.put;
748 world->in_flight[world->in_flight_count].channel_generation = world->channel_generation;
749 world->in_flight[world->in_flight_count].output_cpu_protected = submission->c_bytes != 0;
750 world->in_flight[world->in_flight_count].committed = false;
751 sha256_hash((const uint8_t *)submission->semantic_words,
752 sizeof submission->semantic_words,
753 world->in_flight[world->in_flight_count].semantic_id);
754 uint8_t a_digest[32], b_digest[32], bindings[64];
755 sha256_hash(a->cpu_addr, submission->a_bytes, a_digest);
756 sha256_hash(b->cpu_addr, submission->b_bytes, b_digest);
757 memcpy(bindings, a_digest, 32);
758 memcpy(bindings + 32, b_digest, 32);
759 sha256_hash(bindings, sizeof bindings,
760 world->in_flight[world->in_flight_count].input_bindings_digest);
761 world->in_flight_count++;
762 return OMEGA_WORLD_OK;
763}
764
765int omega_world_ring(OmegaAcceleratorWorld *world) {
766 if (!world || !world->initialized) return OMEGA_WORLD_ERR_INVALID_ARG;
767 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
768 if (!world->channel_active) return OMEGA_WORLD_ERR_HARDWARE;
769 nvrm_ring(&world->m16.rm);
770 return OMEGA_WORLD_OK;
771}
772
773int omega_world_drain(OmegaAcceleratorWorld *world,
774 uint32_t upto_payload,
775 int timeout_ms) {
776 if (!world || !world->initialized) return OMEGA_WORLD_ERR_INVALID_ARG;
777 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
778 if (!world->channel_active) return OMEGA_WORLD_ERR_HARDWARE;
779 if (timeout_ms < 0) return OMEGA_WORLD_ERR_INVALID_ARG;
780
781 /* Caller-supplied budget (API contract, gates pass 0..10000 ms): hard total = timeout_ms
782 * (0 becomes 1 ms because the primitive maps 0 to its 600 s default), stall = total, so the
783 * caller keeps its full budget as before.
784 * Waits on the marker only: pushbuffers built by callers (omega_world_gates.c) release
785 * the marker without a flush or marker2. */
786 uint32_t drain_total = timeout_ms ? (uint32_t)timeout_ms : 1u;
787 if (world_wait_seq(__func__, "marker", world->completion.cpu_marker, upto_payload,
788 drain_total, &drain_total) != 0) {
789 world->faulted = true;
790 return OMEGA_WORLD_ERR_HARDWARE;
791 }
792
793 /* Pass 1: validate every completed submission before any side effect, so
794 * a failure never leaves a partially committed queue behind. */
795 for (uint32_t i = 0; i < world->in_flight_count; i++) {
796 const OmegaWorldSubmission *s = &world->in_flight[i];
797 if (!payload_reached(s->completion_val, upto_payload)) continue;
798 if (s->channel_generation != world->channel_generation) {
799 world->faulted = true;
800 return OMEGA_WORLD_ERR_FAULT;
801 }
802 if (s->code_object_id >= OMEGA_WORLD_MAX_CODE_ENTRIES ||
803 s->a_object_id >= OMEGA_WORLD_MAX_BUFFERS ||
804 s->b_object_id >= OMEGA_WORLD_MAX_BUFFERS ||
805 s->c_object_id >= OMEGA_WORLD_MAX_BUFFERS) {
806 world->faulted = true;
807 return OMEGA_WORLD_ERR_INVALID_ARG;
808 }
809 }
810
811 /* Pass 2: each completed submission is committed exactly once, or
812 * abandoned if an object it bound was revoked while it was in flight. */
813 int committed = 0;
814 uint32_t keep = 0;
815 uint32_t retire_to = world->m16.rm.retired;
816 for (uint32_t i = 0; i < world->in_flight_count; i++) {
817 const OmegaWorldSubmission s = world->in_flight[i];
818 if (!payload_reached(s.completion_val, upto_payload)) {
819 world->in_flight[keep++] = s;
820 continue;
821 }
822 /* Retire by GPFIFO queue sequence, never by completion payload. */
823 if ((int32_t)(s.gp_seq - retire_to) > 0) retire_to = s.gp_seq;
824
825 const OmegaBufferEntry *ab = &world->buffers[s.a_object_id];
826 const OmegaBufferEntry *bb = &world->buffers[s.b_object_id];
827 const OmegaBufferEntry *cb = &world->buffers[s.c_object_id];
828 const OmegaCodeEntry *ce = &world->code_entries[s.code_object_id];
829 if (!ab->active || ab->generation != s.a_generation ||
830 !bb->active || bb->generation != s.b_generation ||
831 !cb->active || cb->generation != s.c_generation ||
832 !ce->active || ce->generation != s.code_generation) {
833 if (world->abandoned_dispatches == UINT64_MAX) {
834 world->faulted = true;
835 return OMEGA_WORLD_ERR_FAULT;
836 }
837 world->abandoned_dispatches++;
838 if (s.output_cpu_protected && cb->active &&
839 mprotect(cb->cpu_addr, cb->mem.size, PROT_READ | PROT_WRITE) != 0) {
840 world->faulted = true;
841 return OMEGA_WORLD_ERR_FAULT;
842 }
843 continue;
844 }
845
846 OmegaHandle ch = {
847 .world_epoch = world->handle_epoch,
848 .object_id = s.code_object_id,
849 .object_generation = s.code_generation,
850 .object_type = OMEGA_OBJ_CODE,
851 .permissions = s.code_permissions & OMEGA_PERM_EXECUTE
852 };
853 OmegaHandle ah = {
854 .world_epoch = world->handle_epoch,
855 .object_id = s.a_object_id,
856 .object_generation = s.a_generation,
857 .object_type = OMEGA_OBJ_BUFFER,
858 .permissions = s.a_permissions
859 };
860 OmegaHandle bh = {
861 .world_epoch = world->handle_epoch,
862 .object_id = s.b_object_id,
863 .object_generation = s.b_generation,
864 .object_type = OMEGA_OBJ_BUFFER,
865 .permissions = s.b_permissions
866 };
867 OmegaHandle cwh = {
868 .world_epoch = world->handle_epoch,
869 .object_id = s.c_object_id,
870 .object_generation = s.c_generation,
871 .object_type = OMEGA_OBJ_BUFFER,
872 .permissions = s.c_permissions
873 };
874
875 if (record_completed_dispatch(world, s.semantic_words, &ch, &ah, &bh, &cwh,
876 ab->cpu_addr, s.a_bytes,
877 bb->cpu_addr, s.b_bytes,
878 cb->cpu_addr, s.c_bytes,
879 s.completion_val, s.semantic_id,
880 s.input_bindings_digest) != OMEGA_WORLD_OK) {
881 /* Keep this and every later submission uncommitted; drop only
882 * those already committed above. */
883 for (uint32_t j = i; j < world->in_flight_count; j++) {
884 world->in_flight[keep++] = world->in_flight[j];
885 }
886 world->in_flight_count = keep;
887 world->faulted = true;
888 return OMEGA_WORLD_ERR_FAULT;
889 }
890 world->in_flight[i].committed = true;
891 world->total_dispatches++;
892 if (s.output_cpu_protected &&
893 mprotect(cb->cpu_addr, cb->mem.size, PROT_READ | PROT_WRITE) != 0) {
894 world->faulted = true;
895 return OMEGA_WORLD_ERR_FAULT;
896 }
897 committed++;
898 }
899 nvrm_retire(&world->m16.rm, retire_to);
900 world->in_flight_count = keep;
901 release_unreferenced_retiring(world);
902 return committed;
903}
904
905int omega_world_dispatch_vector(OmegaAcceleratorWorld *world,
906 const OmegaHandle *code_handle,
907 const OmegaHandle *h_a,
908 const OmegaHandle *h_b,
909 const OmegaHandle *h_c,
910 uint32_t n,
911 uint32_t *out_completion_code) {
912 if (!world || !world->initialized || !code_handle || !h_a || !h_b || !h_c || n == 0) {
913 return OMEGA_WORLD_ERR_INVALID_ARG;
914 }
915 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
916 if (!world->channel_active) return OMEGA_WORLD_ERR_HARDWARE;
917
918 /* Resolve code and buffers with internal bounds and permission authority */
919 uint64_t code_va = 0;
920 size_t code_size = 0;
921 if (omega_world_resolve_code(world, code_handle, NULL, &code_va, &code_size) != 0) {
922 return OMEGA_WORLD_ERR_FAULT;
923 }
924
925 size_t req_bytes;
926 if (n > UINT32_MAX - 63 ||
927 __builtin_mul_overflow((size_t)n, sizeof(uint32_t), &req_bytes))
928 return OMEGA_WORLD_ERR_BOUNDS;
929 void *a_cpu = NULL, *b_cpu = NULL, *c_cpu = NULL;
930 uint64_t a_va = 0, b_va = 0, c_va = 0;
931
932 if (omega_world_resolve_buffer(world, h_a, OMEGA_PERM_READ, 0, req_bytes, &a_cpu, &a_va) != 0) {
933 return OMEGA_WORLD_ERR_PERM_DENIED;
934 }
935 if (omega_world_resolve_buffer(world, h_b, OMEGA_PERM_READ, 0, req_bytes, &b_cpu, &b_va) != 0) {
936 return OMEGA_WORLD_ERR_PERM_DENIED;
937 }
938 if (omega_world_resolve_buffer(world, h_c, OMEGA_PERM_WRITE, 0, req_bytes, &c_cpu, &c_va) != 0) {
939 return OMEGA_WORLD_ERR_PERM_DENIED;
940 }
941
942 /* Acquire scratch space */
943 void *cbank_cpu = NULL, *qmd0_cpu = NULL, *qmd1_cpu = NULL, *sem_cpu = NULL;
944 uint64_t cbank_va = 0, qmd0_va = 0, qmd1_va = 0, sem_va = 0;
945 size_t dummy_off = 0;
946
947 if (omega_world_scratch_acquire(world, 0x1000, &cbank_cpu, &cbank_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
948 if (omega_world_scratch_acquire(world, 0x1000, &qmd0_cpu, &qmd0_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
949 if (omega_world_scratch_acquire(world, 0x1000, &qmd1_cpu, &qmd1_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
950 if (omega_world_scratch_acquire(world, 0x1000, &sem_cpu, &sem_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
951
952 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
953 omega_blackwell_build_cbank_driver(cbank_data, cbank_va);
954
955 uint32_t cbank_args[OMEGA_BW_CBANK_ARGS_WORDS];
956 omega_blackwell_build_cbank_args(cbank_args, a_va, b_va, c_va, n);
957
958 memcpy(cbank_cpu, cbank_data, sizeof(cbank_data));
959 memcpy((uint8_t *)cbank_cpu + 0x380, cbank_args, sizeof(cbank_args));
960
961 OmegaBlackwellQmdConfig qmd_cfg = {
962 .code_va = code_va,
963 .cbank_va = cbank_va,
964 .scratch_va = cbank_va + 0x2000,
965 .sem_va = sem_va,
966 .qmd0_va = qmd0_va,
967 .qmd1_va = qmd1_va,
968 .num_elements = n,
969 .threads_per_block = 64,
970 .grid_width = (n + 63) / 64
971 };
972 if (qmd_cfg.grid_width == 0) qmd_cfg.grid_width = 1;
973
974 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS];
975 uint32_t qmd1_words[OMEGA_BW_QMD_WORDS];
976 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va);
977 omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg);
978
979 memcpy(qmd0_cpu, qmd0_words, sizeof(qmd0_words));
980 memcpy(qmd1_cpu, qmd1_words, sizeof(qmd1_words));
981
982 volatile uint32_t *hsem = (volatile uint32_t *)sem_cpu;
983 *hsem = 0;
984 /* The completion marker is monotonic and never written by the CPU after
985 * init; this dispatch takes the next payload in the world's sequence. */
986 uint32_t payload = ++world->completion.last_payload;
987
988 /* Assemble pushbuffer */
989 uint32_t pb[1024];
990 size_t pb_len = 0;
991
992 memcpy(&pb[pb_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS));
993 pb_len += sizeof(WORLD_SETUP_WORDS) / 4;
994
995 /* Cbank driver upload */
996 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
997 pb[pb_len++] = (uint32_t)(cbank_va >> 32);
998 pb[pb_len++] = (uint32_t)cbank_va;
999 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
1000 pb[pb_len++] = 0x00000380;
1001 pb[pb_len++] = 0x00000001;
1002 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
1003 pb[pb_len++] = 0x00000041;
1004 pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1005 memcpy(&pb[pb_len], cbank_data, 224 * 4);
1006 pb_len += 224;
1007
1008 /* Kernel args upload */
1009 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
1010 pb[pb_len++] = (uint32_t)((cbank_va + 0x380) >> 32);
1011 pb[pb_len++] = (uint32_t)(cbank_va + 0x380);
1012 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
1013 pb[pb_len++] = 0x0000001c;
1014 pb[pb_len++] = 0x00000001;
1015 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
1016 pb[pb_len++] = 0x00000041;
1017 pb[pb_len++] = (7 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1018 memcpy(&pb[pb_len], cbank_args, 7 * 4);
1019 pb_len += 7;
1020
1021 /* Inline QMD0 */
1022 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
1023 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff);
1024 pb[pb_len++] = (uint32_t)(qmd0_va >> 8);
1025 memcpy(&pb[pb_len], qmd0_words, 96 * 4);
1026 pb_len += 96;
1027
1028 /* Intermediate semaphore upload */
1029 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
1030 pb[pb_len++] = (uint32_t)(sem_va >> 32);
1031 pb[pb_len++] = (uint32_t)sem_va;
1032 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
1033 pb[pb_len++] = 0x00000004;
1034 pb[pb_len++] = 0x00000001;
1035 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
1036 pb[pb_len++] = 0x00000041;
1037 pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1038 pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
1039
1040 /* Inline QMD1 */
1041 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
1042 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff);
1043 pb[pb_len++] = (uint32_t)(qmd1_va >> 8);
1044 memcpy(&pb[pb_len], qmd1_words, 96 * 4);
1045 pb_len += 96;
1046
1047 /* Subchannel 0 completion release (WFI) */
1048 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5);
1049 pb[pb_len++] = (uint32_t)world->completion.gpu_va;
1050 pb[pb_len++] = (uint32_t)(world->completion.gpu_va >> 32);
1051 pb[pb_len++] = payload;
1052 pb[pb_len++] = 0;
1053 pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI
1054
1055 /* C3 tail, copied from omega_blackwell_engine.c:461-472 (MEM_OP_A..D =
1056 * 0x28..0x34, L2_FLUSH_DIRTY 0x10 in bits 31:27, clc96f.h:36-73): flush the
1057 * L2, then a second WFI release of the same payload at marker page + 0x10.
1058 * Dispatch waits for this one before it reads any output. */
1059 pb[pb_len++] = nvrm_mthd(0, 0x0028, 4);
1060 pb[pb_len++] = 0;
1061 pb[pb_len++] = 0;
1062 pb[pb_len++] = 0;
1063 pb[pb_len++] = 0x10u << 27;
1064 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5);
1065 pb[pb_len++] = (uint32_t)world->completion.gpu_va2;
1066 pb[pb_len++] = (uint32_t)(world->completion.gpu_va2 >> 32);
1067 pb[pb_len++] = payload;
1068 pb[pb_len++] = 0;
1069 pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI
1070
1071 /* Submit to persistent GPFIFO ring */
1072 if (m16_native_submit_methods(&world->m16, pb, pb_len) != 0) {
1073 omega_world_scratch_reset(world);
1074 return OMEGA_WORLD_ERR_HARDWARE;
1075 }
1076
1077 if (world_wait_dispatch(world, __func__, payload) != 0) {
1078 world->faulted = true;
1079 omega_world_scratch_reset(world);
1080 return OMEGA_WORLD_ERR_HARDWARE;
1081 }
1082
1083 nvrm_retire(&world->m16.rm, world->m16.rm.put);
1084
1085 const uint32_t semantic_words[5] = {1, n, 0, 0, 0};
1086 if (record_completed_dispatch(world, semantic_words, code_handle,
1087 h_a, h_b, h_c, a_cpu, req_bytes,
1088 b_cpu, req_bytes, c_cpu, req_bytes,
1089 payload, NULL, NULL) != OMEGA_WORLD_OK) {
1090 omega_world_scratch_reset(world);
1091 return OMEGA_WORLD_ERR_FAULT;
1092 }
1093
1094 if (out_completion_code) {
1095 *out_completion_code = payload;
1096 }
1097 world->total_dispatches++;
1098
1099 /* Reset scratch arena for next dispatch */
1100 omega_world_scratch_reset(world);
1101 return OMEGA_WORLD_OK;
1102}
1103
1104int omega_world_dispatch_matmul(OmegaAcceleratorWorld *world,
1105 const OmegaMatMulSpec *spec,
1106 const OmegaHandle *code_handle,
1107 const OmegaHandle *h_a,
1108 const OmegaHandle *h_b,
1109 const OmegaHandle *h_c,
1110 uint32_t *out_completion_code) {
1111 if (!world || !world->initialized || !spec || !code_handle || !h_a || !h_b || !h_c) {
1112 return OMEGA_WORLD_ERR_INVALID_ARG;
1113 }
1114 if (world->faulted) return OMEGA_WORLD_ERR_FAULTED;
1115 if (!world->channel_active) return OMEGA_WORLD_ERR_HARDWARE;
1116
1117 uint64_t code_va = 0;
1118 size_t code_size = 0;
1119 if (omega_world_resolve_code(world, code_handle, NULL, &code_va, &code_size) != 0) {
1120 return OMEGA_WORLD_ERR_FAULT;
1121 }
1122
1123 size_t elem_size = (spec->precision == OMEGA_MATMUL_PRECISION_INT32) ? 4 : 2;
1124 size_t out_elem_size = (spec->precision == OMEGA_MATMUL_PRECISION_INT32) ? 4 : 4; /* FP32 accum */
1125
1126 size_t req_a, req_b, req_c;
1127 if (__builtin_mul_overflow((size_t)spec->m, (size_t)spec->k, &req_a) ||
1128 __builtin_mul_overflow(req_a, elem_size, &req_a) ||
1129 __builtin_mul_overflow((size_t)spec->k, (size_t)spec->n, &req_b) ||
1130 __builtin_mul_overflow(req_b, elem_size, &req_b) ||
1131 __builtin_mul_overflow((size_t)spec->m, (size_t)spec->n, &req_c) ||
1132 __builtin_mul_overflow(req_c, out_elem_size, &req_c))
1133 return OMEGA_WORLD_ERR_BOUNDS;
1134 if (spec->precision == OMEGA_MATMUL_PRECISION_INT32 &&
1135 (spec->n > UINT32_MAX - 15 || spec->m > UINT32_MAX - 15))
1136 return OMEGA_WORLD_ERR_BOUNDS;
1137
1138 void *a_cpu = NULL, *b_cpu = NULL, *c_cpu = NULL;
1139 uint64_t a_va = 0, b_va = 0, c_va = 0;
1140
1141 if (omega_world_resolve_buffer(world, h_a, OMEGA_PERM_READ, 0, req_a, &a_cpu, &a_va) != 0) {
1142 return OMEGA_WORLD_ERR_PERM_DENIED;
1143 }
1144 if (omega_world_resolve_buffer(world, h_b, OMEGA_PERM_READ, 0, req_b, &b_cpu, &b_va) != 0) {
1145 return OMEGA_WORLD_ERR_PERM_DENIED;
1146 }
1147 if (omega_world_resolve_buffer(world, h_c, OMEGA_PERM_WRITE, 0, req_c, &c_cpu, &c_va) != 0) {
1148 return OMEGA_WORLD_ERR_PERM_DENIED;
1149 }
1150
1151 void *cbank_cpu = NULL, *qmd0_cpu = NULL, *qmd1_cpu = NULL, *sem_cpu = NULL, *kernel_scratch_cpu = NULL;
1152 uint64_t cbank_va = 0, qmd0_va = 0, qmd1_va = 0, sem_va = 0, kernel_scratch_va = 0;
1153 size_t dummy_off = 0;
1154
1155 if (omega_world_scratch_acquire(world, 0x1000, &cbank_cpu, &cbank_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
1156 if (omega_world_scratch_acquire(world, 0x1000, &qmd0_cpu, &qmd0_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
1157 if (omega_world_scratch_acquire(world, 0x1000, &qmd1_cpu, &qmd1_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
1158 if (omega_world_scratch_acquire(world, 0x1000, &sem_cpu, &sem_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
1159 if (omega_world_scratch_acquire(world, 0x4000, &kernel_scratch_cpu, &kernel_scratch_va, &dummy_off) != 0) return OMEGA_WORLD_ERR_NO_MEM;
1160
1161 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
1162 uint32_t threads_x, threads_y, grid_x, grid_y, threads_per_block, gpr_count;
1163 if (spec->precision == OMEGA_MATMUL_PRECISION_INT32) {
1164 threads_x = 16;
1165 threads_y = 16;
1166 grid_x = (spec->n + threads_x - 1) / threads_x;
1167 grid_y = (spec->m + threads_y - 1) / threads_y;
1168 threads_per_block = threads_x * threads_y;
1169 gpr_count = 32;
1170 } else {
1171 threads_x = 32;
1172 threads_y = 1;
1173 grid_x = spec->n / 8;
1174 grid_y = spec->m / 16;
1175 threads_per_block = 32;
1176 gpr_count = 64;
1177 }
1178 if (grid_x == 0) grid_x = 1;
1179 if (grid_y == 0) grid_y = 1;
1180 if (grid_y > UINT32_MAX / grid_x) {
1181 omega_world_scratch_reset(world);
1182 return OMEGA_WORLD_ERR_BOUNDS;
1183 }
1184
1185 omega_blackwell_build_cbank_driver_2d(cbank_data, cbank_va, threads_x, threads_y, grid_x, grid_y);
1186
1187 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS];
1188 omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, spec->m, spec->k, spec->n);
1189
1190 memcpy(cbank_cpu, cbank_data, sizeof(cbank_data));
1191 memcpy((uint8_t *)cbank_cpu + 0x380, cbank_args, sizeof(cbank_args));
1192
1193 OmegaBlackwellQmdConfig qmd_cfg = {
1194 .code_va = code_va,
1195 .cbank_va = cbank_va,
1196 .scratch_va = kernel_scratch_va,
1197 .sem_va = sem_va,
1198 .qmd0_va = qmd0_va,
1199 .qmd1_va = qmd1_va,
1200 .threads_x = threads_x,
1201 .threads_y = threads_y,
1202 .grid_x = grid_x,
1203 .grid_y = grid_y,
1204 .threads_per_block = threads_per_block,
1205 .grid_width = grid_x * grid_y,
1206 .num_elements = spec->m * spec->n,
1207 .gpr_count = gpr_count
1208 };
1209
1210 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS];
1211 uint32_t qmd1_words[OMEGA_BW_QMD_WORDS];
1212 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va);
1213 omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg);
1214
1215 memcpy(qmd0_cpu, qmd0_words, sizeof(qmd0_words));
1216 memcpy(qmd1_cpu, qmd1_words, sizeof(qmd1_words));
1217
1218 volatile uint32_t *hsem = (volatile uint32_t *)sem_cpu;
1219 *hsem = 0;
1220 /* The completion marker is monotonic and never written by the CPU after
1221 * init; this dispatch takes the next payload in the world's sequence. */
1222 uint32_t payload = ++world->completion.last_payload;
1223
1224 /* Assemble pushbuffer */
1225 uint32_t pb[1024];
1226 size_t pb_len = 0;
1227
1228 memcpy(&pb[pb_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS));
1229 pb_len += sizeof(WORLD_SETUP_WORDS) / 4;
1230
1231 /* Cbank driver upload */
1232 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
1233 pb[pb_len++] = (uint32_t)(cbank_va >> 32);
1234 pb[pb_len++] = (uint32_t)cbank_va;
1235 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
1236 pb[pb_len++] = 0x00000380;
1237 pb[pb_len++] = 0x00000001;
1238 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
1239 pb[pb_len++] = 0x00000041;
1240 pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1241 memcpy(&pb[pb_len], cbank_data, 224 * 4);
1242 pb_len += 224;
1243
1244 /* Kernel args upload (10 words) */
1245 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
1246 pb[pb_len++] = (uint32_t)((cbank_va + 0x380) >> 32);
1247 pb[pb_len++] = (uint32_t)(cbank_va + 0x380);
1248 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
1249 pb[pb_len++] = 0x00000028;
1250 pb[pb_len++] = 0x00000001;
1251 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
1252 pb[pb_len++] = 0x00000041;
1253 pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1254 memcpy(&pb[pb_len], cbank_args, 10 * 4);
1255 pb_len += 10;
1256
1257 /* Inline QMD0 */
1258 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
1259 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff);
1260 pb[pb_len++] = (uint32_t)(qmd0_va >> 8);
1261 memcpy(&pb[pb_len], qmd0_words, 96 * 4);
1262 pb_len += 96;
1263
1264 /* Intermediate semaphore upload */
1265 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
1266 pb[pb_len++] = (uint32_t)(sem_va >> 32);
1267 pb[pb_len++] = (uint32_t)sem_va;
1268 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
1269 pb[pb_len++] = 0x00000004;
1270 pb[pb_len++] = 0x00000001;
1271 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
1272 pb[pb_len++] = 0x00000041;
1273 pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1274 pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
1275
1276 /* Inline QMD1 */
1277 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
1278 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff);
1279 pb[pb_len++] = (uint32_t)(qmd1_va >> 8);
1280 memcpy(&pb[pb_len], qmd1_words, 96 * 4);
1281 pb_len += 96;
1282
1283 /* Subchannel 0 completion release (WFI) */
1284 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5);
1285 pb[pb_len++] = (uint32_t)world->completion.gpu_va;
1286 pb[pb_len++] = (uint32_t)(world->completion.gpu_va >> 32);
1287 pb[pb_len++] = payload;
1288 pb[pb_len++] = 0;
1289 pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI
1290
1291 /* C3 tail, copied from omega_blackwell_engine.c:461-472 (MEM_OP_A..D =
1292 * 0x28..0x34, L2_FLUSH_DIRTY 0x10 in bits 31:27, clc96f.h:36-73): flush the
1293 * L2, then a second WFI release of the same payload at marker page + 0x10.
1294 * Dispatch waits for this one before it reads any output. */
1295 pb[pb_len++] = nvrm_mthd(0, 0x0028, 4);
1296 pb[pb_len++] = 0;
1297 pb[pb_len++] = 0;
1298 pb[pb_len++] = 0;
1299 pb[pb_len++] = 0x10u << 27;
1300 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5);
1301 pb[pb_len++] = (uint32_t)world->completion.gpu_va2;
1302 pb[pb_len++] = (uint32_t)(world->completion.gpu_va2 >> 32);
1303 pb[pb_len++] = payload;
1304 pb[pb_len++] = 0;
1305 pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI
1306
1307 /* Submit to persistent GPFIFO ring */
1308 if (m16_native_submit_methods(&world->m16, pb, pb_len) != 0) {
1309 omega_world_scratch_reset(world);
1310 return OMEGA_WORLD_ERR_HARDWARE;
1311 }
1312
1313 if (world_wait_dispatch(world, __func__, payload) != 0) {
1314 world->faulted = true;
1315 omega_world_scratch_reset(world);
1316 return OMEGA_WORLD_ERR_HARDWARE;
1317 }
1318
1319 nvrm_retire(&world->m16.rm, world->m16.rm.put);
1320
1321 const uint32_t semantic_words[5] = {2, spec->m, spec->k, spec->n,
1322 (uint32_t)spec->precision};
1323 if (record_completed_dispatch(world, semantic_words, code_handle,
1324 h_a, h_b, h_c, a_cpu, req_a,
1325 b_cpu, req_b, c_cpu, req_c,
1326 payload, NULL, NULL) != OMEGA_WORLD_OK) {
1327 omega_world_scratch_reset(world);
1328 return OMEGA_WORLD_ERR_FAULT;
1329 }
1330
1331 if (out_completion_code) {
1332 *out_completion_code = payload;
1333 }
1334 world->total_dispatches++;
1335
1336 omega_world_scratch_reset(world);
1337 return OMEGA_WORLD_OK;
1338}