smpboot.c 26 KB

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