interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_gpu_platform.h

252 lines · 15450 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_gpu_platform.h -- native platform contract for the Omega GPU engine.
3 *
4 * Native v1 P07 cut A (aienos#286 cut A, aien-architecture#190). This header
5 * names what a GPU backend may ask of the platform it runs on, in a form that
6 * works the same under Linux (today's nvrm/m16 backend) and under AIENOS
7 * (the native backend of later cuts). It is pure C with no Linux header and
8 * no libc call; it builds in the freestanding link target
9 * (`make gpu-engine-freestanding`).
10 *
11 * Rules (from aienos#286 cut A):
12 * - Resources are typed handles (index + generation), never a Linux fd, a
13 * raw pointer or an unchecked physical address. A handle is valid only
14 * while its slot generation matches, so a closed handle is refused.
15 * - Every byte range carries an explicit length and explicit rights.
16 * - CPU views, GPU virtual addresses and bus (SMMU input) addresses are
17 * three distinct types. There is no type for a raw physical address on
18 * purpose: the native backend resolves DMA through the SMMU (cut B).
19 * - Buffers, code and queues have separate kinds and lifetimes.
20 * - A backend negotiates an ABI version and a service set up front; what
21 * the platform does not provide is refused, never emulated. There is no
22 * silent fallback to Linux, to the CPU, or to a remote device.
23 * - Polling a word is not interrupt support: OMEGA_GPU_SVC_IRQ is claimed
24 * only by a platform whose irq_wait blocks on a real device interrupt.
25 * - Anything the CPU polls for a device write (completion markers,
26 * semaphores) is OMEGA_GPU_RIGHT_HOST_POLLED and must be GPU-uncached
27 * (nvidia-open 580.173.02 nvos.h: GPU-cacheable system memory "will not
28 * be coherent with direct CPU mappings"; the 2026-10-02 E1 stall).
29 *
30 * What this header does NOT claim: no device has been opened through it, no
31 * GB10 register has been touched, and the Linux backend still owns every
32 * driver responsibility (docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md). Hardware
33 * evidence for device ownership is NOT_RUN until cut B runs in an attended
34 * window.
35 */
36#ifndef OMEGA_GPU_PLATFORM_H
37#define OMEGA_GPU_PLATFORM_H
38
39#include <stddef.h>
40#include <stdint.h>
41
42#define OMEGA_GPU_PLATFORM_ABI_MAJOR 1u
43/* minor: additive change (a service bit or a caps field appended); negotiate refuses a provided minor older than the
44 * required one. 1.1 (P07 cut C session part): DEVICE, QUEUE, CLOCK_NS, HOST_MEM; COMPLETION is poll_word.
45 * 1.2 (omega #457/#459): caps.submit_prefix_words. */
46#define OMEGA_GPU_PLATFORM_ABI_MINOR 2u
47
48/* ---- typed handles ------------------------------------------------------ */
49
50/* Generation 0 is never issued, so a zeroed handle is "none". */
51typedef struct { uint32_t index; uint32_t generation; } OmegaGpuDeviceHandle;
52typedef struct { uint32_t index; uint32_t generation; } OmegaGpuBufferHandle;
53typedef struct { uint32_t index; uint32_t generation; } OmegaGpuQueueHandle;
54
55/* Handle kinds (also the buffer kinds below share the BUF_* space). */
56typedef enum {
57 OMEGA_GPU_HANDLE_DEVICE = 1,
58 OMEGA_GPU_HANDLE_BUF_DATA = 2, /* inputs, outputs, scratch */
59 OMEGA_GPU_HANDLE_BUF_CODE = 3, /* kernel code: CPU writes once, GPU executes, GPU never writes */
60 OMEGA_GPU_HANDLE_BUF_QUEUE = 4, /* pushbuffer / submission ring */
61 OMEGA_GPU_HANDLE_QUEUE = 5, /* a channel or submission queue */
62 OMEGA_GPU_HANDLE_KIND_COUNT
63} OmegaGpuHandleKind;
64
65/* ---- address kinds ------------------------------------------------------ */
66
67typedef struct { void *ptr; size_t len; } OmegaCpuView; /* CPU mapping with its length */
68typedef struct { uint64_t va; } OmegaGpuVa; /* GPU virtual address (device page tables) */
69typedef struct { uint64_t iova; } OmegaGpuBusAddr; /* what the device emits on the fabric (SMMU input) */
70
71/* ---- rights and buffer descriptors -------------------------------------- */
72
73enum {
74 OMEGA_GPU_RIGHT_CPU_READ = 1u << 0,
75 OMEGA_GPU_RIGHT_CPU_WRITE = 1u << 1,
76 OMEGA_GPU_RIGHT_GPU_READ = 1u << 2,
77 OMEGA_GPU_RIGHT_GPU_WRITE = 1u << 3,
78 OMEGA_GPU_RIGHT_GPU_EXEC = 1u << 4, /* code buffers only */
79 OMEGA_GPU_RIGHT_HOST_POLLED = 1u << 5, /* CPU polls a device write here: must be GPU-uncached */
80 OMEGA_GPU_RIGHT_ALL = (1u << 6) - 1u
81};
82
83typedef enum { OMEGA_GPU_CACHE_GPU_UNCACHED = 0, OMEGA_GPU_CACHE_GPU_CACHED = 1 } OmegaGpuCachePolicy;
84
85typedef struct {
86 uint32_t kind; /* OMEGA_GPU_HANDLE_BUF_DATA, _BUF_CODE or _BUF_QUEUE */
87 uint32_t rights; /* OMEGA_GPU_RIGHT_* mask, never 0 */
88 uint32_t gpu_cache; /* OmegaGpuCachePolicy */
89 uint32_t align; /* bytes, a power of two, 0 means the platform minimum */
90 uint64_t bytes; /* explicit length, never 0 */
91} OmegaGpuBufferDesc;
92
93/* ---- services and capabilities ------------------------------------------ */
94
95enum {
96 OMEGA_GPU_SVC_TIME = 1u << 0, /* monotonic millisecond clock */
97 OMEGA_GPU_SVC_WAIT = 1u << 1, /* bounded sleep or yield */
98 OMEGA_GPU_SVC_LOCK = 1u << 2, /* mutual exclusion for queues and tables */
99 OMEGA_GPU_SVC_MMIO = 1u << 3, /* bounded register access by device handle and region */
100 OMEGA_GPU_SVC_DMA = 1u << 4, /* device-visible memory with CPU view, GPU VA and bus address */
101 OMEGA_GPU_SVC_SYNC = 1u << 5, /* explicit CPU<->GPU visibility (cache maintenance, flush) */
102 OMEGA_GPU_SVC_COMPLETION = 1u << 6, /* POLL an uncached word (poll_word); polling is never an interrupt */
103 OMEGA_GPU_SVC_IRQ = 1u << 7, /* real device interrupt wait; polling never counts */
104 OMEGA_GPU_SVC_REQUIRED_V1 = OMEGA_GPU_SVC_TIME | OMEGA_GPU_SVC_WAIT | OMEGA_GPU_SVC_LOCK |
105 OMEGA_GPU_SVC_MMIO | OMEGA_GPU_SVC_DMA | OMEGA_GPU_SVC_SYNC |
106 OMEGA_GPU_SVC_COMPLETION,
107 /* ABI 1.1: what the persistent GPU session (src/omega_gpu_session.c) needs on top of v1 */
108 OMEGA_GPU_SVC_DEVICE = 1u << 8, /* open / close one device context, driver text of the last failure */
109 OMEGA_GPU_SVC_QUEUE = 1u << 9, /* submission queue over a BUF_QUEUE ring: open, submit, retire, close */
110 OMEGA_GPU_SVC_CLOCK_NS = 1u << 10, /* monotonic nanosecond clock (launch timing) */
111 OMEGA_GPU_SVC_HOST_MEM = 1u << 11, /* CPU-only memory for tables and staging (never device-visible) */
112 OMEGA_GPU_SVC_REQUIRED_SESSION = OMEGA_GPU_SVC_TIME | OMEGA_GPU_SVC_WAIT | OMEGA_GPU_SVC_LOCK |
113 OMEGA_GPU_SVC_DMA | OMEGA_GPU_SVC_SYNC | OMEGA_GPU_SVC_COMPLETION |
114 OMEGA_GPU_SVC_DEVICE | OMEGA_GPU_SVC_QUEUE | OMEGA_GPU_SVC_CLOCK_NS |
115 OMEGA_GPU_SVC_HOST_MEM,
116 OMEGA_GPU_SVC_ALL = (1u << 12) - 1u
117};
118
119typedef struct {
120 uint32_t abi_major;
121 uint32_t abi_minor;
122 uint32_t services; /* OMEGA_GPU_SVC_* provided (or required) */
123 uint32_t max_devices;
124 uint32_t max_buffers; /* live buffer handles */
125 uint32_t max_queues;
126 uint32_t max_inflight; /* jobs in flight per queue (concurrency limit) */
127 uint64_t max_buffer_bytes; /* one buffer */
128 uint64_t max_total_bytes; /* all live buffers */
129 uint32_t submit_prefix_words; /* ABI 1.2: most words queue_submit may put ahead of the caller's words in the ring (the Linux
130 * reference: the omega #457 L2 invalidate); a caller filling the ring budgets
131 * ring words minus this. 0 = never. Not negotiated. */
132} OmegaGpuPlatformCaps;
133
134/* ---- refusals ----------------------------------------------------------- */
135
136typedef enum {
137 OMEGA_GPU_PLAT_OK = 0,
138 OMEGA_GPU_PLAT_REFUSED_ARGS, /* NULL or malformed argument */
139 OMEGA_GPU_PLAT_REFUSED_ABI, /* major differs, or provided minor older than required */
140 OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE, /* a required service is not provided */
141 OMEGA_GPU_PLAT_REFUSED_NULL_SERVICE, /* a claimed service has no function */
142 OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE, /* index out of range or kind mismatch */
143 OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE, /* generation mismatch: closed or never opened */
144 OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH, /* zero length */
145 OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS, /* empty, unknown or contradictory rights */
146 OMEGA_GPU_PLAT_REFUSED_BAD_KIND, /* unknown kind */
147 OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY, /* host-polled buffer asked to be GPU-cached */
148 OMEGA_GPU_PLAT_REFUSED_LIMIT, /* a capability limit would be exceeded */
149 OMEGA_GPU_PLAT_REFUSED_BUSY, /* concurrency limit reached */
150 OMEGA_GPU_PLAT_REFUSED_DEVICE, /* 1.1: the device or its driver refused (diag_text says why) */
151 OMEGA_GPU_PLAT_REFUSAL_COUNT
152} OmegaGpuPlatformRefusal;
153
154const char *omega_gpu_platform_refusal_name(int refusal); /* static strings, never NULL */
155
156/* poll_word result when the word did not reach the value in time (not a refusal: nothing was wrong with the call). */
157#define OMEGA_GPU_POLL_TIMED_OUT 100
158
159/* ---- the platform service table ----------------------------------------- */
160
161typedef struct {
162 void *ctx;
163 /* TIME, WAIT, LOCK */
164 uint64_t (*time_now_ms)(void *ctx);
165 int (*wait_ms)(void *ctx, uint64_t ms);
166 int (*lock)(void *ctx, uint32_t lock_id);
167 int (*unlock)(void *ctx, uint32_t lock_id);
168 /* MMIO: bounded by (device, region); the platform refuses an offset past the region. */
169 int (*mmio_read32)(void *ctx, OmegaGpuDeviceHandle dev, uint32_t region, uint64_t offset, uint32_t *out);
170 int (*mmio_write32)(void *ctx, OmegaGpuDeviceHandle dev, uint32_t region, uint64_t offset, uint32_t value);
171 /* DMA: one allocation yields a handle plus the three views; free by handle only. */
172 int (*dma_alloc)(void *ctx, OmegaGpuDeviceHandle dev, const OmegaGpuBufferDesc *desc,
173 OmegaGpuBufferHandle *out, OmegaCpuView *cpu, OmegaGpuVa *gpu, OmegaGpuBusAddr *bus);
174 int (*dma_free)(void *ctx, OmegaGpuBufferHandle buf);
175 /* SYNC: explicit visibility over a byte range of one buffer. */
176 int (*sync_cpu_to_gpu)(void *ctx, OmegaGpuBufferHandle buf, uint64_t offset, uint64_t len);
177 int (*sync_gpu_to_cpu)(void *ctx, OmegaGpuBufferHandle buf, uint64_t offset, uint64_t len);
178 /* COMPLETION, by POLLING (never an interrupt): read one GPU-uncached 32-bit word of a HOST_POLLED buffer until
179 * it has reached expected in serial-number order, (int32_t)(word - expected) >= 0 (the m16_native_wait_marker
180 * rule), or timeout_ms passes. 0 reached, OMEGA_GPU_POLL_TIMED_OUT not reached, else a refusal. *observed is
181 * the last value read. A timeout says nothing about the device: the work may still be running. */
182 int (*poll_word)(void *ctx, OmegaGpuBufferHandle buf, uint64_t offset, uint32_t expected,
183 uint64_t timeout_ms, uint32_t *observed);
184 /* IRQ: NULL unless OMEGA_GPU_SVC_IRQ is claimed. */
185 int (*irq_wait)(void *ctx, OmegaGpuDeviceHandle dev, uint64_t timeout_ms);
186 /* ---- ABI 1.1 ---- */
187 /* DEVICE: open the device context with this ordinal (0 = the one GB10), close it. Closing makes every buffer and
188 * queue handle of the device stale. diag_text: driver text of the most recent failure, "" if none, never NULL. */
189 int (*device_open)(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *out);
190 int (*device_close)(void *ctx, OmegaGpuDeviceHandle dev);
191 const char *(*diag_text)(void *ctx);
192 /* QUEUE: a submission queue whose ring is a BUF_QUEUE buffer of the same device. queue_submit copies nwords method
193 * words into the ring and rings the doorbell (one GPFIFO entry); queue_retire marks every submitted entry consumed
194 * and is called only after the caller saw that work complete. A refused queue_submit rang no doorbell: nothing of it is in
195 * flight (m16_native_submit_methods fails before nvrm_ring). */
196 int (*queue_open)(void *ctx, OmegaGpuDeviceHandle dev, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *out);
197 int (*queue_close)(void *ctx, OmegaGpuQueueHandle q);
198 int (*queue_submit)(void *ctx, OmegaGpuQueueHandle q, const uint32_t *words, uint32_t nwords);
199 int (*queue_retire)(void *ctx, OmegaGpuQueueHandle q);
200 /* CLOCK_NS */
201 uint64_t (*time_now_ns)(void *ctx);
202 /* HOST_MEM: NULL on refusal; host_free(NULL) is a no-op. */
203 void *(*host_alloc)(void *ctx, size_t bytes);
204 void (*host_free)(void *ctx, void *p);
205} OmegaGpuPlatform;
206
207/* ---- contract checks (pure, freestanding) -------------------------------- */
208
209/* Version and service negotiation. required.services must be a subset of
210 * provided.services, required limits must not exceed provided limits. */
211int omega_gpu_platform_negotiate(const OmegaGpuPlatformCaps *provided, const OmegaGpuPlatformCaps *required);
212
213/* Every service in caps->services has a function in p; every function
214 * outside caps->services is NULL (no undeclared service, no fallback); the
215 * v1 required set is present. */
216int omega_gpu_platform_check(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps);
217/* The same, requiring the service set `required` (for example OMEGA_GPU_SVC_REQUIRED_SESSION) instead of the v1 set.
218 * omega_gpu_platform_check(p, caps) is omega_gpu_platform_check_for(p, caps, OMEGA_GPU_SVC_REQUIRED_V1). */
219int omega_gpu_platform_check_for(const OmegaGpuPlatform *p, const OmegaGpuPlatformCaps *caps, uint32_t required);
220
221/* Buffer descriptor rules: kind known, length non-zero and within caps,
222 * rights non-empty and consistent with the kind, host-polled means
223 * GPU-uncached, align a power of two. */
224int omega_gpu_platform_check_buffer(const OmegaGpuBufferDesc *d, const OmegaGpuPlatformCaps *caps);
225
226/* ---- handle table (index + generation) ----------------------------------- */
227
228#define OMEGA_GPU_HANDLE_SLOTS 4096u /* 1.1: a serving session holds one buffer per weight tensor (Qwen3-8B: ~260);
229 * speed5/batching: each Qwen3.8 conversation slot adds about 29 (16 KV planes, the
230 * DeltaNet state, launch buffers), so 32 slots on top of the 27B weights passed 1024 */
231
232typedef struct {
233 uint32_t generation[OMEGA_GPU_HANDLE_SLOTS]; /* next generation to issue for the slot; starts at 1 */
234 uint32_t kind[OMEGA_GPU_HANDLE_SLOTS];
235 uint64_t bytes[OMEGA_GPU_HANDLE_SLOTS];
236 uint8_t live[OMEGA_GPU_HANDLE_SLOTS];
237 uint32_t nlive;
238 uint64_t total_bytes;
239} OmegaGpuHandleTable;
240
241void omega_gpu_handles_init(OmegaGpuHandleTable *t);
242/* Opens a slot of the given kind. limit_slots caps live handles of that kind.
243 * bytes is accounted against limit_bytes across all slots; devices and queues
244 * pass bytes 0 and are not byte-accounted. */
245int omega_gpu_handles_open(OmegaGpuHandleTable *t, uint32_t kind, uint64_t bytes, uint32_t limit_slots,
246 uint64_t limit_bytes, uint32_t *index, uint32_t *generation);
247/* OK only for a live slot whose generation and kind match. */
248int omega_gpu_handles_check(const OmegaGpuHandleTable *t, uint32_t index, uint32_t generation, uint32_t kind);
249/* Closes a live, matching slot and bumps its generation so the old handle is stale. */
250int omega_gpu_handles_close(OmegaGpuHandleTable *t, uint32_t index, uint32_t generation, uint32_t kind);
251
252#endif /* OMEGA_GPU_PLATFORM_H */