xref: /linux/arch/arc/kernel/smp.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4  *
5  * RajeshwarR: Dec 11, 2007
6  *   -- Added support for Inter Processor Interrupts
7  *
8  * Vineetg: Nov 1st, 2007
9  *    -- Initial Write (Borrowed heavily from ARM)
10  */
11 
12 #include <linux/spinlock.h>
13 #include <linux/sched/mm.h>
14 #include <linux/interrupt.h>
15 #include <linux/profile.h>
16 #include <linux/mm.h>
17 #include <linux/cpu.h>
18 #include <linux/irq.h>
19 #include <linux/atomic.h>
20 #include <linux/cpumask.h>
21 #include <linux/reboot.h>
22 #include <linux/irqdomain.h>
23 #include <linux/export.h>
24 #include <linux/of_fdt.h>
25 #include <linux/string.h>
26 
27 #include <asm/mach_desc.h>
28 #include <asm/setup.h>
29 #include <asm/smp.h>
30 #include <asm/processor.h>
31 
32 #ifndef CONFIG_ARC_HAS_LLSC
33 arch_spinlock_t smp_atomic_ops_lock = __ARCH_SPIN_LOCK_UNLOCKED;
34 
35 EXPORT_SYMBOL_GPL(smp_atomic_ops_lock);
36 #endif
37 
38 struct plat_smp_ops  __weak plat_smp_ops;
39 
40 /* XXX: per cpu ? Only needed once in early secondary boot */
41 struct task_struct *secondary_idle_tsk;
42 
43 static int __init arc_get_cpu_map(const char *name, struct cpumask *cpumask)
44 {
45 	unsigned long dt_root = of_get_flat_dt_root();
46 	const char *buf;
47 	int len;
48 
49 	buf = of_get_flat_dt_prop(dt_root, name, &len);
50 	if (!buf || !memchr(buf, '\0', len))
51 		return -EINVAL;
52 
53 	if (cpulist_parse(buf, cpumask))
54 		return -EINVAL;
55 
56 	return 0;
57 }
58 
59 /*
60  * Read from DeviceTree and setup cpu possible mask. If there is no
61  * "possible-cpus" property in DeviceTree pretend all [0..NR_CPUS-1] exist.
62  */
63 static void __init arc_init_cpu_possible(void)
64 {
65 	struct cpumask cpumask;
66 
67 	if (arc_get_cpu_map("possible-cpus", &cpumask)) {
68 		pr_warn("Failed to get possible-cpus from dtb, pretending all %u cpus exist\n",
69 			NR_CPUS);
70 
71 		cpumask_setall(&cpumask);
72 	}
73 
74 	if (!cpumask_test_cpu(0, &cpumask))
75 		panic("Master cpu (cpu[0]) is missed in cpu possible mask!");
76 
77 	init_cpu_possible(&cpumask);
78 }
79 
80 /*
81  * Called from setup_arch() before calling setup_processor()
82  *
83  * - Initialise the CPU possible map early - this describes the CPUs
84  *   which may be present or become present in the system.
85  * - Call early smp init hook. This can initialize a specific multi-core
86  *   IP which is say common to several platforms (hence not part of
87  *   platform specific int_early() hook)
88  */
89 void __init smp_init_cpus(void)
90 {
91 	arc_init_cpu_possible();
92 
93 	if (plat_smp_ops.init_early_smp)
94 		plat_smp_ops.init_early_smp();
95 }
96 
97 /* called from init ( ) =>  process 1 */
98 void __init smp_prepare_cpus(unsigned int max_cpus)
99 {
100 	/*
101 	 * if platform didn't set the present map already, do it now
102 	 * boot cpu is set to present already by init/main.c
103 	 */
104 	if (num_present_cpus() <= 1)
105 		init_cpu_present(cpu_possible_mask);
106 }
107 
108 void __init smp_cpus_done(unsigned int max_cpus)
109 {
110 
111 }
112 
113 /*
114  * Default smp boot helper for Run-on-reset case where all cores start off
115  * together. Non-masters need to wait for Master to start running.
116  * This is implemented using a flag in memory, which Non-masters spin-wait on.
117  * Master sets it to cpu-id of core to "ungate" it.
118  */
119 static volatile int wake_flag;
120 
121 #ifdef CONFIG_ISA_ARCOMPACT
122 
123 #define __boot_read(f)		f
124 #define __boot_write(f, v)	f = v
125 
126 #else
127 
128 #define __boot_read(f)		arc_read_uncached_32(&f)
129 #define __boot_write(f, v)	arc_write_uncached_32(&f, v)
130 
131 #endif
132 
133 static void arc_default_smp_cpu_kick(int cpu, unsigned long pc)
134 {
135 	BUG_ON(cpu == 0);
136 
137 	__boot_write(wake_flag, cpu);
138 }
139 
140 void arc_platform_smp_wait_to_boot(int cpu)
141 {
142 	/* for halt-on-reset, we've waited already */
143 	if (IS_ENABLED(CONFIG_ARC_SMP_HALT_ON_RESET))
144 		return;
145 
146 	while (__boot_read(wake_flag) != cpu)
147 		;
148 
149 	__boot_write(wake_flag, 0);
150 }
151 
152 const char *arc_platform_smp_cpuinfo(void)
153 {
154 	return plat_smp_ops.info ? : "";
155 }
156 
157 /*
158  * The very first "C" code executed by secondary
159  * Called from asm stub in head.S
160  * "current"/R25 already setup by low level boot code
161  */
162 void start_kernel_secondary(void)
163 {
164 	struct mm_struct *mm = &init_mm;
165 	unsigned int cpu = smp_processor_id();
166 
167 	/* MMU, Caches, Vector Table, Interrupts etc */
168 	setup_processor();
169 
170 	mmget(mm);
171 	mmgrab(mm);
172 	current->active_mm = mm;
173 	cpumask_set_cpu(cpu, mm_cpumask(mm));
174 
175 	/* Some SMP H/w setup - for each cpu */
176 	if (plat_smp_ops.init_per_cpu)
177 		plat_smp_ops.init_per_cpu(cpu);
178 
179 	if (machine_desc->init_per_cpu)
180 		machine_desc->init_per_cpu(cpu);
181 
182 	notify_cpu_starting(cpu);
183 	set_cpu_online(cpu, true);
184 
185 	pr_info("## CPU%u LIVE ##: Executing Code...\n", cpu);
186 
187 	local_irq_enable();
188 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
189 }
190 
191 /*
192  * Called from kernel_init( ) -> smp_init( ) - for each CPU
193  *
194  * At this point, Secondary Processor  is "HALT"ed:
195  *  -It booted, but was halted in head.S
196  *  -It was configured to halt-on-reset
197  *  So need to wake it up.
198  *
199  * Essential requirements being where to run from (PC) and stack (SP)
200 */
201 int __cpu_up(unsigned int cpu, struct task_struct *idle)
202 {
203 	unsigned long wait_till;
204 
205 	secondary_idle_tsk = idle;
206 
207 	pr_info("Idle Task [%d] %p", cpu, idle);
208 	pr_info("Trying to bring up CPU%u ...\n", cpu);
209 
210 	if (plat_smp_ops.cpu_kick)
211 		plat_smp_ops.cpu_kick(cpu,
212 				(unsigned long)first_lines_of_secondary);
213 	else
214 		arc_default_smp_cpu_kick(cpu, (unsigned long)NULL);
215 
216 	/* wait for 1 sec after kicking the secondary */
217 	wait_till = jiffies + HZ;
218 	while (time_before(jiffies, wait_till)) {
219 		if (cpu_online(cpu))
220 			break;
221 	}
222 
223 	if (!cpu_online(cpu)) {
224 		pr_info("Timeout: CPU%u FAILED to come up !!!\n", cpu);
225 		return -1;
226 	}
227 
228 	secondary_idle_tsk = NULL;
229 
230 	return 0;
231 }
232 
233 /*****************************************************************************/
234 /*              Inter Processor Interrupt Handling                           */
235 /*****************************************************************************/
236 
237 enum ipi_msg_type {
238 	IPI_EMPTY = 0,
239 	IPI_RESCHEDULE = 1,
240 	IPI_CALL_FUNC,
241 	IPI_CPU_STOP,
242 };
243 
244 /*
245  * In arches with IRQ for each msg type (above), receiver can use IRQ-id  to
246  * figure out what msg was sent. For those which don't (ARC has dedicated IPI
247  * IRQ), the msg-type needs to be conveyed via per-cpu data
248  */
249 
250 static DEFINE_PER_CPU(unsigned long, ipi_data);
251 
252 static void ipi_send_msg_one(int cpu, enum ipi_msg_type msg)
253 {
254 	unsigned long __percpu *ipi_data_ptr = per_cpu_ptr(&ipi_data, cpu);
255 	unsigned long old, new;
256 	unsigned long flags;
257 
258 	pr_debug("%d Sending msg [%d] to %d\n", smp_processor_id(), msg, cpu);
259 
260 	local_irq_save(flags);
261 
262 	/*
263 	 * Atomically write new msg bit (in case others are writing too),
264 	 * and read back old value
265 	 */
266 	do {
267 		new = old = *ipi_data_ptr;
268 		new |= 1U << msg;
269 	} while (cmpxchg(ipi_data_ptr, old, new) != old);
270 
271 	/*
272 	 * Call the platform specific IPI kick function, but avoid if possible:
273 	 * Only do so if there's no pending msg from other concurrent sender(s).
274 	 * Otherwise, receiver will see this msg as well when it takes the
275 	 * IPI corresponding to that msg. This is true, even if it is already in
276 	 * IPI handler, because !@old means it has not yet dequeued the msg(s)
277 	 * so @new msg can be a free-loader
278 	 */
279 	if (plat_smp_ops.ipi_send && !old)
280 		plat_smp_ops.ipi_send(cpu);
281 
282 	local_irq_restore(flags);
283 }
284 
285 static void ipi_send_msg(const struct cpumask *callmap, enum ipi_msg_type msg)
286 {
287 	unsigned int cpu;
288 
289 	for_each_cpu(cpu, callmap)
290 		ipi_send_msg_one(cpu, msg);
291 }
292 
293 void arch_smp_send_reschedule(int cpu)
294 {
295 	ipi_send_msg_one(cpu, IPI_RESCHEDULE);
296 }
297 
298 void smp_send_stop(void)
299 {
300 	struct cpumask targets;
301 	cpumask_copy(&targets, cpu_online_mask);
302 	cpumask_clear_cpu(smp_processor_id(), &targets);
303 	ipi_send_msg(&targets, IPI_CPU_STOP);
304 }
305 
306 void arch_send_call_function_single_ipi(int cpu)
307 {
308 	ipi_send_msg_one(cpu, IPI_CALL_FUNC);
309 }
310 
311 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
312 {
313 	ipi_send_msg(mask, IPI_CALL_FUNC);
314 }
315 
316 /*
317  * ipi_cpu_stop - handle IPI from smp_send_stop()
318  */
319 static void ipi_cpu_stop(void)
320 {
321 	machine_halt();
322 }
323 
324 static inline int __do_IPI(unsigned long msg)
325 {
326 	int rc = 0;
327 
328 	switch (msg) {
329 	case IPI_RESCHEDULE:
330 		scheduler_ipi();
331 		break;
332 
333 	case IPI_CALL_FUNC:
334 		generic_smp_call_function_interrupt();
335 		break;
336 
337 	case IPI_CPU_STOP:
338 		ipi_cpu_stop();
339 		break;
340 
341 	default:
342 		rc = 1;
343 	}
344 
345 	return rc;
346 }
347 
348 /*
349  * arch-common ISR to handle for inter-processor interrupts
350  * Has hooks for platform specific IPI
351  */
352 static irqreturn_t do_IPI(int irq, void *dev_id)
353 {
354 	unsigned long pending;
355 	unsigned long __maybe_unused copy;
356 
357 	pr_debug("IPI [%ld] received on cpu %d\n",
358 		 *this_cpu_ptr(&ipi_data), smp_processor_id());
359 
360 	if (plat_smp_ops.ipi_clear)
361 		plat_smp_ops.ipi_clear(irq);
362 
363 	/*
364 	 * "dequeue" the msg corresponding to this IPI (and possibly other
365 	 * piggybacked msg from elided IPIs: see ipi_send_msg_one() above)
366 	 */
367 	copy = pending = xchg(this_cpu_ptr(&ipi_data), 0);
368 
369 	do {
370 		unsigned long msg = __ffs(pending);
371 		int rc;
372 
373 		rc = __do_IPI(msg);
374 		if (rc)
375 			pr_info("IPI with bogus msg %ld in %ld\n", msg, copy);
376 		pending &= ~(1U << msg);
377 	} while (pending);
378 
379 	return IRQ_HANDLED;
380 }
381 
382 /*
383  * API called by platform code to hookup arch-common ISR to their IPI IRQ
384  *
385  * Note: If IPI is provided by platform (vs. say ARC MCIP), their intc setup/map
386  * function needs to call irq_set_percpu_devid() for IPI IRQ, otherwise
387  * request_percpu_irq() below will fail
388  */
389 static DEFINE_PER_CPU(int, ipi_dev);
390 
391 int smp_ipi_irq_setup(int cpu, irq_hw_number_t hwirq)
392 {
393 	int *dev = per_cpu_ptr(&ipi_dev, cpu);
394 	unsigned int virq = irq_find_mapping(NULL, hwirq);
395 
396 	if (!virq)
397 		panic("Cannot find virq for root domain and hwirq=%lu", hwirq);
398 
399 	/* Boot cpu calls request, all call enable */
400 	if (!cpu) {
401 		int rc;
402 
403 		rc = request_percpu_irq(virq, do_IPI, "IPI Interrupt", dev);
404 		if (rc)
405 			panic("Percpu IRQ request failed for %u\n", virq);
406 	}
407 
408 	enable_percpu_irq(virq, 0);
409 
410 	return 0;
411 }
412