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/nvrm/nvrm_policy.h # 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 / nvrm / nvrm_policy.h` 225 lines · 8702 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * nvrm_policy.h -- decision policy for the NVRM client. 4 * 5 * Omega owns each decision (native/nvrm/policy.om, unit nvrm_policy). The raw 6 * ioctl call sites stay in nvrm.c. The C oracle below is the original formula, 7 * compiled only for the differential and when CUDAPP_MIGRATION_PENDING_NVRM_POLICY 8 * is set; production defaults to the Omega wrapper. 9 * 10 * Opcode map: 1 nrstat, 2 hnext, 3 vunits, 4 vcover, 5 palign, 6 pspan, 11 * 7 pfull, 8 chadm, 9 retry, 10 mthd, 11 gpentry. 12 */ 13 #ifndef NVRM_POLICY_H 14 #define NVRM_POLICY_H 15 16 #include 17 18 #include "cudapp_native.h" 19 20 #define NVRM_POL_NRSTAT 1u 21 #define NVRM_POL_HNEXT 2u 22 #define NVRM_POL_VUNITS 3u 23 #define NVRM_POL_VCOVER 4u 24 #define NVRM_POL_PALIGN 5u 25 #define NVRM_POL_PSPAN 6u 26 #define NVRM_POL_PFULL 7u 27 #define NVRM_POL_CHADM 8u 28 #define NVRM_POL_RETRY 9u 29 #define NVRM_POL_MTHD 10u 30 #define NVRM_POL_GPENTRY 11u 31 32 #ifndef CUDAPP_MIGRATION_PENDING_NVRM_POLICY 33 #define CUDAPP_MIGRATION_PENDING_NVRM_POLICY 0 34 #endif 35 36 /* A result of 0 is a real answer. Bridge failure is the only error path. */ 37 static inline int nvrm_om_call(uint64_t op, const uint64_t in[8], uint64_t *result, uint64_t out[4]) { 38 uint64_t scratch[4] = {0, 0, 0, 0}; 39 uint64_t *dst = out ? out : scratch; 40 return omega_nvrm_policy(result, op, in, 8, dst, 4) == 0; 41 } 42 43 /* --- C oracle: the original formulas, for the differential --- */ 44 45 static inline uint64_t nvrm_c_nrstat(uint64_t st) { 46 if (st == 0) return 0; 47 if (st == 0x51u) return 1; /* NV_ERR_NO_MEMORY */ 48 return 2; 49 } 50 51 static inline uint64_t nvrm_c_hnext(uint64_t cls, uint64_t nxt, uint64_t accepted) { 52 if (accepted == UINT64_MAX) return (1ull << 32); /* refused: accounting exhausted */ 53 if (cls == 0x41u) return 0; /* NV01_ROOT_CLIENT: handle 0, no advance */ 54 if (nxt == 0xffffffffu) return (1ull << 32); /* refused: handle space exhausted */ 55 return (nxt & 0xffffffffu) | (1ull << 33); /* handle in bits 0..31, advance in bit 33 */ 56 } 57 58 static inline uint64_t nvrm_c_vunits(uint64_t b) { 59 uint64_t u = b == 0 ? 1 : b; /* at least one 4 GiB unit */ 60 if (u > UINT64_MAX - 0xffffffffull) return 0; /* overflow: failure */ 61 return (u + 0xffffffffull) & ~0xffffffffull; 62 } 63 64 /* 0 = covered or no slot (no CAS), 1 = CAS needed (out[2]=owned_end, out[3]=want_end), 2 = refuse. */ 65 static inline uint64_t nvrm_c_vcover(uint64_t base, uint64_t vanext, uint64_t endv, uint64_t out[4]) { 66 if (base == 0) return 0; 67 if (endv < base || vanext < base) return 2; 68 uint64_t owned = nvrm_c_vunits(vanext - base); 69 uint64_t want = nvrm_c_vunits(endv - base); 70 if (owned == 0 || want == 0) return 2; 71 if (want <= owned) return 0; 72 if (base > UINT64_MAX - owned || base > UINT64_MAX - want) return 2; 73 out[2] = base + owned; 74 out[3] = base + want; 75 return 1; 76 } 77 78 static inline uint64_t nvrm_c_palign(uint64_t base, uint64_t align) { 79 if (align == 0 || (align & (align - 1)) != 0) return 0; /* not a power of two */ 80 uint64_t m = align - 1; 81 if (base > UINT64_MAX - m) return 0; /* overflow */ 82 return (base + m) & ~m; 83 } 84 85 /* 0 = ok, 1 = bad span, 2 = VA overflow. */ 86 static inline uint64_t nvrm_c_pspan(uint64_t off, uint64_t nwords, uint64_t size, uint64_t va) { 87 if ((off & 3u) != 0) return 1; 88 if (nwords == 0 || nwords > 0x1fffffu) return 1; 89 uint64_t bytes = nwords * 4u; 90 if (off > UINT64_MAX - bytes) return 1; 91 if (off + bytes > size) return 1; 92 if (va > UINT64_MAX - off) return 2; 93 return 0; 94 } 95 96 static inline uint64_t nvrm_c_pfull(uint64_t putv, uint64_t retired, uint64_t entries) { 97 uint32_t d = (uint32_t)putv - (uint32_t)retired; /* modulo 2^32, as in C */ 98 uint32_t e = (uint32_t)entries - 1u; 99 return d >= e ? 1u : 0u; 100 } 101 102 /* 0 = ok, 1 = dirty state, 2 = extra exceeds max. */ 103 static inline uint64_t nvrm_c_chadm(uint64_t faulted, uint64_t chgroup, uint64_t regd, 104 uint64_t nextra, uint64_t extra, uint64_t maxv) { 105 if (faulted != 0 || chgroup != 0 || regd != 0 || nextra != 0) return 1; 106 if (extra > maxv) return 2; 107 return 0; 108 } 109 110 /* 0 = success, 1 = retry, 2 = budget exhausted, 3 = non-memory error. */ 111 static inline uint64_t nvrm_c_retry(uint64_t failed, uint64_t tries, uint64_t budget, uint64_t nomem) { 112 if (failed == 0) return 0; 113 if (nomem == 0) return 3; 114 if (tries >= budget) return 2; 115 return 1; 116 } 117 118 static inline uint64_t nvrm_c_mthd(uint64_t subc, uint64_t mthd, uint64_t count) { 119 return ((2ull << 28) | ((count & 0xffffffffu) << 16) | ((subc & 0xffffffffu) << 13) | 120 ((mthd & 0xffffffffu) >> 2)) & 121 0xffffffffull; 122 } 123 124 static inline uint64_t nvrm_c_gpentry(uint64_t va, uint64_t nwords) { 125 return (va & ~3ull) | ((nwords & 0x1fffffu) << 42) | (1ull << 41); 126 } 127 128 /* --- Omega wrappers --- */ 129 130 static inline uint64_t nvrm_om_nrstat(uint64_t st) { 131 uint64_t in[8] = {st, 0, 0, 0, 0, 0, 0, 0}, result = 0; 132 if (!nvrm_om_call(NVRM_POL_NRSTAT, in, &result, 0)) return 2; 133 return result; 134 } 135 136 static inline uint64_t nvrm_om_hnext(uint64_t cls, uint64_t nxt, uint64_t accepted) { 137 uint64_t in[8] = {cls, nxt, accepted, 0, 0, 0, 0, 0}, result = 0; 138 if (!nvrm_om_call(NVRM_POL_HNEXT, in, &result, 0)) return (1ull << 32); 139 return result; 140 } 141 142 static inline uint64_t nvrm_om_vunits(uint64_t b) { 143 uint64_t in[8] = {b, 0, 0, 0, 0, 0, 0, 0}, result = 0; 144 if (!nvrm_om_call(NVRM_POL_VUNITS, in, &result, 0)) return 0; 145 return result; 146 } 147 148 static inline uint64_t nvrm_om_vcover(uint64_t base, uint64_t vanext, uint64_t endv, uint64_t out[4]) { 149 uint64_t in[8] = {base, vanext, endv, 0, 0, 0, 0, 0}, result = 0; 150 if (!nvrm_om_call(NVRM_POL_VCOVER, in, &result, out)) return 2; 151 return result; 152 } 153 154 static inline uint64_t nvrm_om_palign(uint64_t base, uint64_t align) { 155 uint64_t in[8] = {base, align, 0, 0, 0, 0, 0, 0}, result = 0; 156 if (!nvrm_om_call(NVRM_POL_PALIGN, in, &result, 0)) return 0; 157 return result; 158 } 159 160 static inline uint64_t nvrm_om_pspan(uint64_t off, uint64_t nwords, uint64_t size, uint64_t va) { 161 uint64_t in[8] = {off, nwords, size, va, 0, 0, 0, 0}, result = 0; 162 if (!nvrm_om_call(NVRM_POL_PSPAN, in, &result, 0)) return 1; 163 return result; 164 } 165 166 static inline uint64_t nvrm_om_pfull(uint64_t putv, uint64_t retired, uint64_t entries) { 167 uint64_t in[8] = {putv, retired, entries, 0, 0, 0, 0, 0}, result = 0; 168 if (!nvrm_om_call(NVRM_POL_PFULL, in, &result, 0)) return 1; 169 return result; 170 } 171 172 static inline uint64_t nvrm_om_chadm(uint64_t faulted, uint64_t chgroup, uint64_t regd, 173 uint64_t nextra, uint64_t extra, uint64_t maxv) { 174 uint64_t in[8] = {faulted, chgroup, regd, nextra, extra, maxv, 0, 0}, result = 0; 175 if (!nvrm_om_call(NVRM_POL_CHADM, in, &result, 0)) return 1; 176 return result; 177 } 178 179 static inline uint64_t nvrm_om_retry(uint64_t failed, uint64_t tries, uint64_t budget, uint64_t nomem) { 180 uint64_t in[8] = {failed, tries, budget, nomem, 0, 0, 0, 0}, result = 0; 181 if (!nvrm_om_call(NVRM_POL_RETRY, in, &result, 0)) return 3; 182 return result; 183 } 184 185 static inline uint64_t nvrm_om_mthd(uint64_t subc, uint64_t mthd, uint64_t count) { 186 uint64_t in[8] = {subc, mthd, count, 0, 0, 0, 0, 0}, result = 0; 187 if (!nvrm_om_call(NVRM_POL_MTHD, in, &result, 0)) return 0; 188 return result; 189 } 190 191 static inline uint64_t nvrm_om_gpentry(uint64_t va, uint64_t nwords) { 192 uint64_t in[8] = {va, nwords, 0, 0, 0, 0, 0, 0}, result = 0; 193 if (!nvrm_om_call(NVRM_POL_GPENTRY, in, &result, 0)) return 0; 194 return result; 195 } 196 197 /* --- production selection: Omega by default, C oracle behind the macro --- */ 198 199 #if CUDAPP_MIGRATION_PENDING_NVRM_POLICY 200 #define nvrm_policy_nrstat nvrm_c_nrstat 201 #define nvrm_policy_hnext nvrm_c_hnext 202 #define nvrm_policy_vunits nvrm_c_vunits 203 #define nvrm_policy_vcover nvrm_c_vcover 204 #define nvrm_policy_palign nvrm_c_palign 205 #define nvrm_policy_pspan nvrm_c_pspan 206 #define nvrm_policy_pfull nvrm_c_pfull 207 #define nvrm_policy_chadm nvrm_c_chadm 208 #define nvrm_policy_retry nvrm_c_retry 209 #define nvrm_policy_mthd nvrm_c_mthd 210 #define nvrm_policy_gpentry nvrm_c_gpentry 211 #else 212 #define nvrm_policy_nrstat nvrm_om_nrstat 213 #define nvrm_policy_hnext nvrm_om_hnext 214 #define nvrm_policy_vunits nvrm_om_vunits 215 #define nvrm_policy_vcover nvrm_om_vcover 216 #define nvrm_policy_palign nvrm_om_palign 217 #define nvrm_policy_pspan nvrm_om_pspan 218 #define nvrm_policy_pfull nvrm_om_pfull 219 #define nvrm_policy_chadm nvrm_om_chadm 220 #define nvrm_policy_retry nvrm_om_retry 221 #define nvrm_policy_mthd nvrm_om_mthd 222 #define nvrm_policy_gpentry nvrm_om_gpentry 223 #endif 224 225 #endif /* NVRM_POLICY_H */