4 ** Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
5 ** Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
6 ** Copyright (C) 2001,2004 Grant Grundler <grundler@parisc-linux.org>
8 ** Lots of stuff stolen from arch/alpha/kernel/smp.c
9 ** ...and then parisc stole from arch/ia64/kernel/smp.c. Thanks David! :^)
11 ** Thanks to John Curry and Ullas Ponnadi. I learned alot from their work.
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License as published by
16 ** the Free Software Foundation; either version 2 of the License, or
17 ** (at your option) any later version.
19 #undef ENTRY_SYS_CPUS /* syscall support for iCOD-like functionality */
22 #include <linux/types.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/smp.h>
32 #include <linux/kernel_stat.h>
34 #include <linux/delay.h>
35 #include <linux/bitops.h>
37 #include <asm/system.h>
38 #include <asm/atomic.h>
39 #include <asm/current.h>
40 #include <asm/delay.h>
41 #include <asm/tlbflush.h>
44 #include <asm/irq.h> /* for CPU_IRQ_REGION and friends */
45 #include <asm/mmu_context.h>
47 #include <asm/pgtable.h>
48 #include <asm/pgalloc.h>
49 #include <asm/processor.h>
50 #include <asm/ptrace.h>
51 #include <asm/unistd.h>
52 #include <asm/cacheflush.h>
56 DEFINE_SPINLOCK(smp_lock);
58 volatile struct task_struct *smp_init_current_idle_task;
60 static volatile int cpu_now_booting __read_mostly = 0; /* track which CPU is booting */
62 static int parisc_max_cpus __read_mostly = 1;
64 /* online cpus are ones that we've managed to bring up completely
65 * possible cpus are all valid cpu
66 * present cpus are all detected cpu
68 * On startup we bring up the "possible" cpus. Since we discover
69 * CPUs later, we add them as hotplug, so the possible cpu mask is
70 * empty in the beginning.
73 cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE; /* Bitmap of online CPUs */
74 cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; /* Bitmap of Present CPUs */
76 EXPORT_SYMBOL(cpu_online_map);
77 EXPORT_SYMBOL(cpu_possible_map);
80 struct smp_call_struct {
81 void (*func) (void *info);
84 atomic_t unstarted_count;
85 atomic_t unfinished_count;
87 static volatile struct smp_call_struct *smp_call_function_data;
89 enum ipi_message_type {
99 /********** SMP inter processor interrupt and communication routines */
101 #undef PER_CPU_IRQ_REGION
102 #ifdef PER_CPU_IRQ_REGION
103 /* XXX REVISIT Ignore for now.
104 ** *May* need this "hook" to register IPI handler
105 ** once we have perCPU ExtIntr switch tables.
111 /* If CPU is present ... */
112 #ifdef ENTRY_SYS_CPUS
113 /* *and* running (not stopped) ... */
114 #error iCOD support wants state checked here.
117 #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
119 if(cpu_online(cpuid) )
121 switch_to_idle_task(current);
130 ** Yoink this CPU from the runnable list...
136 #ifdef ENTRY_SYS_CPUS
137 #error halt_processor() needs rework
139 ** o migrate I/O interrupts off this CPU.
140 ** o leave IPI enabled - __cli() will disable IPI.
141 ** o leave CPU in online map - just change the state
143 cpu_data[this_cpu].state = STATE_STOPPED;
146 /* REVISIT : redirect I/O Interrupts to another CPU? */
147 /* REVISIT : does PM *know* this CPU isn't available? */
148 cpu_clear(smp_processor_id(), cpu_online_map);
157 ipi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
159 int this_cpu = smp_processor_id();
160 struct cpuinfo_parisc *p = &cpu_data[this_cpu];
164 /* Count this now; we may make a call that never returns. */
167 mb(); /* Order interrupt and bit testing. */
170 spin_lock_irqsave(&(p->lock),flags);
171 ops = p->pending_ipi;
173 spin_unlock_irqrestore(&(p->lock),flags);
175 mb(); /* Order bit clearing and data access. */
181 unsigned long which = ffz(~ops);
183 ops &= ~(1 << which);
188 printk(KERN_DEBUG "CPU%d IPI_NOP\n",this_cpu);
194 printk(KERN_DEBUG "CPU%d IPI_RESCHEDULE\n",this_cpu);
197 * Reschedule callback. Everything to be
198 * done is done by the interrupt return path.
204 printk(KERN_DEBUG "CPU%d IPI_CALL_FUNC\n",this_cpu);
207 volatile struct smp_call_struct *data;
208 void (*func)(void *info);
212 data = smp_call_function_data;
218 atomic_dec ((atomic_t *)&data->unstarted_count);
220 /* At this point, *data can't
226 /* Notify the sending CPU that the
231 atomic_dec ((atomic_t *)&data->unfinished_count);
237 printk(KERN_DEBUG "CPU%d IPI_CPU_START\n",this_cpu);
239 #ifdef ENTRY_SYS_CPUS
240 p->state = STATE_RUNNING;
246 printk(KERN_DEBUG "CPU%d IPI_CPU_STOP\n",this_cpu);
248 #ifdef ENTRY_SYS_CPUS
256 printk(KERN_DEBUG "CPU%d is alive!\n",this_cpu);
261 printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
272 ipi_send(int cpu, enum ipi_message_type op)
274 struct cpuinfo_parisc *p = &cpu_data[cpu];
277 spin_lock_irqsave(&(p->lock),flags);
278 p->pending_ipi |= 1 << op;
279 gsc_writel(IPI_IRQ - CPU_IRQ_BASE, cpu_data[cpu].hpa);
280 spin_unlock_irqrestore(&(p->lock),flags);
285 send_IPI_single(int dest_cpu, enum ipi_message_type op)
287 if (dest_cpu == NO_PROC_ID) {
292 ipi_send(dest_cpu, op);
296 send_IPI_allbutself(enum ipi_message_type op)
300 for_each_online_cpu(i) {
301 if (i != smp_processor_id())
302 send_IPI_single(i, op);
308 smp_send_stop(void) { send_IPI_allbutself(IPI_CPU_STOP); }
311 smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
314 smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
317 smp_send_all_nop(void)
319 send_IPI_allbutself(IPI_NOP);
324 * Run a function on all other CPUs.
325 * <func> The function to run. This must be fast and non-blocking.
326 * <info> An arbitrary pointer to pass to the function.
327 * <retry> If true, keep retrying until ready.
328 * <wait> If true, wait until function has completed on other CPUs.
329 * [RETURNS] 0 on success, else a negative status code.
331 * Does not return until remote CPUs are nearly ready to execute <func>
336 smp_call_function (void (*func) (void *info), void *info, int retry, int wait)
338 struct smp_call_struct data;
339 unsigned long timeout;
340 static DEFINE_SPINLOCK(lock);
343 if (num_online_cpus() < 2)
346 /* Can deadlock when called with interrupts disabled */
347 WARN_ON(irqs_disabled());
349 /* can also deadlock if IPIs are disabled */
350 WARN_ON((get_eiem() & (1UL<<(CPU_IRQ_MAX - IPI_IRQ))) == 0);
356 atomic_set(&data.unstarted_count, num_online_cpus() - 1);
357 atomic_set(&data.unfinished_count, num_online_cpus() - 1);
361 while (smp_call_function_data != 0)
366 if (smp_call_function_data) {
372 smp_call_function_data = &data;
375 /* Send a message to all other CPUs and wait for them to respond */
376 send_IPI_allbutself(IPI_CALL_FUNC);
379 /* Wait for response */
380 timeout = jiffies + HZ;
381 while ( (atomic_read (&data.unstarted_count) > 0) &&
382 time_before (jiffies, timeout) )
385 if (atomic_read (&data.unstarted_count) > 0) {
386 printk(KERN_CRIT "SMP CALL FUNCTION TIMED OUT! (cpu=%d), try %d\n",
387 smp_processor_id(), ++retries);
390 /* We either got one or timed out. Release the lock */
393 smp_call_function_data = NULL;
395 while (wait && atomic_read (&data.unfinished_count) > 0)
401 EXPORT_SYMBOL(smp_call_function);
404 * Flush all other CPU's tlb and then mine. Do this with on_each_cpu()
405 * as we want to ensure all TLB's flushed before proceeding.
409 smp_flush_tlb_all(void)
411 on_each_cpu(flush_tlb_all_local, NULL, 1, 1);
416 smp_do_timer(struct pt_regs *regs)
418 int cpu = smp_processor_id();
419 struct cpuinfo_parisc *data = &cpu_data[cpu];
421 if (!--data->prof_counter) {
422 data->prof_counter = data->prof_multiplier;
423 update_process_times(user_mode(regs));
428 * Called by secondaries to update state and initialize CPU registers.
431 smp_cpu_init(int cpunum)
433 extern int init_per_cpu(int); /* arch/parisc/kernel/processor.c */
434 extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
435 extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
437 /* Set modes and Enable floating point coprocessor */
438 (void) init_per_cpu(cpunum);
440 disable_sr_hashing();
444 /* Well, support 2.4 linux scheme as well. */
445 if (cpu_test_and_set(cpunum, cpu_online_map))
447 extern void machine_halt(void); /* arch/parisc.../process.c */
449 printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
453 /* Initialise the idle task for this CPU */
454 atomic_inc(&init_mm.mm_count);
455 current->active_mm = &init_mm;
458 enter_lazy_tlb(&init_mm, current);
460 init_IRQ(); /* make sure no IRQ's are enabled or pending */
466 * Slaves start using C here. Indirectly called from smp_slave_stext.
467 * Do what start_kernel() and main() do for boot strap processor (aka monarch)
469 void __init smp_callin(void)
471 int slave_id = cpu_now_booting;
476 smp_cpu_init(slave_id);
479 #if 0 /* NOT WORKING YET - see entry.S */
480 istack = (void *)__get_free_pages(GFP_KERNEL,ISTACK_ORDER);
481 if (istack == NULL) {
482 printk(KERN_CRIT "Failed to allocate interrupt stack for cpu %d\n",slave_id);
488 flush_cache_all_local(); /* start with known state */
489 flush_tlb_all_local(NULL);
491 local_irq_enable(); /* Interrupts have been off until now */
493 cpu_idle(); /* Wait for timer to schedule some work */
496 panic("smp_callin() AAAAaaaaahhhh....\n");
500 * Bring one cpu online.
502 int __init smp_boot_one_cpu(int cpuid)
504 struct task_struct *idle;
508 * Create an idle task for this CPU. Note the address wed* give
509 * to kernel_thread is irrelevant -- it's going to start
510 * where OS_BOOT_RENDEVZ vector in SAL says to start. But
511 * this gets all the other task-y sort of data structures set
512 * up like we wish. We need to pull the just created idle task
513 * off the run queue and stuff it into the init_tasks[] array.
517 idle = fork_idle(cpuid);
519 panic("SMP: fork failed for CPU:%d", cpuid);
521 task_thread_info(idle)->cpu = cpuid;
523 /* Let _start know what logical CPU we're booting
524 ** (offset into init_tasks[],cpu_data[])
526 cpu_now_booting = cpuid;
529 ** boot strap code needs to know the task address since
530 ** it also contains the process stack.
532 smp_init_current_idle_task = idle ;
535 printk("Releasing cpu %d now, hpa=%lx\n", cpuid, cpu_data[cpuid].hpa);
538 ** This gets PDC to release the CPU from a very tight loop.
540 ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
541 ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
542 ** is executed after receiving the rendezvous signal (an interrupt to
543 ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
544 ** contents of memory are valid."
546 gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, cpu_data[cpuid].hpa);
550 * OK, wait a bit for that CPU to finish staggering about.
551 * Slave will set a bit when it reaches smp_cpu_init().
552 * Once the "monarch CPU" sees the bit change, it can move on.
554 for (timeout = 0; timeout < 10000; timeout++) {
555 if(cpu_online(cpuid)) {
556 /* Which implies Slave has started up */
558 smp_init_current_idle_task = NULL;
565 put_task_struct(idle);
568 printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
572 /* Remember the Slave data */
574 printk(KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
575 cpuid, timeout * 100);
577 #ifdef ENTRY_SYS_CPUS
578 cpu_data[cpuid].state = STATE_RUNNING;
583 void __devinit smp_prepare_boot_cpu(void)
585 int bootstrap_processor=cpu_data[0].cpuid; /* CPU ID of BSP */
587 #ifdef ENTRY_SYS_CPUS
588 cpu_data[0].state = STATE_RUNNING;
591 /* Setup BSP mappings */
592 printk("SMP: bootstrap CPU ID is %d\n",bootstrap_processor);
594 cpu_set(bootstrap_processor, cpu_online_map);
595 cpu_set(bootstrap_processor, cpu_present_map);
601 ** inventory.c:do_inventory() hasn't yet been run and thus we
602 ** don't 'discover' the additional CPU's until later.
604 void __init smp_prepare_cpus(unsigned int max_cpus)
606 cpus_clear(cpu_present_map);
607 cpu_set(0, cpu_present_map);
609 parisc_max_cpus = max_cpus;
611 printk(KERN_INFO "SMP mode deactivated.\n");
615 void smp_cpus_done(unsigned int cpu_max)
621 int __devinit __cpu_up(unsigned int cpu)
623 if (cpu != 0 && cpu < parisc_max_cpus)
624 smp_boot_one_cpu(cpu);
626 return cpu_online(cpu) ? 0 : -ENOSYS;
631 #ifdef ENTRY_SYS_CPUS
632 /* Code goes along with:
633 ** entry.s: ENTRY_NAME(sys_cpus) / * 215, for cpu stat * /
635 int sys_cpus(int argc, char **argv)
638 extern int current_pid(int cpu);
641 printk("sys_cpus:Only one argument supported\n");
646 #ifdef DUMP_MORE_STATE
647 for_each_online_cpu(i) {
648 int cpus_per_line = 4;
650 if (j++ % cpus_per_line)
659 } else if((argc==2) && !(strcmp(argv[1],"-l"))) {
660 printk("\nCPUSTATE TASK CPUNUM CPUID HARDCPU(HPA)\n");
661 #ifdef DUMP_MORE_STATE
662 for_each_online_cpu(i) {
663 if (cpu_data[i].cpuid != NO_PROC_ID) {
664 switch(cpu_data[i].state) {
665 case STATE_RENDEZVOUS:
669 printk((current_pid(i)!=0) ? "RUNNING " : "IDLING ");
678 printk("%08x?", cpu_data[i].state);
682 printk(" %4d",current_pid(i));
684 printk(" %6d",cpu_number_map(i));
686 printk(" 0x%lx\n",cpu_data[i].hpa);
690 printk("\n%s %4d 0 0 --------",
691 (current->pid)?"RUNNING ": "IDLING ",current->pid);
693 } else if ((argc==2) && !(strcmp(argv[1],"-s"))) {
694 #ifdef DUMP_MORE_STATE
695 printk("\nCPUSTATE CPUID\n");
696 for_each_online_cpu(i) {
697 if (cpu_data[i].cpuid != NO_PROC_ID) {
698 switch(cpu_data[i].state) {
699 case STATE_RENDEZVOUS:
700 printk("RENDEZVS");break;
702 printk((current_pid(i)!=0) ? "RUNNING " : "IDLING");
705 printk("STOPPED ");break;
707 printk("HALTED ");break;
714 printk("\n%s CPU0",(current->pid==0)?"RUNNING ":"IDLING ");
717 printk("sys_cpus:Unknown request\n");
722 #endif /* ENTRY_SYS_CPUS */
724 #ifdef CONFIG_PROC_FS
726 setup_profiling_timer(unsigned int multiplier)