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