1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/kernel.h>
7 #include <linux/sched.h>
8 #include <linux/kernel_stat.h>
9 #include <linux/notifier.h>
10 #include <linux/cpu.h>
11 #include <linux/percpu.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
17 #include <linux/sched/task_stack.h>
18 #include <linux/sched/mm.h>
19 #include <linux/sched/hotplug.h>
21 #include <asm/traps.h>
22 #include <asm/sections.h>
23 #include <asm/mmu_context.h>
24 #include <asm/pgalloc.h>
25 #ifdef CONFIG_CPU_HAS_FPU
29 struct ipi_data_struct {
30 unsigned long bits ____cacheline_aligned;
32 static DEFINE_PER_CPU(struct ipi_data_struct, ipi_data);
34 enum ipi_message_type {
41 static irqreturn_t handle_ipi(int irq, void *dev)
46 ops = xchg(&this_cpu_ptr(&ipi_data)->bits, 0);
50 if (ops & (1 << IPI_RESCHEDULE))
53 if (ops & (1 << IPI_CALL_FUNC))
54 generic_smp_call_function_interrupt();
56 BUG_ON((ops >> IPI_MAX) != 0);
62 static void (*send_arch_ipi)(const struct cpumask *mask);
65 void __init set_send_ipi(void (*func)(const struct cpumask *mask), int irq)
75 send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
79 for_each_cpu(i, to_whom)
80 set_bit(operation, &per_cpu_ptr(&ipi_data, i)->bits);
83 send_arch_ipi(to_whom);
86 void arch_send_call_function_ipi_mask(struct cpumask *mask)
88 send_ipi_message(mask, IPI_CALL_FUNC);
91 void arch_send_call_function_single_ipi(int cpu)
93 send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
96 static void ipi_stop(void *unused)
101 void smp_send_stop(void)
103 on_each_cpu(ipi_stop, NULL, 1);
106 void smp_send_reschedule(int cpu)
108 send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
111 void __init smp_prepare_boot_cpu(void)
115 void __init smp_prepare_cpus(unsigned int max_cpus)
119 static int ipi_dummy_dev;
121 void __init setup_smp_ipi(void)
128 rc = request_percpu_irq(ipi_irq, handle_ipi, "IPI Interrupt",
131 panic("%s IRQ request failed\n", __func__);
133 enable_percpu_irq(ipi_irq, 0);
136 void __init setup_smp(void)
138 struct device_node *node = NULL;
141 for_each_of_cpu_node(node) {
142 if (!of_device_is_available(node))
145 if (of_property_read_u32(node, "reg", &cpu))
151 set_cpu_possible(cpu, true);
152 set_cpu_present(cpu, true);
156 extern void _start_smp_secondary(void);
158 volatile unsigned int secondary_hint;
159 volatile unsigned int secondary_ccr;
160 volatile unsigned int secondary_stack;
162 unsigned long secondary_msa1;
164 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
166 unsigned long mask = 1 << cpu;
169 (unsigned int) task_stack_page(tidle) + THREAD_SIZE - 8;
170 secondary_hint = mfcr("cr31");
171 secondary_ccr = mfcr("cr18");
172 secondary_msa1 = read_mmu_msa1();
175 * Because other CPUs are in reset status, we must flush data
176 * from cache to out and secondary CPUs use them in
177 * csky_start_secondary(void)
181 if (mask & mfcr("cr<29, 0>")) {
182 send_arch_ipi(cpumask_of(cpu));
184 /* Enable cpu in SMP reset ctrl reg */
185 mask |= mfcr("cr<29, 0>");
186 mtcr("cr<29, 0>", mask);
189 /* Wait for the cpu online */
190 while (!cpu_online(cpu));
197 void __init smp_cpus_done(unsigned int max_cpus)
201 int setup_profiling_timer(unsigned int multiplier)
206 void csky_start_secondary(void)
208 struct mm_struct *mm = &init_mm;
209 unsigned int cpu = smp_processor_id();
211 mtcr("cr31", secondary_hint);
212 mtcr("cr18", secondary_ccr);
214 mtcr("vbr", vec_base);
217 write_mmu_pagemask(0);
218 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);
219 TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir);
221 #ifdef CONFIG_CPU_HAS_FPU
225 enable_percpu_irq(ipi_irq, 0);
229 current->active_mm = mm;
230 cpumask_set_cpu(cpu, mm_cpumask(mm));
232 notify_cpu_starting(cpu);
233 set_cpu_online(cpu, true);
235 pr_info("CPU%u Online: %s...\n", cpu, __func__);
239 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
242 #ifdef CONFIG_HOTPLUG_CPU
243 int __cpu_disable(void)
245 unsigned int cpu = smp_processor_id();
247 set_cpu_online(cpu, false);
249 irq_migrate_all_off_this_cpu();
251 clear_tasks_mm_cpumask(cpu);
256 void __cpu_die(unsigned int cpu)
258 if (!cpu_wait_death(cpu, 5)) {
259 pr_crit("CPU%u: shutdown failed\n", cpu);
262 pr_notice("CPU%u: shutdown\n", cpu);
265 void arch_cpu_idle_dead(void)
271 while (!secondary_stack)
279 "jmpi csky_start_secondary"
281 : "r" (secondary_stack));