pgtable-ppc64.h 11 KB

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