[ARM SMP] Fix a couple of warnings
[powerpc.git] / arch / arm / kernel / smp.c
1 /*
2  *  linux/arch/arm/kernel/smp.c
3  *
4  *  Copyright (C) 2002 ARM Limited, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/config.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/cache.h>
17 #include <linux/profile.h>
18 #include <linux/errno.h>
19 #include <linux/mm.h>
20 #include <linux/cpu.h>
21 #include <linux/smp.h>
22 #include <linux/seq_file.h>
23
24 #include <asm/atomic.h>
25 #include <asm/cacheflush.h>
26 #include <asm/cpu.h>
27 #include <asm/mmu_context.h>
28 #include <asm/pgtable.h>
29 #include <asm/pgalloc.h>
30 #include <asm/processor.h>
31 #include <asm/tlbflush.h>
32 #include <asm/ptrace.h>
33
34 /*
35  * bitmask of present and online CPUs.
36  * The present bitmask indicates that the CPU is physically present.
37  * The online bitmask indicates that the CPU is up and running.
38  */
39 cpumask_t cpu_possible_map;
40 cpumask_t cpu_online_map;
41
42 /*
43  * as from 2.5, kernels no longer have an init_tasks structure
44  * so we need some other way of telling a new secondary core
45  * where to place its SVC stack
46  */
47 struct secondary_data secondary_data;
48
49 /*
50  * structures for inter-processor calls
51  * - A collection of single bit ipi messages.
52  */
53 struct ipi_data {
54         spinlock_t lock;
55         unsigned long ipi_count;
56         unsigned long bits;
57 };
58
59 static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
60         .lock   = SPIN_LOCK_UNLOCKED,
61 };
62
63 enum ipi_msg_type {
64         IPI_TIMER,
65         IPI_RESCHEDULE,
66         IPI_CALL_FUNC,
67         IPI_CPU_STOP,
68 };
69
70 struct smp_call_struct {
71         void (*func)(void *info);
72         void *info;
73         int wait;
74         cpumask_t pending;
75         cpumask_t unfinished;
76 };
77
78 static struct smp_call_struct * volatile smp_call_function_data;
79 static DEFINE_SPINLOCK(smp_call_function_lock);
80
81 int __cpuinit __cpu_up(unsigned int cpu)
82 {
83         struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
84         struct task_struct *idle = ci->idle;
85         pgd_t *pgd;
86         pmd_t *pmd;
87         int ret;
88
89         /*
90          * Spawn a new process manually, if not already done.
91          * Grab a pointer to its task struct so we can mess with it
92          */
93         if (!idle) {
94                 idle = fork_idle(cpu);
95                 if (IS_ERR(idle)) {
96                         printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
97                         return PTR_ERR(idle);
98                 }
99                 ci->idle = idle;
100         }
101
102         /*
103          * Allocate initial page tables to allow the new CPU to
104          * enable the MMU safely.  This essentially means a set
105          * of our "standard" page tables, with the addition of
106          * a 1:1 mapping for the physical address of the kernel.
107          */
108         pgd = pgd_alloc(&init_mm);
109         pmd = pmd_offset(pgd, PHYS_OFFSET);
110         *pmd = __pmd((PHYS_OFFSET & PGDIR_MASK) |
111                      PMD_TYPE_SECT | PMD_SECT_AP_WRITE);
112
113         /*
114          * We need to tell the secondary core where to find
115          * its stack and the page tables.
116          */
117         secondary_data.stack = (void *)idle->thread_info + THREAD_START_SP;
118         secondary_data.pgdir = virt_to_phys(pgd);
119         wmb();
120
121         /*
122          * Now bring the CPU into our world.
123          */
124         ret = boot_secondary(cpu, idle);
125         if (ret == 0) {
126                 unsigned long timeout;
127
128                 /*
129                  * CPU was successfully started, wait for it
130                  * to come online or time out.
131                  */
132                 timeout = jiffies + HZ;
133                 while (time_before(jiffies, timeout)) {
134                         if (cpu_online(cpu))
135                                 break;
136
137                         udelay(10);
138                         barrier();
139                 }
140
141                 if (!cpu_online(cpu))
142                         ret = -EIO;
143         }
144
145         secondary_data.stack = 0;
146         secondary_data.pgdir = 0;
147
148         *pmd_offset(pgd, PHYS_OFFSET) = __pmd(0);
149         pgd_free(pgd);
150
151         if (ret) {
152                 printk(KERN_CRIT "CPU%u: processor failed to boot\n", cpu);
153
154                 /*
155                  * FIXME: We need to clean up the new idle thread. --rmk
156                  */
157         }
158
159         return ret;
160 }
161
162 /*
163  * This is the secondary CPU boot entry.  We're using this CPUs
164  * idle thread stack, but a set of temporary page tables.
165  */
166 asmlinkage void __cpuinit secondary_start_kernel(void)
167 {
168         struct mm_struct *mm = &init_mm;
169         unsigned int cpu = smp_processor_id();
170
171         printk("CPU%u: Booted secondary processor\n", cpu);
172
173         /*
174          * All kernel threads share the same mm context; grab a
175          * reference and switch to it.
176          */
177         atomic_inc(&mm->mm_users);
178         atomic_inc(&mm->mm_count);
179         current->active_mm = mm;
180         cpu_set(cpu, mm->cpu_vm_mask);
181         cpu_switch_mm(mm->pgd, mm);
182         enter_lazy_tlb(mm, current);
183         local_flush_tlb_all();
184
185         cpu_init();
186
187         /*
188          * Give the platform a chance to do its own initialisation.
189          */
190         platform_secondary_init(cpu);
191
192         /*
193          * Enable local interrupts.
194          */
195         local_irq_enable();
196         local_fiq_enable();
197
198         calibrate_delay();
199
200         smp_store_cpu_info(cpu);
201
202         /*
203          * OK, now it's safe to let the boot CPU continue
204          */
205         cpu_set(cpu, cpu_online_map);
206
207         /*
208          * OK, it's off to the idle thread for us
209          */
210         cpu_idle();
211 }
212
213 /*
214  * Called by both boot and secondaries to move global data into
215  * per-processor storage.
216  */
217 void __cpuinit smp_store_cpu_info(unsigned int cpuid)
218 {
219         struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
220
221         cpu_info->loops_per_jiffy = loops_per_jiffy;
222 }
223
224 void __init smp_cpus_done(unsigned int max_cpus)
225 {
226         int cpu;
227         unsigned long bogosum = 0;
228
229         for_each_online_cpu(cpu)
230                 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
231
232         printk(KERN_INFO "SMP: Total of %d processors activated "
233                "(%lu.%02lu BogoMIPS).\n",
234                num_online_cpus(),
235                bogosum / (500000/HZ),
236                (bogosum / (5000/HZ)) % 100);
237 }
238
239 void __init smp_prepare_boot_cpu(void)
240 {
241         unsigned int cpu = smp_processor_id();
242
243         per_cpu(cpu_data, cpu).idle = current;
244
245         cpu_set(cpu, cpu_possible_map);
246         cpu_set(cpu, cpu_present_map);
247         cpu_set(cpu, cpu_online_map);
248 }
249
250 static void send_ipi_message(cpumask_t callmap, enum ipi_msg_type msg)
251 {
252         unsigned long flags;
253         unsigned int cpu;
254
255         local_irq_save(flags);
256
257         for_each_cpu_mask(cpu, callmap) {
258                 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
259
260                 spin_lock(&ipi->lock);
261                 ipi->bits |= 1 << msg;
262                 spin_unlock(&ipi->lock);
263         }
264
265         /*
266          * Call the platform specific cross-CPU call function.
267          */
268         smp_cross_call(callmap);
269
270         local_irq_restore(flags);
271 }
272
273 /*
274  * You must not call this function with disabled interrupts, from a
275  * hardware interrupt handler, nor from a bottom half handler.
276  */
277 int smp_call_function_on_cpu(void (*func)(void *info), void *info, int retry,
278                              int wait, cpumask_t callmap)
279 {
280         struct smp_call_struct data;
281         unsigned long timeout;
282         int ret = 0;
283
284         data.func = func;
285         data.info = info;
286         data.wait = wait;
287
288         cpu_clear(smp_processor_id(), callmap);
289         if (cpus_empty(callmap))
290                 goto out;
291
292         data.pending = callmap;
293         if (wait)
294                 data.unfinished = callmap;
295
296         /*
297          * try to get the mutex on smp_call_function_data
298          */
299         spin_lock(&smp_call_function_lock);
300         smp_call_function_data = &data;
301
302         send_ipi_message(callmap, IPI_CALL_FUNC);
303
304         timeout = jiffies + HZ;
305         while (!cpus_empty(data.pending) && time_before(jiffies, timeout))
306                 barrier();
307
308         /*
309          * did we time out?
310          */
311         if (!cpus_empty(data.pending)) {
312                 /*
313                  * this may be causing our panic - report it
314                  */
315                 printk(KERN_CRIT
316                        "CPU%u: smp_call_function timeout for %p(%p)\n"
317                        "      callmap %lx pending %lx, %swait\n",
318                        smp_processor_id(), func, info, *cpus_addr(callmap),
319                        *cpus_addr(data.pending), wait ? "" : "no ");
320
321                 /*
322                  * TRACE
323                  */
324                 timeout = jiffies + (5 * HZ);
325                 while (!cpus_empty(data.pending) && time_before(jiffies, timeout))
326                         barrier();
327
328                 if (cpus_empty(data.pending))
329                         printk(KERN_CRIT "     RESOLVED\n");
330                 else
331                         printk(KERN_CRIT "     STILL STUCK\n");
332         }
333
334         /*
335          * whatever happened, we're done with the data, so release it
336          */
337         smp_call_function_data = NULL;
338         spin_unlock(&smp_call_function_lock);
339
340         if (!cpus_empty(data.pending)) {
341                 ret = -ETIMEDOUT;
342                 goto out;
343         }
344
345         if (wait)
346                 while (!cpus_empty(data.unfinished))
347                         barrier();
348  out:
349
350         return 0;
351 }
352
353 int smp_call_function(void (*func)(void *info), void *info, int retry,
354                       int wait)
355 {
356         return smp_call_function_on_cpu(func, info, retry, wait,
357                                         cpu_online_map);
358 }
359
360 void show_ipi_list(struct seq_file *p)
361 {
362         unsigned int cpu;
363
364         seq_puts(p, "IPI:");
365
366         for_each_present_cpu(cpu)
367                 seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
368
369         seq_putc(p, '\n');
370 }
371
372 static void ipi_timer(struct pt_regs *regs)
373 {
374         int user = user_mode(regs);
375
376         irq_enter();
377         profile_tick(CPU_PROFILING, regs);
378         update_process_times(user);
379         irq_exit();
380 }
381
382 /*
383  * ipi_call_function - handle IPI from smp_call_function()
384  *
385  * Note that we copy data out of the cross-call structure and then
386  * let the caller know that we're here and have done with their data
387  */
388 static void ipi_call_function(unsigned int cpu)
389 {
390         struct smp_call_struct *data = smp_call_function_data;
391         void (*func)(void *info) = data->func;
392         void *info = data->info;
393         int wait = data->wait;
394
395         cpu_clear(cpu, data->pending);
396
397         func(info);
398
399         if (wait)
400                 cpu_clear(cpu, data->unfinished);
401 }
402
403 static DEFINE_SPINLOCK(stop_lock);
404
405 /*
406  * ipi_cpu_stop - handle IPI from smp_send_stop()
407  */
408 static void ipi_cpu_stop(unsigned int cpu)
409 {
410         spin_lock(&stop_lock);
411         printk(KERN_CRIT "CPU%u: stopping\n", cpu);
412         dump_stack();
413         spin_unlock(&stop_lock);
414
415         cpu_clear(cpu, cpu_online_map);
416
417         local_fiq_disable();
418         local_irq_disable();
419
420         while (1)
421                 cpu_relax();
422 }
423
424 /*
425  * Main handler for inter-processor interrupts
426  *
427  * For ARM, the ipimask now only identifies a single
428  * category of IPI (Bit 1 IPIs have been replaced by a
429  * different mechanism):
430  *
431  *  Bit 0 - Inter-processor function call
432  */
433 void do_IPI(struct pt_regs *regs)
434 {
435         unsigned int cpu = smp_processor_id();
436         struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
437
438         ipi->ipi_count++;
439
440         for (;;) {
441                 unsigned long msgs;
442
443                 spin_lock(&ipi->lock);
444                 msgs = ipi->bits;
445                 ipi->bits = 0;
446                 spin_unlock(&ipi->lock);
447
448                 if (!msgs)
449                         break;
450
451                 do {
452                         unsigned nextmsg;
453
454                         nextmsg = msgs & -msgs;
455                         msgs &= ~nextmsg;
456                         nextmsg = ffz(~nextmsg);
457
458                         switch (nextmsg) {
459                         case IPI_TIMER:
460                                 ipi_timer(regs);
461                                 break;
462
463                         case IPI_RESCHEDULE:
464                                 /*
465                                  * nothing more to do - eveything is
466                                  * done on the interrupt return path
467                                  */
468                                 break;
469
470                         case IPI_CALL_FUNC:
471                                 ipi_call_function(cpu);
472                                 break;
473
474                         case IPI_CPU_STOP:
475                                 ipi_cpu_stop(cpu);
476                                 break;
477
478                         default:
479                                 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
480                                        cpu, nextmsg);
481                                 break;
482                         }
483                 } while (msgs);
484         }
485 }
486
487 void smp_send_reschedule(int cpu)
488 {
489         send_ipi_message(cpumask_of_cpu(cpu), IPI_RESCHEDULE);
490 }
491
492 void smp_send_timer(void)
493 {
494         cpumask_t mask = cpu_online_map;
495         cpu_clear(smp_processor_id(), mask);
496         send_ipi_message(mask, IPI_TIMER);
497 }
498
499 void smp_send_stop(void)
500 {
501         cpumask_t mask = cpu_online_map;
502         cpu_clear(smp_processor_id(), mask);
503         send_ipi_message(mask, IPI_CPU_STOP);
504 }
505
506 /*
507  * not supported here
508  */
509 int __init setup_profiling_timer(unsigned int multiplier)
510 {
511         return -EINVAL;
512 }
513
514 static int
515 on_each_cpu_mask(void (*func)(void *), void *info, int retry, int wait,
516                  cpumask_t mask)
517 {
518         int ret = 0;
519
520         preempt_disable();
521
522         ret = smp_call_function_on_cpu(func, info, retry, wait, mask);
523         if (cpu_isset(smp_processor_id(), mask))
524                 func(info);
525
526         preempt_enable();
527
528         return ret;
529 }
530
531 /**********************************************************************/
532
533 /*
534  * TLB operations
535  */
536 struct tlb_args {
537         struct vm_area_struct *ta_vma;
538         unsigned long ta_start;
539         unsigned long ta_end;
540 };
541
542 static inline void ipi_flush_tlb_all(void *ignored)
543 {
544         local_flush_tlb_all();
545 }
546
547 static inline void ipi_flush_tlb_mm(void *arg)
548 {
549         struct mm_struct *mm = (struct mm_struct *)arg;
550
551         local_flush_tlb_mm(mm);
552 }
553
554 static inline void ipi_flush_tlb_page(void *arg)
555 {
556         struct tlb_args *ta = (struct tlb_args *)arg;
557
558         local_flush_tlb_page(ta->ta_vma, ta->ta_start);
559 }
560
561 static inline void ipi_flush_tlb_kernel_page(void *arg)
562 {
563         struct tlb_args *ta = (struct tlb_args *)arg;
564
565         local_flush_tlb_kernel_page(ta->ta_start);
566 }
567
568 static inline void ipi_flush_tlb_range(void *arg)
569 {
570         struct tlb_args *ta = (struct tlb_args *)arg;
571
572         local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
573 }
574
575 static inline void ipi_flush_tlb_kernel_range(void *arg)
576 {
577         struct tlb_args *ta = (struct tlb_args *)arg;
578
579         local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
580 }
581
582 void flush_tlb_all(void)
583 {
584         on_each_cpu(ipi_flush_tlb_all, NULL, 1, 1);
585 }
586
587 void flush_tlb_mm(struct mm_struct *mm)
588 {
589         cpumask_t mask = mm->cpu_vm_mask;
590
591         on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, 1, mask);
592 }
593
594 void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
595 {
596         cpumask_t mask = vma->vm_mm->cpu_vm_mask;
597         struct tlb_args ta;
598
599         ta.ta_vma = vma;
600         ta.ta_start = uaddr;
601
602         on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, 1, mask);
603 }
604
605 void flush_tlb_kernel_page(unsigned long kaddr)
606 {
607         struct tlb_args ta;
608
609         ta.ta_start = kaddr;
610
611         on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1, 1);
612 }
613
614 void flush_tlb_range(struct vm_area_struct *vma,
615                      unsigned long start, unsigned long end)
616 {
617         cpumask_t mask = vma->vm_mm->cpu_vm_mask;
618         struct tlb_args ta;
619
620         ta.ta_vma = vma;
621         ta.ta_start = start;
622         ta.ta_end = end;
623
624         on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, 1, mask);
625 }
626
627 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
628 {
629         struct tlb_args ta;
630
631         ta.ta_start = start;
632         ta.ta_end = end;
633
634         on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1, 1);
635 }