smpboot.c 22.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * SMP boot-related support
 *
4
 * Copyright (C) 1998-2003, 2005 Hewlett-Packard Co
L
Linus Torvalds 已提交
5
 *	David Mosberger-Tang <davidm@hpl.hp.com>
6 7 8 9 10
 * Copyright (C) 2001, 2004-2005 Intel Corp
 * 	Rohit Seth <rohit.seth@intel.com>
 * 	Suresh Siddha <suresh.b.siddha@intel.com>
 * 	Gordon Jin <gordon.jin@intel.com>
 *	Ashok Raj  <ashok.raj@intel.com>
L
Linus Torvalds 已提交
11 12 13 14 15 16
 *
 * 01/05/16 Rohit Seth <rohit.seth@intel.com>	Moved SMP booting functions from smp.c to here.
 * 01/04/27 David Mosberger <davidm@hpl.hp.com>	Added ITC synching code.
 * 02/07/31 David Mosberger <davidm@hpl.hp.com>	Switch over to hotplug-CPU boot-sequence.
 *						smp_boot_cpus()/smp_commence() is replaced by
 *						smp_prepare_cpus()/__cpu_up()/smp_cpus_done().
17
 * 04/06/21 Ashok Raj		<ashok.raj@intel.com> Added CPU Hotplug Support
18 19 20 21 22
 * 04/12/26 Jin Gordon <gordon.jin@intel.com>
 * 04/12/26 Rohit Seth <rohit.seth@intel.com>
 *						Add multi-threading and multi-core detection
 * 05/01/30 Suresh Siddha <suresh.b.siddha@intel.com>
 *						Setup cpu_sibling_map and cpu_core_map
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 */

#include <linux/module.h>
#include <linux/acpi.h>
#include <linux/bootmem.h>
#include <linux/cpu.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/kernel_stat.h>
#include <linux/mm.h>
#include <linux/notifier.h>
#include <linux/smp.h>
#include <linux/spinlock.h>
#include <linux/efi.h>
#include <linux/percpu.h>
#include <linux/bitops.h>

#include <asm/atomic.h>
#include <asm/cache.h>
#include <asm/current.h>
#include <asm/delay.h>
#include <asm/ia32.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/machvec.h>
#include <asm/mca.h>
#include <asm/page.h>
53
#include <asm/paravirt.h>
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61
#include <asm/pgalloc.h>
#include <asm/pgtable.h>
#include <asm/processor.h>
#include <asm/ptrace.h>
#include <asm/sal.h>
#include <asm/system.h>
#include <asm/tlbflush.h>
#include <asm/unistd.h>
62
#include <asm/sn/arch.h>
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71

#define SMP_DEBUG 0

#if SMP_DEBUG
#define Dprintk(x...)  printk(x)
#else
#define Dprintk(x...)
#endif

72
#ifdef CONFIG_HOTPLUG_CPU
A
Ashok Raj 已提交
73 74 75 76 77 78
#ifdef CONFIG_PERMIT_BSP_REMOVE
#define bsp_remove_ok	1
#else
#define bsp_remove_ok	0
#endif

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*
 * Store all idle threads, this can be reused instead of creating
 * a new thread. Also avoids complicated thread destroy functionality
 * for idle threads.
 */
struct task_struct *idle_thread_array[NR_CPUS];

/*
 * Global array allocated for NR_CPUS at boot time
 */
struct sal_to_os_boot sal_boot_rendez_state[NR_CPUS];

/*
 * start_ap in head.S uses this to store current booting cpu
 * info.
 */
struct sal_to_os_boot *sal_state_for_booting_cpu = &sal_boot_rendez_state[0];

#define set_brendez_area(x) (sal_state_for_booting_cpu = &sal_boot_rendez_state[(x)]);

#define get_idle_for_cpu(x)		(idle_thread_array[(x)])
#define set_idle_for_cpu(x,p)	(idle_thread_array[(x)] = (p))

#else

