// SPDX-License-Identifier: GPL-2.0-only /* * CPU/APIC topology * * The APIC IDs describe the system topology in multiple domain levels. * The CPUID topology parser provides the information which part of the * APIC ID is associated to the individual levels: * * [PACKAGE][DIEGRP][DIE][TILE][MODULE][CORE][THREAD] * * The root space contains the package (socket) IDs. * * Not enumerated levels consume 0 bits space, but conceptually they are * always represented. If e.g. only CORE and THREAD levels are enumerated * then the DIE, MODULE and TILE have the same physical ID as the PACKAGE. * * If SMT is not supported, then the THREAD domain is still used. It then * has the same physical ID as the CORE domain and is the only child of * the core domain. * * This allows a unified view on the system independent of the enumerated * domain levels without requiring any conditionals in the code.
*/ #define pr_fmt(fmt) "CPU topo: " fmt #include <linux/cpu.h>
/* * Map cpu index to physical APIC ID
*/
DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_apicid, BAD_APICID);
DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_acpiid, CPU_ACPIID_INVALID);
EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_acpiid);
/* Bitmap of physically present CPUs. */
DECLARE_BITMAP(phys_cpu_present_map, MAX_LOCAL_APIC) __read_mostly;
/* Used for CPU number allocation and parallel CPU bringup */
u32 cpuid_to_apicid[] __ro_after_init = { [0 ... NR_CPUS - 1] = BAD_APICID, };
/* Bitmaps to mark registered APICs at each topology domain */ staticstruct { DECLARE_BITMAP(map, MAX_LOCAL_APIC); } apic_maps[TOPO_MAX_DOMAIN] __ro_after_init;
/* * Keep track of assigned, disabled and rejected CPUs. Present assigned * with 1 as CPU #0 is reserved for the boot CPU.
*/ staticstruct { unsignedint nr_assigned_cpus; unsignedint nr_disabled_cpus; unsignedint nr_rejected_cpus;
u32 boot_cpu_apic_id;
u32 real_bsp_apic_id;
} topo_info __ro_after_init = {
.nr_assigned_cpus = 1,
.boot_cpu_apic_id = BAD_APICID,
.real_bsp_apic_id = BAD_APICID,
};
/* * Convert the APIC ID to a domain level ID by masking out the low bits * below the domain level @dom.
*/ staticinline u32 topo_apicid(u32 apicid, enum x86_topology_domains dom)
{ if (dom == TOPO_SMT_DOMAIN) return apicid; return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
}
staticint topo_lookup_cpuid(u32 apic_id)
{ int i;
/* CPU# to APICID mapping is persistent once it is established */ for (i = 0; i < topo_info.nr_assigned_cpus; i++) { if (cpuid_to_apicid[i] == apic_id) return i;
} return -ENODEV;
}
static __init int topo_get_cpunr(u32 apic_id)
{ int cpu = topo_lookup_cpuid(apic_id);
/* * There is no real good way to detect whether this a kdump() * kernel, but except on the Voyager SMP monstrosity which is not * longer supported, the real BSP APIC ID is the first one which is * enumerated by firmware. That allows to detect whether the boot * CPU is the real BSP. If it is not, then do not register the APIC * because sending INIT to the real BSP would reset the whole * system. * * The first APIC ID which is enumerated by firmware is detectable * because the boot CPU APIC ID is registered before that without * invoking this code.
*/ if (topo_info.real_bsp_apic_id != BAD_APICID) returnfalse;
/* * Check whether the enumeration order is broken by evaluating the * BSP bit in the APICBASE MSR. If the CPU does not have the * APICBASE MSR then the BSP detection is not possible and the * kernel must rely on the firmware enumeration order.
*/ if (has_apic_base) {
rdmsrq(MSR_IA32_APICBASE, msr);
is_bsp = !!(msr & MSR_IA32_APICBASE_BSP);
}
if (apic_id == topo_info.boot_cpu_apic_id) { /* * If the boot CPU has the APIC BSP bit set then the * firmware enumeration is agreeing. If the CPU does not * have the APICBASE MSR then the only choice is to trust * the enumeration order.
*/ if (is_bsp || !has_apic_base) {
topo_info.real_bsp_apic_id = apic_id; returnfalse;
} /* * If the boot APIC is enumerated first, but the APICBASE * MSR does not have the BSP bit set, then there is no way * to discover the real BSP here. Assume a crash kernel and * limit the number of CPUs to 1 as an INIT to the real BSP * would reset the machine.
*/
pr_warn("Enumerated BSP APIC %x is not marked in APICBASE MSR\n", apic_id);
pr_warn("Assuming crash kernel. Limiting to one CPU to prevent machine INIT\n");
set_nr_cpu_ids(1); goto fwbug;
}
pr_warn("Boot CPU APIC ID not the first enumerated APIC ID: %x != %x\n",
topo_info.boot_cpu_apic_id, apic_id);
if (is_bsp) { /* * The boot CPU has the APIC BSP bit set. Use it and complain * about the broken firmware enumeration.
*/
topo_info.real_bsp_apic_id = topo_info.boot_cpu_apic_id; goto fwbug;
}
pr_warn("Crash kernel detected. Disabling real BSP to prevent machine INIT\n");
topo_info.real_bsp_apic_id = apic_id; returntrue;
fwbug:
pr_warn(FW_BUG "APIC enumeration order not specification compliant\n"); returnfalse;
}
/* Calculate the exclusive end */
end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]);
/* Unfortunately there is no bitmap_weight_range() */ for (id = find_next_bit(map, end, lvlid); id < end; id = find_next_bit(map, end, ++id))
cnt++; return cnt;
}
if (present) {
set_bit(apic_id, phys_cpu_present_map);
/* * Double registration is valid in case of the boot CPU * APIC because that is registered before the enumeration * of the APICs via firmware parsers or VM guest * mechanisms.
*/ if (apic_id == topo_info.boot_cpu_apic_id)
cpu = 0; else
cpu = topo_get_cpunr(apic_id);
/* * Check for present APICs in the same package when running * on bare metal. Allow the bogosity in a guest.
*/ if (hypervisor_is_type(X86_HYPER_NATIVE) &&
topo_unit_count(pkgid, TOPO_PKG_DOMAIN, phys_cpu_present_map)) {
pr_info_once("Ignoring hot-pluggable APIC ID %x in present package.\n",
apic_id);
topo_info.nr_rejected_cpus++; return;
}
topo_info.nr_disabled_cpus++;
}
/* * Register present and possible CPUs in the domain * maps. cpu_possible_map will be updated in * topology_init_possible_cpus() after enumeration is done.
*/ for (dom = TOPO_SMT_DOMAIN; dom < TOPO_MAX_DOMAIN; dom++)
set_bit(topo_apicid(apic_id, dom), apic_maps[dom].map);
}
/** * topology_register_apic - Register an APIC in early topology maps * @apic_id: The APIC ID to set up * @acpi_id: The ACPI ID associated to the APIC * @present: True if the corresponding CPU is present
*/ void __init topology_register_apic(u32 apic_id, u32 acpi_id, bool present)
{ if (apic_id >= MAX_LOCAL_APIC) {
pr_err_once("APIC ID %x exceeds kernel limit of: %x\n", apic_id, MAX_LOCAL_APIC - 1);
topo_info.nr_rejected_cpus++; return;
}
if (check_for_real_bsp(apic_id)) {
topo_info.nr_rejected_cpus++; return;
}
/* CPU numbers exhausted? */ if (apic_id != topo_info.boot_cpu_apic_id && topo_info.nr_assigned_cpus >= nr_cpu_ids) {
pr_warn_once("CPU limit of %d reached. Ignoring further CPUs\n", nr_cpu_ids);
topo_info.nr_rejected_cpus++; return;
}
topo_register_apic(apic_id, acpi_id, present);
}
/** * topology_register_boot_apic - Register the boot CPU APIC * @apic_id: The APIC ID to set up * * Separate so CPU #0 can be assigned
*/ void __init topology_register_boot_apic(u32 apic_id)
{
WARN_ON_ONCE(topo_info.boot_cpu_apic_id != BAD_APICID);
/** * topology_get_logical_id - Retrieve the logical ID at a given topology domain level * @apicid: The APIC ID for which to lookup the logical ID * @at_level: The topology domain level to use * * @apicid must be a full APIC ID, not the normalized variant. It's valid to have * all bits below the domain level specified by @at_level to be clear. So both * real APIC IDs and backshifted normalized APIC IDs work correctly. * * Returns: * - >= 0: The requested logical ID * - -ERANGE: @apicid is out of range * - -ENODEV: @apicid is not registered
*/ int topology_get_logical_id(u32 apicid, enum x86_topology_domains at_level)
{ /* Remove the bits below @at_level to get the proper level ID of @apicid */ unsignedint lvlid = topo_apicid(apicid, at_level);
if (lvlid >= MAX_LOCAL_APIC) return -ERANGE; if (!test_bit(lvlid, apic_maps[at_level].map)) return -ENODEV; /* Get the number of set bits before @lvlid. */ return bitmap_weight(apic_maps[at_level].map, lvlid);
}
EXPORT_SYMBOL_GPL(topology_get_logical_id);
/** * topology_unit_count - Retrieve the count of specified units at a given topology domain level * @apicid: The APIC ID which specifies the search range * @which_units: The domain level specifying the units to count * @at_level: The domain level at which @which_units have to be counted * * This returns the number of possible units according to the enumerated * information. * * E.g. topology_count_units(apicid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN) * counts the number of possible cores in the package to which @apicid * belongs. * * @at_level must obviously be greater than @which_level to produce useful * results. If @at_level is equal to @which_units the result is * unsurprisingly 1. If @at_level is less than @which_units the results * is by definition undefined and the function returns 0.
*/ unsignedint topology_unit_count(u32 apicid, enum x86_topology_domains which_units, enum x86_topology_domains at_level)
{ /* Remove the bits below @at_level to get the proper level ID of @apicid */ unsignedint lvlid = topo_apicid(apicid, at_level);
if (lvlid >= MAX_LOCAL_APIC) return 0; if (!test_bit(lvlid, apic_maps[at_level].map)) return 0; if (which_units > at_level) return 0; if (which_units == at_level) return 1; return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
}
#ifdef CONFIG_SMP int topology_get_primary_thread(unsignedint cpu)
{
u32 apic_id = cpuid_to_apicid[cpu];
/* * Get the core domain level APIC id, which is the primary thread * and return the CPU number assigned to it.
*/ return topo_lookup_cpuid(topo_apicid(apic_id, TOPO_CORE_DOMAIN));
} #endif
#ifdef CONFIG_ACPI_HOTPLUG_CPU /** * topology_hotplug_apic - Handle a physical hotplugged APIC after boot * @apic_id: The APIC ID to set up * @acpi_id: The ACPI ID associated to the APIC
*/ int topology_hotplug_apic(u32 apic_id, u32 acpi_id)
{ int cpu;
if (apic_id >= MAX_LOCAL_APIC) return -EINVAL;
/* Reject if the APIC ID was not registered during enumeration. */ if (!test_bit(apic_id, apic_maps[TOPO_SMT_DOMAIN].map)) return -ENODEV;
cpu = topo_lookup_cpuid(apic_id); if (cpu < 0) return -ENOSPC;
/** * topology_hotunplug_apic - Remove a physical hotplugged APIC after boot * @cpu: The CPU number for which the APIC ID is removed
*/ void topology_hotunplug_apic(unsignedint cpu)
{
u32 apic_id = cpuid_to_apicid[cpu];
/** * topology_apply_cmdline_limits_early - Apply topology command line limits early * * Ensure that command line limits are in effect before firmware parsing * takes place.
*/ void __init topology_apply_cmdline_limits_early(void)
{ unsignedint possible = nr_cpu_ids;
/* 'maxcpus=0' 'nosmp' 'nolapic' */ if (!setup_max_cpus || apic_is_disabled)
possible = 1;
/* 'possible_cpus=N' */
possible = min_t(unsignedint, max_possible_cpus, possible);
if (possible < nr_cpu_ids) {
pr_info("Limiting to %u possible CPUs\n", possible);
set_nr_cpu_ids(possible);
}
}
static __init bool restrict_to_up(void)
{ if (!smp_found_config) returntrue; /* * XEN PV is special as it does not advertise the local APIC * properly, but provides a fake topology for it so that the * infrastructure works. So don't apply the restrictions vs. APIC * here.
*/ if (xen_pv_domain()) returnfalse;
/* * If there was no APIC registered, then fake one so that the * topology bitmap is populated. That ensures that the code below * is valid and the various query interfaces can be used * unconditionally. This does not affect the actual APIC code in * any way because either the local APIC address has not been * registered or the local APIC was disabled on the command line.
*/ if (topo_info.boot_cpu_apic_id == BAD_APICID)
topology_register_boot_apic(0);
pr_info("Max. logical packages: %3u\n", cnta);
pr_info("Max. logical dies: %3u\n", cntb);
pr_info("Max. dies per package: %3u\n", __max_dies_per_package);
cnta = domain_weight(TOPO_CORE_DOMAIN);
cntb = domain_weight(TOPO_SMT_DOMAIN); /* * Can't use order delta here as order(cnta) can be equal * order(cntb) even if cnta != cntb.
*/
__max_threads_per_core = DIV_ROUND_UP(cntb, cnta);
pr_info("Max. threads per core: %3u\n", __max_threads_per_core);
/* * Late SMP disable after sizing CPU masks when APIC/IOAPIC setup failed.
*/ void __init topology_reset_possible_cpus_up(void)
{
init_cpu_present(cpumask_of(0));
init_cpu_possible(cpumask_of(0));
bitmap_zero(phys_cpu_present_map, MAX_LOCAL_APIC); if (topo_info.boot_cpu_apic_id != BAD_APICID)
set_bit(topo_info.boot_cpu_apic_id, phys_cpu_present_map);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.