smpboot.c 34 KB

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