Skip to content

Commit 12d3b55

Browse files
committed
LoongArch: Always enumerate MADT and setup logical-physical CPU mapping
Some drivers want to use cpu_logical_map(), early_cpu_to_node() and some other CPU mapping APIs, even if we use "nr_cpus=1" to hard limit the CPU number. This is strongly required for the multi-bridges machines. Currently, we stop parsing the MADT if the nr_cpus limit is reached, but to achieve the above goal we should always enumerate the MADT table and setup logical-physical CPU mapping whether there is a nr_cpus limit. Rework the MADT enumeration: 1. Define a flag "cpu_enumerated" to distinguish the first enumeration (cpu_enumerated=0) and the physical hotplug case (cpu_enumerated=1) for set_processor_mask(). 2. If cpu_enumerated=0, stop parsing only when NR_CPUS limit is reached, so we can setup logical-physical CPU mapping; if cpu_enumerated=1, stop parsing when nr_cpu_ids limit is reached, so we can avoid some runtime bugs. Once logical-physical CPU mapping is setup, we will let cpu_enumerated=1. 3. Use find_first_zero_bit() instead of cpumask_next_zero() to find the next zero bit (free logical CPU id) in the cpu_present_mask, because cpumask_next_zero() will stop at nr_cpu_ids. 4. Only touch cpu_possible_mask if cpu_enumerated=0, this is in order to avoid some potential crashes, because cpu_possible_mask is marked as __ro_after_init. 5. In prefill_possible_map(), clear cpu_present_mask bits greater than nr_cpu_ids, in order to avoid a CPU be "present" but not "possible". Signed-off-by: Huacai Chen <[email protected]>
1 parent 7697a0f commit 12d3b55

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

arch/loongarch/kernel/acpi.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,36 @@ void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
5757
return ioremap_cache(phys, size);
5858
}
5959

60+
static int cpu_enumerated = 0;
61+
6062
#ifdef CONFIG_SMP
6163
static int set_processor_mask(u32 id, u32 flags)
6264
{
63-
65+
int nr_cpus;
6466
int cpu, cpuid = id;
6567

66-
if (num_processors >= nr_cpu_ids) {
67-
pr_warn(PREFIX "nr_cpus/possible_cpus limit of %i reached."
68-
" processor 0x%x ignored.\n", nr_cpu_ids, cpuid);
68+
if (!cpu_enumerated)
69+
nr_cpus = NR_CPUS;
70+
else
71+
nr_cpus = nr_cpu_ids;
72+
73+
if (num_processors >= nr_cpus) {
74+
pr_warn(PREFIX "nr_cpus limit of %i reached."
75+
" processor 0x%x ignored.\n", nr_cpus, cpuid);
6976

7077
return -ENODEV;
7178

7279
}
7380
if (cpuid == loongson_sysconf.boot_cpu_id)
7481
cpu = 0;
7582
else
76-
cpu = cpumask_next_zero(-1, cpu_present_mask);
83+
cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
84+
85+
if (!cpu_enumerated)
86+
set_cpu_possible(cpu, true);
7787

7888
if (flags & ACPI_MADT_ENABLED) {
7989
num_processors++;
80-
set_cpu_possible(cpu, true);
8190
set_cpu_present(cpu, true);
8291
__cpu_number_map[cpuid] = cpu;
8392
__cpu_logical_map[cpu] = cpuid;
@@ -138,6 +147,7 @@ static void __init acpi_process_madt(void)
138147
acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC,
139148
acpi_parse_eio_master, MAX_IO_PICS);
140149

150+
cpu_enumerated = 1;
141151
loongson_sysconf.nr_cpus = num_processors;
142152
}
143153

arch/loongarch/kernel/setup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,10 @@ static void __init prefill_possible_map(void)
576576

577577
for (i = 0; i < possible; i++)
578578
set_cpu_possible(i, true);
579-
for (; i < NR_CPUS; i++)
579+
for (; i < NR_CPUS; i++) {
580+
set_cpu_present(i, false);
580581
set_cpu_possible(i, false);
582+
}
581583

582584
set_nr_cpu_ids(possible);
583585
}

arch/loongarch/kernel/smp.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,10 @@ static void __init fdt_smp_setup(void)
271271
if (cpuid >= nr_cpu_ids)
272272
continue;
273273

274-
if (cpuid == loongson_sysconf.boot_cpu_id) {
274+
if (cpuid == loongson_sysconf.boot_cpu_id)
275275
cpu = 0;
276-
} else {
277-
cpu = cpumask_next_zero(-1, cpu_present_mask);
278-
}
276+
else
277+
cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
279278

280279
num_processors++;
281280
set_cpu_possible(cpu, true);

0 commit comments

Comments
 (0)