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