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/config.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/tlbflush.h>
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 DEFINE_SPINLOCK(smp_lock
);
59 volatile struct task_struct
*smp_init_current_idle_task
;
61 static volatile int cpu_now_booting __read_mostly
= 0; /* track which CPU is booting */
63 static int parisc_max_cpus __read_mostly
= 1;
65 /* online cpus are ones that we've managed to bring up completely
66 * possible cpus are all valid cpu
67 * present cpus are all detected cpu
69 * On startup we bring up the "possible" cpus. Since we discover
70 * CPUs later, we add them as hotplug, so the possible cpu mask is
71 * empty in the beginning.
74 cpumask_t cpu_online_map __read_mostly
= CPU_MASK_NONE
; /* Bitmap of online CPUs */
75 cpumask_t cpu_possible_map __read_mostly
= CPU_MASK_ALL
; /* Bitmap of Present CPUs */
77 EXPORT_SYMBOL(cpu_online_map
);
78 EXPORT_SYMBOL(cpu_possible_map
);
81 struct smp_call_struct
{
82 void (*func
) (void *info
);
85 atomic_t unstarted_count
;
86 atomic_t unfinished_count
;
88 static volatile struct smp_call_struct
*smp_call_function_data
;
90 enum ipi_message_type
{
100 /********** SMP inter processor interrupt and communication routines */
102 #undef PER_CPU_IRQ_REGION
103 #ifdef PER_CPU_IRQ_REGION
104 /* XXX REVISIT Ignore for now.
105 ** *May* need this "hook" to register IPI handler
106 ** once we have perCPU ExtIntr switch tables.
112 /* If CPU is present ... */
113 #ifdef ENTRY_SYS_CPUS
114 /* *and* running (not stopped) ... */
115 #error iCOD support wants state checked here.
118 #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
120 if(cpu_online(cpuid
) )
122 switch_to_idle_task(current
);
131 ** Yoink this CPU from the runnable list...
137 #ifdef ENTRY_SYS_CPUS
138 #error halt_processor() needs rework
140 ** o migrate I/O interrupts off this CPU.
141 ** o leave IPI enabled - __cli() will disable IPI.
142 ** o leave CPU in online map - just change the state
144 cpu_data
[this_cpu
].state
= STATE_STOPPED
;
147 /* REVISIT : redirect I/O Interrupts to another CPU? */
148 /* REVISIT : does PM *know* this CPU isn't available? */
149 cpu_clear(smp_processor_id(), cpu_online_map
);
158 ipi_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
160 int this_cpu
= smp_processor_id();
161 struct cpuinfo_parisc
*p
= &cpu_data
[this_cpu
];
165 /* Count this now; we may make a call that never returns. */
168 mb(); /* Order interrupt and bit testing. */
171 spin_lock_irqsave(&(p
->lock
),flags
);
172 ops
= p
->pending_ipi
;
174 spin_unlock_irqrestore(&(p
->lock
),flags
);
176 mb(); /* Order bit clearing and data access. */
182 unsigned long which
= ffz(~ops
);
184 ops
&= ~(1 << which
);
189 printk(KERN_DEBUG
"CPU%d IPI_NOP\n",this_cpu
);
195 printk(KERN_DEBUG
"CPU%d IPI_RESCHEDULE\n",this_cpu
);
198 * Reschedule callback. Everything to be
199 * done is done by the interrupt return path.
205 printk(KERN_DEBUG
"CPU%d IPI_CALL_FUNC\n",this_cpu
);
208 volatile struct smp_call_struct
*data
;
209 void (*func
)(void *info
);
213 data
= smp_call_function_data
;
219 atomic_dec ((atomic_t
*)&data
->unstarted_count
);
221 /* At this point, *data can't
227 /* Notify the sending CPU that the
232 atomic_dec ((atomic_t
*)&data
->unfinished_count
);
238 printk(KERN_DEBUG
"CPU%d IPI_CPU_START\n",this_cpu
);
240 #ifdef ENTRY_SYS_CPUS
241 p
->state
= STATE_RUNNING
;
247 printk(KERN_DEBUG
"CPU%d IPI_CPU_STOP\n",this_cpu
);
249 #ifdef ENTRY_SYS_CPUS
257 printk(KERN_DEBUG
"CPU%d is alive!\n",this_cpu
);
262 printk(KERN_CRIT
"Unknown IPI num on CPU%d: %lu\n",
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 gsc_writel(IPI_IRQ
- CPU_IRQ_BASE
, 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_each_online_cpu(i
) {
302 if (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
); }
318 smp_send_all_nop(void)
320 send_IPI_allbutself(IPI_NOP
);
325 * Run a function on all other CPUs.
326 * <func> The function to run. This must be fast and non-blocking.
327 * <info> An arbitrary pointer to pass to the function.
328 * <retry> If true, keep retrying until ready.
329 * <wait> If true, wait until function has completed on other CPUs.
330 * [RETURNS] 0 on success, else a negative status code.
332 * Does not return until remote CPUs are nearly ready to execute <func>
337 smp_call_function (void (*func
) (void *info
), void *info
, int retry
, int wait
)
339 struct smp_call_struct data
;
340 unsigned long timeout
;
341 static DEFINE_SPINLOCK(lock
);
344 if (num_online_cpus() < 2)
347 /* Can deadlock when called with interrupts disabled */
348 WARN_ON(irqs_disabled());
350 /* can also deadlock if IPIs are disabled */
351 WARN_ON((get_eiem() & (1UL<<(CPU_IRQ_MAX
- IPI_IRQ
))) == 0);
357 atomic_set(&data
.unstarted_count
, num_online_cpus() - 1);
358 atomic_set(&data
.unfinished_count
, num_online_cpus() - 1);
362 while (smp_call_function_data
!= 0)
367 if (smp_call_function_data
) {
373 smp_call_function_data
= &data
;
376 /* Send a message to all other CPUs and wait for them to respond */
377 send_IPI_allbutself(IPI_CALL_FUNC
);
380 /* Wait for response */
381 timeout
= jiffies
+ HZ
;
382 while ( (atomic_read (&data
.unstarted_count
) > 0) &&
383 time_before (jiffies
, timeout
) )
386 if (atomic_read (&data
.unstarted_count
) > 0) {
387 printk(KERN_CRIT
"SMP CALL FUNCTION TIMED OUT! (cpu=%d), try %d\n",
388 smp_processor_id(), ++retries
);
391 /* We either got one or timed out. Release the lock */
394 smp_call_function_data
= NULL
;
396 while (wait
&& atomic_read (&data
.unfinished_count
) > 0)
402 EXPORT_SYMBOL(smp_call_function
);
405 * Flush all other CPU's tlb and then mine. Do this with on_each_cpu()
406 * as we want to ensure all TLB's flushed before proceeding.
410 smp_flush_tlb_all(void)
412 on_each_cpu(flush_tlb_all_local
, NULL
, 1, 1);
417 smp_do_timer(struct pt_regs
*regs
)
419 int cpu
= smp_processor_id();
420 struct cpuinfo_parisc
*data
= &cpu_data
[cpu
];
422 if (!--data
->prof_counter
) {
423 data
->prof_counter
= data
->prof_multiplier
;
424 update_process_times(user_mode(regs
));
429 * Called by secondaries to update state and initialize CPU registers.
432 smp_cpu_init(int cpunum
)
434 extern int init_per_cpu(int); /* arch/parisc/kernel/setup.c */
435 extern void init_IRQ(void); /* arch/parisc/kernel/irq.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 */
465 * Slaves start using C here. Indirectly called from smp_slave_stext.
466 * Do what start_kernel() and main() do for boot strap processor (aka monarch)
468 void __init
smp_callin(void)
470 int slave_id
= cpu_now_booting
;
475 smp_cpu_init(slave_id
);
478 #if 0 /* NOT WORKING YET - see entry.S */
479 istack
= (void *)__get_free_pages(GFP_KERNEL
,ISTACK_ORDER
);
480 if (istack
== NULL
) {
481 printk(KERN_CRIT
"Failed to allocate interrupt stack for cpu %d\n",slave_id
);
487 flush_cache_all_local(); /* start with known state */
488 flush_tlb_all_local(NULL
);
490 local_irq_enable(); /* Interrupts have been off until now */
492 cpu_idle(); /* Wait for timer to schedule some work */
495 panic("smp_callin() AAAAaaaaahhhh....\n");
499 * Bring one cpu online.
501 int __init
smp_boot_one_cpu(int cpuid
)
503 struct task_struct
*idle
;
507 * Create an idle task for this CPU. Note the address wed* give
508 * to kernel_thread is irrelevant -- it's going to start
509 * where OS_BOOT_RENDEVZ vector in SAL says to start. But
510 * this gets all the other task-y sort of data structures set
511 * up like we wish. We need to pull the just created idle task
512 * off the run queue and stuff it into the init_tasks[] array.
516 idle
= fork_idle(cpuid
);
518 panic("SMP: fork failed for CPU:%d", cpuid
);
520 task_thread_info(idle
)->cpu
= cpuid
;
522 /* Let _start know what logical CPU we're booting
523 ** (offset into init_tasks[],cpu_data[])
525 cpu_now_booting
= cpuid
;
528 ** boot strap code needs to know the task address since
529 ** it also contains the process stack.
531 smp_init_current_idle_task
= idle
;
534 printk("Releasing cpu %d now, hpa=%lx\n", cpuid
, cpu_data
[cpuid
].hpa
);
537 ** This gets PDC to release the CPU from a very tight loop.
539 ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
540 ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
541 ** is executed after receiving the rendezvous signal (an interrupt to
542 ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
543 ** contents of memory are valid."
545 gsc_writel(TIMER_IRQ
- CPU_IRQ_BASE
, cpu_data
[cpuid
].hpa
);
549 * OK, wait a bit for that CPU to finish staggering about.
550 * Slave will set a bit when it reaches smp_cpu_init().
551 * Once the "monarch CPU" sees the bit change, it can move on.
553 for (timeout
= 0; timeout
< 10000; timeout
++) {
554 if(cpu_online(cpuid
)) {
555 /* Which implies Slave has started up */
557 smp_init_current_idle_task
= NULL
;
564 put_task_struct(idle
);
567 printk(KERN_CRIT
"SMP: CPU:%d is stuck.\n", cpuid
);
571 /* Remember the Slave data */
573 printk(KERN_DEBUG
"SMP: CPU:%d came alive after %ld _us\n",
574 cpuid
, timeout
* 100);
576 #ifdef ENTRY_SYS_CPUS
577 cpu_data
[cpuid
].state
= STATE_RUNNING
;
582 void __devinit
smp_prepare_boot_cpu(void)
584 int bootstrap_processor
=cpu_data
[0].cpuid
; /* CPU ID of BSP */
586 #ifdef ENTRY_SYS_CPUS
587 cpu_data
[0].state
= STATE_RUNNING
;
590 /* Setup BSP mappings */
591 printk("SMP: bootstrap CPU ID is %d\n",bootstrap_processor
);
593 cpu_set(bootstrap_processor
, cpu_online_map
);
594 cpu_set(bootstrap_processor
, cpu_present_map
);
600 ** inventory.c:do_inventory() hasn't yet been run and thus we
601 ** don't 'discover' the additional CPU's until later.
603 void __init
smp_prepare_cpus(unsigned int max_cpus
)
605 cpus_clear(cpu_present_map
);
606 cpu_set(0, cpu_present_map
);
608 parisc_max_cpus
= max_cpus
;
610 printk(KERN_INFO
"SMP mode deactivated.\n");
614 void smp_cpus_done(unsigned int cpu_max
)
620 int __devinit
__cpu_up(unsigned int cpu
)
622 if (cpu
!= 0 && cpu
< parisc_max_cpus
)
623 smp_boot_one_cpu(cpu
);
625 return cpu_online(cpu
) ? 0 : -ENOSYS
;
630 #ifdef ENTRY_SYS_CPUS
631 /* Code goes along with:
632 ** entry.s: ENTRY_NAME(sys_cpus) / * 215, for cpu stat * /
634 int sys_cpus(int argc
, char **argv
)
637 extern int current_pid(int cpu
);
640 printk("sys_cpus:Only one argument supported\n");
645 #ifdef DUMP_MORE_STATE
646 for_each_online_cpu(i
) {
647 int cpus_per_line
= 4;
649 if (j
++ % cpus_per_line
)
658 } else if((argc
==2) && !(strcmp(argv
[1],"-l"))) {
659 printk("\nCPUSTATE TASK CPUNUM CPUID HARDCPU(HPA)\n");
660 #ifdef DUMP_MORE_STATE
661 for_each_online_cpu(i
) {
662 if (cpu_data
[i
].cpuid
!= NO_PROC_ID
) {
663 switch(cpu_data
[i
].state
) {
664 case STATE_RENDEZVOUS
:
668 printk((current_pid(i
)!=0) ? "RUNNING " : "IDLING ");
677 printk("%08x?", cpu_data
[i
].state
);
681 printk(" %4d",current_pid(i
));
683 printk(" %6d",cpu_number_map(i
));
685 printk(" 0x%lx\n",cpu_data
[i
].hpa
);
689 printk("\n%s %4d 0 0 --------",
690 (current
->pid
)?"RUNNING ": "IDLING ",current
->pid
);
692 } else if ((argc
==2) && !(strcmp(argv
[1],"-s"))) {
693 #ifdef DUMP_MORE_STATE
694 printk("\nCPUSTATE CPUID\n");
695 for_each_online_cpu(i
) {
696 if (cpu_data
[i
].cpuid
!= NO_PROC_ID
) {
697 switch(cpu_data
[i
].state
) {
698 case STATE_RENDEZVOUS
:
699 printk("RENDEZVS");break;
701 printk((current_pid(i
)!=0) ? "RUNNING " : "IDLING");
704 printk("STOPPED ");break;
706 printk("HALTED ");break;
713 printk("\n%s CPU0",(current
->pid
==0)?"RUNNING ":"IDLING ");
716 printk("sys_cpus:Unknown request\n");
721 #endif /* ENTRY_SYS_CPUS */
723 #ifdef CONFIG_PROC_FS
725 setup_profiling_timer(unsigned int multiplier
)