xref: /openbmc/linux/arch/ia64/kernel/smpboot.c (revision 5eda7861)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * SMP boot-related support
31da177e4SLinus Torvalds  *
482975115SDavid Mosberger-Tang  * Copyright (C) 1998-2003, 2005 Hewlett-Packard Co
51da177e4SLinus Torvalds  *	David Mosberger-Tang <davidm@hpl.hp.com>
6e927ecb0SSuresh Siddha  * Copyright (C) 2001, 2004-2005 Intel Corp
7e927ecb0SSuresh Siddha  * 	Rohit Seth <rohit.seth@intel.com>
8e927ecb0SSuresh Siddha  * 	Suresh Siddha <suresh.b.siddha@intel.com>
9e927ecb0SSuresh Siddha  * 	Gordon Jin <gordon.jin@intel.com>
10e927ecb0SSuresh Siddha  *	Ashok Raj  <ashok.raj@intel.com>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * 01/05/16 Rohit Seth <rohit.seth@intel.com>	Moved SMP booting functions from smp.c to here.
131da177e4SLinus Torvalds  * 01/04/27 David Mosberger <davidm@hpl.hp.com>	Added ITC synching code.
141da177e4SLinus Torvalds  * 02/07/31 David Mosberger <davidm@hpl.hp.com>	Switch over to hotplug-CPU boot-sequence.
151da177e4SLinus Torvalds  *						smp_boot_cpus()/smp_commence() is replaced by
161da177e4SLinus Torvalds  *						smp_prepare_cpus()/__cpu_up()/smp_cpus_done().
17b8d8b883SAshok Raj  * 04/06/21 Ashok Raj		<ashok.raj@intel.com> Added CPU Hotplug Support
18e927ecb0SSuresh Siddha  * 04/12/26 Jin Gordon <gordon.jin@intel.com>
19e927ecb0SSuresh Siddha  * 04/12/26 Rohit Seth <rohit.seth@intel.com>
20e927ecb0SSuresh Siddha  *						Add multi-threading and multi-core detection
21e927ecb0SSuresh Siddha  * 05/01/30 Suresh Siddha <suresh.b.siddha@intel.com>
22e927ecb0SSuresh Siddha  *						Setup cpu_sibling_map and cpu_core_map
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <linux/module.h>
261da177e4SLinus Torvalds #include <linux/acpi.h>
271da177e4SLinus Torvalds #include <linux/bootmem.h>
281da177e4SLinus Torvalds #include <linux/cpu.h>
291da177e4SLinus Torvalds #include <linux/delay.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds #include <linux/irq.h>
331da177e4SLinus Torvalds #include <linux/kernel.h>
341da177e4SLinus Torvalds #include <linux/kernel_stat.h>
351da177e4SLinus Torvalds #include <linux/mm.h>
361da177e4SLinus Torvalds #include <linux/notifier.h>
371da177e4SLinus Torvalds #include <linux/smp.h>
381da177e4SLinus Torvalds #include <linux/spinlock.h>
391da177e4SLinus Torvalds #include <linux/efi.h>
401da177e4SLinus Torvalds #include <linux/percpu.h>
411da177e4SLinus Torvalds #include <linux/bitops.h>
421da177e4SLinus Torvalds 
4360063497SArun Sharma #include <linux/atomic.h>
441da177e4SLinus Torvalds #include <asm/cache.h>
451da177e4SLinus Torvalds #include <asm/current.h>
461da177e4SLinus Torvalds #include <asm/delay.h>
471da177e4SLinus Torvalds #include <asm/io.h>
481da177e4SLinus Torvalds #include <asm/irq.h>
491da177e4SLinus Torvalds #include <asm/machvec.h>
501da177e4SLinus Torvalds #include <asm/mca.h>
511da177e4SLinus Torvalds #include <asm/page.h>
52e51835d5SIsaku Yamahata #include <asm/paravirt.h>
531da177e4SLinus Torvalds #include <asm/pgalloc.h>
541da177e4SLinus Torvalds #include <asm/pgtable.h>
551da177e4SLinus Torvalds #include <asm/processor.h>
561da177e4SLinus Torvalds #include <asm/ptrace.h>
571da177e4SLinus Torvalds #include <asm/sal.h>
581da177e4SLinus Torvalds #include <asm/tlbflush.h>
591da177e4SLinus Torvalds #include <asm/unistd.h>
606e9de181SJohn Keller #include <asm/sn/arch.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define SMP_DEBUG 0
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #if SMP_DEBUG
651da177e4SLinus Torvalds #define Dprintk(x...)  printk(x)
661da177e4SLinus Torvalds #else
671da177e4SLinus Torvalds #define Dprintk(x...)
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds 
70b8d8b883SAshok Raj #ifdef CONFIG_HOTPLUG_CPU
71ff741906SAshok Raj #ifdef CONFIG_PERMIT_BSP_REMOVE
72ff741906SAshok Raj #define bsp_remove_ok	1
73ff741906SAshok Raj #else
74ff741906SAshok Raj #define bsp_remove_ok	0
75ff741906SAshok Raj #endif
76ff741906SAshok Raj 
77b8d8b883SAshok Raj /*
78b8d8b883SAshok Raj  * Global array allocated for NR_CPUS at boot time
79b8d8b883SAshok Raj  */
80b8d8b883SAshok Raj struct sal_to_os_boot sal_boot_rendez_state[NR_CPUS];
81b8d8b883SAshok Raj 
82b8d8b883SAshok Raj /*
83b8d8b883SAshok Raj  * start_ap in head.S uses this to store current booting cpu
84b8d8b883SAshok Raj  * info.
85b8d8b883SAshok Raj  */
86b8d8b883SAshok Raj struct sal_to_os_boot *sal_state_for_booting_cpu = &sal_boot_rendez_state[0];
87b8d8b883SAshok Raj 
88b8d8b883SAshok Raj #define set_brendez_area(x) (sal_state_for_booting_cpu = &sal_boot_rendez_state[(x)]);
89b8d8b883SAshok Raj 
90b8d8b883SAshok Raj #else
91b8d8b883SAshok Raj #define set_brendez_area(x)
92b8d8b883SAshok Raj #endif
93b8d8b883SAshok Raj 
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /*
961da177e4SLinus Torvalds  * ITC synchronization related stuff:
971da177e4SLinus Torvalds  */
98ff741906SAshok Raj #define MASTER	(0)
991da177e4SLinus Torvalds #define SLAVE	(SMP_CACHE_BYTES/8)
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #define NUM_ROUNDS	64	/* magic value */
1021da177e4SLinus Torvalds #define NUM_ITERS	5	/* likewise */
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static DEFINE_SPINLOCK(itc_sync_lock);
1051da177e4SLinus Torvalds static volatile unsigned long go[SLAVE + 1];
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define DEBUG_ITC_SYNC	0
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds extern void start_ap (void);
1101da177e4SLinus Torvalds extern unsigned long ia64_iobase;
1111da177e4SLinus Torvalds 
11236c8b586SIngo Molnar struct task_struct *task_for_booting_cpu;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * State for each CPU
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds DEFINE_PER_CPU(int, cpu_state);
1181da177e4SLinus Torvalds 
119e927ecb0SSuresh Siddha cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
12042aca483SRobin Holt EXPORT_SYMBOL(cpu_core_map);
121d5a7430dSMike Travis DEFINE_PER_CPU_SHARED_ALIGNED(cpumask_t, cpu_sibling_map);
122d5a7430dSMike Travis EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
123d5a7430dSMike Travis 
124e927ecb0SSuresh Siddha int smp_num_siblings = 1;
125e927ecb0SSuresh Siddha 
1261da177e4SLinus Torvalds /* which logical CPU number maps to which CPU (physical APIC ID) */
1271da177e4SLinus Torvalds volatile int ia64_cpu_to_sapicid[NR_CPUS];
1281da177e4SLinus Torvalds EXPORT_SYMBOL(ia64_cpu_to_sapicid);
1291da177e4SLinus Torvalds 
1305eda7861SRusty Russell static cpumask_t cpu_callin_map;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds struct smp_boot_data smp_boot_data __initdata;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds char __initdata no_int_routing;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */
1391da177e4SLinus Torvalds 
140ff741906SAshok Raj #ifdef CONFIG_FORCE_CPEI_RETARGET
141ff741906SAshok Raj #define CPEI_OVERRIDE_DEFAULT	(1)
142ff741906SAshok Raj #else
143ff741906SAshok Raj #define CPEI_OVERRIDE_DEFAULT	(0)
144ff741906SAshok Raj #endif
145ff741906SAshok Raj 
146ff741906SAshok Raj unsigned int force_cpei_retarget = CPEI_OVERRIDE_DEFAULT;
147ff741906SAshok Raj 
148ff741906SAshok Raj static int __init
149ff741906SAshok Raj cmdl_force_cpei(char *str)
150ff741906SAshok Raj {
151ff741906SAshok Raj 	int value=0;
152ff741906SAshok Raj 
153ff741906SAshok Raj 	get_option (&str, &value);
154ff741906SAshok Raj 	force_cpei_retarget = value;
155ff741906SAshok Raj 
156ff741906SAshok Raj 	return 1;
157ff741906SAshok Raj }
158ff741906SAshok Raj 
159ff741906SAshok Raj __setup("force_cpei=", cmdl_force_cpei);
160ff741906SAshok Raj 
1611da177e4SLinus Torvalds static int __init
1621da177e4SLinus Torvalds nointroute (char *str)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	no_int_routing = 1;
1651da177e4SLinus Torvalds 	printk ("no_int_routing on\n");
1661da177e4SLinus Torvalds 	return 1;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds __setup("nointroute", nointroute);
1701da177e4SLinus Torvalds 
171ff741906SAshok Raj static void fix_b0_for_bsp(void)
172ff741906SAshok Raj {
173ff741906SAshok Raj #ifdef CONFIG_HOTPLUG_CPU
174ff741906SAshok Raj 	int cpuid;
175ff741906SAshok Raj 	static int fix_bsp_b0 = 1;
176ff741906SAshok Raj 
177ff741906SAshok Raj 	cpuid = smp_processor_id();
178ff741906SAshok Raj 
179ff741906SAshok Raj 	/*
180ff741906SAshok Raj 	 * Cache the b0 value on the first AP that comes up
181ff741906SAshok Raj 	 */
182ff741906SAshok Raj 	if (!(fix_bsp_b0 && cpuid))
183ff741906SAshok Raj 		return;
184ff741906SAshok Raj 
185ff741906SAshok Raj 	sal_boot_rendez_state[0].br[0] = sal_boot_rendez_state[cpuid].br[0];
186ff741906SAshok Raj 	printk ("Fixed BSP b0 value from CPU %d\n", cpuid);
187ff741906SAshok Raj 
188ff741906SAshok Raj 	fix_bsp_b0 = 0;
189ff741906SAshok Raj #endif
190ff741906SAshok Raj }
191ff741906SAshok Raj 
1921da177e4SLinus Torvalds void
1931da177e4SLinus Torvalds sync_master (void *arg)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	unsigned long flags, i;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	go[MASTER] = 0;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	local_irq_save(flags);
2001da177e4SLinus Torvalds 	{
2011da177e4SLinus Torvalds 		for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
20282975115SDavid Mosberger-Tang 			while (!go[MASTER])
20382975115SDavid Mosberger-Tang 				cpu_relax();
2041da177e4SLinus Torvalds 			go[MASTER] = 0;
2051da177e4SLinus Torvalds 			go[SLAVE] = ia64_get_itc();
2061da177e4SLinus Torvalds 		}
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 	local_irq_restore(flags);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  * Return the number of cycles by which our itc differs from the itc on the master
2131da177e4SLinus Torvalds  * (time-keeper) CPU.  A positive number indicates our itc is ahead of the master,
2141da177e4SLinus Torvalds  * negative that it is behind.
2151da177e4SLinus Torvalds  */
2161da177e4SLinus Torvalds static inline long
2171da177e4SLinus Torvalds get_delta (long *rt, long *master)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
2201da177e4SLinus Torvalds 	unsigned long tcenter, t0, t1, tm;
2211da177e4SLinus Torvalds 	long i;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	for (i = 0; i < NUM_ITERS; ++i) {
2241da177e4SLinus Torvalds 		t0 = ia64_get_itc();
2251da177e4SLinus Torvalds 		go[MASTER] = 1;
22682975115SDavid Mosberger-Tang 		while (!(tm = go[SLAVE]))
22782975115SDavid Mosberger-Tang 			cpu_relax();
2281da177e4SLinus Torvalds 		go[SLAVE] = 0;
2291da177e4SLinus Torvalds 		t1 = ia64_get_itc();
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		if (t1 - t0 < best_t1 - best_t0)
2321da177e4SLinus Torvalds 			best_t0 = t0, best_t1 = t1, best_tm = tm;
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	*rt = best_t1 - best_t0;
2361da177e4SLinus Torvalds 	*master = best_tm - best_t0;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* average best_t0 and best_t1 without overflow: */
2391da177e4SLinus Torvalds 	tcenter = (best_t0/2 + best_t1/2);
2401da177e4SLinus Torvalds 	if (best_t0 % 2 + best_t1 % 2 == 2)
2411da177e4SLinus Torvalds 		++tcenter;
2421da177e4SLinus Torvalds 	return tcenter - best_tm;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds /*
2461da177e4SLinus Torvalds  * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU
2471da177e4SLinus Torvalds  * (normally the time-keeper CPU).  We use a closed loop to eliminate the possibility of
2481da177e4SLinus Torvalds  * unaccounted-for errors (such as getting a machine check in the middle of a calibration
2491da177e4SLinus Torvalds  * step).  The basic idea is for the slave to ask the master what itc value it has and to
2501da177e4SLinus Torvalds  * read its own itc before and after the master responds.  Each iteration gives us three
2511da177e4SLinus Torvalds  * timestamps:
2521da177e4SLinus Torvalds  *
2531da177e4SLinus Torvalds  *	slave		master
2541da177e4SLinus Torvalds  *
2551da177e4SLinus Torvalds  *	t0 ---\
2561da177e4SLinus Torvalds  *             ---\
2571da177e4SLinus Torvalds  *		   --->
2581da177e4SLinus Torvalds  *			tm
2591da177e4SLinus Torvalds  *		   /---
2601da177e4SLinus Torvalds  *	       /---
2611da177e4SLinus Torvalds  *	t1 <---
2621da177e4SLinus Torvalds  *
2631da177e4SLinus Torvalds  *
2641da177e4SLinus Torvalds  * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0
2651da177e4SLinus Torvalds  * and t1.  If we achieve this, the clocks are synchronized provided the interconnect
2661da177e4SLinus Torvalds  * between the slave and the master is symmetric.  Even if the interconnect were
2671da177e4SLinus Torvalds  * asymmetric, we would still know that the synchronization error is smaller than the
2681da177e4SLinus Torvalds  * roundtrip latency (t0 - t1).
2691da177e4SLinus Torvalds  *
2701da177e4SLinus Torvalds  * When the interconnect is quiet and symmetric, this lets us synchronize the itc to
2711da177e4SLinus Torvalds  * within one or two cycles.  However, we can only *guarantee* that the synchronization is
2721da177e4SLinus Torvalds  * accurate to within a round-trip time, which is typically in the range of several
2731da177e4SLinus Torvalds  * hundred cycles (e.g., ~500 cycles).  In practice, this means that the itc's are usually
2741da177e4SLinus Torvalds  * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better
2751da177e4SLinus Torvalds  * than half a micro second or so.
2761da177e4SLinus Torvalds  */
2771da177e4SLinus Torvalds void
2781da177e4SLinus Torvalds ia64_sync_itc (unsigned int master)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	long i, delta, adj, adjust_latency = 0, done = 0;
2811da177e4SLinus Torvalds 	unsigned long flags, rt, master_time_stamp, bound;
2821da177e4SLinus Torvalds #if DEBUG_ITC_SYNC
2831da177e4SLinus Torvalds 	struct {
2841da177e4SLinus Torvalds 		long rt;	/* roundtrip time */
2851da177e4SLinus Torvalds 		long master;	/* master's timestamp */
2861da177e4SLinus Torvalds 		long diff;	/* difference between midpoint and master's timestamp */
2871da177e4SLinus Torvalds 		long lat;	/* estimate of itc adjustment latency */
2881da177e4SLinus Torvalds 	} t[NUM_ROUNDS];
2891da177e4SLinus Torvalds #endif
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/*
2921da177e4SLinus Torvalds 	 * Make sure local timer ticks are disabled while we sync.  If
2931da177e4SLinus Torvalds 	 * they were enabled, we'd have to worry about nasty issues
2941da177e4SLinus Torvalds 	 * like setting the ITC ahead of (or a long time before) the
2951da177e4SLinus Torvalds 	 * next scheduled tick.
2961da177e4SLinus Torvalds 	 */
2971da177e4SLinus Torvalds 	BUG_ON((ia64_get_itv() & (1 << 16)) == 0);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	go[MASTER] = 1;
3001da177e4SLinus Torvalds 
3018691e5a8SJens Axboe 	if (smp_call_function_single(master, sync_master, NULL, 0) < 0) {
3021da177e4SLinus Torvalds 		printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master);
3031da177e4SLinus Torvalds 		return;
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 
30682975115SDavid Mosberger-Tang 	while (go[MASTER])
30782975115SDavid Mosberger-Tang 		cpu_relax();	/* wait for master to be ready */
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	spin_lock_irqsave(&itc_sync_lock, flags);
3101da177e4SLinus Torvalds 	{
3111da177e4SLinus Torvalds 		for (i = 0; i < NUM_ROUNDS; ++i) {
3121da177e4SLinus Torvalds 			delta = get_delta(&rt, &master_time_stamp);
3131da177e4SLinus Torvalds 			if (delta == 0) {
3141da177e4SLinus Torvalds 				done = 1;	/* let's lock on to this... */
3151da177e4SLinus Torvalds 				bound = rt;
3161da177e4SLinus Torvalds 			}
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 			if (!done) {
3191da177e4SLinus Torvalds 				if (i > 0) {
3201da177e4SLinus Torvalds 					adjust_latency += -delta;
3211da177e4SLinus Torvalds 					adj = -delta + adjust_latency/4;
3221da177e4SLinus Torvalds 				} else
3231da177e4SLinus Torvalds 					adj = -delta;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 				ia64_set_itc(ia64_get_itc() + adj);
3261da177e4SLinus Torvalds 			}
3271da177e4SLinus Torvalds #if DEBUG_ITC_SYNC
3281da177e4SLinus Torvalds 			t[i].rt = rt;
3291da177e4SLinus Torvalds 			t[i].master = master_time_stamp;
3301da177e4SLinus Torvalds 			t[i].diff = delta;
3311da177e4SLinus Torvalds 			t[i].lat = adjust_latency/4;
3321da177e4SLinus Torvalds #endif
3331da177e4SLinus Torvalds 		}
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 	spin_unlock_irqrestore(&itc_sync_lock, flags);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds #if DEBUG_ITC_SYNC
3381da177e4SLinus Torvalds 	for (i = 0; i < NUM_ROUNDS; ++i)
3391da177e4SLinus Torvalds 		printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
3401da177e4SLinus Torvalds 		       t[i].rt, t[i].master, t[i].diff, t[i].lat);
3411da177e4SLinus Torvalds #endif
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, "
3441da177e4SLinus Torvalds 	       "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*
3481da177e4SLinus Torvalds  * Ideally sets up per-cpu profiling hooks.  Doesn't do much now...
3491da177e4SLinus Torvalds  */
3505b5e76e9SGreg Kroah-Hartman static inline void smp_setup_percpu_timer(void)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
354ccce9bb8SPaul Gortmaker static void
3551da177e4SLinus Torvalds smp_callin (void)
3561da177e4SLinus Torvalds {
357ff741906SAshok Raj 	int cpuid, phys_id, itc_master;
358ead6caaeSJack Steiner 	struct cpuinfo_ia64 *last_cpuinfo, *this_cpuinfo;
3591da177e4SLinus Torvalds 	extern void ia64_init_itm(void);
360ff741906SAshok Raj 	extern volatile int time_keeper_id;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds #ifdef CONFIG_PERFMON
3631da177e4SLinus Torvalds 	extern void pfm_init_percpu(void);
3641da177e4SLinus Torvalds #endif
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	cpuid = smp_processor_id();
3671da177e4SLinus Torvalds 	phys_id = hard_smp_processor_id();
368ff741906SAshok Raj 	itc_master = time_keeper_id;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if (cpu_online(cpuid)) {
3711da177e4SLinus Torvalds 		printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
3721da177e4SLinus Torvalds 		       phys_id, cpuid);
3731da177e4SLinus Torvalds 		BUG();
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
376ff741906SAshok Raj 	fix_b0_for_bsp();
377ff741906SAshok Raj 
3783bccd996SLee Schermerhorn 	/*
3793bccd996SLee Schermerhorn 	 * numa_node_id() works after this.
3803bccd996SLee Schermerhorn 	 */
3813bccd996SLee Schermerhorn 	set_numa_node(cpu_to_node_map[cpuid]);
382fd1197f1SLee Schermerhorn 	set_numa_mem(local_memory_node(cpu_to_node_map[cpuid]));
3833bccd996SLee Schermerhorn 
384e1b30a39SYasuaki Ishimatsu 	spin_lock(&vector_lock);
385e1b30a39SYasuaki Ishimatsu 	/* Setup the per cpu irq handling data structures */
386e1b30a39SYasuaki Ishimatsu 	__setup_vector_irq(cpuid);
387e545a614SManfred Spraul 	notify_cpu_starting(cpuid);
3887d7f9848SSrivatsa S. Bhat 	set_cpu_online(cpuid, true);
389a9fa06c2SShaohua Li 	per_cpu(cpu_state, cpuid) = CPU_ONLINE;
390e1b30a39SYasuaki Ishimatsu 	spin_unlock(&vector_lock);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	smp_setup_percpu_timer();
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	ia64_mca_cmc_vector_setup();	/* Setup vector on AP */
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds #ifdef CONFIG_PERFMON
3971da177e4SLinus Torvalds 	pfm_init_percpu();
3981da177e4SLinus Torvalds #endif
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	local_irq_enable();
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
4031da177e4SLinus Torvalds 		/*
4041da177e4SLinus Torvalds 		 * Synchronize the ITC with the BP.  Need to do this after irqs are
4051da177e4SLinus Torvalds 		 * enabled because ia64_sync_itc() calls smp_call_function_single(), which
4061da177e4SLinus Torvalds 		 * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
4071da177e4SLinus Torvalds 		 * local_bh_enable(), which bugs out if irqs are not enabled...
4081da177e4SLinus Torvalds 		 */
409ff741906SAshok Raj 		Dprintk("Going to syncup ITC with ITC Master.\n");
410ff741906SAshok Raj 		ia64_sync_itc(itc_master);
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	/*
4141da177e4SLinus Torvalds 	 * Get our bogomips.
4151da177e4SLinus Torvalds 	 */
4161da177e4SLinus Torvalds 	ia64_init_itm();
417ead6caaeSJack Steiner 
418ead6caaeSJack Steiner 	/*
419ead6caaeSJack Steiner 	 * Delay calibration can be skipped if new processor is identical to the
420ead6caaeSJack Steiner 	 * previous processor.
421ead6caaeSJack Steiner 	 */
422ead6caaeSJack Steiner 	last_cpuinfo = cpu_data(cpuid - 1);
423ead6caaeSJack Steiner 	this_cpuinfo = local_cpu_data;
424ead6caaeSJack Steiner 	if (last_cpuinfo->itc_freq != this_cpuinfo->itc_freq ||
425ead6caaeSJack Steiner 	    last_cpuinfo->proc_freq != this_cpuinfo->proc_freq ||
426ead6caaeSJack Steiner 	    last_cpuinfo->features != this_cpuinfo->features ||
427ead6caaeSJack Steiner 	    last_cpuinfo->revision != this_cpuinfo->revision ||
428ead6caaeSJack Steiner 	    last_cpuinfo->family != this_cpuinfo->family ||
429ead6caaeSJack Steiner 	    last_cpuinfo->archrev != this_cpuinfo->archrev ||
430ead6caaeSJack Steiner 	    last_cpuinfo->model != this_cpuinfo->model)
4311da177e4SLinus Torvalds 		calibrate_delay();
4321da177e4SLinus Torvalds 	local_cpu_data->loops_per_jiffy = loops_per_jiffy;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/*
4351da177e4SLinus Torvalds 	 * Allow the master to continue.
4361da177e4SLinus Torvalds 	 */
4375d2068daSRusty Russell 	cpumask_set_cpu(cpuid, &cpu_callin_map);
4381da177e4SLinus Torvalds 	Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /*
4431da177e4SLinus Torvalds  * Activate a secondary processor.  head.S calls this.
4441da177e4SLinus Torvalds  */
445ccce9bb8SPaul Gortmaker int
4461da177e4SLinus Torvalds start_secondary (void *unused)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 	/* Early console may use I/O ports */
4491da177e4SLinus Torvalds 	ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
45010617bbeSTony Luck #ifndef CONFIG_PRINTK_TIME
4511da177e4SLinus Torvalds 	Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
45210617bbeSTony Luck #endif
4531da177e4SLinus Torvalds 	efi_map_pal_code();
4541da177e4SLinus Torvalds 	cpu_init();
4555bfb5d69SNick Piggin 	preempt_disable();
4561da177e4SLinus Torvalds 	smp_callin();
4571da177e4SLinus Torvalds 
45891d591c3SThomas Gleixner 	cpu_startup_entry(CPUHP_ONLINE);
4591da177e4SLinus Torvalds 	return 0;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
462ccce9bb8SPaul Gortmaker static int
46313583bf1SThomas Gleixner do_boot_cpu (int sapicid, int cpu, struct task_struct *idle)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	int timeout;
466b8d8b883SAshok Raj 
46713583bf1SThomas Gleixner 	task_for_booting_cpu = idle;
4681da177e4SLinus Torvalds 	Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);
4691da177e4SLinus Torvalds 
470b8d8b883SAshok Raj 	set_brendez_area(cpu);
4711da177e4SLinus Torvalds 	platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/*
4741da177e4SLinus Torvalds 	 * Wait 10s total for the AP to start
4751da177e4SLinus Torvalds 	 */
4761da177e4SLinus Torvalds 	Dprintk("Waiting on callin_map ...");
4771da177e4SLinus Torvalds 	for (timeout = 0; timeout < 100000; timeout++) {
4785d2068daSRusty Russell 		if (cpumask_test_cpu(cpu, &cpu_callin_map))
4791da177e4SLinus Torvalds 			break;  /* It has booted */
4805eda7861SRusty Russell 		barrier(); /* Make sure we re-read cpu_callin_map */
4811da177e4SLinus Torvalds 		udelay(100);
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	Dprintk("\n");
4841da177e4SLinus Torvalds 
4855d2068daSRusty Russell 	if (!cpumask_test_cpu(cpu, &cpu_callin_map)) {
4861da177e4SLinus Torvalds 		printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
4871da177e4SLinus Torvalds 		ia64_cpu_to_sapicid[cpu] = -1;
4887d7f9848SSrivatsa S. Bhat 		set_cpu_online(cpu, false);  /* was set in smp_callin() */
4891da177e4SLinus Torvalds 		return -EINVAL;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 	return 0;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds static int __init
4951da177e4SLinus Torvalds decay (char *str)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	int ticks;
4981da177e4SLinus Torvalds 	get_option (&str, &ticks);
4991da177e4SLinus Torvalds 	return 1;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds __setup("decay=", decay);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /*
5051da177e4SLinus Torvalds  * Initialize the logical CPU number to SAPICID mapping
5061da177e4SLinus Torvalds  */
5071da177e4SLinus Torvalds void __init
5081da177e4SLinus Torvalds smp_build_cpu_map (void)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	int sapicid, cpu, i;
5111da177e4SLinus Torvalds 	int boot_cpu_id = hard_smp_processor_id();
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	for (cpu = 0; cpu < NR_CPUS; cpu++) {
5141da177e4SLinus Torvalds 		ia64_cpu_to_sapicid[cpu] = -1;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	ia64_cpu_to_sapicid[0] = boot_cpu_id;
5187d7f9848SSrivatsa S. Bhat 	init_cpu_present(cpumask_of(0));
5192af51a3fSRusty Russell 	set_cpu_possible(0, true);
5201da177e4SLinus Torvalds 	for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
5211da177e4SLinus Torvalds 		sapicid = smp_boot_data.cpu_phys_id[i];
5221da177e4SLinus Torvalds 		if (sapicid == boot_cpu_id)
5231da177e4SLinus Torvalds 			continue;
5242af51a3fSRusty Russell 		set_cpu_present(cpu, true);
5252af51a3fSRusty Russell 		set_cpu_possible(cpu, true);
5261da177e4SLinus Torvalds 		ia64_cpu_to_sapicid[cpu] = sapicid;
5271da177e4SLinus Torvalds 		cpu++;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds /*
5321da177e4SLinus Torvalds  * Cycle through the APs sending Wakeup IPIs to boot each.
5331da177e4SLinus Torvalds  */
5341da177e4SLinus Torvalds void __init
5351da177e4SLinus Torvalds smp_prepare_cpus (unsigned int max_cpus)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	int boot_cpu_id = hard_smp_processor_id();
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	/*
5401da177e4SLinus Torvalds 	 * Initialize the per-CPU profiling counter/multiplier
5411da177e4SLinus Torvalds 	 */
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	smp_setup_percpu_timer();
5441da177e4SLinus Torvalds 
5455d2068daSRusty Russell 	cpumask_set_cpu(0, &cpu_callin_map);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	local_cpu_data->loops_per_jiffy = loops_per_jiffy;
5481da177e4SLinus Torvalds 	ia64_cpu_to_sapicid[0] = boot_cpu_id;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	current_thread_info()->cpu = 0;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	/*
5551da177e4SLinus Torvalds 	 * If SMP should be disabled, then really disable it!
5561da177e4SLinus Torvalds 	 */
5571da177e4SLinus Torvalds 	if (!max_cpus) {
5581da177e4SLinus Torvalds 		printk(KERN_INFO "SMP mode deactivated.\n");
5592af51a3fSRusty Russell 		init_cpu_online(cpumask_of(0));
5602af51a3fSRusty Russell 		init_cpu_present(cpumask_of(0));
5612af51a3fSRusty Russell 		init_cpu_possible(cpumask_of(0));
5621da177e4SLinus Torvalds 		return;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5665b5e76e9SGreg Kroah-Hartman void smp_prepare_boot_cpu(void)
5671da177e4SLinus Torvalds {
5687d7f9848SSrivatsa S. Bhat 	set_cpu_online(smp_processor_id(), true);
5695d2068daSRusty Russell 	cpumask_set_cpu(smp_processor_id(), &cpu_callin_map);
5703bccd996SLee Schermerhorn 	set_numa_node(cpu_to_node_map[smp_processor_id()]);
571a9fa06c2SShaohua Li 	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
572e51835d5SIsaku Yamahata 	paravirt_post_smp_prepare_boot_cpu();
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
576e927ecb0SSuresh Siddha static inline void
577e927ecb0SSuresh Siddha clear_cpu_sibling_map(int cpu)
578e927ecb0SSuresh Siddha {
579e927ecb0SSuresh Siddha 	int i;
580e927ecb0SSuresh Siddha 
5815d2068daSRusty Russell 	for_each_cpu(i, &per_cpu(cpu_sibling_map, cpu))
5825d2068daSRusty Russell 		cpumask_clear_cpu(cpu, &per_cpu(cpu_sibling_map, i));
5835d2068daSRusty Russell 	for_each_cpu(i, &cpu_core_map[cpu])
5845d2068daSRusty Russell 		cpumask_clear_cpu(cpu, &cpu_core_map[i]);
585e927ecb0SSuresh Siddha 
586d5a7430dSMike Travis 	per_cpu(cpu_sibling_map, cpu) = cpu_core_map[cpu] = CPU_MASK_NONE;
587e927ecb0SSuresh Siddha }
588e927ecb0SSuresh Siddha 
589e927ecb0SSuresh Siddha static void
590e927ecb0SSuresh Siddha remove_siblinginfo(int cpu)
591e927ecb0SSuresh Siddha {
592e927ecb0SSuresh Siddha 	int last = 0;
593e927ecb0SSuresh Siddha 
594e927ecb0SSuresh Siddha 	if (cpu_data(cpu)->threads_per_core == 1 &&
595e927ecb0SSuresh Siddha 	    cpu_data(cpu)->cores_per_socket == 1) {
5965d2068daSRusty Russell 		cpumask_clear_cpu(cpu, &cpu_core_map[cpu]);
5975d2068daSRusty Russell 		cpumask_clear_cpu(cpu, &per_cpu(cpu_sibling_map, cpu));
598e927ecb0SSuresh Siddha 		return;
599e927ecb0SSuresh Siddha 	}
600e927ecb0SSuresh Siddha 
6015d2068daSRusty Russell 	last = (cpumask_weight(&cpu_core_map[cpu]) == 1 ? 1 : 0);
602e927ecb0SSuresh Siddha 
603e927ecb0SSuresh Siddha 	/* remove it from all sibling map's */
604e927ecb0SSuresh Siddha 	clear_cpu_sibling_map(cpu);
605e927ecb0SSuresh Siddha }
606e927ecb0SSuresh Siddha 
6071da177e4SLinus Torvalds extern void fixup_irqs(void);
608ff741906SAshok Raj 
609ff741906SAshok Raj int migrate_platform_irqs(unsigned int cpu)
610ff741906SAshok Raj {
611ff741906SAshok Raj 	int new_cpei_cpu;
612097e98b4SThomas Gleixner 	struct irq_data *data = NULL;
6130de26520SRusty Russell 	const struct cpumask *mask;
614ff741906SAshok Raj 	int 		retval = 0;
615ff741906SAshok Raj 
616ff741906SAshok Raj 	/*
617ff741906SAshok Raj 	 * dont permit CPEI target to removed.
618ff741906SAshok Raj 	 */
619ff741906SAshok Raj 	if (cpe_vector > 0 && is_cpu_cpei_target(cpu)) {
620ff741906SAshok Raj 		printk ("CPU (%d) is CPEI Target\n", cpu);
621ff741906SAshok Raj 		if (can_cpei_retarget()) {
622ff741906SAshok Raj 			/*
623ff741906SAshok Raj 			 * Now re-target the CPEI to a different processor
624ff741906SAshok Raj 			 */
6257d7f9848SSrivatsa S. Bhat 			new_cpei_cpu = cpumask_any(cpu_online_mask);
6260de26520SRusty Russell 			mask = cpumask_of(new_cpei_cpu);
627ff741906SAshok Raj 			set_cpei_target_cpu(new_cpei_cpu);
628097e98b4SThomas Gleixner 			data = irq_get_irq_data(ia64_cpe_irq);
629ff741906SAshok Raj 			/*
63072fdbdceSSimon Arlott 			 * Switch for now, immediately, we need to do fake intr
631ff741906SAshok Raj 			 * as other interrupts, but need to study CPEI behaviour with
632ff741906SAshok Raj 			 * polling before making changes.
633ff741906SAshok Raj 			 */
634097e98b4SThomas Gleixner 			if (data && data->chip) {
635097e98b4SThomas Gleixner 				data->chip->irq_disable(data);
636097e98b4SThomas Gleixner 				data->chip->irq_set_affinity(data, mask, false);
637097e98b4SThomas Gleixner 				data->chip->irq_enable(data);
63825985edcSLucas De Marchi 				printk ("Re-targeting CPEI to cpu %d\n", new_cpei_cpu);
639ff741906SAshok Raj 			}
640ff741906SAshok Raj 		}
641097e98b4SThomas Gleixner 		if (!data) {
642ff741906SAshok Raj 			printk ("Unable to retarget CPEI, offline cpu [%d] failed\n", cpu);
643ff741906SAshok Raj 			retval = -EBUSY;
644ff741906SAshok Raj 		}
645ff741906SAshok Raj 	}
646ff741906SAshok Raj 	return retval;
647ff741906SAshok Raj }
648ff741906SAshok Raj 
6491da177e4SLinus Torvalds /* must be called with cpucontrol mutex held */
6501da177e4SLinus Torvalds int __cpu_disable(void)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	int cpu = smp_processor_id();
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	/*
6551da177e4SLinus Torvalds 	 * dont permit boot processor for now
6561da177e4SLinus Torvalds 	 */
657ff741906SAshok Raj 	if (cpu == 0 && !bsp_remove_ok) {
658ff741906SAshok Raj 		printk ("Your platform does not support removal of BSP\n");
659ff741906SAshok Raj 		return (-EBUSY);
660ff741906SAshok Raj 	}
661ff741906SAshok Raj 
6626e9de181SJohn Keller 	if (ia64_platform_is("sn2")) {
6636e9de181SJohn Keller 		if (!sn_cpu_disable_allowed(cpu))
6646e9de181SJohn Keller 			return -EBUSY;
6656e9de181SJohn Keller 	}
6666e9de181SJohn Keller 
6677d7f9848SSrivatsa S. Bhat 	set_cpu_online(cpu, false);
66866db2e63SAlex Chiang 
669ff741906SAshok Raj 	if (migrate_platform_irqs(cpu)) {
6707d7f9848SSrivatsa S. Bhat 		set_cpu_online(cpu, true);
671c0acdea2SAlex Chiang 		return -EBUSY;
672ff741906SAshok Raj 	}
6731da177e4SLinus Torvalds 
674e927ecb0SSuresh Siddha 	remove_siblinginfo(cpu);
67566db2e63SAlex Chiang 	fixup_irqs();
6761da177e4SLinus Torvalds 	local_flush_tlb_all();
6775d2068daSRusty Russell 	cpumask_clear_cpu(cpu, &cpu_callin_map);
6781da177e4SLinus Torvalds 	return 0;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds void __cpu_die(unsigned int cpu)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	unsigned int i;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	for (i = 0; i < 100; i++) {
6861da177e4SLinus Torvalds 		/* They ack this in play_dead by setting CPU_DEAD */
6871da177e4SLinus Torvalds 		if (per_cpu(cpu_state, cpu) == CPU_DEAD)
6881da177e4SLinus Torvalds 		{
689b8d8b883SAshok Raj 			printk ("CPU %d is now offline\n", cpu);
6901da177e4SLinus Torvalds 			return;
6911da177e4SLinus Torvalds 		}
6921da177e4SLinus Torvalds 		msleep(100);
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds  	printk(KERN_ERR "CPU %u didn't die...\n", cpu);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds void
6991da177e4SLinus Torvalds smp_cpus_done (unsigned int dummy)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	int cpu;
7021da177e4SLinus Torvalds 	unsigned long bogosum = 0;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/*
7051da177e4SLinus Torvalds 	 * Allow the user to impress friends.
7061da177e4SLinus Torvalds 	 */
7071da177e4SLinus Torvalds 
708dc565b52Shawkes@sgi.com 	for_each_online_cpu(cpu) {
7091da177e4SLinus Torvalds 		bogosum += cpu_data(cpu)->loops_per_jiffy;
710dc565b52Shawkes@sgi.com 	}
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
7131da177e4SLinus Torvalds 	       (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7165b5e76e9SGreg Kroah-Hartman static inline void set_cpu_sibling_map(int cpu)
717e927ecb0SSuresh Siddha {
718e927ecb0SSuresh Siddha 	int i;
719e927ecb0SSuresh Siddha 
720e927ecb0SSuresh Siddha 	for_each_online_cpu(i) {
721e927ecb0SSuresh Siddha 		if ((cpu_data(cpu)->socket_id == cpu_data(i)->socket_id)) {
7225d2068daSRusty Russell 			cpumask_set_cpu(i, &cpu_core_map[cpu]);
7235d2068daSRusty Russell 			cpumask_set_cpu(cpu, &cpu_core_map[i]);
724e927ecb0SSuresh Siddha 			if (cpu_data(cpu)->core_id == cpu_data(i)->core_id) {
7255d2068daSRusty Russell 				cpumask_set_cpu(i,
7265d2068daSRusty Russell 						&per_cpu(cpu_sibling_map, cpu));
7275d2068daSRusty Russell 				cpumask_set_cpu(cpu,
7285d2068daSRusty Russell 						&per_cpu(cpu_sibling_map, i));
729e927ecb0SSuresh Siddha 			}
730e927ecb0SSuresh Siddha 		}
731e927ecb0SSuresh Siddha 	}
732e927ecb0SSuresh Siddha }
733e927ecb0SSuresh Siddha 
734ccce9bb8SPaul Gortmaker int
7358239c25fSThomas Gleixner __cpu_up(unsigned int cpu, struct task_struct *tidle)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	int ret;
7381da177e4SLinus Torvalds 	int sapicid;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	sapicid = ia64_cpu_to_sapicid[cpu];
7411da177e4SLinus Torvalds 	if (sapicid == -1)
7421da177e4SLinus Torvalds 		return -EINVAL;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	/*
745b8d8b883SAshok Raj 	 * Already booted cpu? not valid anymore since we dont
746b8d8b883SAshok Raj 	 * do idle loop tightspin anymore.
7471da177e4SLinus Torvalds 	 */
7485d2068daSRusty Russell 	if (cpumask_test_cpu(cpu, &cpu_callin_map))
749b8d8b883SAshok Raj 		return -EINVAL;
750b8d8b883SAshok Raj 
751a9fa06c2SShaohua Li 	per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
7521da177e4SLinus Torvalds 	/* Processor goes to start_secondary(), sets online flag */
75313583bf1SThomas Gleixner 	ret = do_boot_cpu(sapicid, cpu, tidle);
7541da177e4SLinus Torvalds 	if (ret < 0)
7551da177e4SLinus Torvalds 		return ret;
7561da177e4SLinus Torvalds 
757e927ecb0SSuresh Siddha 	if (cpu_data(cpu)->threads_per_core == 1 &&
758e927ecb0SSuresh Siddha 	    cpu_data(cpu)->cores_per_socket == 1) {
7595d2068daSRusty Russell 		cpumask_set_cpu(cpu, &per_cpu(cpu_sibling_map, cpu));
7605d2068daSRusty Russell 		cpumask_set_cpu(cpu, &cpu_core_map[cpu]);
761e927ecb0SSuresh Siddha 		return 0;
762e927ecb0SSuresh Siddha 	}
763e927ecb0SSuresh Siddha 
764e927ecb0SSuresh Siddha 	set_cpu_sibling_map(cpu);
765e927ecb0SSuresh Siddha 
7661da177e4SLinus Torvalds 	return 0;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds /*
77072fdbdceSSimon Arlott  * Assume that CPUs have been discovered by some platform-dependent interface.  For
7711da177e4SLinus Torvalds  * SoftSDV/Lion, that would be ACPI.
7721da177e4SLinus Torvalds  *
7731da177e4SLinus Torvalds  * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
7741da177e4SLinus Torvalds  */
7751da177e4SLinus Torvalds void __init
7761da177e4SLinus Torvalds init_smp_config(void)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	struct fptr {
7791da177e4SLinus Torvalds 		unsigned long fp;
7801da177e4SLinus Torvalds 		unsigned long gp;
7811da177e4SLinus Torvalds 	} *ap_startup;
7821da177e4SLinus Torvalds 	long sal_ret;
7831da177e4SLinus Torvalds 
78472fdbdceSSimon Arlott 	/* Tell SAL where to drop the APs.  */
7851da177e4SLinus Torvalds 	ap_startup = (struct fptr *) start_ap;
7861da177e4SLinus Torvalds 	sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
7871da177e4SLinus Torvalds 				       ia64_tpa(ap_startup->fp), ia64_tpa(ap_startup->gp), 0, 0, 0, 0);
7881da177e4SLinus Torvalds 	if (sal_ret < 0)
7891da177e4SLinus Torvalds 		printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
7901da177e4SLinus Torvalds 		       ia64_sal_strerror(sal_ret));
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793e927ecb0SSuresh Siddha /*
794e927ecb0SSuresh Siddha  * identify_siblings(cpu) gets called from identify_cpu. This populates the
795e927ecb0SSuresh Siddha  * information related to logical execution units in per_cpu_data structure.
796e927ecb0SSuresh Siddha  */
7975b5e76e9SGreg Kroah-Hartman void identify_siblings(struct cpuinfo_ia64 *c)
798e927ecb0SSuresh Siddha {
799e088a4adSMatthew Wilcox 	long status;
800e927ecb0SSuresh Siddha 	u16 pltid;
801e927ecb0SSuresh Siddha 	pal_logical_to_physical_t info;
802e927ecb0SSuresh Siddha 
8036ff0bc94SAlex Chiang 	status = ia64_pal_logical_to_phys(-1, &info);
8046ff0bc94SAlex Chiang 	if (status != PAL_STATUS_SUCCESS) {
805113134fcSAlex Chiang 		if (status != PAL_STATUS_UNIMPLEMENTED) {
806113134fcSAlex Chiang 			printk(KERN_ERR
807113134fcSAlex Chiang 				"ia64_pal_logical_to_phys failed with %ld\n",
808e927ecb0SSuresh Siddha 				status);
809e927ecb0SSuresh Siddha 			return;
810e927ecb0SSuresh Siddha 		}
811113134fcSAlex Chiang 
812113134fcSAlex Chiang 		info.overview_ppid = 0;
813113134fcSAlex Chiang 		info.overview_cpp  = 1;
814113134fcSAlex Chiang 		info.overview_tpc  = 1;
815113134fcSAlex Chiang 	}
8166ff0bc94SAlex Chiang 
8176ff0bc94SAlex Chiang 	status = ia64_sal_physical_id_info(&pltid);
8186ff0bc94SAlex Chiang 	if (status != PAL_STATUS_SUCCESS) {
8196ff0bc94SAlex Chiang 		if (status != PAL_STATUS_UNIMPLEMENTED)
8206ff0bc94SAlex Chiang 			printk(KERN_ERR
8216ff0bc94SAlex Chiang 				"ia64_sal_pltid failed with %ld\n",
8226ff0bc94SAlex Chiang 				status);
823e927ecb0SSuresh Siddha 		return;
824e927ecb0SSuresh Siddha 	}
825e927ecb0SSuresh Siddha 
826e927ecb0SSuresh Siddha 	c->socket_id =  (pltid << 8) | info.overview_ppid;
827113134fcSAlex Chiang 
828113134fcSAlex Chiang 	if (info.overview_cpp == 1 && info.overview_tpc == 1)
829113134fcSAlex Chiang 		return;
830113134fcSAlex Chiang 
831e927ecb0SSuresh Siddha 	c->cores_per_socket = info.overview_cpp;
832e927ecb0SSuresh Siddha 	c->threads_per_core = info.overview_tpc;
8334129a953SFenghua Yu 	c->num_log = info.overview_num_log;
834e927ecb0SSuresh Siddha 
835e927ecb0SSuresh Siddha 	c->core_id = info.log1_cid;
836e927ecb0SSuresh Siddha 	c->thread_id = info.log1_tid;
837e927ecb0SSuresh Siddha }
838dd562c05SStephane Eranian 
839dd562c05SStephane Eranian /*
840dd562c05SStephane Eranian  * returns non zero, if multi-threading is enabled
841dd562c05SStephane Eranian  * on at least one physical package. Due to hotplug cpu
842dd562c05SStephane Eranian  * and (maxcpus=), all threads may not necessarily be enabled
843dd562c05SStephane Eranian  * even though the processor supports multi-threading.
844dd562c05SStephane Eranian  */
845dd562c05SStephane Eranian int is_multithreading_enabled(void)
846dd562c05SStephane Eranian {
847dd562c05SStephane Eranian 	int i, j;
848dd562c05SStephane Eranian 
849dd562c05SStephane Eranian 	for_each_present_cpu(i) {
850dd562c05SStephane Eranian 		for_each_present_cpu(j) {
851dd562c05SStephane Eranian 			if (j == i)
852dd562c05SStephane Eranian 				continue;
853dd562c05SStephane Eranian 			if ((cpu_data(j)->socket_id == cpu_data(i)->socket_id)) {
854dd562c05SStephane Eranian 				if (cpu_data(j)->core_id == cpu_data(i)->core_id)
855dd562c05SStephane Eranian 					return 1;
856dd562c05SStephane Eranian 			}
857dd562c05SStephane Eranian 		}
858dd562c05SStephane Eranian 	}
859dd562c05SStephane Eranian 	return 0;
860dd562c05SStephane Eranian }
861dd562c05SStephane Eranian EXPORT_SYMBOL_GPL(is_multithreading_enabled);
862