pgtable-ppc64.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #define pgd_index(address) (((address) >> (PGDIR_SHIFT)) & (PTRS_PER_PGD - 1))
  142. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  143. #define pmd_offset(pudp,addr) \
  144. (((pmd_t *) pud_page_vaddr(*(pudp))) + (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)))
  145. #define pte_offset_kernel(dir,addr) \
  146. (((pte_t *) pmd_page_vaddr(*(dir))) + (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)))
  147. #define pte_offset_map(dir,addr) pte_offset_kernel((dir), (addr))
  148. #define pte_unmap(pte) do { } while(0)
  149. /* to find an entry in a kernel page-table-directory */
  150. /* This now only contains the vmalloc pages */
  151. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  152. extern void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
  153. pte_t *ptep, unsigned long pte, int huge);
  154. /* Atomic PTE updates */
  155. static inline unsigned long pte_update(struct mm_struct *mm,
  156. unsigned long addr,
  157. pte_t *ptep, unsigned long clr,
  158. int huge)
  159. {
  160. #ifdef PTE_ATOMIC_UPDATES
  161. unsigned long old, tmp;
  162. __asm__ __volatile__(
  163. "1: ldarx %0,0,%3 # pte_update\n\
  164. andi. %1,%0,%6\n\
  165. bne- 1b \n\
  166. andc %1,%0,%4 \n\
  167. stdcx. %1,0,%3 \n\
  168. bne- 1b"
  169. : "=&r" (old), "=&r" (tmp), "=m" (*ptep)
  170. : "r" (ptep), "r" (clr), "m" (*ptep), "i" (_PAGE_BUSY)
  171. : "cc" );
  172. #else
  173. unsigned long old = pte_val(*ptep);
  174. *ptep = __pte(old & ~clr);
  175. #endif
  176. /* huge pages use the old page table lock */
  177. if (!huge)
  178. assert_pte_locked(mm, addr);
  179. #ifdef CONFIG_PPC_STD_MMU_64
  180. if (old & _PAGE_HASHPTE)
  181. hpte_need_flush(mm, addr, ptep, old, huge);
  182. #endif
  183. return old;
  184. }
  185. static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
  186. unsigned long addr, pte_t *ptep)
  187. {
  188. unsigned long old;
  189. if ((pte_val(*ptep) & (_PAGE_ACCESSED | _PAGE_HASHPTE)) == 0)
  190. return 0;
  191. old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0);
  192. return (old & _PAGE_ACCESSED) != 0;
  193. }
  194. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  195. #define ptep_test_and_clear_young(__vma, __addr, __ptep) \
  196. ({ \
  197. int __r; \
  198. __r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
  199. __r; \
  200. })
  201. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  202. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
  203. pte_t *ptep)
  204. {
  205. if ((pte_val(*ptep) & _PAGE_RW) == 0)
  206. return;
  207. pte_update(mm, addr, ptep, _PAGE_RW, 0);
  208. }
  209. static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
  210. unsigned long addr, pte_t *ptep)
  211. {
  212. if ((pte_val(*ptep) & _PAGE_RW) == 0)
  213. return;
  214. pte_update(mm, addr, ptep, _PAGE_RW, 1);
  215. }
  216. /*
  217. * We currently remove entries from the hashtable regardless of whether
  218. * the entry was young or dirty. The generic routines only flush if the
  219. * entry was young or dirty which is not good enough.
  220. *
  221. * We should be more intelligent about this but for the moment we override
  222. * these functions and force a tlb flush unconditionally
  223. */
  224. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  225. #define ptep_clear_flush_young(__vma, __address, __ptep) \
  226. ({ \
  227. int __young = __ptep_test_and_clear_young((__vma)->vm_mm, __address, \
  228. __ptep); \
  229. __young; \
  230. })
  231. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  232. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  233. unsigned long addr, pte_t *ptep)
  234. {
  235. unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0);
  236. return __pte(old);
  237. }
  238. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  239. pte_t * ptep)
  240. {
  241. pte_update(mm, addr, ptep, ~0UL, 0);
  242. }
  243. /* Set the dirty and/or accessed bits atomically in a linux PTE, this
  244. * function doesn't need to flush the hash entry
  245. */
  246. static inline void __ptep_set_access_flags(pte_t *ptep, pte_t entry)
  247. {
  248. unsigned long bits = pte_val(entry) &
  249. (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);
  250. #ifdef PTE_ATOMIC_UPDATES
  251. unsigned long old, tmp;
  252. __asm__ __volatile__(
  253. "1: ldarx %0,0,%4\n\
  254. andi. %1,%0,%6\n\
  255. bne- 1b \n\
  256. or %0,%3,%0\n\
  257. stdcx. %0,0,%4\n\
  258. bne- 1b"
  259. :"=&r" (old), "=&r" (tmp), "=m" (*ptep)
  260. :"r" (bits), "r" (ptep), "m" (*ptep), "i" (_PAGE_BUSY)
  261. :"cc");
  262. #else
  263. unsigned long old = pte_val(*ptep);
  264. *ptep = __pte(old | bits);
  265. #endif
  266. }
  267. #define __HAVE_ARCH_PTE_SAME
  268. #define pte_same(A,B) (((pte_val(A) ^ pte_val(B)) & ~_PAGE_HPTEFLAGS) == 0)
  269. #define pte_ERROR(e) \
  270. printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
  271. #define pmd_ERROR(e) \
  272. printk("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
  273. #define pgd_ERROR(e) \
  274. printk("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
  275. /* Encode and de-code a swap entry */
  276. #define __swp_type(entry) (((entry).val >> 1) & 0x3f)
  277. #define __swp_offset(entry) ((entry).val >> 8)
  278. #define __swp_entry(type, offset) ((swp_entry_t){((type)<< 1)|((offset)<<8)})
  279. #define __pte_to_swp_entry(pte) ((swp_entry_t){pte_val(pte) >> PTE_RPN_SHIFT})
  280. #define __swp_entry_to_pte(x) ((pte_t) { (x).val << PTE_RPN_SHIFT })
  281. #define pte_to_pgoff(pte) (pte_val(pte) >> PTE_RPN_SHIFT)
  282. #define pgoff_to_pte(off) ((pte_t) {((off) << PTE_RPN_SHIFT)|_PAGE_FILE})
  283. #define PTE_FILE_MAX_BITS (BITS_PER_LONG - PTE_RPN_SHIFT)
  284. void pgtable_cache_add(unsigned shift, void (*ctor)(void *));
  285. void pgtable_cache_init(void);
  286. /*
  287. * find_linux_pte returns the address of a linux pte for a given
  288. * effective address and directory. If not found, it returns zero.
  289. */
  290. static inline pte_t *find_linux_pte(pgd_t *pgdir, unsigned long ea)
  291. {
  292. pgd_t *pg;
  293. pud_t *pu;
  294. pmd_t *pm;
  295. pte_t *pt = NULL;
  296. pg = pgdir + pgd_index(ea);
  297. if (!pgd_none(*pg)) {
  298. pu = pud_offset(pg, ea);
  299. if (!pud_none(*pu)) {
  300. pm = pmd_offset(pu, ea);
  301. if (pmd_present(*pm))
  302. pt = pte_offset_kernel(pm, ea);
  303. }
  304. }
  305. return pt;
  306. }
  307. #ifdef CONFIG_HUGETLB_PAGE
  308. pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  309. unsigned *shift);
  310. #else
  311. static inline pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  312. unsigned *shift)
  313. {
  314. if (shift)
  315. *shift = 0;
  316. return find_linux_pte(pgdir, ea);
  317. }
  318. #endif /* !CONFIG_HUGETLB_PAGE */
  319. #endif /* __ASSEMBLY__ */
  320. #endif /* _ASM_POWERPC_PGTABLE_PPC64_H_ */