[PATCH v2 1/5] x86/topology: Export helper to get CPU number from APIC ID

From: Yazen Ghannam
Date: Mon Jun 24 2024 - 17:20:34 EST


The need to look up a CPU number from an APIC ID is done in at least one
other place outside of APIC/topology code: apei_smca_report_x86_error().

With the recent topology rework, there is now a helper function to do
just this task.

Export the helper so other code can use it. Also, update the name to
match other exported topology_* helpers.

Signed-off-by: Yazen Ghannam <yazen.ghannam@xxxxxxx>
---
Link:
https://lkml.kernel.org/r/20240618172447.GA1387@xxxxxxxxxxxxxxxxx

v1->v2:
* New in v2.

arch/x86/include/asm/topology.h | 6 ++++++
arch/x86/kernel/cpu/topology.c | 15 ++++++++++++---
2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h
index abe3a8f22cbd..d8c3c3c818bc 100644
--- a/arch/x86/include/asm/topology.h
+++ b/arch/x86/include/asm/topology.h
@@ -171,11 +171,17 @@ static inline unsigned int topology_num_threads_per_package(void)

#ifdef CONFIG_X86_LOCAL_APIC
int topology_get_logical_id(u32 apicid, enum x86_topology_domains at_level);
+int topology_get_cpunr(u32 apic_id);
#else
static inline int topology_get_logical_id(u32 apicid, enum x86_topology_domains at_level)
{
return 0;
}
+
+static inline int topology_get_cpunr(u32 apic_id)
+{
+ return -ENODEV;
+}
#endif

#ifdef CONFIG_SMP
diff --git a/arch/x86/kernel/cpu/topology.c b/arch/x86/kernel/cpu/topology.c
index 621a151ccf7d..fc74578ee3bd 100644
--- a/arch/x86/kernel/cpu/topology.c
+++ b/arch/x86/kernel/cpu/topology.c
@@ -95,7 +95,15 @@ static inline u32 topo_apicid(u32 apicid, enum x86_topology_domains dom)
return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
}

-static int topo_lookup_cpuid(u32 apic_id)
+/**
+ * topology_get_cpunr - Retrieve the CPU number for the given APIC ID
+ * @apic_id: The APIC ID for which to lookup the CPU number
+ *
+ * Returns:
+ * - >= 0: The CPU number for the given APIC ID
+ * - -ENODEV: @apic_id does not match any known CPU
+ */
+int topology_get_cpunr(u32 apic_id)
{
int i;

@@ -106,10 +114,11 @@ static int topo_lookup_cpuid(u32 apic_id)
}
return -ENODEV;
}
+EXPORT_SYMBOL_GPL(topology_get_cpunr);

static __init int topo_get_cpunr(u32 apic_id)
{
- int cpu = topo_lookup_cpuid(apic_id);
+ int cpu = topology_get_cpunr(apic_id);

if (cpu >= 0)
return cpu;
@@ -388,7 +397,7 @@ int topology_hotplug_apic(u32 apic_id, u32 acpi_id)
if (!test_bit(apic_id, apic_maps[TOPO_SMT_DOMAIN].map))
return -ENODEV;

- cpu = topo_lookup_cpuid(apic_id);
+ cpu = topology_get_cpunr(apic_id);
if (cpu < 0)
return -ENOSPC;

--
2.34.1