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.
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/module.h>
43 #include <linux/cpu.h>
44 #include <linux/init.h>
45 #include <linux/kernel.h>
47 #include <linux/sched.h>
48 #include <linux/sched/task.h>
49 #include <linux/err.h>
50 #include <linux/irq.h>
51 #include <linux/bootmem.h>
52 #include <linux/delay.h>
55 #include <asm/pgalloc.h>
56 #include <asm/tlbflush.h>
60 #define Dprintk(x...) printk(x)
65 extern cpumask_t cpu_initialized;
67 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
68 /* Data structures and variables */
69 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
71 /* Processor that is doing the boot up */
72 static unsigned int bsp_phys_id = -1;
74 /* Bitmask of physically existing CPUs */
75 physid_mask_t phys_cpu_present_map;
77 cpumask_t cpu_bootout_map;
78 cpumask_t cpu_bootin_map;
79 static cpumask_t cpu_callin_map;
80 cpumask_t cpu_callout_map;
81 EXPORT_SYMBOL(cpu_callout_map);
83 /* Per CPU bogomips and other parameters */
84 struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
87 static cpumask_t smp_commenced_mask;
94 /* which physical physical ID maps to which logical CPU number */
95 static volatile int physid_2_cpu[NR_CPUS];
96 #define physid_to_cpu(physid) physid_2_cpu[physid]
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 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
110 /* Function Prototypes */
111 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
113 static void init_ipi_lock(void);
114 static void do_boot_cpu(int);
116 int start_secondary(void *);
117 static void smp_callin(void);
118 static void smp_online(void);
120 static void show_mp_info(int);
121 static void smp_store_cpu_info(int);
122 static void show_cpu_info(int);
123 int setup_profiling_timer(unsigned int);
124 static void init_cpu_to_physid(void);
125 static void map_cpu_to_physid(int, int);
126 static void unmap_cpu_to_physid(int, int);
128 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
129 /* Boot up APs Routines : BSP */
130 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
131 void smp_prepare_boot_cpu(void)
133 bsp_phys_id = hard_smp_processor_id();
134 physid_set(bsp_phys_id, phys_cpu_present_map);
135 set_cpu_online(0, true); /* BSP's cpu_id == 0 */
136 cpumask_set_cpu(0, &cpu_callout_map);
137 cpumask_set_cpu(0, &cpu_callin_map);
140 * Initialize the logical to physical CPU number mapping
142 init_cpu_to_physid();
143 map_cpu_to_physid(0, bsp_phys_id);
144 current_thread_info()->cpu = 0;
147 /*==========================================================================*
148 * Name: smp_prepare_cpus (old smp_boot_cpus)
150 * Description: This routine boot up APs.
152 * Born on Date: 2002.02.05
156 * Returns: void (cannot fail)
159 * Date Who Description
160 * ---------- --- --------------------------------------------------------
161 * 2003-06-24 hy modify for linux-2.5.69
163 *==========================================================================*/
164 void __init smp_prepare_cpus(unsigned int max_cpus)
167 unsigned long nr_cpu;
169 nr_cpu = inl(M32R_FPGA_NUM_OF_CPUS_PORTL);
170 if (nr_cpu > NR_CPUS) {
171 printk(KERN_INFO "NUM_OF_CPUS reg. value [%ld] > NR_CPU [%d]",
175 for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
176 physid_set(phys_id, phys_cpu_present_map);
177 #ifndef CONFIG_HOTPLUG_CPU
178 init_cpu_present(cpu_possible_mask);
181 show_mp_info(nr_cpu);
186 * Setup boot CPU information
188 smp_store_cpu_info(0); /* Final full version of the data */
191 * If SMP should be disabled, then really disable it!
194 printk(KERN_INFO "SMP mode deactivated by commandline.\n");
199 * Now scan the CPU present map and fire up the other CPUs.
201 Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
203 for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
205 * Don't even attempt to start the boot CPU!
207 if (phys_id == bsp_phys_id)
210 if (!physid_isset(phys_id, phys_cpu_present_map))
213 if (max_cpus <= cpucount + 1)
216 do_boot_cpu(phys_id);
219 * Make sure we unmap all failed CPUs
221 if (physid_to_cpu(phys_id) == -1) {
222 physid_clear(phys_id, phys_cpu_present_map);
223 printk("phys CPU#%d not responding - " \
224 "cannot use it.\n", phys_id);
229 Dprintk("Boot done.\n");
233 * init_ipi_lock : Initialize IPI locks.
235 static void __init init_ipi_lock(void)
239 for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
240 spin_lock_init(&ipi_lock[ipi]);
243 /*==========================================================================*
246 * Description: This routine boot up one AP.
248 * Born on Date: 2002.02.05
250 * Arguments: phys_id - Target CPU physical ID
252 * Returns: void (cannot fail)
255 * Date Who Description
256 * ---------- --- --------------------------------------------------------
257 * 2003-06-24 hy modify for linux-2.5.69
259 *==========================================================================*/
260 static void __init do_boot_cpu(int phys_id)
262 struct task_struct *idle;
263 unsigned long send_status, boot_status;
269 * We can't use kernel_thread since we must avoid to
270 * reschedule the child.
272 idle = fork_idle(cpu_id);
274 panic("failed fork for CPU#%d.", cpu_id);
276 idle->thread.lr = (unsigned long)start_secondary;
278 map_cpu_to_physid(cpu_id, phys_id);
280 /* So we see what's up */
281 printk("Booting processor %d/%d\n", phys_id, cpu_id);
282 stack_start.spi = (void *)idle->thread.sp;
283 task_thread_info(idle)->cpu = cpu_id;
287 * 1.IPI received by CPU#(phys_id).
288 * 2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
289 * 3.CPU#(phys_id) enter start_secondary()
294 cpumask_set_cpu(phys_id, &cpu_bootout_map);
296 /* Send Startup IPI */
297 send_IPI_mask_phys(cpumask_of(phys_id), CPU_BOOT_IPI, 0);
299 Dprintk("Waiting for send to finish...\n");
306 send_status = !cpumask_test_cpu(phys_id, &cpu_bootin_map);
307 } while (send_status && (timeout++ < 100));
309 Dprintk("After Startup.\n");
313 * allow APs to start initializing.
315 Dprintk("Before Callout %d.\n", cpu_id);
316 cpumask_set_cpu(cpu_id, &cpu_callout_map);
317 Dprintk("After Callout %d.\n", cpu_id);
320 * Wait 5s total for a response
322 for (timeout = 0; timeout < 5000; timeout++) {
323 if (cpumask_test_cpu(cpu_id, &cpu_callin_map))
324 break; /* It has booted */
328 if (cpumask_test_cpu(cpu_id, &cpu_callin_map)) {
329 /* number CPUs logically, starting from 1 (BSP is 0) */
333 printk("Not responding.\n");
336 printk("IPI never delivered???\n");
338 if (send_status || boot_status) {
339 unmap_cpu_to_physid(cpu_id, phys_id);
340 cpumask_clear_cpu(cpu_id, &cpu_callout_map);
341 cpumask_clear_cpu(cpu_id, &cpu_callin_map);
342 cpumask_clear_cpu(cpu_id, &cpu_initialized);
347 int __cpu_up(unsigned int cpu_id, struct task_struct *tidle)
351 cpumask_set_cpu(cpu_id, &smp_commenced_mask);
354 * Wait 5s total for a response
356 for (timeout = 0; timeout < 5000; timeout++) {
357 if (cpu_online(cpu_id))
361 if (!cpu_online(cpu_id))
367 void __init smp_cpus_done(unsigned int max_cpus)
370 unsigned long bogosum = 0;
372 for (timeout = 0; timeout < 5000; timeout++) {
373 if (cpumask_equal(&cpu_callin_map, cpu_online_mask))
377 if (!cpumask_equal(&cpu_callin_map, cpu_online_mask))
380 for_each_online_cpu(cpu_id)
381 show_cpu_info(cpu_id);
384 * Allow the user to impress friends.
386 Dprintk("Before bogomips.\n");
388 for_each_cpu(cpu_id,cpu_online_mask)
389 bogosum += cpu_data[cpu_id].loops_per_jiffy;
391 printk(KERN_INFO "Total of %d processors activated " \
392 "(%lu.%02lu BogoMIPS).\n", cpucount + 1,
393 bogosum / (500000 / HZ),
394 (bogosum / (5000 / HZ)) % 100);
395 Dprintk("Before bogocount - setting activated=1.\n");
399 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
400 /* Activate a secondary processor Routines */
401 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
403 /*==========================================================================*
404 * Name: start_secondary
406 * Description: This routine activate a secondary processor.
408 * Born on Date: 2002.02.05
410 * Arguments: *unused - currently unused.
412 * Returns: void (cannot fail)
415 * Date Who Description
416 * ---------- --- --------------------------------------------------------
417 * 2003-06-24 hy modify for linux-2.5.69
419 *==========================================================================*/
420 int __init start_secondary(void *unused)
425 while (!cpumask_test_cpu(smp_processor_id(), &smp_commenced_mask))
431 * low-memory mappings have been cleared, flush them from
432 * the local TLBs too.
434 local_flush_tlb_all();
436 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
440 /*==========================================================================*
443 * Description: This routine activate a secondary processor.
445 * Born on Date: 2002.02.05
449 * Returns: void (cannot fail)
452 * Date Who Description
453 * ---------- --- --------------------------------------------------------
454 * 2003-06-24 hy modify for linux-2.5.69
456 *==========================================================================*/
457 static void __init smp_callin(void)
459 int phys_id = hard_smp_processor_id();
460 int cpu_id = smp_processor_id();
461 unsigned long timeout;
463 if (cpumask_test_cpu(cpu_id, &cpu_callin_map)) {
464 printk("huh, phys CPU#%d, CPU#%d already present??\n",
468 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id, phys_id);
470 /* Waiting 2s total for startup (udelay is not yet working) */
471 timeout = jiffies + (2 * HZ);
472 while (time_before(jiffies, timeout)) {
473 /* Has the boot CPU finished it's STARTUP sequence ? */
474 if (cpumask_test_cpu(cpu_id, &cpu_callout_map))
479 if (!time_before(jiffies, timeout)) {
480 printk("BUG: CPU#%d started up but did not get a callout!\n",
485 /* Allow the master to continue. */
486 cpumask_set_cpu(cpu_id, &cpu_callin_map);
489 static void __init smp_online(void)
491 int cpu_id = smp_processor_id();
493 notify_cpu_starting(cpu_id);
497 /* Get our bogomips. */
500 /* Save our processor parameters */
501 smp_store_cpu_info(cpu_id);
503 set_cpu_online(cpu_id, true);
506 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
507 /* Boot up CPUs common Routines */
508 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
509 static void __init show_mp_info(int nr_cpu)
512 char cpu_model0[17], cpu_model1[17], cpu_ver[9];
514 strncpy(cpu_model0, (char *)M32R_FPGA_CPU_NAME_ADDR, 16);
515 strncpy(cpu_model1, (char *)M32R_FPGA_MODEL_ID_ADDR, 16);
516 strncpy(cpu_ver, (char *)M32R_FPGA_VERSION_ADDR, 8);
518 cpu_model0[16] = '\0';
519 for (i = 15 ; i >= 0 ; i--) {
520 if (cpu_model0[i] != ' ')
522 cpu_model0[i] = '\0';
524 cpu_model1[16] = '\0';
525 for (i = 15 ; i >= 0 ; i--) {
526 if (cpu_model1[i] != ' ')
528 cpu_model1[i] = '\0';
531 for (i = 7 ; i >= 0 ; i--) {
532 if (cpu_ver[i] != ' ')
537 printk(KERN_INFO "M32R-mp information\n");
538 printk(KERN_INFO " On-chip CPUs : %d\n", nr_cpu);
539 printk(KERN_INFO " CPU model : %s/%s(%s)\n", cpu_model0,
540 cpu_model1, cpu_ver);
544 * The bootstrap kernel entry code has set these up. Save them for
547 static void __init smp_store_cpu_info(int cpu_id)
549 struct cpuinfo_m32r *ci = cpu_data + cpu_id;
552 ci->loops_per_jiffy = loops_per_jiffy;
555 static void __init show_cpu_info(int cpu_id)
557 struct cpuinfo_m32r *ci = &cpu_data[cpu_id];
559 printk("CPU#%d : ", cpu_id);
561 #define PRINT_CLOCK(name, value) \
562 printk(name " clock %d.%02dMHz", \
563 ((value) / 1000000), ((value) % 1000000) / 10000)
565 PRINT_CLOCK("CPU", (int)ci->cpu_clock);
566 PRINT_CLOCK(", Bus", (int)ci->bus_clock);
567 printk(", loops_per_jiffy[%ld]\n", ci->loops_per_jiffy);
571 * the frequency of the profiling timer can be changed
572 * by writing a multiplier value into /proc/profile.
574 int setup_profiling_timer(unsigned int multiplier)
579 * Sanity check. [at least 500 APIC cycles should be
580 * between APIC interrupts as a rule of thumb, to avoid
583 if ( (!multiplier) || (calibration_result / multiplier < 500))
587 * Set the new multiplier for each CPU. CPUs don't start using the
588 * new values until the next timer interrupt in which they do process
589 * accounting. At that time they also adjust their APIC timers
592 for_each_possible_cpu(i)
593 per_cpu(prof_multiplier, i) = multiplier;
598 /* Initialize all maps between cpu number and apicids */
599 static void __init init_cpu_to_physid(void)
603 for (i = 0 ; i < NR_CPUS ; i++) {
604 cpu_2_physid[i] = -1;
605 physid_2_cpu[i] = -1;
610 * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
611 * else physical apic ids
613 static void __init map_cpu_to_physid(int cpu_id, int phys_id)
615 physid_2_cpu[phys_id] = cpu_id;
616 cpu_2_physid[cpu_id] = phys_id;
620 * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
621 * else physical apic ids
623 static void __init unmap_cpu_to_physid(int cpu_id, int phys_id)
625 physid_2_cpu[phys_id] = -1;
626 cpu_2_physid[cpu_id] = -1;