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