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