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.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.c -- contract checks and the handle table of |
| 18 | * src/omega_gpu_platform.h. Freestanding: no libc beyond memset. */ |
| 19 | #include "omega_gpu_platform.h" |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | static const char *const REFUSAL_NAMES[OMEGA_GPU_PLAT_REFUSAL_COUNT] = { |
| 24 | "OK", |
| 25 | "REFUSED_ARGS", |
| 26 | "REFUSED_ABI", |
| 27 | "REFUSED_MISSING_SERVICE", |
| 28 | "REFUSED_NULL_SERVICE", |
| 29 | "REFUSED_BAD_HANDLE", |
| 30 | "REFUSED_STALE_HANDLE", |
| 31 | "REFUSED_BAD_LENGTH", |
| 32 | "REFUSED_BAD_RIGHTS", |
| 33 | "REFUSED_BAD_KIND", |
| 34 | "REFUSED_CACHE_POLICY", |
| 35 | "REFUSED_LIMIT", |
| 36 | "REFUSED_BUSY", |
| 37 | "REFUSED_DEVICE", |
| 38 | }; |
| 39 | |
| 40 | const char *omega_gpu_platform_refusal_name(int refusal) |
| 41 | { |
| 42 | if (refusal < 0 || refusal >= (int)OMEGA_GPU_PLAT_REFUSAL_COUNT) return "UNKNOWN"; |
| 43 | return REFUSAL_NAMES[refusal]; |
| 44 | } |
| 45 | |
| 46 | int omega_gpu_platform_negotiate(const OmegaGpuPlatformCaps *provided, const OmegaGpuPlatformCaps *required) |
| 47 | { |
| 48 | if (!provided || !required) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 49 | if (provided->abi_major != required->abi_major) return OMEGA_GPU_PLAT_REFUSED_ABI; |
| 50 | if (provided->abi_minor < required->abi_minor) return OMEGA_GPU_PLAT_REFUSED_ABI; |
| 51 | if ((provided->services | required->services) & ~OMEGA_GPU_SVC_ALL) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 52 | if (required->services & ~provided->services) return OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE; |
| 53 | if (required->max_devices > provided->max_devices) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 54 | if (required->max_buffers > provided->max_buffers) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 55 | if (required->max_queues > provided->max_queues) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 56 | if (required->max_inflight > provided->max_inflight) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 57 | if (required->max_buffer_bytes > provided->max_buffer_bytes) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 58 | if (required->max_total_bytes > provided->max_total_bytes) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 59 | return OMEGA_GPU_PLAT_OK; |
| 60 | } |
| 61 | |
| 62 | /* A claimed service must have its functions; an unclaimed one must have none |
| 63 | * (a function the table never negotiated would be a silent fallback path). */ |
| 64 | static int svc(uint32_t services, uint32_t bit, int present_all, int present_any) |
| 65 | { |
| 66 | if (services & bit) return present_all ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_NULL_SERVICE; |
| 67 | return present_any ? OMEGA_GPU_PLAT_REFUSED_ARGS : OMEGA_GPU_PLAT_OK; |
| 68 | } |
| 69 | |
| 70 | int omega_gpu_platform_check(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps) |
| 71 | { |
| 72 | return omega_gpu_platform_check_for(p, caps, OMEGA_GPU_SVC_REQUIRED_V1); |
| 73 | } |
| 74 | |
| 75 | int omega_gpu_platform_check_for(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps, uint32_t required) |
| 76 | { |
| 77 | int rc; |
| 78 | uint32_t s; |
| 79 | if (!p || !caps) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 80 | s = caps->services; |
| 81 | if (s & ~OMEGA_GPU_SVC_ALL) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 82 | if (required & ~OMEGA_GPU_SVC_ALL) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 83 | if ((s & required) != required) return OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE; |
| 84 | if ((rc = svc(s, OMEGA_GPU_SVC_TIME, p->time_now_ms != 0, p->time_now_ms != 0)) != 0) return rc; |
| 85 | if ((rc = svc(s, OMEGA_GPU_SVC_WAIT, p->wait_ms != 0, p->wait_ms != 0)) != 0) return rc; |
| 86 | if ((rc = svc(s, OMEGA_GPU_SVC_LOCK, p->lock != 0 && p->unlock != 0, p->lock != 0 || p->unlock != 0)) != 0) |
| 87 | return rc; |
| 88 | if ((rc = svc(s, OMEGA_GPU_SVC_MMIO, p->mmio_read32 != 0 && p->mmio_write32 != 0, |
| 89 | p->mmio_read32 != 0 || p->mmio_write32 != 0)) != 0) |
| 90 | return rc; |
| 91 | if ((rc = svc(s, OMEGA_GPU_SVC_DMA, p->dma_alloc != 0 && p->dma_free != 0, |
| 92 | p->dma_alloc != 0 || p->dma_free != 0)) != 0) |
| 93 | return rc; |
| 94 | if ((rc = svc(s, OMEGA_GPU_SVC_SYNC, p->sync_cpu_to_gpu != 0 && p->sync_gpu_to_cpu != 0, |
| 95 | p->sync_cpu_to_gpu != 0 || p->sync_gpu_to_cpu != 0)) != 0) |
| 96 | return rc; |
| 97 | if ((rc = svc(s, OMEGA_GPU_SVC_COMPLETION, p->poll_word != 0, p->poll_word != 0)) != 0) return rc; |
| 98 | if ((rc = svc(s, OMEGA_GPU_SVC_IRQ, p->irq_wait != 0, p->irq_wait != 0)) != 0) return rc; |
| 99 | if ((rc = svc(s, OMEGA_GPU_SVC_DEVICE, p->device_open != 0 && p->device_close != 0 && p->diag_text != 0, |
| 100 | p->device_open != 0 || p->device_close != 0 || p->diag_text != 0)) != 0) |
| 101 | return rc; |
| 102 | if ((rc = svc(s, OMEGA_GPU_SVC_QUEUE, |
| 103 | p->queue_open != 0 && p->queue_close != 0 && p->queue_submit != 0 && p->queue_retire != 0, |
| 104 | p->queue_open != 0 || p->queue_close != 0 || p->queue_submit != 0 || p->queue_retire != 0)) != 0) |
| 105 | return rc; |
| 106 | if ((rc = svc(s, OMEGA_GPU_SVC_CLOCK_NS, p->time_now_ns != 0, p->time_now_ns != 0)) != 0) return rc; |
| 107 | if ((rc = svc(s, OMEGA_GPU_SVC_HOST_MEM, p->host_alloc != 0 && p->host_free != 0, |
| 108 | p->host_alloc != 0 || p->host_free != 0)) != 0) |
| 109 | return rc; |
| 110 | return OMEGA_GPU_PLAT_OK; |
| 111 | } |
| 112 | |
| 113 | static int is_pow2(uint32_t v) |
| 114 | { |
| 115 | return v != 0u && (v & (v - 1u)) == 0u; |
| 116 | } |
| 117 | |
| 118 | int omega_gpu_platform_check_buffer(const OmegaGpuBufferDesc *d, const OmegaGpuPlatformCaps *caps) |
| 119 | { |
| 120 | uint32_t r; |
| 121 | if (!d || !caps) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 122 | if (d->kind != OMEGA_GPU_HANDLE_BUF_DATA && d->kind != OMEGA_GPU_HANDLE_BUF_CODE && |
| 123 | d->kind != OMEGA_GPU_HANDLE_BUF_QUEUE) |
| 124 | return OMEGA_GPU_PLAT_REFUSED_BAD_KIND; |
| 125 | if (d->bytes == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 126 | if (d->bytes > caps->max_buffer_bytes) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 127 | if (d->align != 0u && !is_pow2(d->align)) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 128 | if (d->gpu_cache != OMEGA_GPU_CACHE_GPU_UNCACHED && d->gpu_cache != OMEGA_GPU_CACHE_GPU_CACHED) |
| 129 | return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 130 | r = d->rights; |
| 131 | if (r == 0u || (r & ~OMEGA_GPU_RIGHT_ALL)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 132 | if ((r & OMEGA_GPU_RIGHT_GPU_EXEC) && d->kind != OMEGA_GPU_HANDLE_BUF_CODE) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 133 | if (d->kind == OMEGA_GPU_HANDLE_BUF_CODE) { |
| 134 | /* code: CPU writes it, GPU executes it, GPU never writes it, nobody polls it */ |
| 135 | if (r & (OMEGA_GPU_RIGHT_GPU_WRITE | OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 136 | if (!(r & OMEGA_GPU_RIGHT_GPU_EXEC) || !(r & OMEGA_GPU_RIGHT_CPU_WRITE)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 137 | } |
| 138 | if (d->kind == OMEGA_GPU_HANDLE_BUF_QUEUE) { |
| 139 | /* a submission ring: the CPU writes methods, the device reads them */ |
| 140 | if (!(r & OMEGA_GPU_RIGHT_CPU_WRITE) || !(r & OMEGA_GPU_RIGHT_GPU_READ)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 141 | } |
| 142 | if (r & OMEGA_GPU_RIGHT_HOST_POLLED) { |
| 143 | if (!(r & OMEGA_GPU_RIGHT_CPU_READ) || !(r & OMEGA_GPU_RIGHT_GPU_WRITE)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; |
| 144 | if (d->gpu_cache != OMEGA_GPU_CACHE_GPU_UNCACHED) return OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY; |
| 145 | } |
| 146 | return OMEGA_GPU_PLAT_OK; |
| 147 | } |
| 148 | |
| 149 | void omega_gpu_handles_init(OmegaGpuHandleTable *t) |
| 150 | { |
| 151 | uint32_t i; |
| 152 | if (!t) return; |
| 153 | memset(t, 0, sizeof *t); |
| 154 | for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) t->generation[i] = 1u; |
| 155 | } |
| 156 | |
| 157 | static int kind_known(uint32_t kind) |
| 158 | { |
| 159 | return kind >= OMEGA_GPU_HANDLE_DEVICE && kind < OMEGA_GPU_HANDLE_KIND_COUNT; |
| 160 | } |
| 161 | |
| 162 | int omega_gpu_handles_open(OmegaGpuHandleTable *t, uint32_t kind, uint64_t bytes, uint32_t limit_slots, |
| 163 | uint64_t limit_bytes, uint32_t *index, uint32_t *generation) |
| 164 | { |
| 165 | uint32_t i; |
| 166 | uint32_t same_kind = 0; |
| 167 | if (!t || !index || !generation) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 168 | if (!kind_known(kind)) return OMEGA_GPU_PLAT_REFUSED_BAD_KIND; |
| 169 | for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) |
| 170 | if (t->live[i] && t->kind[i] == kind) same_kind++; |
| 171 | if (same_kind >= limit_slots) return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 172 | if (bytes != 0u && (bytes > limit_bytes || t->total_bytes > limit_bytes - bytes)) /* byte accounting: buffers only */ |
| 173 | return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 174 | for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) { |
| 175 | if (t->live[i]) continue; |
| 176 | if (t->generation[i] == 0u) continue; /* generation wrapped: the slot is retired, never reissued */ |
| 177 | t->live[i] = 1; |
| 178 | t->kind[i] = kind; |
| 179 | t->bytes[i] = bytes; |
| 180 | t->nlive++; |
| 181 | t->total_bytes += bytes; |
| 182 | *index = i; |
| 183 | *generation = t->generation[i]; |
| 184 | return OMEGA_GPU_PLAT_OK; |
| 185 | } |
| 186 | return OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 187 | } |
| 188 | |
| 189 | int omega_gpu_handles_check(const OmegaGpuHandleTable *t, uint32_t index, uint32_t generation, uint32_t kind) |
| 190 | { |
| 191 | if (!t) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 192 | if (index >= OMEGA_GPU_HANDLE_SLOTS || generation == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; |
| 193 | if (!t->live[index] || t->generation[index] != generation) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; |
| 194 | if (t->kind[index] != kind) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; |
| 195 | return OMEGA_GPU_PLAT_OK; |
| 196 | } |
| 197 | |
| 198 | int omega_gpu_handles_close(OmegaGpuHandleTable *t, uint32_t index, uint32_t generation, uint32_t kind) |
| 199 | { |
| 200 | int rc = omega_gpu_handles_check(t, index, generation, kind); |
| 201 | if (rc != OMEGA_GPU_PLAT_OK) return rc; |
| 202 | t->live[index] = 0; |
| 203 | t->total_bytes -= t->bytes[index]; |
| 204 | t->bytes[index] = 0u; |
| 205 | t->nlive--; |
| 206 | t->generation[index]++; /* the closed handle is now stale; 0 after wrap retires the slot */ |
| 207 | return OMEGA_GPU_PLAT_OK; |
| 208 | } |