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
CUDApp / src / omega_gpu_platform_aien.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_platform_aien.c -- OmegaGpuPlatform 1.2 over aien-gpu.ko (/dev/aien-gpu, compute ABI 1). See the header for |
| 3 | * the call map, the evidence level (IMPLEMENTED, hardware NOT_RUN) and how it is selected. The contract, caps and |
| 4 | * refusals are those of src/omega_gpu_platform_linux.c; the submission path is nvrm_enqueue + nvrm_ring |
| 5 | * (nvrm/nvrm.c) written against memory and a doorbell page this platform got from aien-gpu.ko instead of RM. |
| 6 | * |
| 7 | * Decisions and request bytes: native/aiengpu/context.om (CUDApp#128), called through its bridged export |
| 8 | * omega_aiengpu (src/cudapp_om_aiengpu.c, regenerated by om/units.sh). The C those rules replaced stays behind |
| 9 | * CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN (OFF) as the differential oracle; see "Decisions and request bytes" below. |
| 10 | * |
| 11 | * Effects: native/aiengpu/effects.om (open, the five AGPU ioctls, the mappings, the ordered ring/USERD/doorbell |
| 12 | * stores) through the bridge's explicit mapping context (src/cudapp_aiengpu_effects.c, regenerated by |
| 13 | * native/aiengpu/units.sh); the default on AArch64. The C system calls stay behind |
| 14 | * CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS (OFF on AArch64) as the oracle; see "Effects" below. Still C: the |
| 15 | * diagnostic text, the poll/barrier helpers and |
| 16 | * the OmegaGpuPlatform function table (seeded Omega has no text constants or C function-table export; extension |
| 17 | * proposal aien-dev/omega-bridge#29). The platform exists only when CUDAPP_MIGRATION_PENDING_AIEN_GPU=1; it is OFF |
| 18 | * in libomega_gpu.a and every other build, and ON only for tests/test_omega_gpu_platform_aien*.c and their mutants |
| 19 | * (mk/omega-gpu-engine.mk). |
| 20 | */ |
| 21 | #ifndef CUDAPP_MIGRATION_PENDING_AIEN_GPU |
| 22 | #define CUDAPP_MIGRATION_PENDING_AIEN_GPU 0 /* migration-pending switch, OFF by default (see above) */ |
| 23 | #endif |
| 24 | #if CUDAPP_MIGRATION_PENDING_AIEN_GPU |
| 25 | #include "omega_gpu_platform_aien.h" |
| 26 | |
| 27 | #include "omega_gpu_mem_reserve.h" |
| 28 | #include "aien_gpu_uapi.h" |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <pthread.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <time.h> |
| 39 | #include <stddef.h> |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | #define AIEN_BUFFERS (OMEGA_GPU_HANDLE_SLOTS - 2u) /* one slot each for the device and the queue */ |
| 43 | #define AIEN_PAGE 4096ull |
| 44 | |
| 45 | typedef struct { |
| 46 | void *cpu; /* == GPU VA */ |
| 47 | uint64_t size; /* bytes mapped, page rounded */ |
| 48 | uint32_t handle; /* AGPU_MEM_ALLOC handle */ |
| 49 | } AienMem; |
| 50 | |
| 51 | typedef struct { |
| 52 | pthread_mutex_t mu; /* table, device and diagnostics */ |
| 53 | pthread_mutex_t locks[OMEGA_GPU_PLATFORM_AIEN_LOCKS]; |
| 54 | int ready; |
| 55 | OmegaGpuHandleTable t; |
| 56 | AienMem mem[OMEGA_GPU_HANDLE_SLOTS]; |
| 57 | uint32_t rights[OMEGA_GPU_HANDLE_SLOTS]; |
| 58 | uint8_t gpu_cached[OMEGA_GPU_HANDLE_SLOTS]; |
| 59 | int l2_inval_pending; /* omega #457, as the Linux reference */ |
| 60 | const OmegaGpuAienSys *sys; |
| 61 | int fd; |
| 62 | OmegaGpuDeviceHandle dev; /* generation 0 = closed */ |
| 63 | OmegaGpuQueueHandle queue; |
| 64 | uint64_t va_next; /* VA window cursor (mem_map) */ |
| 65 | uint32_t ring_index; /* slot of the caller's BUF_QUEUE buffer (the pushbuffer) */ |
| 66 | AienMem fifo, notifier; /* GPFIFO ring + USERD, error notifier */ |
| 67 | uint32_t channel, token; |
| 68 | volatile uint8_t *doorbell; |
| 69 | uint32_t put, retired; /* entries enqueued / seen complete, monotonic (nvrm.h) */ |
| 70 | char err[256]; /* driver text of the last REFUSED_DEVICE */ |
| 71 | char diag[320]; |
| 72 | } AienRef; |
| 73 | |
| 74 | /* ---- Effects ------------------------------------------------------------------------------------------------------ |
| 75 | * native/aiengpu/effects.om performs the device open/close, the five AGPU ioctls, the CPU mappings (reserve, bind |
| 76 | * over the reservation, the doorbell page, drop) and the ordered submit stores, through the bridge's explicit mapping |
| 77 | * context (generated by native/aiengpu/units.sh into cudapp_aiengpu_effects.c / cudapp_aiengpu_native.h; outside |
| 78 | * the src/cudapp_om_*.c binding glob, so only the aien-gpu platform's builds link it). |
| 79 | * MIGRATION-PENDING C: CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS=1 selects the C system calls below, kept as |
| 80 | * the differential oracle (mk/omega-gpu-engine.mk test-gpu-platform-aien-effects). It is OFF on AArch64. It stays ON |
| 81 | * on other hosts: the unit needs the OM-2 compiler carrying the aien-gpu devctl allowlist (omega-mv#1047, omc sha256 |
| 82 | * b8f8f137...), which targets aarch64linux only, and aien-gpu.ko itself exists only for the AArch64 GB10. |
| 83 | * Limitation of the Omega effects: one mapping is at most 64 GiB (maphold/mapnew bound), where the C path maps up to |
| 84 | * the platform's own limit. */ |
| 85 | #ifndef CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS |
| 86 | #if defined(__aarch64__) |
| 87 | #define CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS 0 |
| 88 | #else |
| 89 | #define CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS 1 |
| 90 | #endif |
| 91 | #endif |
| 92 | #if !CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS |
| 93 | #if !defined(__aarch64__) |
| 94 | #error "the Omega aien-gpu effects are generated for Linux AArch64 only; build with CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS=1" |
| 95 | #endif |
| 96 | #include "cudapp_aiengpu_native.h" |
| 97 | |
| 98 | /* Owner of every mapping the unit made: the CPU address and size the platform knows it by, and the unit's handle. |
| 99 | * Buffers, the GPFIFO buffer, the notifier and the doorbell page: OMEGA_GPU_HANDLE_SLOTS + 1 at most. Accessed only |
| 100 | * with r->mu held (every sys call is). */ |
| 101 | #define AIEN_OM_MAPS (OMEGA_GPU_HANDLE_SLOTS + 2u) |
| 102 | typedef struct { uint64_t addr, size, handle; } AienOmMap; |
| 103 | static struct { |
| 104 | omega_native_context *ctx; |
| 105 | uint32_t n; |
| 106 | AienOmMap m[AIEN_OM_MAPS]; |
| 107 | } g_om; |
| 108 | |
| 109 | /* A unit result as a Linux one: status != 0 (a trap or a refused argument at the bridge) is EPROTO; a negative |
| 110 | * result is -errno. */ |
| 111 | static int om_failed(uint32_t status, uint64_t result) { |
| 112 | if (status != 0) { errno = EPROTO; return 1; } |
| 113 | if ((int64_t)result < 0) { errno = (int)-(int64_t)result; return 1; } |
| 114 | return 0; |
| 115 | } |
| 116 | static AienOmMap *om_find(uint64_t addr) { |
| 117 | for (uint32_t i = 0; i < g_om.n; i++) |
| 118 | if (g_om.m[i].addr == addr) return &g_om.m[i]; |
| 119 | return NULL; |
| 120 | } |
| 121 | static void om_forget(AienOmMap *m) { *m = g_om.m[--g_om.n]; } |
| 122 | static int om_ctx(void) { |
| 123 | if (g_om.ctx) return 0; |
| 124 | if (omega_native_context_create(AIEN_OM_MAPS, &g_om.ctx) != 0) { g_om.ctx = NULL; errno = ENOMEM; return -1; } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int sys_open(const char *path, int flags) { |
| 129 | uint64_t res = 0; |
| 130 | if (flags != (O_RDWR | O_CLOEXEC)) { errno = EINVAL; return -1; } /* the unit opens O_RDWR|O_CLOEXEC only */ |
| 131 | if (om_ctx() != 0) return -1; |
| 132 | if (om_failed(omega2_ctx_aiengpu_open(g_om.ctx, &res, (const uint8_t *)path, strlen(path)), res)) return -1; |
| 133 | return (int)res; |
| 134 | } |
| 135 | static int sys_close(int fd) { |
| 136 | uint64_t res = 0; |
| 137 | const int bad = om_failed(omega2_ctx_aiengpu_close(g_om.ctx, &res, (uint64_t)(int64_t)fd), res); |
| 138 | if (g_om.n == 0 && g_om.ctx && omega_native_context_destroy(g_om.ctx) == 0) g_om.ctx = NULL; |
| 139 | return bad ? -1 : 0; |
| 140 | } |
| 141 | static int sys_ioctl(int fd, unsigned long req, void *arg) { |
| 142 | uint64_t res = 0; |
| 143 | if (om_failed(omega2_ctx_aiengpu_ioctl(g_om.ctx, &res, (uint64_t)(int64_t)fd, req, arg, _IOC_SIZE(req)), res)) return -1; |
| 144 | return (int)res; |
| 145 | } |
| 146 | /* The three mappings the platform makes: a PROT_NONE MAP_FIXED_NOREPLACE reservation (mem_map), the object bound |
| 147 | * MAP_FIXED|MAP_SHARED over a reservation (mem_map), the doorbell page anywhere (channel_create). Anything else is |
| 148 | * EINVAL. */ |
| 149 | static void *sys_map(void *addr, size_t len, int prot, int flags, int fd, uint64_t off) { |
| 150 | uint64_t res = 0; |
| 151 | const uint64_t at = (uint64_t)(uintptr_t)addr; |
| 152 | if (prot == PROT_NONE && fd == -1 && (flags & MAP_FIXED_NOREPLACE)) { |
| 153 | if (g_om.n == AIEN_OM_MAPS) { errno = ENOMEM; return MAP_FAILED; } |
| 154 | if (om_failed(omega2_ctx_aiengpu_reserve(g_om.ctx, &res, at, len), res)) return MAP_FAILED; |
| 155 | g_om.m[g_om.n++] = (AienOmMap){at, len, res}; |
| 156 | return addr; |
| 157 | } |
| 158 | if (prot == (PROT_READ | PROT_WRITE) && flags == (MAP_FIXED | MAP_SHARED) && addr) { |
| 159 | AienOmMap *m = om_find(at); |
| 160 | if (!m || m->size != len) { errno = EINVAL; return MAP_FAILED; } |
| 161 | const uint32_t st = omega2_ctx_aiengpu_bind(g_om.ctx, &res, m->handle, (uint64_t)(int64_t)fd, off); |
| 162 | if (st == 0 && (int64_t)res < 0) om_forget(m); /* the failed bind retired the reservation */ |
| 163 | return om_failed(st, res) ? MAP_FAILED : addr; /* status != 0: quarantined, kept for drop */ |
| 164 | } |
| 165 | if (prot == (PROT_READ | PROT_WRITE) && flags == MAP_SHARED && !addr) { |
| 166 | if (g_om.n == AIEN_OM_MAPS) { errno = ENOMEM; return MAP_FAILED; } |
| 167 | if (om_failed(omega2_ctx_aiengpu_map_file(g_om.ctx, &res, (uint64_t)(int64_t)fd, off, len), res)) return MAP_FAILED; |
| 168 | const uint64_t handle = res; |
| 169 | if (omega2_ctx_aiengpu_address(g_om.ctx, &res, handle) != 0) { |
| 170 | uint64_t ignore = 0; |
| 171 | (void)omega2_ctx_aiengpu_drop(g_om.ctx, &ignore, handle); |
| 172 | errno = EPROTO; |
| 173 | return MAP_FAILED; |
| 174 | } |
| 175 | g_om.m[g_om.n++] = (AienOmMap){res, len, handle}; |
| 176 | return (void *)(uintptr_t)res; |
| 177 | } |
| 178 | errno = EINVAL; |
| 179 | return MAP_FAILED; |
| 180 | } |
| 181 | static int sys_unmap(void *addr, size_t len) { |
| 182 | uint64_t res = 0; |
| 183 | AienOmMap *m = om_find((uint64_t)(uintptr_t)addr); |
| 184 | if (!m || m->size != len) { errno = EINVAL; return -1; } |
| 185 | if (om_failed(omega2_ctx_aiengpu_drop(g_om.ctx, &res, m->handle), res)) return -1; |
| 186 | om_forget(m); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* The ordered stores of one submit: pushbuffer (plan prefix, then the words), then fence, GP entry, USERD GPPut and |
| 191 | * the doorbell token, each by the unit through the owned mappings. A refusal is a defect (the plan was checked and |
| 192 | * every mapping is ours), never a device answer. */ |
| 193 | static void om_publish(const uint8_t plan[128], const void *pb, const void *fifo, const volatile void *db, |
| 194 | const uint32_t *words, uint32_t nwords, uint32_t token) { |
| 195 | const AienOmMap *p = om_find((uint64_t)(uintptr_t)pb), *f = om_find((uint64_t)(uintptr_t)fifo), |
| 196 | *d = om_find((uint64_t)(uintptr_t)db); |
| 197 | uint64_t res = 0; |
| 198 | uint32_t st = 1; |
| 199 | if (p && f && d) { |
| 200 | st = omega2_ctx_aiengpu_push(g_om.ctx, &res, p->handle, plan, 128, (const uint8_t *)words, (uint64_t)nwords * 4u); |
| 201 | if (st == 0 && res == 0) st = omega2_ctx_aiengpu_ring(g_om.ctx, &res, f->handle, d->handle, plan, 128, token); |
| 202 | } |
| 203 | if (st != 0 || res != 0) { |
| 204 | fprintf(stderr, "omega_aiengpu publish refused: status %u result %lld\n", st, (long long)res); |
| 205 | abort(); |
| 206 | } |
| 207 | } |
| 208 | #else |
| 209 | static int sys_open(const char *path, int flags) { return open(path, flags); } |
| 210 | static int sys_close(int fd) { return close(fd); } |
| 211 | static int sys_ioctl(int fd, unsigned long req, void *arg) { return ioctl(fd, req, arg); } |
| 212 | static void *sys_map(void *addr, size_t len, int prot, int flags, int fd, uint64_t off) { |
| 213 | return mmap(addr, len, prot, flags, fd, (off_t)off); |
| 214 | } |
| 215 | static int sys_unmap(void *addr, size_t len) { return munmap(addr, len); } |
| 216 | #endif |
| 217 | static const OmegaGpuAienSys g_real_sys = { sys_open, sys_close, sys_ioctl, sys_map, sys_unmap }; |
| 218 | |
| 219 | static AienRef g_ref = { |
| 220 | .mu = PTHREAD_MUTEX_INITIALIZER, |
| 221 | .locks = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER}, |
| 222 | .fd = -1, |
| 223 | }; |
| 224 | |
| 225 | /* ---- Decisions and request bytes --------------------------------------------------------------------------------- |
| 226 | * Every rule and every byte this platform hands the module comes from native/aiengpu/context.om (export |
| 227 | * omega_aiengpu, src/cudapp_om_aiengpu.c): the AGPU_INFO verdict, the page rounding and size bound, the VA window |
| 228 | * first fit, the MEM_ALLOC/MEM_FREE/CHAN_CREATE/CHAN_DESTROY request bytes, the RM verdict, the cache policy, the |
| 229 | * range and poll checks, the serial compare, the submission plan (omega #457 prefix, GPFIFO entry, ring slot, GPPut, |
| 230 | * ring-full rule) and the GP entry. This file keeps the effects the unit cannot perform yet: open, ioctl, mmap, |
| 231 | * the ordered stores to the ring, USERD and the doorbell, and the diagnostic text. |
| 232 | * MIGRATION-PENDING C: CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN=1 selects the C these rules replaced. It is OFF by |
| 233 | * default and kept only as the differential oracle (mk/omega-gpu-engine.mk test-gpu-platform-aien-diff). */ |
| 234 | #ifndef CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN |
| 235 | #define CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN 0 |
| 236 | #endif |
| 237 | |
| 238 | /* One AGPU request/reply block: the unit writes it by bytes, the module reads and answers it by struct. */ |
| 239 | typedef union { |
| 240 | uint8_t b[128]; |
| 241 | struct agpu_info info; |
| 242 | struct agpu_mem_alloc alloc; |
| 243 | struct agpu_mem_free free; |
| 244 | struct agpu_chan_create chan; |
| 245 | struct agpu_chan_destroy destroy; |
| 246 | } AienReq; |
| 247 | _Static_assert(sizeof(AienReq) == 128, "the unit's request block is 128 bytes"); |
| 248 | |
| 249 | /* Submission plan written into an AienReq by AG_SUBMIT: prefix words at 0, then these little-endian fields. */ |
| 250 | #define PLAN_GP 24u /* u64 GPFIFO entry */ |
| 251 | #define PLAN_SLOT 32u /* u32 ring slot */ |
| 252 | #define PLAN_GPPUT 36u /* u32 new USERD GPPut */ |
| 253 | #define PLAN_PRE 40u /* u32 omega #457 prefix words */ |
| 254 | #define PLAN_TOTAL 44u /* u32 words in the entry */ |
| 255 | #define L2_INVAL_PREFIX_WORDS 5u |
| 256 | |
| 257 | /* Operation numbers of native/aiengpu/context.om (m[0]); the unit's header comment gives each one's arguments. */ |
| 258 | enum { AG_INFO = 1, AG_MEM_SIZE, AG_VA_START, AG_VA_ALIGN, AG_VA_TRY, AG_VA_BASE, AG_VA_TOP, AG_VA_STEP, AG_REQ_ALLOC, |
| 259 | AG_RM_OK, AG_REQ_FREE, AG_REQ_CHAN, AG_REQ_DESTROY, AG_CACHE, AG_RANGE, AG_POLL, AG_SERIAL_GE, AG_SUBMIT, |
| 260 | AG_GP_ENTRY }; |
| 261 | enum { VA_TRY_RESERVE = 0, VA_TRY_STOP = 1, VA_TRY_WRAP = 2 }; |
| 262 | enum { SUBMIT_OK = 0, SUBMIT_ARGS = 1, SUBMIT_LENGTH = 2, SUBMIT_FULL = 3 }; |
| 263 | enum { POLL_OK = 0, POLL_RIGHTS = 1, POLL_ALIGN = 2, POLL_RANGE = 3 }; |
| 264 | |
| 265 | static uint32_t plan32(const AienReq *q, uint32_t off) { uint32_t v; memcpy(&v, q->b + off, sizeof v); return v; } |
| 266 | static uint64_t plan64(const AienReq *q, uint32_t off) { uint64_t v; memcpy(&v, q->b + off, sizeof v); return v; } |
| 267 | |
| 268 | #if !CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN |
| 269 | #include "cudapp_native.h" |
| 270 | |
| 271 | /* One call of the unit. It is total over every argument this file passes, so a bridge status (trap, bad argument) |
| 272 | * is a build defect, never a device answer: abort rather than guess. */ |
| 273 | static uint64_t ag_call(AienReq *q, const uint64_t m[16]) { |
| 274 | AienReq scratch; |
| 275 | uint64_t res = 0; |
| 276 | if (!q) { memset(&scratch, 0, sizeof scratch); q = &scratch; } |
| 277 | const uint32_t s = omega_aiengpu(&res, m, 16, q->b, sizeof q->b); |
| 278 | if (s != 0) { |
| 279 | fprintf(stderr, "omega_aiengpu op %llu: bridge status %u\n", (unsigned long long)m[0], s); |
| 280 | abort(); |
| 281 | } |
| 282 | return res; |
| 283 | } |
| 284 | #define AG(q, ...) ag_call((q), (const uint64_t[16]){__VA_ARGS__}) |
| 285 | |
| 286 | static int ag_info_check(AienReq *q) { return (int)AG(q, AG_INFO); } |
| 287 | static uint64_t ag_mem_size(uint64_t bytes) { return AG(NULL, AG_MEM_SIZE, bytes); } |
| 288 | static uint64_t ag_va_start(uint64_t cursor) { return AG(NULL, AG_VA_START, cursor); } |
| 289 | static uint64_t ag_va_align(uint64_t x, uint64_t align) { return AG(NULL, AG_VA_ALIGN, x, align); } |
| 290 | static int ag_va_try(uint64_t at, uint64_t size, int wrapped, uint64_t start) { |
| 291 | return (int)AG(NULL, AG_VA_TRY, at, size, (uint64_t)wrapped, start); |
| 292 | } |
| 293 | static uint64_t ag_va_base(void) { return AG(NULL, AG_VA_BASE); } |
| 294 | static uint64_t ag_va_top(void) { return AG(NULL, AG_VA_TOP); } |
| 295 | static uint64_t ag_va_step(uint64_t at, uint64_t size, uint64_t align) { return AG(NULL, AG_VA_STEP, at, size, align); } |
| 296 | static void ag_req_alloc(AienReq *q, uint64_t size, uint64_t va, uint32_t flags) { (void)AG(q, AG_REQ_ALLOC, size, va, flags); } |
| 297 | static int ag_rm_ok(AienReq *q, uint32_t st_off) { return AG(q, AG_RM_OK, st_off) == 1u; } |
| 298 | static void ag_req_free(AienReq *q, uint32_t handle) { (void)AG(q, AG_REQ_FREE, handle); } |
| 299 | static void ag_req_chan(AienReq *q, uint32_t fifo, uint32_t notifier) { (void)AG(q, AG_REQ_CHAN, fifo, notifier); } |
| 300 | static void ag_req_destroy(AienReq *q, uint32_t channel) { (void)AG(q, AG_REQ_DESTROY, channel); } |
| 301 | static int ag_cache(uint32_t kind, uint32_t rights, uint32_t cache) { return (int)AG(NULL, AG_CACHE, kind, rights, cache); } |
| 302 | static int ag_range(uint64_t size, uint64_t off, uint64_t len) { return AG(NULL, AG_RANGE, size, off, len) == 1u; } |
| 303 | static int ag_poll(uint32_t rights, uint64_t off, uint64_t size) { return (int)AG(NULL, AG_POLL, rights, off, size); } |
| 304 | static int ag_serial_ge(uint32_t w, uint32_t target) { return AG(NULL, AG_SERIAL_GE, w, target) == 1u; } |
| 305 | static int ag_submit(AienReq *plan, uint32_t nwords, int have_words, int pending, uint64_t pb_size, uint32_t put, |
| 306 | uint32_t retired, uint64_t pb_va) { |
| 307 | return (int)AG(plan, AG_SUBMIT, nwords, (uint64_t)have_words, (uint64_t)pending, pb_size, put, retired, pb_va); |
| 308 | } |
| 309 | uint64_t omega_gpu_aien_gp_entry(uint64_t va, uint32_t nwords) { return AG(NULL, AG_GP_ENTRY, va, nwords); } |
| 310 | #else /* CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN: the C oracle, unchanged rules */ |
| 311 | /* Every buffer lives in [64 GiB, 1 TiB): a GPFIFO entry carries 40 address bits (GET 31:2, GET_HI 39:32; |
| 312 | * nvrm_gp_entry puts LEVEL at bit 41 and LENGTH at 42), and the kernel's own choice on arm64 is near 2^48. The |
| 313 | * window is below AGPU_MEM_ALLOC's 2^48 limit and above GSP's [4 GiB, 4.5 GiB). */ |
| 314 | #define AIEN_VA_BASE (64ull << 30) |
| 315 | #define AIEN_VA_TOP (1ull << 40) |
| 316 | #define AIEN_GP_MAX_WORDS 0x1fffffu /* LENGTH field, 21 bits */ |
| 317 | /* omega #457: the same L2_SYSMEM_NCOH_INVALIDATE MEM_OP prefix as the Linux reference. */ |
| 318 | static const uint32_t L2_INVAL_PREFIX[] = { (2u << 28) | (4u << 16) | (0x0028u >> 2), 0, 0, 0, 0x11u << 27 }; |
| 319 | _Static_assert(sizeof L2_INVAL_PREFIX / sizeof L2_INVAL_PREFIX[0] == L2_INVAL_PREFIX_WORDS, "prefix length"); |
| 320 | |
| 321 | static int ag_info_check(AienReq *q) { |
| 322 | return q->info.abi != AGPU_ABI_VERSION ? 1 : q->info.state != AGPU_STATE_RM_READY ? 2 : 0; |
| 323 | } |
| 324 | static uint64_t ag_mem_size(uint64_t bytes) { |
| 325 | if (bytes == 0 || bytes > (1ull << 37)) return 0; |
| 326 | return (bytes + AIEN_PAGE - 1u) & ~(AIEN_PAGE - 1u); |
| 327 | } |
| 328 | static uint64_t ag_va_start(uint64_t cursor) { return (cursor < AIEN_VA_BASE || cursor >= AIEN_VA_TOP) ? AIEN_VA_BASE : cursor; } |
| 329 | static uint64_t ag_va_align(uint64_t x, uint64_t align) { |
| 330 | if (align < AIEN_PAGE) align = AIEN_PAGE; |
| 331 | return (x + align - 1u) & ~(align - 1u); |
| 332 | } |
| 333 | static int ag_va_try(uint64_t at, uint64_t size, int wrapped, uint64_t start) { |
| 334 | if (at + size > AIEN_VA_TOP) return wrapped ? VA_TRY_STOP : VA_TRY_WRAP; |
| 335 | return (wrapped && at >= start) ? VA_TRY_STOP : VA_TRY_RESERVE; |
| 336 | } |
| 337 | static uint64_t ag_va_base(void) { return AIEN_VA_BASE; } |
| 338 | static uint64_t ag_va_top(void) { return AIEN_VA_TOP; } |
| 339 | static uint64_t ag_va_step(uint64_t at, uint64_t size, uint64_t align) { return ag_va_align(at + size, align); } |
| 340 | static void ag_req_alloc(AienReq *q, uint64_t size, uint64_t va, uint32_t flags) { |
| 341 | memset(q, 0, sizeof *q); |
| 342 | q->alloc.size = size; q->alloc.va = va; q->alloc.flags = flags; |
| 343 | } |
| 344 | static int ag_rm_ok(AienReq *q, uint32_t st_off) { |
| 345 | struct agpu_rm_status st; |
| 346 | memcpy(&st, q->b + st_off, sizeof st); |
| 347 | return st.verdict == AGPU_RM_OK; |
| 348 | } |
| 349 | static void ag_req_free(AienReq *q, uint32_t handle) { memset(q, 0, sizeof *q); q->free.handle = handle; } |
| 350 | static void ag_req_chan(AienReq *q, uint32_t fifo, uint32_t notifier) { |
| 351 | memset(q, 0, sizeof *q); |
| 352 | q->chan.ring_handle = fifo; |
| 353 | q->chan.entries = OMEGA_GPU_AIEN_GP_ENTRIES; |
| 354 | q->chan.ring_off = 0; |
| 355 | q->chan.userd_handle = fifo; |
| 356 | q->chan.err_handle = notifier; |
| 357 | q->chan.userd_off = OMEGA_GPU_AIEN_USERD_OFF; |
| 358 | } |
| 359 | static void ag_req_destroy(AienReq *q, uint32_t channel) { memset(q, 0, sizeof *q); q->destroy.channel = channel; } |
| 360 | /* The Linux reference's rule: GPU-cached only for device-private DATA buffers (no CPU_READ, no HOST_POLLED). */ |
| 361 | static int ag_cache(uint32_t kind, uint32_t rights, uint32_t cache) { |
| 362 | if (cache != OMEGA_GPU_CACHE_GPU_CACHED) return AGPU_MEM_GPU_UNCACHED; |
| 363 | if (kind != OMEGA_GPU_HANDLE_BUF_DATA || (rights & (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_HOST_POLLED))) return 2; |
| 364 | return 0; |
| 365 | } |
| 366 | static int ag_range(uint64_t size, uint64_t off, uint64_t len) { return !(len == 0u || off > size || len > size - off); } |
| 367 | static int ag_poll(uint32_t rights, uint64_t off, uint64_t size) { |
| 368 | if (!(rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return POLL_RIGHTS; |
| 369 | if (off & 3u) return POLL_ALIGN; |
| 370 | return ag_range(size, off, 4u) ? POLL_OK : POLL_RANGE; |
| 371 | } |
| 372 | static int ag_serial_ge(uint32_t w, uint32_t target) { return (int32_t)(w - target) >= 0; } |
| 373 | uint64_t omega_gpu_aien_gp_entry(uint64_t va, uint32_t nwords) { |
| 374 | return (va & ~3ull) | ((uint64_t)(nwords & AIEN_GP_MAX_WORDS) << 42) | (1ull << 41); |
| 375 | } |
| 376 | static int ag_submit(AienReq *plan, uint32_t nwords, int have_words, int pending, uint64_t pb_size, uint32_t put, |
| 377 | uint32_t retired, uint64_t pb_va) { |
| 378 | if (!have_words || nwords == 0u) return SUBMIT_ARGS; |
| 379 | const uint32_t pre = pending ? L2_INVAL_PREFIX_WORDS : 0u; |
| 380 | const uint64_t total = (uint64_t)nwords + pre; |
| 381 | if (total * 4u > pb_size || total > AIEN_GP_MAX_WORDS) return SUBMIT_LENGTH; |
| 382 | if (put - retired >= OMEGA_GPU_AIEN_GP_ENTRIES - 1u) return SUBMIT_FULL; |
| 383 | memset(plan, 0, sizeof *plan); |
| 384 | if (pre) memcpy(plan->b, L2_INVAL_PREFIX, sizeof L2_INVAL_PREFIX); |
| 385 | const uint64_t gp = omega_gpu_aien_gp_entry(pb_va, (uint32_t)total); |
| 386 | const uint32_t slot = put % OMEGA_GPU_AIEN_GP_ENTRIES, gpput = (put + 1u) % OMEGA_GPU_AIEN_GP_ENTRIES, t32 = (uint32_t)total; |
| 387 | memcpy(plan->b + PLAN_GP, &gp, 8); memcpy(plan->b + PLAN_SLOT, &slot, 4); memcpy(plan->b + PLAN_GPPUT, &gpput, 4); |
| 388 | memcpy(plan->b + PLAN_PRE, &pre, 4); memcpy(plan->b + PLAN_TOTAL, &t32, 4); |
| 389 | return SUBMIT_OK; |
| 390 | } |
| 391 | #endif /* CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN */ |
| 392 | |
| 393 | static const OmegaGpuPlatformCaps g_caps = { |
| 394 | .submit_prefix_words = L2_INVAL_PREFIX_WORDS, |
| 395 | .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, |
| 396 | .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, |
| 397 | .services = OMEGA_GPU_SVC_REQUIRED_SESSION, /* no MMIO (the module owns the registers), no IRQ (polling only) */ |
| 398 | .max_devices = 1, |
| 399 | .max_buffers = AIEN_BUFFERS, |
| 400 | .max_queues = 1, |
| 401 | .max_inflight = 1, |
| 402 | .max_buffer_bytes = 1ull << 37, |
| 403 | .max_total_bytes = 1ull << 37, |
| 404 | }; |
| 405 | |
| 406 | static void ref_lock(AienRef *r) { |
| 407 | pthread_mutex_lock(&r->mu); |
| 408 | if (!r->ready) { omega_gpu_handles_init(&r->t); if (!r->sys) r->sys = &g_real_sys; r->ready = 1; } |
| 409 | } |
| 410 | static void ref_unlock(AienRef *r) { pthread_mutex_unlock(&r->mu); } |
| 411 | |
| 412 | int omega_gpu_platform_aien_set_sys(const OmegaGpuAienSys *sys) { |
| 413 | AienRef *r = &g_ref; |
| 414 | ref_lock(r); |
| 415 | int rc = r->dev.generation != 0u ? OMEGA_GPU_PLAT_REFUSED_BUSY : OMEGA_GPU_PLAT_OK; |
| 416 | if (rc == OMEGA_GPU_PLAT_OK) r->sys = sys ? sys : &g_real_sys; |
| 417 | ref_unlock(r); |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | /* Records why the call failed; REFUSED_DEVICE carries the driver text (r->err). Called with r->mu held. */ |
| 422 | static int refuse(AienRef *r, int rc, const char *stage) { |
| 423 | if (rc == OMEGA_GPU_PLAT_REFUSED_DEVICE) |
| 424 | snprintf(r->diag, sizeof r->diag, "%s: %s", stage, r->err[0] ? r->err : "(no driver text)"); |
| 425 | else |
| 426 | snprintf(r->diag, sizeof r->diag, "%s: %s", stage, omega_gpu_platform_refusal_name(rc)); |
| 427 | return rc; |
| 428 | } |
| 429 | |
| 430 | static const char *state_name(uint32_t s) { |
| 431 | switch (s) { |
| 432 | case AGPU_STATE_BOUND: return "BOUND"; |
| 433 | case AGPU_STATE_BOOTING: return "BOOTING"; |
| 434 | case AGPU_STATE_GSP_UP: return "GSP_UP"; |
| 435 | case AGPU_STATE_RM_READY: return "RM_READY"; |
| 436 | case AGPU_STATE_FAILED: return "FAILED"; |
| 437 | default: return "UNKNOWN"; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static const char *verdict_name(uint32_t v) { |
| 442 | static const char *const n[] = { "0", "OK", "E_STATE", "E_ARG", "E_QUEUE", "E_TIMEOUT", "E_RESULT", "E_STATUS", |
| 443 | "E_VERSION", "E_STATIC", "E_REPLY", "E_NOMEM", "E_UNSUPPORTED" }; |
| 444 | return v < sizeof n / sizeof n[0] ? n[v] : "UNKNOWN"; |
| 445 | } |
| 446 | |
| 447 | /* One ioctl on a request block the unit wrote; its agpu_rm_status sits at st_off. 0, or -1 with r->err naming the |
| 448 | * call and why. */ |
| 449 | static int rm_call(AienRef *r, unsigned long req, AienReq *q, uint32_t st_off, const char *what) { |
| 450 | if (r->sys->ioctl_dev(r->fd, req, q) != 0) { |
| 451 | snprintf(r->err, sizeof r->err, "%s: %s", what, strerror(errno)); |
| 452 | return -1; |
| 453 | } |
| 454 | if (!ag_rm_ok(q, st_off)) { |
| 455 | struct agpu_rm_status st; |
| 456 | memcpy(&st, q->b + st_off, sizeof st); |
| 457 | snprintf(r->err, sizeof r->err, "%s: AGPU_RM_%s code %u fn %u step %u", what, verdict_name(st.verdict), |
| 458 | st.code, st.fn, st.step); |
| 459 | return -1; |
| 460 | } |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /* AGPU_MEM_ALLOC at a CPU VA this process reserved, then the object mapped over the reservation. */ |
| 465 | static int mem_map(AienRef *r, uint64_t bytes, uint64_t align, uint32_t flags, AienMem *m) { |
| 466 | memset(m, 0, sizeof *m); |
| 467 | const uint64_t size = ag_mem_size(bytes); |
| 468 | if (size == 0) { snprintf(r->err, sizeof r->err, "mem: %llu bytes", (unsigned long long)bytes); return -1; } |
| 469 | /* Reserve [va, va + size) in the window with MAP_FIXED_NOREPLACE, first fit from a moving cursor: a range taken by |
| 470 | * anything else in the process (EEXIST) is stepped over; the search wraps once. The unit picks every candidate. */ |
| 471 | uint64_t va = 0; |
| 472 | const uint64_t start = ag_va_start(r->va_next); |
| 473 | uint64_t at = ag_va_align(start, align); |
| 474 | for (int wrapped = 0;;) { |
| 475 | const int t = ag_va_try(at, size, wrapped, start); |
| 476 | if (t == VA_TRY_WRAP) { wrapped = 1; at = ag_va_base(); continue; } |
| 477 | if (t != VA_TRY_RESERVE) break; |
| 478 | void *p = r->sys->map((void *)(uintptr_t)at, (size_t)size, PROT_NONE, |
| 479 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0); |
| 480 | if (p == (void *)(uintptr_t)at) { va = at; break; } |
| 481 | if (p != MAP_FAILED) (void)r->sys->unmap(p, (size_t)size); /* a kernel without NOREPLACE moved it */ |
| 482 | else if (errno != EEXIST) break; |
| 483 | at = ag_va_step(at, size, align); |
| 484 | } |
| 485 | if (!va) { |
| 486 | snprintf(r->err, sizeof r->err, "no free %llu-byte range in VA window [0x%llx, 0x%llx): %s", (unsigned long long)size, |
| 487 | (unsigned long long)ag_va_base(), (unsigned long long)ag_va_top(), strerror(errno)); |
| 488 | return -1; |
| 489 | } |
| 490 | r->va_next = va + size; |
| 491 | AienReq a; |
| 492 | ag_req_alloc(&a, size, va, flags); |
| 493 | if (rm_call(r, AGPU_MEM_ALLOC, &a, offsetof(struct agpu_mem_alloc, st), "AGPU_MEM_ALLOC") != 0) { |
| 494 | (void)r->sys->unmap((void *)(uintptr_t)va, (size_t)size); |
| 495 | return -1; |
| 496 | } |
| 497 | void *cpu = r->sys->map((void *)(uintptr_t)va, (size_t)size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, r->fd, |
| 498 | a.alloc.mmap_off); |
| 499 | if (cpu != (void *)(uintptr_t)va) { |
| 500 | snprintf(r->err, sizeof r->err, "mmap AGPU_MEM_ALLOC handle %u at 0x%llx: %s", a.alloc.handle, (unsigned long long)va, |
| 501 | cpu == MAP_FAILED ? strerror(errno) : "placed elsewhere"); |
| 502 | if (cpu != MAP_FAILED) (void)r->sys->unmap(cpu, (size_t)size); |
| 503 | AienReq f; |
| 504 | ag_req_free(&f, a.alloc.handle); |
| 505 | (void)r->sys->ioctl_dev(r->fd, AGPU_MEM_FREE, &f); |
| 506 | (void)r->sys->unmap((void *)(uintptr_t)va, (size_t)size); |
| 507 | return -1; |
| 508 | } |
| 509 | m->cpu = cpu; m->size = size; m->handle = a.alloc.handle; |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* AGPU_MEM_FREE (the GPU mapping goes first), then the CPU mapping. On a refused free nothing is unmapped unless |
| 514 | * force (device close: the descriptor closes next and takes the object with it). */ |
| 515 | static int mem_unmap(AienRef *r, AienMem *m, int force) { |
| 516 | if (!m->cpu) return 0; |
| 517 | AienReq f; |
| 518 | ag_req_free(&f, m->handle); |
| 519 | const int rc = rm_call(r, AGPU_MEM_FREE, &f, offsetof(struct agpu_mem_free, st), "AGPU_MEM_FREE"); |
| 520 | if (rc != 0 && !force) return -1; |
| 521 | (void)r->sys->unmap(m->cpu, (size_t)m->size); |
| 522 | memset(m, 0, sizeof *m); |
| 523 | return rc; |
| 524 | } |
| 525 | |
| 526 | static int dev_ok(AienRef *r, OmegaGpuDeviceHandle d) { |
| 527 | return omega_gpu_handles_check(&r->t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 528 | } |
| 529 | |
| 530 | static int buf_ok(AienRef *r, OmegaGpuBufferHandle b, uint32_t *kind) { |
| 531 | if (b.index >= OMEGA_GPU_HANDLE_SLOTS || b.generation == 0u) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; |
| 532 | if (!r->t.live[b.index] || r->t.generation[b.index] != b.generation) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; |
| 533 | const uint32_t k = r->t.kind[b.index]; |
| 534 | if (k != OMEGA_GPU_HANDLE_BUF_DATA && k != OMEGA_GPU_HANDLE_BUF_CODE && k != OMEGA_GPU_HANDLE_BUF_QUEUE) |
| 535 | return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; |
| 536 | if (kind) *kind = k; |
| 537 | return OMEGA_GPU_PLAT_OK; |
| 538 | } |
| 539 | |
| 540 | static int range_ok(AienRef *r, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { |
| 541 | return ag_range(r->mem[b.index].size, off, len) ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; |
| 542 | } |
| 543 | |
| 544 | static uint64_t ref_now_ns(void *ctx) { |
| 545 | (void)ctx; |
| 546 | struct timespec t; |
| 547 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 548 | return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; |
| 549 | } |
| 550 | static uint64_t ref_now_ms(void *ctx) { return ref_now_ns(ctx) / 1000000ull; } |
| 551 | |
| 552 | static int ref_wait_ms(void *ctx, uint64_t ms) { |
| 553 | (void)ctx; |
| 554 | struct timespec t = {.tv_sec = (time_t)(ms / 1000u), .tv_nsec = (long)(ms % 1000u) * 1000000L}; |
| 555 | while (nanosleep(&t, &t) != 0) {} |
| 556 | return OMEGA_GPU_PLAT_OK; |
| 557 | } |
| 558 | |
| 559 | static int ref_lock_id(void *ctx, uint32_t id) { |
| 560 | AienRef *r = ctx; |
| 561 | if (id >= OMEGA_GPU_PLATFORM_AIEN_LOCKS) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 562 | return pthread_mutex_lock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_BUSY; |
| 563 | } |
| 564 | static int ref_unlock_id(void *ctx, uint32_t id) { |
| 565 | AienRef *r = ctx; |
| 566 | if (id >= OMEGA_GPU_PLATFORM_AIEN_LOCKS) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 567 | return pthread_mutex_unlock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 568 | } |
| 569 | |
| 570 | static int ref_device_open(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *out) { |
| 571 | AienRef *r = ctx; |
| 572 | int rc; |
| 573 | if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 574 | memset(out, 0, sizeof *out); |
| 575 | ref_lock(r); |
| 576 | if (ordinal != 0u) { rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_ARGS, "device_open"); goto out; } |
| 577 | if (r->dev.generation != 0u) { rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_LIMIT, "device_open (already open)"); goto out; } |
| 578 | const char *path = getenv("CUDAPP_AIEN_GPU_DEV"); |
| 579 | if (!path || !*path) path = OMEGA_GPU_AIEN_DEFAULT_DEV; |
| 580 | r->err[0] = '\0'; |
| 581 | r->fd = r->sys->open_dev(path, O_RDWR | O_CLOEXEC); |
| 582 | if (r->fd < 0) { |
| 583 | snprintf(r->err, sizeof r->err, "open %s: %s (aien-gpu.ko not loaded?)", path, strerror(errno)); |
| 584 | r->fd = -1; |
| 585 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "aien_gpu_open"); |
| 586 | goto out; |
| 587 | } |
| 588 | AienReq q; |
| 589 | memset(&q, 0, sizeof q); |
| 590 | if (r->sys->ioctl_dev(r->fd, AGPU_INFO, &q) != 0) { |
| 591 | snprintf(r->err, sizeof r->err, "AGPU_INFO on %s: %s", path, strerror(errno)); |
| 592 | } else { |
| 593 | const int v = ag_info_check(&q); |
| 594 | if (v == 1) |
| 595 | snprintf(r->err, sizeof r->err, "%s speaks ABI %u; this platform speaks ABI %u", path, q.info.abi, AGPU_ABI_VERSION); |
| 596 | else if (v != 0) |
| 597 | snprintf(r->err, sizeof r->err, "%s is in state %s (%u), not RM_READY: AGPU_BOOT and AGPU_RM_INIT must finish first", |
| 598 | path, state_name(q.info.state), q.info.state); |
| 599 | } |
| 600 | if (r->err[0]) { |
| 601 | (void)r->sys->close_dev(r->fd); |
| 602 | r->fd = -1; |
| 603 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "aien_gpu_info"); |
| 604 | goto out; |
| 605 | } |
| 606 | rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); |
| 607 | if (rc != OMEGA_GPU_PLAT_OK) { (void)r->sys->close_dev(r->fd); r->fd = -1; rc = refuse(r, rc, "device_open"); goto out; } |
| 608 | r->dev = *out; |
| 609 | r->diag[0] = '\0'; |
| 610 | out: |
| 611 | ref_unlock(r); |
| 612 | return rc; |
| 613 | } |
| 614 | |
| 615 | /* Channel and its memory; called with r->mu held. -1 (r->err set) when AGPU_CHAN_DESTROY refused and !force. */ |
| 616 | static int channel_release(AienRef *r, int force) { |
| 617 | AienReq d; |
| 618 | ag_req_destroy(&d, r->channel); |
| 619 | if (rm_call(r, AGPU_CHAN_DESTROY, &d, offsetof(struct agpu_chan_destroy, st), "AGPU_CHAN_DESTROY") != 0 && !force) return -1; |
| 620 | if (r->doorbell) (void)r->sys->unmap((void *)(uintptr_t)r->doorbell, OMEGA_GPU_AIEN_DOORBELL_BYTES); |
| 621 | (void)mem_unmap(r, &r->fifo, 1); |
| 622 | (void)mem_unmap(r, &r->notifier, 1); |
| 623 | r->doorbell = NULL; |
| 624 | r->channel = r->token = 0; |
| 625 | r->put = r->retired = 0; |
| 626 | memset(&r->queue, 0, sizeof r->queue); |
| 627 | r->ring_index = 0; |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int ref_device_close(void *ctx, OmegaGpuDeviceHandle dev) { |
| 632 | AienRef *r = ctx; |
| 633 | ref_lock(r); |
| 634 | int rc = dev_ok(r, dev); |
| 635 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "device_close"); ref_unlock(r); return rc; } |
| 636 | /* Every handle of the device goes stale. Unlike the RM client, a mapping of an aien-gpu object keeps it alive past |
| 637 | * close(fd), so every live buffer is released here. */ |
| 638 | if (r->queue.generation != 0u) (void)channel_release(r, 1); |
| 639 | for (uint32_t i = 0; i < OMEGA_GPU_HANDLE_SLOTS; i++) { |
| 640 | if (!r->t.live[i] || i == dev.index) continue; |
| 641 | if (r->t.kind[i] != OMEGA_GPU_HANDLE_QUEUE) (void)mem_unmap(r, &r->mem[i], 1); |
| 642 | (void)omega_gpu_handles_close(&r->t, i, r->t.generation[i], r->t.kind[i]); |
| 643 | memset(&r->mem[i], 0, sizeof r->mem[i]); |
| 644 | r->rights[i] = 0; |
| 645 | r->gpu_cached[i] = 0; |
| 646 | } |
| 647 | (void)omega_gpu_handles_close(&r->t, dev.index, dev.generation, OMEGA_GPU_HANDLE_DEVICE); |
| 648 | memset(&r->dev, 0, sizeof r->dev); |
| 649 | r->l2_inval_pending = 0; |
| 650 | (void)r->sys->close_dev(r->fd); |
| 651 | r->fd = -1; |
| 652 | ref_unlock(r); |
| 653 | return OMEGA_GPU_PLAT_OK; |
| 654 | } |
| 655 | |
| 656 | static const char *ref_diag_text(void *ctx) { return ((AienRef *)ctx)->diag; } |
| 657 | |
| 658 | static int ref_dma_alloc(void *ctx, OmegaGpuDeviceHandle dev, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *out, |
| 659 | OmegaCpuView *cpu, OmegaGpuVa *gpu, OmegaGpuBusAddr *bus) { |
| 660 | AienRef *r = ctx; |
| 661 | if (!desc || !out || !cpu || !gpu) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 662 | memset(out, 0, sizeof *out); memset(cpu, 0, sizeof *cpu); memset(gpu, 0, sizeof *gpu); |
| 663 | if (bus) memset(bus, 0, sizeof *bus); |
| 664 | ref_lock(r); |
| 665 | int rc = dev_ok(r, dev); |
| 666 | if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_platform_check_buffer(desc, &g_caps); |
| 667 | const int policy = ag_cache(desc->kind, desc->rights, desc->gpu_cache); /* 1 uncached, 0 cached, 2 refused */ |
| 668 | const int cached = policy == 0; |
| 669 | if (rc == OMEGA_GPU_PLAT_OK && policy != 0 && policy != AGPU_MEM_GPU_UNCACHED) rc = OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY; |
| 670 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; } |
| 671 | rc = omega_gpu_mem_reserve_check(desc->bytes, r->diag, sizeof r->diag); /* D1, before any slot or driver call */ |
| 672 | if (rc != OMEGA_GPU_PLAT_OK) { ref_unlock(r); return rc; } |
| 673 | uint32_t idx = 0, gen = 0; |
| 674 | rc = omega_gpu_handles_open(&r->t, desc->kind, desc->bytes, AIEN_BUFFERS, g_caps.max_total_bytes, &idx, &gen); |
| 675 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_alloc"); ref_unlock(r); return rc; } |
| 676 | AienMem m; |
| 677 | if (mem_map(r, desc->bytes, desc->align, cached ? 0u : AGPU_MEM_GPU_UNCACHED, &m) != 0) { |
| 678 | (void)omega_gpu_handles_close(&r->t, idx, gen, desc->kind); |
| 679 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "aien_gpu_mem_alloc"); |
| 680 | ref_unlock(r); |
| 681 | return rc; |
| 682 | } |
| 683 | r->mem[idx] = m; |
| 684 | r->rights[idx] = desc->rights; |
| 685 | r->gpu_cached[idx] = (uint8_t)cached; |
| 686 | out->index = idx; out->generation = gen; |
| 687 | cpu->ptr = m.cpu; cpu->len = (size_t)m.size; |
| 688 | gpu->va = (uint64_t)(uintptr_t)m.cpu; /* GPU VA == CPU VA */ |
| 689 | ref_unlock(r); |
| 690 | return OMEGA_GPU_PLAT_OK; |
| 691 | } |
| 692 | |
| 693 | static int ref_dma_free(void *ctx, OmegaGpuBufferHandle b) { |
| 694 | AienRef *r = ctx; |
| 695 | uint32_t kind = 0; |
| 696 | ref_lock(r); |
| 697 | int rc = buf_ok(r, b, &kind); |
| 698 | if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u && r->ring_index == b.index) |
| 699 | rc = OMEGA_GPU_PLAT_REFUSED_BUSY; /* the open queue still uses it as its ring */ |
| 700 | if (rc == OMEGA_GPU_PLAT_OK && mem_unmap(r, &r->mem[b.index], 0) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; |
| 701 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_free"); ref_unlock(r); return rc; } |
| 702 | (void)omega_gpu_handles_close(&r->t, b.index, b.generation, kind); |
| 703 | r->rights[b.index] = 0; |
| 704 | r->gpu_cached[b.index] = 0; |
| 705 | ref_unlock(r); |
| 706 | return OMEGA_GPU_PLAT_OK; |
| 707 | } |
| 708 | |
| 709 | static void barrier(void) { |
| 710 | #if defined(__aarch64__) |
| 711 | __asm__ volatile("dsb sy" ::: "memory"); |
| 712 | #else |
| 713 | __sync_synchronize(); |
| 714 | #endif |
| 715 | } |
| 716 | |
| 717 | static int ref_sync(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len, const char *stage, int to_gpu) { |
| 718 | AienRef *r = ctx; |
| 719 | ref_lock(r); |
| 720 | int rc = buf_ok(r, b, NULL); |
| 721 | if (rc == OMEGA_GPU_PLAT_OK) rc = range_ok(r, b, off, len); |
| 722 | if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, stage); |
| 723 | else if (to_gpu && r->gpu_cached[b.index]) r->l2_inval_pending = 1; /* omega #457 */ |
| 724 | ref_unlock(r); |
| 725 | if (rc == OMEGA_GPU_PLAT_OK) barrier(); |
| 726 | return rc; |
| 727 | } |
| 728 | static int ref_sync_to_gpu(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { return ref_sync(ctx, b, off, len, "sync_cpu_to_gpu", 1); } |
| 729 | static int ref_sync_to_cpu(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t len) { return ref_sync(ctx, b, off, len, "sync_gpu_to_cpu", 0); } |
| 730 | |
| 731 | /* The m16_native_wait_marker_ge rule: serial-number compare, 50 us sleep-poll, one last read at the deadline. */ |
| 732 | static int wait_ge(volatile uint32_t *w, uint32_t target, uint64_t timeout_ms) { |
| 733 | uint64_t duration, end; |
| 734 | if (__builtin_mul_overflow(timeout_ms, 1000000ull, &duration) || __builtin_add_overflow(ref_now_ns(NULL), duration, &end)) |
| 735 | end = UINT64_MAX; |
| 736 | while (ref_now_ns(NULL) < end) { |
| 737 | barrier(); |
| 738 | if (ag_serial_ge(*w, target)) return 0; |
| 739 | usleep(50); |
| 740 | } |
| 741 | barrier(); |
| 742 | return ag_serial_ge(*w, target) ? 0 : -1; |
| 743 | } |
| 744 | |
| 745 | static int ref_poll_word(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint32_t expected, uint64_t timeout_ms, uint32_t *observed) { |
| 746 | AienRef *r = ctx; |
| 747 | ref_lock(r); |
| 748 | int rc = buf_ok(r, b, NULL); |
| 749 | if (rc == OMEGA_GPU_PLAT_OK) { |
| 750 | static const int poll_rc[] = { OMEGA_GPU_PLAT_OK, OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS, OMEGA_GPU_PLAT_REFUSED_ARGS, |
| 751 | OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH }; |
| 752 | rc = poll_rc[ag_poll(r->rights[b.index], off, r->mem[b.index].size) & 3]; |
| 753 | } |
| 754 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "poll_word"); ref_unlock(r); return rc; } |
| 755 | volatile uint32_t *w = (volatile uint32_t *)((uint8_t *)r->mem[b.index].cpu + off); |
| 756 | ref_unlock(r); /* never hold the table lock while polling */ |
| 757 | rc = wait_ge(w, expected, timeout_ms) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_POLL_TIMED_OUT; |
| 758 | if (observed) *observed = *w; |
| 759 | return rc; |
| 760 | } |
| 761 | |
| 762 | static int queue_ok(AienRef *r, OmegaGpuQueueHandle q) { |
| 763 | return omega_gpu_handles_check(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); |
| 764 | } |
| 765 | |
| 766 | /* GPFIFO ring + USERD in one GPU-uncached buffer (the CPU publishes both; nvrm_channel), the error notifier, |
| 767 | * AGPU_CHAN_CREATE, the doorbell page. Called with r->mu held; -1 with r->err set and nothing left behind. */ |
| 768 | static int channel_create(AienRef *r) { |
| 769 | if (mem_map(r, OMEGA_GPU_AIEN_FIFO_BYTES, 0, AGPU_MEM_GPU_UNCACHED, &r->fifo) != 0) return -1; |
| 770 | if (mem_map(r, OMEGA_GPU_AIEN_NOTIFIER_BYTES, 0, AGPU_MEM_GPU_UNCACHED, &r->notifier) != 0) { |
| 771 | (void)mem_unmap(r, &r->fifo, 1); |
| 772 | return -1; |
| 773 | } |
| 774 | AienReq c; |
| 775 | ag_req_chan(&c, r->fifo.handle, r->notifier.handle); |
| 776 | if (rm_call(r, AGPU_CHAN_CREATE, &c, offsetof(struct agpu_chan_create, st), "AGPU_CHAN_CREATE") != 0) { |
| 777 | (void)mem_unmap(r, &r->fifo, 1); (void)mem_unmap(r, &r->notifier, 1); |
| 778 | return -1; |
| 779 | } |
| 780 | r->channel = c.chan.channel; |
| 781 | r->token = c.chan.token; |
| 782 | void *db = r->sys->map(NULL, OMEGA_GPU_AIEN_DOORBELL_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, r->fd, c.chan.doorbell_off); |
| 783 | if (db == MAP_FAILED) { |
| 784 | char keep[sizeof r->err]; |
| 785 | snprintf(keep, sizeof keep, "mmap doorbell page (offset 0x%llx): %s", (unsigned long long)c.chan.doorbell_off, strerror(errno)); |
| 786 | (void)channel_release(r, 1); |
| 787 | memcpy(r->err, keep, sizeof keep); |
| 788 | return -1; |
| 789 | } |
| 790 | r->doorbell = db; |
| 791 | r->put = r->retired = 0; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static int ref_queue_open(void *ctx, OmegaGpuDeviceHandle dev, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *out) { |
| 796 | AienRef *r = ctx; |
| 797 | uint32_t kind = 0; |
| 798 | if (!out) return OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 799 | memset(out, 0, sizeof *out); |
| 800 | ref_lock(r); |
| 801 | int rc = dev_ok(r, dev); |
| 802 | if (rc == OMEGA_GPU_PLAT_OK) rc = buf_ok(r, ring, &kind); |
| 803 | if (rc == OMEGA_GPU_PLAT_OK && kind != OMEGA_GPU_HANDLE_BUF_QUEUE) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; |
| 804 | if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u) rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; |
| 805 | if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &out->index, &out->generation); |
| 806 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_open"); ref_unlock(r); return rc; } |
| 807 | if (channel_create(r) != 0) { |
| 808 | (void)omega_gpu_handles_close(&r->t, out->index, out->generation, OMEGA_GPU_HANDLE_QUEUE); |
| 809 | memset(out, 0, sizeof *out); |
| 810 | rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "aien_gpu_chan_create"); |
| 811 | ref_unlock(r); |
| 812 | return rc; |
| 813 | } |
| 814 | r->ring_index = ring.index; |
| 815 | r->queue = *out; |
| 816 | ref_unlock(r); |
| 817 | return OMEGA_GPU_PLAT_OK; |
| 818 | } |
| 819 | |
| 820 | static int ref_queue_close(void *ctx, OmegaGpuQueueHandle q) { |
| 821 | AienRef *r = ctx; |
| 822 | ref_lock(r); |
| 823 | int rc = queue_ok(r, q); |
| 824 | if (rc == OMEGA_GPU_PLAT_OK && channel_release(r, 0) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; |
| 825 | if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_close(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); |
| 826 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_close"); ref_unlock(r); return rc; } |
| 827 | ref_unlock(r); |
| 828 | return OMEGA_GPU_PLAT_OK; |
| 829 | } |
| 830 | |
| 831 | /* Every check comes before the first write, so a refused submit rang no doorbell and changed no ring state. The unit |
| 832 | * plans the entry (checks, omega #457 prefix, GPFIFO entry, slot, GPPut); this function performs the ordered stores. */ |
| 833 | static int ref_queue_submit(void *ctx, OmegaGpuQueueHandle q, const uint32_t *words, uint32_t nwords) { |
| 834 | AienRef *r = ctx; |
| 835 | ref_lock(r); |
| 836 | int rc = queue_ok(r, q); |
| 837 | AienReq plan; |
| 838 | const AienMem *pb = &r->mem[r->ring_index]; |
| 839 | if (rc == OMEGA_GPU_PLAT_OK) { |
| 840 | switch (ag_submit(&plan, nwords, words != NULL, r->l2_inval_pending, pb->size, r->put, r->retired, |
| 841 | (uint64_t)(uintptr_t)pb->cpu)) { |
| 842 | case SUBMIT_OK: break; |
| 843 | case SUBMIT_ARGS: rc = OMEGA_GPU_PLAT_REFUSED_ARGS; break; |
| 844 | case SUBMIT_LENGTH: rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; break; |
| 845 | default: |
| 846 | snprintf(r->err, sizeof r->err, "GPFIFO full: %u entries unretired", r->put - r->retired); |
| 847 | rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "submit_methods"); ref_unlock(r); return rc; } |
| 852 | const uint32_t pre = plan32(&plan, PLAN_PRE); |
| 853 | if (pre > L2_INVAL_PREFIX_WORDS || plan32(&plan, PLAN_SLOT) >= OMEGA_GPU_AIEN_GP_ENTRIES || |
| 854 | plan32(&plan, PLAN_TOTAL) != nwords + pre) { |
| 855 | fprintf(stderr, "omega_aiengpu submit plan out of range: pre %u slot %u total %u\n", pre, plan32(&plan, PLAN_SLOT), |
| 856 | plan32(&plan, PLAN_TOTAL)); |
| 857 | abort(); /* a defect in the unit, never a device answer */ |
| 858 | } |
| 859 | #if !CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS |
| 860 | if (r->sys == &g_real_sys) { |
| 861 | om_publish(plan.b, pb->cpu, r->fifo.cpu, r->doorbell, words, nwords, r->token); |
| 862 | r->put++; |
| 863 | } else |
| 864 | #endif |
| 865 | { |
| 866 | uint32_t *dst = pb->cpu; |
| 867 | if (pre) memcpy(dst, plan.b, (size_t)pre * 4u); |
| 868 | memcpy(dst + pre, words, (size_t)nwords * 4u); |
| 869 | barrier(); |
| 870 | volatile uint64_t *ring = (volatile uint64_t *)r->fifo.cpu; |
| 871 | ring[plan32(&plan, PLAN_SLOT)] = plan64(&plan, PLAN_GP); |
| 872 | r->put++; |
| 873 | barrier(); |
| 874 | *(volatile uint32_t *)((uint8_t *)r->fifo.cpu + OMEGA_GPU_AIEN_USERD_OFF + OMEGA_GPU_AIEN_USERD_GPPUT) = |
| 875 | plan32(&plan, PLAN_GPPUT); |
| 876 | barrier(); |
| 877 | *(volatile uint32_t *)(r->doorbell + OMEGA_GPU_AIEN_DOORBELL_WORD) = r->token; |
| 878 | } |
| 879 | if (pre) r->l2_inval_pending = 0; |
| 880 | ref_unlock(r); |
| 881 | return OMEGA_GPU_PLAT_OK; |
| 882 | } |
| 883 | |
| 884 | static int ref_queue_retire(void *ctx, OmegaGpuQueueHandle q) { |
| 885 | AienRef *r = ctx; |
| 886 | ref_lock(r); |
| 887 | int rc = queue_ok(r, q); |
| 888 | if (rc == OMEGA_GPU_PLAT_OK) r->retired = r->put; /* the caller saw the work complete */ |
| 889 | else rc = refuse(r, rc, "queue_retire"); |
| 890 | ref_unlock(r); |
| 891 | return rc; |
| 892 | } |
| 893 | |
| 894 | static void *ref_host_alloc(void *ctx, size_t bytes) { (void)ctx; return bytes ? malloc(bytes) : NULL; } |
| 895 | static void ref_host_free(void *ctx, void *p) { (void)ctx; free(p); } |
| 896 | |
| 897 | static const OmegaGpuPlatform g_platform = { |
| 898 | .ctx = &g_ref, |
| 899 | .time_now_ms = ref_now_ms, |
| 900 | .wait_ms = ref_wait_ms, |
| 901 | .lock = ref_lock_id, |
| 902 | .unlock = ref_unlock_id, |
| 903 | .mmio_read32 = NULL, /* not provided: the module owns the registers */ |
| 904 | .mmio_write32 = NULL, |
| 905 | .dma_alloc = ref_dma_alloc, |
| 906 | .dma_free = ref_dma_free, |
| 907 | .sync_cpu_to_gpu = ref_sync_to_gpu, |
| 908 | .sync_gpu_to_cpu = ref_sync_to_cpu, |
| 909 | .poll_word = ref_poll_word, |
| 910 | .irq_wait = NULL, /* polling is not interrupt support */ |
| 911 | .device_open = ref_device_open, |
| 912 | .device_close = ref_device_close, |
| 913 | .diag_text = ref_diag_text, |
| 914 | .queue_open = ref_queue_open, |
| 915 | .queue_close = ref_queue_close, |
| 916 | .queue_submit = ref_queue_submit, |
| 917 | .queue_retire = ref_queue_retire, |
| 918 | .time_now_ns = ref_now_ns, |
| 919 | .host_alloc = ref_host_alloc, |
| 920 | .host_free = ref_host_free, |
| 921 | }; |
| 922 | |
| 923 | const OmegaGpuPlatform *omega_gpu_platform_aien(OmegaGpuPlatformCaps *caps) { |
| 924 | if (caps) *caps = g_caps; |
| 925 | return &g_platform; |
| 926 | } |
| 927 | #endif /* CUDAPP_MIGRATION_PENDING_AIEN_GPU */ |