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.c -- 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 | * See nvrm.h. Sequence follows the NVIDIA open kernel module ABI (580.173.02). |
| 7 | */ |
| 8 | |
| 9 | #include "nvrm.h" |
| 10 | #include "omega_ampere_qmd.h" |
| 11 | #include "omega_gpu_chip.h" |
| 12 | #include "cpu_barrier.h" |
| 13 | #include "cudapp_native.h" |
| 14 | #include "nvrm_policy.h" |
| 15 | #include "nvrm_struct.h" |
| 16 | |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <stddef.h> |
| 20 | #include <stdatomic.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include "nvos.h" |
| 33 | #include "nv-ioctl.h" |
| 34 | #include "nv-ioctl-numbers.h" |
| 35 | #include "nv_escape.h" |
| 36 | #include "uvm_linux_ioctl.h" |
| 37 | #include "uvm_ioctl.h" |
| 38 | #include "nv-unix-nvos-params-wrappers.h" |
| 39 | #include "class/clc361.h" |
| 40 | #include "class/cl0000.h" |
| 41 | #include "class/cl0080.h" |
| 42 | #include "class/cl2080.h" |
| 43 | #include "class/cl0070.h" |
| 44 | #include "class/cl003e.h" |
| 45 | #include "class/cl0040.h" |
| 46 | #include "class/cl90f1.h" |
| 47 | #include "class/cl9067.h" |
| 48 | #include "class/cla06c.h" |
| 49 | #include "class/clc96f.h" |
| 50 | #include "class/clcec0.h" |
| 51 | #include "class/clc661.h" |
| 52 | #include "alloc/alloc_channel.h" |
| 53 | #include "ctrl/ctrl0000/ctrl0000gpu.h" |
| 54 | #include "ctrl/ctrl0080/ctrl0080gpu.h" |
| 55 | #include "ctrl/ctrl2080/ctrl2080gpu.h" |
| 56 | #include "ctrl/ctrl2080/ctrl2080gr.h" |
| 57 | #include "ctrl/ctrla06c.h" |
| 58 | #include "ctrl/ctrlc36f.h" |
| 59 | /* Keep the vendor C layout as an explicit differential oracle, not a fallback. */ |
| 60 | #ifndef CUDAPP_MIGRATION_PENDING_C_NVRM |
| 61 | #define CUDAPP_MIGRATION_PENDING_C_NVRM 0 |
| 62 | #endif |
| 63 | |
| 64 | #if !CUDAPP_MIGRATION_PENDING_C_NVRM |
| 65 | static int rm_wire(void *out, size_t size, uint64_t op, const uint64_t fields[16]) { |
| 66 | unsigned char bytes[64]; |
| 67 | uint64_t length = 0; |
| 68 | if (omega_nvrm_wire_pack(&length, op, fields, 16, bytes, 64) || length != size) { |
| 69 | errno = EPROTO; |
| 70 | return -1; |
| 71 | } |
| 72 | memcpy(out, bytes, size); |
| 73 | return 0; |
| 74 | } |
| 75 | /* Marshal API values, then let Omega own every byte of the UVM request, |
| 76 | * including unused per-GPU attributes and ABI padding. */ |
| 77 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 78 | static int uvm_wire(unsigned long cmd, void *params) { |
| 79 | uint64_t a[16] = {0}, n = 0; |
| 80 | unsigned char uuid[16] = {0}, bytes[56]; |
| 81 | size_t size = 0; |
| 82 | switch (cmd) { |
| 83 | case UVM_INITIALIZE: { |
| 84 | UVM_INITIALIZE_PARAMS *p = params; a[0] = p->flags; size = sizeof *p; break; |
| 85 | } |
| 86 | case UVM_MM_INITIALIZE: { |
| 87 | UVM_MM_INITIALIZE_PARAMS *p = params; a[0] = (uint32_t)p->uvmFd; size = sizeof *p; break; |
| 88 | } |
| 89 | case UVM_REGISTER_GPU: { |
| 90 | UVM_REGISTER_GPU_PARAMS *p = params; |
| 91 | memcpy(uuid, p->gpu_uuid.uuid, sizeof uuid); |
| 92 | a[0] = (uint32_t)p->rmCtrlFd; a[1] = p->hClient; a[2] = p->hSmcPartRef; |
| 93 | size = sizeof *p; break; |
| 94 | } |
| 95 | case UVM_UNREGISTER_GPU: { |
| 96 | UVM_UNREGISTER_GPU_PARAMS *p = params; |
| 97 | memcpy(uuid, p->gpu_uuid.uuid, sizeof uuid); size = sizeof *p; break; |
| 98 | } |
| 99 | case UVM_REGISTER_GPU_VASPACE: { |
| 100 | UVM_REGISTER_GPU_VASPACE_PARAMS *p = params; |
| 101 | memcpy(uuid, p->gpuUuid.uuid, sizeof uuid); |
| 102 | a[0] = (uint32_t)p->rmCtrlFd; a[1] = p->hClient; a[2] = p->hVaSpace; |
| 103 | size = sizeof *p; break; |
| 104 | } |
| 105 | case UVM_UNREGISTER_GPU_VASPACE: { |
| 106 | UVM_UNREGISTER_GPU_VASPACE_PARAMS *p = params; |
| 107 | memcpy(uuid, p->gpuUuid.uuid, sizeof uuid); size = sizeof *p; break; |
| 108 | } |
| 109 | case UVM_REGISTER_CHANNEL: { |
| 110 | UVM_REGISTER_CHANNEL_PARAMS *p = params; |
| 111 | memcpy(uuid, p->gpuUuid.uuid, sizeof uuid); |
| 112 | a[0] = (uint32_t)p->rmCtrlFd; a[1] = p->hClient; a[2] = p->hChannel; |
| 113 | a[3] = p->base; a[4] = p->length; size = sizeof *p; break; |
| 114 | } |
| 115 | case UVM_UNREGISTER_CHANNEL: { |
| 116 | UVM_UNREGISTER_CHANNEL_PARAMS *p = params; |
| 117 | memcpy(uuid, p->gpuUuid.uuid, sizeof uuid); |
| 118 | a[0] = p->hClient; a[1] = p->hChannel; size = sizeof *p; break; |
| 119 | } |
| 120 | case UVM_CREATE_EXTERNAL_RANGE: { |
| 121 | UVM_CREATE_EXTERNAL_RANGE_PARAMS *p = params; |
| 122 | a[0] = p->base; a[1] = p->length; size = sizeof *p; break; |
| 123 | } |
| 124 | case UVM_FREE: { |
| 125 | UVM_FREE_PARAMS *p = params; |
| 126 | a[0] = p->base; a[1] = p->length; size = sizeof *p; break; |
| 127 | } |
| 128 | case UVM_MAP_EXTERNAL_ALLOCATION: { |
| 129 | UVM_MAP_EXTERNAL_ALLOCATION_PARAMS *p = params; |
| 130 | memcpy(uuid, p->perGpuAttributes[0].gpuUuid.uuid, sizeof uuid); |
| 131 | a[0] = p->base; a[1] = p->length; a[2] = p->offset; a[3] = p->gpuAttributesCount; |
| 132 | a[4] = (uint32_t)p->rmCtrlFd; a[5] = p->hClient; a[6] = p->hMemory; |
| 133 | a[7] = p->perGpuAttributes[0].gpuMappingType; |
| 134 | /* The client deliberately maps exactly one GPU. Never silently drop a |
| 135 | * future multi-GPU caller's attributes. */ |
| 136 | if (a[3] != 1 || omega_nvrm_uvm_map_wire(&n, a, 16, uuid, 16, |
| 137 | params, sizeof *p) || n != sizeof *p) { |
| 138 | errno = EPROTO; return -1; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | default: errno = EINVAL; return -1; |
| 143 | } |
| 144 | if (omega_nvrm_uvm_wire(&n, cmd, a, 16, uuid, 16, bytes, sizeof bytes) || n != size) { |
| 145 | errno = EPROTO; return -1; |
| 146 | } |
| 147 | memcpy(params, bytes, size); |
| 148 | return 0; |
| 149 | } |
| 150 | #endif |
| 151 | #endif |
| 152 | |
| 153 | static int fail(Nvrm *rm, const char *fmt, ...); |
| 154 | |
| 155 | /* Pure class admission lives in seeded Omega. Class IDs are vendor constants: |
| 156 | * open-gpu-doc clc7c0.h, clc56f.h, and vendored clc361.h / clc661.h. */ |
| 157 | static int select_classes(Nvrm *rm, const uint32_t *classes, uint32_t count, |
| 158 | uint16_t vendor_id, uint16_t device_id) { |
| 159 | rm->compute_class = rm->gpfifo_class = rm->usermode_class = 0; |
| 160 | rm->target_sm = 0; rm->discrete = 0; |
| 161 | uint64_t m[16] = {1, rm->sm_version, |
| 162 | NV2080_CTRL_GR_INFO_SM_VERSION_8_06, NV2080_CTRL_GR_INFO_SM_VERSION_12_01, |
| 163 | OMEGA_AMPERE_COMPUTE_B, OMEGA_AMPERE_GPFIFO_A, VOLTA_USERMODE_A, |
| 164 | BLACKWELL_COMPUTE_B, BLACKWELL_CHANNEL_GPFIFO_A, HOPPER_USERMODE_A, |
| 165 | 0, 0, NV2080_CTRL_GR_INFO_SM_VERSION_10_04, vendor_id, device_id}; |
| 166 | /* Marshal the vendor's class-list presence, without selecting a target. */ |
| 167 | for (uint32_t i = 0; i < count; i++) { |
| 168 | for (uint32_t k = 0; k < 3; k++) { |
| 169 | if (classes[i] == m[4 + k]) m[10] |= 1u << k; |
| 170 | if (classes[i] == m[7 + k]) m[11] |= 1u << k; |
| 171 | } |
| 172 | } |
| 173 | uint32_t selected[64]; |
| 174 | if (omega_ampere_policy(m, selected) != 0) |
| 175 | return fail(rm, "unsupported SM 0x%x or missing target classes", rm->sm_version); |
| 176 | rm->target_sm = selected[0]; rm->discrete = (uint8_t)selected[1]; |
| 177 | rm->compute_class = selected[2]; rm->gpfifo_class = selected[3]; rm->usermode_class = selected[4]; |
| 178 | return 0; |
| 179 | } |
| 180 | #define NV_IOWR(nr, sz) _IOC(_IOC_READ | _IOC_WRITE, NV_IOCTL_MAGIC, (nr), (sz)) |
| 181 | #include "native_client.h" |
| 182 | |
| 183 | /* The platform marker poll's lookup (src/omega_gpu_platform_linux.c ref_poll_word). 0: cpu lies in one checked |
| 184 | * live mapping of rm; *mapping and *offset are that mapping's token and byte offset and *ctx is rm's own transport |
| 185 | * context, the only context the token is valid in. No registry, no global: the caller names the Nvrm. */ |
| 186 | int omega_gpu_live_marker(const Nvrm *rm, const void *cpu, uint64_t *mapping, uint64_t *offset, |
| 187 | struct omega_native_context **ctx) { |
| 188 | if (!ctx || !mapping || !offset) { errno = EINVAL; return -1; } |
| 189 | *ctx = NULL; |
| 190 | #if NVRM_C_TRANSPORT |
| 191 | (void)rm; (void)cpu; |
| 192 | errno = ENOTSUP; |
| 193 | return -1; |
| 194 | #else |
| 195 | if (!rm) { errno = EINVAL; return -1; } |
| 196 | if (nvrm_live_token(rm, cpu, sizeof(uint32_t), mapping, offset) != 0) return -1; |
| 197 | *ctx = rm->omega_context; |
| 198 | return 0; |
| 199 | #endif |
| 200 | } |
| 201 | |
| 202 | static int fail(Nvrm *rm, const char *fmt, ...) { |
| 203 | va_list ap; |
| 204 | va_start(ap, fmt); |
| 205 | vsnprintf(rm->err, sizeof rm->err, fmt, ap); |
| 206 | va_end(ap); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /* One /proc/meminfo value in kB, or 0 when it cannot be read. An RM_ALLOC |
| 211 | * refused with NV_ERR_NO_MEMORY is only explained next to the size asked for |
| 212 | * and the host's memory at that moment (sovereign-core #239). Both numbers |
| 213 | * matter: the RM system-memory path failed on low MemFree while MemAvailable |
| 214 | * was high (omega#327, two arms), so MemAvailable alone would read as fine. */ |
| 215 | static unsigned long long meminfo_kb(const char *key) { |
| 216 | FILE *f = fopen("/proc/meminfo", "r"); |
| 217 | if (!f) return 0; |
| 218 | char line[256]; |
| 219 | unsigned long long kb = 0; |
| 220 | size_t klen = strlen(key); |
| 221 | while (fgets(line, sizeof line, f)) { |
| 222 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 223 | if (strncmp(line, key, klen) != 0 || line[klen] != ':') continue; |
| 224 | if (sscanf(line + klen + 1, " %llu kB", &kb) != 1) kb = 0; |
| 225 | #else |
| 226 | uint64_t state[2] = {0, 0}, matched = 0; |
| 227 | if (omega_nvrm_counts(&matched, 0, line, strlen(line), key, klen, state, 2)) break; |
| 228 | if (!matched) continue; |
| 229 | kb = state[1]; |
| 230 | #endif |
| 231 | break; |
| 232 | } |
| 233 | fclose(f); |
| 234 | return kb; |
| 235 | } |
| 236 | |
| 237 | /* Rewrite rm->err after a failed memory RM_ALLOC so it also carries the |
| 238 | * requested size, MemFree and MemAvailable. */ |
| 239 | static int fail_alloc_context(Nvrm *rm, uint64_t size) { |
| 240 | char prev[sizeof rm->err]; |
| 241 | memcpy(prev, rm->err, sizeof prev); |
| 242 | return fail(rm, "%s for %llu bytes, MemFree %llu kB, MemAvailable %llu kB", prev, |
| 243 | (unsigned long long)size, meminfo_kb("MemFree"), meminfo_kb("MemAvailable")); |
| 244 | } |
| 245 | |
| 246 | /* Free physically contiguous blocks of at least `order` pages across all zones |
| 247 | * (/proc/buddyinfo), or ULLONG_MAX when the file cannot be read. Order 9 is the |
| 248 | * 2 MiB block the channel's main context buffer needs. */ |
| 249 | static unsigned long long buddy_free_blocks_ge(unsigned order) { |
| 250 | FILE *f = fopen("/proc/buddyinfo", "r"); |
| 251 | if (!f) return ULLONG_MAX; |
| 252 | char line[512]; |
| 253 | unsigned long long total = 0; |
| 254 | while (fgets(line, sizeof line, f)) { |
| 255 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 256 | const char *p = strstr(line, "zone"); |
| 257 | if (!p) continue; |
| 258 | p += 4; |
| 259 | while (*p == ' ') p++; |
| 260 | while (*p && *p != ' ') p++; /* zone name */ |
| 261 | unsigned long long n; |
| 262 | for (unsigned o = 0; o < 32 && sscanf(p, " %llu", &n) == 1; o++) { |
| 263 | if (o >= order) total += n; |
| 264 | while (*p == ' ') p++; |
| 265 | while (*p && *p != ' ') p++; |
| 266 | } |
| 267 | #else |
| 268 | uint64_t state[2] = {order, total}, unused; |
| 269 | if (omega_nvrm_counts(&unused, 1, line, strlen(line), "", 0, state, 2)) { |
| 270 | fclose(f); |
| 271 | return ULLONG_MAX; |
| 272 | } |
| 273 | total = state[1]; |
| 274 | #endif |
| 275 | } |
| 276 | fclose(f); |
| 277 | return total; |
| 278 | } |
| 279 | |
| 280 | /* Rewrite rm->err after the compute-object RM_ALLOC stayed NV_ERR_NO_MEMORY |
| 281 | * through every retry, so the line names the attempts, the wait and the host's |
| 282 | * supply of 2 MiB blocks at that moment. */ |
| 283 | static int fail_channel_no_memory(Nvrm *rm, uint32_t attempts, uint32_t delay_ms) { |
| 284 | char prev[sizeof rm->err]; |
| 285 | memcpy(prev, rm->err, sizeof prev); |
| 286 | unsigned long long blocks = buddy_free_blocks_ge(9); |
| 287 | char blocks_text[32]; |
| 288 | if (blocks == ULLONG_MAX) snprintf(blocks_text, sizeof blocks_text, "UNKNOWN"); |
| 289 | else snprintf(blocks_text, sizeof blocks_text, "%llu", blocks); |
| 290 | return fail(rm, "%s after %u attempts over %u ms; MemFree %llu kB, free 2 MiB blocks %s", prev, attempts, |
| 291 | (attempts > 1 ? attempts - 1 : 0) * delay_ms, meminfo_kb("MemFree"), blocks_text); |
| 292 | } |
| 293 | |
| 294 | static int nv_ioctl(int fd, unsigned nr, void *arg, size_t sz) { |
| 295 | return nvrm_rm_ioctl(fd, nr, arg, sz); |
| 296 | } |
| 297 | |
| 298 | static int uvm(Nvrm *rm, int fd, unsigned long cmd, void *params, NV_STATUS *st); |
| 299 | |
| 300 | static int rm_alloc(Nvrm *rm, uint32_t parent, uint32_t cls, void *params, uint32_t psz, uint32_t *out) { |
| 301 | rm->last_rm_status = 0; |
| 302 | if (cls == rm->compute_class && rm->inject_rm_alloc_no_memory) { |
| 303 | rm->inject_rm_alloc_no_memory--; |
| 304 | rm->last_rm_status = NV_ERR_NO_MEMORY; |
| 305 | return fail(rm, "RM_ALLOC class 0x%x status 0x%x (NV_ERR_NO_MEMORY) [injected]", cls, NV_ERR_NO_MEMORY); |
| 306 | } |
| 307 | uint64_t hdec = nvrm_policy_hnext(cls, rm->next_handle, rm->rm_alloc_accepted); |
| 308 | if (hdec & (1ull << 32)) |
| 309 | return fail(rm, "RM handle or accounting space exhausted"); |
| 310 | uint32_t new_handle = (uint32_t)hdec; |
| 311 | if (hdec & (1ull << 33)) rm->next_handle++; |
| 312 | NVOS21_PARAMETERS p; |
| 313 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 314 | memset(&p, 0, sizeof p); |
| 315 | p.hRoot = rm->root; |
| 316 | p.hObjectParent = parent; |
| 317 | p.hObjectNew = new_handle; |
| 318 | p.hClass = cls; |
| 319 | p.pAllocParms = NV_PTR_TO_NvP64(params); |
| 320 | p.paramsSize = psz; |
| 321 | #else |
| 322 | uint64_t fields[16] = {rm->root, parent, |
| 323 | new_handle, cls, (uintptr_t)params, psz}; |
| 324 | if (rm_wire(&p, sizeof p, 1, fields)) return fail(rm, "RM_ALLOC wire refused"); |
| 325 | #endif |
| 326 | if (nv_ioctl(rm->fd_ctl, NV_ESC_RM_ALLOC, &p, sizeof p) != 0) |
| 327 | return fail(rm, "RM_ALLOC class 0x%x ioctl errno %d", cls, errno); |
| 328 | rm->last_rm_status = p.status; |
| 329 | uint64_t sc = nvrm_policy_nrstat(p.status); |
| 330 | if (sc != 0) |
| 331 | return fail(rm, "RM_ALLOC class 0x%x status 0x%x%s", cls, p.status, |
| 332 | sc == 1 ? " (NV_ERR_NO_MEMORY)" : ""); |
| 333 | rm->rm_alloc_accepted++; |
| 334 | *out = p.hObjectNew; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int rm_free(Nvrm *rm, uint32_t parent, uint32_t obj) { |
| 339 | if (!obj) return 0; |
| 340 | if (rm->rm_free_accepted == UINT64_MAX) return fail(rm, "RM free accounting exhausted"); |
| 341 | if (rm->inject_rm_free_failure) { |
| 342 | rm->inject_rm_free_failure = 0; |
| 343 | return fail(rm, "injected RM_FREE failure for handle 0x%x", obj); |
| 344 | } |
| 345 | NVOS00_PARAMETERS f; |
| 346 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 347 | memset(&f, 0, sizeof f); |
| 348 | f.hRoot = rm->root; |
| 349 | f.hObjectParent = parent; |
| 350 | f.hObjectOld = obj; |
| 351 | #else |
| 352 | uint64_t fields[16] = {rm->root, parent, obj}; |
| 353 | if (rm_wire(&f, sizeof f, 3, fields)) return fail(rm, "RM_FREE wire refused"); |
| 354 | #endif |
| 355 | if (nv_ioctl(rm->fd_ctl, NV_ESC_RM_FREE, &f, sizeof f) != 0) |
| 356 | return fail(rm, "RM_FREE handle 0x%x errno %d", obj, errno); |
| 357 | if (f.status != NV_OK) return fail(rm, "RM_FREE handle 0x%x status 0x%x", obj, f.status); |
| 358 | rm->rm_free_accepted++; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int uvm_free_checked(Nvrm *rm, uint64_t va, uint64_t size) { |
| 363 | if (rm->inject_uvm_free_failure) { |
| 364 | rm->inject_uvm_free_failure = 0; |
| 365 | return fail(rm, "injected UVM_FREE failure for VA 0x%lx", (unsigned long)va); |
| 366 | } |
| 367 | UVM_FREE_PARAMS uf; |
| 368 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 369 | memset(&uf, 0, sizeof uf); |
| 370 | uf.base = va; |
| 371 | uf.length = size; |
| 372 | #else |
| 373 | NS_FILL(uf, 20, va, size); |
| 374 | #endif |
| 375 | return uvm(rm, rm->fd_uvm, UVM_FREE, &uf, &uf.rmStatus); |
| 376 | } |
| 377 | |
| 378 | static int dma_unmap_checked(Nvrm *rm, uint32_t hmem, uint64_t va, uint64_t size) { |
| 379 | NVOS47_PARAMETERS p; |
| 380 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 381 | memset(&p, 0, sizeof p); |
| 382 | p.hClient = rm->root; |
| 383 | p.hDevice = rm->device; |
| 384 | p.hDma = rm->virtmem; |
| 385 | p.hMemory = hmem; |
| 386 | p.dmaOffset = va; |
| 387 | p.size = size; |
| 388 | #else |
| 389 | uint64_t fields[16] = {rm->root, rm->device, rm->virtmem, hmem, 0, va, size}; |
| 390 | if (rm_wire(&p, sizeof p, 6, fields)) return fail(rm, "RM_UNMAP_MEMORY_DMA wire refused"); |
| 391 | #endif |
| 392 | if (nv_ioctl(rm->fd_ctl, NV_ESC_RM_UNMAP_MEMORY_DMA, &p, sizeof p) != 0) |
| 393 | return fail(rm, "RM_UNMAP_MEMORY_DMA errno %d", errno); |
| 394 | if (p.status != NV_OK) return fail(rm, "RM_UNMAP_MEMORY_DMA status 0x%x", p.status); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static int rm_control(Nvrm *rm, uint32_t obj, uint32_t cmd, void *params, uint32_t psz) { |
| 399 | NVOS54_PARAMETERS p; |
| 400 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 401 | memset(&p, 0, sizeof p); |
| 402 | p.hClient = rm->root; |
| 403 | p.hObject = obj; |
| 404 | p.cmd = cmd; |
| 405 | p.params = NV_PTR_TO_NvP64(params); |
| 406 | p.paramsSize = psz; |
| 407 | #else |
| 408 | uint64_t fields[16] = {rm->root, obj, cmd, 0, (uintptr_t)params, psz}; |
| 409 | if (rm_wire(&p, sizeof p, 2, fields)) return fail(rm, "RM_CONTROL wire refused"); |
| 410 | #endif |
| 411 | if (nv_ioctl(rm->fd_ctl, NV_ESC_RM_CONTROL, &p, sizeof p) != 0) |
| 412 | return fail(rm, "RM_CONTROL 0x%x ioctl errno %d", cmd, errno); |
| 413 | if (p.status != 0) return fail(rm, "RM_CONTROL 0x%x status 0x%x", cmd, p.status); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int uvm(Nvrm *rm, int fd, unsigned long cmd, void *params, NV_STATUS *st) { |
| 418 | #if !CUDAPP_MIGRATION_PENDING_C_NVRM && CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 419 | if (uvm_wire(cmd, params)) return fail(rm, "UVM 0x%lx wire refused", cmd); |
| 420 | #endif |
| 421 | if (nvrm_uvm_ioctl(fd, cmd, params) != 0) return fail(rm, "UVM 0x%lx errno %d", cmd, errno); |
| 422 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 423 | if (st && *st != NV_OK) return fail(rm, "UVM 0x%lx status 0x%x", cmd, *st); |
| 424 | #else |
| 425 | (void)st; |
| 426 | uint64_t size = 0, status = 0, unused = 0; |
| 427 | if (omega_nvrm_struct(&size, 8, cmd, &unused, 1, params, 0) || !size || |
| 428 | omega_nvrm_struct(&status, 6, cmd, &unused, 1, params, size)) |
| 429 | return fail(rm, "UVM response decode refused"); |
| 430 | if (status != NV_OK) return fail(rm, "UVM 0x%lx status 0x%x", cmd, (unsigned)status); |
| 431 | #endif |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int open_dev_fd(Nvrm *rm) { |
| 436 | char path[32]; |
| 437 | snprintf(path, sizeof path, "/dev/nvidia%u", rm->minor); |
| 438 | int fd = nvrm_file_open(path); |
| 439 | if (fd < 0) return fail(rm, "open %s errno %d", path, errno); |
| 440 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 441 | nv_ioctl_register_fd_t r = { .ctl_fd = rm->fd_ctl }; |
| 442 | #else |
| 443 | nv_ioctl_register_fd_t r; |
| 444 | uint64_t fields[16] = {(uint32_t)rm->fd_ctl}; |
| 445 | if (rm_wire(&r, sizeof r, 7, fields)) { nvrm_file_close(fd); return fail(rm, "REGISTER_FD wire refused"); } |
| 446 | #endif |
| 447 | if (nv_ioctl(fd, NV_ESC_REGISTER_FD, &r, sizeof r) != 0) { nvrm_file_close(fd); return fail(rm, "REGISTER_FD errno %d", errno); } |
| 448 | return fd; |
| 449 | } |
| 450 | |
| 451 | /* Map an RM memory object into this process at a fixed CPU address. */ |
| 452 | static void *map_to_cpu(Nvrm *rm, uint32_t hmem, uint64_t size, void *target, uint32_t flags, int system) { |
| 453 | int fd = system ? nvrm_file_open("/dev/nvidiactl") : open_dev_fd(rm); |
| 454 | if (fd < 0) { fail(rm, "map fd"); return MAP_FAILED; } |
| 455 | nv_ioctl_nvos33_parameters_with_fd m; |
| 456 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 457 | memset(&m, 0, sizeof m); |
| 458 | m.fd = fd; |
| 459 | m.params.hClient = rm->root; |
| 460 | m.params.hDevice = rm->device; |
| 461 | m.params.hMemory = hmem; |
| 462 | m.params.length = size; |
| 463 | m.params.flags = flags; |
| 464 | #else |
| 465 | uint64_t fields[16] = {rm->root, rm->device, hmem, 0, size, 0, flags, (uint32_t)fd}; |
| 466 | if (rm_wire(&m, sizeof m, 4, fields)) { nvrm_file_close(fd); fail(rm, "RM_MAP_MEMORY wire refused"); return MAP_FAILED; } |
| 467 | #endif |
| 468 | if (nv_ioctl(rm->fd_ctl, NV_ESC_RM_MAP_MEMORY, &m, sizeof m) != 0 || m.params.status != 0) { |
| 469 | fail(rm, "RM_MAP_MEMORY errno %d status 0x%x", errno, m.params.status); |
| 470 | nvrm_file_close(fd); |
| 471 | return MAP_FAILED; |
| 472 | } |
| 473 | void *p = nvrm_cpu_map(rm, fd, size, target); |
| 474 | nvrm_file_close(fd); |
| 475 | if (p == MAP_FAILED) fail(rm, "mmap errno %d", errno); |
| 476 | return p; |
| 477 | } |
| 478 | |
| 479 | static _Atomic uint64_t g_va_counter = 0x1000000000ull; |
| 480 | static atomic_flag g_va_slots_lock = ATOMIC_FLAG_INIT; |
| 481 | static uint64_t g_va_recycled[1024]; |
| 482 | static uint32_t g_va_recycled_count; |
| 483 | |
| 484 | static uint64_t acquire_va_slot(void) { |
| 485 | while (atomic_flag_test_and_set_explicit(&g_va_slots_lock, memory_order_acquire)) { } |
| 486 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 487 | uint64_t base = g_va_recycled_count ? g_va_recycled[--g_va_recycled_count] : 0; |
| 488 | #else |
| 489 | uint64_t state[4] = {g_va_recycled_count, 0, 0, 0}, base = 0; |
| 490 | int status = omega_nvrm_va(&base, 2, state, 4, g_va_recycled, 1024); |
| 491 | g_va_recycled_count = (uint32_t)state[0]; |
| 492 | if (status) abort(); |
| 493 | #endif |
| 494 | atomic_flag_clear_explicit(&g_va_slots_lock, memory_order_release); |
| 495 | if (!base) { |
| 496 | uint64_t old = atomic_load_explicit(&g_va_counter, memory_order_relaxed); |
| 497 | for (;;) { |
| 498 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 499 | if (old > UINT64_MAX - 0x100000000ull) return 0; |
| 500 | if (atomic_compare_exchange_weak_explicit(&g_va_counter, &old, |
| 501 | old + 0x100000000ull, memory_order_relaxed, memory_order_relaxed)) { |
| 502 | #else |
| 503 | uint64_t next; |
| 504 | if (omega_nvrm_alloc_scalar(&next, 0, old, 0, 0) || !next) return 0; |
| 505 | if (atomic_compare_exchange_weak_explicit(&g_va_counter, &old, |
| 506 | next, memory_order_relaxed, memory_order_relaxed)) { |
| 507 | #endif |
| 508 | base = old; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | return base; |
| 514 | } |
| 515 | |
| 516 | static void recycle_va_slot(uint64_t base) { |
| 517 | while (atomic_flag_test_and_set_explicit(&g_va_slots_lock, memory_order_acquire)) { } |
| 518 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 519 | if (g_va_recycled_count < 1024) g_va_recycled[g_va_recycled_count++] = base; |
| 520 | #else |
| 521 | uint64_t state[4] = {g_va_recycled_count, 0, 0, base}, unused; |
| 522 | if (omega_nvrm_va(&unused, 3, state, 4, g_va_recycled, 1024)) abort(); |
| 523 | g_va_recycled_count = (uint32_t)state[0]; |
| 524 | #endif |
| 525 | atomic_flag_clear_explicit(&g_va_slots_lock, memory_order_release); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | |
| 530 | /* CudaAuditFable RtMem F5: the bump allocator may only hand out VA inside this client's slot. The slot is the 4 GiB |
| 531 | * units from va_slot_base up to the units va_next already covers; a request past them grows the slot only while it |
| 532 | * is the newest one (the shared counter still points at its end), and the counter moves past the growth, so a later |
| 533 | * acquire_va_slot starts above it. Otherwise the next slot belongs to another Nvrm and the request is refused. |
| 534 | * Growing (not a hard 4 GiB cap) keeps single allocations above 4 GiB, which the 30B/35B weights need. */ |
| 535 | static int va_slot_cover(Nvrm *rm, uint64_t end) { |
| 536 | uint64_t o[4]; |
| 537 | uint64_t d = nvrm_policy_vcover(rm->va_slot_base, rm->va_next, end, o); |
| 538 | if (d == 0) return 1; /* no slot, or the request is already covered */ |
| 539 | if (d != 1) return 0; /* refused: bad range or overflow */ |
| 540 | uint64_t owned_end = o[2], want_end = o[3]; |
| 541 | return atomic_compare_exchange_strong_explicit(&g_va_counter, &owned_end, want_end, |
| 542 | memory_order_relaxed, memory_order_relaxed) ? 1 : 0; |
| 543 | } |
| 544 | |
| 545 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 546 | #define CARD_GET(cards, i, field, member) ((cards)[i].member) |
| 547 | #else |
| 548 | #define CARD_GET(cards, i, field, member) ns_get((void *)(cards), sizeof(nv_ioctl_card_info_t) * 64, 16, field, i) |
| 549 | #endif |
| 550 | static int card_index(const nv_ioctl_card_info_t cards[64], const char *bdf) { |
| 551 | if (bdf) { |
| 552 | unsigned domain, bus, slot, function; |
| 553 | char end, canonical[32]; |
| 554 | if (sscanf(bdf, "%x:%x:%x.%x%c", &domain, &bus, &slot, &function, &end) != 4 || |
| 555 | domain > 0xffff || bus > 0xff || slot > 0x1f || function > 7) return -1; |
| 556 | snprintf(canonical, sizeof canonical, "%04x:%02x:%02x.%x", domain, bus, slot, function); |
| 557 | if (strcmp(bdf, canonical) != 0) return -1; |
| 558 | for (int i = 0; i < 64; i++) |
| 559 | if (CARD_GET(cards, i, 0, valid) && CARD_GET(cards, i, 1, pci_info.domain) == domain && |
| 560 | CARD_GET(cards, i, 2, pci_info.bus) == bus && CARD_GET(cards, i, 3, pci_info.slot) == slot && |
| 561 | CARD_GET(cards, i, 4, pci_info.function) == function) return i; |
| 562 | return -1; |
| 563 | } |
| 564 | for (int i = 0; i < 64; i++) if (CARD_GET(cards, i, 0, valid)) return i; |
| 565 | return -1; |
| 566 | } |
| 567 | int nvrm_open(Nvrm *rm) { |
| 568 | memset(rm, 0, sizeof *rm); |
| 569 | /* D2/D6 library admission (om/admission/chip_admit.om): refuse a GPU CUDA++ has no target for from |
| 570 | * sysfs alone, before the VA slot is taken and before any /dev/nvidia node is opened. */ |
| 571 | const char *bdf = getenv("CUDAPP_GPU_BDF"); |
| 572 | if (omega_gpu_chip_admit_open(bdf, rm->err, sizeof rm->err) != 0) return -1; |
| 573 | rm->next_handle = 0xcf000001u; |
| 574 | rm->va_next = acquire_va_slot(); |
| 575 | if (!rm->va_next) return fail(rm, "VA slot space exhausted"); |
| 576 | rm->va_slot_base = rm->va_next; |
| 577 | rm->fd_ctl = nvrm_file_open("/dev/nvidiactl"); |
| 578 | rm->fd_uvm = nvrm_file_open("/dev/nvidia-uvm"); |
| 579 | rm->fd_uvm2 = nvrm_file_open("/dev/nvidia-uvm"); |
| 580 | if (rm->fd_ctl < 0 || rm->fd_uvm < 0 || rm->fd_uvm2 < 0) return fail(rm, "open control nodes errno %d", errno); |
| 581 | |
| 582 | if (rm_alloc(rm, 0, NV01_ROOT_CLIENT, NULL, 0, &rm->root)) return -1; |
| 583 | |
| 584 | UVM_INITIALIZE_PARAMS ui; |
| 585 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 586 | memset(&ui, 0, sizeof ui); |
| 587 | #else |
| 588 | NS_FILL(ui, 18, 0); |
| 589 | #endif |
| 590 | if (uvm(rm, rm->fd_uvm, UVM_INITIALIZE, &ui, &ui.rmStatus)) return -1; |
| 591 | UVM_MM_INITIALIZE_PARAMS mi; |
| 592 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 593 | memset(&mi, 0, sizeof mi); |
| 594 | mi.uvmFd = rm->fd_uvm; |
| 595 | #if !CUDAPP_MIGRATION_PENDING_C_NVRM |
| 596 | if (uvm_wire(UVM_MM_INITIALIZE, &mi)) return fail(rm, "UVM_MM_INITIALIZE wire refused"); |
| 597 | #endif |
| 598 | #else |
| 599 | NS_FILL(mi, 19, (uint32_t)rm->fd_uvm); |
| 600 | #endif |
| 601 | (void)nvrm_uvm_ioctl(rm->fd_uvm2, UVM_MM_INITIALIZE, &mi); /* libcuda also tolerates failure here */ |
| 602 | |
| 603 | nv_ioctl_card_info_t cards[64]; |
| 604 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 605 | memset(cards, 0, sizeof cards); |
| 606 | #else |
| 607 | NS_FILL(cards, 16, 0); |
| 608 | #endif |
| 609 | if (nv_ioctl(rm->fd_ctl, NV_ESC_CARD_INFO, cards, sizeof cards) != 0) return fail(rm, "CARD_INFO errno %d", errno); |
| 610 | int found = card_index(cards, bdf); |
| 611 | if (found < 0) return fail(rm, "no GPU matches CUDAPP_GPU_BDF=%s (canonical dddd:bb:ss.f required)", bdf ? bdf : ""); |
| 612 | /* The same decision on the card the driver names, before its /dev/nvidiaN node is opened. */ |
| 613 | const nv_pci_info_t *pci = &cards[found].pci_info; |
| 614 | uint32_t admitted_sm = 0; |
| 615 | if (omega_gpu_chip_admit_card(pci->domain, pci->bus, pci->slot, pci->function, pci->vendor_id, pci->device_id, |
| 616 | &admitted_sm, rm->err, sizeof rm->err) != 0) return -1; |
| 617 | rm->gpu_id = (uint32_t)CARD_GET(cards, found, 5, gpu_id); |
| 618 | rm->minor = (int)CARD_GET(cards, found, 6, minor_number); |
| 619 | |
| 620 | rm->fd_dev = open_dev_fd(rm); |
| 621 | if (rm->fd_dev < 0) return -1; |
| 622 | |
| 623 | NV0000_CTRL_GPU_GET_ID_INFO_V2_PARAMS idinfo; |
| 624 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 625 | memset(&idinfo, 0, sizeof idinfo); |
| 626 | idinfo.gpuId = rm->gpu_id; |
| 627 | #else |
| 628 | NS_FILL(idinfo, 1, rm->gpu_id); |
| 629 | #endif |
| 630 | if (rm_control(rm, rm->root, NV0000_CTRL_CMD_GPU_GET_ID_INFO_V2, &idinfo, sizeof idinfo)) return -1; |
| 631 | |
| 632 | NV0080_ALLOC_PARAMETERS dp; |
| 633 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 634 | memset(&dp, 0, sizeof dp); |
| 635 | dp.deviceId = idinfo.deviceInstance; |
| 636 | dp.hClientShare = rm->root; |
| 637 | dp.vaMode = NV_DEVICE_ALLOCATION_VAMODE_OPTIONAL_MULTIPLE_VASPACES; |
| 638 | #else |
| 639 | NS_FILL(dp, 2, NS_GET(idinfo, 1, 1), rm->root, NV_DEVICE_ALLOCATION_VAMODE_OPTIONAL_MULTIPLE_VASPACES); |
| 640 | #endif |
| 641 | if (rm_alloc(rm, rm->root, NV01_DEVICE_0, &dp, sizeof dp, &rm->device)) return -1; |
| 642 | NV2080_ALLOC_PARAMETERS sp; |
| 643 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 644 | memset(&sp, 0, sizeof sp); |
| 645 | #else |
| 646 | NS_FILL(sp, 3, 0); |
| 647 | #endif |
| 648 | if (rm_alloc(rm, rm->device, NV20_SUBDEVICE_0, &sp, sizeof sp, &rm->subdevice)) return -1; |
| 649 | NV_MEMORY_VIRTUAL_ALLOCATION_PARAMS vp; |
| 650 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 651 | memset(&vp, 0, sizeof vp); |
| 652 | vp.limit = 0x1ffffffffffffull; |
| 653 | #else |
| 654 | NS_FILL(vp, 4, 0, 0x1ffffffffffffull); |
| 655 | #endif |
| 656 | if (rm_alloc(rm, rm->device, NV01_MEMORY_VIRTUAL, &vp, sizeof vp, &rm->virtmem)) return -1; |
| 657 | |
| 658 | NV2080_CTRL_GR_INFO gi; |
| 659 | NV2080_CTRL_GR_GET_INFO_PARAMS gp; |
| 660 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 661 | gi = (NV2080_CTRL_GR_INFO){ .index = NV2080_CTRL_GR_INFO_INDEX_SM_VERSION }; |
| 662 | memset(&gp, 0, sizeof gp); |
| 663 | gp.grInfoListSize = 1; |
| 664 | gp.grInfoList = NV_PTR_TO_NvP64(&gi); |
| 665 | #else |
| 666 | NS_FILL(gi, 17, NV2080_CTRL_GR_INFO_INDEX_SM_VERSION); |
| 667 | NS_FILL(gp, 6, 1, (uintptr_t)&gi); |
| 668 | #endif |
| 669 | if (rm_control(rm, rm->subdevice, NV2080_CTRL_CMD_GR_GET_INFO, &gp, sizeof gp)) return -1; |
| 670 | rm->sm_version = NS_VALUE(gi, 17, 1, gi.data); |
| 671 | |
| 672 | NV0080_CTRL_GPU_GET_CLASSLIST_PARAMS cl; |
| 673 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 674 | memset(&cl, 0, sizeof cl); |
| 675 | #else |
| 676 | NS_FILL(cl, 7, 0); |
| 677 | #endif |
| 678 | if (rm_control(rm, rm->device, NV0080_CTRL_CMD_GPU_GET_CLASSLIST, &cl, sizeof cl)) return -1; |
| 679 | uint32_t classes[512]; |
| 680 | uint32_t class_count = (uint32_t)NS_VALUE(cl, 7, 0, cl.numClasses); |
| 681 | if (class_count > 512) return fail(rm, "too many classes"); |
| 682 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 683 | cl.classList = NV_PTR_TO_NvP64(classes); |
| 684 | #else |
| 685 | NS_FILL(cl, 7, class_count, (uintptr_t)classes); |
| 686 | #endif |
| 687 | if (rm_control(rm, rm->device, NV0080_CTRL_CMD_GPU_GET_CLASSLIST, &cl, sizeof cl)) return -1; |
| 688 | class_count = (uint32_t)NS_VALUE(cl, 7, 0, cl.numClasses); |
| 689 | if (class_count > 512 || select_classes(rm, classes, class_count, |
| 690 | (uint16_t)CARD_GET(cards, found, 7, pci_info.vendor_id), |
| 691 | (uint16_t)CARD_GET(cards, found, 8, pci_info.device_id))) return -1; |
| 692 | if (rm->target_sm != admitted_sm) |
| 693 | return fail(rm, "GPU admission admitted sm_%u but the driver classes select sm_%u; refused", admitted_sm, |
| 694 | rm->target_sm); |
| 695 | if (rm_alloc(rm, rm->subdevice, rm->usermode_class, NULL, 0, &rm->usermode)) return -1; |
| 696 | void *mmio = map_to_cpu(rm, rm->usermode, 0x10000, NULL, 0, 0); |
| 697 | if (mmio == MAP_FAILED) return -1; |
| 698 | rm->usermode_cpu = mmio; |
| 699 | rm->doorbell = (volatile uint32_t *)((uint8_t *)mmio + NVC361_NOTIFY_CHANNEL_PENDING); |
| 700 | |
| 701 | NV_VASPACE_ALLOCATION_PARAMETERS vap; |
| 702 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 703 | memset(&vap, 0, sizeof vap); |
| 704 | vap.vaBase = 0x1000; |
| 705 | vap.vaSize = 0x1fffffb000000ull; |
| 706 | vap.flags = NV_VASPACE_ALLOCATION_FLAGS_ENABLE_PAGE_FAULTING | NV_VASPACE_ALLOCATION_FLAGS_IS_EXTERNALLY_OWNED; |
| 707 | #else |
| 708 | NS_FILL(vap, 8, 0, NV_VASPACE_ALLOCATION_FLAGS_ENABLE_PAGE_FAULTING | NV_VASPACE_ALLOCATION_FLAGS_IS_EXTERNALLY_OWNED, 0x1fffffb000000ull, 0x1000); |
| 709 | #endif |
| 710 | if (rm_alloc(rm, rm->device, FERMI_VASPACE_A, &vap, sizeof vap, &rm->vaspace)) return -1; |
| 711 | |
| 712 | NV2080_CTRL_GPU_GET_GID_INFO_PARAMS gid; |
| 713 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 714 | memset(&gid, 0, sizeof gid); |
| 715 | gid.flags = NV2080_GPU_CMD_GPU_GET_GID_FLAGS_FORMAT_BINARY; |
| 716 | gid.length = 16; |
| 717 | #else |
| 718 | NS_FILL(gid, 9, 0, NV2080_GPU_CMD_GPU_GET_GID_FLAGS_FORMAT_BINARY, 16); |
| 719 | #endif |
| 720 | if (rm_control(rm, rm->subdevice, NV2080_CTRL_CMD_GPU_GET_GID_INFO, &gid, sizeof gid)) return -1; |
| 721 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 722 | memcpy(rm->gpu_uuid, gid.data, 16); |
| 723 | #else |
| 724 | for (unsigned i = 0; i < sizeof rm->gpu_uuid; i++) rm->gpu_uuid[i] = (uint8_t)NS_GET(gid, 9, 3 + i); |
| 725 | #endif |
| 726 | |
| 727 | UVM_REGISTER_GPU_PARAMS rg; |
| 728 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 729 | memset(&rg, 0, sizeof rg); |
| 730 | memcpy(rg.gpu_uuid.uuid, rm->gpu_uuid, 16); |
| 731 | rg.rmCtrlFd = -1; |
| 732 | #else |
| 733 | uint64_t uuid[2]; |
| 734 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 735 | NS_FILL(rg, 21, uuid[0], uuid[1], UINT32_MAX); |
| 736 | #endif |
| 737 | if (uvm(rm, rm->fd_uvm, UVM_REGISTER_GPU, &rg, &rg.rmStatus)) return -1; |
| 738 | rm->gpu_registered = 1; |
| 739 | UVM_REGISTER_GPU_VASPACE_PARAMS rv; |
| 740 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 741 | memset(&rv, 0, sizeof rv); |
| 742 | memcpy(rv.gpuUuid.uuid, rm->gpu_uuid, 16); |
| 743 | rv.rmCtrlFd = rm->fd_ctl; |
| 744 | rv.hClient = rm->root; |
| 745 | rv.hVaSpace = rm->vaspace; |
| 746 | #else |
| 747 | NS_FILL(rv, 23, uuid[0], uuid[1], (uint32_t)rm->fd_ctl, rm->root, rm->vaspace); |
| 748 | #endif |
| 749 | if (uvm(rm, rm->fd_uvm, UVM_REGISTER_GPU_VASPACE, &rv, &rv.rmStatus)) return -1; |
| 750 | rm->vas_registered = 1; |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* Return a freed VA range to the sorted, coalesced free list. */ |
| 755 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 756 | static int va_release(Nvrm *rm, uint64_t va, uint64_t size) { |
| 757 | uint64_t end; |
| 758 | if (!size || __builtin_add_overflow(va, size, &end)) { |
| 759 | rm->faulted = 1; |
| 760 | return fail(rm, "invalid VA release extent"); |
| 761 | } |
| 762 | uint32_t pos = 0; |
| 763 | while (pos < rm->free_count && rm->free_list[pos].va < va) pos++; |
| 764 | if ((pos && rm->free_list[pos - 1].va + rm->free_list[pos - 1].size > va) || |
| 765 | (pos < rm->free_count && end > rm->free_list[pos].va)) { |
| 766 | rm->faulted = 1; |
| 767 | return fail(rm, "overlapping VA release extent"); |
| 768 | } |
| 769 | |
| 770 | if (rm->free_count >= NVRM_MAX_FREE) { |
| 771 | /* Free-list table full: leak this VA range (address space only -- |
| 772 | * no RM object, CPU mapping, or UVM range remains associated with it). */ |
| 773 | rm->faulted = 1; |
| 774 | return fail(rm, "VA free-list full; VA 0x%lx quarantined", (unsigned long)va); |
| 775 | } |
| 776 | for (uint32_t i = rm->free_count; i > pos; i--) rm->free_list[i] = rm->free_list[i - 1]; |
| 777 | rm->free_list[pos].va = va; |
| 778 | rm->free_list[pos].size = size; |
| 779 | rm->free_count++; |
| 780 | |
| 781 | /* Coalesce with the following entry. */ |
| 782 | if (pos + 1 < rm->free_count && rm->free_list[pos].va + rm->free_list[pos].size == rm->free_list[pos + 1].va) { |
| 783 | rm->free_list[pos].size += rm->free_list[pos + 1].size; |
| 784 | for (uint32_t i = pos + 1; i + 1 < rm->free_count; i++) rm->free_list[i] = rm->free_list[i + 1]; |
| 785 | rm->free_count--; |
| 786 | } |
| 787 | /* Coalesce with the preceding entry. */ |
| 788 | if (pos > 0 && rm->free_list[pos - 1].va + rm->free_list[pos - 1].size == rm->free_list[pos].va) { |
| 789 | rm->free_list[pos - 1].size += rm->free_list[pos].size; |
| 790 | for (uint32_t i = pos; i + 1 < rm->free_count; i++) rm->free_list[i] = rm->free_list[i + 1]; |
| 791 | rm->free_count--; |
| 792 | } |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static uint64_t va_take(Nvrm *rm, uint64_t size, uint64_t align) { |
| 797 | /* First-fit reuse from the free list (sorted ascending by va), splitting |
| 798 | * off any leading alignment pad or trailing remainder as smaller free |
| 799 | * entries. Falls back to the bump allocator when nothing fits. */ |
| 800 | for (uint32_t i = 0; i < rm->free_count; i++) { |
| 801 | uint64_t base = rm->free_list[i].va; |
| 802 | uint64_t sz = rm->free_list[i].size; |
| 803 | uint64_t range_end; |
| 804 | if (__builtin_add_overflow(base, sz, &range_end)) return 0; |
| 805 | uint64_t aligned = nvrm_policy_palign(base, align); |
| 806 | if (aligned == 0) return 0; |
| 807 | uint64_t pad = aligned - base; |
| 808 | if (pad > sz || size > sz - pad) continue; |
| 809 | uint64_t used_end; |
| 810 | if (__builtin_add_overflow(aligned, size, &used_end)) return 0; |
| 811 | |
| 812 | if (pad == 0 && used_end == range_end) { |
| 813 | /* Exact fit: remove entry i. */ |
| 814 | for (uint32_t j = i; j + 1 < rm->free_count; j++) rm->free_list[j] = rm->free_list[j + 1]; |
| 815 | rm->free_count--; |
| 816 | } else if (pad == 0) { |
| 817 | /* Consume from the front: shrink entry i. */ |
| 818 | rm->free_list[i].va = used_end; |
| 819 | rm->free_list[i].size = range_end - used_end; |
| 820 | } else { |
| 821 | /* Leading pad remains free at entry i. */ |
| 822 | rm->free_list[i].size = pad; |
| 823 | if (used_end < range_end && rm->free_count < NVRM_MAX_FREE) { |
| 824 | for (uint32_t j = rm->free_count; j > i + 1; j--) rm->free_list[j] = rm->free_list[j - 1]; |
| 825 | rm->free_list[i + 1].va = used_end; |
| 826 | rm->free_list[i + 1].size = range_end - used_end; |
| 827 | rm->free_count++; |
| 828 | } |
| 829 | else if (used_end < range_end) { |
| 830 | /* Preserve the range intact rather than silently losing VA. */ |
| 831 | rm->free_list[i].size = sz; |
| 832 | continue; |
| 833 | } |
| 834 | } |
| 835 | return aligned; |
| 836 | } |
| 837 | |
| 838 | uint64_t end; |
| 839 | uint64_t va = nvrm_policy_palign(rm->va_next, align); |
| 840 | if (va == 0) return 0; |
| 841 | if (__builtin_add_overflow(va, size, &end)) return 0; |
| 842 | if (!va_slot_cover(rm, end)) return 0; |
| 843 | rm->va_next = end; |
| 844 | return va; |
| 845 | } |
| 846 | #else |
| 847 | /* The unit mutates the borrowed range table in place. Only the atomic slot |
| 848 | * reservation and the client-visible error effects remain in this shell. */ |
| 849 | _Static_assert(sizeof(NvrmVaRange) == 2 * sizeof(uint64_t), "VA range wire layout"); |
| 850 | _Static_assert(offsetof(NvrmVaRange, size) == sizeof(uint64_t), "VA size offset"); |
| 851 | static int va_release(Nvrm *rm, uint64_t va, uint64_t size) { |
| 852 | uint64_t state[4] = {rm->free_count, rm->va_next, size, va}, result; |
| 853 | int status = omega_nvrm_va(&result, 1, state, 4, rm->free_list, NVRM_MAX_FREE * 2); |
| 854 | if (status || result) { |
| 855 | rm->faulted = 1; |
| 856 | if (status) return fail(rm, "Omega VA release refused"); |
| 857 | if (result == 1) return fail(rm, "invalid VA release extent"); |
| 858 | if (result == 2) return fail(rm, "overlapping VA release extent"); |
| 859 | return fail(rm, "VA free-list full; VA 0x%lx quarantined", (unsigned long)va); |
| 860 | } |
| 861 | rm->free_count = (uint32_t)state[0]; |
| 862 | return 0; |
| 863 | } |
| 864 | static uint64_t va_take(Nvrm *rm, uint64_t size, uint64_t align) { |
| 865 | uint64_t state[4] = {rm->free_count, rm->va_next, size, align}, va; |
| 866 | if (omega_nvrm_va(&va, 0, state, 4, rm->free_list, NVRM_MAX_FREE * 2)) return 0; |
| 867 | if (!va) return 0; |
| 868 | if (state[1] != rm->va_next && !va_slot_cover(rm, state[1])) return 0; |
| 869 | rm->free_count = (uint32_t)state[0]; |
| 870 | rm->va_next = state[1]; |
| 871 | return va; |
| 872 | } |
| 873 | #endif |
| 874 | |
| 875 | static int uvm_map(Nvrm *rm, uint64_t va, uint64_t size, uint32_t hmem, |
| 876 | uint8_t *uvm_created, uint8_t *dma_mapped) { |
| 877 | UVM_CREATE_EXTERNAL_RANGE_PARAMS cr; |
| 878 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 879 | memset(&cr, 0, sizeof cr); |
| 880 | cr.base = va; |
| 881 | cr.length = size; |
| 882 | #else |
| 883 | NS_FILL(cr, 20, va, size); |
| 884 | #endif |
| 885 | if (uvm(rm, rm->fd_uvm, UVM_CREATE_EXTERNAL_RANGE, &cr, &cr.rmStatus)) return -1; |
| 886 | *uvm_created = 1; |
| 887 | |
| 888 | NVOS46_PARAMETERS d; |
| 889 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 890 | memset(&d, 0, sizeof d); |
| 891 | d.hClient = rm->root; |
| 892 | d.hDevice = rm->device; |
| 893 | d.hDma = rm->virtmem; |
| 894 | d.hMemory = hmem; |
| 895 | d.length = size; |
| 896 | d.flags = (NVOS46_FLAGS_PAGE_SIZE_4KB << 8) | (NVOS46_FLAGS_CACHE_SNOOP_ENABLE << 4) | (NVOS46_FLAGS_DMA_OFFSET_FIXED_TRUE << 15); |
| 897 | d.dmaOffset = va; |
| 898 | #else |
| 899 | uint64_t fields[16] = {rm->root, rm->device, rm->virtmem, hmem, 0, size, |
| 900 | (NVOS46_FLAGS_PAGE_SIZE_4KB << 8) | (NVOS46_FLAGS_CACHE_SNOOP_ENABLE << 4) | (NVOS46_FLAGS_DMA_OFFSET_FIXED_TRUE << 15), |
| 901 | 0, 0, va}; |
| 902 | if (rm_wire(&d, sizeof d, 5, fields)) return fail(rm, "RM_MAP_MEMORY_DMA wire refused"); |
| 903 | #endif |
| 904 | if (nv_ioctl(rm->fd_ctl, NV_ESC_RM_MAP_MEMORY_DMA, &d, sizeof d) != 0 || d.status != 0) |
| 905 | return fail(rm, "RM_MAP_MEMORY_DMA errno %d status 0x%x", errno, d.status); |
| 906 | if (d.dmaOffset != va) return fail(rm, "dmaOffset mismatch"); |
| 907 | *dma_mapped = 1; |
| 908 | |
| 909 | static UVM_MAP_EXTERNAL_ALLOCATION_PARAMS ma; |
| 910 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 911 | memset(&ma, 0, sizeof ma); |
| 912 | ma.base = va; |
| 913 | ma.length = size; |
| 914 | ma.rmCtrlFd = rm->fd_ctl; |
| 915 | ma.hClient = rm->root; |
| 916 | ma.hMemory = hmem; |
| 917 | ma.gpuAttributesCount = 1; |
| 918 | memcpy(ma.perGpuAttributes[0].gpuUuid.uuid, rm->gpu_uuid, 16); |
| 919 | ma.perGpuAttributes[0].gpuMappingType = UvmGpuMappingTypeReadWriteAtomic; |
| 920 | #else |
| 921 | uint64_t uuid[2]; |
| 922 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 923 | NS_FILL(ma, 26, va, size, 0, uuid[0], uuid[1], UvmGpuMappingTypeReadWriteAtomic, |
| 924 | 1, (uint32_t)rm->fd_ctl, rm->root, hmem); |
| 925 | #endif |
| 926 | return uvm(rm, rm->fd_uvm, UVM_MAP_EXTERNAL_ALLOCATION, &ma, &ma.rmStatus); |
| 927 | } |
| 928 | |
| 929 | static void quarantine_allocation(Nvrm *rm, uint32_t h, uint64_t va, uint64_t size, |
| 930 | void *cpu, uint8_t uvm_live, uint8_t dma_live) { |
| 931 | NvrmLiveAlloc *e = &rm->live[rm->live_count++]; |
| 932 | *e = (NvrmLiveAlloc){ .handle = h, .va = va, .size = size, .cpu = cpu, |
| 933 | .uvm_live = uvm_live, .dma_live = dma_live, |
| 934 | .rm_live = 1, .cpu_live = 1, .quarantined = 1 }; |
| 935 | nvrm_mapping_transfer(rm, e); |
| 936 | rm->faulted = 1; |
| 937 | } |
| 938 | |
| 939 | static int alloc_with_cacheability(Nvrm *rm, uint64_t size, NvrmMem *out, uint32_t gpu_cacheable, int local); |
| 940 | |
| 941 | /* Coherent, CPU-mapped, GPU-mapped buffer at one shared virtual address. */ |
| 942 | int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out) { |
| 943 | return alloc_with_cacheability(rm, size, out, NVOS32_ATTR2_GPU_CACHEABLE_YES, 0); |
| 944 | } |
| 945 | |
| 946 | /* Same buffer, but the graphics chip does not keep it in its L2. The CPU |
| 947 | * mapping is already uncached, so a CPU store reaches memory; without this a |
| 948 | * resident chip program polling the buffer keeps reading its own L2 copy and |
| 949 | * never sees the store. For memory both sides poll, not for bulk data. */ |
| 950 | int nvrm_alloc_gpu_uncached(Nvrm *rm, uint64_t size, NvrmMem *out) { |
| 951 | return alloc_with_cacheability(rm, size, out, NVOS32_ATTR2_GPU_CACHEABLE_NO, 0); |
| 952 | } |
| 953 | |
| 954 | int nvrm_alloc_vram(Nvrm *rm, uint64_t size, NvrmMem *out) { |
| 955 | if (!rm || !rm->discrete || rm->target_sm != 86) |
| 956 | return rm ? fail(rm, "local framebuffer requires SM86") : -1; |
| 957 | return alloc_with_cacheability(rm, size, out, NVOS32_ATTR2_GPU_CACHEABLE_NO, 1); |
| 958 | } |
| 959 | |
| 960 | int nvrm_vram_info(Nvrm *rm, uint64_t *free_bytes, uint64_t *total_bytes) { |
| 961 | if (!rm || !free_bytes || !total_bytes || !rm->discrete) return -1; |
| 962 | /* NVIDIA nvos.h NVOS32_FUNCTION_INFO, Info.attr LOCATION_VIDMEM. |
| 963 | * This ioctl queries the selected device's framebuffer heap, not host RAM. */ |
| 964 | NVOS32_PARAMETERS p; |
| 965 | #if CUDAPP_MIGRATION_PENDING_C_NVRM |
| 966 | memset(&p, 0, sizeof p); |
| 967 | p.hRoot = rm->root; |
| 968 | p.hObjectParent = rm->device; |
| 969 | p.function = NVOS32_FUNCTION_INFO; |
| 970 | p.data.Info.attr = NVOS32_ATTR_LOCATION_VIDMEM << 25; |
| 971 | #else |
| 972 | uint64_t fields[16] = {rm->root, rm->device, NVOS32_FUNCTION_INFO, NVOS32_ATTR_LOCATION_VIDMEM << 25}; |
| 973 | uint64_t bytes = 0; |
| 974 | if (omega_nvrm_heap_wire(&bytes, fields, 16, (unsigned char *)&p, sizeof p) || bytes != sizeof p) |
| 975 | return fail(rm, "VID_HEAP_INFO wire refused"); |
| 976 | #endif |
| 977 | int iorc = nv_ioctl(rm->fd_ctl, NV_ESC_RM_VID_HEAP_CONTROL, &p, sizeof p); |
| 978 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 979 | uint64_t status = p.status, total = p.total, avail = p.free; |
| 980 | #else |
| 981 | uint64_t status = 0, total = 0, avail = 0, a[1] = {0}; |
| 982 | if (omega_nvrm_struct(&status, 7, 0, a, 1, &p, sizeof p)) return fail(rm, "heap status decode refused"); |
| 983 | a[0] = 1; |
| 984 | if (omega_nvrm_struct(&total, 7, 0, a, 1, &p, sizeof p)) return fail(rm, "heap total decode refused"); |
| 985 | a[0] = 2; |
| 986 | if (omega_nvrm_struct(&avail, 7, 0, a, 1, &p, sizeof p)) return fail(rm, "heap free decode refused"); |
| 987 | #endif |
| 988 | if (iorc != 0 || status) |
| 989 | return fail(rm, "VID_HEAP_INFO errno %d status 0x%x", errno, (unsigned)status); |
| 990 | if (!total || avail > total) return fail(rm, "invalid framebuffer heap information"); |
| 991 | *free_bytes = avail; |
| 992 | *total_bytes = total; |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | static int alloc_with_cacheability(Nvrm *rm, uint64_t size, NvrmMem *out, uint32_t gpu_cacheable, int local) { |
| 997 | if (!rm || !out || !size || rm->faulted || size > SIZE_MAX - 0xfffULL) |
| 998 | return rm ? fail(rm, "invalid or overflowed allocation size") : -1; |
| 999 | size = nvrm_policy_palign(size, 0x1000); |
| 1000 | if (!size) return fail(rm, "invalid or overflowed allocation size"); |
| 1001 | if (rm->live_count >= NVRM_MAX_LIVE) return fail(rm, "live allocation table full (%d)", NVRM_MAX_LIVE); |
| 1002 | uint64_t va = va_take(rm, size, 0x1000); |
| 1003 | if (!va) return fail(rm, "GPU VA exhausted: this client's VA slot is full (the next slot belongs to another client) or the VA overflowed"); |
| 1004 | /* Reserve the CPU range first so the fixed mapping can never clobber anything. */ |
| 1005 | void *res = nvrm_cpu_reserve(rm, va, size); |
| 1006 | if (res != (void *)va) { |
| 1007 | if (res != MAP_FAILED) nvrm_cpu_drop(rm, res, size); |
| 1008 | rm->faulted = 1; |
| 1009 | return fail(rm, "VA 0x%lx unavailable", (unsigned long)va); |
| 1010 | } |
| 1011 | |
| 1012 | NV_MEMORY_ALLOCATION_PARAMS mp; |
| 1013 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1014 | memset(&mp, 0, sizeof mp); |
| 1015 | mp.owner = rm->root; |
| 1016 | mp.type = NVOS32_TYPE_IMAGE; |
| 1017 | mp.flags = NVOS32_ALLOC_FLAGS_MAP_NOT_REQUIRED | NVOS32_ALLOC_FLAGS_MEMORY_HANDLE_PROVIDED | |
| 1018 | NVOS32_ALLOC_FLAGS_ALIGNMENT_FORCE | NVOS32_ALLOC_FLAGS_IGNORE_BANK_PLACEMENT; |
| 1019 | /* GB10 stays coherent system memory. Discrete payloads explicitly request |
| 1020 | * LOCAL_USER / VIDMEM; failure must never fall back into host RAM. */ |
| 1021 | mp.attr = (NVOS32_ATTR_PHYSICALITY_ALLOW_NONCONTIGUOUS << 27) | |
| 1022 | ((local ? NVOS32_ATTR_LOCATION_VIDMEM : NVOS32_ATTR_LOCATION_PCI) << 25); |
| 1023 | mp.attr2 = (gpu_cacheable << 2) | NVOS32_ATTR2_ZBC_PREFER_NO_ZBC; |
| 1024 | mp.format = 6; |
| 1025 | mp.size = size; |
| 1026 | mp.alignment = 0x1000; |
| 1027 | mp.limit = size - 1; |
| 1028 | #else |
| 1029 | NS_FILL(mp, 10, rm->root, NVOS32_TYPE_IMAGE, |
| 1030 | NVOS32_ALLOC_FLAGS_MAP_NOT_REQUIRED | NVOS32_ALLOC_FLAGS_MEMORY_HANDLE_PROVIDED | |
| 1031 | NVOS32_ALLOC_FLAGS_ALIGNMENT_FORCE | NVOS32_ALLOC_FLAGS_IGNORE_BANK_PLACEMENT, |
| 1032 | (NVOS32_ATTR_PHYSICALITY_ALLOW_NONCONTIGUOUS << 27) | |
| 1033 | ((local ? NVOS32_ATTR_LOCATION_VIDMEM : NVOS32_ATTR_LOCATION_PCI) << 25), |
| 1034 | (gpu_cacheable << 2) | NVOS32_ATTR2_ZBC_PREFER_NO_ZBC, 6, size, 0x1000, size - 1); |
| 1035 | #endif |
| 1036 | uint32_t h; |
| 1037 | if (rm_alloc(rm, rm->device, local ? NV01_MEMORY_LOCAL_USER : NV01_MEMORY_SYSTEM, &mp, sizeof mp, &h)) { |
| 1038 | if (nvrm_cpu_drop(rm, res, size) != 0) rm->faulted = 1; |
| 1039 | if (local && !rm->faulted && rm->last_rm_status != 0) { |
| 1040 | (void)va_release(rm, va, size); |
| 1041 | return -1; |
| 1042 | } |
| 1043 | rm->faulted = 1; /* unknown driver state: do not recycle this VA */ |
| 1044 | return fail_alloc_context(rm, size); |
| 1045 | } |
| 1046 | void *cpu = map_to_cpu(rm, h, size, (void *)va, 0, !local); |
| 1047 | if (cpu != (void *)va) { |
| 1048 | char why[sizeof rm->err]; |
| 1049 | memcpy(why, rm->err, sizeof why); |
| 1050 | quarantine_allocation(rm, h, va, size, (void *)va, 0, 0); |
| 1051 | if (local) { |
| 1052 | NvrmMem failed = {.handle = h, .va = va, .size = size, .cpu = (void *)va}; |
| 1053 | if (nvrm_free(rm, &failed) == 0) rm->faulted = 0; |
| 1054 | } |
| 1055 | return fail(rm, "cpu map at 0x%lx failed: %s", (unsigned long)va, why); |
| 1056 | } |
| 1057 | uint8_t uvm_created = 0, dma_mapped = 0; |
| 1058 | if (uvm_map(rm, va, size, h, &uvm_created, &dma_mapped)) { |
| 1059 | quarantine_allocation(rm, h, va, size, cpu, uvm_created, dma_mapped); |
| 1060 | if (local) { |
| 1061 | char why[sizeof rm->err]; |
| 1062 | memcpy(why, rm->err, sizeof why); |
| 1063 | NvrmMem failed = {.handle = h, .va = va, .size = size, .cpu = cpu}; |
| 1064 | if (nvrm_free(rm, &failed) == 0) rm->faulted = 0; |
| 1065 | memcpy(rm->err, why, sizeof rm->err); |
| 1066 | } |
| 1067 | return -1; |
| 1068 | } |
| 1069 | out->handle = h; |
| 1070 | out->va = va; |
| 1071 | out->size = size; |
| 1072 | out->cpu = cpu; |
| 1073 | memset(cpu, 0, size); |
| 1074 | |
| 1075 | rm->live[rm->live_count].handle = h; |
| 1076 | rm->live[rm->live_count].va = va; |
| 1077 | rm->live[rm->live_count].size = size; |
| 1078 | rm->live[rm->live_count].cpu = cpu; |
| 1079 | rm->live[rm->live_count].uvm_live = 1; |
| 1080 | rm->live[rm->live_count].dma_live = 1; |
| 1081 | rm->live[rm->live_count].rm_live = 1; |
| 1082 | rm->live[rm->live_count].cpu_live = 1; |
| 1083 | nvrm_mapping_transfer(rm, &rm->live[rm->live_count]); |
| 1084 | rm->live_count++; |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static int copy_live(Nvrm *rm, const NvrmMem *m, uint64_t off, size_t bytes) { |
| 1089 | if (!rm || !m || rm->faulted || !bytes || off > m->size || bytes > m->size - off) return -1; |
| 1090 | for (uint32_t i = 0; i < rm->live_count; i++) { |
| 1091 | const NvrmLiveAlloc *a = &rm->live[i]; |
| 1092 | if (a->handle == m->handle && a->va == m->va && a->size == m->size && |
| 1093 | a->cpu == m->cpu && a->cpu_live && a->rm_live && !a->quarantined) return 0; |
| 1094 | } |
| 1095 | return fail(rm, "copy requires a live allocation and an in-bounds span"); |
| 1096 | } |
| 1097 | |
| 1098 | int nvrm_copy_to(Nvrm *rm, const NvrmMem *m, uint64_t off, const void *src, size_t bytes) { |
| 1099 | if (!src || copy_live(rm, m, off, bytes)) return -1; |
| 1100 | volatile uint8_t *dst = (volatile uint8_t *)m->cpu + off; |
| 1101 | const uint8_t *in = src; |
| 1102 | for (size_t i = 0; i < bytes; i++) dst[i] = in[i]; |
| 1103 | nvrm_cpu_barrier(); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | int nvrm_copy_from(Nvrm *rm, const NvrmMem *m, uint64_t off, void *dst, size_t bytes) { |
| 1108 | if (!dst || copy_live(rm, m, off, bytes)) return -1; |
| 1109 | nvrm_cpu_barrier(); |
| 1110 | const volatile uint8_t *src = (const volatile uint8_t *)m->cpu + off; |
| 1111 | uint8_t *out = dst; |
| 1112 | for (size_t i = 0; i < bytes; i++) out[i] = src[i]; |
| 1113 | nvrm_cpu_barrier(); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | /* Reverse of nvrm_alloc(): drop the UVM external range, free the RM memory |
| 1118 | * object, unmap the CPU range, and return the VA range to the free list. */ |
| 1119 | int nvrm_free(Nvrm *rm, NvrmMem *m) { |
| 1120 | if (!rm || !m) return -1; |
| 1121 | |
| 1122 | /* Idempotent: freeing an already-zeroed NvrmMem is a silent no-op. */ |
| 1123 | if (m->handle == 0 && m->va == 0 && m->size == 0 && m->cpu == NULL) return 0; |
| 1124 | |
| 1125 | /* Reject anything that is not a currently-live allocation (e.g. a stale |
| 1126 | * copy of an NvrmMem that was already freed elsewhere) without touching |
| 1127 | * the driver at all. */ |
| 1128 | int idx = -1; |
| 1129 | for (uint32_t i = 0; i < rm->live_count; i++) { |
| 1130 | if (rm->live[i].va == m->va && rm->live[i].handle == m->handle && rm->live[i].size == m->size) { |
| 1131 | if (idx >= 0) { rm->faulted = 1; return fail(rm, "duplicate live allocation table entry"); } |
| 1132 | idx = (int)i; |
| 1133 | } |
| 1134 | } |
| 1135 | if (idx < 0) |
| 1136 | return fail(rm, "nvrm_free: va 0x%lx handle 0x%x size 0x%lx is not a live allocation", |
| 1137 | (unsigned long)m->va, m->handle, (unsigned long)m->size); |
| 1138 | |
| 1139 | NvrmLiveAlloc *live = &rm->live[idx]; |
| 1140 | if (live->cpu != m->cpu || !m->cpu || !m->size || |
| 1141 | (m->va & 0xfff) || (m->size & 0xfff)) { |
| 1142 | rm->faulted = 1; |
| 1143 | return fail(rm, "nvrm_free: allocation table identity corrupt"); |
| 1144 | } |
| 1145 | /* Every completed step is irreversible. Preserve the stage flags if the |
| 1146 | * next step fails, and quarantine the VA until all releases are proven. */ |
| 1147 | if (live->uvm_live) { |
| 1148 | if (uvm_free_checked(rm, m->va, m->size) != 0) { |
| 1149 | live->quarantined = 1; |
| 1150 | rm->faulted = 1; |
| 1151 | return -1; |
| 1152 | } |
| 1153 | live->uvm_live = 0; |
| 1154 | } |
| 1155 | if (live->dma_live) { |
| 1156 | if (dma_unmap_checked(rm, m->handle, m->va, m->size) != 0) { |
| 1157 | live->quarantined = 1; |
| 1158 | rm->faulted = 1; |
| 1159 | return -1; |
| 1160 | } |
| 1161 | live->dma_live = 0; |
| 1162 | } |
| 1163 | if (live->rm_live) { |
| 1164 | if (rm_free(rm, rm->device, m->handle) != 0) { |
| 1165 | live->quarantined = 1; |
| 1166 | rm->faulted = 1; |
| 1167 | return -1; |
| 1168 | } |
| 1169 | live->rm_live = 0; |
| 1170 | } |
| 1171 | if (live->cpu_live) { |
| 1172 | if (nvrm_cpu_drop(rm, m->cpu, m->size) != 0) { |
| 1173 | live->quarantined = 1; |
| 1174 | rm->faulted = 1; |
| 1175 | return fail(rm, "munmap VA 0x%lx errno %d", (unsigned long)m->va, errno); |
| 1176 | } |
| 1177 | live->cpu_live = 0; |
| 1178 | } |
| 1179 | |
| 1180 | /* Remove from the live table (swap with last). */ |
| 1181 | rm->live[idx] = rm->live[rm->live_count - 1]; |
| 1182 | rm->live_count--; |
| 1183 | |
| 1184 | if (va_release(rm, m->va, m->size) != 0) return -1; |
| 1185 | |
| 1186 | memset(m, 0, sizeof *m); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | /* One more GPFIFO channel in rm->chgroup on its own asynchronous subcontext: the same steps as channel 0 below (ring + USERD |
| 1191 | * GPU-uncached, notifier, GPFIFO, compute object, work-submit token, UVM_REGISTER_CHANNEL). */ |
| 1192 | static int chan_extra(Nvrm *rm, NvrmChan *c) { |
| 1193 | /* NVIDIA nvos.h:3195-3243, FERMI_CONTEXT_SHARE_A represents a subcontext. |
| 1194 | * Reusing channel 0's context share serialized the C4 event control on GB10; |
| 1195 | * a separate ASYNC allocation lets the independent queue run before its peer. |
| 1196 | * n_extra is counted before this call, so partial construction is unwound too. */ |
| 1197 | NV_CTXSHARE_ALLOCATION_PARAMETERS cs; |
| 1198 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1199 | memset(&cs, 0, sizeof cs); |
| 1200 | cs.hVASpace = rm->vaspace; |
| 1201 | cs.flags = NV_CTXSHARE_ALLOCATION_FLAGS_SUBCONTEXT_ASYNC; |
| 1202 | #else |
| 1203 | NS_FILL(cs, 13, rm->vaspace, NV_CTXSHARE_ALLOCATION_FLAGS_SUBCONTEXT_ASYNC); |
| 1204 | #endif |
| 1205 | if (rm_alloc(rm, rm->chgroup, FERMI_CONTEXT_SHARE_A, &cs, sizeof cs, &c->ctxshare)) return -1; |
| 1206 | c->entries = 1024; |
| 1207 | if (nvrm_alloc_gpu_uncached(rm, 0x10000, &c->fifo)) return -1; |
| 1208 | if (nvrm_alloc(rm, 0x100000, &c->notifier)) return -1; |
| 1209 | NV_CHANNEL_ALLOC_PARAMS gp; |
| 1210 | c->userd_off = c->entries * 8; |
| 1211 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1212 | memset(&gp, 0, sizeof gp); |
| 1213 | gp.gpFifoOffset = c->fifo.va; |
| 1214 | gp.gpFifoEntries = c->entries; |
| 1215 | gp.hObjectError = c->notifier.handle; |
| 1216 | gp.hObjectBuffer = c->fifo.handle; |
| 1217 | gp.hUserdMemory[0] = c->fifo.handle; |
| 1218 | gp.userdOffset[0] = c->userd_off; |
| 1219 | gp.engineType = 0; |
| 1220 | gp.hContextShare = c->ctxshare; |
| 1221 | #else |
| 1222 | NS_FILL(gp, 11, c->notifier.handle, c->fifo.handle, c->fifo.va, c->entries, |
| 1223 | c->ctxshare, 0, c->fifo.handle, c->userd_off, 0); |
| 1224 | #endif |
| 1225 | if (rm_alloc(rm, rm->chgroup, rm->gpfifo_class, &gp, sizeof gp, &c->gpfifo)) return -1; |
| 1226 | if (rm_alloc(rm, c->gpfifo, rm->compute_class, NULL, 0, &c->compute_obj)) return -1; |
| 1227 | NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN_PARAMS tk; |
| 1228 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1229 | tk.workSubmitToken = UINT32_MAX; |
| 1230 | #else |
| 1231 | NS_FILL(tk, 14, UINT32_MAX); |
| 1232 | #endif |
| 1233 | if (rm_control(rm, c->gpfifo, NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN, &tk, sizeof tk)) return -1; |
| 1234 | c->token = (uint32_t)NS_VALUE(tk, 14, 0, tk.workSubmitToken); |
| 1235 | UVM_REGISTER_CHANNEL_PARAMS rc; |
| 1236 | c->channel_va = va_take(rm, 0x4000000, 0x1000); |
| 1237 | if (!c->channel_va) { rm->faulted = 1; return fail(rm, "channel VA exhausted (extra channel)"); } |
| 1238 | c->channel_va_size = 0x4000000; |
| 1239 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1240 | memset(&rc, 0, sizeof rc); |
| 1241 | memcpy(rc.gpuUuid.uuid, rm->gpu_uuid, 16); |
| 1242 | rc.rmCtrlFd = rm->fd_ctl; |
| 1243 | rc.hClient = rm->root; |
| 1244 | rc.hChannel = c->gpfifo; |
| 1245 | rc.base = c->channel_va; |
| 1246 | rc.length = c->channel_va_size; |
| 1247 | #else |
| 1248 | uint64_t uuid[2]; |
| 1249 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 1250 | NS_FILL(rc, 24, uuid[0], uuid[1], (uint32_t)rm->fd_ctl, rm->root, c->gpfifo, |
| 1251 | c->channel_va, c->channel_va_size); |
| 1252 | #endif |
| 1253 | if (uvm(rm, rm->fd_uvm, UVM_REGISTER_CHANNEL, &rc, &rc.rmStatus)) { |
| 1254 | rm->faulted = 1; |
| 1255 | return -1; |
| 1256 | } |
| 1257 | c->registered = 1; |
| 1258 | c->put = c->retired = 0; |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | static int chan_extra_destroy(Nvrm *rm, NvrmChan *c) { |
| 1263 | if (c->registered) { |
| 1264 | UVM_UNREGISTER_CHANNEL_PARAMS p; |
| 1265 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1266 | memset(&p, 0, sizeof p); |
| 1267 | memcpy(p.gpuUuid.uuid, rm->gpu_uuid, sizeof rm->gpu_uuid); |
| 1268 | p.hClient = rm->root; |
| 1269 | p.hChannel = c->gpfifo; |
| 1270 | #else |
| 1271 | uint64_t uuid[2]; |
| 1272 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 1273 | NS_FILL(p, 25, uuid[0], uuid[1], rm->root, c->gpfifo); |
| 1274 | #endif |
| 1275 | if (uvm(rm, rm->fd_uvm, UVM_UNREGISTER_CHANNEL, &p, &p.rmStatus)) return -1; |
| 1276 | c->registered = 0; |
| 1277 | } |
| 1278 | if (c->compute_obj && rm_free(rm, c->gpfifo, c->compute_obj)) return -1; |
| 1279 | c->compute_obj = 0; |
| 1280 | if (c->gpfifo && rm_free(rm, rm->chgroup, c->gpfifo)) return -1; |
| 1281 | c->gpfifo = 0; |
| 1282 | if (c->ctxshare && rm_free(rm, rm->chgroup, c->ctxshare)) return -1; |
| 1283 | c->ctxshare = 0; |
| 1284 | if (c->fifo.handle && nvrm_free(rm, &c->fifo)) return -1; |
| 1285 | if (c->notifier.handle && nvrm_free(rm, &c->notifier)) return -1; |
| 1286 | if (c->channel_va_size) { |
| 1287 | if (va_release(rm, c->channel_va, c->channel_va_size) != 0) return -1; |
| 1288 | c->channel_va = 0; |
| 1289 | c->channel_va_size = 0; |
| 1290 | } |
| 1291 | c->put = c->retired = 0; |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | /* One decision per compute-object RM_ALLOC attempt: 0 = allocated, 1 = retry |
| 1296 | * after NV_ERR_NO_MEMORY, 2 = NO_MEMORY budget spent, 3 = any other failure. |
| 1297 | * The policy signature is (failed, tries, budget, nomem). Keep the call here, |
| 1298 | * where tests/test_ampere_runtime.c checks it on the success path too. */ |
| 1299 | static uint64_t channel_retry_decision(const Nvrm *rm, int arc, uint32_t budget) { |
| 1300 | return nvrm_policy_retry(arc != 0, rm->channel_no_memory_retries, budget, |
| 1301 | rm->last_rm_status == NV_ERR_NO_MEMORY); |
| 1302 | } |
| 1303 | |
| 1304 | int nvrm_channel(Nvrm *rm) { return nvrm_channel_n(rm, 0); } |
| 1305 | |
| 1306 | int nvrm_channel_n(Nvrm *rm, uint32_t extra) { |
| 1307 | if (!rm) return -1; |
| 1308 | uint64_t adm = nvrm_policy_chadm(rm->faulted, rm->chgroup, rm->channel_registered, |
| 1309 | rm->n_extra, extra, NVRM_MAX_EXTRA_CHANNELS); |
| 1310 | if (adm == 1) return fail(rm, "channel construction requires clean state"); |
| 1311 | if (adm == 2) return fail(rm, "extra channels %u > %d", extra, NVRM_MAX_EXTRA_CHANNELS); |
| 1312 | NV_CHANNEL_GROUP_ALLOCATION_PARAMETERS cg; |
| 1313 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1314 | memset(&cg, 0, sizeof cg); |
| 1315 | cg.engineType = NV2080_ENGINE_TYPE_GRAPHICS; |
| 1316 | #else |
| 1317 | NS_FILL(cg, 12, 0, NV2080_ENGINE_TYPE_GRAPHICS); |
| 1318 | #endif |
| 1319 | if (rm_alloc(rm, rm->device, KEPLER_CHANNEL_GROUP_A, &cg, sizeof cg, &rm->chgroup)) return -1; |
| 1320 | NV_CTXSHARE_ALLOCATION_PARAMETERS cs; |
| 1321 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1322 | memset(&cs, 0, sizeof cs); |
| 1323 | cs.hVASpace = rm->vaspace; |
| 1324 | cs.flags = NV_CTXSHARE_ALLOCATION_FLAGS_SUBCONTEXT_ASYNC; |
| 1325 | #else |
| 1326 | NS_FILL(cs, 13, rm->vaspace, NV_CTXSHARE_ALLOCATION_FLAGS_SUBCONTEXT_ASYNC); |
| 1327 | #endif |
| 1328 | if (rm_alloc(rm, rm->chgroup, FERMI_CONTEXT_SHARE_A, &cs, sizeof cs, &rm->ctxshare)) return -1; |
| 1329 | |
| 1330 | rm->entries = 1024; |
| 1331 | /* CPU publishes new queue entries and USERD GPPut after each completion. |
| 1332 | * A GPU-cached allocation can retain their previous contents on GB10; |
| 1333 | * CPU store barriers alone do not invalidate that cached copy. */ |
| 1334 | if (nvrm_alloc_gpu_uncached(rm, 0x10000, &rm->fifo)) return -1; |
| 1335 | if (nvrm_alloc(rm, 0x100000, &rm->notifier)) return -1; |
| 1336 | |
| 1337 | NV_CHANNEL_ALLOC_PARAMS gp; |
| 1338 | rm->userd_off = rm->entries * 8; /* USERD follows the ring. */ |
| 1339 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1340 | memset(&gp, 0, sizeof gp); |
| 1341 | gp.gpFifoOffset = rm->fifo.va; |
| 1342 | gp.gpFifoEntries = rm->entries; |
| 1343 | gp.hObjectError = rm->notifier.handle; |
| 1344 | gp.hObjectBuffer = rm->fifo.handle; |
| 1345 | gp.hUserdMemory[0] = rm->fifo.handle; |
| 1346 | gp.userdOffset[0] = rm->userd_off; |
| 1347 | gp.engineType = 0; |
| 1348 | gp.hContextShare = rm->ctxshare; |
| 1349 | #else |
| 1350 | NS_FILL(gp, 11, rm->notifier.handle, rm->fifo.handle, rm->fifo.va, rm->entries, |
| 1351 | rm->ctxshare, 0, rm->fifo.handle, rm->userd_off, 0); |
| 1352 | #endif |
| 1353 | if (rm_alloc(rm, rm->chgroup, rm->gpfifo_class, &gp, sizeof gp, &rm->gpfifo)) return -1; |
| 1354 | /* NV_ERR_NO_MEMORY here is the host lacking one free 2 MiB block for the |
| 1355 | * channel's main context buffer at this instant (see nvrm.h, omega#523). |
| 1356 | * Retry that one RM_ALLOC, bounded; any other status is final at once. */ |
| 1357 | uint32_t delay_ms = rm->channel_no_memory_retry_ms ? rm->channel_no_memory_retry_ms |
| 1358 | : NVRM_CHANNEL_NO_MEMORY_RETRY_MS; |
| 1359 | const uint32_t budget = NVRM_CHANNEL_NO_MEMORY_RETRIES; |
| 1360 | rm->channel_no_memory_retries = 0; |
| 1361 | for (;;) { |
| 1362 | int arc = rm_alloc(rm, rm->gpfifo, rm->compute_class, NULL, 0, &rm->compute_obj); |
| 1363 | uint64_t rd = channel_retry_decision(rm, arc, budget); |
| 1364 | if (rd == 0) break; |
| 1365 | if (rd == 3) return -1; |
| 1366 | if (rd == 2) return fail_channel_no_memory(rm, rm->channel_no_memory_retries + 1, delay_ms); |
| 1367 | rm->channel_no_memory_retries++; |
| 1368 | usleep((useconds_t)delay_ms * 1000u); |
| 1369 | } |
| 1370 | |
| 1371 | NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN_PARAMS tk; |
| 1372 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1373 | tk.workSubmitToken = UINT32_MAX; |
| 1374 | #else |
| 1375 | NS_FILL(tk, 14, UINT32_MAX); |
| 1376 | #endif |
| 1377 | if (rm_control(rm, rm->gpfifo, NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN, &tk, sizeof tk)) return -1; |
| 1378 | rm->token = (uint32_t)NS_VALUE(tk, 14, 0, tk.workSubmitToken); |
| 1379 | |
| 1380 | /* The VA space is UVM-owned: UVM must bind the channel's context buffers before RM will schedule it. */ |
| 1381 | UVM_REGISTER_CHANNEL_PARAMS rc; |
| 1382 | rm->channel_va = va_take(rm, 0x4000000, 0x1000); |
| 1383 | if (!rm->channel_va) { rm->faulted = 1; return fail(rm, "channel VA exhausted"); } |
| 1384 | rm->channel_va_size = 0x4000000; |
| 1385 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1386 | memset(&rc, 0, sizeof rc); |
| 1387 | memcpy(rc.gpuUuid.uuid, rm->gpu_uuid, 16); |
| 1388 | rc.rmCtrlFd = rm->fd_ctl; |
| 1389 | rc.hClient = rm->root; |
| 1390 | rc.hChannel = rm->gpfifo; |
| 1391 | rc.base = rm->channel_va; |
| 1392 | rc.length = rm->channel_va_size; |
| 1393 | #else |
| 1394 | uint64_t uuid[2]; |
| 1395 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 1396 | NS_FILL(rc, 24, uuid[0], uuid[1], (uint32_t)rm->fd_ctl, rm->root, rm->gpfifo, |
| 1397 | rm->channel_va, rm->channel_va_size); |
| 1398 | #endif |
| 1399 | if (uvm(rm, rm->fd_uvm, UVM_REGISTER_CHANNEL, &rc, &rc.rmStatus)) { |
| 1400 | rm->faulted = 1; |
| 1401 | return -1; |
| 1402 | } |
| 1403 | rm->channel_registered = 1; |
| 1404 | |
| 1405 | for (uint32_t i = 0; i < extra; i++) { |
| 1406 | rm->n_extra = i + 1; /* counted first: destroy undoes a half-built channel */ |
| 1407 | if (chan_extra(rm, &rm->extra[i])) return -1; |
| 1408 | } |
| 1409 | |
| 1410 | NVA06C_CTRL_GPFIFO_SCHEDULE_PARAMS sch; |
| 1411 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1412 | sch = (NVA06C_CTRL_GPFIFO_SCHEDULE_PARAMS){ .bEnable = 1 }; |
| 1413 | #else |
| 1414 | NS_FILL(sch, 15, 1); |
| 1415 | #endif |
| 1416 | if (rm_control(rm, rm->chgroup, NVA06C_CTRL_CMD_GPFIFO_SCHEDULE, &sch, sizeof sch)) return -1; |
| 1417 | rm->put = 0; |
| 1418 | rm->retired = 0; |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | int nvrm_channel_destroy(Nvrm *rm) { |
| 1423 | if (!rm) return -1; |
| 1424 | while (rm->n_extra) { |
| 1425 | if (chan_extra_destroy(rm, &rm->extra[rm->n_extra - 1])) { rm->faulted = 1; return -1; } |
| 1426 | rm->n_extra--; |
| 1427 | } |
| 1428 | if (rm->channel_registered) { |
| 1429 | UVM_UNREGISTER_CHANNEL_PARAMS p; |
| 1430 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1431 | memset(&p, 0, sizeof p); |
| 1432 | memcpy(p.gpuUuid.uuid, rm->gpu_uuid, sizeof rm->gpu_uuid); |
| 1433 | p.hClient = rm->root; |
| 1434 | p.hChannel = rm->gpfifo; |
| 1435 | #else |
| 1436 | uint64_t uuid[2]; |
| 1437 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 1438 | NS_FILL(p, 25, uuid[0], uuid[1], rm->root, rm->gpfifo); |
| 1439 | #endif |
| 1440 | if (uvm(rm, rm->fd_uvm, UVM_UNREGISTER_CHANNEL, &p, &p.rmStatus)) { |
| 1441 | rm->faulted = 1; |
| 1442 | return -1; |
| 1443 | } |
| 1444 | rm->channel_registered = 0; |
| 1445 | } |
| 1446 | if (rm->compute_obj && rm_free(rm, rm->gpfifo, rm->compute_obj)) goto failed; |
| 1447 | rm->compute_obj = 0; |
| 1448 | if (rm->gpfifo && rm_free(rm, rm->chgroup, rm->gpfifo)) goto failed; |
| 1449 | rm->gpfifo = 0; |
| 1450 | if (rm->ctxshare && rm_free(rm, rm->chgroup, rm->ctxshare)) goto failed; |
| 1451 | rm->ctxshare = 0; |
| 1452 | if (rm->chgroup && rm_free(rm, rm->device, rm->chgroup)) goto failed; |
| 1453 | rm->chgroup = 0; |
| 1454 | if (rm->fifo.handle && nvrm_free(rm, &rm->fifo)) goto failed; |
| 1455 | if (rm->notifier.handle && nvrm_free(rm, &rm->notifier)) goto failed; |
| 1456 | if (rm->channel_va_size) { |
| 1457 | if (va_release(rm, rm->channel_va, rm->channel_va_size) != 0) goto failed; |
| 1458 | rm->channel_va = 0; |
| 1459 | rm->channel_va_size = 0; |
| 1460 | } |
| 1461 | rm->put = rm->retired = 0; |
| 1462 | return 0; |
| 1463 | failed: |
| 1464 | rm->faulted = 1; |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
| 1468 | /* GP entry per clc96f.h: ENTRY0 GET 31:2 (VA low, dword aligned), ENTRY1 GET_HI 7:0, |
| 1469 | * LEVEL 9:9, LENGTH 30:10 in dwords, SYNC 31:31 = PROCEED. Bit 41 is ENTRY1 bit 9 set, which |
| 1470 | * clc96f.h names LEVEL_SUBROUTINE (MAIN is 0). The M16 chip receipts ran with this bit set, so it |
| 1471 | * stays until a GPU window qualifies LEVEL_MAIN. */ |
| 1472 | uint64_t nvrm_gp_entry(uint64_t va, uint32_t nwords) { |
| 1473 | return nvrm_policy_gpentry(va, nwords); |
| 1474 | } |
| 1475 | |
| 1476 | volatile uint32_t *nvrm_userd_gpput(Nvrm *rm) { |
| 1477 | return (volatile uint32_t *)((uint8_t *)rm->fifo.cpu + rm->userd_off + offsetof(Nvc96fControl, GPPut)); |
| 1478 | } |
| 1479 | |
| 1480 | /* Ampere admits only its implemented launch protocol, below both m16 and the |
| 1481 | * platform queue. Omega decodes the envelope and applies method/class policy. */ |
| 1482 | static int validate_ampere_push(Nvrm *rm, const NvrmMem *pb, uint32_t off, uint32_t nwords) { |
| 1483 | if (!rm->discrete) return 0; |
| 1484 | if (!pb->cpu) return fail(rm, "Ampere pushbuffer has no CPU mapping"); |
| 1485 | const uint32_t *w = (const uint32_t *)((const uint8_t *)pb->cpu + off); |
| 1486 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC |
| 1487 | for (uint32_t i = 0; i < nwords;) { |
| 1488 | const uint32_t h = w[i++], count = (h >> 16) & 0x1fffu; |
| 1489 | if ((h >> 28) != 2 || !count || count > nwords - i) |
| 1490 | return fail(rm, "Ampere push packet envelope refused"); |
| 1491 | const uint64_t m[16] = {4, (h & 0x1fffu) << 2, count, (h >> 13) & 7u, |
| 1492 | w[i], OMEGA_AMPERE_COMPUTE_B, 0x02b4, 0x02c0, 0x005c, 0x0028, |
| 1493 | 0x021c, (1u << 0) | (1u << 4) | (1u << 12)}; |
| 1494 | if (omega_ampere_policy(m, NULL)) return fail(rm, "Ampere method/class refused before submission"); |
| 1495 | i += count; |
| 1496 | } |
| 1497 | #else |
| 1498 | uint64_t result; |
| 1499 | if (omega_nvrm_alloc_push(&result, w, nwords)) |
| 1500 | return fail(rm, "Omega Ampere push validation refused"); |
| 1501 | if (result == 1) return fail(rm, "Ampere push packet envelope refused"); |
| 1502 | if (result == 2) return fail(rm, "Ampere method/class refused before submission"); |
| 1503 | #endif |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | /* GPFIFO submit stores (C4 streams, CUDApp#115). The Omega path is |
| 1508 | * native/streams/submit.om on the client's mapping context: the fifo and |
| 1509 | * usermode pages are owned handles there, each store one aligned access |
| 1510 | * bracketed by DSB SY / MFENCE, the barrier nvrm_cpu_barrier issues below. |
| 1511 | * CUDAPP_MIGRATION_PENDING_C_SUBMIT=1 (or the C transport, which has no |
| 1512 | * handles) keeps the C stores as the differential oracle. */ |
| 1513 | #ifndef CUDAPP_MIGRATION_PENDING_C_SUBMIT |
| 1514 | #define CUDAPP_MIGRATION_PENDING_C_SUBMIT 0 |
| 1515 | #endif |
| 1516 | #define NVRM_C_SUBMIT (NVRM_C_TRANSPORT || CUDAPP_MIGRATION_PENDING_C_SUBMIT) |
| 1517 | static int nvrm_publish(Nvrm *rm, const NvrmMem *fifo, uint32_t entries, uint32_t put, |
| 1518 | uint32_t userd_off, uint64_t va, uint32_t nwords) { |
| 1519 | #if NVRM_C_SUBMIT |
| 1520 | (void)rm; |
| 1521 | volatile uint64_t *ring = (volatile uint64_t *)fifo->cpu; |
| 1522 | ring[put % entries] = nvrm_gp_entry(va, nwords); |
| 1523 | nvrm_cpu_barrier(); |
| 1524 | *(volatile uint32_t *)((uint8_t *)fifo->cpu + userd_off + offsetof(Nvc96fControl, GPPut)) = (put + 1u) % entries; |
| 1525 | nvrm_cpu_barrier(); |
| 1526 | return 0; |
| 1527 | #else |
| 1528 | uint64_t token = 0, base = 0, result = 0; |
| 1529 | /* The ring starts at the mapping: GP slot offsets are relative to the handle. */ |
| 1530 | if (nvrm_live_token(rm, fifo->cpu, fifo->size, &token, &base)) return -1; |
| 1531 | if (base) { errno = EFAULT; return -1; } |
| 1532 | uint32_t status = omega2_ctx_streams_gp_publish(rm->omega_context, &result, token, entries, put, |
| 1533 | (uint64_t)userd_off + offsetof(Nvc96fControl, GPPut), va, nwords); |
| 1534 | return nvrm_native_ok(status, result); |
| 1535 | #endif |
| 1536 | } |
| 1537 | static int nvrm_doorbell(Nvrm *rm, uint32_t token) { |
| 1538 | #if NVRM_C_SUBMIT |
| 1539 | *rm->doorbell = token; |
| 1540 | return 0; |
| 1541 | #else |
| 1542 | uint64_t result = 0; |
| 1543 | if (!rm->omega_usermode_mapping || !rm->omega_context) { errno = EINVAL; return -1; } |
| 1544 | uint32_t status = omega2_ctx_streams_ring(rm->omega_context, &result, rm->omega_usermode_mapping, |
| 1545 | NVC361_NOTIFY_CHANNEL_PENDING, token); |
| 1546 | return nvrm_native_ok(status, result); |
| 1547 | #endif |
| 1548 | } |
| 1549 | |
| 1550 | int nvrm_enqueue(Nvrm *rm, const NvrmMem *pb, uint32_t off, uint32_t nwords) { |
| 1551 | if (!rm || !pb || rm->faulted || !rm->gpfifo || !rm->fifo.cpu || rm->entries < 2) |
| 1552 | return rm ? fail(rm, "channel not ready") : -1; |
| 1553 | uint64_t sp = nvrm_policy_pspan(off, nwords, pb->size, pb->va); |
| 1554 | if (sp == 1) return fail(rm, "bad pushbuffer span"); |
| 1555 | if (validate_ampere_push(rm, pb, off, nwords)) return -1; |
| 1556 | if (sp == 2) return fail(rm, "pushbuffer VA overflow"); |
| 1557 | uint64_t full = nvrm_policy_pfull(rm->put, rm->retired, rm->entries); |
| 1558 | if (full) return fail(rm, "GPFIFO full: %u entries unretired", rm->put - rm->retired); |
| 1559 | if (nvrm_publish(rm, &rm->fifo, rm->entries, rm->put, rm->userd_off, pb->va + off, nwords)) |
| 1560 | return fail(rm, "GPFIFO publish refused"); |
| 1561 | rm->put++; |
| 1562 | return 0; |
| 1563 | } |
| 1564 | |
| 1565 | void nvrm_retire(Nvrm *rm, uint32_t upto) { |
| 1566 | if (upto - rm->retired <= rm->put - rm->retired) rm->retired = upto; |
| 1567 | } |
| 1568 | |
| 1569 | void nvrm_ring(Nvrm *rm) { |
| 1570 | if (nvrm_doorbell(rm, rm->token)) rm->faulted = 1; /* NVC361_NOTIFY_CHANNEL_PENDING */ |
| 1571 | } |
| 1572 | |
| 1573 | int nvrm_submit(Nvrm *rm, const NvrmMem *pb, uint32_t off, uint32_t nwords) { |
| 1574 | if (nvrm_enqueue(rm, pb, off, nwords)) return -1; |
| 1575 | nvrm_ring(rm); |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | /* nvrm_enqueue + nvrm_ring for an extra channel: the same GP entry, barriers and USERD GPPut, its own ring and token. */ |
| 1580 | int nvrm_chan_submit(Nvrm *rm, uint32_t ch, const NvrmMem *pb, uint32_t off, uint32_t nwords) { |
| 1581 | if (!rm || !pb || rm->faulted || ch == 0 || ch > rm->n_extra) return rm ? fail(rm, "extra channel %u not ready", ch) : -1; |
| 1582 | NvrmChan *c = &rm->extra[ch - 1]; |
| 1583 | if (!c->gpfifo || !c->fifo.cpu || c->entries < 2) return fail(rm, "extra channel %u not ready", ch); |
| 1584 | uint64_t sp = nvrm_policy_pspan(off, nwords, pb->size, pb->va); |
| 1585 | if (sp == 1) return fail(rm, "bad pushbuffer span"); |
| 1586 | if (sp == 2) return fail(rm, "pushbuffer VA overflow"); |
| 1587 | if (validate_ampere_push(rm, pb, off, nwords)) return -1; |
| 1588 | uint64_t full = nvrm_policy_pfull(c->put, c->retired, c->entries); |
| 1589 | if (full) return fail(rm, "GPFIFO full (channel %u): %u entries unretired", ch, c->put - c->retired); |
| 1590 | if (nvrm_publish(rm, &c->fifo, c->entries, c->put, c->userd_off, pb->va + off, nwords)) |
| 1591 | return fail(rm, "GPFIFO publish refused (channel %u)", ch); |
| 1592 | c->put++; |
| 1593 | /* NVC361_NOTIFY_CHANNEL_PENDING: one doorbell page, the token names the channel */ |
| 1594 | if (nvrm_doorbell(rm, c->token)) return fail(rm, "doorbell refused (channel %u)", ch); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | void nvrm_chan_retire(Nvrm *rm, uint32_t ch, uint32_t upto) { |
| 1599 | if (!rm || ch == 0 || ch > rm->n_extra) return; |
| 1600 | NvrmChan *c = &rm->extra[ch - 1]; |
| 1601 | if (upto - c->retired <= c->put - c->retired) c->retired = upto; |
| 1602 | } |
| 1603 | |
| 1604 | uint32_t nvrm_chan_put(const Nvrm *rm, uint32_t ch) { |
| 1605 | if (!rm) return 0; |
| 1606 | if (ch == 0) return rm->put; |
| 1607 | return ch <= rm->n_extra ? rm->extra[ch - 1].put : 0; |
| 1608 | } |
| 1609 | |
| 1610 | int nvrm_close(Nvrm *rm) { |
| 1611 | if (!rm) return -1; |
| 1612 | if (rm->root && nvrm_channel_destroy(rm)) rm->faulted = 1; |
| 1613 | /* Release every remaining allocation, including caller-owned buffers and |
| 1614 | * allocations left behind by an interrupted construction path. */ |
| 1615 | for (uint32_t i = rm->live_count; i > 0;) { |
| 1616 | NvrmLiveAlloc e = rm->live[--i]; |
| 1617 | NvrmMem m = { .handle = e.handle, .va = e.va, .size = e.size, .cpu = e.cpu }; |
| 1618 | if (nvrm_free(rm, &m) != 0) { |
| 1619 | rm->faulted = 1; |
| 1620 | /* Closing the client tears down driver state. The CPU mapping is |
| 1621 | * process state, so explicitly unmap it even on teardown error. */ |
| 1622 | if (e.cpu_live && e.cpu) (void)nvrm_cpu_drop(rm, e.cpu, e.size); |
| 1623 | } else if (i < rm->live_count) i++; |
| 1624 | } |
| 1625 | if (rm->usermode_cpu) { |
| 1626 | if (nvrm_cpu_drop(rm, rm->usermode_cpu, 0x10000) != 0) rm->faulted = 1; |
| 1627 | else { |
| 1628 | rm->usermode_cpu = NULL; |
| 1629 | rm->doorbell = NULL; |
| 1630 | } |
| 1631 | } |
| 1632 | if (rm->vas_registered) { |
| 1633 | UVM_UNREGISTER_GPU_VASPACE_PARAMS p; |
| 1634 | int packed = 0; |
| 1635 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1636 | memset(&p, 0, sizeof p); |
| 1637 | memcpy(p.gpuUuid.uuid, rm->gpu_uuid, sizeof rm->gpu_uuid); |
| 1638 | #else |
| 1639 | uint64_t uuid[2]; |
| 1640 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 1641 | packed = NS_PACK(p, 22, uuid[0], uuid[1]); |
| 1642 | #endif |
| 1643 | if (packed || uvm(rm, rm->fd_uvm, UVM_UNREGISTER_GPU_VASPACE, &p, &p.rmStatus)) rm->faulted = 1; |
| 1644 | rm->vas_registered = 0; |
| 1645 | } |
| 1646 | if (rm->gpu_registered) { |
| 1647 | UVM_UNREGISTER_GPU_PARAMS p; |
| 1648 | int packed = 0; |
| 1649 | #if CUDAPP_MIGRATION_PENDING_C_NVRM_STRUCT |
| 1650 | memset(&p, 0, sizeof p); |
| 1651 | memcpy(p.gpu_uuid.uuid, rm->gpu_uuid, sizeof rm->gpu_uuid); |
| 1652 | #else |
| 1653 | uint64_t uuid[2]; |
| 1654 | memcpy(uuid, rm->gpu_uuid, sizeof uuid); |
| 1655 | packed = NS_PACK(p, 22, uuid[0], uuid[1]); |
| 1656 | #endif |
| 1657 | if (packed || uvm(rm, rm->fd_uvm, UVM_UNREGISTER_GPU, &p, &p.rmStatus)) rm->faulted = 1; |
| 1658 | rm->gpu_registered = 0; |
| 1659 | } |
| 1660 | /* Free root children explicitly so a clean close proves every RM object |
| 1661 | * was accepted for release; parent teardown alone hides leaked handles. */ |
| 1662 | if (rm->usermode && rm_free(rm, rm->subdevice, rm->usermode)) rm->faulted = 1; |
| 1663 | rm->usermode = 0; |
| 1664 | if (rm->vaspace && rm_free(rm, rm->device, rm->vaspace)) rm->faulted = 1; |
| 1665 | rm->vaspace = 0; |
| 1666 | if (rm->virtmem && rm_free(rm, rm->device, rm->virtmem)) rm->faulted = 1; |
| 1667 | rm->virtmem = 0; |
| 1668 | if (rm->subdevice && rm_free(rm, rm->device, rm->subdevice)) rm->faulted = 1; |
| 1669 | rm->subdevice = 0; |
| 1670 | if (rm->device && rm_free(rm, rm->root, rm->device)) rm->faulted = 1; |
| 1671 | rm->device = 0; |
| 1672 | if (rm->root && rm_free(rm, rm->root, rm->root)) rm->faulted = 1; |
| 1673 | rm->root = 0; |
| 1674 | /* F12: clear each descriptor after closing it, so a second nvrm_close cannot close a number reused since */ |
| 1675 | if (rm->fd_dev > 0) nvrm_file_close(rm->fd_dev); |
| 1676 | rm->fd_dev = 0; |
| 1677 | if (rm->fd_uvm2 > 0) nvrm_file_close(rm->fd_uvm2); |
| 1678 | rm->fd_uvm2 = 0; |
| 1679 | if (rm->fd_uvm > 0) nvrm_file_close(rm->fd_uvm); |
| 1680 | rm->fd_uvm = 0; |
| 1681 | if (rm->fd_ctl > 0) nvrm_file_close(rm->fd_ctl); |
| 1682 | rm->fd_ctl = 0; |
| 1683 | if (nvrm_mapping_finish(rm)) rm->faulted = 1; |
| 1684 | if (!rm->faulted && rm->live_count == 0 && !rm->channel_registered && |
| 1685 | rm->va_slot_base && rm->va_next >= rm->va_slot_base && |
| 1686 | rm->va_next - rm->va_slot_base <= 0x100000000ull) |
| 1687 | recycle_va_slot(rm->va_slot_base); |
| 1688 | rm->va_slot_base = 0; |
| 1689 | return rm->faulted ? -1 : 0; |
| 1690 | } |