smpboot_32.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. *
  7. * Much of the core SMP work is based on previous work by Thomas Radke, to
  8. * whom a great many thanks are extended.
  9. *
  10. * Thanks to Intel for making available several different Pentium,
  11. * Pentium Pro and Pentium-II/Xeon MP machines.
  12. * Original development of Linux SMP code supported by Caldera.
  13. *
  14. * This code is released under the GNU General Public License version 2 or
  15. * later.
  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 BogoMIPS 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. * Martin J. Bligh : Added support for multi-quad systems
  33. * Dave Jones : Report invalid combinations of Athlon CPUs.
  34. * Rusty Russell : Hacked into shape for new "hotplug" boot process. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/kernel.h>
  38. #include <linux/mm.h>
  39. #include <linux/sched.h>
  40. #include <linux/kernel_stat.h>
  41. #include <linux/bootmem.h>
  42. #include <linux/notifier.h>
  43. #include <linux/cpu.h>
  44. #include <linux/percpu.h>
  45. #include <linux/nmi.h>
  46. #include <linux/delay.h>
  47. #include <linux/mc146818rtc.h>
  48. #include <asm/tlbflush.h>
  49. #include <asm/desc.h>
  50. #include <asm/arch_hooks.h>
  51. #include <asm/nmi.h>
  52. #include <mach_apic.h>
  53. #include <mach_wakecpu.h>
  54. #include <smpboot_hooks.h>
  55. #include <asm/vmi.h>
  56. #include <asm/mtrr.h>
  57. /* Set if we find a B stepping CPU */
  58. static int __cpuinitdata smp_b_stepping;
  59. static cpumask_t smp_commenced_mask;
  60. /* which logical CPU number maps to which CPU (physical APIC ID) */
  61. u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
  62. { [0 ... NR_CPUS-1] = BAD_APICID };
  63. void *x86_cpu_to_apicid_early_ptr;
  64. DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
  65. EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  66. u8 apicid_2_node[MAX_APICID];
  67. /*
  68. * Trampoline 80x86 program as an array.
  69. */
  70. extern const unsigned char trampoline_data [];
  71. extern const unsigned char trampoline_end [];
  72. static unsigned char *trampoline_base;
  73. static void map_cpu_to_logical_apicid(void);
  74. /* State of each CPU. */
  75. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  76. /*
  77. * Currently trivial. Write the real->protected mode
  78. * bootstrap into the page concerned. The caller
  79. * has made sure it's suitably aligned.
  80. */
  81. static unsigned long __cpuinit setup_trampoline(void)
  82. {
  83. memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
  84. return virt_to_phys(trampoline_base);
  85. }
  86. /*
  87. * We are called very early to get the low memory for the
  88. * SMP bootup trampoline page.
  89. */
  90. void __init smp_alloc_memory(void)
  91. {
  92. trampoline_base = alloc_bootmem_low_pages(PAGE_SIZE);
  93. /*
  94. * Has to be in very low memory so we can execute
  95. * real-mode AP code.
  96. */
  97. if (__pa(trampoline_base) >= 0x9F000)
  98. BUG();
  99. }
  100. /*
  101. * The bootstrap kernel entry code has set these up. Save them for
  102. * a given CPU
  103. */
  104. void __cpuinit smp_store_cpu_info(int id)
  105. {
  106. struct cpuinfo_x86 *c = &cpu_data(id);
  107. *c = boot_cpu_data;
  108. c->cpu_index = id;
  109. if (id!=0)
  110. identify_secondary_cpu(c);
  111. /*
  112. * Mask B, Pentium, but not Pentium MMX
  113. */
  114. if (c->x86_vendor == X86_VENDOR_INTEL &&
  115. c->x86 == 5 &&
  116. c->x86_mask >= 1 && c->x86_mask <= 4 &&
  117. c->x86_model <= 3)
  118. /*
  119. * Remember we have B step Pentia with bugs
  120. */
  121. smp_b_stepping = 1;
  122. /*
  123. * Certain Athlons might work (for various values of 'work') in SMP
  124. * but they are not certified as MP capable.
  125. */
  126. if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
  127. if (num_possible_cpus() == 1)
  128. goto valid_k7;
  129. /* Athlon 660/661 is valid. */
  130. if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
  131. goto valid_k7;
  132. /* Duron 670 is valid */
  133. if ((c->x86_model==7) && (c->x86_mask==0))
  134. goto valid_k7;
  135. /*
  136. * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
  137. * It's worth noting that the A5 stepping (662) of some Athlon XP's
  138. * have the MP bit set.
  139. * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
  140. */
  141. if (((c->x86_model==6) && (c->x86_mask>=2)) ||
  142. ((c->x86_model==7) && (c->x86_mask>=1)) ||
  143. (c->x86_model> 7))
  144. if (cpu_has_mp)
  145. goto valid_k7;
  146. /* If we get here, it's not a certified SMP capable AMD system. */
  147. add_taint(TAINT_UNSAFE_SMP);
  148. }
  149. valid_k7:
  150. ;
  151. }
  152. static atomic_t init_deasserted;
  153. static void __cpuinit smp_callin(void)
  154. {
  155. int cpuid, phys_id;
  156. unsigned long timeout;
  157. /*
  158. * If waken up by an INIT in an 82489DX configuration
  159. * we may get here before an INIT-deassert IPI reaches
  160. * our local APIC. We have to wait for the IPI or we'll
  161. * lock up on an APIC access.
  162. */
  163. wait_for_init_deassert(&init_deasserted);
  164. /*
  165. * (This works even if the APIC is not enabled.)
  166. */
  167. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  168. cpuid = smp_processor_id();
  169. if (cpu_isset(cpuid, cpu_callin_map)) {
  170. printk("huh, phys CPU#%d, CPU#%d already present??\n",
  171. phys_id, cpuid);
  172. BUG();
  173. }
  174. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  175. /*
  176. * STARTUP IPIs are fragile beasts as they might sometimes
  177. * trigger some glue motherboard logic. Complete APIC bus
  178. * silence for 1 second, this overestimates the time the
  179. * boot CPU is spending to send the up to 2 STARTUP IPIs
  180. * by a factor of two. This should be enough.
  181. */
  182. /*
  183. * Waiting 2s total for startup (udelay is not yet working)
  184. */
  185. timeout = jiffies + 2*HZ;
  186. while (time_before(jiffies, timeout)) {
  187. /*
  188. * Has the boot CPU finished it's STARTUP sequence?
  189. */
  190. if (cpu_isset(cpuid, cpu_callout_map))
  191. break;
  192. rep_nop();
  193. }
  194. if (!time_before(jiffies, timeout)) {
  195. printk("BUG: CPU%d started up but did not get a callout!\n",
  196. cpuid);
  197. BUG();
  198. }
  199. /*
  200. * the boot CPU has finished the init stage and is spinning
  201. * on callin_map until we finish. We are free to set up this
  202. * CPU, first the APIC. (this is probably redundant on most
  203. * boards)
  204. */
  205. Dprintk("CALLIN, before setup_local_APIC().\n");
  206. smp_callin_clear_local_apic();
  207. setup_local_APIC();
  208. map_cpu_to_logical_apicid();
  209. /*
  210. * Get our bogomips.
  211. */
  212. calibrate_delay();
  213. Dprintk("Stack at about %p\n",&cpuid);
  214. /*
  215. * Save our processor parameters
  216. */
  217. smp_store_cpu_info(cpuid);
  218. /*
  219. * Allow the master to continue.
  220. */
  221. cpu_set(cpuid, cpu_callin_map);
  222. }
  223. static int cpucount;
  224. /*
  225. * Activate a secondary processor.
  226. */
  227. static void __cpuinit start_secondary(void *unused)
  228. {
  229. /*
  230. * Don't put *anything* before cpu_init(), SMP booting is too
  231. * fragile that we want to limit the things done here to the
  232. * most necessary things.
  233. */
  234. #ifdef CONFIG_VMI
  235. vmi_bringup();
  236. #endif
  237. cpu_init();
  238. preempt_disable();
  239. smp_callin();
  240. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  241. rep_nop();
  242. /*
  243. * Check TSC synchronization with the BP:
  244. */
  245. check_tsc_sync_target();
  246. setup_secondary_clock();
  247. if (nmi_watchdog == NMI_IO_APIC) {
  248. disable_8259A_irq(0);
  249. enable_NMI_through_LVT0();
  250. enable_8259A_irq(0);
  251. }
  252. /*
  253. * low-memory mappings have been cleared, flush them from
  254. * the local TLBs too.
  255. */
  256. local_flush_tlb();
  257. /* This must be done before setting cpu_online_map */
  258. set_cpu_sibling_map(raw_smp_processor_id());
  259. wmb();
  260. /*
  261. * We need to hold call_lock, so there is no inconsistency
  262. * between the time smp_call_function() determines number of
  263. * IPI recipients, and the time when the determination is made
  264. * for which cpus receive the IPI. Holding this
  265. * lock helps us to not include this cpu in a currently in progress
  266. * smp_call_function().
  267. */
  268. lock_ipi_call_lock();
  269. cpu_set(smp_processor_id(), cpu_online_map);
  270. unlock_ipi_call_lock();
  271. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  272. /* We can take interrupts now: we're officially "up". */
  273. local_irq_enable();
  274. wmb();
  275. cpu_idle();
  276. }
  277. /*
  278. * Everything has been set up for the secondary
  279. * CPUs - they just need to reload everything
  280. * from the task structure
  281. * This function must not return.
  282. */
  283. void __devinit initialize_secondary(void)
  284. {
  285. /*
  286. * We don't actually need to load the full TSS,
  287. * basically just the stack pointer and the ip.
  288. */
  289. asm volatile(
  290. "movl %0,%%esp\n\t"
  291. "jmp *%1"
  292. :
  293. :"m" (current->thread.sp),"m" (current->thread.ip));
  294. }
  295. /* Static state in head.S used to set up a CPU */
  296. extern struct {
  297. void * sp;
  298. unsigned short ss;
  299. } stack_start;
  300. #ifdef CONFIG_NUMA
  301. /* which logical CPUs are on which nodes */
  302. cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
  303. { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
  304. EXPORT_SYMBOL(node_to_cpumask_map);
  305. /* which node each logical CPU is on */
  306. int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  307. EXPORT_SYMBOL(cpu_to_node_map);
  308. /* set up a mapping between cpu and node. */
  309. static inline void map_cpu_to_node(int cpu, int node)
  310. {
  311. printk("Mapping cpu %d to node %d\n", cpu, node);
  312. cpu_set(cpu, node_to_cpumask_map[node]);
  313. cpu_to_node_map[cpu] = node;
  314. }
  315. /* undo a mapping between cpu and node. */
  316. static inline void unmap_cpu_to_node(int cpu)
  317. {
  318. int node;
  319. printk("Unmapping cpu %d from all nodes\n", cpu);
  320. for (node = 0; node < MAX_NUMNODES; node ++)
  321. cpu_clear(cpu, node_to_cpumask_map[node]);
  322. cpu_to_node_map[cpu] = 0;
  323. }
  324. #else /* !CONFIG_NUMA */
  325. #define map_cpu_to_node(cpu, node) ({})
  326. #define unmap_cpu_to_node(cpu) ({})
  327. #endif /* CONFIG_NUMA */
  328. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
  329. static void map_cpu_to_logical_apicid(void)
  330. {
  331. int cpu = smp_processor_id();
  332. int apicid = logical_smp_processor_id();
  333. int node = apicid_to_node(apicid);
  334. if (!node_online(node))
  335. node = first_online_node;
  336. cpu_2_logical_apicid[cpu] = apicid;
  337. map_cpu_to_node(cpu, node);
  338. }
  339. static void unmap_cpu_to_logical_apicid(int cpu)
  340. {
  341. cpu_2_logical_apicid[cpu] = BAD_APICID;
  342. unmap_cpu_to_node(cpu);
  343. }
  344. static inline void __inquire_remote_apic(int apicid)
  345. {
  346. int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  347. char *names[] = { "ID", "VERSION", "SPIV" };
  348. int timeout;
  349. unsigned long status;
  350. printk("Inquiring remote APIC #%d...\n", apicid);
  351. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  352. printk("... APIC #%d %s: ", apicid, names[i]);
  353. /*
  354. * Wait for idle.
  355. */
  356. status = safe_apic_wait_icr_idle();
  357. if (status)
  358. printk("a previous APIC delivery may have failed\n");
  359. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  360. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  361. timeout = 0;
  362. do {
  363. udelay(100);
  364. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  365. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  366. switch (status) {
  367. case APIC_ICR_RR_VALID:
  368. status = apic_read(APIC_RRR);
  369. printk("%lx\n", status);
  370. break;
  371. default:
  372. printk("failed\n");
  373. }
  374. }
  375. }
  376. #ifdef WAKE_SECONDARY_VIA_NMI
  377. /*
  378. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  379. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  380. * won't ... remember to clear down the APIC, etc later.
  381. */
  382. static int __devinit
  383. wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
  384. {
  385. unsigned long send_status, accept_status = 0;
  386. int maxlvt;
  387. /* Target chip */
  388. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
  389. /* Boot on the stack */
  390. /* Kick the second */
  391. apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
  392. Dprintk("Waiting for send to finish...\n");
  393. send_status = safe_apic_wait_icr_idle();
  394. /*
  395. * Give the other CPU some time to accept the IPI.
  396. */
  397. udelay(200);
  398. /*
  399. * Due to the Pentium erratum 3AP.
  400. */
  401. maxlvt = lapic_get_maxlvt();
  402. if (maxlvt > 3) {
  403. apic_read_around(APIC_SPIV);
  404. apic_write(APIC_ESR, 0);
  405. }
  406. accept_status = (apic_read(APIC_ESR) & 0xEF);
  407. Dprintk("NMI sent.\n");
  408. if (send_status)
  409. printk("APIC never delivered???\n");
  410. if (accept_status)
  411. printk("APIC delivery error (%lx).\n", accept_status);
  412. return (send_status | accept_status);
  413. }
  414. #endif /* WAKE_SECONDARY_VIA_NMI */
  415. #ifdef WAKE_SECONDARY_VIA_INIT
  416. static int __devinit
  417. wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
  418. {
  419. unsigned long send_status, accept_status = 0;
  420. int maxlvt, num_starts, j;
  421. /*
  422. * Be paranoid about clearing APIC errors.
  423. */
  424. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  425. apic_read_around(APIC_SPIV);
  426. apic_write(APIC_ESR, 0);
  427. apic_read(APIC_ESR);
  428. }
  429. Dprintk("Asserting INIT.\n");
  430. /*
  431. * Turn INIT on target chip
  432. */
  433. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  434. /*
  435. * Send IPI
  436. */
  437. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  438. | APIC_DM_INIT);
  439. Dprintk("Waiting for send to finish...\n");
  440. send_status = safe_apic_wait_icr_idle();
  441. mdelay(10);
  442. Dprintk("Deasserting INIT.\n");
  443. /* Target chip */
  444. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  445. /* Send IPI */
  446. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  447. Dprintk("Waiting for send to finish...\n");
  448. send_status = safe_apic_wait_icr_idle();
  449. atomic_set(&init_deasserted, 1);
  450. /*
  451. * Should we send STARTUP IPIs ?
  452. *
  453. * Determine this based on the APIC version.
  454. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  455. */
  456. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  457. num_starts = 2;
  458. else
  459. num_starts = 0;
  460. /*
  461. * Paravirt / VMI wants a startup IPI hook here to set up the
  462. * target processor state.
  463. */
  464. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  465. (unsigned long) stack_start.sp);
  466. /*
  467. * Run STARTUP IPI loop.
  468. */
  469. Dprintk("#startup loops: %d.\n", num_starts);
  470. maxlvt = lapic_get_maxlvt();
  471. for (j = 1; j <= num_starts; j++) {
  472. Dprintk("Sending STARTUP #%d.\n",j);
  473. apic_read_around(APIC_SPIV);
  474. apic_write(APIC_ESR, 0);
  475. apic_read(APIC_ESR);
  476. Dprintk("After apic_write.\n");
  477. /*
  478. * STARTUP IPI
  479. */
  480. /* Target chip */
  481. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  482. /* Boot on the stack */
  483. /* Kick the second */
  484. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  485. | (start_eip >> 12));
  486. /*
  487. * Give the other CPU some time to accept the IPI.
  488. */
  489. udelay(300);
  490. Dprintk("Startup point 1.\n");
  491. Dprintk("Waiting for send to finish...\n");
  492. send_status = safe_apic_wait_icr_idle();
  493. /*
  494. * Give the other CPU some time to accept the IPI.
  495. */
  496. udelay(200);
  497. /*
  498. * Due to the Pentium erratum 3AP.
  499. */
  500. if (maxlvt > 3) {
  501. apic_read_around(APIC_SPIV);
  502. apic_write(APIC_ESR, 0);
  503. }
  504. accept_status = (apic_read(APIC_ESR) & 0xEF);
  505. if (send_status || accept_status)
  506. break;
  507. }
  508. Dprintk("After Startup.\n");
  509. if (send_status)
  510. printk("APIC never delivered???\n");
  511. if (accept_status)
  512. printk("APIC delivery error (%lx).\n", accept_status);
  513. return (send_status | accept_status);
  514. }
  515. #endif /* WAKE_SECONDARY_VIA_INIT */
  516. extern cpumask_t cpu_initialized;
  517. static inline int alloc_cpu_id(void)
  518. {
  519. cpumask_t tmp_map;
  520. int cpu;
  521. cpus_complement(tmp_map, cpu_present_map);
  522. cpu = first_cpu(tmp_map);
  523. if (cpu >= NR_CPUS)
  524. return -ENODEV;
  525. return cpu;
  526. }
  527. #ifdef CONFIG_HOTPLUG_CPU
  528. static struct task_struct * __cpuinitdata cpu_idle_tasks[NR_CPUS];
  529. static inline struct task_struct * __cpuinit alloc_idle_task(int cpu)
  530. {
  531. struct task_struct *idle;
  532. if ((idle = cpu_idle_tasks[cpu]) != NULL) {
  533. /* initialize thread_struct. we really want to avoid destroy
  534. * idle tread
  535. */
  536. idle->thread.sp = (unsigned long)task_pt_regs(idle);
  537. init_idle(idle, cpu);
  538. return idle;
  539. }
  540. idle = fork_idle(cpu);
  541. if (!IS_ERR(idle))
  542. cpu_idle_tasks[cpu] = idle;
  543. return idle;
  544. }
  545. #else
  546. #define alloc_idle_task(cpu) fork_idle(cpu)
  547. #endif
  548. static int __cpuinit do_boot_cpu(int apicid, int cpu)
  549. /*
  550. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  551. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  552. * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
  553. */
  554. {
  555. struct task_struct *idle;
  556. unsigned long boot_error;
  557. int timeout;
  558. unsigned long start_eip;
  559. unsigned short nmi_high = 0, nmi_low = 0;
  560. /*
  561. * Save current MTRR state in case it was changed since early boot
  562. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  563. */
  564. mtrr_save_state();
  565. /*
  566. * We can't use kernel_thread since we must avoid to
  567. * reschedule the child.
  568. */
  569. idle = alloc_idle_task(cpu);
  570. if (IS_ERR(idle))
  571. panic("failed fork for CPU %d", cpu);
  572. init_gdt(cpu);
  573. per_cpu(current_task, cpu) = idle;
  574. early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
  575. idle->thread.ip = (unsigned long) start_secondary;
  576. /* start_eip had better be page-aligned! */
  577. start_eip = setup_trampoline();
  578. ++cpucount;
  579. alternatives_smp_switch(1);
  580. /* So we see what's up */
  581. printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
  582. /* Stack for startup_32 can be just as for start_secondary onwards */
  583. stack_start.sp = (void *) idle->thread.sp;
  584. irq_ctx_init(cpu);
  585. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  586. /*
  587. * This grunge runs the startup process for
  588. * the targeted processor.
  589. */
  590. atomic_set(&init_deasserted, 0);
  591. Dprintk("Setting warm reset code and vector.\n");
  592. store_NMI_vector(&nmi_high, &nmi_low);
  593. smpboot_setup_warm_reset_vector(start_eip);
  594. /*
  595. * Starting actual IPI sequence...
  596. */
  597. boot_error = wakeup_secondary_cpu(apicid, start_eip);
  598. if (!boot_error) {
  599. /*
  600. * allow APs to start initializing.
  601. */
  602. Dprintk("Before Callout %d.\n", cpu);
  603. cpu_set(cpu, cpu_callout_map);
  604. Dprintk("After Callout %d.\n", cpu);
  605. /*
  606. * Wait 5s total for a response
  607. */
  608. for (timeout = 0; timeout < 50000; timeout++) {
  609. if (cpu_isset(cpu, cpu_callin_map))
  610. break; /* It has booted */
  611. udelay(100);
  612. }
  613. if (cpu_isset(cpu, cpu_callin_map)) {
  614. /* number CPUs logically, starting from 1 (BSP is 0) */
  615. Dprintk("OK.\n");
  616. printk("CPU%d: ", cpu);
  617. print_cpu_info(&cpu_data(cpu));
  618. Dprintk("CPU has booted.\n");
  619. } else {
  620. boot_error= 1;
  621. if (*((volatile unsigned char *)trampoline_base)
  622. == 0xA5)
  623. /* trampoline started but...? */
  624. printk("Stuck ??\n");
  625. else
  626. /* trampoline code not run */
  627. printk("Not responding.\n");
  628. inquire_remote_apic(apicid);
  629. }
  630. }
  631. if (boot_error) {
  632. /* Try to put things back the way they were before ... */
  633. unmap_cpu_to_logical_apicid(cpu);
  634. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  635. cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
  636. cpucount--;
  637. } else {
  638. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  639. cpu_set(cpu, cpu_present_map);
  640. }
  641. /* mark "stuck" area as not stuck */
  642. *((volatile unsigned long *)trampoline_base) = 0;
  643. return boot_error;
  644. }
  645. #ifdef CONFIG_HOTPLUG_CPU
  646. void cpu_exit_clear(void)
  647. {
  648. int cpu = raw_smp_processor_id();
  649. idle_task_exit();
  650. cpucount --;
  651. cpu_uninit();
  652. irq_ctx_exit(cpu);
  653. cpu_clear(cpu, cpu_callout_map);
  654. cpu_clear(cpu, cpu_callin_map);
  655. cpu_clear(cpu, smp_commenced_mask);
  656. unmap_cpu_to_logical_apicid(cpu);
  657. }
  658. struct warm_boot_cpu_info {
  659. struct completion *complete;
  660. struct work_struct task;
  661. int apicid;
  662. int cpu;
  663. };
  664. static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
  665. {
  666. struct warm_boot_cpu_info *info =
  667. container_of(work, struct warm_boot_cpu_info, task);
  668. do_boot_cpu(info->apicid, info->cpu);
  669. complete(info->complete);
  670. }
  671. static int __cpuinit __smp_prepare_cpu(int cpu)
  672. {
  673. DECLARE_COMPLETION_ONSTACK(done);
  674. struct warm_boot_cpu_info info;
  675. int apicid, ret;
  676. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  677. if (apicid == BAD_APICID) {
  678. ret = -ENODEV;
  679. goto exit;
  680. }
  681. info.complete = &done;
  682. info.apicid = apicid;
  683. info.cpu = cpu;
  684. INIT_WORK(&info.task, do_warm_boot_cpu);
  685. /* init low mem mapping */
  686. clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
  687. min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
  688. flush_tlb_all();
  689. schedule_work(&info.task);
  690. wait_for_completion(&done);
  691. zap_low_mappings();
  692. ret = 0;
  693. exit:
  694. return ret;
  695. }
  696. #endif
  697. /*
  698. * Cycle through the processors sending APIC IPIs to boot each.
  699. */
  700. static int boot_cpu_logical_apicid;
  701. /* Where the IO area was mapped on multiquad, always 0 otherwise */
  702. void *xquad_portio;
  703. #ifdef CONFIG_X86_NUMAQ
  704. EXPORT_SYMBOL(xquad_portio);
  705. #endif
  706. static void __init smp_boot_cpus(unsigned int max_cpus)
  707. {
  708. int apicid, cpu, bit, kicked;
  709. unsigned long bogosum = 0;
  710. /*
  711. * Setup boot CPU information
  712. */
  713. smp_store_cpu_info(0); /* Final full version of the data */
  714. printk("CPU%d: ", 0);
  715. print_cpu_info(&cpu_data(0));
  716. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  717. boot_cpu_logical_apicid = logical_smp_processor_id();
  718. per_cpu(x86_cpu_to_apicid, 0) = boot_cpu_physical_apicid;
  719. current_thread_info()->cpu = 0;
  720. set_cpu_sibling_map(0);
  721. /*
  722. * If we couldn't find an SMP configuration at boot time,
  723. * get out of here now!
  724. */
  725. if (!smp_found_config && !acpi_lapic) {
  726. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  727. smpboot_clear_io_apic_irqs();
  728. phys_cpu_present_map = physid_mask_of_physid(0);
  729. if (APIC_init_uniprocessor())
  730. printk(KERN_NOTICE "Local APIC not detected."
  731. " Using dummy APIC emulation.\n");
  732. map_cpu_to_logical_apicid();
  733. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  734. cpu_set(0, per_cpu(cpu_core_map, 0));
  735. return;
  736. }
  737. /*
  738. * Should not be necessary because the MP table should list the boot
  739. * CPU too, but we do it for the sake of robustness anyway.
  740. * Makes no sense to do this check in clustered apic mode, so skip it
  741. */
  742. if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
  743. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  744. boot_cpu_physical_apicid);
  745. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  746. }
  747. /*
  748. * If we couldn't find a local APIC, then get out of here now!
  749. */
  750. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
  751. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  752. boot_cpu_physical_apicid);
  753. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  754. smpboot_clear_io_apic_irqs();
  755. phys_cpu_present_map = physid_mask_of_physid(0);
  756. map_cpu_to_logical_apicid();
  757. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  758. cpu_set(0, per_cpu(cpu_core_map, 0));
  759. return;
  760. }
  761. verify_local_APIC();
  762. /*
  763. * If SMP should be disabled, then really disable it!
  764. */
  765. if (!max_cpus) {
  766. smp_found_config = 0;
  767. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  768. if (nmi_watchdog == NMI_LOCAL_APIC) {
  769. printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
  770. connect_bsp_APIC();
  771. setup_local_APIC();
  772. }
  773. smpboot_clear_io_apic_irqs();
  774. phys_cpu_present_map = physid_mask_of_physid(0);
  775. map_cpu_to_logical_apicid();
  776. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  777. cpu_set(0, per_cpu(cpu_core_map, 0));
  778. return;
  779. }
  780. connect_bsp_APIC();
  781. setup_local_APIC();
  782. map_cpu_to_logical_apicid();
  783. setup_portio_remap();
  784. /*
  785. * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
  786. *
  787. * In clustered apic mode, phys_cpu_present_map is a constructed thus:
  788. * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
  789. * clustered apic ID.
  790. */
  791. Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
  792. kicked = 1;
  793. for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
  794. apicid = cpu_present_to_apicid(bit);
  795. /*
  796. * Don't even attempt to start the boot CPU!
  797. */
  798. if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
  799. continue;
  800. if (!check_apicid_present(bit))
  801. continue;
  802. if (max_cpus <= cpucount+1)
  803. continue;
  804. if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
  805. printk("CPU #%d not responding - cannot use it.\n",
  806. apicid);
  807. else
  808. ++kicked;
  809. }
  810. /*
  811. * Cleanup possible dangling ends...
  812. */
  813. smpboot_restore_warm_reset_vector();
  814. /*
  815. * Allow the user to impress friends.
  816. */
  817. Dprintk("Before bogomips.\n");
  818. for_each_possible_cpu(cpu)
  819. if (cpu_isset(cpu, cpu_callout_map))
  820. bogosum += cpu_data(cpu).loops_per_jiffy;
  821. printk(KERN_INFO
  822. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  823. cpucount+1,
  824. bogosum/(500000/HZ),
  825. (bogosum/(5000/HZ))%100);
  826. Dprintk("Before bogocount - setting activated=1.\n");
  827. if (smp_b_stepping)
  828. printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
  829. /*
  830. * Don't taint if we are running SMP kernel on a single non-MP
  831. * approved Athlon
  832. */
  833. if (tainted & TAINT_UNSAFE_SMP) {
  834. if (cpucount)
  835. printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
  836. else
  837. tainted &= ~TAINT_UNSAFE_SMP;
  838. }
  839. Dprintk("Boot done.\n");
  840. /*
  841. * construct cpu_sibling_map, so that we can tell sibling CPUs
  842. * efficiently.
  843. */
  844. for_each_possible_cpu(cpu) {
  845. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  846. cpus_clear(per_cpu(cpu_core_map, cpu));
  847. }
  848. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  849. cpu_set(0, per_cpu(cpu_core_map, 0));
  850. smpboot_setup_io_apic();
  851. setup_boot_clock();
  852. }
  853. /* These are wrappers to interface to the new boot process. Someone
  854. who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
  855. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  856. {
  857. smp_commenced_mask = cpumask_of_cpu(0);
  858. cpu_callin_map = cpumask_of_cpu(0);
  859. mb();
  860. smp_boot_cpus(max_cpus);
  861. }
  862. void __init native_smp_prepare_boot_cpu(void)
  863. {
  864. unsigned int cpu = smp_processor_id();
  865. init_gdt(cpu);
  866. switch_to_new_gdt();
  867. cpu_set(cpu, cpu_online_map);
  868. cpu_set(cpu, cpu_callout_map);
  869. cpu_set(cpu, cpu_present_map);
  870. cpu_set(cpu, cpu_possible_map);
  871. __get_cpu_var(cpu_state) = CPU_ONLINE;
  872. }
  873. int __cpuinit native_cpu_up(unsigned int cpu)
  874. {
  875. unsigned long flags;
  876. #ifdef CONFIG_HOTPLUG_CPU
  877. int ret = 0;
  878. /*
  879. * We do warm boot only on cpus that had booted earlier
  880. * Otherwise cold boot is all handled from smp_boot_cpus().
  881. * cpu_callin_map is set during AP kickstart process. Its reset
  882. * when a cpu is taken offline from cpu_exit_clear().
  883. */
  884. if (!cpu_isset(cpu, cpu_callin_map))
  885. ret = __smp_prepare_cpu(cpu);
  886. if (ret)
  887. return -EIO;
  888. #endif
  889. /* In case one didn't come up */
  890. if (!cpu_isset(cpu, cpu_callin_map)) {
  891. printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
  892. return -EIO;
  893. }
  894. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  895. /* Unleash the CPU! */
  896. cpu_set(cpu, smp_commenced_mask);
  897. /*
  898. * Check TSC synchronization with the AP (keep irqs disabled
  899. * while doing so):
  900. */
  901. local_irq_save(flags);
  902. check_tsc_sync_source(cpu);
  903. local_irq_restore(flags);
  904. while (!cpu_isset(cpu, cpu_online_map)) {
  905. cpu_relax();
  906. touch_nmi_watchdog();
  907. }
  908. return 0;
  909. }
  910. void __init native_smp_cpus_done(unsigned int max_cpus)
  911. {
  912. #ifdef CONFIG_X86_IO_APIC
  913. setup_ioapic_dest();
  914. #endif
  915. zap_low_mappings();
  916. }
  917. void __init smp_intr_init(void)
  918. {
  919. /*
  920. * IRQ0 must be given a fixed assignment and initialized,
  921. * because it's used before the IO-APIC is set up.
  922. */
  923. set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
  924. /*
  925. * The reschedule interrupt is a CPU-to-CPU reschedule-helper
  926. * IPI, driven by wakeup.
  927. */
  928. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  929. /* IPI for invalidation */
  930. set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
  931. /* IPI for generic function call */
  932. set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
  933. }
  934. /*
  935. * If the BIOS enumerates physical processors before logical,
  936. * maxcpus=N at enumeration-time can be used to disable HT.
  937. */
  938. static int __init parse_maxcpus(char *arg)
  939. {
  940. extern unsigned int maxcpus;
  941. maxcpus = simple_strtoul(arg, NULL, 0);
  942. return 0;
  943. }
  944. early_param("maxcpus", parse_maxcpus);