smpboot.c 26 KB

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