Skip to content

Commit 3205c98

Browse files
committed
x86/cpu/topology: Retrieve cores per package from topology bitmaps
Similar to other sizing information the number of cores per package can be established from the topology bitmap. Provide a function for retrieving that information and replace the buggy hack in the CPUID evaluation with it. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Michael Kelley <mhklinux@outlook.com> Tested-by: Sohil Mehta <sohil.mehta@intel.com> Link: https://lore.kernel.org/r/20240213210252.956858282@linutronix.de
1 parent 380414b commit 3205c98

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

arch/x86/kernel/cpu/topology.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,49 @@ int topology_get_logical_id(u32 apicid, enum x86_topology_domains at_level)
257257
}
258258
EXPORT_SYMBOL_GPL(topology_get_logical_id);
259259

260+
/**
261+
* topology_unit_count - Retrieve the count of specified units at a given topology domain level
262+
* @apicid: The APIC ID which specifies the search range
263+
* @which_units: The domain level specifying the units to count
264+
* @at_level: The domain level at which @which_units have to be counted
265+
*
266+
* This returns the number of possible units according to the enumerated
267+
* information.
268+
*
269+
* E.g. topology_count_units(apicid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN)
270+
* counts the number of possible cores in the package to which @apicid
271+
* belongs.
272+
*
273+
* @at_level must obviously be greater than @which_level to produce useful
274+
* results. If @at_level is equal to @which_units the result is
275+
* unsurprisingly 1. If @at_level is less than @which_units the results
276+
* is by definition undefined and the function returns 0.
277+
*/
278+
unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units,
279+
enum x86_topology_domains at_level)
280+
{
281+
/* Remove the bits below @at_level to get the proper level ID of @apicid */
282+
unsigned int lvlid = topo_apicid(apicid, at_level);
283+
unsigned int id, end, cnt = 0;
284+
285+
if (lvlid >= MAX_LOCAL_APIC)
286+
return 0;
287+
if (!test_bit(lvlid, apic_maps[at_level].map))
288+
return 0;
289+
if (which_units > at_level)
290+
return 0;
291+
if (which_units == at_level)
292+
return 1;
293+
294+
/* Calculate the exclusive end */
295+
end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]);
296+
/* Unfortunately there is no bitmap_weight_range() */
297+
for (id = find_next_bit(apic_maps[which_units].map, end, lvlid);
298+
id < end; id = find_next_bit(apic_maps[which_units].map, end, ++id))
299+
cnt++;
300+
return cnt;
301+
}
302+
260303
#ifdef CONFIG_ACPI_HOTPLUG_CPU
261304
/**
262305
* topology_hotplug_apic - Handle a physical hotplugged APIC after boot

arch/x86/kernel/cpu/topology.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,15 @@ static inline void topology_update_dom(struct topo_scan *tscan, enum x86_topolog
5353
tscan->dom_ncpus[dom] = ncpus;
5454
}
5555

56+
#ifdef CONFIG_X86_LOCAL_APIC
57+
unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units,
58+
enum x86_topology_domains at_level);
59+
#else
60+
static inline unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units,
61+
enum x86_topology_domains at_level)
62+
{
63+
return 1;
64+
}
65+
#endif
66+
5667
#endif /* ARCH_X86_TOPOLOGY_H */

arch/x86/kernel/cpu/topology_common.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,15 @@ static void topo_set_ids(struct topo_scan *tscan)
155155
c->topo.core_id = (apicid & topo_domain_mask(TOPO_PKG_DOMAIN)) >>
156156
x86_topo_system.dom_shifts[TOPO_SMT_DOMAIN];
157157

158+
/* Maximum number of cores on this package */
159+
c->x86_max_cores = topology_unit_count(apicid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN);
160+
158161
c->topo.amd_node_id = tscan->amd_node_id;
159162

160163
if (c->x86_vendor == X86_VENDOR_AMD)
161164
cpu_topology_fixup_amd(tscan);
162165
}
163166

164-
static void topo_set_max_cores(struct topo_scan *tscan)
165-
{
166-
/*
167-
* Bug compatible for now. This is broken on hybrid systems:
168-
* 8 cores SMT + 8 cores w/o SMT
169-
* tscan.dom_ncpus[TOPO_DIEGRP_DOMAIN] = 24; 24 / 2 = 12 !!
170-
*
171-
* Cannot be fixed without further topology enumeration changes.
172-
*/
173-
tscan->c->x86_max_cores = tscan->dom_ncpus[TOPO_DIEGRP_DOMAIN] >>
174-
x86_topo_system.dom_shifts[TOPO_SMT_DOMAIN];
175-
}
176-
177167
void cpu_parse_topology(struct cpuinfo_x86 *c)
178168
{
179169
unsigned int dom, cpu = smp_processor_id();
@@ -201,7 +191,6 @@ void cpu_parse_topology(struct cpuinfo_x86 *c)
201191
}
202192

203193
topo_set_ids(&tscan);
204-
topo_set_max_cores(&tscan);
205194
}
206195

207196
void __init cpu_init_topology(struct cpuinfo_x86 *c)
@@ -223,7 +212,6 @@ void __init cpu_init_topology(struct cpuinfo_x86 *c)
223212
}
224213

225214
topo_set_ids(&tscan);
226-
topo_set_max_cores(&tscan);
227215

228216
/*
229217
* AMD systems have Nodes per package which cannot be mapped to

0 commit comments

Comments
 (0)