Linux-2.6.12-rc2
[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
95 /* which logical CPU number maps to which physical ID */
96 volatile int cpu_2_physid[NR_CPUS];
97
98 DEFINE_PER_CPU(int, prof_multiplier) = 1;
99 DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
100 DEFINE_PER_CPU(int, prof_counter) = 1;
101
102 spinlock_t ipi_lock[NR_IPIS];
103
104 static unsigned int calibration_result;
105
106 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
107 /* Function Prototypes                                                       */
108 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
109
110 void smp_prepare_boot_cpu(void);
111 void smp_prepare_cpus(unsigned int);
112 static void smp_tune_scheduling(void);
113 static void init_ipi_lock(void);
114 static void do_boot_cpu(int);
115 int __cpu_up(unsigned int);
116 void smp_cpus_done(unsigned int);
117
118 int start_secondary(void *);
119 static void smp_callin(void);
120 static void smp_online(void);
121
122 static void show_mp_info(int);
123 static void smp_store_cpu_info(int);
124 static void show_cpu_info(int);
125 int setup_profiling_timer(unsigned int);
126 static void init_cpu_to_physid(void);
127 static void map_cpu_to_physid(int, int);
128 static void unmap_cpu_to_physid(int, int);
129
130 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
131 /* Boot up APs Routins : BSP                                                 */
132 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
133 void __devinit smp_prepare_boot_cpu(void)
134 {
135         bsp_phys_id = hard_smp_processor_id();
136         physid_set(bsp_phys_id, phys_cpu_present_map);
137         cpu_set(0, cpu_online_map);     /* BSP's cpu_id == 0 */
138         cpu_set(0, cpu_callout_map);
139         cpu_set(0, cpu_callin_map);
140
141         /*
142          * Initialize the logical to physical CPU number mapping
143          */
144         init_cpu_to_physid();
145         map_cpu_to_physid(0, bsp_phys_id);
146         current_thread_info()->cpu = 0;
147 }
148
149 /*==========================================================================*
150  * Name:         smp_prepare_cpus (old smp_boot_cpus)
151  *
152  * Description:  This routine boot up APs.
153  *
154  * Born on Date: 2002.02.05
155  *
156  * Arguments:    NONE
157  *
158  * Returns:      void (cannot fail)
159  *
160  * Modification log:
161  * Date       Who Description
162  * ---------- --- --------------------------------------------------------
163  * 2003-06-24 hy  modify for linux-2.5.69
164  *
165  *==========================================================================*/
166 void __init smp_prepare_cpus(unsigned int max_cpus)
167 {
168         int phys_id;
169         unsigned long nr_cpu;
170
171         nr_cpu = inl(M32R_FPGA_NUM_OF_CPUS_PORTL);
172         if (nr_cpu > NR_CPUS) {
173                 printk(KERN_INFO "NUM_OF_CPUS reg. value [%ld] > NR_CPU [%d]",
174                         nr_cpu, NR_CPUS);
175                 goto smp_done;
176         }
177         for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
178                 physid_set(phys_id, phys_cpu_present_map);
179
180         show_mp_info(nr_cpu);
181
182         init_ipi_lock();
183
184         /*
185          * Setup boot CPU information
186          */
187         smp_store_cpu_info(0); /* Final full version of the data */
188         smp_tune_scheduling();
189
190         /*
191          * If SMP should be disabled, then really disable it!
192          */
193         if (!max_cpus) {
194                 printk(KERN_INFO "SMP mode deactivated by commandline.\n");
195                 goto smp_done;
196         }
197
198         /*
199          * Now scan the CPU present map and fire up the other CPUs.
200          */
201         Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
202
203         for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
204                 /*
205                  * Don't even attempt to start the boot CPU!
206                  */
207                 if (phys_id == bsp_phys_id)
208                         continue;
209
210                 if (!physid_isset(phys_id, phys_cpu_present_map))
211                         continue;
212
213                 if ((max_cpus >= 0) && (max_cpus <= cpucount + 1))
214                         continue;
215
216                 do_boot_cpu(phys_id);
217
218                 /*
219                  * Make sure we unmap all failed CPUs
220                  */
221                 if (physid_to_cpu(phys_id) == -1) {
222                         physid_clear(phys_id, phys_cpu_present_map);
223                         printk("phys CPU#%d not responding - " \
224                                 "cannot use it.\n", phys_id);
225                 }
226         }
227
228 smp_done:
229         Dprintk("Boot done.\n");
230 }
231
232 static void __init smp_tune_scheduling(void)
233 {
234         /* Nothing to do. */
235 }
236
237 /*
238  * init_ipi_lock : Initialize IPI locks.
239  */
240 static void __init init_ipi_lock(void)
241 {
242         int ipi;
243
244         for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
245                 spin_lock_init(&ipi_lock[ipi]);
246 }
247
248 /*==========================================================================*
249  * Name:         do_boot_cpu
250  *
251  * Description:  This routine boot up one AP.
252  *
253  * Born on Date: 2002.02.05
254  *
255  * Arguments:    phys_id - Target CPU physical ID
256  *
257  * Returns:      void (cannot fail)
258  *
259  * Modification log:
260  * Date       Who Description
261  * ---------- --- --------------------------------------------------------
262  * 2003-06-24 hy  modify for linux-2.5.69
263  *
264  *==========================================================================*/
265 static void __init do_boot_cpu(int phys_id)
266 {
267         struct task_struct *idle;
268         unsigned long send_status, boot_status;
269         int timeout, cpu_id;
270
271         cpu_id = ++cpucount;
272
273         /*
274          * We can't use kernel_thread since we must avoid to
275          * reschedule the child.
276          */
277         idle = fork_idle(cpu_id);
278         if (IS_ERR(idle))
279                 panic("failed fork for CPU#%d.", cpu_id);
280
281         idle->thread.lr = (unsigned long)start_secondary;
282
283         map_cpu_to_physid(cpu_id, phys_id);
284
285         /* So we see what's up   */
286         printk("Booting processor %d/%d\n", phys_id, cpu_id);
287         stack_start.spi = (void *)idle->thread.sp;
288         idle->thread_info->cpu = cpu_id;
289
290         /*
291          * Send Startup IPI
292          *   1.IPI received by CPU#(phys_id).
293          *   2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
294          *   3.CPU#(phys_id) enter start_secondary()
295          */
296         send_status = 0;
297         boot_status = 0;
298
299         cpu_set(phys_id, cpu_bootout_map);
300
301         /* Send Startup IPI */
302         send_IPI_mask_phys(cpumask_of_cpu(phys_id), CPU_BOOT_IPI, 0);
303
304         Dprintk("Waiting for send to finish...\n");
305         timeout = 0;
306
307         /* Wait 100[ms] */
308         do {
309                 Dprintk("+");
310                 udelay(1000);
311                 send_status = !cpu_isset(phys_id, cpu_bootin_map);
312         } while (send_status && (timeout++ < 100));
313
314         Dprintk("After Startup.\n");
315
316         if (!send_status) {
317                 /*
318                  * allow APs to start initializing.
319                  */
320                 Dprintk("Before Callout %d.\n", cpu_id);
321                 cpu_set(cpu_id, cpu_callout_map);
322                 Dprintk("After Callout %d.\n", cpu_id);
323
324                 /*
325                  * Wait 5s total for a response
326                  */
327                 for (timeout = 0; timeout < 5000; timeout++) {
328                         if (cpu_isset(cpu_id, cpu_callin_map))
329                                 break;  /* It has booted */
330                         udelay(1000);
331                 }
332
333                 if (cpu_isset(cpu_id, cpu_callin_map)) {
334                         /* number CPUs logically, starting from 1 (BSP is 0) */
335                         Dprintk("OK.\n");
336                 } else {
337                         boot_status = 1;
338                         printk("Not responding.\n");
339                 }
340         } else
341                 printk("IPI never delivered???\n");
342
343         if (send_status || boot_status) {
344                 unmap_cpu_to_physid(cpu_id, phys_id);
345                 cpu_clear(cpu_id, cpu_callout_map);
346                 cpu_clear(cpu_id, cpu_callin_map);
347                 cpu_clear(cpu_id, cpu_initialized);
348                 cpucount--;
349         }
350 }
351
352 int __devinit __cpu_up(unsigned int cpu_id)
353 {
354         int timeout;
355
356         cpu_set(cpu_id, smp_commenced_mask);
357
358         /*
359          * Wait 5s total for a response
360          */
361         for (timeout = 0; timeout < 5000; timeout++) {
362                 if (cpu_isset(cpu_id, cpu_online_map))
363                         break;
364                 udelay(1000);
365         }
366         if (!cpu_isset(cpu_id, cpu_online_map))
367                 BUG();
368
369         return 0;
370 }
371
372 void __init smp_cpus_done(unsigned int max_cpus)
373 {
374         int cpu_id, timeout;
375         unsigned long bogosum = 0;
376
377         for (timeout = 0; timeout < 5000; timeout++) {
378                 if (cpus_equal(cpu_callin_map, cpu_online_map))
379                         break;
380                 udelay(1000);
381         }
382         if (!cpus_equal(cpu_callin_map, cpu_online_map))
383                 BUG();
384
385         for (cpu_id = 0 ; cpu_id < num_online_cpus() ; cpu_id++)
386                 show_cpu_info(cpu_id);
387
388         /*
389          * Allow the user to impress friends.
390          */
391         Dprintk("Before bogomips.\n");
392         if (cpucount) {
393                 for_each_cpu_mask(cpu_id, cpu_online_map)
394                         bogosum += cpu_data[cpu_id].loops_per_jiffy;
395
396                 printk(KERN_INFO "Total of %d processors activated " \
397                         "(%lu.%02lu BogoMIPS).\n", cpucount + 1,
398                         bogosum / (500000 / HZ),
399                         (bogosum / (5000 / HZ)) % 100);
400                 Dprintk("Before bogocount - setting activated=1.\n");
401         }
402 }
403
404 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
405 /* Activate a secondary processor Routins                                    */
406 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
407
408 /*==========================================================================*
409  * Name:         start_secondary
410  *
411  * Description:  This routine activate a secondary processor.
412  *
413  * Born on Date: 2002.02.05
414  *
415  * Arguments:    *unused - currently unused.
416  *
417  * Returns:      void (cannot fail)
418  *
419  * Modification log:
420  * Date       Who Description
421  * ---------- --- --------------------------------------------------------
422  * 2003-06-24 hy  modify for linux-2.5.69
423  *
424  *==========================================================================*/
425 int __init start_secondary(void *unused)
426 {
427         cpu_init();
428         smp_callin();
429         while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
430                 cpu_relax();
431
432         smp_online();
433
434         /*
435          * low-memory mappings have been cleared, flush them from
436          * the local TLBs too.
437          */
438         local_flush_tlb_all();
439
440         cpu_idle();
441         return 0;
442 }
443
444 /*==========================================================================*
445  * Name:         smp_callin
446  *
447  * Description:  This routine activate a secondary processor.
448  *
449  * Born on Date: 2002.02.05
450  *
451  * Arguments:    NONE
452  *
453  * Returns:      void (cannot fail)
454  *
455  * Modification log:
456  * Date       Who Description
457  * ---------- --- --------------------------------------------------------
458  * 2003-06-24 hy  modify for linux-2.5.69
459  *
460  *==========================================================================*/
461 static void __init smp_callin(void)
462 {
463         int phys_id = hard_smp_processor_id();
464         int cpu_id = smp_processor_id();
465         unsigned long timeout;
466
467         if (cpu_isset(cpu_id, cpu_callin_map)) {
468                 printk("huh, phys CPU#%d, CPU#%d already present??\n",
469                         phys_id, cpu_id);
470                 BUG();
471         }
472         Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id, phys_id);
473
474         /* Waiting 2s total for startup (udelay is not yet working) */
475         timeout = jiffies + (2 * HZ);
476         while (time_before(jiffies, timeout)) {
477                 /* Has the boot CPU finished it's STARTUP sequence ? */
478                 if (cpu_isset(cpu_id, cpu_callout_map))
479                         break;
480                 cpu_relax();
481         }
482
483         if (!time_before(jiffies, timeout)) {
484                 printk("BUG: CPU#%d started up but did not get a callout!\n",
485                         cpu_id);
486                 BUG();
487         }
488
489         /* Allow the master to continue. */
490         cpu_set(cpu_id, cpu_callin_map);
491 }
492
493 static void __init smp_online(void)
494 {
495         int cpu_id = smp_processor_id();
496
497         local_irq_enable();
498
499         /* Get our bogomips. */
500         calibrate_delay();
501
502         /* Save our processor parameters */
503         smp_store_cpu_info(cpu_id);
504
505         cpu_set(cpu_id, cpu_online_map);
506 }
507
508 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
509 /* Boot up CPUs common Routins                                               */
510 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
511 static void __init show_mp_info(int nr_cpu)
512 {
513         int i;
514         char cpu_model0[17], cpu_model1[17], cpu_ver[9];
515
516         strncpy(cpu_model0, (char *)M32R_FPGA_CPU_NAME_ADDR, 16);
517         strncpy(cpu_model1, (char *)M32R_FPGA_MODEL_ID_ADDR, 16);
518         strncpy(cpu_ver, (char *)M32R_FPGA_VERSION_ADDR, 8);
519
520         cpu_model0[16] = '\0';
521         for (i = 15 ; i >= 0 ; i--) {
522                 if (cpu_model0[i] != ' ')
523                         break;
524                 cpu_model0[i] = '\0';
525         }
526         cpu_model1[16] = '\0';
527         for (i = 15 ; i >= 0 ; i--) {
528                 if (cpu_model1[i] != ' ')
529                         break;
530                 cpu_model1[i] = '\0';
531         }
532         cpu_ver[8] = '\0';
533         for (i = 7 ; i >= 0 ; i--) {
534                 if (cpu_ver[i] != ' ')
535                         break;
536                 cpu_ver[i] = '\0';
537         }
538
539         printk(KERN_INFO "M32R-mp information\n");
540         printk(KERN_INFO "  On-chip CPUs : %d\n", nr_cpu);
541         printk(KERN_INFO "  CPU model : %s/%s(%s)\n", cpu_model0,
542                 cpu_model1, cpu_ver);
543 }
544
545 /*
546  * The bootstrap kernel entry code has set these up. Save them for
547  * a given CPU
548  */
549 static void __init smp_store_cpu_info(int cpu_id)
550 {
551         struct cpuinfo_m32r *ci = cpu_data + cpu_id;
552
553         *ci = boot_cpu_data;
554         ci->loops_per_jiffy = loops_per_jiffy;
555 }
556
557 static void __init show_cpu_info(int cpu_id)
558 {
559         struct cpuinfo_m32r *ci = &cpu_data[cpu_id];
560
561         printk("CPU#%d : ", cpu_id);
562
563 #define PRINT_CLOCK(name, value) \
564         printk(name " clock %d.%02dMHz", \
565                 ((value) / 1000000), ((value) % 1000000) / 10000)
566
567         PRINT_CLOCK("CPU", (int)ci->cpu_clock);
568         PRINT_CLOCK(", Bus", (int)ci->bus_clock);
569         printk(", loops_per_jiffy[%ld]\n", ci->loops_per_jiffy);
570 }
571
572 /*
573  * the frequency of the profiling timer can be changed
574  * by writing a multiplier value into /proc/profile.
575  */
576 int setup_profiling_timer(unsigned int multiplier)
577 {
578         int i;
579
580         /*
581          * Sanity check. [at least 500 APIC cycles should be
582          * between APIC interrupts as a rule of thumb, to avoid
583          * irqs flooding us]
584          */
585         if ( (!multiplier) || (calibration_result / multiplier < 500))
586                 return -EINVAL;
587
588         /*
589          * Set the new multiplier for each CPU. CPUs don't start using the
590          * new values until the next timer interrupt in which they do process
591          * accounting. At that time they also adjust their APIC timers
592          * accordingly.
593          */
594         for (i = 0; i < NR_CPUS; ++i)
595                 per_cpu(prof_multiplier, i) = multiplier;
596
597         return 0;
598 }
599
600 /* Initialize all maps between cpu number and apicids */
601 static void __init init_cpu_to_physid(void)
602 {
603         int  i;
604
605         for (i = 0 ; i < NR_CPUS ; i++) {
606                 cpu_2_physid[i] = -1;
607                 physid_2_cpu[i] = -1;
608         }
609 }
610
611 /*
612  * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
613  * else physical apic ids
614  */
615 static void __init map_cpu_to_physid(int cpu_id, int phys_id)
616 {
617         physid_2_cpu[phys_id] = cpu_id;
618         cpu_2_physid[cpu_id] = phys_id;
619 }
620
621 /*
622  * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
623  * else physical apic ids
624  */
625 static void __init unmap_cpu_to_physid(int cpu_id, int phys_id)
626 {
627         physid_2_cpu[phys_id] = -1;
628         cpu_2_physid[cpu_id] = -1;
629 }
630