tlb_hash64.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/percpu.h>
  26. #include <linux/hardirq.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/tlb.h>
  30. #include <asm/bug.h>
  31. #include <trace/events/thp.h>
  32. DEFINE_PER_CPU(struct ppc64_tlb_batch, ppc64_tlb_batch);
  33. /*
  34. * A linux PTE was changed and the corresponding hash table entry
  35. * neesd to be flushed. This function will either perform the flush
  36. * immediately or will batch it up if the current CPU has an active
  37. * batch on it.
  38. */
  39. void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
  40. pte_t *ptep, unsigned long pte, int huge)
  41. {
  42. unsigned long vpn;
  43. struct ppc64_tlb_batch *batch = &get_cpu_var(ppc64_tlb_batch);
  44. unsigned long vsid;
  45. unsigned int psize;
  46. int ssize;
  47. real_pte_t rpte;
  48. int i;
  49. i = batch->index;
  50. /* Get page size (maybe move back to caller).
  51. *
  52. * NOTE: when using special 64K mappings in 4K environment like
  53. * for SPEs, we obtain the page size from the slice, which thus
  54. * must still exist (and thus the VMA not reused) at the time
  55. * of this call
  56. */
  57. if (huge) {
  58. #ifdef CONFIG_HUGETLB_PAGE
  59. psize = get_slice_psize(mm, addr);
  60. /* Mask the address for the correct page size */
  61. addr &= ~((1UL << mmu_psize_defs[psize].shift) - 1);
  62. #else
  63. BUG();
  64. psize = pte_pagesize_index(mm, addr, pte); /* shutup gcc */
  65. #endif
  66. } else {
  67. psize = pte_pagesize_index(mm, addr, pte);
  68. /* Mask the address for the standard page size. If we
  69. * have a 64k page kernel, but the hardware does not
  70. * support 64k pages, this might be different from the
  71. * hardware page size encoded in the slice table. */
  72. addr &= PAGE_MASK;
  73. }
  74. /* Build full vaddr */
  75. if (!is_kernel_addr(addr)) {
  76. ssize = user_segment_size(addr);
  77. vsid = get_vsid(mm->context.id, addr, ssize);
  78. } else {
  79. vsid = get_kernel_vsid(addr, mmu_kernel_ssize);
  80. ssize = mmu_kernel_ssize;
  81. }
  82. WARN_ON(vsid == 0);
  83. vpn = hpt_vpn(addr, vsid, ssize);
  84. rpte = __real_pte(__pte(pte), ptep);
  85. /*
  86. * Check if we have an active batch on this CPU. If not, just
  87. * flush now and return. For now, we don global invalidates
  88. * in that case, might be worth testing the mm cpu mask though
  89. * and decide to use local invalidates instead...
  90. */
  91. if (!batch->active) {
  92. flush_hash_page(vpn, rpte, psize, ssize, 0);
  93. put_cpu_var(ppc64_tlb_batch);
  94. return;
  95. }
  96. /*
  97. * This can happen when we are in the middle of a TLB batch and
  98. * we encounter memory pressure (eg copy_page_range when it tries
  99. * to allocate a new pte). If we have to reclaim memory and end
  100. * up scanning and resetting referenced bits then our batch context
  101. * will change mid stream.
  102. *
  103. * We also need to ensure only one page size is present in a given
  104. * batch
  105. */
  106. if (i != 0 && (mm != batch->mm || batch->psize != psize ||
  107. batch->ssize != ssize)) {
  108. __flush_tlb_pending(batch);
  109. i = 0;
  110. }
  111. if (i == 0) {
  112. batch->mm = mm;
  113. batch->psize = psize;
  114. batch->ssize = ssize;
  115. }
  116. batch->pte[i] = rpte;
  117. batch->vpn[i] = vpn;
  118. batch->index = ++i;
  119. if (i >= PPC64_TLB_BATCH_NR)
  120. __flush_tlb_pending(batch);
  121. put_cpu_var(ppc64_tlb_batch);
  122. }
  123. /*
  124. * This function is called when terminating an mmu batch or when a batch
  125. * is full. It will perform the flush of all the entries currently stored
  126. * in a batch.
  127. *
  128. * Must be called from within some kind of spinlock/non-preempt region...
  129. */
  130. void __flush_tlb_pending(struct ppc64_tlb_batch *batch)
  131. {
  132. const struct cpumask *tmp;
  133. int i, local = 0;
  134. i = batch->index;
  135. tmp = cpumask_of(smp_processor_id());
  136. if (cpumask_equal(mm_cpumask(batch->mm), tmp))
  137. local = 1;
  138. if (i == 1)
  139. flush_hash_page(batch->vpn[0], batch->pte[0],
  140. batch->psize, batch->ssize, local);
  141. else
  142. flush_hash_range(i, local);
  143. batch->index = 0;
  144. }
  145. void tlb_flush(struct mmu_gather *tlb)
  146. {
  147. struct ppc64_tlb_batch *tlbbatch = &get_cpu_var(ppc64_tlb_batch);
  148. /* If there's a TLB batch pending, then we must flush it because the
  149. * pages are going to be freed and we really don't want to have a CPU
  150. * access a freed page because it has a stale TLB
  151. */
  152. if (tlbbatch->index)
  153. __flush_tlb_pending(tlbbatch);
  154. put_cpu_var(ppc64_tlb_batch);
  155. }
  156. /**
  157. * __flush_hash_table_range - Flush all HPTEs for a given address range
  158. * from the hash table (and the TLB). But keeps
  159. * the linux PTEs intact.
  160. *
  161. * @mm : mm_struct of the target address space (generally init_mm)
  162. * @start : starting address
  163. * @end : ending address (not included in the flush)
  164. *
  165. * This function is mostly to be used by some IO hotplug code in order
  166. * to remove all hash entries from a given address range used to map IO
  167. * space on a removed PCI-PCI bidge without tearing down the full mapping
  168. * since 64K pages may overlap with other bridges when using 64K pages
  169. * with 4K HW pages on IO space.
  170. *
  171. * Because of that usage pattern, it is implemented for small size rather
  172. * than speed.
  173. */
  174. void __flush_hash_table_range(struct mm_struct *mm, unsigned long start,
  175. unsigned long end)
  176. {
  177. bool is_thp;
  178. int hugepage_shift;
  179. unsigned long flags;
  180. start = _ALIGN_DOWN(start, PAGE_SIZE);
  181. end = _ALIGN_UP(end, PAGE_SIZE);
  182. BUG_ON(!mm->pgd);
  183. /* Note: Normally, we should only ever use a batch within a
  184. * PTE locked section. This violates the rule, but will work
  185. * since we don't actually modify the PTEs, we just flush the
  186. * hash while leaving the PTEs intact (including their reference
  187. * to being hashed). This is not the most performance oriented
  188. * way to do things but is fine for our needs here.
  189. */
  190. local_irq_save(flags);
  191. arch_enter_lazy_mmu_mode();
  192. for (; start < end; start += PAGE_SIZE) {
  193. pte_t *ptep = find_linux_pte_or_hugepte(mm->pgd, start, &is_thp,
  194. &hugepage_shift);
  195. unsigned long pte;
  196. if (ptep == NULL)
  197. continue;
  198. pte = pte_val(*ptep);
  199. if (is_thp)
  200. trace_hugepage_invalidate(start, pte);
  201. if (!(pte & _PAGE_HASHPTE))
  202. continue;
  203. if (unlikely(is_thp))
  204. hpte_do_hugepage_flush(mm, start, (pmd_t *)ptep, pte);
  205. else
  206. hpte_need_flush(mm, start, ptep, pte, hugepage_shift);
  207. }
  208. arch_leave_lazy_mmu_mode();
  209. local_irq_restore(flags);
  210. }
  211. void flush_tlb_pmd_range(struct mm_struct *mm, pmd_t *pmd, unsigned long addr)
  212. {
  213. pte_t *pte;
  214. pte_t *start_pte;
  215. unsigned long flags;
  216. addr = _ALIGN_DOWN(addr, PMD_SIZE);
  217. /* Note: Normally, we should only ever use a batch within a
  218. * PTE locked section. This violates the rule, but will work
  219. * since we don't actually modify the PTEs, we just flush the
  220. * hash while leaving the PTEs intact (including their reference
  221. * to being hashed). This is not the most performance oriented
  222. * way to do things but is fine for our needs here.
  223. */
  224. local_irq_save(flags);
  225. arch_enter_lazy_mmu_mode();
  226. start_pte = pte_offset_map(pmd, addr);
  227. for (pte = start_pte; pte < start_pte + PTRS_PER_PTE; pte++) {
  228. unsigned long pteval = pte_val(*pte);
  229. if (pteval & _PAGE_HASHPTE)
  230. hpte_need_flush(mm, addr, pte, pteval, 0);
  231. addr += PAGE_SIZE;
  232. }
  233. arch_leave_lazy_mmu_mode();
  234. local_irq_restore(flags);
  235. }