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 int cpu_idle(void);
62 extern cpumask_t cpu_initialized;
64 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
65 /* Data structures and variables */
66 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
68 /* Processor that is doing the boot up */
69 static unsigned int bsp_phys_id = -1;
71 /* Bitmask of physically existing CPUs */
72 physid_mask_t phys_cpu_present_map;
74 /* Bitmask of currently online CPUs */
75 cpumask_t cpu_online_map;
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;
82 /* Per CPU bogomips and other parameters */
83 struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
85 /* Set when the idlers are all forked */
86 int smp_threads_ready;
89 static cpumask_t smp_commenced_mask;
96 /* which physical physical ID maps to which logical CPU number */
97 static volatile int physid_2_cpu[NR_CPUS];
99 /* which logical CPU number maps to which physical ID */
100 volatile int cpu_2_physid[NR_CPUS];
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;
106 spinlock_t ipi_lock[NR_IPIS];
108 static unsigned int calibration_result;
110 unsigned long cache_decay_ticks = HZ / 100;
112 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
113 /* Function Prototypes */
114 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
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);
124 int start_secondary(void *);
125 static void smp_callin(void);
126 static void smp_online(void);
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);
136 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
137 /* Boot up APs Routins : BSP */
138 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
139 void __devinit smp_prepare_boot_cpu(void)
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);
148 * Initialize the logical to physical CPU number mapping
150 init_cpu_to_physid();
151 map_cpu_to_physid(0, bsp_phys_id);
152 current_thread_info()->cpu = 0;
155 /*==========================================================================*
156 * Name: smp_prepare_cpus (old smp_boot_cpus)
158 * Description: This routine boot up APs.
160 * Born on Date: 2002.02.05
164 * Returns: void (cannot fail)
167 * Date Who Description
168 * ---------- --- --------------------------------------------------------
169 * 2003-06-24 hy modify for linux-2.5.69
171 *==========================================================================*/
172 void __init smp_prepare_cpus(unsigned int max_cpus)
175 unsigned long nr_cpu;
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]",
183 for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
184 physid_set(phys_id, phys_cpu_present_map);
186 show_mp_info(nr_cpu);
191 * Setup boot CPU information
193 smp_store_cpu_info(0); /* Final full version of the data */
194 smp_tune_scheduling();
197 * If SMP should be disabled, then really disable it!
200 printk(KERN_INFO "SMP mode deactivated by commandline.\n");
205 * Now scan the CPU present map and fire up the other CPUs.
207 Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
209 for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
211 * Don't even attempt to start the boot CPU!
213 if (phys_id == bsp_phys_id)
216 if (!physid_isset(phys_id, phys_cpu_present_map))
219 if ((max_cpus >= 0) && (max_cpus <= cpucount + 1))
222 do_boot_cpu(phys_id);
225 * Make sure we unmap all failed CPUs
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);
235 Dprintk("Boot done.\n");
238 static void __init smp_tune_scheduling(void)
244 * init_ipi_lock : Initialize IPI locks.
246 static void __init init_ipi_lock(void)
250 for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
251 ipi_lock[ipi] = SPIN_LOCK_UNLOCKED;
254 /*==========================================================================*
257 * Description: This routine boot up one AP.
259 * Born on Date: 2002.02.05
261 * Arguments: phys_id - Target CPU physical ID
263 * Returns: void (cannot fail)
266 * Date Who Description
267 * ---------- --- --------------------------------------------------------
268 * 2003-06-24 hy modify for linux-2.5.69
270 *==========================================================================*/
271 static void __init do_boot_cpu(int phys_id)
273 struct task_struct *idle;
274 unsigned long send_status, boot_status;
280 * We can't use kernel_thread since we must avoid to
281 * reschedule the child.
283 idle = fork_idle(cpu_id);
285 panic("failed fork for CPU#%d.", cpu_id);
287 idle->thread.lr = (unsigned long)start_secondary;
289 map_cpu_to_physid(cpu_id, phys_id);
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;
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()
305 cpu_set(phys_id, cpu_bootout_map);
307 /* Send Startup IPI */
308 send_IPI_mask_phys(cpumask_of_cpu(phys_id), CPU_BOOT_IPI, 0);
310 Dprintk("Waiting for send to finish...\n");
317 send_status = !cpu_isset(phys_id, cpu_bootin_map);
318 } while (send_status && (timeout++ < 100));
320 Dprintk("After Startup.\n");
324 * allow APs to start initializing.
326 Dprintk("Before Callout %d.\n", cpu_id);
327 cpu_set(cpu_id, cpu_callout_map);
328 Dprintk("After Callout %d.\n", cpu_id);
331 * Wait 5s total for a response
333 for (timeout = 0; timeout < 5000; timeout++) {
334 if (cpu_isset(cpu_id, cpu_callin_map))
335 break; /* It has booted */
339 if (cpu_isset(cpu_id, cpu_callin_map)) {
340 /* number CPUs logically, starting from 1 (BSP is 0) */
344 printk("Not responding.\n");
347 printk("IPI never delivered???\n");
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);
358 int __devinit __cpu_up(unsigned int cpu_id)
362 cpu_set(cpu_id, smp_commenced_mask);
365 * Wait 5s total for a response
367 for (timeout = 0; timeout < 5000; timeout++) {
368 if (cpu_isset(cpu_id, cpu_online_map))
372 if (!cpu_isset(cpu_id, cpu_online_map))
378 void __init smp_cpus_done(unsigned int max_cpus)
381 unsigned long bogosum = 0;
383 for (timeout = 0; timeout < 5000; timeout++) {
384 if (cpus_equal(cpu_callin_map, cpu_online_map))
388 if (!cpus_equal(cpu_callin_map, cpu_online_map))
391 for (cpu_id = 0 ; cpu_id < num_online_cpus() ; cpu_id++)
392 show_cpu_info(cpu_id);
395 * Allow the user to impress friends.
397 Dprintk("Before bogomips.\n");
399 for_each_cpu_mask(cpu_id, cpu_online_map)
400 bogosum += cpu_data[cpu_id].loops_per_jiffy;
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");
410 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
411 /* Activate a secondary processor Routins */
412 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
414 /*==========================================================================*
415 * Name: start_secondary
417 * Description: This routine activate a secondary processor.
419 * Born on Date: 2002.02.05
421 * Arguments: *unused - currently unused.
423 * Returns: void (cannot fail)
426 * Date Who Description
427 * ---------- --- --------------------------------------------------------
428 * 2003-06-24 hy modify for linux-2.5.69
430 *==========================================================================*/
431 int __init start_secondary(void *unused)
435 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
441 * low-memory mappings have been cleared, flush them from
442 * the local TLBs too.
444 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;