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