fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / kernel / smp.c
1 /*
2  * BK Id: SCCS/s.smp.c 1.50 04/16/02 12:02:07 hozer
3  */
4 /*
5  * Smp support for ppc.
6  *
7  * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
8  * deal of code from the sparc and intel versions.
9  *
10  * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
11  *
12  */
13
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/delay.h>
22 #define __KERNEL_SYSCALLS__
23 #include <linux/unistd.h>
24 #include <linux/init.h>
25 #include <linux/spinlock.h>
26 #include <linux/cache.h>
27
28 #include <asm/ptrace.h>
29 #include <asm/atomic.h>
30 #include <asm/irq.h>
31 #include <asm/page.h>
32 #include <asm/pgtable.h>
33 #include <asm/hardirq.h>
34 #include <asm/softirq.h>
35 #include <asm/io.h>
36 #include <asm/prom.h>
37 #include <asm/smp.h>
38 #include <asm/residual.h>
39 #include <asm/time.h>
40
41 int smp_threads_ready;
42 volatile int smp_commenced;
43 int smp_num_cpus = 1;
44 int smp_tb_synchronized;
45 struct cpuinfo_PPC cpu_data[NR_CPUS];
46 struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
47 atomic_t ipi_recv;
48 atomic_t ipi_sent;
49 spinlock_t kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
50 unsigned int prof_multiplier[NR_CPUS];
51 unsigned int prof_counter[NR_CPUS];
52 cycles_t cacheflush_time;
53 static int max_cpus __initdata = NR_CPUS;
54 unsigned long cpu_online_map;
55 int smp_hw_index[NR_CPUS];
56 static struct smp_ops_t *smp_ops;
57
58 /* all cpu mappings are 1-1 -- Cort */
59 volatile unsigned long cpu_callin_map[NR_CPUS];
60
61 #define TB_SYNC_PASSES 4
62 volatile unsigned long __initdata tb_sync_flag = 0;
63 volatile unsigned long __initdata tb_offset = 0;
64
65 int start_secondary(void *);
66 extern int cpu_idle(void *unused);
67 void smp_call_function_interrupt(void);
68
69 #ifdef CONFIG_PPC_ISERIES
70 extern void smp_iSeries_space_timers( unsigned nr );
71 #endif
72
73 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
74  * 
75  * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
76  * in /proc/interrupts will be wrong!!! --Troy */
77 #define PPC_MSG_CALL_FUNCTION   0
78 #define PPC_MSG_RESCHEDULE      1
79 #define PPC_MSG_INVALIDATE_TLB  2
80 #define PPC_MSG_XMON_BREAK      3
81
82 static inline void 
83 smp_message_pass(int target, int msg, unsigned long data, int wait)
84 {
85         if (smp_ops){
86                 atomic_inc(&ipi_sent);
87                 smp_ops->message_pass(target,msg,data,wait);
88         }
89 }
90
91 /* 
92  * Common functions
93  */
94 void smp_local_timer_interrupt(struct pt_regs * regs)
95 {
96         int cpu = smp_processor_id();
97
98         if (!--prof_counter[cpu]) {
99                 update_process_times(user_mode(regs));
100                 prof_counter[cpu]=prof_multiplier[cpu];
101         }
102 }
103
104 void smp_message_recv(int msg, struct pt_regs *regs)
105 {
106         atomic_inc(&ipi_recv);
107         
108         switch( msg ) {
109         case PPC_MSG_CALL_FUNCTION:
110                 smp_call_function_interrupt();
111                 break;
112         case PPC_MSG_RESCHEDULE: 
113                 current->need_resched = 1;
114                 break;
115         case PPC_MSG_INVALIDATE_TLB:
116                 _tlbia();
117                 break;
118 #ifdef CONFIG_XMON
119         case PPC_MSG_XMON_BREAK:
120                 xmon(regs);
121                 break;
122 #endif /* CONFIG_XMON */
123         default:
124                 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
125                        smp_processor_id(), msg);
126                 break;
127         }
128 }
129
130 #ifdef CONFIG_750_SMP
131 /*
132  * 750's don't broadcast tlb invalidates so
133  * we have to emulate that behavior.
134  *   -- Cort
135  */
136 void smp_ppc750_send_tlb_invalidate(int cpu)
137 {
138         if ( PVR_VER(mfspr(PVR)) == 8 )
139                 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0);
140 }
141 #endif
142
143 void smp_send_reschedule(int cpu)
144 {
145         /*
146          * This is only used if `cpu' is running an idle task,
147          * so it will reschedule itself anyway...
148          *
149          * This isn't the case anymore since the other CPU could be
150          * sleeping and won't reschedule until the next interrupt (such
151          * as the timer).
152          *  -- Cort
153          */
154         /* This is only used if `cpu' is running an idle task,
155            so it will reschedule itself anyway... */
156         smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0);
157 }
158
159 #ifdef CONFIG_XMON
160 void smp_send_xmon_break(int cpu)
161 {
162         smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
163 }
164 #endif /* CONFIG_XMON */
165
166 static void stop_this_cpu(void *dummy)
167 {
168         __cli();
169         while (1)
170                 ;
171 }
172
173 void smp_send_stop(void)
174 {
175         smp_call_function(stop_this_cpu, NULL, 1, 0);
176         smp_num_cpus = 1;
177 }
178
179 /*
180  * Structure and data for smp_call_function(). This is designed to minimise
181  * static memory requirements. It also looks cleaner.
182  * Stolen from the i386 version.
183  */
184 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
185
186 static struct call_data_struct {
187         void (*func) (void *info);
188         void *info;
189         atomic_t started;
190         atomic_t finished;
191         int wait;
192 } *call_data;
193
194 /*
195  * this function sends a 'generic call function' IPI to all other CPUs
196  * in the system.
197  */
198
199 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
200                         int wait)
201 /*
202  * [SUMMARY] Run a function on all other CPUs.
203  * <func> The function to run. This must be fast and non-blocking.
204  * <info> An arbitrary pointer to pass to the function.
205  * <nonatomic> currently unused.
206  * <wait> If true, wait (atomically) until function has completed on other CPUs.
207  * [RETURNS] 0 on success, else a negative status code. Does not return until
208  * remote CPUs are nearly ready to execute <<func>> or are or have executed.
209  *
210  * You must not call this function with disabled interrupts or from a
211  * hardware interrupt handler, you may call it from a bottom half handler.
212  */
213 {
214         struct call_data_struct data;
215         int ret = -1, cpus = smp_num_cpus-1;
216         int timeout;
217
218         if (!cpus)
219                 return 0;
220
221         data.func = func;
222         data.info = info;
223         atomic_set(&data.started, 0);
224         data.wait = wait;
225         if (wait)
226                 atomic_set(&data.finished, 0);
227
228         spin_lock_bh(&call_lock);
229         call_data = &data;
230         /* Send a message to all other CPUs and wait for them to respond */
231         smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION, 0, 0);
232
233         /* Wait for response */
234         timeout = 1000000;
235         while (atomic_read(&data.started) != cpus) {
236                 if (--timeout == 0) {
237                         printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
238                                smp_processor_id(), atomic_read(&data.started));
239                         goto out;
240                 }
241                 barrier();
242                 udelay(1);
243         }
244
245         if (wait) {
246                 timeout = 1000000;
247                 while (atomic_read(&data.finished) != cpus) {
248                         if (--timeout == 0) {
249                                 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
250                                        smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
251                                 goto out;
252                         }
253                         barrier();
254                         udelay(1);
255                 }
256         }
257         ret = 0;
258
259  out:
260         spin_unlock_bh(&call_lock);
261         return ret;
262 }
263
264 void smp_call_function_interrupt(void)
265 {
266         void (*func) (void *info) = call_data->func;
267         void *info = call_data->info;
268         int wait = call_data->wait;
269
270         /*
271          * Notify initiating CPU that I've grabbed the data and am
272          * about to execute the function
273          */
274         atomic_inc(&call_data->started);
275         /*
276          * At this point the info structure may be out of scope unless wait==1
277          */
278         (*func)(info);
279         if (wait)
280                 atomic_inc(&call_data->finished);
281 }
282
283 void __init smp_boot_cpus(void)
284 {
285         extern struct task_struct *current_set[NR_CPUS];
286         int i, cpu_nr;
287         struct task_struct *p;
288
289         printk("Entering SMP Mode...\n");
290         smp_num_cpus = 1;
291         smp_store_cpu_info(0);
292         cpu_online_map = 1UL;
293
294         /*
295          * assume for now that the first cpu booted is
296          * cpu 0, the master -- Cort
297          */
298         cpu_callin_map[0] = 1;
299         current->processor = 0;
300
301         init_idle();
302
303         for (i = 0; i < NR_CPUS; i++) {
304                 prof_counter[i] = 1;
305                 prof_multiplier[i] = 1;
306         }
307
308         /*
309          * XXX very rough, assumes 20 bus cycles to read a cache line,
310          * timebase increments every 4 bus cycles, 32kB L1 data cache.
311          */
312         cacheflush_time = 5 * 1024;
313
314         smp_ops = ppc_md.smp_ops;
315         if (smp_ops == NULL) {
316                 printk("SMP not supported on this machine.\n");
317                 return;
318         }
319
320 #ifndef CONFIG_750_SMP
321         /* check for 750's, they just don't work with linux SMP.
322          * If you actually have 750 SMP hardware and want to try to get
323          * it to work, send me a patch to make it work and
324          * I'll make CONFIG_750_SMP a config option.  -- Troy (hozer@drgw.net)
325          */
326         if ( PVR_VER(mfspr(PVR)) == 8 ){
327                 printk("SMP not supported on 750 cpus. %s line %d\n",
328                                 __FILE__, __LINE__);
329                 return;
330         }
331 #endif
332
333
334         /* Probe arch for CPUs */
335         cpu_nr = smp_ops->probe();
336
337         /*
338          * only check for cpus we know exist.  We keep the callin map
339          * with cpus at the bottom -- Cort
340          */
341         if (cpu_nr > max_cpus)
342                 cpu_nr = max_cpus;
343 #ifdef CONFIG_PPC_ISERIES
344         smp_iSeries_space_timers( cpu_nr );
345 #endif
346         for (i = 1; i < cpu_nr; i++) {
347                 int c;
348                 struct pt_regs regs;
349                 
350                 /* create a process for the processor */
351                 /* only regs.msr is actually used, and 0 is OK for it */
352                 memset(&regs, 0, sizeof(struct pt_regs));
353                 if (do_fork(CLONE_VM|CLONE_PID, 0, &regs, 0) < 0)
354                         panic("failed fork for CPU %d", i);
355                 p = init_task.prev_task;
356                 if (!p)
357                         panic("No idle task for CPU %d", i);
358                 del_from_runqueue(p);
359                 unhash_process(p);
360                 init_tasks[i] = p;
361
362                 p->processor = i;
363                 p->cpus_runnable = 1 << i; /* we schedule the first task manually */
364                 current_set[i] = p;
365
366                 /*
367                  * There was a cache flush loop here to flush the cache
368                  * to memory for the first 8MB of RAM.  The cache flush
369                  * has been pushed into the kick_cpu function for those
370                  * platforms that need it.
371                  */
372
373                 /* wake up cpus */
374                 smp_ops->kick_cpu(i);
375                 
376                 /*
377                  * wait to see if the cpu made a callin (is actually up).
378                  * use this value that I found through experimentation.
379                  * -- Cort
380                  */
381                 for ( c = 1000; c && !cpu_callin_map[i] ; c-- )
382                         udelay(100);
383                 
384                 if ( cpu_callin_map[i] )
385                 {
386                         char buf[32];
387                         sprintf(buf, "found cpu %d", i);
388                         if (ppc_md.progress) ppc_md.progress(buf, 0x350+i);
389                         printk("Processor %d found.\n", i);
390                         smp_num_cpus++;
391                 } else {
392                         char buf[32];
393                         sprintf(buf, "didn't find cpu %d", i);
394                         if (ppc_md.progress) ppc_md.progress(buf, 0x360+i);
395                         printk("Processor %d is stuck.\n", i);
396                 }
397         }
398
399         /* Setup CPU 0 last (important) */
400         smp_ops->setup_cpu(0);
401         
402         if (smp_num_cpus < 2)
403                 smp_tb_synchronized = 1;
404 }
405
406 void __init smp_software_tb_sync(int cpu)
407 {
408 #define PASSES 4        /* 4 passes.. */
409         int pass;
410         int i, j;
411
412         /* stop - start will be the number of timebase ticks it takes for cpu0
413          * to send a message to all others and the first reponse to show up.
414          *
415          * ASSUMPTION: this time is similiar for all cpus
416          * ASSUMPTION: the time to send a one-way message is ping/2
417          */
418         register unsigned long start = 0;
419         register unsigned long stop = 0;
420         register unsigned long temp = 0;
421
422         set_tb(0, 0);
423
424         /* multiple passes to get in l1 cache.. */
425         for (pass = 2; pass < 2+PASSES; pass++){
426                 if (cpu == 0){
427                         mb();
428                         for (i = j = 1; i < smp_num_cpus; i++, j++){
429                                 /* skip stuck cpus */
430                                 while (!cpu_callin_map[j])
431                                         ++j;
432                                 while (cpu_callin_map[j] != pass)
433                                         barrier();
434                         }
435                         mb();
436                         tb_sync_flag = pass;
437                         start = get_tbl();      /* start timing */
438                         while (tb_sync_flag)
439                                 mb();
440                         stop = get_tbl();       /* end timing */
441                         /* theoretically, the divisor should be 2, but
442                          * I get better results on my dual mtx. someone
443                          * please report results on other smp machines..
444                          */
445                         tb_offset = (stop-start)/4;
446                         mb();
447                         tb_sync_flag = pass;
448                         udelay(10);
449                         mb();
450                         tb_sync_flag = 0;
451                         mb();
452                         set_tb(0,0);
453                         mb();
454                 } else {
455                         cpu_callin_map[cpu] = pass;
456                         mb();
457                         while (!tb_sync_flag)
458                                 mb();           /* wait for cpu0 */
459                         mb();
460                         tb_sync_flag = 0;       /* send response for timing */
461                         mb();
462                         while (!tb_sync_flag)
463                                 mb();
464                         temp = tb_offset;       /* make sure offset is loaded */
465                         while (tb_sync_flag)
466                                 mb();
467                         set_tb(0,temp);         /* now, set the timebase */
468                         mb();
469                 }
470         }
471         if (cpu == 0) {
472                 smp_tb_synchronized = 1;
473                 printk("smp_software_tb_sync: %d passes, final offset: %ld\n",
474                         PASSES, tb_offset);
475         }
476         /* so time.c doesn't get confused */
477         set_dec(tb_ticks_per_jiffy);
478         last_jiffy_stamp(cpu) = 0;
479 }
480
481 void __init smp_commence(void)
482 {
483         /*
484          *      Lets the callin's below out of their loop.
485          */
486         if (ppc_md.progress) ppc_md.progress("smp_commence", 0x370);
487         wmb();
488         smp_commenced = 1;
489
490         /* if the smp_ops->setup_cpu function has not already synched the
491          * timebases with a nicer hardware-based method, do so now
492          *
493          * I am open to suggestions for improvements to this method
494          * -- Troy <hozer@drgw.net>
495          *
496          * NOTE: if you are debugging, set smp_tb_synchronized for now
497          * since if this code runs pretty early and needs all cpus that
498          * reported in in smp_callin_map to be working
499          *
500          * NOTE2: this code doesn't seem to work on > 2 cpus. -- paulus/BenH
501          */
502         if (!smp_tb_synchronized && smp_num_cpus == 2) {
503                 unsigned long flags;
504                 __save_and_cli(flags);  
505                 smp_software_tb_sync(0);
506                 __restore_flags(flags);
507         }
508 }
509
510 void __init smp_callin(void)
511 {
512         int cpu = current->processor;
513         
514         smp_store_cpu_info(cpu);
515         set_dec(tb_ticks_per_jiffy);
516         cpu_callin_map[cpu] = 1;
517
518         smp_ops->setup_cpu(cpu);
519
520         /*
521          * This cpu is now "online".  Only set them online
522          * before they enter the loop below since write access
523          * to the below variable is _not_ guaranteed to be
524          * atomic.
525          *   -- Cort <cort@fsmlabs.com>
526          */
527         cpu_online_map |= 1UL << smp_processor_id();
528         
529         while(!smp_commenced)
530                 barrier();
531
532         /* see smp_commence for more info */
533         if (!smp_tb_synchronized && smp_num_cpus == 2) {
534                 smp_software_tb_sync(cpu);
535         }
536         __sti();
537 }
538
539 /* intel needs this */
540 void __init initialize_secondary(void)
541 {
542 }
543
544 /* Activate a secondary processor. */
545 int __init start_secondary(void *unused)
546 {
547         atomic_inc(&init_mm.mm_count);
548         current->active_mm = &init_mm;
549         smp_callin();
550         return cpu_idle(NULL);
551 }
552
553 void __init smp_setup(char *str, int *ints)
554 {
555 }
556
557 int __init setup_profiling_timer(unsigned int multiplier)
558 {
559         return 0;
560 }
561
562 void __init smp_store_cpu_info(int id)
563 {
564         struct cpuinfo_PPC *c = &cpu_data[id];
565
566         /* assume bogomips are same for everything */
567         c->loops_per_jiffy = loops_per_jiffy;
568         c->pvr = mfspr(PVR);
569 }
570
571 static int __init maxcpus(char *str)
572 {
573         get_option(&str, &max_cpus);
574         return 1;
575 }
576
577 __setup("maxcpus=", maxcpus);