pgtable.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifndef _ASM_POWERPC_PGTABLE_H
  2. #define _ASM_POWERPC_PGTABLE_H
  3. #ifdef __KERNEL__
  4. #ifndef __ASSEMBLY__
  5. #include <linux/mmdebug.h>
  6. #include <linux/mmzone.h>
  7. #include <asm/processor.h> /* For TASK_SIZE */
  8. #include <asm/mmu.h>
  9. #include <asm/page.h>
  10. struct mm_struct;
  11. #endif /* !__ASSEMBLY__ */
  12. #if defined(CONFIG_PPC64)
  13. # include <asm/pgtable-ppc64.h>
  14. #else
  15. # include <asm/pgtable-ppc32.h>
  16. #endif
  17. /*
  18. * We save the slot number & secondary bit in the second half of the
  19. * PTE page. We use the 8 bytes per each pte entry.
  20. */
  21. #define PTE_PAGE_HIDX_OFFSET (PTRS_PER_PTE * 8)
  22. #ifndef __ASSEMBLY__
  23. #include <asm/tlbflush.h>
  24. /* Generic accessors to PTE bits */
  25. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_RW; }
  26. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  27. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  28. static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
  29. static inline int pte_special(pte_t pte) { return pte_val(pte) & _PAGE_SPECIAL; }
  30. static inline int pte_none(pte_t pte) { return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }
  31. static inline pgprot_t pte_pgprot(pte_t pte) { return __pgprot(pte_val(pte) & PAGE_PROT_BITS); }
  32. #ifdef CONFIG_NUMA_BALANCING
  33. static inline int pte_present(pte_t pte)
  34. {
  35. return pte_val(pte) & _PAGE_NUMA_MASK;
  36. }
  37. #define pte_present_nonuma pte_present_nonuma
  38. static inline int pte_present_nonuma(pte_t pte)
  39. {
  40. return pte_val(pte) & (_PAGE_PRESENT);
  41. }
  42. #define ptep_set_numa ptep_set_numa
  43. static inline void ptep_set_numa(struct mm_struct *mm, unsigned long addr,
  44. pte_t *ptep)
  45. {
  46. if ((pte_val(*ptep) & _PAGE_PRESENT) == 0)
  47. VM_BUG_ON(1);
  48. pte_update(mm, addr, ptep, _PAGE_PRESENT, _PAGE_NUMA, 0);
  49. return;
  50. }
  51. #define pmdp_set_numa pmdp_set_numa
  52. static inline void pmdp_set_numa(struct mm_struct *mm, unsigned long addr,
  53. pmd_t *pmdp)
  54. {
  55. if ((pmd_val(*pmdp) & _PAGE_PRESENT) == 0)
  56. VM_BUG_ON(1);
  57. pmd_hugepage_update(mm, addr, pmdp, _PAGE_PRESENT, _PAGE_NUMA);
  58. return;
  59. }
  60. /*
  61. * Generic NUMA pte helpers expect pteval_t and pmdval_t types to exist
  62. * which was inherited from x86. For the purposes of powerpc pte_basic_t and
  63. * pmd_t are equivalent
  64. */
  65. #define pteval_t pte_basic_t
  66. #define pmdval_t pmd_t
  67. static inline pteval_t ptenuma_flags(pte_t pte)
  68. {
  69. return pte_val(pte) & _PAGE_NUMA_MASK;
  70. }
  71. static inline pmdval_t pmdnuma_flags(pmd_t pmd)
  72. {
  73. return pmd_val(pmd) & _PAGE_NUMA_MASK;
  74. }
  75. # else
  76. static inline int pte_present(pte_t pte)
  77. {
  78. return pte_val(pte) & _PAGE_PRESENT;
  79. }
  80. #endif /* CONFIG_NUMA_BALANCING */
  81. /* Conversion functions: convert a page and protection to a page entry,
  82. * and a page entry and page directory to the page they refer to.
  83. *
  84. * Even if PTEs can be unsigned long long, a PFN is always an unsigned
  85. * long for now.
  86. */
  87. static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) {
  88. return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |
  89. pgprot_val(pgprot)); }
  90. static inline unsigned long pte_pfn(pte_t pte) {
  91. return pte_val(pte) >> PTE_RPN_SHIFT; }
  92. /* Keep these as a macros to avoid include dependency mess */
  93. #define pte_page(x) pfn_to_page(pte_pfn(x))
  94. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
  95. /* Generic modifiers for PTE bits */
  96. static inline pte_t pte_wrprotect(pte_t pte) {
  97. pte_val(pte) &= ~(_PAGE_RW | _PAGE_HWWRITE); return pte; }
  98. static inline pte_t pte_mkclean(pte_t pte) {
  99. pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HWWRITE); return pte; }
  100. static inline pte_t pte_mkold(pte_t pte) {
  101. pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
  102. static inline pte_t pte_mkwrite(pte_t pte) {
  103. pte_val(pte) |= _PAGE_RW; return pte; }
  104. static inline pte_t pte_mkdirty(pte_t pte) {
  105. pte_val(pte) |= _PAGE_DIRTY; return pte; }
  106. static inline pte_t pte_mkyoung(pte_t pte) {
  107. pte_val(pte) |= _PAGE_ACCESSED; return pte; }
  108. static inline pte_t pte_mkspecial(pte_t pte) {
  109. pte_val(pte) |= _PAGE_SPECIAL; return pte; }
  110. static inline pte_t pte_mkhuge(pte_t pte) {
  111. return pte; }
  112. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  113. {
  114. pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot);
  115. return pte;
  116. }
  117. /* Insert a PTE, top-level function is out of line. It uses an inline
  118. * low level function in the respective pgtable-* files
  119. */
  120. extern void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  121. pte_t pte);
  122. /* This low level function performs the actual PTE insertion
  123. * Setting the PTE depends on the MMU type and other factors. It's
  124. * an horrible mess that I'm not going to try to clean up now but
  125. * I'm keeping it in one place rather than spread around
  126. */
  127. static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
  128. pte_t *ptep, pte_t pte, int percpu)
  129. {
  130. #if defined(CONFIG_PPC_STD_MMU_32) && defined(CONFIG_SMP) && !defined(CONFIG_PTE_64BIT)
  131. /* First case is 32-bit Hash MMU in SMP mode with 32-bit PTEs. We use the
  132. * helper pte_update() which does an atomic update. We need to do that
  133. * because a concurrent invalidation can clear _PAGE_HASHPTE. If it's a
  134. * per-CPU PTE such as a kmap_atomic, we do a simple update preserving
  135. * the hash bits instead (ie, same as the non-SMP case)
  136. */
  137. if (percpu)
  138. *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE)
  139. | (pte_val(pte) & ~_PAGE_HASHPTE));
  140. else
  141. pte_update(ptep, ~_PAGE_HASHPTE, pte_val(pte));
  142. #elif defined(CONFIG_PPC32) && defined(CONFIG_PTE_64BIT)
  143. /* Second case is 32-bit with 64-bit PTE. In this case, we
  144. * can just store as long as we do the two halves in the right order
  145. * with a barrier in between. This is possible because we take care,
  146. * in the hash code, to pre-invalidate if the PTE was already hashed,
  147. * which synchronizes us with any concurrent invalidation.
  148. * In the percpu case, we also fallback to the simple update preserving
  149. * the hash bits
  150. */
  151. if (percpu) {
  152. *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE)
  153. | (pte_val(pte) & ~_PAGE_HASHPTE));
  154. return;
  155. }
  156. #if _PAGE_HASHPTE != 0
  157. if (pte_val(*ptep) & _PAGE_HASHPTE)
  158. flush_hash_entry(mm, ptep, addr);
  159. #endif
  160. __asm__ __volatile__("\
  161. stw%U0%X0 %2,%0\n\
  162. eieio\n\
  163. stw%U0%X0 %L2,%1"
  164. : "=m" (*ptep), "=m" (*((unsigned char *)ptep+4))
  165. : "r" (pte) : "memory");
  166. #elif defined(CONFIG_PPC_STD_MMU_32)
  167. /* Third case is 32-bit hash table in UP mode, we need to preserve
  168. * the _PAGE_HASHPTE bit since we may not have invalidated the previous
  169. * translation in the hash yet (done in a subsequent flush_tlb_xxx())
  170. * and see we need to keep track that this PTE needs invalidating
  171. */
  172. *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE)
  173. | (pte_val(pte) & ~_PAGE_HASHPTE));
  174. #else
  175. /* Anything else just stores the PTE normally. That covers all 64-bit
  176. * cases, and 32-bit non-hash with 32-bit PTEs.
  177. */
  178. *ptep = pte;
  179. #endif
  180. }
  181. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  182. extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  183. pte_t *ptep, pte_t entry, int dirty);
  184. /*
  185. * Macro to mark a page protection value as "uncacheable".
  186. */
  187. #define _PAGE_CACHE_CTL (_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \
  188. _PAGE_WRITETHRU)
  189. #define pgprot_noncached(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  190. _PAGE_NO_CACHE | _PAGE_GUARDED))
  191. #define pgprot_noncached_wc(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  192. _PAGE_NO_CACHE))
  193. #define pgprot_cached(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  194. _PAGE_COHERENT))
  195. #define pgprot_cached_wthru(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  196. _PAGE_COHERENT | _PAGE_WRITETHRU))
  197. #define pgprot_cached_noncoherent(prot) \
  198. (__pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL))
  199. #define pgprot_writecombine pgprot_noncached_wc
  200. struct file;
  201. extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  202. unsigned long size, pgprot_t vma_prot);
  203. #define __HAVE_PHYS_MEM_ACCESS_PROT
  204. /*
  205. * ZERO_PAGE is a global shared page that is always zero: used
  206. * for zero-mapped memory areas etc..
  207. */
  208. extern unsigned long empty_zero_page[];
  209. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  210. extern pgd_t swapper_pg_dir[];
  211. void limit_zone_pfn(enum zone_type zone, unsigned long max_pfn);
  212. int dma_pfn_limit_to_zone(u64 pfn_limit);
  213. extern void paging_init(void);
  214. /*
  215. * kern_addr_valid is intended to indicate whether an address is a valid
  216. * kernel address. Most 32-bit archs define it as always true (like this)
  217. * but most 64-bit archs actually perform a test. What should we do here?
  218. */
  219. #define kern_addr_valid(addr) (1)
  220. #include <asm-generic/pgtable.h>
  221. /*
  222. * This gets called at the end of handling a page fault, when
  223. * the kernel has put a new PTE into the page table for the process.
  224. * We use it to ensure coherency between the i-cache and d-cache
  225. * for the page which has just been mapped in.
  226. * On machines which use an MMU hash table, we use this to put a
  227. * corresponding HPTE into the hash table ahead of time, instead of
  228. * waiting for the inevitable extra hash-table miss exception.
  229. */
  230. extern void update_mmu_cache(struct vm_area_struct *, unsigned long, pte_t *);
  231. extern int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  232. unsigned long end, int write,
  233. struct page **pages, int *nr);
  234. #ifndef CONFIG_TRANSPARENT_HUGEPAGE
  235. #define pmd_large(pmd) 0
  236. #define has_transparent_hugepage() 0
  237. #endif
  238. pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  239. unsigned *shift);
  240. static inline pte_t *lookup_linux_ptep(pgd_t *pgdir, unsigned long hva,
  241. unsigned long *pte_sizep)
  242. {
  243. pte_t *ptep;
  244. unsigned long ps = *pte_sizep;
  245. unsigned int shift;
  246. ptep = find_linux_pte_or_hugepte(pgdir, hva, &shift);
  247. if (!ptep)
  248. return NULL;
  249. if (shift)
  250. *pte_sizep = 1ul << shift;
  251. else
  252. *pte_sizep = PAGE_SIZE;
  253. if (ps > *pte_sizep)
  254. return NULL;
  255. return ptep;
  256. }
  257. #endif /* __ASSEMBLY__ */
  258. #endif /* __KERNEL__ */
  259. #endif /* _ASM_POWERPC_PGTABLE_H */