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