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