blob: 03e4ef3893c93dd7a242f02f9d732ebf0144bcf8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * SMP boot-related support
3 *
David Mosberger-Tang82975112005-04-25 11:44:02 -07004 * Copyright (C) 1998-2003, 2005 Hewlett-Packard Co
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * David Mosberger-Tang <davidm@hpl.hp.com>
Suresh Siddhae927ecb2005-04-25 13:25:06 -07006 * Copyright (C) 2001, 2004-2005 Intel Corp
7 * Rohit Seth <rohit.seth@intel.com>
8 * Suresh Siddha <suresh.b.siddha@intel.com>
9 * Gordon Jin <gordon.jin@intel.com>
10 * Ashok Raj <ashok.raj@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * 01/05/16 Rohit Seth <rohit.seth@intel.com> Moved SMP booting functions from smp.c to here.
13 * 01/04/27 David Mosberger <davidm@hpl.hp.com> Added ITC synching code.
14 * 02/07/31 David Mosberger <davidm@hpl.hp.com> Switch over to hotplug-CPU boot-sequence.
15 * smp_boot_cpus()/smp_commence() is replaced by
16 * smp_prepare_cpus()/__cpu_up()/smp_cpus_done().
Ashok Rajb8d8b882005-04-22 14:44:40 -070017 * 04/06/21 Ashok Raj <ashok.raj@intel.com> Added CPU Hotplug Support
Suresh Siddhae927ecb2005-04-25 13:25:06 -070018 * 04/12/26 Jin Gordon <gordon.jin@intel.com>
19 * 04/12/26 Rohit Seth <rohit.seth@intel.com>
20 * Add multi-threading and multi-core detection
21 * 05/01/30 Suresh Siddha <suresh.b.siddha@intel.com>
22 * Setup cpu_sibling_map and cpu_core_map
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <linux/module.h>
26#include <linux/acpi.h>
27#include <linux/bootmem.h>
28#include <linux/cpu.h>
29#include <linux/delay.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/irq.h>
33#include <linux/kernel.h>
34#include <linux/kernel_stat.h>
35#include <linux/mm.h>
36#include <linux/notifier.h>
37#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/spinlock.h>
39#include <linux/efi.h>
40#include <linux/percpu.h>
41#include <linux/bitops.h>
42
Arun Sharma600634972011-07-26 16:09:06 -070043#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/cache.h>
45#include <asm/current.h>
46#include <asm/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/irq.h>
49#include <asm/machvec.h>
50#include <asm/mca.h>
51#include <asm/page.h>
Isaku Yamahatae51835d2008-05-19 22:13:41 +090052#include <asm/paravirt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/pgalloc.h>
54#include <asm/pgtable.h>
55#include <asm/processor.h>
56#include <asm/ptrace.h>
57#include <asm/sal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/tlbflush.h>
59#include <asm/unistd.h>
John Keller6e9de182007-08-22 19:32:06 -050060#include <asm/sn/arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define SMP_DEBUG 0
63
64#if SMP_DEBUG
65#define Dprintk(x...) printk(x)
66#else
67#define Dprintk(x...)
68#endif
69
Ashok Rajb8d8b882005-04-22 14:44:40 -070070#ifdef CONFIG_HOTPLUG_CPU
Ashok Rajff741902005-11-11 14:32:40 -080071#ifdef CONFIG_PERMIT_BSP_REMOVE
72#define bsp_remove_ok 1
73#else
74#define bsp_remove_ok 0
75#endif
76
Ashok Rajb8d8b882005-04-22 14:44:40 -070077/*
78 * Store all idle threads, this can be reused instead of creating
79 * a new thread. Also avoids complicated thread destroy functionality
80 * for idle threads.
81 */
82struct task_struct *idle_thread_array[NR_CPUS];
83
84/*
85 * Global array allocated for NR_CPUS at boot time
86 */
87struct sal_to_os_boot sal_boot_rendez_state[NR_CPUS];
88
89/*
90 * start_ap in head.S uses this to store current booting cpu
91 * info.
92 */
93struct sal_to_os_boot *sal_state_for_booting_cpu = &sal_boot_rendez_state[0];
94
95#define set_brendez_area(x) (sal_state_for_booting_cpu = &sal_boot_rendez_state[(x)]);
96
97#define get_idle_for_cpu(x) (idle_thread_array[(x)])
98#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
99
100#else
101
102#define get_idle_for_cpu(x) (NULL)
103#define set_idle_for_cpu(x,p)
104#define set_brendez_area(x)
105#endif
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * ITC synchronization related stuff:
110 */
Ashok Rajff741902005-11-11 14:32:40 -0800111#define MASTER (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define SLAVE (SMP_CACHE_BYTES/8)
113
114#define NUM_ROUNDS 64 /* magic value */
115#define NUM_ITERS 5 /* likewise */
116
117static DEFINE_SPINLOCK(itc_sync_lock);
118static volatile unsigned long go[SLAVE + 1];
119
120#define DEBUG_ITC_SYNC 0
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122extern void start_ap (void);
123extern unsigned long ia64_iobase;
124
Ingo Molnar36c8b582006-07-03 00:25:41 -0700125struct task_struct *task_for_booting_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
128 * State for each CPU
129 */
130DEFINE_PER_CPU(int, cpu_state);
131
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700132cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
Robin Holt42aca482008-07-28 20:36:50 -0500133EXPORT_SYMBOL(cpu_core_map);
Mike Travisd5a74302007-10-16 01:24:05 -0700134DEFINE_PER_CPU_SHARED_ALIGNED(cpumask_t, cpu_sibling_map);
135EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
136
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700137int smp_num_siblings = 1;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* which logical CPU number maps to which CPU (physical APIC ID) */
140volatile int ia64_cpu_to_sapicid[NR_CPUS];
141EXPORT_SYMBOL(ia64_cpu_to_sapicid);
142
143static volatile cpumask_t cpu_callin_map;
144
145struct smp_boot_data smp_boot_data __initdata;
146
147unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */
148
149char __initdata no_int_routing;
150
151unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */
152
Ashok Rajff741902005-11-11 14:32:40 -0800153#ifdef CONFIG_FORCE_CPEI_RETARGET
154#define CPEI_OVERRIDE_DEFAULT (1)
155#else
156#define CPEI_OVERRIDE_DEFAULT (0)
157#endif
158
159unsigned int force_cpei_retarget = CPEI_OVERRIDE_DEFAULT;
160
161static int __init
162cmdl_force_cpei(char *str)
163{
164 int value=0;
165
166 get_option (&str, &value);
167 force_cpei_retarget = value;
168
169 return 1;
170}
171
172__setup("force_cpei=", cmdl_force_cpei);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int __init
175nointroute (char *str)
176{
177 no_int_routing = 1;
178 printk ("no_int_routing on\n");
179 return 1;
180}
181
182__setup("nointroute", nointroute);
183
Ashok Rajff741902005-11-11 14:32:40 -0800184static void fix_b0_for_bsp(void)
185{
186#ifdef CONFIG_HOTPLUG_CPU
187 int cpuid;
188 static int fix_bsp_b0 = 1;
189
190 cpuid = smp_processor_id();
191
192 /*
193 * Cache the b0 value on the first AP that comes up
194 */
195 if (!(fix_bsp_b0 && cpuid))
196 return;
197
198 sal_boot_rendez_state[0].br[0] = sal_boot_rendez_state[cpuid].br[0];
199 printk ("Fixed BSP b0 value from CPU %d\n", cpuid);
200
201 fix_bsp_b0 = 0;
202#endif
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205void
206sync_master (void *arg)
207{
208 unsigned long flags, i;
209
210 go[MASTER] = 0;
211
212 local_irq_save(flags);
213 {
214 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
David Mosberger-Tang82975112005-04-25 11:44:02 -0700215 while (!go[MASTER])
216 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 go[MASTER] = 0;
218 go[SLAVE] = ia64_get_itc();
219 }
220 }
221 local_irq_restore(flags);
222}
223
224/*
225 * Return the number of cycles by which our itc differs from the itc on the master
226 * (time-keeper) CPU. A positive number indicates our itc is ahead of the master,
227 * negative that it is behind.
228 */
229static inline long
230get_delta (long *rt, long *master)
231{
232 unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
233 unsigned long tcenter, t0, t1, tm;
234 long i;
235
236 for (i = 0; i < NUM_ITERS; ++i) {
237 t0 = ia64_get_itc();
238 go[MASTER] = 1;
David Mosberger-Tang82975112005-04-25 11:44:02 -0700239 while (!(tm = go[SLAVE]))
240 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 go[SLAVE] = 0;
242 t1 = ia64_get_itc();
243
244 if (t1 - t0 < best_t1 - best_t0)
245 best_t0 = t0, best_t1 = t1, best_tm = tm;
246 }
247
248 *rt = best_t1 - best_t0;
249 *master = best_tm - best_t0;
250
251 /* average best_t0 and best_t1 without overflow: */
252 tcenter = (best_t0/2 + best_t1/2);
253 if (best_t0 % 2 + best_t1 % 2 == 2)
254 ++tcenter;
255 return tcenter - best_tm;
256}
257
258/*
259 * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU
260 * (normally the time-keeper CPU). We use a closed loop to eliminate the possibility of
261 * unaccounted-for errors (such as getting a machine check in the middle of a calibration
262 * step). The basic idea is for the slave to ask the master what itc value it has and to
263 * read its own itc before and after the master responds. Each iteration gives us three
264 * timestamps:
265 *
266 * slave master
267 *
268 * t0 ---\
269 * ---\
270 * --->
271 * tm
272 * /---
273 * /---
274 * t1 <---
275 *
276 *
277 * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0
278 * and t1. If we achieve this, the clocks are synchronized provided the interconnect
279 * between the slave and the master is symmetric. Even if the interconnect were
280 * asymmetric, we would still know that the synchronization error is smaller than the
281 * roundtrip latency (t0 - t1).
282 *
283 * When the interconnect is quiet and symmetric, this lets us synchronize the itc to
284 * within one or two cycles. However, we can only *guarantee* that the synchronization is
285 * accurate to within a round-trip time, which is typically in the range of several
286 * hundred cycles (e.g., ~500 cycles). In practice, this means that the itc's are usually
287 * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better
288 * than half a micro second or so.
289 */
290void
291ia64_sync_itc (unsigned int master)
292{
293 long i, delta, adj, adjust_latency = 0, done = 0;
294 unsigned long flags, rt, master_time_stamp, bound;
295#if DEBUG_ITC_SYNC
296 struct {
297 long rt; /* roundtrip time */
298 long master; /* master's timestamp */
299 long diff; /* difference between midpoint and master's timestamp */
300 long lat; /* estimate of itc adjustment latency */
301 } t[NUM_ROUNDS];
302#endif
303
304 /*
305 * Make sure local timer ticks are disabled while we sync. If
306 * they were enabled, we'd have to worry about nasty issues
307 * like setting the ITC ahead of (or a long time before) the
308 * next scheduled tick.
309 */
310 BUG_ON((ia64_get_itv() & (1 << 16)) == 0);
311
312 go[MASTER] = 1;
313
Jens Axboe8691e5a2008-06-06 11:18:06 +0200314 if (smp_call_function_single(master, sync_master, NULL, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master);
316 return;
317 }
318
David Mosberger-Tang82975112005-04-25 11:44:02 -0700319 while (go[MASTER])
320 cpu_relax(); /* wait for master to be ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 spin_lock_irqsave(&itc_sync_lock, flags);
323 {
324 for (i = 0; i < NUM_ROUNDS; ++i) {
325 delta = get_delta(&rt, &master_time_stamp);
326 if (delta == 0) {
327 done = 1; /* let's lock on to this... */
328 bound = rt;
329 }
330
331 if (!done) {
332 if (i > 0) {
333 adjust_latency += -delta;
334 adj = -delta + adjust_latency/4;
335 } else
336 adj = -delta;
337
338 ia64_set_itc(ia64_get_itc() + adj);
339 }
340#if DEBUG_ITC_SYNC
341 t[i].rt = rt;
342 t[i].master = master_time_stamp;
343 t[i].diff = delta;
344 t[i].lat = adjust_latency/4;
345#endif
346 }
347 }
348 spin_unlock_irqrestore(&itc_sync_lock, flags);
349
350#if DEBUG_ITC_SYNC
351 for (i = 0; i < NUM_ROUNDS; ++i)
352 printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
353 t[i].rt, t[i].master, t[i].diff, t[i].lat);
354#endif
355
356 printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, "
357 "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);
358}
359
360/*
361 * Ideally sets up per-cpu profiling hooks. Doesn't do much now...
362 */
363static inline void __devinit
364smp_setup_percpu_timer (void)
365{
366}
367
Tony Luckd86ebd12007-05-23 16:46:40 -0700368static void __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369smp_callin (void)
370{
Ashok Rajff741902005-11-11 14:32:40 -0800371 int cpuid, phys_id, itc_master;
Jack Steineread6caa2007-03-27 14:30:19 -0500372 struct cpuinfo_ia64 *last_cpuinfo, *this_cpuinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 extern void ia64_init_itm(void);
Ashok Rajff741902005-11-11 14:32:40 -0800374 extern volatile int time_keeper_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376#ifdef CONFIG_PERFMON
377 extern void pfm_init_percpu(void);
378#endif
379
380 cpuid = smp_processor_id();
381 phys_id = hard_smp_processor_id();
Ashok Rajff741902005-11-11 14:32:40 -0800382 itc_master = time_keeper_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (cpu_online(cpuid)) {
385 printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
386 phys_id, cpuid);
387 BUG();
388 }
389
Ashok Rajff741902005-11-11 14:32:40 -0800390 fix_b0_for_bsp();
391
Lee Schermerhorn3bccd992010-05-26 14:44:59 -0700392 /*
393 * numa_node_id() works after this.
394 */
395 set_numa_node(cpu_to_node_map[cpuid]);
Lee Schermerhornfd1197f2010-05-26 14:45:01 -0700396 set_numa_mem(local_memory_node(cpu_to_node_map[cpuid]));
Lee Schermerhorn3bccd992010-05-26 14:44:59 -0700397
Jens Axboef27b4332008-06-26 11:22:30 +0200398 ipi_call_lock_irq();
Yasuaki Ishimatsue1b30a32007-07-17 21:22:23 +0900399 spin_lock(&vector_lock);
400 /* Setup the per cpu irq handling data structures */
401 __setup_vector_irq(cpuid);
Manfred Spraule545a612008-09-07 16:57:22 +0200402 notify_cpu_starting(cpuid);
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700403 set_cpu_online(cpuid, true);
Shaohua Lia9fa06c2005-06-25 14:55:05 -0700404 per_cpu(cpu_state, cpuid) = CPU_ONLINE;
Yasuaki Ishimatsue1b30a32007-07-17 21:22:23 +0900405 spin_unlock(&vector_lock);
Jens Axboef27b4332008-06-26 11:22:30 +0200406 ipi_call_unlock_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 smp_setup_percpu_timer();
409
410 ia64_mca_cmc_vector_setup(); /* Setup vector on AP */
411
412#ifdef CONFIG_PERFMON
413 pfm_init_percpu();
414#endif
415
416 local_irq_enable();
417
418 if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
419 /*
420 * Synchronize the ITC with the BP. Need to do this after irqs are
421 * enabled because ia64_sync_itc() calls smp_call_function_single(), which
422 * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
423 * local_bh_enable(), which bugs out if irqs are not enabled...
424 */
Ashok Rajff741902005-11-11 14:32:40 -0800425 Dprintk("Going to syncup ITC with ITC Master.\n");
426 ia64_sync_itc(itc_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 /*
430 * Get our bogomips.
431 */
432 ia64_init_itm();
Jack Steineread6caa2007-03-27 14:30:19 -0500433
434 /*
435 * Delay calibration can be skipped if new processor is identical to the
436 * previous processor.
437 */
438 last_cpuinfo = cpu_data(cpuid - 1);
439 this_cpuinfo = local_cpu_data;
440 if (last_cpuinfo->itc_freq != this_cpuinfo->itc_freq ||
441 last_cpuinfo->proc_freq != this_cpuinfo->proc_freq ||
442 last_cpuinfo->features != this_cpuinfo->features ||
443 last_cpuinfo->revision != this_cpuinfo->revision ||
444 last_cpuinfo->family != this_cpuinfo->family ||
445 last_cpuinfo->archrev != this_cpuinfo->archrev ||
446 last_cpuinfo->model != this_cpuinfo->model)
447 calibrate_delay();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 local_cpu_data->loops_per_jiffy = loops_per_jiffy;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /*
451 * Allow the master to continue.
452 */
453 cpu_set(cpuid, cpu_callin_map);
454 Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
455}
456
457
458/*
459 * Activate a secondary processor. head.S calls this.
460 */
Tony Luckd86ebd12007-05-23 16:46:40 -0700461int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462start_secondary (void *unused)
463{
464 /* Early console may use I/O ports */
465 ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
Tony Luck10617bb2008-08-12 10:34:20 -0700466#ifndef CONFIG_PRINTK_TIME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
Tony Luck10617bb2008-08-12 10:34:20 -0700468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 efi_map_pal_code();
470 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800471 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 smp_callin();
473
474 cpu_idle();
475 return 0;
476}
477
Adrian Bunk6b2fb3c2008-02-06 01:37:55 -0800478struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 return NULL;
481}
482
483struct create_idle {
David Howells6d5aefb2006-12-05 19:36:26 +0000484 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct task_struct *idle;
486 struct completion done;
487 int cpu;
488};
489
Tony Luck9d6f40b2007-07-20 14:39:24 -0700490void __cpuinit
David Howells6d5aefb2006-12-05 19:36:26 +0000491do_fork_idle(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
David Howells6d5aefb2006-12-05 19:36:26 +0000493 struct create_idle *c_idle =
494 container_of(work, struct create_idle, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 c_idle->idle = fork_idle(c_idle->cpu);
497 complete(&c_idle->done);
498}
499
Tony Luck9d6f40b2007-07-20 14:39:24 -0700500static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501do_boot_cpu (int sapicid, int cpu)
502{
503 int timeout;
504 struct create_idle c_idle = {
David Howells6d5aefb2006-12-05 19:36:26 +0000505 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 .cpu = cpu,
507 .done = COMPLETION_INITIALIZER(c_idle.done),
508 };
Ashok Rajb8d8b882005-04-22 14:44:40 -0700509
Suresh Siddhad7a7c572010-08-09 17:20:33 -0700510 /*
511 * We can't use kernel_thread since we must avoid to
512 * reschedule the child.
513 */
Ashok Rajb8d8b882005-04-22 14:44:40 -0700514 c_idle.idle = get_idle_for_cpu(cpu);
515 if (c_idle.idle) {
516 init_idle(c_idle.idle, cpu);
517 goto do_rest;
518 }
519
Suresh Siddhad7a7c572010-08-09 17:20:33 -0700520 schedule_work(&c_idle.work);
521 wait_for_completion(&c_idle.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (IS_ERR(c_idle.idle))
524 panic("failed fork for CPU %d", cpu);
Ashok Rajb8d8b882005-04-22 14:44:40 -0700525
526 set_idle_for_cpu(cpu, c_idle.idle);
527
528do_rest:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 task_for_booting_cpu = c_idle.idle;
530
531 Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);
532
Ashok Rajb8d8b882005-04-22 14:44:40 -0700533 set_brendez_area(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);
535
536 /*
537 * Wait 10s total for the AP to start
538 */
539 Dprintk("Waiting on callin_map ...");
540 for (timeout = 0; timeout < 100000; timeout++) {
541 if (cpu_isset(cpu, cpu_callin_map))
542 break; /* It has booted */
543 udelay(100);
544 }
545 Dprintk("\n");
546
547 if (!cpu_isset(cpu, cpu_callin_map)) {
548 printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
549 ia64_cpu_to_sapicid[cpu] = -1;
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700550 set_cpu_online(cpu, false); /* was set in smp_callin() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -EINVAL;
552 }
553 return 0;
554}
555
556static int __init
557decay (char *str)
558{
559 int ticks;
560 get_option (&str, &ticks);
561 return 1;
562}
563
564__setup("decay=", decay);
565
566/*
567 * Initialize the logical CPU number to SAPICID mapping
568 */
569void __init
570smp_build_cpu_map (void)
571{
572 int sapicid, cpu, i;
573 int boot_cpu_id = hard_smp_processor_id();
574
575 for (cpu = 0; cpu < NR_CPUS; cpu++) {
576 ia64_cpu_to_sapicid[cpu] = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579 ia64_cpu_to_sapicid[0] = boot_cpu_id;
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700580 init_cpu_present(cpumask_of(0));
Rusty Russell2af51a32009-03-16 14:12:43 +1030581 set_cpu_possible(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
583 sapicid = smp_boot_data.cpu_phys_id[i];
584 if (sapicid == boot_cpu_id)
585 continue;
Rusty Russell2af51a32009-03-16 14:12:43 +1030586 set_cpu_present(cpu, true);
587 set_cpu_possible(cpu, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ia64_cpu_to_sapicid[cpu] = sapicid;
589 cpu++;
590 }
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * Cycle through the APs sending Wakeup IPIs to boot each.
595 */
596void __init
597smp_prepare_cpus (unsigned int max_cpus)
598{
599 int boot_cpu_id = hard_smp_processor_id();
600
601 /*
602 * Initialize the per-CPU profiling counter/multiplier
603 */
604
605 smp_setup_percpu_timer();
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 cpu_set(0, cpu_callin_map);
608
609 local_cpu_data->loops_per_jiffy = loops_per_jiffy;
610 ia64_cpu_to_sapicid[0] = boot_cpu_id;
611
612 printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);
613
614 current_thread_info()->cpu = 0;
615
616 /*
617 * If SMP should be disabled, then really disable it!
618 */
619 if (!max_cpus) {
620 printk(KERN_INFO "SMP mode deactivated.\n");
Rusty Russell2af51a32009-03-16 14:12:43 +1030621 init_cpu_online(cpumask_of(0));
622 init_cpu_present(cpumask_of(0));
623 init_cpu_possible(cpumask_of(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return;
625 }
626}
627
628void __devinit smp_prepare_boot_cpu(void)
629{
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700630 set_cpu_online(smp_processor_id(), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 cpu_set(smp_processor_id(), cpu_callin_map);
Lee Schermerhorn3bccd992010-05-26 14:44:59 -0700632 set_numa_node(cpu_to_node_map[smp_processor_id()]);
Shaohua Lia9fa06c2005-06-25 14:55:05 -0700633 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Isaku Yamahatae51835d2008-05-19 22:13:41 +0900634 paravirt_post_smp_prepare_boot_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637#ifdef CONFIG_HOTPLUG_CPU
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700638static inline void
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700639clear_cpu_sibling_map(int cpu)
640{
641 int i;
642
Mike Travisd5a74302007-10-16 01:24:05 -0700643 for_each_cpu_mask(i, per_cpu(cpu_sibling_map, cpu))
644 cpu_clear(cpu, per_cpu(cpu_sibling_map, i));
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700645 for_each_cpu_mask(i, cpu_core_map[cpu])
646 cpu_clear(cpu, cpu_core_map[i]);
647
Mike Travisd5a74302007-10-16 01:24:05 -0700648 per_cpu(cpu_sibling_map, cpu) = cpu_core_map[cpu] = CPU_MASK_NONE;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700649}
650
651static void
652remove_siblinginfo(int cpu)
653{
654 int last = 0;
655
656 if (cpu_data(cpu)->threads_per_core == 1 &&
657 cpu_data(cpu)->cores_per_socket == 1) {
658 cpu_clear(cpu, cpu_core_map[cpu]);
Mike Travisd5a74302007-10-16 01:24:05 -0700659 cpu_clear(cpu, per_cpu(cpu_sibling_map, cpu));
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700660 return;
661 }
662
663 last = (cpus_weight(cpu_core_map[cpu]) == 1 ? 1 : 0);
664
665 /* remove it from all sibling map's */
666 clear_cpu_sibling_map(cpu);
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669extern void fixup_irqs(void);
Ashok Rajff741902005-11-11 14:32:40 -0800670
671int migrate_platform_irqs(unsigned int cpu)
672{
673 int new_cpei_cpu;
Thomas Gleixner097e98b2011-03-25 19:00:33 +0100674 struct irq_data *data = NULL;
Rusty Russell0de26522008-12-13 21:20:26 +1030675 const struct cpumask *mask;
Ashok Rajff741902005-11-11 14:32:40 -0800676 int retval = 0;
677
678 /*
679 * dont permit CPEI target to removed.
680 */
681 if (cpe_vector > 0 && is_cpu_cpei_target(cpu)) {
682 printk ("CPU (%d) is CPEI Target\n", cpu);
683 if (can_cpei_retarget()) {
684 /*
685 * Now re-target the CPEI to a different processor
686 */
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700687 new_cpei_cpu = cpumask_any(cpu_online_mask);
Rusty Russell0de26522008-12-13 21:20:26 +1030688 mask = cpumask_of(new_cpei_cpu);
Ashok Rajff741902005-11-11 14:32:40 -0800689 set_cpei_target_cpu(new_cpei_cpu);
Thomas Gleixner097e98b2011-03-25 19:00:33 +0100690 data = irq_get_irq_data(ia64_cpe_irq);
Ashok Rajff741902005-11-11 14:32:40 -0800691 /*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700692 * Switch for now, immediately, we need to do fake intr
Ashok Rajff741902005-11-11 14:32:40 -0800693 * as other interrupts, but need to study CPEI behaviour with
694 * polling before making changes.
695 */
Thomas Gleixner097e98b2011-03-25 19:00:33 +0100696 if (data && data->chip) {
697 data->chip->irq_disable(data);
698 data->chip->irq_set_affinity(data, mask, false);
699 data->chip->irq_enable(data);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300700 printk ("Re-targeting CPEI to cpu %d\n", new_cpei_cpu);
Ashok Rajff741902005-11-11 14:32:40 -0800701 }
702 }
Thomas Gleixner097e98b2011-03-25 19:00:33 +0100703 if (!data) {
Ashok Rajff741902005-11-11 14:32:40 -0800704 printk ("Unable to retarget CPEI, offline cpu [%d] failed\n", cpu);
705 retval = -EBUSY;
706 }
707 }
708 return retval;
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/* must be called with cpucontrol mutex held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712int __cpu_disable(void)
713{
714 int cpu = smp_processor_id();
715
716 /*
717 * dont permit boot processor for now
718 */
Ashok Rajff741902005-11-11 14:32:40 -0800719 if (cpu == 0 && !bsp_remove_ok) {
720 printk ("Your platform does not support removal of BSP\n");
721 return (-EBUSY);
722 }
723
John Keller6e9de182007-08-22 19:32:06 -0500724 if (ia64_platform_is("sn2")) {
725 if (!sn_cpu_disable_allowed(cpu))
726 return -EBUSY;
727 }
728
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700729 set_cpu_online(cpu, false);
Alex Chiang66db2e62009-02-09 11:16:16 -0700730
Ashok Rajff741902005-11-11 14:32:40 -0800731 if (migrate_platform_irqs(cpu)) {
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -0700732 set_cpu_online(cpu, true);
Alex Chiangc0acdea2009-02-09 11:16:57 -0700733 return -EBUSY;
Ashok Rajff741902005-11-11 14:32:40 -0800734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700736 remove_siblinginfo(cpu);
Alex Chiang66db2e62009-02-09 11:16:16 -0700737 fixup_irqs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 local_flush_tlb_all();
Ashok Rajb8d8b882005-04-22 14:44:40 -0700739 cpu_clear(cpu, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741}
742
743void __cpu_die(unsigned int cpu)
744{
745 unsigned int i;
746
747 for (i = 0; i < 100; i++) {
748 /* They ack this in play_dead by setting CPU_DEAD */
749 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
750 {
Ashok Rajb8d8b882005-04-22 14:44:40 -0700751 printk ("CPU %d is now offline\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return;
753 }
754 msleep(100);
755 }
756 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
757}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif /* CONFIG_HOTPLUG_CPU */
759
760void
761smp_cpus_done (unsigned int dummy)
762{
763 int cpu;
764 unsigned long bogosum = 0;
765
766 /*
767 * Allow the user to impress friends.
768 */
769
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700770 for_each_online_cpu(cpu) {
771 bogosum += cpu_data(cpu)->loops_per_jiffy;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
775 (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
776}
777
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700778static inline void __devinit
779set_cpu_sibling_map(int cpu)
780{
781 int i;
782
783 for_each_online_cpu(i) {
784 if ((cpu_data(cpu)->socket_id == cpu_data(i)->socket_id)) {
785 cpu_set(i, cpu_core_map[cpu]);
786 cpu_set(cpu, cpu_core_map[i]);
787 if (cpu_data(cpu)->core_id == cpu_data(i)->core_id) {
Mike Travisd5a74302007-10-16 01:24:05 -0700788 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
789 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700790 }
791 }
792 }
793}
794
Tony Luck9d6f40b2007-07-20 14:39:24 -0700795int __cpuinit
Thomas Gleixner8239c252012-04-20 13:05:42 +0000796__cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 int ret;
799 int sapicid;
800
801 sapicid = ia64_cpu_to_sapicid[cpu];
802 if (sapicid == -1)
803 return -EINVAL;
804
805 /*
Ashok Rajb8d8b882005-04-22 14:44:40 -0700806 * Already booted cpu? not valid anymore since we dont
807 * do idle loop tightspin anymore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809 if (cpu_isset(cpu, cpu_callin_map))
Ashok Rajb8d8b882005-04-22 14:44:40 -0700810 return -EINVAL;
811
Shaohua Lia9fa06c2005-06-25 14:55:05 -0700812 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Processor goes to start_secondary(), sets online flag */
814 ret = do_boot_cpu(sapicid, cpu);
815 if (ret < 0)
816 return ret;
817
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700818 if (cpu_data(cpu)->threads_per_core == 1 &&
819 cpu_data(cpu)->cores_per_socket == 1) {
Mike Travisd5a74302007-10-16 01:24:05 -0700820 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700821 cpu_set(cpu, cpu_core_map[cpu]);
822 return 0;
823 }
824
825 set_cpu_sibling_map(cpu);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828}
829
830/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700831 * Assume that CPUs have been discovered by some platform-dependent interface. For
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * SoftSDV/Lion, that would be ACPI.
833 *
834 * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
835 */
836void __init
837init_smp_config(void)
838{
839 struct fptr {
840 unsigned long fp;
841 unsigned long gp;
842 } *ap_startup;
843 long sal_ret;
844
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700845 /* Tell SAL where to drop the APs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 ap_startup = (struct fptr *) start_ap;
847 sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
848 ia64_tpa(ap_startup->fp), ia64_tpa(ap_startup->gp), 0, 0, 0, 0);
849 if (sal_ret < 0)
850 printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
851 ia64_sal_strerror(sal_ret));
852}
853
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700854/*
855 * identify_siblings(cpu) gets called from identify_cpu. This populates the
856 * information related to logical execution units in per_cpu_data structure.
857 */
858void __devinit
859identify_siblings(struct cpuinfo_ia64 *c)
860{
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700861 long status;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700862 u16 pltid;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700863 pal_logical_to_physical_t info;
864
Alex Chiang6ff0bc92008-04-24 12:57:08 -0600865 status = ia64_pal_logical_to_phys(-1, &info);
866 if (status != PAL_STATUS_SUCCESS) {
Alex Chiang113134f2007-10-19 13:20:09 -0600867 if (status != PAL_STATUS_UNIMPLEMENTED) {
868 printk(KERN_ERR
869 "ia64_pal_logical_to_phys failed with %ld\n",
870 status);
871 return;
872 }
873
874 info.overview_ppid = 0;
875 info.overview_cpp = 1;
876 info.overview_tpc = 1;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700877 }
Alex Chiang6ff0bc92008-04-24 12:57:08 -0600878
879 status = ia64_sal_physical_id_info(&pltid);
880 if (status != PAL_STATUS_SUCCESS) {
881 if (status != PAL_STATUS_UNIMPLEMENTED)
882 printk(KERN_ERR
883 "ia64_sal_pltid failed with %ld\n",
884 status);
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700885 return;
886 }
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700887
888 c->socket_id = (pltid << 8) | info.overview_ppid;
Alex Chiang113134f2007-10-19 13:20:09 -0600889
890 if (info.overview_cpp == 1 && info.overview_tpc == 1)
891 return;
892
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700893 c->cores_per_socket = info.overview_cpp;
894 c->threads_per_core = info.overview_tpc;
Fenghua Yu4129a952006-02-27 16:16:22 -0800895 c->num_log = info.overview_num_log;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700896
Fenghua Yu4129a952006-02-27 16:16:22 -0800897 c->core_id = info.log1_cid;
898 c->thread_id = info.log1_tid;
Suresh Siddhae927ecb2005-04-25 13:25:06 -0700899}
Stephane Eraniandd562c02006-09-21 10:35:44 -0700900
901/*
902 * returns non zero, if multi-threading is enabled
903 * on at least one physical package. Due to hotplug cpu
904 * and (maxcpus=), all threads may not necessarily be enabled
905 * even though the processor supports multi-threading.
906 */
907int is_multithreading_enabled(void)
908{
909 int i, j;
910
911 for_each_present_cpu(i) {
912 for_each_present_cpu(j) {
913 if (j == i)
914 continue;
915 if ((cpu_data(j)->socket_id == cpu_data(i)->socket_id)) {
916 if (cpu_data(j)->core_id == cpu_data(i)->core_id)
917 return 1;
918 }
919 }
920 }
921 return 0;
922}
923EXPORT_SYMBOL_GPL(is_multithreading_enabled);