This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 int cpu_idle(void);
62 extern cpumask_t cpu_initialized;
63
64 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
65 /* Data structures and variables                                             */
66 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
67
68 /* Processor that is doing the boot up */
69 static unsigned int bsp_phys_id = -1;
70
71 /* Bitmask of physically existing CPUs */
72 physid_mask_t phys_cpu_present_map;
73
74 /* Bitmask of currently online CPUs */
75 cpumask_t cpu_online_map;
76
77 cpumask_t cpu_bootout_map;
78 cpumask_t cpu_bootin_map;
79 cpumask_t cpu_callout_map;
80 static cpumask_t cpu_callin_map;
81
82 /* Per CPU bogomips and other parameters */
83 struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
84
85 /* Set when the idlers are all forked */
86 int smp_threads_ready;
87
88 static int cpucount;
89 static cpumask_t smp_commenced_mask;
90
91 extern struct {
92         void * spi;
93         unsigned short ss;
94 } stack_start;
95
96 /* which physical physical ID maps to which logical CPU number */
97 static volatile int physid_2_cpu[NR_CPUS];
98
99 /* which logical CPU number maps to which physical ID */
100 volatile int cpu_2_physid[NR_CPUS];
101
102 DEFINE_PER_CPU(int, prof_multiplier) = 1;
103 DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
104 DEFINE_PER_CPU(int, prof_counter) = 1;
105
106 spinlock_t ipi_lock[NR_IPIS];
107
108 static unsigned int calibration_result;
109
110 unsigned long cache_decay_ticks = HZ / 100;
111
112 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
113 /* Function Prototypes                                                       */
114 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
115
116 void smp_prepare_boot_cpu(void);
117 void smp_prepare_cpus(unsigned int);
118 static void smp_tune_scheduling(void);
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
186         show_mp_info(nr_cpu);
187
188         init_ipi_lock();
189
190         /*
191          * Setup boot CPU information
192          */
193         smp_store_cpu_info(0); /* Final full version of the data */
194         smp_tune_scheduling();
195
196         /*
197          * If SMP should be disabled, then really disable it!
198          */
199         if (!max_cpus) {
200                 printk(KERN_INFO "SMP mode deactivated by commandline.\n");
201                 goto smp_done;
202         }
203
204         /*
205          * Now scan the CPU present map and fire up the other CPUs.
206          */
207         Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
208
209         for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
210                 /*
211                  * Don't even attempt to start the boot CPU!
212                  */
213                 if (phys_id == bsp_phys_id)
214                         continue;
215
216                 if (!physid_isset(phys_id, phys_cpu_present_map))
217                         continue;
218
219                 if ((max_cpus >= 0) && (max_cpus <= cpucount + 1))
220                         continue;
221
222                 do_boot_cpu(phys_id);
223
224                 /*
225                  * Make sure we unmap all failed CPUs
226                  */
227                 if (physid_to_cpu(phys_id) == -1) {
228                         physid_clear(phys_id, phys_cpu_present_map);
229                         printk("phys CPU#%d not responding - " \
230                                 "cannot use it.\n", phys_id);
231                 }
232         }
233
234 smp_done:
235         Dprintk("Boot done.\n");
236 }
237
238 static void __init smp_tune_scheduling(void)
239 {
240         /* Nothing to do. */
241 }
242
243 /*
244  * init_ipi_lock : Initialize IPI locks.
245  */
246 static void __init init_ipi_lock(void)
247 {
248         int ipi;
249
250         for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
251                 ipi_lock[ipi] = SPIN_LOCK_UNLOCKED;
252 }
253
254 /*==========================================================================*
255  * Name:         do_boot_cpu
256  *
257  * Description:  This routine boot up one AP.
258  *
259  * Born on Date: 2002.02.05
260  *
261  * Arguments:    phys_id - Target CPU physical ID
262  *
263  * Returns:      void (cannot fail)
264  *
265  * Modification log:
266  * Date       Who Description
267  * ---------- --- --------------------------------------------------------
268  * 2003-06-24 hy  modify for linux-2.5.69
269  *
270  *==========================================================================*/
271 static void __init do_boot_cpu(int phys_id)
272 {
273         struct task_struct *idle;
274         unsigned long send_status, boot_status;
275         int timeout, cpu_id;
276
277         cpu_id = ++cpucount;
278
279         /*
280          * We can't use kernel_thread since we must avoid to
281          * reschedule the child.
282          */
283         idle = fork_idle(cpu_id);
284         if (IS_ERR(idle))
285                 panic("failed fork for CPU#%d.", cpu_id);
286
287         idle->thread.lr = (unsigned long)start_secondary;
288
289         map_cpu_to_physid(cpu_id, phys_id);
290
291         /* So we see what's up   */
292         printk("Booting processor %d/%d\n", phys_id, cpu_id);
293         stack_start.spi = (void *)idle->thread.sp;
294         idle->thread_info->cpu = cpu_id;
295
296         /*
297          * Send Startup IPI
298          *   1.IPI received by CPU#(phys_id).
299          *   2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
300          *   3.CPU#(phys_id) enter start_secondary()
301          */
302         send_status = 0;
303         boot_status = 0;
304
305         cpu_set(phys_id, cpu_bootout_map);
306
307         /* Send Startup IPI */
308         send_IPI_mask_phys(cpumask_of_cpu(phys_id), CPU_BOOT_IPI, 0);
309
310         Dprintk("Waiting for send to finish...\n");
311         timeout = 0;
312
313         /* Wait 100[ms] */
314         do {
315                 Dprintk("+");
316                 udelay(1000);
317                 send_status = !cpu_isset(phys_id, cpu_bootin_map);
318         } while (send_status && (timeout++ < 100));
319
320         Dprintk("After Startup.\n");
321
322         if (!send_status) {
323                 /*
324                  * allow APs to start initializing.
325                  */
326                 Dprintk("Before Callout %d.\n", cpu_id);
327                 cpu_set(cpu_id, cpu_callout_map);
328                 Dprintk("After Callout %d.\n", cpu_id);
329
330                 /*
331                  * Wait 5s total for a response
332                  */
333                 for (timeout = 0; timeout < 5000; timeout++) {
334                         if (cpu_isset(cpu_id, cpu_callin_map))
335                                 break;  /* It has booted */
336                         udelay(1000);
337                 }
338
339                 if (cpu_isset(cpu_id, cpu_callin_map)) {
340                         /* number CPUs logically, starting from 1 (BSP is 0) */
341                         Dprintk("OK.\n");
342                 } else {
343                         boot_status = 1;
344                         printk("Not responding.\n");
345                 }
346         } else
347                 printk("IPI never delivered???\n");
348
349         if (send_status || boot_status) {
350                 unmap_cpu_to_physid(cpu_id, phys_id);
351                 cpu_clear(cpu_id, cpu_callout_map);
352                 cpu_clear(cpu_id, cpu_callin_map);
353                 cpu_clear(cpu_id, cpu_initialized);
354                 cpucount--;
355         }
356 }
357
358 int __devinit __cpu_up(unsigned int cpu_id)
359 {
360         int timeout;
361
362         cpu_set(cpu_id, smp_commenced_mask);
363
364         /*
365          * Wait 5s total for a response
366          */
367         for (timeout = 0; timeout < 5000; timeout++) {
368                 if (cpu_isset(cpu_id, cpu_online_map))
369                         break;
370                 udelay(1000);
371         }
372         if (!cpu_isset(cpu_id, cpu_online_map))
373                 BUG();
374
375         return 0;
376 }
377
378 void __init smp_cpus_done(unsigned int max_cpus)
379 {
380         int cpu_id, timeout;
381         unsigned long bogosum = 0;
382
383         for (timeout = 0; timeout < 5000; timeout++) {
384                 if (cpus_equal(cpu_callin_map, cpu_online_map))
385                         break;
386                 udelay(1000);
387         }
388         if (!cpus_equal(cpu_callin_map, cpu_online_map))
389                 BUG();
390
391         for (cpu_id = 0 ; cpu_id < num_online_cpus() ; cpu_id++)
392                 show_cpu_info(cpu_id);
393
394         /*
395          * Allow the user to impress friends.
396          */
397         Dprintk("Before bogomips.\n");
398         if (cpucount) {
399                 for_each_cpu_mask(cpu_id, cpu_online_map)
400                         bogosum += cpu_data[cpu_id].loops_per_jiffy;
401
402                 printk(KERN_INFO "Total of %d processors activated " \
403                         "(%lu.%02lu BogoMIPS).\n", cpucount + 1,
404                         bogosum / (500000 / HZ),
405                         (bogosum / (5000 / HZ)) % 100);
406                 Dprintk("Before bogocount - setting activated=1.\n");
407         }
408 }
409
410 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
411 /* Activate a secondary processor Routins                                    */
412 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
413
414 /*==========================================================================*
415  * Name:         start_secondary
416  *
417  * Description:  This routine activate a secondary processor.
418  *
419  * Born on Date: 2002.02.05
420  *
421  * Arguments:    *unused - currently unused.
422  *
423  * Returns:      void (cannot fail)
424  *
425  * Modification log:
426  * Date       Who Description
427  * ---------- --- --------------------------------------------------------
428  * 2003-06-24 hy  modify for linux-2.5.69
429  *
430  *==========================================================================*/
431 int __init start_secondary(void *unused)
432 {
433         cpu_init();
434         smp_callin();
435         while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
436                 rep_nop();
437
438         smp_online();
439
440         /*
441          * low-memory mappings have been cleared, flush them from
442          * the local TLBs too.
443          */
444         local_flush_tlb_all();
445
446         return cpu_idle();
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                 rep_nop();
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