GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / parisc / kernel / smp.c
1 /*
2 ** SMP Support
3 **
4 ** Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
5 ** Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
6 ** Copyright (C) 2001,2004 Grant Grundler <grundler@parisc-linux.org>
7 ** 
8 ** Lots of stuff stolen from arch/alpha/kernel/smp.c
9 ** ...and then parisc stole from arch/ia64/kernel/smp.c. Thanks David! :^)
10 **
11 ** Thanks to John Curry and Ullas Ponnadi. I learned a lot from their work.
12 ** -grant (1/12/2001)
13 **
14 **      This program is free software; you can redistribute it and/or modify
15 **      it under the terms of the GNU General Public License as published by
16 **      the Free Software Foundation; either version 2 of the License, or
17 **      (at your option) any later version.
18 */
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/sched/mm.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/smp.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/mm.h>
30 #include <linux/err.h>
31 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/ftrace.h>
34 #include <linux/cpu.h>
35 #include <linux/kgdb.h>
36
37 #include <linux/atomic.h>
38 #include <asm/current.h>
39 #include <asm/delay.h>
40 #include <asm/tlbflush.h>
41
42 #include <asm/io.h>
43 #include <asm/irq.h>            /* for CPU_IRQ_REGION and friends */
44 #include <asm/mmu_context.h>
45 #include <asm/page.h>
46 #include <asm/pgtable.h>
47 #include <asm/pgalloc.h>
48 #include <asm/processor.h>
49 #include <asm/ptrace.h>
50 #include <asm/unistd.h>
51 #include <asm/cacheflush.h>
52
53 #undef DEBUG_SMP
54 #ifdef DEBUG_SMP
55 static int smp_debug_lvl = 0;
56 #define smp_debug(lvl, printargs...)            \
57                 if (lvl >= smp_debug_lvl)       \
58                         printk(printargs);
59 #else
60 #define smp_debug(lvl, ...)     do { } while(0)
61 #endif /* DEBUG_SMP */
62
63 volatile struct task_struct *smp_init_current_idle_task;
64
65 /* track which CPU is booting */
66 static volatile int cpu_now_booting;
67
68 static int parisc_max_cpus = 1;
69
70 static DEFINE_PER_CPU(spinlock_t, ipi_lock);
71
72 enum ipi_message_type {
73         IPI_NOP=0,
74         IPI_RESCHEDULE=1,
75         IPI_CALL_FUNC,
76         IPI_CPU_START,
77         IPI_CPU_STOP,
78         IPI_CPU_TEST,
79 #ifdef CONFIG_KGDB
80         IPI_ENTER_KGDB,
81 #endif
82 };
83
84
85 /********** SMP inter processor interrupt and communication routines */
86
87 #undef PER_CPU_IRQ_REGION
88 #ifdef PER_CPU_IRQ_REGION
89 /* XXX REVISIT Ignore for now.
90 **    *May* need this "hook" to register IPI handler
91 **    once we have perCPU ExtIntr switch tables.
92 */
93 static void
94 ipi_init(int cpuid)
95 {
96 #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
97
98         if(cpu_online(cpuid) )
99         {
100                 switch_to_idle_task(current);
101         }
102
103         return;
104 }
105 #endif
106
107
108 /*
109 ** Yoink this CPU from the runnable list... 
110 **
111 */
112 static void
113 halt_processor(void) 
114 {
115         /* REVISIT : redirect I/O Interrupts to another CPU? */
116         /* REVISIT : does PM *know* this CPU isn't available? */
117         set_cpu_online(smp_processor_id(), false);
118         local_irq_disable();
119         for (;;)
120                 ;
121 }
122
123
124 irqreturn_t __irq_entry
125 ipi_interrupt(int irq, void *dev_id) 
126 {
127         int this_cpu = smp_processor_id();
128         struct cpuinfo_parisc *p = &per_cpu(cpu_data, this_cpu);
129         unsigned long ops;
130         unsigned long flags;
131
132         for (;;) {
133                 spinlock_t *lock = &per_cpu(ipi_lock, this_cpu);
134                 spin_lock_irqsave(lock, flags);
135                 ops = p->pending_ipi;
136                 p->pending_ipi = 0;
137                 spin_unlock_irqrestore(lock, flags);
138
139                 mb(); /* Order bit clearing and data access. */
140
141                 if (!ops)
142                     break;
143
144                 while (ops) {
145                         unsigned long which = ffz(~ops);
146
147                         ops &= ~(1 << which);
148
149                         switch (which) {
150                         case IPI_NOP:
151                                 smp_debug(100, KERN_DEBUG "CPU%d IPI_NOP\n", this_cpu);
152                                 break;
153                                 
154                         case IPI_RESCHEDULE:
155                                 smp_debug(100, KERN_DEBUG "CPU%d IPI_RESCHEDULE\n", this_cpu);
156                                 inc_irq_stat(irq_resched_count);
157                                 scheduler_ipi();
158                                 break;
159
160                         case IPI_CALL_FUNC:
161                                 smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC\n", this_cpu);
162                                 generic_smp_call_function_interrupt();
163                                 break;
164
165                         case IPI_CPU_START:
166                                 smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_START\n", this_cpu);
167                                 break;
168
169                         case IPI_CPU_STOP:
170                                 smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_STOP\n", this_cpu);
171                                 halt_processor();
172                                 break;
173
174                         case IPI_CPU_TEST:
175                                 smp_debug(100, KERN_DEBUG "CPU%d is alive!\n", this_cpu);
176                                 break;
177 #ifdef CONFIG_KGDB
178                         case IPI_ENTER_KGDB:
179                                 smp_debug(100, KERN_DEBUG "CPU%d ENTER_KGDB\n", this_cpu);
180                                 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
181                                 break;
182 #endif
183                         default:
184                                 printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
185                                         this_cpu, which);
186                                 return IRQ_NONE;
187                         } /* Switch */
188                 /* let in any pending interrupts */
189                 local_irq_enable();
190                 local_irq_disable();
191                 } /* while (ops) */
192         }
193         return IRQ_HANDLED;
194 }
195
196
197 static inline void
198 ipi_send(int cpu, enum ipi_message_type op)
199 {
200         struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpu);
201         spinlock_t *lock = &per_cpu(ipi_lock, cpu);
202         unsigned long flags;
203
204         spin_lock_irqsave(lock, flags);
205         p->pending_ipi |= 1 << op;
206         gsc_writel(IPI_IRQ - CPU_IRQ_BASE, p->hpa);
207         spin_unlock_irqrestore(lock, flags);
208 }
209
210 static void
211 send_IPI_mask(const struct cpumask *mask, enum ipi_message_type op)
212 {
213         int cpu;
214
215         for_each_cpu(cpu, mask)
216                 ipi_send(cpu, op);
217 }
218
219 static inline void
220 send_IPI_single(int dest_cpu, enum ipi_message_type op)
221 {
222         BUG_ON(dest_cpu == NO_PROC_ID);
223
224         ipi_send(dest_cpu, op);
225 }
226
227 static inline void
228 send_IPI_allbutself(enum ipi_message_type op)
229 {
230         int i;
231         
232         for_each_online_cpu(i) {
233                 if (i != smp_processor_id())
234                         send_IPI_single(i, op);
235         }
236 }
237
238 #ifdef CONFIG_KGDB
239 void kgdb_roundup_cpus(void)
240 {
241         send_IPI_allbutself(IPI_ENTER_KGDB);
242 }
243 #endif
244
245 inline void 
246 smp_send_stop(void)     { send_IPI_allbutself(IPI_CPU_STOP); }
247
248 void 
249 smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
250
251 void
252 smp_send_all_nop(void)
253 {
254         send_IPI_allbutself(IPI_NOP);
255 }
256
257 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
258 {
259         send_IPI_mask(mask, IPI_CALL_FUNC);
260 }
261
262 void arch_send_call_function_single_ipi(int cpu)
263 {
264         send_IPI_single(cpu, IPI_CALL_FUNC);
265 }
266
267 /*
268  * Called by secondaries to update state and initialize CPU registers.
269  */
270 static void __init
271 smp_cpu_init(int cpunum)
272 {
273         extern void init_IRQ(void);    /* arch/parisc/kernel/irq.c */
274         extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
275
276         /* Set modes and Enable floating point coprocessor */
277         init_per_cpu(cpunum);
278
279         disable_sr_hashing();
280
281         mb();
282
283         /* Well, support 2.4 linux scheme as well. */
284         if (cpu_online(cpunum)) {
285                 extern void machine_halt(void); /* arch/parisc.../process.c */
286
287                 printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
288                 machine_halt();
289         }
290
291         notify_cpu_starting(cpunum);
292
293         set_cpu_online(cpunum, true);
294
295         /* Initialise the idle task for this CPU */
296         mmgrab(&init_mm);
297         current->active_mm = &init_mm;
298         BUG_ON(current->mm);
299         enter_lazy_tlb(&init_mm, current);
300
301         init_IRQ();   /* make sure no IRQs are enabled or pending */
302         start_cpu_itimer();
303 }
304
305
306 /*
307  * Slaves start using C here. Indirectly called from smp_slave_stext.
308  * Do what start_kernel() and main() do for boot strap processor (aka monarch)
309  */
310 void __init smp_callin(unsigned long pdce_proc)
311 {
312         int slave_id = cpu_now_booting;
313
314 #ifdef CONFIG_64BIT
315         WARN_ON(((unsigned long)(PAGE0->mem_pdc_hi) << 32
316                         | PAGE0->mem_pdc) != pdce_proc);
317 #endif
318
319         smp_cpu_init(slave_id);
320         preempt_disable();
321
322         flush_cache_all_local(); /* start with known state */
323         flush_tlb_all_local(NULL);
324
325         local_irq_enable();  /* Interrupts have been off until now */
326
327         cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
328
329         /* NOTREACHED */
330         panic("smp_callin() AAAAaaaaahhhh....\n");
331 }
332
333 /*
334  * Bring one cpu online.
335  */
336 int smp_boot_one_cpu(int cpuid, struct task_struct *idle)
337 {
338         const struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpuid);
339         long timeout;
340
341         task_thread_info(idle)->cpu = cpuid;
342
343         /* Let _start know what logical CPU we're booting
344         ** (offset into init_tasks[],cpu_data[])
345         */
346         cpu_now_booting = cpuid;
347
348         /* 
349         ** boot strap code needs to know the task address since
350         ** it also contains the process stack.
351         */
352         smp_init_current_idle_task = idle ;
353         mb();
354
355         printk(KERN_INFO "Releasing cpu %d now, hpa=%lx\n", cpuid, p->hpa);
356
357         /*
358         ** This gets PDC to release the CPU from a very tight loop.
359         **
360         ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
361         ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which 
362         ** is executed after receiving the rendezvous signal (an interrupt to 
363         ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the 
364         ** contents of memory are valid."
365         */
366         gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, p->hpa);
367         mb();
368
369         /* 
370          * OK, wait a bit for that CPU to finish staggering about. 
371          * Slave will set a bit when it reaches smp_cpu_init().
372          * Once the "monarch CPU" sees the bit change, it can move on.
373          */
374         for (timeout = 0; timeout < 10000; timeout++) {
375                 if(cpu_online(cpuid)) {
376                         /* Which implies Slave has started up */
377                         cpu_now_booting = 0;
378                         smp_init_current_idle_task = NULL;
379                         goto alive ;
380                 }
381                 udelay(100);
382                 barrier();
383         }
384         printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
385         return -1;
386
387 alive:
388         /* Remember the Slave data */
389         smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
390                 cpuid, timeout * 100);
391         return 0;
392 }
393
394 void __init smp_prepare_boot_cpu(void)
395 {
396         int bootstrap_processor = per_cpu(cpu_data, 0).cpuid;
397
398         /* Setup BSP mappings */
399         printk(KERN_INFO "SMP: bootstrap CPU ID is %d\n", bootstrap_processor);
400
401         set_cpu_online(bootstrap_processor, true);
402         set_cpu_present(bootstrap_processor, true);
403 }
404
405
406
407 /*
408 ** inventory.c:do_inventory() hasn't yet been run and thus we
409 ** don't 'discover' the additional CPUs until later.
410 */
411 void __init smp_prepare_cpus(unsigned int max_cpus)
412 {
413         int cpu;
414
415         for_each_possible_cpu(cpu)
416                 spin_lock_init(&per_cpu(ipi_lock, cpu));
417
418         init_cpu_present(cpumask_of(0));
419
420         parisc_max_cpus = max_cpus;
421         if (!max_cpus)
422                 printk(KERN_INFO "SMP mode deactivated.\n");
423 }
424
425
426 void smp_cpus_done(unsigned int cpu_max)
427 {
428         return;
429 }
430
431
432 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
433 {
434         if (cpu != 0 && cpu < parisc_max_cpus && smp_boot_one_cpu(cpu, tidle))
435                 return -ENOSYS;
436
437         return cpu_online(cpu) ? 0 : -ENOSYS;
438 }
439
440 #ifdef CONFIG_PROC_FS
441 int setup_profiling_timer(unsigned int multiplier)
442 {
443         return -EINVAL;
444 }
445 #endif