blob: 818a58fc3f4f967d3bc4524c32e64ba4529c4cca [file] [log] [blame]
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301#ifndef _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
2#define _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +10003
Kirill A. Shutemov9849a562017-03-09 17:24:05 +03004#include <asm-generic/5level-fixup.h>
5
Aneesh Kumar K.Vc137a272017-02-24 14:59:21 -08006#ifndef __ASSEMBLY__
7#include <linux/mmdebug.h>
Oliver O'Halloranebd31192017-06-28 11:32:34 +10008#include <linux/bug.h>
Aneesh Kumar K.Vc137a272017-02-24 14:59:21 -08009#endif
Kirill A. Shutemov9849a562017-03-09 17:24:05 +030010
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +053011/*
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +100012 * Common bits between hash and Radix page table
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +053013 */
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +100014#define _PAGE_BIT_SWAP_TYPE 0
15
Christophe Leroy6b8cb662016-09-19 12:58:54 +020016#define _PAGE_RO 0
Christophe Leroyfd893fe2017-04-14 07:45:16 +020017#define _PAGE_SHARED 0
Christophe Leroy6b8cb662016-09-19 12:58:54 +020018
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +100019#define _PAGE_EXEC 0x00001 /* execute permission */
20#define _PAGE_WRITE 0x00002 /* write access allowed */
21#define _PAGE_READ 0x00004 /* read access allowed */
22#define _PAGE_RW (_PAGE_READ | _PAGE_WRITE)
23#define _PAGE_RWX (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC)
24#define _PAGE_PRIVILEGED 0x00008 /* kernel access only */
25#define _PAGE_SAO 0x00010 /* Strong access order */
26#define _PAGE_NON_IDEMPOTENT 0x00020 /* non idempotent memory */
27#define _PAGE_TOLERANT 0x00030 /* tolerant memory, cache inhibited */
28#define _PAGE_DIRTY 0x00080 /* C: page changed */
29#define _PAGE_ACCESSED 0x00100 /* R: page referenced */
30/*
31 * Software bits
32 */
Aneesh Kumar K.V69dfbae2016-04-29 23:26:33 +100033#define _RPAGE_SW0 0x2000000000000000UL
34#define _RPAGE_SW1 0x00800
35#define _RPAGE_SW2 0x00400
36#define _RPAGE_SW3 0x00200
Aneesh Kumar K.V049d5672016-11-28 11:47:00 +053037#define _RPAGE_RSV1 0x1000000000000000UL
38#define _RPAGE_RSV2 0x0800000000000000UL
39#define _RPAGE_RSV3 0x0400000000000000UL
40#define _RPAGE_RSV4 0x0200000000000000UL
Aneesh Kumar K.V6aa59f52017-03-28 15:21:12 +110041
42#define _PAGE_PTE 0x4000000000000000UL /* distinguishes PTEs from pointers */
43#define _PAGE_PRESENT 0x8000000000000000UL /* pte contains a translation */
44
45/*
46 * Top and bottom bits of RPN which can be used by hash
47 * translation mode, because we expect them to be zero
48 * otherwise.
49 */
Aneesh Kumar K.V32789d32017-03-21 22:59:58 +053050#define _RPAGE_RPN0 0x01000
51#define _RPAGE_RPN1 0x02000
Aneesh Kumar K.V6aa59f52017-03-28 15:21:12 +110052#define _RPAGE_RPN44 0x0100000000000000UL
53#define _RPAGE_RPN43 0x0080000000000000UL
54#define _RPAGE_RPN42 0x0040000000000000UL
55#define _RPAGE_RPN41 0x0020000000000000UL
Aneesh Kumar K.V049d5672016-11-28 11:47:00 +053056
Aneesh Kumar K.V2f18d532017-03-21 22:59:59 +053057/* Max physical address bit as per radix table */
58#define _RPAGE_PA_MAX 57
59
60/*
61 * Max physical address bit we will use for now.
62 *
63 * This is mostly a hardware limitation and for now Power9 has
64 * a 51 bit limit.
65 *
66 * This is different from the number of physical bit required to address
67 * the last byte of memory. That is defined by MAX_PHYSMEM_BITS.
68 * MAX_PHYSMEM_BITS is a linux limitation imposed by the maximum
69 * number of sections we can support (SECTIONS_SHIFT).
70 *
71 * This is different from Radix page table limitation above and
72 * should always be less than that. The limit is done such that
73 * we can overload the bits between _RPAGE_PA_MAX and _PAGE_PA_MAX
74 * for hash linux page table specific bits.
75 *
76 * In order to be compatible with future hardware generations we keep
77 * some offsets and limit this for now to 53
78 */
79#define _PAGE_PA_MAX 53
80
Aneesh Kumar K.V69dfbae2016-04-29 23:26:33 +100081#define _PAGE_SOFT_DIRTY _RPAGE_SW3 /* software: software dirty tracking */
Aneesh Kumar K.V69dfbae2016-04-29 23:26:33 +100082#define _PAGE_SPECIAL _RPAGE_SW2 /* software: special page */
Oliver O'Halloranebd31192017-06-28 11:32:34 +100083#define _PAGE_DEVMAP _RPAGE_SW1 /* software: ZONE_DEVICE page */
84#define __HAVE_ARCH_PTE_DEVMAP
85
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +100086/*
87 * Drivers request for cache inhibited pte mapping using _PAGE_NO_CACHE
88 * Instead of fixing all of them, add an alternate define which
89 * maps CI pte mapping.
90 */
91#define _PAGE_NO_CACHE _PAGE_TOLERANT
92/*
Aneesh Kumar K.V2f18d532017-03-21 22:59:59 +053093 * We support _RPAGE_PA_MAX bit real address in pte. On the linux side
94 * we are limited by _PAGE_PA_MAX. Clear everything above _PAGE_PA_MAX
95 * and every thing below PAGE_SHIFT;
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +100096 */
Aneesh Kumar K.V2f18d532017-03-21 22:59:59 +053097#define PTE_RPN_MASK (((1UL << _PAGE_PA_MAX) - 1) & (PAGE_MASK))
Aneesh Kumar K.V2e873512016-04-29 23:25:47 +100098/*
99 * set of bits not changed in pmd_modify. Even though we have hash specific bits
100 * in here, on radix we expect them to be zero.
101 */
102#define _HPAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HPTEFLAGS | _PAGE_DIRTY | \
103 _PAGE_ACCESSED | H_PAGE_THP_HUGE | _PAGE_PTE | \
104 _PAGE_SOFT_DIRTY)
105/*
106 * user access blocked by key
107 */
108#define _PAGE_KERNEL_RW (_PAGE_PRIVILEGED | _PAGE_RW | _PAGE_DIRTY)
109#define _PAGE_KERNEL_RO (_PAGE_PRIVILEGED | _PAGE_READ)
110#define _PAGE_KERNEL_RWX (_PAGE_PRIVILEGED | _PAGE_DIRTY | \
111 _PAGE_RW | _PAGE_EXEC)
112/*
113 * No page size encoding in the linux PTE
114 */
115#define _PAGE_PSIZE 0
116/*
117 * _PAGE_CHG_MASK masks of bits that are to be preserved across
118 * pgprot changes
119 */
120#define _PAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HPTEFLAGS | _PAGE_DIRTY | \
121 _PAGE_ACCESSED | _PAGE_SPECIAL | _PAGE_PTE | \
122 _PAGE_SOFT_DIRTY)
123/*
124 * Mask of bits returned by pte_pgprot()
125 */
126#define PAGE_PROT_BITS (_PAGE_SAO | _PAGE_NON_IDEMPOTENT | _PAGE_TOLERANT | \
127 H_PAGE_4K_PFN | _PAGE_PRIVILEGED | _PAGE_ACCESSED | \
128 _PAGE_READ | _PAGE_WRITE | _PAGE_DIRTY | _PAGE_EXEC | \
129 _PAGE_SOFT_DIRTY)
130/*
131 * We define 2 sets of base prot bits, one for basic pages (ie,
132 * cacheable kernel and user pages) and one for non cacheable
133 * pages. We always set _PAGE_COHERENT when SMP is enabled or
134 * the processor might need it for DMA coherency.
135 */
136#define _PAGE_BASE_NC (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_PSIZE)
137#define _PAGE_BASE (_PAGE_BASE_NC)
138
139/* Permission masks used to generate the __P and __S table,
140 *
141 * Note:__pgprot is defined in arch/powerpc/include/asm/page.h
142 *
143 * Write permissions imply read permissions for now (we could make write-only
144 * pages on BookE but we don't bother for now). Execute permission control is
145 * possible on platforms that define _PAGE_EXEC
146 *
147 * Note due to the way vm flags are laid out, the bits are XWR
148 */
149#define PAGE_NONE __pgprot(_PAGE_BASE | _PAGE_PRIVILEGED)
150#define PAGE_SHARED __pgprot(_PAGE_BASE | _PAGE_RW)
151#define PAGE_SHARED_X __pgprot(_PAGE_BASE | _PAGE_RW | _PAGE_EXEC)
152#define PAGE_COPY __pgprot(_PAGE_BASE | _PAGE_READ)
153#define PAGE_COPY_X __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC)
154#define PAGE_READONLY __pgprot(_PAGE_BASE | _PAGE_READ)
155#define PAGE_READONLY_X __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC)
156
157#define __P000 PAGE_NONE
158#define __P001 PAGE_READONLY
159#define __P010 PAGE_COPY
160#define __P011 PAGE_COPY
161#define __P100 PAGE_READONLY_X
162#define __P101 PAGE_READONLY_X
163#define __P110 PAGE_COPY_X
164#define __P111 PAGE_COPY_X
165
166#define __S000 PAGE_NONE
167#define __S001 PAGE_READONLY
168#define __S010 PAGE_SHARED
169#define __S011 PAGE_SHARED
170#define __S100 PAGE_READONLY_X
171#define __S101 PAGE_READONLY_X
172#define __S110 PAGE_SHARED_X
173#define __S111 PAGE_SHARED_X
174
175/* Permission masks used for kernel mappings */
176#define PAGE_KERNEL __pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)
177#define PAGE_KERNEL_NC __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
178 _PAGE_TOLERANT)
179#define PAGE_KERNEL_NCG __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
180 _PAGE_NON_IDEMPOTENT)
181#define PAGE_KERNEL_X __pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)
182#define PAGE_KERNEL_RO __pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)
183#define PAGE_KERNEL_ROX __pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
184
185/*
186 * Protection used for kernel text. We want the debuggers to be able to
187 * set breakpoints anywhere, so don't write protect the kernel text
188 * on platforms where such control is possible.
189 */
190#if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) || \
191 defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
192#define PAGE_KERNEL_TEXT PAGE_KERNEL_X
193#else
194#define PAGE_KERNEL_TEXT PAGE_KERNEL_ROX
195#endif
196
197/* Make modules code happy. We don't set RO yet */
198#define PAGE_KERNEL_EXEC PAGE_KERNEL_X
199#define PAGE_AGP (PAGE_KERNEL_NC)
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530200
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000201#ifndef __ASSEMBLY__
202/*
203 * page table defines
204 */
205extern unsigned long __pte_index_size;
206extern unsigned long __pmd_index_size;
207extern unsigned long __pud_index_size;
208extern unsigned long __pgd_index_size;
209extern unsigned long __pmd_cache_index;
210#define PTE_INDEX_SIZE __pte_index_size
211#define PMD_INDEX_SIZE __pmd_index_size
212#define PUD_INDEX_SIZE __pud_index_size
213#define PGD_INDEX_SIZE __pgd_index_size
214#define PMD_CACHE_INDEX __pmd_cache_index
215/*
216 * Because of use of pte fragments and THP, size of page table
217 * are not always derived out of index size above.
218 */
219extern unsigned long __pte_table_size;
220extern unsigned long __pmd_table_size;
221extern unsigned long __pud_table_size;
222extern unsigned long __pgd_table_size;
223#define PTE_TABLE_SIZE __pte_table_size
224#define PMD_TABLE_SIZE __pmd_table_size
225#define PUD_TABLE_SIZE __pud_table_size
226#define PGD_TABLE_SIZE __pgd_table_size
Aneesh Kumar K.Va2f41eb2016-04-29 23:26:19 +1000227
228extern unsigned long __pmd_val_bits;
229extern unsigned long __pud_val_bits;
230extern unsigned long __pgd_val_bits;
231#define PMD_VAL_BITS __pmd_val_bits
232#define PUD_VAL_BITS __pud_val_bits
233#define PGD_VAL_BITS __pgd_val_bits
Aneesh Kumar K.V5ed7ecd2016-04-29 23:26:23 +1000234
235extern unsigned long __pte_frag_nr;
236#define PTE_FRAG_NR __pte_frag_nr
237extern unsigned long __pte_frag_size_shift;
238#define PTE_FRAG_SIZE_SHIFT __pte_frag_size_shift
239#define PTE_FRAG_SIZE (1UL << PTE_FRAG_SIZE_SHIFT)
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000240
241#define PTRS_PER_PTE (1 << PTE_INDEX_SIZE)
242#define PTRS_PER_PMD (1 << PMD_INDEX_SIZE)
243#define PTRS_PER_PUD (1 << PUD_INDEX_SIZE)
244#define PTRS_PER_PGD (1 << PGD_INDEX_SIZE)
245
246/* PMD_SHIFT determines what a second-level page table entry can map */
247#define PMD_SHIFT (PAGE_SHIFT + PTE_INDEX_SIZE)
248#define PMD_SIZE (1UL << PMD_SHIFT)
249#define PMD_MASK (~(PMD_SIZE-1))
250
251/* PUD_SHIFT determines what a third-level page table entry can map */
252#define PUD_SHIFT (PMD_SHIFT + PMD_INDEX_SIZE)
253#define PUD_SIZE (1UL << PUD_SHIFT)
254#define PUD_MASK (~(PUD_SIZE-1))
255
256/* PGDIR_SHIFT determines what a fourth-level page table entry can map */
257#define PGDIR_SHIFT (PUD_SHIFT + PUD_INDEX_SIZE)
258#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
259#define PGDIR_MASK (~(PGDIR_SIZE-1))
260
261/* Bits to mask out from a PMD to get to the PTE page */
262#define PMD_MASKED_BITS 0xc0000000000000ffUL
263/* Bits to mask out from a PUD to get to the PMD page */
264#define PUD_MASKED_BITS 0xc0000000000000ffUL
265/* Bits to mask out from a PGD to get to the PUD page */
266#define PGD_MASKED_BITS 0xc0000000000000ffUL
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +1000267
268extern unsigned long __vmalloc_start;
269extern unsigned long __vmalloc_end;
270#define VMALLOC_START __vmalloc_start
271#define VMALLOC_END __vmalloc_end
272
273extern unsigned long __kernel_virt_start;
274extern unsigned long __kernel_virt_size;
275#define KERN_VIRT_START __kernel_virt_start
276#define KERN_VIRT_SIZE __kernel_virt_size
277extern struct page *vmemmap;
278extern unsigned long ioremap_bot;
Darren Stevensbfa37082016-06-29 21:06:28 +0100279extern unsigned long pci_io_base;
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000280#endif /* __ASSEMBLY__ */
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530281
Aneesh Kumar K.Vab537dc2015-12-01 09:06:30 +0530282#include <asm/book3s/64/hash.h>
Aneesh Kumar K.Vb0b5e9b2016-04-29 23:25:52 +1000283#include <asm/book3s/64/radix.h>
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530284
Aneesh Kumar K.Va9252aae2016-04-29 23:25:55 +1000285#ifdef CONFIG_PPC_64K_PAGES
286#include <asm/book3s/64/pgtable-64k.h>
287#else
288#include <asm/book3s/64/pgtable-4k.h>
289#endif
290
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530291#include <asm/barrier.h>
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530292/*
293 * The second half of the kernel virtual space is used for IO mappings,
294 * it's itself carved into the PIO region (ISA and PHB IO space) and
295 * the ioremap space
296 *
297 * ISA_IO_BASE = KERN_IO_START, 64K reserved area
298 * PHB_IO_BASE = ISA_IO_BASE + 64K to ISA_IO_BASE + 2G, PHB IO spaces
299 * IOREMAP_BASE = ISA_IO_BASE + 2G to VMALLOC_START + PGTABLE_RANGE
300 */
301#define KERN_IO_START (KERN_VIRT_START + (KERN_VIRT_SIZE >> 1))
302#define FULL_IO_SIZE 0x80000000ul
303#define ISA_IO_BASE (KERN_IO_START)
304#define ISA_IO_END (KERN_IO_START + 0x10000ul)
305#define PHB_IO_BASE (ISA_IO_END)
306#define PHB_IO_END (KERN_IO_START + FULL_IO_SIZE)
307#define IOREMAP_BASE (PHB_IO_END)
308#define IOREMAP_END (KERN_VIRT_START + KERN_VIRT_SIZE)
309
Aneesh Kumar K.Vb0412ea2015-12-01 09:06:33 +0530310/* Advertise special mapping type for AGP */
Aneesh Kumar K.Vb0412ea2015-12-01 09:06:33 +0530311#define HAVE_PAGE_AGP
312
313/* Advertise support for _PAGE_SPECIAL */
314#define __HAVE_ARCH_PTE_SPECIAL
315
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530316#ifndef __ASSEMBLY__
317
318/*
319 * This is the default implementation of various PTE accessors, it's
320 * used in all cases except Book3S with 64K pages where we have a
321 * concept of sub-pages
322 */
323#ifndef __real_pte
324
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530325#define __real_pte(e,p) ((real_pte_t){(e)})
326#define __rpte_to_pte(r) ((r).pte)
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000327#define __rpte_to_hidx(r,index) (pte_val(__rpte_to_pte(r)) >> H_PAGE_F_GIX_SHIFT)
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530328
329#define pte_iterate_hashed_subpages(rpte, psize, va, index, shift) \
330 do { \
331 index = 0; \
332 shift = mmu_psize_defs[psize].shift; \
333
334#define pte_iterate_hashed_end() } while(0)
335
336/*
337 * We expect this to be called only for user addresses or kernel virtual
338 * addresses other than the linear mapping.
339 */
340#define pte_pagesize_index(mm, addr, pte) MMU_PAGE_4K
341
342#endif /* __real_pte */
343
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000344static inline unsigned long pte_update(struct mm_struct *mm, unsigned long addr,
345 pte_t *ptep, unsigned long clr,
346 unsigned long set, int huge)
347{
348 if (radix_enabled())
349 return radix__pte_update(mm, addr, ptep, clr, set, huge);
350 return hash__pte_update(mm, addr, ptep, clr, set, huge);
351}
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000352/*
353 * For hash even if we have _PAGE_ACCESSED = 0, we do a pte_update.
354 * We currently remove entries from the hashtable regardless of whether
355 * the entry was young or dirty.
356 *
357 * We should be more intelligent about this but for the moment we override
358 * these functions and force a tlb flush unconditionally
359 * For radix: H_PAGE_HASHPTE should be zero. Hence we can use the same
360 * function for both hash and radix.
361 */
362static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
363 unsigned long addr, pte_t *ptep)
364{
365 unsigned long old;
366
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530367 if ((pte_raw(*ptep) & cpu_to_be64(_PAGE_ACCESSED | H_PAGE_HASHPTE)) == 0)
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000368 return 0;
369 old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);
370 return (old & _PAGE_ACCESSED) != 0;
371}
372
373#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
374#define ptep_test_and_clear_young(__vma, __addr, __ptep) \
375({ \
376 int __r; \
377 __r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
378 __r; \
379})
380
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -0800381static inline int __pte_write(pte_t pte)
Aneesh Kumar K.V52c50ca2017-03-09 16:16:36 -0800382{
383 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_WRITE));
384}
385
386#ifdef CONFIG_NUMA_BALANCING
387#define pte_savedwrite pte_savedwrite
388static inline bool pte_savedwrite(pte_t pte)
389{
390 /*
391 * Saved write ptes are prot none ptes that doesn't have
392 * privileged bit sit. We mark prot none as one which has
393 * present and pviliged bit set and RWX cleared. To mark
394 * protnone which used to have _PAGE_WRITE set we clear
395 * the privileged bit.
396 */
397 return !(pte_raw(pte) & cpu_to_be64(_PAGE_RWX | _PAGE_PRIVILEGED));
398}
399#else
400#define pte_savedwrite pte_savedwrite
401static inline bool pte_savedwrite(pte_t pte)
402{
403 return false;
404}
405#endif
406
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -0800407static inline int pte_write(pte_t pte)
408{
409 return __pte_write(pte) || pte_savedwrite(pte);
410}
411
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000412#define __HAVE_ARCH_PTEP_SET_WRPROTECT
413static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
414 pte_t *ptep)
415{
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -0800416 if (__pte_write(*ptep))
Aneesh Kumar K.V52c50ca2017-03-09 16:16:36 -0800417 pte_update(mm, addr, ptep, _PAGE_WRITE, 0, 0);
418 else if (unlikely(pte_savedwrite(*ptep)))
419 pte_update(mm, addr, ptep, 0, _PAGE_PRIVILEGED, 0);
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000420}
421
422static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
423 unsigned long addr, pte_t *ptep)
424{
Aneesh Kumar K.V52c50ca2017-03-09 16:16:36 -0800425 /*
426 * We should not find protnone for hugetlb, but this complete the
427 * interface.
428 */
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -0800429 if (__pte_write(*ptep))
Aneesh Kumar K.V52c50ca2017-03-09 16:16:36 -0800430 pte_update(mm, addr, ptep, _PAGE_WRITE, 0, 1);
431 else if (unlikely(pte_savedwrite(*ptep)))
432 pte_update(mm, addr, ptep, 0, _PAGE_PRIVILEGED, 1);
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000433}
434
435#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
436static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
437 unsigned long addr, pte_t *ptep)
438{
439 unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
440 return __pte(old);
441}
442
Aneesh Kumar K.Vf4894b82017-02-09 08:28:20 +0530443#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
444static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
445 unsigned long addr,
446 pte_t *ptep, int full)
447{
448 if (full && radix_enabled()) {
449 /*
450 * Let's skip the DD1 style pte update here. We know that
451 * this is a full mm pte clear and hence can be sure there is
452 * no parallel set_pte.
453 */
454 return radix__ptep_get_and_clear_full(mm, addr, ptep, full);
455 }
456 return ptep_get_and_clear(mm, addr, ptep);
457}
458
459
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000460static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
461 pte_t * ptep)
462{
463 pte_update(mm, addr, ptep, ~0UL, 0, 0);
464}
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530465
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530466static inline int pte_dirty(pte_t pte)
467{
468 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_DIRTY));
469}
470
471static inline int pte_young(pte_t pte)
472{
473 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_ACCESSED));
474}
475
476static inline int pte_special(pte_t pte)
477{
478 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_SPECIAL));
479}
480
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000481static inline pgprot_t pte_pgprot(pte_t pte) { return __pgprot(pte_val(pte) & PAGE_PROT_BITS); }
482
483#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
484static inline bool pte_soft_dirty(pte_t pte)
485{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530486 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_SOFT_DIRTY));
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000487}
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530488
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000489static inline pte_t pte_mksoft_dirty(pte_t pte)
490{
491 return __pte(pte_val(pte) | _PAGE_SOFT_DIRTY);
492}
493
494static inline pte_t pte_clear_soft_dirty(pte_t pte)
495{
496 return __pte(pte_val(pte) & ~_PAGE_SOFT_DIRTY);
497}
498#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
499
500#ifdef CONFIG_NUMA_BALANCING
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000501static inline int pte_protnone(pte_t pte)
502{
Aneesh Kumar K.Vc137a272017-02-24 14:59:21 -0800503 return (pte_raw(pte) & cpu_to_be64(_PAGE_PRESENT | _PAGE_PTE | _PAGE_RWX)) ==
504 cpu_to_be64(_PAGE_PRESENT | _PAGE_PTE);
505}
506
507#define pte_mk_savedwrite pte_mk_savedwrite
508static inline pte_t pte_mk_savedwrite(pte_t pte)
509{
510 /*
511 * Used by Autonuma subsystem to preserve the write bit
512 * while marking the pte PROT_NONE. Only allow this
513 * on PROT_NONE pte
514 */
515 VM_BUG_ON((pte_raw(pte) & cpu_to_be64(_PAGE_PRESENT | _PAGE_RWX | _PAGE_PRIVILEGED)) !=
516 cpu_to_be64(_PAGE_PRESENT | _PAGE_PRIVILEGED));
517 return __pte(pte_val(pte) & ~_PAGE_PRIVILEGED);
518}
519
520#define pte_clear_savedwrite pte_clear_savedwrite
521static inline pte_t pte_clear_savedwrite(pte_t pte)
522{
523 /*
524 * Used by KSM subsystem to make a protnone pte readonly.
525 */
526 VM_BUG_ON(!pte_protnone(pte));
527 return __pte(pte_val(pte) | _PAGE_PRIVILEGED);
528}
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -0800529#else
530#define pte_clear_savedwrite pte_clear_savedwrite
531static inline pte_t pte_clear_savedwrite(pte_t pte)
532{
533 VM_WARN_ON(1);
534 return __pte(pte_val(pte) & ~_PAGE_WRITE);
535}
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000536#endif /* CONFIG_NUMA_BALANCING */
537
538static inline int pte_present(pte_t pte)
539{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530540 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_PRESENT));
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000541}
542/*
543 * Conversion functions: convert a page and protection to a page entry,
544 * and a page entry and page directory to the page they refer to.
545 *
546 * Even if PTEs can be unsigned long long, a PFN is always an unsigned
547 * long for now.
548 */
549static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)
550{
551 return __pte((((pte_basic_t)(pfn) << PAGE_SHIFT) & PTE_RPN_MASK) |
552 pgprot_val(pgprot));
553}
554
555static inline unsigned long pte_pfn(pte_t pte)
556{
557 return (pte_val(pte) & PTE_RPN_MASK) >> PAGE_SHIFT;
558}
559
560/* Generic modifiers for PTE bits */
561static inline pte_t pte_wrprotect(pte_t pte)
562{
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -0800563 if (unlikely(pte_savedwrite(pte)))
564 return pte_clear_savedwrite(pte);
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000565 return __pte(pte_val(pte) & ~_PAGE_WRITE);
566}
567
568static inline pte_t pte_mkclean(pte_t pte)
569{
570 return __pte(pte_val(pte) & ~_PAGE_DIRTY);
571}
572
573static inline pte_t pte_mkold(pte_t pte)
574{
575 return __pte(pte_val(pte) & ~_PAGE_ACCESSED);
576}
577
578static inline pte_t pte_mkwrite(pte_t pte)
579{
580 /*
581 * write implies read, hence set both
582 */
583 return __pte(pte_val(pte) | _PAGE_RW);
584}
585
586static inline pte_t pte_mkdirty(pte_t pte)
587{
588 return __pte(pte_val(pte) | _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
589}
590
591static inline pte_t pte_mkyoung(pte_t pte)
592{
593 return __pte(pte_val(pte) | _PAGE_ACCESSED);
594}
595
596static inline pte_t pte_mkspecial(pte_t pte)
597{
598 return __pte(pte_val(pte) | _PAGE_SPECIAL);
599}
600
601static inline pte_t pte_mkhuge(pte_t pte)
602{
603 return pte;
604}
605
Oliver O'Halloranebd31192017-06-28 11:32:34 +1000606static inline pte_t pte_mkdevmap(pte_t pte)
607{
608 return __pte(pte_val(pte) | _PAGE_SPECIAL|_PAGE_DEVMAP);
609}
610
Oliver O'Halloranc9c98bc2017-07-28 01:35:53 +1000611/*
612 * This is potentially called with a pmd as the argument, in which case it's not
613 * safe to check _PAGE_DEVMAP unless we also confirm that _PAGE_PTE is set.
614 * That's because the bit we use for _PAGE_DEVMAP is not reserved for software
615 * use in page directory entries (ie. non-ptes).
616 */
Oliver O'Halloranebd31192017-06-28 11:32:34 +1000617static inline int pte_devmap(pte_t pte)
618{
Oliver O'Halloranc9c98bc2017-07-28 01:35:53 +1000619 u64 mask = cpu_to_be64(_PAGE_DEVMAP | _PAGE_PTE);
620
621 return (pte_raw(pte) & mask) == mask;
Oliver O'Halloranebd31192017-06-28 11:32:34 +1000622}
623
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000624static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
625{
626 /* FIXME!! check whether this need to be a conditional */
627 return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
628}
629
Aneesh Kumar K.V34fbadd2016-04-29 23:25:51 +1000630static inline bool pte_user(pte_t pte)
631{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530632 return !(pte_raw(pte) & cpu_to_be64(_PAGE_PRIVILEGED));
Aneesh Kumar K.V34fbadd2016-04-29 23:25:51 +1000633}
634
635/* Encode and de-code a swap entry */
636#define MAX_SWAPFILES_CHECK() do { \
637 BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS); \
638 /* \
639 * Don't have overlapping bits with _PAGE_HPTEFLAGS \
640 * We filter HPTEFLAGS on set_pte. \
641 */ \
642 BUILD_BUG_ON(_PAGE_HPTEFLAGS & (0x1f << _PAGE_BIT_SWAP_TYPE)); \
643 BUILD_BUG_ON(_PAGE_HPTEFLAGS & _PAGE_SWP_SOFT_DIRTY); \
644 } while (0)
645/*
646 * on pte we don't need handle RADIX_TREE_EXCEPTIONAL_SHIFT;
647 */
648#define SWP_TYPE_BITS 5
649#define __swp_type(x) (((x).val >> _PAGE_BIT_SWAP_TYPE) \
650 & ((1UL << SWP_TYPE_BITS) - 1))
651#define __swp_offset(x) (((x).val & PTE_RPN_MASK) >> PAGE_SHIFT)
652#define __swp_entry(type, offset) ((swp_entry_t) { \
653 ((type) << _PAGE_BIT_SWAP_TYPE) \
654 | (((offset) << PAGE_SHIFT) & PTE_RPN_MASK)})
655/*
656 * swp_entry_t must be independent of pte bits. We build a swp_entry_t from
657 * swap type and offset we get from swap and convert that to pte to find a
658 * matching pte in linux page table.
659 * Clear bits not found in swap entries here.
660 */
661#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val((pte)) & ~_PAGE_PTE })
662#define __swp_entry_to_pte(x) __pte((x).val | _PAGE_PTE)
663
664#ifdef CONFIG_MEM_SOFT_DIRTY
665#define _PAGE_SWP_SOFT_DIRTY (1UL << (SWP_TYPE_BITS + _PAGE_BIT_SWAP_TYPE))
666#else
667#define _PAGE_SWP_SOFT_DIRTY 0UL
668#endif /* CONFIG_MEM_SOFT_DIRTY */
669
670#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
671static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
672{
673 return __pte(pte_val(pte) | _PAGE_SWP_SOFT_DIRTY);
674}
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530675
Aneesh Kumar K.V34fbadd2016-04-29 23:25:51 +1000676static inline bool pte_swp_soft_dirty(pte_t pte)
677{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530678 return !!(pte_raw(pte) & cpu_to_be64(_PAGE_SWP_SOFT_DIRTY));
Aneesh Kumar K.V34fbadd2016-04-29 23:25:51 +1000679}
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530680
Aneesh Kumar K.V34fbadd2016-04-29 23:25:51 +1000681static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
682{
683 return __pte(pte_val(pte) & ~_PAGE_SWP_SOFT_DIRTY);
684}
685#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
686
687static inline bool check_pte_access(unsigned long access, unsigned long ptev)
688{
689 /*
690 * This check for _PAGE_RWX and _PAGE_PRESENT bits
691 */
692 if (access & ~ptev)
693 return false;
694 /*
695 * This check for access to privilege space
696 */
697 if ((access & _PAGE_PRIVILEGED) != (ptev & _PAGE_PRIVILEGED))
698 return false;
699
700 return true;
701}
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000702/*
703 * Generic functions with hash/radix callbacks
704 */
705
Aneesh Kumar K.Vc6d1a762016-08-24 15:03:38 +0530706static inline void __ptep_set_access_flags(struct mm_struct *mm,
Aneesh Kumar K.Vb3603e12016-11-28 11:47:02 +0530707 pte_t *ptep, pte_t entry,
708 unsigned long address)
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000709{
710 if (radix_enabled())
Aneesh Kumar K.Vb3603e12016-11-28 11:47:02 +0530711 return radix__ptep_set_access_flags(mm, ptep, entry, address);
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000712 return hash__ptep_set_access_flags(ptep, entry);
713}
714
715#define __HAVE_ARCH_PTE_SAME
716static inline int pte_same(pte_t pte_a, pte_t pte_b)
717{
718 if (radix_enabled())
719 return radix__pte_same(pte_a, pte_b);
720 return hash__pte_same(pte_a, pte_b);
721}
722
723static inline int pte_none(pte_t pte)
724{
725 if (radix_enabled())
726 return radix__pte_none(pte);
727 return hash__pte_none(pte);
728}
729
730static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
731 pte_t *ptep, pte_t pte, int percpu)
732{
733 if (radix_enabled())
734 return radix__set_pte_at(mm, addr, ptep, pte, percpu);
735 return hash__set_pte_at(mm, addr, ptep, pte, percpu);
736}
Aneesh Kumar K.V34fbadd2016-04-29 23:25:51 +1000737
Aneesh Kumar K.V13f829a2016-04-29 23:25:48 +1000738#define _PAGE_CACHE_CTL (_PAGE_NON_IDEMPOTENT | _PAGE_TOLERANT)
739
740#define pgprot_noncached pgprot_noncached
741static inline pgprot_t pgprot_noncached(pgprot_t prot)
742{
743 return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |
744 _PAGE_NON_IDEMPOTENT);
745}
746
747#define pgprot_noncached_wc pgprot_noncached_wc
748static inline pgprot_t pgprot_noncached_wc(pgprot_t prot)
749{
750 return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |
751 _PAGE_TOLERANT);
752}
753
754#define pgprot_cached pgprot_cached
755static inline pgprot_t pgprot_cached(pgprot_t prot)
756{
757 return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL));
758}
759
760#define pgprot_writecombine pgprot_writecombine
761static inline pgprot_t pgprot_writecombine(pgprot_t prot)
762{
763 return pgprot_noncached_wc(prot);
764}
765/*
766 * check a pte mapping have cache inhibited property
767 */
768static inline bool pte_ci(pte_t pte)
769{
770 unsigned long pte_v = pte_val(pte);
771
772 if (((pte_v & _PAGE_CACHE_CTL) == _PAGE_TOLERANT) ||
773 ((pte_v & _PAGE_CACHE_CTL) == _PAGE_NON_IDEMPOTENT))
774 return true;
775 return false;
776}
777
Aneesh Kumar K.Vf281b5d2015-12-01 09:06:35 +0530778static inline void pmd_set(pmd_t *pmdp, unsigned long val)
779{
780 *pmdp = __pmd(val);
781}
782
783static inline void pmd_clear(pmd_t *pmdp)
784{
785 *pmdp = __pmd(0);
786}
787
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530788static inline int pmd_none(pmd_t pmd)
789{
790 return !pmd_raw(pmd);
791}
792
793static inline int pmd_present(pmd_t pmd)
794{
795
796 return !pmd_none(pmd);
797}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530798
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000799static inline int pmd_bad(pmd_t pmd)
800{
801 if (radix_enabled())
802 return radix__pmd_bad(pmd);
803 return hash__pmd_bad(pmd);
804}
805
Aneesh Kumar K.Vf281b5d2015-12-01 09:06:35 +0530806static inline void pud_set(pud_t *pudp, unsigned long val)
807{
808 *pudp = __pud(val);
809}
810
811static inline void pud_clear(pud_t *pudp)
812{
813 *pudp = __pud(0);
814}
815
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530816static inline int pud_none(pud_t pud)
817{
818 return !pud_raw(pud);
819}
820
821static inline int pud_present(pud_t pud)
822{
823 return !pud_none(pud);
824}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530825
826extern struct page *pud_page(pud_t pud);
Aneesh Kumar K.V371352c2015-12-01 09:06:36 +0530827extern struct page *pmd_page(pmd_t pmd);
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530828static inline pte_t pud_pte(pud_t pud)
829{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530830 return __pte_raw(pud_raw(pud));
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530831}
832
833static inline pud_t pte_pud(pte_t pte)
834{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530835 return __pud_raw(pte_raw(pte));
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530836}
837#define pud_write(pud) pte_write(pud_pte(pud))
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000838
839static inline int pud_bad(pud_t pud)
840{
841 if (radix_enabled())
842 return radix__pud_bad(pud);
843 return hash__pud_bad(pud);
844}
845
846
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530847#define pgd_write(pgd) pte_write(pgd_pte(pgd))
Aneesh Kumar K.Vf281b5d2015-12-01 09:06:35 +0530848static inline void pgd_set(pgd_t *pgdp, unsigned long val)
849{
850 *pgdp = __pgd(val);
851}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530852
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530853static inline void pgd_clear(pgd_t *pgdp)
854{
855 *pgdp = __pgd(0);
856}
857
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530858static inline int pgd_none(pgd_t pgd)
859{
860 return !pgd_raw(pgd);
861}
862
863static inline int pgd_present(pgd_t pgd)
864{
865 return !pgd_none(pgd);
866}
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530867
868static inline pte_t pgd_pte(pgd_t pgd)
869{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530870 return __pte_raw(pgd_raw(pgd));
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530871}
872
873static inline pgd_t pte_pgd(pte_t pte)
874{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530875 return __pgd_raw(pte_raw(pte));
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530876}
877
Aneesh Kumar K.Vac94ac792016-04-29 23:25:54 +1000878static inline int pgd_bad(pgd_t pgd)
879{
880 if (radix_enabled())
881 return radix__pgd_bad(pgd);
882 return hash__pgd_bad(pgd);
883}
884
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530885extern struct page *pgd_page(pgd_t pgd);
886
Aneesh Kumar K.Vaba480e2016-04-29 23:25:50 +1000887/* Pointers in the page table tree are physical addresses */
888#define __pgtable_ptr_val(ptr) __pa(ptr)
889
890#define pmd_page_vaddr(pmd) __va(pmd_val(pmd) & ~PMD_MASKED_BITS)
891#define pud_page_vaddr(pud) __va(pud_val(pud) & ~PUD_MASKED_BITS)
892#define pgd_page_vaddr(pgd) __va(pgd_val(pgd) & ~PGD_MASKED_BITS)
893
894#define pgd_index(address) (((address) >> (PGDIR_SHIFT)) & (PTRS_PER_PGD - 1))
895#define pud_index(address) (((address) >> (PUD_SHIFT)) & (PTRS_PER_PUD - 1))
896#define pmd_index(address) (((address) >> (PMD_SHIFT)) & (PTRS_PER_PMD - 1))
897#define pte_index(address) (((address) >> (PAGE_SHIFT)) & (PTRS_PER_PTE - 1))
898
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530899/*
900 * Find an entry in a page-table-directory. We combine the address region
901 * (the high order N bits) and the pgd portion of the address.
902 */
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530903
904#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
905
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530906#define pud_offset(pgdp, addr) \
907 (((pud_t *) pgd_page_vaddr(*(pgdp))) + pud_index(addr))
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530908#define pmd_offset(pudp,addr) \
Aneesh Kumar K.V371352c2015-12-01 09:06:36 +0530909 (((pmd_t *) pud_page_vaddr(*(pudp))) + pmd_index(addr))
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530910#define pte_offset_kernel(dir,addr) \
Aneesh Kumar K.V371352c2015-12-01 09:06:36 +0530911 (((pte_t *) pmd_page_vaddr(*(dir))) + pte_index(addr))
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530912
913#define pte_offset_map(dir,addr) pte_offset_kernel((dir), (addr))
914#define pte_unmap(pte) do { } while(0)
915
916/* to find an entry in a kernel page-table-directory */
917/* This now only contains the vmalloc pages */
918#define pgd_offset_k(address) pgd_offset(&init_mm, address)
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530919
920#define pte_ERROR(e) \
921 pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
922#define pmd_ERROR(e) \
923 pr_err("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
Aneesh Kumar K.V368ced72016-03-01 09:45:13 +0530924#define pud_ERROR(e) \
925 pr_err("%s:%d: bad pud %08lx.\n", __FILE__, __LINE__, pud_val(e))
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530926#define pgd_ERROR(e) \
927 pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
928
Aneesh Kumar K.V31a14fa2016-04-29 23:25:59 +1000929static inline int map_kernel_page(unsigned long ea, unsigned long pa,
930 unsigned long flags)
931{
Aneesh Kumar K.Vd9225ad2016-04-29 23:26:00 +1000932 if (radix_enabled()) {
933#if defined(CONFIG_PPC_RADIX_MMU) && defined(DEBUG_VM)
934 unsigned long page_size = 1 << mmu_psize_defs[mmu_io_psize].shift;
935 WARN((page_size != PAGE_SIZE), "I/O page size != PAGE_SIZE");
936#endif
937 return radix__map_kernel_page(ea, pa, __pgprot(flags), PAGE_SIZE);
938 }
Aneesh Kumar K.V31a14fa2016-04-29 23:25:59 +1000939 return hash__map_kernel_page(ea, pa, flags);
940}
941
942static inline int __meminit vmemmap_create_mapping(unsigned long start,
943 unsigned long page_size,
944 unsigned long phys)
945{
Aneesh Kumar K.Vd9225ad2016-04-29 23:26:00 +1000946 if (radix_enabled())
947 return radix__vmemmap_create_mapping(start, page_size, phys);
Aneesh Kumar K.V31a14fa2016-04-29 23:25:59 +1000948 return hash__vmemmap_create_mapping(start, page_size, phys);
949}
950
951#ifdef CONFIG_MEMORY_HOTPLUG
952static inline void vmemmap_remove_mapping(unsigned long start,
953 unsigned long page_size)
954{
Aneesh Kumar K.Vd9225ad2016-04-29 23:26:00 +1000955 if (radix_enabled())
956 return radix__vmemmap_remove_mapping(start, page_size);
Aneesh Kumar K.V31a14fa2016-04-29 23:25:59 +1000957 return hash__vmemmap_remove_mapping(start, page_size);
958}
959#endif
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530960struct page *realmode_pfn_to_page(unsigned long pfn);
961
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530962static inline pte_t pmd_pte(pmd_t pmd)
963{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530964 return __pte_raw(pmd_raw(pmd));
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530965}
966
967static inline pmd_t pte_pmd(pte_t pte)
968{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +0530969 return __pmd_raw(pte_raw(pte));
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530970}
971
972static inline pte_t *pmdp_ptep(pmd_t *pmd)
973{
974 return (pte_t *)pmd;
975}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530976#define pmd_pfn(pmd) pte_pfn(pmd_pte(pmd))
977#define pmd_dirty(pmd) pte_dirty(pmd_pte(pmd))
978#define pmd_young(pmd) pte_young(pmd_pte(pmd))
979#define pmd_mkold(pmd) pte_pmd(pte_mkold(pmd_pte(pmd)))
980#define pmd_wrprotect(pmd) pte_pmd(pte_wrprotect(pmd_pte(pmd)))
981#define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))
Minchan Kimd5d6a442016-01-15 16:55:29 -0800982#define pmd_mkclean(pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +0530983#define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd)))
984#define pmd_mkwrite(pmd) pte_pmd(pte_mkwrite(pmd_pte(pmd)))
Aneesh Kumar K.Vc137a272017-02-24 14:59:21 -0800985#define pmd_mk_savedwrite(pmd) pte_pmd(pte_mk_savedwrite(pmd_pte(pmd)))
986#define pmd_clear_savedwrite(pmd) pte_pmd(pte_clear_savedwrite(pmd_pte(pmd)))
Laurent Dufour7207f432015-12-03 11:29:19 +0100987
988#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
989#define pmd_soft_dirty(pmd) pte_soft_dirty(pmd_pte(pmd))
990#define pmd_mksoft_dirty(pmd) pte_pmd(pte_mksoft_dirty(pmd_pte(pmd)))
991#define pmd_clear_soft_dirty(pmd) pte_pmd(pte_clear_soft_dirty(pmd_pte(pmd)))
992#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
993
Aneesh Kumar K.V1ca72122015-12-01 09:06:37 +0530994#ifdef CONFIG_NUMA_BALANCING
995static inline int pmd_protnone(pmd_t pmd)
996{
997 return pte_protnone(pmd_pte(pmd));
998}
999#endif /* CONFIG_NUMA_BALANCING */
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301000
1001#define __HAVE_ARCH_PMD_WRITE
1002#define pmd_write(pmd) pte_write(pmd_pte(pmd))
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -08001003#define __pmd_write(pmd) __pte_write(pmd_pte(pmd))
Aneesh Kumar K.Vc137a272017-02-24 14:59:21 -08001004#define pmd_savedwrite(pmd) pte_savedwrite(pmd_pte(pmd))
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301005
Aneesh Kumar K.V6a1ea362016-04-29 23:26:28 +10001006#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1007extern pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot);
1008extern pmd_t mk_pmd(struct page *page, pgprot_t pgprot);
1009extern pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot);
1010extern void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1011 pmd_t *pmdp, pmd_t pmd);
1012extern void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
1013 pmd_t *pmd);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001014extern int hash__has_transparent_hugepage(void);
1015static inline int has_transparent_hugepage(void)
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301016{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001017 if (radix_enabled())
1018 return radix__has_transparent_hugepage();
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001019 return hash__has_transparent_hugepage();
1020}
Linus Torvaldsc04a5882016-05-20 10:12:41 -07001021#define has_transparent_hugepage has_transparent_hugepage
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001022
1023static inline unsigned long
1024pmd_hugepage_update(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp,
1025 unsigned long clr, unsigned long set)
1026{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001027 if (radix_enabled())
1028 return radix__pmd_hugepage_update(mm, addr, pmdp, clr, set);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001029 return hash__pmd_hugepage_update(mm, addr, pmdp, clr, set);
1030}
1031
1032static inline int pmd_large(pmd_t pmd)
1033{
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +05301034 return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001035}
1036
1037static inline pmd_t pmd_mknotpresent(pmd_t pmd)
1038{
1039 return __pmd(pmd_val(pmd) & ~_PAGE_PRESENT);
1040}
1041/*
1042 * For radix we should always find H_PAGE_HASHPTE zero. Hence
1043 * the below will work for radix too
1044 */
1045static inline int __pmdp_test_and_clear_young(struct mm_struct *mm,
1046 unsigned long addr, pmd_t *pmdp)
1047{
1048 unsigned long old;
1049
Aneesh Kumar K.V66c570f2016-07-13 15:05:22 +05301050 if ((pmd_raw(*pmdp) & cpu_to_be64(_PAGE_ACCESSED | H_PAGE_HASHPTE)) == 0)
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001051 return 0;
1052 old = pmd_hugepage_update(mm, addr, pmdp, _PAGE_ACCESSED, 0);
1053 return ((old & _PAGE_ACCESSED) != 0);
1054}
1055
1056#define __HAVE_ARCH_PMDP_SET_WRPROTECT
1057static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr,
1058 pmd_t *pmdp)
1059{
Aneesh Kumar K.Vd19469e2017-03-09 16:16:39 -08001060 if (__pmd_write((*pmdp)))
Aneesh Kumar K.V52c50ca2017-03-09 16:16:36 -08001061 pmd_hugepage_update(mm, addr, pmdp, _PAGE_WRITE, 0);
1062 else if (unlikely(pmd_savedwrite(*pmdp)))
1063 pmd_hugepage_update(mm, addr, pmdp, 0, _PAGE_PRIVILEGED);
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301064}
1065
Aneesh Kumar K.Vab624762016-04-29 23:26:31 +10001066static inline int pmd_trans_huge(pmd_t pmd)
1067{
1068 if (radix_enabled())
1069 return radix__pmd_trans_huge(pmd);
1070 return hash__pmd_trans_huge(pmd);
1071}
1072
1073#define __HAVE_ARCH_PMD_SAME
1074static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
1075{
1076 if (radix_enabled())
1077 return radix__pmd_same(pmd_a, pmd_b);
1078 return hash__pmd_same(pmd_a, pmd_b);
1079}
1080
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301081static inline pmd_t pmd_mkhuge(pmd_t pmd)
1082{
Aneesh Kumar K.Vab624762016-04-29 23:26:31 +10001083 if (radix_enabled())
1084 return radix__pmd_mkhuge(pmd);
1085 return hash__pmd_mkhuge(pmd);
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301086}
1087
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301088#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1089extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1090 unsigned long address, pmd_t *pmdp,
1091 pmd_t entry, int dirty);
1092
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301093#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1094extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1095 unsigned long address, pmd_t *pmdp);
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301096
1097#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001098static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
1099 unsigned long addr, pmd_t *pmdp)
1100{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001101 if (radix_enabled())
1102 return radix__pmdp_huge_get_and_clear(mm, addr, pmdp);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001103 return hash__pmdp_huge_get_and_clear(mm, addr, pmdp);
1104}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301105
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001106static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
1107 unsigned long address, pmd_t *pmdp)
1108{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001109 if (radix_enabled())
1110 return radix__pmdp_collapse_flush(vma, address, pmdp);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001111 return hash__pmdp_collapse_flush(vma, address, pmdp);
1112}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301113#define pmdp_collapse_flush pmdp_collapse_flush
1114
1115#define __HAVE_ARCH_PGTABLE_DEPOSIT
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001116static inline void pgtable_trans_huge_deposit(struct mm_struct *mm,
1117 pmd_t *pmdp, pgtable_t pgtable)
1118{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001119 if (radix_enabled())
1120 return radix__pgtable_trans_huge_deposit(mm, pmdp, pgtable);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001121 return hash__pgtable_trans_huge_deposit(mm, pmdp, pgtable);
1122}
1123
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301124#define __HAVE_ARCH_PGTABLE_WITHDRAW
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001125static inline pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm,
1126 pmd_t *pmdp)
1127{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001128 if (radix_enabled())
1129 return radix__pgtable_trans_huge_withdraw(mm, pmdp);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001130 return hash__pgtable_trans_huge_withdraw(mm, pmdp);
1131}
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301132
1133#define __HAVE_ARCH_PMDP_INVALIDATE
1134extern void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
1135 pmd_t *pmdp);
1136
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05301137#define __HAVE_ARCH_PMDP_HUGE_SPLIT_PREPARE
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001138static inline void pmdp_huge_split_prepare(struct vm_area_struct *vma,
1139 unsigned long address, pmd_t *pmdp)
1140{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001141 if (radix_enabled())
1142 return radix__pmdp_huge_split_prepare(vma, address, pmdp);
Aneesh Kumar K.V3df33f12016-04-29 23:26:29 +10001143 return hash__pmdp_huge_split_prepare(vma, address, pmdp);
1144}
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05301145
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301146#define pmd_move_must_withdraw pmd_move_must_withdraw
1147struct spinlock;
1148static inline int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
Aneesh Kumar K.V1dd38b62016-12-12 16:44:29 -08001149 struct spinlock *old_pmd_ptl,
1150 struct vm_area_struct *vma)
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301151{
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +10001152 if (radix_enabled())
1153 return false;
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301154 /*
1155 * Archs like ppc64 use pgtable to store per pmd
1156 * specific information. So when we switch the pmd,
1157 * we should also withdraw and deposit the pgtable
1158 */
1159 return true;
1160}
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001161
1162
1163#define arch_needs_pgtable_deposit arch_needs_pgtable_deposit
1164static inline bool arch_needs_pgtable_deposit(void)
1165{
1166 if (radix_enabled())
1167 return false;
1168 return true;
1169}
1170
Oliver O'Halloranebd31192017-06-28 11:32:34 +10001171
1172static inline pmd_t pmd_mkdevmap(pmd_t pmd)
1173{
1174 return __pmd(pmd_val(pmd) | (_PAGE_PTE | _PAGE_DEVMAP));
1175}
1176
1177static inline int pmd_devmap(pmd_t pmd)
1178{
1179 return pte_devmap(pmd_pte(pmd));
1180}
1181
1182static inline int pud_devmap(pud_t pud)
1183{
1184 return 0;
1185}
1186
1187static inline int pgd_devmap(pgd_t pgd)
1188{
1189 return 0;
1190}
Aneesh Kumar K.V6a1ea362016-04-29 23:26:28 +10001191#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Oliver O'Halloranebd31192017-06-28 11:32:34 +10001192
1193static inline const int pud_pfn(pud_t pud)
1194{
1195 /*
1196 * Currently all calls to pud_pfn() are gated around a pud_devmap()
1197 * check so this should never be used. If it grows another user we
1198 * want to know about it.
1199 */
1200 BUILD_BUG();
1201 return 0;
1202}
Michael Ellerman029d9252017-07-14 16:51:23 +10001203
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +05301204#endif /* __ASSEMBLY__ */
1205#endif /* _ASM_POWERPC_BOOK3S_64_PGTABLE_H_ */