smpboot_32.c 26 KB

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