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/physics-mv/blob/main/nvrm/nvrm.h # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. 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 `physics-mv / nvrm / nvrm.h` 181 lines ยท 8748 bytes Raw Blame History 1 /* 2 * nvrm.h -- PHYSICS native accelerator substrate (NVRM client), used by M16 qualification, 3 * FORGE realization and the NVRM gates in this tree. M17 (OMEGA_BLACKWELL_VECTOR) was 4 * qualified in aien-spark/omega, not here (aien-architecture doctrine/ROADMAP.md). 5 * 6 * Drives the NVIDIA open kernel module (RM + UVM) directly through ioctls on 7 * /dev/nvidiactl, /dev/nvidia0 and /dev/nvidia-uvm. No libcuda, no CUDA 8 * runtime, no CUDA toolkit: every object (client, device, VA space, memory, 9 * channel group, GPFIFO channel, compute object) is allocated here. 10 */ 11 #ifndef PHYSICS_NVRM_H 12 #define PHYSICS_NVRM_H 13 14 #include 15 #include 16 17 #define NVRM_MAX_MEM 16 18 19 /* Capacity of the free-VA-range table (nvrm_free / va_take reuse) and the 20 * live-allocation table (nvrm_free validation). Separate from NVRM_MAX_MEM, 21 * which no caller currently uses, so existing behavior is unaffected. */ 22 #define NVRM_MAX_FREE 4096 23 #define NVRM_MAX_LIVE 4096 24 25 typedef struct { 26 uint32_t handle; 27 uint64_t va; /* identical GPU and CPU virtual address */ 28 uint64_t size; 29 void *cpu; /* CPU mapping (== (void *)va) */ 30 } NvrmMem; 31 32 /* One coalesced free VA range available for reuse by va_take(). */ 33 typedef struct { 34 uint64_t va; 35 uint64_t size; 36 } NvrmVaRange; 37 38 /* The compute object makes the RM allocate the channel's main graphics context 39 * buffer as ONE physically contiguous 2 MiB system-memory block (order 9), asked 40 * for once with __GFP_RETRY_MAYFAIL and no scattered fallback (open driver 41 * kernel-open/nvidia/nv-vm.c:377-391, kernel_graphics_context.c:1129; kernel 42 * tracepoint record in omega#523). On a long-running host that block is often 43 * missing for a few hundred milliseconds, so nvrm_channel retries that one 44 * RM_ALLOC, bounded, before NV_ERR_NO_MEMORY is final. Measured 2026-10-17 on 45 * the Spark: every refusal cleared within 5 attempts at 100 ms (omega#523). */ 46 #ifndef NVRM_CHANNEL_NO_MEMORY_RETRIES 47 #define NVRM_CHANNEL_NO_MEMORY_RETRIES 50 48 #endif 49 #ifndef NVRM_CHANNEL_NO_MEMORY_RETRY_MS 50 #define NVRM_CHANNEL_NO_MEMORY_RETRY_MS 100 51 #endif 52 53 /* One outstanding allocation, tracked so nvrm_free() can tell a real, 54 * still-live NvrmMem apart from a stale/duplicate copy. */ 55 typedef struct { 56 uint32_t handle; 57 uint64_t va; 58 uint64_t size; 59 void *cpu; 60 uint8_t uvm_live; 61 uint8_t dma_live; 62 uint8_t rm_live; 63 uint8_t cpu_live; 64 uint8_t quarantined; 65 } NvrmLiveAlloc; 66 67 /* An extra command queue (GPFIFO channel) in the same channel group (TSG) and 68 * sub-context (FERMI_CONTEXT_SHARE_A) as channel 0, created by nvrm_channel_n 69 * before the group is scheduled: same VA space, so every buffer is visible to 70 * both, and work on different channels is not ordered against each other 71 * (CUDA guide, CUDA_DEVICE_MAX_CONNECTIONS: tasks on one work queue get a 72 * false dependency; separate queues remove it). */ 73 #define NVRM_MAX_EXTRA_CHANNELS 3 74 typedef struct { 75 uint32_t gpfifo, compute_obj; 76 uint32_t ctxshare; /* Owned asynchronous subcontext, distinct from channel 0. */ 77 NvrmMem fifo; /* GPFIFO ring, then USERD at userd_off */ 78 NvrmMem notifier; /* channel error notifier */ 79 uint32_t entries, userd_off; 80 uint32_t token; /* work submit token */ 81 uint32_t put, retired; 82 uint64_t channel_va, channel_va_size; 83 uint8_t registered; 84 } NvrmChan; 85 86 typedef struct { 87 int fd_ctl, fd_dev, fd_uvm, fd_uvm2; 88 uint32_t root, device, subdevice, virtmem, usermode, vaspace; 89 uint32_t chgroup, ctxshare, gpfifo, compute_obj; 90 uint32_t next_handle; 91 uint8_t gpu_uuid[16]; 92 uint32_t gpu_id, minor; 93 uint32_t compute_class, gpfifo_class, usermode_class; 94 uint32_t sm_version; /* NV2080_CTRL_GR_INFO_INDEX_SM_VERSION */ 95 void *usermode_cpu; /* HOPPER_USERMODE_A MMIO mapping (RM-provided, BAR0-relative) */ 96 volatile uint32_t *doorbell; /* usermode MMIO + NVC361_NOTIFY_CHANNEL_PENDING */ 97 NvrmMem fifo; /* GPFIFO ring, then USERD at userd_off */ 98 NvrmMem notifier; /* channel error notifier */ 99 uint32_t entries; 100 uint32_t userd_off; 101 uint32_t token; /* work submit token */ 102 uint32_t put; /* entries enqueued (monotonic) */ 103 uint32_t retired; /* entries the caller has seen complete */ 104 uint64_t va_next; 105 uint64_t va_slot_base; 106 NvrmVaRange free_list[NVRM_MAX_FREE]; /* sorted ascending by va, coalesced */ 107 uint32_t free_count; 108 NvrmLiveAlloc live[NVRM_MAX_LIVE]; 109 uint32_t live_count; 110 uint64_t channel_va; 111 uint64_t channel_va_size; 112 uint8_t channel_registered; 113 uint8_t gpu_registered; 114 uint8_t vas_registered; 115 uint8_t faulted; 116 /* Fault injection applies once at the driver wrapper boundary. */ 117 uint8_t inject_uvm_free_failure; 118 uint8_t inject_rm_free_failure; 119 /* Refuse the next N compute-object RM_ALLOCs with NV_ERR_NO_MEMORY before 120 * the ioctl, so the retry in nvrm_channel can be exercised deterministically. */ 121 uint8_t inject_rm_alloc_no_memory; 122 uint32_t last_rm_status; /* status of the last RM_ALLOC that reached the driver (or was injected) */ 123 uint32_t channel_no_memory_retries; /* NV_ERR_NO_MEMORY refusals the last nvrm_channel retried */ 124 uint32_t channel_no_memory_retry_ms; /* delay between those retries; 0 selects NVRM_CHANNEL_NO_MEMORY_RETRY_MS */ 125 uint64_t rm_alloc_accepted; 126 uint64_t rm_free_accepted; 127 NvrmChan extra[NVRM_MAX_EXTRA_CHANNELS]; /* channels 1.. (nvrm_channel_n) */ 128 uint32_t n_extra; 129 char err[256]; 130 } Nvrm; 131 132 int nvrm_open(Nvrm *rm); 133 int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out); 134 /* Reverse of nvrm_alloc: drops the UVM external range, frees the RM memory 135 * object, unmaps the CPU range, and returns the VA range for reuse by a 136 * later nvrm_alloc. Idempotent on an already-zeroed NvrmMem (returns 0, 137 * no syscalls). Returns -1 and sets rm->err, without touching the driver, 138 * if *m does not match a currently-live allocation (e.g. a stale copy). 139 * Zeroes *m on success. */ 140 int nvrm_free(Nvrm *rm, NvrmMem *m); 141 int nvrm_channel(Nvrm *rm); 142 /* nvrm_channel plus `extra` (0..NVRM_MAX_EXTRA_CHANNELS) more GPFIFO channels in 143 * the same group and address space, each with its own asynchronous subcontext, 144 * all registered with UVM before the one GPFIFO_SCHEDULE (the 145 * RM refuses to schedule a group holding an unschedulable channel, 146 * kernel_channel_group_api.c:1095-1107). extra = 0 is nvrm_channel exactly. */ 147 int nvrm_channel_n(Nvrm *rm, uint32_t extra); 148 int nvrm_channel_destroy(Nvrm *rm); 149 /* Channel ch (1..n_extra) of nvrm_channel_n: enqueue + doorbell, retire, entries 150 * submitted so far. Same contract as nvrm_submit / nvrm_retire on channel 0. */ 151 int nvrm_chan_submit(Nvrm *rm, uint32_t ch, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); 152 void nvrm_chan_retire(Nvrm *rm, uint32_t ch, uint32_t upto); 153 uint32_t nvrm_chan_put(const Nvrm *rm, uint32_t ch); 154 /* Write a GP entry and advance USERD GPPut; does NOT ring the doorbell. */ 155 int nvrm_enqueue(Nvrm *rm, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); 156 /* Mark entries [.., upto) complete; the caller must have observed their completion. */ 157 void nvrm_retire(Nvrm *rm, uint32_t upto); 158 /* Usermode doorbell store (NVC361_NOTIFY_CHANNEL_PENDING <- work submit token). */ 159 void nvrm_ring(Nvrm *rm); 160 /* enqueue + ring */ 161 int nvrm_submit(Nvrm *rm, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); 162 uint64_t nvrm_gp_entry(uint64_t va, uint32_t nwords); 163 volatile uint32_t *nvrm_userd_gpput(Nvrm *rm); 164 int nvrm_close(Nvrm *rm); 165 /* Free blocks of at least `order` pages in the buddy table at `path` 166 * (/proc/buddyinfo), only in zones named `zone` (NULL: every zone); 167 * ULLONG_MAX when the file cannot be read. */ 168 unsigned long long nvrm_buddy_free_blocks_ge(const char *path, const char *zone, unsigned order); 169 /* The 2 MiB-block part of the NV_ERR_NO_MEMORY report from the buddy table at 170 * `path`: "free 2 MiB blocks in the Normal zone N (all zones M)". */ 171 void nvrm_no_memory_blocks(const char *path, char *out, size_t cap); 172 173 /* Pushbuffer method header (non-incrementing=1, incrementing=2 / SEC_OP_INC_METHOD) */ 174 static inline uint32_t nvrm_mthd(uint32_t subc, uint32_t mthd, uint32_t count) { 175 return (2u << 28) | (count << 16) | (subc << 13) | (mthd >> 2); 176 } 177 178 #endif 179 /* As nvrm_alloc, but the graphics chip does not cache it in L2. For memory 180 * that a resident chip program polls while the CPU writes it. */ 181 int nvrm_alloc_gpu_uncached(Nvrm *rm, uint64_t size, NvrmMem *out);