1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_X86_CPUID_API_H 3 #define _ASM_X86_CPUID_API_H 4 5 #include <asm/cpuid/types.h> 6 7 #include <linux/build_bug.h> 8 #include <linux/types.h> 9 10 #include <asm/processor.h> 11 #include <asm/string.h> 12 13 /* 14 * Raw CPUID accessors: 15 */ 16 17 #ifdef CONFIG_X86_32 18 bool cpuid_feature(void); 19 #else 20 static inline bool cpuid_feature(void) 21 { 22 return true; 23 } 24 #endif 25 26 static inline void native_cpuid(u32 *eax, u32 *ebx, 27 u32 *ecx, u32 *edx) 28 { 29 /* ecx is often an input as well as an output. */ 30 asm volatile("cpuid" 31 : "=a" (*eax), 32 "=b" (*ebx), 33 "=c" (*ecx), 34 "=d" (*edx) 35 : "0" (*eax), "2" (*ecx) 36 : "memory"); 37 } 38 39 #define NATIVE_CPUID_REG(reg) \ 40 static inline u32 native_cpuid_##reg(u32 op) \ 41 { \ 42 u32 eax = op, ebx, ecx = 0, edx; \ 43 \ 44 native_cpuid(&eax, &ebx, &ecx, &edx); \ 45 \ 46 return reg; \ 47 } 48 49 /* 50 * Native CPUID functions returning a single datum: 51 */ 52 NATIVE_CPUID_REG(eax) 53 NATIVE_CPUID_REG(ebx) 54 NATIVE_CPUID_REG(ecx) 55 NATIVE_CPUID_REG(edx) 56 57 #ifdef CONFIG_PARAVIRT_XXL 58 # include <asm/paravirt.h> 59 #else 60 # define __cpuid native_cpuid 61 #endif 62 63 /* 64 * Generic CPUID function 65 * 66 * Clear ECX since some CPUs (Cyrix MII) do not set or clear ECX 67 * resulting in stale register contents being returned. 68 */ 69 static inline void cpuid(u32 op, 70 u32 *eax, u32 *ebx, 71 u32 *ecx, u32 *edx) 72 { 73 *eax = op; 74 *ecx = 0; 75 __cpuid(eax, ebx, ecx, edx); 76 } 77 78 /* Some CPUID calls want 'count' to be placed in ECX */ 79 static inline void cpuid_count(u32 op, int count, 80 u32 *eax, u32 *ebx, 81 u32 *ecx, u32 *edx) 82 { 83 *eax = op; 84 *ecx = count; 85 __cpuid(eax, ebx, ecx, edx); 86 } 87 88 /* 89 * CPUID functions returning a single datum: 90 */ 91 92 static inline u32 cpuid_eax(u32 op) 93 { 94 u32 eax, ebx, ecx, edx; 95 96 cpuid(op, &eax, &ebx, &ecx, &edx); 97 98 return eax; 99 } 100 101 static inline u32 cpuid_ebx(u32 op) 102 { 103 u32 eax, ebx, ecx, edx; 104 105 cpuid(op, &eax, &ebx, &ecx, &edx); 106 107 return ebx; 108 } 109 110 static inline u32 cpuid_ecx(u32 op) 111 { 112 u32 eax, ebx, ecx, edx; 113 114 cpuid(op, &eax, &ebx, &ecx, &edx); 115 116 return ecx; 117 } 118 119 static inline u32 cpuid_edx(u32 op) 120 { 121 u32 eax, ebx, ecx, edx; 122 123 cpuid(op, &eax, &ebx, &ecx, &edx); 124 125 return edx; 126 } 127 128 static inline void __cpuid_read(u32 leaf, u32 subleaf, u32 *regs) 129 { 130 regs[CPUID_EAX] = leaf; 131 regs[CPUID_ECX] = subleaf; 132 __cpuid(regs + CPUID_EAX, regs + CPUID_EBX, regs + CPUID_ECX, regs + CPUID_EDX); 133 } 134 135 #define cpuid_read_subleaf(leaf, subleaf, regs) { \ 136 static_assert(sizeof(*(regs)) == 16); \ 137 __cpuid_read(leaf, subleaf, (u32 *)(regs)); \ 138 } 139 140 #define cpuid_read(leaf, regs) { \ 141 static_assert(sizeof(*(regs)) == 16); \ 142 __cpuid_read(leaf, 0, (u32 *)(regs)); \ 143 } 144 145 static inline void __cpuid_read_reg(u32 leaf, u32 subleaf, 146 enum cpuid_regs_idx regidx, u32 *reg) 147 { 148 u32 regs[4]; 149 150 __cpuid_read(leaf, subleaf, regs); 151 *reg = regs[regidx]; 152 } 153 154 #define cpuid_subleaf_reg(leaf, subleaf, regidx, reg) { \ 155 static_assert(sizeof(*(reg)) == 4); \ 156 __cpuid_read_reg(leaf, subleaf, regidx, (u32 *)(reg)); \ 157 } 158 159 #define cpuid_leaf_reg(leaf, regidx, reg) { \ 160 static_assert(sizeof(*(reg)) == 4); \ 161 __cpuid_read_reg(leaf, 0, regidx, (u32 *)(reg)); \ 162 } 163 164 /* 165 * Hypervisor-related APIs: 166 */ 167 168 static __always_inline bool cpuid_function_is_indexed(u32 function) 169 { 170 switch (function) { 171 case 4: 172 case 7: 173 case 0xb: 174 case 0xd: 175 case 0xf: 176 case 0x10: 177 case 0x12: 178 case 0x14: 179 case 0x17: 180 case 0x18: 181 case 0x1d: 182 case 0x1e: 183 case 0x1f: 184 case 0x23: 185 case 0x24: 186 case 0x8000001d: 187 return true; 188 } 189 190 return false; 191 } 192 193 #define for_each_possible_cpuid_base_hypervisor(function) \ 194 for (function = 0x40000000; function < 0x40010000; function += 0x100) 195 196 static inline u32 cpuid_base_hypervisor(const char *sig, u32 leaves) 197 { 198 u32 base, eax, signature[3]; 199 200 for_each_possible_cpuid_base_hypervisor(base) { 201 cpuid(base, &eax, &signature[0], &signature[1], &signature[2]); 202 203 /* 204 * This must not compile to "call memcmp" because it's called 205 * from PVH early boot code before instrumentation is set up 206 * and memcmp() itself may be instrumented. 207 */ 208 if (!__inline_memcmp(sig, signature, 12) && 209 (leaves == 0 || ((eax - base) >= leaves))) 210 return base; 211 } 212 213 return 0; 214 } 215 216 /* 217 * CPUID(0x2) parsing: 218 */ 219 220 /** 221 * cpuid_leaf_0x2() - Return sanitized CPUID(0x2) register output 222 * @regs: Output parameter 223 * 224 * Query CPUID(0x2) and store its output in @regs. Force set any 225 * invalid 1-byte descriptor returned by the hardware to zero (the NULL 226 * cache/TLB descriptor) before returning it to the caller. 227 * 228 * Use for_each_cpuid_0x2_desc() to iterate over the register output in 229 * parsed form. 230 */ 231 static inline void cpuid_leaf_0x2(union leaf_0x2_regs *regs) 232 { 233 cpuid_read(0x2, regs); 234 235 /* 236 * All Intel CPUs must report an iteration count of 1. In case 237 * of bogus hardware, treat all returned descriptors as NULL. 238 */ 239 if (regs->desc[0] != 0x01) { 240 for (int i = 0; i < 4; i++) 241 regs->regv[i] = 0; 242 return; 243 } 244 245 /* 246 * The most significant bit (MSB) of each register must be clear. 247 * If a register is invalid, replace its descriptors with NULL. 248 */ 249 for (int i = 0; i < 4; i++) { 250 if (regs->reg[i].invalid) 251 regs->regv[i] = 0; 252 } 253 } 254 255 /** 256 * for_each_cpuid_0x2_desc() - Iterator for parsed CPUID(0x2) descriptors 257 * @_regs: CPUID(0x2) register output, as returned by cpuid_leaf_0x2() 258 * @_ptr: u8 pointer, for macro internal use only 259 * @_desc: Pointer to the parsed CPUID(0x2) descriptor at each iteration 260 * 261 * Loop over the 1-byte descriptors in the passed CPUID(0x2) output registers 262 * @_regs. Provide the parsed information for each descriptor through @_desc. 263 * 264 * To handle cache-specific descriptors, switch on @_desc->c_type. For TLB 265 * descriptors, switch on @_desc->t_type. 266 * 267 * Example usage for cache descriptors:: 268 * 269 * const struct leaf_0x2_table *desc; 270 * union leaf_0x2_regs regs; 271 * u8 *ptr; 272 * 273 * cpuid_leaf_0x2(®s); 274 * for_each_cpuid_0x2_desc(regs, ptr, desc) { 275 * switch (desc->c_type) { 276 * ... 277 * } 278 * } 279 */ 280 #define for_each_cpuid_0x2_desc(_regs, _ptr, _desc) \ 281 for (_ptr = &(_regs).desc[1]; \ 282 _ptr < &(_regs).desc[16] && (_desc = &cpuid_0x2_table[*_ptr]); \ 283 _ptr++) 284 285 /* 286 * CPUID(0x80000006) parsing: 287 */ 288 289 static inline bool cpuid_amd_hygon_has_l3_cache(void) 290 { 291 return cpuid_edx(0x80000006); 292 } 293 294 /* 295 * 'struct cpuid_leaves' accessors (without sanity checks): 296 * 297 * For internal use by the CPUID parser. 298 */ 299 300 /* Return constified pointers for all call-site APIs */ 301 #define __const_ptr(_ptr) \ 302 ((const __typeof__(*(_ptr)) *)(_ptr)) 303 304 #define __cpuid_leaves_subleaf(_leaves, _leaf, _subleaf) \ 305 __const_ptr(&((_leaves)->leaf_ ## _leaf ## _ ## _subleaf)[0]) 306 307 #define __cpuid_leaves_subleaf_n(_leaves, _leaf, _index) \ 308 __const_ptr(&((_leaves)->leaf_ ## _leaf ## _ ## n)[_index]) 309 310 #define __cpuid_leaves_subleaf_info(_leaves, _leaf, _subleaf) \ 311 __const_ptr(&((_leaves)->leaf_ ## _leaf ## _ ## _subleaf ## _ ## info)) 312 313 /* 314 * 'struct cpuid_table' accessors (with sanity checks): 315 * 316 * For internal use by the CPUID parser. 317 */ 318 319 #define __cpuid_table_nr_filled_subleaves(_table, _leaf, _subleaf) \ 320 __cpuid_leaves_subleaf_info(&((_table)->leaves), _leaf, _subleaf)->nr_entries 321 322 #define __cpuid_table_subleaf_range_size(_table, _leaf) \ 323 ARRAY_SIZE((_table)->leaves.leaf_ ## _leaf ## _n) 324 325 #define __cpuid_table_invalid_subleaf(_table, _leaf, _subleaf) \ 326 (((_subleaf) < (__cpuid_leaf_first_subleaf(_leaf))) || \ 327 ((_subleaf) > (__cpuid_leaf_first_subleaf(_leaf) + \ 328 __cpuid_table_subleaf_range_size(_table, _leaf) - 1))) 329 330 /* Return NULL if the parser did not fill that leaf. Check cpuid_subleaf(). */ 331 #define __cpuid_table_subleaf(_table, _leaf, _subleaf) \ 332 ({ \ 333 unsigned int ____f = __cpuid_table_nr_filled_subleaves(_table, _leaf, _subleaf); \ 334 \ 335 (____f != 1) ? NULL : __cpuid_leaves_subleaf(&((_table)->leaves), _leaf, _subleaf); \ 336 }) 337 338 /* 339 * Return NULL if the CPUID parser did not fill this leaf, or if the given 340 * dynamic subleaf value is out of range. Check cpuid_subleaf_n(). 341 */ 342 #define __cpuid_table_subleaf_n(_table, _leaf, _subleaf) \ 343 ({ \ 344 unsigned int ____i = (_subleaf) - __cpuid_leaf_first_subleaf(_leaf); \ 345 unsigned int ____f = __cpuid_table_nr_filled_subleaves(_table, _leaf, n); \ 346 \ 347 /* CPUID parser might not have filled the entire subleaf range */ \ 348 ((____i >= ____f) || __cpuid_table_invalid_subleaf(_table, _leaf, _subleaf)) ? \ 349 NULL : __cpuid_leaves_subleaf_n(&((_table)->leaves), _leaf, ____i); \ 350 }) 351 352 /* 353 * Compile-time checks for leaves with a subleaf range: 354 */ 355 356 #define __cpuid_assert_subleaf_range(_cpuinfo, _leaf) \ 357 static_assert(__cpuid_table_subleaf_range_size(&(_cpuinfo)->cpuid, _leaf) > 1) 358 359 #define __cpuid_assert_subleaf_within_range(_cpuinfo, _leaf, _subleaf) \ 360 BUILD_BUG_ON(__builtin_constant_p(_subleaf) && \ 361 __cpuid_table_invalid_subleaf(&(_cpuinfo)->cpuid, _leaf, _subleaf)) 362 363 /* 364 * CPUID Parser Call-site APIs 365 * 366 * Call sites should use below APIs instead of invoking direct CPUID queries. 367 * 368 * Benefits include: 369 * 370 * - Return CPUID output as typed C structures that are auto-generated from a 371 * centralized database (see <asm/cpuid/leaf_types.h). Such data types have a 372 * full C99 bitfield layout per CPUID leaf/subleaf combination. Call sites 373 * can thus avoid doing ugly and cryptic bitwise operations on raw CPUID data. 374 * 375 * - Return cached, per-CPU, CPUID output. Below APIs do not invoke any CPUID 376 * queries, thus avoiding their side effects like serialization and VM exits. 377 * Call-site-specific hard coded constants and macros for caching CPUID query 378 * outputs can also be avoided. 379 * 380 * - Return sanitized CPUID data. Below APIs return NULL if the given CPUID 381 * leaf/subleaf input is not supported by hardware, or if the hardware CPUID 382 * output was deemed invalid by the CPUID parser. This centralizes all CPUID 383 * data sanitization in one place (the kernel's CPUID parser.) 384 * 385 * - A centralized global view of system CPUID data. Below APIs will reflect 386 * any kernel-enforced feature masking or overrides, unlike ad hoc parsing of 387 * raw CPUID output by drivers and individual call sites. 388 */ 389 390 /* 391 * Call-site APIs for CPUID leaves with a single subleaf: 392 */ 393 394 /** 395 * cpuid_subleaf() - Access parsed CPUID 396 * @_cpuinfo: CPU capability structure reference ('struct cpuinfo_x86') 397 * @_leaf: CPUID leaf, in compile-time 0xN format; e.g. 0x7, 0xf 398 * @_subleaf: CPUID subleaf, in compile-time decimal format; e.g. 0, 1, 3 399 * 400 * Returns a pointer to parsed CPUID output, from the CPUID table inside 401 * @_cpuinfo, as a <cpuid/leaf_types.h> data type: 'struct leaf_0xM_N', where 402 * 0xM is the token provided at @_leaf, and N is the token provided at 403 * @_subleaf; e.g. struct leaf_0x7_0. 404 * 405 * Returns NULL if the requested CPUID @_leaf/@_subleaf query output is not 406 * present at the parsed CPUID table inside @_cpuinfo. This can happen if: 407 * 408 * - The CPUID table inside @_cpuinfo has not yet been populated. 409 * - The CPUID table inside @_cpuinfo was populated, but the CPU does not 410 * implement the requested CPUID @_leaf/@_subleaf combination. 411 * - The CPUID table inside @_cpuinfo was populated, but the kernel's CPUID 412 * parser has predetermined that the requested CPUID @_leaf/@_subleaf 413 * hardware output is invalid or unsupported. 414 * 415 * Example usage:: 416 * 417 * const struct leaf_0x7_0 *l7_0 = cpuid_subleaf(c, 0x7, 0); 418 * if (!l7_0) { 419 * // Handle error 420 * } 421 * 422 * const struct leaf_0x7_1 *l7_1 = cpuid_subleaf(c, 0x7, 1); 423 * if (!l7_1) { 424 * // Handle error 425 * } 426 */ 427 #define cpuid_subleaf(_cpuinfo, _leaf, _subleaf) \ 428 __cpuid_table_subleaf(&(_cpuinfo)->cpuid, _leaf, _subleaf) \ 429 430 /** 431 * cpuid_leaf() - Access parsed CPUID data 432 * @_cpuinfo: CPU capability structure reference ('struct cpuinfo_x86') 433 * @_leaf: CPUID leaf, in compile-time 0xN format; e.g. 0x0, 0x2, 0x80000000 434 * 435 * Similar to cpuid_subleaf(), but with a CPUID subleaf = 0. 436 * 437 * Example usage:: 438 * 439 * const struct leaf_0x0_0 *l0 = cpuid_leaf(c, 0x0); 440 * if (!l0) { 441 * // Handle error 442 * } 443 * 444 * const struct leaf_0x80000000_0 *el0 = cpuid_leaf(c, 0x80000000); 445 * if (!el0) { 446 * // Handle error 447 * } 448 */ 449 #define cpuid_leaf(_cpuinfo, _leaf) \ 450 cpuid_subleaf(_cpuinfo, _leaf, 0) 451 452 /** 453 * cpuid_leaf_raw() - Access parsed CPUID data in raw format 454 * @_cpuinfo: CPU capability structure reference ('struct cpuinfo_x86') 455 * @_leaf: CPUID leaf, in compile-time 0xN format 456 * 457 * Similar to cpuid_leaf(), but returns a raw 'struct cpuid_regs' pointer to 458 * the parsed CPUID data instead of a "typed" <asm/cpuid/leaf_types.h> pointer. 459 */ 460 #define cpuid_leaf_raw(_cpuinfo, _leaf) \ 461 ((const struct cpuid_regs *)(cpuid_leaf(_cpuinfo, _leaf))) 462 463 /* 464 * Call-site APIs for CPUID leaves with a subleaf range: 465 */ 466 467 /** 468 * cpuid_subleaf_n() - Access parsed CPUID data for leaf with a subleaf range 469 * @_cpuinfo: CPU capability structure reference ('struct cpuinfo_x86') 470 * @_leaf: CPUID leaf, in compile-time 0xN format; e.g. 0x4, 0x8000001d 471 * @_subleaf: Subleaf number, which can be passed dynamically. It must be smaller 472 * than cpuid_subleaf_count(@_cpuinfo, @_leaf). 473 * 474 * Build-time errors will be emitted in the following cases: 475 * 476 * - @_leaf has no subleaf range. Leaves with a subleaf range have an '_n' type 477 * suffix and are listed at <asm/cpuid/types.h> using the CPUID_LEAF_N() macro. 478 * 479 * - @_subleaf is known at compile-time but is out of range. 480 * 481 * Example usage:: 482 * 483 * const struct leaf_0x4_n *l4; 484 * 485 * for (int i = 0; i < cpuid_subleaf_count(c, 0x4); i++) { 486 * l4 = cpuid_subleaf_n(c, 0x4, i); 487 * if (!l4) { 488 * // Handle error 489 * } 490 * ... 491 * } 492 * 493 * Beside the standard error situations detailed at cpuid_subleaf(), this 494 * macro will also return NULL if @_subleaf is out of the leaf's subleaf range. 495 */ 496 #define cpuid_subleaf_n(_cpuinfo, _leaf, _subleaf) \ 497 ({ \ 498 __cpuid_assert_subleaf_range(_cpuinfo, _leaf); \ 499 __cpuid_assert_subleaf_within_range(_cpuinfo, _leaf, _subleaf); \ 500 __cpuid_table_subleaf_n(&(_cpuinfo)->cpuid, _leaf, _subleaf); \ 501 }) 502 503 /** 504 * cpuid_subleaf_n_raw() - Access parsed CPUID data for leaf with subleaf range 505 * @_cpuinfo: CPU capability structure reference ('struct cpuinfo_x86') 506 * @_leaf: CPUID leaf, in compile-time 0xN format; e.g. 0x4, 0x8000001d 507 * @_subleaf: Subleaf number, which can be passed dynamically. It must be smaller 508 * than cpuid_subleaf_count(@_cpuinfo, @_leaf). 509 * 510 * Similar to cpuid_subleaf_n(), but returns a raw 'struct cpuid_regs' pointer to 511 * the parsed CPUID data instead of a "typed" <asm/cpuid/leaf_types.h> pointer. 512 */ 513 #define cpuid_subleaf_n_raw(_cpuinfo, _leaf, _subleaf) \ 514 ((const struct cpuid_regs *)cpuid_subleaf_n(_cpuinfo, _leaf, _subleaf)) 515 516 /** 517 * cpuid_subleaf_count() - Number of filled subleaves for @_leaf 518 * @_cpuinfo: CPU capability structure reference ('struct cpuinfo_x86') 519 * @_leaf: CPUID leaf, in compile-time 0xN format; e.g. 0x4, 0x8000001d 520 * 521 * Return the number of subleaves filled by the CPUID parser for @_leaf. 522 * 523 * @_leaf must have subleaf range. Leaves with a subleaf range have an '_n' type 524 * suffix and are listed at <asm/cpuid/types.h> using the CPUID_LEAF_N() macro. 525 */ 526 #define cpuid_subleaf_count(_cpuinfo, _leaf) \ 527 ({ \ 528 __cpuid_assert_subleaf_range(_cpuinfo, _leaf); \ 529 __cpuid_table_nr_filled_subleaves(&(_cpuinfo)->cpuid, _leaf, n); \ 530 }) 531 532 /* 533 * CPUID parser exported APIs: 534 */ 535 536 void cpuid_scan_cpu(struct cpuinfo_x86 *c); 537 void cpuid_refresh_leaf(struct cpuinfo_x86 *c, u32 leaf); 538 void cpuid_refresh_range(struct cpuinfo_x86 *c, u32 start, u32 end); 539 540 #endif /* _ASM_X86_CPUID_API_H */ 541