#define get_idle_for_cpu(x)		(NULL)
#define set_idle_for_cpu(x,p)
#define set_brendez_area(x)
#endif

L
Linus Torvalds 已提交
109 110 111 112

/*
 * ITC synchronization related stuff:
 */
A
Ashok Raj 已提交
113
#define MASTER	(0)
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
#define SLAVE	(SMP_CACHE_BYTES/8)

#define NUM_ROUNDS	64	/* magic value */
#define NUM_ITERS	5	/* likewise */

static DEFINE_SPINLOCK(itc_sync_lock);
static volatile unsigned long go[SLAVE + 1];

#define DEBUG_ITC_SYNC	0

extern void start_ap (void);
extern unsigned long ia64_iobase;

127
struct task_struct *task_for_booting_cpu;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136

/*
 * State for each CPU
 */
DEFINE_PER_CPU(int, cpu_state);

/* Bitmasks of currently online, and possible CPUs */
cpumask_t cpu_online_map;
EXPORT_SYMBOL(cpu_online_map);
137
cpumask_t cpu_possible_map = CPU_MASK_NONE;
L
Linus Torvalds 已提交
138 139
EXPORT_SYMBOL(cpu_possible_map);

140
cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
141
EXPORT_SYMBOL(cpu_core_map);
142 143 144
DEFINE_PER_CPU_SHARED_ALIGNED(cpumask_t, cpu_sibling_map);
EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);

145 146
int smp_num_siblings = 1;

L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160
/* which logical CPU number maps to which CPU (physical APIC ID) */
volatile int ia64_cpu_to_sapicid[NR_CPUS];
EXPORT_SYMBOL(ia64_cpu_to_sapicid);

static volatile cpumask_t cpu_callin_map;

struct smp_boot_data smp_boot_data __initdata;

unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */

char __initdata no_int_routing;

unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */

A
Ashok Raj 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#ifdef CONFIG_FORCE_CPEI_RETARGET
#define CPEI_OVERRIDE_DEFAULT	(1)
#else
#define CPEI_OVERRIDE_DEFAULT	(0)
#endif

unsigned int force_cpei_retarget = CPEI_OVERRIDE_DEFAULT;

static int __init
cmdl_force_cpei(char *str)
{
	int value=0;

	get_option (&str, &value);
	force_cpei_retarget = value;

	return 1;
}

__setup("force_cpei=", cmdl_force_cpei);

L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191
static int __init
nointroute (char *str)
{
	no_int_routing = 1;
	printk ("no_int_routing on\n");
	return 1;
}

__setup("nointroute", nointroute);

A
Ashok Raj 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static void fix_b0_for_bsp(void)
{
#ifdef CONFIG_HOTPLUG_CPU
	int cpuid;
	static int fix_bsp_b0 = 1;

	cpuid = smp_processor_id();

	/*
	 * Cache the b0 value on the first AP that comes up
	 */
	if (!(fix_bsp_b0 && cpuid))
		return;

	sal_boot_rendez_state[0].br[0] = sal_boot_rendez_state[cpuid].br[0];
	printk ("Fixed BSP b0 value from CPU %d\n", cpuid);

	fix_bsp_b0 = 0;
#endif
}

L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222
void
sync_master (void *arg)
{
	unsigned long flags, i;

	go[MASTER] = 0;

	local_irq_save(flags);
	{
		for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
223 224
			while (!go[MASTER])
				cpu_relax();
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
			go[MASTER] = 0;
			go[SLAVE] = ia64_get_itc();
		}
	}
	local_irq_restore(flags);
}

/*
 * Return the number of cycles by which our itc differs from the itc on the master
 * (time-keeper) CPU.  A positive number indicates our itc is ahead of the master,
 * negative that it is behind.
 */
