]> Pileus Git - ~andy/linux/blob - arch/parisc/kernel/smp.c
Merge branch 'parisc' from /home/kyle/repos/parisc-2.6.git
[~andy/linux] / arch / parisc / kernel / smp.c
1 /*
2 ** SMP Support
3 **
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>
7 ** 
8 ** Lots of stuff stolen from arch/alpha/kernel/smp.c
9 ** ...and then parisc stole from arch/ia64/kernel/smp.c. Thanks David! :^)
10 **
11 ** Thanks to John Curry and Ullas Ponnadi. I learned alot from their work.
12 ** -grant (1/12/2001)
13 **
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.
18 */
19 #undef ENTRY_SYS_CPUS   /* syscall support for iCOD-like functionality */
20
21
22 #include <linux/types.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25
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>
33 #include <linux/mm.h>
34 #include <linux/delay.h>
35 #include <linux/bitops.h>
36
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>
42
43 #include <asm/io.h>
44 #include <asm/irq.h>            /* for CPU_IRQ_REGION and friends */
45 #include <asm/mmu_context.h>
46 #include <asm/page.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>
53
54 #define kDEBUG 0
55
56 DEFINE_SPINLOCK(smp_lock);
57
58 volatile struct task_struct *smp_init_current_idle_task;
59
60 static volatile int cpu_now_booting __read_mostly = 0;  /* track which CPU is booting */
61
62 static int parisc_max_cpus __read_mostly = 1;
63
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
67  *
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.
71  */
72
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 */
75
76 EXPORT_SYMBOL(cpu_online_map);
77 EXPORT_SYMBOL(cpu_possible_map);
78
79 DEFINE_PER_CPU(spinlock_t, ipi_lock) = SPIN_LOCK_UNLOCKED;
80
81 struct smp_call_struct {
82         void (*func) (void *info);
83         void *info;
84         long wait;
85         atomic_t unstarted_count;
86         atomic_t unfinished_count;
87 };
88 static volatile struct smp_call_struct *smp_call_function_data;
89
90 enum ipi_message_type {
91         IPI_NOP=0,
92         IPI_RESCHEDULE=1,
93         IPI_CALL_FUNC,
94         IPI_CPU_START,
95         IPI_CPU_STOP,
96         IPI_CPU_TEST
97 };
98
99
100 /********** SMP inter processor interrupt and communication routines */
101
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.
107 */
108 static void
109 ipi_init(int cpuid)
110 {
111
112         /* If CPU is present ... */
113 #ifdef ENTRY_SYS_CPUS
114         /* *and* running (not stopped) ... */
115 #error iCOD support wants state checked here.
116 #endif
117
118 #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
119
120         if(cpu_online(cpuid) )
121         {
122                 switch_to_idle_task(current);
123         }
124
125         return;
126 }
127 #endif
128
129
130 /*
131 ** Yoink this CPU from the runnable list... 
132 **
133 */
134 static void
135 halt_processor(void) 
136 {
137 #ifdef ENTRY_SYS_CPUS
138 #error halt_processor() needs rework
139 /*
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
143 */
144         cpu_data[this_cpu].state = STATE_STOPPED;
145         mark_bh(IPI_BH);
146 #else
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);
150         local_irq_disable();
151         for (;;)
152                 ;
153 #endif
154 }
155
156
157 irqreturn_t
158 ipi_interrupt(int irq, void *dev_id) 
159 {
160         int this_cpu = smp_processor_id();
161         struct cpuinfo_parisc *p = &cpu_data[this_cpu];
162         unsigned long ops;
163         unsigned long flags;
164
165         /* Count this now; we may make a call that never returns. */
166         p->ipi_count++;
167
168         mb();   /* Order interrupt and bit testing. */
169
170         for (;;) {
171                 spinlock_t *lock = &per_cpu(ipi_lock, this_cpu);
172                 spin_lock_irqsave(lock, flags);
173                 ops = p->pending_ipi;
174                 p->pending_ipi = 0;
175                 spin_unlock_irqrestore(lock, flags);
176
177                 mb(); /* Order bit clearing and data access. */
178
179                 if (!ops)
180                     break;
181
182                 while (ops) {
183                         unsigned long which = ffz(~ops);
184
185                         ops &= ~(1 << which);
186
187                         switch (which) {
188                         case IPI_NOP:
189 #if (kDEBUG>=100)
190                                 printk(KERN_DEBUG "CPU%d IPI_NOP\n",this_cpu);
191 #endif /* kDEBUG */
192                                 break;
193                                 
194                         case IPI_RESCHEDULE:
195 #if (kDEBUG>=100)
196                                 printk(KERN_DEBUG "CPU%d IPI_RESCHEDULE\n",this_cpu);
197 #endif /* kDEBUG */
198                                 /*
199                                  * Reschedule callback.  Everything to be
200                                  * done is done by the interrupt return path.
201                                  */
202                                 break;
203
204                         case IPI_CALL_FUNC:
205 #if (kDEBUG>=100)
206                                 printk(KERN_DEBUG "CPU%d IPI_CALL_FUNC\n",this_cpu);
207 #endif /* kDEBUG */
208                                 {
209                                         volatile struct smp_call_struct *data;
210                                         void (*func)(void *info);
211                                         void *info;
212                                         int wait;
213
214                                         data = smp_call_function_data;
215                                         func = data->func;
216                                         info = data->info;
217                                         wait = data->wait;
218
219                                         mb();
220                                         atomic_dec ((atomic_t *)&data->unstarted_count);
221
222                                         /* At this point, *data can't
223                                          * be relied upon.
224                                          */
225
226                                         (*func)(info);
227
228                                         /* Notify the sending CPU that the
229                                          * task is done.
230                                          */
231                                         mb();
232                                         if (wait)
233                                                 atomic_dec ((atomic_t *)&data->unfinished_count);
234                                 }
235                                 break;
236
237                         case IPI_CPU_START:
238 #if (kDEBUG>=100)
239                                 printk(KERN_DEBUG "CPU%d IPI_CPU_START\n",this_cpu);
240 #endif /* kDEBUG */
241 #ifdef ENTRY_SYS_CPUS
242                                 p->state = STATE_RUNNING;
243 #endif
244                                 break;
245
246                         case IPI_CPU_STOP:
247 #if (kDEBUG>=100)
248                                 printk(KERN_DEBUG "CPU%d IPI_CPU_STOP\n",this_cpu);
249 #endif /* kDEBUG */
250 #ifdef ENTRY_SYS_CPUS
251 #else
252                                 halt_processor();
253 #endif
254                                 break;
255
256                         case IPI_CPU_TEST:
257 #if (kDEBUG>=100)
258                                 printk(KERN_DEBUG "CPU%d is alive!\n",this_cpu);
259 #endif /* kDEBUG */
260                                 break;
261
262                         default:
263                                 printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
264                                         this_cpu, which);
265                                 return IRQ_NONE;
266                         } /* Switch */
267                 /* let in any pending interrupts */
268                 local_irq_enable();
269                 local_irq_disable();
270                 } /* while (ops) */
271         }
272         return IRQ_HANDLED;
273 }
274
275
276 static inline void
277 ipi_send(int cpu, enum ipi_message_type op)
278 {
279         struct cpuinfo_parisc *p = &cpu_data[cpu];
280         spinlock_t *lock = &per_cpu(ipi_lock, cpu);
281         unsigned long flags;
282
283         spin_lock_irqsave(lock, flags);
284         p->pending_ipi |= 1 << op;
285         gsc_writel(IPI_IRQ - CPU_IRQ_BASE, cpu_data[cpu].hpa);
286         spin_unlock_irqrestore(lock, flags);
287 }
288
289
290 static inline void
291 send_IPI_single(int dest_cpu, enum ipi_message_type op)
292 {
293         if (dest_cpu == NO_PROC_ID) {
294                 BUG();
295                 return;
296         }
297
298         ipi_send(dest_cpu, op);
299 }
300
301 static inline void
302 send_IPI_allbutself(enum ipi_message_type op)
303 {
304         int i;
305         
306         for_each_online_cpu(i) {
307                 if (i != smp_processor_id())
308                         send_IPI_single(i, op);
309         }
310 }
311
312
313 inline void 
314 smp_send_stop(void)     { send_IPI_allbutself(IPI_CPU_STOP); }
315
316 static inline void
317 smp_send_start(void)    { send_IPI_allbutself(IPI_CPU_START); }
318
319 void 
320 smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
321
322 void
323 smp_send_all_nop(void)
324 {
325         send_IPI_allbutself(IPI_NOP);
326 }
327
328
329 /**
330  * Run a function on all other CPUs.
331  *  <func>      The function to run. This must be fast and non-blocking.
332  *  <info>      An arbitrary pointer to pass to the function.
333  *  <retry>     If true, keep retrying until ready.
334  *  <wait>      If true, wait until function has completed on other CPUs.
335  *  [RETURNS]   0 on success, else a negative status code.
336  *
337  * Does not return until remote CPUs are nearly ready to execute <func>
338  * or have executed.
339  */
340
341 int
342 smp_call_function (void (*func) (void *info), void *info, int retry, int wait)
343 {
344         struct smp_call_struct data;
345         unsigned long timeout;
346         static DEFINE_SPINLOCK(lock);
347         int retries = 0;
348
349         if (num_online_cpus() < 2)
350                 return 0;
351
352         /* Can deadlock when called with interrupts disabled */
353         WARN_ON(irqs_disabled());
354
355         /* can also deadlock if IPIs are disabled */
356         WARN_ON((get_eiem() & (1UL<<(CPU_IRQ_MAX - IPI_IRQ))) == 0);
357
358         
359         data.func = func;
360         data.info = info;
361         data.wait = wait;
362         atomic_set(&data.unstarted_count, num_online_cpus() - 1);
363         atomic_set(&data.unfinished_count, num_online_cpus() - 1);
364
365         if (retry) {
366                 spin_lock (&lock);
367                 while (smp_call_function_data != 0)
368                         barrier();
369         }
370         else {
371                 spin_lock (&lock);
372                 if (smp_call_function_data) {
373                         spin_unlock (&lock);
374                         return -EBUSY;
375                 }
376         }
377
378         smp_call_function_data = &data;
379         spin_unlock (&lock);
380         
381         /*  Send a message to all other CPUs and wait for them to respond  */
382         send_IPI_allbutself(IPI_CALL_FUNC);
383
384  retry:
385         /*  Wait for response  */
386         timeout = jiffies + HZ;
387         while ( (atomic_read (&data.unstarted_count) > 0) &&
388                 time_before (jiffies, timeout) )
389                 barrier ();
390
391         if (atomic_read (&data.unstarted_count) > 0) {
392                 printk(KERN_CRIT "SMP CALL FUNCTION TIMED OUT! (cpu=%d), try %d\n",
393                       smp_processor_id(), ++retries);
394                 goto retry;
395         }
396         /* We either got one or timed out. Release the lock */
397
398         mb();
399         smp_call_function_data = NULL;
400
401         while (wait && atomic_read (&data.unfinished_count) > 0)
402                         barrier ();
403
404         return 0;
405 }
406
407 EXPORT_SYMBOL(smp_call_function);
408
409 /*
410  * Flush all other CPU's tlb and then mine.  Do this with on_each_cpu()
411  * as we want to ensure all TLB's flushed before proceeding.
412  */
413
414 void
415 smp_flush_tlb_all(void)
416 {
417         on_each_cpu(flush_tlb_all_local, NULL, 1, 1);
418 }
419
420 /*
421  * Called by secondaries to update state and initialize CPU registers.
422  */
423 static void __init
424 smp_cpu_init(int cpunum)
425 {
426         extern int init_per_cpu(int);  /* arch/parisc/kernel/processor.c */
427         extern void init_IRQ(void);    /* arch/parisc/kernel/irq.c */
428         extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
429
430         /* Set modes and Enable floating point coprocessor */
431         (void) init_per_cpu(cpunum);
432
433         disable_sr_hashing();
434
435         mb();
436
437         /* Well, support 2.4 linux scheme as well. */
438         if (cpu_test_and_set(cpunum, cpu_online_map))
439         {
440                 extern void machine_halt(void); /* arch/parisc.../process.c */
441
442                 printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
443                 machine_halt();
444         }  
445
446         /* Initialise the idle task for this CPU */
447         atomic_inc(&init_mm.mm_count);
448         current->active_mm = &init_mm;
449         if(current->mm)
450                 BUG();
451         enter_lazy_tlb(&init_mm, current);
452
453         init_IRQ();   /* make sure no IRQ's are enabled or pending */
454         start_cpu_itimer();
455 }
456
457
458 /*
459  * Slaves start using C here. Indirectly called from smp_slave_stext.
460  * Do what start_kernel() and main() do for boot strap processor (aka monarch)
461  */
462 void __init smp_callin(void)
463 {
464         int slave_id = cpu_now_booting;
465 #if 0
466         void *istack;
467 #endif
468
469         smp_cpu_init(slave_id);
470         preempt_disable();
471
472 #if 0   /* NOT WORKING YET - see entry.S */
473         istack = (void *)__get_free_pages(GFP_KERNEL,ISTACK_ORDER);
474         if (istack == NULL) {
475             printk(KERN_CRIT "Failed to allocate interrupt stack for cpu %d\n",slave_id);
476             BUG();
477         }
478         mtctl(istack,31);
479 #endif
480
481         flush_cache_all_local(); /* start with known state */
482         flush_tlb_all_local(NULL);
483
484         local_irq_enable();  /* Interrupts have been off until now */
485
486         cpu_idle();      /* Wait for timer to schedule some work */
487
488         /* NOTREACHED */
489         panic("smp_callin() AAAAaaaaahhhh....\n");
490 }
491
492 /*
493  * Bring one cpu online.
494  */
495 int __init smp_boot_one_cpu(int cpuid)
496 {
497         struct task_struct *idle;
498         long timeout;
499
500         /* 
501          * Create an idle task for this CPU.  Note the address wed* give 
502          * to kernel_thread is irrelevant -- it's going to start
503          * where OS_BOOT_RENDEVZ vector in SAL says to start.  But
504          * this gets all the other task-y sort of data structures set
505          * up like we wish.   We need to pull the just created idle task 
506          * off the run queue and stuff it into the init_tasks[] array.  
507          * Sheesh . . .
508          */
509
510         idle = fork_idle(cpuid);
511         if (IS_ERR(idle))
512                 panic("SMP: fork failed for CPU:%d", cpuid);
513
514         task_thread_info(idle)->cpu = cpuid;
515
516         /* Let _start know what logical CPU we're booting
517         ** (offset into init_tasks[],cpu_data[])
518         */
519         cpu_now_booting = cpuid;
520
521         /* 
522         ** boot strap code needs to know the task address since
523         ** it also contains the process stack.
524         */
525         smp_init_current_idle_task = idle ;
526         mb();
527
528         printk("Releasing cpu %d now, hpa=%lx\n", cpuid, cpu_data[cpuid].hpa);
529
530         /*
531         ** This gets PDC to release the CPU from a very tight loop.
532         **
533         ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
534         ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which 
535         ** is executed after receiving the rendezvous signal (an interrupt to 
536         ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the 
537         ** contents of memory are valid."
538         */
539         gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, cpu_data[cpuid].hpa);
540         mb();
541
542         /* 
543          * OK, wait a bit for that CPU to finish staggering about. 
544          * Slave will set a bit when it reaches smp_cpu_init().
545          * Once the "monarch CPU" sees the bit change, it can move on.
546          */
547         for (timeout = 0; timeout < 10000; timeout++) {
548                 if(cpu_online(cpuid)) {
549                         /* Which implies Slave has started up */
550                         cpu_now_booting = 0;
551                         smp_init_current_idle_task = NULL;
552                         goto alive ;
553                 }
554                 udelay(100);
555                 barrier();
556         }
557
558         put_task_struct(idle);
559         idle = NULL;
560
561         printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
562         return -1;
563
564 alive:
565         /* Remember the Slave data */
566 #if (kDEBUG>=100)
567         printk(KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
568                 cpuid, timeout * 100);
569 #endif /* kDEBUG */
570 #ifdef ENTRY_SYS_CPUS
571         cpu_data[cpuid].state = STATE_RUNNING;
572 #endif
573         return 0;
574 }
575
576 void __devinit smp_prepare_boot_cpu(void)
577 {
578         int bootstrap_processor=cpu_data[0].cpuid;      /* CPU ID of BSP */
579
580 #ifdef ENTRY_SYS_CPUS
581         cpu_data[0].state = STATE_RUNNING;
582 #endif
583
584         /* Setup BSP mappings */
585         printk("SMP: bootstrap CPU ID is %d\n",bootstrap_processor);
586
587         cpu_set(bootstrap_processor, cpu_online_map);
588         cpu_set(bootstrap_processor, cpu_present_map);
589 }
590
591
592
593 /*
594 ** inventory.c:do_inventory() hasn't yet been run and thus we
595 ** don't 'discover' the additional CPU's until later.
596 */
597 void __init smp_prepare_cpus(unsigned int max_cpus)
598 {
599         cpus_clear(cpu_present_map);
600         cpu_set(0, cpu_present_map);
601
602         parisc_max_cpus = max_cpus;
603         if (!max_cpus)
604                 printk(KERN_INFO "SMP mode deactivated.\n");
605 }
606
607
608 void smp_cpus_done(unsigned int cpu_max)
609 {
610         return;
611 }
612
613
614 int __cpuinit __cpu_up(unsigned int cpu)
615 {
616         if (cpu != 0 && cpu < parisc_max_cpus)
617                 smp_boot_one_cpu(cpu);
618
619         return cpu_online(cpu) ? 0 : -ENOSYS;
620 }
621
622
623
624 #ifdef ENTRY_SYS_CPUS
625 /* Code goes along with:
626 **    entry.s:        ENTRY_NAME(sys_cpus)   / * 215, for cpu stat * /
627 */
628 int sys_cpus(int argc, char **argv)
629 {
630         int i,j=0;
631         extern int current_pid(int cpu);
632
633         if( argc > 2 ) {
634                 printk("sys_cpus:Only one argument supported\n");
635                 return (-1);
636         }
637         if ( argc == 1 ){
638         
639 #ifdef DUMP_MORE_STATE
640                 for_each_online_cpu(i) {
641                         int cpus_per_line = 4;
642
643                         if (j++ % cpus_per_line)
644                                 printk(" %3d",i);
645                         else
646                                 printk("\n %3d",i);
647                 }
648                 printk("\n"); 
649 #else
650                 printk("\n 0\n"); 
651 #endif
652         } else if((argc==2) && !(strcmp(argv[1],"-l"))) {
653                 printk("\nCPUSTATE  TASK CPUNUM CPUID HARDCPU(HPA)\n");
654 #ifdef DUMP_MORE_STATE
655                 for_each_online_cpu(i) {
656                         if (cpu_data[i].cpuid != NO_PROC_ID) {
657                                 switch(cpu_data[i].state) {
658                                         case STATE_RENDEZVOUS:
659                                                 printk("RENDEZVS ");
660                                                 break;
661                                         case STATE_RUNNING:
662                                                 printk((current_pid(i)!=0) ? "RUNNING  " : "IDLING   ");
663                                                 break;
664                                         case STATE_STOPPED:
665                                                 printk("STOPPED  ");
666                                                 break;
667                                         case STATE_HALTED:
668                                                 printk("HALTED   ");
669                                                 break;
670                                         default:
671                                                 printk("%08x?", cpu_data[i].state);
672                                                 break;
673                                 }
674                                 if(cpu_online(i)) {
675                                         printk(" %4d",current_pid(i));
676                                 }       
677                                 printk(" %6d",cpu_number_map(i));
678                                 printk(" %5d",i);
679                                 printk(" 0x%lx\n",cpu_data[i].hpa);
680                         }       
681                 }
682 #else
683                 printk("\n%s  %4d      0     0 --------",
684                         (current->pid)?"RUNNING ": "IDLING  ",current->pid); 
685 #endif
686         } else if ((argc==2) && !(strcmp(argv[1],"-s"))) { 
687 #ifdef DUMP_MORE_STATE
688                 printk("\nCPUSTATE   CPUID\n");
689                 for_each_online_cpu(i) {
690                         if (cpu_data[i].cpuid != NO_PROC_ID) {
691                                 switch(cpu_data[i].state) {
692                                         case STATE_RENDEZVOUS:
693                                                 printk("RENDEZVS");break;
694                                         case STATE_RUNNING:
695                                                 printk((current_pid(i)!=0) ? "RUNNING " : "IDLING");
696                                                 break;
697                                         case STATE_STOPPED:
698                                                 printk("STOPPED ");break;
699                                         case STATE_HALTED:
700                                                 printk("HALTED  ");break;
701                                         default:
702                                 }
703                                 printk("  %5d\n",i);
704                         }       
705                 }
706 #else
707                 printk("\n%s    CPU0",(current->pid==0)?"RUNNING ":"IDLING  "); 
708 #endif
709         } else {
710                 printk("sys_cpus:Unknown request\n");
711                 return (-1);
712         }
713         return 0;
714 }
715 #endif /* ENTRY_SYS_CPUS */
716
717 #ifdef CONFIG_PROC_FS
718 int __init
719 setup_profiling_timer(unsigned int multiplier)
720 {
721         return -EINVAL;
722 }
723 #endif