tlb_64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * This file contains the routines for flushing entries from the
  3. * TLB and MMU hash table.
  4. *
  5. * Derived from arch/ppc64/mm/init.c:
  6. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  7. *
  8. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  9. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  10. * Copyright (C) 1996 Paul Mackerras
  11. *
  12. * Derived from "arch/i386/mm/init.c"
  13. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  14. *
  15. * Dave Engebretsen <engebret@us.ibm.com>
  16. * Rework for PPC64 port.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/init.h>
  26. #include <linux/percpu.h>
  27. #include <linux/hardirq.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/tlb.h>
  31. #include <asm/bug.h>
  32. DEFINE_PER_CPU(struct ppc64_tlb_batch, ppc64_tlb_batch);
  33. /* This is declared as we are using the more or less generic
  34. * include/asm-powerpc/tlb.h file -- tgall
  35. */
  36. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  37. DEFINE_PER_CPU(struct pte_freelist_batch *, pte_freelist_cur);
  38. unsigned long pte_freelist_forced_free;
  39. struct pte_freelist_batch
  40. {
  41. struct rcu_head rcu;
  42. unsigned int index;
  43. pgtable_free_t tables[0];
  44. };
  45. DEFINE_PER_CPU(struct pte_freelist_batch *, pte_freelist_cur);
  46. unsigned long pte_freelist_forced_free;
  47. #define PTE_FREELIST_SIZE \
  48. ((PAGE_SIZE - sizeof(struct pte_freelist_batch)) \
  49. / sizeof(pgtable_free_t))
  50. #ifdef CONFIG_SMP
  51. static void pte_free_smp_sync(void *arg)
  52. {
  53. /* Do nothing, just ensure we sync with all CPUs */
  54. }
  55. #endif
  56. /* This is only called when we are critically out of memory
  57. * (and fail to get a page in pte_free_tlb).
  58. */
  59. static void pgtable_free_now(pgtable_free_t pgf)
  60. {
  61. pte_freelist_forced_free++;
  62. smp_call_function(pte_free_smp_sync, NULL, 0, 1);
  63. pgtable_free(pgf);
  64. }
  65. static void pte_free_rcu_callback(struct rcu_head *head)
  66. {
  67. struct pte_freelist_batch *batch =
  68. container_of(head, struct pte_freelist_batch, rcu);
  69. unsigned int i;
  70. for (i = 0; i < batch->index; i++)
  71. pgtable_free(batch->tables[i]);
  72. free_page((unsigned long)batch);
  73. }
  74. static void pte_free_submit(struct pte_freelist_batch *batch)
  75. {
  76. INIT_RCU_HEAD(&batch->rcu);
  77. call_rcu(&batch->rcu, pte_free_rcu_callback);
  78. }
  79. void pgtable_free_tlb(struct mmu_gather *tlb, pgtable_free_t pgf)
  80. {
  81. /* This is safe since tlb_gather_mmu has disabled preemption */
  82. cpumask_t local_cpumask = cpumask_of_cpu(smp_processor_id());
  83. struct pte_freelist_batch **batchp = &__get_cpu_var(pte_freelist_cur);
  84. if (atomic_read(&tlb->mm->mm_users) < 2 ||
  85. cpus_equal(tlb->mm->cpu_vm_mask, local_cpumask)) {
  86. pgtable_free(pgf);
  87. return;
  88. }
  89. if (*batchp == NULL) {
  90. *batchp = (struct pte_freelist_batch *)__get_free_page(GFP_ATOMIC);
  91. if (*batchp == NULL) {
  92. pgtable_free_now(pgf);
  93. return;
  94. }
  95. (*batchp)->index = 0;
  96. }
  97. (*batchp)->tables[(*batchp)->index++] = pgf;
  98. if ((*batchp)->index == PTE_FREELIST_SIZE) {
  99. pte_free_submit(*batchp);
  100. *batchp = NULL;
  101. }
  102. }
  103. /*
  104. * A linux PTE was changed and the corresponding hash table entry
  105. * neesd to be flushed. This function will either perform the flush
  106. * immediately or will batch it up if the current CPU has an active
  107. * batch on it.
  108. *
  109. * Must be called from within some kind of spinlock/non-preempt region...
  110. */
  111. void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
  112. pte_t *ptep, unsigned long pte, int huge)
  113. {
  114. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  115. unsigned long vsid, vaddr;
  116. unsigned int psize;
  117. int ssize;
  118. real_pte_t rpte;
  119. int i;
  120. i = batch->index;
  121. /* We mask the address for the base page size. Huge pages will
  122. * have applied their own masking already
  123. */
  124. addr &= PAGE_MASK;
  125. /* Get page size (maybe move back to caller).
  126. *
  127. * NOTE: when using special 64K mappings in 4K environment like
  128. * for SPEs, we obtain the page size from the slice, which thus
  129. * must still exist (and thus the VMA not reused) at the time
  130. * of this call
  131. */
  132. if (huge) {
  133. #ifdef CONFIG_HUGETLB_PAGE
  134. psize = mmu_huge_psize;
  135. #else
  136. BUG();
  137. psize = pte_pagesize_index(mm, addr, pte); /* shutup gcc */
  138. #endif
  139. } else
  140. psize = pte_pagesize_index(mm, addr, pte);
  141. /* Build full vaddr */
  142. if (!is_kernel_addr(addr)) {
  143. ssize = user_segment_size(addr);
  144. vsid = get_vsid(mm->context.id, addr, ssize);
  145. WARN_ON(vsid == 0);
  146. } else {
  147. vsid = get_kernel_vsid(addr, mmu_kernel_ssize);
  148. ssize = mmu_kernel_ssize;
  149. }
  150. vaddr = hpt_va(addr, vsid, ssize);
  151. rpte = __real_pte(__pte(pte), ptep);
  152. /*
  153. * Check if we have an active batch on this CPU. If not, just
  154. * flush now and return. For now, we don global invalidates
  155. * in that case, might be worth testing the mm cpu mask though
  156. * and decide to use local invalidates instead...
  157. */
  158. if (!batch->active) {
  159. flush_hash_page(vaddr, rpte, psize, ssize, 0);
  160. return;
  161. }
  162. /*
  163. * This can happen when we are in the middle of a TLB batch and
  164. * we encounter memory pressure (eg copy_page_range when it tries
  165. * to allocate a new pte). If we have to reclaim memory and end
  166. * up scanning and resetting referenced bits then our batch context
  167. * will change mid stream.
  168. *
  169. * We also need to ensure only one page size is present in a given
  170. * batch
  171. */
  172. if (i != 0 && (mm != batch->mm || batch->psize != psize ||
  173. batch->ssize != ssize)) {
  174. __flush_tlb_pending(batch);
  175. i = 0;
  176. }
  177. if (i == 0) {
  178. batch->mm = mm;
  179. batch->psize = psize;
  180. batch->ssize = ssize;
  181. }
  182. batch->pte[i] = rpte;
  183. batch->vaddr[i] = vaddr;
  184. batch->index = ++i;
  185. if (i >= PPC64_TLB_BATCH_NR)
  186. __flush_tlb_pending(batch);
  187. }
  188. /*
  189. * This function is called when terminating an mmu batch or when a batch
  190. * is full. It will perform the flush of all the entries currently stored
  191. * in a batch.
  192. *
  193. * Must be called from within some kind of spinlock/non-preempt region...
  194. */
  195. void __flush_tlb_pending(struct ppc64_tlb_batch *batch)
  196. {
  197. cpumask_t tmp;
  198. int i, local = 0;
  199. i = batch->index;
  200. tmp = cpumask_of_cpu(smp_processor_id());
  201. if (cpus_equal(batch->mm->cpu_vm_mask, tmp))
  202. local = 1;
  203. if (i == 1)
  204. flush_hash_page(batch->vaddr[0], batch->pte[0],
  205. batch->psize, batch->ssize, local);
  206. else
  207. flush_hash_range(i, local);
  208. batch->index = 0;
  209. }
  210. void pte_free_finish(void)
  211. {
  212. /* This is safe since tlb_gather_mmu has disabled preemption */
  213. struct pte_freelist_batch **batchp = &__get_cpu_var(pte_freelist_cur);
  214. if (*batchp == NULL)
  215. return;
  216. pte_free_submit(*batchp);
  217. *batchp = NULL;
  218. }
  219. /**
  220. * __flush_hash_table_range - Flush all HPTEs for a given address range
  221. * from the hash table (and the TLB). But keeps
  222. * the linux PTEs intact.
  223. *
  224. * @mm : mm_struct of the target address space (generally init_mm)
  225. * @start : starting address
  226. * @end : ending address (not included in the flush)
  227. *
  228. * This function is mostly to be used by some IO hotplug code in order
  229. * to remove all hash entries from a given address range used to map IO
  230. * space on a removed PCI-PCI bidge without tearing down the full mapping
  231. * since 64K pages may overlap with other bridges when using 64K pages
  232. * with 4K HW pages on IO space.
  233. *
  234. * Because of that usage pattern, it's only available with CONFIG_HOTPLUG
  235. * and is implemented for small size rather than speed.
  236. */
  237. #ifdef CONFIG_HOTPLUG
  238. void __flush_hash_table_range(struct mm_struct *mm, unsigned long start,
  239. unsigned long end)
  240. {
  241. unsigned long flags;
  242. start = _ALIGN_DOWN(start, PAGE_SIZE);
  243. end = _ALIGN_UP(end, PAGE_SIZE);
  244. BUG_ON(!mm->pgd);
  245. /* Note: Normally, we should only ever use a batch within a
  246. * PTE locked section. This violates the rule, but will work
  247. * since we don't actually modify the PTEs, we just flush the
  248. * hash while leaving the PTEs intact (including their reference
  249. * to being hashed). This is not the most performance oriented
  250. * way to do things but is fine for our needs here.
  251. */
  252. local_irq_save(flags);
  253. arch_enter_lazy_mmu_mode();
  254. for (; start < end; start += PAGE_SIZE) {
  255. pte_t *ptep = find_linux_pte(mm->pgd, start);
  256. unsigned long pte;
  257. if (ptep == NULL)
  258. continue;
  259. pte = pte_val(*ptep);
  260. if (!(pte & _PAGE_HASHPTE))
  261. continue;
  262. hpte_need_flush(mm, start, ptep, pte, 0);
  263. }
  264. arch_leave_lazy_mmu_mode();
  265. local_irq_restore(flags);
  266. }
  267. #endif /* CONFIG_HOTPLUG */