smpboot_32.c 29 KB

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