5 * Based on arm64 and arc implementations
6 * Copyright (C) 2013 ARM Ltd.
7 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
14 #include <linux/smp.h>
15 #include <linux/cpu.h>
16 #include <linux/sched.h>
17 #include <linux/sched/mm.h>
18 #include <linux/irq.h>
20 #include <asm/cpuinfo.h>
21 #include <asm/mmu_context.h>
22 #include <asm/tlbflush.h>
23 #include <asm/cacheflush.h>
26 asmlinkage __init void secondary_start_kernel(void);
28 static void (*smp_cross_call)(const struct cpumask *, unsigned int);
30 unsigned long secondary_release = -1;
31 struct thread_info *secondary_thread_info;
40 static DEFINE_SPINLOCK(boot_lock);
42 static void boot_secondary(unsigned int cpu, struct task_struct *idle)
45 * set synchronisation state between this boot processor
46 * and the secondary one
48 spin_lock(&boot_lock);
50 secondary_release = cpu;
51 smp_cross_call(cpumask_of(cpu), IPI_WAKEUP);
54 * now the secondary core is starting up let it run its
55 * calibrations, then wait for it to finish
57 spin_unlock(&boot_lock);
60 void __init smp_prepare_boot_cpu(void)
64 void __init smp_init_cpus(void)
66 struct device_node *cpu;
69 for_each_of_cpu_node(cpu) {
70 cpu_id = of_get_cpu_hwid(cpu, 0);
72 set_cpu_possible(cpu_id, true);
76 void __init smp_prepare_cpus(unsigned int max_cpus)
81 * Initialise the present map, which describes the set of CPUs
82 * actually populated at the present time.
84 for_each_possible_cpu(cpu) {
86 set_cpu_present(cpu, true);
90 void __init smp_cpus_done(unsigned int max_cpus)
94 static DECLARE_COMPLETION(cpu_running);
96 int __cpu_up(unsigned int cpu, struct task_struct *idle)
98 if (smp_cross_call == NULL) {
99 pr_warn("CPU%u: failed to start, IPI controller missing",
104 secondary_thread_info = task_thread_info(idle);
105 current_pgd[cpu] = init_mm.pgd;
107 boot_secondary(cpu, idle);
108 if (!wait_for_completion_timeout(&cpu_running,
109 msecs_to_jiffies(1000))) {
110 pr_crit("CPU%u: failed to start\n", cpu);
113 synchronise_count_master(cpu);
118 asmlinkage __init void secondary_start_kernel(void)
120 struct mm_struct *mm = &init_mm;
121 unsigned int cpu = smp_processor_id();
123 * All kernel threads share the same mm context; grab a
124 * reference and switch to it.
127 current->active_mm = mm;
128 cpumask_set_cpu(cpu, mm_cpumask(mm));
130 pr_info("CPU%u: Booted secondary processor\n", cpu);
133 openrisc_clockevent_init();
135 notify_cpu_starting(cpu);
138 * OK, now it's safe to let the boot CPU continue
140 complete(&cpu_running);
142 synchronise_count_slave(cpu);
143 set_cpu_online(cpu, true);
147 * OK, it's off to the idle thread for us
149 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
152 void handle_IPI(unsigned int ipi_msg)
154 unsigned int cpu = smp_processor_id();
165 generic_smp_call_function_interrupt();
168 case IPI_CALL_FUNC_SINGLE:
169 generic_smp_call_function_single_interrupt();
173 WARN(1, "CPU%u: Unknown IPI message 0x%x\n", cpu, ipi_msg);
178 void arch_smp_send_reschedule(int cpu)
180 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
183 static void stop_this_cpu(void *dummy)
185 /* Remove this CPU */
186 set_cpu_online(smp_processor_id(), false);
190 if (mfspr(SPR_UPR) & SPR_UPR_PMP)
191 mtspr(SPR_PMR, mfspr(SPR_PMR) | SPR_PMR_DME);
192 /* If that didn't work, infinite loop */
197 void smp_send_stop(void)
199 smp_call_function(stop_this_cpu, NULL, 0);
202 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
207 void arch_send_call_function_single_ipi(int cpu)
209 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
212 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
214 smp_cross_call(mask, IPI_CALL_FUNC);
217 /* TLB flush operations - Performed on each CPU*/
218 static inline void ipi_flush_tlb_all(void *ignored)
220 local_flush_tlb_all();
223 static inline void ipi_flush_tlb_mm(void *info)
225 struct mm_struct *mm = (struct mm_struct *)info;
227 local_flush_tlb_mm(mm);
230 static void smp_flush_tlb_mm(struct cpumask *cmask, struct mm_struct *mm)
234 if (cpumask_empty(cmask))
239 if (cpumask_any_but(cmask, cpuid) >= nr_cpu_ids) {
240 /* local cpu is the only cpu present in cpumask */
241 local_flush_tlb_mm(mm);
243 on_each_cpu_mask(cmask, ipi_flush_tlb_mm, mm, 1);
248 struct flush_tlb_data {
253 static inline void ipi_flush_tlb_page(void *info)
255 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
257 local_flush_tlb_page(NULL, fd->addr1);
260 static inline void ipi_flush_tlb_range(void *info)
262 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
264 local_flush_tlb_range(NULL, fd->addr1, fd->addr2);
267 static void smp_flush_tlb_range(const struct cpumask *cmask, unsigned long start,
272 if (cpumask_empty(cmask))
277 if (cpumask_any_but(cmask, cpuid) >= nr_cpu_ids) {
278 /* local cpu is the only cpu present in cpumask */
279 if ((end - start) <= PAGE_SIZE)
280 local_flush_tlb_page(NULL, start);
282 local_flush_tlb_range(NULL, start, end);
284 struct flush_tlb_data fd;
289 if ((end - start) <= PAGE_SIZE)
290 on_each_cpu_mask(cmask, ipi_flush_tlb_page, &fd, 1);
292 on_each_cpu_mask(cmask, ipi_flush_tlb_range, &fd, 1);
297 void flush_tlb_all(void)
299 on_each_cpu(ipi_flush_tlb_all, NULL, 1);
302 void flush_tlb_mm(struct mm_struct *mm)
304 smp_flush_tlb_mm(mm_cpumask(mm), mm);
307 void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
309 smp_flush_tlb_range(mm_cpumask(vma->vm_mm), uaddr, uaddr + PAGE_SIZE);
312 void flush_tlb_range(struct vm_area_struct *vma,
313 unsigned long start, unsigned long end)
315 const struct cpumask *cmask = vma ? mm_cpumask(vma->vm_mm)
317 smp_flush_tlb_range(cmask, start, end);
320 /* Instruction cache invalidate - performed on each cpu */
321 static void ipi_icache_page_inv(void *arg)
323 struct page *page = arg;
325 local_icache_page_inv(page);
328 void smp_icache_page_inv(struct page *page)
330 on_each_cpu(ipi_icache_page_inv, page, 1);
332 EXPORT_SYMBOL(smp_icache_page_inv);