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/module.h>
43 #include <linux/init.h>
44 #include <linux/kernel.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
;
75 EXPORT_SYMBOL(cpu_online_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
);
82 cpumask_t cpu_possible_map
= CPU_MASK_ALL
;
83 EXPORT_SYMBOL(cpu_possible_map
);
85 /* Per CPU bogomips and other parameters */
86 struct cpuinfo_m32r cpu_data
[NR_CPUS
] __cacheline_aligned
;
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
];
98 #define physid_to_cpu(physid) physid_2_cpu[physid]
100 /* which logical CPU number maps to which physical ID */
101 volatile int cpu_2_physid
[NR_CPUS
];
103 DEFINE_PER_CPU(int, prof_multiplier
) = 1;
104 DEFINE_PER_CPU(int, prof_old_multiplier
) = 1;
105 DEFINE_PER_CPU(int, prof_counter
) = 1;
107 spinlock_t ipi_lock
[NR_IPIS
];
109 static unsigned int calibration_result
;
111 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
112 /* Function Prototypes */
113 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
115 void smp_prepare_boot_cpu(void);
116 void smp_prepare_cpus(unsigned int);
117 static void init_ipi_lock(void);
118 static void do_boot_cpu(int);
119 int __cpu_up(unsigned int);
120 void smp_cpus_done(unsigned int);
122 int start_secondary(void *);
123 static void smp_callin(void);
124 static void smp_online(void);
126 static void show_mp_info(int);
127 static void smp_store_cpu_info(int);
128 static void show_cpu_info(int);
129 int setup_profiling_timer(unsigned int);
130 static void init_cpu_to_physid(void);
131 static void map_cpu_to_physid(int, int);
132 static void unmap_cpu_to_physid(int, int);
134 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
135 /* Boot up APs Routins : BSP */
136 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
137 void __devinit
smp_prepare_boot_cpu(void)
139 bsp_phys_id
= hard_smp_processor_id();
140 physid_set(bsp_phys_id
, phys_cpu_present_map
);
141 cpu_set(0, cpu_online_map
); /* BSP's cpu_id == 0 */
142 cpu_set(0, cpu_callout_map
);
143 cpu_set(0, cpu_callin_map
);
146 * Initialize the logical to physical CPU number mapping
148 init_cpu_to_physid();
149 map_cpu_to_physid(0, bsp_phys_id
);
150 current_thread_info()->cpu
= 0;
153 /*==========================================================================*
154 * Name: smp_prepare_cpus (old smp_boot_cpus)
156 * Description: This routine boot up APs.
158 * Born on Date: 2002.02.05
162 * Returns: void (cannot fail)
165 * Date Who Description
166 * ---------- --- --------------------------------------------------------
167 * 2003-06-24 hy modify for linux-2.5.69
169 *==========================================================================*/
170 void __init
smp_prepare_cpus(unsigned int max_cpus
)
173 unsigned long nr_cpu
;
175 nr_cpu
= inl(M32R_FPGA_NUM_OF_CPUS_PORTL
);
176 if (nr_cpu
> NR_CPUS
) {
177 printk(KERN_INFO
"NUM_OF_CPUS reg. value [%ld] > NR_CPU [%d]",
181 for (phys_id
= 0 ; phys_id
< nr_cpu
; phys_id
++)
182 physid_set(phys_id
, phys_cpu_present_map
);
183 #ifndef CONFIG_HOTPLUG_CPU
184 cpu_present_map
= cpu_possible_map
;
187 show_mp_info(nr_cpu
);
192 * Setup boot CPU information
194 smp_store_cpu_info(0); /* Final full version of the data */
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");
239 * init_ipi_lock : Initialize IPI locks.
241 static void __init
init_ipi_lock(void)
245 for (ipi
= 0 ; ipi
< NR_IPIS
; ipi
++)
246 spin_lock_init(&ipi_lock
[ipi
]);
249 /*==========================================================================*
252 * Description: This routine boot up one AP.
254 * Born on Date: 2002.02.05
256 * Arguments: phys_id - Target CPU physical ID
258 * Returns: void (cannot fail)
261 * Date Who Description
262 * ---------- --- --------------------------------------------------------
263 * 2003-06-24 hy modify for linux-2.5.69
265 *==========================================================================*/
266 static void __init
do_boot_cpu(int phys_id
)
268 struct task_struct
*idle
;
269 unsigned long send_status
, boot_status
;
275 * We can't use kernel_thread since we must avoid to
276 * reschedule the child.
278 idle
= fork_idle(cpu_id
);
280 panic("failed fork for CPU#%d.", cpu_id
);
282 idle
->thread
.lr
= (unsigned long)start_secondary
;
284 map_cpu_to_physid(cpu_id
, phys_id
);
286 /* So we see what's up */
287 printk("Booting processor %d/%d\n", phys_id
, cpu_id
);
288 stack_start
.spi
= (void *)idle
->thread
.sp
;
289 task_thread_info(idle
)->cpu
= cpu_id
;
293 * 1.IPI received by CPU#(phys_id).
294 * 2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
295 * 3.CPU#(phys_id) enter start_secondary()
300 cpu_set(phys_id
, cpu_bootout_map
);
302 /* Send Startup IPI */
303 send_IPI_mask_phys(cpumask_of_cpu(phys_id
), CPU_BOOT_IPI
, 0);
305 Dprintk("Waiting for send to finish...\n");
312 send_status
= !cpu_isset(phys_id
, cpu_bootin_map
);
313 } while (send_status
&& (timeout
++ < 100));
315 Dprintk("After Startup.\n");
319 * allow APs to start initializing.
321 Dprintk("Before Callout %d.\n", cpu_id
);
322 cpu_set(cpu_id
, cpu_callout_map
);
323 Dprintk("After Callout %d.\n", cpu_id
);
326 * Wait 5s total for a response
328 for (timeout
= 0; timeout
< 5000; timeout
++) {
329 if (cpu_isset(cpu_id
, cpu_callin_map
))
330 break; /* It has booted */
334 if (cpu_isset(cpu_id
, cpu_callin_map
)) {
335 /* number CPUs logically, starting from 1 (BSP is 0) */
339 printk("Not responding.\n");
342 printk("IPI never delivered???\n");
344 if (send_status
|| boot_status
) {
345 unmap_cpu_to_physid(cpu_id
, phys_id
);
346 cpu_clear(cpu_id
, cpu_callout_map
);
347 cpu_clear(cpu_id
, cpu_callin_map
);
348 cpu_clear(cpu_id
, cpu_initialized
);
353 int __cpuinit
__cpu_up(unsigned int cpu_id
)
357 cpu_set(cpu_id
, smp_commenced_mask
);
360 * Wait 5s total for a response
362 for (timeout
= 0; timeout
< 5000; timeout
++) {
363 if (cpu_isset(cpu_id
, cpu_online_map
))
367 if (!cpu_isset(cpu_id
, cpu_online_map
))
373 void __init
smp_cpus_done(unsigned int max_cpus
)
376 unsigned long bogosum
= 0;
378 for (timeout
= 0; timeout
< 5000; timeout
++) {
379 if (cpus_equal(cpu_callin_map
, cpu_online_map
))
383 if (!cpus_equal(cpu_callin_map
, cpu_online_map
))
386 for (cpu_id
= 0 ; cpu_id
< num_online_cpus() ; cpu_id
++)
387 show_cpu_info(cpu_id
);
390 * Allow the user to impress friends.
392 Dprintk("Before bogomips.\n");
394 for_each_cpu_mask(cpu_id
, cpu_online_map
)
395 bogosum
+= cpu_data
[cpu_id
].loops_per_jiffy
;
397 printk(KERN_INFO
"Total of %d processors activated " \
398 "(%lu.%02lu BogoMIPS).\n", cpucount
+ 1,
399 bogosum
/ (500000 / HZ
),
400 (bogosum
/ (5000 / HZ
)) % 100);
401 Dprintk("Before bogocount - setting activated=1.\n");
405 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
406 /* Activate a secondary processor Routins */
407 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
409 /*==========================================================================*
410 * Name: start_secondary
412 * Description: This routine activate a secondary processor.
414 * Born on Date: 2002.02.05
416 * Arguments: *unused - currently unused.
418 * Returns: void (cannot fail)
421 * Date Who Description
422 * ---------- --- --------------------------------------------------------
423 * 2003-06-24 hy modify for linux-2.5.69
425 *==========================================================================*/
426 int __init
start_secondary(void *unused
)
431 while (!cpu_isset(smp_processor_id(), smp_commenced_mask
))
437 * low-memory mappings have been cleared, flush them from
438 * the local TLBs too.
440 local_flush_tlb_all();
446 /*==========================================================================*
449 * Description: This routine activate a secondary processor.
451 * Born on Date: 2002.02.05
455 * Returns: void (cannot fail)
458 * Date Who Description
459 * ---------- --- --------------------------------------------------------
460 * 2003-06-24 hy modify for linux-2.5.69
462 *==========================================================================*/
463 static void __init
smp_callin(void)
465 int phys_id
= hard_smp_processor_id();
466 int cpu_id
= smp_processor_id();
467 unsigned long timeout
;
469 if (cpu_isset(cpu_id
, cpu_callin_map
)) {
470 printk("huh, phys CPU#%d, CPU#%d already present??\n",
474 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id
, phys_id
);
476 /* Waiting 2s total for startup (udelay is not yet working) */
477 timeout
= jiffies
+ (2 * HZ
);
478 while (time_before(jiffies
, timeout
)) {
479 /* Has the boot CPU finished it's STARTUP sequence ? */
480 if (cpu_isset(cpu_id
, cpu_callout_map
))
485 if (!time_before(jiffies
, timeout
)) {
486 printk("BUG: CPU#%d started up but did not get a callout!\n",
491 /* Allow the master to continue. */
492 cpu_set(cpu_id
, cpu_callin_map
);
495 static void __init
smp_online(void)
497 int cpu_id
= smp_processor_id();
501 /* Get our bogomips. */
504 /* Save our processor parameters */
505 smp_store_cpu_info(cpu_id
);
507 cpu_set(cpu_id
, cpu_online_map
);
510 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
511 /* Boot up CPUs common Routins */
512 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
513 static void __init
show_mp_info(int nr_cpu
)
516 char cpu_model0
[17], cpu_model1
[17], cpu_ver
[9];
518 strncpy(cpu_model0
, (char *)M32R_FPGA_CPU_NAME_ADDR
, 16);
519 strncpy(cpu_model1
, (char *)M32R_FPGA_MODEL_ID_ADDR
, 16);
520 strncpy(cpu_ver
, (char *)M32R_FPGA_VERSION_ADDR
, 8);
522 cpu_model0
[16] = '\0';
523 for (i
= 15 ; i
>= 0 ; i
--) {
524 if (cpu_model0
[i
] != ' ')
526 cpu_model0
[i
] = '\0';
528 cpu_model1
[16] = '\0';
529 for (i
= 15 ; i
>= 0 ; i
--) {
530 if (cpu_model1
[i
] != ' ')
532 cpu_model1
[i
] = '\0';
535 for (i
= 7 ; i
>= 0 ; i
--) {
536 if (cpu_ver
[i
] != ' ')
541 printk(KERN_INFO
"M32R-mp information\n");
542 printk(KERN_INFO
" On-chip CPUs : %d\n", nr_cpu
);
543 printk(KERN_INFO
" CPU model : %s/%s(%s)\n", cpu_model0
,
544 cpu_model1
, cpu_ver
);
548 * The bootstrap kernel entry code has set these up. Save them for
551 static void __init
smp_store_cpu_info(int cpu_id
)
553 struct cpuinfo_m32r
*ci
= cpu_data
+ cpu_id
;
556 ci
->loops_per_jiffy
= loops_per_jiffy
;
559 static void __init
show_cpu_info(int cpu_id
)
561 struct cpuinfo_m32r
*ci
= &cpu_data
[cpu_id
];
563 printk("CPU#%d : ", cpu_id
);
565 #define PRINT_CLOCK(name, value) \
566 printk(name " clock %d.%02dMHz", \
567 ((value) / 1000000), ((value) % 1000000) / 10000)
569 PRINT_CLOCK("CPU", (int)ci
->cpu_clock
);
570 PRINT_CLOCK(", Bus", (int)ci
->bus_clock
);
571 printk(", loops_per_jiffy[%ld]\n", ci
->loops_per_jiffy
);
575 * the frequency of the profiling timer can be changed
576 * by writing a multiplier value into /proc/profile.
578 int setup_profiling_timer(unsigned int multiplier
)
583 * Sanity check. [at least 500 APIC cycles should be
584 * between APIC interrupts as a rule of thumb, to avoid
587 if ( (!multiplier
) || (calibration_result
/ multiplier
< 500))
591 * Set the new multiplier for each CPU. CPUs don't start using the
592 * new values until the next timer interrupt in which they do process
593 * accounting. At that time they also adjust their APIC timers
596 for (i
= 0; i
< NR_CPUS
; ++i
)
597 per_cpu(prof_multiplier
, i
) = multiplier
;
602 /* Initialize all maps between cpu number and apicids */
603 static void __init
init_cpu_to_physid(void)
607 for (i
= 0 ; i
< NR_CPUS
; i
++) {
608 cpu_2_physid
[i
] = -1;
609 physid_2_cpu
[i
] = -1;
614 * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
615 * else physical apic ids
617 static void __init
map_cpu_to_physid(int cpu_id
, int phys_id
)
619 physid_2_cpu
[phys_id
] = cpu_id
;
620 cpu_2_physid
[cpu_id
] = phys_id
;
624 * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
625 * else physical apic ids
627 static void __init
unmap_cpu_to_physid(int cpu_id
, int phys_id
)
629 physid_2_cpu
[phys_id
] = -1;
630 cpu_2_physid
[cpu_id
] = -1;