Debian Codegraph @

src/cpu/x86/topology.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <cpu/cpu.h>
5 #include <cpu/x86/topology.h>
6 #include <device/device.h>
7
8 /*
9 * The following code defines functions for populating the topology information of a given CPU
10 * device (struct device *cpu).
11 *
12 * Reference: Intel Software Developer Manual Extended Topology Enumeration Leaves (CPUID EAX 0xb
13 * and 0x1f).
14 */
15
16 #define CPUID_EXTENDED_CPU_TOPOLOGY 0x0b
17 #define CPUID_EXTENDED_CPU_TOPOLOGY2 0x1f
18
19 enum {
20 LEVEL_TYPE_PACKAGE, /* Use unused level zero for package. */
21 LEVEL_TYPE_SMT,
22 LEVEL_TYPE_CORE,
23 LEVEL_TYPE_MODULE,
24 LEVEL_TYPE_TILE,
25 LEVEL_TYPE_DIE,
26 LEVEL_TYPE_DIEGRP,
27 LEVEL_TYPE_MAX
28 };
29
30 #define CPUID_CPU_TOPOLOGY(x, val) \
31 (((val) >> CPUID_CPU_TOPOLOGY_##x##_SHIFT) & CPUID_CPU_TOPOLOGY_##x##_MASK)
32
33 #define CPUID_CPU_TOPOLOGY_LEVEL_TYPE_SHIFT 0x8
34 #define CPUID_CPU_TOPOLOGY_LEVEL_TYPE_MASK 0xff
35 #define CPUID_CPU_TOPOLOGY_LEVEL(res) CPUID_CPU_TOPOLOGY(LEVEL_TYPE, (res).ecx)
36
37 #define CPUID_CPU_TOPOLOGY_LEVEL_BITS_SHIFT 0x0
38 #define CPUID_CPU_TOPOLOGY_LEVEL_BITS_MASK 0x1f
39
40 struct bitfield_descriptor {
41 uint8_t first_bit;
42 uint8_t num_bits;
43 };
44
45 /*
46 * Fills in the supplied bit field APIC ID descriptor structure array.
47 *
48 * Per Intel Software Developer Manual recommendation, it prioritizes CPUID EAX=0x1f over CPUID
49 * EAX=0xb if available. The function iterates over the hierarchy levels to extract per-domain
50 * (level) APIC bit field information and populates the provided topology array with the
51 * bitfield descriptors for each topology level.
52 *
53 * Returns CB_ERR if:
54 * - CPUID EAX=0x1f is not supported.
55 * - CPUID EAX=0xb is not supported.
56 * - The first topology level is 0.
57 *
58 * Returns CB_SUCCESS otherwise.
59 */
60 static enum cb_err read_topology_structure(struct bitfield_descriptor *topology)
61 {
62 const uint32_t cpuid_max_func = cpuid_get_max_func();
63 struct cpuid_result cpuid_regs;
64 int level_num = 0, cpu_id_op;
65 unsigned int level;
66 uint8_t next_level_first_bit, first_bit = 0;
67
68 /*
69 * Not all CPUs support this, those won't get topology filled in here. CPU specific
70 * code can do this however.
71 */
72 if (cpuid_max_func >= CPUID_EXTENDED_CPU_TOPOLOGY2)
73 cpu_id_op = CPUID_EXTENDED_CPU_TOPOLOGY2;
74 else if (cpuid_max_func >= CPUID_EXTENDED_CPU_TOPOLOGY)
75 cpu_id_op = CPUID_EXTENDED_CPU_TOPOLOGY;
76 else
77 return CB_ERR;
78
79 cpuid_regs = cpuid_ext(cpu_id_op, level_num);
80
81 /*
82 * Sub-leaf index 0 enumerates SMT level, some AMD CPUs leave this CPUID leaf reserved
83 * so bail out. Cpu specific code can fill in the topology later.
84 */
85 if (CPUID_CPU_TOPOLOGY_LEVEL(cpuid_regs) != LEVEL_TYPE_SMT)
86 return CB_ERR;
87
88 do {
89 level = CPUID_CPU_TOPOLOGY_LEVEL(cpuid_regs);
90 next_level_first_bit = CPUID_CPU_TOPOLOGY(LEVEL_BITS, cpuid_regs.eax);
91 if (level < LEVEL_TYPE_MAX) {
92 topology[level].num_bits = next_level_first_bit - first_bit;
93 topology[level].first_bit = first_bit;
94 } else {
95 printk(BIOS_ERR, "Unknown processor topology level %d\n", level);
96 }
97 first_bit = next_level_first_bit;
98 level_num++;
99 cpuid_regs = cpuid_ext(cpu_id_op, level_num);
100 /* Stop when level type is invalid i.e 0. */
101 } while (CPUID_CPU_TOPOLOGY_LEVEL(cpuid_regs));
102
103 topology[LEVEL_TYPE_PACKAGE].first_bit = next_level_first_bit;
104 return CB_SUCCESS;
105 }
106
107 void set_cpu_topology(struct device *cpu)
108 {
109 const uint32_t apicid = cpu->path.apic.initial_lapicid;
110 static struct bitfield_descriptor topology[LEVEL_TYPE_MAX];
111 static enum cb_err ret;
112 static bool done;
113 unsigned int core_id_within_package;
114 struct {
115 unsigned int level;
116 unsigned int *field;
117 } apic_fields[] = {
118 { LEVEL_TYPE_SMT, &cpu->path.apic.thread_id },
119 { LEVEL_TYPE_CORE, &cpu->path.apic.core_id },
120 { LEVEL_TYPE_MODULE, &cpu->path.apic.module_id },
121 { LEVEL_TYPE_PACKAGE, &cpu->path.apic.package_id },
122 { LEVEL_TYPE_PACKAGE, &cpu->path.apic.node_id }
123 };
124
125 if (!done) {
126 ret = read_topology_structure(topology);
127 done = true;
128 }
129
130 /*
131 * If no APIC descriptor is found, set the following best-guess defaults:
132 * - 1 package
133 * - core_id = apicid
134 * - no SMP
135 * - 1 node
136 * CPU specific code can always update these later on.
137 */
138 if (ret != CB_SUCCESS) {
139 cpu->path.apic.package_id = 0;
140 cpu->path.apic.core_id = apicid;
141 cpu->path.apic.thread_id = 0;
142 cpu->path.apic.node_id = 0;
143 return;
144 }
145
146 for (size_t i = 0; i < ARRAY_SIZE(apic_fields); i++) {
147 struct bitfield_descriptor *desc = &topology[apic_fields[i].level];
148 if (desc->first_bit || desc->num_bits) {
149 unsigned int value = apicid >> desc->first_bit;
150 if (desc->num_bits)
151 value &= (1 << (desc->num_bits)) - 1;
152 *apic_fields[i].field = value;
153 }
154 }
155
156 core_id_within_package = apicid & ((1 << topology[LEVEL_TYPE_PACKAGE].first_bit) - 1);
157 core_id_within_package >>= topology[LEVEL_TYPE_CORE].first_bit;
158 cpu->path.apic.core_id_within_package = core_id_within_package;
159 }