tlb.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef _ASM_IA64_TLB_H
  2. #define _ASM_IA64_TLB_H
  3. /*
  4. * Based on <asm-generic/tlb.h>.
  5. *
  6. * Copyright (C) 2002-2003 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. */
  9. /*
  10. * Removing a translation from a page table (including TLB-shootdown) is a four-step
  11. * procedure:
  12. *
  13. * (1) Flush (virtual) caches --- ensures virtual memory is coherent with kernel memory
  14. * (this is a no-op on ia64).
  15. * (2) Clear the relevant portions of the page-table
  16. * (3) Flush the TLBs --- ensures that stale content is gone from CPU TLBs
  17. * (4) Release the pages that were freed up in step (2).
  18. *
  19. * Note that the ordering of these steps is crucial to avoid races on MP machines.
  20. *
  21. * The Linux kernel defines several platform-specific hooks for TLB-shootdown. When
  22. * unmapping a portion of the virtual address space, these hooks are called according to
  23. * the following template:
  24. *
  25. * tlb <- tlb_gather_mmu(mm, start, end); // start unmap for address space MM
  26. * {
  27. * for each vma that needs a shootdown do {
  28. * tlb_start_vma(tlb, vma);
  29. * for each page-table-entry PTE that needs to be removed do {
  30. * tlb_remove_tlb_entry(tlb, pte, address);
  31. * if (pte refers to a normal page) {
  32. * tlb_remove_page(tlb, page);
  33. * }
  34. * }
  35. * tlb_end_vma(tlb, vma);
  36. * }
  37. * }
  38. * tlb_finish_mmu(tlb, start, end); // finish unmap for address space MM
  39. */
  40. #include <linux/mm.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/swap.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/processor.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/machvec.h>
  47. /*
  48. * If we can't allocate a page to make a big batch of page pointers
  49. * to work on, then just handle a few from the on-stack structure.
  50. */
  51. #define IA64_GATHER_BUNDLE 8
  52. struct mmu_gather {
  53. struct mm_struct *mm;
  54. unsigned int nr;
  55. unsigned int max;
  56. unsigned char fullmm; /* non-zero means full mm flush */
  57. unsigned char need_flush; /* really unmapped some PTEs? */
  58. unsigned long start, end;
  59. unsigned long start_addr;
  60. unsigned long end_addr;
  61. struct page **pages;
  62. struct page *local[IA64_GATHER_BUNDLE];
  63. };
  64. struct ia64_tr_entry {
  65. u64 ifa;
  66. u64 itir;
  67. u64 pte;
  68. u64 rr;
  69. }; /*Record for tr entry!*/
  70. extern int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size);
  71. extern void ia64_ptr_entry(u64 target_mask, int slot);
  72. extern struct ia64_tr_entry *ia64_idtrs[NR_CPUS];
  73. /*
  74. region register macros
  75. */
  76. #define RR_TO_VE(val) (((val) >> 0) & 0x0000000000000001)
  77. #define RR_VE(val) (((val) & 0x0000000000000001) << 0)
  78. #define RR_VE_MASK 0x0000000000000001L
  79. #define RR_VE_SHIFT 0
  80. #define RR_TO_PS(val) (((val) >> 2) & 0x000000000000003f)
  81. #define RR_PS(val) (((val) & 0x000000000000003f) << 2)
  82. #define RR_PS_MASK 0x00000000000000fcL
  83. #define RR_PS_SHIFT 2
  84. #define RR_RID_MASK 0x00000000ffffff00L
  85. #define RR_TO_RID(val) ((val >> 8) & 0xffffff)
  86. static inline void
  87. ia64_tlb_flush_mmu_tlbonly(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  88. {
  89. tlb->need_flush = 0;
  90. if (tlb->fullmm) {
  91. /*
  92. * Tearing down the entire address space. This happens both as a result
  93. * of exit() and execve(). The latter case necessitates the call to
  94. * flush_tlb_mm() here.
  95. */
  96. flush_tlb_mm(tlb->mm);
  97. } else if (unlikely (end - start >= 1024*1024*1024*1024UL
  98. || REGION_NUMBER(start) != REGION_NUMBER(end - 1)))
  99. {
  100. /*
  101. * If we flush more than a tera-byte or across regions, we're probably
  102. * better off just flushing the entire TLB(s). This should be very rare
  103. * and is not worth optimizing for.
  104. */
  105. flush_tlb_all();
  106. } else {
  107. /*
  108. * XXX fix me: flush_tlb_range() should take an mm pointer instead of a
  109. * vma pointer.
  110. */
  111. struct vm_area_struct vma;
  112. vma.vm_mm = tlb->mm;
  113. /* flush the address range from the tlb: */
  114. flush_tlb_range(&vma, start, end);
  115. /* now flush the virt. page-table area mapping the address range: */
  116. flush_tlb_range(&vma, ia64_thash(start), ia64_thash(end));
  117. }
  118. }
  119. static inline void
  120. ia64_tlb_flush_mmu_free(struct mmu_gather *tlb)
  121. {
  122. unsigned long i;
  123. unsigned int nr;
  124. /* lastly, release the freed pages */
  125. nr = tlb->nr;
  126. tlb->nr = 0;
  127. tlb->start_addr = ~0UL;
  128. for (i = 0; i < nr; ++i)
  129. free_page_and_swap_cache(tlb->pages[i]);
  130. }
  131. /*
  132. * Flush the TLB for address range START to END and, if not in fast mode, release the
  133. * freed pages that where gathered up to this point.
  134. */
  135. static inline void
  136. ia64_tlb_flush_mmu (struct mmu_gather *tlb, unsigned long start, unsigned long end)
  137. {
  138. if (!tlb->need_flush)
  139. return;
  140. ia64_tlb_flush_mmu_tlbonly(tlb, start, end);
  141. ia64_tlb_flush_mmu_free(tlb);
  142. }
  143. static inline void __tlb_alloc_page(struct mmu_gather *tlb)
  144. {
  145. unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  146. if (addr) {
  147. tlb->pages = (void *)addr;
  148. tlb->max = PAGE_SIZE / sizeof(void *);
  149. }
  150. }
  151. static inline void
  152. tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
  153. {
  154. tlb->mm = mm;
  155. tlb->max = ARRAY_SIZE(tlb->local);
  156. tlb->pages = tlb->local;
  157. tlb->nr = 0;
  158. tlb->fullmm = !(start | (end+1));
  159. tlb->start = start;
  160. tlb->end = end;
  161. tlb->start_addr = ~0UL;
  162. }
  163. /*
  164. * Called at the end of the shootdown operation to free up any resources that were
  165. * collected.
  166. */
  167. static inline void
  168. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  169. {
  170. /*
  171. * Note: tlb->nr may be 0 at this point, so we can't rely on tlb->start_addr and
  172. * tlb->end_addr.
  173. */
  174. ia64_tlb_flush_mmu(tlb, start, end);
  175. /* keep the page table cache within bounds */
  176. check_pgt_cache();
  177. if (tlb->pages != tlb->local)
  178. free_pages((unsigned long)tlb->pages, 0);
  179. }
  180. /*
  181. * Logically, this routine frees PAGE. On MP machines, the actual freeing of the page
  182. * must be delayed until after the TLB has been flushed (see comments at the beginning of
  183. * this file).
  184. */
  185. static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  186. {
  187. tlb->need_flush = 1;
  188. if (!tlb->nr && tlb->pages == tlb->local)
  189. __tlb_alloc_page(tlb);
  190. tlb->pages[tlb->nr++] = page;
  191. VM_BUG_ON(tlb->nr > tlb->max);
  192. return tlb->max - tlb->nr;
  193. }
  194. static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
  195. {
  196. ia64_tlb_flush_mmu_tlbonly(tlb, tlb->start_addr, tlb->end_addr);
  197. }
  198. static inline void tlb_flush_mmu_free(struct mmu_gather *tlb)
  199. {
  200. ia64_tlb_flush_mmu_free(tlb);
  201. }
  202. static inline void tlb_flush_mmu(struct mmu_gather *tlb)
  203. {
  204. ia64_tlb_flush_mmu(tlb, tlb->start_addr, tlb->end_addr);
  205. }
  206. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  207. {
  208. if (!__tlb_remove_page(tlb, page))
  209. tlb_flush_mmu(tlb);
  210. }
  211. /*
  212. * Remove TLB entry for PTE mapped at virtual address ADDRESS. This is called for any
  213. * PTE, not just those pointing to (normal) physical memory.
  214. */
  215. static inline void
  216. __tlb_remove_tlb_entry (struct mmu_gather *tlb, pte_t *ptep, unsigned long address)
  217. {
  218. if (tlb->start_addr == ~0UL)
  219. tlb->start_addr = address;
  220. tlb->end_addr = address + PAGE_SIZE;
  221. }
  222. #define tlb_migrate_finish(mm) platform_tlb_migrate_finish(mm)
  223. #define tlb_start_vma(tlb, vma) do { } while (0)
  224. #define tlb_end_vma(tlb, vma) do { } while (0)
  225. #define tlb_remove_tlb_entry(tlb, ptep, addr) \
  226. do { \
  227. tlb->need_flush = 1; \
  228. __tlb_remove_tlb_entry(tlb, ptep, addr); \
  229. } while (0)
  230. #define pte_free_tlb(tlb, ptep, address) \
  231. do { \
  232. tlb->need_flush = 1; \
  233. __pte_free_tlb(tlb, ptep, address); \
  234. } while (0)
  235. #define pmd_free_tlb(tlb, ptep, address) \
  236. do { \
  237. tlb->need_flush = 1; \
  238. __pmd_free_tlb(tlb, ptep, address); \
  239. } while (0)
  240. #define pud_free_tlb(tlb, pudp, address) \
  241. do { \
  242. tlb->need_flush = 1; \
  243. __pud_free_tlb(tlb, pudp, address); \
  244. } while (0)
  245. #endif /* _ASM_IA64_TLB_H */