smpboot_64.c 26.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *	x86 SMP booting functions
 *
 *	(c) 1995 Alan Cox, Building #3 <alan@redhat.com>
 *	(c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
 *	Copyright 2001 Andi Kleen, SuSE Labs.
 *
 *	Much of the core SMP work is based on previous work by Thomas Radke, to
 *	whom a great many thanks are extended.
 *
 *	Thanks to Intel for making available several different Pentium,
 *	Pentium Pro and Pentium-II/Xeon MP machines.
 *	Original development of Linux SMP code supported by Caldera.
 *
15
 *	This code is released under the GNU General Public License version 2
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 *	Fixes
 *		Felix Koop	:	NR_CPUS used properly
 *		Jose Renau	:	Handle single CPU case.
 *		Alan Cox	:	By repeated request 8) - Total BogoMIP report.
 *		Greg Wright	:	Fix for kernel stacks panic.
 *		Erich Boleyn	:	MP v1.4 and additional changes.
 *	Matthias Sattler	:	Changes for 2.1 kernel map.
 *	Michel Lespinasse	:	Changes for 2.1 kernel map.
 *	Michael Chastain	:	Change trampoline.S to gnu as.
 *		Alan Cox	:	Dumb bug: 'B' step PPro's are fine
 *		Ingo Molnar	:	Added APIC timers, based on code
 *					from Jose Renau
 *		Ingo Molnar	:	various cleanups and rewrites
 *		Tigran Aivazian	:	fixed "0.00 in /proc/uptime on SMP" bug.
 *	Maciej W. Rozycki	:	Bits for genuine 82489DX APICs
 *	Andi Kleen		:	Changed for SMP boot into long mode.
33 34 35 36
 *		Rusty Russell	:	Hacked into shape for new "hotplug" boot process.
 *      Andi Kleen              :       Converted to new state machine.
 *					Various cleanups.
 *					Probably mostly hotplug CPU ready now.
A
Ashok Raj 已提交
37
 *	Ashok Raj			: CPU hotplug support
L
Linus Torvalds 已提交
38 39
 */

40

L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49
#include <linux/init.h>

#include <linux/mm.h>
#include <linux/kernel_stat.h>
#include <linux/bootmem.h>
#include <linux/thread_info.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/mc146818rtc.h>
50
#include <linux/smp.h>
51
#include <linux/kdebug.h>
52

L
Linus Torvalds 已提交
53 54 55 56 57
#include <asm/mtrr.h>
#include <asm/pgalloc.h>
#include <asm/desc.h>
#include <asm/tlbflush.h>
#include <asm/proto.h>
58
#include <asm/nmi.h>
59 60
#include <asm/irq.h>
#include <asm/hw_irq.h>
61
#include <asm/numa.h>
L
Linus Torvalds 已提交
62 63 64

/* Number of siblings per CPU package */
int smp_num_siblings = 1;
65
EXPORT_SYMBOL(smp_num_siblings);
L
Linus Torvalds 已提交
66

67
/* Last level cache ID of each logical CPU */
68
DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
69

L
Linus Torvalds 已提交
70
/* Bitmask of currently online CPUs */
71
cpumask_t cpu_online_map __read_mostly;
L
Linus Torvalds 已提交
72

73 74 75 76 77 78
EXPORT_SYMBOL(cpu_online_map);

/*
 * Private maps to synchronize booting between AP and BP.
 * Probably not needed anymore, but it makes for easier debugging. -AK
 */
L
Linus Torvalds 已提交
79 80
cpumask_t cpu_callin_map;
cpumask_t cpu_callout_map;
81 82
cpumask_t cpu_possible_map;
EXPORT_SYMBOL(cpu_possible_map);
L
Linus Torvalds 已提交
83 84

/* Per CPU bogomips and other parameters */
85 86
DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
EXPORT_PER_CPU_SYMBOL(cpu_info);
L
Linus Torvalds 已提交
87

88 89 90
/* Set when the idlers are all forked */
int smp_threads_ready;

91
/* representing HT siblings of each logical CPU */
92 93
DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
94 95

/* representing HT and core siblings of each logical CPU */
96 97
DEFINE_PER_CPU(cpumask_t, cpu_core_map);
EXPORT_PER_CPU_SYMBOL(cpu_core_map);
L
Linus Torvalds 已提交
98 99 100 101 102

/*
 * Trampoline 80x86 program as an array.
 */

J
Jan Beulich 已提交
103 104
extern const unsigned char trampoline_data[];
extern const unsigned char trampoline_end[];
L
Linus Torvalds 已提交
105

A
Ashok Raj 已提交
106 107 108 109 110 111 112 113
/* State of each CPU */
DEFINE_PER_CPU(int, cpu_state) = { 0 };

/*
 * Store all idle threads, this can be reused instead of creating
 * a new thread. Also avoids complicated thread destroy functionality
 * for idle threads.
 */
