smp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <linux/init.h>
  2. #include <linux/mm.h>
  3. #include <linux/delay.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/kernel_stat.h>
  6. #include <linux/mc146818rtc.h>
  7. #include <linux/cache.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/cpu.h>
  10. #include <asm/mtrr.h>
  11. #include <asm/tlbflush.h>
  12. #include <asm/mmu_context.h>
  13. #include <asm/proto.h>
  14. #ifdef CONFIG_X86_32
  15. #include <mach_apic.h>
  16. #include <mach_ipi.h>
  17. #else
  18. #include <asm/mach_apic.h>
  19. #endif
  20. /*
  21. * this function sends a 'reschedule' IPI to another CPU.
  22. * it goes straight through and wastes no time serializing
  23. * anything. Worst case is that we lose a reschedule ...
  24. */
  25. static void native_smp_send_reschedule(int cpu)
  26. {
  27. WARN_ON(cpu_is_offline(cpu));
  28. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  29. }
  30. /*
  31. * Structure and data for smp_call_function(). This is designed to minimise
  32. * static memory requirements. It also looks cleaner.
  33. */
  34. static DEFINE_SPINLOCK(call_lock);
  35. struct call_data_struct {
  36. void (*func) (void *info);
  37. void *info;
  38. atomic_t started;
  39. atomic_t finished;
  40. int wait;
  41. };
  42. void lock_ipi_call_lock(void)
  43. {
  44. spin_lock_irq(&call_lock);
  45. }
  46. void unlock_ipi_call_lock(void)
  47. {
  48. spin_unlock_irq(&call_lock);
  49. }
  50. static struct call_data_struct *call_data;
  51. static void __smp_call_function(void (*func) (void *info), void *info,
  52. int nonatomic, int wait)
  53. {
  54. struct call_data_struct data;
  55. int cpus = num_online_cpus() - 1;
  56. if (!cpus)
  57. return;
  58. data.func = func;
  59. data.info = info;
  60. atomic_set(&data.started, 0);
  61. data.wait = wait;
  62. if (wait)
  63. atomic_set(&data.finished, 0);
  64. call_data = &data;
  65. mb();
  66. /* Send a message to all other CPUs and wait for them to respond */
  67. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  68. /* Wait for response */
  69. while (atomic_read(&data.started) != cpus)
  70. cpu_relax();
  71. if (wait)
  72. while (atomic_read(&data.finished) != cpus)
  73. cpu_relax();
  74. }
  75. /**
  76. * smp_call_function_mask(): Run a function on a set of other CPUs.
  77. * @mask: The set of cpus to run on. Must not include the current cpu.
  78. * @func: The function to run. This must be fast and non-blocking.
  79. * @info: An arbitrary pointer to pass to the function.
  80. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  81. *
  82. * Returns 0 on success, else a negative status code.
  83. *
  84. * If @wait is true, then returns once @func has returned; otherwise
  85. * it returns just before the target cpu calls @func.
  86. *
  87. * You must not call this function with disabled interrupts or from a
  88. * hardware interrupt handler or from a bottom half handler.
  89. */
  90. static int
  91. native_smp_call_function_mask(cpumask_t mask,
  92. void (*func)(void *), void *info,
  93. int wait)
  94. {
  95. struct call_data_struct data;
  96. cpumask_t allbutself;
  97. int cpus;
  98. /* Can deadlock when called with interrupts disabled */
  99. WARN_ON(irqs_disabled());
  100. /* Holding any lock stops cpus from going down. */
  101. spin_lock(&call_lock);
  102. allbutself = cpu_online_map;
  103. cpu_clear(smp_processor_id(), allbutself);
  104. cpus_and(mask, mask, allbutself);
  105. cpus = cpus_weight(mask);
  106. if (!cpus) {
  107. spin_unlock(&call_lock);
  108. return 0;
  109. }
  110. data.func = func;
  111. data.info = info;
  112. atomic_set(&data.started, 0);
  113. data.wait = wait;
  114. if (wait)
  115. atomic_set(&data.finished, 0);
  116. call_data = &data;
  117. wmb();
  118. /* Send a message to other CPUs */
  119. if (cpus_equal(mask, allbutself))
  120. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  121. else
  122. send_IPI_mask(mask, CALL_FUNCTION_VECTOR);
  123. /* Wait for response */
  124. while (atomic_read(&data.started) != cpus)
  125. cpu_relax();
  126. if (wait)
  127. while (atomic_read(&data.finished) != cpus)
  128. cpu_relax();
  129. spin_unlock(&call_lock);
  130. return 0;
  131. }
  132. static void stop_this_cpu(void *dummy)
  133. {
  134. local_irq_disable();
  135. /*
  136. * Remove this CPU:
  137. */
  138. cpu_clear(smp_processor_id(), cpu_online_map);
  139. disable_local_APIC();
  140. if (hlt_works(smp_processor_id()))
  141. for (;;) halt();
  142. for (;;);
  143. }
  144. /*
  145. * this function calls the 'stop' function on all other CPUs in the system.
  146. */
  147. static void native_smp_send_stop(void)
  148. {
  149. int nolock;
  150. unsigned long flags;
  151. if (reboot_force)
  152. return;
  153. /* Don't deadlock on the call lock in panic */
  154. nolock = !spin_trylock(&call_lock);
  155. local_irq_save(flags);
  156. __smp_call_function(stop_this_cpu, NULL, 0, 0);
  157. if (!nolock)
  158. spin_unlock(&call_lock);
  159. disable_local_APIC();
  160. local_irq_restore(flags);
  161. }
  162. /*
  163. * Reschedule call back. Nothing to do,
  164. * all the work is done automatically when
  165. * we return from the interrupt.
  166. */
  167. void smp_reschedule_interrupt(struct pt_regs *regs)
  168. {
  169. ack_APIC_irq();
  170. #ifdef CONFIG_X86_32
  171. __get_cpu_var(irq_stat).irq_resched_count++;
  172. #else
  173. add_pda(irq_resched_count, 1);
  174. #endif
  175. }
  176. void smp_call_function_interrupt(struct pt_regs *regs)
  177. {
  178. void (*func) (void *info) = call_data->func;
  179. void *info = call_data->info;
  180. int wait = call_data->wait;
  181. ack_APIC_irq();
  182. /*
  183. * Notify initiating CPU that I've grabbed the data and am
  184. * about to execute the function
  185. */
  186. mb();
  187. atomic_inc(&call_data->started);
  188. /*
  189. * At this point the info structure may be out of scope unless wait==1
  190. */
  191. irq_enter();
  192. (*func)(info);
  193. #ifdef CONFIG_X86_32
  194. __get_cpu_var(irq_stat).irq_call_count++;
  195. #else
  196. add_pda(irq_call_count, 1);
  197. #endif
  198. irq_exit();
  199. if (wait) {
  200. mb();
  201. atomic_inc(&call_data->finished);
  202. }
  203. }
  204. struct smp_ops smp_ops = {
  205. .smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
  206. .smp_prepare_cpus = native_smp_prepare_cpus,
  207. .cpu_up = native_cpu_up,
  208. .smp_cpus_done = native_smp_cpus_done,
  209. .smp_send_stop = native_smp_send_stop,
  210. .smp_send_reschedule = native_smp_send_reschedule,
  211. .smp_call_function_mask = native_smp_call_function_mask,
  212. };
  213. EXPORT_SYMBOL_GPL(smp_ops);