Merge HEAD from master.kernel.org:/home/rmk/linux-2.6-ucb.git
[pandora-kernel.git] / arch / m32r / kernel / smpboot.c
1 /*
2  *  linux/arch/m32r/kernel/smpboot.c
3  *    orig : i386 2.4.10
4  *
5  *  M32R SMP booting functions
6  *
7  *  Copyright (c) 2001, 2002, 2003  Hitoshi Yamamoto
8  *
9  *  Taken from i386 version.
10  *        (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
11  *        (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
12  *
13  *      Much of the core SMP work is based on previous work by Thomas Radke, to
14  *      whom a great many thanks are extended.
15  *
16  *      Thanks to Intel for making available several different Pentium,
17  *      Pentium Pro and Pentium-II/Xeon MP machines.
18  *      Original development of Linux SMP code supported by Caldera.
19  *
20  *      This code is released under the GNU General Public License version 2 or
21  *      later.
22  *
23  *      Fixes
24  *              Felix Koop      :       NR_CPUS used properly
25  *              Jose Renau      :       Handle single CPU case.
26  *              Alan Cox        :       By repeated request
27  *                                      8) - Total BogoMIP report.
28  *              Greg Wright     :       Fix for kernel stacks panic.
29  *              Erich Boleyn    :       MP v1.4 and additional changes.
30  *      Matthias Sattler        :       Changes for 2.1 kernel map.
31  *      Michel Lespinasse       :       Changes for 2.1 kernel map.
32  *      Michael Chastain        :       Change trampoline.S to gnu as.
33  *              Alan Cox        :       Dumb bug: 'B' step PPro's are fine
34  *              Ingo Molnar     :       Added APIC timers, based on code
35  *                                      from Jose Renau
36  *              Ingo Molnar     :       various cleanups and rewrites
37  *              Tigran Aivazian :       fixed "0.00 in /proc/uptime on SMP" bug.
38  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs
39  *              Martin J. Bligh :       Added support for multi-quad systems
40  */
41
42 #include <linux/config.h>
43 #include <linux/init.h>
44 #include <linux/mm.h>
45 #include <linux/smp_lock.h>
46 #include <linux/irq.h>
47 #include <linux/bootmem.h>
48 #include <linux/delay.h>
49
50 #include <asm/io.h>
51 #include <asm/pgalloc.h>
52 #include <asm/tlbflush.h>
53
54 #define DEBUG_SMP
55 #ifdef DEBUG_SMP
56 #define Dprintk(x...) printk(x)
57 #else
58 #define Dprintk(x...)
59 #endif
60
61 extern cpumask_t cpu_initialized;
62
63 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
64 /* Data structures and variables                                             */
65 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
66
67 /* Processor that is doing the boot up */
68 static unsigned int bsp_phys_id = -1;
69
70 /* Bitmask of physically existing CPUs */
71 physid_mask_t phys_cpu_present_map;
72
73 /* Bitmask of currently online CPUs */
74 cpumask_t cpu_online_map;
75
76 cpumask_t cpu_bootout_map;
77 cpumask_t cpu_bootin_map;
78 cpumask_t cpu_callout_map;
79 static cpumask_t cpu_callin_map;
80
81 /* Per CPU bogomips and other parameters */
82 struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
83
84 static int cpucount;
85 static cpumask_t smp_commenced_mask;
86
87 extern struct {
88         void * spi;
89         unsigned short ss;
90 } stack_start;
91
92 /* which physical physical ID maps to which logical CPU number */
93 static volatile int physid_2_cpu[NR_CPUS];
94 #define physid_to_cpu(physid)   physid_2_cpu[physid]
95
96 /* which logical CPU number maps to which physical ID */
97 volatile int cpu_2_physid[NR_CPUS];
98
99 DEFINE_PER_CPU(int, prof_multiplier) = 1;
100 DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
101 DEFINE_PER_CPU(int, prof_counter) = 1;
102
103 spinlock_t ipi_lock[NR_IPIS];
104
105 static unsigned int calibration_result;
106
107 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
108 /* Function Prototypes                                                       */
109 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
110
111 void smp_prepare_boot_cpu(void);
112 void smp_prepare_cpus(unsigned int);
113 static void smp_tune_scheduling(void);
114 static void init_ipi_lock(void);
115 static void do_boot_cpu(int);
116 int __cpu_up(unsigned int);
117 void smp_cpus_done(unsigned int);
118
119 int start_secondary(void *);
120 static void smp_callin(void);
121 static void smp_online(void);
122
123 static void show_mp_info(int);
124 static void smp_store_cpu_info(int);
125 static void show_cpu_info(int);
126 int setup_profiling_timer(unsigned int);
127 static void init_cpu_to_physid(void);
128 static void map_cpu_to_physid(int, int);
129 static void unmap_cpu_to_physid(int, int);
130
131 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
132 /* Boot up APs Routins : BSP                                                 */
133 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
134 void __devinit smp_prepare_boot_cpu(void)
135 {
136         bsp_phys_id = hard_smp_processor_id();
137         physid_set(bsp_phys_id, phys_cpu_present_map);
138         cpu_set(0, cpu_online_map);     /* BSP's cpu_id == 0 */
139         cpu_set(0, cpu_callout_map);
140         cpu_set(0, cpu_callin_map);
141
142         /*
143          * Initialize the logical to physical CPU number mapping
144          */
145         init_cpu_to_physid();
146         map_cpu_to_physid(0, bsp_phys_id);
147         current_thread_info()->cpu = 0;
148 }
149
150 /*==========================================================================*
151  * Name:         smp_prepare_cpus (old smp_boot_cpus)
152  *
153  * Description:  This routine boot up APs.
154  *
155  * Born on Date: 2002.02.05
156  *
157  * Arguments:    NONE
158  *
159  * Returns:      void (cannot fail)
160  *
161  * Modification log:
162  * Date       Who Description
163  * ---------- --- --------------------------------------------------------
164  * 2003-06-24 hy  modify for linux-2.5.69
165  *
166  *==========================================================================*/
167 void __init smp_prepare_cpus(unsigned int max_cpus)
168 {
169         int phys_id;
170         unsigned long nr_cpu;
171
172         nr_cpu = inl(M32R_FPGA_NUM_OF_CPUS_PORTL);
173         if (nr_cpu > NR_CPUS) {
174                 printk(KERN_INFO "NUM_OF_CPUS reg. value [%ld] > NR_CPU [%d]",
175                         nr_cpu, NR_CPUS);
176                 goto smp_done;
177         }
178         for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
179                 physid_set(phys_id, phys_cpu_present_map);
180
181         show_mp_info(nr_cpu);
182
183         init_ipi_lock();
184
185         /*
186          * Setup boot CPU information
187          */
188         smp_store_cpu_info(0); /* Final full version of the data */
189         smp_tune_scheduling();
190
191         /*
192          * If SMP should be disabled, then really disable it!
193          */
194         if (!max_cpus) {
195                 printk(KERN_INFO "SMP mode deactivated by commandline.\n");
196                 goto smp_done;
197         }
198
199         /*
200          * Now scan the CPU present map and fire up the other CPUs.
201          */
202         Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
203
204         for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
205                 /*
206                  * Don't even attempt to start the boot CPU!
207                  */
208                 if (phys_id == bsp_phys_id)
209                         continue;
210
211                 if (!physid_isset(phys_id, phys_cpu_present_map))
212                         continue;
213
214                 if ((max_cpus >= 0) && (max_cpus <= cpucount + 1))
215                         continue;
216
217                 do_boot_cpu(phys_id);
218
219                 /*
220                  * Make sure we unmap all failed CPUs
221                  */
222                 if (physid_to_cpu(phys_id) == -1) {
223                         physid_clear(phys_id, phys_cpu_present_map);
224                         printk("phys CPU#%d not responding - " \
225                                 "cannot use it.\n", phys_id);
226                 }
227         }
228
229 smp_done:
230         Dprintk("Boot done.\n");
231 }
232
233 static void __init smp_tune_scheduling(void)
234 {
235         /* Nothing to do. */
236 }
237
238 /*
239  * init_ipi_lock : Initialize IPI locks.
240  */
241 static void __init init_ipi_lock(void)
242 {
243         int ipi;
244
245         for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
246                 spin_lock_init(&ipi_lock[ipi]);
247 }
248
249 /*==========================================================================*
250  * Name:         do_boot_cpu
251  *
252  * Description:  This routine boot up one AP.
253  *
254  * Born on Date: 2002.02.05
255  *
256  * Arguments:    phys_id - Target CPU physical ID
257  *
258  * Returns:      void (cannot fail)
259  *
260  * Modification log:
261  * Date       Who Description
262  * ---------- --- --------------------------------------------------------
263  * 2003-06-24 hy  modify for linux-2.5.69
264  *
265  *==========================================================================*/
266 static void __init do_boot_cpu(int phys_id)
267 {
268         struct task_struct *idle;
269         unsigned long send_status, boot_status;
270         int timeout, cpu_id;
271
272         cpu_id = ++cpucount;
273
274         /*
275          * We can't use kernel_thread since we must avoid to
276          * reschedule the child.
277          */
278         idle = fork_idle(cpu_id);
279         if (IS_ERR(idle))
280                 panic("failed fork for CPU#%d.", cpu_id);
281
282         idle->thread.lr = (unsigned long)start_secondary;
283
284         map_cpu_to_physid(cpu_id, phys_id);
285
286         /* So we see what's up   */
287         printk("Booting processor %d/%d\n", phys_id, cpu_id);
288         stack_start.spi = (void *)idle->thread.sp;
289         idle->thread_info->cpu = cpu_id;
290
291         /*
292          * Send Startup IPI
293          *   1.IPI received by CPU#(phys_id).
294          *   2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
295          *   3.CPU#(phys_id) enter start_secondary()
296          */
297         send_status = 0;
298         boot_status = 0;
299
300         cpu_set(phys_id, cpu_bootout_map);
301
302         /* Send Startup IPI */
303         send_IPI_mask_phys(cpumask_of_cpu(phys_id), CPU_BOOT_IPI, 0);
304
305         Dprintk("Waiting for send to finish...\n");
306         timeout = 0;
307
308         /* Wait 100[ms] */
309         do {
310                 Dprintk("+");
311                 udelay(1000);
312                 send_status = !cpu_isset(phys_id, cpu_bootin_map);
313         } while (send_status && (timeout++ < 100));
314
315         Dprintk("After Startup.\n");
316
317         if (!send_status) {
318                 /*
319                  * allow APs to start initializing.
320                  */
321                 Dprintk("Before Callout %d.\n", cpu_id);
322                 cpu_set(cpu_id, cpu_callout_map);
323                 Dprintk("After Callout %d.\n", cpu_id);
324
325                 /*
326                  * Wait 5s total for a response
327                  */
328                 for (timeout = 0; timeout < 5000; timeout++) {
329                         if (cpu_isset(cpu_id, cpu_callin_map))
330                                 break;  /* It has booted */
331                         udelay(1000);
332                 }
333
334                 if (cpu_isset(cpu_id, cpu_callin_map)) {
335                         /* number CPUs logically, starting from 1 (BSP is 0) */
336                         Dprintk("OK.\n");
337                 } else {
338                         boot_status = 1;
339                         printk("Not responding.\n");
340                 }
341         } else
342                 printk("IPI never delivered???\n");
343
344         if (send_status || boot_status) {
345                 unmap_cpu_to_physid(cpu_id, phys_id);
346                 cpu_clear(cpu_id, cpu_callout_map);
347                 cpu_clear(cpu_id, cpu_callin_map);
348                 cpu_clear(cpu_id, cpu_initialized);
349                 cpucount--;
350         }
351 }
352
353 int __devinit __cpu_up(unsigned int cpu_id)
354 {
355         int timeout;
356
357         cpu_set(cpu_id, smp_commenced_mask);
358
359         /*
360          * Wait 5s total for a response
361          */
362         for (timeout = 0; timeout < 5000; timeout++) {
363                 if (cpu_isset(cpu_id, cpu_online_map))
364                         break;
365                 udelay(1000);
366         }
367         if (!cpu_isset(cpu_id, cpu_online_map))
368                 BUG();
369
370         return 0;
371 }
372
373 void __init smp_cpus_done(unsigned int max_cpus)
374 {
375         int cpu_id, timeout;
376         unsigned long bogosum = 0;
377
378         for (timeout = 0; timeout < 5000; timeout++) {
379                 if (cpus_equal(cpu_callin_map, cpu_online_map))
380                         break;
381                 udelay(1000);
382         }
383         if (!cpus_equal(cpu_callin_map, cpu_online_map))
384                 BUG();
385
386         for (cpu_id = 0 ; cpu_id < num_online_cpus() ; cpu_id++)
387                 show_cpu_info(cpu_id);
388
389         /*
390          * Allow the user to impress friends.
391          */
392         Dprintk("Before bogomips.\n");
393         if (cpucount) {
394                 for_each_cpu_mask(cpu_id, cpu_online_map)
395                         bogosum += cpu_data[cpu_id].loops_per_jiffy;
396
397                 printk(KERN_INFO "Total of %d processors activated " \
398                         "(%lu.%02lu BogoMIPS).\n", cpucount + 1,
399                         bogosum / (500000 / HZ),
400                         (bogosum / (5000 / HZ)) % 100);
401                 Dprintk("Before bogocount - setting activated=1.\n");
402         }
403 }
404
405 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
406 /* Activate a secondary processor Routins                                    */
407 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
408
409 /*==========================================================================*
410  * Name:         start_secondary
411  *
412  * Description:  This routine activate a secondary processor.
413  *
414  * Born on Date: 2002.02.05
415  *
416  * Arguments:    *unused - currently unused.
417  *
418  * Returns:      void (cannot fail)
419  *
420  * Modification log:
421  * Date       Who Description
422  * ---------- --- --------------------------------------------------------
423  * 2003-06-24 hy  modify for linux-2.5.69
424  *
425  *==========================================================================*/
426 int __init start_secondary(void *unused)
427 {
428         cpu_init();
429         smp_callin();
430         while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
431                 cpu_relax();
432
433         smp_online();
434
435         /*
436          * low-memory mappings have been cleared, flush them from
437          * the local TLBs too.
438          */
439         local_flush_tlb_all();
440
441         cpu_idle();
442         return 0;
443 }
444
445 /*==========================================================================*
446  * Name:         smp_callin
447  *
448  * Description:  This routine activate a secondary processor.
449  *
450  * Born on Date: 2002.02.05
451  *
452  * Arguments:    NONE
453  *
454  * Returns:      void (cannot fail)
455  *
456  * Modification log:
457  * Date       Who Description
458  * ---------- --- --------------------------------------------------------
459  * 2003-06-24 hy  modify for linux-2.5.69
460  *
461  *==========================================================================*/
462 static void __init smp_callin(void)
463 {
464         int phys_id = hard_smp_processor_id();
465         int cpu_id = smp_processor_id();
466         unsigned long timeout;
467
468         if (cpu_isset(cpu_id, cpu_callin_map)) {
469                 printk("huh, phys CPU#%d, CPU#%d already present??\n",
470                         phys_id, cpu_id);
471                 BUG();
472         }
473         Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id, phys_id);
474
475         /* Waiting 2s total for startup (udelay is not yet working) */
476         timeout = jiffies + (2 * HZ);
477         while (time_before(jiffies, timeout)) {
478                 /* Has the boot CPU finished it's STARTUP sequence ? */
479                 if (cpu_isset(cpu_id, cpu_callout_map))
480                         break;
481                 cpu_relax();
482         }
483
484         if (!time_before(jiffies, timeout)) {
485                 printk("BUG: CPU#%d started up but did not get a callout!\n",
486                         cpu_id);
487                 BUG();
488         }
489
490         /* Allow the master to continue. */
491         cpu_set(cpu_id, cpu_callin_map);
492 }
493
494 static void __init smp_online(void)
495 {
496         int cpu_id = smp_processor_id();
497
498         local_irq_enable();
499
500         /* Get our bogomips. */
501         calibrate_delay();
502
503         /* Save our processor parameters */
504         smp_store_cpu_info(cpu_id);
505
506         cpu_set(cpu_id, cpu_online_map);
507 }
508
509 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
510 /* Boot up CPUs common Routins                                               */
511 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
512 static void __init show_mp_info(int nr_cpu)
513 {
514         int i;
515         char cpu_model0[17], cpu_model1[17], cpu_ver[9];
516
517         strncpy(cpu_model0, (char *)M32R_FPGA_CPU_NAME_ADDR, 16);
518         strncpy(cpu_model1, (char *)M32R_FPGA_MODEL_ID_ADDR, 16);
519         strncpy(cpu_ver, (char *)M32R_FPGA_VERSION_ADDR, 8);
520
521         cpu_model0[16] = '\0';
522         for (i = 15 ; i >= 0 ; i--) {
523                 if (cpu_model0[i] != ' ')
524                         break;
525                 cpu_model0[i] = '\0';
526         }
527         cpu_model1[16] = '\0';
528         for (i = 15 ; i >= 0 ; i--) {
529                 if (cpu_model1[i] != ' ')
530                         break;
531                 cpu_model1[i] = '\0';
532         }
533         cpu_ver[8] = '\0';
534         for (i = 7 ; i >= 0 ; i--) {
535                 if (cpu_ver[i] != ' ')
536                         break;
537                 cpu_ver[i] = '\0';
538         }
539
540         printk(KERN_INFO "M32R-mp information\n");
541         printk(KERN_INFO "  On-chip CPUs : %d\n", nr_cpu);
542         printk(KERN_INFO "  CPU model : %s/%s(%s)\n", cpu_model0,
543                 cpu_model1, cpu_ver);
544 }
545
546 /*
547  * The bootstrap kernel entry code has set these up. Save them for
548  * a given CPU
549  */
550 static void __init smp_store_cpu_info(int cpu_id)
551 {
552         struct cpuinfo_m32r *ci = cpu_data + cpu_id;
553
554         *ci = boot_cpu_data;
555         ci->loops_per_jiffy = loops_per_jiffy;
556 }
557
558 static void __init show_cpu_info(int cpu_id)
559 {
560         struct cpuinfo_m32r *ci = &cpu_data[cpu_id];
561
562         printk("CPU#%d : ", cpu_id);
563
564 #define PRINT_CLOCK(name, value) \
565         printk(name " clock %d.%02dMHz", \
566                 ((value) / 1000000), ((value) % 1000000) / 10000)
567
568         PRINT_CLOCK("CPU", (int)ci->cpu_clock);
569         PRINT_CLOCK(", Bus", (int)ci->bus_clock);
570         printk(", loops_per_jiffy[%ld]\n", ci->loops_per_jiffy);
571 }
572
573 /*
574  * the frequency of the profiling timer can be changed
575  * by writing a multiplier value into /proc/profile.
576  */
577 int setup_profiling_timer(unsigned int multiplier)
578 {
579         int i;
580
581         /*
582          * Sanity check. [at least 500 APIC cycles should be
583          * between APIC interrupts as a rule of thumb, to avoid
584          * irqs flooding us]
585          */
586         if ( (!multiplier) || (calibration_result / multiplier < 500))
587                 return -EINVAL;
588
589         /*
590          * Set the new multiplier for each CPU. CPUs don't start using the
591          * new values until the next timer interrupt in which they do process
592          * accounting. At that time they also adjust their APIC timers
593          * accordingly.
594          */
595         for (i = 0; i < NR_CPUS; ++i)
596                 per_cpu(prof_multiplier, i) = multiplier;
597
598         return 0;
599 }
600
601 /* Initialize all maps between cpu number and apicids */
602 static void __init init_cpu_to_physid(void)
603 {
604         int  i;
605
606         for (i = 0 ; i < NR_CPUS ; i++) {
607                 cpu_2_physid[i] = -1;
608                 physid_2_cpu[i] = -1;
609         }
610 }
611
612 /*
613  * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
614  * else physical apic ids
615  */
616 static void __init map_cpu_to_physid(int cpu_id, int phys_id)
617 {
618         physid_2_cpu[phys_id] = cpu_id;
619         cpu_2_physid[cpu_id] = phys_id;
620 }
621
622 /*
623  * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
624  * else physical apic ids
625  */
626 static void __init unmap_cpu_to_physid(int cpu_id, int phys_id)
627 {
628         physid_2_cpu[phys_id] = -1;
629         cpu_2_physid[cpu_id] = -1;
630 }
631