114 115 116 117 118 119 120 121 122
#ifdef CONFIG_HOTPLUG_CPU
/*
 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
 * removed after init for !CONFIG_HOTPLUG_CPU.
 */
static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
#define get_idle_for_cpu(x)     (per_cpu(idle_thread_array, x))
#define set_idle_for_cpu(x,p)   (per_cpu(idle_thread_array, x) = (p))
#else
A
Ashok Raj 已提交
123 124 125
struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
#define get_idle_for_cpu(x)     (idle_thread_array[(x)])
#define set_idle_for_cpu(x,p)   (idle_thread_array[(x)] = (p))
126 127
#endif

A
Ashok Raj 已提交
128

L
Linus Torvalds 已提交
129 130 131 132 133 134
/*
 * Currently trivial. Write the real->protected mode
 * bootstrap into the page concerned. The caller
 * has made sure it's suitably aligned.
 */

135
static unsigned long __cpuinit setup_trampoline(void)
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146
{
	void *tramp = __va(SMP_TRAMPOLINE_BASE); 
	memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
	return virt_to_phys(tramp);
}

/*
 * The bootstrap kernel entry code has set these up. Save them for
 * a given CPU
 */

147
static void __cpuinit smp_store_cpu_info(int id)
L
Linus Torvalds 已提交
148
{
149
	struct cpuinfo_x86 *c = &cpu_data(id);
L
Linus Torvalds 已提交
150 151

	*c = boot_cpu_data;
152
	c->cpu_index = id;
153
	identify_cpu(c);
154
	print_cpu_info(c);
L
Linus Torvalds 已提交
155 156
}

157
static atomic_t init_deasserted __cpuinitdata;
L
Linus Torvalds 已提交
158

159 160 161 162 163
/*
 * Report back to the Boot Processor.
 * Running on AP.
 */
void __cpuinit smp_callin(void)
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173
{
	int cpuid, phys_id;
	unsigned long timeout;

	/*
	 * If waken up by an INIT in an 82489DX configuration
	 * we may get here before an INIT-deassert IPI reaches
	 * our local APIC.  We have to wait for the IPI or we'll
	 * lock up on an APIC access.
	 */
174 175
	while (!atomic_read(&init_deasserted))
		cpu_relax();
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	/*
	 * (This works even if the APIC is not enabled.)
	 */
	phys_id = GET_APIC_ID(apic_read(APIC_ID));
	cpuid = smp_processor_id();
	if (cpu_isset(cpuid, cpu_callin_map)) {
		panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
					phys_id, cpuid);
	}
	Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);

	/*
	 * STARTUP IPIs are fragile beasts as they might sometimes
	 * trigger some glue motherboard logic. Complete APIC bus
	 * silence for 1 second, this overestimates the time the
	 * boot CPU is spending to send the up to 2 STARTUP IPIs
	 * by a factor of two. This should be enough.
	 */

	/*
	 * Waiting 2s total for startup (udelay is not yet working)
	 */
	timeout = jiffies + 2*HZ;
	while (time_before(jiffies, timeout)) {
		/*
		 * Has the boot CPU finished it's STARTUP sequence?
		 */
		if (cpu_isset(cpuid, cpu_callout_map))
			break;
206
		cpu_relax();
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	}

	if (!time_before(jiffies, timeout)) {
		panic("smp_callin: CPU%d started up but did not get a callout!\n",
			cpuid);
	}

	/*
	 * the boot CPU has finished the init stage and is spinning
	 * on callin_map until we finish. We are free to set up this
	 * CPU, first the APIC. (this is probably redundant on most
	 * boards)
	 */

	Dprintk("CALLIN, before setup_local_APIC().\n");
	setup_local_APIC();
223
	end_local_APIC_setup();
L
Linus Torvalds 已提交
224 225 226

	/*
	 * Get our bogomips.
227 228 229
 	 *
 	 * Need to enable IRQs because it can take longer and then
	 * the NMI watchdog might kill us.
L
Linus Torvalds 已提交
230
	 */
231
	local_irq_enable();
L
Linus Torvalds 已提交
232
	calibrate_delay();
233
	local_irq_disable();
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246
	Dprintk("Stack at about %p\n",&cpuid);

	/*
	 * Save our processor parameters
	 */
 	smp_store_cpu_info(cpuid);

	/*
	 * Allow the master to continue.
	 */
	cpu_set(cpuid, cpu_callin_map);
}

247 248 249
/* maps the cpu to the sched domain representing multi-core */
cpumask_t cpu_coregroup_map(int cpu)
{
250
	struct cpuinfo_x86 *c = &cpu_data(cpu);
251 252
	/*
	 * For perf, we return last level cache shared map.
253
	 * And for power savings, we return cpu_core_map
254
	 */
255
	if (sched_mc_power_savings || sched_smt_power_savings)
256
		return per_cpu(cpu_core_map, cpu);
257 258
	else
		return c->llc_shared_map;
259 260
}

