LoongArch: Give a chance to build with !CONFIG_SMP
authorTiezhu Yang <yangtiezhu@loongson.cn>
Tue, 14 May 2024 04:24:18 +0000 (12:24 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Tue, 14 May 2024 04:24:18 +0000 (12:24 +0800)
In the current code, SMP is selected in Kconfig for LoongArch, the users
can not unset it, this is reasonable for a multi-processor machine. But
as the help info of config SMP said, if you have a system with only one
CPU, say N. On a uni-processor machine, the kernel will run faster if you
say N here.

Loongson-2K0500 is a single-core CPU for applications like industrial
control, printing terminals, and BMC (Baseboard Management Controller),
there are many development boards, products and solutions on the market,
so it is better and necessary to give a chance to build with !CONFIG_SMP
for a uni-processor machine.

First of all, do not select SMP for config LOONGARCH in Kconfig to make
it possible to unset CONFIG_SMP. Then, do some changes to fix warnings
and errors if CONFIG_SMP is not set.

(1) Define get_ipi_irq() only if CONFIG_SMP is set to fix the warning:
arch/loongarch/kernel/irq.c:90:19: warning: 'get_ipi_irq' defined but not used [-Wunused-function]

(2) Add "#ifdef CONFIG_SMP" in asm/smp.h to fix the warning:
./arch/loongarch/include/asm/smp.h:49:9: warning: "raw_smp_processor_id" redefined
   49 | #define raw_smp_processor_id raw_smp_processor_id
      |         ^~~~~~~~~~~~~~~~~~~~
./include/linux/smp.h:198:9: note: this is the location of the previous definition
  198 | #define raw_smp_processor_id()                  0

(3) Define machine_shutdown() as empty under !CONFIG_SMP to fix the error:
arch/loongarch/kernel/machine_kexec.c: In function 'machine_shutdown':
arch/loongarch/kernel/machine_kexec.c:233:25: error: implicit declaration of function 'cpu_device_up'; did you mean 'put_device'? [-Wimplicit-function-declaration]

(4) Make config SCHED_SMT depends on SMP to fix many errors such as:
kernel/sched/core.c: In function 'sched_core_find':
kernel/sched/core.c:310:43: error: 'struct rq' has no member named 'cpu'

(5) Define cpu_logical_map(cpu) as 0 under !CONFIG_SMP in asm/smp.h,
then include asm/smp.h in asm/acpi.h (because acpi.h is included in
linux/irq.h indirectly) to fix many build errors under drivers/irqchip
such as:
drivers/irqchip/irq-loongson-eiointc.c: In function 'cpu_to_eio_node':
drivers/irqchip/irq-loongson-eiointc.c:59:16: error: implicit declaration of function 'cpu_logical_map' [-Wimplicit-function-declaration]

(6) Do not write per_cpu_offset(0) to PERCPU_BASE_KS when resume because
the per_cpu_offset(x) macro is defined as (__per_cpu_offset[x]) only
under CONFIG_SMP in include/asm-generic/percpu.h. Just save the value of
PERCPU_BASE_KS when suspend and restore it when resume to fix the error:
arch/loongarch/power/suspend.c: In function 'loongarch_common_resume':
arch/loongarch/power/suspend.c:47:21: error: implicit declaration of function 'per_cpu_offset' [-Wimplicit-function-declaration]

(7) Fix huge page handling under !CONFIG_SMP in tlbex.S.

When running the UnixBench tests with "-c 1" single-streamed pass, the
improvement of performance is about 9 percent with this patch.

By the way, it is helpful to debug and analysis the kernel issues of
multi-processor system under !CONFIG_SMP.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/Kconfig
arch/loongarch/include/asm/acpi.h
arch/loongarch/include/asm/smp.h
arch/loongarch/kernel/irq.c
arch/loongarch/kernel/machine_kexec.c
arch/loongarch/mm/tlbex.S
arch/loongarch/power/suspend.c

index e22330064e98a4bf903906dfe71ed7392fb0926b..73246b21aae840eb91dcee334b78e0ac1751600b 100644 (file)
@@ -178,7 +178,6 @@ config LOONGARCH
        select PCI_QUIRKS
        select PERF_USE_VMALLOC
        select RTC_LIB
-       select SMP
        select SPARSE_IRQ
        select SYSCTL_ARCH_UNALIGN_ALLOW
        select SYSCTL_ARCH_UNALIGN_NO_WARN
@@ -424,6 +423,7 @@ config EFI_STUB
 
 config SCHED_SMT
        bool "SMT scheduler support"
+       depends on SMP
        default y
        help
          Improves scheduler's performance when there are multiple
index 49e29b29996f0f4473c5d628c936c7528630ad52..313f66f7913af362f91e91355886258d0ae4237e 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef _ASM_LOONGARCH_ACPI_H
 #define _ASM_LOONGARCH_ACPI_H
 
+#include <asm/smp.h>
 #include <asm/suspend.h>
 
 #ifdef CONFIG_ACPI
index f81e5f01d61905f5b8d7da4786ba512258381acd..b3a0868d29eace8ca5debc7ee6541e06e6e224c1 100644 (file)
@@ -6,6 +6,8 @@
 #ifndef __ASM_SMP_H
 #define __ASM_SMP_H
 
+#ifdef CONFIG_SMP
+
 #include <linux/atomic.h>
 #include <linux/bitops.h>
 #include <linux/linkage.h>
@@ -101,4 +103,8 @@ static inline void __cpu_die(unsigned int cpu)
 }
 #endif
 
