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