261 262 263
/* representing cpus for which sibling maps can be computed */
static cpumask_t cpu_sibling_setup_map;

264 265 266
static inline void set_cpu_sibling_map(int cpu)
{
	int i;
267
	struct cpuinfo_x86 *c = &cpu_data(cpu);
268 269

	cpu_set(cpu, cpu_sibling_setup_map);
270 271

	if (smp_num_siblings > 1) {
272
		for_each_cpu_mask(i, cpu_sibling_setup_map) {
273 274
			if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
			    c->cpu_core_id == cpu_data(i).cpu_core_id) {
275 276
				cpu_set(i, per_cpu(cpu_sibling_map, cpu));
				cpu_set(cpu, per_cpu(cpu_sibling_map, i));
277 278
				cpu_set(i, per_cpu(cpu_core_map, cpu));
				cpu_set(cpu, per_cpu(cpu_core_map, i));
279 280
				cpu_set(i, c->llc_shared_map);
				cpu_set(cpu, cpu_data(i).llc_shared_map);
281 282 283
			}
		}
	} else {
284
		cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
285 286
	}

287
	cpu_set(cpu, c->llc_shared_map);
288

289
	if (current_cpu_data.x86_max_cores == 1) {
290
		per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
291
		c->booted_cores = 1;
292 293 294 295
		return;
	}

	for_each_cpu_mask(i, cpu_sibling_setup_map) {
296 297
		if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
		    per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
298 299
			cpu_set(i, c->llc_shared_map);
			cpu_set(cpu, cpu_data(i).llc_shared_map);
300
		}
301
		if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
302 303
			cpu_set(i, per_cpu(cpu_core_map, cpu));
			cpu_set(cpu, per_cpu(cpu_core_map, i));
304 305 306
			/*
			 *  Does this new cpu bringup a new core?
			 */
307
			if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
308 309 310 311
				/*
				 * for each core in package, increment
				 * the booted_cores for this new cpu
				 */
312
				if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
313
					c->booted_cores++;
314 315 316 317 318
				/*
				 * increment the core count for all
				 * the other cpus in this package
				 */
				if (i != cpu)
319 320 321
					cpu_data(i).booted_cores++;
			} else if (i != cpu && !c->booted_cores)
				c->booted_cores = cpu_data(i).booted_cores;
322
		}
323 324 325
	}
}

L
Linus Torvalds 已提交
326
/*
327
 * Setup code on secondary processor (after comming out of the trampoline)
L
Linus Torvalds 已提交
328
 */
329
void __cpuinit start_secondary(void)
L
Linus Torvalds 已提交
330 331 332 333 334 335 336
{
	/*
	 * Dont put anything before smp_callin(), SMP
	 * booting is too fragile that we want to limit the
	 * things done here to the most necessary things.
	 */
	cpu_init();
337
	preempt_disable();
L
Linus Torvalds 已提交
338 339 340 341 342
	smp_callin();

	/* otherwise gcc will move up the smp_processor_id before the cpu_init */
	barrier();

343 344 345 346 347
	/*
  	 * Check TSC sync first:
 	 */
	check_tsc_sync_target();

L
Linus Torvalds 已提交
348 349
	if (nmi_watchdog == NMI_IO_APIC) {
		disable_8259A_irq(0);
350
		enable_NMI_through_LVT0();
L
Linus Torvalds 已提交
351 352 353
		enable_8259A_irq(0);
	}

354 355 356 357 358 359
	/*
	 * The sibling maps must be set before turing the online map on for
	 * this cpu
	 */
	set_cpu_sibling_map(smp_processor_id());

360 361 362
	/*
	 * We need to hold call_lock, so there is no inconsistency
	 * between the time smp_call_function() determines number of
S
Simon Arlott 已提交
363
	 * IPI recipients, and the time when the determination is made
364 365 366 367 368
	 * for which cpus receive the IPI in genapic_flat.c. Holding this
	 * lock helps us to not include this cpu in a currently in progress
	 * smp_call_function().
	 */
	lock_ipi_call_lock();
369
	spin_lock(&vector_lock);
370

371 372
	/* Setup the per cpu irq handling data structures */
	__setup_vector_irq(smp_processor_id());
L
Linus Torvalds 已提交
373
	/*
374
	 * Allow the master to continue.
L
Linus Torvalds 已提交
375 376
	 */
	cpu_set(smp_processor_id(), cpu_online_map);
377
	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
378
	spin_unlock(&vector_lock);
379

380 381
	unlock_ipi_call_lock();

382
	setup_secondary_clock();
383

L
Linus Torvalds 已提交
384 385 386
	cpu_idle();
}

