smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. * This code is released under the GNU General Public License version 2 or
  9. * later.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/irq.h>
  14. #include <linux/delay.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mc146818rtc.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/mtrr.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mach_apic.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/proto.h>
  27. #include <asm/apicdef.h>
  28. /*
  29. * Smarter SMP flushing macros.
  30. * c/o Linus Torvalds.
  31. *
  32. * These mean you can really definitely utterly forget about
  33. * writing to user space from interrupts. (Its not allowed anyway).
  34. *
  35. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  36. */
  37. static cpumask_t flush_cpumask;
  38. static struct mm_struct * flush_mm;
  39. static unsigned long flush_va;
  40. static DEFINE_SPINLOCK(tlbstate_lock);
  41. #define FLUSH_ALL -1ULL
  42. /*
  43. * We cannot call mmdrop() because we are in interrupt context,
  44. * instead update mm->cpu_vm_mask.
  45. */
  46. static inline void leave_mm (unsigned long cpu)
  47. {
  48. if (read_pda(mmu_state) == TLBSTATE_OK)
  49. BUG();
  50. clear_bit(cpu, &read_pda(active_mm)->cpu_vm_mask);
  51. load_cr3(swapper_pg_dir);
  52. }
  53. /*
  54. *
  55. * The flush IPI assumes that a thread switch happens in this order:
  56. * [cpu0: the cpu that switches]
  57. * 1) switch_mm() either 1a) or 1b)
  58. * 1a) thread switch to a different mm
  59. * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
  60. * Stop ipi delivery for the old mm. This is not synchronized with
  61. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  62. * for the wrong mm, and in the worst case we perform a superfluous
  63. * tlb flush.
  64. * 1a2) set cpu mmu_state to TLBSTATE_OK
  65. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  66. * was in lazy tlb mode.
  67. * 1a3) update cpu active_mm
  68. * Now cpu0 accepts tlb flushes for the new mm.
  69. * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
  70. * Now the other cpus will send tlb flush ipis.
  71. * 1a4) change cr3.
  72. * 1b) thread switch without mm change
  73. * cpu active_mm is correct, cpu0 already handles
  74. * flush ipis.
  75. * 1b1) set cpu mmu_state to TLBSTATE_OK
  76. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  77. * Atomically set the bit [other cpus will start sending flush ipis],
  78. * and test the bit.
  79. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  80. * 2) switch %%esp, ie current
  81. *
  82. * The interrupt must handle 2 special cases:
  83. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  84. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  85. * runs in kernel space, the cpu could load tlb entries for user space
  86. * pages.
  87. *
  88. * The good news is that cpu mmu_state is local to each cpu, no
  89. * write/read ordering problems.
  90. */
  91. /*
  92. * TLB flush IPI:
  93. *
  94. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  95. * 2) Leave the mm if we are in the lazy tlb mode.
  96. */
  97. asmlinkage void smp_invalidate_interrupt (void)
  98. {
  99. unsigned long cpu;
  100. cpu = get_cpu();
  101. if (!cpu_isset(cpu, flush_cpumask))
  102. goto out;
  103. /*
  104. * This was a BUG() but until someone can quote me the
  105. * line from the intel manual that guarantees an IPI to
  106. * multiple CPUs is retried _only_ on the erroring CPUs
  107. * its staying as a return
  108. *
  109. * BUG();
  110. */
  111. if (flush_mm == read_pda(active_mm)) {
  112. if (read_pda(mmu_state) == TLBSTATE_OK) {
  113. if (flush_va == FLUSH_ALL)
  114. local_flush_tlb();
  115. else
  116. __flush_tlb_one(flush_va);
  117. } else
  118. leave_mm(cpu);
  119. }
  120. out:
  121. ack_APIC_irq();
  122. cpu_clear(cpu, flush_cpumask);
  123. put_cpu_no_resched();
  124. }
  125. static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  126. unsigned long va)
  127. {
  128. cpumask_t tmp;
  129. /*
  130. * A couple of (to be removed) sanity checks:
  131. *
  132. * - we do not send IPIs to not-yet booted CPUs.
  133. * - current CPU must not be in mask
  134. * - mask must exist :)
  135. */
  136. BUG_ON(cpus_empty(cpumask));
  137. cpus_and(tmp, cpumask, cpu_online_map);
  138. BUG_ON(!cpus_equal(tmp, cpumask));
  139. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  140. if (!mm)
  141. BUG();
  142. /*
  143. * I'm not happy about this global shared spinlock in the
  144. * MM hot path, but we'll see how contended it is.
  145. * Temporarily this turns IRQs off, so that lockups are
  146. * detected by the NMI watchdog.
  147. */
  148. spin_lock(&tlbstate_lock);
  149. flush_mm = mm;
  150. flush_va = va;
  151. cpus_or(flush_cpumask, cpumask, flush_cpumask);
  152. /*
  153. * We have to send the IPI only to
  154. * CPUs affected.
  155. */
  156. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR);
  157. while (!cpus_empty(flush_cpumask))
  158. mb(); /* nothing. lockup detection does not belong here */;
  159. flush_mm = NULL;
  160. flush_va = 0;
  161. spin_unlock(&tlbstate_lock);
  162. }
  163. void flush_tlb_current_task(void)
  164. {
  165. struct mm_struct *mm = current->mm;
  166. cpumask_t cpu_mask;
  167. preempt_disable();
  168. cpu_mask = mm->cpu_vm_mask;
  169. cpu_clear(smp_processor_id(), cpu_mask);
  170. local_flush_tlb();
  171. if (!cpus_empty(cpu_mask))
  172. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  173. preempt_enable();
  174. }
  175. void flush_tlb_mm (struct mm_struct * mm)
  176. {
  177. cpumask_t cpu_mask;
  178. preempt_disable();
  179. cpu_mask = mm->cpu_vm_mask;
  180. cpu_clear(smp_processor_id(), cpu_mask);
  181. if (current->active_mm == mm) {
  182. if (current->mm)
  183. local_flush_tlb();
  184. else
  185. leave_mm(smp_processor_id());
  186. }
  187. if (!cpus_empty(cpu_mask))
  188. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  189. preempt_enable();
  190. }
  191. void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
  192. {
  193. struct mm_struct *mm = vma->vm_mm;
  194. cpumask_t cpu_mask;
  195. preempt_disable();
  196. cpu_mask = mm->cpu_vm_mask;
  197. cpu_clear(smp_processor_id(), cpu_mask);
  198. if (current->active_mm == mm) {
  199. if(current->mm)
  200. __flush_tlb_one(va);
  201. else
  202. leave_mm(smp_processor_id());
  203. }
  204. if (!cpus_empty(cpu_mask))
  205. flush_tlb_others(cpu_mask, mm, va);
  206. preempt_enable();
  207. }
  208. static void do_flush_tlb_all(void* info)
  209. {
  210. unsigned long cpu = smp_processor_id();
  211. __flush_tlb_all();
  212. if (read_pda(mmu_state) == TLBSTATE_LAZY)
  213. leave_mm(cpu);
  214. }
  215. void flush_tlb_all(void)
  216. {
  217. on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
  218. }
  219. void smp_kdb_stop(void)
  220. {
  221. send_IPI_allbutself(KDB_VECTOR);
  222. }
  223. /*
  224. * this function sends a 'reschedule' IPI to another CPU.
  225. * it goes straight through and wastes no time serializing
  226. * anything. Worst case is that we lose a reschedule ...
  227. */
  228. void smp_send_reschedule(int cpu)
  229. {
  230. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  231. }
  232. /*
  233. * Structure and data for smp_call_function(). This is designed to minimise
  234. * static memory requirements. It also looks cleaner.
  235. */
  236. static DEFINE_SPINLOCK(call_lock);
  237. struct call_data_struct {
  238. void (*func) (void *info);
  239. void *info;
  240. atomic_t started;
  241. atomic_t finished;
  242. int wait;
  243. };
  244. static struct call_data_struct * call_data;
  245. void lock_ipi_call_lock(void)
  246. {
  247. spin_lock_irq(&call_lock);
  248. }
  249. void unlock_ipi_call_lock(void)
  250. {
  251. spin_unlock_irq(&call_lock);
  252. }
  253. /*
  254. * this function sends a 'generic call function' IPI to one other CPU
  255. * in the system.
  256. */
  257. static void __smp_call_function_single (int cpu, void (*func) (void *info), void *info,
  258. int nonatomic, int wait)
  259. {
  260. struct call_data_struct data;
  261. int cpus = 1;
  262. data.func = func;
  263. data.info = info;
  264. atomic_set(&data.started, 0);
  265. data.wait = wait;
  266. if (wait)
  267. atomic_set(&data.finished, 0);
  268. call_data = &data;
  269. wmb();
  270. /* Send a message to all other CPUs and wait for them to respond */
  271. send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
  272. /* Wait for response */
  273. while (atomic_read(&data.started) != cpus)
  274. cpu_relax();
  275. if (!wait)
  276. return;
  277. while (atomic_read(&data.finished) != cpus)
  278. cpu_relax();
  279. }
  280. /*
  281. * smp_call_function_single - Run a function on another CPU
  282. * @func: The function to run. This must be fast and non-blocking.
  283. * @info: An arbitrary pointer to pass to the function.
  284. * @nonatomic: Currently unused.
  285. * @wait: If true, wait until function has completed on other CPUs.
  286. *
  287. * Retrurns 0 on success, else a negative status code.
  288. *
  289. * Does not return until the remote CPU is nearly ready to execute <func>
  290. * or is or has executed.
  291. */
  292. int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
  293. int nonatomic, int wait)
  294. {
  295. /* prevent preemption and reschedule on another processor */
  296. int me = get_cpu();
  297. if (cpu == me) {
  298. WARN_ON(1);
  299. put_cpu();
  300. return -EBUSY;
  301. }
  302. spin_lock_bh(&call_lock);
  303. __smp_call_function_single(cpu, func, info, nonatomic, wait);
  304. spin_unlock_bh(&call_lock);
  305. put_cpu();
  306. return 0;
  307. }
  308. /*
  309. * this function sends a 'generic call function' IPI to all other CPUs
  310. * in the system.
  311. */
  312. static void __smp_call_function (void (*func) (void *info), void *info,
  313. int nonatomic, int wait)
  314. {
  315. struct call_data_struct data;
  316. int cpus = num_online_cpus()-1;
  317. if (!cpus)
  318. return;
  319. data.func = func;
  320. data.info = info;
  321. atomic_set(&data.started, 0);
  322. data.wait = wait;
  323. if (wait)
  324. atomic_set(&data.finished, 0);
  325. call_data = &data;
  326. wmb();
  327. /* Send a message to all other CPUs and wait for them to respond */
  328. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  329. /* Wait for response */
  330. while (atomic_read(&data.started) != cpus)
  331. cpu_relax();
  332. if (!wait)
  333. return;
  334. while (atomic_read(&data.finished) != cpus)
  335. cpu_relax();
  336. }
  337. /*
  338. * smp_call_function - run a function on all other CPUs.
  339. * @func: The function to run. This must be fast and non-blocking.
  340. * @info: An arbitrary pointer to pass to the function.
  341. * @nonatomic: currently unused.
  342. * @wait: If true, wait (atomically) until function has completed on other
  343. * CPUs.
  344. *
  345. * Returns 0 on success, else a negative status code. Does not return until
  346. * remote CPUs are nearly ready to execute func or are or have executed.
  347. *
  348. * You must not call this function with disabled interrupts or from a
  349. * hardware interrupt handler or from a bottom half handler.
  350. * Actually there are a few legal cases, like panic.
  351. */
  352. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  353. int wait)
  354. {
  355. spin_lock(&call_lock);
  356. __smp_call_function(func,info,nonatomic,wait);
  357. spin_unlock(&call_lock);
  358. return 0;
  359. }
  360. void smp_stop_cpu(void)
  361. {
  362. /*
  363. * Remove this CPU:
  364. */
  365. cpu_clear(smp_processor_id(), cpu_online_map);
  366. local_irq_disable();
  367. disable_local_APIC();
  368. local_irq_enable();
  369. }
  370. static void smp_really_stop_cpu(void *dummy)
  371. {
  372. smp_stop_cpu();
  373. for (;;)
  374. asm("hlt");
  375. }
  376. void smp_send_stop(void)
  377. {
  378. int nolock = 0;
  379. if (reboot_force)
  380. return;
  381. /* Don't deadlock on the call lock in panic */
  382. if (!spin_trylock(&call_lock)) {
  383. /* ignore locking because we have paniced anyways */
  384. nolock = 1;
  385. }
  386. __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
  387. if (!nolock)
  388. spin_unlock(&call_lock);
  389. local_irq_disable();
  390. disable_local_APIC();
  391. local_irq_enable();
  392. }
  393. /*
  394. * Reschedule call back. Nothing to do,
  395. * all the work is done automatically when
  396. * we return from the interrupt.
  397. */
  398. asmlinkage void smp_reschedule_interrupt(void)
  399. {
  400. ack_APIC_irq();
  401. }
  402. asmlinkage void smp_call_function_interrupt(void)
  403. {
  404. void (*func) (void *info) = call_data->func;
  405. void *info = call_data->info;
  406. int wait = call_data->wait;
  407. ack_APIC_irq();
  408. /*
  409. * Notify initiating CPU that I've grabbed the data and am
  410. * about to execute the function
  411. */
  412. mb();
  413. atomic_inc(&call_data->started);
  414. /*
  415. * At this point the info structure may be out of scope unless wait==1
  416. */
  417. irq_enter();
  418. (*func)(info);
  419. irq_exit();
  420. if (wait) {
  421. mb();
  422. atomic_inc(&call_data->finished);
  423. }
  424. }
  425. int safe_smp_processor_id(void)
  426. {
  427. int apicid, i;
  428. if (disable_apic)
  429. return 0;
  430. apicid = hard_smp_processor_id();
  431. if (x86_cpu_to_apicid[apicid] == apicid)
  432. return apicid;
  433. for (i = 0; i < NR_CPUS; ++i) {
  434. if (x86_cpu_to_apicid[i] == apicid)
  435. return i;
  436. }
  437. /* No entries in x86_cpu_to_apicid? Either no MPS|ACPI,
  438. * or called too early. Either way, we must be CPU 0. */
  439. if (x86_cpu_to_apicid[0] == BAD_APICID)
  440. return 0;
  441. return 0; /* Should not happen */
  442. }