xref: /linux/arch/powerpc/include/asm/book3s/64/pgtable.h (revision b0412ea94bcbd08dc1e61043dfdd9c33272cec48)
1 #ifndef _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
2 #define _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
3 /*
4  * This file contains the functions and defines necessary to modify and use
5  * the ppc64 hashed page table.
6  */
7 
8 #include <asm/book3s/64/hash.h>
9 #include <asm/barrier.h>
10 
11 
12 /*
13  * Size of EA range mapped by our pagetables.
14  */
15 #define PGTABLE_EADDR_SIZE (PTE_INDEX_SIZE + PMD_INDEX_SIZE + \
16 			    PUD_INDEX_SIZE + PGD_INDEX_SIZE + PAGE_SHIFT)
17 #define PGTABLE_RANGE (ASM_CONST(1) << PGTABLE_EADDR_SIZE)
18 
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 #define KERN_VIRT_START ASM_CONST(0xD000000000000000)
28 #define KERN_VIRT_SIZE	ASM_CONST(0x0000100000000000)
29 /*
30  * The vmalloc space starts at the beginning of that region, and
31  * occupies half of it on hash CPUs and a quarter of it on Book3E
32  * (we keep a quarter for the virtual memmap)
33  */
34 #define VMALLOC_START	KERN_VIRT_START
35 #define VMALLOC_SIZE	(KERN_VIRT_SIZE >> 1)
36 #define VMALLOC_END	(VMALLOC_START + VMALLOC_SIZE)
37 /*
38  * The second half of the kernel virtual space is used for IO mappings,
39  * it's itself carved into the PIO region (ISA and PHB IO space) and
40  * the ioremap space
41  *
42  *  ISA_IO_BASE = KERN_IO_START, 64K reserved area
43  *  PHB_IO_BASE = ISA_IO_BASE + 64K to ISA_IO_BASE + 2G, PHB IO spaces
44  * IOREMAP_BASE = ISA_IO_BASE + 2G to VMALLOC_START + PGTABLE_RANGE
45  */
46 #define KERN_IO_START	(KERN_VIRT_START + (KERN_VIRT_SIZE >> 1))
47 #define FULL_IO_SIZE	0x80000000ul
48 #define  ISA_IO_BASE	(KERN_IO_START)
49 #define  ISA_IO_END	(KERN_IO_START + 0x10000ul)
50 #define  PHB_IO_BASE	(ISA_IO_END)
51 #define  PHB_IO_END	(KERN_IO_START + FULL_IO_SIZE)
52 #define IOREMAP_BASE	(PHB_IO_END)
53 #define IOREMAP_END	(KERN_VIRT_START + KERN_VIRT_SIZE)
54 
55 /*
56  * Region IDs
57  */
58 #define REGION_SHIFT		60UL
59 #define REGION_MASK		(0xfUL << REGION_SHIFT)
60 #define REGION_ID(ea)		(((unsigned long)(ea)) >> REGION_SHIFT)
61 
62 #define VMALLOC_REGION_ID	(REGION_ID(VMALLOC_START))
63 #define KERNEL_REGION_ID	(REGION_ID(PAGE_OFFSET))
64 #define VMEMMAP_REGION_ID	(0xfUL)	/* Server only */
65 #define USER_REGION_ID		(0UL)
66 
67 /*
68  * Defines the address of the vmemap area, in its own region on
69  * hash table CPUs.
70  */
71 #define VMEMMAP_BASE		(VMEMMAP_REGION_ID << REGION_SHIFT)
72 #define vmemmap			((struct page *)VMEMMAP_BASE)
73 
74 
75 #ifdef CONFIG_PPC_MM_SLICES
76 #define HAVE_ARCH_UNMAPPED_AREA
77 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
78 #endif /* CONFIG_PPC_MM_SLICES */
79 
80 /*
81  * THP pages can't be special. So use the _PAGE_SPECIAL
82  */
83 #define _PAGE_SPLITTING _PAGE_SPECIAL
84 
85 /*
86  * We need to differentiate between explicit huge page and THP huge
87  * page, since THP huge page also need to track real subpage details
88  */
89 #define _PAGE_THP_HUGE  _PAGE_4K_PFN
90 
91 /*
92  * set of bits not changed in pmd_modify.
93  */
94 #define _HPAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HPTEFLAGS |		\
95 			 _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SPLITTING | \
96 			 _PAGE_THP_HUGE)
97 #define _PTE_NONE_MASK	_PAGE_HPTEFLAGS
98 /*
99  * The mask convered by the RPN must be a ULL on 32-bit platforms with
100  * 64-bit PTEs
101  */
102 #define PTE_RPN_MASK	(~((1UL << PTE_RPN_SHIFT) - 1))
103 /*
104  * _PAGE_CHG_MASK masks of bits that are to be preserved across
105  * pgprot changes
106  */
107 #define _PAGE_CHG_MASK	(PTE_RPN_MASK | _PAGE_HPTEFLAGS | _PAGE_DIRTY | \
108 			 _PAGE_ACCESSED | _PAGE_SPECIAL)
109 /*
110  * Mask of bits returned by pte_pgprot()
111  */
112 #define PAGE_PROT_BITS	(_PAGE_GUARDED | _PAGE_COHERENT | _PAGE_NO_CACHE | \
113 			 _PAGE_WRITETHRU | _PAGE_4K_PFN | \
114 			 _PAGE_USER | _PAGE_ACCESSED |  \
115 			 _PAGE_RW |  _PAGE_DIRTY | _PAGE_EXEC)
116 /*
117  * We define 2 sets of base prot bits, one for basic pages (ie,
118  * cacheable kernel and user pages) and one for non cacheable
119  * pages. We always set _PAGE_COHERENT when SMP is enabled or
120  * the processor might need it for DMA coherency.
121  */
122 #define _PAGE_BASE_NC	(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_PSIZE)
123 #define _PAGE_BASE	(_PAGE_BASE_NC | _PAGE_COHERENT)
124 
125 /* Permission masks used to generate the __P and __S table,
126  *
127  * Note:__pgprot is defined in arch/powerpc/include/asm/page.h
128  *
129  * Write permissions imply read permissions for now (we could make write-only
130  * pages on BookE but we don't bother for now). Execute permission control is
131  * possible on platforms that define _PAGE_EXEC
132  *
133  * Note due to the way vm flags are laid out, the bits are XWR
134  */
135 #define PAGE_NONE	__pgprot(_PAGE_BASE)
136 #define PAGE_SHARED	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW)
137 #define PAGE_SHARED_X	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW | \
138 				 _PAGE_EXEC)
139 #define PAGE_COPY	__pgprot(_PAGE_BASE | _PAGE_USER )
140 #define PAGE_COPY_X	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC)
141 #define PAGE_READONLY	__pgprot(_PAGE_BASE | _PAGE_USER )
142 #define PAGE_READONLY_X	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC)
143 
144 #define __P000	PAGE_NONE
145 #define __P001	PAGE_READONLY
146 #define __P010	PAGE_COPY
147 #define __P011	PAGE_COPY
148 #define __P100	PAGE_READONLY_X
149 #define __P101	PAGE_READONLY_X
150 #define __P110	PAGE_COPY_X
151 #define __P111	PAGE_COPY_X
152 
153 #define __S000	PAGE_NONE
154 #define __S001	PAGE_READONLY
155 #define __S010	PAGE_SHARED
156 #define __S011	PAGE_SHARED
157 #define __S100	PAGE_READONLY_X
158 #define __S101	PAGE_READONLY_X
159 #define __S110	PAGE_SHARED_X
160 #define __S111	PAGE_SHARED_X
161 
162 /* Permission masks used for kernel mappings */
163 #define PAGE_KERNEL	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)
164 #define PAGE_KERNEL_NC	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
165 				 _PAGE_NO_CACHE)
166 #define PAGE_KERNEL_NCG	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
167 				 _PAGE_NO_CACHE | _PAGE_GUARDED)
168 #define PAGE_KERNEL_X	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)
169 #define PAGE_KERNEL_RO	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)
170 #define PAGE_KERNEL_ROX	__pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
171 
172 /* Protection used for kernel text. We want the debuggers to be able to
173  * set breakpoints anywhere, so don't write protect the kernel text
174  * on platforms where such control is possible.
175  */
176 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\
177 	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
178 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
179 #else
180 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
181 #endif
182 
183 /* Make modules code happy. We don't set RO yet */
184 #define PAGE_KERNEL_EXEC	PAGE_KERNEL_X
185 
186 /*
187  * Don't just check for any non zero bits in __PAGE_USER, since for book3e
188  * and PTE_64BIT, PAGE_KERNEL_X contains _PAGE_BAP_SR which is also in
189  * _PAGE_USER.  Need to explicitly match _PAGE_BAP_UR bit in that case too.
190  */
191 #define pte_user(val)		((val & _PAGE_USER) == _PAGE_USER)
192 
193 /* Advertise special mapping type for AGP */
194 #define PAGE_AGP		(PAGE_KERNEL_NC)
195 #define HAVE_PAGE_AGP
196 
197 /* Advertise support for _PAGE_SPECIAL */
198 #define __HAVE_ARCH_PTE_SPECIAL
199 
200 #ifndef __ASSEMBLY__
201 
202 /*
203  * This is the default implementation of various PTE accessors, it's
204  * used in all cases except Book3S with 64K pages where we have a
205  * concept of sub-pages
206  */
207 #ifndef __real_pte
208 
209 #ifdef CONFIG_STRICT_MM_TYPECHECKS
210 #define __real_pte(e,p)		((real_pte_t){(e)})
211 #define __rpte_to_pte(r)	((r).pte)
212 #else
213 #define __real_pte(e,p)		(e)
214 #define __rpte_to_pte(r)	(__pte(r))
215 #endif
216 #define __rpte_to_hidx(r,index)	(pte_val(__rpte_to_pte(r)) >> 12)
217 
218 #define pte_iterate_hashed_subpages(rpte, psize, va, index, shift)       \
219 	do {							         \
220 		index = 0;					         \
221 		shift = mmu_psize_defs[psize].shift;		         \
222 
223 #define pte_iterate_hashed_end() } while(0)
224 
225 /*
226  * We expect this to be called only for user addresses or kernel virtual
227  * addresses other than the linear mapping.
228  */
229 #define pte_pagesize_index(mm, addr, pte)	MMU_PAGE_4K
230 
231 #endif /* __real_pte */
232 
233 
234 /* pte_clear moved to later in this file */
235 
236 #define PMD_BAD_BITS		(PTE_TABLE_SIZE-1)
237 #define PUD_BAD_BITS		(PMD_TABLE_SIZE-1)
238 
239 #define pmd_set(pmdp, pmdval)	(pmd_val(*(pmdp)) = (pmdval))
240 #define pmd_none(pmd)		(!pmd_val(pmd))
241 #define	pmd_bad(pmd)		(!is_kernel_addr(pmd_val(pmd)) \
242 				 || (pmd_val(pmd) & PMD_BAD_BITS))
243 #define	pmd_present(pmd)	(!pmd_none(pmd))
244 #define	pmd_clear(pmdp)		(pmd_val(*(pmdp)) = 0)
245 #define pmd_page_vaddr(pmd)	(pmd_val(pmd) & ~PMD_MASKED_BITS)
246 extern struct page *pmd_page(pmd_t pmd);
247 
248 #define pud_set(pudp, pudval)	(pud_val(*(pudp)) = (pudval))
249 #define pud_none(pud)		(!pud_val(pud))
250 #define	pud_bad(pud)		(!is_kernel_addr(pud_val(pud)) \
251 				 || (pud_val(pud) & PUD_BAD_BITS))
252 #define pud_present(pud)	(pud_val(pud) != 0)
253 #define pud_clear(pudp)		(pud_val(*(pudp)) = 0)
254 #define pud_page_vaddr(pud)	(pud_val(pud) & ~PUD_MASKED_BITS)
255 
256 extern struct page *pud_page(pud_t pud);
257 
258 static inline pte_t pud_pte(pud_t pud)
259 {
260 	return __pte(pud_val(pud));
261 }
262 
263 static inline pud_t pte_pud(pte_t pte)
264 {
265 	return __pud(pte_val(pte));
266 }
267 #define pud_write(pud)		pte_write(pud_pte(pud))
268 #define pgd_set(pgdp, pudp)	({pgd_val(*(pgdp)) = (unsigned long)(pudp);})
269 #define pgd_write(pgd)		pte_write(pgd_pte(pgd))
270 
271 /*
272  * Find an entry in a page-table-directory.  We combine the address region
273  * (the high order N bits) and the pgd portion of the address.
274  */
275 #define pgd_index(address) (((address) >> (PGDIR_SHIFT)) & (PTRS_PER_PGD - 1))
276 
277 #define pgd_offset(mm, address)	 ((mm)->pgd + pgd_index(address))
278 
279 #define pmd_offset(pudp,addr) \
280   (((pmd_t *) pud_page_vaddr(*(pudp))) + (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)))
281 
282 #define pte_offset_kernel(dir,addr) \
283   (((pte_t *) pmd_page_vaddr(*(dir))) + (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)))
284 
285 #define pte_offset_map(dir,addr)	pte_offset_kernel((dir), (addr))
286 #define pte_unmap(pte)			do { } while(0)
287 
288 /* to find an entry in a kernel page-table-directory */
289 /* This now only contains the vmalloc pages */
290 #define pgd_offset_k(address) pgd_offset(&init_mm, address)
291 extern void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
292 			    pte_t *ptep, unsigned long pte, int huge);
293 
294 /* Atomic PTE updates */
295 static inline unsigned long pte_update(struct mm_struct *mm,
296 				       unsigned long addr,
297 				       pte_t *ptep, unsigned long clr,
298 				       unsigned long set,
299 				       int huge)
300 {
301 	unsigned long old, tmp;
302 
303 	__asm__ __volatile__(
304 	"1:	ldarx	%0,0,%3		# pte_update\n\
305 	andi.	%1,%0,%6\n\
306 	bne-	1b \n\
307 	andc	%1,%0,%4 \n\
308 	or	%1,%1,%7\n\
309 	stdcx.	%1,0,%3 \n\
310 	bne-	1b"
311 	: "=&r" (old), "=&r" (tmp), "=m" (*ptep)
312 	: "r" (ptep), "r" (clr), "m" (*ptep), "i" (_PAGE_BUSY), "r" (set)
313 	: "cc" );
314 	/* huge pages use the old page table lock */
315 	if (!huge)
316 		assert_pte_locked(mm, addr);
317 
318 	if (old & _PAGE_HASHPTE)
319 		hpte_need_flush(mm, addr, ptep, old, huge);
320 
321 	return old;
322 }
323 
324 static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
325 					      unsigned long addr, pte_t *ptep)
326 {
327 	unsigned long old;
328 
329 	if ((pte_val(*ptep) & (_PAGE_ACCESSED | _PAGE_HASHPTE)) == 0)
330 		return 0;
331 	old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);
332 	return (old & _PAGE_ACCESSED) != 0;
333 }
334 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
335 #define ptep_test_and_clear_young(__vma, __addr, __ptep)		   \
336 ({									   \
337 	int __r;							   \
338 	__r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
339 	__r;								   \
340 })
341 
342 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
343 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
344 				      pte_t *ptep)
345 {
346 
347 	if ((pte_val(*ptep) & _PAGE_RW) == 0)
348 		return;
349 
350 	pte_update(mm, addr, ptep, _PAGE_RW, 0, 0);
351 }
352 
353 static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
354 					   unsigned long addr, pte_t *ptep)
355 {
356 	if ((pte_val(*ptep) & _PAGE_RW) == 0)
357 		return;
358 
359 	pte_update(mm, addr, ptep, _PAGE_RW, 0, 1);
360 }
361 
362 /*
363  * We currently remove entries from the hashtable regardless of whether
364  * the entry was young or dirty. The generic routines only flush if the
365  * entry was young or dirty which is not good enough.
366  *
367  * We should be more intelligent about this but for the moment we override
368  * these functions and force a tlb flush unconditionally
369  */
370 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
371 #define ptep_clear_flush_young(__vma, __address, __ptep)		\
372 ({									\
373 	int __young = __ptep_test_and_clear_young((__vma)->vm_mm, __address, \
374 						  __ptep);		\
375 	__young;							\
376 })
377 
378 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
379 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
380 				       unsigned long addr, pte_t *ptep)
381 {
382 	unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
383 	return __pte(old);
384 }
385 
386 static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
387 			     pte_t * ptep)
388 {
389 	pte_update(mm, addr, ptep, ~0UL, 0, 0);
390 }
391 
392 
393 /* Set the dirty and/or accessed bits atomically in a linux PTE, this
394  * function doesn't need to flush the hash entry
395  */
396 static inline void __ptep_set_access_flags(pte_t *ptep, pte_t entry)
397 {
398 	unsigned long bits = pte_val(entry) &
399 		(_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);
400 
401 	unsigned long old, tmp;
402 
403 	__asm__ __volatile__(
404 	"1:	ldarx	%0,0,%4\n\
405 		andi.	%1,%0,%6\n\
406 		bne-	1b \n\
407 		or	%0,%3,%0\n\
408 		stdcx.	%0,0,%4\n\
409 		bne-	1b"
410 	:"=&r" (old), "=&r" (tmp), "=m" (*ptep)
411 	:"r" (bits), "r" (ptep), "m" (*ptep), "i" (_PAGE_BUSY)
412 	:"cc");
413 }
414 
415 #define __HAVE_ARCH_PTE_SAME
416 #define pte_same(A,B)	(((pte_val(A) ^ pte_val(B)) & ~_PAGE_HPTEFLAGS) == 0)
417 
418 #define pte_ERROR(e) \
419 	pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
420 #define pmd_ERROR(e) \
421 	pr_err("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
422 #define pgd_ERROR(e) \
423 	pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
424 
425 /* Encode and de-code a swap entry */
426 #define MAX_SWAPFILES_CHECK() do { \
427 	BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS); \
428 	/*							\
429 	 * Don't have overlapping bits with _PAGE_HPTEFLAGS	\
430 	 * We filter HPTEFLAGS on set_pte.			\
431 	 */							\
432 	BUILD_BUG_ON(_PAGE_HPTEFLAGS & (0x1f << _PAGE_BIT_SWAP_TYPE)); \
433 	} while (0)
434 /*
435  * on pte we don't need handle RADIX_TREE_EXCEPTIONAL_SHIFT;
436  */
437 #define SWP_TYPE_BITS 5
438 #define __swp_type(x)		(((x).val >> _PAGE_BIT_SWAP_TYPE) \
439 				& ((1UL << SWP_TYPE_BITS) - 1))
440 #define __swp_offset(x)		((x).val >> PTE_RPN_SHIFT)
441 #define __swp_entry(type, offset)	((swp_entry_t) { \
442 					((type) << _PAGE_BIT_SWAP_TYPE) \
443 					| ((offset) << PTE_RPN_SHIFT) })
444 
445 #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val((pte)) })
446 #define __swp_entry_to_pte(x)		__pte((x).val)
447 
448 void pgtable_cache_add(unsigned shift, void (*ctor)(void *));
449 void pgtable_cache_init(void);
450 
451 /*
452  * The linux hugepage PMD now include the pmd entries followed by the address
453  * to the stashed pgtable_t. The stashed pgtable_t contains the hpte bits.
454  * [ 1 bit secondary | 3 bit hidx | 1 bit valid | 000]. We use one byte per
455  * each HPTE entry. With 16MB hugepage and 64K HPTE we need 256 entries and
456  * with 4K HPTE we need 4096 entries. Both will fit in a 4K pgtable_t.
457  *
458  * The last three bits are intentionally left to zero. This memory location
459  * are also used as normal page PTE pointers. So if we have any pointers
460  * left around while we collapse a hugepage, we need to make sure
461  * _PAGE_PRESENT bit of that is zero when we look at them
462  */
463 static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index)
464 {
465 	return (hpte_slot_array[index] >> 3) & 0x1;
466 }
467 
468 static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array,
469 					   int index)
470 {
471 	return hpte_slot_array[index] >> 4;
472 }
473 
474 static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array,
475 					unsigned int index, unsigned int hidx)
476 {
477 	hpte_slot_array[index] = hidx << 4 | 0x1 << 3;
478 }
479 
480 struct page *realmode_pfn_to_page(unsigned long pfn);
481 
482 static inline char *get_hpte_slot_array(pmd_t *pmdp)
483 {
484 	/*
485 	 * The hpte hindex is stored in the pgtable whose address is in the
486 	 * second half of the PMD
487 	 *
488 	 * Order this load with the test for pmd_trans_huge in the caller
489 	 */
490 	smp_rmb();
491 	return *(char **)(pmdp + PTRS_PER_PMD);
492 
493 
494 }
495 
496 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
497 extern void hpte_do_hugepage_flush(struct mm_struct *mm, unsigned long addr,
498 				   pmd_t *pmdp, unsigned long old_pmd);
499 extern pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot);
500 extern pmd_t mk_pmd(struct page *page, pgprot_t pgprot);
501 extern pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot);
502 extern void set_pmd_at(struct mm_struct *mm, unsigned long addr,
503 		       pmd_t *pmdp, pmd_t pmd);
504 extern void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
505 				 pmd_t *pmd);
506 /*
507  *
508  * For core kernel code by design pmd_trans_huge is never run on any hugetlbfs
509  * page. The hugetlbfs page table walking and mangling paths are totally
510  * separated form the core VM paths and they're differentiated by
511  *  VM_HUGETLB being set on vm_flags well before any pmd_trans_huge could run.
512  *
513  * pmd_trans_huge() is defined as false at build time if
514  * CONFIG_TRANSPARENT_HUGEPAGE=n to optimize away code blocks at build
515  * time in such case.
516  *
517  * For ppc64 we need to differntiate from explicit hugepages from THP, because
518  * for THP we also track the subpage details at the pmd level. We don't do
519  * that for explicit huge pages.
520  *
521  */
522 static inline int pmd_trans_huge(pmd_t pmd)
523 {
524 	/*
525 	 * leaf pte for huge page, bottom two bits != 00
526 	 */
527 	return (pmd_val(pmd) & 0x3) && (pmd_val(pmd) & _PAGE_THP_HUGE);
528 }
529 
530 static inline int pmd_trans_splitting(pmd_t pmd)
531 {
532 	if (pmd_trans_huge(pmd))
533 		return pmd_val(pmd) & _PAGE_SPLITTING;
534 	return 0;
535 }
536 
537 extern int has_transparent_hugepage(void);
538 #else
539 static inline void hpte_do_hugepage_flush(struct mm_struct *mm,
540 					  unsigned long addr, pmd_t *pmdp,
541 					  unsigned long old_pmd)
542 {
543 
544 	WARN(1, "%s called with THP disabled\n", __func__);
545 }
546 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
547 
548 static inline int pmd_large(pmd_t pmd)
549 {
550 	/*
551 	 * leaf pte for huge page, bottom two bits != 00
552 	 */
553 	return ((pmd_val(pmd) & 0x3) != 0x0);
554 }
555 
556 static inline pte_t pmd_pte(pmd_t pmd)
557 {
558 	return __pte(pmd_val(pmd));
559 }
560 
561 static inline pmd_t pte_pmd(pte_t pte)
562 {
563 	return __pmd(pte_val(pte));
564 }
565 
566 static inline pte_t *pmdp_ptep(pmd_t *pmd)
567 {
568 	return (pte_t *)pmd;
569 }
570 
571 #define pmd_pfn(pmd)		pte_pfn(pmd_pte(pmd))
572 #define pmd_dirty(pmd)		pte_dirty(pmd_pte(pmd))
573 #define pmd_young(pmd)		pte_young(pmd_pte(pmd))
574 #define pmd_mkold(pmd)		pte_pmd(pte_mkold(pmd_pte(pmd)))
575 #define pmd_wrprotect(pmd)	pte_pmd(pte_wrprotect(pmd_pte(pmd)))
576 #define pmd_mkdirty(pmd)	pte_pmd(pte_mkdirty(pmd_pte(pmd)))
577 #define pmd_mkyoung(pmd)	pte_pmd(pte_mkyoung(pmd_pte(pmd)))
578 #define pmd_mkwrite(pmd)	pte_pmd(pte_mkwrite(pmd_pte(pmd)))
579 
580 #define __HAVE_ARCH_PMD_WRITE
581 #define pmd_write(pmd)		pte_write(pmd_pte(pmd))
582 
583 static inline pmd_t pmd_mkhuge(pmd_t pmd)
584 {
585 	/* Do nothing, mk_pmd() does this part.  */
586 	return pmd;
587 }
588 
589 static inline pmd_t pmd_mknotpresent(pmd_t pmd)
590 {
591 	pmd_val(pmd) &= ~_PAGE_PRESENT;
592 	return pmd;
593 }
594 
595 static inline pmd_t pmd_mksplitting(pmd_t pmd)
596 {
597 	pmd_val(pmd) |= _PAGE_SPLITTING;
598 	return pmd;
599 }
600 
601 #define __HAVE_ARCH_PMD_SAME
602 static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
603 {
604 	return (((pmd_val(pmd_a) ^ pmd_val(pmd_b)) & ~_PAGE_HPTEFLAGS) == 0);
605 }
606 
607 #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
608 extern int pmdp_set_access_flags(struct vm_area_struct *vma,
609 				 unsigned long address, pmd_t *pmdp,
610 				 pmd_t entry, int dirty);
611 
612 extern unsigned long pmd_hugepage_update(struct mm_struct *mm,
613 					 unsigned long addr,
614 					 pmd_t *pmdp,
615 					 unsigned long clr,
616 					 unsigned long set);
617 
618 static inline int __pmdp_test_and_clear_young(struct mm_struct *mm,
619 					      unsigned long addr, pmd_t *pmdp)
620 {
621 	unsigned long old;
622 
623 	if ((pmd_val(*pmdp) & (_PAGE_ACCESSED | _PAGE_HASHPTE)) == 0)
624 		return 0;
625 	old = pmd_hugepage_update(mm, addr, pmdp, _PAGE_ACCESSED, 0);
626 	return ((old & _PAGE_ACCESSED) != 0);
627 }
628 
629 #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
630 extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
631 				     unsigned long address, pmd_t *pmdp);
632 #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
633 extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
634 				  unsigned long address, pmd_t *pmdp);
635 
636 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
637 extern pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
638 				     unsigned long addr, pmd_t *pmdp);
639 
640 #define __HAVE_ARCH_PMDP_SET_WRPROTECT
641 static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr,
642 				      pmd_t *pmdp)
643 {
644 
645 	if ((pmd_val(*pmdp) & _PAGE_RW) == 0)
646 		return;
647 
648 	pmd_hugepage_update(mm, addr, pmdp, _PAGE_RW, 0);
649 }
650 
651 #define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
652 extern void pmdp_splitting_flush(struct vm_area_struct *vma,
653 				 unsigned long address, pmd_t *pmdp);
654 
655 extern pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
656 				 unsigned long address, pmd_t *pmdp);
657 #define pmdp_collapse_flush pmdp_collapse_flush
658 
659 #define __HAVE_ARCH_PGTABLE_DEPOSIT
660 extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
661 				       pgtable_t pgtable);
662 #define __HAVE_ARCH_PGTABLE_WITHDRAW
663 extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
664 
665 #define __HAVE_ARCH_PMDP_INVALIDATE
666 extern void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
667 			    pmd_t *pmdp);
668 
669 #define pmd_move_must_withdraw pmd_move_must_withdraw
670 struct spinlock;
671 static inline int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
672 					 struct spinlock *old_pmd_ptl)
673 {
674 	/*
675 	 * Archs like ppc64 use pgtable to store per pmd
676 	 * specific information. So when we switch the pmd,
677 	 * we should also withdraw and deposit the pgtable
678 	 */
679 	return true;
680 }
681 #endif /* __ASSEMBLY__ */
682 #endif /* _ASM_POWERPC_BOOK3S_64_PGTABLE_H_ */
683