pgtable-ppc64.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #ifndef _ASM_POWERPC_PGTABLE_PPC64_H_
  2. #define _ASM_POWERPC_PGTABLE_PPC64_H_
  3. /*
  4. * This file contains the functions and defines necessary to modify and use
  5. * the ppc64 hashed page table.
  6. */
  7. #ifdef CONFIG_PPC_64K_PAGES
  8. #include <asm/pgtable-ppc64-64k.h>
  9. #else
  10. #include <asm/pgtable-ppc64-4k.h>
  11. #endif
  12. #define FIRST_USER_ADDRESS 0
  13. /*
  14. * Size of EA range mapped by our pagetables.
  15. */
  16. #define PGTABLE_EADDR_SIZE (PTE_INDEX_SIZE + PMD_INDEX_SIZE + \
  17. PUD_INDEX_SIZE + PGD_INDEX_SIZE + PAGE_SHIFT)
  18. #define PGTABLE_RANGE (ASM_CONST(1) << PGTABLE_EADDR_SIZE)
  19. /*
  20. * Define the address range of the kernel non-linear virtual area
  21. */
  22. #ifdef CONFIG_PPC_BOOK3E
  23. #define KERN_VIRT_START ASM_CONST(0x8000000000000000)
  24. #else
  25. #define KERN_VIRT_START ASM_CONST(0xD000000000000000)
  26. #endif
  27. #define KERN_VIRT_SIZE ASM_CONST(0x0000100000000000)
  28. /*
  29. * The vmalloc space starts at the beginning of that region, and
  30. * occupies half of it on hash CPUs and a quarter of it on Book3E
  31. * (we keep a quarter for the virtual memmap)
  32. */
  33. #define VMALLOC_START KERN_VIRT_START
  34. #ifdef CONFIG_PPC_BOOK3E
  35. #define VMALLOC_SIZE (KERN_VIRT_SIZE >> 2)
  36. #else
  37. #define VMALLOC_SIZE (KERN_VIRT_SIZE >> 1)
  38. #endif
  39. #define VMALLOC_END (VMALLOC_START + VMALLOC_SIZE)
  40. /*
  41. * The second half of the kernel virtual space is used for IO mappings,
  42. * it's itself carved into the PIO region (ISA and PHB IO space) and
  43. * the ioremap space
  44. *
  45. * ISA_IO_BASE = KERN_IO_START, 64K reserved area
  46. * PHB_IO_BASE = ISA_IO_BASE + 64K to ISA_IO_BASE + 2G, PHB IO spaces
  47. * IOREMAP_BASE = ISA_IO_BASE + 2G to VMALLOC_START + PGTABLE_RANGE
  48. */
  49. #define KERN_IO_START (KERN_VIRT_START + (KERN_VIRT_SIZE >> 1))
  50. #define FULL_IO_SIZE 0x80000000ul
  51. #define ISA_IO_BASE (KERN_IO_START)
  52. #define ISA_IO_END (KERN_IO_START + 0x10000ul)
  53. #define PHB_IO_BASE (ISA_IO_END)
  54. #define PHB_IO_END (KERN_IO_START + FULL_IO_SIZE)
  55. #define IOREMAP_BASE (PHB_IO_END)
  56. #define IOREMAP_END (KERN_VIRT_START + KERN_VIRT_SIZE)
  57. /*
  58. * Region IDs
  59. */
  60. #define REGION_SHIFT 60UL
  61. #define REGION_MASK (0xfUL << REGION_SHIFT)
  62. #define REGION_ID(ea) (((unsigned long)(ea)) >> REGION_SHIFT)
  63. #define VMALLOC_REGION_ID (REGION_ID(VMALLOC_START))
  64. #define KERNEL_REGION_ID (REGION_ID(PAGE_OFFSET))
  65. #define VMEMMAP_REGION_ID (0xfUL) /* Server only */
  66. #define USER_REGION_ID (0UL)
  67. /*
  68. * Defines the address of the vmemap area, in its own region on
  69. * hash table CPUs and after the vmalloc space on Book3E
  70. */
  71. #ifdef CONFIG_PPC_BOOK3E
  72. #define VMEMMAP_BASE VMALLOC_END
  73. #define VMEMMAP_END KERN_IO_START
  74. #else
  75. #define VMEMMAP_BASE (VMEMMAP_REGION_ID << REGION_SHIFT)
  76. #endif
  77. #define vmemmap ((struct page *)VMEMMAP_BASE)
  78. /*
  79. * Include the PTE bits definitions
  80. */
  81. #ifdef CONFIG_PPC_BOOK3S
  82. #include <asm/pte-hash64.h>
  83. #else
  84. #include <asm/pte-book3e.h>
  85. #endif
  86. #include <asm/pte-common.h>
  87. #ifdef CONFIG_PPC_MM_SLICES
  88. #define HAVE_ARCH_UNMAPPED_AREA
  89. #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  90. #endif /* CONFIG_PPC_MM_SLICES */
  91. #ifndef __ASSEMBLY__
  92. /*
  93. * This is the default implementation of various PTE accessors, it's
  94. * used in all cases except Book3S with 64K pages where we have a
  95. * concept of sub-pages
  96. */
  97. #ifndef __real_pte
  98. #ifdef STRICT_MM_TYPECHECKS
  99. #define __real_pte(e,p) ((real_pte_t){(e)})
  100. #define __rpte_to_pte(r) ((r).pte)
  101. #else
  102. #define __real_pte(e,p) (e)
  103. #define __rpte_to_pte(r) (__pte(r))
  104. #endif
  105. #define __rpte_to_hidx(r,index) (pte_val(__rpte_to_pte(r)) >> 12)
  106. #define pte_iterate_hashed_subpages(rpte, psize, va, index, shift) \
  107. do { \
  108. index = 0; \
  109. shift = mmu_psize_defs[psize].shift; \
  110. #define pte_iterate_hashed_end() } while(0)
  111. #ifdef CONFIG_PPC_HAS_HASH_64K
  112. #define pte_pagesize_index(mm, addr, pte) get_slice_psize(mm, addr)
  113. #else
  114. #define pte_pagesize_index(mm, addr, pte) MMU_PAGE_4K
  115. #endif
  116. #endif /* __real_pte */
  117. /* pte_clear moved to later in this file */
  118. #define PMD_BAD_BITS (PTE_TABLE_SIZE-1)
  119. #define PUD_BAD_BITS (PMD_TABLE_SIZE-1)
  120. #define pmd_set(pmdp, pmdval) (pmd_val(*(pmdp)) = (pmdval))
  121. #define pmd_none(pmd) (!pmd_val(pmd))
  122. #define pmd_bad(pmd) (!is_kernel_addr(pmd_val(pmd)) \
  123. || (pmd_val(pmd) & PMD_BAD_BITS))
  124. #define pmd_present(pmd) (pmd_val(pmd) != 0)
  125. #define pmd_clear(pmdp) (pmd_val(*(pmdp)) = 0)
  126. #define pmd_page_vaddr(pmd) (pmd_val(pmd) & ~PMD_MASKED_BITS)
  127. #define pmd_page(pmd) virt_to_page(pmd_page_vaddr(pmd))
  128. #define pud_set(pudp, pudval) (pud_val(*(pudp)) = (pudval))
  129. #define pud_none(pud) (!pud_val(pud))
  130. #define pud_bad(pud) (!is_kernel_addr(pud_val(pud)) \
  131. || (pud_val(pud) & PUD_BAD_BITS))
  132. #define pud_present(pud) (pud_val(pud) != 0)
  133. #define pud_clear(pudp) (pud_val(*(pudp)) = 0)
  134. #define pud_page_vaddr(pud) (pud_val(pud) & ~PUD_MASKED_BITS)
  135. #define pud_page(pud) virt_to_page(pud_page_vaddr(pud))
  136. #define pgd_set(pgdp, pudp) ({pgd_val(*(pgdp)) = (unsigned long)(pudp);})
  137. /*
  138. * Find an entry in a page-table-directory. We combine the address region
  139. * (the high order N bits) and the pgd portion of the address.
  140. */
  141. /* to avoid overflow in free_pgtables we don't use PTRS_PER_PGD here */
  142. #define pgd_index(address) (((address) >> (PGDIR_SHIFT)) & 0x1ff)
  143. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  144. #define pmd_offset(pudp,addr) \
  145. (((pmd_t *) pud_page_vaddr(*(pudp))) + (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)))
  146. #define pte_offset_kernel(dir,addr) \
  147. (((pte_t *) pmd_page_vaddr(*(dir))) + (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)))
  148. #define pte_offset_map(dir,addr) pte_offset_kernel((dir), (addr))
  149. #define pte_unmap(pte) do { } while(0)
  150. /* to find an entry in a kernel page-table-directory */
  151. /* This now only contains the vmalloc pages */
  152. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  153. extern void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
  154. pte_t *ptep, unsigned long pte, int huge);
  155. /* Atomic PTE updates */
  156. static inline unsigned long pte_update(struct mm_struct *mm,
  157. unsigned long addr,
  158. pte_t *ptep, unsigned long clr,
  159. int huge)
  160. {
  161. #ifdef PTE_ATOMIC_UPDATES
  162. unsigned long old, tmp;
  163. __asm__ __volatile__(
  164. "1: ldarx %0,0,%3 # pte_update\n\
  165. andi. %1,%0,%6\n\
  166. bne- 1b \n\
  167. andc %1,%0,%4 \n\
  168. stdcx. %1,0,%3 \n\
  169. bne- 1b"
  170. : "=&r" (old), "=&r" (tmp), "=m" (*ptep)
  171. : "r" (ptep), "r" (clr), "m" (*ptep), "i" (_PAGE_BUSY)
  172. : "cc" );
  173. #else
  174. unsigned long old = pte_val(*ptep);
  175. *ptep = __pte(old & ~clr);
  176. #endif
  177. /* huge pages use the old page table lock */
  178. if (!huge)
  179. assert_pte_locked(mm, addr);
  180. #ifdef CONFIG_PPC_STD_MMU_64
  181. if (old & _PAGE_HASHPTE)
  182. hpte_need_flush(mm, addr, ptep, old, huge);
  183. #endif
  184. return old;
  185. }
  186. static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
  187. unsigned long addr, pte_t *ptep)
  188. {
  189. unsigned long old;
  190. if ((pte_val(*ptep) & (_PAGE_ACCESSED | _PAGE_HASHPTE)) == 0)
  191. return 0;
  192. old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0);
  193. return (old & _PAGE_ACCESSED) != 0;
  194. }
  195. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  196. #define ptep_test_and_clear_young(__vma, __addr, __ptep) \
  197. ({ \
  198. int __r; \
  199. __r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
  200. __r; \
  201. })
  202. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  203. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
  204. pte_t *ptep)
  205. {
  206. if ((pte_val(*ptep) & _PAGE_RW) == 0)
  207. return;
  208. pte_update(mm, addr, ptep, _PAGE_RW, 0);
  209. }
  210. static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
  211. unsigned long addr, pte_t *ptep)
  212. {
  213. if ((pte_val(*ptep) & _PAGE_RW) == 0)
  214. return;
  215. pte_update(mm, addr, ptep, _PAGE_RW, 1);
  216. }
  217. /*
  218. * We currently remove entries from the hashtable regardless of whether
  219. * the entry was young or dirty. The generic routines only flush if the
  220. * entry was young or dirty which is not good enough.
  221. *
  222. * We should be more intelligent about this but for the moment we override
  223. * these functions and force a tlb flush unconditionally
  224. */
  225. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  226. #define ptep_clear_flush_young(__vma, __address, __ptep) \
  227. ({ \
  228. int __young = __ptep_test_and_clear_young((__vma)->vm_mm, __address, \
  229. __ptep); \
  230. __young; \
  231. })
  232. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  233. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  234. unsigned long addr, pte_t *ptep)
  235. {
  236. unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0);
  237. return __pte(old);
  238. }
  239. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  240. pte_t * ptep)
  241. {
  242. pte_update(mm, addr, ptep, ~0UL, 0);
  243. }
  244. /* Set the dirty and/or accessed bits atomically in a linux PTE, this
  245. * function doesn't need to flush the hash entry
  246. */
  247. static inline void __ptep_set_access_flags(pte_t *ptep, pte_t entry)
  248. {
  249. unsigned long bits = pte_val(entry) &
  250. (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);
  251. #ifdef PTE_ATOMIC_UPDATES
  252. unsigned long old, tmp;
  253. __asm__ __volatile__(
  254. "1: ldarx %0,0,%4\n\
  255. andi. %1,%0,%6\n\
  256. bne- 1b \n\
  257. or %0,%3,%0\n\
  258. stdcx. %0,0,%4\n\
  259. bne- 1b"
  260. :"=&r" (old), "=&r" (tmp), "=m" (*ptep)
  261. :"r" (bits), "r" (ptep), "m" (*ptep), "i" (_PAGE_BUSY)
  262. :"cc");
  263. #else
  264. unsigned long old = pte_val(*ptep);
  265. *ptep = __pte(old | bits);
  266. #endif
  267. }
  268. #define __HAVE_ARCH_PTE_SAME
  269. #define pte_same(A,B) (((pte_val(A) ^ pte_val(B)) & ~_PAGE_HPTEFLAGS) == 0)
  270. #define pte_ERROR(e) \
  271. printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
  272. #define pmd_ERROR(e) \
  273. printk("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
  274. #define pgd_ERROR(e) \
  275. printk("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
  276. /* Encode and de-code a swap entry */
  277. #define __swp_type(entry) (((entry).val >> 1) & 0x3f)
  278. #define __swp_offset(entry) ((entry).val >> 8)
  279. #define __swp_entry(type, offset) ((swp_entry_t){((type)<< 1)|((offset)<<8)})
  280. #define __pte_to_swp_entry(pte) ((swp_entry_t){pte_val(pte) >> PTE_RPN_SHIFT})
  281. #define __swp_entry_to_pte(x) ((pte_t) { (x).val << PTE_RPN_SHIFT })
  282. #define pte_to_pgoff(pte) (pte_val(pte) >> PTE_RPN_SHIFT)
  283. #define pgoff_to_pte(off) ((pte_t) {((off) << PTE_RPN_SHIFT)|_PAGE_FILE})
  284. #define PTE_FILE_MAX_BITS (BITS_PER_LONG - PTE_RPN_SHIFT)
  285. void pgtable_cache_add(unsigned shift, void (*ctor)(void *));
  286. void pgtable_cache_init(void);
  287. /*
  288. * find_linux_pte returns the address of a linux pte for a given
  289. * effective address and directory. If not found, it returns zero.
  290. */
  291. static inline pte_t *find_linux_pte(pgd_t *pgdir, unsigned long ea)
  292. {
  293. pgd_t *pg;
  294. pud_t *pu;
  295. pmd_t *pm;
  296. pte_t *pt = NULL;
  297. pg = pgdir + pgd_index(ea);
  298. if (!pgd_none(*pg)) {
  299. pu = pud_offset(pg, ea);
  300. if (!pud_none(*pu)) {
  301. pm = pmd_offset(pu, ea);
  302. if (pmd_present(*pm))
  303. pt = pte_offset_kernel(pm, ea);
  304. }
  305. }
  306. return pt;
  307. }
  308. #ifdef CONFIG_HUGETLB_PAGE
  309. pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  310. unsigned *shift);
  311. #else
  312. static inline pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  313. unsigned *shift)
  314. {
  315. if (shift)
  316. *shift = 0;
  317. return find_linux_pte(pgdir, ea);
  318. }
  319. #endif /* !CONFIG_HUGETLB_PAGE */
  320. #endif /* __ASSEMBLY__ */
  321. #endif /* _ASM_POWERPC_PGTABLE_PPC64_H_ */