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