2 * linux/arch/m32r/kernel/smpboot.c
5 * M32R SMP booting functions
7 * Copyright (c) 2001, 2002, 2003 Hitoshi Yamamoto
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>
13 * Much of the core SMP work is based on previous work by Thomas Radke, to
14 * whom a great many thanks are extended.
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.
20 * This code is released under the GNU General Public License version 2 or
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
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
42 #include <linux/config.h>
43 #include <linux/init.h>
45 #include <linux/smp_lock.h>
46 #include <linux/irq.h>
47 #include <linux/bootmem.h>
48 #include <linux/delay.h>
51 #include <asm/pgalloc.h>
52 #include <asm/tlbflush.h>
56 #define Dprintk(x...) printk(x)
61 extern cpumask_t cpu_initialized;
63 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
64 /* Data structures and variables */
65 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
67 /* Processor that is doing the boot up */
68 static unsigned int bsp_phys_id = -1;
70 /* Bitmask of physically existing CPUs */
71 physid_mask_t phys_cpu_present_map;
73 /* Bitmask of currently online CPUs */
74 cpumask_t cpu_online_map;
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;
81 /* Per CPU bogomips and other parameters */
82 struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
84 /* Set when the idlers are all forked */
85 int smp_threads_ready;
88 static cpumask_t smp_commenced_mask;
95 /* which physical physical ID maps to which logical CPU number */
96 static volatile int physid_2_cpu[NR_CPUS];
98 /* which logical CPU number maps to which physical ID */
99 volatile int cpu_2_physid[NR_CPUS];
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;
105 spinlock_t ipi_lock[NR_IPIS];
107 static unsigned int calibration_result;
109 unsigned long cache_decay_ticks = HZ / 100;
111 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
112 /* Function Prototypes */
113 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
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);
123 int start_secondary(void *);
124 static void smp_callin(void);
125 static void smp_online(void);
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);
135 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
136 /* Boot up APs Routins : BSP */
137 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
138 void __devinit smp_prepare_boot_cpu(void)
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);
147 * Initialize the logical to physical CPU number mapping
149 init_cpu_to_physid();
150 map_cpu_to_physid(0, bsp_phys_id);
151 current_thread_info()->cpu = 0;
154 /*==========================================================================*
155 * Name: smp_prepare_cpus (old smp_boot_cpus)
157 * Description: This routine boot up APs.
159 * Born on Date: 2002.02.05
163 * Returns: void (cannot fail)
166 * Date Who Description
167 * ---------- --- --------------------------------------------------------
168 * 2003-06-24 hy modify for linux-2.5.69
170 *==========================================================================*/
171 void __init smp_prepare_cpus(unsigned int max_cpus)
174 unsigned long nr_cpu;
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]",
182 for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
183 physid_set(phys_id, phys_cpu_present_map);
185 show_mp_info(nr_cpu);
190 * Setup boot CPU information
192 smp_store_cpu_info(0); /* Final full version of the data */
193 smp_tune_scheduling();
196 * If SMP should be disabled, then really disable it!
199 printk(KERN_INFO "SMP mode deactivated by commandline.\n");
204 * Now scan the CPU present map and fire up the other CPUs.
206 Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
208 for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
210 * Don't even attempt to start the boot CPU!
212 if (phys_id == bsp_phys_id)
215 if (!physid_isset(phys_id, phys_cpu_present_map))
218 if ((max_cpus >= 0) && (max_cpus <= cpucount + 1))
221 do_boot_cpu(phys_id);
224 * Make sure we unmap all failed CPUs
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);
234 Dprintk("Boot done.\n");
237 static void __init smp_tune_scheduling(void)
243 * init_ipi_lock : Initialize IPI locks.
245 static void __init init_ipi_lock(void)
249 for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
250 spin_lock_init(&ipi_lock[ipi]);
253 /*==========================================================================*
256 * Description: This routine boot up one AP.
258 * Born on Date: 2002.02.05
260 * Arguments: phys_id - Target CPU physical ID
262 * Returns: void (cannot fail)
265 * Date Who Description
266 * ---------- --- --------------------------------------------------------
267 * 2003-06-24 hy modify for linux-2.5.69
269 *==========================================================================*/
270 static void __init do_boot_cpu(int phys_id)
272 struct task_struct *idle;
273 unsigned long send_status, boot_status;
279 * We can't use kernel_thread since we must avoid to
280 * reschedule the child.
282 idle = fork_idle(cpu_id);
284 panic("failed fork for CPU#%d.", cpu_id);
286 idle->thread.lr = (unsigned long)start_secondary;
288 map_cpu_to_physid(cpu_id, phys_id);
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;
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()
304 cpu_set(phys_id, cpu_bootout_map);
306 /* Send Startup IPI */
307 send_IPI_mask_phys(cpumask_of_cpu(phys_id), CPU_BOOT_IPI, 0);
309 Dprintk("Waiting for send to finish...\n");
316 send_status = !cpu_isset(phys_id, cpu_bootin_map);
317 } while (send_status && (timeout++ < 100));
319 Dprintk("After Startup.\n");
323 * allow APs to start initializing.
325 Dprintk("Before Callout %d.\n", cpu_id);
326 cpu_set(cpu_id, cpu_callout_map);
327 Dprintk("After Callout %d.\n", cpu_id);
330 * Wait 5s total for a response
332 for (timeout = 0; timeout < 5000; timeout++) {
333 if (cpu_isset(cpu_id, cpu_callin_map))
334 break; /* It has booted */
338 if (cpu_isset(cpu_id, cpu_callin_map)) {
339 /* number CPUs logically, starting from 1 (BSP is 0) */
343 printk("Not responding.\n");
346 printk("IPI never delivered???\n");
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);
357 int __devinit __cpu_up(unsigned int cpu_id)
361 cpu_set(cpu_id, smp_commenced_mask);
364 * Wait 5s total for a response
366 for (timeout = 0; timeout < 5000; timeout++) {
367 if (cpu_isset(cpu_id, cpu_online_map))
371 if (!cpu_isset(cpu_id, cpu_online_map))
377 void __init smp_cpus_done(unsigned int max_cpus)
380 unsigned long bogosum = 0;
382 for (timeout = 0; timeout < 5000; timeout++) {
383 if (cpus_equal(cpu_callin_map, cpu_online_map))
387 if (!cpus_equal(cpu_callin_map, cpu_online_map))
390 for (cpu_id = 0 ; cpu_id < num_online_cpus() ; cpu_id++)
391 show_cpu_info(cpu_id);
394 * Allow the user to impress friends.
396 Dprintk("Before bogomips.\n");
398 for_each_cpu_mask(cpu_id, cpu_online_map)
399 bogosum += cpu_data[cpu_id].loops_per_jiffy;
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");
409 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
410 /* Activate a secondary processor Routins */
411 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
413 /*==========================================================================*
414 * Name: start_secondary
416 * Description: This routine activate a secondary processor.
418 * Born on Date: 2002.02.05
420 * Arguments: *unused - currently unused.
422 * Returns: void (cannot fail)
425 * Date Who Description
426 * ---------- --- --------------------------------------------------------
427 * 2003-06-24 hy modify for linux-2.5.69
429 *==========================================================================*/
430 int __init start_secondary(void *unused)
434 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
440 * low-memory mappings have been cleared, flush them from
441 * the local TLBs too.
443 local_flush_tlb_all();
449 /*==========================================================================*
452 * Description: This routine activate a secondary processor.
454 * Born on Date: 2002.02.05
458 * Returns: void (cannot fail)
461 * Date Who Description
462 * ---------- --- --------------------------------------------------------
463 * 2003-06-24 hy modify for linux-2.5.69
465 *==========================================================================*/
466 static void __init smp_callin(void)
468 int phys_id = hard_smp_processor_id();
469 int cpu_id = smp_processor_id();
470 unsigned long timeout;
472 if (cpu_isset(cpu_id, cpu_callin_map)) {
473 printk("huh, phys CPU#%d, CPU#%d already present??\n",
477 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id, phys_id);
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))
488 if (!time_before(jiffies, timeout)) {
489 printk("BUG: CPU#%d started up but did not get a callout!\n",
494 /* Allow the master to continue. */
495 cpu_set(cpu_id, cpu_callin_map);
498 static void __init smp_online(void)
500 int cpu_id = smp_processor_id();
504 /* Get our bogomips. */
507 /* Save our processor parameters */
508 smp_store_cpu_info(cpu_id);
510 cpu_set(cpu_id, cpu_online_map);
513 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
514 /* Boot up CPUs common Routins */
515 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
516 static void __init show_mp_info(int nr_cpu)
519 char cpu_model0[17], cpu_model1[17], cpu_ver[9];
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);
525 cpu_model0[16] = '\0';
526 for (i = 15 ; i >= 0 ; i--) {
527 if (cpu_model0[i] != ' ')
529 cpu_model0[i] = '\0';
531 cpu_model1[16] = '\0';
532 for (i = 15 ; i >= 0 ; i--) {
533 if (cpu_model1[i] != ' ')
535 cpu_model1[i] = '\0';
538 for (i = 7 ; i >= 0 ; i--) {
539 if (cpu_ver[i] != ' ')
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);
551 * The bootstrap kernel entry code has set these up. Save them for
554 static void __init smp_store_cpu_info(int cpu_id)
556 struct cpuinfo_m32r *ci = cpu_data + cpu_id;
559 ci->loops_per_jiffy = loops_per_jiffy;
562 static void __init show_cpu_info(int cpu_id)
564 struct cpuinfo_m32r *ci = &cpu_data[cpu_id];
566 printk("CPU#%d : ", cpu_id);
568 #define PRINT_CLOCK(name, value) \
569 printk(name " clock %d.%02dMHz", \
570 ((value) / 1000000), ((value) % 1000000) / 10000)
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);
578 * the frequency of the profiling timer can be changed
579 * by writing a multiplier value into /proc/profile.
581 int setup_profiling_timer(unsigned int multiplier)
586 * Sanity check. [at least 500 APIC cycles should be
587 * between APIC interrupts as a rule of thumb, to avoid
590 if ( (!multiplier) || (calibration_result / multiplier < 500))
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
599 for (i = 0; i < NR_CPUS; ++i)
600 per_cpu(prof_multiplier, i) = multiplier;
605 /* Initialize all maps between cpu number and apicids */
606 static void __init init_cpu_to_physid(void)
610 for (i = 0 ; i < NR_CPUS ; i++) {
611 cpu_2_physid[i] = -1;
612 physid_2_cpu[i] = -1;
617 * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
618 * else physical apic ids
620 static void __init map_cpu_to_physid(int cpu_id, int phys_id)
622 physid_2_cpu[phys_id] = cpu_id;
623 cpu_2_physid[cpu_id] = phys_id;
627 * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
628 * else physical apic ids
630 static void __init unmap_cpu_to_physid(int cpu_id, int phys_id)
632 physid_2_cpu[phys_id] = -1;
633 cpu_2_physid[cpu_id] = -1;