smpboot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <asm/nmi.h>
  8. #include <asm/irq.h>
  9. #include <asm/smp.h>
  10. #include <asm/cpu.h>
  11. #include <asm/numa.h>
  12. #include <mach_apic.h>
  13. /* Number of siblings per CPU package */
  14. int smp_num_siblings = 1;
  15. EXPORT_SYMBOL(smp_num_siblings);
  16. /* Last level cache ID of each logical CPU */
  17. DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
  18. /* bitmap of online cpus */
  19. cpumask_t cpu_online_map __read_mostly;
  20. EXPORT_SYMBOL(cpu_online_map);
  21. cpumask_t cpu_callin_map;
  22. cpumask_t cpu_callout_map;
  23. cpumask_t cpu_possible_map;
  24. EXPORT_SYMBOL(cpu_possible_map);
  25. /* representing HT siblings of each logical CPU */
  26. DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
  27. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  28. /* representing HT and core siblings of each logical CPU */
  29. DEFINE_PER_CPU(cpumask_t, cpu_core_map);
  30. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  31. /* Per CPU bogomips and other parameters */
  32. DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
  33. EXPORT_PER_CPU_SYMBOL(cpu_info);
  34. /* ready for x86_64, no harm for x86, since it will overwrite after alloc */
  35. unsigned char *trampoline_base = __va(SMP_TRAMPOLINE_BASE);
  36. /* representing cpus for which sibling maps can be computed */
  37. static cpumask_t cpu_sibling_setup_map;
  38. /* Set if we find a B stepping CPU */
  39. int __cpuinitdata smp_b_stepping;
  40. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_32)
  41. /* which logical CPUs are on which nodes */
  42. cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
  43. { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
  44. EXPORT_SYMBOL(node_to_cpumask_map);
  45. /* which node each logical CPU is on */
  46. int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  47. EXPORT_SYMBOL(cpu_to_node_map);
  48. /* set up a mapping between cpu and node. */
  49. static void map_cpu_to_node(int cpu, int node)
  50. {
  51. printk(KERN_INFO "Mapping cpu %d to node %d\n", cpu, node);
  52. cpu_set(cpu, node_to_cpumask_map[node]);
  53. cpu_to_node_map[cpu] = node;
  54. }
  55. /* undo a mapping between cpu and node. */
  56. static void unmap_cpu_to_node(int cpu)
  57. {
  58. int node;
  59. printk(KERN_INFO "Unmapping cpu %d from all nodes\n", cpu);
  60. for (node = 0; node < MAX_NUMNODES; node++)
  61. cpu_clear(cpu, node_to_cpumask_map[node]);
  62. cpu_to_node_map[cpu] = 0;
  63. }
  64. #else /* !(CONFIG_NUMA && CONFIG_X86_32) */
  65. #define map_cpu_to_node(cpu, node) ({})
  66. #define unmap_cpu_to_node(cpu) ({})
  67. #endif
  68. #ifdef CONFIG_X86_32
  69. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly =
  70. { [0 ... NR_CPUS-1] = BAD_APICID };
  71. void map_cpu_to_logical_apicid(void)
  72. {
  73. int cpu = smp_processor_id();
  74. int apicid = logical_smp_processor_id();
  75. int node = apicid_to_node(apicid);
  76. if (!node_online(node))
  77. node = first_online_node;
  78. cpu_2_logical_apicid[cpu] = apicid;
  79. map_cpu_to_node(cpu, node);
  80. }
  81. void unmap_cpu_to_logical_apicid(int cpu)
  82. {
  83. cpu_2_logical_apicid[cpu] = BAD_APICID;
  84. unmap_cpu_to_node(cpu);
  85. }
  86. #else
  87. #define unmap_cpu_to_logical_apicid(cpu) do {} while (0)
  88. #define map_cpu_to_logical_apicid() do {} while (0)
  89. #endif
  90. static void __cpuinit smp_apply_quirks(struct cpuinfo_x86 *c)
  91. {
  92. #ifdef CONFIG_X86_32
  93. /*
  94. * Mask B, Pentium, but not Pentium MMX
  95. */
  96. if (c->x86_vendor == X86_VENDOR_INTEL &&
  97. c->x86 == 5 &&
  98. c->x86_mask >= 1 && c->x86_mask <= 4 &&
  99. c->x86_model <= 3)
  100. /*
  101. * Remember we have B step Pentia with bugs
  102. */
  103. smp_b_stepping = 1;
  104. /*
  105. * Certain Athlons might work (for various values of 'work') in SMP
  106. * but they are not certified as MP capable.
  107. */
  108. if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
  109. if (num_possible_cpus() == 1)
  110. goto valid_k7;
  111. /* Athlon 660/661 is valid. */
  112. if ((c->x86_model == 6) && ((c->x86_mask == 0) ||
  113. (c->x86_mask == 1)))
  114. goto valid_k7;
  115. /* Duron 670 is valid */
  116. if ((c->x86_model == 7) && (c->x86_mask == 0))
  117. goto valid_k7;
  118. /*
  119. * Athlon 662, Duron 671, and Athlon >model 7 have capability
  120. * bit. It's worth noting that the A5 stepping (662) of some
  121. * Athlon XP's have the MP bit set.
  122. * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for
  123. * more.
  124. */
  125. if (((c->x86_model == 6) && (c->x86_mask >= 2)) ||
  126. ((c->x86_model == 7) && (c->x86_mask >= 1)) ||
  127. (c->x86_model > 7))
  128. if (cpu_has_mp)
  129. goto valid_k7;
  130. /* If we get here, not a certified SMP capable AMD system. */
  131. add_taint(TAINT_UNSAFE_SMP);
  132. }
  133. valid_k7:
  134. ;
  135. #endif
  136. }
  137. void smp_checks(void)
  138. {
  139. if (smp_b_stepping)
  140. printk(KERN_WARNING "WARNING: SMP operation may be unreliable"
  141. "with B stepping processors.\n");
  142. /*
  143. * Don't taint if we are running SMP kernel on a single non-MP
  144. * approved Athlon
  145. */
  146. if (tainted & TAINT_UNSAFE_SMP) {
  147. if (num_online_cpus())
  148. printk(KERN_INFO "WARNING: This combination of AMD"
  149. "processors is not suitable for SMP.\n");
  150. else
  151. tainted &= ~TAINT_UNSAFE_SMP;
  152. }
  153. }
  154. /*
  155. * The bootstrap kernel entry code has set these up. Save them for
  156. * a given CPU
  157. */
  158. void __cpuinit smp_store_cpu_info(int id)
  159. {
  160. struct cpuinfo_x86 *c = &cpu_data(id);
  161. *c = boot_cpu_data;
  162. c->cpu_index = id;
  163. if (id != 0)
  164. identify_secondary_cpu(c);
  165. smp_apply_quirks(c);
  166. }
  167. void __cpuinit set_cpu_sibling_map(int cpu)
  168. {
  169. int i;
  170. struct cpuinfo_x86 *c = &cpu_data(cpu);
  171. cpu_set(cpu, cpu_sibling_setup_map);
  172. if (smp_num_siblings > 1) {
  173. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  174. if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
  175. c->cpu_core_id == cpu_data(i).cpu_core_id) {
  176. cpu_set(i, per_cpu(cpu_sibling_map, cpu));
  177. cpu_set(cpu, per_cpu(cpu_sibling_map, i));
  178. cpu_set(i, per_cpu(cpu_core_map, cpu));
  179. cpu_set(cpu, per_cpu(cpu_core_map, i));
  180. cpu_set(i, c->llc_shared_map);
  181. cpu_set(cpu, cpu_data(i).llc_shared_map);
  182. }
  183. }
  184. } else {
  185. cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
  186. }
  187. cpu_set(cpu, c->llc_shared_map);
  188. if (current_cpu_data.x86_max_cores == 1) {
  189. per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
  190. c->booted_cores = 1;
  191. return;
  192. }
  193. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  194. if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
  195. per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
  196. cpu_set(i, c->llc_shared_map);
  197. cpu_set(cpu, cpu_data(i).llc_shared_map);
  198. }
  199. if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
  200. cpu_set(i, per_cpu(cpu_core_map, cpu));
  201. cpu_set(cpu, per_cpu(cpu_core_map, i));
  202. /*
  203. * Does this new cpu bringup a new core?
  204. */
  205. if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
  206. /*
  207. * for each core in package, increment
  208. * the booted_cores for this new cpu
  209. */
  210. if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
  211. c->booted_cores++;
  212. /*
  213. * increment the core count for all
  214. * the other cpus in this package
  215. */
  216. if (i != cpu)
  217. cpu_data(i).booted_cores++;
  218. } else if (i != cpu && !c->booted_cores)
  219. c->booted_cores = cpu_data(i).booted_cores;
  220. }
  221. }
  222. }
  223. /* maps the cpu to the sched domain representing multi-core */
  224. cpumask_t cpu_coregroup_map(int cpu)
  225. {
  226. struct cpuinfo_x86 *c = &cpu_data(cpu);
  227. /*
  228. * For perf, we return last level cache shared map.
  229. * And for power savings, we return cpu_core_map
  230. */
  231. if (sched_mc_power_savings || sched_smt_power_savings)
  232. return per_cpu(cpu_core_map, cpu);
  233. else
  234. return c->llc_shared_map;
  235. }
  236. /*
  237. * Currently trivial. Write the real->protected mode
  238. * bootstrap into the page concerned. The caller
  239. * has made sure it's suitably aligned.
  240. */
  241. unsigned long __cpuinit setup_trampoline(void)
  242. {
  243. memcpy(trampoline_base, trampoline_data,
  244. trampoline_end - trampoline_data);
  245. return virt_to_phys(trampoline_base);
  246. }
  247. #ifdef CONFIG_X86_32
  248. /*
  249. * We are called very early to get the low memory for the
  250. * SMP bootup trampoline page.
  251. */
  252. void __init smp_alloc_memory(void)
  253. {
  254. trampoline_base = alloc_bootmem_low_pages(PAGE_SIZE);
  255. /*
  256. * Has to be in very low memory so we can execute
  257. * real-mode AP code.
  258. */
  259. if (__pa(trampoline_base) >= 0x9F000)
  260. BUG();
  261. }
  262. #endif
  263. void impress_friends(void)
  264. {
  265. int cpu;
  266. unsigned long bogosum = 0;
  267. /*
  268. * Allow the user to impress friends.
  269. */
  270. Dprintk("Before bogomips.\n");
  271. for_each_possible_cpu(cpu)
  272. if (cpu_isset(cpu, cpu_callout_map))
  273. bogosum += cpu_data(cpu).loops_per_jiffy;
  274. printk(KERN_INFO
  275. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  276. num_online_cpus(),
  277. bogosum/(500000/HZ),
  278. (bogosum/(5000/HZ))%100);
  279. Dprintk("Before bogocount - setting activated=1.\n");
  280. }
  281. #ifdef CONFIG_HOTPLUG_CPU
  282. void remove_siblinginfo(int cpu)
  283. {
  284. int sibling;
  285. struct cpuinfo_x86 *c = &cpu_data(cpu);
  286. for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
  287. cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
  288. /*/
  289. * last thread sibling in this cpu core going down
  290. */
  291. if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
  292. cpu_data(sibling).booted_cores--;
  293. }
  294. for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
  295. cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
  296. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  297. cpus_clear(per_cpu(cpu_core_map, cpu));
  298. c->phys_proc_id = 0;
  299. c->cpu_core_id = 0;
  300. cpu_clear(cpu, cpu_sibling_setup_map);
  301. }
  302. int additional_cpus __initdata = -1;
  303. static __init int setup_additional_cpus(char *s)
  304. {
  305. return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
  306. }
  307. early_param("additional_cpus", setup_additional_cpus);
  308. /*
  309. * cpu_possible_map should be static, it cannot change as cpu's
  310. * are onlined, or offlined. The reason is per-cpu data-structures
  311. * are allocated by some modules at init time, and dont expect to
  312. * do this dynamically on cpu arrival/departure.
  313. * cpu_present_map on the other hand can change dynamically.
  314. * In case when cpu_hotplug is not compiled, then we resort to current
  315. * behaviour, which is cpu_possible == cpu_present.
  316. * - Ashok Raj
  317. *
  318. * Three ways to find out the number of additional hotplug CPUs:
  319. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  320. * - The user can overwrite it with additional_cpus=NUM
  321. * - Otherwise don't reserve additional CPUs.
  322. * We do this because additional CPUs waste a lot of memory.
  323. * -AK
  324. */
  325. __init void prefill_possible_map(void)
  326. {
  327. int i;
  328. int possible;
  329. if (additional_cpus == -1) {
  330. if (disabled_cpus > 0)
  331. additional_cpus = disabled_cpus;
  332. else
  333. additional_cpus = 0;
  334. }
  335. possible = num_processors + additional_cpus;
  336. if (possible > NR_CPUS)
  337. possible = NR_CPUS;
  338. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  339. possible, max_t(int, possible - num_processors, 0));
  340. for (i = 0; i < possible; i++)
  341. cpu_set(i, cpu_possible_map);
  342. }
  343. static void __ref remove_cpu_from_maps(int cpu)
  344. {
  345. cpu_clear(cpu, cpu_online_map);
  346. #ifdef CONFIG_X86_64
  347. cpu_clear(cpu, cpu_callout_map);
  348. cpu_clear(cpu, cpu_callin_map);
  349. /* was set by cpu_init() */
  350. clear_bit(cpu, (unsigned long *)&cpu_initialized);
  351. clear_node_cpumask(cpu);
  352. #endif
  353. }
  354. int __cpu_disable(void)
  355. {
  356. int cpu = smp_processor_id();
  357. /*
  358. * Perhaps use cpufreq to drop frequency, but that could go
  359. * into generic code.
  360. *
  361. * We won't take down the boot processor on i386 due to some
  362. * interrupts only being able to be serviced by the BSP.
  363. * Especially so if we're not using an IOAPIC -zwane
  364. */
  365. if (cpu == 0)
  366. return -EBUSY;
  367. if (nmi_watchdog == NMI_LOCAL_APIC)
  368. stop_apic_nmi_watchdog(NULL);
  369. clear_local_APIC();
  370. /*
  371. * HACK:
  372. * Allow any queued timer interrupts to get serviced
  373. * This is only a temporary solution until we cleanup
  374. * fixup_irqs as we do for IA64.
  375. */
  376. local_irq_enable();
  377. mdelay(1);
  378. local_irq_disable();
  379. remove_siblinginfo(cpu);
  380. /* It's now safe to remove this processor from the online map */
  381. remove_cpu_from_maps(cpu);
  382. fixup_irqs(cpu_online_map);
  383. return 0;
  384. }
  385. void __cpu_die(unsigned int cpu)
  386. {
  387. /* We don't do anything here: idle task is faking death itself. */
  388. unsigned int i;
  389. for (i = 0; i < 10; i++) {
  390. /* They ack this in play_dead by setting CPU_DEAD */
  391. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  392. printk(KERN_INFO "CPU %d is now offline\n", cpu);
  393. if (1 == num_online_cpus())
  394. alternatives_smp_switch(0);
  395. return;
  396. }
  397. msleep(100);
  398. }
  399. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  400. }
  401. #else /* ... !CONFIG_HOTPLUG_CPU */
  402. int __cpu_disable(void)
  403. {
  404. return -ENOSYS;
  405. }
  406. void __cpu_die(unsigned int cpu)
  407. {
  408. /* We said "no" in __cpu_disable */
  409. BUG();
  410. }
  411. #endif
  412. /*
  413. * If the BIOS enumerates physical processors before logical,
  414. * maxcpus=N at enumeration-time can be used to disable HT.
  415. */
  416. static int __init parse_maxcpus(char *arg)
  417. {
  418. extern unsigned int maxcpus;
  419. maxcpus = simple_strtoul(arg, NULL, 0);
  420. return 0;
  421. }
  422. early_param("maxcpus", parse_maxcpus);