static inline long
get_delta (long *rt, long *master)
{
	unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
	unsigned long tcenter, t0, t1, tm;
	long i;

	for (i = 0; i < NUM_ITERS; ++i) {
		t0 = ia64_get_itc();
		go[MASTER] = 1;
247 248
		while (!(tm = go[SLAVE]))
			cpu_relax();
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		go[SLAVE] = 0;
		t1 = ia64_get_itc();

		if (t1 - t0 < best_t1 - best_t0)
			best_t0 = t0, best_t1 = t1, best_tm = tm;
	}

	*rt = best_t1 - best_t0;
	*master = best_tm - best_t0;

	/* average best_t0 and best_t1 without overflow: */
	tcenter = (best_t0/2 + best_t1/2);
	if (best_t0 % 2 + best_t1 % 2 == 2)
		++tcenter;
	return tcenter - best_tm;
}

/*
 * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU
 * (normally the time-keeper CPU).  We use a closed loop to eliminate the possibility of
 * unaccounted-for errors (such as getting a machine check in the middle of a calibration
 * step).  The basic idea is for the slave to ask the master what itc value it has and to
 * read its own itc before and after the master responds.  Each iteration gives us three
 * timestamps:
 *
 *	slave		master
 *
 *	t0 ---\
 *             ---\
 *		   --->
 *			tm
 *		   /---
 *	       /---
 *	t1 <---
 *
 *
 * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0
 * and t1.  If we achieve this, the clocks are synchronized provided the interconnect
 * between the slave and the master is symmetric.  Even if the interconnect were
 * asymmetric, we would still know that the synchronization error is smaller than the
 * roundtrip latency (t0 - t1).
 *
 * When the interconnect is quiet and symmetric, this lets us synchronize the itc to
 * within one or two cycles.  However, we can only *guarantee* that the synchronization is
 * accurate to within a round-trip time, which is typically in the range of several
 * hundred cycles (e.g., ~500 cycles).  In practice, this means that the itc's are usually
 * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better
 * than half a micro second or so.
 */
void
ia64_sync_itc (unsigned int master)
{
	long i, delta, adj, adjust_latency = 0, done = 0;
	unsigned long flags, rt, master_time_stamp, bound;
#if DEBUG_ITC_SYNC
	struct {
		long rt;	/* roundtrip time */
		long master;	/* master's timestamp */
		long diff;	/* difference between midpoint and master's timestamp */
		long lat;	/* estimate of itc adjustment latency */
	} t[NUM_ROUNDS];
#endif

	/*
	 * Make sure local timer ticks are disabled while we sync.  If
	 * they were enabled, we'd have to worry about nasty issues
	 * like setting the ITC ahead of (or a long time before) the
	 * next scheduled tick.
	 */
	BUG_ON((ia64_get_itv() & (1 << 16)) == 0);

	go[MASTER] = 1;

322
	if (smp_call_function_single(master, sync_master, NULL, 0) < 0) {
L
Linus Torvalds 已提交
323 324 325 326
		printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master);
		return;
	}

327 328
	while (go[MASTER])
		cpu_relax();	/* wait for master to be ready */
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

	spin_lock_irqsave(&itc_sync_lock, flags);
	{
		for (i = 0; i < NUM_ROUNDS; ++i) {
			delta = get_delta(&rt, &master_time_stamp);
			if (delta == 0) {
				done = 1;	/* let's lock on to this... */
				bound = rt;
			}

			if (!done) {
				if (i > 0) {
					adjust_latency += -delta;
					adj = -delta + adjust_latency/4;
				} else
					adj = -delta;

				ia64_set_itc(ia64_get_itc() + adj);
			}
#if DEBUG_ITC_SYNC
			t[i].rt = rt;
			t[i].master = master_time_stamp;
			t[i].diff = delta;
			t[i].lat = adjust_latency/4;
#endif
		}
	}
	spin_unlock_irqrestore(&itc_sync_lock, flags);

#if DEBUG_ITC_SYNC
	for (i = 0; i < NUM_ROUNDS; ++i)
		printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
		       t[i].rt, t[i].master, t[i].diff, t[i].lat);
#endif

	printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, "
	       "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);
}

