cpu/x86/mp_init.c: Keep track of initial lapic ID inside device_path

It's quite confusing to keep track of lapic ID inside the device
struct and initial lapic ID inside an array.

Change-Id: I4d9f8d23c0b0e5c142f6907593428d8509e4e7bb
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/64342
Reviewed-by: Maximilian Brune <maximilian.brune@9elements.com>
Reviewed-by: Patrick Rudolph <siro@das-labor.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/arch/x86/cpu.c b/src/arch/x86/cpu.c
index b0227c2..60b236c 100644
--- a/src/arch/x86/cpu.c
+++ b/src/arch/x86/cpu.c
@@ -211,24 +211,6 @@
 	cpu->ops = driver ? driver->ops : NULL;
 }
 
-/* Keep track of default APIC ids for SMM. */
-static int cpus_default_apic_id[CONFIG_MAX_CPUS];
-
-/* Function to keep track of cpu default apic_id */
-void cpu_add_map_entry(unsigned int index)
-{
-	cpus_default_apic_id[index] = initial_lapicid();
-}
-
-/* Returns default APIC id based on logical_cpu number or < 0 on failure. */
-int cpu_get_apic_id(int logical_cpu)
-{
-	if (logical_cpu >= CONFIG_MAX_CPUS || logical_cpu < 0)
-		return -1;
-
-	return cpus_default_apic_id[logical_cpu];
-}
-
 void cpu_initialize(void)
 {
 	/* Because we busy wait at the printk spinlock.
diff --git a/src/cpu/x86/mp_init.c b/src/cpu/x86/mp_init.c
index 8301e80..2909022 100644
--- a/src/cpu/x86/mp_init.c
+++ b/src/cpu/x86/mp_init.c
@@ -195,18 +195,16 @@
 
 	set_cpu_info(index, dev);
 
-	struct cpu_info *info = cpu_info();
-	cpu_add_map_entry(info->index);
-
 	/* Fix up APIC id with reality. */
-	info->cpu->path.apic.apic_id = lapicid();
+	dev->path.apic.apic_id = lapicid();
+	dev->path.apic.initial_lapicid = initial_lapicid();
 
 	if (cpu_is_intel())
-		printk(BIOS_INFO, "AP: slot %zu apic_id %x, MCU rev: 0x%08x\n", info->index,
-		       info->cpu->path.apic.apic_id, get_current_microcode_rev());
+		printk(BIOS_INFO, "AP: slot %u apic_id %x, MCU rev: 0x%08x\n", index,
+		       dev->path.apic.apic_id, get_current_microcode_rev());
 	else
-		printk(BIOS_INFO, "AP: slot %zu apic_id %x\n", info->index,
-		       info->cpu->path.apic.apic_id);
+		printk(BIOS_INFO, "AP: slot %u apic_id %x\n", index,
+		       dev->path.apic.apic_id);
 
 	/* Walk the flight plan */
 	ap_do_flight_plan();
@@ -547,6 +545,7 @@
 		printk(BIOS_CRIT, "Failed to find or allocate BSP struct device\n");
 		return CB_ERR;
 	}
+	bsp->path.apic.initial_lapicid = initial_lapicid();
 
 	/* Find the device structure for the boot CPU. */
 	set_cpu_info(0, bsp);
@@ -558,9 +557,6 @@
 		printk(BIOS_CRIT, "BSP index(%zd) != 0!\n", info->index);
 		return CB_ERR;
 	}
-
-	/* Track BSP in cpu_map structures. */
-	cpu_add_map_entry(info->index);
 	return CB_SUCCESS;
 }
 
@@ -748,11 +744,12 @@
 
 static void adjust_smm_apic_id_map(struct smm_loader_params *smm_params)
 {
-	int i;
 	struct smm_stub_params *stub_params = smm_params->stub_params;
 
-	for (i = 0; i < CONFIG_MAX_CPUS; i++)
-		stub_params->apic_id_to_cpu[i] = cpu_get_apic_id(i);
+	int i = 0;
+	for (struct device *dev = g_cpu_bus->children; dev; dev = dev->sibling)
+		if (dev->enabled)
+			stub_params->apic_id_to_cpu[i++] = dev->path.apic.initial_lapicid;
 }
 
 static enum cb_err install_relocation_handler(int num_cpus, size_t save_state_size)
diff --git a/src/device/cpu_device.c b/src/device/cpu_device.c
index f406e23..9185cc6 100644
--- a/src/device/cpu_device.c
+++ b/src/device/cpu_device.c
@@ -13,6 +13,7 @@
 	/* Build the CPU device path */
 	cpu_path.type = DEVICE_PATH_APIC;
 	cpu_path.apic.apic_id = apic_id;
+	cpu_path.apic.initial_lapicid = apic_id;
 
 	/* Update CPU in devicetree. */
 	if (enabled)
diff --git a/src/include/cpu/cpu.h b/src/include/cpu/cpu.h
index a77cb33..fc662ee 100644
--- a/src/include/cpu/cpu.h
+++ b/src/include/cpu/cpu.h
@@ -7,11 +7,7 @@
 #include <stdint.h>
 
 void cpu_initialize(void);
-/* Returns default APIC id based on logical_cpu number or < 0 on failure. */
-int cpu_get_apic_id(int logical_cpu);
 uintptr_t cpu_get_lapic_addr(void);
-/* Function to keep track of cpu default apic_id */
-void cpu_add_map_entry(unsigned int index);
 struct bus;
 int cpu_phys_address_size(void);
 
diff --git a/src/include/device/path.h b/src/include/device/path.h
index a1ea42c..c0df66b 100644
--- a/src/include/device/path.h
+++ b/src/include/device/path.h
@@ -73,6 +73,7 @@
 };
 
 struct apic_path {
+	unsigned int initial_lapicid;
 	unsigned int apic_id;
 	unsigned int package_id;
 	unsigned int node_id;