| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | #include <linux/export.h> |
|---|
| 2 | 3 | #include <linux/bitops.h> |
|---|
| 3 | 4 | #include <linux/elf.h> |
|---|
| .. | .. |
|---|
| 7 | 8 | #include <linux/sched.h> |
|---|
| 8 | 9 | #include <linux/sched/clock.h> |
|---|
| 9 | 10 | #include <linux/random.h> |
|---|
| 11 | +#include <linux/topology.h> |
|---|
| 10 | 12 | #include <asm/processor.h> |
|---|
| 11 | 13 | #include <asm/apic.h> |
|---|
| 12 | 14 | #include <asm/cacheinfo.h> |
|---|
| 13 | 15 | #include <asm/cpu.h> |
|---|
| 14 | 16 | #include <asm/spec-ctrl.h> |
|---|
| 15 | 17 | #include <asm/smp.h> |
|---|
| 18 | +#include <asm/numa.h> |
|---|
| 16 | 19 | #include <asm/pci-direct.h> |
|---|
| 17 | 20 | #include <asm/delay.h> |
|---|
| 21 | +#include <asm/debugreg.h> |
|---|
| 22 | +#include <asm/resctrl.h> |
|---|
| 18 | 23 | |
|---|
| 19 | 24 | #ifdef CONFIG_X86_64 |
|---|
| 20 | 25 | # include <asm/mmconfig.h> |
|---|
| .. | .. |
|---|
| 23 | 28 | |
|---|
| 24 | 29 | #include "cpu.h" |
|---|
| 25 | 30 | |
|---|
| 26 | | -static const int amd_erratum_383[]; |
|---|
| 27 | | -static const int amd_erratum_400[]; |
|---|
| 28 | | -static const int amd_erratum_1054[]; |
|---|
| 29 | | -static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum); |
|---|
| 30 | | - |
|---|
| 31 | 31 | /* |
|---|
| 32 | 32 | * nodes_per_socket: Stores the number of nodes per socket. |
|---|
| 33 | 33 | * Refer to Fam15h Models 00-0fh BKDG - CPUID Fn8000_001E_ECX |
|---|
| 34 | 34 | * Node Identifiers[10:8] |
|---|
| 35 | 35 | */ |
|---|
| 36 | 36 | static u32 nodes_per_socket = 1; |
|---|
| 37 | + |
|---|
| 38 | +/* |
|---|
| 39 | + * AMD errata checking |
|---|
| 40 | + * |
|---|
| 41 | + * Errata are defined as arrays of ints using the AMD_LEGACY_ERRATUM() or |
|---|
| 42 | + * AMD_OSVW_ERRATUM() macros. The latter is intended for newer errata that |
|---|
| 43 | + * have an OSVW id assigned, which it takes as first argument. Both take a |
|---|
| 44 | + * variable number of family-specific model-stepping ranges created by |
|---|
| 45 | + * AMD_MODEL_RANGE(). |
|---|
| 46 | + * |
|---|
| 47 | + * Example: |
|---|
| 48 | + * |
|---|
| 49 | + * const int amd_erratum_319[] = |
|---|
| 50 | + * AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0x4, 0x2), |
|---|
| 51 | + * AMD_MODEL_RANGE(0x10, 0x8, 0x0, 0x8, 0x0), |
|---|
| 52 | + * AMD_MODEL_RANGE(0x10, 0x9, 0x0, 0x9, 0x0)); |
|---|
| 53 | + */ |
|---|
| 54 | + |
|---|
| 55 | +#define AMD_LEGACY_ERRATUM(...) { -1, __VA_ARGS__, 0 } |
|---|
| 56 | +#define AMD_OSVW_ERRATUM(osvw_id, ...) { osvw_id, __VA_ARGS__, 0 } |
|---|
| 57 | +#define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \ |
|---|
| 58 | + ((f << 24) | (m_start << 16) | (s_start << 12) | (m_end << 4) | (s_end)) |
|---|
| 59 | +#define AMD_MODEL_RANGE_FAMILY(range) (((range) >> 24) & 0xff) |
|---|
| 60 | +#define AMD_MODEL_RANGE_START(range) (((range) >> 12) & 0xfff) |
|---|
| 61 | +#define AMD_MODEL_RANGE_END(range) ((range) & 0xfff) |
|---|
| 62 | + |
|---|
| 63 | +static const int amd_erratum_400[] = |
|---|
| 64 | + AMD_OSVW_ERRATUM(1, AMD_MODEL_RANGE(0xf, 0x41, 0x2, 0xff, 0xf), |
|---|
| 65 | + AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf)); |
|---|
| 66 | + |
|---|
| 67 | +static const int amd_erratum_383[] = |
|---|
| 68 | + AMD_OSVW_ERRATUM(3, AMD_MODEL_RANGE(0x10, 0, 0, 0xff, 0xf)); |
|---|
| 69 | + |
|---|
| 70 | +/* #1054: Instructions Retired Performance Counter May Be Inaccurate */ |
|---|
| 71 | +static const int amd_erratum_1054[] = |
|---|
| 72 | + AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x17, 0, 0, 0x2f, 0xf)); |
|---|
| 73 | + |
|---|
| 74 | +static const int amd_zenbleed[] = |
|---|
| 75 | + AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x17, 0x30, 0x0, 0x4f, 0xf), |
|---|
| 76 | + AMD_MODEL_RANGE(0x17, 0x60, 0x0, 0x7f, 0xf), |
|---|
| 77 | + AMD_MODEL_RANGE(0x17, 0x90, 0x0, 0x91, 0xf), |
|---|
| 78 | + AMD_MODEL_RANGE(0x17, 0xa0, 0x0, 0xaf, 0xf)); |
|---|
| 79 | + |
|---|
| 80 | +static const int amd_div0[] = |
|---|
| 81 | + AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x17, 0x00, 0x0, 0x2f, 0xf), |
|---|
| 82 | + AMD_MODEL_RANGE(0x17, 0x50, 0x0, 0x5f, 0xf)); |
|---|
| 83 | + |
|---|
| 84 | +static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum) |
|---|
| 85 | +{ |
|---|
| 86 | + int osvw_id = *erratum++; |
|---|
| 87 | + u32 range; |
|---|
| 88 | + u32 ms; |
|---|
| 89 | + |
|---|
| 90 | + if (osvw_id >= 0 && osvw_id < 65536 && |
|---|
| 91 | + cpu_has(cpu, X86_FEATURE_OSVW)) { |
|---|
| 92 | + u64 osvw_len; |
|---|
| 93 | + |
|---|
| 94 | + rdmsrl(MSR_AMD64_OSVW_ID_LENGTH, osvw_len); |
|---|
| 95 | + if (osvw_id < osvw_len) { |
|---|
| 96 | + u64 osvw_bits; |
|---|
| 97 | + |
|---|
| 98 | + rdmsrl(MSR_AMD64_OSVW_STATUS + (osvw_id >> 6), |
|---|
| 99 | + osvw_bits); |
|---|
| 100 | + return osvw_bits & (1ULL << (osvw_id & 0x3f)); |
|---|
| 101 | + } |
|---|
| 102 | + } |
|---|
| 103 | + |
|---|
| 104 | + /* OSVW unavailable or ID unknown, match family-model-stepping range */ |
|---|
| 105 | + ms = (cpu->x86_model << 4) | cpu->x86_stepping; |
|---|
| 106 | + while ((range = *erratum++)) |
|---|
| 107 | + if ((cpu->x86 == AMD_MODEL_RANGE_FAMILY(range)) && |
|---|
| 108 | + (ms >= AMD_MODEL_RANGE_START(range)) && |
|---|
| 109 | + (ms <= AMD_MODEL_RANGE_END(range))) |
|---|
| 110 | + return true; |
|---|
| 111 | + |
|---|
| 112 | + return false; |
|---|
| 113 | +} |
|---|
| 37 | 114 | |
|---|
| 38 | 115 | static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p) |
|---|
| 39 | 116 | { |
|---|
| .. | .. |
|---|
| 82 | 159 | * performance at the same time.. |
|---|
| 83 | 160 | */ |
|---|
| 84 | 161 | |
|---|
| 162 | +#ifdef CONFIG_X86_32 |
|---|
| 85 | 163 | extern __visible void vide(void); |
|---|
| 86 | | -__asm__(".globl vide\n" |
|---|
| 164 | +__asm__(".text\n" |
|---|
| 165 | + ".globl vide\n" |
|---|
| 87 | 166 | ".type vide, @function\n" |
|---|
| 88 | 167 | ".align 4\n" |
|---|
| 89 | 168 | "vide: ret\n"); |
|---|
| 169 | +#endif |
|---|
| 90 | 170 | |
|---|
| 91 | 171 | static void init_amd_k5(struct cpuinfo_x86 *c) |
|---|
| 92 | 172 | { |
|---|
| .. | .. |
|---|
| 314 | 394 | c->cpu_core_id %= cus_per_node; |
|---|
| 315 | 395 | } |
|---|
| 316 | 396 | |
|---|
| 317 | | - |
|---|
| 318 | | -static void amd_get_topology_early(struct cpuinfo_x86 *c) |
|---|
| 319 | | -{ |
|---|
| 320 | | - if (cpu_has(c, X86_FEATURE_TOPOEXT)) |
|---|
| 321 | | - smp_num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1; |
|---|
| 322 | | -} |
|---|
| 323 | | - |
|---|
| 324 | 397 | /* |
|---|
| 325 | 398 | * Fixup core topology information for |
|---|
| 326 | 399 | * (1) AMD multi-node processors |
|---|
| .. | .. |
|---|
| 329 | 402 | */ |
|---|
| 330 | 403 | static void amd_get_topology(struct cpuinfo_x86 *c) |
|---|
| 331 | 404 | { |
|---|
| 332 | | - u8 node_id; |
|---|
| 333 | 405 | int cpu = smp_processor_id(); |
|---|
| 334 | 406 | |
|---|
| 335 | 407 | /* get information required for multi-node processors */ |
|---|
| .. | .. |
|---|
| 339 | 411 | |
|---|
| 340 | 412 | cpuid(0x8000001e, &eax, &ebx, &ecx, &edx); |
|---|
| 341 | 413 | |
|---|
| 342 | | - node_id = ecx & 0xff; |
|---|
| 414 | + c->cpu_die_id = ecx & 0xff; |
|---|
| 343 | 415 | |
|---|
| 344 | 416 | if (c->x86 == 0x15) |
|---|
| 345 | 417 | c->cu_id = ebx & 0xff; |
|---|
| .. | .. |
|---|
| 359 | 431 | if (!err) |
|---|
| 360 | 432 | c->x86_coreid_bits = get_count_order(c->x86_max_cores); |
|---|
| 361 | 433 | |
|---|
| 362 | | - cacheinfo_amd_init_llc_id(c, cpu, node_id); |
|---|
| 434 | + cacheinfo_amd_init_llc_id(c, cpu); |
|---|
| 363 | 435 | |
|---|
| 364 | 436 | } else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) { |
|---|
| 365 | 437 | u64 value; |
|---|
| 366 | 438 | |
|---|
| 367 | 439 | rdmsrl(MSR_FAM10H_NODE_ID, value); |
|---|
| 368 | | - node_id = value & 7; |
|---|
| 440 | + c->cpu_die_id = value & 7; |
|---|
| 369 | 441 | |
|---|
| 370 | | - per_cpu(cpu_llc_id, cpu) = node_id; |
|---|
| 442 | + per_cpu(cpu_llc_id, cpu) = c->cpu_die_id; |
|---|
| 371 | 443 | } else |
|---|
| 372 | 444 | return; |
|---|
| 373 | 445 | |
|---|
| .. | .. |
|---|
| 392 | 464 | /* Convert the initial APIC ID into the socket ID */ |
|---|
| 393 | 465 | c->phys_proc_id = c->initial_apicid >> bits; |
|---|
| 394 | 466 | /* use socket ID also for last level cache */ |
|---|
| 395 | | - per_cpu(cpu_llc_id, cpu) = c->phys_proc_id; |
|---|
| 467 | + per_cpu(cpu_llc_id, cpu) = c->cpu_die_id = c->phys_proc_id; |
|---|
| 468 | +} |
|---|
| 469 | + |
|---|
| 470 | +static void amd_detect_ppin(struct cpuinfo_x86 *c) |
|---|
| 471 | +{ |
|---|
| 472 | + unsigned long long val; |
|---|
| 473 | + |
|---|
| 474 | + if (!cpu_has(c, X86_FEATURE_AMD_PPIN)) |
|---|
| 475 | + return; |
|---|
| 476 | + |
|---|
| 477 | + /* When PPIN is defined in CPUID, still need to check PPIN_CTL MSR */ |
|---|
| 478 | + if (rdmsrl_safe(MSR_AMD_PPIN_CTL, &val)) |
|---|
| 479 | + goto clear_ppin; |
|---|
| 480 | + |
|---|
| 481 | + /* PPIN is locked in disabled mode, clear feature bit */ |
|---|
| 482 | + if ((val & 3UL) == 1UL) |
|---|
| 483 | + goto clear_ppin; |
|---|
| 484 | + |
|---|
| 485 | + /* If PPIN is disabled, try to enable it */ |
|---|
| 486 | + if (!(val & 2UL)) { |
|---|
| 487 | + wrmsrl_safe(MSR_AMD_PPIN_CTL, val | 2UL); |
|---|
| 488 | + rdmsrl_safe(MSR_AMD_PPIN_CTL, &val); |
|---|
| 489 | + } |
|---|
| 490 | + |
|---|
| 491 | + /* If PPIN_EN bit is 1, return from here; otherwise fall through */ |
|---|
| 492 | + if (val & 2UL) |
|---|
| 493 | + return; |
|---|
| 494 | + |
|---|
| 495 | +clear_ppin: |
|---|
| 496 | + clear_cpu_cap(c, X86_FEATURE_AMD_PPIN); |
|---|
| 396 | 497 | } |
|---|
| 397 | 498 | |
|---|
| 398 | 499 | u16 amd_get_nb_id(int cpu) |
|---|
| .. | .. |
|---|
| 540 | 641 | u32 ecx; |
|---|
| 541 | 642 | |
|---|
| 542 | 643 | ecx = cpuid_ecx(0x8000001e); |
|---|
| 543 | | - nodes_per_socket = ((ecx >> 8) & 7) + 1; |
|---|
| 644 | + __max_die_per_package = nodes_per_socket = ((ecx >> 8) & 7) + 1; |
|---|
| 544 | 645 | } else if (boot_cpu_has(X86_FEATURE_NODEID_MSR)) { |
|---|
| 545 | 646 | u64 value; |
|---|
| 546 | 647 | |
|---|
| 547 | 648 | rdmsrl(MSR_FAM10H_NODE_ID, value); |
|---|
| 548 | | - nodes_per_socket = ((value >> 3) & 7) + 1; |
|---|
| 649 | + __max_die_per_package = nodes_per_socket = ((value >> 3) & 7) + 1; |
|---|
| 549 | 650 | } |
|---|
| 550 | 651 | |
|---|
| 551 | 652 | if (!boot_cpu_has(X86_FEATURE_AMD_SSBD) && |
|---|
| .. | .. |
|---|
| 569 | 670 | x86_amd_ls_cfg_ssbd_mask = 1ULL << bit; |
|---|
| 570 | 671 | } |
|---|
| 571 | 672 | } |
|---|
| 673 | + |
|---|
| 674 | + resctrl_cpu_detect(c); |
|---|
| 572 | 675 | } |
|---|
| 573 | 676 | |
|---|
| 574 | 677 | static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) |
|---|
| .. | .. |
|---|
| 582 | 685 | * If BIOS has not enabled SME then don't advertise the |
|---|
| 583 | 686 | * SME feature (set in scattered.c). |
|---|
| 584 | 687 | * For SEV: If BIOS has not enabled SEV then don't advertise the |
|---|
| 585 | | - * SEV feature (set in scattered.c). |
|---|
| 688 | + * SEV and SEV_ES feature (set in scattered.c). |
|---|
| 586 | 689 | * |
|---|
| 587 | 690 | * In all cases, since support for SME and SEV requires long mode, |
|---|
| 588 | 691 | * don't advertise the feature under CONFIG_X86_32. |
|---|
| .. | .. |
|---|
| 613 | 716 | setup_clear_cpu_cap(X86_FEATURE_SME); |
|---|
| 614 | 717 | clear_sev: |
|---|
| 615 | 718 | setup_clear_cpu_cap(X86_FEATURE_SEV); |
|---|
| 719 | + setup_clear_cpu_cap(X86_FEATURE_SEV_ES); |
|---|
| 616 | 720 | } |
|---|
| 617 | 721 | } |
|---|
| 618 | 722 | |
|---|
| .. | .. |
|---|
| 712 | 816 | } |
|---|
| 713 | 817 | } |
|---|
| 714 | 818 | |
|---|
| 715 | | - amd_get_topology_early(c); |
|---|
| 819 | + if (cpu_has(c, X86_FEATURE_TOPOEXT)) |
|---|
| 820 | + smp_num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1; |
|---|
| 716 | 821 | } |
|---|
| 717 | 822 | |
|---|
| 718 | 823 | static void init_amd_k8(struct cpuinfo_x86 *c) |
|---|
| .. | .. |
|---|
| 788 | 893 | if (cpu_has_amd_erratum(c, amd_erratum_383)) |
|---|
| 789 | 894 | set_cpu_bug(c, X86_BUG_AMD_TLB_MMATCH); |
|---|
| 790 | 895 | } |
|---|
| 791 | | - |
|---|
| 792 | | -#define MSR_AMD64_DE_CFG 0xC0011029 |
|---|
| 793 | 896 | |
|---|
| 794 | 897 | static void init_amd_ln(struct cpuinfo_x86 *c) |
|---|
| 795 | 898 | { |
|---|
| .. | .. |
|---|
| 881 | 984 | clear_rdrand_cpuid_bit(c); |
|---|
| 882 | 985 | } |
|---|
| 883 | 986 | |
|---|
| 987 | +void init_spectral_chicken(struct cpuinfo_x86 *c) |
|---|
| 988 | +{ |
|---|
| 989 | +#ifdef CONFIG_CPU_UNRET_ENTRY |
|---|
| 990 | + u64 value; |
|---|
| 991 | + |
|---|
| 992 | + /* |
|---|
| 993 | + * On Zen2 we offer this chicken (bit) on the altar of Speculation. |
|---|
| 994 | + * |
|---|
| 995 | + * This suppresses speculation from the middle of a basic block, i.e. it |
|---|
| 996 | + * suppresses non-branch predictions. |
|---|
| 997 | + * |
|---|
| 998 | + * We use STIBP as a heuristic to filter out Zen2 from the rest of F17H |
|---|
| 999 | + */ |
|---|
| 1000 | + if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_AMD_STIBP)) { |
|---|
| 1001 | + if (!rdmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, &value)) { |
|---|
| 1002 | + value |= MSR_ZEN2_SPECTRAL_CHICKEN_BIT; |
|---|
| 1003 | + wrmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, value); |
|---|
| 1004 | + } |
|---|
| 1005 | + } |
|---|
| 1006 | +#endif |
|---|
| 1007 | + /* |
|---|
| 1008 | + * Work around Erratum 1386. The XSAVES instruction malfunctions in |
|---|
| 1009 | + * certain circumstances on Zen1/2 uarch, and not all parts have had |
|---|
| 1010 | + * updated microcode at the time of writing (March 2023). |
|---|
| 1011 | + * |
|---|
| 1012 | + * Affected parts all have no supervisor XSAVE states, meaning that |
|---|
| 1013 | + * the XSAVEC instruction (which works fine) is equivalent. |
|---|
| 1014 | + */ |
|---|
| 1015 | + clear_cpu_cap(c, X86_FEATURE_XSAVES); |
|---|
| 1016 | +} |
|---|
| 1017 | + |
|---|
| 884 | 1018 | static void init_amd_zn(struct cpuinfo_x86 *c) |
|---|
| 885 | 1019 | { |
|---|
| 886 | 1020 | set_cpu_cap(c, X86_FEATURE_ZEN); |
|---|
| 887 | 1021 | |
|---|
| 888 | | - /* |
|---|
| 889 | | - * Fix erratum 1076: CPB feature bit not being set in CPUID. |
|---|
| 890 | | - * Always set it, except when running under a hypervisor. |
|---|
| 891 | | - */ |
|---|
| 892 | | - if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && !cpu_has(c, X86_FEATURE_CPB)) |
|---|
| 893 | | - set_cpu_cap(c, X86_FEATURE_CPB); |
|---|
| 1022 | +#ifdef CONFIG_NUMA |
|---|
| 1023 | + node_reclaim_distance = 32; |
|---|
| 1024 | +#endif |
|---|
| 1025 | + |
|---|
| 1026 | + /* Fix up CPUID bits, but only if not virtualised. */ |
|---|
| 1027 | + if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) { |
|---|
| 1028 | + |
|---|
| 1029 | + /* Erratum 1076: CPB feature bit not being set in CPUID. */ |
|---|
| 1030 | + if (!cpu_has(c, X86_FEATURE_CPB)) |
|---|
| 1031 | + set_cpu_cap(c, X86_FEATURE_CPB); |
|---|
| 1032 | + |
|---|
| 1033 | + /* |
|---|
| 1034 | + * Zen3 (Fam19 model < 0x10) parts are not susceptible to |
|---|
| 1035 | + * Branch Type Confusion, but predate the allocation of the |
|---|
| 1036 | + * BTC_NO bit. |
|---|
| 1037 | + */ |
|---|
| 1038 | + if (c->x86 == 0x19 && !cpu_has(c, X86_FEATURE_BTC_NO)) |
|---|
| 1039 | + set_cpu_cap(c, X86_FEATURE_BTC_NO); |
|---|
| 1040 | + } |
|---|
| 1041 | +} |
|---|
| 1042 | + |
|---|
| 1043 | +static bool cpu_has_zenbleed_microcode(void) |
|---|
| 1044 | +{ |
|---|
| 1045 | + u32 good_rev = 0; |
|---|
| 1046 | + |
|---|
| 1047 | + switch (boot_cpu_data.x86_model) { |
|---|
| 1048 | + case 0x30 ... 0x3f: good_rev = 0x0830107a; break; |
|---|
| 1049 | + case 0x60 ... 0x67: good_rev = 0x0860010b; break; |
|---|
| 1050 | + case 0x68 ... 0x6f: good_rev = 0x08608105; break; |
|---|
| 1051 | + case 0x70 ... 0x7f: good_rev = 0x08701032; break; |
|---|
| 1052 | + case 0xa0 ... 0xaf: good_rev = 0x08a00008; break; |
|---|
| 1053 | + |
|---|
| 1054 | + default: |
|---|
| 1055 | + return false; |
|---|
| 1056 | + break; |
|---|
| 1057 | + } |
|---|
| 1058 | + |
|---|
| 1059 | + if (boot_cpu_data.microcode < good_rev) |
|---|
| 1060 | + return false; |
|---|
| 1061 | + |
|---|
| 1062 | + return true; |
|---|
| 1063 | +} |
|---|
| 1064 | + |
|---|
| 1065 | +static void zenbleed_check(struct cpuinfo_x86 *c) |
|---|
| 1066 | +{ |
|---|
| 1067 | + if (!cpu_has_amd_erratum(c, amd_zenbleed)) |
|---|
| 1068 | + return; |
|---|
| 1069 | + |
|---|
| 1070 | + if (cpu_has(c, X86_FEATURE_HYPERVISOR)) |
|---|
| 1071 | + return; |
|---|
| 1072 | + |
|---|
| 1073 | + if (!cpu_has(c, X86_FEATURE_AVX)) |
|---|
| 1074 | + return; |
|---|
| 1075 | + |
|---|
| 1076 | + if (!cpu_has_zenbleed_microcode()) { |
|---|
| 1077 | + pr_notice_once("Zenbleed: please update your microcode for the most optimal fix\n"); |
|---|
| 1078 | + msr_set_bit(MSR_AMD64_DE_CFG, MSR_AMD64_DE_CFG_ZEN2_FP_BACKUP_FIX_BIT); |
|---|
| 1079 | + } else { |
|---|
| 1080 | + msr_clear_bit(MSR_AMD64_DE_CFG, MSR_AMD64_DE_CFG_ZEN2_FP_BACKUP_FIX_BIT); |
|---|
| 1081 | + } |
|---|
| 894 | 1082 | } |
|---|
| 895 | 1083 | |
|---|
| 896 | 1084 | static void init_amd(struct cpuinfo_x86 *c) |
|---|
| .. | .. |
|---|
| 922 | 1110 | case 0x12: init_amd_ln(c); break; |
|---|
| 923 | 1111 | case 0x15: init_amd_bd(c); break; |
|---|
| 924 | 1112 | case 0x16: init_amd_jg(c); break; |
|---|
| 925 | | - case 0x17: init_amd_zn(c); break; |
|---|
| 1113 | + case 0x17: init_spectral_chicken(c); |
|---|
| 1114 | + fallthrough; |
|---|
| 1115 | + case 0x19: init_amd_zn(c); break; |
|---|
| 926 | 1116 | } |
|---|
| 927 | 1117 | |
|---|
| 928 | 1118 | /* |
|---|
| .. | .. |
|---|
| 937 | 1127 | amd_detect_cmp(c); |
|---|
| 938 | 1128 | amd_get_topology(c); |
|---|
| 939 | 1129 | srat_detect_node(c); |
|---|
| 1130 | + amd_detect_ppin(c); |
|---|
| 940 | 1131 | |
|---|
| 941 | 1132 | init_amd_cacheinfo(c); |
|---|
| 942 | 1133 | |
|---|
| 943 | 1134 | if (cpu_has(c, X86_FEATURE_XMM2)) { |
|---|
| 944 | | - unsigned long long val; |
|---|
| 945 | | - int ret; |
|---|
| 946 | | - |
|---|
| 947 | 1135 | /* |
|---|
| 948 | | - * A serializing LFENCE has less overhead than MFENCE, so |
|---|
| 949 | | - * use it for execution serialization. On families which |
|---|
| 1136 | + * Use LFENCE for execution serialization. On families which |
|---|
| 950 | 1137 | * don't have that MSR, LFENCE is already serializing. |
|---|
| 951 | 1138 | * msr_set_bit() uses the safe accessors, too, even if the MSR |
|---|
| 952 | 1139 | * is not present. |
|---|
| 953 | 1140 | */ |
|---|
| 954 | | - msr_set_bit(MSR_F10H_DECFG, |
|---|
| 955 | | - MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT); |
|---|
| 1141 | + msr_set_bit(MSR_AMD64_DE_CFG, |
|---|
| 1142 | + MSR_AMD64_DE_CFG_LFENCE_SERIALIZE_BIT); |
|---|
| 956 | 1143 | |
|---|
| 957 | | - /* |
|---|
| 958 | | - * Verify that the MSR write was successful (could be running |
|---|
| 959 | | - * under a hypervisor) and only then assume that LFENCE is |
|---|
| 960 | | - * serializing. |
|---|
| 961 | | - */ |
|---|
| 962 | | - ret = rdmsrl_safe(MSR_F10H_DECFG, &val); |
|---|
| 963 | | - if (!ret && (val & MSR_F10H_DECFG_LFENCE_SERIALIZE)) { |
|---|
| 964 | | - /* A serializing LFENCE stops RDTSC speculation */ |
|---|
| 965 | | - set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC); |
|---|
| 966 | | - } else { |
|---|
| 967 | | - /* MFENCE stops RDTSC speculation */ |
|---|
| 968 | | - set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC); |
|---|
| 969 | | - } |
|---|
| 1144 | + /* A serializing LFENCE stops RDTSC speculation */ |
|---|
| 1145 | + set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC); |
|---|
| 970 | 1146 | } |
|---|
| 971 | 1147 | |
|---|
| 972 | 1148 | /* |
|---|
| .. | .. |
|---|
| 995 | 1171 | msr_set_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT); |
|---|
| 996 | 1172 | |
|---|
| 997 | 1173 | check_null_seg_clears_base(c); |
|---|
| 1174 | + |
|---|
| 1175 | + zenbleed_check(c); |
|---|
| 1176 | + |
|---|
| 1177 | + if (cpu_has_amd_erratum(c, amd_div0)) { |
|---|
| 1178 | + pr_notice_once("AMD Zen1 DIV0 bug detected. Disable SMT for full protection.\n"); |
|---|
| 1179 | + setup_force_cpu_bug(X86_BUG_DIV0); |
|---|
| 1180 | + } |
|---|
| 998 | 1181 | } |
|---|
| 999 | 1182 | |
|---|
| 1000 | 1183 | #ifdef CONFIG_X86_32 |
|---|
| .. | .. |
|---|
| 1090 | 1273 | |
|---|
| 1091 | 1274 | cpu_dev_register(amd_cpu_dev); |
|---|
| 1092 | 1275 | |
|---|
| 1093 | | -/* |
|---|
| 1094 | | - * AMD errata checking |
|---|
| 1095 | | - * |
|---|
| 1096 | | - * Errata are defined as arrays of ints using the AMD_LEGACY_ERRATUM() or |
|---|
| 1097 | | - * AMD_OSVW_ERRATUM() macros. The latter is intended for newer errata that |
|---|
| 1098 | | - * have an OSVW id assigned, which it takes as first argument. Both take a |
|---|
| 1099 | | - * variable number of family-specific model-stepping ranges created by |
|---|
| 1100 | | - * AMD_MODEL_RANGE(). |
|---|
| 1101 | | - * |
|---|
| 1102 | | - * Example: |
|---|
| 1103 | | - * |
|---|
| 1104 | | - * const int amd_erratum_319[] = |
|---|
| 1105 | | - * AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0x4, 0x2), |
|---|
| 1106 | | - * AMD_MODEL_RANGE(0x10, 0x8, 0x0, 0x8, 0x0), |
|---|
| 1107 | | - * AMD_MODEL_RANGE(0x10, 0x9, 0x0, 0x9, 0x0)); |
|---|
| 1108 | | - */ |
|---|
| 1109 | | - |
|---|
| 1110 | | -#define AMD_LEGACY_ERRATUM(...) { -1, __VA_ARGS__, 0 } |
|---|
| 1111 | | -#define AMD_OSVW_ERRATUM(osvw_id, ...) { osvw_id, __VA_ARGS__, 0 } |
|---|
| 1112 | | -#define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \ |
|---|
| 1113 | | - ((f << 24) | (m_start << 16) | (s_start << 12) | (m_end << 4) | (s_end)) |
|---|
| 1114 | | -#define AMD_MODEL_RANGE_FAMILY(range) (((range) >> 24) & 0xff) |
|---|
| 1115 | | -#define AMD_MODEL_RANGE_START(range) (((range) >> 12) & 0xfff) |
|---|
| 1116 | | -#define AMD_MODEL_RANGE_END(range) ((range) & 0xfff) |
|---|
| 1117 | | - |
|---|
| 1118 | | -static const int amd_erratum_400[] = |
|---|
| 1119 | | - AMD_OSVW_ERRATUM(1, AMD_MODEL_RANGE(0xf, 0x41, 0x2, 0xff, 0xf), |
|---|
| 1120 | | - AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf)); |
|---|
| 1121 | | - |
|---|
| 1122 | | -static const int amd_erratum_383[] = |
|---|
| 1123 | | - AMD_OSVW_ERRATUM(3, AMD_MODEL_RANGE(0x10, 0, 0, 0xff, 0xf)); |
|---|
| 1124 | | - |
|---|
| 1125 | | -/* #1054: Instructions Retired Performance Counter May Be Inaccurate */ |
|---|
| 1126 | | -static const int amd_erratum_1054[] = |
|---|
| 1127 | | - AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x17, 0, 0, 0x2f, 0xf)); |
|---|
| 1128 | | - |
|---|
| 1129 | | -static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum) |
|---|
| 1130 | | -{ |
|---|
| 1131 | | - int osvw_id = *erratum++; |
|---|
| 1132 | | - u32 range; |
|---|
| 1133 | | - u32 ms; |
|---|
| 1134 | | - |
|---|
| 1135 | | - if (osvw_id >= 0 && osvw_id < 65536 && |
|---|
| 1136 | | - cpu_has(cpu, X86_FEATURE_OSVW)) { |
|---|
| 1137 | | - u64 osvw_len; |
|---|
| 1138 | | - |
|---|
| 1139 | | - rdmsrl(MSR_AMD64_OSVW_ID_LENGTH, osvw_len); |
|---|
| 1140 | | - if (osvw_id < osvw_len) { |
|---|
| 1141 | | - u64 osvw_bits; |
|---|
| 1142 | | - |
|---|
| 1143 | | - rdmsrl(MSR_AMD64_OSVW_STATUS + (osvw_id >> 6), |
|---|
| 1144 | | - osvw_bits); |
|---|
| 1145 | | - return osvw_bits & (1ULL << (osvw_id & 0x3f)); |
|---|
| 1146 | | - } |
|---|
| 1147 | | - } |
|---|
| 1148 | | - |
|---|
| 1149 | | - /* OSVW unavailable or ID unknown, match family-model-stepping range */ |
|---|
| 1150 | | - ms = (cpu->x86_model << 4) | cpu->x86_stepping; |
|---|
| 1151 | | - while ((range = *erratum++)) |
|---|
| 1152 | | - if ((cpu->x86 == AMD_MODEL_RANGE_FAMILY(range)) && |
|---|
| 1153 | | - (ms >= AMD_MODEL_RANGE_START(range)) && |
|---|
| 1154 | | - (ms <= AMD_MODEL_RANGE_END(range))) |
|---|
| 1155 | | - return true; |
|---|
| 1156 | | - |
|---|
| 1157 | | - return false; |
|---|
| 1158 | | -} |
|---|
| 1159 | | - |
|---|
| 1160 | 1276 | void set_dr_addr_mask(unsigned long mask, int dr) |
|---|
| 1161 | 1277 | { |
|---|
| 1162 | 1278 | if (!boot_cpu_has(X86_FEATURE_BPEXT)) |
|---|
| .. | .. |
|---|
| 1175 | 1291 | break; |
|---|
| 1176 | 1292 | } |
|---|
| 1177 | 1293 | } |
|---|
| 1294 | + |
|---|
| 1295 | +bool cpu_has_ibpb_brtype_microcode(void) |
|---|
| 1296 | +{ |
|---|
| 1297 | + switch (boot_cpu_data.x86) { |
|---|
| 1298 | + /* Zen1/2 IBPB flushes branch type predictions too. */ |
|---|
| 1299 | + case 0x17: |
|---|
| 1300 | + return boot_cpu_has(X86_FEATURE_AMD_IBPB); |
|---|
| 1301 | + case 0x19: |
|---|
| 1302 | + /* Poke the MSR bit on Zen3/4 to check its presence. */ |
|---|
| 1303 | + if (!wrmsrl_safe(MSR_IA32_PRED_CMD, PRED_CMD_SBPB)) { |
|---|
| 1304 | + setup_force_cpu_cap(X86_FEATURE_SBPB); |
|---|
| 1305 | + return true; |
|---|
| 1306 | + } else { |
|---|
| 1307 | + return false; |
|---|
| 1308 | + } |
|---|
| 1309 | + default: |
|---|
| 1310 | + return false; |
|---|
| 1311 | + } |
|---|
| 1312 | +} |
|---|
| 1313 | + |
|---|
| 1314 | +static void zenbleed_check_cpu(void *unused) |
|---|
| 1315 | +{ |
|---|
| 1316 | + struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
|---|
| 1317 | + |
|---|
| 1318 | + zenbleed_check(c); |
|---|
| 1319 | +} |
|---|
| 1320 | + |
|---|
| 1321 | +void amd_check_microcode(void) |
|---|
| 1322 | +{ |
|---|
| 1323 | + on_each_cpu(zenbleed_check_cpu, NULL, 1); |
|---|
| 1324 | +} |
|---|
| 1325 | + |
|---|
| 1326 | +/* |
|---|
| 1327 | + * Issue a DIV 0/1 insn to clear any division data from previous DIV |
|---|
| 1328 | + * operations. |
|---|
| 1329 | + */ |
|---|
| 1330 | +void noinstr amd_clear_divider(void) |
|---|
| 1331 | +{ |
|---|
| 1332 | + asm volatile(ALTERNATIVE("", "div %2\n\t", X86_BUG_DIV0) |
|---|
| 1333 | + :: "a" (0), "d" (0), "r" (1)); |
|---|
| 1334 | +} |
|---|
| 1335 | +EXPORT_SYMBOL_GPL(amd_clear_divider); |
|---|