]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
LoongArch: Retrieve CPU package ID from PPTT when available
authorRong Bao <rong.bao@csmantle.top>
Thu, 23 Jul 2026 14:27:29 +0000 (22:27 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Thu, 23 Jul 2026 14:27:29 +0000 (22:27 +0800)
Currently, the LoongArch CPU topology initialization code calculates
each core's package ID by dividing its physical ID by loongson_sysconf.
cores_per_package. This relies on the assumption that cores_per_package
counts in the same domain as physical IDs.

On Loongson-3B6000 (XB612B0V_1.2), cores_per_package matches the visible
core count -- 24 in this case. However, the physical IDs range from 0 to
31 in a noncontinuous fashion:

        $ cat /proc/cpuinfo | grep -i -F 'global_id'
        global_id               : 0
        global_id               : 1
        global_id               : 4
        global_id               : 5
        global_id               : 6
        global_id               : 7
        global_id               : 8
        global_id               : 9
        global_id               : 10
        global_id               : 11
        global_id               : 14
        global_id               : 15
        global_id               : 16
        global_id               : 17
        global_id               : 20
        global_id               : 21
        global_id               : 22
        global_id               : 23
        global_id               : 26
        global_id               : 27
        global_id               : 28
        global_id               : 29
        global_id               : 30
        global_id               : 31

Retrieve the exact package ID from ACPI PPTT when available, in the same
style as retrieving the core ID and thread ID in parse_acpi_topology().
Use this information in loongson_init_secondary() when the PPTT readout
is successful. The original division logic is kept as a fallback.

Meanwhile, since some existing code paths like loongson3_cpufreq expect
a continuous integer sequence of package IDs in [0, MAX_PACKAGES) when
retrieving from cpu_data[], here we also canonicalize the package ID to
be filled in parse_acpi_topology() to meet such an expectation.

Cc: stable@vger.kernel.org
Tested-by: Mingcong Bai <jeffbai@aosc.io>
Co-developed-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Rong Bao <rong.bao@csmantle.top>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/kernel/acpi.c
arch/loongarch/kernel/smp.c

index 8f650c9ffecdecdac6bcb324123e222bd04dbcf2..873e90990771b0cddbf7023fb6663f43857cf0cb 100644 (file)
@@ -201,10 +201,12 @@ static void __init acpi_process_madt(void)
 }
 
 int pptt_enabled;
+static int acpi_nr_packages;
+static int acpi_package_ids[MAX_PACKAGES];
 
 int __init parse_acpi_topology(void)
 {
-       int cpu, topology_id;
+       int i, cpu, topology_id;
 
        for_each_possible_cpu(cpu) {
                topology_id = find_acpi_cpu_topology(cpu, 0);
@@ -222,6 +224,29 @@ int __init parse_acpi_topology(void)
 
                        cpu_data[cpu].core = topology_id;
                }
+
+               topology_id = find_acpi_cpu_topology_package(cpu);
+               if (topology_id < 0) {
+                       pr_warn("Invalid BIOS PPTT\n");
+                       return -ENOENT;
+               }
+
+               for (i = 0; i < acpi_nr_packages; i++)
+                       if (acpi_package_ids[i] == topology_id)
+                               break;
+
+               if (i == acpi_nr_packages)
+                       acpi_package_ids[acpi_nr_packages++] = topology_id;
+
+               cpu_data[cpu].package = topology_id;
+       }
+
+       for_each_possible_cpu(cpu) {
+               for (i = 0; i < acpi_nr_packages; i++)
+                       if (cpu_data[cpu].package == acpi_package_ids[i]) {
+                               cpu_data[cpu].package = i; /* Canonicalize */
+                               break;
+                       }
        }
 
        pptt_enabled = 1;
index 5d792256bbb99a41d0032add6b5ffacffb20b684..d4b5d1b6bb015b92a9796db37fd34db525912213 100644 (file)
@@ -426,10 +426,10 @@ void loongson_init_secondary(void)
        numa_add_cpu(cpu);
 #endif
        per_cpu(cpu_state, cpu) = CPU_ONLINE;
-       cpu_data[cpu].package =
-                    cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
        cpu_data[cpu].core = pptt_enabled ? cpu_data[cpu].core :
                     cpu_logical_map(cpu) % loongson_sysconf.cores_per_package;
+       cpu_data[cpu].package = pptt_enabled ? cpu_data[cpu].package :
+                    cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
        cpu_data[cpu].global_id = cpu_logical_map(cpu);
 }