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 | # The driver client, call by call |
| 2 | |
| 3 | This is what replaces `libcuda`: `nvrm/nvrm.c` (about 900 lines) and the qualification programs in `m16/`. They talk to NVIDIA's open kernel module, version 580.173.02, through the ioctls its public headers define (`third_party/nvidia-open-580.173.02/`, MIT, unchanged). Line numbers below are for `nvrm/nvrm.c` on the commit that added this file. |
| 4 | |
| 5 |  |
| 6 | |
| 7 | ## The three device files |
| 8 | |
| 9 | | File | Opened by | Used for | |
| 10 | | --- | --- | --- | |
| 11 | | `/dev/nvidiactl` | `nvrm_open()` | The resource manager (RM): every object is created with `NV_ESC_RM_ALLOC`, configured with `NV_ESC_RM_CONTROL` and destroyed with `NV_ESC_RM_FREE`. | |
| 12 | | `/dev/nvidia<minor>` | `open_dev_fd()` (line 231), registered with `NV_ESC_REGISTER_FD` | CPU mappings of memory and of the usermode register page (`NV_ESC_RM_MAP_MEMORY`, then `mmap`). | |
| 13 | | `/dev/nvidia-uvm`, opened twice | `nvrm_open()` | The unified-memory driver, which owns the GPU page tables of the address space. | |
| 14 | |
| 15 | ## 1. Open: `nvrm_open()` (line 293) |
| 16 | |
| 17 | In order: |
| 18 | |
| 19 | 1. Reserve a slot of virtual addresses for this process, then open `/dev/nvidiactl` and `/dev/nvidia-uvm`. |
| 20 | 2. `RM_ALLOC NV01_ROOT_CLIENT`: the root handle. Everything below hangs off it and is freed with it. |
| 21 | 3. `UVM_INITIALIZE`, then `UVM_MM_INITIALIZE` on the second UVM file (a failure there is tolerated, as `libcuda` tolerates it). |
| 22 | 4. `NV_ESC_CARD_INFO` lists the GPUs; take the first valid one and open its device file. |
| 23 | 5. `RM_CONTROL NV0000_CTRL_CMD_GPU_GET_ID_INFO_V2` gives the device instance. |
| 24 | 6. `RM_ALLOC NV01_DEVICE_0`, then `NV20_SUBDEVICE_0`, then `NV01_MEMORY_VIRTUAL` (the GPU address range). |
| 25 | 7. `RM_CONTROL NV0080_CTRL_CMD_GPU_GET_CLASSLIST`, called twice (count, then list). The client looks for three classes and refuses to go on without them: `BLACKWELL_COMPUTE_B`, `BLACKWELL_CHANNEL_GPFIFO_A` and `HOPPER_USERMODE_A`. |
| 26 | 8. `RM_ALLOC HOPPER_USERMODE_A` under the subdevice and map 64 KiB of it. The doorbell is the word at `NVC361_NOTIFY_CHANNEL_PENDING` inside that page. |
| 27 | 9. `RM_CONTROL NV2080_CTRL_CMD_GR_GET_INFO` reads the SM version (sm_121 on the GB10). |
| 28 | 10. `RM_ALLOC FERMI_VASPACE_A` with page faulting on and `IS_EXTERNALLY_OWNED`, so the unified-memory driver manages it. |
| 29 | 11. `RM_CONTROL NV2080_CTRL_CMD_GPU_GET_GID_INFO` reads the GPU UUID, then `UVM_REGISTER_GPU` and `UVM_REGISTER_GPU_VASPACE` hand the GPU and the address space to UVM. |
| 30 | |
| 31 | ## 2. Memory: `nvrm_alloc()` (line 546) and `nvrm_free()` (line 624) |
| 32 | |
| 33 | The GB10 has no separate video memory; the CPU and GPU share one LPDDR5X memory. Each buffer is: |
| 34 | |
| 35 | 1. a range taken from the process's free list and reserved on the CPU side with `mmap(MAP_FIXED_NOREPLACE)`, so nothing else can land there; |
| 36 | 2. `RM_ALLOC NV01_MEMORY_SYSTEM` (non-contiguous, PCI location, GPU-cacheable or not); |
| 37 | 3. mapped for the CPU at exactly that address (`NV_ESC_RM_MAP_MEMORY` and `mmap`); |
| 38 | 4. mapped for the GPU at the same address: `UVM_CREATE_EXTERNAL_RANGE`, `NV_ESC_RM_MAP_MEMORY_DMA`, `UVM_MAP_EXTERNAL_ALLOCATION` (line 498). |
| 39 | |
| 40 | So the CPU pointer and the GPU address of a buffer are the same number. Nothing is copied between a "host" and a "device" copy, because there is only one copy. |
| 41 | |
| 42 | `nvrm_alloc_gpu_uncached()` (line 554) makes the GPU skip its L2 cache for the buffer. Anything the CPU polls while the GPU writes it (completion markers, semaphores, the command ring) must be allocated this way; the platform contract refuses a host-polled buffer that is not (`src/omega_gpu_platform.h`). |
| 43 | |
| 44 | `nvrm_free()` undoes the four steps in reverse and returns the range to the free list. It refuses a stale or duplicate handle without calling the driver. If any step fails half way, the allocation is quarantined rather than reused, because the driver's state is then unknown. |
| 45 | |
| 46 | ## 3. The channel: `nvrm_channel()` (line 695) |
| 47 | |
| 48 | 1. `RM_ALLOC KEPLER_CHANNEL_GROUP_A` on the graphics/compute engine, then `FERMI_CONTEXT_SHARE_A` bound to the address space. |
| 49 | 2. One GPU-uncached 64 KiB buffer holds the command ring (1,024 entries of 8 bytes) followed by its USERD control page; a 1 MiB buffer receives channel errors. |
| 50 | 3. `RM_ALLOC BLACKWELL_CHANNEL_GPFIFO_A` with the ring, USERD and error buffer. |
| 51 | 4. `RM_ALLOC BLACKWELL_COMPUTE_B` under the channel. This one allocation needs one free, physically contiguous 2 MiB block of host memory for the channel's context buffer. On a long-running host that block can be missing for a few hundred milliseconds, so `NV_ERR_NO_MEMORY` here, and only here, is retried: 50 times at 100 ms (`NVRM_CHANNEL_NO_MEMORY_RETRIES`, omega#523). Every other error is final at once. |
| 52 | 5. `RM_CONTROL NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN`: the number the doorbell is rung with. |
| 53 | 6. `UVM_REGISTER_CHANNEL` with 64 MiB of address space for the channel's context buffers, then `RM_CONTROL NVA06C_CTRL_CMD_GPFIFO_SCHEDULE` switches the channel on. |
| 54 | |
| 55 | ## 4. Submitting work: `nvrm_enqueue()` (line 818) and `nvrm_ring()` (line 840) |
| 56 | |
| 57 | A ring entry is 8 bytes (`nvrm_gp_entry()`, line 810): the pushbuffer's GPU address in the low bits and its length in 32-bit words from bit 42. To submit: |
| 58 | |
| 59 | 1. refuse if the ring would overrun entries that have not been seen to finish (Blackwell's USERD reports no read pointer, so the client counts: `put` and `retired`); |
| 60 | 2. write the entry, `dsb sy`, write the new `GPPut` into USERD, `dsb sy`; |
| 61 | 3. ring the doorbell: one 32-bit store of the work-submit token to the usermode page. |
| 62 | |
| 63 | There is no system call on this path. The pushbuffer itself is a list of method headers (`nvrm_mthd()` in `nvrm/nvrm.h`) and data words; what goes in it for a kernel launch is in `src/omega_gpu_cmdlist.c` and `src/omega_blackwell_submit.c`. |
| 64 | |
| 65 | ## 5. Waiting: `m16/m16_gpu_wait.c` |
| 66 | |
| 67 | Completion is a word in GPU-uncached memory that the GPU writes with a semaphore release. The one wait primitive has two kinds (an exact value, or a counter that has reached a target, correct across wrap-around) and two bounds: a hard deadline that is never extended and a stall deadline that restarts only when a watched word changes. A memory barrier runs before every read. A wait that times out is reported as uncertain: the session then keeps every buffer and refuses all further GPU work in the process, because the GPU may still be running. |
| 68 | |
| 69 | ## 6. Proof on the chip: `m16/` |
| 70 | |
| 71 | - `m16_requalify.c`: queue a semaphore release without ringing the doorbell and check the marker stays 0 for 100 ms; ring it and check the marker becomes `0x16c0ffee`; read `/proc/self/maps` to prove no `libcuda` was loaded. Receipt: `evidence/m16-blackwell-native-path-requalification-receipt.json`. |
| 72 | - `m16_concurrent.c`: two independent channels; ringing one never runs the other's work. |
| 73 | - `nvrm/lifecycle_gates.c`: `nvrm_free()` on the real chip (address reuse, stale and duplicate handles, CPU unmap) and the counter wait. |
| 74 | - `nvrm/submission_visibility_gates.c`: ring entries and rewritten command buffers published by the CPU are seen by the GPU on the second submission too (before the fix, the second submission stalled on the GB10). |
| 75 | |
| 76 | ## 7. Above and below the client |
| 77 | |
| 78 | Above it, `src/omega_gpu_platform.h` is the contract the runtime is written against: typed handles instead of file descriptors, explicit rights and kinds for every buffer, polling named as polling, and no silent fallback. `src/omega_gpu_platform_linux.c` implements it on this client. The C reference of Aien's own GB10 driver (L332, aienux#441) implements the same contract in a simulator. `src/omega_gpu_platform_aien.c` is that driver's user-space side (section 8): migration-pending C, compiled only for its host test. |
| 79 | |
| 80 | Below it, today, NVIDIA's open kernel module (`nvidia.ko`, `nvidia-uvm.ko`) does the rest: it boots the GPU's GSP firmware, builds page tables and schedules channels. Aien's own GB10 driver replaces that module (aien-dev/aien-gpu, aien-dev/omega-gpu-driver, aienux#441; BUILDING in `ROADMAP.md`, nothing run on the GB10 yet). This client speaks NVIDIA's ioctl interface, so it has to move onto that driver's interface; that move is PROPOSED. The firmware stays NVIDIA's; the chip checks its signature. |
| 81 | |
| 82 | ## 8. The second implementation: aien-gpu.ko (`src/omega_gpu_platform_aien.c`) |
| 83 | |
| 84 | Evidence level: IMPLEMENTED, host-tested on a simulated device. Hardware NOT_RUN: it has never opened a real `/dev/aien-gpu`. The shipped path stays NVIDIA's open kernel module (section 7) until the move onto Aien's own driver lands. |
| 85 | |
| 86 | Migration-pending C: the definition exists only when built with `CUDAPP_MIGRATION_PENDING_AIEN_GPU=1`, which only the host test and its mutants set; `libomega_gpu.a` and every other build leave it OFF. Its decisions and request bytes are now seeded Omega, `native/aiengpu/context.om` (aien-dev/CUDApp#128), called through the generated `src/cudapp_om_aiengpu.c` (`omega_aiengpu`): the AGPU_INFO verdict, buffer size and VA-window search, the AGPU_MEM_ALLOC/MEM_FREE/CHAN_CREATE/CHAN_DESTROY request bytes and RM verdict, the cache policy, poll and range checks, the serial compare, the GPFIFO entry and the submit plan (length and ring-full refusals, omega #457 prefix, ring slot, GPPut). The C rules they replaced stay behind `CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN` (OFF) as the oracle: `make test-gpu-platform-aien` requires the simulated-device traces of both (every ioctl's bytes in and out, map arguments, final ring, USERD GPPut, pushbuffer and doorbell) to be byte-identical, and `make test-gpu-platform-aien-om-mutants` compiles mutant `context.om` files with the pinned omc. Open, ioctl, mmap, the ordered ring/USERD/doorbell stores, the diagnostic text and the function table stay C: function-mode Omega has no devctl for AGPU ('A') requests, no maphold/mapbind (refused in function mode at omega-mv#1040), no text constants and no C function-table export (aien-dev/omega-bridge#29). |
| 87 | |
| 88 | aien-gpu.ko is Aien's own GB10 kernel module (aien-dev/aien-gpu, L332, aienux#441); in place of `nvidia.ko` and `nvidia-uvm.ko` it exposes a small compute interface of its own, `third_party/aien-gpu-abi1/aien_gpu_uapi.h` (ABI 1). The platform fills the same table with the same caps and refusals as the Linux reference, except that it drives one queue where the reference can open extra channels, so the session, command list and libraries run on it unchanged: |
| 89 | |
| 90 | - `device_open`: open `/dev/aien-gpu` and `AGPU_INFO`; anything but ABI 1 in state `RM_READY` is a REFUSED_DEVICE with the state named. A missing node is a REFUSED_DEVICE naming the path. |
| 91 | - `dma_alloc`: reserve a CPU range in [64 GiB, 1 TiB) with `MAP_FIXED_NOREPLACE` (a GPFIFO entry carries 40 address bits), `AGPU_MEM_ALLOC` at that address (`AGPU_MEM_GPU_UNCACHED` unless a device-private DATA buffer asked for GPU_CACHED), then map the object `MAP_FIXED|MAP_SHARED` over the reservation. The GPU address is the CPU address. |
| 92 | - `queue_open`: one GPU-uncached buffer with the 1024-entry ring and USERD after it, an error notifier, `AGPU_CHAN_CREATE`, and a mapping of the usermode page. |
| 93 | - `queue_submit`: the three steps of section 4 (entry from `omega_gpu_aien_gp_entry`, bit for bit `nvrm_gp_entry`; `GPPut` at `Nvc96fControl.GPPut`; the token stored at +0x90). `queue_retire`, `poll_word` and the omega #457 L2 invalidate prefix work as in the Linux reference. |
| 94 | - `device_close` frees every buffer and the channel (a mapping keeps an aien-gpu object alive past `close`). |
| 95 | |
| 96 | What selects it: a program built with the switch ON calls `omega_gpu_session_set_platform(omega_gpu_platform_aien(&caps), &caps)` before the first session open; the hosted session still installs the Linux reference by default. `CUDAPP_AIEN_GPU_DEV` names another device node. `make test-gpu-platform-aien` runs `tests/test_omega_gpu_platform_aien.c` against a simulated `/dev/aien-gpu` and a simulated GPU that executes the semaphore releases from the ring, plus six mutants that must fail it. |