smpboot.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * x86 SMP booting functions
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  5. * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
  6. * Copyright 2001 Andi Kleen, SuSE Labs.
  7. *
  8. * Much of the core SMP work is based on previous work by Thomas Radke, to
  9. * whom a great many thanks are extended.
  10. *
  11. * Thanks to Intel for making available several different Pentium,
  12. * Pentium Pro and Pentium-II/Xeon MP machines.
  13. * Original development of Linux SMP code supported by Caldera.
  14. *
  15. * This code is released under the GNU General Public License version 2
  16. *
  17. * Fixes
  18. * Felix Koop : NR_CPUS used properly
  19. * Jose Renau : Handle single CPU case.
  20. * Alan Cox : By repeated request 8) - Total BogoMIP report.
  21. * Greg Wright : Fix for kernel stacks panic.
  22. * Erich Boleyn : MP v1.4 and additional changes.
  23. * Matthias Sattler : Changes for 2.1 kernel map.
  24. * Michel Lespinasse : Changes for 2.1 kernel map.
  25. * Michael Chastain : Change trampoline.S to gnu as.
  26. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  27. * Ingo Molnar : Added APIC timers, based on code
  28. * from Jose Renau
  29. * Ingo Molnar : various cleanups and rewrites
  30. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  31. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  32. * Andi Kleen : Changed for SMP boot into long mode.
  33. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  34. * Andi Kleen : Converted to new state machine.
  35. * Various cleanups.
  36. * Probably mostly hotplug CPU ready now.
  37. * Ashok Raj : CPU hotplug support
  38. */
  39. #include <linux/init.h>
  40. #include <linux/mm.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/bootmem.h>
  44. #include <linux/thread_info.h>
  45. #include <linux/module.h>
  46. #include <linux/delay.h>
  47. #include <linux/mc146818rtc.h>
  48. #include <linux/smp.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/pgalloc.h>
  51. #include <asm/desc.h>
  52. #include <asm/kdebug.h>
  53. #include <asm/tlbflush.h>
  54. #include <asm/proto.h>
  55. #include <asm/nmi.h>
  56. #include <asm/irq.h>
  57. #include <asm/hw_irq.h>
  58. #include <asm/numa.h>
  59. /* Number of siblings per CPU package */
  60. int smp_num_siblings = 1;
  61. EXPORT_SYMBOL(smp_num_siblings);
  62. /* Last level cache ID of each logical CPU */
  63. u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
  64. EXPORT_SYMBOL(cpu_llc_id);
  65. /* Bitmask of currently online CPUs */
  66. cpumask_t cpu_online_map __read_mostly;
  67. EXPORT_SYMBOL(cpu_online_map);
  68. /*
  69. * Private maps to synchronize booting between AP and BP.
  70. * Probably not needed anymore, but it makes for easier debugging. -AK
  71. */
  72. cpumask_t cpu_callin_map;
  73. cpumask_t cpu_callout_map;
  74. EXPORT_SYMBOL(cpu_callout_map);
  75. cpumask_t cpu_possible_map;
  76. EXPORT_SYMBOL(cpu_possible_map);
  77. /* Per CPU bogomips and other parameters */
  78. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  79. EXPORT_SYMBOL(cpu_data);
  80. /* Set when the idlers are all forked */
  81. int smp_threads_ready;
  82. /* representing HT siblings of each logical CPU */
  83. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
  84. EXPORT_SYMBOL(cpu_sibling_map);
  85. /* representing HT and core siblings of each logical CPU */
  86. cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
  87. EXPORT_SYMBOL(cpu_core_map);
  88. /*
  89. * Trampoline 80x86 program as an array.
  90. */
  91. extern unsigned char trampoline_data[];
  92. extern unsigned char trampoline_end[];
  93. /* State of each CPU */
  94. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  95. /*
  96. * Store all idle threads, this can be reused instead of creating
  97. * a new thread. Also avoids complicated thread destroy functionality
  98. * for idle threads.
  99. */
  100. struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  101. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  102. #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
  103. /*
  104. * Currently trivial. Write the real->protected mode
  105. * bootstrap into the page concerned. The caller
  106. * has made sure it's suitably aligned.
  107. */
  108. static unsigned long __cpuinit setup_trampoline(void)
  109. {
  110. void *tramp = __va(SMP_TRAMPOLINE_BASE);
  111. memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
  112. return virt_to_phys(tramp);
  113. }
  114. /*
  115. * The bootstrap kernel entry code has set these up. Save them for
  116. * a given CPU
  117. */
  118. static void __cpuinit smp_store_cpu_info(int id)
  119. {
  120. struct cpuinfo_x86 *c = cpu_data + id;
  121. *c = boot_cpu_data;
  122. identify_cpu(c);
  123. print_cpu_info(c);
  124. }
  125. static atomic_t init_deasserted __cpuinitdata;
  126. /*
  127. * Report back to the Boot Processor.
  128. * Running on AP.
  129. */
  130. void __cpuinit smp_callin(void)
  131. {
  132. int cpuid, phys_id;
  133. unsigned long timeout;
  134. /*
  135. * If waken up by an INIT in an 82489DX configuration
  136. * we may get here before an INIT-deassert IPI reaches
  137. * our local APIC. We have to wait for the IPI or we'll
  138. * lock up on an APIC access.
  139. */
  140. while (!atomic_read(&init_deasserted))
  141. cpu_relax();
  142. /*
  143. * (This works even if the APIC is not enabled.)
  144. */
  145. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  146. cpuid = smp_processor_id();
  147. if (cpu_isset(cpuid, cpu_callin_map)) {
  148. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  149. phys_id, cpuid);
  150. }
  151. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  152. /*
  153. * STARTUP IPIs are fragile beasts as they might sometimes
  154. * trigger some glue motherboard logic. Complete APIC bus
  155. * silence for 1 second, this overestimates the time the
  156. * boot CPU is spending to send the up to 2 STARTUP IPIs
  157. * by a factor of two. This should be enough.
  158. */
  159. /*
  160. * Waiting 2s total for startup (udelay is not yet working)
  161. */
  162. timeout = jiffies + 2*HZ;
  163. while (time_before(jiffies, timeout)) {
  164. /*
  165. * Has the boot CPU finished it's STARTUP sequence?
  166. */
  167. if (cpu_isset(cpuid, cpu_callout_map))
  168. break;
  169. cpu_relax();
  170. }
  171. if (!time_before(jiffies, timeout)) {
  172. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  173. cpuid);
  174. }
  175. /*
  176. * the boot CPU has finished the init stage and is spinning
  177. * on callin_map until we finish. We are free to set up this
  178. * CPU, first the APIC. (this is probably redundant on most
  179. * boards)
  180. */
  181. Dprintk("CALLIN, before setup_local_APIC().\n");
  182. setup_local_APIC();
  183. /*
  184. * Get our bogomips.
  185. *
  186. * Need to enable IRQs because it can take longer and then
  187. * the NMI watchdog might kill us.
  188. */
  189. local_irq_enable();
  190. calibrate_delay();
  191. local_irq_disable();
  192. Dprintk("Stack at about %p\n",&cpuid);
  193. disable_APIC_timer();
  194. /*
  195. * Save our processor parameters
  196. */
  197. smp_store_cpu_info(cpuid);
  198. /*
  199. * Allow the master to continue.
  200. */
  201. cpu_set(cpuid, cpu_callin_map);
  202. }
  203. /* maps the cpu to the sched domain representing multi-core */
  204. cpumask_t cpu_coregroup_map(int cpu)
  205. {
  206. struct cpuinfo_x86 *c = cpu_data + cpu;
  207. /*
  208. * For perf, we return last level cache shared map.
  209. * And for power savings, we return cpu_core_map
  210. */
  211. if (sched_mc_power_savings || sched_smt_power_savings)
  212. return cpu_core_map[cpu];
  213. else
  214. return c->llc_shared_map;
  215. }
  216. /* representing cpus for which sibling maps can be computed */
  217. static cpumask_t cpu_sibling_setup_map;
  218. static inline void set_cpu_sibling_map(int cpu)
  219. {
  220. int i;
  221. struct cpuinfo_x86 *c = cpu_data;
  222. cpu_set(cpu, cpu_sibling_setup_map);
  223. if (smp_num_siblings > 1) {
  224. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  225. if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
  226. c[cpu].cpu_core_id == c[i].cpu_core_id) {
  227. cpu_set(i, cpu_sibling_map[cpu]);
  228. cpu_set(cpu, cpu_sibling_map[i]);
  229. cpu_set(i, cpu_core_map[cpu]);
  230. cpu_set(cpu, cpu_core_map[i]);
  231. cpu_set(i, c[cpu].llc_shared_map);
  232. cpu_set(cpu, c[i].llc_shared_map);
  233. }
  234. }
  235. } else {
  236. cpu_set(cpu, cpu_sibling_map[cpu]);
  237. }
  238. cpu_set(cpu, c[cpu].llc_shared_map);
  239. if (current_cpu_data.x86_max_cores == 1) {
  240. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  241. c[cpu].booted_cores = 1;
  242. return;
  243. }
  244. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  245. if (cpu_llc_id[cpu] != BAD_APICID &&
  246. cpu_llc_id[cpu] == cpu_llc_id[i]) {
  247. cpu_set(i, c[cpu].llc_shared_map);
  248. cpu_set(cpu, c[i].llc_shared_map);
  249. }
  250. if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
  251. cpu_set(i, cpu_core_map[cpu]);
  252. cpu_set(cpu, cpu_core_map[i]);
  253. /*
  254. * Does this new cpu bringup a new core?
  255. */
  256. if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
  257. /*
  258. * for each core in package, increment
  259. * the booted_cores for this new cpu
  260. */
  261. if (first_cpu(cpu_sibling_map[i]) == i)
  262. c[cpu].booted_cores++;
  263. /*
  264. * increment the core count for all
  265. * the other cpus in this package
  266. */
  267. if (i != cpu)
  268. c[i].booted_cores++;
  269. } else if (i != cpu && !c[cpu].booted_cores)
  270. c[cpu].booted_cores = c[i].booted_cores;
  271. }
  272. }
  273. }
  274. /*
  275. * Setup code on secondary processor (after comming out of the trampoline)
  276. */
  277. void __cpuinit start_secondary(void)
  278. {
  279. /*
  280. * Dont put anything before smp_callin(), SMP
  281. * booting is too fragile that we want to limit the
  282. * things done here to the most necessary things.
  283. */
  284. cpu_init();
  285. preempt_disable();
  286. smp_callin();
  287. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  288. barrier();
  289. /*
  290. * Check TSC sync first:
  291. */
  292. check_tsc_sync_target();
  293. Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
  294. setup_secondary_APIC_clock();
  295. Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
  296. if (nmi_watchdog == NMI_IO_APIC) {
  297. disable_8259A_irq(0);
  298. enable_NMI_through_LVT0(NULL);
  299. enable_8259A_irq(0);
  300. }
  301. enable_APIC_timer();
  302. /*
  303. * The sibling maps must be set before turing the online map on for
  304. * this cpu
  305. */
  306. set_cpu_sibling_map(smp_processor_id());
  307. /*
  308. * We need to hold call_lock, so there is no inconsistency
  309. * between the time smp_call_function() determines number of
  310. * IPI receipients, and the time when the determination is made
  311. * for which cpus receive the IPI in genapic_flat.c. Holding this
  312. * lock helps us to not include this cpu in a currently in progress
  313. * smp_call_function().
  314. */
  315. lock_ipi_call_lock();
  316. spin_lock(&vector_lock);
  317. /* Setup the per cpu irq handling data structures */
  318. __setup_vector_irq(smp_processor_id());
  319. /*
  320. * Allow the master to continue.
  321. */
  322. cpu_set(smp_processor_id(), cpu_online_map);
  323. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  324. spin_unlock(&vector_lock);
  325. unlock_ipi_call_lock();
  326. cpu_idle();
  327. }
  328. extern volatile unsigned long init_rsp;
  329. extern void (*initial_code)(void);
  330. #ifdef APIC_DEBUG
  331. static void inquire_remote_apic(int apicid)
  332. {
  333. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  334. char *names[] = { "ID", "VERSION", "SPIV" };
  335. int timeout, status;
  336. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  337. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  338. printk("... APIC #%d %s: ", apicid, names[i]);
  339. /*
  340. * Wait for idle.
  341. */
  342. apic_wait_icr_idle();
  343. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  344. apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
  345. timeout = 0;
  346. do {
  347. udelay(100);
  348. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  349. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  350. switch (status) {
  351. case APIC_ICR_RR_VALID:
  352. status = apic_read(APIC_RRR);
  353. printk("%08x\n", status);
  354. break;
  355. default:
  356. printk("failed\n");
  357. }
  358. }
  359. }
  360. #endif
  361. /*
  362. * Kick the secondary to wake up.
  363. */
  364. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  365. {
  366. unsigned long send_status = 0, accept_status = 0;
  367. int maxlvt, timeout, num_starts, j;
  368. Dprintk("Asserting INIT.\n");
  369. /*
  370. * Turn INIT on target chip
  371. */
  372. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  373. /*
  374. * Send IPI
  375. */
  376. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  377. | APIC_DM_INIT);
  378. Dprintk("Waiting for send to finish...\n");
  379. timeout = 0;
  380. do {
  381. Dprintk("+");
  382. udelay(100);
  383. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  384. } while (send_status && (timeout++ < 1000));
  385. mdelay(10);
  386. Dprintk("Deasserting INIT.\n");
  387. /* Target chip */
  388. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  389. /* Send IPI */
  390. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  391. Dprintk("Waiting for send to finish...\n");
  392. timeout = 0;
  393. do {
  394. Dprintk("+");
  395. udelay(100);
  396. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  397. } while (send_status && (timeout++ < 1000));
  398. mb();
  399. atomic_set(&init_deasserted, 1);
  400. num_starts = 2;
  401. /*
  402. * Run STARTUP IPI loop.
  403. */
  404. Dprintk("#startup loops: %d.\n", num_starts);
  405. maxlvt = get_maxlvt();
  406. for (j = 1; j <= num_starts; j++) {
  407. Dprintk("Sending STARTUP #%d.\n",j);
  408. apic_write(APIC_ESR, 0);
  409. apic_read(APIC_ESR);
  410. Dprintk("After apic_write.\n");
  411. /*
  412. * STARTUP IPI
  413. */
  414. /* Target chip */
  415. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  416. /* Boot on the stack */
  417. /* Kick the second */
  418. apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
  419. /*
  420. * Give the other CPU some time to accept the IPI.
  421. */
  422. udelay(300);
  423. Dprintk("Startup point 1.\n");
  424. Dprintk("Waiting for send to finish...\n");
  425. timeout = 0;
  426. do {
  427. Dprintk("+");
  428. udelay(100);
  429. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  430. } while (send_status && (timeout++ < 1000));
  431. /*
  432. * Give the other CPU some time to accept the IPI.
  433. */
  434. udelay(200);
  435. /*
  436. * Due to the Pentium erratum 3AP.
  437. */
  438. if (maxlvt > 3) {
  439. apic_write(APIC_ESR, 0);
  440. }
  441. accept_status = (apic_read(APIC_ESR) & 0xEF);
  442. if (send_status || accept_status)
  443. break;
  444. }
  445. Dprintk("After Startup.\n");
  446. if (send_status)
  447. printk(KERN_ERR "APIC never delivered???\n");
  448. if (accept_status)
  449. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  450. return (send_status | accept_status);
  451. }
  452. struct create_idle {
  453. struct work_struct work;
  454. struct task_struct *idle;
  455. struct completion done;
  456. int cpu;
  457. };
  458. void do_fork_idle(struct work_struct *work)
  459. {
  460. struct create_idle *c_idle =
  461. container_of(work, struct create_idle, work);
  462. c_idle->idle = fork_idle(c_idle->cpu);
  463. complete(&c_idle->done);
  464. }
  465. /*
  466. * Boot one CPU.
  467. */
  468. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  469. {
  470. unsigned long boot_error;
  471. int timeout;
  472. unsigned long start_rip;
  473. struct create_idle c_idle = {
  474. .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
  475. .cpu = cpu,
  476. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  477. };
  478. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  479. if (!cpu_gdt_descr[cpu].address &&
  480. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  481. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  482. return -1;
  483. }
  484. /* Allocate node local memory for AP pdas */
  485. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  486. struct x8664_pda *newpda, *pda;
  487. int node = cpu_to_node(cpu);
  488. pda = cpu_pda(cpu);
  489. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  490. node);
  491. if (newpda) {
  492. memcpy(newpda, pda, sizeof (struct x8664_pda));
  493. cpu_pda(cpu) = newpda;
  494. } else
  495. printk(KERN_ERR
  496. "Could not allocate node local PDA for CPU %d on node %d\n",
  497. cpu, node);
  498. }
  499. alternatives_smp_switch(1);
  500. c_idle.idle = get_idle_for_cpu(cpu);
  501. if (c_idle.idle) {
  502. c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
  503. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  504. init_idle(c_idle.idle, cpu);
  505. goto do_rest;
  506. }
  507. /*
  508. * During cold boot process, keventd thread is not spun up yet.
  509. * When we do cpu hot-add, we create idle threads on the fly, we should
  510. * not acquire any attributes from the calling context. Hence the clean
  511. * way to create kernel_threads() is to do that from keventd().
  512. * We do the current_is_keventd() due to the fact that ACPI notifier
  513. * was also queuing to keventd() and when the caller is already running
  514. * in context of keventd(), we would end up with locking up the keventd
  515. * thread.
  516. */
  517. if (!keventd_up() || current_is_keventd())
  518. c_idle.work.func(&c_idle.work);
  519. else {
  520. schedule_work(&c_idle.work);
  521. wait_for_completion(&c_idle.done);
  522. }
  523. if (IS_ERR(c_idle.idle)) {
  524. printk("failed fork for CPU %d\n", cpu);
  525. return PTR_ERR(c_idle.idle);
  526. }
  527. set_idle_for_cpu(cpu, c_idle.idle);
  528. do_rest:
  529. cpu_pda(cpu)->pcurrent = c_idle.idle;
  530. start_rip = setup_trampoline();
  531. init_rsp = c_idle.idle->thread.rsp;
  532. per_cpu(init_tss,cpu).rsp0 = init_rsp;
  533. initial_code = start_secondary;
  534. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  535. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  536. cpus_weight(cpu_present_map),
  537. apicid);
  538. /*
  539. * This grunge runs the startup process for
  540. * the targeted processor.
  541. */
  542. atomic_set(&init_deasserted, 0);
  543. Dprintk("Setting warm reset code and vector.\n");
  544. CMOS_WRITE(0xa, 0xf);
  545. local_flush_tlb();
  546. Dprintk("1.\n");
  547. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  548. Dprintk("2.\n");
  549. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  550. Dprintk("3.\n");
  551. /*
  552. * Be paranoid about clearing APIC errors.
  553. */
  554. apic_write(APIC_ESR, 0);
  555. apic_read(APIC_ESR);
  556. /*
  557. * Status is now clean
  558. */
  559. boot_error = 0;
  560. /*
  561. * Starting actual IPI sequence...
  562. */
  563. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  564. if (!boot_error) {
  565. /*
  566. * allow APs to start initializing.
  567. */
  568. Dprintk("Before Callout %d.\n", cpu);
  569. cpu_set(cpu, cpu_callout_map);
  570. Dprintk("After Callout %d.\n", cpu);
  571. /*
  572. * Wait 5s total for a response
  573. */
  574. for (timeout = 0; timeout < 50000; timeout++) {
  575. if (cpu_isset(cpu, cpu_callin_map))
  576. break; /* It has booted */
  577. udelay(100);
  578. }
  579. if (cpu_isset(cpu, cpu_callin_map)) {
  580. /* number CPUs logically, starting from 1 (BSP is 0) */
  581. Dprintk("CPU has booted.\n");
  582. } else {
  583. boot_error = 1;
  584. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  585. == 0xA5)
  586. /* trampoline started but...? */
  587. printk("Stuck ??\n");
  588. else
  589. /* trampoline code not run */
  590. printk("Not responding.\n");
  591. #ifdef APIC_DEBUG
  592. inquire_remote_apic(apicid);
  593. #endif
  594. }
  595. }
  596. if (boot_error) {
  597. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  598. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  599. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  600. cpu_clear(cpu, cpu_present_map);
  601. cpu_clear(cpu, cpu_possible_map);
  602. x86_cpu_to_apicid[cpu] = BAD_APICID;
  603. x86_cpu_to_log_apicid[cpu] = BAD_APICID;
  604. return -EIO;
  605. }
  606. return 0;
  607. }
  608. cycles_t cacheflush_time;
  609. unsigned long cache_decay_ticks;
  610. /*
  611. * Cleanup possible dangling ends...
  612. */
  613. static __cpuinit void smp_cleanup_boot(void)
  614. {
  615. /*
  616. * Paranoid: Set warm reset code and vector here back
  617. * to default values.
  618. */
  619. CMOS_WRITE(0, 0xf);
  620. /*
  621. * Reset trampoline flag
  622. */
  623. *((volatile int *) phys_to_virt(0x467)) = 0;
  624. }
  625. /*
  626. * Fall back to non SMP mode after errors.
  627. *
  628. * RED-PEN audit/test this more. I bet there is more state messed up here.
  629. */
  630. static __init void disable_smp(void)
  631. {
  632. cpu_present_map = cpumask_of_cpu(0);
  633. cpu_possible_map = cpumask_of_cpu(0);
  634. if (smp_found_config)
  635. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  636. else
  637. phys_cpu_present_map = physid_mask_of_physid(0);
  638. cpu_set(0, cpu_sibling_map[0]);
  639. cpu_set(0, cpu_core_map[0]);
  640. }
  641. #ifdef CONFIG_HOTPLUG_CPU
  642. int additional_cpus __initdata = -1;
  643. /*
  644. * cpu_possible_map should be static, it cannot change as cpu's
  645. * are onlined, or offlined. The reason is per-cpu data-structures
  646. * are allocated by some modules at init time, and dont expect to
  647. * do this dynamically on cpu arrival/departure.
  648. * cpu_present_map on the other hand can change dynamically.
  649. * In case when cpu_hotplug is not compiled, then we resort to current
  650. * behaviour, which is cpu_possible == cpu_present.
  651. * - Ashok Raj
  652. *
  653. * Three ways to find out the number of additional hotplug CPUs:
  654. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  655. * - The user can overwrite it with additional_cpus=NUM
  656. * - Otherwise don't reserve additional CPUs.
  657. * We do this because additional CPUs waste a lot of memory.
  658. * -AK
  659. */
  660. __init void prefill_possible_map(void)
  661. {
  662. int i;
  663. int possible;
  664. if (additional_cpus == -1) {
  665. if (disabled_cpus > 0)
  666. additional_cpus = disabled_cpus;
  667. else
  668. additional_cpus = 0;
  669. }
  670. possible = num_processors + additional_cpus;
  671. if (possible > NR_CPUS)
  672. possible = NR_CPUS;
  673. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  674. possible,
  675. max_t(int, possible - num_processors, 0));
  676. for (i = 0; i < possible; i++)
  677. cpu_set(i, cpu_possible_map);
  678. }
  679. #endif
  680. /*
  681. * Various sanity checks.
  682. */
  683. static int __init smp_sanity_check(unsigned max_cpus)
  684. {
  685. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  686. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  687. hard_smp_processor_id());
  688. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  689. }
  690. /*
  691. * If we couldn't find an SMP configuration at boot time,
  692. * get out of here now!
  693. */
  694. if (!smp_found_config) {
  695. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  696. disable_smp();
  697. if (APIC_init_uniprocessor())
  698. printk(KERN_NOTICE "Local APIC not detected."
  699. " Using dummy APIC emulation.\n");
  700. return -1;
  701. }
  702. /*
  703. * Should not be necessary because the MP table should list the boot
  704. * CPU too, but we do it for the sake of robustness anyway.
  705. */
  706. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  707. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  708. boot_cpu_id);
  709. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  710. }
  711. /*
  712. * If we couldn't find a local APIC, then get out of here now!
  713. */
  714. if (!cpu_has_apic) {
  715. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  716. boot_cpu_id);
  717. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  718. nr_ioapics = 0;
  719. return -1;
  720. }
  721. /*
  722. * If SMP should be disabled, then really disable it!
  723. */
  724. if (!max_cpus) {
  725. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  726. nr_ioapics = 0;
  727. return -1;
  728. }
  729. return 0;
  730. }
  731. /*
  732. * Prepare for SMP bootup. The MP table or ACPI has been read
  733. * earlier. Just do some sanity checking here and enable APIC mode.
  734. */
  735. void __init smp_prepare_cpus(unsigned int max_cpus)
  736. {
  737. nmi_watchdog_default();
  738. current_cpu_data = boot_cpu_data;
  739. current_thread_info()->cpu = 0; /* needed? */
  740. set_cpu_sibling_map(0);
  741. if (smp_sanity_check(max_cpus) < 0) {
  742. printk(KERN_INFO "SMP disabled\n");
  743. disable_smp();
  744. return;
  745. }
  746. /*
  747. * Switch from PIC to APIC mode.
  748. */
  749. setup_local_APIC();
  750. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  751. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  752. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  753. /* Or can we switch back to PIC here? */
  754. }
  755. /*
  756. * Now start the IO-APICs
  757. */
  758. if (!skip_ioapic_setup && nr_ioapics)
  759. setup_IO_APIC();
  760. else
  761. nr_ioapics = 0;
  762. /*
  763. * Set up local APIC timer on boot CPU.
  764. */
  765. setup_boot_APIC_clock();
  766. }
  767. /*
  768. * Early setup to make printk work.
  769. */
  770. void __init smp_prepare_boot_cpu(void)
  771. {
  772. int me = smp_processor_id();
  773. cpu_set(me, cpu_online_map);
  774. cpu_set(me, cpu_callout_map);
  775. per_cpu(cpu_state, me) = CPU_ONLINE;
  776. }
  777. /*
  778. * Entry point to boot a CPU.
  779. */
  780. int __cpuinit __cpu_up(unsigned int cpu)
  781. {
  782. int apicid = cpu_present_to_apicid(cpu);
  783. unsigned long flags;
  784. int err;
  785. WARN_ON(irqs_disabled());
  786. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  787. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  788. !physid_isset(apicid, phys_cpu_present_map)) {
  789. printk("__cpu_up: bad cpu %d\n", cpu);
  790. return -EINVAL;
  791. }
  792. /*
  793. * Already booted CPU?
  794. */
  795. if (cpu_isset(cpu, cpu_callin_map)) {
  796. Dprintk("do_boot_cpu %d Already started\n", cpu);
  797. return -ENOSYS;
  798. }
  799. /*
  800. * Save current MTRR state in case it was changed since early boot
  801. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  802. */
  803. mtrr_save_state();
  804. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  805. /* Boot it! */
  806. err = do_boot_cpu(cpu, apicid);
  807. if (err < 0) {
  808. Dprintk("do_boot_cpu failed %d\n", err);
  809. return err;
  810. }
  811. /* Unleash the CPU! */
  812. Dprintk("waiting for cpu %d\n", cpu);
  813. /*
  814. * Make sure and check TSC sync:
  815. */
  816. local_irq_save(flags);
  817. check_tsc_sync_source(cpu);
  818. local_irq_restore(flags);
  819. while (!cpu_isset(cpu, cpu_online_map))
  820. cpu_relax();
  821. err = 0;
  822. return err;
  823. }
  824. /*
  825. * Finish the SMP boot.
  826. */
  827. void __init smp_cpus_done(unsigned int max_cpus)
  828. {
  829. smp_cleanup_boot();
  830. setup_ioapic_dest();
  831. check_nmi_watchdog();
  832. }
  833. #ifdef CONFIG_HOTPLUG_CPU
  834. static void remove_siblinginfo(int cpu)
  835. {
  836. int sibling;
  837. struct cpuinfo_x86 *c = cpu_data;
  838. for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
  839. cpu_clear(cpu, cpu_core_map[sibling]);
  840. /*
  841. * last thread sibling in this cpu core going down
  842. */
  843. if (cpus_weight(cpu_sibling_map[cpu]) == 1)
  844. c[sibling].booted_cores--;
  845. }
  846. for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
  847. cpu_clear(cpu, cpu_sibling_map[sibling]);
  848. cpus_clear(cpu_sibling_map[cpu]);
  849. cpus_clear(cpu_core_map[cpu]);
  850. c[cpu].phys_proc_id = 0;
  851. c[cpu].cpu_core_id = 0;
  852. cpu_clear(cpu, cpu_sibling_setup_map);
  853. }
  854. void remove_cpu_from_maps(void)
  855. {
  856. int cpu = smp_processor_id();
  857. cpu_clear(cpu, cpu_callout_map);
  858. cpu_clear(cpu, cpu_callin_map);
  859. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  860. clear_node_cpumask(cpu);
  861. }
  862. int __cpu_disable(void)
  863. {
  864. int cpu = smp_processor_id();
  865. /*
  866. * Perhaps use cpufreq to drop frequency, but that could go
  867. * into generic code.
  868. *
  869. * We won't take down the boot processor on i386 due to some
  870. * interrupts only being able to be serviced by the BSP.
  871. * Especially so if we're not using an IOAPIC -zwane
  872. */
  873. if (cpu == 0)
  874. return -EBUSY;
  875. if (nmi_watchdog == NMI_LOCAL_APIC)
  876. stop_apic_nmi_watchdog(NULL);
  877. clear_local_APIC();
  878. /*
  879. * HACK:
  880. * Allow any queued timer interrupts to get serviced
  881. * This is only a temporary solution until we cleanup
  882. * fixup_irqs as we do for IA64.
  883. */
  884. local_irq_enable();
  885. mdelay(1);
  886. local_irq_disable();
  887. remove_siblinginfo(cpu);
  888. spin_lock(&vector_lock);
  889. /* It's now safe to remove this processor from the online map */
  890. cpu_clear(cpu, cpu_online_map);
  891. spin_unlock(&vector_lock);
  892. remove_cpu_from_maps();
  893. fixup_irqs(cpu_online_map);
  894. return 0;
  895. }
  896. void __cpu_die(unsigned int cpu)
  897. {
  898. /* We don't do anything here: idle task is faking death itself. */
  899. unsigned int i;
  900. for (i = 0; i < 10; i++) {
  901. /* They ack this in play_dead by setting CPU_DEAD */
  902. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  903. printk ("CPU %d is now offline\n", cpu);
  904. if (1 == num_online_cpus())
  905. alternatives_smp_switch(0);
  906. return;
  907. }
  908. msleep(100);
  909. }
  910. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  911. }
  912. static __init int setup_additional_cpus(char *s)
  913. {
  914. return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
  915. }
  916. early_param("additional_cpus", setup_additional_cpus);
  917. #else /* ... !CONFIG_HOTPLUG_CPU */
  918. int __cpu_disable(void)
  919. {
  920. return -ENOSYS;
  921. }
  922. void __cpu_die(unsigned int cpu)
  923. {
  924. /* We said "no" in __cpu_disable */
  925. BUG();
  926. }
  927. #endif /* CONFIG_HOTPLUG_CPU */