smpboot_64.c 26 KB

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