smpboot.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. * x86 SMP booting functions
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
  5. * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
  6. * Copyright 2001 Andi Kleen, SuSE Labs.
  7. *
  8. * Much of the core SMP work is based on previous work by Thomas Radke, to
  9. * whom a great many thanks are extended.
  10. *
  11. * Thanks to Intel for making available several different Pentium,
  12. * Pentium Pro and Pentium-II/Xeon MP machines.
  13. * Original development of Linux SMP code supported by Caldera.
  14. *
  15. * This code is released under the GNU General Public License version 2 or
  16. * later.
  17. *
  18. * Fixes
  19. * Felix Koop : NR_CPUS used properly
  20. * Jose Renau : Handle single CPU case.
  21. * Alan Cox : By repeated request 8) - Total BogoMIPS report.
  22. * Greg Wright : Fix for kernel stacks panic.
  23. * Erich Boleyn : MP v1.4 and additional changes.
  24. * Matthias Sattler : Changes for 2.1 kernel map.
  25. * Michel Lespinasse : Changes for 2.1 kernel map.
  26. * Michael Chastain : Change trampoline.S to gnu as.
  27. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  28. * Ingo Molnar : Added APIC timers, based on code
  29. * from Jose Renau
  30. * Ingo Molnar : various cleanups and rewrites
  31. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  32. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  33. * Andi Kleen : Changed for SMP boot into long mode.
  34. * Martin J. Bligh : Added support for multi-quad systems
  35. * Dave Jones : Report invalid combinations of Athlon CPUs.
  36. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  37. * Andi Kleen : Converted to new state machine.
  38. * Ashok Raj : CPU hotplug support
  39. * Glauber Costa : i386 and x86_64 integration
  40. */
  41. #include <linux/init.h>
  42. #include <linux/smp.h>
  43. #include <linux/module.h>
  44. #include <linux/sched.h>
  45. #include <linux/percpu.h>
  46. #include <linux/bootmem.h>
  47. #include <linux/err.h>
  48. #include <linux/nmi.h>
  49. #include <linux/tboot.h>
  50. #include <linux/stackprotector.h>
  51. #include <linux/gfp.h>
  52. #include <asm/acpi.h>
  53. #include <asm/desc.h>
  54. #include <asm/nmi.h>
  55. #include <asm/irq.h>
  56. #include <asm/idle.h>
  57. #include <asm/trampoline.h>
  58. #include <asm/cpu.h>
  59. #include <asm/numa.h>
  60. #include <asm/pgtable.h>
  61. #include <asm/tlbflush.h>
  62. #include <asm/mtrr.h>
  63. #include <asm/vmi.h>
  64. #include <asm/apic.h>
  65. #include <asm/setup.h>
  66. #include <asm/uv/uv.h>
  67. #include <linux/mc146818rtc.h>
  68. #include <asm/smpboot_hooks.h>
  69. #include <asm/i8259.h>
  70. #ifdef CONFIG_X86_32
  71. u8 apicid_2_node[MAX_APICID];
  72. static int low_mappings;
  73. #endif
  74. /* State of each CPU */
  75. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  76. /* Store all idle threads, this can be reused instead of creating
  77. * a new thread. Also avoids complicated thread destroy functionality
  78. * for idle threads.
  79. */
  80. #ifdef CONFIG_HOTPLUG_CPU
  81. /*
  82. * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
  83. * removed after init for !CONFIG_HOTPLUG_CPU.
  84. */
  85. static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
  86. #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
  87. #define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
  88. #else
  89. static struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  90. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  91. #define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
  92. #endif
  93. /* Number of siblings per CPU package */
  94. int smp_num_siblings = 1;
  95. EXPORT_SYMBOL(smp_num_siblings);
  96. /* Last level cache ID of each logical CPU */
  97. DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
  98. /* representing HT siblings of each logical CPU */
  99. DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
  100. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  101. /* representing HT and core siblings of each logical CPU */
  102. DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
  103. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  104. /* Per CPU bogomips and other parameters */
  105. DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
  106. EXPORT_PER_CPU_SYMBOL(cpu_info);
  107. atomic_t init_deasserted;
  108. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_32)
  109. /* which node each logical CPU is on */
  110. int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  111. EXPORT_SYMBOL(cpu_to_node_map);
  112. /* set up a mapping between cpu and node. */
  113. static void map_cpu_to_node(int cpu, int node)
  114. {
  115. printk(KERN_INFO "Mapping cpu %d to node %d\n", cpu, node);
  116. cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
  117. cpu_to_node_map[cpu] = node;
  118. }
  119. /* undo a mapping between cpu and node. */
  120. static void unmap_cpu_to_node(int cpu)
  121. {
  122. int node;
  123. printk(KERN_INFO "Unmapping cpu %d from all nodes\n", cpu);
  124. for (node = 0; node < MAX_NUMNODES; node++)
  125. cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
  126. cpu_to_node_map[cpu] = 0;
  127. }
  128. #else /* !(CONFIG_NUMA && CONFIG_X86_32) */
  129. #define map_cpu_to_node(cpu, node) ({})
  130. #define unmap_cpu_to_node(cpu) ({})
  131. #endif
  132. #ifdef CONFIG_X86_32
  133. static int boot_cpu_logical_apicid;
  134. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly =
  135. { [0 ... NR_CPUS-1] = BAD_APICID };
  136. static void map_cpu_to_logical_apicid(void)
  137. {
  138. int cpu = smp_processor_id();
  139. int apicid = logical_smp_processor_id();
  140. int node = apic->apicid_to_node(apicid);
  141. if (!node_online(node))
  142. node = first_online_node;
  143. cpu_2_logical_apicid[cpu] = apicid;
  144. map_cpu_to_node(cpu, node);
  145. }
  146. void numa_remove_cpu(int cpu)
  147. {
  148. cpu_2_logical_apicid[cpu] = BAD_APICID;
  149. unmap_cpu_to_node(cpu);
  150. }
  151. #else
  152. #define map_cpu_to_logical_apicid() do {} while (0)
  153. #endif
  154. /*
  155. * Report back to the Boot Processor.
  156. * Running on AP.
  157. */
  158. static void __cpuinit smp_callin(void)
  159. {
  160. int cpuid, phys_id;
  161. unsigned long timeout;
  162. /*
  163. * If waken up by an INIT in an 82489DX configuration
  164. * we may get here before an INIT-deassert IPI reaches
  165. * our local APIC. We have to wait for the IPI or we'll
  166. * lock up on an APIC access.
  167. */
  168. if (apic->wait_for_init_deassert)
  169. apic->wait_for_init_deassert(&init_deasserted);
  170. /*
  171. * (This works even if the APIC is not enabled.)
  172. */
  173. phys_id = read_apic_id();
  174. cpuid = smp_processor_id();
  175. if (cpumask_test_cpu(cpuid, cpu_callin_mask)) {
  176. panic("%s: phys CPU#%d, CPU#%d already present??\n", __func__,
  177. phys_id, cpuid);
  178. }
  179. pr_debug("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  180. /*
  181. * STARTUP IPIs are fragile beasts as they might sometimes
  182. * trigger some glue motherboard logic. Complete APIC bus
  183. * silence for 1 second, this overestimates the time the
  184. * boot CPU is spending to send the up to 2 STARTUP IPIs
  185. * by a factor of two. This should be enough.
  186. */
  187. /*
  188. * Waiting 2s total for startup (udelay is not yet working)
  189. */
  190. timeout = jiffies + 2*HZ;
  191. while (time_before(jiffies, timeout)) {
  192. /*
  193. * Has the boot CPU finished it's STARTUP sequence?
  194. */
  195. if (cpumask_test_cpu(cpuid, cpu_callout_mask))
  196. break;
  197. cpu_relax();
  198. }
  199. if (!time_before(jiffies, timeout)) {
  200. panic("%s: CPU%d started up but did not get a callout!\n",
  201. __func__, cpuid);
  202. }
  203. /*
  204. * the boot CPU has finished the init stage and is spinning
  205. * on callin_map until we finish. We are free to set up this
  206. * CPU, first the APIC. (this is probably redundant on most
  207. * boards)
  208. */
  209. pr_debug("CALLIN, before setup_local_APIC().\n");
  210. if (apic->smp_callin_clear_local_apic)
  211. apic->smp_callin_clear_local_apic();
  212. setup_local_APIC();
  213. end_local_APIC_setup();
  214. map_cpu_to_logical_apicid();
  215. /*
  216. * Need to setup vector mappings before we enable interrupts.
  217. */
  218. setup_vector_irq(smp_processor_id());
  219. /*
  220. * Get our bogomips.
  221. *
  222. * Need to enable IRQs because it can take longer and then
  223. * the NMI watchdog might kill us.
  224. */
  225. local_irq_enable();
  226. calibrate_delay();
  227. local_irq_disable();
  228. pr_debug("Stack at about %p\n", &cpuid);
  229. /*
  230. * Save our processor parameters
  231. */
  232. smp_store_cpu_info(cpuid);
  233. notify_cpu_starting(cpuid);
  234. /*
  235. * Allow the master to continue.
  236. */
  237. cpumask_set_cpu(cpuid, cpu_callin_mask);
  238. }
  239. /*
  240. * Activate a secondary processor.
  241. */
  242. notrace static void __cpuinit start_secondary(void *unused)
  243. {
  244. /*
  245. * Don't put *anything* before cpu_init(), SMP booting is too
  246. * fragile that we want to limit the things done here to the
  247. * most necessary things.
  248. */
  249. vmi_bringup();
  250. cpu_init();
  251. preempt_disable();
  252. smp_callin();
  253. /* otherwise gcc will move up smp_processor_id before the cpu_init */
  254. barrier();
  255. /*
  256. * Check TSC synchronization with the BP:
  257. */
  258. check_tsc_sync_target();
  259. if (nmi_watchdog == NMI_IO_APIC) {
  260. legacy_pic->chip->mask(0);
  261. enable_NMI_through_LVT0();
  262. legacy_pic->chip->unmask(0);
  263. }
  264. #ifdef CONFIG_X86_32
  265. while (low_mappings)
  266. cpu_relax();
  267. __flush_tlb_all();
  268. #endif
  269. /* This must be done before setting cpu_online_mask */
  270. set_cpu_sibling_map(raw_smp_processor_id());
  271. wmb();
  272. /*
  273. * We need to hold call_lock, so there is no inconsistency
  274. * between the time smp_call_function() determines number of
  275. * IPI recipients, and the time when the determination is made
  276. * for which cpus receive the IPI. Holding this
  277. * lock helps us to not include this cpu in a currently in progress
  278. * smp_call_function().
  279. *
  280. * We need to hold vector_lock so there the set of online cpus
  281. * does not change while we are assigning vectors to cpus. Holding
  282. * this lock ensures we don't half assign or remove an irq from a cpu.
  283. */
  284. ipi_call_lock();
  285. lock_vector_lock();
  286. set_cpu_online(smp_processor_id(), true);
  287. unlock_vector_lock();
  288. ipi_call_unlock();
  289. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  290. x86_platform.nmi_init();
  291. /* enable local interrupts */
  292. local_irq_enable();
  293. /* to prevent fake stack check failure in clock setup */
  294. boot_init_stack_canary();
  295. x86_cpuinit.setup_percpu_clockev();
  296. wmb();
  297. cpu_idle();
  298. }
  299. #ifdef CONFIG_CPUMASK_OFFSTACK
  300. /* In this case, llc_shared_map is a pointer to a cpumask. */
  301. static inline void copy_cpuinfo_x86(struct cpuinfo_x86 *dst,
  302. const struct cpuinfo_x86 *src)
  303. {
  304. struct cpumask *llc = dst->llc_shared_map;
  305. *dst = *src;
  306. dst->llc_shared_map = llc;
  307. }
  308. #else
  309. static inline void copy_cpuinfo_x86(struct cpuinfo_x86 *dst,
  310. const struct cpuinfo_x86 *src)
  311. {
  312. *dst = *src;
  313. }
  314. #endif /* CONFIG_CPUMASK_OFFSTACK */
  315. /*
  316. * The bootstrap kernel entry code has set these up. Save them for
  317. * a given CPU
  318. */
  319. void __cpuinit smp_store_cpu_info(int id)
  320. {
  321. struct cpuinfo_x86 *c = &cpu_data(id);
  322. copy_cpuinfo_x86(c, &boot_cpu_data);
  323. c->cpu_index = id;
  324. if (id != 0)
  325. identify_secondary_cpu(c);
  326. }
  327. void __cpuinit set_cpu_sibling_map(int cpu)
  328. {
  329. int i;
  330. struct cpuinfo_x86 *c = &cpu_data(cpu);
  331. cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
  332. if (smp_num_siblings > 1) {
  333. for_each_cpu(i, cpu_sibling_setup_mask) {
  334. struct cpuinfo_x86 *o = &cpu_data(i);
  335. if (c->phys_proc_id == o->phys_proc_id &&
  336. c->cpu_core_id == o->cpu_core_id) {
  337. cpumask_set_cpu(i, cpu_sibling_mask(cpu));
  338. cpumask_set_cpu(cpu, cpu_sibling_mask(i));
  339. cpumask_set_cpu(i, cpu_core_mask(cpu));
  340. cpumask_set_cpu(cpu, cpu_core_mask(i));
  341. cpumask_set_cpu(i, c->llc_shared_map);
  342. cpumask_set_cpu(cpu, o->llc_shared_map);
  343. }
  344. }
  345. } else {
  346. cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
  347. }
  348. cpumask_set_cpu(cpu, c->llc_shared_map);
  349. if (current_cpu_data.x86_max_cores == 1) {
  350. cpumask_copy(cpu_core_mask(cpu), cpu_sibling_mask(cpu));
  351. c->booted_cores = 1;
  352. return;
  353. }
  354. for_each_cpu(i, cpu_sibling_setup_mask) {
  355. if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
  356. per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
  357. cpumask_set_cpu(i, c->llc_shared_map);
  358. cpumask_set_cpu(cpu, cpu_data(i).llc_shared_map);
  359. }
  360. if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
  361. cpumask_set_cpu(i, cpu_core_mask(cpu));
  362. cpumask_set_cpu(cpu, cpu_core_mask(i));
  363. /*
  364. * Does this new cpu bringup a new core?
  365. */
  366. if (cpumask_weight(cpu_sibling_mask(cpu)) == 1) {
  367. /*
  368. * for each core in package, increment
  369. * the booted_cores for this new cpu
  370. */
  371. if (cpumask_first(cpu_sibling_mask(i)) == i)
  372. c->booted_cores++;
  373. /*
  374. * increment the core count for all
  375. * the other cpus in this package
  376. */
  377. if (i != cpu)
  378. cpu_data(i).booted_cores++;
  379. } else if (i != cpu && !c->booted_cores)
  380. c->booted_cores = cpu_data(i).booted_cores;
  381. }
  382. }
  383. }
  384. /* maps the cpu to the sched domain representing multi-core */
  385. const struct cpumask *cpu_coregroup_mask(int cpu)
  386. {
  387. struct cpuinfo_x86 *c = &cpu_data(cpu);
  388. /*
  389. * For perf, we return last level cache shared map.
  390. * And for power savings, we return cpu_core_map
  391. */
  392. if ((sched_mc_power_savings || sched_smt_power_savings) &&
  393. !(cpu_has(c, X86_FEATURE_AMD_DCM)))
  394. return cpu_core_mask(cpu);
  395. else
  396. return c->llc_shared_map;
  397. }
  398. static void impress_friends(void)
  399. {
  400. int cpu;
  401. unsigned long bogosum = 0;
  402. /*
  403. * Allow the user to impress friends.
  404. */
  405. pr_debug("Before bogomips.\n");
  406. for_each_possible_cpu(cpu)
  407. if (cpumask_test_cpu(cpu, cpu_callout_mask))
  408. bogosum += cpu_data(cpu).loops_per_jiffy;
  409. printk(KERN_INFO
  410. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  411. num_online_cpus(),
  412. bogosum/(500000/HZ),
  413. (bogosum/(5000/HZ))%100);
  414. pr_debug("Before bogocount - setting activated=1.\n");
  415. }
  416. void __inquire_remote_apic(int apicid)
  417. {
  418. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  419. char *names[] = { "ID", "VERSION", "SPIV" };
  420. int timeout;
  421. u32 status;
  422. printk(KERN_INFO "Inquiring remote APIC 0x%x...\n", apicid);
  423. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  424. printk(KERN_INFO "... APIC 0x%x %s: ", apicid, names[i]);
  425. /*
  426. * Wait for idle.
  427. */
  428. status = safe_apic_wait_icr_idle();
  429. if (status)
  430. printk(KERN_CONT
  431. "a previous APIC delivery may have failed\n");
  432. apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
  433. timeout = 0;
  434. do {
  435. udelay(100);
  436. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  437. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  438. switch (status) {
  439. case APIC_ICR_RR_VALID:
  440. status = apic_read(APIC_RRR);
  441. printk(KERN_CONT "%08x\n", status);
  442. break;
  443. default:
  444. printk(KERN_CONT "failed\n");
  445. }
  446. }
  447. }
  448. /*
  449. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  450. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  451. * won't ... remember to clear down the APIC, etc later.
  452. */
  453. int __cpuinit
  454. wakeup_secondary_cpu_via_nmi(int logical_apicid, unsigned long start_eip)
  455. {
  456. unsigned long send_status, accept_status = 0;
  457. int maxlvt;
  458. /* Target chip */
  459. /* Boot on the stack */
  460. /* Kick the second */
  461. apic_icr_write(APIC_DM_NMI | apic->dest_logical, logical_apicid);
  462. pr_debug("Waiting for send to finish...\n");
  463. send_status = safe_apic_wait_icr_idle();
  464. /*
  465. * Give the other CPU some time to accept the IPI.
  466. */
  467. udelay(200);
  468. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  469. maxlvt = lapic_get_maxlvt();
  470. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  471. apic_write(APIC_ESR, 0);
  472. accept_status = (apic_read(APIC_ESR) & 0xEF);
  473. }
  474. pr_debug("NMI sent.\n");
  475. if (send_status)
  476. printk(KERN_ERR "APIC never delivered???\n");
  477. if (accept_status)
  478. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  479. return (send_status | accept_status);
  480. }
  481. static int __cpuinit
  482. wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
  483. {
  484. unsigned long send_status, accept_status = 0;
  485. int maxlvt, num_starts, j;
  486. maxlvt = lapic_get_maxlvt();
  487. /*
  488. * Be paranoid about clearing APIC errors.
  489. */
  490. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  491. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  492. apic_write(APIC_ESR, 0);
  493. apic_read(APIC_ESR);
  494. }
  495. pr_debug("Asserting INIT.\n");
  496. /*
  497. * Turn INIT on target chip
  498. */
  499. /*
  500. * Send IPI
  501. */
  502. apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
  503. phys_apicid);
  504. pr_debug("Waiting for send to finish...\n");
  505. send_status = safe_apic_wait_icr_idle();
  506. mdelay(10);
  507. pr_debug("Deasserting INIT.\n");
  508. /* Target chip */
  509. /* Send IPI */
  510. apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
  511. pr_debug("Waiting for send to finish...\n");
  512. send_status = safe_apic_wait_icr_idle();
  513. mb();
  514. atomic_set(&init_deasserted, 1);
  515. /*
  516. * Should we send STARTUP IPIs ?
  517. *
  518. * Determine this based on the APIC version.
  519. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  520. */
  521. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  522. num_starts = 2;
  523. else
  524. num_starts = 0;
  525. /*
  526. * Paravirt / VMI wants a startup IPI hook here to set up the
  527. * target processor state.
  528. */
  529. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  530. (unsigned long)stack_start.sp);
  531. /*
  532. * Run STARTUP IPI loop.
  533. */
  534. pr_debug("#startup loops: %d.\n", num_starts);
  535. for (j = 1; j <= num_starts; j++) {
  536. pr_debug("Sending STARTUP #%d.\n", j);
  537. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  538. apic_write(APIC_ESR, 0);
  539. apic_read(APIC_ESR);
  540. pr_debug("After apic_write.\n");
  541. /*
  542. * STARTUP IPI
  543. */
  544. /* Target chip */
  545. /* Boot on the stack */
  546. /* Kick the second */
  547. apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
  548. phys_apicid);
  549. /*
  550. * Give the other CPU some time to accept the IPI.
  551. */
  552. udelay(300);
  553. pr_debug("Startup point 1.\n");
  554. pr_debug("Waiting for send to finish...\n");
  555. send_status = safe_apic_wait_icr_idle();
  556. /*
  557. * Give the other CPU some time to accept the IPI.
  558. */
  559. udelay(200);
  560. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  561. apic_write(APIC_ESR, 0);
  562. accept_status = (apic_read(APIC_ESR) & 0xEF);
  563. if (send_status || accept_status)
  564. break;
  565. }
  566. pr_debug("After Startup.\n");
  567. if (send_status)
  568. printk(KERN_ERR "APIC never delivered???\n");
  569. if (accept_status)
  570. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  571. return (send_status | accept_status);
  572. }
  573. struct create_idle {
  574. struct work_struct work;
  575. struct task_struct *idle;
  576. struct completion done;
  577. int cpu;
  578. };
  579. static void __cpuinit do_fork_idle(struct work_struct *work)
  580. {
  581. struct create_idle *c_idle =
  582. container_of(work, struct create_idle, work);
  583. c_idle->idle = fork_idle(c_idle->cpu);
  584. complete(&c_idle->done);
  585. }
  586. /* reduce the number of lines printed when booting a large cpu count system */
  587. static void __cpuinit announce_cpu(int cpu, int apicid)
  588. {
  589. static int current_node = -1;
  590. int node = early_cpu_to_node(cpu);
  591. if (system_state == SYSTEM_BOOTING) {
  592. if (node != current_node) {
  593. if (current_node > (-1))
  594. pr_cont(" Ok.\n");
  595. current_node = node;
  596. pr_info("Booting Node %3d, Processors ", node);
  597. }
  598. pr_cont(" #%d%s", cpu, cpu == (nr_cpu_ids - 1) ? " Ok.\n" : "");
  599. return;
  600. } else
  601. pr_info("Booting Node %d Processor %d APIC 0x%x\n",
  602. node, cpu, apicid);
  603. }
  604. /*
  605. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  606. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  607. * Returns zero if CPU booted OK, else error code from
  608. * ->wakeup_secondary_cpu.
  609. */
  610. static int __cpuinit do_boot_cpu(int apicid, int cpu)
  611. {
  612. unsigned long boot_error = 0;
  613. unsigned long start_ip;
  614. int timeout;
  615. struct create_idle c_idle = {
  616. .cpu = cpu,
  617. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  618. };
  619. INIT_WORK_ON_STACK(&c_idle.work, do_fork_idle);
  620. alternatives_smp_switch(1);
  621. c_idle.idle = get_idle_for_cpu(cpu);
  622. /*
  623. * We can't use kernel_thread since we must avoid to
  624. * reschedule the child.
  625. */
  626. if (c_idle.idle) {
  627. c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
  628. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  629. init_idle(c_idle.idle, cpu);
  630. goto do_rest;
  631. }
  632. if (!keventd_up() || current_is_keventd())
  633. c_idle.work.func(&c_idle.work);
  634. else {
  635. schedule_work(&c_idle.work);
  636. wait_for_completion(&c_idle.done);
  637. }
  638. if (IS_ERR(c_idle.idle)) {
  639. printk("failed fork for CPU %d\n", cpu);
  640. destroy_work_on_stack(&c_idle.work);
  641. return PTR_ERR(c_idle.idle);
  642. }
  643. set_idle_for_cpu(cpu, c_idle.idle);
  644. do_rest:
  645. per_cpu(current_task, cpu) = c_idle.idle;
  646. #ifdef CONFIG_X86_32
  647. /* Stack for startup_32 can be just as for start_secondary onwards */
  648. irq_ctx_init(cpu);
  649. #else
  650. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  651. initial_gs = per_cpu_offset(cpu);
  652. per_cpu(kernel_stack, cpu) =
  653. (unsigned long)task_stack_page(c_idle.idle) -
  654. KERNEL_STACK_OFFSET + THREAD_SIZE;
  655. #endif
  656. early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
  657. initial_code = (unsigned long)start_secondary;
  658. stack_start.sp = (void *) c_idle.idle->thread.sp;
  659. /* start_ip had better be page-aligned! */
  660. start_ip = setup_trampoline();
  661. /* So we see what's up */
  662. announce_cpu(cpu, apicid);
  663. /*
  664. * This grunge runs the startup process for
  665. * the targeted processor.
  666. */
  667. atomic_set(&init_deasserted, 0);
  668. if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
  669. pr_debug("Setting warm reset code and vector.\n");
  670. smpboot_setup_warm_reset_vector(start_ip);
  671. /*
  672. * Be paranoid about clearing APIC errors.
  673. */
  674. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  675. apic_write(APIC_ESR, 0);
  676. apic_read(APIC_ESR);
  677. }
  678. }
  679. /*
  680. * Kick the secondary CPU. Use the method in the APIC driver
  681. * if it's defined - or use an INIT boot APIC message otherwise:
  682. */
  683. if (apic->wakeup_secondary_cpu)
  684. boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
  685. else
  686. boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
  687. if (!boot_error) {
  688. /*
  689. * allow APs to start initializing.
  690. */
  691. pr_debug("Before Callout %d.\n", cpu);
  692. cpumask_set_cpu(cpu, cpu_callout_mask);
  693. pr_debug("After Callout %d.\n", cpu);
  694. /*
  695. * Wait 5s total for a response
  696. */
  697. for (timeout = 0; timeout < 50000; timeout++) {
  698. if (cpumask_test_cpu(cpu, cpu_callin_mask))
  699. break; /* It has booted */
  700. udelay(100);
  701. }
  702. if (cpumask_test_cpu(cpu, cpu_callin_mask))
  703. pr_debug("CPU%d: has booted.\n", cpu);
  704. else {
  705. boot_error = 1;
  706. if (*((volatile unsigned char *)trampoline_base)
  707. == 0xA5)
  708. /* trampoline started but...? */
  709. pr_err("CPU%d: Stuck ??\n", cpu);
  710. else
  711. /* trampoline code not run */
  712. pr_err("CPU%d: Not responding.\n", cpu);
  713. if (apic->inquire_remote_apic)
  714. apic->inquire_remote_apic(apicid);
  715. }
  716. }
  717. if (boot_error) {
  718. /* Try to put things back the way they were before ... */
  719. numa_remove_cpu(cpu); /* was set by numa_add_cpu */
  720. /* was set by do_boot_cpu() */
  721. cpumask_clear_cpu(cpu, cpu_callout_mask);
  722. /* was set by cpu_init() */
  723. cpumask_clear_cpu(cpu, cpu_initialized_mask);
  724. set_cpu_present(cpu, false);
  725. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  726. }
  727. /* mark "stuck" area as not stuck */
  728. *((volatile unsigned long *)trampoline_base) = 0;
  729. if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
  730. /*
  731. * Cleanup possible dangling ends...
  732. */
  733. smpboot_restore_warm_reset_vector();
  734. }
  735. destroy_work_on_stack(&c_idle.work);
  736. return boot_error;
  737. }
  738. int __cpuinit native_cpu_up(unsigned int cpu)
  739. {
  740. int apicid = apic->cpu_present_to_apicid(cpu);
  741. unsigned long flags;
  742. int err;
  743. WARN_ON(irqs_disabled());
  744. pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  745. if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
  746. !physid_isset(apicid, phys_cpu_present_map)) {
  747. printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
  748. return -EINVAL;
  749. }
  750. /*
  751. * Already booted CPU?
  752. */
  753. if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
  754. pr_debug("do_boot_cpu %d Already started\n", cpu);
  755. return -ENOSYS;
  756. }
  757. /*
  758. * Save current MTRR state in case it was changed since early boot
  759. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  760. */
  761. mtrr_save_state();
  762. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  763. #ifdef CONFIG_X86_32
  764. /* init low mem mapping */
  765. clone_pgd_range(swapper_pg_dir, swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  766. min_t(unsigned long, KERNEL_PGD_PTRS, KERNEL_PGD_BOUNDARY));
  767. flush_tlb_all();
  768. low_mappings = 1;
  769. err = do_boot_cpu(apicid, cpu);
  770. zap_low_mappings(false);
  771. low_mappings = 0;
  772. #else
  773. err = do_boot_cpu(apicid, cpu);
  774. #endif
  775. if (err) {
  776. pr_debug("do_boot_cpu failed %d\n", err);
  777. return -EIO;
  778. }
  779. /*
  780. * Check TSC synchronization with the AP (keep irqs disabled
  781. * while doing so):
  782. */
  783. local_irq_save(flags);
  784. check_tsc_sync_source(cpu);
  785. local_irq_restore(flags);
  786. while (!cpu_online(cpu)) {
  787. cpu_relax();
  788. touch_nmi_watchdog();
  789. }
  790. return 0;
  791. }
  792. /*
  793. * Fall back to non SMP mode after errors.
  794. *
  795. * RED-PEN audit/test this more. I bet there is more state messed up here.
  796. */
  797. static __init void disable_smp(void)
  798. {
  799. init_cpu_present(cpumask_of(0));
  800. init_cpu_possible(cpumask_of(0));
  801. smpboot_clear_io_apic_irqs();
  802. if (smp_found_config)
  803. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  804. else
  805. physid_set_mask_of_physid(0, &phys_cpu_present_map);
  806. map_cpu_to_logical_apicid();
  807. cpumask_set_cpu(0, cpu_sibling_mask(0));
  808. cpumask_set_cpu(0, cpu_core_mask(0));
  809. }
  810. /*
  811. * Various sanity checks.
  812. */
  813. static int __init smp_sanity_check(unsigned max_cpus)
  814. {
  815. preempt_disable();
  816. #if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
  817. if (def_to_bigsmp && nr_cpu_ids > 8) {
  818. unsigned int cpu;
  819. unsigned nr;
  820. printk(KERN_WARNING
  821. "More than 8 CPUs detected - skipping them.\n"
  822. "Use CONFIG_X86_BIGSMP.\n");
  823. nr = 0;
  824. for_each_present_cpu(cpu) {
  825. if (nr >= 8)
  826. set_cpu_present(cpu, false);
  827. nr++;
  828. }
  829. nr = 0;
  830. for_each_possible_cpu(cpu) {
  831. if (nr >= 8)
  832. set_cpu_possible(cpu, false);
  833. nr++;
  834. }
  835. nr_cpu_ids = 8;
  836. }
  837. #endif
  838. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  839. printk(KERN_WARNING
  840. "weird, boot CPU (#%d) not listed by the BIOS.\n",
  841. hard_smp_processor_id());
  842. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  843. }
  844. /*
  845. * If we couldn't find an SMP configuration at boot time,
  846. * get out of here now!
  847. */
  848. if (!smp_found_config && !acpi_lapic) {
  849. preempt_enable();
  850. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  851. disable_smp();
  852. if (APIC_init_uniprocessor())
  853. printk(KERN_NOTICE "Local APIC not detected."
  854. " Using dummy APIC emulation.\n");
  855. return -1;
  856. }
  857. /*
  858. * Should not be necessary because the MP table should list the boot
  859. * CPU too, but we do it for the sake of robustness anyway.
  860. */
  861. if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
  862. printk(KERN_NOTICE
  863. "weird, boot CPU (#%d) not listed by the BIOS.\n",
  864. boot_cpu_physical_apicid);
  865. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  866. }
  867. preempt_enable();
  868. /*
  869. * If we couldn't find a local APIC, then get out of here now!
  870. */
  871. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
  872. !cpu_has_apic) {
  873. if (!disable_apic) {
  874. pr_err("BIOS bug, local APIC #%d not detected!...\n",
  875. boot_cpu_physical_apicid);
  876. pr_err("... forcing use of dummy APIC emulation."
  877. "(tell your hw vendor)\n");
  878. }
  879. smpboot_clear_io_apic();
  880. arch_disable_smp_support();
  881. return -1;
  882. }
  883. verify_local_APIC();
  884. /*
  885. * If SMP should be disabled, then really disable it!
  886. */
  887. if (!max_cpus) {
  888. printk(KERN_INFO "SMP mode deactivated.\n");
  889. smpboot_clear_io_apic();
  890. localise_nmi_watchdog();
  891. connect_bsp_APIC();
  892. setup_local_APIC();
  893. end_local_APIC_setup();
  894. return -1;
  895. }
  896. return 0;
  897. }
  898. static void __init smp_cpu_index_default(void)
  899. {
  900. int i;
  901. struct cpuinfo_x86 *c;
  902. for_each_possible_cpu(i) {
  903. c = &cpu_data(i);
  904. /* mark all to hotplug */
  905. c->cpu_index = nr_cpu_ids;
  906. }
  907. }
  908. /*
  909. * Prepare for SMP bootup. The MP table or ACPI has been read
  910. * earlier. Just do some sanity checking here and enable APIC mode.
  911. */
  912. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  913. {
  914. unsigned int i;
  915. preempt_disable();
  916. smp_cpu_index_default();
  917. current_cpu_data = boot_cpu_data;
  918. cpumask_copy(cpu_callin_mask, cpumask_of(0));
  919. mb();
  920. /*
  921. * Setup boot CPU information
  922. */
  923. smp_store_cpu_info(0); /* Final full version of the data */
  924. #ifdef CONFIG_X86_32
  925. boot_cpu_logical_apicid = logical_smp_processor_id();
  926. #endif
  927. current_thread_info()->cpu = 0; /* needed? */
  928. for_each_possible_cpu(i) {
  929. zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
  930. zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
  931. zalloc_cpumask_var(&cpu_data(i).llc_shared_map, GFP_KERNEL);
  932. }
  933. set_cpu_sibling_map(0);
  934. enable_IR_x2apic();
  935. default_setup_apic_routing();
  936. if (smp_sanity_check(max_cpus) < 0) {
  937. printk(KERN_INFO "SMP disabled\n");
  938. disable_smp();
  939. goto out;
  940. }
  941. preempt_disable();
  942. if (read_apic_id() != boot_cpu_physical_apicid) {
  943. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  944. read_apic_id(), boot_cpu_physical_apicid);
  945. /* Or can we switch back to PIC here? */
  946. }
  947. preempt_enable();
  948. connect_bsp_APIC();
  949. /*
  950. * Switch from PIC to APIC mode.
  951. */
  952. setup_local_APIC();
  953. /*
  954. * Enable IO APIC before setting up error vector
  955. */
  956. if (!skip_ioapic_setup && nr_ioapics)
  957. enable_IO_APIC();
  958. end_local_APIC_setup();
  959. map_cpu_to_logical_apicid();
  960. if (apic->setup_portio_remap)
  961. apic->setup_portio_remap();
  962. smpboot_setup_io_apic();
  963. /*
  964. * Set up local APIC timer on boot CPU.
  965. */
  966. printk(KERN_INFO "CPU%d: ", 0);
  967. print_cpu_info(&cpu_data(0));
  968. x86_init.timers.setup_percpu_clockev();
  969. if (is_uv_system())
  970. uv_system_init();
  971. set_mtrr_aps_delayed_init();
  972. out:
  973. preempt_enable();
  974. }
  975. void arch_enable_nonboot_cpus_begin(void)
  976. {
  977. set_mtrr_aps_delayed_init();
  978. }
  979. void arch_enable_nonboot_cpus_end(void)
  980. {
  981. mtrr_aps_init();
  982. }
  983. /*
  984. * Early setup to make printk work.
  985. */
  986. void __init native_smp_prepare_boot_cpu(void)
  987. {
  988. int me = smp_processor_id();
  989. switch_to_new_gdt(me);
  990. /* already set me in cpu_online_mask in boot_cpu_init() */
  991. cpumask_set_cpu(me, cpu_callout_mask);
  992. per_cpu(cpu_state, me) = CPU_ONLINE;
  993. }
  994. void __init native_smp_cpus_done(unsigned int max_cpus)
  995. {
  996. pr_debug("Boot done.\n");
  997. impress_friends();
  998. #ifdef CONFIG_X86_IO_APIC
  999. setup_ioapic_dest();
  1000. #endif
  1001. check_nmi_watchdog();
  1002. mtrr_aps_init();
  1003. }
  1004. static int __initdata setup_possible_cpus = -1;
  1005. static int __init _setup_possible_cpus(char *str)
  1006. {
  1007. get_option(&str, &setup_possible_cpus);
  1008. return 0;
  1009. }
  1010. early_param("possible_cpus", _setup_possible_cpus);
  1011. /*
  1012. * cpu_possible_mask should be static, it cannot change as cpu's
  1013. * are onlined, or offlined. The reason is per-cpu data-structures
  1014. * are allocated by some modules at init time, and dont expect to
  1015. * do this dynamically on cpu arrival/departure.
  1016. * cpu_present_mask on the other hand can change dynamically.
  1017. * In case when cpu_hotplug is not compiled, then we resort to current
  1018. * behaviour, which is cpu_possible == cpu_present.
  1019. * - Ashok Raj
  1020. *
  1021. * Three ways to find out the number of additional hotplug CPUs:
  1022. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  1023. * - The user can overwrite it with possible_cpus=NUM
  1024. * - Otherwise don't reserve additional CPUs.
  1025. * We do this because additional CPUs waste a lot of memory.
  1026. * -AK
  1027. */
  1028. __init void prefill_possible_map(void)
  1029. {
  1030. int i, possible;
  1031. /* no processor from mptable or madt */
  1032. if (!num_processors)
  1033. num_processors = 1;
  1034. i = setup_max_cpus ?: 1;
  1035. if (setup_possible_cpus == -1) {
  1036. possible = num_processors;
  1037. #ifdef CONFIG_HOTPLUG_CPU
  1038. if (setup_max_cpus)
  1039. possible += disabled_cpus;
  1040. #else
  1041. if (possible > i)
  1042. possible = i;
  1043. #endif
  1044. } else
  1045. possible = setup_possible_cpus;
  1046. total_cpus = max_t(int, possible, num_processors + disabled_cpus);
  1047. /* nr_cpu_ids could be reduced via nr_cpus= */
  1048. if (possible > nr_cpu_ids) {
  1049. printk(KERN_WARNING
  1050. "%d Processors exceeds NR_CPUS limit of %d\n",
  1051. possible, nr_cpu_ids);
  1052. possible = nr_cpu_ids;
  1053. }
  1054. #ifdef CONFIG_HOTPLUG_CPU
  1055. if (!setup_max_cpus)
  1056. #endif
  1057. if (possible > i) {
  1058. printk(KERN_WARNING
  1059. "%d Processors exceeds max_cpus limit of %u\n",
  1060. possible, setup_max_cpus);
  1061. possible = i;
  1062. }
  1063. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  1064. possible, max_t(int, possible - num_processors, 0));
  1065. for (i = 0; i < possible; i++)
  1066. set_cpu_possible(i, true);
  1067. for (; i < NR_CPUS; i++)
  1068. set_cpu_possible(i, false);
  1069. nr_cpu_ids = possible;
  1070. }
  1071. #ifdef CONFIG_HOTPLUG_CPU
  1072. static void remove_siblinginfo(int cpu)
  1073. {
  1074. int sibling;
  1075. struct cpuinfo_x86 *c = &cpu_data(cpu);
  1076. for_each_cpu(sibling, cpu_core_mask(cpu)) {
  1077. cpumask_clear_cpu(cpu, cpu_core_mask(sibling));
  1078. /*/
  1079. * last thread sibling in this cpu core going down
  1080. */
  1081. if (cpumask_weight(cpu_sibling_mask(cpu)) == 1)
  1082. cpu_data(sibling).booted_cores--;
  1083. }
  1084. for_each_cpu(sibling, cpu_sibling_mask(cpu))
  1085. cpumask_clear_cpu(cpu, cpu_sibling_mask(sibling));
  1086. cpumask_clear(cpu_sibling_mask(cpu));
  1087. cpumask_clear(cpu_core_mask(cpu));
  1088. c->phys_proc_id = 0;
  1089. c->cpu_core_id = 0;
  1090. cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
  1091. }
  1092. static void __ref remove_cpu_from_maps(int cpu)
  1093. {
  1094. set_cpu_online(cpu, false);
  1095. cpumask_clear_cpu(cpu, cpu_callout_mask);
  1096. cpumask_clear_cpu(cpu, cpu_callin_mask);
  1097. /* was set by cpu_init() */
  1098. cpumask_clear_cpu(cpu, cpu_initialized_mask);
  1099. numa_remove_cpu(cpu);
  1100. }
  1101. void cpu_disable_common(void)
  1102. {
  1103. int cpu = smp_processor_id();
  1104. remove_siblinginfo(cpu);
  1105. /* It's now safe to remove this processor from the online map */
  1106. lock_vector_lock();
  1107. remove_cpu_from_maps(cpu);
  1108. unlock_vector_lock();
  1109. fixup_irqs();
  1110. }
  1111. int native_cpu_disable(void)
  1112. {
  1113. int cpu = smp_processor_id();
  1114. /*
  1115. * Perhaps use cpufreq to drop frequency, but that could go
  1116. * into generic code.
  1117. *
  1118. * We won't take down the boot processor on i386 due to some
  1119. * interrupts only being able to be serviced by the BSP.
  1120. * Especially so if we're not using an IOAPIC -zwane
  1121. */
  1122. if (cpu == 0)
  1123. return -EBUSY;
  1124. if (nmi_watchdog == NMI_LOCAL_APIC)
  1125. stop_apic_nmi_watchdog(NULL);
  1126. clear_local_APIC();
  1127. cpu_disable_common();
  1128. return 0;
  1129. }
  1130. void native_cpu_die(unsigned int cpu)
  1131. {
  1132. /* We don't do anything here: idle task is faking death itself. */
  1133. unsigned int i;
  1134. for (i = 0; i < 10; i++) {
  1135. /* They ack this in play_dead by setting CPU_DEAD */
  1136. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  1137. if (system_state == SYSTEM_RUNNING)
  1138. pr_info("CPU %u is now offline\n", cpu);
  1139. if (1 == num_online_cpus())
  1140. alternatives_smp_switch(0);
  1141. return;
  1142. }
  1143. msleep(100);
  1144. }
  1145. pr_err("CPU %u didn't die...\n", cpu);
  1146. }
  1147. void play_dead_common(void)
  1148. {
  1149. idle_task_exit();
  1150. reset_lazy_tlbstate();
  1151. irq_ctx_exit(raw_smp_processor_id());
  1152. c1e_remove_cpu(raw_smp_processor_id());
  1153. mb();
  1154. /* Ack it */
  1155. __get_cpu_var(cpu_state) = CPU_DEAD;
  1156. /*
  1157. * With physical CPU hotplug, we should halt the cpu
  1158. */
  1159. local_irq_disable();
  1160. }
  1161. void native_play_dead(void)
  1162. {
  1163. play_dead_common();
  1164. tboot_shutdown(TB_SHUTDOWN_WFS);
  1165. wbinvd_halt();
  1166. }
  1167. #else /* ... !CONFIG_HOTPLUG_CPU */
  1168. int native_cpu_disable(void)
  1169. {
  1170. return -ENOSYS;
  1171. }
  1172. void native_cpu_die(unsigned int cpu)
  1173. {
  1174. /* We said "no" in __cpu_disable */
  1175. BUG();
  1176. }
  1177. void native_play_dead(void)
  1178. {
  1179. BUG();
  1180. }
  1181. #endif