smpboot_32.c 25 KB

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