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_accelerator_world.h
| 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 | #ifndef OMEGA_ACCELERATOR_WORLD_H |
| 18 | #define OMEGA_ACCELERATOR_WORLD_H |
| 19 | |
| 20 | #include "omega_types.h" |
| 21 | #include "omega_vector.h" |
| 22 | #include "omega_blackwell_realize.h" |
| 23 | #include "omega_blackwell_matmul.h" |
| 24 | #include "m16_native.h" |
| 25 | #include <stdbool.h> |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | #define OMEGA_WORLD_MAX_BUFFERS 128 |
| 30 | #define OMEGA_WORLD_MAX_CODE_ENTRIES 64 |
| 31 | #define OMEGA_WORLD_SCRATCH_SIZE 0x200000 /* 2 MiB persistent scratch */ |
| 32 | #define OMEGA_WORLD_MAX_PB_WORDS 4096 |
| 33 | #define OMEGA_WORLD_MAX_IN_FLIGHT 256 |
| 34 | |
| 35 | /* Object Types */ |
| 36 | #define OMEGA_OBJ_NONE 0x00000000 |
| 37 | #define OMEGA_OBJ_BUFFER 0x00000001 |
| 38 | #define OMEGA_OBJ_CODE 0x00000002 |
| 39 | |
| 40 | /* Permission Flags */ |
| 41 | #define OMEGA_PERM_READ 0x00000001 |
| 42 | #define OMEGA_PERM_WRITE 0x00000002 |
| 43 | #define OMEGA_PERM_EXECUTE 0x00000004 |
| 44 | |
| 45 | /* Error Codes */ |
| 46 | #define OMEGA_WORLD_OK 0 |
| 47 | #define OMEGA_WORLD_ERR_INVALID_ARG -1 |
| 48 | #define OMEGA_WORLD_ERR_NO_MEM -2 |
| 49 | #define OMEGA_WORLD_ERR_STALE_EPOCH -3 |
| 50 | #define OMEGA_WORLD_ERR_STALE_GEN -4 |
| 51 | #define OMEGA_WORLD_ERR_PERM_DENIED -5 |
| 52 | #define OMEGA_WORLD_ERR_BOUNDS -6 |
| 53 | #define OMEGA_WORLD_ERR_NOT_FOUND -7 |
| 54 | #define OMEGA_WORLD_ERR_HARDWARE -8 |
| 55 | #define OMEGA_WORLD_ERR_FAULT -9 |
| 56 | /* The world observed a hardware or accounting failure it could not resolve. |
| 57 | * Every submission path refuses until recover_channel_fault or rebuild. */ |
| 58 | #define OMEGA_WORLD_ERR_FAULTED -10 |
| 59 | |
| 60 | /* External Capability Handle */ |
| 61 | typedef struct { |
| 62 | uint32_t world_epoch; |
| 63 | uint32_t object_id; |
| 64 | uint32_t object_generation; |
| 65 | uint32_t object_type; |
| 66 | uint32_t permissions; |
| 67 | } OmegaHandle; |
| 68 | |
| 69 | /* Internal Buffer Registry Entry */ |
| 70 | typedef struct { |
| 71 | uint32_t object_id; |
| 72 | uint32_t generation; |
| 73 | uint32_t permissions; |
| 74 | bool active; |
| 75 | /* Revoked while an in-flight submission still referenced it: the handle |
| 76 | * is already refused, but the memory is released only after drain. */ |
| 77 | bool retiring; |
| 78 | size_t size_bytes; |
| 79 | void *cpu_addr; |
| 80 | uint64_t gpu_va; |
| 81 | NvrmMem mem; |
| 82 | } OmegaBufferEntry; |
| 83 | |
| 84 | /* Internal Code Registry Entry */ |
| 85 | typedef struct { |
| 86 | uint32_t object_id; |
| 87 | uint32_t generation; |
| 88 | bool active; |
| 89 | bool retiring; |
| 90 | size_t size_bytes; |
| 91 | void *cpu_addr; |
| 92 | uint64_t gpu_va; |
| 93 | uint8_t code_digest[32]; |
| 94 | uint8_t realization_id[32]; |
| 95 | NvrmMem mem; |
| 96 | } OmegaCodeEntry; |
| 97 | |
| 98 | /* Scratch Arena Allocation */ |
| 99 | typedef struct { |
| 100 | NvrmMem mem; |
| 101 | size_t capacity_bytes; |
| 102 | size_t allocated_bytes; |
| 103 | uint32_t active_slices; |
| 104 | } OmegaScratchArena; |
| 105 | |
| 106 | /* Persistent Completion Marker */ |
| 107 | typedef struct { |
| 108 | NvrmMem mem; |
| 109 | volatile uint32_t *cpu_marker; |
| 110 | /* Second release at page + 0x10, written after the L2 flush (C3). Dispatch |
| 111 | * waits on this; its payload sequence matches cpu_marker. */ |
| 112 | volatile uint32_t *cpu_marker2; |
| 113 | uint64_t gpu_va2; |
| 114 | uint64_t gpu_va; |
| 115 | uint32_t last_payload; |
| 116 | } OmegaCompletionTracker; |
| 117 | |
| 118 | /* Semantic descriptor for one queued dispatch. Captured at submission time and |
| 119 | * folded into the rolling digest only after completion is observed, so digest |
| 120 | * provenance is bound to real code/realization/input/result identities. */ |
| 121 | typedef struct { |
| 122 | uint32_t semantic_words[5]; |
| 123 | uint32_t code_object_id; |
| 124 | uint32_t code_generation; |
| 125 | uint32_t code_permissions; |
| 126 | uint32_t a_object_id; |
| 127 | uint32_t a_generation; |
| 128 | uint32_t a_permissions; |
| 129 | uint32_t b_object_id; |
| 130 | uint32_t b_generation; |
| 131 | uint32_t b_permissions; |
| 132 | uint32_t c_object_id; |
| 133 | uint32_t c_generation; |
| 134 | uint32_t c_permissions; |
| 135 | uint32_t a_bytes; |
| 136 | uint32_t b_bytes; |
| 137 | uint32_t c_bytes; |
| 138 | uint32_t completion_val; |
| 139 | /* GPFIFO put count immediately after this submission was enqueued. |
| 140 | * This is queue accounting used for retirement and is deliberately |
| 141 | * distinct from completion_val (hardware-completion identity). The |
| 142 | * world fills it in omega_world_submit; callers must leave it zero. */ |
| 143 | uint32_t gp_seq; |
| 144 | uint32_t channel_generation; |
| 145 | uint8_t semantic_id[32]; |
| 146 | uint8_t input_bindings_digest[32]; |
| 147 | bool output_cpu_protected; |
| 148 | bool committed; |
| 149 | } OmegaWorldSubmission; |
| 150 | |
| 151 | /* Omega Accelerator World Context */ |
| 152 | typedef struct { |
| 153 | uint32_t current_epoch; |
| 154 | uint32_t handle_epoch; /* unique across destroy/init, never reused */ |
| 155 | uint32_t channel_generation; |
| 156 | bool initialized; |
| 157 | /* Latched on an unresolved hardware/accounting failure; cleared only by |
| 158 | * omega_world_recover_channel_fault or omega_world_rebuild. */ |
| 159 | bool faulted; |
| 160 | |
| 161 | /* Persistent hardware context */ |
| 162 | M16NativeContext m16; |
| 163 | bool channel_active; |
| 164 | |
| 165 | /* Live queue ring topology */ |
| 166 | uint32_t ring_capacity; |
| 167 | uint32_t put_count; |
| 168 | uint32_t retired_count; |
| 169 | |
| 170 | /* Registries */ |
| 171 | OmegaBufferEntry buffers[OMEGA_WORLD_MAX_BUFFERS]; |
| 172 | uint32_t buffer_count; |
| 173 | |
| 174 | OmegaCodeEntry code_entries[OMEGA_WORLD_MAX_CODE_ENTRIES]; |
| 175 | uint32_t code_count; |
| 176 | |
| 177 | /* Persistent scratch arena & completion */ |
| 178 | OmegaScratchArena scratch; |
| 179 | OmegaCompletionTracker completion; |
| 180 | |
| 181 | /* Batched submissions awaiting observed completion */ |
| 182 | OmegaWorldSubmission in_flight[OMEGA_WORLD_MAX_IN_FLIGHT]; |
| 183 | uint32_t in_flight_count; |
| 184 | |
| 185 | /* Pushbuffer allocation */ |
| 186 | NvrmMem pb_mem; |
| 187 | |
| 188 | /* Rolling state digest */ |
| 189 | uint64_t sequence_number; |
| 190 | uint8_t rolling_state_digest[32]; |
| 191 | |
| 192 | /* Statistics */ |
| 193 | uint64_t total_dispatches; |
| 194 | uint64_t channel_reconstructions; |
| 195 | /* Completed submissions that were not committed because an object they |
| 196 | * bound was revoked while they were in flight, or lost to a channel |
| 197 | * recovery. Never folded into total_dispatches or the digest. */ |
| 198 | uint64_t abandoned_dispatches; |
| 199 | } OmegaAcceleratorWorld; |
| 200 | |
| 201 | /* World Lifecycle */ |
| 202 | int omega_world_init(OmegaAcceleratorWorld *world); |
| 203 | int omega_world_destroy(OmegaAcceleratorWorld *world); |
| 204 | int omega_world_rebuild(OmegaAcceleratorWorld *world); |
| 205 | |
| 206 | /* Buffer Registry Operations */ |
| 207 | int omega_world_register_buffer(OmegaAcceleratorWorld *world, |
| 208 | size_t size_bytes, |
| 209 | uint32_t permissions, |
| 210 | OmegaHandle *out_handle); |
| 211 | int omega_world_revoke_buffer(OmegaAcceleratorWorld *world, |
| 212 | const OmegaHandle *handle); |
| 213 | int omega_world_resolve_buffer(OmegaAcceleratorWorld *world, |
| 214 | const OmegaHandle *handle, |
| 215 | uint32_t required_perms, |
| 216 | size_t req_offset, |
| 217 | size_t req_size, |
| 218 | void **out_cpu, |
| 219 | uint64_t *out_gpu_va); |
| 220 | |
| 221 | /* Code Registry Operations */ |
| 222 | int omega_world_register_code(OmegaAcceleratorWorld *world, |
| 223 | const void *code_bytes, |
| 224 | size_t code_size, |
| 225 | const uint8_t realization_id[32], |
| 226 | OmegaHandle *out_handle); |
| 227 | int omega_world_revoke_code(OmegaAcceleratorWorld *world, |
| 228 | const OmegaHandle *handle); |
| 229 | int omega_world_resolve_code(OmegaAcceleratorWorld *world, |
| 230 | const OmegaHandle *handle, |
| 231 | const void **out_cpu, |
| 232 | uint64_t *out_gpu_va, |
| 233 | size_t *out_size); |
| 234 | |
| 235 | /* Handle Validation */ |
| 236 | int omega_world_validate_handle(const OmegaAcceleratorWorld *world, |
| 237 | const OmegaHandle *handle, |
| 238 | uint32_t expected_type, |
| 239 | uint32_t required_perms); |
| 240 | |
| 241 | /* Scratch Arena */ |
| 242 | int omega_world_scratch_acquire(OmegaAcceleratorWorld *world, |
| 243 | size_t size_bytes, |
| 244 | void **out_cpu, |
| 245 | uint64_t *out_gpu_va, |
| 246 | size_t *out_offset); |
| 247 | void omega_world_scratch_reset(OmegaAcceleratorWorld *world); |
| 248 | |
| 249 | /* Hardware Ring Topology */ |
| 250 | int omega_world_discover_ring_capacity(OmegaAcceleratorWorld *world, |
| 251 | uint32_t *out_capacity); |
| 252 | |
| 253 | /* Persistent Execution Operations */ |
| 254 | int omega_world_dispatch_vector(OmegaAcceleratorWorld *world, |
| 255 | const OmegaHandle *code_handle, |
| 256 | const OmegaHandle *h_a, |
| 257 | const OmegaHandle *h_b, |
| 258 | const OmegaHandle *h_c, |
| 259 | uint32_t n, |
| 260 | uint32_t *out_completion_code); |
| 261 | |
| 262 | int omega_world_dispatch_matmul(OmegaAcceleratorWorld *world, |
| 263 | const OmegaMatMulSpec *spec, |
| 264 | const OmegaHandle *code_handle, |
| 265 | const OmegaHandle *h_a, |
| 266 | const OmegaHandle *h_b, |
| 267 | const OmegaHandle *h_c, |
| 268 | uint32_t *out_completion_code); |
| 269 | |
| 270 | /* Batched persistent submission. Enqueues a prebuilt pushbuffer whose terminal |
| 271 | * semaphore release carries submission->completion_val. The world owns the |
| 272 | * authoritative sequence/dispatch accounting: counters and the rolling digest |
| 273 | * advance only when completion is observed, never at enqueue time. */ |
| 274 | int omega_world_submit(OmegaAcceleratorWorld *world, |
| 275 | const NvrmMem *pb_mem, |
| 276 | uint32_t off_bytes, |
| 277 | uint32_t nwords, |
| 278 | const OmegaWorldSubmission *submission); |
| 279 | |
| 280 | /* Ring the doorbell after a run of submissions. */ |
| 281 | int omega_world_ring(OmegaAcceleratorWorld *world); |
| 282 | |
| 283 | /* Block until the completion marker reaches upto_payload, retire the GPFIFO |
| 284 | * entries, and commit every in-flight dispatch whose completion_val <= |
| 285 | * upto_payload by advancing total_dispatches and folding the dispatch into the |
| 286 | * rolling digest. Returns the number committed, or a negative error code. */ |
| 287 | int omega_world_drain(OmegaAcceleratorWorld *world, |
| 288 | uint32_t upto_payload, |
| 289 | int timeout_ms); |
| 290 | |
| 291 | /* Channel Fault Injection and Recovery */ |
| 292 | int omega_world_recover_channel_fault(OmegaAcceleratorWorld *world); |
| 293 | |
| 294 | /* Rolling State Digest */ |
| 295 | int omega_world_update_digest(OmegaAcceleratorWorld *world, |
| 296 | const uint8_t *semantic_id, |
| 297 | const uint8_t *realization_id, |
| 298 | const uint8_t *code_digest, |
| 299 | const uint8_t *input_bindings_digest, |
| 300 | const uint32_t *obj_id_gen_pairs, |
| 301 | uint32_t obj_count, |
| 302 | uint32_t capability_scope, |
| 303 | uint32_t completion_val, |
| 304 | const uint8_t *result_digest, |
| 305 | uint32_t outcome_code); |
| 306 | |
| 307 | #endif /* OMEGA_ACCELERATOR_WORLD_H */ |