1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/export.h> 3 #include <linux/bitops.h> 4 #include <linux/dmi.h> 5 #include <linux/elf.h> 6 #include <linux/mm.h> 7 #include <linux/kvm_types.h> 8 #include <linux/io.h> 9 #include <linux/sched.h> 10 #include <linux/sched/clock.h> 11 #include <linux/random.h> 12 #include <linux/topology.h> 13 #include <linux/platform_data/x86/amd-fch.h> 14 #include <asm/processor.h> 15 #include <asm/apic.h> 16 #include <asm/cacheinfo.h> 17 #include <asm/cpu.h> 18 #include <asm/cpu_device_id.h> 19 #include <asm/cpuid/api.h> 20 #include <asm/spec-ctrl.h> 21 #include <asm/smp.h> 22 #include <asm/numa.h> 23 #include <asm/pci-direct.h> 24 #include <asm/delay.h> 25 #include <asm/debugreg.h> 26 #include <asm/resctrl.h> 27 #include <asm/msr.h> 28 #include <asm/sev.h> 29 30 #ifdef CONFIG_X86_64 31 # include <asm/mmconfig.h> 32 #endif 33 34 #include "cpu.h" 35 36 u16 invlpgb_count_max __ro_after_init = 1; 37 38 static inline int rdmsrq_amd_safe(unsigned msr, u64 *p) 39 { 40 u32 gprs[8] = { 0 }; 41 int err; 42 43 WARN_ONCE((boot_cpu_data.x86 != 0xf), 44 "%s should only be used on K8!\n", __func__); 45 46 gprs[1] = msr; 47 gprs[7] = 0x9c5a203a; 48 49 err = rdmsr_safe_regs(gprs); 50 51 *p = gprs[0] | ((u64)gprs[2] << 32); 52 53 return err; 54 } 55 56 static inline int wrmsrq_amd_safe(unsigned msr, u64 val) 57 { 58 u32 gprs[8] = { 0 }; 59 60 WARN_ONCE((boot_cpu_data.x86 != 0xf), 61 "%s should only be used on K8!\n", __func__); 62 63 gprs[0] = (u32)val; 64 gprs[1] = msr; 65 gprs[2] = val >> 32; 66 gprs[7] = 0x9c5a203a; 67 68 return wrmsr_safe_regs(gprs); 69 } 70 71 /* 72 * B step AMD K6 before B 9730xxxx have hardware bugs that can cause 73 * misexecution of code under Linux. Owners of such processors should 74 * contact AMD for precise details and a CPU swap. 75 * 76 * See http://www.multimania.com/poulot/k6bug.html 77 * and section 2.6.2 of "AMD-K6 Processor Revision Guide - Model 6" 78 * (Publication # 21266 Issue Date: August 1998) 79 * 80 * The following test is erm.. interesting. AMD neglected to up 81 * the chip setting when fixing the bug but they also tweaked some 82 * performance at the same time.. 83 */ 84 85 #ifdef CONFIG_X86_32 86 extern __visible void vide(void); 87 __asm__(".text\n" 88 ".globl vide\n" 89 ".type vide, @function\n" 90 ".align 4\n" 91 "vide: ret\n"); 92 #endif 93 94 static void init_amd_k5(struct cpuinfo_x86 *c) 95 { 96 #ifdef CONFIG_X86_32 97 /* 98 * General Systems BIOSen alias the cpu frequency registers 99 * of the Elan at 0x000df000. Unfortunately, one of the Linux 100 * drivers subsequently pokes it, and changes the CPU speed. 101 * Workaround : Remove the unneeded alias. 102 */ 103 #define CBAR (0xfffc) /* Configuration Base Address (32-bit) */ 104 #define CBAR_ENB (0x80000000) 105 #define CBAR_KEY (0X000000CB) 106 if (c->x86_model == 9 || c->x86_model == 10) { 107 if (inl(CBAR) & CBAR_ENB) 108 outl(0 | CBAR_KEY, CBAR); 109 } 110 #endif 111 } 112 113 static void init_amd_k6(struct cpuinfo_x86 *c) 114 { 115 #ifdef CONFIG_X86_32 116 struct msr val; 117 int mbytes = get_num_physpages() >> (20-PAGE_SHIFT); 118 119 if (c->x86_model < 6) { 120 /* Based on AMD doc 20734R - June 2000 */ 121 if (c->x86_model == 0) { 122 clear_cpu_cap(c, X86_FEATURE_APIC); 123 set_cpu_cap(c, X86_FEATURE_PGE); 124 } 125 return; 126 } 127 128 if (c->x86_model == 6 && c->x86_stepping == 1) { 129 const int K6_BUG_LOOP = 1000000; 130 int n; 131 void (*f_vide)(void); 132 u64 d, d2; 133 134 pr_info("AMD K6 stepping B detected - "); 135 136 /* 137 * It looks like AMD fixed the 2.6.2 bug and improved indirect 138 * calls at the same time. 139 */ 140 141 n = K6_BUG_LOOP; 142 f_vide = vide; 143 OPTIMIZER_HIDE_VAR(f_vide); 144 d = rdtsc(); 145 while (n--) 146 f_vide(); 147 d2 = rdtsc(); 148 d = d2-d; 149 150 if (d > 20*K6_BUG_LOOP) 151 pr_cont("system stability may be impaired when more than 32 MB are used.\n"); 152 else 153 pr_cont("probably OK (after B9730xxxx).\n"); 154 } 155 156 /* K6 with old style WHCR */ 157 if (c->x86_model < 8 || 158 (c->x86_model == 8 && c->x86_stepping < 8)) { 159 /* We can only write allocate on the low 508Mb */ 160 if (mbytes > 508) 161 mbytes = 508; 162 163 rdmsrq(MSR_K6_WHCR, val.q); 164 if ((val.l & 0x0000FFFF) == 0) { 165 unsigned long flags; 166 val.l = (1 << 0) | ((mbytes / 4) << 1); 167 local_irq_save(flags); 168 wbinvd(); 169 wrmsrq(MSR_K6_WHCR, val.q); 170 local_irq_restore(flags); 171 pr_info("Enabling old style K6 write allocation for %d Mb\n", 172 mbytes); 173 } 174 return; 175 } 176 177 if ((c->x86_model == 8 && c->x86_stepping > 7) || 178 c->x86_model == 9 || c->x86_model == 13) { 179 /* The more serious chips .. */ 180 181 if (mbytes > 4092) 182 mbytes = 4092; 183 184 rdmsrq(MSR_K6_WHCR, val.q); 185 if ((val.l & 0xFFFF0000) == 0) { 186 unsigned long flags; 187 val.l = ((mbytes >> 2) << 22) | (1 << 16); 188 local_irq_save(flags); 189 wbinvd(); 190 wrmsrq(MSR_K6_WHCR, val.q); 191 local_irq_restore(flags); 192 pr_info("Enabling new style K6 write allocation for %d Mb\n", 193 mbytes); 194 } 195 196 return; 197 } 198 199 if (c->x86_model == 10) { 200 /* AMD Geode LX is model 10 */ 201 /* placeholder for any needed mods */ 202 return; 203 } 204 #endif 205 } 206 207 static void init_amd_k7(struct cpuinfo_x86 *c) 208 { 209 #ifdef CONFIG_X86_32 210 struct msr val; 211 212 /* 213 * Bit 15 of Athlon specific MSR 15, needs to be 0 214 * to enable SSE on Palomino/Morgan/Barton CPU's. 215 * If the BIOS didn't enable it already, enable it here. 216 */ 217 if (c->x86_model >= 6 && c->x86_model <= 10) { 218 if (!cpu_has(c, X86_FEATURE_XMM)) { 219 pr_info("Enabling disabled K7/SSE Support.\n"); 220 msr_clear_bit(MSR_K7_HWCR, 15); 221 set_cpu_cap(c, X86_FEATURE_XMM); 222 } 223 } 224 225 /* 226 * It's been determined by AMD that Athlons since model 8 stepping 1 227 * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx 228 * As per AMD technical note 27212 0.2 229 */ 230 if ((c->x86_model == 8 && c->x86_stepping >= 1) || (c->x86_model > 8)) { 231 rdmsrq(MSR_K7_CLK_CTL, val.q); 232 if ((val.l & 0xfff00000) != 0x20000000) { 233 pr_info("CPU: CLK_CTL MSR was %x. Reprogramming to %x\n", 234 val.l, ((val.l & 0x000fffff) | 0x20000000)); 235 val.l = (val.l & 0x000fffff) | 0x20000000; 236 wrmsrq(MSR_K7_CLK_CTL, val.q); 237 } 238 } 239 240 /* calling is from identify_secondary_cpu() ? */ 241 if (!c->cpu_index) 242 return; 243 244 /* 245 * Certain Athlons might work (for various values of 'work') in SMP 246 * but they are not certified as MP capable. 247 */ 248 /* Athlon 660/661 is valid. */ 249 if ((c->x86_model == 6) && ((c->x86_stepping == 0) || 250 (c->x86_stepping == 1))) 251 return; 252 253 /* Duron 670 is valid */ 254 if ((c->x86_model == 7) && (c->x86_stepping == 0)) 255 return; 256 257 /* 258 * Athlon 662, Duron 671, and Athlon >model 7 have capability 259 * bit. It's worth noting that the A5 stepping (662) of some 260 * Athlon XP's have the MP bit set. 261 * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for 262 * more. 263 */ 264 if (((c->x86_model == 6) && (c->x86_stepping >= 2)) || 265 ((c->x86_model == 7) && (c->x86_stepping >= 1)) || 266 (c->x86_model > 7)) 267 if (cpu_has(c, X86_FEATURE_MP)) 268 return; 269 270 /* If we get here, not a certified SMP capable AMD system. */ 271 272 /* 273 * Don't taint if we are running SMP kernel on a single non-MP 274 * approved Athlon 275 */ 276 WARN_ONCE(1, "WARNING: This combination of AMD" 277 " processors is not suitable for SMP.\n"); 278 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_NOW_UNRELIABLE); 279 #endif 280 } 281 282 #ifdef CONFIG_NUMA 283 /* 284 * To workaround broken NUMA config. Read the comment in 285 * srat_detect_node(). 286 */ 287 static int nearby_node(int apicid) 288 { 289 int i, node; 290 291 for (i = apicid - 1; i >= 0; i--) { 292 node = __apicid_to_node[i]; 293 if (node != NUMA_NO_NODE && node_online(node)) 294 return node; 295 } 296 for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) { 297 node = __apicid_to_node[i]; 298 if (node != NUMA_NO_NODE && node_online(node)) 299 return node; 300 } 301 return first_node(node_online_map); /* Shouldn't happen */ 302 } 303 #endif 304 305 static void srat_detect_node(struct cpuinfo_x86 *c) 306 { 307 #ifdef CONFIG_NUMA 308 int cpu = smp_processor_id(); 309 int node; 310 unsigned apicid = c->topo.apicid; 311 312 node = numa_cpu_node(cpu); 313 if (node == NUMA_NO_NODE) 314 node = per_cpu_llc_id(cpu); 315 316 /* 317 * On multi-fabric platform (e.g. Numascale NumaChip) a 318 * platform-specific handler needs to be called to fixup some 319 * IDs of the CPU. 320 */ 321 if (x86_cpuinit.fixup_cpu_id) 322 x86_cpuinit.fixup_cpu_id(c, node); 323 324 if (!node_online(node)) { 325 /* 326 * Two possibilities here: 327 * 328 * - The CPU is missing memory and no node was created. In 329 * that case try picking one from a nearby CPU. 330 * 331 * - The APIC IDs differ from the HyperTransport node IDs 332 * which the K8 northbridge parsing fills in. Assume 333 * they are all increased by a constant offset, but in 334 * the same order as the HT nodeids. If that doesn't 335 * result in a usable node fall back to the path for the 336 * previous case. 337 * 338 * This workaround operates directly on the mapping between 339 * APIC ID and NUMA node, assuming certain relationship 340 * between APIC ID, HT node ID and NUMA topology. As going 341 * through CPU mapping may alter the outcome, directly 342 * access __apicid_to_node[]. 343 */ 344 int ht_nodeid = c->topo.initial_apicid; 345 346 if (__apicid_to_node[ht_nodeid] != NUMA_NO_NODE) 347 node = __apicid_to_node[ht_nodeid]; 348 /* Pick a nearby node */ 349 if (!node_online(node)) 350 node = nearby_node(apicid); 351 } 352 numa_set_node(cpu, node); 353 #endif 354 } 355 356 static void bsp_determine_snp(struct cpuinfo_x86 *c) 357 { 358 #ifdef CONFIG_ARCH_HAS_CC_PLATFORM 359 cc_vendor = CC_VENDOR_AMD; 360 361 if (cpu_has(c, X86_FEATURE_SEV_SNP)) { 362 /* 363 * RMP table entry format is not architectural and is defined by the 364 * per-processor PPR. Restrict SNP support on the known CPU models 365 * for which the RMP table entry format is currently defined or for 366 * processors which support the architecturally defined RMPREAD 367 * instruction. 368 */ 369 if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && 370 (cpu_feature_enabled(X86_FEATURE_ZEN3) || 371 cpu_feature_enabled(X86_FEATURE_ZEN4) || 372 cpu_feature_enabled(X86_FEATURE_RMPREAD)) && 373 snp_probe_rmptable_info()) { 374 cc_platform_set(CC_ATTR_HOST_SEV_SNP); 375 } else { 376 setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); 377 cc_platform_clear(CC_ATTR_HOST_SEV_SNP); 378 } 379 } 380 #endif 381 } 382 383 #define ZEN_MODEL_STEP_UCODE(fam, model, step, ucode) \ 384 X86_MATCH_VFM_STEPS(VFM_MAKE(X86_VENDOR_AMD, fam, model), \ 385 step, step, ucode) 386 387 static const struct x86_cpu_id amd_tsa_microcode[] = { 388 ZEN_MODEL_STEP_UCODE(0x19, 0x01, 0x1, 0x0a0011d7), 389 ZEN_MODEL_STEP_UCODE(0x19, 0x01, 0x2, 0x0a00123b), 390 ZEN_MODEL_STEP_UCODE(0x19, 0x08, 0x2, 0x0a00820d), 391 ZEN_MODEL_STEP_UCODE(0x19, 0x11, 0x1, 0x0a10114c), 392 ZEN_MODEL_STEP_UCODE(0x19, 0x11, 0x2, 0x0a10124c), 393 ZEN_MODEL_STEP_UCODE(0x19, 0x18, 0x1, 0x0a108109), 394 ZEN_MODEL_STEP_UCODE(0x19, 0x21, 0x0, 0x0a20102e), 395 ZEN_MODEL_STEP_UCODE(0x19, 0x21, 0x2, 0x0a201211), 396 ZEN_MODEL_STEP_UCODE(0x19, 0x44, 0x1, 0x0a404108), 397 ZEN_MODEL_STEP_UCODE(0x19, 0x50, 0x0, 0x0a500012), 398 ZEN_MODEL_STEP_UCODE(0x19, 0x61, 0x2, 0x0a60120a), 399 ZEN_MODEL_STEP_UCODE(0x19, 0x74, 0x1, 0x0a704108), 400 ZEN_MODEL_STEP_UCODE(0x19, 0x75, 0x2, 0x0a705208), 401 ZEN_MODEL_STEP_UCODE(0x19, 0x78, 0x0, 0x0a708008), 402 ZEN_MODEL_STEP_UCODE(0x19, 0x7c, 0x0, 0x0a70c008), 403 ZEN_MODEL_STEP_UCODE(0x19, 0xa0, 0x2, 0x0aa00216), 404 {}, 405 }; 406 407 static void tsa_init(struct cpuinfo_x86 *c) 408 { 409 if (cpu_has(c, X86_FEATURE_HYPERVISOR)) 410 return; 411 412 if (cpu_has(c, X86_FEATURE_ZEN3) || 413 cpu_has(c, X86_FEATURE_ZEN4)) { 414 if (x86_match_min_microcode_rev(amd_tsa_microcode)) 415 setup_force_cpu_cap(X86_FEATURE_VERW_CLEAR); 416 else 417 pr_debug("%s: current revision: 0x%x\n", __func__, c->microcode); 418 } else { 419 if (!cpu_has(c, X86_FEATURE_ZEN5)) { 420 setup_force_cpu_cap(X86_FEATURE_TSA_SQ_NO); 421 setup_force_cpu_cap(X86_FEATURE_TSA_L1_NO); 422 } 423 } 424 425 if (!cpu_has(c, X86_FEATURE_ZEN5)) 426 setup_force_cpu_cap(X86_FEATURE_TSA_FILL_NO); 427 } 428 429 static void bsp_init_amd(struct cpuinfo_x86 *c) 430 { 431 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) { 432 433 if (c->x86 > 0x10 || 434 (c->x86 == 0x10 && c->x86_model >= 0x2)) { 435 u64 val; 436 437 rdmsrq(MSR_K7_HWCR, val); 438 if (!(val & BIT(24))) 439 pr_warn(FW_BUG "TSC doesn't count with P0 frequency!\n"); 440 } 441 } 442 443 if (c->x86 == 0x15) { 444 unsigned long upperbit; 445 u32 cpuid, assoc; 446 447 cpuid = cpuid_edx(0x80000005); 448 assoc = cpuid >> 16 & 0xff; 449 upperbit = ((cpuid >> 24) << 10) / assoc; 450 451 va_align.mask = (upperbit - 1) & PAGE_MASK; 452 va_align.flags = ALIGN_VA_32 | ALIGN_VA_64; 453 454 /* A random value per boot for bit slice [12:upper_bit) */ 455 va_align.bits = get_random_u32() & va_align.mask; 456 } 457 458 if (cpu_has(c, X86_FEATURE_MWAITX)) 459 use_mwaitx_delay(); 460 461 if (!boot_cpu_has(X86_FEATURE_AMD_SSBD) && 462 !boot_cpu_has(X86_FEATURE_VIRT_SSBD) && 463 c->x86 >= 0x15 && c->x86 <= 0x17) { 464 unsigned int bit; 465 466 switch (c->x86) { 467 case 0x15: bit = 54; break; 468 case 0x16: bit = 33; break; 469 case 0x17: bit = 10; break; 470 default: return; 471 } 472 /* 473 * Try to cache the base value so further operations can 474 * avoid RMW. If that faults, do not enable SSBD. 475 */ 476 if (!rdmsrq_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) { 477 setup_force_cpu_cap(X86_FEATURE_LS_CFG_SSBD); 478 setup_force_cpu_cap(X86_FEATURE_SSBD); 479 x86_amd_ls_cfg_ssbd_mask = 1ULL << bit; 480 } 481 } 482 483 resctrl_cpu_detect(c); 484 485 /* Figure out Zen generations: */ 486 switch (c->x86) { 487 case 0x17: 488 switch (c->x86_model) { 489 case 0x00 ... 0x2f: 490 case 0x50 ... 0x5f: 491 setup_force_cpu_cap(X86_FEATURE_ZEN1); 492 break; 493 case 0x30 ... 0x4f: 494 case 0x60 ... 0x7f: 495 case 0x90 ... 0x91: 496 case 0xa0 ... 0xaf: 497 setup_force_cpu_cap(X86_FEATURE_ZEN2); 498 break; 499 default: 500 goto warn; 501 } 502 break; 503 504 case 0x19: 505 switch (c->x86_model) { 506 case 0x00 ... 0x0f: 507 case 0x20 ... 0x5f: 508 setup_force_cpu_cap(X86_FEATURE_ZEN3); 509 break; 510 case 0x10 ... 0x1f: 511 case 0x60 ... 0xaf: 512 setup_force_cpu_cap(X86_FEATURE_ZEN4); 513 break; 514 default: 515 goto warn; 516 } 517 break; 518 519 case 0x1a: 520 switch (c->x86_model) { 521 case 0x00 ... 0x2f: 522 case 0x40 ... 0x4f: 523 case 0x60 ... 0x7f: 524 case 0xd0 ... 0xd7: 525 setup_force_cpu_cap(X86_FEATURE_ZEN5); 526 break; 527 case 0x50 ... 0x5f: 528 case 0x80 ... 0xaf: 529 case 0xc0 ... 0xcf: 530 case 0xd8 ... 0xef: 531 setup_force_cpu_cap(X86_FEATURE_ZEN6); 532 break; 533 default: 534 goto warn; 535 } 536 break; 537 538 default: 539 break; 540 } 541 542 bsp_determine_snp(c); 543 tsa_init(c); 544 545 if (cpu_has(c, X86_FEATURE_GP_ON_USER_CPUID)) 546 setup_force_cpu_cap(X86_FEATURE_CPUID_FAULT); 547 548 return; 549 550 warn: 551 WARN_ONCE(1, "Family 0x%x, model: 0x%x??\n", c->x86, c->x86_model); 552 } 553 554 static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) 555 { 556 u64 msr; 557 558 /* 559 * Mark using WBINVD is needed during kexec on processors that 560 * support SME. This provides support for performing a successful 561 * kexec when going from SME inactive to SME active (or vice-versa). 562 * 563 * The cache must be cleared so that if there are entries with the 564 * same physical address, both with and without the encryption bit, 565 * they don't race each other when flushed and potentially end up 566 * with the wrong entry being committed to memory. 567 * 568 * Test the CPUID bit directly because with mem_encrypt=off the 569 * BSP will clear the X86_FEATURE_SME bit and the APs will not 570 * see it set after that. 571 */ 572 if (c->extended_cpuid_level >= 0x8000001f && (cpuid_eax(0x8000001f) & BIT(0))) 573 __this_cpu_write(cache_state_incoherent, true); 574 575 /* 576 * BIOS support is required for SME and SEV. 577 * For SME: If BIOS has enabled SME then adjust x86_phys_bits by 578 * the SME physical address space reduction value. 579 * If BIOS has not enabled SME then don't advertise the 580 * SME feature (set in scattered.c). 581 * If the kernel has not enabled SME via any means then 582 * don't advertise the SME feature. 583 * For SEV: If BIOS has not enabled SEV then don't advertise SEV and 584 * any additional functionality based on it. 585 * 586 * In all cases, since support for SME and SEV requires long mode, 587 * don't advertise the feature under CONFIG_X86_32. 588 */ 589 if (cpu_has(c, X86_FEATURE_SME) || cpu_has(c, X86_FEATURE_SEV)) { 590 /* Check if memory encryption is enabled */ 591 rdmsrq(MSR_AMD64_SYSCFG, msr); 592 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT)) 593 goto clear_all; 594 595 /* 596 * Always adjust physical address bits. Even though this 597 * will be a value above 32-bits this is still done for 598 * CONFIG_X86_32 so that accurate values are reported. 599 */ 600 c->x86_phys_bits -= (cpuid_ebx(0x8000001f) >> 6) & 0x3f; 601 602 if (IS_ENABLED(CONFIG_X86_32)) 603 goto clear_all; 604 605 if (!sme_me_mask) 606 setup_clear_cpu_cap(X86_FEATURE_SME); 607 608 rdmsrq(MSR_K7_HWCR, msr); 609 if (!(msr & MSR_K7_HWCR_SMMLOCK)) 610 goto clear_sev; 611 612 return; 613 614 clear_all: 615 setup_clear_cpu_cap(X86_FEATURE_SME); 616 clear_sev: 617 setup_clear_cpu_cap(X86_FEATURE_SEV); 618 setup_clear_cpu_cap(X86_FEATURE_SEV_ES); 619 setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); 620 } 621 } 622 623 static void early_init_amd(struct cpuinfo_x86 *c) 624 { 625 u64 val; 626 627 if (c->x86 >= 0xf) 628 set_cpu_cap(c, X86_FEATURE_K8); 629 630 rdmsrq_safe(MSR_AMD64_PATCH_LEVEL, &val); 631 c->microcode = (u32)val; 632 633 /* 634 * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate 635 * with P/T states and does not stop in deep C-states 636 */ 637 if (c->x86_power & (1 << 8)) { 638 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); 639 set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC); 640 } 641 642 /* Bit 12 of 8000_0007 edx is accumulated power mechanism. */ 643 if (c->x86_power & BIT(12)) 644 set_cpu_cap(c, X86_FEATURE_ACC_POWER); 645 646 /* Bit 14 indicates the Runtime Average Power Limit interface. */ 647 if (c->x86_power & BIT(14)) 648 set_cpu_cap(c, X86_FEATURE_RAPL); 649 650 #ifdef CONFIG_X86_64 651 set_cpu_cap(c, X86_FEATURE_SYSCALL32); 652 #else 653 /* Set MTRR capability flag if appropriate */ 654 if (c->x86 == 5) 655 if (c->x86_model == 13 || c->x86_model == 9 || 656 (c->x86_model == 8 && c->x86_stepping >= 8)) 657 set_cpu_cap(c, X86_FEATURE_K6_MTRR); 658 #endif 659 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_PCI) 660 /* 661 * ApicID can always be treated as an 8-bit value for AMD APIC versions 662 * >= 0x10, but even old K8s came out of reset with version 0x10. So, we 663 * can safely set X86_FEATURE_EXTD_APICID unconditionally for families 664 * after 16h. 665 */ 666 if (boot_cpu_has(X86_FEATURE_APIC)) { 667 if (c->x86 > 0x16) 668 set_cpu_cap(c, X86_FEATURE_EXTD_APICID); 669 else if (c->x86 >= 0xf) { 670 /* check CPU config space for extended APIC ID */ 671 unsigned int val; 672 673 val = read_pci_config(0, 24, 0, 0x68); 674 if ((val >> 17 & 0x3) == 0x3) 675 set_cpu_cap(c, X86_FEATURE_EXTD_APICID); 676 } 677 } 678 #endif 679 680 /* 681 * This is only needed to tell the kernel whether to use VMCALL 682 * and VMMCALL. VMMCALL is never executed except under virt, so 683 * we can set it unconditionally. 684 */ 685 set_cpu_cap(c, X86_FEATURE_VMMCALL); 686 687 /* F16h erratum 793, CVE-2013-6885 */ 688 if (c->x86 == 0x16 && c->x86_model <= 0xf) 689 msr_set_bit(MSR_AMD64_LS_CFG, 15); 690 691 early_detect_mem_encrypt(c); 692 693 if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && !cpu_has(c, X86_FEATURE_IBPB_BRTYPE)) { 694 if (c->x86 == 0x17 && boot_cpu_has(X86_FEATURE_AMD_IBPB)) 695 setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE); 696 else if (c->x86 >= 0x19 && !wrmsrq_safe(MSR_IA32_PRED_CMD, PRED_CMD_SBPB)) { 697 setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE); 698 setup_force_cpu_cap(X86_FEATURE_SBPB); 699 } 700 } 701 } 702 703 static void init_amd_k8(struct cpuinfo_x86 *c) 704 { 705 u32 level; 706 u64 value; 707 708 /* On C+ stepping K8 rep microcode works well for copy/memset */ 709 level = cpuid_eax(1); 710 if ((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58) 711 set_cpu_cap(c, X86_FEATURE_REP_GOOD); 712 713 /* 714 * Some BIOSes incorrectly force this feature, but only K8 revision D 715 * (model = 0x14) and later actually support it. 716 * (AMD Erratum #110, docId: 25759). 717 */ 718 if (c->x86_model < 0x14 && cpu_has(c, X86_FEATURE_LAHF_LM) && !cpu_has(c, X86_FEATURE_HYPERVISOR)) { 719 clear_cpu_cap(c, X86_FEATURE_LAHF_LM); 720 if (!rdmsrq_amd_safe(0xc001100d, &value)) { 721 value &= ~BIT_64(32); 722 wrmsrq_amd_safe(0xc001100d, value); 723 } 724 } 725 726 if (!c->x86_model_id[0]) 727 strscpy(c->x86_model_id, "Hammer"); 728 729 #ifdef CONFIG_SMP 730 /* 731 * Disable TLB flush filter by setting HWCR.FFDIS on K8 732 * bit 6 of msr C001_0015 733 * 734 * Errata 63 for SH-B3 steppings 735 * Errata 122 for all steppings (F+ have it disabled by default) 736 */ 737 msr_set_bit(MSR_K7_HWCR, 6); 738 #endif 739 set_cpu_bug(c, X86_BUG_SWAPGS_FENCE); 740 741 /* 742 * Check models and steppings affected by erratum 400. This is 743 * used to select the proper idle routine and to enable the 744 * check whether the machine is affected in arch_post_acpi_subsys_init() 745 * which sets the X86_BUG_AMD_APIC_C1E bug depending on the MSR check. 746 */ 747 if (c->x86_model > 0x41 || 748 (c->x86_model == 0x41 && c->x86_stepping >= 0x2)) 749 setup_force_cpu_bug(X86_BUG_AMD_E400); 750 } 751 752 static void init_amd_gh(struct cpuinfo_x86 *c) 753 { 754 #ifdef CONFIG_MMCONF_FAM10H 755 /* do this for boot cpu */ 756 if (c == &boot_cpu_data) 757 check_enable_amd_mmconf_dmi(); 758 759 fam10h_check_enable_mmcfg(); 760 #endif 761 762 /* 763 * Disable GART TLB Walk Errors on Fam10h. We do this here because this 764 * is always needed when GART is enabled, even in a kernel which has no 765 * MCE support built in. BIOS should disable GartTlbWlk Errors already. 766 * If it doesn't, we do it here as suggested by the BKDG. 767 * 768 * Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=33012 769 */ 770 msr_set_bit(MSR_AMD64_MCx_MASK(4), 10); 771 772 /* 773 * On family 10h BIOS may not have properly enabled WC+ support, causing 774 * it to be converted to CD memtype. This may result in performance 775 * degradation for certain nested-paging guests. Prevent this conversion 776 * by clearing bit 24 in MSR_AMD64_BU_CFG2. 777 * 778 * NOTE: we want to use the _safe accessors so as not to #GP kvm 779 * guests on older kvm hosts. 780 */ 781 msr_clear_bit(MSR_AMD64_BU_CFG2, 24); 782 783 set_cpu_bug(c, X86_BUG_AMD_TLB_MMATCH); 784 785 /* 786 * Check models and steppings affected by erratum 400. This is 787 * used to select the proper idle routine and to enable the 788 * check whether the machine is affected in arch_post_acpi_subsys_init() 789 * which sets the X86_BUG_AMD_APIC_C1E bug depending on the MSR check. 790 */ 791 if (c->x86_model > 0x2 || 792 (c->x86_model == 0x2 && c->x86_stepping >= 0x1)) 793 setup_force_cpu_bug(X86_BUG_AMD_E400); 794 } 795 796 static void init_amd_ln(struct cpuinfo_x86 *c) 797 { 798 /* 799 * Apply erratum 665 fix unconditionally so machines without a BIOS 800 * fix work. 801 */ 802 msr_set_bit(MSR_AMD64_DE_CFG, 31); 803 } 804 805 static bool rdrand_force; 806 807 static int __init rdrand_cmdline(char *str) 808 { 809 if (!str) 810 return -EINVAL; 811 812 if (!strcmp(str, "force")) 813 rdrand_force = true; 814 else 815 return -EINVAL; 816 817 return 0; 818 } 819 early_param("rdrand", rdrand_cmdline); 820 821 static void clear_rdrand_cpuid_bit(struct cpuinfo_x86 *c) 822 { 823 /* 824 * Saving of the MSR used to hide the RDRAND support during 825 * suspend/resume is done by arch/x86/power/cpu.c, which is 826 * dependent on CONFIG_PM_SLEEP. 827 */ 828 if (!IS_ENABLED(CONFIG_PM_SLEEP)) 829 return; 830 831 /* 832 * The self-test can clear X86_FEATURE_RDRAND, so check for 833 * RDRAND support using the CPUID function directly. 834 */ 835 if (!(cpuid_ecx(1) & BIT(30)) || rdrand_force) 836 return; 837 838 msr_clear_bit(MSR_AMD64_CPUID_FN_1, 62); 839 840 /* 841 * Verify that the CPUID change has occurred in case the kernel is 842 * running virtualized and the hypervisor doesn't support the MSR. 843 */ 844 if (cpuid_ecx(1) & BIT(30)) { 845 pr_info_once("BIOS may not properly restore RDRAND after suspend, but hypervisor does not support hiding RDRAND via CPUID.\n"); 846 return; 847 } 848 849 clear_cpu_cap(c, X86_FEATURE_RDRAND); 850 pr_info_once("BIOS may not properly restore RDRAND after suspend, hiding RDRAND via CPUID. Use rdrand=force to reenable.\n"); 851 } 852 853 static void init_amd_jg(struct cpuinfo_x86 *c) 854 { 855 /* 856 * Some BIOS implementations do not restore proper RDRAND support 857 * across suspend and resume. Check on whether to hide the RDRAND 858 * instruction support via CPUID. 859 */ 860 clear_rdrand_cpuid_bit(c); 861 } 862 863 static void init_amd_bd(struct cpuinfo_x86 *c) 864 { 865 u64 value; 866 867 /* 868 * The way access filter has a performance penalty on some workloads. 869 * Disable it on the affected CPUs. 870 */ 871 if ((c->x86_model >= 0x02) && (c->x86_model < 0x20)) { 872 if (!rdmsrq_safe(MSR_F15H_IC_CFG, &value) && !(value & 0x1E)) { 873 value |= 0x1E; 874 wrmsrq_safe(MSR_F15H_IC_CFG, value); 875 } 876 } 877 878 /* 879 * Some BIOS implementations do not restore proper RDRAND support 880 * across suspend and resume. Check on whether to hide the RDRAND 881 * instruction support via CPUID. 882 */ 883 clear_rdrand_cpuid_bit(c); 884 } 885 886 static const struct x86_cpu_id erratum_1386_microcode[] = { 887 ZEN_MODEL_STEP_UCODE(0x17, 0x01, 0x2, 0x0800126e), 888 ZEN_MODEL_STEP_UCODE(0x17, 0x31, 0x0, 0x08301052), 889 {} 890 }; 891 892 static void fix_erratum_1386(struct cpuinfo_x86 *c) 893 { 894 /* 895 * Work around Erratum 1386. The XSAVES instruction malfunctions in 896 * certain circumstances on Zen1/2 uarch, and not all parts have had 897 * updated microcode at the time of writing (March 2023). 898 * 899 * Affected parts all have no supervisor XSAVE states, meaning that 900 * the XSAVEC instruction (which works fine) is equivalent. 901 * 902 * Clear the feature flag only on microcode revisions which 903 * don't have the fix. 904 */ 905 if (x86_match_min_microcode_rev(erratum_1386_microcode)) 906 return; 907 908 clear_cpu_cap(c, X86_FEATURE_XSAVES); 909 } 910 911 void init_spectral_chicken(struct cpuinfo_x86 *c) 912 { 913 #ifdef CONFIG_MITIGATION_UNRET_ENTRY 914 /* 915 * On Zen2 we offer this chicken (bit) on the altar of Speculation. 916 * 917 * This suppresses speculation from the middle of a basic block, i.e. it 918 * suppresses non-branch predictions. 919 */ 920 if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) 921 msr_set_bit(MSR_ZEN2_SPECTRAL_CHICKEN, MSR_ZEN2_SPECTRAL_CHICKEN_BIT); 922 #endif 923 } 924 925 static void init_amd_zen_common(void) 926 { 927 setup_force_cpu_cap(X86_FEATURE_ZEN); 928 #ifdef CONFIG_NUMA 929 node_reclaim_distance = 32; 930 #endif 931 } 932 933 static void init_amd_zen1(struct cpuinfo_x86 *c) 934 { 935 fix_erratum_1386(c); 936 937 /* Fix up CPUID bits, but only if not virtualised. */ 938 if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) { 939 940 /* Erratum 1076: CPB feature bit not being set in CPUID. */ 941 if (!cpu_has(c, X86_FEATURE_CPB)) 942 set_cpu_cap(c, X86_FEATURE_CPB); 943 } 944 945 pr_notice_once("AMD Zen1 DIV0 bug detected. Disable SMT for full protection.\n"); 946 setup_force_cpu_bug(X86_BUG_DIV0); 947 948 /* 949 * Turn off the Instructions Retired free counter on machines that are 950 * susceptible to erratum #1054 "Instructions Retired Performance 951 * Counter May Be Inaccurate". 952 */ 953 if (c->x86_model < 0x30) { 954 msr_clear_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT); 955 clear_cpu_cap(c, X86_FEATURE_IRPERF); 956 } 957 958 pr_notice_once("AMD Zen1 FPDSS bug detected, enabling mitigation.\n"); 959 msr_set_bit(MSR_AMD64_FP_CFG, MSR_AMD64_FP_CFG_ZEN1_DENORM_FIX_BIT); 960 } 961 962 static const struct x86_cpu_id amd_zenbleed_microcode[] = { 963 ZEN_MODEL_STEP_UCODE(0x17, 0x31, 0x0, 0x0830107b), 964 ZEN_MODEL_STEP_UCODE(0x17, 0x60, 0x1, 0x0860010c), 965 ZEN_MODEL_STEP_UCODE(0x17, 0x68, 0x1, 0x08608107), 966 ZEN_MODEL_STEP_UCODE(0x17, 0x71, 0x0, 0x08701033), 967 ZEN_MODEL_STEP_UCODE(0x17, 0xa0, 0x0, 0x08a00009), 968 {} 969 }; 970 971 static void zen2_zenbleed_check(struct cpuinfo_x86 *c) 972 { 973 if (cpu_has(c, X86_FEATURE_HYPERVISOR)) 974 return; 975 976 if (!cpu_has(c, X86_FEATURE_AVX)) 977 return; 978 979 if (!x86_match_min_microcode_rev(amd_zenbleed_microcode)) { 980 pr_notice_once("Zenbleed: please update your microcode for the most optimal fix\n"); 981 msr_set_bit(MSR_AMD64_DE_CFG, MSR_AMD64_DE_CFG_ZEN2_FP_BACKUP_FIX_BIT); 982 } else { 983 msr_clear_bit(MSR_AMD64_DE_CFG, MSR_AMD64_DE_CFG_ZEN2_FP_BACKUP_FIX_BIT); 984 } 985 } 986 987 static void init_amd_zen2(struct cpuinfo_x86 *c) 988 { 989 init_spectral_chicken(c); 990 fix_erratum_1386(c); 991 zen2_zenbleed_check(c); 992 993 /* Disable RDSEED on AMD Cyan Skillfish because of an error. */ 994 if (c->x86_model == 0x47 && c->x86_stepping == 0x0) { 995 clear_cpu_cap(c, X86_FEATURE_RDSEED); 996 msr_clear_bit(MSR_AMD64_CPUID_FN_7, 18); 997 pr_emerg("RDSEED is not reliable on this platform; disabling.\n"); 998 } 999 1000 /* Correct misconfigured CPUID on some clients. */ 1001 clear_cpu_cap(c, X86_FEATURE_INVLPGB); 1002 1003 if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) 1004 msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN2_BP_CFG_BUG_FIX_BIT); 1005 } 1006 1007 static void init_amd_zen3(struct cpuinfo_x86 *c) 1008 { 1009 if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) { 1010 /* 1011 * Zen3 (Fam19 model < 0x10) parts are not susceptible to 1012 * Branch Type Confusion, but predate the allocation of the 1013 * BTC_NO bit. 1014 */ 1015 if (!cpu_has(c, X86_FEATURE_BTC_NO)) 1016 set_cpu_cap(c, X86_FEATURE_BTC_NO); 1017 } 1018 } 1019 1020 static void init_amd_zen4(struct cpuinfo_x86 *c) 1021 { 1022 if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) 1023 msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_SHARED_BTB_FIX_BIT); 1024 1025 /* 1026 * These Zen4 SoCs advertise support for virtualized VMLOAD/VMSAVE 1027 * in some BIOS versions but they can lead to random host reboots. 1028 */ 1029 switch (c->x86_model) { 1030 case 0x18 ... 0x1f: 1031 case 0x60 ... 0x7f: 1032 clear_cpu_cap(c, X86_FEATURE_V_VMSAVE_VMLOAD); 1033 break; 1034 } 1035 } 1036 1037 static const struct x86_cpu_id zen5_rdseed_microcode[] = { 1038 ZEN_MODEL_STEP_UCODE(0x1a, 0x02, 0x1, 0x0b00215a), 1039 ZEN_MODEL_STEP_UCODE(0x1a, 0x08, 0x1, 0x0b008121), 1040 ZEN_MODEL_STEP_UCODE(0x1a, 0x11, 0x0, 0x0b101054), 1041 ZEN_MODEL_STEP_UCODE(0x1a, 0x24, 0x0, 0x0b204037), 1042 ZEN_MODEL_STEP_UCODE(0x1a, 0x44, 0x0, 0x0b404035), 1043 ZEN_MODEL_STEP_UCODE(0x1a, 0x44, 0x1, 0x0b404108), 1044 ZEN_MODEL_STEP_UCODE(0x1a, 0x60, 0x0, 0x0b600037), 1045 ZEN_MODEL_STEP_UCODE(0x1a, 0x68, 0x0, 0x0b608038), 1046 ZEN_MODEL_STEP_UCODE(0x1a, 0x70, 0x0, 0x0b700037), 1047 {}, 1048 }; 1049 1050 static void init_amd_zen5(struct cpuinfo_x86 *c) 1051 { 1052 if (!x86_match_min_microcode_rev(zen5_rdseed_microcode)) { 1053 clear_cpu_cap(c, X86_FEATURE_RDSEED); 1054 msr_clear_bit(MSR_AMD64_CPUID_FN_7, 18); 1055 pr_emerg_once("RDSEED32 is broken. Disabling the corresponding CPUID bit.\n"); 1056 } 1057 1058 if (cpu_has(c, X86_FEATURE_INVLPGB) && 1059 !cpu_has(c, X86_FEATURE_TLBSYNC_SFW_NO)) 1060 set_cpu_bug(c, X86_BUG_TLBI); 1061 } 1062 1063 static void init_amd(struct cpuinfo_x86 *c) 1064 { 1065 u64 vm_cr; 1066 1067 early_init_amd(c); 1068 1069 if (c->x86 >= 0x10) 1070 set_cpu_cap(c, X86_FEATURE_REP_GOOD); 1071 1072 /* AMD FSRM also implies FSRS */ 1073 if (cpu_has(c, X86_FEATURE_FSRM)) 1074 set_cpu_cap(c, X86_FEATURE_FSRS); 1075 1076 /* K6s reports MCEs but don't actually have all the MSRs */ 1077 if (c->x86 < 6) 1078 clear_cpu_cap(c, X86_FEATURE_MCE); 1079 1080 switch (c->x86) { 1081 case 4: init_amd_k5(c); break; 1082 case 5: init_amd_k6(c); break; 1083 case 6: init_amd_k7(c); break; 1084 case 0xf: init_amd_k8(c); break; 1085 case 0x10: init_amd_gh(c); break; 1086 case 0x12: init_amd_ln(c); break; 1087 case 0x15: init_amd_bd(c); break; 1088 case 0x16: init_amd_jg(c); break; 1089 } 1090 1091 /* 1092 * Save up on some future enablement work and do common Zen 1093 * settings. 1094 */ 1095 if (c->x86 >= 0x17) 1096 init_amd_zen_common(); 1097 1098 if (boot_cpu_has(X86_FEATURE_ZEN1)) 1099 init_amd_zen1(c); 1100 else if (boot_cpu_has(X86_FEATURE_ZEN2)) 1101 init_amd_zen2(c); 1102 else if (boot_cpu_has(X86_FEATURE_ZEN3)) 1103 init_amd_zen3(c); 1104 else if (boot_cpu_has(X86_FEATURE_ZEN4)) 1105 init_amd_zen4(c); 1106 else if (boot_cpu_has(X86_FEATURE_ZEN5)) 1107 init_amd_zen5(c); 1108 1109 /* 1110 * Enable workaround for FXSAVE leak on CPUs 1111 * without a XSaveErPtr feature 1112 */ 1113 if ((c->x86 >= 6) && (!cpu_has(c, X86_FEATURE_XSAVEERPTR))) 1114 set_cpu_bug(c, X86_BUG_FXSAVE_LEAK); 1115 1116 cpu_detect_cache_sizes(c); 1117 1118 srat_detect_node(c); 1119 1120 init_amd_cacheinfo(c); 1121 1122 if (cpu_has(c, X86_FEATURE_SVM)) { 1123 rdmsrq(MSR_VM_CR, vm_cr); 1124 if (vm_cr & SVM_VM_CR_SVM_DIS_MASK) { 1125 pr_notice_once("SVM disabled (by BIOS) in MSR_VM_CR\n"); 1126 clear_cpu_cap(c, X86_FEATURE_SVM); 1127 } 1128 } 1129 1130 if (!cpu_has(c, X86_FEATURE_LFENCE_RDTSC) && cpu_has(c, X86_FEATURE_XMM2)) { 1131 /* 1132 * Use LFENCE for execution serialization. On families which 1133 * don't have that MSR, LFENCE is already serializing. 1134 * msr_set_bit() uses the safe accessors, too, even if the MSR 1135 * is not present. 1136 */ 1137 msr_set_bit(MSR_AMD64_DE_CFG, 1138 MSR_AMD64_DE_CFG_LFENCE_SERIALIZE_BIT); 1139 1140 /* A serializing LFENCE stops RDTSC speculation */ 1141 set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC); 1142 } 1143 1144 /* 1145 * Family 0x12 and above processors have APIC timer 1146 * running in deep C states. 1147 */ 1148 if (c->x86 > 0x11) 1149 set_cpu_cap(c, X86_FEATURE_ARAT); 1150 1151 /* 3DNow or LM implies PREFETCHW */ 1152 if (!cpu_has(c, X86_FEATURE_3DNOWPREFETCH)) 1153 if (cpu_has(c, X86_FEATURE_3DNOW) || cpu_has(c, X86_FEATURE_LM)) 1154 set_cpu_cap(c, X86_FEATURE_3DNOWPREFETCH); 1155 1156 /* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */ 1157 if (!cpu_feature_enabled(X86_FEATURE_XENPV)) 1158 set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS); 1159 1160 /* Enable the Instructions Retired free counter */ 1161 if (cpu_has(c, X86_FEATURE_IRPERF)) 1162 msr_set_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT); 1163 1164 check_null_seg_clears_base(c); 1165 1166 /* 1167 * Make sure EFER[AIBRSE - Automatic IBRS Enable] is set. The APs are brought up 1168 * using the trampoline code and as part of it, MSR_EFER gets prepared there in 1169 * order to be replicated onto them. Regardless, set it here again, if not set, 1170 * to protect against any future refactoring/code reorganization which might 1171 * miss setting this important bit. 1172 */ 1173 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) && 1174 cpu_has(c, X86_FEATURE_AUTOIBRS)) 1175 WARN_ON_ONCE(msr_set_bit(MSR_EFER, _EFER_AUTOIBRS) < 0); 1176 1177 /* AMD CPUs don't need fencing after x2APIC/TSC_DEADLINE MSR writes. */ 1178 clear_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE); 1179 1180 /* Enable Translation Cache Extension */ 1181 if (cpu_has(c, X86_FEATURE_TCE)) 1182 msr_set_bit(MSR_EFER, _EFER_TCE); 1183 } 1184 1185 #ifdef CONFIG_X86_32 1186 static unsigned int amd_size_cache(struct cpuinfo_x86 *c, unsigned int size) 1187 { 1188 /* AMD errata T13 (order #21922) */ 1189 if (c->x86 == 6) { 1190 /* Duron Rev A0 */ 1191 if (c->x86_model == 3 && c->x86_stepping == 0) 1192 size = 64; 1193 /* Tbird rev A1/A2 */ 1194 if (c->x86_model == 4 && 1195 (c->x86_stepping == 0 || c->x86_stepping == 1)) 1196 size = 256; 1197 } 1198 return size; 1199 } 1200 #endif 1201 1202 static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c) 1203 { 1204 u32 ebx, eax, ecx, edx, shift, tmp; 1205 u16 mask = 0xfff; 1206 1207 if (c->x86 < 0xf) 1208 return; 1209 1210 if (c->extended_cpuid_level < 0x80000006) 1211 return; 1212 1213 shift = !!cpu_has(c, X86_FEATURE_L2_TLB_SIZE_X32) * 5; 1214 1215 cpuid(0x80000006, &eax, &ebx, &ecx, &edx); 1216 1217 tlb_lld_4k = ((ebx >> 16) & mask) << shift; 1218 tlb_lli_4k = (ebx & mask) << shift; 1219 1220 /* 1221 * K8 doesn't have 2M/4M entries in the L2 TLB so read out the L1 TLB 1222 * characteristics from the CPUID function 0x80000005 instead. 1223 */ 1224 if (c->x86 == 0xf) { 1225 cpuid(0x80000005, &eax, &ebx, &ecx, &edx); 1226 mask = 0xff; 1227 } 1228 1229 /* Handle DTLB 2M and 4M sizes, fall back to L1 if L2 is disabled */ 1230 tmp = ((eax >> 16) & mask) << shift; 1231 if (!tmp) 1232 tlb_lld_2m = (cpuid_eax(0x80000005) >> 16) & 0xff; 1233 else 1234 tlb_lld_2m = tmp; 1235 1236 /* a 4M entry uses two 2M entries */ 1237 tlb_lld_4m = tlb_lld_2m >> 1; 1238 1239 /* Handle ITLB 2M and 4M sizes, fall back to L1 if L2 is disabled */ 1240 tmp = (eax & mask) << shift; 1241 if (!tmp) { 1242 /* Erratum 658 */ 1243 if (c->x86 == 0x15 && c->x86_model <= 0x1f) { 1244 tlb_lli_2m = 1024; 1245 } else { 1246 cpuid(0x80000005, &eax, &ebx, &ecx, &edx); 1247 tlb_lli_2m = eax & 0xff; 1248 } 1249 } else { 1250 tlb_lli_2m = tmp; 1251 } 1252 1253 tlb_lli_4m = tlb_lli_2m >> 1; 1254 1255 /* Max number of pages INVLPGB can invalidate in one shot */ 1256 if (cpu_has(c, X86_FEATURE_INVLPGB)) 1257 invlpgb_count_max = (cpuid_edx(0x80000008) & 0xffff) + 1; 1258 } 1259 1260 static const struct cpu_dev amd_cpu_dev = { 1261 .c_vendor = "AMD", 1262 .c_ident = { "AuthenticAMD" }, 1263 #ifdef CONFIG_X86_32 1264 .legacy_models = { 1265 { .family = 4, .model_names = 1266 { 1267 [3] = "486 DX/2", 1268 [7] = "486 DX/2-WB", 1269 [8] = "486 DX/4", 1270 [9] = "486 DX/4-WB", 1271 [14] = "Am5x86-WT", 1272 [15] = "Am5x86-WB" 1273 } 1274 }, 1275 }, 1276 .legacy_cache_size = amd_size_cache, 1277 #endif 1278 .c_early_init = early_init_amd, 1279 .c_detect_tlb = cpu_detect_tlb_amd, 1280 .c_bsp_init = bsp_init_amd, 1281 .c_init = init_amd, 1282 .c_x86_vendor = X86_VENDOR_AMD, 1283 }; 1284 1285 cpu_dev_register(amd_cpu_dev); 1286 1287 static DEFINE_PER_CPU_READ_MOSTLY(unsigned long[4], amd_dr_addr_mask); 1288 1289 static unsigned int amd_msr_dr_addr_masks[] = { 1290 MSR_F16H_DR0_ADDR_MASK, 1291 MSR_F16H_DR1_ADDR_MASK, 1292 MSR_F16H_DR1_ADDR_MASK + 1, 1293 MSR_F16H_DR1_ADDR_MASK + 2 1294 }; 1295 1296 void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr) 1297 { 1298 int cpu = smp_processor_id(); 1299 1300 if (!cpu_feature_enabled(X86_FEATURE_BPEXT)) 1301 return; 1302 1303 if (WARN_ON_ONCE(dr >= ARRAY_SIZE(amd_msr_dr_addr_masks))) 1304 return; 1305 1306 if (per_cpu(amd_dr_addr_mask, cpu)[dr] == mask) 1307 return; 1308 1309 wrmsrq(amd_msr_dr_addr_masks[dr], mask); 1310 per_cpu(amd_dr_addr_mask, cpu)[dr] = mask; 1311 } 1312 1313 unsigned long amd_get_dr_addr_mask(unsigned int dr) 1314 { 1315 if (!cpu_feature_enabled(X86_FEATURE_BPEXT)) 1316 return 0; 1317 1318 if (WARN_ON_ONCE(dr >= ARRAY_SIZE(amd_msr_dr_addr_masks))) 1319 return 0; 1320 1321 return per_cpu(amd_dr_addr_mask[dr], smp_processor_id()); 1322 } 1323 EXPORT_SYMBOL_FOR_KVM(amd_get_dr_addr_mask); 1324 1325 static void zenbleed_check_cpu(void *unused) 1326 { 1327 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); 1328 1329 zen2_zenbleed_check(c); 1330 } 1331 1332 void amd_check_microcode(void) 1333 { 1334 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) 1335 return; 1336 1337 if (cpu_feature_enabled(X86_FEATURE_ZEN2)) 1338 on_each_cpu(zenbleed_check_cpu, NULL, 1); 1339 } 1340 1341 static const char * const s5_reset_reason_txt[] = { 1342 [0] = "thermal pin BP_THERMTRIP_L was tripped", 1343 [1] = "power button was pressed for 4 seconds", 1344 [2] = "shutdown pin was tripped", 1345 [4] = "remote ASF power off command was received", 1346 [9] = "internal CPU thermal limit was tripped", 1347 [16] = "system reset pin BP_SYS_RST_L was tripped", 1348 [17] = "software issued PCI reset", 1349 [18] = "software wrote 0x4 to reset control register 0xCF9", 1350 [19] = "software wrote 0x6 to reset control register 0xCF9", 1351 [20] = "software wrote 0xE to reset control register 0xCF9", 1352 [21] = "ACPI power state transition occurred", 1353 [22] = "keyboard reset pin KB_RST_L was tripped", 1354 [23] = "internal CPU shutdown event occurred", 1355 [24] = "system failed to boot before failed boot timer expired", 1356 [25] = "hardware watchdog timer expired", 1357 [26] = "remote ASF reset command was received", 1358 [27] = "an uncorrected error caused a data fabric sync flood event", 1359 [29] = "FCH and MP1 failed warm reset handshake", 1360 [30] = "a parity error occurred", 1361 [31] = "a software sync flood event occurred", 1362 }; 1363 1364 static __init int print_s5_reset_status_mmio(void) 1365 { 1366 void __iomem *addr; 1367 u32 value; 1368 int i; 1369 1370 if (!cpu_feature_enabled(X86_FEATURE_ZEN) || 1371 cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) 1372 return 0; 1373 1374 addr = ioremap(FCH_PM_BASE + FCH_PM_S5_RESET_STATUS, sizeof(value)); 1375 if (!addr) 1376 return 0; 1377 1378 value = ioread32(addr); 1379 1380 /* Value with "all bits set" is an error response and should be ignored. */ 1381 if (value == U32_MAX) { 1382 iounmap(addr); 1383 return 0; 1384 } 1385 1386 /* 1387 * Clear all reason bits so they won't be retained if the next reset 1388 * does not update the register. Besides, some bits are never cleared by 1389 * hardware so it's software's responsibility to clear them. 1390 * 1391 * Writing the value back effectively clears all reason bits as they are 1392 * write-1-to-clear. 1393 */ 1394 iowrite32(value, addr); 1395 iounmap(addr); 1396 1397 for (i = 0; i < ARRAY_SIZE(s5_reset_reason_txt); i++) { 1398 if (!(value & BIT(i))) 1399 continue; 1400 1401 if (s5_reset_reason_txt[i]) { 1402 pr_info("x86/amd: Previous system reset reason [0x%08x]: %s\n", 1403 value, s5_reset_reason_txt[i]); 1404 } 1405 } 1406 1407 return 0; 1408 } 1409 late_initcall(print_s5_reset_status_mmio); 1410 1411 static void __init dmi_scan_additional(const struct dmi_header *d, void *p) 1412 { 1413 struct dmi_a_info *info = (struct dmi_a_info *)d; 1414 void *next, *end; 1415 1416 if (!IS_ENABLED(CONFIG_DMI)) 1417 return; 1418 1419 if (info->header.type != DMI_ENTRY_ADDITIONAL || 1420 info->header.length < DMI_A_INFO_MIN_SIZE || 1421 info->count < 1) 1422 return; 1423 1424 next = (void *)(info + 1); 1425 end = (void *)info + info->header.length; 1426 1427 do { 1428 struct dmi_a_info_entry *entry; 1429 const char *string_ptr; 1430 1431 entry = (struct dmi_a_info_entry *)next; 1432 1433 /* 1434 * Not much can be done to validate data. At least the entry 1435 * length shouldn't be 0. 1436 */ 1437 if (!entry->length) 1438 return; 1439 1440 string_ptr = dmi_string_nosave(&info->header, entry->str_num); 1441 1442 /* Sample string: AGESA!V9 StrixKrackanPI-FP8 1.1.0.0c */ 1443 if (!strncmp(string_ptr, "AGESA", 5)) { 1444 pr_info("AGESA: %s\n", string_ptr); 1445 break; 1446 } 1447 1448 next += entry->length; 1449 } while (end - next >= DMI_A_INFO_ENT_MIN_SIZE); 1450 } 1451 1452 static __init int print_dmi_agesa(void) 1453 { 1454 dmi_walk(dmi_scan_additional, NULL); 1455 return 0; 1456 } 1457 late_initcall(print_dmi_agesa); 1458