smpboot_32.c 25 KB

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