smpboot_64.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * x86 SMP booting functions
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  5. * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
  6. * 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
  16. *
  17. * Fixes
  18. * Felix Koop : NR_CPUS used properly
  19. * Jose Renau : Handle single CPU case.
  20. * Alan Cox : By repeated request 8) - Total BogoMIP report.
  21. * Greg Wright : Fix for kernel stacks panic.
  22. * Erich Boleyn : MP v1.4 and additional changes.
  23. * Matthias Sattler : Changes for 2.1 kernel map.
  24. * Michel Lespinasse : Changes for 2.1 kernel map.
  25. * Michael Chastain : Change trampoline.S to gnu as.
  26. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  27. * Ingo Molnar : Added APIC timers, based on code
  28. * from Jose Renau
  29. * Ingo Molnar : various cleanups and rewrites
  30. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  31. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  32. * Andi Kleen : Changed for SMP boot into long mode.
  33. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  34. * Andi Kleen : Converted to new state machine.
  35. * Various cleanups.
  36. * Probably mostly hotplug CPU ready now.
  37. * Ashok Raj : CPU hotplug support
  38. */
  39. #include <linux/init.h>
  40. #include <linux/mm.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/bootmem.h>
  43. #include <linux/thread_info.h>
  44. #include <linux/module.h>
  45. #include <linux/delay.h>
  46. #include <linux/mc146818rtc.h>
  47. #include <linux/smp.h>
  48. #include <linux/kdebug.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/pgalloc.h>
  51. #include <asm/desc.h>
  52. #include <asm/tlbflush.h>
  53. #include <asm/proto.h>
  54. #include <asm/nmi.h>
  55. #include <asm/irq.h>
  56. #include <asm/hw_irq.h>
  57. #include <asm/numa.h>
  58. #include <mach_wakecpu.h>
  59. #include <mach_apic.h>
  60. #include <smpboot_hooks.h>
  61. #include <mach_apic.h>
  62. /* Set when the idlers are all forked */
  63. int smp_threads_ready;
  64. /* State of each CPU */
  65. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  66. extern void smp_callin(void);
  67. /*
  68. * Setup code on secondary processor (after comming out of the trampoline)
  69. */
  70. void __cpuinit start_secondary(void)
  71. {
  72. /*
  73. * Dont put anything before smp_callin(), SMP
  74. * booting is too fragile that we want to limit the
  75. * things done here to the most necessary things.
  76. */
  77. cpu_init();
  78. preempt_disable();
  79. smp_callin();
  80. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  81. barrier();
  82. /*
  83. * Check TSC sync first:
  84. */
  85. check_tsc_sync_target();
  86. if (nmi_watchdog == NMI_IO_APIC) {
  87. disable_8259A_irq(0);
  88. enable_NMI_through_LVT0();
  89. enable_8259A_irq(0);
  90. }
  91. /*
  92. * The sibling maps must be set before turing the online map on for
  93. * this cpu
  94. */
  95. set_cpu_sibling_map(smp_processor_id());
  96. /*
  97. * We need to hold call_lock, so there is no inconsistency
  98. * between the time smp_call_function() determines number of
  99. * IPI recipients, and the time when the determination is made
  100. * for which cpus receive the IPI in genapic_flat.c. Holding this
  101. * lock helps us to not include this cpu in a currently in progress
  102. * smp_call_function().
  103. */
  104. lock_ipi_call_lock();
  105. spin_lock(&vector_lock);
  106. /* Setup the per cpu irq handling data structures */
  107. __setup_vector_irq(smp_processor_id());
  108. /*
  109. * Allow the master to continue.
  110. */
  111. spin_unlock(&vector_lock);
  112. cpu_set(smp_processor_id(), cpu_online_map);
  113. unlock_ipi_call_lock();
  114. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  115. setup_secondary_clock();
  116. wmb();
  117. cpu_idle();
  118. }
  119. cycles_t cacheflush_time;
  120. unsigned long cache_decay_ticks;
  121. /*
  122. * Fall back to non SMP mode after errors.
  123. *
  124. * RED-PEN audit/test this more. I bet there is more state messed up here.
  125. */
  126. static __init void disable_smp(void)
  127. {
  128. cpu_present_map = cpumask_of_cpu(0);
  129. cpu_possible_map = cpumask_of_cpu(0);
  130. if (smp_found_config)
  131. phys_cpu_present_map =
  132. physid_mask_of_physid(boot_cpu_physical_apicid);
  133. else
  134. phys_cpu_present_map = physid_mask_of_physid(0);
  135. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  136. cpu_set(0, per_cpu(cpu_core_map, 0));
  137. }
  138. /*
  139. * Various sanity checks.
  140. */
  141. static int __init smp_sanity_check(unsigned max_cpus)
  142. {
  143. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  144. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  145. hard_smp_processor_id());
  146. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  147. }
  148. /*
  149. * If we couldn't find an SMP configuration at boot time,
  150. * get out of here now!
  151. */
  152. if (!smp_found_config) {
  153. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  154. disable_smp();
  155. if (APIC_init_uniprocessor())
  156. printk(KERN_NOTICE "Local APIC not detected."
  157. " Using dummy APIC emulation.\n");
  158. return -1;
  159. }
  160. /*
  161. * Should not be necessary because the MP table should list the boot
  162. * CPU too, but we do it for the sake of robustness anyway.
  163. */
  164. if (!physid_isset(boot_cpu_physical_apicid, phys_cpu_present_map)) {
  165. printk(KERN_NOTICE
  166. "weird, boot CPU (#%d) not listed by the BIOS.\n",
  167. boot_cpu_physical_apicid);
  168. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  169. }
  170. /*
  171. * If we couldn't find a local APIC, then get out of here now!
  172. */
  173. if (!cpu_has_apic) {
  174. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  175. boot_cpu_physical_apicid);
  176. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  177. nr_ioapics = 0;
  178. return -1;
  179. }
  180. /*
  181. * If SMP should be disabled, then really disable it!
  182. */
  183. if (!max_cpus) {
  184. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  185. nr_ioapics = 0;
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. static void __init smp_cpu_index_default(void)
  191. {
  192. int i;
  193. struct cpuinfo_x86 *c;
  194. for_each_cpu_mask(i, cpu_possible_map) {
  195. c = &cpu_data(i);
  196. /* mark all to hotplug */
  197. c->cpu_index = NR_CPUS;
  198. }
  199. }
  200. /*
  201. * Prepare for SMP bootup. The MP table or ACPI has been read
  202. * earlier. Just do some sanity checking here and enable APIC mode.
  203. */
  204. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  205. {
  206. nmi_watchdog_default();
  207. smp_cpu_index_default();
  208. current_cpu_data = boot_cpu_data;
  209. current_thread_info()->cpu = 0; /* needed? */
  210. set_cpu_sibling_map(0);
  211. if (smp_sanity_check(max_cpus) < 0) {
  212. printk(KERN_INFO "SMP disabled\n");
  213. disable_smp();
  214. return;
  215. }
  216. /*
  217. * Switch from PIC to APIC mode.
  218. */
  219. setup_local_APIC();
  220. /*
  221. * Enable IO APIC before setting up error vector
  222. */
  223. if (!skip_ioapic_setup && nr_ioapics)
  224. enable_IO_APIC();
  225. end_local_APIC_setup();
  226. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_physical_apicid) {
  227. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  228. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_physical_apicid);
  229. /* Or can we switch back to PIC here? */
  230. }
  231. /*
  232. * Now start the IO-APICs
  233. */
  234. if (!skip_ioapic_setup && nr_ioapics)
  235. setup_IO_APIC();
  236. else
  237. nr_ioapics = 0;
  238. /*
  239. * Set up local APIC timer on boot CPU.
  240. */
  241. setup_boot_clock();
  242. printk(KERN_INFO "CPU%d: ", 0);
  243. print_cpu_info(&cpu_data(0));
  244. }
  245. /*
  246. * Early setup to make printk work.
  247. */
  248. void __init native_smp_prepare_boot_cpu(void)
  249. {
  250. int me = smp_processor_id();
  251. /* already set me in cpu_online_map in boot_cpu_init() */
  252. cpu_set(me, cpu_callout_map);
  253. per_cpu(cpu_state, me) = CPU_ONLINE;
  254. }
  255. extern void impress_friends(void);
  256. extern void smp_checks(void);
  257. /*
  258. * Finish the SMP boot.
  259. */
  260. void __init native_smp_cpus_done(unsigned int max_cpus)
  261. {
  262. smpboot_restore_warm_reset_vector();
  263. Dprintk("Boot done.\n");
  264. impress_friends();
  265. smp_checks();
  266. setup_ioapic_dest();
  267. check_nmi_watchdog();
  268. }