387
extern volatile unsigned long init_rsp;
L
Linus Torvalds 已提交
388 389
extern void (*initial_code)(void);

390
#ifdef APIC_DEBUG
391
static void inquire_remote_apic(int apicid)
L
Linus Torvalds 已提交
392 393 394
{
	unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
	char *names[] = { "ID", "VERSION", "SPIV" };
395
	int timeout;
396
	u32 status;
L
Linus Torvalds 已提交
397 398 399

	printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);

A
Alejandro Martinez Ruiz 已提交
400
	for (i = 0; i < ARRAY_SIZE(regs); i++) {
401
		printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
L
Linus Torvalds 已提交
402 403 404 405

		/*
		 * Wait for idle.
		 */
406 407
		status = safe_apic_wait_icr_idle();
		if (status)
408 409
			printk(KERN_CONT
			       "a previous APIC delivery may have failed\n");
L
Linus Torvalds 已提交
410

411 412
		apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
		apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422

		timeout = 0;
		do {
			udelay(100);
			status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
		} while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);

		switch (status) {
		case APIC_ICR_RR_VALID:
			status = apic_read(APIC_RRR);
423
			printk(KERN_CONT "%08x\n", status);
L
Linus Torvalds 已提交
424 425
			break;
		default:
426
			printk(KERN_CONT "failed\n");
L
Linus Torvalds 已提交
427 428 429 430 431
		}
	}
}
#endif

432 433 434 435
/*
 * Kick the secondary to wake up.
 */
static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
L
Linus Torvalds 已提交
436
{
437 438
	unsigned long send_status, accept_status = 0;
	int maxlvt, num_starts, j;
L
Linus Torvalds 已提交
439 440 441 442 443 444

	Dprintk("Asserting INIT.\n");

	/*
	 * Turn INIT on target chip
	 */
445
	apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
L
Linus Torvalds 已提交
446 447 448 449

	/*
	 * Send IPI
	 */
450
	apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
L
Linus Torvalds 已提交
451 452 453
				| APIC_DM_INIT);

	Dprintk("Waiting for send to finish...\n");
454
	send_status = safe_apic_wait_icr_idle();
L
Linus Torvalds 已提交
455 456 457 458 459 460

	mdelay(10);

	Dprintk("Deasserting INIT.\n");

	/* Target chip */
461
	apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
L
Linus Torvalds 已提交
462 463

	/* Send IPI */
464
	apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
L
Linus Torvalds 已提交
465 466

	Dprintk("Waiting for send to finish...\n");
467
	send_status = safe_apic_wait_icr_idle();
L
Linus Torvalds 已提交
468

469
	mb();
L
Linus Torvalds 已提交
470 471
	atomic_set(&init_deasserted, 1);

472
	num_starts = 2;
L
Linus Torvalds 已提交
473 474 475 476 477 478

	/*
	 * Run STARTUP IPI loop.
	 */
	Dprintk("#startup loops: %d.\n", num_starts);

479
	maxlvt = lapic_get_maxlvt();
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488 489 490 491

	for (j = 1; j <= num_starts; j++) {
		Dprintk("Sending STARTUP #%d.\n",j);
		apic_write(APIC_ESR, 0);
		apic_read(APIC_ESR);
		Dprintk("After apic_write.\n");

		/*
		 * STARTUP IPI
		 */

		/* Target chip */
492
		apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
L
Linus Torvalds 已提交
493 494 495

		/* Boot on the stack */
		/* Kick the second */
496
		apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505

		/*
		 * Give the other CPU some time to accept the IPI.
		 */
		udelay(300);

		Dprintk("Startup point 1.\n");

		Dprintk("Waiting for send to finish...\n");
506
		send_status = safe_apic_wait_icr_idle();
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

		/*
		 * Give the other CPU some time to accept the IPI.
		 */
		udelay(200);
		/*
		 * Due to the Pentium erratum 3AP.
		 */
		if (maxlvt > 3) {
			apic_write(APIC_ESR, 0);
		}
		accept_status = (apic_read(APIC_ESR) & 0xEF);
		if (send_status || accept_status)
			break;
	}
	Dprintk("After Startup.\n");

	if (send_status)
		printk(KERN_ERR "APIC never delivered???\n");
	if (accept_status)
		printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);

	return (send_status | accept_status);
}

A
Ashok Raj 已提交
532
struct create_idle {
533
	struct work_struct work;
A
Ashok Raj 已提交
534 535 536 537 538
	struct task_struct *idle;
	struct completion done;
	int cpu;
};

