xref: /linux/arch/hexagon/kernel/smp.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SMP support for Hexagon
4  *
5  * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
6  */
7 
8 #include <linux/err.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/percpu.h>
15 #include <linux/sched/mm.h>
16 #include <linux/smp.h>
17 #include <linux/spinlock.h>
18 #include <linux/cpu.h>
19 #include <linux/mm_types.h>
20 
21 #include <asm/time.h>    /*  timer_interrupt  */
22 #include <asm/hexagon_vm.h>
23 
24 #define BASE_IPI_IRQ 26
25 
26 /*
27  * cpu_possible_mask needs to be filled out prior to setup_per_cpu_areas
28  * (which is prior to any of our smp_prepare_cpu crap), in order to set
29  * up the...  per_cpu areas.
30  */
31 
32 struct ipi_data {
33 	unsigned long bits;
34 };
35 
36 static DEFINE_PER_CPU(struct ipi_data, ipi_data);
37 
38 static inline void __handle_ipi(unsigned long *ops, struct ipi_data *ipi,
39 				int cpu)
40 {
41 	unsigned long msg = 0;
42 	do {
43 		msg = find_next_bit(ops, BITS_PER_LONG, msg+1);
44 
45 		switch (msg) {
46 
47 		case IPI_TIMER:
48 			ipi_timer();
49 			break;
50 
51 		case IPI_CALL_FUNC:
52 			generic_smp_call_function_interrupt();
53 			break;
54 
55 		case IPI_CPU_STOP:
56 			/*
57 			 * call vmstop()
58 			 */
59 			__vmstop();
60 			break;
61 
62 		case IPI_RESCHEDULE:
63 			scheduler_ipi();
64 			break;
65 		}
66 	} while (msg < BITS_PER_LONG);
67 }
68 
69 /*  Used for IPI call from other CPU's to unmask int  */
70 void smp_vm_unmask_irq(void *info)
71 {
72 	__vmintop_locen((long) info);
73 }
74 
75 
76 /*
77  * This is based on Alpha's IPI stuff.
78  * Supposed to take (int, void*) as args now.
79  * Specifically, first arg is irq, second is the irq_desc.
80  */
81 
82 irqreturn_t handle_ipi(int irq, void *desc)
83 {
84 	int cpu = smp_processor_id();
85 	struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
86 	unsigned long ops;
87 
88 	while ((ops = xchg(&ipi->bits, 0)) != 0)
89 		__handle_ipi(&ops, ipi, cpu);
90 	return IRQ_HANDLED;
91 }
92 
93 void send_ipi(const struct cpumask *cpumask, enum ipi_message_type msg)
94 {
95 	unsigned long flags;
96 	unsigned long cpu;
97 	unsigned long retval;
98 
99 	local_irq_save(flags);
100 
101 	for_each_cpu(cpu, cpumask) {
102 		struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
103 
104 		set_bit(msg, &ipi->bits);
105 		/*  Possible barrier here  */
106 		retval = __vmintop_post(BASE_IPI_IRQ+cpu);
107 
108 		if (retval != 0) {
109 			printk(KERN_ERR "interrupt %ld not configured?\n",
110 				BASE_IPI_IRQ+cpu);
111 		}
112 	}
113 
114 	local_irq_restore(flags);
115 }
116 
117 static struct irqaction ipi_intdesc = {
118 	.handler = handle_ipi,
119 	.flags = IRQF_TRIGGER_RISING,
120 	.name = "ipi_handler"
121 };
122 
123 void __init smp_prepare_boot_cpu(void)
124 {
125 }
126 
127 /*
128  * interrupts should already be disabled from the VM
129  * SP should already be correct; need to set THREADINFO_REG
130  * to point to current thread info
131  */
132 
133 void start_secondary(void)
134 {
135 	unsigned int cpu;
136 	unsigned long thread_ptr;
137 
138 	/*  Calculate thread_info pointer from stack pointer  */
139 	__asm__ __volatile__(
140 		"%0 = SP;\n"
141 		: "=r" (thread_ptr)
142 	);
143 
144 	thread_ptr = thread_ptr & ~(THREAD_SIZE-1);
145 
146 	__asm__ __volatile__(
147 		QUOTED_THREADINFO_REG " = %0;\n"
148 		:
149 		: "r" (thread_ptr)
150 	);
151 
152 	/*  Set the memory struct  */
153 	mmgrab(&init_mm);
154 	current->active_mm = &init_mm;
155 
156 	cpu = smp_processor_id();
157 
158 	setup_irq(BASE_IPI_IRQ + cpu, &ipi_intdesc);
159 
160 	/*  Register the clock_event dummy  */
161 	setup_percpu_clockdev();
162 
163 	printk(KERN_INFO "%s cpu %d\n", __func__, current_thread_info()->cpu);
164 
165 	notify_cpu_starting(cpu);
166 
167 	set_cpu_online(cpu, true);
168 
169 	local_irq_enable();
170 
171 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
172 }
173 
174 
175 /*
176  * called once for each present cpu
177  * apparently starts up the CPU and then
178  * maintains control until "cpu_online(cpu)" is set.
179  */
180 
181 int __cpu_up(unsigned int cpu, struct task_struct *idle)
182 {
183 	struct thread_info *thread = (struct thread_info *)idle->stack;
184 	void *stack_start;
185 
186 	thread->cpu = cpu;
187 
188 	/*  Boot to the head.  */
189 	stack_start =  ((void *) thread) + THREAD_SIZE;
190 	__vmstart(start_secondary, stack_start);
191 
192 	while (!cpu_online(cpu))
193 		barrier();
194 
195 	return 0;
196 }
197 
198 void __init smp_cpus_done(unsigned int max_cpus)
199 {
200 }
201 
202 void __init smp_prepare_cpus(unsigned int max_cpus)
203 {
204 	int i;
205 
206 	/*
207 	 * should eventually have some sort of machine
208 	 * descriptor that has this stuff
209 	 */
210 
211 	/*  Right now, let's just fake it. */
212 	for (i = 0; i < max_cpus; i++)
213 		set_cpu_present(i, true);
214 
215 	/*  Also need to register the interrupts for IPI  */
216 	if (max_cpus > 1)
217 		setup_irq(BASE_IPI_IRQ, &ipi_intdesc);
218 }
219 
220 void smp_send_reschedule(int cpu)
221 {
222 	send_ipi(cpumask_of(cpu), IPI_RESCHEDULE);
223 }
224 
225 void smp_send_stop(void)
226 {
227 	struct cpumask targets;
228 	cpumask_copy(&targets, cpu_online_mask);
229 	cpumask_clear_cpu(smp_processor_id(), &targets);
230 	send_ipi(&targets, IPI_CPU_STOP);
231 }
232 
233 void arch_send_call_function_single_ipi(int cpu)
234 {
235 	send_ipi(cpumask_of(cpu), IPI_CALL_FUNC);
236 }
237 
238 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
239 {
240 	send_ipi(mask, IPI_CALL_FUNC);
241 }
242 
243 int setup_profiling_timer(unsigned int multiplier)
244 {
245 	return -EINVAL;
246 }
247 
248 void smp_start_cpus(void)
249 {
250 	int i;
251 
252 	for (i = 0; i < NR_CPUS; i++)
253 		set_cpu_possible(i, true);
254 }
255