smpboot_64.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. DEFINE_PER_CPU(u16, cpu_llc_id) = 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. cpumask_t cpu_possible_map;
  73. EXPORT_SYMBOL(cpu_possible_map);
  74. /* Per CPU bogomips and other parameters */
  75. DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
  76. EXPORT_PER_CPU_SYMBOL(cpu_info);
  77. /* Set when the idlers are all forked */
  78. int smp_threads_ready;
  79. /* representing HT siblings of each logical CPU */
  80. DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
  81. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  82. /* representing HT and core siblings of each logical CPU */
  83. DEFINE_PER_CPU(cpumask_t, cpu_core_map);
  84. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  85. /*
  86. * Trampoline 80x86 program as an array.
  87. */
  88. extern const unsigned char trampoline_data[];
  89. extern const unsigned char trampoline_end[];
  90. /* State of each CPU */
  91. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  92. /*
  93. * Store all idle threads, this can be reused instead of creating
  94. * a new thread. Also avoids complicated thread destroy functionality
  95. * for idle threads.
  96. */
  97. struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  98. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  99. #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
  100. /*
  101. * Currently trivial. Write the real->protected mode
  102. * bootstrap into the page concerned. The caller
  103. * has made sure it's suitably aligned.
  104. */
  105. static unsigned long __cpuinit setup_trampoline(void)
  106. {
  107. void *tramp = __va(SMP_TRAMPOLINE_BASE);
  108. memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
  109. return virt_to_phys(tramp);
  110. }
  111. /*
  112. * The bootstrap kernel entry code has set these up. Save them for
  113. * a given CPU
  114. */
  115. static void __cpuinit smp_store_cpu_info(int id)
  116. {
  117. struct cpuinfo_x86 *c = &cpu_data(id);
  118. *c = boot_cpu_data;
  119. c->cpu_index = id;
  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. end_local_APIC_setup();
  182. /*
  183. * Get our bogomips.
  184. *
  185. * Need to enable IRQs because it can take longer and then
  186. * the NMI watchdog might kill us.
  187. */
  188. local_irq_enable();
  189. calibrate_delay();
  190. local_irq_disable();
  191. Dprintk("Stack at about %p\n",&cpuid);
  192. /*
  193. * Save our processor parameters
  194. */
  195. smp_store_cpu_info(cpuid);
  196. /*
  197. * Allow the master to continue.
  198. */
  199. cpu_set(cpuid, cpu_callin_map);
  200. }
  201. /* maps the cpu to the sched domain representing multi-core */
  202. cpumask_t cpu_coregroup_map(int cpu)
  203. {
  204. struct cpuinfo_x86 *c = &cpu_data(cpu);
  205. /*
  206. * For perf, we return last level cache shared map.
  207. * And for power savings, we return cpu_core_map
  208. */
  209. if (sched_mc_power_savings || sched_smt_power_savings)
  210. return per_cpu(cpu_core_map, cpu);
  211. else
  212. return c->llc_shared_map;
  213. }
  214. /* representing cpus for which sibling maps can be computed */
  215. static cpumask_t cpu_sibling_setup_map;
  216. static inline void set_cpu_sibling_map(int cpu)
  217. {
  218. int i;
  219. struct cpuinfo_x86 *c = &cpu_data(cpu);
  220. cpu_set(cpu, cpu_sibling_setup_map);
  221. if (smp_num_siblings > 1) {
  222. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  223. if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
  224. c->cpu_core_id == cpu_data(i).cpu_core_id) {
  225. cpu_set(i, per_cpu(cpu_sibling_map, cpu));
  226. cpu_set(cpu, per_cpu(cpu_sibling_map, i));
  227. cpu_set(i, per_cpu(cpu_core_map, cpu));
  228. cpu_set(cpu, per_cpu(cpu_core_map, i));
  229. cpu_set(i, c->llc_shared_map);
  230. cpu_set(cpu, cpu_data(i).llc_shared_map);
  231. }
  232. }
  233. } else {
  234. cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
  235. }
  236. cpu_set(cpu, c->llc_shared_map);
  237. if (current_cpu_data.x86_max_cores == 1) {
  238. per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
  239. c->booted_cores = 1;
  240. return;
  241. }
  242. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  243. if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
  244. per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
  245. cpu_set(i, c->llc_shared_map);
  246. cpu_set(cpu, cpu_data(i).llc_shared_map);
  247. }
  248. if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
  249. cpu_set(i, per_cpu(cpu_core_map, cpu));
  250. cpu_set(cpu, per_cpu(cpu_core_map, i));
  251. /*
  252. * Does this new cpu bringup a new core?
  253. */
  254. if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
  255. /*
  256. * for each core in package, increment
  257. * the booted_cores for this new cpu
  258. */
  259. if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
  260. c->booted_cores++;
  261. /*
  262. * increment the core count for all
  263. * the other cpus in this package
  264. */
  265. if (i != cpu)
  266. cpu_data(i).booted_cores++;
  267. } else if (i != cpu && !c->booted_cores)
  268. c->booted_cores = cpu_data(i).booted_cores;
  269. }
  270. }
  271. }
  272. /*
  273. * Setup code on secondary processor (after comming out of the trampoline)
  274. */
  275. void __cpuinit start_secondary(void)
  276. {
  277. /*
  278. * Dont put anything before smp_callin(), SMP
  279. * booting is too fragile that we want to limit the
  280. * things done here to the most necessary things.
  281. */
  282. cpu_init();
  283. preempt_disable();
  284. smp_callin();
  285. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  286. barrier();
  287. /*
  288. * Check TSC sync first:
  289. */
  290. check_tsc_sync_target();
  291. if (nmi_watchdog == NMI_IO_APIC) {
  292. disable_8259A_irq(0);
  293. enable_NMI_through_LVT0();
  294. enable_8259A_irq(0);
  295. }
  296. /*
  297. * The sibling maps must be set before turing the online map on for
  298. * this cpu
  299. */
  300. set_cpu_sibling_map(smp_processor_id());
  301. /*
  302. * We need to hold call_lock, so there is no inconsistency
  303. * between the time smp_call_function() determines number of
  304. * IPI recipients, and the time when the determination is made
  305. * for which cpus receive the IPI in genapic_flat.c. Holding this
  306. * lock helps us to not include this cpu in a currently in progress
  307. * smp_call_function().
  308. */
  309. lock_ipi_call_lock();
  310. spin_lock(&vector_lock);
  311. /* Setup the per cpu irq handling data structures */
  312. __setup_vector_irq(smp_processor_id());
  313. /*
  314. * Allow the master to continue.
  315. */
  316. cpu_set(smp_processor_id(), cpu_online_map);
  317. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  318. spin_unlock(&vector_lock);
  319. unlock_ipi_call_lock();
  320. setup_secondary_clock();
  321. cpu_idle();
  322. }
  323. extern volatile unsigned long init_rsp;
  324. extern void (*initial_code)(void);
  325. #ifdef APIC_DEBUG
  326. static void inquire_remote_apic(int apicid)
  327. {
  328. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  329. char *names[] = { "ID", "VERSION", "SPIV" };
  330. int timeout;
  331. u32 status;
  332. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  333. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  334. printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
  335. /*
  336. * Wait for idle.
  337. */
  338. status = safe_apic_wait_icr_idle();
  339. if (status)
  340. printk(KERN_CONT
  341. "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(KERN_CONT "%08x\n", status);
  353. break;
  354. default:
  355. printk(KERN_CONT "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 = lapic_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. static void __cpuinit 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.sp = (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.sp;
  516. load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
  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, (unsigned long *)&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. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  587. return -EIO;
  588. }
  589. return 0;
  590. }
  591. cycles_t cacheflush_time;
  592. unsigned long cache_decay_ticks;
  593. /*
  594. * Cleanup possible dangling ends...
  595. */
  596. static __cpuinit void smp_cleanup_boot(void)
  597. {
  598. /*
  599. * Paranoid: Set warm reset code and vector here back
  600. * to default values.
  601. */
  602. CMOS_WRITE(0, 0xf);
  603. /*
  604. * Reset trampoline flag
  605. */
  606. *((volatile int *) phys_to_virt(0x467)) = 0;
  607. }
  608. /*
  609. * Fall back to non SMP mode after errors.
  610. *
  611. * RED-PEN audit/test this more. I bet there is more state messed up here.
  612. */
  613. static __init void disable_smp(void)
  614. {
  615. cpu_present_map = cpumask_of_cpu(0);
  616. cpu_possible_map = cpumask_of_cpu(0);
  617. if (smp_found_config)
  618. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  619. else
  620. phys_cpu_present_map = physid_mask_of_physid(0);
  621. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  622. cpu_set(0, per_cpu(cpu_core_map, 0));
  623. }
  624. #ifdef CONFIG_HOTPLUG_CPU
  625. int additional_cpus __initdata = -1;
  626. /*
  627. * cpu_possible_map should be static, it cannot change as cpu's
  628. * are onlined, or offlined. The reason is per-cpu data-structures
  629. * are allocated by some modules at init time, and dont expect to
  630. * do this dynamically on cpu arrival/departure.
  631. * cpu_present_map on the other hand can change dynamically.
  632. * In case when cpu_hotplug is not compiled, then we resort to current
  633. * behaviour, which is cpu_possible == cpu_present.
  634. * - Ashok Raj
  635. *
  636. * Three ways to find out the number of additional hotplug CPUs:
  637. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  638. * - The user can overwrite it with additional_cpus=NUM
  639. * - Otherwise don't reserve additional CPUs.
  640. * We do this because additional CPUs waste a lot of memory.
  641. * -AK
  642. */
  643. __init void prefill_possible_map(void)
  644. {
  645. int i;
  646. int possible;
  647. if (additional_cpus == -1) {
  648. if (disabled_cpus > 0)
  649. additional_cpus = disabled_cpus;
  650. else
  651. additional_cpus = 0;
  652. }
  653. possible = num_processors + additional_cpus;
  654. if (possible > NR_CPUS)
  655. possible = NR_CPUS;
  656. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  657. possible,
  658. max_t(int, possible - num_processors, 0));
  659. for (i = 0; i < possible; i++)
  660. cpu_set(i, cpu_possible_map);
  661. }
  662. #endif
  663. /*
  664. * Various sanity checks.
  665. */
  666. static int __init smp_sanity_check(unsigned max_cpus)
  667. {
  668. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  669. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  670. hard_smp_processor_id());
  671. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  672. }
  673. /*
  674. * If we couldn't find an SMP configuration at boot time,
  675. * get out of here now!
  676. */
  677. if (!smp_found_config) {
  678. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  679. disable_smp();
  680. if (APIC_init_uniprocessor())
  681. printk(KERN_NOTICE "Local APIC not detected."
  682. " Using dummy APIC emulation.\n");
  683. return -1;
  684. }
  685. /*
  686. * Should not be necessary because the MP table should list the boot
  687. * CPU too, but we do it for the sake of robustness anyway.
  688. */
  689. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  690. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  691. boot_cpu_id);
  692. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  693. }
  694. /*
  695. * If we couldn't find a local APIC, then get out of here now!
  696. */
  697. if (!cpu_has_apic) {
  698. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  699. boot_cpu_id);
  700. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  701. nr_ioapics = 0;
  702. return -1;
  703. }
  704. /*
  705. * If SMP should be disabled, then really disable it!
  706. */
  707. if (!max_cpus) {
  708. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  709. nr_ioapics = 0;
  710. return -1;
  711. }
  712. return 0;
  713. }
  714. /*
  715. * Copy apicid's found by MP_processor_info from initial array to the per cpu
  716. * data area. The x86_cpu_to_apicid_init array is then expendable and the
  717. * x86_cpu_to_apicid_ptr is zeroed indicating that the static array is no
  718. * longer available.
  719. */
  720. void __init smp_set_apicids(void)
  721. {
  722. int cpu;
  723. for_each_cpu_mask(cpu, cpu_possible_map) {
  724. if (per_cpu_offset(cpu))
  725. per_cpu(x86_cpu_to_apicid, cpu) =
  726. x86_cpu_to_apicid_init[cpu];
  727. }
  728. /* indicate the static array will be going away soon */
  729. x86_cpu_to_apicid_ptr = NULL;
  730. }
  731. static void __init smp_cpu_index_default(void)
  732. {
  733. int i;
  734. struct cpuinfo_x86 *c;
  735. for_each_cpu_mask(i, cpu_possible_map) {
  736. c = &cpu_data(i);
  737. /* mark all to hotplug */
  738. c->cpu_index = NR_CPUS;
  739. }
  740. }
  741. /*
  742. * Prepare for SMP bootup. The MP table or ACPI has been read
  743. * earlier. Just do some sanity checking here and enable APIC mode.
  744. */
  745. void __init smp_prepare_cpus(unsigned int max_cpus)
  746. {
  747. nmi_watchdog_default();
  748. smp_cpu_index_default();
  749. current_cpu_data = boot_cpu_data;
  750. current_thread_info()->cpu = 0; /* needed? */
  751. smp_set_apicids();
  752. set_cpu_sibling_map(0);
  753. if (smp_sanity_check(max_cpus) < 0) {
  754. printk(KERN_INFO "SMP disabled\n");
  755. disable_smp();
  756. return;
  757. }
  758. /*
  759. * Switch from PIC to APIC mode.
  760. */
  761. setup_local_APIC();
  762. /*
  763. * Enable IO APIC before setting up error vector
  764. */
  765. if (!skip_ioapic_setup && nr_ioapics)
  766. enable_IO_APIC();
  767. end_local_APIC_setup();
  768. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  769. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  770. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  771. /* Or can we switch back to PIC here? */
  772. }
  773. /*
  774. * Now start the IO-APICs
  775. */
  776. if (!skip_ioapic_setup && nr_ioapics)
  777. setup_IO_APIC();
  778. else
  779. nr_ioapics = 0;
  780. /*
  781. * Set up local APIC timer on boot CPU.
  782. */
  783. setup_boot_clock();
  784. }
  785. /*
  786. * Early setup to make printk work.
  787. */
  788. void __init smp_prepare_boot_cpu(void)
  789. {
  790. int me = smp_processor_id();
  791. cpu_set(me, cpu_online_map);
  792. cpu_set(me, cpu_callout_map);
  793. per_cpu(cpu_state, me) = CPU_ONLINE;
  794. }
  795. /*
  796. * Entry point to boot a CPU.
  797. */
  798. int __cpuinit __cpu_up(unsigned int cpu)
  799. {
  800. int apicid = cpu_present_to_apicid(cpu);
  801. unsigned long flags;
  802. int err;
  803. WARN_ON(irqs_disabled());
  804. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  805. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  806. !physid_isset(apicid, phys_cpu_present_map)) {
  807. printk("__cpu_up: bad cpu %d\n", cpu);
  808. return -EINVAL;
  809. }
  810. /*
  811. * Already booted CPU?
  812. */
  813. if (cpu_isset(cpu, cpu_callin_map)) {
  814. Dprintk("do_boot_cpu %d Already started\n", cpu);
  815. return -ENOSYS;
  816. }
  817. /*
  818. * Save current MTRR state in case it was changed since early boot
  819. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  820. */
  821. mtrr_save_state();
  822. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  823. /* Boot it! */
  824. err = do_boot_cpu(cpu, apicid);
  825. if (err < 0) {
  826. Dprintk("do_boot_cpu failed %d\n", err);
  827. return err;
  828. }
  829. /* Unleash the CPU! */
  830. Dprintk("waiting for cpu %d\n", cpu);
  831. /*
  832. * Make sure and check TSC sync:
  833. */
  834. local_irq_save(flags);
  835. check_tsc_sync_source(cpu);
  836. local_irq_restore(flags);
  837. while (!cpu_isset(cpu, cpu_online_map))
  838. cpu_relax();
  839. err = 0;
  840. return err;
  841. }
  842. /*
  843. * Finish the SMP boot.
  844. */
  845. void __init smp_cpus_done(unsigned int max_cpus)
  846. {
  847. smp_cleanup_boot();
  848. setup_ioapic_dest();
  849. check_nmi_watchdog();
  850. }
  851. #ifdef CONFIG_HOTPLUG_CPU
  852. static void remove_siblinginfo(int cpu)
  853. {
  854. int sibling;
  855. struct cpuinfo_x86 *c = &cpu_data(cpu);
  856. for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
  857. cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
  858. /*
  859. * last thread sibling in this cpu core going down
  860. */
  861. if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
  862. cpu_data(sibling).booted_cores--;
  863. }
  864. for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
  865. cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
  866. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  867. cpus_clear(per_cpu(cpu_core_map, cpu));
  868. c->phys_proc_id = 0;
  869. c->cpu_core_id = 0;
  870. cpu_clear(cpu, cpu_sibling_setup_map);
  871. }
  872. void remove_cpu_from_maps(void)
  873. {
  874. int cpu = smp_processor_id();
  875. cpu_clear(cpu, cpu_callout_map);
  876. cpu_clear(cpu, cpu_callin_map);
  877. clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
  878. clear_node_cpumask(cpu);
  879. }
  880. int __cpu_disable(void)
  881. {
  882. int cpu = smp_processor_id();
  883. /*
  884. * Perhaps use cpufreq to drop frequency, but that could go
  885. * into generic code.
  886. *
  887. * We won't take down the boot processor on i386 due to some
  888. * interrupts only being able to be serviced by the BSP.
  889. * Especially so if we're not using an IOAPIC -zwane
  890. */
  891. if (cpu == 0)
  892. return -EBUSY;
  893. if (nmi_watchdog == NMI_LOCAL_APIC)
  894. stop_apic_nmi_watchdog(NULL);
  895. clear_local_APIC();
  896. /*
  897. * HACK:
  898. * Allow any queued timer interrupts to get serviced
  899. * This is only a temporary solution until we cleanup
  900. * fixup_irqs as we do for IA64.
  901. */
  902. local_irq_enable();
  903. mdelay(1);
  904. local_irq_disable();
  905. remove_siblinginfo(cpu);
  906. spin_lock(&vector_lock);
  907. /* It's now safe to remove this processor from the online map */
  908. cpu_clear(cpu, cpu_online_map);
  909. spin_unlock(&vector_lock);
  910. remove_cpu_from_maps();
  911. fixup_irqs(cpu_online_map);
  912. return 0;
  913. }
  914. void __cpu_die(unsigned int cpu)
  915. {
  916. /* We don't do anything here: idle task is faking death itself. */
  917. unsigned int i;
  918. for (i = 0; i < 10; i++) {
  919. /* They ack this in play_dead by setting CPU_DEAD */
  920. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  921. printk ("CPU %d is now offline\n", cpu);
  922. if (1 == num_online_cpus())
  923. alternatives_smp_switch(0);
  924. return;
  925. }
  926. msleep(100);
  927. }
  928. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  929. }
  930. static __init int setup_additional_cpus(char *s)
  931. {
  932. return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
  933. }
  934. early_param("additional_cpus", setup_additional_cpus);
  935. #else /* ... !CONFIG_HOTPLUG_CPU */
  936. int __cpu_disable(void)
  937. {
  938. return -ENOSYS;
  939. }
  940. void __cpu_die(unsigned int cpu)
  941. {
  942. /* We said "no" in __cpu_disable */
  943. BUG();
  944. }
  945. #endif /* CONFIG_HOTPLUG_CPU */