539
static void __cpuinit do_fork_idle(struct work_struct *work)
A
Ashok Raj 已提交
540
{
541 542
	struct create_idle *c_idle =
		container_of(work, struct create_idle, work);
A
Ashok Raj 已提交
543 544 545 546 547

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

548 549 550 551
/*
 * Boot one CPU.
 */
static int __cpuinit do_boot_cpu(int cpu, int apicid)
L
Linus Torvalds 已提交
552 553
{
	unsigned long boot_error;
554
	int timeout;
L
Linus Torvalds 已提交
555
	unsigned long start_rip;
A
Ashok Raj 已提交
556
	struct create_idle c_idle = {
557
		.work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
A
Ashok Raj 已提交
558
		.cpu = cpu,
559
		.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
A
Ashok Raj 已提交
560 561
	};

562 563 564 565 566 567 568
	/* allocate memory for gdts of secondary cpus. Hotplug is considered */
	if (!cpu_gdt_descr[cpu].address &&
		!(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
		printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
		return -1;
	}

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	/* Allocate node local memory for AP pdas */
	if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
		struct x8664_pda *newpda, *pda;
		int node = cpu_to_node(cpu);
		pda = cpu_pda(cpu);
		newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
				      node);
		if (newpda) {
			memcpy(newpda, pda, sizeof (struct x8664_pda));
			cpu_pda(cpu) = newpda;
		} else
			printk(KERN_ERR
		"Could not allocate node local PDA for CPU %d on node %d\n",
				cpu, node);
	}

585 586
	alternatives_smp_switch(1);

A
Ashok Raj 已提交
587 588 589
	c_idle.idle = get_idle_for_cpu(cpu);

	if (c_idle.idle) {
590
		c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
A
Al Viro 已提交
591
			(THREAD_SIZE +  task_stack_page(c_idle.idle))) - 1);
A
Ashok Raj 已提交
592 593 594 595
		init_idle(c_idle.idle, cpu);
		goto do_rest;
	}

L
Linus Torvalds 已提交
596
	/*
A
Ashok Raj 已提交
597 598 599 600 601 602 603 604
	 * During cold boot process, keventd thread is not spun up yet.
	 * When we do cpu hot-add, we create idle threads on the fly, we should
	 * not acquire any attributes from the calling context. Hence the clean
	 * way to create kernel_threads() is to do that from keventd().
	 * We do the current_is_keventd() due to the fact that ACPI notifier
	 * was also queuing to keventd() and when the caller is already running
	 * in context of keventd(), we would end up with locking up the keventd
	 * thread.
L
Linus Torvalds 已提交
605
	 */
A
Ashok Raj 已提交
606
	if (!keventd_up() || current_is_keventd())
607
		c_idle.work.func(&c_idle.work);
A
Ashok Raj 已提交
608
	else {
609
		schedule_work(&c_idle.work);
A
Ashok Raj 已提交
610 611 612 613
		wait_for_completion(&c_idle.done);
	}

	if (IS_ERR(c_idle.idle)) {
614
		printk("failed fork for CPU %d\n", cpu);
A
Ashok Raj 已提交
615
		return PTR_ERR(c_idle.idle);
616
	}
L
Linus Torvalds 已提交
617

A
Ashok Raj 已提交
618 619 620 621
	set_idle_for_cpu(cpu, c_idle.idle);

do_rest:

622
	cpu_pda(cpu)->pcurrent = c_idle.idle;
L
Linus Torvalds 已提交
623 624 625

	start_rip = setup_trampoline();

626
	init_rsp = c_idle.idle->thread.sp;
627
	load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
L
Linus Torvalds 已提交
628
	initial_code = start_secondary;
A
Al Viro 已提交
629
	clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
L
Linus Torvalds 已提交
630

631 632 633
	printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
		cpus_weight(cpu_present_map),
		apicid);
