smpboot.c 26 KB

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