smp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Intel SMP support routines.
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  5. * (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com>
  6. * (c) 2002,2003 Andi Kleen, SuSE Labs.
  7. *
  8. * i386 and x86_64 integration by Glauber Costa <gcosta@redhat.com>
  9. *
  10. * This code is released under the GNU General Public License version 2 or
  11. * later.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/delay.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/mc146818rtc.h>
  19. #include <linux/cache.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/cpu.h>
  22. #include <asm/mtrr.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/proto.h>
  26. #ifdef CONFIG_X86_32
  27. #include <mach_apic.h>
  28. #include <mach_ipi.h>
  29. #else
  30. #include <asm/mach_apic.h>
  31. #endif
  32. /*
  33. * Some notes on x86 processor bugs affecting SMP operation:
  34. *
  35. * Pentium, Pentium Pro, II, III (and all CPUs) have bugs.
  36. * The Linux implications for SMP are handled as follows:
  37. *
  38. * Pentium III / [Xeon]
  39. * None of the E1AP-E3AP errata are visible to the user.
  40. *
  41. * E1AP. see PII A1AP
  42. * E2AP. see PII A2AP
  43. * E3AP. see PII A3AP
  44. *
  45. * Pentium II / [Xeon]
  46. * None of the A1AP-A3AP errata are visible to the user.
  47. *
  48. * A1AP. see PPro 1AP
  49. * A2AP. see PPro 2AP
  50. * A3AP. see PPro 7AP
  51. *
  52. * Pentium Pro
  53. * None of 1AP-9AP errata are visible to the normal user,
  54. * except occasional delivery of 'spurious interrupt' as trap #15.
  55. * This is very rare and a non-problem.
  56. *
  57. * 1AP. Linux maps APIC as non-cacheable
  58. * 2AP. worked around in hardware
  59. * 3AP. fixed in C0 and above steppings microcode update.
  60. * Linux does not use excessive STARTUP_IPIs.
  61. * 4AP. worked around in hardware
  62. * 5AP. symmetric IO mode (normal Linux operation) not affected.
  63. * 'noapic' mode has vector 0xf filled out properly.
  64. * 6AP. 'noapic' mode might be affected - fixed in later steppings
  65. * 7AP. We do not assume writes to the LVT deassering IRQs
  66. * 8AP. We do not enable low power mode (deep sleep) during MP bootup
  67. * 9AP. We do not use mixed mode
  68. *
  69. * Pentium
  70. * There is a marginal case where REP MOVS on 100MHz SMP
  71. * machines with B stepping processors can fail. XXX should provide
  72. * an L1cache=Writethrough or L1cache=off option.
  73. *
  74. * B stepping CPUs may hang. There are hardware work arounds
  75. * for this. We warn about it in case your board doesn't have the work
  76. * arounds. Basically that's so I can tell anyone with a B stepping
  77. * CPU and SMP problems "tough".
  78. *
  79. * Specific items [From Pentium Processor Specification Update]
  80. *
  81. * 1AP. Linux doesn't use remote read
  82. * 2AP. Linux doesn't trust APIC errors
  83. * 3AP. We work around this
  84. * 4AP. Linux never generated 3 interrupts of the same priority
  85. * to cause a lost local interrupt.
  86. * 5AP. Remote read is never used
  87. * 6AP. not affected - worked around in hardware
  88. * 7AP. not affected - worked around in hardware
  89. * 8AP. worked around in hardware - we get explicit CS errors if not
  90. * 9AP. only 'noapic' mode affected. Might generate spurious
  91. * interrupts, we log only the first one and count the
  92. * rest silently.
  93. * 10AP. not affected - worked around in hardware
  94. * 11AP. Linux reads the APIC between writes to avoid this, as per
  95. * the documentation. Make sure you preserve this as it affects
  96. * the C stepping chips too.
  97. * 12AP. not affected - worked around in hardware
  98. * 13AP. not affected - worked around in hardware
  99. * 14AP. we always deassert INIT during bootup
  100. * 15AP. not affected - worked around in hardware
  101. * 16AP. not affected - worked around in hardware
  102. * 17AP. not affected - worked around in hardware
  103. * 18AP. not affected - worked around in hardware
  104. * 19AP. not affected - worked around in BIOS
  105. *
  106. * If this sounds worrying believe me these bugs are either ___RARE___,
  107. * or are signal timing bugs worked around in hardware and there's
  108. * about nothing of note with C stepping upwards.
  109. */
  110. /*
  111. * this function sends a 'reschedule' IPI to another CPU.
  112. * it goes straight through and wastes no time serializing
  113. * anything. Worst case is that we lose a reschedule ...
  114. */
  115. static void native_smp_send_reschedule(int cpu)
  116. {
  117. if (unlikely(cpu_is_offline(cpu))) {
  118. WARN_ON(1);
  119. return;
  120. }
  121. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  122. }
  123. /*
  124. * Structure and data for smp_call_function(). This is designed to minimise
  125. * static memory requirements. It also looks cleaner.
  126. */
  127. static DEFINE_SPINLOCK(call_lock);
  128. struct call_data_struct {
  129. void (*func) (void *info);
  130. void *info;
  131. atomic_t started;
  132. atomic_t finished;
  133. int wait;
  134. };
  135. void lock_ipi_call_lock(void)
  136. {
  137. spin_lock_irq(&call_lock);
  138. }
  139. void unlock_ipi_call_lock(void)
  140. {
  141. spin_unlock_irq(&call_lock);
  142. }
  143. static struct call_data_struct *call_data;
  144. static void __smp_call_function(void (*func) (void *info), void *info,
  145. int nonatomic, int wait)
  146. {
  147. struct call_data_struct data;
  148. int cpus = num_online_cpus() - 1;
  149. if (!cpus)
  150. return;
  151. data.func = func;
  152. data.info = info;
  153. atomic_set(&data.started, 0);
  154. data.wait = wait;
  155. if (wait)
  156. atomic_set(&data.finished, 0);
  157. call_data = &data;
  158. mb();
  159. /* Send a message to all other CPUs and wait for them to respond */
  160. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  161. /* Wait for response */
  162. while (atomic_read(&data.started) != cpus)
  163. cpu_relax();
  164. if (wait)
  165. while (atomic_read(&data.finished) != cpus)
  166. cpu_relax();
  167. }
  168. /**
  169. * smp_call_function_mask(): Run a function on a set of other CPUs.
  170. * @mask: The set of cpus to run on. Must not include the current cpu.
  171. * @func: The function to run. This must be fast and non-blocking.
  172. * @info: An arbitrary pointer to pass to the function.
  173. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  174. *
  175. * Returns 0 on success, else a negative status code.
  176. *
  177. * If @wait is true, then returns once @func has returned; otherwise
  178. * it returns just before the target cpu calls @func.
  179. *
  180. * You must not call this function with disabled interrupts or from a
  181. * hardware interrupt handler or from a bottom half handler.
  182. */
  183. static int
  184. native_smp_call_function_mask(cpumask_t mask,
  185. void (*func)(void *), void *info,
  186. int wait)
  187. {
  188. struct call_data_struct data;
  189. cpumask_t allbutself;
  190. int cpus;
  191. /* Can deadlock when called with interrupts disabled */
  192. WARN_ON(irqs_disabled());
  193. /* Holding any lock stops cpus from going down. */
  194. spin_lock(&call_lock);
  195. allbutself = cpu_online_map;
  196. cpu_clear(smp_processor_id(), allbutself);
  197. cpus_and(mask, mask, allbutself);
  198. cpus = cpus_weight(mask);
  199. if (!cpus) {
  200. spin_unlock(&call_lock);
  201. return 0;
  202. }
  203. data.func = func;
  204. data.info = info;
  205. atomic_set(&data.started, 0);
  206. data.wait = wait;
  207. if (wait)
  208. atomic_set(&data.finished, 0);
  209. call_data = &data;
  210. wmb();
  211. /* Send a message to other CPUs */
  212. if (cpus_equal(mask, allbutself))
  213. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  214. else
  215. send_IPI_mask(mask, CALL_FUNCTION_VECTOR);
  216. /* Wait for response */
  217. while (atomic_read(&data.started) != cpus)
  218. cpu_relax();
  219. if (wait)
  220. while (atomic_read(&data.finished) != cpus)
  221. cpu_relax();
  222. spin_unlock(&call_lock);
  223. return 0;
  224. }
  225. static void stop_this_cpu(void *dummy)
  226. {
  227. local_irq_disable();
  228. /*
  229. * Remove this CPU:
  230. */
  231. cpu_clear(smp_processor_id(), cpu_online_map);
  232. disable_local_APIC();
  233. if (hlt_works(smp_processor_id()))
  234. for (;;) halt();
  235. for (;;);
  236. }
  237. /*
  238. * this function calls the 'stop' function on all other CPUs in the system.
  239. */
  240. static void native_smp_send_stop(void)
  241. {
  242. int nolock;
  243. unsigned long flags;
  244. if (reboot_force)
  245. return;
  246. /* Don't deadlock on the call lock in panic */
  247. nolock = !spin_trylock(&call_lock);
  248. local_irq_save(flags);
  249. __smp_call_function(stop_this_cpu, NULL, 0, 0);
  250. if (!nolock)
  251. spin_unlock(&call_lock);
  252. disable_local_APIC();
  253. local_irq_restore(flags);
  254. }
  255. /*
  256. * Reschedule call back. Nothing to do,
  257. * all the work is done automatically when
  258. * we return from the interrupt.
  259. */
  260. void smp_reschedule_interrupt(struct pt_regs *regs)
  261. {
  262. ack_APIC_irq();
  263. #ifdef CONFIG_X86_32
  264. __get_cpu_var(irq_stat).irq_resched_count++;
  265. #else
  266. add_pda(irq_resched_count, 1);
  267. #endif
  268. }
  269. void smp_call_function_interrupt(struct pt_regs *regs)
  270. {
  271. void (*func) (void *info) = call_data->func;
  272. void *info = call_data->info;
  273. int wait = call_data->wait;
  274. ack_APIC_irq();
  275. /*
  276. * Notify initiating CPU that I've grabbed the data and am
  277. * about to execute the function
  278. */
  279. mb();
  280. atomic_inc(&call_data->started);
  281. /*
  282. * At this point the info structure may be out of scope unless wait==1
  283. */
  284. irq_enter();
  285. (*func)(info);
  286. #ifdef CONFIG_X86_32
  287. __get_cpu_var(irq_stat).irq_call_count++;
  288. #else
  289. add_pda(irq_call_count, 1);
  290. #endif
  291. irq_exit();
  292. if (wait) {
  293. mb();
  294. atomic_inc(&call_data->finished);
  295. }
  296. }
  297. struct smp_ops smp_ops = {
  298. .smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
  299. .smp_prepare_cpus = native_smp_prepare_cpus,
  300. .cpu_up = native_cpu_up,
  301. .smp_cpus_done = native_smp_cpus_done,
  302. .smp_send_stop = native_smp_send_stop,
  303. .smp_send_reschedule = native_smp_send_reschedule,
  304. .smp_call_function_mask = native_smp_call_function_mask,
  305. };
  306. EXPORT_SYMBOL_GPL(smp_ops);