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