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/CUDApp/blob/main/tests/test_ampere_runtime.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / test_ampere_runtime.c` 239 lines · 11793 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Simulated RM/UVM boundary, not device evidence. Never opens a driver node. 3 * Compile nvrm.c into this TU to exercise its real allocation state machine. */ 4 #ifndef _GNU_SOURCE 5 #define _GNU_SOURCE 6 #endif 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 static int sim_ioctl(int, unsigned long, ...); 17 static int sim_open(const char *, int, ...); 18 static int sim_close(int); 19 static void *sim_mmap(void *, size_t, int, int, int, off_t); 20 #define ioctl sim_ioctl 21 #define open sim_open 22 #define close sim_close 23 #define mmap sim_mmap 24 #include "../nvrm/nvrm.c" 25 #undef ioctl 26 #undef open 27 #undef close 28 #undef mmap 29 static unsigned fail_stage, allocs, frees, dma_maps, dma_unmaps, ranges, range_frees, maps; 30 static int sim_open(const char *path, int flags, ...) { 31 (void)flags; 32 assert(!strncmp(path, "/dev/nvidia", 11)); 33 return 700; /* fake descriptor, not an OS/driver open */ 34 } 35 static int sim_close(int fd) { assert(fd == 700); return 0; } 36 static void *sim_mmap(void *va, size_t size, int prot, int flags, int fd, off_t offset) { 37 if (fd == 700) return mmap(va, size, prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); 38 return mmap(va, size, prot, flags, fd, offset); 39 } 40 static int sim_ioctl(int fd, unsigned long request, ...) { 41 (void)fd; 42 va_list ap; va_start(ap, request); void *arg = va_arg(ap, void *); va_end(ap); 43 if (request == UVM_CREATE_EXTERNAL_RANGE) { 44 UVM_CREATE_EXTERNAL_RANGE_PARAMS *p = arg; 45 if (fail_stage == 3) p->rmStatus = NV_ERR_NO_MEMORY; 46 else ranges++; 47 return 0; 48 } 49 if (request == UVM_MAP_EXTERNAL_ALLOCATION) { 50 UVM_MAP_EXTERNAL_ALLOCATION_PARAMS *p = arg; 51 if (fail_stage == 5) p->rmStatus = NV_ERR_NO_MEMORY; 52 return 0; 53 } 54 if (request == UVM_FREE) { range_frees++; return 0; } 55 assert(_IOC_TYPE(request) == NV_IOCTL_MAGIC); 56 switch (_IOC_NR(request)) { 57 case NV_ESC_RM_ALLOC: { 58 NVOS21_PARAMETERS *p = arg; 59 NV_MEMORY_ALLOCATION_PARAMS *mp = (void *)(uintptr_t)p->pAllocParms; 60 assert(p->hClass == NV01_MEMORY_LOCAL_USER); 61 assert(((mp->attr >> 25) & 3u) == NVOS32_ATTR_LOCATION_VIDMEM); 62 assert(mp->size == 4096); 63 allocs++; 64 if (fail_stage == 1) p->status = NV_ERR_NO_MEMORY; 65 return 0; 66 } 67 case NV_ESC_REGISTER_FD: return 0; 68 case NV_ESC_RM_MAP_MEMORY: { 69 nv_ioctl_nvos33_parameters_with_fd *p = arg; 70 maps++; 71 assert(p->fd == 700 && p->params.length == 4096); 72 if (fail_stage == 2) p->params.status = NV_ERR_NO_MEMORY; 73 return 0; 74 } 75 case NV_ESC_RM_MAP_MEMORY_DMA: { 76 NVOS46_PARAMETERS *p = arg; 77 if (fail_stage == 4) p->status = NV_ERR_NO_MEMORY; 78 else dma_maps++; 79 return 0; 80 } 81 case NV_ESC_RM_UNMAP_MEMORY_DMA: dma_unmaps++; return 0; 82 case NV_ESC_RM_FREE: frees++; return 0; 83 case NV_ESC_RM_VID_HEAP_CONTROL: { 84 NVOS32_PARAMETERS *p = arg; 85 assert(p->function == NVOS32_FUNCTION_INFO); 86 assert(p->data.Info.attr == (NVOS32_ATTR_LOCATION_VIDMEM << 25)); 87 p->total = 8ull << 30; p->free = 4096; 88 return 0; 89 } 90 default: assert(!"unexpected simulated driver operation"); return -1; 91 } 92 } 93 static void fresh(Nvrm *rm) { 94 memset(rm, 0, sizeof *rm); 95 rm->target_sm = 86; rm->discrete = 1; 96 rm->root = 1; rm->device = 2; rm->virtmem = 3; rm->next_handle = 100; 97 rm->fd_ctl = rm->fd_uvm = 700; 98 rm->va_next = rm->va_slot_base = acquire_va_slot(); 99 allocs = frees = dma_maps = dma_unmaps = ranges = range_frees = maps = 0; 100 } 101 static void class_and_bdf(void) { 102 static Nvrm rm; 103 const uint32_t amp[] = {OMEGA_AMPERE_COMPUTE_B, OMEGA_AMPERE_GPFIFO_A, VOLTA_USERMODE_A}; 104 const uint32_t bw[] = {BLACKWELL_COMPUTE_B, BLACKWELL_CHANNEL_GPFIFO_A, HOPPER_USERMODE_A}; 105 rm.sm_version = NV2080_CTRL_GR_INFO_SM_VERSION_8_06; 106 assert(select_classes(&rm, amp, 3, 0, 0) == 0 && rm.target_sm == 86 && rm.discrete); 107 assert(rm.compute_class == OMEGA_AMPERE_COMPUTE_B && rm.gpfifo_class == OMEGA_AMPERE_GPFIFO_A); 108 assert(select_classes(&rm, amp, 2, 0, 0) != 0); /* missing usermode: no stale class reuse */ 109 assert(select_classes(&rm, bw, 3, 0, 0) != 0); /* a Blackwell class set cannot authorize SM86 */ 110 rm.sm_version = NV2080_CTRL_GR_INFO_SM_VERSION_12_01; 111 assert(select_classes(&rm, bw, 3, 0, 0) == 0 && rm.target_sm == 121 && !rm.discrete); 112 /* Physical Spark reported raw RM SM 10.04 with PCI 10de:2e12. Its 113 * execution target is SM121. Do not normalize an unrelated device. */ 114 rm.sm_version = NV2080_CTRL_GR_INFO_SM_VERSION_10_04; 115 assert(select_classes(&rm, bw, 3, 0x10de, 0x2e12) == 0 && rm.target_sm == 121 && !rm.discrete); 116 assert(select_classes(&rm, bw, 3, 0x10de, 0x2e13) != 0); 117 assert(select_classes(&rm, bw, 3, 0x10df, 0x2e12) != 0); 118 assert(select_classes(&rm, bw, 2, 0x10de, 0x2e12) != 0); 119 assert(select_classes(&rm, amp, 3, 0x10de, 0x2e12) != 0); 120 rm.sm_version = NV2080_CTRL_GR_INFO_SM_VERSION_10_03; 121 assert(select_classes(&rm, bw, 3, 0x10de, 0x2e12) != 0); 122 rm.sm_version = NV2080_CTRL_GR_INFO_SM_VERSION_8_09; 123 assert(select_classes(&rm, amp, 3, 0, 0) != 0); /* Ada is not silently treated as GA10x */ 124 nv_ioctl_card_info_t cards[64] = {0}; 125 cards[0].valid = cards[1].valid = 1; 126 cards[1].pci_info.bus = 1; cards[1].pci_info.slot = 2; 127 assert(card_index(cards, NULL) == 0); 128 assert(card_index(cards, "0000:01:02.0") == 1); 129 assert(card_index(cards, "0000:00:00.0") == 0); 130 assert(card_index(cards, "0:1:2.0") == -1); 131 assert(card_index(cards, "0000:01:02.0junk") == -1); 132 assert(card_index(cards, "0000:01:03.0") == -1); 133 } 134 static void allocation_lifetime(void) { 135 static Nvrm rm; 136 fresh(&rm); fail_stage = 0; 137 NvrmMem mem = {0}; 138 assert(nvrm_alloc_vram(&rm, 1, &mem) == 0); 139 assert(mem.size == 4096 && rm.live_count == 1 && allocs == 1 && maps == 1); 140 const uint8_t in[] = {1, 0, 255, 3}; uint8_t out[sizeof in] = {0}; 141 assert(nvrm_copy_to(&rm, &mem, 7, in, sizeof in) == 0); 142 assert(nvrm_copy_from(&rm, &mem, 7, out, sizeof out) == 0); 143 assert(!memcmp(in, out, sizeof in)); 144 assert(nvrm_copy_to(&rm, &mem, 4095, in, sizeof in) != 0); 145 NvrmMem forged = mem; forged.handle++; 146 assert(nvrm_copy_from(&rm, &forged, 0, out, sizeof out) != 0); 147 uint64_t free_bytes, total; 148 assert(nvrm_vram_info(&rm, &free_bytes, &total) == 0 && free_bytes == 4096 && total == (8ull << 30)); 149 NvrmMem stale = mem; 150 rm.inject_rm_free_failure = 1; 151 assert(nvrm_free(&rm, &mem) != 0 && rm.live_count == 1 && rm.live[0].quarantined); 152 assert(nvrm_copy_to(&rm, &mem, 0, in, sizeof in) != 0); 153 assert(nvrm_free(&rm, &mem) == 0 && rm.live_count == 0); 154 assert(nvrm_free(&rm, &mem) == 0); /* idempotent */ 155 assert(frees == 1 && dma_unmaps == 1 && range_frees == 1); 156 assert(nvrm_free(&rm, &stale) != 0 && frees == 1); 157 } 158 static void failed_allocation_cleanup(void) { 159 static Nvrm rm; 160 for (fail_stage = 1; fail_stage <= 5; fail_stage++) { 161 fresh(&rm); 162 NvrmMem mem = {0}; 163 assert(nvrm_alloc_vram(&rm, 4096, &mem) != 0); 164 assert(!mem.handle && rm.live_count == 0 && !rm.faulted); 165 assert(allocs == 1 && frees == (fail_stage == 1 ? 0u : 1u)); 166 assert(dma_unmaps == dma_maps && range_frees == ranges); 167 } 168 fail_stage = 0; 169 } 170 static void qmd_and_submission_admission(void) { 171 uint32_t q[64]; 172 assert(omega_ampere_build_qmd(q, 0x100000, 0x200000, 0x300000, 32, 1, 2, 1, 64, 0) == 0); 173 /* Vendor clc7c0qmd.h QMDV03_00: major[583:580], minor[579:576], 174 * CB0[1055:1024], program[1567:1536], release0 payload[863:832]. */ 175 assert(((q[18] >> 4) & 15) == 3 && (q[18] & 15) == 0); 176 assert(q[32] == 0x200000 && q[48] == 0x100000 && q[26] == 1); 177 assert(omega_ampere_build_qmd(q, 0x100001, 0x200000, 0x300000, 32, 1, 2, 1, 64, 0) != 0); 178 assert(omega_ampere_build_qmd(q, 0x100000, 0x200000, 0x300000, 1024, 2, 2, 1, 64, 0) != 0); 179 assert(omega_ampere_build_qmd(q, 0x100000, 0x200000, 0x300000, 32, 1, 1, 1, 64, 1) == 0); 180 assert((q[17] & 0x3ffffu) == 256); /* shared memory is rounded to vendor granularity */ 181 assert(omega_ampere_build_qmd(q, 0x100000, 0x200000, 0x300000, 32, 1, 1, 1, 64, 99u << 10) == 0); 182 assert(omega_ampere_build_qmd(q, 0x100000, 0x200000, 0x300000, 32, 1, 1, 1, 64, 100u << 10) != 0); 183 assert(omega_ampere_build_qmd(q, 0x100000, 0x200000, 0x300001, 32, 1, 1, 1, 64, 0) != 0); 184 /* Same generated inputs as tests/amp_qmd_test.om. The AMP_QMD_DIFF build 185 * compares this real ABI caller against the retained C wrapper, byte for byte. */ 186 for (uint64_t n = 0; n < 4096; n++) { 187 uint64_t m[16] = {3, (n + 1) * 128, n * 16, n * 16, 188 n % 1024 + 1, 1, n + 1, n % 65535 + 1, n % 255 + 1, n * 127 % 102400}; 189 if (n % 8 == 0) m[1]++; 190 if (n % 8 == 1) m[3]++; 191 if (n % 8 == 2) m[5] = 2; 192 if (n % 8 == 3) m[9] = 102400; 193 int rc = omega_ampere_build_qmd(q, m[1], m[2], m[3], (uint32_t)m[4], 194 (uint32_t)m[5], (uint32_t)m[6], (uint32_t)m[7], (uint32_t)m[8], (uint32_t)m[9]); 195 assert(omega_ampere_policy(m, NULL) == rc); 196 } 197 puts("PASS ampere ABI parity inputs: 4096 build + 4096 policy states"); 198 static Nvrm rm; fresh(&rm); 199 uint32_t good[] = {nvrm_mthd(1, 0, 1), OMEGA_AMPERE_COMPUTE_B, 200 nvrm_mthd(1, 0x021c, 1), (1u << 0) | (1u << 4) | (1u << 12), 201 nvrm_mthd(1, 0x02b4, 1), 0x1000, nvrm_mthd(1, 0x02c0, 1), 3}; 202 NvrmMem pb = {.cpu = good, .size = sizeof good}; 203 assert(validate_ampere_push(&rm, &pb, 0, sizeof good / sizeof good[0]) == 0); 204 good[3] = 0; /* The supported cache method does not admit arbitrary values. */ 205 assert(validate_ampere_push(&rm, &pb, 0, sizeof good / sizeof good[0]) != 0); 206 good[3] = (1u << 0) | (1u << 4) | (1u << 12); 207 good[1] = BLACKWELL_COMPUTE_B; 208 assert(validate_ampere_push(&rm, &pb, 0, sizeof good / sizeof good[0]) != 0); 209 good[0] = 0x206220c6; /* existing Blackwell inline QMD5 packet */ 210 assert(validate_ampere_push(&rm, &pb, 0, sizeof good / sizeof good[0]) != 0); 211 assert(allocs == 0 && frees == 0); /* admission itself makes no ioctl */ 212 } 213 /* The compute-object retry loop's own call into the policy unit. The C/Omega 214 * policy differential cannot see a call site that hands both units the same 215 * misordered arguments; this checks the decisions nvrm_channel_n acts on. */ 216 static void channel_retry_call_site(void) { 217 static Nvrm rm; fresh(&rm); 218 rm.channel_no_memory_retries = 0; rm.last_rm_status = NV_OK; 219 assert(channel_retry_decision(&rm, 0, NVRM_CHANNEL_NO_MEMORY_RETRIES) == 0); 220 assert(channel_retry_decision(&rm, 0, 0) == 0); 221 rm.last_rm_status = NV_ERR_NO_MEMORY; /* a stale status never overrides success */ 222 assert(channel_retry_decision(&rm, 0, 3) == 0); 223 assert(channel_retry_decision(&rm, -1, 3) == 1); 224 rm.channel_no_memory_retries = 2; 225 assert(channel_retry_decision(&rm, -1, 3) == 1); 226 rm.channel_no_memory_retries = 3; 227 assert(channel_retry_decision(&rm, -1, 3) == 2); 228 rm.channel_no_memory_retries = 0; 229 assert(channel_retry_decision(&rm, -1, 0) == 2); 230 rm.last_rm_status = NV_ERR_INVALID_ARGUMENT; 231 assert(channel_retry_decision(&rm, -1, 3) == 3); 232 assert(allocs == 0 && frees == 0); /* the decision itself makes no ioctl */ 233 } 234 int main(void) { 235 class_and_bdf(); allocation_lifetime(); failed_allocation_cleanup(); qmd_and_submission_admission(); 236 channel_retry_call_site(); 237 puts("Ampere simulated RM allocation/class/QMD tests passed (HOST_ONLY; device NOT_RUN)"); 238 return 0; 239 }