Merge commit 'v2.6.37' into next
[pandora-kernel.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/module.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/err.h>
21 #include <linux/cpu.h>
22 #include <linux/smp.h>
23 #include <linux/seq_file.h>
24 #include <linux/irq.h>
25 #include <linux/percpu.h>
26 #include <linux/clockchips.h>
27
28 #include <asm/atomic.h>
29 #include <asm/cacheflush.h>
30 #include <asm/cpu.h>
31 #include <asm/cputype.h>
32 #include <asm/mmu_context.h>
33 #include <asm/pgtable.h>
34 #include <asm/pgalloc.h>
35 #include <asm/processor.h>
36 #include <asm/sections.h>
37 #include <asm/tlbflush.h>
38 #include <asm/ptrace.h>
39 #include <asm/localtimer.h>
40 #include <asm/smp_plat.h>
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_CALL_FUNC_SINGLE,
68         IPI_CPU_STOP,
69 };
70
71 static inline void identity_mapping_add(pgd_t *pgd, unsigned long start,
72         unsigned long end)
73 {
74         unsigned long addr, prot;
75         pmd_t *pmd;
76
77         prot = PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
78         if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
79                 prot |= PMD_BIT4;
80
81         for (addr = start & PGDIR_MASK; addr < end;) {
82                 pmd = pmd_offset(pgd + pgd_index(addr), addr);
83                 pmd[0] = __pmd(addr | prot);
84                 addr += SECTION_SIZE;
85                 pmd[1] = __pmd(addr | prot);
86                 addr += SECTION_SIZE;
87                 flush_pmd_entry(pmd);
88                 outer_clean_range(__pa(pmd), __pa(pmd + 1));
89         }
90 }
91
92 static inline void identity_mapping_del(pgd_t *pgd, unsigned long start,
93         unsigned long end)
94 {
95         unsigned long addr;
96         pmd_t *pmd;
97
98         for (addr = start & PGDIR_MASK; addr < end; addr += PGDIR_SIZE) {
99                 pmd = pmd_offset(pgd + pgd_index(addr), addr);
100                 pmd[0] = __pmd(0);
101                 pmd[1] = __pmd(0);
102                 clean_pmd_entry(pmd);
103                 outer_clean_range(__pa(pmd), __pa(pmd + 1));
104         }
105 }
106
107 int __cpuinit __cpu_up(unsigned int cpu)
108 {
109         struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
110         struct task_struct *idle = ci->idle;
111         pgd_t *pgd;
112         int ret;
113
114         /*
115          * Spawn a new process manually, if not already done.
116          * Grab a pointer to its task struct so we can mess with it
117          */
118         if (!idle) {
119                 idle = fork_idle(cpu);
120                 if (IS_ERR(idle)) {
121                         printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
122                         return PTR_ERR(idle);
123                 }
124                 ci->idle = idle;
125         } else {
126                 /*
127                  * Since this idle thread is being re-used, call
128                  * init_idle() to reinitialize the thread structure.
129                  */
130                 init_idle(idle, cpu);
131         }
132
133         /*
134          * Allocate initial page tables to allow the new CPU to
135          * enable the MMU safely.  This essentially means a set
136          * of our "standard" page tables, with the addition of
137          * a 1:1 mapping for the physical address of the kernel.
138          */
139         pgd = pgd_alloc(&init_mm);
140         if (!pgd)
141                 return -ENOMEM;
142
143         if (PHYS_OFFSET != PAGE_OFFSET) {
144 #ifndef CONFIG_HOTPLUG_CPU
145                 identity_mapping_add(pgd, __pa(__init_begin), __pa(__init_end));
146 #endif
147                 identity_mapping_add(pgd, __pa(_stext), __pa(_etext));
148                 identity_mapping_add(pgd, __pa(_sdata), __pa(_edata));
149         }
150
151         /*
152          * We need to tell the secondary core where to find
153          * its stack and the page tables.
154          */
155         secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
156         secondary_data.pgdir = virt_to_phys(pgd);
157         __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data));
158         outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1));
159
160         /*
161          * Now bring the CPU into our world.
162          */
163         ret = boot_secondary(cpu, idle);
164         if (ret == 0) {
165                 unsigned long timeout;
166
167                 /*
168                  * CPU was successfully started, wait for it
169                  * to come online or time out.
170                  */
171                 timeout = jiffies + HZ;
172                 while (time_before(jiffies, timeout)) {
173                         if (cpu_online(cpu))
174                                 break;
175
176                         udelay(10);
177                         barrier();
178                 }
179
180                 if (!cpu_online(cpu))
181                         ret = -EIO;
182         }
183
184         secondary_data.stack = NULL;
185         secondary_data.pgdir = 0;
186
187         if (PHYS_OFFSET != PAGE_OFFSET) {
188 #ifndef CONFIG_HOTPLUG_CPU
189                 identity_mapping_del(pgd, __pa(__init_begin), __pa(__init_end));
190 #endif
191                 identity_mapping_del(pgd, __pa(_stext), __pa(_etext));
192                 identity_mapping_del(pgd, __pa(_sdata), __pa(_edata));
193         }
194
195         pgd_free(&init_mm, pgd);
196
197         if (ret) {
198                 printk(KERN_CRIT "CPU%u: processor failed to boot\n", cpu);
199
200                 /*
201                  * FIXME: We need to clean up the new idle thread. --rmk
202                  */
203         }
204
205         return ret;
206 }
207
208 #ifdef CONFIG_HOTPLUG_CPU
209 /*
210  * __cpu_disable runs on the processor to be shutdown.
211  */
212 int __cpu_disable(void)
213 {
214         unsigned int cpu = smp_processor_id();
215         struct task_struct *p;
216         int ret;
217
218         ret = platform_cpu_disable(cpu);
219         if (ret)
220                 return ret;
221
222         /*
223          * Take this CPU offline.  Once we clear this, we can't return,
224          * and we must not schedule until we're ready to give up the cpu.
225          */
226         set_cpu_online(cpu, false);
227
228         /*
229          * OK - migrate IRQs away from this CPU
230          */
231         migrate_irqs();
232
233         /*
234          * Stop the local timer for this CPU.
235          */
236         local_timer_stop();
237
238         /*
239          * Flush user cache and TLB mappings, and then remove this CPU
240          * from the vm mask set of all processes.
241          */
242         flush_cache_all();
243         local_flush_tlb_all();
244
245         read_lock(&tasklist_lock);
246         for_each_process(p) {
247                 if (p->mm)
248                         cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
249         }
250         read_unlock(&tasklist_lock);
251
252         return 0;
253 }
254
255 /*
256  * called on the thread which is asking for a CPU to be shutdown -
257  * waits until shutdown has completed, or it is timed out.
258  */
259 void __cpu_die(unsigned int cpu)
260 {
261         if (!platform_cpu_kill(cpu))
262                 printk("CPU%u: unable to kill\n", cpu);
263 }
264
265 /*
266  * Called from the idle thread for the CPU which has been shutdown.
267  *
268  * Note that we disable IRQs here, but do not re-enable them
269  * before returning to the caller. This is also the behaviour
270  * of the other hotplug-cpu capable cores, so presumably coming
271  * out of idle fixes this.
272  */
273 void __ref cpu_die(void)
274 {
275         unsigned int cpu = smp_processor_id();
276
277         local_irq_disable();
278         idle_task_exit();
279
280         /*
281          * actual CPU shutdown procedure is at least platform (if not
282          * CPU) specific
283          */
284         platform_cpu_die(cpu);
285
286         /*
287          * Do not return to the idle loop - jump back to the secondary
288          * cpu initialisation.  There's some initialisation which needs
289          * to be repeated to undo the effects of taking the CPU offline.
290          */
291         __asm__("mov    sp, %0\n"
292         "       b       secondary_start_kernel"
293                 :
294                 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
295 }
296 #endif /* CONFIG_HOTPLUG_CPU */
297
298 /*
299  * This is the secondary CPU boot entry.  We're using this CPUs
300  * idle thread stack, but a set of temporary page tables.
301  */
302 asmlinkage void __cpuinit secondary_start_kernel(void)
303 {
304         struct mm_struct *mm = &init_mm;
305         unsigned int cpu = smp_processor_id();
306
307         printk("CPU%u: Booted secondary processor\n", cpu);
308
309         /*
310          * All kernel threads share the same mm context; grab a
311          * reference and switch to it.
312          */
313         atomic_inc(&mm->mm_count);
314         current->active_mm = mm;
315         cpumask_set_cpu(cpu, mm_cpumask(mm));
316         cpu_switch_mm(mm->pgd, mm);
317         enter_lazy_tlb(mm, current);
318         local_flush_tlb_all();
319
320         cpu_init();
321         preempt_disable();
322
323         /*
324          * Give the platform a chance to do its own initialisation.
325          */
326         platform_secondary_init(cpu);
327
328         /*
329          * Enable local interrupts.
330          */
331         notify_cpu_starting(cpu);
332         local_irq_enable();
333         local_fiq_enable();
334
335         /*
336          * Setup the percpu timer for this CPU.
337          */
338         percpu_timer_setup();
339
340         calibrate_delay();
341
342         smp_store_cpu_info(cpu);
343
344         /*
345          * OK, now it's safe to let the boot CPU continue
346          */
347         set_cpu_online(cpu, true);
348
349         /*
350          * OK, it's off to the idle thread for us
351          */
352         cpu_idle();
353 }
354
355 /*
356  * Called by both boot and secondaries to move global data into
357  * per-processor storage.
358  */
359 void __cpuinit smp_store_cpu_info(unsigned int cpuid)
360 {
361         struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
362
363         cpu_info->loops_per_jiffy = loops_per_jiffy;
364 }
365
366 void __init smp_cpus_done(unsigned int max_cpus)
367 {
368         int cpu;
369         unsigned long bogosum = 0;
370
371         for_each_online_cpu(cpu)
372                 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
373
374         printk(KERN_INFO "SMP: Total of %d processors activated "
375                "(%lu.%02lu BogoMIPS).\n",
376                num_online_cpus(),
377                bogosum / (500000/HZ),
378                (bogosum / (5000/HZ)) % 100);
379 }
380
381 void __init smp_prepare_boot_cpu(void)
382 {
383         unsigned int cpu = smp_processor_id();
384
385         per_cpu(cpu_data, cpu).idle = current;
386 }
387
388 static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
389 {
390         unsigned long flags;
391         unsigned int cpu;
392
393         local_irq_save(flags);
394
395         for_each_cpu(cpu, mask) {
396                 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
397
398                 spin_lock(&ipi->lock);
399                 ipi->bits |= 1 << msg;
400                 spin_unlock(&ipi->lock);
401         }
402
403         /*
404          * Call the platform specific cross-CPU call function.
405          */
406         smp_cross_call(mask);
407
408         local_irq_restore(flags);
409 }
410
411 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
412 {
413         send_ipi_message(mask, IPI_CALL_FUNC);
414 }
415
416 void arch_send_call_function_single_ipi(int cpu)
417 {
418         send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
419 }
420
421 void show_ipi_list(struct seq_file *p)
422 {
423         unsigned int cpu;
424
425         seq_puts(p, "IPI:");
426
427         for_each_present_cpu(cpu)
428                 seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
429
430         seq_putc(p, '\n');
431 }
432
433 void show_local_irqs(struct seq_file *p)
434 {
435         unsigned int cpu;
436
437         seq_printf(p, "LOC: ");
438
439         for_each_present_cpu(cpu)
440                 seq_printf(p, "%10u ", irq_stat[cpu].local_timer_irqs);
441
442         seq_putc(p, '\n');
443 }
444
445 /*
446  * Timer (local or broadcast) support
447  */
448 static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
449
450 static void ipi_timer(void)
451 {
452         struct clock_event_device *evt = &__get_cpu_var(percpu_clockevent);
453         irq_enter();
454         evt->event_handler(evt);
455         irq_exit();
456 }
457
458 #ifdef CONFIG_LOCAL_TIMERS
459 asmlinkage void __exception do_local_timer(struct pt_regs *regs)
460 {
461         struct pt_regs *old_regs = set_irq_regs(regs);
462         int cpu = smp_processor_id();
463
464         if (local_timer_ack()) {
465                 irq_stat[cpu].local_timer_irqs++;
466                 ipi_timer();
467         }
468
469         set_irq_regs(old_regs);
470 }
471 #endif
472
473 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
474 static void smp_timer_broadcast(const struct cpumask *mask)
475 {
476         send_ipi_message(mask, IPI_TIMER);
477 }
478 #else
479 #define smp_timer_broadcast     NULL
480 #endif
481
482 #ifndef CONFIG_LOCAL_TIMERS
483 static void broadcast_timer_set_mode(enum clock_event_mode mode,
484         struct clock_event_device *evt)
485 {
486 }
487
488 static void local_timer_setup(struct clock_event_device *evt)
489 {
490         evt->name       = "dummy_timer";
491         evt->features   = CLOCK_EVT_FEAT_ONESHOT |
492                           CLOCK_EVT_FEAT_PERIODIC |
493                           CLOCK_EVT_FEAT_DUMMY;
494         evt->rating     = 400;
495         evt->mult       = 1;
496         evt->set_mode   = broadcast_timer_set_mode;
497
498         clockevents_register_device(evt);
499 }
500 #endif
501
502 void __cpuinit percpu_timer_setup(void)
503 {
504         unsigned int cpu = smp_processor_id();
505         struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
506
507         evt->cpumask = cpumask_of(cpu);
508         evt->broadcast = smp_timer_broadcast;
509
510         local_timer_setup(evt);
511 }
512
513 static DEFINE_SPINLOCK(stop_lock);
514
515 /*
516  * ipi_cpu_stop - handle IPI from smp_send_stop()
517  */
518 static void ipi_cpu_stop(unsigned int cpu)
519 {
520         if (system_state == SYSTEM_BOOTING ||
521             system_state == SYSTEM_RUNNING) {
522                 spin_lock(&stop_lock);
523                 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
524                 dump_stack();
525                 spin_unlock(&stop_lock);
526         }
527
528         set_cpu_online(cpu, false);
529
530         local_fiq_disable();
531         local_irq_disable();
532
533         while (1)
534                 cpu_relax();
535 }
536
537 /*
538  * Main handler for inter-processor interrupts
539  *
540  * For ARM, the ipimask now only identifies a single
541  * category of IPI (Bit 1 IPIs have been replaced by a
542  * different mechanism):
543  *
544  *  Bit 0 - Inter-processor function call
545  */
546 asmlinkage void __exception do_IPI(struct pt_regs *regs)
547 {
548         unsigned int cpu = smp_processor_id();
549         struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
550         struct pt_regs *old_regs = set_irq_regs(regs);
551
552         ipi->ipi_count++;
553
554         for (;;) {
555                 unsigned long msgs;
556
557                 spin_lock(&ipi->lock);
558                 msgs = ipi->bits;
559                 ipi->bits = 0;
560                 spin_unlock(&ipi->lock);
561
562                 if (!msgs)
563                         break;
564
565                 do {
566                         unsigned nextmsg;
567
568                         nextmsg = msgs & -msgs;
569                         msgs &= ~nextmsg;
570                         nextmsg = ffz(~nextmsg);
571
572                         switch (nextmsg) {
573                         case IPI_TIMER:
574                                 ipi_timer();
575                                 break;
576
577                         case IPI_RESCHEDULE:
578                                 /*
579                                  * nothing more to do - eveything is
580                                  * done on the interrupt return path
581                                  */
582                                 break;
583
584                         case IPI_CALL_FUNC:
585                                 generic_smp_call_function_interrupt();
586                                 break;
587
588                         case IPI_CALL_FUNC_SINGLE:
589                                 generic_smp_call_function_single_interrupt();
590                                 break;
591
592                         case IPI_CPU_STOP:
593                                 ipi_cpu_stop(cpu);
594                                 break;
595
596                         default:
597                                 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
598                                        cpu, nextmsg);
599                                 break;
600                         }
601                 } while (msgs);
602         }
603
604         set_irq_regs(old_regs);
605 }
606
607 void smp_send_reschedule(int cpu)
608 {
609         send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
610 }
611
612 void smp_send_stop(void)
613 {
614         cpumask_t mask = cpu_online_map;
615         cpu_clear(smp_processor_id(), mask);
616         if (!cpus_empty(mask))
617                 send_ipi_message(&mask, IPI_CPU_STOP);
618 }
619
620 /*
621  * not supported here
622  */
623 int setup_profiling_timer(unsigned int multiplier)
624 {
625         return -EINVAL;
626 }
627
628 static void
629 on_each_cpu_mask(void (*func)(void *), void *info, int wait,
630                 const struct cpumask *mask)
631 {
632         preempt_disable();
633
634         smp_call_function_many(mask, func, info, wait);
635         if (cpumask_test_cpu(smp_processor_id(), mask))
636                 func(info);
637
638         preempt_enable();
639 }
640
641 /**********************************************************************/
642
643 /*
644  * TLB operations
645  */
646 struct tlb_args {
647         struct vm_area_struct *ta_vma;
648         unsigned long ta_start;
649         unsigned long ta_end;
650 };
651
652 static inline void ipi_flush_tlb_all(void *ignored)
653 {
654         local_flush_tlb_all();
655 }
656
657 static inline void ipi_flush_tlb_mm(void *arg)
658 {
659         struct mm_struct *mm = (struct mm_struct *)arg;
660
661         local_flush_tlb_mm(mm);
662 }
663
664 static inline void ipi_flush_tlb_page(void *arg)
665 {
666         struct tlb_args *ta = (struct tlb_args *)arg;
667
668         local_flush_tlb_page(ta->ta_vma, ta->ta_start);
669 }
670
671 static inline void ipi_flush_tlb_kernel_page(void *arg)
672 {
673         struct tlb_args *ta = (struct tlb_args *)arg;
674
675         local_flush_tlb_kernel_page(ta->ta_start);
676 }
677
678 static inline void ipi_flush_tlb_range(void *arg)
679 {
680         struct tlb_args *ta = (struct tlb_args *)arg;
681
682         local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
683 }
684
685 static inline void ipi_flush_tlb_kernel_range(void *arg)
686 {
687         struct tlb_args *ta = (struct tlb_args *)arg;
688
689         local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
690 }
691
692 void flush_tlb_all(void)
693 {
694         if (tlb_ops_need_broadcast())
695                 on_each_cpu(ipi_flush_tlb_all, NULL, 1);
696         else
697                 local_flush_tlb_all();
698 }
699
700 void flush_tlb_mm(struct mm_struct *mm)
701 {
702         if (tlb_ops_need_broadcast())
703                 on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, mm_cpumask(mm));
704         else
705                 local_flush_tlb_mm(mm);
706 }
707
708 void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
709 {
710         if (tlb_ops_need_broadcast()) {
711                 struct tlb_args ta;
712                 ta.ta_vma = vma;
713                 ta.ta_start = uaddr;
714                 on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, mm_cpumask(vma->vm_mm));
715         } else
716                 local_flush_tlb_page(vma, uaddr);
717 }
718
719 void flush_tlb_kernel_page(unsigned long kaddr)
720 {
721         if (tlb_ops_need_broadcast()) {
722                 struct tlb_args ta;
723                 ta.ta_start = kaddr;
724                 on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1);
725         } else
726                 local_flush_tlb_kernel_page(kaddr);
727 }
728
729 void flush_tlb_range(struct vm_area_struct *vma,
730                      unsigned long start, unsigned long end)
731 {
732         if (tlb_ops_need_broadcast()) {
733                 struct tlb_args ta;
734                 ta.ta_vma = vma;
735                 ta.ta_start = start;
736                 ta.ta_end = end;
737                 on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, mm_cpumask(vma->vm_mm));
738         } else
739                 local_flush_tlb_range(vma, start, end);
740 }
741
742 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
743 {
744         if (tlb_ops_need_broadcast()) {
745                 struct tlb_args ta;
746                 ta.ta_start = start;
747                 ta.ta_end = end;
748                 on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1);
749         } else
750                 local_flush_tlb_kernel_range(start, end);
751 }