blob: 45877f5d5717e2580570af7494528bbacaea4e02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Russell Kinge65f38e2005-06-18 09:33:31 +010027#include <asm/mmu_context.h>
28#include <asm/pgtable.h>
29#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 */
Russell Kingd12734d2005-07-11 17:38:36 +010039cpumask_t cpu_possible_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040cpumask_t cpu_online_map;
41
42/*
Russell Kinge65f38e2005-06-18 09:33:31 +010043 * 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 */
47struct secondary_data secondary_data;
48
49/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * structures for inter-processor calls
51 * - A collection of single bit ipi messages.
52 */
53struct ipi_data {
54 spinlock_t lock;
55 unsigned long ipi_count;
56 unsigned long bits;
57};
58
59static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
60 .lock = SPIN_LOCK_UNLOCKED,
61};
62
63enum ipi_msg_type {
64 IPI_TIMER,
65 IPI_RESCHEDULE,
66 IPI_CALL_FUNC,
67 IPI_CPU_STOP,
68};
69
70struct smp_call_struct {
71 void (*func)(void *info);
72 void *info;
73 int wait;
74 cpumask_t pending;
75 cpumask_t unfinished;
76};
77
78static struct smp_call_struct * volatile smp_call_function_data;
79static DEFINE_SPINLOCK(smp_call_function_lock);
80
Russell Kingbd6f68a2005-07-17 21:35:41 +010081int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Russell King71f512e82005-11-02 21:51:40 +000083 struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
84 struct task_struct *idle = ci->idle;
Russell Kinge65f38e2005-06-18 09:33:31 +010085 pgd_t *pgd;
86 pmd_t *pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int ret;
88
89 /*
Russell King71f512e82005-11-02 21:51:40 +000090 * Spawn a new process manually, if not already done.
91 * Grab a pointer to its task struct so we can mess with it
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Russell King71f512e82005-11-02 21:51:40 +000093 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
102 /*
Russell Kinge65f38e2005-06-18 09:33:31 +0100103 * 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 */
Russell King7db078be2005-09-04 11:03:15 +0100117 secondary_data.stack = (void *)idle->thread_info + THREAD_START_SP;
Russell Kinge65f38e2005-06-18 09:33:31 +0100118 secondary_data.pgdir = virt_to_phys(pgd);
119 wmb();
120
121 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Now bring the CPU into our world.
123 */
124 ret = boot_secondary(cpu, idle);
Russell Kinge65f38e2005-06-18 09:33:31 +0100125 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (ret) {
Russell King0908db22005-06-19 19:48:16 +0100152 printk(KERN_CRIT "CPU%u: processor failed to boot\n", cpu);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /*
155 * FIXME: We need to clean up the new idle thread. --rmk
156 */
157 }
158
159 return ret;
160}
161
162/*
Russell Kinge65f38e2005-06-18 09:33:31 +0100163 * This is the secondary CPU boot entry. We're using this CPUs
164 * idle thread stack, but a set of temporary page tables.
165 */
Russell Kingbd6f68a2005-07-17 21:35:41 +0100166asmlinkage void __cpuinit secondary_start_kernel(void)
Russell Kinge65f38e2005-06-18 09:33:31 +0100167{
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);
Russell King505d7b12005-07-28 20:32:47 +0100183 local_flush_tlb_all();
Russell Kinge65f38e2005-06-18 09:33:31 +0100184
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * Called by both boot and secondaries to move global data into
215 * per-processor storage.
216 */
Russell Kingbd6f68a2005-07-17 21:35:41 +0100217void __cpuinit smp_store_cpu_info(unsigned int cpuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
220
221 cpu_info->loops_per_jiffy = loops_per_jiffy;
222}
223
224void __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
239void __init smp_prepare_boot_cpu(void)
240{
241 unsigned int cpu = smp_processor_id();
242
Russell King71f512e82005-11-02 21:51:40 +0000243 per_cpu(cpu_data, cpu).idle = current;
244
Russell Kingd12734d2005-07-11 17:38:36 +0100245 cpu_set(cpu, cpu_possible_map);
Russell King73eb7d92005-07-11 19:42:58 +0100246 cpu_set(cpu, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 cpu_set(cpu, cpu_online_map);
248}
249
250static 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 */
277int 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, callmap, data.pending,
319 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
353int 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
360void show_ipi_list(struct seq_file *p)
361{
362 unsigned int cpu;
363
364 seq_puts(p, "IPI:");
365
Russell Kinge11b2232005-07-11 19:26:31 +0100366 for_each_present_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
368
369 seq_putc(p, '\n');
370}
371
372static 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 */
388static 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
403static DEFINE_SPINLOCK(stop_lock);
404
405/*
406 * ipi_cpu_stop - handle IPI from smp_send_stop()
407 */
408static 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 */
433void 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
487void smp_send_reschedule(int cpu)
488{
489 send_ipi_message(cpumask_of_cpu(cpu), IPI_RESCHEDULE);
490}
491
492void 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
499void 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 */
509int __init setup_profiling_timer(unsigned int multiplier)
510{
511 return -EINVAL;
512}
Russell King4b0ef3b2005-06-28 13:49:16 +0100513
514static int
515on_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 */
536struct tlb_args {
537 struct vm_area_struct *ta_vma;
538 unsigned long ta_start;
539 unsigned long ta_end;
540};
541
542static inline void ipi_flush_tlb_all(void *ignored)
543{
544 local_flush_tlb_all();
545}
546
547static 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
554static 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
561static 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
568static 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
575static 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
582void flush_tlb_all(void)
583{
584 on_each_cpu(ipi_flush_tlb_all, NULL, 1, 1);
585}
586
587void 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
594void 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
605void 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
614void 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
627void 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}