xref: /linux/arch/openrisc/kernel/smp.c (revision c056718464512da06d7f65a27d5e4f1707b24c80)
1 /*
2  * Copyright (C) 2014 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
3  * Copyright (C) 2017 Stafford Horne <shorne@gmail.com>
4  *
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)
8  *
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.
12  */
13 
14 #include <linux/smp.h>
15 #include <linux/cpu.h>
16 #include <linux/sched.h>
17 #include <linux/irq.h>
18 #include <asm/cpuinfo.h>
19 #include <asm/mmu_context.h>
20 #include <asm/tlbflush.h>
21 #include <asm/time.h>
22 
23 static void (*smp_cross_call)(const struct cpumask *, unsigned int);
24 
25 unsigned long secondary_release = -1;
26 struct thread_info *secondary_thread_info;
27 
28 enum ipi_msg_type {
29 	IPI_WAKEUP,
30 	IPI_RESCHEDULE,
31 	IPI_CALL_FUNC,
32 	IPI_CALL_FUNC_SINGLE,
33 };
34 
35 static DEFINE_SPINLOCK(boot_lock);
36 
37 static void boot_secondary(unsigned int cpu, struct task_struct *idle)
38 {
39 	/*
40 	 * set synchronisation state between this boot processor
41 	 * and the secondary one
42 	 */
43 	spin_lock(&boot_lock);
44 
45 	secondary_release = cpu;
46 	smp_cross_call(cpumask_of(cpu), IPI_WAKEUP);
47 
48 	/*
49 	 * now the secondary core is starting up let it run its
50 	 * calibrations, then wait for it to finish
51 	 */
52 	spin_unlock(&boot_lock);
53 }
54 
55 void __init smp_prepare_boot_cpu(void)
56 {
57 }
58 
59 void __init smp_init_cpus(void)
60 {
61 	int i;
62 
63 	for (i = 0; i < NR_CPUS; i++)
64 		set_cpu_possible(i, true);
65 }
66 
67 void __init smp_prepare_cpus(unsigned int max_cpus)
68 {
69 	int i;
70 
71 	/*
72 	 * Initialise the present map, which describes the set of CPUs
73 	 * actually populated at the present time.
74 	 */
75 	for (i = 0; i < max_cpus; i++)
76 		set_cpu_present(i, true);
77 }
78 
79 void __init smp_cpus_done(unsigned int max_cpus)
80 {
81 }
82 
83 static DECLARE_COMPLETION(cpu_running);
84 
85 int __cpu_up(unsigned int cpu, struct task_struct *idle)
86 {
87 	if (smp_cross_call == NULL) {
88 		pr_warn("CPU%u: failed to start, IPI controller missing",
89 			cpu);
90 		return -EIO;
91 	}
92 
93 	secondary_thread_info = task_thread_info(idle);
94 	current_pgd[cpu] = init_mm.pgd;
95 
96 	boot_secondary(cpu, idle);
97 	if (!wait_for_completion_timeout(&cpu_running,
98 					msecs_to_jiffies(1000))) {
99 		pr_crit("CPU%u: failed to start\n", cpu);
100 		return -EIO;
101 	}
102 
103 	return 0;
104 }
105 
106 asmlinkage __init void secondary_start_kernel(void)
107 {
108 	struct mm_struct *mm = &init_mm;
109 	unsigned int cpu = smp_processor_id();
110 	/*
111 	 * All kernel threads share the same mm context; grab a
112 	 * reference and switch to it.
113 	 */
114 	atomic_inc(&mm->mm_count);
115 	current->active_mm = mm;
116 	cpumask_set_cpu(cpu, mm_cpumask(mm));
117 
118 	pr_info("CPU%u: Booted secondary processor\n", cpu);
119 
120 	setup_cpuinfo();
121 	openrisc_clockevent_init();
122 
123 	notify_cpu_starting(cpu);
124 
125 	/*
126 	 * OK, now it's safe to let the boot CPU continue
127 	 */
128 	set_cpu_online(cpu, true);
129 	complete(&cpu_running);
130 
131 	local_irq_enable();
132 
133 	preempt_disable();
134 	/*
135 	 * OK, it's off to the idle thread for us
136 	 */
137 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
138 }
139 
140 void handle_IPI(unsigned int ipi_msg)
141 {
142 	unsigned int cpu = smp_processor_id();
143 
144 	switch (ipi_msg) {
145 	case IPI_WAKEUP:
146 		break;
147 
148 	case IPI_RESCHEDULE:
149 		scheduler_ipi();
150 		break;
151 
152 	case IPI_CALL_FUNC:
153 		generic_smp_call_function_interrupt();
154 		break;
155 
156 	case IPI_CALL_FUNC_SINGLE:
157 		generic_smp_call_function_single_interrupt();
158 		break;
159 
160 	default:
161 		WARN(1, "CPU%u: Unknown IPI message 0x%x\n", cpu, ipi_msg);
162 		break;
163 	}
164 }
165 
166 void smp_send_reschedule(int cpu)
167 {
168 	smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
169 }
170 
171 static void stop_this_cpu(void *dummy)
172 {
173 	/* Remove this CPU */
174 	set_cpu_online(smp_processor_id(), false);
175 
176 	local_irq_disable();
177 	/* CPU Doze */
178 	if (mfspr(SPR_UPR) & SPR_UPR_PMP)
179 		mtspr(SPR_PMR, mfspr(SPR_PMR) | SPR_PMR_DME);
180 	/* If that didn't work, infinite loop */
181 	while (1)
182 		;
183 }
184 
185 void smp_send_stop(void)
186 {
187 	smp_call_function(stop_this_cpu, NULL, 0);
188 }
189 
190 /* not supported, yet */
191 int setup_profiling_timer(unsigned int multiplier)
192 {
193 	return -EINVAL;
194 }
195 
196 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
197 {
198 	smp_cross_call = fn;
199 }
200 
201 void arch_send_call_function_single_ipi(int cpu)
202 {
203 	smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
204 }
205 
206 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
207 {
208 	smp_cross_call(mask, IPI_CALL_FUNC);
209 }
210 
211 /* TLB flush operations - Performed on each CPU*/
212 static inline void ipi_flush_tlb_all(void *ignored)
213 {
214 	local_flush_tlb_all();
215 }
216 
217 void flush_tlb_all(void)
218 {
219 	on_each_cpu(ipi_flush_tlb_all, NULL, 1);
220 }
221 
222 /*
223  * FIXME: implement proper functionality instead of flush_tlb_all.
224  * *But*, as things currently stands, the local_tlb_flush_* functions will
225  * all boil down to local_tlb_flush_all anyway.
226  */
227 void flush_tlb_mm(struct mm_struct *mm)
228 {
229 	on_each_cpu(ipi_flush_tlb_all, NULL, 1);
230 }
231 
232 void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
233 {
234 	on_each_cpu(ipi_flush_tlb_all, NULL, 1);
235 }
236 
237 void flush_tlb_range(struct vm_area_struct *vma,
238 		     unsigned long start, unsigned long end)
239 {
240 	on_each_cpu(ipi_flush_tlb_all, NULL, 1);
241 }
242