L
Linus Torvalds 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

	/*
	 * This grunge runs the startup process for
	 * the targeted processor.
	 */

	atomic_set(&init_deasserted, 0);

	Dprintk("Setting warm reset code and vector.\n");

	CMOS_WRITE(0xa, 0xf);
	local_flush_tlb();
	Dprintk("1.\n");
	*((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
	Dprintk("2.\n");
	*((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
	Dprintk("3.\n");

	/*
	 * Be paranoid about clearing APIC errors.
	 */
655 656
	apic_write(APIC_ESR, 0);
	apic_read(APIC_ESR);
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665

	/*
	 * Status is now clean
	 */
	boot_error = 0;

	/*
	 * Starting actual IPI sequence...
	 */
666
	boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

	if (!boot_error) {
		/*
		 * allow APs to start initializing.
		 */
		Dprintk("Before Callout %d.\n", cpu);
		cpu_set(cpu, cpu_callout_map);
		Dprintk("After Callout %d.\n", cpu);

		/*
		 * Wait 5s total for a response
		 */
		for (timeout = 0; timeout < 50000; timeout++) {
			if (cpu_isset(cpu, cpu_callin_map))
				break;	/* It has booted */
			udelay(100);
		}

		if (cpu_isset(cpu, cpu_callin_map)) {
			/* number CPUs logically, starting from 1 (BSP is 0) */
			Dprintk("CPU has booted.\n");
		} else {
			boot_error = 1;
			if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
					== 0xA5)
				/* trampoline started but...? */
				printk("Stuck ??\n");
			else
				/* trampoline code not run */
				printk("Not responding.\n");
697
#ifdef APIC_DEBUG
L
Linus Torvalds 已提交
698 699 700 701 702 703
			inquire_remote_apic(apicid);
#endif
		}
	}
	if (boot_error) {
		cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
704
		clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
705
		clear_node_cpumask(cpu); /* was set by numa_add_cpu */
706 707
		cpu_clear(cpu, cpu_present_map);
		cpu_clear(cpu, cpu_possible_map);
708
		per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
709
		return -EIO;
L
Linus Torvalds 已提交
710
	}
711 712

	return 0;
L
Linus Torvalds 已提交
713 714
}

715 716 717
cycles_t cacheflush_time;
unsigned long cache_decay_ticks;

L
Linus Torvalds 已提交
718
/*
719
 * Cleanup possible dangling ends...
L
Linus Torvalds 已提交
720
 */
721
static __cpuinit void smp_cleanup_boot(void)
L
Linus Torvalds 已提交
722
{
723 724 725 726 727
	/*
	 * Paranoid:  Set warm reset code and vector here back
	 * to default values.
	 */
	CMOS_WRITE(0, 0xf);
L
Linus Torvalds 已提交
728

729 730 731 732 733 734 735 736 737 738 739
	/*
	 * Reset trampoline flag
	 */
	*((volatile int *) phys_to_virt(0x467)) = 0;
}

/*
 * Fall back to non SMP mode after errors.
 *
 * RED-PEN audit/test this more. I bet there is more state messed up here.
 */
740
static __init void disable_smp(void)
741 742 743 744 745 746 747
{
	cpu_present_map = cpumask_of_cpu(0);
	cpu_possible_map = cpumask_of_cpu(0);
	if (smp_found_config)
		phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
	else
		phys_cpu_present_map = physid_mask_of_physid(0);
748
	cpu_set(0, per_cpu(cpu_sibling_map, 0));
749
	cpu_set(0, per_cpu(cpu_core_map, 0));
750 751
}

752
#ifdef CONFIG_HOTPLUG_CPU
753 754 755

int additional_cpus __initdata = -1;

756 757 758 759 760 761 762 763 764
/*
 * cpu_possible_map should be static, it cannot change as cpu's
 * are onlined, or offlined. The reason is per-cpu data-structures
 * are allocated by some modules at init time, and dont expect to
 * do this dynamically on cpu arrival/departure.
 * cpu_present_map on the other hand can change dynamically.
 * In case when cpu_hotplug is not compiled, then we resort to current
 * behaviour, which is cpu_possible == cpu_present.
 * - Ashok Raj
765 766 767 768
 *
 * Three ways to find out the number of additional hotplug CPUs:
 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
 * - The user can overwrite it with additional_cpus=NUM
769
 * - Otherwise don't reserve additional CPUs.
770 771
 * We do this because additional CPUs waste a lot of memory.
 * -AK
772
 */
773
__init void prefill_possible_map(void)
774 775
{
	int i;
776 777 778
	int possible;

 	if (additional_cpus == -1) {
779
 		if (disabled_cpus > 0)
780
 			additional_cpus = disabled_cpus;
781 782
 		else
			additional_cpus = 0;
783 784 785 786 787 788 789 790 791 792
 	}
	possible = num_processors + additional_cpus;
	if (possible > NR_CPUS) 
		possible = NR_CPUS;

	printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
		possible,
	        max_t(int, possible - num_processors, 0));

	for (i = 0; i < possible; i++)
793 794 795 796
		cpu_set(i, cpu_possible_map);
}
#endif

797 798 799
/*
 * Various sanity checks.
 */
800
static int __init smp_sanity_check(unsigned max_cpus)
801
{
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813
	if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
		printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
		       hard_smp_processor_id());
		physid_set(hard_smp_processor_id(), phys_cpu_present_map);
	}

	/*
	 * If we couldn't find an SMP configuration at boot time,
	 * get out of here now!
	 */
	if (!smp_found_config) {
		printk(KERN_NOTICE "SMP motherboard not detected.\n");
814
		disable_smp();
L
Linus Torvalds 已提交
815 816 817
		if (APIC_init_uniprocessor())
			printk(KERN_NOTICE "Local APIC not detected."
					   " Using dummy APIC emulation.\n");
818
		return -1;
L
Linus Torvalds 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	}

	/*
	 * Should not be necessary because the MP table should list the boot
	 * CPU too, but we do it for the sake of robustness anyway.
	 */
	if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
		printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
								 boot_cpu_id);
		physid_set(hard_smp_processor_id(), phys_cpu_present_map);
	}

	/*
	 * If we couldn't find a local APIC, then get out of here now!
	 */
