1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
3 #define _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
4 
5 #include <asm-generic/pgtable-nop4d.h>
6 
7 #ifndef __ASSEMBLY__
8 #include <linux/mmdebug.h>
9 #include <linux/bug.h>
10 #endif
11 
12 /*
13  * Common bits between hash and Radix page table
14  */
15 #define _PAGE_BIT_SWAP_TYPE	0
16 
17 #define _PAGE_EXEC		0x00001 /* execute permission */
18 #define _PAGE_WRITE		0x00002 /* write access allowed */
19 #define _PAGE_READ		0x00004	/* read access allowed */
20 #define _PAGE_RW		(_PAGE_READ | _PAGE_WRITE)
21 #define _PAGE_RWX		(_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC)
22 #define _PAGE_PRIVILEGED	0x00008 /* kernel access only */
23 #define _PAGE_SAO		0x00010 /* Strong access order */
24 #define _PAGE_NON_IDEMPOTENT	0x00020 /* non idempotent memory */
25 #define _PAGE_TOLERANT		0x00030 /* tolerant memory, cache inhibited */
26 #define _PAGE_DIRTY		0x00080 /* C: page changed */
27 #define _PAGE_ACCESSED		0x00100 /* R: page referenced */
28 /*
29  * Software bits
30  */
31 #define _RPAGE_SW0		0x2000000000000000UL
32 #define _RPAGE_SW1		0x00800
33 #define _RPAGE_SW2		0x00400
34 #define _RPAGE_SW3		0x00200
35 #define _RPAGE_RSV1		0x00040UL
36 
37 #define _RPAGE_PKEY_BIT4	0x1000000000000000UL
38 #define _RPAGE_PKEY_BIT3	0x0800000000000000UL
39 #define _RPAGE_PKEY_BIT2	0x0400000000000000UL
40 #define _RPAGE_PKEY_BIT1	0x0200000000000000UL
41 #define _RPAGE_PKEY_BIT0	0x0100000000000000UL
42 
43 #define _PAGE_PTE		0x4000000000000000UL	/* distinguishes PTEs from pointers */
44 #define _PAGE_PRESENT		0x8000000000000000UL	/* pte contains a translation */
45 /*
46  * We need to mark a pmd pte invalid while splitting. We can do that by clearing
47  * the _PAGE_PRESENT bit. But then that will be taken as a swap pte. In order to
48  * differentiate between two use a SW field when invalidating.
49  *
50  * We do that temporary invalidate for regular pte entry in ptep_set_access_flags
51  *
52  * This is used only when _PAGE_PRESENT is cleared.
53  */
54 #define _PAGE_INVALID		_RPAGE_SW0
55 
56 /*
57  * Top and bottom bits of RPN which can be used by hash
58  * translation mode, because we expect them to be zero
59  * otherwise.
60  */
61 #define _RPAGE_RPN0		0x01000
62 #define _RPAGE_RPN1		0x02000
63 #define _RPAGE_RPN43		0x0080000000000000UL
64 #define _RPAGE_RPN42		0x0040000000000000UL
65 #define _RPAGE_RPN41		0x0020000000000000UL
66 
67 /* Max physical address bit as per radix table */
68 #define _RPAGE_PA_MAX		56
69 
70 /*
71  * Max physical address bit we will use for now.
72  *
73  * This is mostly a hardware limitation and for now Power9 has
74  * a 51 bit limit.
75  *
76  * This is different from the number of physical bit required to address
77  * the last byte of memory. That is defined by MAX_PHYSMEM_BITS.
78  * MAX_PHYSMEM_BITS is a linux limitation imposed by the maximum
79  * number of sections we can support (SECTIONS_SHIFT).
80  *
81  * This is different from Radix page table limitation above and
82  * should always be less than that. The limit is done such that
83  * we can overload the bits between _RPAGE_PA_MAX and _PAGE_PA_MAX
84  * for hash linux page table specific bits.
85  *
86  * In order to be compatible with future hardware generations we keep
87  * some offsets and limit this for now to 53
88  */
89 #define _PAGE_PA_MAX		53
90 
91 #define _PAGE_SOFT_DIRTY	_RPAGE_SW3 /* software: software dirty tracking */
92 #define _PAGE_SPECIAL		_RPAGE_SW2 /* software: special page */
93 #define _PAGE_DEVMAP		_RPAGE_SW1 /* software: ZONE_DEVICE page */
94 
95 /*
96  * Drivers request for cache inhibited pte mapping using _PAGE_NO_CACHE
97  * Instead of fixing all of them, add an alternate define which
98  * maps CI pte mapping.
99  */
100 #define _PAGE_NO_CACHE		_PAGE_TOLERANT
101 /*
102  * We support _RPAGE_PA_MAX bit real address in pte. On the linux side
103  * we are limited by _PAGE_PA_MAX. Clear everything above _PAGE_PA_MAX
104  * and every thing below PAGE_SHIFT;
105  */
106 #define PTE_RPN_MASK	(((1UL << _PAGE_PA_MAX) - 1) & (PAGE_MASK))
107 /*
108  * set of bits not changed in pmd_modify. Even though we have hash specific bits
109  * in here, on radix we expect them to be zero.
110  */
111 #define _HPAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HPTEFLAGS | _PAGE_DIRTY | \
112 			 _PAGE_ACCESSED | H_PAGE_THP_HUGE | _PAGE_PTE | \
113 			 _PAGE_SOFT_DIRTY | _PAGE_DEVMAP)
114 /*
115  * user access blocked by key
116  */
117 #define _PAGE_KERNEL_RW		(_PAGE_PRIVILEGED | _PAGE_RW | _PAGE_DIRTY)
118 #define _PAGE_KERNEL_RO		 (_PAGE_PRIVILEGED | _PAGE_READ)
119 #define _PAGE_KERNEL_RWX	(_PAGE_PRIVILEGED | _PAGE_DIRTY |	\
120 				 _PAGE_RW | _PAGE_EXEC)
121 /*
122  * _PAGE_CHG_MASK masks of bits that are to be preserved across
123  * pgprot changes
124  */
125 #define _PAGE_CHG_MASK	(PTE_RPN_MASK | _PAGE_HPTEFLAGS | _PAGE_DIRTY | \
126 			 _PAGE_ACCESSED | _PAGE_SPECIAL | _PAGE_PTE |	\
127 			 _PAGE_SOFT_DIRTY | _PAGE_DEVMAP)
128 
129 /*
130  * We define 2 sets of base prot bits, one for basic pages (ie,
131  * cacheable kernel and user pages) and one for non cacheable
132  * pages. We always set _PAGE_COHERENT when SMP is enabled or
133  * the processor might need it for DMA coherency.
134  */
135 #define _PAGE_BASE_NC	(_PAGE_PRESENT | _PAGE_ACCESSED)
136 #define _PAGE_BASE	(_PAGE_BASE_NC)
137 
138 /* Permission masks used to generate the __P and __S table,
139  *
140  * Note:__pgprot is defined in arch/powerpc/include/asm/page.h
141  *
142  * Write permissions imply read permissions for now (we could make write-only
143  * pages on BookE but we don't bother for now). Execute permission control is
144  * possible on platforms that define _PAGE_EXEC
145  */
146 #define PAGE_NONE	__pgprot(_PAGE_BASE | _PAGE_PRIVILEGED)
147 #define PAGE_SHARED	__pgprot(_PAGE_BASE | _PAGE_RW)
148 #define PAGE_SHARED_X	__pgprot(_PAGE_BASE | _PAGE_RW | _PAGE_EXEC)
149 #define PAGE_COPY	__pgprot(_PAGE_BASE | _PAGE_READ)
150 #define PAGE_COPY_X	__pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC)
151 #define PAGE_READONLY	__pgprot(_PAGE_BASE | _PAGE_READ)
152 #define PAGE_READONLY_X	__pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC)
153 
154 /* Permission masks used for kernel mappings */
155 #define PAGE_KERNEL	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)
156 #define PAGE_KERNEL_NC	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
157 				 _PAGE_TOLERANT)
158 #define PAGE_KERNEL_NCG	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
159 				 _PAGE_NON_IDEMPOTENT)
160 #define PAGE_KERNEL_X	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)
161 #define PAGE_KERNEL_RO	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)
162 #define PAGE_KERNEL_ROX	__pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
163 
164 /*
165  * Protection used for kernel text. We want the debuggers to be able to
166  * set breakpoints anywhere, so don't write protect the kernel text
167  * on platforms where such control is possible.
168  */
169 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) || \
170 	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
171 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
172 #else
173 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
174 #endif
175 
176 /* Make modules code happy. We don't set RO yet */
177 #define PAGE_KERNEL_EXEC	PAGE_KERNEL_X
178 #define PAGE_AGP		(PAGE_KERNEL_NC)
179 
180 #ifndef __ASSEMBLY__
181 /*
182  * page table defines
183  */
184 extern unsigned long __pte_index_size;
185 extern unsigned long __pmd_index_size;
186 extern unsigned long __pud_index_size;
187 extern unsigned long __pgd_index_size;
188 extern unsigned long __pud_cache_index;
189 #define PTE_INDEX_SIZE  __pte_index_size
190 #define PMD_INDEX_SIZE  __pmd_index_size
191 #define PUD_INDEX_SIZE  __pud_index_size
192 #define PGD_INDEX_SIZE  __pgd_index_size
193 /* pmd table use page table fragments */
194 #define PMD_CACHE_INDEX  0
195 #define PUD_CACHE_INDEX __pud_cache_index
196 /*
197  * Because of use of pte fragments and THP, size of page table
198  * are not always derived out of index size above.
199  */
200 extern unsigned long __pte_table_size;
201 extern unsigned long __pmd_table_size;
202 extern unsigned long __pud_table_size;
203 extern unsigned long __pgd_table_size;
204 #define PTE_TABLE_SIZE	__pte_table_size
205 #define PMD_TABLE_SIZE	__pmd_table_size
206 #define PUD_TABLE_SIZE	__pud_table_size
207 #define PGD_TABLE_SIZE	__pgd_table_size
208 
209 extern unsigned long __pmd_val_bits;
210 extern unsigned long __pud_val_bits;
211 extern unsigned long __pgd_val_bits;
212 #define PMD_VAL_BITS	__pmd_val_bits
213 #define PUD_VAL_BITS	__pud_val_bits
214 #define PGD_VAL_BITS	__pgd_val_bits
215 
216 extern unsigned long __pte_frag_nr;
217 #define PTE_FRAG_NR __pte_frag_nr
218 extern unsigned long __pte_frag_size_shift;
219 #define PTE_FRAG_SIZE_SHIFT __pte_frag_size_shift
220 #define PTE_FRAG_SIZE (1UL << PTE_FRAG_SIZE_SHIFT)
221 
222 extern unsigned long __pmd_frag_nr;
223 #define PMD_FRAG_NR __pmd_frag_nr
224 extern unsigned long __pmd_frag_size_shift;
225 #define PMD_FRAG_SIZE_SHIFT __pmd_frag_size_shift
226 #define PMD_FRAG_SIZE (1UL << PMD_FRAG_SIZE_SHIFT)
227 
228 #define PTRS_PER_PTE	(1 << PTE_INDEX_SIZE)
229 #define PTRS_PER_PMD	(1 << PMD_INDEX_SIZE)
230 #define PTRS_PER_PUD	(1 << PUD_INDEX_SIZE)
231 #define PTRS_PER_PGD	(1 << PGD_INDEX_SIZE)
232 
233 /* PMD_SHIFT determines what a second-level page table entry can map */
234 #define PMD_SHIFT	(PAGE_SHIFT + PTE_INDEX_SIZE)
235 #define PMD_SIZE	(1UL << PMD_SHIFT)
236 #define PMD_MASK	(~(PMD_SIZE-1))
237 
238 /* PUD_SHIFT determines what a third-level page table entry can map */
239 #define PUD_SHIFT	(PMD_SHIFT + PMD_INDEX_SIZE)
240 #define PUD_SIZE	(1UL << PUD_SHIFT)
241 #define PUD_MASK	(~(PUD_SIZE-1))
242 
243 /* PGDIR_SHIFT determines what a fourth-level page table entry can map */
244 #define PGDIR_SHIFT	(PUD_SHIFT + PUD_INDEX_SIZE)
245 #define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
246 #define PGDIR_MASK	(~(PGDIR_SIZE-1))
247 
248 /* Bits to mask out from a PMD to get to the PTE page */
249 #define PMD_MASKED_BITS		0xc0000000000000ffUL
250 /* Bits to mask out from a PUD to get to the PMD page */
251 #define PUD_MASKED_BITS		0xc0000000000000ffUL
252 /* Bits to mask out from a PGD to get to the PUD page */
253 #define P4D_MASKED_BITS		0xc0000000000000ffUL
254 
255 /*
256  * Used as an indicator for rcu callback functions
257  */
258 enum pgtable_index {
259 	PTE_INDEX = 0,
260 	PMD_INDEX,
261 	PUD_INDEX,
262 	PGD_INDEX,
263 	/*
264 	 * Below are used with 4k page size and hugetlb
265 	 */
266 	HTLB_16M_INDEX,
267 	HTLB_16G_INDEX,
268 };
269 
270 extern unsigned long __vmalloc_start;
271 extern unsigned long __vmalloc_end;
272 #define VMALLOC_START	__vmalloc_start
273 #define VMALLOC_END	__vmalloc_end
274 
275 static inline unsigned int ioremap_max_order(void)
276 {
277 	if (radix_enabled())
278 		return PUD_SHIFT;
279 	return 7 + PAGE_SHIFT; /* default from linux/vmalloc.h */
280 }
281 #define IOREMAP_MAX_ORDER ioremap_max_order()
282 
283 extern unsigned long __kernel_virt_start;
284 extern unsigned long __kernel_io_start;
285 extern unsigned long __kernel_io_end;
286 #define KERN_VIRT_START __kernel_virt_start
287 #define KERN_IO_START  __kernel_io_start
288 #define KERN_IO_END __kernel_io_end
289 
290 extern struct page *vmemmap;
291 extern unsigned long pci_io_base;
292 #endif /* __ASSEMBLY__ */
293 
294 #include <asm/book3s/64/hash.h>
295 #include <asm/book3s/64/radix.h>
296 
297 #ifdef CONFIG_PPC_64K_PAGES
298 #include <asm/book3s/64/pgtable-64k.h>
299 #else
300 #include <asm/book3s/64/pgtable-4k.h>
301 #endif
302 
303 #include <asm/barrier.h>
304 /*
305  * IO space itself carved into the PIO region (ISA and PHB IO space) and
306  * the ioremap space
307  *
308  *  ISA_IO_BASE = KERN_IO_START, 64K reserved area
309  *  PHB_IO_BASE = ISA_IO_BASE + 64K to ISA_IO_BASE + 2G, PHB IO spaces
310  * IOREMAP_BASE = ISA_IO_BASE + 2G to VMALLOC_START + PGTABLE_RANGE
311  */
312 #define FULL_IO_SIZE	0x80000000ul
313 #define  ISA_IO_BASE	(KERN_IO_START)
314 #define  ISA_IO_END	(KERN_IO_START + 0x10000ul)
315 #define  PHB_IO_BASE	(ISA_IO_END)
316 #define  PHB_IO_END	(KERN_IO_START + FULL_IO_SIZE)
317 #define IOREMAP_BASE	(PHB_IO_END)
318 #define IOREMAP_START	(ioremap_bot)
319 #define IOREMAP_END	(KERN_IO_END)
320 
321 /* Advertise special mapping type for AGP */
322 #define HAVE_PAGE_AGP
323 
324 #ifndef __ASSEMBLY__
325 
326 /*
327  * This is the default implementation of various PTE accessors, it's
328  * used in all cases except Book3S with 64K pages where we have a
329  * concept of sub-pages
330  */
331 #ifndef __real_pte
332 
333 #define __real_pte(e, p, o)		((real_pte_t){(e)})
334 #define __rpte_to_pte(r)	((r).pte)
335 #define __rpte_to_hidx(r,index)	(pte_val(__rpte_to_pte(r)) >> H_PAGE_F_GIX_SHIFT)
336 
337 #define pte_iterate_hashed_subpages(rpte, psize, va, index, shift)       \
338 	do {							         \
339 		index = 0;					         \
340 		shift = mmu_psize_defs[psize].shift;		         \
341 
342 #define pte_iterate_hashed_end() } while(0)
343 
344 /*
345  * We expect this to be called only for user addresses or kernel virtual
346  * addresses other than the linear mapping.
347  */
348 #define pte_pagesize_index(mm, addr, pte)	MMU_PAGE_4K
349 
350 #endif /* __real_pte */
351 
352 static inline unsigned long pte_update(struct mm_struct *mm, unsigned long addr,
353 				       pte_t *ptep, unsigned long clr,
354 				       unsigned long set, int huge)
355 {
356 	if (radix_enabled())
357 		return radix__pte_update(mm, addr, ptep, clr, set, huge);
358 	return hash__pte_update(mm, addr, ptep, clr, set, huge);
359 }
360 /*
361  * For hash even if we have _PAGE_ACCESSED = 0, we do a pte_update.
362  * We currently remove entries from the hashtable regardless of whether
363  * the entry was young or dirty.
364  *
365  * We should be more intelligent about this but for the moment we override
366  * these functions and force a tlb flush unconditionally
367  * For radix: H_PAGE_HASHPTE should be zero. Hence we can use the same
368  * function for both hash and radix.
369  */
370 static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
371 					      unsigned long addr, pte_t *ptep)
372 {
373 	unsigned long old;
374 
375 	if ((pte_raw(*ptep) & cpu_to_be64(_PAGE_ACCESSED | H_PAGE_HASHPTE)) == 0)
376 		return 0;
377 	old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);
378 	return (old & _PAGE_ACCESSED) != 0;
379 }
380 
381 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
382 #define ptep_test_and_clear_young(__vma, __addr, __ptep)	\
383 ({								\
384 	int __r;						\
385 	__r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
386 	__r;							\
387 })
388 
389 static inline int __pte_write(pte_t pte)
390 {
391 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_WRITE));
392 }
393 
394 #ifdef CONFIG_NUMA_BALANCING
395 #define pte_savedwrite pte_savedwrite
396 static inline bool pte_savedwrite(pte_t pte)
397 {
398 	/*
399 	 * Saved write ptes are prot none ptes that doesn't have
400 	 * privileged bit sit. We mark prot none as one which has
401 	 * present and pviliged bit set and RWX cleared. To mark
402 	 * protnone which used to have _PAGE_WRITE set we clear
403 	 * the privileged bit.
404 	 */
405 	return !(pte_raw(pte) & cpu_to_be64(_PAGE_RWX | _PAGE_PRIVILEGED));
406 }
407 #else
408 #define pte_savedwrite pte_savedwrite
409 static inline bool pte_savedwrite(pte_t pte)
410 {
411 	return false;
412 }
413 #endif
414 
415 static inline int pte_write(pte_t pte)
416 {
417 	return __pte_write(pte) || pte_savedwrite(pte);
418 }
419 
420 static inline int pte_read(pte_t pte)
421 {
422 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_READ));
423 }
424 
425 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
426 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
427 				      pte_t *ptep)
428 {
429 	if (__pte_write(*ptep))
430 		pte_update(mm, addr, ptep, _PAGE_WRITE, 0, 0);
431 	else if (unlikely(pte_savedwrite(*ptep)))
432 		pte_update(mm, addr, ptep, 0, _PAGE_PRIVILEGED, 0);
433 }
434 
435 #define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT
436 static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
437 					   unsigned long addr, pte_t *ptep)
438 {
439 	/*
440 	 * We should not find protnone for hugetlb, but this complete the
441 	 * interface.
442 	 */
443 	if (__pte_write(*ptep))
444 		pte_update(mm, addr, ptep, _PAGE_WRITE, 0, 1);
445 	else if (unlikely(pte_savedwrite(*ptep)))
446 		pte_update(mm, addr, ptep, 0, _PAGE_PRIVILEGED, 1);
447 }
448 
449 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
450 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
451 				       unsigned long addr, pte_t *ptep)
452 {
453 	unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
454 	return __pte(old);
455 }
456 
457 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
458 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
459 					    unsigned long addr,
460 					    pte_t *ptep, int full)
461 {
462 	if (full && radix_enabled()) {
463 		/*
464 		 * We know that this is a full mm pte clear and
465 		 * hence can be sure there is no parallel set_pte.
466 		 */
467 		return radix__ptep_get_and_clear_full(mm, addr, ptep, full);
468 	}
469 	return ptep_get_and_clear(mm, addr, ptep);
470 }
471 
472 
473 static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
474 			     pte_t * ptep)
475 {
476 	pte_update(mm, addr, ptep, ~0UL, 0, 0);
477 }
478 
479 static inline int pte_dirty(pte_t pte)
480 {
481 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_DIRTY));
482 }
483 
484 static inline int pte_young(pte_t pte)
485 {
486 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_ACCESSED));
487 }
488 
489 static inline int pte_special(pte_t pte)
490 {
491 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_SPECIAL));
492 }
493 
494 static inline bool pte_exec(pte_t pte)
495 {
496 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_EXEC));
497 }
498 
499 
500 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
501 static inline bool pte_soft_dirty(pte_t pte)
502 {
503 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_SOFT_DIRTY));
504 }
505 
506 static inline pte_t pte_mksoft_dirty(pte_t pte)
507 {
508 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_SOFT_DIRTY));
509 }
510 
511 static inline pte_t pte_clear_soft_dirty(pte_t pte)
512 {
513 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_SOFT_DIRTY));
514 }
515 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
516 
517 #ifdef CONFIG_NUMA_BALANCING
518 static inline int pte_protnone(pte_t pte)
519 {
520 	return (pte_raw(pte) & cpu_to_be64(_PAGE_PRESENT | _PAGE_PTE | _PAGE_RWX)) ==
521 		cpu_to_be64(_PAGE_PRESENT | _PAGE_PTE);
522 }
523 
524 #define pte_mk_savedwrite pte_mk_savedwrite
525 static inline pte_t pte_mk_savedwrite(pte_t pte)
526 {
527 	/*
528 	 * Used by Autonuma subsystem to preserve the write bit
529 	 * while marking the pte PROT_NONE. Only allow this
530 	 * on PROT_NONE pte
531 	 */
532 	VM_BUG_ON((pte_raw(pte) & cpu_to_be64(_PAGE_PRESENT | _PAGE_RWX | _PAGE_PRIVILEGED)) !=
533 		  cpu_to_be64(_PAGE_PRESENT | _PAGE_PRIVILEGED));
534 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_PRIVILEGED));
535 }
536 
537 #define pte_clear_savedwrite pte_clear_savedwrite
538 static inline pte_t pte_clear_savedwrite(pte_t pte)
539 {
540 	/*
541 	 * Used by KSM subsystem to make a protnone pte readonly.
542 	 */
543 	VM_BUG_ON(!pte_protnone(pte));
544 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_PRIVILEGED));
545 }
546 #else
547 #define pte_clear_savedwrite pte_clear_savedwrite
548 static inline pte_t pte_clear_savedwrite(pte_t pte)
549 {
550 	VM_WARN_ON(1);
551 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_WRITE));
552 }
553 #endif /* CONFIG_NUMA_BALANCING */
554 
555 static inline bool pte_hw_valid(pte_t pte)
556 {
557 	return (pte_raw(pte) & cpu_to_be64(_PAGE_PRESENT | _PAGE_PTE)) ==
558 		cpu_to_be64(_PAGE_PRESENT | _PAGE_PTE);
559 }
560 
561 static inline int pte_present(pte_t pte)
562 {
563 	/*
564 	 * A pte is considerent present if _PAGE_PRESENT is set.
565 	 * We also need to consider the pte present which is marked
566 	 * invalid during ptep_set_access_flags. Hence we look for _PAGE_INVALID
567 	 * if we find _PAGE_PRESENT cleared.
568 	 */
569 
570 	if (pte_hw_valid(pte))
571 		return true;
572 	return (pte_raw(pte) & cpu_to_be64(_PAGE_INVALID | _PAGE_PTE)) ==
573 		cpu_to_be64(_PAGE_INVALID | _PAGE_PTE);
574 }
575 
576 #ifdef CONFIG_PPC_MEM_KEYS
577 extern bool arch_pte_access_permitted(u64 pte, bool write, bool execute);
578 #else
579 static inline bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
580 {
581 	return true;
582 }
583 #endif /* CONFIG_PPC_MEM_KEYS */
584 
585 static inline bool pte_user(pte_t pte)
586 {
587 	return !(pte_raw(pte) & cpu_to_be64(_PAGE_PRIVILEGED));
588 }
589 
590 #define pte_access_permitted pte_access_permitted
591 static inline bool pte_access_permitted(pte_t pte, bool write)
592 {
593 	/*
594 	 * _PAGE_READ is needed for any access and will be
595 	 * cleared for PROT_NONE
596 	 */
597 	if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte))
598 		return false;
599 
600 	if (write && !pte_write(pte))
601 		return false;
602 
603 	return arch_pte_access_permitted(pte_val(pte), write, 0);
604 }
605 
606 /*
607  * Conversion functions: convert a page and protection to a page entry,
608  * and a page entry and page directory to the page they refer to.
609  *
610  * Even if PTEs can be unsigned long long, a PFN is always an unsigned
611  * long for now.
612  */
613 static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)
614 {
615 	VM_BUG_ON(pfn >> (64 - PAGE_SHIFT));
616 	VM_BUG_ON((pfn << PAGE_SHIFT) & ~PTE_RPN_MASK);
617 
618 	return __pte(((pte_basic_t)pfn << PAGE_SHIFT) | pgprot_val(pgprot));
619 }
620 
621 static inline unsigned long pte_pfn(pte_t pte)
622 {
623 	return (pte_val(pte) & PTE_RPN_MASK) >> PAGE_SHIFT;
624 }
625 
626 /* Generic modifiers for PTE bits */
627 static inline pte_t pte_wrprotect(pte_t pte)
628 {
629 	if (unlikely(pte_savedwrite(pte)))
630 		return pte_clear_savedwrite(pte);
631 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_WRITE));
632 }
633 
634 static inline pte_t pte_exprotect(pte_t pte)
635 {
636 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_EXEC));
637 }
638 
639 static inline pte_t pte_mkclean(pte_t pte)
640 {
641 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_DIRTY));
642 }
643 
644 static inline pte_t pte_mkold(pte_t pte)
645 {
646 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_ACCESSED));
647 }
648 
649 static inline pte_t pte_mkexec(pte_t pte)
650 {
651 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_EXEC));
652 }
653 
654 static inline pte_t pte_mkpte(pte_t pte)
655 {
656 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_PTE));
657 }
658 
659 static inline pte_t pte_mkwrite(pte_t pte)
660 {
661 	/*
662 	 * write implies read, hence set both
663 	 */
664 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_RW));
665 }
666 
667 static inline pte_t pte_mkdirty(pte_t pte)
668 {
669 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_DIRTY | _PAGE_SOFT_DIRTY));
670 }
671 
672 static inline pte_t pte_mkyoung(pte_t pte)
673 {
674 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_ACCESSED));
675 }
676 
677 static inline pte_t pte_mkspecial(pte_t pte)
678 {
679 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_SPECIAL));
680 }
681 
682 static inline pte_t pte_mkhuge(pte_t pte)
683 {
684 	return pte;
685 }
686 
687 static inline pte_t pte_mkdevmap(pte_t pte)
688 {
689 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_SPECIAL | _PAGE_DEVMAP));
690 }
691 
692 static inline pte_t pte_mkprivileged(pte_t pte)
693 {
694 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_PRIVILEGED));
695 }
696 
697 static inline pte_t pte_mkuser(pte_t pte)
698 {
699 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_PRIVILEGED));
700 }
701 
702 /*
703  * This is potentially called with a pmd as the argument, in which case it's not
704  * safe to check _PAGE_DEVMAP unless we also confirm that _PAGE_PTE is set.
705  * That's because the bit we use for _PAGE_DEVMAP is not reserved for software
706  * use in page directory entries (ie. non-ptes).
707  */
708 static inline int pte_devmap(pte_t pte)
709 {
710 	u64 mask = cpu_to_be64(_PAGE_DEVMAP | _PAGE_PTE);
711 
712 	return (pte_raw(pte) & mask) == mask;
713 }
714 
715 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
716 {
717 	/* FIXME!! check whether this need to be a conditional */
718 	return __pte_raw((pte_raw(pte) & cpu_to_be64(_PAGE_CHG_MASK)) |
719 			 cpu_to_be64(pgprot_val(newprot)));
720 }
721 
722 /* Encode and de-code a swap entry */
723 #define MAX_SWAPFILES_CHECK() do { \
724 	BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS); \
725 	/*							\
726 	 * Don't have overlapping bits with _PAGE_HPTEFLAGS	\
727 	 * We filter HPTEFLAGS on set_pte.			\
728 	 */							\
729 	BUILD_BUG_ON(_PAGE_HPTEFLAGS & (0x1f << _PAGE_BIT_SWAP_TYPE)); \
730 	BUILD_BUG_ON(_PAGE_HPTEFLAGS & _PAGE_SWP_SOFT_DIRTY);	\
731 	} while (0)
732 
733 #define SWP_TYPE_BITS 5
734 #define __swp_type(x)		(((x).val >> _PAGE_BIT_SWAP_TYPE) \
735 				& ((1UL << SWP_TYPE_BITS) - 1))
736 #define __swp_offset(x)		(((x).val & PTE_RPN_MASK) >> PAGE_SHIFT)
737 #define __swp_entry(type, offset)	((swp_entry_t) { \
738 				((type) << _PAGE_BIT_SWAP_TYPE) \
739 				| (((offset) << PAGE_SHIFT) & PTE_RPN_MASK)})
740 /*
741  * swp_entry_t must be independent of pte bits. We build a swp_entry_t from
742  * swap type and offset we get from swap and convert that to pte to find a
743  * matching pte in linux page table.
744  * Clear bits not found in swap entries here.
745  */
746 #define __pte_to_swp_entry(pte)	((swp_entry_t) { pte_val((pte)) & ~_PAGE_PTE })
747 #define __swp_entry_to_pte(x)	__pte((x).val | _PAGE_PTE)
748 #define __pmd_to_swp_entry(pmd)	(__pte_to_swp_entry(pmd_pte(pmd)))
749 #define __swp_entry_to_pmd(x)	(pte_pmd(__swp_entry_to_pte(x)))
750 
751 #ifdef CONFIG_MEM_SOFT_DIRTY
752 #define _PAGE_SWP_SOFT_DIRTY   (1UL << (SWP_TYPE_BITS + _PAGE_BIT_SWAP_TYPE))
753 #else
754 #define _PAGE_SWP_SOFT_DIRTY	0UL
755 #endif /* CONFIG_MEM_SOFT_DIRTY */
756 
757 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
758 static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
759 {
760 	return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_SWP_SOFT_DIRTY));
761 }
762 
763 static inline bool pte_swp_soft_dirty(pte_t pte)
764 {
765 	return !!(pte_raw(pte) & cpu_to_be64(_PAGE_SWP_SOFT_DIRTY));
766 }
767 
768 static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
769 {
770 	return __pte_raw(pte_raw(pte) & cpu_to_be64(~_PAGE_SWP_SOFT_DIRTY));
771 }
772 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
773 
774 static inline bool check_pte_access(unsigned long access, unsigned long ptev)
775 {
776 	/*
777 	 * This check for _PAGE_RWX and _PAGE_PRESENT bits
778 	 */
779 	if (access & ~ptev)
780 		return false;
781 	/*
782 	 * This check for access to privilege space
783 	 */
784 	if ((access & _PAGE_PRIVILEGED) != (ptev & _PAGE_PRIVILEGED))
785 		return false;
786 
787 	return true;
788 }
789 /*
790  * Generic functions with hash/radix callbacks
791  */
792 
793 static inline void __ptep_set_access_flags(struct vm_area_struct *vma,
794 					   pte_t *ptep, pte_t entry,
795 					   unsigned long address,
796 					   int psize)
797 {
798 	if (radix_enabled())
799 		return radix__ptep_set_access_flags(vma, ptep, entry,
800 						    address, psize);
801 	return hash__ptep_set_access_flags(ptep, entry);
802 }
803 
804 #define __HAVE_ARCH_PTE_SAME
805 static inline int pte_same(pte_t pte_a, pte_t pte_b)
806 {
807 	if (radix_enabled())
808 		return radix__pte_same(pte_a, pte_b);
809 	return hash__pte_same(pte_a, pte_b);
810 }
811 
812 static inline int pte_none(pte_t pte)
813 {
814 	if (radix_enabled())
815 		return radix__pte_none(pte);
816 	return hash__pte_none(pte);
817 }
818 
819 static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
820 				pte_t *ptep, pte_t pte, int percpu)
821 {
822 	if (radix_enabled())
823 		return radix__set_pte_at(mm, addr, ptep, pte, percpu);
824 	return hash__set_pte_at(mm, addr, ptep, pte, percpu);
825 }
826 
827 #define _PAGE_CACHE_CTL	(_PAGE_SAO | _PAGE_NON_IDEMPOTENT | _PAGE_TOLERANT)
828 
829 #define pgprot_noncached pgprot_noncached
830 static inline pgprot_t pgprot_noncached(pgprot_t prot)
831 {
832 	return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |
833 			_PAGE_NON_IDEMPOTENT);
834 }
835 
836 #define pgprot_noncached_wc pgprot_noncached_wc
837 static inline pgprot_t pgprot_noncached_wc(pgprot_t prot)
838 {
839 	return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |
840 			_PAGE_TOLERANT);
841 }
842 
843 #define pgprot_cached pgprot_cached
844 static inline pgprot_t pgprot_cached(pgprot_t prot)
845 {
846 	return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL));
847 }
848 
849 #define pgprot_writecombine pgprot_writecombine
850 static inline pgprot_t pgprot_writecombine(pgprot_t prot)
851 {
852 	return pgprot_noncached_wc(prot);
853 }
854 /*
855  * check a pte mapping have cache inhibited property
856  */
857 static inline bool pte_ci(pte_t pte)
858 {
859 	__be64 pte_v = pte_raw(pte);
860 
861 	if (((pte_v & cpu_to_be64(_PAGE_CACHE_CTL)) == cpu_to_be64(_PAGE_TOLERANT)) ||
862 	    ((pte_v & cpu_to_be64(_PAGE_CACHE_CTL)) == cpu_to_be64(_PAGE_NON_IDEMPOTENT)))
863 		return true;
864 	return false;
865 }
866 
867 static inline void pmd_clear(pmd_t *pmdp)
868 {
869 	*pmdp = __pmd(0);
870 }
871 
872 static inline int pmd_none(pmd_t pmd)
873 {
874 	return !pmd_raw(pmd);
875 }
876 
877 static inline int pmd_present(pmd_t pmd)
878 {
879 	/*
880 	 * A pmd is considerent present if _PAGE_PRESENT is set.
881 	 * We also need to consider the pmd present which is marked
882 	 * invalid during a split. Hence we look for _PAGE_INVALID
883 	 * if we find _PAGE_PRESENT cleared.
884 	 */
885 	if (pmd_raw(pmd) & cpu_to_be64(_PAGE_PRESENT | _PAGE_INVALID))
886 		return true;
887 
888 	return false;
889 }
890 
891 static inline int pmd_is_serializing(pmd_t pmd)
892 {
893 	/*
894 	 * If the pmd is undergoing a split, the _PAGE_PRESENT bit is clear
895 	 * and _PAGE_INVALID is set (see pmd_present, pmdp_invalidate).
896 	 *
897 	 * This condition may also occur when flushing a pmd while flushing
898 	 * it (see ptep_modify_prot_start), so callers must ensure this
899 	 * case is fine as well.
900 	 */
901 	if ((pmd_raw(pmd) & cpu_to_be64(_PAGE_PRESENT | _PAGE_INVALID)) ==
902 						cpu_to_be64(_PAGE_INVALID))
903 		return true;
904 
905 	return false;
906 }
907 
908 static inline int pmd_bad(pmd_t pmd)
909 {
910 	if (radix_enabled())
911 		return radix__pmd_bad(pmd);
912 	return hash__pmd_bad(pmd);
913 }
914 
915 static inline void pud_clear(pud_t *pudp)
916 {
917 	*pudp = __pud(0);
918 }
919 
920 static inline int pud_none(pud_t pud)
921 {
922 	return !pud_raw(pud);
923 }
924 
925 static inline int pud_present(pud_t pud)
926 {
927 	return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PRESENT));
928 }
929 
930 extern struct page *pud_page(pud_t pud);
931 extern struct page *pmd_page(pmd_t pmd);
932 static inline pte_t pud_pte(pud_t pud)
933 {
934 	return __pte_raw(pud_raw(pud));
935 }
936 
937 static inline pud_t pte_pud(pte_t pte)
938 {
939 	return __pud_raw(pte_raw(pte));
940 }
941 #define pud_write(pud)		pte_write(pud_pte(pud))
942 
943 static inline int pud_bad(pud_t pud)
944 {
945 	if (radix_enabled())
946 		return radix__pud_bad(pud);
947 	return hash__pud_bad(pud);
948 }
949 
950 #define pud_access_permitted pud_access_permitted
951 static inline bool pud_access_permitted(pud_t pud, bool write)
952 {
953 	return pte_access_permitted(pud_pte(pud), write);
954 }
955 
956 #define __p4d_raw(x)	((p4d_t) { __pgd_raw(x) })
957 static inline __be64 p4d_raw(p4d_t x)
958 {
959 	return pgd_raw(x.pgd);
960 }
961 
962 #define p4d_write(p4d)		pte_write(p4d_pte(p4d))
963 
964 static inline void p4d_clear(p4d_t *p4dp)
965 {
966 	*p4dp = __p4d(0);
967 }
968 
969 static inline int p4d_none(p4d_t p4d)
970 {
971 	return !p4d_raw(p4d);
972 }
973 
974 static inline int p4d_present(p4d_t p4d)
975 {
976 	return !!(p4d_raw(p4d) & cpu_to_be64(_PAGE_PRESENT));
977 }
978 
979 static inline pte_t p4d_pte(p4d_t p4d)
980 {
981 	return __pte_raw(p4d_raw(p4d));
982 }
983 
984 static inline p4d_t pte_p4d(pte_t pte)
985 {
986 	return __p4d_raw(pte_raw(pte));
987 }
988 
989 static inline int p4d_bad(p4d_t p4d)
990 {
991 	if (radix_enabled())
992 		return radix__p4d_bad(p4d);
993 	return hash__p4d_bad(p4d);
994 }
995 
996 #define p4d_access_permitted p4d_access_permitted
997 static inline bool p4d_access_permitted(p4d_t p4d, bool write)
998 {
999 	return pte_access_permitted(p4d_pte(p4d), write);
1000 }
1001 
1002 extern struct page *p4d_page(p4d_t p4d);
1003 
1004 /* Pointers in the page table tree are physical addresses */
1005 #define __pgtable_ptr_val(ptr)	__pa(ptr)
1006 
1007 #define pud_page_vaddr(pud)	__va(pud_val(pud) & ~PUD_MASKED_BITS)
1008 #define p4d_page_vaddr(p4d)	__va(p4d_val(p4d) & ~P4D_MASKED_BITS)
1009 
1010 #define pte_ERROR(e) \
1011 	pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
1012 #define pmd_ERROR(e) \
1013 	pr_err("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
1014 #define pud_ERROR(e) \
1015 	pr_err("%s:%d: bad pud %08lx.\n", __FILE__, __LINE__, pud_val(e))
1016 #define pgd_ERROR(e) \
1017 	pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
1018 
1019 static inline int map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot)
1020 {
1021 	if (radix_enabled()) {
1022 #if defined(CONFIG_PPC_RADIX_MMU) && defined(DEBUG_VM)
1023 		unsigned long page_size = 1 << mmu_psize_defs[mmu_io_psize].shift;
1024 		WARN((page_size != PAGE_SIZE), "I/O page size != PAGE_SIZE");
1025 #endif
1026 		return radix__map_kernel_page(ea, pa, prot, PAGE_SIZE);
1027 	}
1028 	return hash__map_kernel_page(ea, pa, prot);
1029 }
1030 
1031 static inline int __meminit vmemmap_create_mapping(unsigned long start,
1032 						   unsigned long page_size,
1033 						   unsigned long phys)
1034 {
1035 	if (radix_enabled())
1036 		return radix__vmemmap_create_mapping(start, page_size, phys);
1037 	return hash__vmemmap_create_mapping(start, page_size, phys);
1038 }
1039 
1040 #ifdef CONFIG_MEMORY_HOTPLUG
1041 static inline void vmemmap_remove_mapping(unsigned long start,
1042 					  unsigned long page_size)
1043 {
1044 	if (radix_enabled())
1045 		return radix__vmemmap_remove_mapping(start, page_size);
1046 	return hash__vmemmap_remove_mapping(start, page_size);
1047 }
1048 #endif
1049 
1050 static inline pte_t pmd_pte(pmd_t pmd)
1051 {
1052 	return __pte_raw(pmd_raw(pmd));
1053 }
1054 
1055 static inline pmd_t pte_pmd(pte_t pte)
1056 {
1057 	return __pmd_raw(pte_raw(pte));
1058 }
1059 
1060 static inline pte_t *pmdp_ptep(pmd_t *pmd)
1061 {
1062 	return (pte_t *)pmd;
1063 }
1064 #define pmd_pfn(pmd)		pte_pfn(pmd_pte(pmd))
1065 #define pmd_dirty(pmd)		pte_dirty(pmd_pte(pmd))
1066 #define pmd_young(pmd)		pte_young(pmd_pte(pmd))
1067 #define pmd_mkold(pmd)		pte_pmd(pte_mkold(pmd_pte(pmd)))
1068 #define pmd_wrprotect(pmd)	pte_pmd(pte_wrprotect(pmd_pte(pmd)))
1069 #define pmd_mkdirty(pmd)	pte_pmd(pte_mkdirty(pmd_pte(pmd)))
1070 #define pmd_mkclean(pmd)	pte_pmd(pte_mkclean(pmd_pte(pmd)))
1071 #define pmd_mkyoung(pmd)	pte_pmd(pte_mkyoung(pmd_pte(pmd)))
1072 #define pmd_mkwrite(pmd)	pte_pmd(pte_mkwrite(pmd_pte(pmd)))
1073 #define pmd_mk_savedwrite(pmd)	pte_pmd(pte_mk_savedwrite(pmd_pte(pmd)))
1074 #define pmd_clear_savedwrite(pmd)	pte_pmd(pte_clear_savedwrite(pmd_pte(pmd)))
1075 
1076 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
1077 #define pmd_soft_dirty(pmd)    pte_soft_dirty(pmd_pte(pmd))
1078 #define pmd_mksoft_dirty(pmd)  pte_pmd(pte_mksoft_dirty(pmd_pte(pmd)))
1079 #define pmd_clear_soft_dirty(pmd) pte_pmd(pte_clear_soft_dirty(pmd_pte(pmd)))
1080 
1081 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1082 #define pmd_swp_mksoft_dirty(pmd)	pte_pmd(pte_swp_mksoft_dirty(pmd_pte(pmd)))
1083 #define pmd_swp_soft_dirty(pmd)		pte_swp_soft_dirty(pmd_pte(pmd))
1084 #define pmd_swp_clear_soft_dirty(pmd)	pte_pmd(pte_swp_clear_soft_dirty(pmd_pte(pmd)))
1085 #endif
1086 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
1087 
1088 #ifdef CONFIG_NUMA_BALANCING
1089 static inline int pmd_protnone(pmd_t pmd)
1090 {
1091 	return pte_protnone(pmd_pte(pmd));
1092 }
1093 #endif /* CONFIG_NUMA_BALANCING */
1094 
1095 #define pmd_write(pmd)		pte_write(pmd_pte(pmd))
1096 #define __pmd_write(pmd)	__pte_write(pmd_pte(pmd))
1097 #define pmd_savedwrite(pmd)	pte_savedwrite(pmd_pte(pmd))
1098 
1099 #define pmd_access_permitted pmd_access_permitted
1100 static inline bool pmd_access_permitted(pmd_t pmd, bool write)
1101 {
1102 	/*
1103 	 * pmdp_invalidate sets this combination (which is not caught by
1104 	 * !pte_present() check in pte_access_permitted), to prevent
1105 	 * lock-free lookups, as part of the serialize_against_pte_lookup()
1106 	 * synchronisation.
1107 	 *
1108 	 * This also catches the case where the PTE's hardware PRESENT bit is
1109 	 * cleared while TLB is flushed, which is suboptimal but should not
1110 	 * be frequent.
1111 	 */
1112 	if (pmd_is_serializing(pmd))
1113 		return false;
1114 
1115 	return pte_access_permitted(pmd_pte(pmd), write);
1116 }
1117 
1118 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1119 extern pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot);
1120 extern pmd_t mk_pmd(struct page *page, pgprot_t pgprot);
1121 extern pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot);
1122 extern void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1123 		       pmd_t *pmdp, pmd_t pmd);
1124 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
1125 					unsigned long addr, pmd_t *pmd)
1126 {
1127 }
1128 
1129 extern int hash__has_transparent_hugepage(void);
1130 static inline int has_transparent_hugepage(void)
1131 {
1132 	if (radix_enabled())
1133 		return radix__has_transparent_hugepage();
1134 	return hash__has_transparent_hugepage();
1135 }
1136 #define has_transparent_hugepage has_transparent_hugepage
1137 
1138 static inline unsigned long
1139 pmd_hugepage_update(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp,
1140 		    unsigned long clr, unsigned long set)
1141 {
1142 	if (radix_enabled())
1143 		return radix__pmd_hugepage_update(mm, addr, pmdp, clr, set);
1144 	return hash__pmd_hugepage_update(mm, addr, pmdp, clr, set);
1145 }
1146 
1147 /*
1148  * returns true for pmd migration entries, THP, devmap, hugetlb
1149  * But compile time dependent on THP config
1150  */
1151 static inline int pmd_large(pmd_t pmd)
1152 {
1153 	return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
1154 }
1155 
1156 /*
1157  * For radix we should always find H_PAGE_HASHPTE zero. Hence
1158  * the below will work for radix too
1159  */
1160 static inline int __pmdp_test_and_clear_young(struct mm_struct *mm,
1161 					      unsigned long addr, pmd_t *pmdp)
1162 {
1163 	unsigned long old;
1164 
1165 	if ((pmd_raw(*pmdp) & cpu_to_be64(_PAGE_ACCESSED | H_PAGE_HASHPTE)) == 0)
1166 		return 0;
1167 	old = pmd_hugepage_update(mm, addr, pmdp, _PAGE_ACCESSED, 0);
1168 	return ((old & _PAGE_ACCESSED) != 0);
1169 }
1170 
1171 #define __HAVE_ARCH_PMDP_SET_WRPROTECT
1172 static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr,
1173 				      pmd_t *pmdp)
1174 {
1175 	if (__pmd_write((*pmdp)))
1176 		pmd_hugepage_update(mm, addr, pmdp, _PAGE_WRITE, 0);
1177 	else if (unlikely(pmd_savedwrite(*pmdp)))
1178 		pmd_hugepage_update(mm, addr, pmdp, 0, _PAGE_PRIVILEGED);
1179 }
1180 
1181 /*
1182  * Only returns true for a THP. False for pmd migration entry.
1183  * We also need to return true when we come across a pte that
1184  * in between a thp split. While splitting THP, we mark the pmd
1185  * invalid (pmdp_invalidate()) before we set it with pte page
1186  * address. A pmd_trans_huge() check against a pmd entry during that time
1187  * should return true.
1188  * We should not call this on a hugetlb entry. We should check for HugeTLB
1189  * entry using vma->vm_flags
1190  * The page table walk rule is explained in Documentation/vm/transhuge.rst
1191  */
1192 static inline int pmd_trans_huge(pmd_t pmd)
1193 {
1194 	if (!pmd_present(pmd))
1195 		return false;
1196 
1197 	if (radix_enabled())
1198 		return radix__pmd_trans_huge(pmd);
1199 	return hash__pmd_trans_huge(pmd);
1200 }
1201 
1202 #define __HAVE_ARCH_PMD_SAME
1203 static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
1204 {
1205 	if (radix_enabled())
1206 		return radix__pmd_same(pmd_a, pmd_b);
1207 	return hash__pmd_same(pmd_a, pmd_b);
1208 }
1209 
1210 static inline pmd_t pmd_mkhuge(pmd_t pmd)
1211 {
1212 	if (radix_enabled())
1213 		return radix__pmd_mkhuge(pmd);
1214 	return hash__pmd_mkhuge(pmd);
1215 }
1216 
1217 #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1218 extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1219 				 unsigned long address, pmd_t *pmdp,
1220 				 pmd_t entry, int dirty);
1221 
1222 #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1223 extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1224 				     unsigned long address, pmd_t *pmdp);
1225 
1226 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
1227 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
1228 					    unsigned long addr, pmd_t *pmdp)
1229 {
1230 	if (radix_enabled())
1231 		return radix__pmdp_huge_get_and_clear(mm, addr, pmdp);
1232 	return hash__pmdp_huge_get_and_clear(mm, addr, pmdp);
1233 }
1234 
1235 static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
1236 					unsigned long address, pmd_t *pmdp)
1237 {
1238 	if (radix_enabled())
1239 		return radix__pmdp_collapse_flush(vma, address, pmdp);
1240 	return hash__pmdp_collapse_flush(vma, address, pmdp);
1241 }
1242 #define pmdp_collapse_flush pmdp_collapse_flush
1243 
1244 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR_FULL
1245 pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma,
1246 				   unsigned long addr,
1247 				   pmd_t *pmdp, int full);
1248 
1249 #define __HAVE_ARCH_PGTABLE_DEPOSIT
1250 static inline void pgtable_trans_huge_deposit(struct mm_struct *mm,
1251 					      pmd_t *pmdp, pgtable_t pgtable)
1252 {
1253 	if (radix_enabled())
1254 		return radix__pgtable_trans_huge_deposit(mm, pmdp, pgtable);
1255 	return hash__pgtable_trans_huge_deposit(mm, pmdp, pgtable);
1256 }
1257 
1258 #define __HAVE_ARCH_PGTABLE_WITHDRAW
1259 static inline pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm,
1260 						    pmd_t *pmdp)
1261 {
1262 	if (radix_enabled())
1263 		return radix__pgtable_trans_huge_withdraw(mm, pmdp);
1264 	return hash__pgtable_trans_huge_withdraw(mm, pmdp);
1265 }
1266 
1267 #define __HAVE_ARCH_PMDP_INVALIDATE
1268 extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
1269 			     pmd_t *pmdp);
1270 
1271 #define pmd_move_must_withdraw pmd_move_must_withdraw
1272 struct spinlock;
1273 extern int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
1274 				  struct spinlock *old_pmd_ptl,
1275 				  struct vm_area_struct *vma);
1276 /*
1277  * Hash translation mode use the deposited table to store hash pte
1278  * slot information.
1279  */
1280 #define arch_needs_pgtable_deposit arch_needs_pgtable_deposit
1281 static inline bool arch_needs_pgtable_deposit(void)
1282 {
1283 	if (radix_enabled())
1284 		return false;
1285 	return true;
1286 }
1287 extern void serialize_against_pte_lookup(struct mm_struct *mm);
1288 
1289 
1290 static inline pmd_t pmd_mkdevmap(pmd_t pmd)
1291 {
1292 	if (radix_enabled())
1293 		return radix__pmd_mkdevmap(pmd);
1294 	return hash__pmd_mkdevmap(pmd);
1295 }
1296 
1297 static inline int pmd_devmap(pmd_t pmd)
1298 {
1299 	return pte_devmap(pmd_pte(pmd));
1300 }
1301 
1302 static inline int pud_devmap(pud_t pud)
1303 {
1304 	return 0;
1305 }
1306 
1307 static inline int pgd_devmap(pgd_t pgd)
1308 {
1309 	return 0;
1310 }
1311 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1312 
1313 static inline int pud_pfn(pud_t pud)
1314 {
1315 	/*
1316 	 * Currently all calls to pud_pfn() are gated around a pud_devmap()
1317 	 * check so this should never be used. If it grows another user we
1318 	 * want to know about it.
1319 	 */
1320 	BUILD_BUG();
1321 	return 0;
1322 }
1323 #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
1324 pte_t ptep_modify_prot_start(struct vm_area_struct *, unsigned long, pte_t *);
1325 void ptep_modify_prot_commit(struct vm_area_struct *, unsigned long,
1326 			     pte_t *, pte_t, pte_t);
1327 
1328 /*
1329  * Returns true for a R -> RW upgrade of pte
1330  */
1331 static inline bool is_pte_rw_upgrade(unsigned long old_val, unsigned long new_val)
1332 {
1333 	if (!(old_val & _PAGE_READ))
1334 		return false;
1335 
1336 	if ((!(old_val & _PAGE_WRITE)) && (new_val & _PAGE_WRITE))
1337 		return true;
1338 
1339 	return false;
1340 }
1341 
1342 /*
1343  * Like pmd_huge() and pmd_large(), but works regardless of config options
1344  */
1345 #define pmd_is_leaf pmd_is_leaf
1346 #define pmd_leaf pmd_is_leaf
1347 static inline bool pmd_is_leaf(pmd_t pmd)
1348 {
1349 	return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
1350 }
1351 
1352 #define pud_is_leaf pud_is_leaf
1353 #define pud_leaf pud_is_leaf
1354 static inline bool pud_is_leaf(pud_t pud)
1355 {
1356 	return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE));
1357 }
1358 
1359 #define p4d_is_leaf p4d_is_leaf
1360 #define p4d_leaf p4d_is_leaf
1361 static inline bool p4d_is_leaf(p4d_t p4d)
1362 {
1363 	return !!(p4d_raw(p4d) & cpu_to_be64(_PAGE_PTE));
1364 }
1365 
1366 #endif /* __ASSEMBLY__ */
1367 #endif /* _ASM_POWERPC_BOOK3S_64_PGTABLE_H_ */
1368