smp.c 8.8 KB

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