smp_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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/delay.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/smp.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/mc146818rtc.h>
  18. #include <linux/interrupt.h>
  19. #include <asm/mtrr.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/mach_apic.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/proto.h>
  25. #include <asm/apicdef.h>
  26. #include <asm/idle.h>
  27. /*
  28. * Smarter SMP flushing macros.
  29. * c/o Linus Torvalds.
  30. *
  31. * These mean you can really definitely utterly forget about
  32. * writing to user space from interrupts. (Its not allowed anyway).
  33. *
  34. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  35. *
  36. * More scalable flush, from Andi Kleen
  37. *
  38. * To avoid global state use 8 different call vectors.
  39. * Each CPU uses a specific vector to trigger flushes on other
  40. * CPUs. Depending on the received vector the target CPUs look into
  41. * the right per cpu variable for the flush data.
  42. *
  43. * With more than 8 CPUs they are hashed to the 8 available
  44. * vectors. The limited global vector space forces us to this right now.
  45. * In future when interrupts are split into per CPU domains this could be
  46. * fixed, at the cost of triggering multiple IPIs in some cases.
  47. */
  48. union smp_flush_state {
  49. struct {
  50. cpumask_t flush_cpumask;
  51. struct mm_struct *flush_mm;
  52. unsigned long flush_va;
  53. #define FLUSH_ALL -1ULL
  54. spinlock_t tlbstate_lock;
  55. };
  56. char pad[SMP_CACHE_BYTES];
  57. } ____cacheline_aligned;
  58. /* State is put into the per CPU data section, but padded
  59. to a full cache line because other CPUs can access it and we don't
  60. want false sharing in the per cpu data segment. */
  61. static DEFINE_PER_CPU(union smp_flush_state, flush_state);
  62. /*
  63. * We cannot call mmdrop() because we are in interrupt context,
  64. * instead update mm->cpu_vm_mask.
  65. */
  66. static inline void leave_mm(int cpu)
  67. {
  68. if (read_pda(mmu_state) == TLBSTATE_OK)
  69. BUG();
  70. cpu_clear(cpu, read_pda(active_mm)->cpu_vm_mask);
  71. load_cr3(swapper_pg_dir);
  72. }
  73. /*
  74. *
  75. * The flush IPI assumes that a thread switch happens in this order:
  76. * [cpu0: the cpu that switches]
  77. * 1) switch_mm() either 1a) or 1b)
  78. * 1a) thread switch to a different mm
  79. * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
  80. * Stop ipi delivery for the old mm. This is not synchronized with
  81. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  82. * for the wrong mm, and in the worst case we perform a superfluous
  83. * tlb flush.
  84. * 1a2) set cpu mmu_state to TLBSTATE_OK
  85. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  86. * was in lazy tlb mode.
  87. * 1a3) update cpu active_mm
  88. * Now cpu0 accepts tlb flushes for the new mm.
  89. * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
  90. * Now the other cpus will send tlb flush ipis.
  91. * 1a4) change cr3.
  92. * 1b) thread switch without mm change
  93. * cpu active_mm is correct, cpu0 already handles
  94. * flush ipis.
  95. * 1b1) set cpu mmu_state to TLBSTATE_OK
  96. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  97. * Atomically set the bit [other cpus will start sending flush ipis],
  98. * and test the bit.
  99. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  100. * 2) switch %%esp, ie current
  101. *
  102. * The interrupt must handle 2 special cases:
  103. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  104. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  105. * runs in kernel space, the cpu could load tlb entries for user space
  106. * pages.
  107. *
  108. * The good news is that cpu mmu_state is local to each cpu, no
  109. * write/read ordering problems.
  110. */
  111. /*
  112. * TLB flush IPI:
  113. *
  114. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  115. * 2) Leave the mm if we are in the lazy tlb mode.
  116. *
  117. * Interrupts are disabled.
  118. */
  119. asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
  120. {
  121. int cpu;
  122. int sender;
  123. union smp_flush_state *f;
  124. cpu = smp_processor_id();
  125. /*
  126. * orig_rax contains the negated interrupt vector.
  127. * Use that to determine where the sender put the data.
  128. */
  129. sender = ~regs->orig_rax - INVALIDATE_TLB_VECTOR_START;
  130. f = &per_cpu(flush_state, sender);
  131. if (!cpu_isset(cpu, f->flush_cpumask))
  132. goto out;
  133. /*
  134. * This was a BUG() but until someone can quote me the
  135. * line from the intel manual that guarantees an IPI to
  136. * multiple CPUs is retried _only_ on the erroring CPUs
  137. * its staying as a return
  138. *
  139. * BUG();
  140. */
  141. if (f->flush_mm == read_pda(active_mm)) {
  142. if (read_pda(mmu_state) == TLBSTATE_OK) {
  143. if (f->flush_va == FLUSH_ALL)
  144. local_flush_tlb();
  145. else
  146. __flush_tlb_one(f->flush_va);
  147. } else
  148. leave_mm(cpu);
  149. }
  150. out:
  151. ack_APIC_irq();
  152. cpu_clear(cpu, f->flush_cpumask);
  153. add_pda(irq_tlb_count, 1);
  154. }
  155. static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  156. unsigned long va)
  157. {
  158. int sender;
  159. union smp_flush_state *f;
  160. /* Caller has disabled preemption */
  161. sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
  162. f = &per_cpu(flush_state, sender);
  163. /* Could avoid this lock when
  164. num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
  165. probably not worth checking this for a cache-hot lock. */
  166. spin_lock(&f->tlbstate_lock);
  167. f->flush_mm = mm;
  168. f->flush_va = va;
  169. cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
  170. /*
  171. * We have to send the IPI only to
  172. * CPUs affected.
  173. */
  174. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
  175. while (!cpus_empty(f->flush_cpumask))
  176. cpu_relax();
  177. f->flush_mm = NULL;
  178. f->flush_va = 0;
  179. spin_unlock(&f->tlbstate_lock);
  180. }
  181. int __cpuinit init_smp_flush(void)
  182. {
  183. int i;
  184. for_each_cpu_mask(i, cpu_possible_map) {
  185. spin_lock_init(&per_cpu(flush_state, i).tlbstate_lock);
  186. }
  187. return 0;
  188. }
  189. core_initcall(init_smp_flush);
  190. void flush_tlb_current_task(void)
  191. {
  192. struct mm_struct *mm = current->mm;
  193. cpumask_t cpu_mask;
  194. preempt_disable();
  195. cpu_mask = mm->cpu_vm_mask;
  196. cpu_clear(smp_processor_id(), cpu_mask);
  197. local_flush_tlb();
  198. if (!cpus_empty(cpu_mask))
  199. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  200. preempt_enable();
  201. }
  202. EXPORT_SYMBOL(flush_tlb_current_task);
  203. void flush_tlb_mm (struct mm_struct * mm)
  204. {
  205. cpumask_t cpu_mask;
  206. preempt_disable();
  207. cpu_mask = mm->cpu_vm_mask;
  208. cpu_clear(smp_processor_id(), cpu_mask);
  209. if (current->active_mm == mm) {
  210. if (current->mm)
  211. local_flush_tlb();
  212. else
  213. leave_mm(smp_processor_id());
  214. }
  215. if (!cpus_empty(cpu_mask))
  216. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  217. preempt_enable();
  218. }
  219. EXPORT_SYMBOL(flush_tlb_mm);
  220. void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
  221. {
  222. struct mm_struct *mm = vma->vm_mm;
  223. cpumask_t cpu_mask;
  224. preempt_disable();
  225. cpu_mask = mm->cpu_vm_mask;
  226. cpu_clear(smp_processor_id(), cpu_mask);
  227. if (current->active_mm == mm) {
  228. if(current->mm)
  229. __flush_tlb_one(va);
  230. else
  231. leave_mm(smp_processor_id());
  232. }
  233. if (!cpus_empty(cpu_mask))
  234. flush_tlb_others(cpu_mask, mm, va);
  235. preempt_enable();
  236. }
  237. EXPORT_SYMBOL(flush_tlb_page);
  238. static void do_flush_tlb_all(void* info)
  239. {
  240. unsigned long cpu = smp_processor_id();
  241. __flush_tlb_all();
  242. if (read_pda(mmu_state) == TLBSTATE_LAZY)
  243. leave_mm(cpu);
  244. }
  245. void flush_tlb_all(void)
  246. {
  247. on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
  248. }
  249. /*
  250. * this function sends a 'reschedule' IPI to another CPU.
  251. * it goes straight through and wastes no time serializing
  252. * anything. Worst case is that we lose a reschedule ...
  253. */
  254. void smp_send_reschedule(int cpu)
  255. {
  256. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  257. }
  258. /*
  259. * Structure and data for smp_call_function(). This is designed to minimise
  260. * static memory requirements. It also looks cleaner.
  261. */
  262. static DEFINE_SPINLOCK(call_lock);
  263. struct call_data_struct {
  264. void (*func) (void *info);
  265. void *info;
  266. atomic_t started;
  267. atomic_t finished;
  268. int wait;
  269. };
  270. static struct call_data_struct * call_data;
  271. void lock_ipi_call_lock(void)
  272. {
  273. spin_lock_irq(&call_lock);
  274. }
  275. void unlock_ipi_call_lock(void)
  276. {
  277. spin_unlock_irq(&call_lock);
  278. }
  279. /*
  280. * this function sends a 'generic call function' IPI to all other CPU
  281. * of the system defined in the mask.
  282. */
  283. static int
  284. __smp_call_function_mask(cpumask_t mask,
  285. void (*func)(void *), void *info,
  286. int wait)
  287. {
  288. struct call_data_struct data;
  289. cpumask_t allbutself;
  290. int cpus;
  291. allbutself = cpu_online_map;
  292. cpu_clear(smp_processor_id(), allbutself);
  293. cpus_and(mask, mask, allbutself);
  294. cpus = cpus_weight(mask);
  295. if (!cpus)
  296. return 0;
  297. data.func = func;
  298. data.info = info;
  299. atomic_set(&data.started, 0);
  300. data.wait = wait;
  301. if (wait)
  302. atomic_set(&data.finished, 0);
  303. call_data = &data;
  304. wmb();
  305. /* Send a message to other CPUs */
  306. if (cpus_equal(mask, allbutself))
  307. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  308. else
  309. send_IPI_mask(mask, CALL_FUNCTION_VECTOR);
  310. /* Wait for response */
  311. while (atomic_read(&data.started) != cpus)
  312. cpu_relax();
  313. if (!wait)
  314. return 0;
  315. while (atomic_read(&data.finished) != cpus)
  316. cpu_relax();
  317. return 0;
  318. }
  319. /**
  320. * smp_call_function_mask(): Run a function on a set of other CPUs.
  321. * @mask: The set of cpus to run on. Must not include the current cpu.
  322. * @func: The function to run. This must be fast and non-blocking.
  323. * @info: An arbitrary pointer to pass to the function.
  324. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  325. *
  326. * Returns 0 on success, else a negative status code.
  327. *
  328. * If @wait is true, then returns once @func has returned; otherwise
  329. * it returns just before the target cpu calls @func.
  330. *
  331. * You must not call this function with disabled interrupts or from a
  332. * hardware interrupt handler or from a bottom half handler.
  333. */
  334. int smp_call_function_mask(cpumask_t mask,
  335. void (*func)(void *), void *info,
  336. int wait)
  337. {
  338. int ret;
  339. /* Can deadlock when called with interrupts disabled */
  340. WARN_ON(irqs_disabled());
  341. spin_lock(&call_lock);
  342. ret = __smp_call_function_mask(mask, func, info, wait);
  343. spin_unlock(&call_lock);
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(smp_call_function_mask);
  347. /*
  348. * smp_call_function_single - Run a function on a specific CPU
  349. * @func: The function to run. This must be fast and non-blocking.
  350. * @info: An arbitrary pointer to pass to the function.
  351. * @nonatomic: Currently unused.
  352. * @wait: If true, wait until function has completed on other CPUs.
  353. *
  354. * Retrurns 0 on success, else a negative status code.
  355. *
  356. * Does not return until the remote CPU is nearly ready to execute <func>
  357. * or is or has executed.
  358. */
  359. int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
  360. int nonatomic, int wait)
  361. {
  362. /* prevent preemption and reschedule on another processor */
  363. int ret;
  364. int me = get_cpu();
  365. /* Can deadlock when called with interrupts disabled */
  366. WARN_ON(irqs_disabled());
  367. if (cpu == me) {
  368. local_irq_disable();
  369. func(info);
  370. local_irq_enable();
  371. put_cpu();
  372. return 0;
  373. }
  374. ret = smp_call_function_mask(cpumask_of_cpu(cpu), func, info, wait);
  375. put_cpu();
  376. return ret;
  377. }
  378. EXPORT_SYMBOL(smp_call_function_single);
  379. /*
  380. * smp_call_function - run a function on all other CPUs.
  381. * @func: The function to run. This must be fast and non-blocking.
  382. * @info: An arbitrary pointer to pass to the function.
  383. * @nonatomic: currently unused.
  384. * @wait: If true, wait (atomically) until function has completed on other
  385. * CPUs.
  386. *
  387. * Returns 0 on success, else a negative status code. Does not return until
  388. * remote CPUs are nearly ready to execute func or are or have executed.
  389. *
  390. * You must not call this function with disabled interrupts or from a
  391. * hardware interrupt handler or from a bottom half handler.
  392. * Actually there are a few legal cases, like panic.
  393. */
  394. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  395. int wait)
  396. {
  397. return smp_call_function_mask(cpu_online_map, func, info, wait);
  398. }
  399. EXPORT_SYMBOL(smp_call_function);
  400. static void stop_this_cpu(void *dummy)
  401. {
  402. local_irq_disable();
  403. /*
  404. * Remove this CPU:
  405. */
  406. cpu_clear(smp_processor_id(), cpu_online_map);
  407. disable_local_APIC();
  408. for (;;)
  409. halt();
  410. }
  411. void smp_send_stop(void)
  412. {
  413. int nolock;
  414. unsigned long flags;
  415. if (reboot_force)
  416. return;
  417. /* Don't deadlock on the call lock in panic */
  418. nolock = !spin_trylock(&call_lock);
  419. local_irq_save(flags);
  420. __smp_call_function_mask(cpu_online_map, stop_this_cpu, NULL, 0);
  421. if (!nolock)
  422. spin_unlock(&call_lock);
  423. disable_local_APIC();
  424. local_irq_restore(flags);
  425. }
  426. /*
  427. * Reschedule call back. Nothing to do,
  428. * all the work is done automatically when
  429. * we return from the interrupt.
  430. */
  431. asmlinkage void smp_reschedule_interrupt(void)
  432. {
  433. ack_APIC_irq();
  434. add_pda(irq_resched_count, 1);
  435. }
  436. asmlinkage void smp_call_function_interrupt(void)
  437. {
  438. void (*func) (void *info) = call_data->func;
  439. void *info = call_data->info;
  440. int wait = call_data->wait;
  441. ack_APIC_irq();
  442. /*
  443. * Notify initiating CPU that I've grabbed the data and am
  444. * about to execute the function
  445. */
  446. mb();
  447. atomic_inc(&call_data->started);
  448. /*
  449. * At this point the info structure may be out of scope unless wait==1
  450. */
  451. exit_idle();
  452. irq_enter();
  453. (*func)(info);
  454. add_pda(irq_call_count, 1);
  455. irq_exit();
  456. if (wait) {
  457. mb();
  458. atomic_inc(&call_data->finished);
  459. }
  460. }