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