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