tlb_hash64.c 6.6 KB

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