/*
 * Ideally sets up per-cpu profiling hooks.  Doesn't do much now...
 */
static inline void __devinit
smp_setup_percpu_timer (void)
{
}

376
static void __cpuinit
L
Linus Torvalds 已提交
377 378
smp_callin (void)
{
A
Ashok Raj 已提交
379
	int cpuid, phys_id, itc_master;
380
	struct cpuinfo_ia64 *last_cpuinfo, *this_cpuinfo;
L
Linus Torvalds 已提交
381
	extern void ia64_init_itm(void);
A
Ashok Raj 已提交
382
	extern volatile int time_keeper_id;
L
Linus Torvalds 已提交
383 384 385 386 387 388 389

#ifdef CONFIG_PERFMON
	extern void pfm_init_percpu(void);
#endif

	cpuid = smp_processor_id();
	phys_id = hard_smp_processor_id();
A
Ashok Raj 已提交
390
	itc_master = time_keeper_id;
L
Linus Torvalds 已提交
391 392 393 394 395 396 397

	if (cpu_online(cpuid)) {
		printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
		       phys_id, cpuid);
		BUG();
	}

A
Ashok Raj 已提交
398 399
	fix_b0_for_bsp();

400
	ipi_call_lock_irq();
401 402 403
	spin_lock(&vector_lock);
	/* Setup the per cpu irq handling data structures */
	__setup_vector_irq(cpuid);
404
	notify_cpu_starting(cpuid);
L
Linus Torvalds 已提交
405
	cpu_set(cpuid, cpu_online_map);
406
	per_cpu(cpu_state, cpuid) = CPU_ONLINE;
407
	spin_unlock(&vector_lock);
408
	ipi_call_unlock_irq();
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

	smp_setup_percpu_timer();

	ia64_mca_cmc_vector_setup();	/* Setup vector on AP */

#ifdef CONFIG_PERFMON
	pfm_init_percpu();
#endif

	local_irq_enable();

	if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
		/*
		 * Synchronize the ITC with the BP.  Need to do this after irqs are
		 * enabled because ia64_sync_itc() calls smp_call_function_single(), which
		 * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
		 * local_bh_enable(), which bugs out if irqs are not enabled...
		 */
A
Ashok Raj 已提交
427 428
		Dprintk("Going to syncup ITC with ITC Master.\n");
		ia64_sync_itc(itc_master);
L
Linus Torvalds 已提交
429 430 431 432 433 434
	}

	/*
	 * Get our bogomips.
	 */
	ia64_init_itm();
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

	/*
	 * Delay calibration can be skipped if new processor is identical to the
	 * previous processor.
	 */
	last_cpuinfo = cpu_data(cpuid - 1);
	this_cpuinfo = local_cpu_data;
	if (last_cpuinfo->itc_freq != this_cpuinfo->itc_freq ||
	    last_cpuinfo->proc_freq != this_cpuinfo->proc_freq ||
	    last_cpuinfo->features != this_cpuinfo->features ||
	    last_cpuinfo->revision != this_cpuinfo->revision ||
	    last_cpuinfo->family != this_cpuinfo->family ||
	    last_cpuinfo->archrev != this_cpuinfo->archrev ||
	    last_cpuinfo->model != this_cpuinfo->model)
		calibrate_delay();
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	local_cpu_data->loops_per_jiffy = loops_per_jiffy;

#ifdef CONFIG_IA32_SUPPORT
	ia32_gdt_init();
#endif

	/*
	 * Allow the master to continue.
	 */
	cpu_set(cpuid, cpu_callin_map);
	Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
}


/*
 * Activate a secondary processor.  head.S calls this.
 */
467
int __cpuinit
L
Linus Torvalds 已提交
468 469 470 471
start_secondary (void *unused)
{
	/* Early console may use I/O ports */
	ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
472
#ifndef CONFIG_PRINTK_TIME
L
Linus Torvalds 已提交
473
	Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
474
#endif
L
Linus Torvalds 已提交
475 476
	efi_map_pal_code();
	cpu_init();
477
	preempt_disable();
L
Linus Torvalds 已提交
478 479 480 481 482 483
	smp_callin();

	cpu_idle();
	return 0;
}

