smpboot_64.c 26 KB

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