Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/linux/aien-gpu/aien_gpu.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / linux / aien-gpu / aien_gpu.c` 380 lines ยท 13958 bytes Raw Blame History 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Linux host shim only: a module that uses Linux GPL-only symbols must be GPLv2. The driver core in native/kernel/gpu stays AGPL. */ 3 /* 4 * aien-gpu: Linux host shim for our own GB10 GPU driver (L332 rung 2, windows 0 and 1). 5 * 6 * What it does, and nothing more: 7 * - binds the GB10 (10de:2e12) ONLY when loaded with allow_bind=1 (nvidia.ko must be unloaded first: 8 * two drivers on one GPU is the one thing that must never happen); 9 * - maps BAR0 and READS two identity registers (NV_PMC_BOOT_0 at 0x0, NV_PMC_BOOT_42 at 0xA00): no write; 10 * - loads the GSP firmware file on loan from NVIDIA (nvidia/580.173.02/gsp_ga10x.bin) and checks its size; 11 * it does not send it to the GPU yet; 12 * - allocates one DMA-coherent arena (arena_mib, default 2) and offers it to user space by mmap of /dev/aien-gpu, 13 * the shape of the memory interface Omega gets (one big mapping); 14 * - AIEN_GPU_INFO ioctl returns what it read; 15 * - window 1: a second DMA-coherent buffer for the GSP sysmem heap (heap_mib, default 0, never mapped to user space) 16 * and AIEN_GPU_REG_RD / AIEN_GPU_REG_WR for exactly the registers of the GSP start-up contract 17 * (gpu/contract/gb10_gsp.devc). Writes need a second switch, allow_boot=1, and CAP_SYS_RAWIO. The SEC2 18 * doorbell (QUEUE_HEAD(0)) can be rung once per load: a failed start-up is never retried without a reset. 19 * remove sets gone under the device lock first. After that, register access, INFO and mmap return -ENODEV. 20 * A live arena VMA or an open fd keeps the arena (leak until reboot) instead of freeing it. A probe after 21 * remove (PCI rescan) is refused with -EBUSY while such an fd, VMA or a rung doorbell survives; otherwise it 22 * clears gone and the info. The lock is static, never re-initialised. arena_mib must be 1..SIZE_MAX >> 20. 23 * sysfs unbind is not offered (suppress_bind_attrs). 24 * The start-up logic itself (gpu/gsp_mmio.c, dev/gb10_gsp_boot.c) runs in user space (aien_gsp_boot) over these two 25 * ioctls, so the driver core stays AGPL and outside this GPL module. GB10B has no FSP: SEC2 takes the start-up packet. 26 * What comes next (not here): the RPC subset (docs/numeric/DRIVER_FLOOR.md), the channel from 27 * gpu/contract/gb10_chan.devc, Omega's typed handles (L202). Each is its own window. 28 */ 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 41 #include "aien_gpu_uapi.h" 42 43 #define AIEN_GPU_FW "nvidia/580.173.02/gsp_ga10x.bin" 44 #define AIEN_GPU_FW_BYTES 75012080u /* /lib/firmware/nvidia/580.173.02/gsp_ga10x.bin, L101 driver floor */ 45 #define NV_PMC_BOOT_0 0x00000000u /* ampere/ga100/dev_boot.h:27 */ 46 #define NV_PMC_BOOT_42 0x00000a00u 47 48 #define AIEN_GPU_DMA_BITS 40 /* nvidia.ko logs "Using 40-bit DMA addresses" for this GPU */ 49 50 /* The registers user space may reach (BAR0 offsets). Reads: the GSP start-up contract's registers except the 51 * write-only data port, plus the two identity registers. Writes: the six the start-up and the notify write. 52 * evidence/own-gpu-driver/rung2/window1_sim_test.sh checks these lists against every access the start-up tool 53 * makes on the simulator. 54 * AIEN-GPU-ALLOW-BEGIN */ 55 static const u32 aien_gpu_rd_ok[] = { 56 0x00000000, 0x00000a00, /* PMC_BOOT_0, PMC_BOOT_42 */ 57 0x00840040, 0x00840044, 0x00840ac0, 0x00840c00, 0x00840c04, /* SEC2 MAILBOX0/1, EMEMC0, QHEAD0, QTAIL0 */ 58 0x00110040, 0x00110044, 0x00110080, 0x001100f4, 0x00110c00, 0x00111388, /* GSP MAILBOX0/1, OS, HWCFG2, QHEAD0, CPUCTL */ 59 }; 60 static const u32 aien_gpu_wr_ok[] = { 61 0x00840ac0, 0x00840ac4, 0x00840c00, 0x00840c04, /* SEC2 EMEMC0, EMEMD0, QHEAD0 (doorbell), QTAIL0 */ 62 0x00110080, 0x00110c00, /* GSP FALCON_OS, GSP QHEAD0 (command-queue notify) */ 63 }; 64 /* AIEN-GPU-ALLOW-END */ 65 #define AIEN_GPU_SEC2_DOORBELL 0x00840c00u 66 67 static bool allow_bind; 68 module_param(allow_bind, bool, 0444); 69 MODULE_PARM_DESC(allow_bind, "bind the GB10 (only after nvidia.ko is unloaded; default off)"); 70 static uint arena_mib = 2; 71 module_param(arena_mib, uint, 0444); 72 MODULE_PARM_DESC(arena_mib, "DMA-coherent arena size in MiB (default 2)"); 73 static uint heap_mib; 74 module_param(heap_mib, uint, 0444); 75 MODULE_PARM_DESC(heap_mib, "GSP sysmem heap in MiB, chip only (default 0 = none; window 1 uses 512)"); 76 static bool allow_boot; 77 module_param(allow_boot, bool, 0444); 78 MODULE_PARM_DESC(allow_boot, "allow the GSP start-up register writes (second switch after allow_bind; default off)"); 79 80 static struct { 81 struct pci_dev *pdev; 82 void __iomem *bar0; 83 void *arena; 84 dma_addr_t arena_iova; 85 size_t arena_bytes; 86 void *heap; 87 dma_addr_t heap_iova; 88 size_t heap_bytes; 89 struct aien_gpu_info info; 90 struct mutex lock; /* register access, the doorbell latch, and gone; initialised once, never per probe */ 91 atomic_t open; /* one opener at a time; stays set until the last file ref drops */ 92 atomic_t maps; /* live arena VMAs (mmap counts; vm_ops open/close track splits) */ 93 bool gone; /* set at the start of remove; register, INFO and mmap then return -ENODEV. 94 * A later probe clears it only when no fd, VMA or doorbell survives. */ 95 } g = { .lock = __MUTEX_INITIALIZER(g.lock) }; 96 97 static bool aien_gpu_listed(const u32 *list, size_t n, u32 off) 98 { 99 size_t i; 100 101 for (i = 0; i < n; i++) 102 if (list[i] == off) 103 return true; 104 return false; 105 } 106 107 static long aien_gpu_reg(unsigned int cmd, struct aien_gpu_reg __user *u) 108 { 109 struct aien_gpu_reg r; 110 long rc = 0; 111 112 if (copy_from_user(&r, u, sizeof(r))) 113 return -EFAULT; 114 mutex_lock(&g.lock); 115 if (g.gone || !g.bar0) { 116 rc = -ENODEV; 117 } else if (cmd == AIEN_GPU_REG_RD) { 118 if (!aien_gpu_listed(aien_gpu_rd_ok, ARRAY_SIZE(aien_gpu_rd_ok), r.off)) 119 rc = -EPERM; 120 else 121 r.val = ioread32(g.bar0 + r.off); 122 } else if (!capable(CAP_SYS_RAWIO) || !allow_boot || 123 !aien_gpu_listed(aien_gpu_wr_ok, ARRAY_SIZE(aien_gpu_wr_ok), r.off)) { 124 rc = -EPERM; 125 } else if (r.off == AIEN_GPU_SEC2_DOORBELL && g.info.doorbells) { 126 rc = -EALREADY; /* one start-up attempt per load: never retried without a reset */ 127 } else { 128 iowrite32(r.val, g.bar0 + r.off); 129 if (r.off == AIEN_GPU_SEC2_DOORBELL) { 130 g.info.doorbells++; 131 dev_info(&g.pdev->dev, "aien-gpu: SEC2 doorbell rung (GSP start-up packet handed over)\n"); 132 } 133 } 134 mutex_unlock(&g.lock); 135 if (rc) 136 return rc; 137 return cmd == AIEN_GPU_REG_RD && copy_to_user(u, &r, sizeof(r)) ? -EFAULT : 0; 138 } 139 140 static long aien_gpu_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 141 { 142 switch (cmd) { 143 case AIEN_GPU_INFO: { 144 struct aien_gpu_info info; 145 146 mutex_lock(&g.lock); 147 if (g.gone) { 148 mutex_unlock(&g.lock); 149 return -ENODEV; 150 } 151 info = g.info; 152 mutex_unlock(&g.lock); 153 return copy_to_user((void __user *)arg, &info, sizeof(info)) ? -EFAULT : 0; 154 } 155 case AIEN_GPU_REG_RD: 156 case AIEN_GPU_REG_WR: 157 return aien_gpu_reg(cmd, (struct aien_gpu_reg __user *)arg); 158 } 159 return -ENOTTY; 160 } 161 162 static int aien_gpu_open(struct inode *i, struct file *f) 163 { 164 int rc = 0; 165 166 mutex_lock(&g.lock); 167 if (g.gone) 168 rc = -ENODEV; 169 else if (atomic_cmpxchg(&g.open, 0, 1)) 170 rc = -EBUSY; 171 mutex_unlock(&g.lock); 172 return rc; 173 } 174 175 static int aien_gpu_release(struct inode *i, struct file *f) 176 { 177 mutex_lock(&g.lock); 178 atomic_set(&g.open, 0); 179 mutex_unlock(&g.lock); 180 return 0; 181 } 182 183 /* mmap does not call ->open. Splits do. VM_DONTCOPY keeps fork from copying the arena. */ 184 static void aien_gpu_vma_open(struct vm_area_struct *vma) 185 { 186 atomic_inc(&g.maps); 187 (void)vma; 188 } 189 190 static void aien_gpu_vma_close(struct vm_area_struct *vma) 191 { 192 atomic_dec(&g.maps); 193 (void)vma; 194 } 195 196 static const struct vm_operations_struct aien_gpu_vm_ops = { 197 .open = aien_gpu_vma_open, 198 .close = aien_gpu_vma_close, 199 }; 200 201 static int aien_gpu_mmap(struct file *f, struct vm_area_struct *vma) 202 { 203 size_t len = vma->vm_end - vma->vm_start; 204 int rc; 205 206 mutex_lock(&g.lock); 207 if (g.gone || !g.pdev) { 208 rc = -ENODEV; 209 goto out; 210 } 211 if (!g.arena || vma->vm_pgoff || len > g.arena_bytes) { 212 rc = -EINVAL; 213 goto out; 214 } 215 rc = dma_mmap_coherent(&g.pdev->dev, vma, g.arena, g.arena_iova, len); 216 if (!rc) { 217 vma->vm_ops = &aien_gpu_vm_ops; 218 vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND); 219 atomic_inc(&g.maps); 220 } 221 out: 222 mutex_unlock(&g.lock); 223 return rc; 224 } 225 226 static const struct file_operations aien_gpu_fops = { 227 .owner = THIS_MODULE, 228 .open = aien_gpu_open, 229 .release = aien_gpu_release, 230 .unlocked_ioctl = aien_gpu_ioctl, 231 .mmap = aien_gpu_mmap, 232 }; 233 234 static struct miscdevice aien_gpu_misc = { 235 .minor = MISC_DYNAMIC_MINOR, 236 .name = "aien-gpu", 237 .fops = &aien_gpu_fops, 238 .mode = 0600, 239 }; 240 241 static int aien_gpu_probe(struct pci_dev *pdev, const struct pci_device_id *id) 242 { 243 const struct firmware *fw; 244 int rc; 245 246 if (!allow_bind) { 247 dev_info(&pdev->dev, "aien-gpu: allow_bind=0, not binding\n"); 248 return -ENODEV; 249 } 250 if (!arena_mib || arena_mib > SIZE_MAX >> 20) { 251 dev_err(&pdev->dev, "aien-gpu: arena_mib %u out of range (1..SIZE_MAX >> 20)\n", arena_mib); 252 return -EINVAL; 253 } 254 /* Probe after remove (PCI remove + rescan). An fd or arena VMA from the previous binding, or an 255 * attempted GSP start-up (arena and heap leaked until reboot), keeps the device refused. Otherwise 256 * start from a clean state: the lock is static, so an old release() can never see it re-initialised. */ 257 mutex_lock(&g.lock); 258 if (g.gone && (atomic_read(&g.open) || atomic_read(&g.maps) || g.info.doorbells)) { 259 mutex_unlock(&g.lock); 260 dev_warn(&pdev->dev, "aien-gpu: previous binding still has an fd, a mapping or a GSP start-up; reboot first\n"); 261 return -EBUSY; 262 } 263 g.gone = false; 264 memset(&g.info, 0, sizeof(g.info)); 265 mutex_unlock(&g.lock); 266 rc = pcim_enable_device(pdev); 267 if (rc) 268 return rc; 269 pci_set_master(pdev); 270 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(AIEN_GPU_DMA_BITS)); 271 if (rc) 272 return rc; 273 g.info.dma_bits = AIEN_GPU_DMA_BITS; 274 g.info.allow_boot = allow_boot; 275 g.info.bar0_pa = pci_resource_start(pdev, 0); 276 g.bar0 = pcim_iomap(pdev, 0, 0); 277 if (!g.bar0) 278 return -ENOMEM; 279 g.pdev = pdev; 280 g.info.bar0_bytes = pci_resource_len(pdev, 0); 281 g.info.boot0 = ioread32(g.bar0 + NV_PMC_BOOT_0); 282 g.info.boot42 = ioread32(g.bar0 + NV_PMC_BOOT_42); 283 dev_info(&pdev->dev, "aien-gpu: BAR0 %llu bytes, PMC_BOOT_0 %08x PMC_BOOT_42 %08x\n", 284 g.info.bar0_bytes, g.info.boot0, g.info.boot42); 285 286 rc = request_firmware(&fw, AIEN_GPU_FW, &pdev->dev); 287 if (!rc) { 288 g.info.fw_bytes = fw->size; 289 g.info.fw_ok = fw->size == AIEN_GPU_FW_BYTES; 290 release_firmware(fw); 291 } 292 dev_info(&pdev->dev, "aien-gpu: firmware %s rc %d bytes %llu ok %u\n", AIEN_GPU_FW, rc, g.info.fw_bytes, g.info.fw_ok); 293 294 g.arena_bytes = (size_t)arena_mib << 20; 295 g.arena = dma_alloc_coherent(&pdev->dev, g.arena_bytes, &g.arena_iova, GFP_KERNEL); 296 if (!g.arena) 297 return -ENOMEM; 298 g.info.arena_bytes = g.arena_bytes; 299 g.info.arena_iova = g.arena_iova; 300 g.heap_bytes = (size_t)heap_mib << 20; 301 if (g.heap_bytes) { 302 g.heap = dma_alloc_coherent(&pdev->dev, g.heap_bytes, &g.heap_iova, GFP_KERNEL); 303 if (!g.heap) { 304 rc = -ENOMEM; 305 goto free_arena; 306 } 307 g.info.heap_bytes = g.heap_bytes; 308 g.info.heap_iova = g.heap_iova; 309 } 310 rc = misc_register(&aien_gpu_misc); 311 if (rc) 312 goto free_heap; 313 dev_info(&pdev->dev, "aien-gpu: /dev/aien-gpu ready, arena %zu bytes at iova %pad, heap %zu bytes at iova %pad, allow_boot %d\n", 314 g.arena_bytes, &g.arena_iova, g.heap_bytes, &g.heap_iova, allow_boot); 315 return 0; 316 free_heap: 317 if (g.heap) 318 dma_free_coherent(&pdev->dev, g.heap_bytes, g.heap, g.heap_iova); 319 g.heap = NULL; 320 free_arena: 321 dma_free_coherent(&pdev->dev, g.arena_bytes, g.arena, g.arena_iova); 322 g.arena = NULL; 323 return rc; 324 } 325 326 static void aien_gpu_remove(struct pci_dev *pdev) 327 { 328 bool leak; 329 330 /* gone is published before any free, under the same lock register access, INFO, mmap and open take. 331 * An fd or VMA that is already live cannot start a new register or map access afterwards. */ 332 mutex_lock(&g.lock); 333 g.gone = true; 334 g.bar0 = NULL; 335 leak = g.info.doorbells || atomic_read(&g.open) || atomic_read(&g.maps); 336 mutex_unlock(&g.lock); 337 338 misc_deregister(&aien_gpu_misc); 339 if (leak) { 340 /* Same leak-until-reboot path as a rung doorbell. gone is already set, so no new map or 341 * register access starts. A VMA that was already live still points at the arena pages (the 342 * mapping outlives the fd), and an fd that was already open keeps its reference. Freeing the 343 * pages under either is a use after free. GSP may also still DMA the arena and the heap after 344 * the doorbell. The window-1 runbook reboots afterwards. Hardware behaviour of this path is 345 * unverified: this tree does not load the module. */ 346 if (g.info.doorbells) 347 dev_warn(&pdev->dev, "aien-gpu: GSP start-up was attempted; arena and heap kept until reboot\n"); 348 else 349 dev_warn(&pdev->dev, "aien-gpu: removed while the fd is open or the arena is mapped; arena and heap kept until reboot\n"); 350 g.heap = NULL; 351 g.arena = NULL; 352 } 353 if (g.heap) 354 dma_free_coherent(&pdev->dev, g.heap_bytes, g.heap, g.heap_iova); 355 g.heap = NULL; 356 if (g.arena) 357 dma_free_coherent(&pdev->dev, g.arena_bytes, g.arena, g.arena_iova); 358 g.arena = NULL; 359 g.pdev = NULL; 360 } 361 362 static const struct pci_device_id aien_gpu_ids[] = { 363 { PCI_DEVICE(0x10de, 0x2e12) }, /* GB10, lspci -nn on the Spark 2026-10-21 */ 364 { } 365 }; 366 MODULE_DEVICE_TABLE(pci, aien_gpu_ids); 367 368 static struct pci_driver aien_gpu_driver = { 369 .name = "aien-gpu", 370 .id_table = aien_gpu_ids, 371 .probe = aien_gpu_probe, 372 .remove = aien_gpu_remove, 373 /* sysfs unbind does not take the fd's module reference. No bind/unbind attributes. */ 374 .driver.suppress_bind_attrs = true, 375 }; 376 module_pci_driver(aien_gpu_driver); 377 378 MODULE_DESCRIPTION("AIEN own GB10 GPU driver, Linux host shim (L332)"); 379 MODULE_LICENSE("GPL"); 380 MODULE_FIRMWARE(AIEN_GPU_FW);