smpboot.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. #include <linux/init.h>
  2. #include <linux/smp.h>
  3. #include <linux/module.h>
  4. #include <linux/sched.h>
  5. #include <linux/percpu.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/err.h>
  8. #include <linux/nmi.h>
  9. #include <asm/desc.h>
  10. #include <asm/nmi.h>
  11. #include <asm/irq.h>
  12. #include <asm/smp.h>
  13. #include <asm/cpu.h>
  14. #include <asm/numa.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/nmi.h>
  19. #include <asm/vmi.h>
  20. #include <linux/mc146818rtc.h>
  21. #include <mach_apic.h>
  22. #include <mach_wakecpu.h>
  23. #include <smpboot_hooks.h>
  24. /* Store all idle threads, this can be reused instead of creating
  25. * a new thread. Also avoids complicated thread destroy functionality
  26. * for idle threads.
  27. */
  28. #ifdef CONFIG_HOTPLUG_CPU
  29. /*
  30. * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
  31. * removed after init for !CONFIG_HOTPLUG_CPU.
  32. */
  33. static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
  34. #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
  35. #define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
  36. #else
  37. struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  38. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  39. #define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
  40. #endif
  41. /* Number of siblings per CPU package */
  42. int smp_num_siblings = 1;
  43. EXPORT_SYMBOL(smp_num_siblings);
  44. /* Last level cache ID of each logical CPU */
  45. DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
  46. /* bitmap of online cpus */
  47. cpumask_t cpu_online_map __read_mostly;
  48. EXPORT_SYMBOL(cpu_online_map);
  49. cpumask_t cpu_callin_map;
  50. cpumask_t cpu_callout_map;
  51. cpumask_t cpu_possible_map;
  52. EXPORT_SYMBOL(cpu_possible_map);
  53. /* representing HT siblings of each logical CPU */
  54. DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
  55. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  56. /* representing HT and core siblings of each logical CPU */
  57. DEFINE_PER_CPU(cpumask_t, cpu_core_map);
  58. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  59. /* Per CPU bogomips and other parameters */
  60. DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
  61. EXPORT_PER_CPU_SYMBOL(cpu_info);
  62. static atomic_t init_deasserted;
  63. /* ready for x86_64, no harm for x86, since it will overwrite after alloc */
  64. unsigned char *trampoline_base = __va(SMP_TRAMPOLINE_BASE);
  65. /* representing cpus for which sibling maps can be computed */
  66. static cpumask_t cpu_sibling_setup_map;
  67. /* Set if we find a B stepping CPU */
  68. int __cpuinitdata smp_b_stepping;
  69. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_32)
  70. /* which logical CPUs are on which nodes */
  71. cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
  72. { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
  73. EXPORT_SYMBOL(node_to_cpumask_map);
  74. /* which node each logical CPU is on */
  75. int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  76. EXPORT_SYMBOL(cpu_to_node_map);
  77. /* set up a mapping between cpu and node. */
  78. static void map_cpu_to_node(int cpu, int node)
  79. {
  80. printk(KERN_INFO "Mapping cpu %d to node %d\n", cpu, node);
  81. cpu_set(cpu, node_to_cpumask_map[node]);
  82. cpu_to_node_map[cpu] = node;
  83. }
  84. /* undo a mapping between cpu and node. */
  85. static void unmap_cpu_to_node(int cpu)
  86. {
  87. int node;
  88. printk(KERN_INFO "Unmapping cpu %d from all nodes\n", cpu);
  89. for (node = 0; node < MAX_NUMNODES; node++)
  90. cpu_clear(cpu, node_to_cpumask_map[node]);
  91. cpu_to_node_map[cpu] = 0;
  92. }
  93. #else /* !(CONFIG_NUMA && CONFIG_X86_32) */
  94. #define map_cpu_to_node(cpu, node) ({})
  95. #define unmap_cpu_to_node(cpu) ({})
  96. #endif
  97. #ifdef CONFIG_X86_32
  98. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly =
  99. { [0 ... NR_CPUS-1] = BAD_APICID };
  100. void map_cpu_to_logical_apicid(void)
  101. {
  102. int cpu = smp_processor_id();
  103. int apicid = logical_smp_processor_id();
  104. int node = apicid_to_node(apicid);
  105. if (!node_online(node))
  106. node = first_online_node;
  107. cpu_2_logical_apicid[cpu] = apicid;
  108. map_cpu_to_node(cpu, node);
  109. }
  110. void unmap_cpu_to_logical_apicid(int cpu)
  111. {
  112. cpu_2_logical_apicid[cpu] = BAD_APICID;
  113. unmap_cpu_to_node(cpu);
  114. }
  115. #else
  116. #define unmap_cpu_to_logical_apicid(cpu) do {} while (0)
  117. #define map_cpu_to_logical_apicid() do {} while (0)
  118. #endif
  119. /*
  120. * Report back to the Boot Processor.
  121. * Running on AP.
  122. */
  123. void __cpuinit smp_callin(void)
  124. {
  125. int cpuid, phys_id;
  126. unsigned long timeout;
  127. /*
  128. * If waken up by an INIT in an 82489DX configuration
  129. * we may get here before an INIT-deassert IPI reaches
  130. * our local APIC. We have to wait for the IPI or we'll
  131. * lock up on an APIC access.
  132. */
  133. wait_for_init_deassert(&init_deasserted);
  134. /*
  135. * (This works even if the APIC is not enabled.)
  136. */
  137. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  138. cpuid = smp_processor_id();
  139. if (cpu_isset(cpuid, cpu_callin_map)) {
  140. panic("%s: phys CPU#%d, CPU#%d already present??\n", __func__,
  141. phys_id, cpuid);
  142. }
  143. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  144. /*
  145. * STARTUP IPIs are fragile beasts as they might sometimes
  146. * trigger some glue motherboard logic. Complete APIC bus
  147. * silence for 1 second, this overestimates the time the
  148. * boot CPU is spending to send the up to 2 STARTUP IPIs
  149. * by a factor of two. This should be enough.
  150. */
  151. /*
  152. * Waiting 2s total for startup (udelay is not yet working)
  153. */
  154. timeout = jiffies + 2*HZ;
  155. while (time_before(jiffies, timeout)) {
  156. /*
  157. * Has the boot CPU finished it's STARTUP sequence?
  158. */
  159. if (cpu_isset(cpuid, cpu_callout_map))
  160. break;
  161. cpu_relax();
  162. }
  163. if (!time_before(jiffies, timeout)) {
  164. panic("%s: CPU%d started up but did not get a callout!\n",
  165. __func__, cpuid);
  166. }
  167. /*
  168. * the boot CPU has finished the init stage and is spinning
  169. * on callin_map until we finish. We are free to set up this
  170. * CPU, first the APIC. (this is probably redundant on most
  171. * boards)
  172. */
  173. Dprintk("CALLIN, before setup_local_APIC().\n");
  174. smp_callin_clear_local_apic();
  175. setup_local_APIC();
  176. end_local_APIC_setup();
  177. map_cpu_to_logical_apicid();
  178. /*
  179. * Get our bogomips.
  180. *
  181. * Need to enable IRQs because it can take longer and then
  182. * the NMI watchdog might kill us.
  183. */
  184. local_irq_enable();
  185. calibrate_delay();
  186. local_irq_disable();
  187. Dprintk("Stack at about %p\n", &cpuid);
  188. /*
  189. * Save our processor parameters
  190. */
  191. smp_store_cpu_info(cpuid);
  192. /*
  193. * Allow the master to continue.
  194. */
  195. cpu_set(cpuid, cpu_callin_map);
  196. }
  197. /*
  198. * Activate a secondary processor.
  199. */
  200. void __cpuinit start_secondary(void *unused)
  201. {
  202. /*
  203. * Don't put *anything* before cpu_init(), SMP booting is too
  204. * fragile that we want to limit the things done here to the
  205. * most necessary things.
  206. */
  207. #ifdef CONFIG_VMI
  208. vmi_bringup();
  209. #endif
  210. cpu_init();
  211. preempt_disable();
  212. smp_callin();
  213. /* otherwise gcc will move up smp_processor_id before the cpu_init */
  214. barrier();
  215. /*
  216. * Check TSC synchronization with the BP:
  217. */
  218. check_tsc_sync_target();
  219. if (nmi_watchdog == NMI_IO_APIC) {
  220. disable_8259A_irq(0);
  221. enable_NMI_through_LVT0();
  222. enable_8259A_irq(0);
  223. }
  224. /* This must be done before setting cpu_online_map */
  225. set_cpu_sibling_map(raw_smp_processor_id());
  226. wmb();
  227. /*
  228. * We need to hold call_lock, so there is no inconsistency
  229. * between the time smp_call_function() determines number of
  230. * IPI recipients, and the time when the determination is made
  231. * for which cpus receive the IPI. Holding this
  232. * lock helps us to not include this cpu in a currently in progress
  233. * smp_call_function().
  234. */
  235. lock_ipi_call_lock();
  236. #ifdef CONFIG_X86_64
  237. spin_lock(&vector_lock);
  238. /* Setup the per cpu irq handling data structures */
  239. __setup_vector_irq(smp_processor_id());
  240. /*
  241. * Allow the master to continue.
  242. */
  243. spin_unlock(&vector_lock);
  244. #endif
  245. cpu_set(smp_processor_id(), cpu_online_map);
  246. unlock_ipi_call_lock();
  247. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  248. setup_secondary_clock();
  249. wmb();
  250. cpu_idle();
  251. }
  252. #ifdef CONFIG_X86_32
  253. /*
  254. * Everything has been set up for the secondary
  255. * CPUs - they just need to reload everything
  256. * from the task structure
  257. * This function must not return.
  258. */
  259. void __devinit initialize_secondary(void)
  260. {
  261. /*
  262. * We don't actually need to load the full TSS,
  263. * basically just the stack pointer and the ip.
  264. */
  265. asm volatile(
  266. "movl %0,%%esp\n\t"
  267. "jmp *%1"
  268. :
  269. :"m" (current->thread.sp), "m" (current->thread.ip));
  270. }
  271. #endif
  272. static void __cpuinit smp_apply_quirks(struct cpuinfo_x86 *c)
  273. {
  274. #ifdef CONFIG_X86_32
  275. /*
  276. * Mask B, Pentium, but not Pentium MMX
  277. */
  278. if (c->x86_vendor == X86_VENDOR_INTEL &&
  279. c->x86 == 5 &&
  280. c->x86_mask >= 1 && c->x86_mask <= 4 &&
  281. c->x86_model <= 3)
  282. /*
  283. * Remember we have B step Pentia with bugs
  284. */
  285. smp_b_stepping = 1;
  286. /*
  287. * Certain Athlons might work (for various values of 'work') in SMP
  288. * but they are not certified as MP capable.
  289. */
  290. if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
  291. if (num_possible_cpus() == 1)
  292. goto valid_k7;
  293. /* Athlon 660/661 is valid. */
  294. if ((c->x86_model == 6) && ((c->x86_mask == 0) ||
  295. (c->x86_mask == 1)))
  296. goto valid_k7;
  297. /* Duron 670 is valid */
  298. if ((c->x86_model == 7) && (c->x86_mask == 0))
  299. goto valid_k7;
  300. /*
  301. * Athlon 662, Duron 671, and Athlon >model 7 have capability
  302. * bit. It's worth noting that the A5 stepping (662) of some
  303. * Athlon XP's have the MP bit set.
  304. * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for
  305. * more.
  306. */
  307. if (((c->x86_model == 6) && (c->x86_mask >= 2)) ||
  308. ((c->x86_model == 7) && (c->x86_mask >= 1)) ||
  309. (c->x86_model > 7))
  310. if (cpu_has_mp)
  311. goto valid_k7;
  312. /* If we get here, not a certified SMP capable AMD system. */
  313. add_taint(TAINT_UNSAFE_SMP);
  314. }
  315. valid_k7:
  316. ;
  317. #endif
  318. }
  319. void smp_checks(void)
  320. {
  321. if (smp_b_stepping)
  322. printk(KERN_WARNING "WARNING: SMP operation may be unreliable"
  323. "with B stepping processors.\n");
  324. /*
  325. * Don't taint if we are running SMP kernel on a single non-MP
  326. * approved Athlon
  327. */
  328. if (tainted & TAINT_UNSAFE_SMP) {
  329. if (num_online_cpus())
  330. printk(KERN_INFO "WARNING: This combination of AMD"
  331. "processors is not suitable for SMP.\n");
  332. else
  333. tainted &= ~TAINT_UNSAFE_SMP;
  334. }
  335. }
  336. /*
  337. * The bootstrap kernel entry code has set these up. Save them for
  338. * a given CPU
  339. */
  340. void __cpuinit smp_store_cpu_info(int id)
  341. {
  342. struct cpuinfo_x86 *c = &cpu_data(id);
  343. *c = boot_cpu_data;
  344. c->cpu_index = id;
  345. if (id != 0)
  346. identify_secondary_cpu(c);
  347. smp_apply_quirks(c);
  348. }
  349. void __cpuinit set_cpu_sibling_map(int cpu)
  350. {
  351. int i;
  352. struct cpuinfo_x86 *c = &cpu_data(cpu);
  353. cpu_set(cpu, cpu_sibling_setup_map);
  354. if (smp_num_siblings > 1) {
  355. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  356. if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
  357. c->cpu_core_id == cpu_data(i).cpu_core_id) {
  358. cpu_set(i, per_cpu(cpu_sibling_map, cpu));
  359. cpu_set(cpu, per_cpu(cpu_sibling_map, i));
  360. cpu_set(i, per_cpu(cpu_core_map, cpu));
  361. cpu_set(cpu, per_cpu(cpu_core_map, i));
  362. cpu_set(i, c->llc_shared_map);
  363. cpu_set(cpu, cpu_data(i).llc_shared_map);
  364. }
  365. }
  366. } else {
  367. cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
  368. }
  369. cpu_set(cpu, c->llc_shared_map);
  370. if (current_cpu_data.x86_max_cores == 1) {
  371. per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
  372. c->booted_cores = 1;
  373. return;
  374. }
  375. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  376. if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
  377. per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
  378. cpu_set(i, c->llc_shared_map);
  379. cpu_set(cpu, cpu_data(i).llc_shared_map);
  380. }
  381. if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
  382. cpu_set(i, per_cpu(cpu_core_map, cpu));
  383. cpu_set(cpu, per_cpu(cpu_core_map, i));
  384. /*
  385. * Does this new cpu bringup a new core?
  386. */
  387. if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
  388. /*
  389. * for each core in package, increment
  390. * the booted_cores for this new cpu
  391. */
  392. if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
  393. c->booted_cores++;
  394. /*
  395. * increment the core count for all
  396. * the other cpus in this package
  397. */
  398. if (i != cpu)
  399. cpu_data(i).booted_cores++;
  400. } else if (i != cpu && !c->booted_cores)
  401. c->booted_cores = cpu_data(i).booted_cores;
  402. }
  403. }
  404. }
  405. /* maps the cpu to the sched domain representing multi-core */
  406. cpumask_t cpu_coregroup_map(int cpu)
  407. {
  408. struct cpuinfo_x86 *c = &cpu_data(cpu);
  409. /*
  410. * For perf, we return last level cache shared map.
  411. * And for power savings, we return cpu_core_map
  412. */
  413. if (sched_mc_power_savings || sched_smt_power_savings)
  414. return per_cpu(cpu_core_map, cpu);
  415. else
  416. return c->llc_shared_map;
  417. }
  418. /*
  419. * Currently trivial. Write the real->protected mode
  420. * bootstrap into the page concerned. The caller
  421. * has made sure it's suitably aligned.
  422. */
  423. unsigned long __cpuinit setup_trampoline(void)
  424. {
  425. memcpy(trampoline_base, trampoline_data,
  426. trampoline_end - trampoline_data);
  427. return virt_to_phys(trampoline_base);
  428. }
  429. #ifdef CONFIG_X86_32
  430. /*
  431. * We are called very early to get the low memory for the
  432. * SMP bootup trampoline page.
  433. */
  434. void __init smp_alloc_memory(void)
  435. {
  436. trampoline_base = alloc_bootmem_low_pages(PAGE_SIZE);
  437. /*
  438. * Has to be in very low memory so we can execute
  439. * real-mode AP code.
  440. */
  441. if (__pa(trampoline_base) >= 0x9F000)
  442. BUG();
  443. }
  444. #endif
  445. void impress_friends(void)
  446. {
  447. int cpu;
  448. unsigned long bogosum = 0;
  449. /*
  450. * Allow the user to impress friends.
  451. */
  452. Dprintk("Before bogomips.\n");
  453. for_each_possible_cpu(cpu)
  454. if (cpu_isset(cpu, cpu_callout_map))
  455. bogosum += cpu_data(cpu).loops_per_jiffy;
  456. printk(KERN_INFO
  457. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  458. num_online_cpus(),
  459. bogosum/(500000/HZ),
  460. (bogosum/(5000/HZ))%100);
  461. Dprintk("Before bogocount - setting activated=1.\n");
  462. }
  463. static inline void __inquire_remote_apic(int apicid)
  464. {
  465. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  466. char *names[] = { "ID", "VERSION", "SPIV" };
  467. int timeout;
  468. u32 status;
  469. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  470. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  471. printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
  472. /*
  473. * Wait for idle.
  474. */
  475. status = safe_apic_wait_icr_idle();
  476. if (status)
  477. printk(KERN_CONT
  478. "a previous APIC delivery may have failed\n");
  479. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  480. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  481. timeout = 0;
  482. do {
  483. udelay(100);
  484. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  485. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  486. switch (status) {
  487. case APIC_ICR_RR_VALID:
  488. status = apic_read(APIC_RRR);
  489. printk(KERN_CONT "%08x\n", status);
  490. break;
  491. default:
  492. printk(KERN_CONT "failed\n");
  493. }
  494. }
  495. }
  496. #ifdef WAKE_SECONDARY_VIA_NMI
  497. /*
  498. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  499. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  500. * won't ... remember to clear down the APIC, etc later.
  501. */
  502. static int __devinit
  503. wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
  504. {
  505. unsigned long send_status, accept_status = 0;
  506. int maxlvt;
  507. /* Target chip */
  508. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
  509. /* Boot on the stack */
  510. /* Kick the second */
  511. apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
  512. Dprintk("Waiting for send to finish...\n");
  513. send_status = safe_apic_wait_icr_idle();
  514. /*
  515. * Give the other CPU some time to accept the IPI.
  516. */
  517. udelay(200);
  518. /*
  519. * Due to the Pentium erratum 3AP.
  520. */
  521. maxlvt = lapic_get_maxlvt();
  522. if (maxlvt > 3) {
  523. apic_read_around(APIC_SPIV);
  524. apic_write(APIC_ESR, 0);
  525. }
  526. accept_status = (apic_read(APIC_ESR) & 0xEF);
  527. Dprintk("NMI sent.\n");
  528. if (send_status)
  529. printk(KERN_ERR "APIC never delivered???\n");
  530. if (accept_status)
  531. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  532. return (send_status | accept_status);
  533. }
  534. #endif /* WAKE_SECONDARY_VIA_NMI */
  535. #ifdef WAKE_SECONDARY_VIA_INIT
  536. static int __devinit
  537. wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
  538. {
  539. unsigned long send_status, accept_status = 0;
  540. int maxlvt, num_starts, j;
  541. /*
  542. * Be paranoid about clearing APIC errors.
  543. */
  544. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  545. apic_read_around(APIC_SPIV);
  546. apic_write(APIC_ESR, 0);
  547. apic_read(APIC_ESR);
  548. }
  549. Dprintk("Asserting INIT.\n");
  550. /*
  551. * Turn INIT on target chip
  552. */
  553. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  554. /*
  555. * Send IPI
  556. */
  557. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  558. | APIC_DM_INIT);
  559. Dprintk("Waiting for send to finish...\n");
  560. send_status = safe_apic_wait_icr_idle();
  561. mdelay(10);
  562. Dprintk("Deasserting INIT.\n");
  563. /* Target chip */
  564. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  565. /* Send IPI */
  566. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  567. Dprintk("Waiting for send to finish...\n");
  568. send_status = safe_apic_wait_icr_idle();
  569. mb();
  570. atomic_set(&init_deasserted, 1);
  571. /*
  572. * Should we send STARTUP IPIs ?
  573. *
  574. * Determine this based on the APIC version.
  575. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  576. */
  577. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  578. num_starts = 2;
  579. else
  580. num_starts = 0;
  581. /*
  582. * Paravirt / VMI wants a startup IPI hook here to set up the
  583. * target processor state.
  584. */
  585. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  586. #ifdef CONFIG_X86_64
  587. (unsigned long)init_rsp);
  588. #else
  589. (unsigned long)stack_start.sp);
  590. #endif
  591. /*
  592. * Run STARTUP IPI loop.
  593. */
  594. Dprintk("#startup loops: %d.\n", num_starts);
  595. maxlvt = lapic_get_maxlvt();
  596. for (j = 1; j <= num_starts; j++) {
  597. Dprintk("Sending STARTUP #%d.\n", j);
  598. apic_read_around(APIC_SPIV);
  599. apic_write(APIC_ESR, 0);
  600. apic_read(APIC_ESR);
  601. Dprintk("After apic_write.\n");
  602. /*
  603. * STARTUP IPI
  604. */
  605. /* Target chip */
  606. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  607. /* Boot on the stack */
  608. /* Kick the second */
  609. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  610. | (start_eip >> 12));
  611. /*
  612. * Give the other CPU some time to accept the IPI.
  613. */
  614. udelay(300);
  615. Dprintk("Startup point 1.\n");
  616. Dprintk("Waiting for send to finish...\n");
  617. send_status = safe_apic_wait_icr_idle();
  618. /*
  619. * Give the other CPU some time to accept the IPI.
  620. */
  621. udelay(200);
  622. /*
  623. * Due to the Pentium erratum 3AP.
  624. */
  625. if (maxlvt > 3) {
  626. apic_read_around(APIC_SPIV);
  627. apic_write(APIC_ESR, 0);
  628. }
  629. accept_status = (apic_read(APIC_ESR) & 0xEF);
  630. if (send_status || accept_status)
  631. break;
  632. }
  633. Dprintk("After Startup.\n");
  634. if (send_status)
  635. printk(KERN_ERR "APIC never delivered???\n");
  636. if (accept_status)
  637. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  638. return (send_status | accept_status);
  639. }
  640. #endif /* WAKE_SECONDARY_VIA_INIT */
  641. struct create_idle {
  642. struct work_struct work;
  643. struct task_struct *idle;
  644. struct completion done;
  645. int cpu;
  646. };
  647. static void __cpuinit do_fork_idle(struct work_struct *work)
  648. {
  649. struct create_idle *c_idle =
  650. container_of(work, struct create_idle, work);
  651. c_idle->idle = fork_idle(c_idle->cpu);
  652. complete(&c_idle->done);
  653. }
  654. static int __cpuinit do_boot_cpu(int apicid, int cpu)
  655. /*
  656. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  657. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  658. * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
  659. */
  660. {
  661. unsigned long boot_error = 0;
  662. int timeout;
  663. unsigned long start_ip;
  664. unsigned short nmi_high = 0, nmi_low = 0;
  665. struct create_idle c_idle = {
  666. .cpu = cpu,
  667. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  668. };
  669. INIT_WORK(&c_idle.work, do_fork_idle);
  670. #ifdef CONFIG_X86_64
  671. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  672. if (!cpu_gdt_descr[cpu].address &&
  673. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  674. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  675. return -1;
  676. }
  677. /* Allocate node local memory for AP pdas */
  678. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  679. struct x8664_pda *newpda, *pda;
  680. int node = cpu_to_node(cpu);
  681. pda = cpu_pda(cpu);
  682. newpda = kmalloc_node(sizeof(struct x8664_pda), GFP_ATOMIC,
  683. node);
  684. if (newpda) {
  685. memcpy(newpda, pda, sizeof(struct x8664_pda));
  686. cpu_pda(cpu) = newpda;
  687. } else
  688. printk(KERN_ERR
  689. "Could not allocate node local PDA for CPU %d on node %d\n",
  690. cpu, node);
  691. }
  692. #endif
  693. alternatives_smp_switch(1);
  694. c_idle.idle = get_idle_for_cpu(cpu);
  695. /*
  696. * We can't use kernel_thread since we must avoid to
  697. * reschedule the child.
  698. */
  699. if (c_idle.idle) {
  700. c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
  701. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  702. init_idle(c_idle.idle, cpu);
  703. goto do_rest;
  704. }
  705. if (!keventd_up() || current_is_keventd())
  706. c_idle.work.func(&c_idle.work);
  707. else {
  708. schedule_work(&c_idle.work);
  709. wait_for_completion(&c_idle.done);
  710. }
  711. if (IS_ERR(c_idle.idle)) {
  712. printk("failed fork for CPU %d\n", cpu);
  713. return PTR_ERR(c_idle.idle);
  714. }
  715. set_idle_for_cpu(cpu, c_idle.idle);
  716. do_rest:
  717. #ifdef CONFIG_X86_32
  718. per_cpu(current_task, cpu) = c_idle.idle;
  719. init_gdt(cpu);
  720. early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
  721. c_idle.idle->thread.ip = (unsigned long) start_secondary;
  722. /* Stack for startup_32 can be just as for start_secondary onwards */
  723. stack_start.sp = (void *) c_idle.idle->thread.sp;
  724. irq_ctx_init(cpu);
  725. #else
  726. cpu_pda(cpu)->pcurrent = c_idle.idle;
  727. init_rsp = c_idle.idle->thread.sp;
  728. load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
  729. initial_code = (unsigned long)start_secondary;
  730. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  731. #endif
  732. /* start_ip had better be page-aligned! */
  733. start_ip = setup_trampoline();
  734. /* So we see what's up */
  735. printk(KERN_INFO "Booting processor %d/%d ip %lx\n",
  736. cpu, apicid, start_ip);
  737. /*
  738. * This grunge runs the startup process for
  739. * the targeted processor.
  740. */
  741. atomic_set(&init_deasserted, 0);
  742. Dprintk("Setting warm reset code and vector.\n");
  743. store_NMI_vector(&nmi_high, &nmi_low);
  744. smpboot_setup_warm_reset_vector(start_ip);
  745. /*
  746. * Be paranoid about clearing APIC errors.
  747. */
  748. apic_write(APIC_ESR, 0);
  749. apic_read(APIC_ESR);
  750. /*
  751. * Starting actual IPI sequence...
  752. */
  753. boot_error = wakeup_secondary_cpu(apicid, start_ip);
  754. if (!boot_error) {
  755. /*
  756. * allow APs to start initializing.
  757. */
  758. Dprintk("Before Callout %d.\n", cpu);
  759. cpu_set(cpu, cpu_callout_map);
  760. Dprintk("After Callout %d.\n", cpu);
  761. /*
  762. * Wait 5s total for a response
  763. */
  764. for (timeout = 0; timeout < 50000; timeout++) {
  765. if (cpu_isset(cpu, cpu_callin_map))
  766. break; /* It has booted */
  767. udelay(100);
  768. }
  769. if (cpu_isset(cpu, cpu_callin_map)) {
  770. /* number CPUs logically, starting from 1 (BSP is 0) */
  771. Dprintk("OK.\n");
  772. printk(KERN_INFO "CPU%d: ", cpu);
  773. print_cpu_info(&cpu_data(cpu));
  774. Dprintk("CPU has booted.\n");
  775. } else {
  776. boot_error = 1;
  777. if (*((volatile unsigned char *)trampoline_base)
  778. == 0xA5)
  779. /* trampoline started but...? */
  780. printk(KERN_ERR "Stuck ??\n");
  781. else
  782. /* trampoline code not run */
  783. printk(KERN_ERR "Not responding.\n");
  784. inquire_remote_apic(apicid);
  785. }
  786. }
  787. if (boot_error) {
  788. /* Try to put things back the way they were before ... */
  789. unmap_cpu_to_logical_apicid(cpu);
  790. #ifdef CONFIG_X86_64
  791. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  792. #endif
  793. cpu_clear(cpu, cpu_callout_map); /* was set by do_boot_cpu() */
  794. cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
  795. cpu_clear(cpu, cpu_possible_map);
  796. cpu_clear(cpu, cpu_present_map);
  797. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  798. }
  799. /* mark "stuck" area as not stuck */
  800. *((volatile unsigned long *)trampoline_base) = 0;
  801. return boot_error;
  802. }
  803. int __cpuinit native_cpu_up(unsigned int cpu)
  804. {
  805. int apicid = cpu_present_to_apicid(cpu);
  806. unsigned long flags;
  807. int err;
  808. WARN_ON(irqs_disabled());
  809. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  810. if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
  811. !physid_isset(apicid, phys_cpu_present_map)) {
  812. printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
  813. return -EINVAL;
  814. }
  815. /*
  816. * Already booted CPU?
  817. */
  818. if (cpu_isset(cpu, cpu_callin_map)) {
  819. Dprintk("do_boot_cpu %d Already started\n", cpu);
  820. return -ENOSYS;
  821. }
  822. /*
  823. * Save current MTRR state in case it was changed since early boot
  824. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  825. */
  826. mtrr_save_state();
  827. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  828. #ifdef CONFIG_X86_32
  829. /* init low mem mapping */
  830. clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
  831. min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
  832. flush_tlb_all();
  833. #endif
  834. err = do_boot_cpu(apicid, cpu);
  835. if (err < 0) {
  836. Dprintk("do_boot_cpu failed %d\n", err);
  837. return err;
  838. }
  839. /*
  840. * Check TSC synchronization with the AP (keep irqs disabled
  841. * while doing so):
  842. */
  843. local_irq_save(flags);
  844. check_tsc_sync_source(cpu);
  845. local_irq_restore(flags);
  846. while (!cpu_isset(cpu, cpu_online_map)) {
  847. cpu_relax();
  848. touch_nmi_watchdog();
  849. }
  850. return 0;
  851. }
  852. #ifdef CONFIG_HOTPLUG_CPU
  853. void remove_siblinginfo(int cpu)
  854. {
  855. int sibling;
  856. struct cpuinfo_x86 *c = &cpu_data(cpu);
  857. for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
  858. cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
  859. /*/
  860. * last thread sibling in this cpu core going down
  861. */
  862. if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
  863. cpu_data(sibling).booted_cores--;
  864. }
  865. for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
  866. cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
  867. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  868. cpus_clear(per_cpu(cpu_core_map, cpu));
  869. c->phys_proc_id = 0;
  870. c->cpu_core_id = 0;
  871. cpu_clear(cpu, cpu_sibling_setup_map);
  872. }
  873. int additional_cpus __initdata = -1;
  874. static __init int setup_additional_cpus(char *s)
  875. {
  876. return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
  877. }
  878. early_param("additional_cpus", setup_additional_cpus);
  879. /*
  880. * cpu_possible_map should be static, it cannot change as cpu's
  881. * are onlined, or offlined. The reason is per-cpu data-structures
  882. * are allocated by some modules at init time, and dont expect to
  883. * do this dynamically on cpu arrival/departure.
  884. * cpu_present_map on the other hand can change dynamically.
  885. * In case when cpu_hotplug is not compiled, then we resort to current
  886. * behaviour, which is cpu_possible == cpu_present.
  887. * - Ashok Raj
  888. *
  889. * Three ways to find out the number of additional hotplug CPUs:
  890. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  891. * - The user can overwrite it with additional_cpus=NUM
  892. * - Otherwise don't reserve additional CPUs.
  893. * We do this because additional CPUs waste a lot of memory.
  894. * -AK
  895. */
  896. __init void prefill_possible_map(void)
  897. {
  898. int i;
  899. int possible;
  900. if (additional_cpus == -1) {
  901. if (disabled_cpus > 0)
  902. additional_cpus = disabled_cpus;
  903. else
  904. additional_cpus = 0;
  905. }
  906. possible = num_processors + additional_cpus;
  907. if (possible > NR_CPUS)
  908. possible = NR_CPUS;
  909. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  910. possible, max_t(int, possible - num_processors, 0));
  911. for (i = 0; i < possible; i++)
  912. cpu_set(i, cpu_possible_map);
  913. }
  914. static void __ref remove_cpu_from_maps(int cpu)
  915. {
  916. cpu_clear(cpu, cpu_online_map);
  917. #ifdef CONFIG_X86_64
  918. cpu_clear(cpu, cpu_callout_map);
  919. cpu_clear(cpu, cpu_callin_map);
  920. /* was set by cpu_init() */
  921. clear_bit(cpu, (unsigned long *)&cpu_initialized);
  922. clear_node_cpumask(cpu);
  923. #endif
  924. }
  925. int __cpu_disable(void)
  926. {
  927. int cpu = smp_processor_id();
  928. /*
  929. * Perhaps use cpufreq to drop frequency, but that could go
  930. * into generic code.
  931. *
  932. * We won't take down the boot processor on i386 due to some
  933. * interrupts only being able to be serviced by the BSP.
  934. * Especially so if we're not using an IOAPIC -zwane
  935. */
  936. if (cpu == 0)
  937. return -EBUSY;
  938. if (nmi_watchdog == NMI_LOCAL_APIC)
  939. stop_apic_nmi_watchdog(NULL);
  940. clear_local_APIC();
  941. /*
  942. * HACK:
  943. * Allow any queued timer interrupts to get serviced
  944. * This is only a temporary solution until we cleanup
  945. * fixup_irqs as we do for IA64.
  946. */
  947. local_irq_enable();
  948. mdelay(1);
  949. local_irq_disable();
  950. remove_siblinginfo(cpu);
  951. /* It's now safe to remove this processor from the online map */
  952. remove_cpu_from_maps(cpu);
  953. fixup_irqs(cpu_online_map);
  954. return 0;
  955. }
  956. void __cpu_die(unsigned int cpu)
  957. {
  958. /* We don't do anything here: idle task is faking death itself. */
  959. unsigned int i;
  960. for (i = 0; i < 10; i++) {
  961. /* They ack this in play_dead by setting CPU_DEAD */
  962. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  963. printk(KERN_INFO "CPU %d is now offline\n", cpu);
  964. if (1 == num_online_cpus())
  965. alternatives_smp_switch(0);
  966. return;
  967. }
  968. msleep(100);
  969. }
  970. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  971. }
  972. #else /* ... !CONFIG_HOTPLUG_CPU */
  973. int __cpu_disable(void)
  974. {
  975. return -ENOSYS;
  976. }
  977. void __cpu_die(unsigned int cpu)
  978. {
  979. /* We said "no" in __cpu_disable */
  980. BUG();
  981. }
  982. #endif
  983. /*
  984. * If the BIOS enumerates physical processors before logical,
  985. * maxcpus=N at enumeration-time can be used to disable HT.
  986. */
  987. static int __init parse_maxcpus(char *arg)
  988. {
  989. extern unsigned int maxcpus;
  990. maxcpus = simple_strtoul(arg, NULL, 0);
  991. return 0;
  992. }
  993. early_param("maxcpus", parse_maxcpus);