Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_platform.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_platform.c` 309 lines · 14083 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_gpu_platform.c -- contract checks and the handle table of 3 * src/omega_gpu_platform.h. Freestanding: no libc beyond memset. 4 * The decisions (negotiate, service check, buffer check, handle open and check) 5 * come from om/engine/eng.om ops NEGO, PCHK, BUFCK, HOPEN and HCHK; the C bodies 6 * are the oracle, kept behind CUDAPP_MIGRATION_PENDING_C_PLATFORM and used by the 7 * freestanding build. Table stores stay here. */ 8 #include "omega_gpu_platform.h" 9 10 #include 11 #if defined(CUDAPP_MIGRATION_PENDING_C_PLATFORM) 12 #define ENG_C_PART 1 13 #endif 14 #include "omega_eng_dec.h" 15 16 static const char *const REFUSAL_NAMES[OMEGA_GPU_PLAT_REFUSAL_COUNT] = { 17 "OK", 18 "REFUSED_ARGS", 19 "REFUSED_ABI", 20 "REFUSED_MISSING_SERVICE", 21 "REFUSED_NULL_SERVICE", 22 "REFUSED_BAD_HANDLE", 23 "REFUSED_STALE_HANDLE", 24 "REFUSED_BAD_LENGTH", 25 "REFUSED_BAD_RIGHTS", 26 "REFUSED_BAD_KIND", 27 "REFUSED_CACHE_POLICY", 28 "REFUSED_LIMIT", 29 "REFUSED_BUSY", 30 "REFUSED_DEVICE", 31 }; 32 33 const char *omega_gpu_platform_refusal_name(int refusal) 34 { 35 if (refusal < 0 || refusal >= (int)OMEGA_GPU_PLAT_REFUSAL_COUNT) return "UNKNOWN"; 36 return REFUSAL_NAMES[refusal]; 37 } 38 39 int omega_gpu_platform_negotiate(const OmegaGpuPlatformCaps *provided, const OmegaGpuPlatformCaps *required) 40 { 41 if (eng_on()) { 42 uint64_t in[19], w[16]; 43 memset(in, 0, sizeof in); 44 in[0] = (!provided || !required) ? 1u : 0u; 45 if (provided) { 46 in[1] = provided->abi_major; 47 in[2] = provided->abi_minor; 48 in[3] = provided->services; 49 in[4] = provided->max_devices; 50 in[5] = provided->max_buffers; 51 in[6] = provided->max_queues; 52 in[7] = provided->max_inflight; 53 in[8] = provided->max_buffer_bytes; 54 in[9] = provided->max_total_bytes; 55 } 56 if (required) { 57 in[10] = required->abi_major; 58 in[11] = required->abi_minor; 59 in[12] = required->services; 60 in[13] = required->max_devices; 61 in[14] = required->max_buffers; 62 in[15] = required->max_queues; 63 in[16] = required->max_inflight; 64 in[17] = required->max_buffer_bytes; 65 in[18] = required->max_total_bytes; 66 } 67 if (eng_ask(ENG_OP_NEGO, in, 19, w) != 0) return OMEGA_GPU_PLAT_REFUSED_ARGS; 68 return (int)w[0]; 69 } 70 if (!provided || !required) return OMEGA_GPU_PLAT_REFUSED_ARGS; 71 if (provided->abi_major != required->abi_major) return OMEGA_GPU_PLAT_REFUSED_ABI; 72 if (provided->abi_minor < required->abi_minor) return OMEGA_GPU_PLAT_REFUSED_ABI; 73 if ((provided->services | required->services) & ~OMEGA_GPU_SVC_ALL) return OMEGA_GPU_PLAT_REFUSED_ARGS; 74 if (required->services & ~provided->services) return OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE; 75 if (required->max_devices > provided->max_devices) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 76 if (required->max_buffers > provided->max_buffers) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 77 if (required->max_queues > provided->max_queues) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 78 if (required->max_inflight > provided->max_inflight) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 79 if (required->max_buffer_bytes > provided->max_buffer_bytes) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 80 if (required->max_total_bytes > provided->max_total_bytes) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 81 return OMEGA_GPU_PLAT_OK; 82 } 83 84 /* A claimed service must have its functions; an unclaimed one must have none 85 * (a function the table never negotiated would be a silent fallback path). */ 86 static int svc(uint32_t services, uint32_t bit, int present_all, int present_any) 87 { 88 if (services & bit) return present_all ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_NULL_SERVICE; 89 return present_any ? OMEGA_GPU_PLAT_REFUSED_ARGS : OMEGA_GPU_PLAT_OK; 90 } 91 92 int omega_gpu_platform_check(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps) 93 { 94 return omega_gpu_platform_check_for(p, caps, OMEGA_GPU_SVC_REQUIRED_V1); 95 } 96 97 int omega_gpu_platform_check_for(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps, uint32_t required) 98 { 99 int rc; 100 uint32_t s; 101 if (eng_on()) { 102 uint64_t in[4], w[16]; 103 uint64_t pr = 0; 104 in[0] = (!p || !caps) ? 1u : 0u; 105 in[1] = caps ? caps->services : 0u; 106 in[2] = required; 107 if (p) { 108 /* Presence bits in service order; see pchk/svgm in om/engine/eng.om. */ 109 pr |= (uint64_t)(p->time_now_ms != 0) << 0; 110 pr |= (uint64_t)(p->wait_ms != 0) << 1; 111 pr |= (uint64_t)(p->lock != 0) << 2; 112 pr |= (uint64_t)(p->unlock != 0) << 3; 113 pr |= (uint64_t)(p->mmio_read32 != 0) << 4; 114 pr |= (uint64_t)(p->mmio_write32 != 0) << 5; 115 pr |= (uint64_t)(p->dma_alloc != 0) << 6; 116 pr |= (uint64_t)(p->dma_free != 0) << 7; 117 pr |= (uint64_t)(p->sync_cpu_to_gpu != 0) << 8; 118 pr |= (uint64_t)(p->sync_gpu_to_cpu != 0) << 9; 119 pr |= (uint64_t)(p->poll_word != 0) << 10; 120 pr |= (uint64_t)(p->irq_wait != 0) << 11; 121 pr |= (uint64_t)(p->device_open != 0) << 12; 122 pr |= (uint64_t)(p->device_close != 0) << 13; 123 pr |= (uint64_t)(p->diag_text != 0) << 14; 124 pr |= (uint64_t)(p->queue_open != 0) << 15; 125 pr |= (uint64_t)(p->queue_close != 0) << 16; 126 pr |= (uint64_t)(p->queue_submit != 0) << 17; 127 pr |= (uint64_t)(p->queue_retire != 0) << 18; 128 pr |= (uint64_t)(p->time_now_ns != 0) << 19; 129 pr |= (uint64_t)(p->host_alloc != 0) << 20; 130 pr |= (uint64_t)(p->host_free != 0) << 21; 131 } 132 in[3] = pr; 133 if (eng_ask(ENG_OP_PCHK, in, 4, w) != 0) return OMEGA_GPU_PLAT_REFUSED_ARGS; 134 return (int)w[0]; 135 } 136 if (!p || !caps) return OMEGA_GPU_PLAT_REFUSED_ARGS; 137 s = caps->services; 138 if (s & ~OMEGA_GPU_SVC_ALL) return OMEGA_GPU_PLAT_REFUSED_ARGS; 139 if (required & ~OMEGA_GPU_SVC_ALL) return OMEGA_GPU_PLAT_REFUSED_ARGS; 140 if ((s & required) != required) return OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE; 141 if ((rc = svc(s, OMEGA_GPU_SVC_TIME, p->time_now_ms != 0, p->time_now_ms != 0)) != 0) return rc; 142 if ((rc = svc(s, OMEGA_GPU_SVC_WAIT, p->wait_ms != 0, p->wait_ms != 0)) != 0) return rc; 143 if ((rc = svc(s, OMEGA_GPU_SVC_LOCK, p->lock != 0 && p->unlock != 0, p->lock != 0 || p->unlock != 0)) != 0) 144 return rc; 145 if ((rc = svc(s, OMEGA_GPU_SVC_MMIO, p->mmio_read32 != 0 && p->mmio_write32 != 0, 146 p->mmio_read32 != 0 || p->mmio_write32 != 0)) != 0) 147 return rc; 148 if ((rc = svc(s, OMEGA_GPU_SVC_DMA, p->dma_alloc != 0 && p->dma_free != 0, 149 p->dma_alloc != 0 || p->dma_free != 0)) != 0) 150 return rc; 151 if ((rc = svc(s, OMEGA_GPU_SVC_SYNC, p->sync_cpu_to_gpu != 0 && p->sync_gpu_to_cpu != 0, 152 p->sync_cpu_to_gpu != 0 || p->sync_gpu_to_cpu != 0)) != 0) 153 return rc; 154 if ((rc = svc(s, OMEGA_GPU_SVC_COMPLETION, p->poll_word != 0, p->poll_word != 0)) != 0) return rc; 155 if ((rc = svc(s, OMEGA_GPU_SVC_IRQ, p->irq_wait != 0, p->irq_wait != 0)) != 0) return rc; 156 if ((rc = svc(s, OMEGA_GPU_SVC_DEVICE, p->device_open != 0 && p->device_close != 0 && p->diag_text != 0, 157 p->device_open != 0 || p->device_close != 0 || p->diag_text != 0)) != 0) 158 return rc; 159 if ((rc = svc(s, OMEGA_GPU_SVC_QUEUE, 160 p->queue_open != 0 && p->queue_close != 0 && p->queue_submit != 0 && p->queue_retire != 0, 161 p->queue_open != 0 || p->queue_close != 0 || p->queue_submit != 0 || p->queue_retire != 0)) != 0) 162 return rc; 163 if ((rc = svc(s, OMEGA_GPU_SVC_CLOCK_NS, p->time_now_ns != 0, p->time_now_ns != 0)) != 0) return rc; 164 if ((rc = svc(s, OMEGA_GPU_SVC_HOST_MEM, p->host_alloc != 0 && p->host_free != 0, 165 p->host_alloc != 0 || p->host_free != 0)) != 0) 166 return rc; 167 return OMEGA_GPU_PLAT_OK; 168 } 169 170 static int is_pow2(uint32_t v) 171 { 172 return v != 0u && (v & (v - 1u)) == 0u; 173 } 174 175 int omega_gpu_platform_check_buffer(const OmegaGpuBufferDesc *d, const OmegaGpuPlatformCaps *caps) 176 { 177 uint32_t r; 178 if (eng_on()) { 179 uint64_t in[7], w[16]; 180 memset(in, 0, sizeof in); 181 in[0] = (!d || !caps) ? 1u : 0u; 182 if (d && caps) { 183 in[1] = (uint64_t)d->kind; 184 in[2] = d->bytes; 185 in[3] = caps->max_buffer_bytes; 186 in[4] = d->align; 187 in[5] = (uint64_t)d->gpu_cache; 188 in[6] = d->rights; 189 } 190 if (eng_ask(ENG_OP_BUFCK, in, 7, w) != 0) return OMEGA_GPU_PLAT_REFUSED_ARGS; 191 return (int)w[0]; 192 } 193 if (!d || !caps) return OMEGA_GPU_PLAT_REFUSED_ARGS; 194 if (d->kind != OMEGA_GPU_HANDLE_BUF_DATA && d->kind != OMEGA_GPU_HANDLE_BUF_CODE && 195 d->kind != OMEGA_GPU_HANDLE_BUF_QUEUE) 196 return OMEGA_GPU_PLAT_REFUSED_BAD_KIND; 197 if (d->bytes == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 198 if (d->bytes > caps->max_buffer_bytes) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 199 if (d->align != 0u && !is_pow2(d->align)) return OMEGA_GPU_PLAT_REFUSED_ARGS; 200 if (d->gpu_cache != OMEGA_GPU_CACHE_GPU_UNCACHED && d->gpu_cache != OMEGA_GPU_CACHE_GPU_CACHED) 201 return OMEGA_GPU_PLAT_REFUSED_ARGS; 202 r = d->rights; 203 if (r == 0u || (r & ~OMEGA_GPU_RIGHT_ALL)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 204 if ((r & OMEGA_GPU_RIGHT_GPU_EXEC) && d->kind != OMEGA_GPU_HANDLE_BUF_CODE) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 205 if (d->kind == OMEGA_GPU_HANDLE_BUF_CODE) { 206 /* code: CPU writes it, GPU executes it, GPU never writes it, nobody polls it */ 207 if (r & (OMEGA_GPU_RIGHT_GPU_WRITE | OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 208 if (!(r & OMEGA_GPU_RIGHT_GPU_EXEC) || !(r & OMEGA_GPU_RIGHT_CPU_WRITE)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 209 } 210 if (d->kind == OMEGA_GPU_HANDLE_BUF_QUEUE) { 211 /* a submission ring: the CPU writes methods, the device reads them */ 212 if (!(r & OMEGA_GPU_RIGHT_CPU_WRITE) || !(r & OMEGA_GPU_RIGHT_GPU_READ)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 213 } 214 if (r & OMEGA_GPU_RIGHT_HOST_POLLED) { 215 if (!(r & OMEGA_GPU_RIGHT_CPU_READ) || !(r & OMEGA_GPU_RIGHT_GPU_WRITE)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 216 if (d->gpu_cache != OMEGA_GPU_CACHE_GPU_UNCACHED) return OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY; 217 } 218 return OMEGA_GPU_PLAT_OK; 219 } 220 221 void omega_gpu_handles_init(OmegaGpuHandleTable *t) 222 { 223 uint32_t i; 224 if (!t) return; 225 memset(t, 0, sizeof *t); 226 for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) t->generation[i] = 1u; 227 } 228 229 static int kind_known(uint32_t kind) 230 { 231 return kind >= OMEGA_GPU_HANDLE_DEVICE && kind < OMEGA_GPU_HANDLE_KIND_COUNT; 232 } 233 234 int omega_gpu_handles_open(OmegaGpuHandleTable *t, uint32_t kind, uint64_t bytes, uint32_t limit_slots, 235 uint64_t limit_bytes, uint32_t *index, uint32_t *generation) 236 { 237 uint32_t i; 238 uint32_t same_kind = 0; 239 if (eng_on()) { 240 uint64_t in[7], w[16]; 241 in[0] = (!t || !index || !generation) ? 1u : 0u; 242 in[1] = kind; 243 if (t) 244 for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) 245 if (t->live[i] && t->kind[i] == kind) same_kind++; 246 in[2] = same_kind; 247 in[3] = limit_slots; 248 in[4] = bytes; 249 in[5] = limit_bytes; 250 in[6] = t ? t->total_bytes : 0u; 251 if (eng_ask(ENG_OP_HOPEN, in, 7, w) != 0) return OMEGA_GPU_PLAT_REFUSED_ARGS; 252 if (w[0] != OMEGA_GPU_PLAT_OK) return (int)w[0]; 253 } else { 254 if (!t || !index || !generation) return OMEGA_GPU_PLAT_REFUSED_ARGS; 255 if (!kind_known(kind)) return OMEGA_GPU_PLAT_REFUSED_BAD_KIND; 256 for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) 257 if (t->live[i] && t->kind[i] == kind) same_kind++; 258 if (same_kind >= limit_slots) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 259 if (bytes != 0u && (bytes > limit_bytes || t->total_bytes > limit_bytes - bytes)) /* byte accounting: buffers only */ 260 return OMEGA_GPU_PLAT_REFUSED_LIMIT; 261 } 262 for (i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) { 263 if (t->live[i]) continue; 264 if (t->generation[i] == 0u) continue; /* generation wrapped: the slot is retired, never reissued */ 265 t->live[i] = 1; 266 t->kind[i] = kind; 267 t->bytes[i] = bytes; 268 t->nlive++; 269 t->total_bytes += bytes; 270 *index = i; 271 *generation = t->generation[i]; 272 return OMEGA_GPU_PLAT_OK; 273 } 274 return OMEGA_GPU_PLAT_REFUSED_LIMIT; 275 } 276 277 int omega_gpu_handles_check(const OmegaGpuHandleTable *t, uint32_t index, uint32_t generation, uint32_t kind) 278 { 279 if (eng_on()) { 280 uint64_t in[7], w[16]; 281 int in_range = t && index < OMEGA_GPU_HANDLE_SLOTS; 282 in[0] = t ? 0u : 1u; 283 in[1] = index; 284 in[2] = generation; 285 in[3] = in_range ? t->live[index] : 0u; 286 in[4] = in_range ? t->generation[index] : 0u; 287 in[5] = in_range ? t->kind[index] : 0u; 288 in[6] = kind; 289 if (eng_ask(ENG_OP_HCHK, in, 7, w) != 0) return OMEGA_GPU_PLAT_REFUSED_ARGS; 290 return (int)w[0]; 291 } 292 if (!t) return OMEGA_GPU_PLAT_REFUSED_ARGS; 293 if (index >= OMEGA_GPU_HANDLE_SLOTS || generation == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; 294 if (!t->live[index] || t->generation[index] != generation) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 295 if (t->kind[index] != kind) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; 296 return OMEGA_GPU_PLAT_OK; 297 } 298 299 int omega_gpu_handles_close(OmegaGpuHandleTable *t, uint32_t index, uint32_t generation, uint32_t kind) 300 { 301 int rc = omega_gpu_handles_check(t, index, generation, kind); 302 if (rc != OMEGA_GPU_PLAT_OK) return rc; 303 t->live[index] = 0; 304 t->total_bytes -= t->bytes[index]; 305 t->bytes[index] = 0u; 306 t->nlive--; 307 t->generation[index]++; /* the closed handle is now stale; 0 after wrap retires the slot */ 308 return OMEGA_GPU_PLAT_OK; 309 }