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
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * nvrm.h -- PHYSICS native accelerator substrate (NVRM client), used by M16 qualification, |
| 4 | * FORGE realization and the NVRM gates in this tree. M17 (OMEGA_BLACKWELL_VECTOR) was |
| 5 | * qualified in aien-spark/omega, not here (aien-architecture doctrine/ROADMAP.md). |
| 6 | * |
| 7 | * Drives the NVIDIA open kernel module (RM + UVM) directly through ioctls on |
| 8 | * /dev/nvidiactl, /dev/nvidia0 and /dev/nvidia-uvm. No libcuda, no CUDA |
| 9 | * runtime, no CUDA toolkit: every object (client, device, VA space, memory, |
| 10 | * channel group, GPFIFO channel, compute object) is allocated here. |
| 11 | */ |
| 12 | #ifndef PHYSICS_NVRM_H |
| 13 | #define PHYSICS_NVRM_H |
| 14 | |
| 15 | #include <stddef.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "cudapp_native.h" |
| 19 | |
| 20 | #ifndef CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 21 | #define CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC 0 |
| 22 | #endif |
| 23 | |
| 24 | #define NVRM_MAX_MEM 16 |
| 25 | |
| 26 | /* Capacity of the free-VA-range table (nvrm_free / va_take reuse) and the |
| 27 | * live-allocation table (nvrm_free validation). Separate from NVRM_MAX_MEM, |
| 28 | * which no caller currently uses, so existing behavior is unaffected. */ |
| 29 | #define NVRM_MAX_FREE 4096 |
| 30 | #define NVRM_MAX_LIVE 4096 |
| 31 | |
| 32 | typedef struct { |
| 33 | uint32_t handle; |
| 34 | uint64_t va; /* identical GPU and CPU virtual address */ |
| 35 | uint64_t size; |
| 36 | void *cpu; /* CPU mapping (== (void *)va) */ |
| 37 | } NvrmMem; |
| 38 | |
| 39 | /* One coalesced free VA range available for reuse by va_take(). */ |
| 40 | typedef struct { |
| 41 | uint64_t va; |
| 42 | uint64_t size; |
| 43 | } NvrmVaRange; |
| 44 | |
| 45 | /* The compute object makes the RM allocate the channel's main graphics context |
| 46 | * buffer as ONE physically contiguous 2 MiB system-memory block (order 9), asked |
| 47 | * for once with __GFP_RETRY_MAYFAIL and no scattered fallback (open driver |
| 48 | * kernel-open/nvidia/nv-vm.c:377-391, kernel_graphics_context.c:1129; kernel |
| 49 | * tracepoint record in omega#523). On a long-running host that block is often |
| 50 | * missing for a few hundred milliseconds, so nvrm_channel retries that one |
| 51 | * RM_ALLOC, bounded, before NV_ERR_NO_MEMORY is final. Measured 2026-10-17 on |
| 52 | * the Spark: every refusal cleared within 5 attempts at 100 ms (omega#523). */ |
| 53 | #ifndef NVRM_CHANNEL_NO_MEMORY_RETRIES |
| 54 | #define NVRM_CHANNEL_NO_MEMORY_RETRIES 50 |
| 55 | #endif |
| 56 | #ifndef NVRM_CHANNEL_NO_MEMORY_RETRY_MS |
| 57 | #define NVRM_CHANNEL_NO_MEMORY_RETRY_MS 100 |
| 58 | #endif |
| 59 | |
| 60 | /* One outstanding allocation, tracked so nvrm_free() can tell a real, |
| 61 | * still-live NvrmMem apart from a stale/duplicate copy. */ |
| 62 | typedef struct { |
| 63 | uint32_t handle; |
| 64 | uint64_t va; |
| 65 | uint64_t size; |
| 66 | void *cpu; |
| 67 | uint8_t uvm_live; |
| 68 | uint8_t dma_live; |
| 69 | uint8_t rm_live; |
| 70 | uint8_t cpu_live; |
| 71 | uint8_t quarantined; |
| 72 | uint64_t omega_mapping; /* owned native-bridge token; never a raw address */ |
| 73 | } NvrmLiveAlloc; |
| 74 | |
| 75 | /* An extra command queue (GPFIFO channel) in the same channel group (TSG) and |
| 76 | * sub-context (FERMI_CONTEXT_SHARE_A) as channel 0, created by nvrm_channel_n |
| 77 | * before the group is scheduled: same VA space, so every buffer is visible to |
| 78 | * both, and work on different channels is not ordered against each other |
| 79 | * (CUDA guide, CUDA_DEVICE_MAX_CONNECTIONS: tasks on one work queue get a |
| 80 | * false dependency; separate queues remove it). */ |
| 81 | #define NVRM_MAX_EXTRA_CHANNELS 3 |
| 82 | typedef struct { |
| 83 | uint32_t gpfifo, compute_obj; |
| 84 | uint32_t ctxshare; /* Owned asynchronous subcontext, distinct from channel 0. */ |
| 85 | NvrmMem fifo; /* GPFIFO ring, then USERD at userd_off */ |
| 86 | NvrmMem notifier; /* channel error notifier */ |
| 87 | uint32_t entries, userd_off; |
| 88 | uint32_t token; /* work submit token */ |
| 89 | uint32_t put, retired; |
| 90 | uint64_t channel_va, channel_va_size; |
| 91 | uint8_t registered; |
| 92 | } NvrmChan; |
| 93 | |
| 94 | typedef struct { |
| 95 | int fd_ctl, fd_dev, fd_uvm, fd_uvm2; |
| 96 | uint32_t root, device, subdevice, virtmem, usermode, vaspace; |
| 97 | uint32_t chgroup, ctxshare, gpfifo, compute_obj; |
| 98 | uint32_t next_handle; |
| 99 | uint8_t gpu_uuid[16]; |
| 100 | uint32_t gpu_id, minor; |
| 101 | uint32_t compute_class, gpfifo_class, usermode_class; |
| 102 | uint32_t sm_version; /* NV2080_CTRL_GR_INFO_INDEX_SM_VERSION */ |
| 103 | uint32_t target_sm; /* exact execution target: 86 or 121 */ |
| 104 | uint8_t discrete; /* local framebuffer, not GB10 coherent system memory */ |
| 105 | void *usermode_cpu; /* HOPPER_USERMODE_A MMIO mapping (RM-provided, BAR0-relative) */ |
| 106 | volatile uint32_t *doorbell; /* usermode MMIO + NVC361_NOTIFY_CHANNEL_PENDING */ |
| 107 | NvrmMem fifo; /* GPFIFO ring, then USERD at userd_off */ |
| 108 | NvrmMem notifier; /* channel error notifier */ |
| 109 | uint32_t entries; |
| 110 | uint32_t userd_off; |
| 111 | uint32_t token; /* work submit token */ |
| 112 | uint32_t put; /* entries enqueued (monotonic) */ |
| 113 | uint32_t retired; /* entries the caller has seen complete */ |
| 114 | uint64_t va_next; |
| 115 | uint64_t va_slot_base; |
| 116 | NvrmVaRange free_list[NVRM_MAX_FREE]; /* sorted ascending by va, coalesced */ |
| 117 | uint32_t free_count; |
| 118 | NvrmLiveAlloc live[NVRM_MAX_LIVE]; |
| 119 | uint32_t live_count; |
| 120 | uint64_t channel_va; |
| 121 | uint64_t channel_va_size; |
| 122 | uint8_t channel_registered; |
| 123 | uint8_t gpu_registered; |
| 124 | uint8_t vas_registered; |
| 125 | uint8_t faulted; |
| 126 | /* One checked mapping table per client, not the bridge's default128 table. |
| 127 | * Callers serialize complete reserve/bind/free transactions on this Nvrm. */ |
| 128 | struct omega_native_context *omega_context; |
| 129 | uint64_t omega_pending_mapping, omega_pending_address, omega_pending_size; |
| 130 | uint64_t omega_usermode_mapping; |
| 131 | /* Fault injection applies once at the driver wrapper boundary. */ |
| 132 | uint8_t inject_uvm_free_failure; |
| 133 | uint8_t inject_rm_free_failure; |
| 134 | /* Refuse the next N compute-object RM_ALLOCs with NV_ERR_NO_MEMORY before |
| 135 | * the ioctl, so the retry in nvrm_channel can be exercised deterministically. */ |
| 136 | uint8_t inject_rm_alloc_no_memory; |
| 137 | uint32_t last_rm_status; /* status of the last RM_ALLOC that reached the driver (or was injected) */ |
| 138 | uint32_t channel_no_memory_retries; /* NV_ERR_NO_MEMORY refusals the last nvrm_channel retried */ |
| 139 | uint32_t channel_no_memory_retry_ms; /* delay between those retries; 0 selects NVRM_CHANNEL_NO_MEMORY_RETRY_MS */ |
| 140 | uint64_t rm_alloc_accepted; |
| 141 | uint64_t rm_free_accepted; |
| 142 | NvrmChan extra[NVRM_MAX_EXTRA_CHANNELS]; /* channels 1.. (nvrm_channel_n) */ |
| 143 | uint32_t n_extra; |
| 144 | char err[256]; |
| 145 | } Nvrm; |
| 146 | |
| 147 | /* 0 when cpu lies in one checked live mapping of rm: the caller reads the 32-bit word through *ctx (rm's own |
| 148 | * transport context) at *mapping + *offset. -1 with errno EINVAL (no rm/outputs), EFAULT (no owner) or ENOTSUP |
| 149 | * (C transport build); *ctx is then NULL. Defined in nvrm.c. */ |
| 150 | struct omega_native_context; |
| 151 | int omega_gpu_live_marker(const Nvrm *rm, const void *cpu, uint64_t *mapping, uint64_t *offset, |
| 152 | struct omega_native_context **ctx); |
| 153 | int nvrm_open(Nvrm *rm); |
| 154 | int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out); |
| 155 | /* Local framebuffer on discrete SM86, CPU BAR mapped by RM_MAP_MEMORY. The |
| 156 | * CPU and GPU views share a VA but not a cache. No system-memory fallback. */ |
| 157 | int nvrm_alloc_vram(Nvrm *rm, uint64_t size, NvrmMem *out); |
| 158 | /* Fresh driver framebuffer heap query. Never substitutes /proc/meminfo. */ |
| 159 | int nvrm_vram_info(Nvrm *rm, uint64_t *free_bytes, uint64_t *total_bytes); |
| 160 | /* Bounds/live-identity checked CPU BAR transfers. Caller proves GPU idle before |
| 161 | * readback or overwrite; the fence orders mapped PCIe writes before submission. */ |
| 162 | int nvrm_copy_to(Nvrm *rm, const NvrmMem *m, uint64_t off, const void *src, size_t bytes); |
| 163 | int nvrm_copy_from(Nvrm *rm, const NvrmMem *m, uint64_t off, void *dst, size_t bytes); |
| 164 | /* Reverse of nvrm_alloc: drops the UVM external range, frees the RM memory |
| 165 | * object, unmaps the CPU range, and returns the VA range for reuse by a |
| 166 | * later nvrm_alloc. Idempotent on an already-zeroed NvrmMem (returns 0, |
| 167 | * no syscalls). Returns -1 and sets rm->err, without touching the driver, |
| 168 | * if *m does not match a currently-live allocation (e.g. a stale copy). |
| 169 | * Zeroes *m on success. */ |
| 170 | int nvrm_free(Nvrm *rm, NvrmMem *m); |
| 171 | int nvrm_channel(Nvrm *rm); |
| 172 | /* nvrm_channel plus `extra` (0..NVRM_MAX_EXTRA_CHANNELS) more GPFIFO channels in |
| 173 | * the same group and address space, each with its own asynchronous subcontext, |
| 174 | * all registered with UVM before the one GPFIFO_SCHEDULE (the |
| 175 | * RM refuses to schedule a group holding an unschedulable channel, |
| 176 | * kernel_channel_group_api.c:1095-1107). extra = 0 is nvrm_channel exactly. */ |
| 177 | int nvrm_channel_n(Nvrm *rm, uint32_t extra); |
| 178 | int nvrm_channel_destroy(Nvrm *rm); |
| 179 | /* Channel ch (1..n_extra) of nvrm_channel_n: enqueue + doorbell, retire, entries |
| 180 | * submitted so far. Same contract as nvrm_submit / nvrm_retire on channel 0. */ |
| 181 | int nvrm_chan_submit(Nvrm *rm, uint32_t ch, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); |
| 182 | void nvrm_chan_retire(Nvrm *rm, uint32_t ch, uint32_t upto); |
| 183 | uint32_t nvrm_chan_put(const Nvrm *rm, uint32_t ch); |
| 184 | /* Write a GP entry and advance USERD GPPut; does NOT ring the doorbell. */ |
| 185 | int nvrm_enqueue(Nvrm *rm, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); |
| 186 | /* Mark entries [.., upto) complete; the caller must have observed their completion. */ |
| 187 | void nvrm_retire(Nvrm *rm, uint32_t upto); |
| 188 | /* Usermode doorbell store (NVC361_NOTIFY_CHANNEL_PENDING <- work submit token). */ |
| 189 | void nvrm_ring(Nvrm *rm); |
| 190 | /* enqueue + ring */ |
| 191 | int nvrm_submit(Nvrm *rm, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); |
| 192 | uint64_t nvrm_gp_entry(uint64_t va, uint32_t nwords); |
| 193 | volatile uint32_t *nvrm_userd_gpput(Nvrm *rm); |
| 194 | int nvrm_close(Nvrm *rm); |
| 195 | |
| 196 | /* Pushbuffer method header (non-incrementing=1, incrementing=2 / SEC_OP_INC_METHOD) */ |
| 197 | static inline uint32_t nvrm_mthd(uint32_t subc, uint32_t mthd, uint32_t count) { |
| 198 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 199 | return (2u << 28) | (count << 16) | (subc << 13) | (mthd >> 2); |
| 200 | #else |
| 201 | uint64_t result; |
| 202 | if (omega_nvrm_alloc_scalar(&result, 1, subc, mthd, count)) abort(); |
| 203 | return (uint32_t)result; |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | /* As nvrm_alloc, but the graphics chip does not cache it in L2. For memory |
| 208 | * that a resident chip program polls while the CPU writes it. */ |
| 209 | int nvrm_alloc_gpu_uncached(Nvrm *rm, uint64_t size, NvrmMem *out); |
| 210 | #endif |