+#else /* !CONFIG_SMP */
+#define cpu_logical_map(cpu)   0
+#endif /* CONFIG_SMP */
+
 #endif /* __ASM_SMP_H */
index 883e5066ae445fa157c74fc9eb65939a5770f159..e791fa275ec59cefa9ed90470b5396d29aaec933 100644 (file)
@@ -87,6 +87,7 @@ static void __init init_vec_parent_group(void)
        acpi_table_parse(ACPI_SIG_MCFG, early_pci_mcfg_parse);
 }
 
+#ifdef CONFIG_SMP
 static int __init get_ipi_irq(void)
 {
        struct irq_domain *d = irq_find_matching_fwnode(cpuintc_handle, DOMAIN_BUS_ANY);
@@ -96,6 +97,7 @@ static int __init get_ipi_irq(void)
 
        return -EINVAL;
 }
+#endif
 
 void __init init_IRQ(void)
 {
index 2dcb9e003657c848adff71078870fe682451e416..8ae641dc53bb77b8964c869177aa72976a440a3d 100644 (file)
@@ -225,6 +225,7 @@ void crash_smp_send_stop(void)
 
 void machine_shutdown(void)
 {
+#ifdef CONFIG_SMP
        int cpu;
 
        /* All CPUs go to reboot_code_buffer */
@@ -232,7 +233,6 @@ void machine_shutdown(void)
                if (!cpu_online(cpu))
                        cpu_device_up(get_cpu_device(cpu));
 
-#ifdef CONFIG_SMP
        smp_call_function(kexec_shutdown_secondary, NULL, 0);
 #endif
 }
index a44387b838af61b3598b734dca6c5efc6c749a55..c08682a89c5824d9d5e6a6ca13f90e6fdd8d6df5 100644 (file)
@@ -125,6 +125,8 @@ vmalloc_load:
 tlb_huge_update_load:
 #ifdef CONFIG_SMP
        ll.d            ra, t1, 0
+#else
+       rotri.d         ra, ra, 64 - (_PAGE_HUGE_SHIFT + 1)
 #endif
        andi            t0, ra, _PAGE_PRESENT
        beqz            t0, nopage_tlb_load
@@ -135,7 +137,6 @@ tlb_huge_update_load:
        beqz            t0, tlb_huge_update_load
        ori             t0, ra, _PAGE_VALID
 #else
-       rotri.d         ra, ra, 64 - (_PAGE_HUGE_SHIFT + 1)
        ori             t0, ra, _PAGE_VALID
        st.d            t0, t1, 0
 #endif
@@ -281,6 +282,8 @@ vmalloc_store:
 tlb_huge_update_store:
 #ifdef CONFIG_SMP
        ll.d            ra, t1, 0
+#else
+       rotri.d         ra, ra, 64 - (_PAGE_HUGE_SHIFT + 1)
 #endif
        andi            t0, ra, _PAGE_PRESENT | _PAGE_WRITE
        xori            t0, t0, _PAGE_PRESENT | _PAGE_WRITE
@@ -292,7 +295,6 @@ tlb_huge_update_store:
        beqz            t0, tlb_huge_update_store
        ori             t0, ra, (_PAGE_VALID | _PAGE_DIRTY | _PAGE_MODIFIED)
 #else
-       rotri.d         ra, ra, 64 - (_PAGE_HUGE_SHIFT + 1)
        ori             t0, ra, (_PAGE_VALID | _PAGE_DIRTY | _PAGE_MODIFIED)
        st.d            t0, t1, 0
 #endif
@@ -438,6 +440,8 @@ vmalloc_modify:
 tlb_huge_update_modify:
 #ifdef CONFIG_SMP
        ll.d            ra, t1, 0
+#else
+       rotri.d         ra, ra, 64 - (_PAGE_HUGE_SHIFT + 1)
 #endif
        andi            t0, ra, _PAGE_WRITE
        beqz            t0, nopage_tlb_modify
@@ -448,7 +452,6 @@ tlb_huge_update_modify:
        beqz            t0, tlb_huge_update_modify
        ori             t0, ra, (_PAGE_VALID | _PAGE_DIRTY | _PAGE_MODIFIED)
 #else
-       rotri.d         ra, ra, 64 - (_PAGE_HUGE_SHIFT + 1)
        ori             t0, ra, (_PAGE_VALID | _PAGE_DIRTY | _PAGE_MODIFIED)
        st.d            t0, t1, 0
 #endif
index 166d9e06a64bdf1d52217d52ec0a00a53ce904fc..c9e594925c473759d84f39637ba508d8f714ba06 100644 (file)
@@ -24,6 +24,7 @@ struct saved_registers {
        u64 kpgd;
        u32 pwctl0;
        u32 pwctl1;
+       u64 pcpu_base;
 };
 static struct saved_registers saved_regs;
 
@@ -36,6 +37,7 @@ void loongarch_common_suspend(void)
        saved_regs.pwctl1 = csr_read32(LOONGARCH_CSR_PWCTL1);
        saved_regs.ecfg = csr_read32(LOONGARCH_CSR_ECFG);
        saved_regs.euen = csr_read32(LOONGARCH_CSR_EUEN);
+       saved_regs.pcpu_base = csr_read64(PERCPU_BASE_KS);
 
        loongarch_suspend_addr = loongson_sysconf.suspend_addr;
 }
@@ -44,7 +46,6 @@ void loongarch_common_resume(void)
 {
        sync_counter();
        local_flush_tlb_all();
-       csr_write64(per_cpu_offset(0), PERCPU_BASE_KS);
        csr_write64(eentry, LOONGARCH_CSR_EENTRY);
        csr_write64(eentry, LOONGARCH_CSR_MERRENTRY);
        csr_write64(tlbrentry, LOONGARCH_CSR_TLBRENTRY);
@@ -55,6 +56,7 @@ void loongarch_common_resume(void)
        csr_write32(saved_regs.pwctl1, LOONGARCH_CSR_PWCTL1);
        csr_write32(saved_regs.ecfg, LOONGARCH_CSR_ECFG);
        csr_write32(saved_regs.euen, LOONGARCH_CSR_EUEN);
+       csr_write64(saved_regs.pcpu_base, PERCPU_BASE_KS);
 }
 
 int loongarch_acpi_suspend(void)