smpboot.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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;
  336. unsigned int status;
  337. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  338. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  339. printk("... APIC #%d %s: ", apicid, names[i]);
  340. /*
  341. * Wait for idle.
  342. */
  343. status = safe_apic_wait_icr_idle();
  344. if (status)
  345. printk("a previous APIC delivery may have failed\n");
  346. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  347. apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
  348. timeout = 0;
  349. do {
  350. udelay(100);
  351. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  352. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  353. switch (status) {
  354. case APIC_ICR_RR_VALID:
  355. status = apic_read(APIC_RRR);
  356. printk("%08x\n", status);
  357. break;
  358. default:
  359. printk("failed\n");
  360. }
  361. }
  362. }
  363. #endif
  364. /*
  365. * Kick the secondary to wake up.
  366. */
  367. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  368. {
  369. unsigned long send_status, accept_status = 0;
  370. int maxlvt, num_starts, j;
  371. Dprintk("Asserting INIT.\n");
  372. /*
  373. * Turn INIT on target chip
  374. */
  375. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  376. /*
  377. * Send IPI
  378. */
  379. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  380. | APIC_DM_INIT);
  381. Dprintk("Waiting for send to finish...\n");
  382. send_status = safe_apic_wait_icr_idle();
  383. mdelay(10);
  384. Dprintk("Deasserting INIT.\n");
  385. /* Target chip */
  386. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  387. /* Send IPI */
  388. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  389. Dprintk("Waiting for send to finish...\n");
  390. send_status = safe_apic_wait_icr_idle();
  391. mb();
  392. atomic_set(&init_deasserted, 1);
  393. num_starts = 2;
  394. /*
  395. * Run STARTUP IPI loop.
  396. */
  397. Dprintk("#startup loops: %d.\n", num_starts);
  398. maxlvt = get_maxlvt();
  399. for (j = 1; j <= num_starts; j++) {
  400. Dprintk("Sending STARTUP #%d.\n",j);
  401. apic_write(APIC_ESR, 0);
  402. apic_read(APIC_ESR);
  403. Dprintk("After apic_write.\n");
  404. /*
  405. * STARTUP IPI
  406. */
  407. /* Target chip */
  408. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  409. /* Boot on the stack */
  410. /* Kick the second */
  411. apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
  412. /*
  413. * Give the other CPU some time to accept the IPI.
  414. */
  415. udelay(300);
  416. Dprintk("Startup point 1.\n");
  417. Dprintk("Waiting for send to finish...\n");
  418. send_status = safe_apic_wait_icr_idle();
  419. /*
  420. * Give the other CPU some time to accept the IPI.
  421. */
  422. udelay(200);
  423. /*
  424. * Due to the Pentium erratum 3AP.
  425. */
  426. if (maxlvt > 3) {
  427. apic_write(APIC_ESR, 0);
  428. }
  429. accept_status = (apic_read(APIC_ESR) & 0xEF);
  430. if (send_status || accept_status)
  431. break;
  432. }
  433. Dprintk("After Startup.\n");
  434. if (send_status)
  435. printk(KERN_ERR "APIC never delivered???\n");
  436. if (accept_status)
  437. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  438. return (send_status | accept_status);
  439. }
  440. struct create_idle {
  441. struct work_struct work;
  442. struct task_struct *idle;
  443. struct completion done;
  444. int cpu;
  445. };
  446. void do_fork_idle(struct work_struct *work)
  447. {
  448. struct create_idle *c_idle =
  449. container_of(work, struct create_idle, work);
  450. c_idle->idle = fork_idle(c_idle->cpu);
  451. complete(&c_idle->done);
  452. }
  453. /*
  454. * Boot one CPU.
  455. */
  456. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  457. {
  458. unsigned long boot_error;
  459. int timeout;
  460. unsigned long start_rip;
  461. struct create_idle c_idle = {
  462. .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
  463. .cpu = cpu,
  464. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  465. };
  466. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  467. if (!cpu_gdt_descr[cpu].address &&
  468. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  469. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  470. return -1;
  471. }
  472. /* Allocate node local memory for AP pdas */
  473. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  474. struct x8664_pda *newpda, *pda;
  475. int node = cpu_to_node(cpu);
  476. pda = cpu_pda(cpu);
  477. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  478. node);
  479. if (newpda) {
  480. memcpy(newpda, pda, sizeof (struct x8664_pda));
  481. cpu_pda(cpu) = newpda;
  482. } else
  483. printk(KERN_ERR
  484. "Could not allocate node local PDA for CPU %d on node %d\n",
  485. cpu, node);
  486. }
  487. alternatives_smp_switch(1);
  488. c_idle.idle = get_idle_for_cpu(cpu);
  489. if (c_idle.idle) {
  490. c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
  491. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  492. init_idle(c_idle.idle, cpu);
  493. goto do_rest;
  494. }
  495. /*
  496. * During cold boot process, keventd thread is not spun up yet.
  497. * When we do cpu hot-add, we create idle threads on the fly, we should
  498. * not acquire any attributes from the calling context. Hence the clean
  499. * way to create kernel_threads() is to do that from keventd().
  500. * We do the current_is_keventd() due to the fact that ACPI notifier
  501. * was also queuing to keventd() and when the caller is already running
  502. * in context of keventd(), we would end up with locking up the keventd
  503. * thread.
  504. */
  505. if (!keventd_up() || current_is_keventd())
  506. c_idle.work.func(&c_idle.work);
  507. else {
  508. schedule_work(&c_idle.work);
  509. wait_for_completion(&c_idle.done);
  510. }
  511. if (IS_ERR(c_idle.idle)) {
  512. printk("failed fork for CPU %d\n", cpu);
  513. return PTR_ERR(c_idle.idle);
  514. }
  515. set_idle_for_cpu(cpu, c_idle.idle);
  516. do_rest:
  517. cpu_pda(cpu)->pcurrent = c_idle.idle;
  518. start_rip = setup_trampoline();
  519. init_rsp = c_idle.idle->thread.rsp;
  520. per_cpu(init_tss,cpu).rsp0 = init_rsp;
  521. initial_code = start_secondary;
  522. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  523. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  524. cpus_weight(cpu_present_map),
  525. apicid);
  526. /*
  527. * This grunge runs the startup process for
  528. * the targeted processor.
  529. */
  530. atomic_set(&init_deasserted, 0);
  531. Dprintk("Setting warm reset code and vector.\n");
  532. CMOS_WRITE(0xa, 0xf);
  533. local_flush_tlb();
  534. Dprintk("1.\n");
  535. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  536. Dprintk("2.\n");
  537. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  538. Dprintk("3.\n");
  539. /*
  540. * Be paranoid about clearing APIC errors.
  541. */
  542. apic_write(APIC_ESR, 0);
  543. apic_read(APIC_ESR);
  544. /*
  545. * Status is now clean
  546. */
  547. boot_error = 0;
  548. /*
  549. * Starting actual IPI sequence...
  550. */
  551. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  552. if (!boot_error) {
  553. /*
  554. * allow APs to start initializing.
  555. */
  556. Dprintk("Before Callout %d.\n", cpu);
  557. cpu_set(cpu, cpu_callout_map);
  558. Dprintk("After Callout %d.\n", cpu);
  559. /*
  560. * Wait 5s total for a response
  561. */
  562. for (timeout = 0; timeout < 50000; timeout++) {
  563. if (cpu_isset(cpu, cpu_callin_map))
  564. break; /* It has booted */
  565. udelay(100);
  566. }
  567. if (cpu_isset(cpu, cpu_callin_map)) {
  568. /* number CPUs logically, starting from 1 (BSP is 0) */
  569. Dprintk("CPU has booted.\n");
  570. } else {
  571. boot_error = 1;
  572. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  573. == 0xA5)
  574. /* trampoline started but...? */
  575. printk("Stuck ??\n");
  576. else
  577. /* trampoline code not run */
  578. printk("Not responding.\n");
  579. #ifdef APIC_DEBUG
  580. inquire_remote_apic(apicid);
  581. #endif
  582. }
  583. }
  584. if (boot_error) {
  585. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  586. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  587. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  588. cpu_clear(cpu, cpu_present_map);
  589. cpu_clear(cpu, cpu_possible_map);
  590. x86_cpu_to_apicid[cpu] = BAD_APICID;
  591. x86_cpu_to_log_apicid[cpu] = BAD_APICID;
  592. return -EIO;
  593. }
  594. return 0;
  595. }
  596. cycles_t cacheflush_time;
  597. unsigned long cache_decay_ticks;
  598. /*
  599. * Cleanup possible dangling ends...
  600. */
  601. static __cpuinit void smp_cleanup_boot(void)
  602. {
  603. /*
  604. * Paranoid: Set warm reset code and vector here back
  605. * to default values.
  606. */
  607. CMOS_WRITE(0, 0xf);
  608. /*
  609. * Reset trampoline flag
  610. */
  611. *((volatile int *) phys_to_virt(0x467)) = 0;
  612. }
  613. /*
  614. * Fall back to non SMP mode after errors.
  615. *
  616. * RED-PEN audit/test this more. I bet there is more state messed up here.
  617. */
  618. static __init void disable_smp(void)
  619. {
  620. cpu_present_map = cpumask_of_cpu(0);
  621. cpu_possible_map = cpumask_of_cpu(0);
  622. if (smp_found_config)
  623. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  624. else
  625. phys_cpu_present_map = physid_mask_of_physid(0);
  626. cpu_set(0, cpu_sibling_map[0]);
  627. cpu_set(0, cpu_core_map[0]);
  628. }
  629. #ifdef CONFIG_HOTPLUG_CPU
  630. int additional_cpus __initdata = -1;
  631. /*
  632. * cpu_possible_map should be static, it cannot change as cpu's
  633. * are onlined, or offlined. The reason is per-cpu data-structures
  634. * are allocated by some modules at init time, and dont expect to
  635. * do this dynamically on cpu arrival/departure.
  636. * cpu_present_map on the other hand can change dynamically.
  637. * In case when cpu_hotplug is not compiled, then we resort to current
  638. * behaviour, which is cpu_possible == cpu_present.
  639. * - Ashok Raj
  640. *
  641. * Three ways to find out the number of additional hotplug CPUs:
  642. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  643. * - The user can overwrite it with additional_cpus=NUM
  644. * - Otherwise don't reserve additional CPUs.
  645. * We do this because additional CPUs waste a lot of memory.
  646. * -AK
  647. */
  648. __init void prefill_possible_map(void)
  649. {
  650. int i;
  651. int possible;
  652. if (additional_cpus == -1) {
  653. if (disabled_cpus > 0)
  654. additional_cpus = disabled_cpus;
  655. else
  656. additional_cpus = 0;
  657. }
  658. possible = num_processors + additional_cpus;
  659. if (possible > NR_CPUS)
  660. possible = NR_CPUS;
  661. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  662. possible,
  663. max_t(int, possible - num_processors, 0));
  664. for (i = 0; i < possible; i++)
  665. cpu_set(i, cpu_possible_map);
  666. }
  667. #endif
  668. /*
  669. * Various sanity checks.
  670. */
  671. static int __init smp_sanity_check(unsigned max_cpus)
  672. {
  673. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  674. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  675. hard_smp_processor_id());
  676. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  677. }
  678. /*
  679. * If we couldn't find an SMP configuration at boot time,
  680. * get out of here now!
  681. */
  682. if (!smp_found_config) {
  683. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  684. disable_smp();
  685. if (APIC_init_uniprocessor())
  686. printk(KERN_NOTICE "Local APIC not detected."
  687. " Using dummy APIC emulation.\n");
  688. return -1;
  689. }
  690. /*
  691. * Should not be necessary because the MP table should list the boot
  692. * CPU too, but we do it for the sake of robustness anyway.
  693. */
  694. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  695. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  696. boot_cpu_id);
  697. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  698. }
  699. /*
  700. * If we couldn't find a local APIC, then get out of here now!
  701. */
  702. if (!cpu_has_apic) {
  703. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  704. boot_cpu_id);
  705. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  706. nr_ioapics = 0;
  707. return -1;
  708. }
  709. /*
  710. * If SMP should be disabled, then really disable it!
  711. */
  712. if (!max_cpus) {
  713. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  714. nr_ioapics = 0;
  715. return -1;
  716. }
  717. return 0;
  718. }
  719. /*
  720. * Prepare for SMP bootup. The MP table or ACPI has been read
  721. * earlier. Just do some sanity checking here and enable APIC mode.
  722. */
  723. void __init smp_prepare_cpus(unsigned int max_cpus)
  724. {
  725. nmi_watchdog_default();
  726. current_cpu_data = boot_cpu_data;
  727. current_thread_info()->cpu = 0; /* needed? */
  728. set_cpu_sibling_map(0);
  729. if (smp_sanity_check(max_cpus) < 0) {
  730. printk(KERN_INFO "SMP disabled\n");
  731. disable_smp();
  732. return;
  733. }
  734. /*
  735. * Switch from PIC to APIC mode.
  736. */
  737. setup_local_APIC();
  738. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  739. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  740. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  741. /* Or can we switch back to PIC here? */
  742. }
  743. /*
  744. * Now start the IO-APICs
  745. */
  746. if (!skip_ioapic_setup && nr_ioapics)
  747. setup_IO_APIC();
  748. else
  749. nr_ioapics = 0;
  750. /*
  751. * Set up local APIC timer on boot CPU.
  752. */
  753. setup_boot_APIC_clock();
  754. }
  755. /*
  756. * Early setup to make printk work.
  757. */
  758. void __init smp_prepare_boot_cpu(void)
  759. {
  760. int me = smp_processor_id();
  761. cpu_set(me, cpu_online_map);
  762. cpu_set(me, cpu_callout_map);
  763. per_cpu(cpu_state, me) = CPU_ONLINE;
  764. }
  765. /*
  766. * Entry point to boot a CPU.
  767. */
  768. int __cpuinit __cpu_up(unsigned int cpu)
  769. {
  770. int apicid = cpu_present_to_apicid(cpu);
  771. unsigned long flags;
  772. int err;
  773. WARN_ON(irqs_disabled());
  774. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  775. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  776. !physid_isset(apicid, phys_cpu_present_map)) {
  777. printk("__cpu_up: bad cpu %d\n", cpu);
  778. return -EINVAL;
  779. }
  780. /*
  781. * Already booted CPU?
  782. */
  783. if (cpu_isset(cpu, cpu_callin_map)) {
  784. Dprintk("do_boot_cpu %d Already started\n", cpu);
  785. return -ENOSYS;
  786. }
  787. /*
  788. * Save current MTRR state in case it was changed since early boot
  789. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  790. */
  791. mtrr_save_state();
  792. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  793. /* Boot it! */
  794. err = do_boot_cpu(cpu, apicid);
  795. if (err < 0) {
  796. Dprintk("do_boot_cpu failed %d\n", err);
  797. return err;
  798. }
  799. /* Unleash the CPU! */
  800. Dprintk("waiting for cpu %d\n", cpu);
  801. /*
  802. * Make sure and check TSC sync:
  803. */
  804. local_irq_save(flags);
  805. check_tsc_sync_source(cpu);
  806. local_irq_restore(flags);
  807. while (!cpu_isset(cpu, cpu_online_map))
  808. cpu_relax();
  809. err = 0;
  810. return err;
  811. }
  812. /*
  813. * Finish the SMP boot.
  814. */
  815. void __init smp_cpus_done(unsigned int max_cpus)
  816. {
  817. smp_cleanup_boot();
  818. setup_ioapic_dest();
  819. check_nmi_watchdog();
  820. }
  821. #ifdef CONFIG_HOTPLUG_CPU
  822. static void remove_siblinginfo(int cpu)
  823. {
  824. int sibling;
  825. struct cpuinfo_x86 *c = cpu_data;
  826. for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
  827. cpu_clear(cpu, cpu_core_map[sibling]);
  828. /*
  829. * last thread sibling in this cpu core going down
  830. */
  831. if (cpus_weight(cpu_sibling_map[cpu]) == 1)
  832. c[sibling].booted_cores--;
  833. }
  834. for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
  835. cpu_clear(cpu, cpu_sibling_map[sibling]);
  836. cpus_clear(cpu_sibling_map[cpu]);
  837. cpus_clear(cpu_core_map[cpu]);
  838. c[cpu].phys_proc_id = 0;
  839. c[cpu].cpu_core_id = 0;
  840. cpu_clear(cpu, cpu_sibling_setup_map);
  841. }
  842. void remove_cpu_from_maps(void)
  843. {
  844. int cpu = smp_processor_id();
  845. cpu_clear(cpu, cpu_callout_map);
  846. cpu_clear(cpu, cpu_callin_map);
  847. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  848. clear_node_cpumask(cpu);
  849. }
  850. int __cpu_disable(void)
  851. {
  852. int cpu = smp_processor_id();
  853. /*
  854. * Perhaps use cpufreq to drop frequency, but that could go
  855. * into generic code.
  856. *
  857. * We won't take down the boot processor on i386 due to some
  858. * interrupts only being able to be serviced by the BSP.
  859. * Especially so if we're not using an IOAPIC -zwane
  860. */
  861. if (cpu == 0)
  862. return -EBUSY;
  863. if (nmi_watchdog == NMI_LOCAL_APIC)
  864. stop_apic_nmi_watchdog(NULL);
  865. clear_local_APIC();
  866. /*
  867. * HACK:
  868. * Allow any queued timer interrupts to get serviced
  869. * This is only a temporary solution until we cleanup
  870. * fixup_irqs as we do for IA64.
  871. */
  872. local_irq_enable();
  873. mdelay(1);
  874. local_irq_disable();
  875. remove_siblinginfo(cpu);
  876. spin_lock(&vector_lock);
  877. /* It's now safe to remove this processor from the online map */
  878. cpu_clear(cpu, cpu_online_map);
  879. spin_unlock(&vector_lock);
  880. remove_cpu_from_maps();
  881. fixup_irqs(cpu_online_map);
  882. return 0;
  883. }
  884. void __cpu_die(unsigned int cpu)
  885. {
  886. /* We don't do anything here: idle task is faking death itself. */
  887. unsigned int i;
  888. for (i = 0; i < 10; i++) {
  889. /* They ack this in play_dead by setting CPU_DEAD */
  890. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  891. printk ("CPU %d is now offline\n", cpu);
  892. if (1 == num_online_cpus())
  893. alternatives_smp_switch(0);
  894. return;
  895. }
  896. msleep(100);
  897. }
  898. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  899. }
  900. static __init int setup_additional_cpus(char *s)
  901. {
  902. return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
  903. }
  904. early_param("additional_cpus", setup_additional_cpus);
  905. #else /* ... !CONFIG_HOTPLUG_CPU */
  906. int __cpu_disable(void)
  907. {
  908. return -ENOSYS;
  909. }
  910. void __cpu_die(unsigned int cpu)
  911. {
  912. /* We said "no" in __cpu_disable */
  913. BUG();
  914. }
  915. #endif /* CONFIG_HOTPLUG_CPU */