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.c

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