A
Adrian Bunk 已提交
484
struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
L
Linus Torvalds 已提交
485 486 487 488 489
{
	return NULL;
}

struct create_idle {
490
	struct work_struct work;
L
Linus Torvalds 已提交
491 492 493 494 495
	struct task_struct *idle;
	struct completion done;
	int cpu;
};

T
Tony Luck 已提交
496
void __cpuinit
497
do_fork_idle(struct work_struct *work)
L
Linus Torvalds 已提交
498
{
499 500
	struct create_idle *c_idle =
		container_of(work, struct create_idle, work);
L
Linus Torvalds 已提交
501 502 503 504 505

	c_idle->idle = fork_idle(c_idle->cpu);
	complete(&c_idle->done);
}

T
Tony Luck 已提交
506
static int __cpuinit
L
Linus Torvalds 已提交
507 508 509 510
do_boot_cpu (int sapicid, int cpu)
{
	int timeout;
	struct create_idle c_idle = {
511
		.work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
L
Linus Torvalds 已提交
512 513 514
		.cpu	= cpu,
		.done	= COMPLETION_INITIALIZER(c_idle.done),
	};
515 516 517 518 519 520 521

 	c_idle.idle = get_idle_for_cpu(cpu);
 	if (c_idle.idle) {
		init_idle(c_idle.idle, cpu);
 		goto do_rest;
	}

L
Linus Torvalds 已提交
522 523 524 525
	/*
	 * We can't use kernel_thread since we must avoid to reschedule the child.
	 */
	if (!keventd_up() || current_is_keventd())
526
		c_idle.work.func(&c_idle.work);
L
Linus Torvalds 已提交
527
	else {
528
		schedule_work(&c_idle.work);
L
Linus Torvalds 已提交
529 530 531 532 533
		wait_for_completion(&c_idle.done);
	}

	if (IS_ERR(c_idle.idle))
		panic("failed fork for CPU %d", cpu);
534 535 536 537

	set_idle_for_cpu(cpu, c_idle.idle);

do_rest:
L
Linus Torvalds 已提交
538 539 540 541
	task_for_booting_cpu = c_idle.idle;

	Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);

542
	set_brendez_area(cpu);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);

	/*
	 * Wait 10s total for the AP to start
	 */
	Dprintk("Waiting on callin_map ...");
	for (timeout = 0; timeout < 100000; timeout++) {
		if (cpu_isset(cpu, cpu_callin_map))
			break;  /* It has booted */
		udelay(100);
	}
	Dprintk("\n");

	if (!cpu_isset(cpu, cpu_callin_map)) {
		printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
		ia64_cpu_to_sapicid[cpu] = -1;
		cpu_clear(cpu, cpu_online_map);  /* was set in smp_callin() */
		return -EINVAL;
	}
	return 0;
}

static int __init
decay (char *str)
{
	int ticks;
	get_option (&str, &ticks);
	return 1;
}

__setup("decay=", decay);

/*
 * Initialize the logical CPU number to SAPICID mapping
 */
void __init
smp_build_cpu_map (void)
{
	int sapicid, cpu, i;
	int boot_cpu_id = hard_smp_processor_id();

	for (cpu = 0; cpu < NR_CPUS; cpu++) {
		ia64_cpu_to_sapicid[cpu] = -1;
	}

	ia64_cpu_to_sapicid[0] = boot_cpu_id;
	cpus_clear(cpu_present_map);
	cpu_set(0, cpu_present_map);
	cpu_set(0, cpu_possible_map);
	for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
		sapicid = smp_boot_data.cpu_phys_id[i];
		if (sapicid == boot_cpu_id)
			continue;
		cpu_set(cpu, cpu_present_map);
		cpu_set(cpu, cpu_possible_map);
		ia64_cpu_to_sapicid[cpu] = sapicid;
		cpu++;
	}
}