834
	if (!cpu_has_apic) {
L
Linus Torvalds 已提交
835 836 837
		printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
			boot_cpu_id);
		printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
838 839
		nr_ioapics = 0;
		return -1;
L
Linus Torvalds 已提交
840 841 842 843 844 845 846
	}

	/*
	 * If SMP should be disabled, then really disable it!
	 */
	if (!max_cpus) {
		printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
847 848
		nr_ioapics = 0;
		return -1;
L
Linus Torvalds 已提交
849 850
	}

851 852
	return 0;
}
L
Linus Torvalds 已提交
853

854 855 856 857 858 859 860 861 862 863 864 865
static void __init smp_cpu_index_default(void)
{
	int i;
	struct cpuinfo_x86 *c;

	for_each_cpu_mask(i, cpu_possible_map) {
		c = &cpu_data(i);
		/* mark all to hotplug */
		c->cpu_index = NR_CPUS;
	}
}

866 867 868 869
/*
 * Prepare for SMP bootup.  The MP table or ACPI has been read
 * earlier.  Just do some sanity checking here and enable APIC mode.
 */
870
void __init smp_prepare_cpus(unsigned int max_cpus)
871 872
{
	nmi_watchdog_default();
873
	smp_cpu_index_default();
874 875
	current_cpu_data = boot_cpu_data;
	current_thread_info()->cpu = 0;  /* needed? */
876
	set_cpu_sibling_map(0);
L
Linus Torvalds 已提交
877

878 879 880 881
	if (smp_sanity_check(max_cpus) < 0) {
		printk(KERN_INFO "SMP disabled\n");
		disable_smp();
		return;
L
Linus Torvalds 已提交
882 883
	}

884

L
Linus Torvalds 已提交
885
	/*
886
	 * Switch from PIC to APIC mode.
L
Linus Torvalds 已提交
887
	 */
888
	setup_local_APIC();
L
Linus Torvalds 已提交
889

890 891 892 893 894 895 896
	/*
	 * Enable IO APIC before setting up error vector
	 */
	if (!skip_ioapic_setup && nr_ioapics)
		enable_IO_APIC();
	end_local_APIC_setup();

897 898 899 900
	if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
		panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
		      GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
		/* Or can we switch back to PIC here? */
L
Linus Torvalds 已提交
901 902 903
	}

	/*
904
	 * Now start the IO-APICs
L
Linus Torvalds 已提交
905 906 907 908 909 910 911
	 */
	if (!skip_ioapic_setup && nr_ioapics)
		setup_IO_APIC();
	else
		nr_ioapics = 0;

	/*
912
	 * Set up local APIC timer on boot CPU.
L
Linus Torvalds 已提交
913 914
	 */

915
	setup_boot_clock();
L
Linus Torvalds 已提交
916 917
}

918 919 920 921
/*
 * Early setup to make printk work.
 */
void __init smp_prepare_boot_cpu(void)
L
Linus Torvalds 已提交
922
{
923
	int me = smp_processor_id();
924
	/* already set me in cpu_online_map in boot_cpu_init() */
925
	cpu_set(me, cpu_callout_map);
926
	per_cpu(cpu_state, me) = CPU_ONLINE;
L
Linus Torvalds 已提交
927 928
}

929 930 931 932
/*
 * Entry point to boot a CPU.
 */
int __cpuinit __cpu_up(unsigned int cpu)
L
Linus Torvalds 已提交
933
{
934
	int apicid = cpu_present_to_apicid(cpu);
935 936
	unsigned long flags;
	int err;
L
Linus Torvalds 已提交
937

938
	WARN_ON(irqs_disabled());
L
Linus Torvalds 已提交
939

940 941 942 943 944 945 946 947
	Dprintk("++++++++++++++++++++=_---CPU UP  %u\n", cpu);

	if (apicid == BAD_APICID || apicid == boot_cpu_id ||
	    !physid_isset(apicid, phys_cpu_present_map)) {
		printk("__cpu_up: bad cpu %d\n", cpu);
		return -EINVAL;
	}

A
Ashok Raj 已提交
948 949 950 951 952 953 954 955
	/*
	 * Already booted CPU?
	 */
 	if (cpu_isset(cpu, cpu_callin_map)) {
		Dprintk("do_boot_cpu %d Already started\n", cpu);
 		return -ENOSYS;
	}

956 957 958 959 960 961
	/*
	 * Save current MTRR state in case it was changed since early boot
	 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
	 */
	mtrr_save_state();

962
	per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
963 964 965 966 967
	/* Boot it! */
	err = do_boot_cpu(cpu, apicid);
	if (err < 0) {
		Dprintk("do_boot_cpu failed %d\n", err);
		return err;
L
Linus Torvalds 已提交
968
	}
969

L
Linus Torvalds 已提交
970 971 972
	/* Unleash the CPU! */
	Dprintk("waiting for cpu %d\n", cpu);

973 974 975
	/*
  	 * Make sure and check TSC sync:
 	 */
976
	local_irq_save(flags);
977
	check_tsc_sync_source(cpu);
978
	local_irq_restore(flags);
979

L
Linus Torvalds 已提交
980
	while (!cpu_isset(cpu, cpu_online_map))
981
		cpu_relax();
A
Ashok Raj 已提交
982 983 984
	err = 0;

	return err;
L
Linus Torvalds 已提交
985 986
}

