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 */
21 #include <linux/autoconf.h>
23 #include <linux/types.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
32 #include <linux/smp.h>
33 #include <linux/kernel_stat.h>
35 #include <linux/delay.h>
36 #include <linux/bitops.h>
38 #include <asm/system.h>
39 #include <asm/atomic.h>
40 #include <asm/current.h>
41 #include <asm/delay.h>
42 #include <asm/pgalloc.h> /* for flush_tlb_all() proto/macro */
45 #include <asm/irq.h> /* for CPU_IRQ_REGION and friends */
46 #include <asm/mmu_context.h>
48 #include <asm/pgtable.h>
49 #include <asm/pgalloc.h>
50 #include <asm/processor.h>
51 #include <asm/ptrace.h>
52 #include <asm/unistd.h>
53 #include <asm/cacheflush.h>
57 spinlock_t smp_lock = SPIN_LOCK_UNLOCKED;
59 volatile struct task_struct *smp_init_current_idle_task;
61 static volatile int cpu_now_booting = 0; /* track which CPU is booting */
63 unsigned long cache_decay_ticks; /* declared by include/linux/sched.h */
65 static int parisc_max_cpus = 1;
67 /* online cpus are ones that we've managed to bring up completely
68 * possible cpus are all valid cpu
69 * present cpus are all detected cpu
71 * On startup we bring up the "possible" cpus. Since we discover
72 * CPUs later, we add them as hotplug, so the possible cpu mask is
73 * empty in the beginning.
76 cpumask_t cpu_online_map = CPU_MASK_NONE; /* Bitmap of online CPUs */
77 cpumask_t cpu_possible_map = CPU_MASK_ALL; /* Bitmap of Present CPUs */
79 EXPORT_SYMBOL(cpu_online_map);
80 EXPORT_SYMBOL(cpu_possible_map);
83 struct smp_call_struct {
84 void (*func) (void *info);
87 atomic_t unstarted_count;
88 atomic_t unfinished_count;
90 static volatile struct smp_call_struct *smp_call_function_data;
92 enum ipi_message_type {
102 /********** SMP inter processor interrupt and communication routines */
104 #undef PER_CPU_IRQ_REGION
105 #ifdef PER_CPU_IRQ_REGION
106 /* XXX REVISIT Ignore for now.
107 ** *May* need this "hook" to register IPI handler
108 ** once we have perCPU ExtIntr switch tables.
114 /* If CPU is present ... */
115 #ifdef ENTRY_SYS_CPUS
116 /* *and* running (not stopped) ... */
117 #error iCOD support wants state checked here.
120 #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
122 if(cpu_online(cpuid) )
124 switch_to_idle_task(current);
133 ** Yoink this CPU from the runnable list...
139 #ifdef ENTRY_SYS_CPUS
140 #error halt_processor() needs rework
142 ** o migrate I/O interrupts off this CPU.
143 ** o leave IPI enabled - __cli() will disable IPI.
144 ** o leave CPU in online map - just change the state
146 cpu_data[this_cpu].state = STATE_STOPPED;
149 /* REVISIT : redirect I/O Interrupts to another CPU? */
150 /* REVISIT : does PM *know* this CPU isn't available? */
151 cpu_clear(smp_processor_id(), cpu_online_map);
160 ipi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
162 int this_cpu = smp_processor_id();
163 struct cpuinfo_parisc *p = &cpu_data[this_cpu];
167 /* Count this now; we may make a call that never returns. */
170 mb(); /* Order interrupt and bit testing. */
173 spin_lock_irqsave(&(p->lock),flags);
174 ops = p->pending_ipi;
176 spin_unlock_irqrestore(&(p->lock),flags);
178 mb(); /* Order bit clearing and data access. */
184 unsigned long which = ffz(~ops);
189 printk(KERN_DEBUG "CPU%d IPI_RESCHEDULE\n",this_cpu);
191 ops &= ~(1 << IPI_RESCHEDULE);
193 * Reschedule callback. Everything to be
194 * done is done by the interrupt return path.
200 printk(KERN_DEBUG "CPU%d IPI_CALL_FUNC\n",this_cpu);
202 ops &= ~(1 << IPI_CALL_FUNC);
204 volatile struct smp_call_struct *data;
205 void (*func)(void *info);
209 data = smp_call_function_data;
215 atomic_dec ((atomic_t *)&data->unstarted_count);
217 /* At this point, *data can't
223 /* Notify the sending CPU that the
228 atomic_dec ((atomic_t *)&data->unfinished_count);
234 printk(KERN_DEBUG "CPU%d IPI_CPU_START\n",this_cpu);
236 ops &= ~(1 << IPI_CPU_START);
237 #ifdef ENTRY_SYS_CPUS
238 p->state = STATE_RUNNING;
244 printk(KERN_DEBUG "CPU%d IPI_CPU_STOP\n",this_cpu);
246 ops &= ~(1 << IPI_CPU_STOP);
247 #ifdef ENTRY_SYS_CPUS
255 printk(KERN_DEBUG "CPU%d is alive!\n",this_cpu);
257 ops &= ~(1 << IPI_CPU_TEST);
261 printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
263 ops &= ~(1 << which);
273 ipi_send(int cpu, enum ipi_message_type op)
275 struct cpuinfo_parisc *p = &cpu_data[cpu];
278 spin_lock_irqsave(&(p->lock),flags);
279 p->pending_ipi |= 1 << op;
280 __raw_writel(IRQ_OFFSET(IPI_IRQ), cpu_data[cpu].hpa);
281 spin_unlock_irqrestore(&(p->lock),flags);
286 send_IPI_single(int dest_cpu, enum ipi_message_type op)
288 if (dest_cpu == NO_PROC_ID) {
293 ipi_send(dest_cpu, op);
297 send_IPI_allbutself(enum ipi_message_type op)
301 for (i = 0; i < NR_CPUS; i++) {
302 if (cpu_online(i) && i != smp_processor_id())
303 send_IPI_single(i, op);
309 smp_send_stop(void) { send_IPI_allbutself(IPI_CPU_STOP); }
312 smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
315 smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
319 * Run a function on all other CPUs.
320 * <func> The function to run. This must be fast and non-blocking.
321 * <info> An arbitrary pointer to pass to the function.
322 * <retry> If true, keep retrying until ready.
323 * <wait> If true, wait until function has completed on other CPUs.
324 * [RETURNS] 0 on success, else a negative status code.
326 * Does not return until remote CPUs are nearly ready to execute <func>
331 smp_call_function (void (*func) (void *info), void *info, int retry, int wait)
333 struct smp_call_struct data;
334 unsigned long timeout;
335 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
338 if (num_online_cpus() < 2)
341 /* Can deadlock when called with interrupts disabled */
342 WARN_ON(irqs_disabled());
347 atomic_set(&data.unstarted_count, num_online_cpus() - 1);
348 atomic_set(&data.unfinished_count, num_online_cpus() - 1);
352 while (smp_call_function_data != 0)
357 if (smp_call_function_data) {
363 smp_call_function_data = &data;
366 /* Send a message to all other CPUs and wait for them to respond */
367 send_IPI_allbutself(IPI_CALL_FUNC);
370 /* Wait for response */
371 timeout = jiffies + HZ;
372 while ( (atomic_read (&data.unstarted_count) > 0) &&
373 time_before (jiffies, timeout) )
376 if (atomic_read (&data.unstarted_count) > 0) {
377 printk(KERN_CRIT "SMP CALL FUNCTION TIMED OUT! (cpu=%d), try %d\n",
378 smp_processor_id(), ++retries);
381 /* We either got one or timed out. Release the lock */
384 smp_call_function_data = NULL;
386 while (wait && atomic_read (&data.unfinished_count) > 0)
392 EXPORT_SYMBOL(smp_call_function);
395 * Flush all other CPU's tlb and then mine. Do this with on_each_cpu()
396 * as we want to ensure all TLB's flushed before proceeding.
399 extern void flush_tlb_all_local(void);
402 smp_flush_tlb_all(void)
404 on_each_cpu((void (*)(void *))flush_tlb_all_local, NULL, 1, 1);
409 smp_do_timer(struct pt_regs *regs)
411 int cpu = smp_processor_id();
412 struct cpuinfo_parisc *data = &cpu_data[cpu];
414 if (!--data->prof_counter) {
415 data->prof_counter = data->prof_multiplier;
416 update_process_times(user_mode(regs));
421 * Called by secondaries to update state and initialize CPU registers.
424 smp_cpu_init(int cpunum)
426 extern int init_per_cpu(int); /* arch/parisc/kernel/setup.c */
427 extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
429 /* Set modes and Enable floating point coprocessor */
430 (void) init_per_cpu(cpunum);
432 disable_sr_hashing();
436 /* Well, support 2.4 linux scheme as well. */
437 if (cpu_test_and_set(cpunum, cpu_online_map))
439 extern void machine_halt(void); /* arch/parisc.../process.c */
441 printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
445 /* Initialise the idle task for this CPU */
446 atomic_inc(&init_mm.mm_count);
447 current->active_mm = &init_mm;
450 enter_lazy_tlb(&init_mm, current);
452 init_IRQ(); /* make sure no IRQ's are enabled or pending */
457 * Slaves start using C here. Indirectly called from smp_slave_stext.
458 * Do what start_kernel() and main() do for boot strap processor (aka monarch)
460 void __init smp_callin(void)
462 extern void cpu_idle(void); /* arch/parisc/kernel/process.c */
463 int slave_id = cpu_now_booting;
468 smp_cpu_init(slave_id);
470 #if 0 /* NOT WORKING YET - see entry.S */
471 istack = (void *)__get_free_pages(GFP_KERNEL,ISTACK_ORDER);
472 if (istack == NULL) {
473 printk(KERN_CRIT "Failed to allocate interrupt stack for cpu %d\n",slave_id);
479 flush_cache_all_local(); /* start with known state */
480 flush_tlb_all_local();
482 local_irq_enable(); /* Interrupts have been off until now */
484 cpu_idle(); /* Wait for timer to schedule some work */
487 panic("smp_callin() AAAAaaaaahhhh....\n");
491 * Bring one cpu online.
493 int __init smp_boot_one_cpu(int cpuid)
495 struct task_struct *idle;
499 * Create an idle task for this CPU. Note the address wed* give
500 * to kernel_thread is irrelevant -- it's going to start
501 * where OS_BOOT_RENDEVZ vector in SAL says to start. But
502 * this gets all the other task-y sort of data structures set
503 * up like we wish. We need to pull the just created idle task
504 * off the run queue and stuff it into the init_tasks[] array.
508 idle = fork_idle(cpuid);
510 panic("SMP: fork failed for CPU:%d", cpuid);
512 idle->thread_info->cpu = cpuid;
514 /* Let _start know what logical CPU we're booting
515 ** (offset into init_tasks[],cpu_data[])
517 cpu_now_booting = cpuid;
520 ** boot strap code needs to know the task address since
521 ** it also contains the process stack.
523 smp_init_current_idle_task = idle ;
526 printk("Releasing cpu %d now, hpa=%lx\n", cpuid, cpu_data[cpuid].hpa);
529 ** This gets PDC to release the CPU from a very tight loop.
531 ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
532 ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
533 ** is executed after receiving the rendezvous signal (an interrupt to
534 ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
535 ** contents of memory are valid."
537 __raw_writel(IRQ_OFFSET(TIMER_IRQ), cpu_data[cpuid].hpa);
541 * OK, wait a bit for that CPU to finish staggering about.
542 * Slave will set a bit when it reaches smp_cpu_init().
543 * Once the "monarch CPU" sees the bit change, it can move on.
545 for (timeout = 0; timeout < 10000; timeout++) {
546 if(cpu_online(cpuid)) {
547 /* Which implies Slave has started up */
549 smp_init_current_idle_task = NULL;
556 put_task_struct(idle);
559 printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
563 /* Remember the Slave data */
565 printk(KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
566 cpuid, timeout * 100);
568 #ifdef ENTRY_SYS_CPUS
569 cpu_data[cpuid].state = STATE_RUNNING;
574 void __devinit smp_prepare_boot_cpu(void)
576 int bootstrap_processor=cpu_data[0].cpuid; /* CPU ID of BSP */
578 #ifdef ENTRY_SYS_CPUS
579 cpu_data[0].state = STATE_RUNNING;
582 /* Setup BSP mappings */
583 printk("SMP: bootstrap CPU ID is %d\n",bootstrap_processor);
585 cpu_set(bootstrap_processor, cpu_online_map);
586 cpu_set(bootstrap_processor, cpu_present_map);
588 cache_decay_ticks = HZ/100; /* FIXME very rough. */
594 ** inventory.c:do_inventory() hasn't yet been run and thus we
595 ** don't 'discover' the additional CPU's until later.
597 void __init smp_prepare_cpus(unsigned int max_cpus)
599 cpus_clear(cpu_present_map);
600 cpu_set(0, cpu_present_map);
602 parisc_max_cpus = max_cpus;
604 printk(KERN_INFO "SMP mode deactivated.\n");
608 void smp_cpus_done(unsigned int cpu_max)
614 int __devinit __cpu_up(unsigned int cpu)
616 if (cpu != 0 && cpu < parisc_max_cpus)
617 smp_boot_one_cpu(cpu);
619 return cpu_online(cpu) ? 0 : -ENOSYS;
624 #ifdef ENTRY_SYS_CPUS
625 /* Code goes along with:
626 ** entry.s: ENTRY_NAME(sys_cpus) / * 215, for cpu stat * /
628 int sys_cpus(int argc, char **argv)
631 extern int current_pid(int cpu);
634 printk("sys_cpus:Only one argument supported\n");
639 #ifdef DUMP_MORE_STATE
640 for(i=0; i<NR_CPUS; i++) {
641 int cpus_per_line = 4;
643 if (j++ % cpus_per_line)
653 } else if((argc==2) && !(strcmp(argv[1],"-l"))) {
654 printk("\nCPUSTATE TASK CPUNUM CPUID HARDCPU(HPA)\n");
655 #ifdef DUMP_MORE_STATE
656 for(i=0;i<NR_CPUS;i++) {
659 if (cpu_data[i].cpuid != NO_PROC_ID) {
660 switch(cpu_data[i].state) {
661 case STATE_RENDEZVOUS:
665 printk((current_pid(i)!=0) ? "RUNNING " : "IDLING ");
674 printk("%08x?", cpu_data[i].state);
678 printk(" %4d",current_pid(i));
680 printk(" %6d",cpu_number_map(i));
682 printk(" 0x%lx\n",cpu_data[i].hpa);
686 printk("\n%s %4d 0 0 --------",
687 (current->pid)?"RUNNING ": "IDLING ",current->pid);
689 } else if ((argc==2) && !(strcmp(argv[1],"-s"))) {
690 #ifdef DUMP_MORE_STATE
691 printk("\nCPUSTATE CPUID\n");
692 for (i=0;i<NR_CPUS;i++) {
695 if (cpu_data[i].cpuid != NO_PROC_ID) {
696 switch(cpu_data[i].state) {
697 case STATE_RENDEZVOUS:
698 printk("RENDEZVS");break;
700 printk((current_pid(i)!=0) ? "RUNNING " : "IDLING");
703 printk("STOPPED ");break;
705 printk("HALTED ");break;
712 printk("\n%s CPU0",(current->pid==0)?"RUNNING ":"IDLING ");
715 printk("sys_cpus:Unknown request\n");
720 #endif /* ENTRY_SYS_CPUS */
722 #ifdef CONFIG_PROC_FS
724 setup_profiling_timer(unsigned int multiplier)