/*
 * Cycle through the APs sending Wakeup IPIs to boot each.
 */
void __init
smp_prepare_cpus (unsigned int max_cpus)
{
	int boot_cpu_id = hard_smp_processor_id();

	/*
	 * Initialize the per-CPU profiling counter/multiplier
	 */

	smp_setup_percpu_timer();

	/*
	 * We have the boot CPU online for sure.
	 */
	cpu_set(0, cpu_online_map);
	cpu_set(0, cpu_callin_map);

	local_cpu_data->loops_per_jiffy = loops_per_jiffy;
	ia64_cpu_to_sapicid[0] = boot_cpu_id;

	printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);

	current_thread_info()->cpu = 0;

	/*
	 * If SMP should be disabled, then really disable it!
	 */
	if (!max_cpus) {
		printk(KERN_INFO "SMP mode deactivated.\n");
		cpus_clear(cpu_online_map);
		cpus_clear(cpu_present_map);
		cpus_clear(cpu_possible_map);
		cpu_set(0, cpu_online_map);
		cpu_set(0, cpu_present_map);
		cpu_set(0, cpu_possible_map);
		return;
	}
}

void __devinit smp_prepare_boot_cpu(void)
{
	cpu_set(smp_processor_id(), cpu_online_map);
	cpu_set(smp_processor_id(), cpu_callin_map);
649
	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
650
	paravirt_post_smp_prepare_boot_cpu();
L
Linus Torvalds 已提交
651 652 653
}

#ifdef CONFIG_HOTPLUG_CPU
654 655 656 657 658
static inline void
clear_cpu_sibling_map(int cpu)
{
	int i;

659 660
	for_each_cpu_mask(i, per_cpu(cpu_sibling_map, cpu))
		cpu_clear(cpu, per_cpu(cpu_sibling_map, i));
661 662 663
	for_each_cpu_mask(i, cpu_core_map[cpu])
		cpu_clear(cpu, cpu_core_map[i]);

664
	per_cpu(cpu_sibling_map, cpu) = cpu_core_map[cpu] = CPU_MASK_NONE;
665 666 667 668 669 670 671 672 673 674
}

static void
remove_siblinginfo(int cpu)
{
	int last = 0;

	if (cpu_data(cpu)->threads_per_core == 1 &&
	    cpu_data(cpu)->cores_per_socket == 1) {
		cpu_clear(cpu, cpu_core_map[cpu]);
675
		cpu_clear(cpu, per_cpu(cpu_sibling_map, cpu));
676 677 678 679 680 681 682 683 684
		return;
	}

	last = (cpus_weight(cpu_core_map[cpu]) == 1 ? 1 : 0);

	/* remove it from all sibling map's */
	clear_cpu_sibling_map(cpu);
}

L
Linus Torvalds 已提交
685
extern void fixup_irqs(void);
A
Ashok Raj 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

int migrate_platform_irqs(unsigned int cpu)
{
	int new_cpei_cpu;
	irq_desc_t *desc = NULL;
	cpumask_t 	mask;
	int 		retval = 0;

	/*
	 * dont permit CPEI target to removed.
	 */
	if (cpe_vector > 0 && is_cpu_cpei_target(cpu)) {
		printk ("CPU (%d) is CPEI Target\n", cpu);
		if (can_cpei_retarget()) {
			/*
			 * Now re-target the CPEI to a different processor
			 */
			new_cpei_cpu = any_online_cpu(cpu_online_map);
			mask = cpumask_of_cpu(new_cpei_cpu);
			set_cpei_target_cpu(new_cpei_cpu);
706
			desc = irq_desc + ia64_cpe_irq;
A
Ashok Raj 已提交
707
			/*
S
Simon Arlott 已提交
708
			 * Switch for now, immediately, we need to do fake intr
A
Ashok Raj 已提交
709 710 711 712
			 * as other interrupts, but need to study CPEI behaviour with
			 * polling before making changes.
			 */
			if (desc) {
713 714 715
				desc->chip->disable(ia64_cpe_irq);
				desc->chip->set_affinity(ia64_cpe_irq, mask);
				desc->chip->enable(ia64_cpe_irq);
A
Ashok Raj 已提交
716 717 718 719 720 721 722 723 724 725 726
				printk ("Re-targetting CPEI to cpu %d\n", new_cpei_cpu);
			}
		}
		if (!desc) {
			printk ("Unable to retarget CPEI, offline cpu [%d] failed\n", cpu);
			retval = -EBUSY;
		}
	}
	return retval;
}