987 988 989
/*
 * Finish the SMP boot.
 */
990
void __init smp_cpus_done(unsigned int max_cpus)
L
Linus Torvalds 已提交
991
{
992
	smp_cleanup_boot();
L
Linus Torvalds 已提交
993
	setup_ioapic_dest();
994
	check_nmi_watchdog();
995
}
A
Ashok Raj 已提交
996 997 998

#ifdef CONFIG_HOTPLUG_CPU

999
static void remove_siblinginfo(int cpu)
A
Ashok Raj 已提交
1000 1001
{
	int sibling;
1002
	struct cpuinfo_x86 *c = &cpu_data(cpu);
A
Ashok Raj 已提交
1003

1004 1005
	for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
		cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
1006 1007 1008
		/*
		 * last thread sibling in this cpu core going down
		 */
1009
		if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
1010
			cpu_data(sibling).booted_cores--;
1011 1012
	}
			
1013 1014 1015
	for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
		cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
	cpus_clear(per_cpu(cpu_sibling_map, cpu));
1016
	cpus_clear(per_cpu(cpu_core_map, cpu));
1017 1018
	c->phys_proc_id = 0;
	c->cpu_core_id = 0;
1019
	cpu_clear(cpu, cpu_sibling_setup_map);
A
Ashok Raj 已提交
1020 1021
}

1022
static void __ref remove_cpu_from_maps(void)
A
Ashok Raj 已提交
1023 1024 1025 1026 1027
{
	int cpu = smp_processor_id();

	cpu_clear(cpu, cpu_callout_map);
	cpu_clear(cpu, cpu_callin_map);
1028
	clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
1029
	clear_node_cpumask(cpu);
A
Ashok Raj 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
}

int __cpu_disable(void)
{
	int cpu = smp_processor_id();

	/*
	 * Perhaps use cpufreq to drop frequency, but that could go
	 * into generic code.
 	 *
	 * We won't take down the boot processor on i386 due to some
	 * interrupts only being able to be serviced by the BSP.
	 * Especially so if we're not using an IOAPIC	-zwane
	 */
	if (cpu == 0)
		return -EBUSY;

1047 1048
	if (nmi_watchdog == NMI_LOCAL_APIC)
		stop_apic_nmi_watchdog(NULL);
1049
	clear_local_APIC();
A
Ashok Raj 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

	/*
	 * HACK:
	 * Allow any queued timer interrupts to get serviced
	 * This is only a temporary solution until we cleanup
	 * fixup_irqs as we do for IA64.
	 */
	local_irq_enable();
	mdelay(1);

	local_irq_disable();
	remove_siblinginfo(cpu);

1063
	spin_lock(&vector_lock);
A
Ashok Raj 已提交
1064 1065
	/* It's now safe to remove this processor from the online map */
	cpu_clear(cpu, cpu_online_map);
1066
	spin_unlock(&vector_lock);
A
Ashok Raj 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	remove_cpu_from_maps();
	fixup_irqs(cpu_online_map);
	return 0;
}

void __cpu_die(unsigned int cpu)
{
	/* We don't do anything here: idle task is faking death itself. */
	unsigned int i;

	for (i = 0; i < 10; i++) {
		/* They ack this in play_dead by setting CPU_DEAD */
1079 1080
		if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
			printk ("CPU %d is now offline\n", cpu);
1081 1082
			if (1 == num_online_cpus())
				alternatives_smp_switch(0);
A
Ashok Raj 已提交
1083
			return;
1084
		}
1085
		msleep(100);
A
Ashok Raj 已提交
1086 1087 1088 1089
	}
 	printk(KERN_ERR "CPU %u didn't die...\n", cpu);
}

1090
static __init int setup_additional_cpus(char *s)
1091
{
1092
	return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
1093
}
1094
early_param("additional_cpus", setup_additional_cpus);
1095

A
Ashok Raj 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
#else /* ... !CONFIG_HOTPLUG_CPU */

int __cpu_disable(void)
{
	return -ENOSYS;
}

void __cpu_die(unsigned int cpu)
{
	/* We said "no" in __cpu_disable */
	BUG();
}
#endif /* CONFIG_HOTPLUG_CPU */