| .. | .. |
|---|
| 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> |
|---|
| .. | .. |
|---|
| 82 | 87 | * performance at the same time.. |
|---|
| 83 | 88 | */ |
|---|
| 84 | 89 | |
|---|
| 90 | +#ifdef CONFIG_X86_32 |
|---|
| 85 | 91 | extern __visible void vide(void); |
|---|
| 86 | | -__asm__(".globl vide\n" |
|---|
| 92 | +__asm__(".text\n" |
|---|
| 93 | + ".globl vide\n" |
|---|
| 87 | 94 | ".type vide, @function\n" |
|---|
| 88 | 95 | ".align 4\n" |
|---|
| 89 | 96 | "vide: ret\n"); |
|---|
| 97 | +#endif |
|---|
| 90 | 98 | |
|---|
| 91 | 99 | static void init_amd_k5(struct cpuinfo_x86 *c) |
|---|
| 92 | 100 | { |
|---|
| .. | .. |
|---|
| 314 | 322 | c->cpu_core_id %= cus_per_node; |
|---|
| 315 | 323 | } |
|---|
| 316 | 324 | |
|---|
| 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 | 325 | /* |
|---|
| 325 | 326 | * Fixup core topology information for |
|---|
| 326 | 327 | * (1) AMD multi-node processors |
|---|
| .. | .. |
|---|
| 329 | 330 | */ |
|---|
| 330 | 331 | static void amd_get_topology(struct cpuinfo_x86 *c) |
|---|
| 331 | 332 | { |
|---|
| 332 | | - u8 node_id; |
|---|
| 333 | 333 | int cpu = smp_processor_id(); |
|---|
| 334 | 334 | |
|---|
| 335 | 335 | /* get information required for multi-node processors */ |
|---|
| .. | .. |
|---|
| 339 | 339 | |
|---|
| 340 | 340 | cpuid(0x8000001e, &eax, &ebx, &ecx, &edx); |
|---|
| 341 | 341 | |
|---|
| 342 | | - node_id = ecx & 0xff; |
|---|
| 342 | + c->cpu_die_id = ecx & 0xff; |
|---|
| 343 | 343 | |
|---|
| 344 | 344 | if (c->x86 == 0x15) |
|---|
| 345 | 345 | c->cu_id = ebx & 0xff; |
|---|
| .. | .. |
|---|
| 359 | 359 | if (!err) |
|---|
| 360 | 360 | c->x86_coreid_bits = get_count_order(c->x86_max_cores); |
|---|
| 361 | 361 | |
|---|
| 362 | | - cacheinfo_amd_init_llc_id(c, cpu, node_id); |
|---|
| 362 | + cacheinfo_amd_init_llc_id(c, cpu); |
|---|
| 363 | 363 | |
|---|
| 364 | 364 | } else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) { |
|---|
| 365 | 365 | u64 value; |
|---|
| 366 | 366 | |
|---|
| 367 | 367 | rdmsrl(MSR_FAM10H_NODE_ID, value); |
|---|
| 368 | | - node_id = value & 7; |
|---|
| 368 | + c->cpu_die_id = value & 7; |
|---|
| 369 | 369 | |
|---|
| 370 | | - per_cpu(cpu_llc_id, cpu) = node_id; |
|---|
| 370 | + per_cpu(cpu_llc_id, cpu) = c->cpu_die_id; |
|---|
| 371 | 371 | } else |
|---|
| 372 | 372 | return; |
|---|
| 373 | 373 | |
|---|
| .. | .. |
|---|
| 392 | 392 | /* Convert the initial APIC ID into the socket ID */ |
|---|
| 393 | 393 | c->phys_proc_id = c->initial_apicid >> bits; |
|---|
| 394 | 394 | /* use socket ID also for last level cache */ |
|---|
| 395 | | - per_cpu(cpu_llc_id, cpu) = c->phys_proc_id; |
|---|
| 395 | + per_cpu(cpu_llc_id, cpu) = c->cpu_die_id = c->phys_proc_id; |
|---|
| 396 | +} |
|---|
| 397 | + |
|---|
| 398 | +static void amd_detect_ppin(struct cpuinfo_x86 *c) |
|---|
| 399 | +{ |
|---|
| 400 | + unsigned long long val; |
|---|
| 401 | + |
|---|
| 402 | + if (!cpu_has(c, X86_FEATURE_AMD_PPIN)) |
|---|
| 403 | + return; |
|---|
| 404 | + |
|---|
| 405 | + /* When PPIN is defined in CPUID, still need to check PPIN_CTL MSR */ |
|---|
| 406 | + if (rdmsrl_safe(MSR_AMD_PPIN_CTL, &val)) |
|---|
| 407 | + goto clear_ppin; |
|---|
| 408 | + |
|---|
| 409 | + /* PPIN is locked in disabled mode, clear feature bit */ |
|---|
| 410 | + if ((val & 3UL) == 1UL) |
|---|
| 411 | + goto clear_ppin; |
|---|
| 412 | + |
|---|
| 413 | + /* If PPIN is disabled, try to enable it */ |
|---|
| 414 | + if (!(val & 2UL)) { |
|---|
| 415 | + wrmsrl_safe(MSR_AMD_PPIN_CTL, val | 2UL); |
|---|
| 416 | + rdmsrl_safe(MSR_AMD_PPIN_CTL, &val); |
|---|
| 417 | + } |
|---|
| 418 | + |
|---|
| 419 | + /* If PPIN_EN bit is 1, return from here; otherwise fall through */ |
|---|
| 420 | + if (val & 2UL) |
|---|
| 421 | + return; |
|---|
| 422 | + |
|---|
| 423 | +clear_ppin: |
|---|
| 424 | + clear_cpu_cap(c, X86_FEATURE_AMD_PPIN); |
|---|
| 396 | 425 | } |
|---|
| 397 | 426 | |
|---|
| 398 | 427 | u16 amd_get_nb_id(int cpu) |
|---|
| .. | .. |
|---|
| 540 | 569 | u32 ecx; |
|---|
| 541 | 570 | |
|---|
| 542 | 571 | ecx = cpuid_ecx(0x8000001e); |
|---|
| 543 | | - nodes_per_socket = ((ecx >> 8) & 7) + 1; |
|---|
| 572 | + __max_die_per_package = nodes_per_socket = ((ecx >> 8) & 7) + 1; |
|---|
| 544 | 573 | } else if (boot_cpu_has(X86_FEATURE_NODEID_MSR)) { |
|---|
| 545 | 574 | u64 value; |
|---|
| 546 | 575 | |
|---|
| 547 | 576 | rdmsrl(MSR_FAM10H_NODE_ID, value); |
|---|
| 548 | | - nodes_per_socket = ((value >> 3) & 7) + 1; |
|---|
| 577 | + __max_die_per_package = nodes_per_socket = ((value >> 3) & 7) + 1; |
|---|
| 549 | 578 | } |
|---|
| 550 | 579 | |
|---|
| 551 | 580 | if (!boot_cpu_has(X86_FEATURE_AMD_SSBD) && |
|---|
| .. | .. |
|---|
| 569 | 598 | x86_amd_ls_cfg_ssbd_mask = 1ULL << bit; |
|---|
| 570 | 599 | } |
|---|
| 571 | 600 | } |
|---|
| 601 | + |
|---|
| 602 | + resctrl_cpu_detect(c); |
|---|
| 572 | 603 | } |
|---|
| 573 | 604 | |
|---|
| 574 | 605 | static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) |
|---|
| .. | .. |
|---|
| 582 | 613 | * If BIOS has not enabled SME then don't advertise the |
|---|
| 583 | 614 | * SME feature (set in scattered.c). |
|---|
| 584 | 615 | * For SEV: If BIOS has not enabled SEV then don't advertise the |
|---|
| 585 | | - * SEV feature (set in scattered.c). |
|---|
| 616 | + * SEV and SEV_ES feature (set in scattered.c). |
|---|
| 586 | 617 | * |
|---|
| 587 | 618 | * In all cases, since support for SME and SEV requires long mode, |
|---|
| 588 | 619 | * don't advertise the feature under CONFIG_X86_32. |
|---|
| .. | .. |
|---|
| 613 | 644 | setup_clear_cpu_cap(X86_FEATURE_SME); |
|---|
| 614 | 645 | clear_sev: |
|---|
| 615 | 646 | setup_clear_cpu_cap(X86_FEATURE_SEV); |
|---|
| 647 | + setup_clear_cpu_cap(X86_FEATURE_SEV_ES); |
|---|
| 616 | 648 | } |
|---|
| 617 | 649 | } |
|---|
| 618 | 650 | |
|---|
| .. | .. |
|---|
| 712 | 744 | } |
|---|
| 713 | 745 | } |
|---|
| 714 | 746 | |
|---|
| 715 | | - amd_get_topology_early(c); |
|---|
| 747 | + if (cpu_has(c, X86_FEATURE_TOPOEXT)) |
|---|
| 748 | + smp_num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1; |
|---|
| 716 | 749 | } |
|---|
| 717 | 750 | |
|---|
| 718 | 751 | static void init_amd_k8(struct cpuinfo_x86 *c) |
|---|
| .. | .. |
|---|
| 788 | 821 | if (cpu_has_amd_erratum(c, amd_erratum_383)) |
|---|
| 789 | 822 | set_cpu_bug(c, X86_BUG_AMD_TLB_MMATCH); |
|---|
| 790 | 823 | } |
|---|
| 791 | | - |
|---|
| 792 | | -#define MSR_AMD64_DE_CFG 0xC0011029 |
|---|
| 793 | 824 | |
|---|
| 794 | 825 | static void init_amd_ln(struct cpuinfo_x86 *c) |
|---|
| 795 | 826 | { |
|---|
| .. | .. |
|---|
| 881 | 912 | clear_rdrand_cpuid_bit(c); |
|---|
| 882 | 913 | } |
|---|
| 883 | 914 | |
|---|
| 915 | +void init_spectral_chicken(struct cpuinfo_x86 *c) |
|---|
| 916 | +{ |
|---|
| 917 | +#ifdef CONFIG_CPU_UNRET_ENTRY |
|---|
| 918 | + u64 value; |
|---|
| 919 | + |
|---|
| 920 | + /* |
|---|
| 921 | + * On Zen2 we offer this chicken (bit) on the altar of Speculation. |
|---|
| 922 | + * |
|---|
| 923 | + * This suppresses speculation from the middle of a basic block, i.e. it |
|---|
| 924 | + * suppresses non-branch predictions. |
|---|
| 925 | + * |
|---|
| 926 | + * We use STIBP as a heuristic to filter out Zen2 from the rest of F17H |
|---|
| 927 | + */ |
|---|
| 928 | + if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_AMD_STIBP)) { |
|---|
| 929 | + if (!rdmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, &value)) { |
|---|
| 930 | + value |= MSR_ZEN2_SPECTRAL_CHICKEN_BIT; |
|---|
| 931 | + wrmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, value); |
|---|
| 932 | + } |
|---|
| 933 | + } |
|---|
| 934 | +#endif |
|---|
| 935 | +} |
|---|
| 936 | + |
|---|
| 884 | 937 | static void init_amd_zn(struct cpuinfo_x86 *c) |
|---|
| 885 | 938 | { |
|---|
| 886 | 939 | set_cpu_cap(c, X86_FEATURE_ZEN); |
|---|
| 887 | 940 | |
|---|
| 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); |
|---|
| 941 | +#ifdef CONFIG_NUMA |
|---|
| 942 | + node_reclaim_distance = 32; |
|---|
| 943 | +#endif |
|---|
| 944 | + |
|---|
| 945 | + /* Fix up CPUID bits, but only if not virtualised. */ |
|---|
| 946 | + if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) { |
|---|
| 947 | + |
|---|
| 948 | + /* Erratum 1076: CPB feature bit not being set in CPUID. */ |
|---|
| 949 | + if (!cpu_has(c, X86_FEATURE_CPB)) |
|---|
| 950 | + set_cpu_cap(c, X86_FEATURE_CPB); |
|---|
| 951 | + |
|---|
| 952 | + /* |
|---|
| 953 | + * Zen3 (Fam19 model < 0x10) parts are not susceptible to |
|---|
| 954 | + * Branch Type Confusion, but predate the allocation of the |
|---|
| 955 | + * BTC_NO bit. |
|---|
| 956 | + */ |
|---|
| 957 | + if (c->x86 == 0x19 && !cpu_has(c, X86_FEATURE_BTC_NO)) |
|---|
| 958 | + set_cpu_cap(c, X86_FEATURE_BTC_NO); |
|---|
| 959 | + } |
|---|
| 894 | 960 | } |
|---|
| 895 | 961 | |
|---|
| 896 | 962 | static void init_amd(struct cpuinfo_x86 *c) |
|---|
| .. | .. |
|---|
| 922 | 988 | case 0x12: init_amd_ln(c); break; |
|---|
| 923 | 989 | case 0x15: init_amd_bd(c); break; |
|---|
| 924 | 990 | case 0x16: init_amd_jg(c); break; |
|---|
| 925 | | - case 0x17: init_amd_zn(c); break; |
|---|
| 991 | + case 0x17: init_spectral_chicken(c); |
|---|
| 992 | + fallthrough; |
|---|
| 993 | + case 0x19: init_amd_zn(c); break; |
|---|
| 926 | 994 | } |
|---|
| 927 | 995 | |
|---|
| 928 | 996 | /* |
|---|
| .. | .. |
|---|
| 937 | 1005 | amd_detect_cmp(c); |
|---|
| 938 | 1006 | amd_get_topology(c); |
|---|
| 939 | 1007 | srat_detect_node(c); |
|---|
| 1008 | + amd_detect_ppin(c); |
|---|
| 940 | 1009 | |
|---|
| 941 | 1010 | init_amd_cacheinfo(c); |
|---|
| 942 | 1011 | |
|---|
| 943 | 1012 | if (cpu_has(c, X86_FEATURE_XMM2)) { |
|---|
| 944 | | - unsigned long long val; |
|---|
| 945 | | - int ret; |
|---|
| 946 | | - |
|---|
| 947 | 1013 | /* |
|---|
| 948 | | - * A serializing LFENCE has less overhead than MFENCE, so |
|---|
| 949 | | - * use it for execution serialization. On families which |
|---|
| 1014 | + * Use LFENCE for execution serialization. On families which |
|---|
| 950 | 1015 | * don't have that MSR, LFENCE is already serializing. |
|---|
| 951 | 1016 | * msr_set_bit() uses the safe accessors, too, even if the MSR |
|---|
| 952 | 1017 | * is not present. |
|---|
| 953 | 1018 | */ |
|---|
| 954 | | - msr_set_bit(MSR_F10H_DECFG, |
|---|
| 955 | | - MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT); |
|---|
| 1019 | + msr_set_bit(MSR_AMD64_DE_CFG, |
|---|
| 1020 | + MSR_AMD64_DE_CFG_LFENCE_SERIALIZE_BIT); |
|---|
| 956 | 1021 | |
|---|
| 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 | | - } |
|---|
| 1022 | + /* A serializing LFENCE stops RDTSC speculation */ |
|---|
| 1023 | + set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC); |
|---|
| 970 | 1024 | } |
|---|
| 971 | 1025 | |
|---|
| 972 | 1026 | /* |
|---|