L
Linus Torvalds 已提交
727 728 729 730 731 732 733 734
/* must be called with cpucontrol mutex held */
int __cpu_disable(void)
{
	int cpu = smp_processor_id();

	/*
	 * dont permit boot processor for now
	 */
A
Ashok Raj 已提交
735 736 737 738 739
	if (cpu == 0 && !bsp_remove_ok) {
		printk ("Your platform does not support removal of BSP\n");
		return (-EBUSY);
	}

740 741 742 743 744
	if (ia64_platform_is("sn2")) {
		if (!sn_cpu_disable_allowed(cpu))
			return -EBUSY;
	}

A
Ashok Raj 已提交
745 746 747 748 749 750
	cpu_clear(cpu, cpu_online_map);

	if (migrate_platform_irqs(cpu)) {
		cpu_set(cpu, cpu_online_map);
		return (-EBUSY);
	}
L
Linus Torvalds 已提交
751

752
	remove_siblinginfo(cpu);
Z
Zwane Mwaikambo 已提交
753
	cpu_clear(cpu, cpu_online_map);
L
Linus Torvalds 已提交
754 755
	fixup_irqs();
	local_flush_tlb_all();
756
	cpu_clear(cpu, cpu_callin_map);
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766 767
	return 0;
}

void __cpu_die(unsigned int cpu)
{
	unsigned int i;

	for (i = 0; i < 100; i++) {
		/* They ack this in play_dead by setting CPU_DEAD */
		if (per_cpu(cpu_state, cpu) == CPU_DEAD)
		{
768
			printk ("CPU %d is now offline\n", cpu);
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
			return;
		}
		msleep(100);
	}
 	printk(KERN_ERR "CPU %u didn't die...\n", cpu);
}
#endif /* CONFIG_HOTPLUG_CPU */

