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
CUDApp / src / omega_gpu_chip.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_chip.c: the supported-target table, the doctor refusal sentence (developer bar D6), and the |
| 3 | * library admission glue around the Omega decision om/admission/chip_admit.om (D2, D6). See omega_gpu_chip.h. */ |
| 4 | #include "omega_gpu_chip.h" |
| 5 | |
| 6 | #include "cudapp_native.h" |
| 7 | #include "omega_chip_enum.h" |
| 8 | |
| 9 | #include <dirent.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | /* Target identities are unchanged. The GB10 row points to an imported historical summary; |
| 15 | * its raw M16 logs are unavailable here, so it is not current-commit qualification. |
| 16 | * Add a row only with durable raw evidence and a receipt; see README evidence limitation. */ |
| 17 | static const OmegaGpuChipRow k_rows[] = { |
| 18 | { OMEGA_GPU_PCI_VENDOR_NVIDIA, 0x2e12u, "NVIDIA GB10", "sm_121", |
| 19 | "evidence/m16-blackwell-native-path-requalification-receipt.json" }, |
| 20 | }; |
| 21 | |
| 22 | const OmegaGpuChipRow *omega_gpu_chip_find(uint16_t vendor, uint16_t device) { |
| 23 | for (size_t i = 0; i < sizeof k_rows / sizeof k_rows[0]; i++) |
| 24 | if (omega_chip_enum_decide(0, vendor, device, k_rows[i].vendor, k_rows[i].device)) return &k_rows[i]; |
| 25 | return NULL; |
| 26 | } |
| 27 | |
| 28 | static int read_hex(const char *path, unsigned long *out) { |
| 29 | FILE *f = fopen(path, "r"); |
| 30 | if (!f) return -1; |
| 31 | char line[64] = { 0 }; |
| 32 | int ok = fgets(line, sizeof line, f) != NULL; |
| 33 | fclose(f); |
| 34 | if (!ok) return -1; |
| 35 | char *end = NULL; |
| 36 | *out = strtoul(line, &end, 16); |
| 37 | return end == line ? -1 : 0; |
| 38 | } |
| 39 | |
| 40 | int omega_gpu_chip_scan(const char *root, OmegaGpuPciDev *out, int max) { |
| 41 | char dir[4096]; |
| 42 | snprintf(dir, sizeof dir, "%s/sys/bus/pci/devices", root ? root : ""); |
| 43 | DIR *d = opendir(dir); |
| 44 | if (!d) return -1; |
| 45 | int found = 0; |
| 46 | struct dirent *e; |
| 47 | while ((e = readdir(d)) != NULL) { |
| 48 | if (omega_chip_enum_decide(5, (unsigned char)e->d_name[0], 0, 0, 0)) continue; |
| 49 | char p[4400]; |
| 50 | unsigned long vendor, device, cls; |
| 51 | snprintf(p, sizeof p, "%s/%s/vendor", dir, e->d_name); |
| 52 | if (read_hex(p, &vendor) != 0 || !omega_chip_enum_decide(1, vendor, 0, 0, 0)) continue; |
| 53 | snprintf(p, sizeof p, "%s/%s/class", dir, e->d_name); |
| 54 | if (read_hex(p, &cls) != 0 || !omega_chip_enum_decide(2, cls, 0, 0, 0)) continue; /* display/3D only */ |
| 55 | snprintf(p, sizeof p, "%s/%s/device", dir, e->d_name); |
| 56 | if (read_hex(p, &device) != 0) continue; |
| 57 | if (omega_chip_enum_decide(4, found, max, out != NULL, 0)) { |
| 58 | OmegaGpuPciDev *g = &out[found]; |
| 59 | memset(g, 0, sizeof *g); |
| 60 | snprintf(g->bdf, sizeof g->bdf, "%.63s", e->d_name); |
| 61 | g->vendor = (uint16_t)vendor; g->device = (uint16_t)device; g->class_code = (uint32_t)cls; |
| 62 | } |
| 63 | found++; |
| 64 | } |
| 65 | closedir(d); |
| 66 | return found; |
| 67 | } |
| 68 | |
| 69 | int omega_gpu_chip_refusal(const OmegaGpuPciDev *d, char *buf, size_t n) { |
| 70 | return snprintf(buf, n, |
| 71 | "GPU %s: NVIDIA device 0x%04x is not one CUDA++ has qualified.\n" |
| 72 | "Why: CUDA++ writes each chip's machine code itself and has run it only on the NVIDIA GB10 (sm_121),\n" |
| 73 | "so it refuses this GPU now instead of failing later with \"no kernel image is available\".\n" |
| 74 | "What is missing: a target row for this chip, a check that the encoder's instruction words are valid on it,\n" |
| 75 | "its launch descriptor and class numbers, and the hello-gpu qualification run with its receipt.\n" |
| 76 | "What to do: follow docs/runtime/PORTABILITY.md section 5 (\"What a new GPU needs\") and CONTRIBUTING.md (\"Port a chip\"),\n" |
| 77 | "or report it with the output of `make doctor` attached.", |
| 78 | d->bdf, d->device); |
| 79 | } |
| 80 | |
| 81 | static const char *g_root; /* NULL: the real /sys */ |
| 82 | |
| 83 | void omega_gpu_chip_set_root(const char *root) { g_root = root; } |
| 84 | |
| 85 | int omega_gpu_chip_admit(const OmegaGpuPciDev *devs, int n, const char *bdf, OmegaGpuChipAdmission *out) { |
| 86 | uint64_t d[4 * OMEGA_GPU_ADMIT_MAX] = { 0 }, o[4] = { 0 }, status = 0; |
| 87 | memset(out, 0, sizeof *out); |
| 88 | if (n < 0 || (n > 0 && !devs)) return out->status = OMEGA_GPU_ADMIT_BAD_INPUT; |
| 89 | /* More than 16 functions reaches the unit as n > 16, which it refuses as bad input. */ |
| 90 | for (int i = 0; i < n && i < OMEGA_GPU_ADMIT_MAX; i++) { |
| 91 | d[4 * i] = devs[i].vendor; |
| 92 | d[4 * i + 1] = devs[i].device; |
| 93 | d[4 * i + 2] = devs[i].class_code; |
| 94 | d[4 * i + 3] = bdf && strcmp(devs[i].bdf, bdf) == 0; |
| 95 | } |
| 96 | if (omega_chip_admit(&status, d, 4 * OMEGA_GPU_ADMIT_MAX, (uint64_t)n, bdf != NULL, o, 4) != OMEGA_OK) |
| 97 | return out->status = OMEGA_GPU_ADMIT_UNIT; |
| 98 | out->status = (int)status; |
| 99 | out->index = (int)o[1]; |
| 100 | out->target_sm = (uint32_t)o[2]; |
| 101 | out->device = (uint16_t)o[3]; |
| 102 | return out->status; |
| 103 | } |
| 104 | |
| 105 | #define NO_NODE "the library opened no /dev/nvidia node" |
| 106 | |
| 107 | static int sentence(const OmegaGpuChipAdmission *a, const OmegaGpuPciDev *dev, const char *bdf, const char *root, |
| 108 | char *why, size_t n) { |
| 109 | switch (a->status) { |
| 110 | case OMEGA_GPU_ADMIT_NO_SYSFS: |
| 111 | snprintf(why, n, "cannot read %.120s/sys/bus/pci/devices to name the GPU, so " NO_NODE ". Run `make doctor`.", |
| 112 | root ? root : ""); |
| 113 | break; |
| 114 | case OMEGA_GPU_ADMIT_NO_GPU: |
| 115 | snprintf(why, n, "no NVIDIA GPU under /sys/bus/pci/devices, so " NO_NODE ". Run `make doctor`."); |
| 116 | break; |
| 117 | case OMEGA_GPU_ADMIT_NO_BDF: |
| 118 | snprintf(why, n, "CUDAPP_GPU_BDF=%.32s names no NVIDIA GPU under /sys/bus/pci/devices, so " NO_NODE |
| 119 | ". Run `make doctor` for the addresses.", bdf ? bdf : ""); |
| 120 | break; |
| 121 | case OMEGA_GPU_ADMIT_REFUSED: |
| 122 | snprintf(why, n, "GPU %.32s: NVIDIA device 0x%04x is not one CUDA++ has qualified, so " NO_NODE |
| 123 | ". Run `make doctor` for why, what is missing and what to do.", dev ? dev->bdf : "?", a->device); |
| 124 | break; |
| 125 | case OMEGA_GPU_ADMIT_BAD_INPUT: |
| 126 | snprintf(why, n, "GPU admission refused: more than %d NVIDIA GPUs, or CUDAPP_GPU_BDF matched twice, so " NO_NODE |
| 127 | ".", OMEGA_GPU_ADMIT_MAX); |
| 128 | break; |
| 129 | default: |
| 130 | snprintf(why, n, "GPU admission unit om/admission/chip_admit.om failed, so " NO_NODE "."); |
| 131 | break; |
| 132 | } |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | int omega_gpu_chip_admit_open(const char *bdf, char *why, size_t n) { |
| 137 | OmegaGpuPciDev devs[OMEGA_GPU_ADMIT_MAX]; |
| 138 | OmegaGpuChipAdmission a = { 0 }; |
| 139 | int found = omega_gpu_chip_scan(g_root, devs, OMEGA_GPU_ADMIT_MAX); |
| 140 | if (found < 0) a.status = OMEGA_GPU_ADMIT_NO_SYSFS; |
| 141 | else if (omega_gpu_chip_admit(devs, found, bdf, &a) == OMEGA_GPU_ADMIT_OK) return 0; |
| 142 | const OmegaGpuPciDev *dev = found > 0 && a.index < found && a.index < OMEGA_GPU_ADMIT_MAX ? &devs[a.index] : NULL; |
| 143 | return sentence(&a, dev, bdf, g_root, why, n); |
| 144 | } |
| 145 | |
| 146 | int omega_gpu_chip_admit_card(uint32_t domain, uint32_t bus, uint32_t slot, uint32_t function, uint16_t vendor, |
| 147 | uint16_t device, uint32_t *target_sm, char *why, size_t n) { |
| 148 | /* NV_ESC_CARD_INFO lists GPUs only, so the card is given the display class 0x030000. */ |
| 149 | OmegaGpuPciDev card = { .vendor = vendor, .device = device, .class_code = 0x030000u }; |
| 150 | snprintf(card.bdf, sizeof card.bdf, "%04x:%02x:%02x.%x", |
| 151 | (unsigned)omega_chip_enum_decide(3, domain, 0xffffu, 0, 0), |
| 152 | (unsigned)omega_chip_enum_decide(3, bus, 0xffu, 0, 0), |
| 153 | (unsigned)omega_chip_enum_decide(3, slot, 0x1fu, 0, 0), |
| 154 | (unsigned)omega_chip_enum_decide(3, function, 7u, 0, 0)); |
| 155 | OmegaGpuChipAdmission a = { 0 }; |
| 156 | *target_sm = 0; |
| 157 | if (omega_gpu_chip_admit(&card, 1, card.bdf, &a) == OMEGA_GPU_ADMIT_OK) { |
| 158 | *target_sm = a.target_sm; |
| 159 | return 0; |
| 160 | } |
| 161 | if (a.status == OMEGA_GPU_ADMIT_NO_BDF) { /* the driver named a non-NVIDIA vendor */ |
| 162 | snprintf(why, n, "GPU %s: the driver reports vendor 0x%04x device 0x%04x, not an NVIDIA GPU CUDA++ has qualified;" |
| 163 | " refused before opening the device node.", card.bdf, vendor, device); |
| 164 | return -1; |
| 165 | } |
| 166 | if (a.status == OMEGA_GPU_ADMIT_REFUSED) { |
| 167 | snprintf(why, n, "GPU %s: the driver reports NVIDIA device 0x%04x, which is not one CUDA++ has qualified;" |
| 168 | " refused before opening the device node. Run `make doctor`.", card.bdf, device); |
| 169 | return -1; |
| 170 | } |
| 171 | return sentence(&a, &card, card.bdf, NULL, why, n); |
| 172 | } |