Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Check for extended topology enumeration cpuid leaf 0xb and if it |
| 4 | * exists, use it for populating initial_apicid and cpu topology |
| 5 | * detection. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/cpu.h> |
| 9 | #include <asm/apic.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 10 | #include <asm/memtype.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11 | #include <asm/processor.h> |
| 12 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 13 | #include "cpu.h" |
| 14 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | /* leaf 0xb SMT level */ |
| 16 | #define SMT_LEVEL 0 |
| 17 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 18 | /* extended topology sub-leaf types */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 19 | #define INVALID_TYPE 0 |
| 20 | #define SMT_TYPE 1 |
| 21 | #define CORE_TYPE 2 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 22 | #define DIE_TYPE 5 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | |
| 24 | #define LEAFB_SUBTYPE(ecx) (((ecx) >> 8) & 0xff) |
| 25 | #define BITS_SHIFT_NEXT_LEVEL(eax) ((eax) & 0x1f) |
| 26 | #define LEVEL_MAX_SIBLINGS(ebx) ((ebx) & 0xffff) |
| 27 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 28 | unsigned int __max_die_per_package __read_mostly = 1; |
| 29 | EXPORT_SYMBOL(__max_die_per_package); |
| 30 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_SMP |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Check if given CPUID extended toplogy "leaf" is implemented |
| 34 | */ |
| 35 | static int check_extended_topology_leaf(int leaf) |
| 36 | { |
| 37 | unsigned int eax, ebx, ecx, edx; |
| 38 | |
| 39 | cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx); |
| 40 | |
| 41 | if (ebx == 0 || (LEAFB_SUBTYPE(ecx) != SMT_TYPE)) |
| 42 | return -1; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | /* |
| 47 | * Return best CPUID Extended Toplogy Leaf supported |
| 48 | */ |
| 49 | static int detect_extended_topology_leaf(struct cpuinfo_x86 *c) |
| 50 | { |
| 51 | if (c->cpuid_level >= 0x1f) { |
| 52 | if (check_extended_topology_leaf(0x1f) == 0) |
| 53 | return 0x1f; |
| 54 | } |
| 55 | |
| 56 | if (c->cpuid_level >= 0xb) { |
| 57 | if (check_extended_topology_leaf(0xb) == 0) |
| 58 | return 0xb; |
| 59 | } |
| 60 | |
| 61 | return -1; |
| 62 | } |
| 63 | #endif |
| 64 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | int detect_extended_topology_early(struct cpuinfo_x86 *c) |
| 66 | { |
| 67 | #ifdef CONFIG_SMP |
| 68 | unsigned int eax, ebx, ecx, edx; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 69 | int leaf; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 70 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 71 | leaf = detect_extended_topology_leaf(c); |
| 72 | if (leaf < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 73 | return -1; |
| 74 | |
| 75 | set_cpu_cap(c, X86_FEATURE_XTOPOLOGY); |
| 76 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 77 | cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 78 | /* |
| 79 | * initial apic id, which also represents 32-bit extended x2apic id. |
| 80 | */ |
| 81 | c->initial_apicid = edx; |
| 82 | smp_num_siblings = LEVEL_MAX_SIBLINGS(ebx); |
| 83 | #endif |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 88 | * Check for extended topology enumeration cpuid leaf, and if it |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 89 | * exists, use it for populating initial_apicid and cpu topology |
| 90 | * detection. |
| 91 | */ |
| 92 | int detect_extended_topology(struct cpuinfo_x86 *c) |
| 93 | { |
| 94 | #ifdef CONFIG_SMP |
| 95 | unsigned int eax, ebx, ecx, edx, sub_index; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 96 | unsigned int ht_mask_width, core_plus_mask_width, die_plus_mask_width; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 97 | unsigned int core_select_mask, core_level_siblings; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 98 | unsigned int die_select_mask, die_level_siblings; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 99 | unsigned int pkg_mask_width; |
| 100 | bool die_level_present = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 101 | int leaf; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 102 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 103 | leaf = detect_extended_topology_leaf(c); |
| 104 | if (leaf < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 105 | return -1; |
| 106 | |
| 107 | /* |
| 108 | * Populate HT related information from sub-leaf level 0. |
| 109 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 110 | cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx); |
| 111 | c->initial_apicid = edx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 112 | core_level_siblings = smp_num_siblings = LEVEL_MAX_SIBLINGS(ebx); |
| 113 | core_plus_mask_width = ht_mask_width = BITS_SHIFT_NEXT_LEVEL(eax); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 114 | die_level_siblings = LEVEL_MAX_SIBLINGS(ebx); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 115 | pkg_mask_width = die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | |
| 117 | sub_index = 1; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 118 | while (true) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 119 | cpuid_count(leaf, sub_index, &eax, &ebx, &ecx, &edx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Check for the Core type in the implemented sub leaves. |
| 123 | */ |
| 124 | if (LEAFB_SUBTYPE(ecx) == CORE_TYPE) { |
| 125 | core_level_siblings = LEVEL_MAX_SIBLINGS(ebx); |
| 126 | core_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 127 | die_level_siblings = core_level_siblings; |
| 128 | die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax); |
| 129 | } |
| 130 | if (LEAFB_SUBTYPE(ecx) == DIE_TYPE) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 131 | die_level_present = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 132 | die_level_siblings = LEVEL_MAX_SIBLINGS(ebx); |
| 133 | die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 136 | if (LEAFB_SUBTYPE(ecx) != INVALID_TYPE) |
| 137 | pkg_mask_width = BITS_SHIFT_NEXT_LEVEL(eax); |
| 138 | else |
| 139 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 140 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 141 | sub_index++; |
| 142 | } |
| 143 | |
| 144 | core_select_mask = (~(-1 << pkg_mask_width)) >> ht_mask_width; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 145 | die_select_mask = (~(-1 << die_plus_mask_width)) >> |
| 146 | core_plus_mask_width; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 147 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 148 | c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, |
| 149 | ht_mask_width) & core_select_mask; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 150 | |
| 151 | if (die_level_present) { |
| 152 | c->cpu_die_id = apic->phys_pkg_id(c->initial_apicid, |
| 153 | core_plus_mask_width) & die_select_mask; |
| 154 | } |
| 155 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 156 | c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 157 | pkg_mask_width); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 158 | /* |
| 159 | * Reinit the apicid, now that we have extended initial_apicid. |
| 160 | */ |
| 161 | c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); |
| 162 | |
| 163 | c->x86_max_cores = (core_level_siblings / smp_num_siblings); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 164 | __max_die_per_package = (die_level_siblings / core_level_siblings); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 165 | #endif |
| 166 | return 0; |
| 167 | } |