void
smp_cpus_done (unsigned int dummy)
{
	int cpu;
	unsigned long bogosum = 0;

	/*
	 * Allow the user to impress friends.
	 */

787 788 789
	for_each_online_cpu(cpu) {
		bogosum += cpu_data(cpu)->loops_per_jiffy;
	}
L
Linus Torvalds 已提交
790 791 792 793 794

	printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
	       (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
}

795 796 797 798 799 800 801 802 803 804
static inline void __devinit
set_cpu_sibling_map(int cpu)
{
	int i;

	for_each_online_cpu(i) {
		if ((cpu_data(cpu)->socket_id == cpu_data(i)->socket_id)) {
			cpu_set(i, cpu_core_map[cpu]);
			cpu_set(cpu, cpu_core_map[i]);
			if (cpu_data(cpu)->core_id == cpu_data(i)->core_id) {
805 806
				cpu_set(i, per_cpu(cpu_sibling_map, cpu));
				cpu_set(cpu, per_cpu(cpu_sibling_map, i));
807 808 809 810 811
			}
		}
	}
}

T
Tony Luck 已提交
812
int __cpuinit
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822
__cpu_up (unsigned int cpu)
{
	int ret;
	int sapicid;

	sapicid = ia64_cpu_to_sapicid[cpu];
	if (sapicid == -1)
		return -EINVAL;

	/*
823 824
	 * Already booted cpu? not valid anymore since we dont
	 * do idle loop tightspin anymore.
L
Linus Torvalds 已提交
825 826
	 */
	if (cpu_isset(cpu, cpu_callin_map))
827 828
		return -EINVAL;

829
	per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
L
Linus Torvalds 已提交
830 831 832 833 834
	/* Processor goes to start_secondary(), sets online flag */
	ret = do_boot_cpu(sapicid, cpu);
	if (ret < 0)
		return ret;

835 836
	if (cpu_data(cpu)->threads_per_core == 1 &&
	    cpu_data(cpu)->cores_per_socket == 1) {
837
		cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
838 839 840 841 842 843
		cpu_set(cpu, cpu_core_map[cpu]);
		return 0;
	}

	set_cpu_sibling_map(cpu);

L
Linus Torvalds 已提交
844 845 846 847
	return 0;
}

/*
S
Simon Arlott 已提交
848
 * Assume that CPUs have been discovered by some platform-dependent interface.  For
L
Linus Torvalds 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861
 * SoftSDV/Lion, that would be ACPI.
 *
 * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
 */
void __init
init_smp_config(void)
{
	struct fptr {
		unsigned long fp;
		unsigned long gp;
	} *ap_startup;
	long sal_ret;

S
Simon Arlott 已提交
862
	/* Tell SAL where to drop the APs.  */
L
Linus Torvalds 已提交
863 864 865 866 867 868 869 870
	ap_startup = (struct fptr *) start_ap;
	sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
				       ia64_tpa(ap_startup->fp), ia64_tpa(ap_startup->gp), 0, 0, 0, 0);
	if (sal_ret < 0)
		printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
		       ia64_sal_strerror(sal_ret));
}

871 872 873 874 875 876 877 878 879 880 881
/*
 * identify_siblings(cpu) gets called from identify_cpu. This populates the 
 * information related to logical execution units in per_cpu_data structure.
 */
void __devinit
identify_siblings(struct cpuinfo_ia64 *c)
{
	s64 status;
	u16 pltid;
	pal_logical_to_physical_t info;

882 883
	status = ia64_pal_logical_to_phys(-1, &info);
	if (status != PAL_STATUS_SUCCESS) {
884 885 886 887 888 889 890 891 892 893
		if (status != PAL_STATUS_UNIMPLEMENTED) {
			printk(KERN_ERR
				"ia64_pal_logical_to_phys failed with %ld\n",
				status);
			return;
		}

		info.overview_ppid = 0;
		info.overview_cpp  = 1;
		info.overview_tpc  = 1;
894
	}
895 896 897 898 899 900 901

	status = ia64_sal_physical_id_info(&pltid);
	if (status != PAL_STATUS_SUCCESS) {
		if (status != PAL_STATUS_UNIMPLEMENTED)
			printk(KERN_ERR
				"ia64_sal_pltid failed with %ld\n",
				status);
902 903 904 905
		return;
	}

	c->socket_id =  (pltid << 8) | info.overview_ppid;
906 907 908 909

	if (info.overview_cpp == 1 && info.overview_tpc == 1)
		return;

910 911
	c->cores_per_socket = info.overview_cpp;
	c->threads_per_core = info.overview_tpc;
912
	c->num_log = info.overview_num_log;
913

914 915
	c->core_id = info.log1_cid;
	c->thread_id = info.log1_tid;
916
}
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

/*
 * returns non zero, if multi-threading is enabled
 * on at least one physical package. Due to hotplug cpu
 * and (maxcpus=), all threads may not necessarily be enabled
 * even though the processor supports multi-threading.
 */
int is_multithreading_enabled(void)
{
	int i, j;

	for_each_present_cpu(i) {
		for_each_present_cpu(j) {
			if (j == i)
				continue;
			if ((cpu_data(j)->socket_id == cpu_data(i)->socket_id)) {
				if (cpu_data(j)->core_id == cpu_data(i)->core_id)
					return 1;
			}
		}
	}
	return 0;
}
EXPORT_SYMBOL_GPL(is_multithreading_enabled);