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