Merge branch 'xarray' of git://git.infradead.org/users/willy/linux-dax
[sfrench/cifs-2.6.git] / arch / powerpc / include / asm / nohash / 64 / pgtable.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_NOHASH_64_PGTABLE_H
3 #define _ASM_POWERPC_NOHASH_64_PGTABLE_H
4 /*
5  * This file contains the functions and defines necessary to modify and use
6  * the ppc64 non-hashed page table.
7  */
8
9 #include <asm/nohash/64/pgtable-4k.h>
10 #include <asm/barrier.h>
11 #include <asm/asm-const.h>
12
13 #ifdef CONFIG_PPC_64K_PAGES
14 #error "Page size not supported"
15 #endif
16
17 #define FIRST_USER_ADDRESS      0UL
18
19 /*
20  * Size of EA range mapped by our pagetables.
21  */
22 #define PGTABLE_EADDR_SIZE (PTE_INDEX_SIZE + PMD_INDEX_SIZE + \
23                             PUD_INDEX_SIZE + PGD_INDEX_SIZE + PAGE_SHIFT)
24 #define PGTABLE_RANGE (ASM_CONST(1) << PGTABLE_EADDR_SIZE)
25
26 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
27 #define PMD_CACHE_INDEX (PMD_INDEX_SIZE + 1)
28 #else
29 #define PMD_CACHE_INDEX PMD_INDEX_SIZE
30 #endif
31 #define PUD_CACHE_INDEX PUD_INDEX_SIZE
32
33 /*
34  * Define the address range of the kernel non-linear virtual area
35  */
36 #define KERN_VIRT_START ASM_CONST(0x8000000000000000)
37 #define KERN_VIRT_SIZE  ASM_CONST(0x0000100000000000)
38
39 /*
40  * The vmalloc space starts at the beginning of that region, and
41  * occupies a quarter of it on Book3E
42  * (we keep a quarter for the virtual memmap)
43  */
44 #define VMALLOC_START   KERN_VIRT_START
45 #define VMALLOC_SIZE    (KERN_VIRT_SIZE >> 2)
46 #define VMALLOC_END     (VMALLOC_START + VMALLOC_SIZE)
47
48 /*
49  * The second half of the kernel virtual space is used for IO mappings,
50  * it's itself carved into the PIO region (ISA and PHB IO space) and
51  * the ioremap space
52  *
53  *  ISA_IO_BASE = KERN_IO_START, 64K reserved area
54  *  PHB_IO_BASE = ISA_IO_BASE + 64K to ISA_IO_BASE + 2G, PHB IO spaces
55  * IOREMAP_BASE = ISA_IO_BASE + 2G to VMALLOC_START + PGTABLE_RANGE
56  */
57 #define KERN_IO_START   (KERN_VIRT_START + (KERN_VIRT_SIZE >> 1))
58 #define FULL_IO_SIZE    0x80000000ul
59 #define  ISA_IO_BASE    (KERN_IO_START)
60 #define  ISA_IO_END     (KERN_IO_START + 0x10000ul)
61 #define  PHB_IO_BASE    (ISA_IO_END)
62 #define  PHB_IO_END     (KERN_IO_START + FULL_IO_SIZE)
63 #define IOREMAP_BASE    (PHB_IO_END)
64 #define IOREMAP_END     (KERN_VIRT_START + KERN_VIRT_SIZE)
65
66
67 /*
68  * Region IDs
69  */
70 #define REGION_SHIFT            60UL
71 #define REGION_MASK             (0xfUL << REGION_SHIFT)
72 #define REGION_ID(ea)           (((unsigned long)(ea)) >> REGION_SHIFT)
73
74 #define VMALLOC_REGION_ID       (REGION_ID(VMALLOC_START))
75 #define KERNEL_REGION_ID        (REGION_ID(PAGE_OFFSET))
76 #define VMEMMAP_REGION_ID       (0xfUL) /* Server only */
77 #define USER_REGION_ID          (0UL)
78
79 /*
80  * Defines the address of the vmemap area, in its own region on
81  * after the vmalloc space on Book3E
82  */
83 #define VMEMMAP_BASE            VMALLOC_END
84 #define VMEMMAP_END             KERN_IO_START
85 #define vmemmap                 ((struct page *)VMEMMAP_BASE)
86
87
88 /*
89  * Include the PTE bits definitions
90  */
91 #include <asm/nohash/pte-book3e.h>
92
93 #define _PAGE_SAO       0
94
95 #define PTE_RPN_MASK    (~((1UL << PTE_RPN_SHIFT) - 1))
96
97 /*
98  * _PAGE_CHG_MASK masks of bits that are to be preserved across
99  * pgprot changes.
100  */
101 #define _PAGE_CHG_MASK  (PTE_RPN_MASK | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SPECIAL)
102
103 #define H_PAGE_4K_PFN 0
104
105 #ifndef __ASSEMBLY__
106 /* pte_clear moved to later in this file */
107
108 static inline pte_t pte_mkwrite(pte_t pte)
109 {
110         return __pte(pte_val(pte) | _PAGE_RW);
111 }
112
113 static inline pte_t pte_mkdirty(pte_t pte)
114 {
115         return __pte(pte_val(pte) | _PAGE_DIRTY);
116 }
117
118 static inline pte_t pte_mkyoung(pte_t pte)
119 {
120         return __pte(pte_val(pte) | _PAGE_ACCESSED);
121 }
122
123 static inline pte_t pte_wrprotect(pte_t pte)
124 {
125         return __pte(pte_val(pte) & ~_PAGE_RW);
126 }
127
128 static inline pte_t pte_mkexec(pte_t pte)
129 {
130         return __pte(pte_val(pte) | _PAGE_EXEC);
131 }
132
133 #define PMD_BAD_BITS            (PTE_TABLE_SIZE-1)
134 #define PUD_BAD_BITS            (PMD_TABLE_SIZE-1)
135
136 static inline void pmd_set(pmd_t *pmdp, unsigned long val)
137 {
138         *pmdp = __pmd(val);
139 }
140
141 static inline void pmd_clear(pmd_t *pmdp)
142 {
143         *pmdp = __pmd(0);
144 }
145
146 static inline pte_t pmd_pte(pmd_t pmd)
147 {
148         return __pte(pmd_val(pmd));
149 }
150
151 #define pmd_none(pmd)           (!pmd_val(pmd))
152 #define pmd_bad(pmd)            (!is_kernel_addr(pmd_val(pmd)) \
153                                  || (pmd_val(pmd) & PMD_BAD_BITS))
154 #define pmd_present(pmd)        (!pmd_none(pmd))
155 #define pmd_page_vaddr(pmd)     (pmd_val(pmd) & ~PMD_MASKED_BITS)
156 extern struct page *pmd_page(pmd_t pmd);
157
158 static inline void pud_set(pud_t *pudp, unsigned long val)
159 {
160         *pudp = __pud(val);
161 }
162
163 static inline void pud_clear(pud_t *pudp)
164 {
165         *pudp = __pud(0);
166 }
167
168 #define pud_none(pud)           (!pud_val(pud))
169 #define pud_bad(pud)            (!is_kernel_addr(pud_val(pud)) \
170                                  || (pud_val(pud) & PUD_BAD_BITS))
171 #define pud_present(pud)        (pud_val(pud) != 0)
172 #define pud_page_vaddr(pud)     (pud_val(pud) & ~PUD_MASKED_BITS)
173
174 extern struct page *pud_page(pud_t pud);
175
176 static inline pte_t pud_pte(pud_t pud)
177 {
178         return __pte(pud_val(pud));
179 }
180
181 static inline pud_t pte_pud(pte_t pte)
182 {
183         return __pud(pte_val(pte));
184 }
185 #define pud_write(pud)          pte_write(pud_pte(pud))
186 #define pgd_write(pgd)          pte_write(pgd_pte(pgd))
187
188 static inline void pgd_set(pgd_t *pgdp, unsigned long val)
189 {
190         *pgdp = __pgd(val);
191 }
192
193 /*
194  * Find an entry in a page-table-directory.  We combine the address region
195  * (the high order N bits) and the pgd portion of the address.
196  */
197 #define pgd_index(address) (((address) >> (PGDIR_SHIFT)) & (PTRS_PER_PGD - 1))
198
199 #define pgd_offset(mm, address)  ((mm)->pgd + pgd_index(address))
200
201 #define pmd_offset(pudp,addr) \
202   (((pmd_t *) pud_page_vaddr(*(pudp))) + (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)))
203
204 #define pte_offset_kernel(dir,addr) \
205   (((pte_t *) pmd_page_vaddr(*(dir))) + (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)))
206
207 #define pte_offset_map(dir,addr)        pte_offset_kernel((dir), (addr))
208 #define pte_unmap(pte)                  do { } while(0)
209
210 /* to find an entry in a kernel page-table-directory */
211 /* This now only contains the vmalloc pages */
212 #define pgd_offset_k(address) pgd_offset(&init_mm, address)
213
214 /* Atomic PTE updates */
215 static inline unsigned long pte_update(struct mm_struct *mm,
216                                        unsigned long addr,
217                                        pte_t *ptep, unsigned long clr,
218                                        unsigned long set,
219                                        int huge)
220 {
221 #ifdef PTE_ATOMIC_UPDATES
222         unsigned long old, tmp;
223
224         __asm__ __volatile__(
225         "1:     ldarx   %0,0,%3         # pte_update\n\
226         andc    %1,%0,%4 \n\
227         or      %1,%1,%6\n\
228         stdcx.  %1,0,%3 \n\
229         bne-    1b"
230         : "=&r" (old), "=&r" (tmp), "=m" (*ptep)
231         : "r" (ptep), "r" (clr), "m" (*ptep), "r" (set)
232         : "cc" );
233 #else
234         unsigned long old = pte_val(*ptep);
235         *ptep = __pte((old & ~clr) | set);
236 #endif
237         /* huge pages use the old page table lock */
238         if (!huge)
239                 assert_pte_locked(mm, addr);
240
241         return old;
242 }
243
244 static inline int pte_young(pte_t pte)
245 {
246         return pte_val(pte) & _PAGE_ACCESSED;
247 }
248
249 static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
250                                               unsigned long addr, pte_t *ptep)
251 {
252         unsigned long old;
253
254         if (pte_young(*ptep))
255                 return 0;
256         old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);
257         return (old & _PAGE_ACCESSED) != 0;
258 }
259 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
260 #define ptep_test_and_clear_young(__vma, __addr, __ptep)                   \
261 ({                                                                         \
262         int __r;                                                           \
263         __r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
264         __r;                                                               \
265 })
266
267 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
268 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
269                                       pte_t *ptep)
270 {
271
272         if ((pte_val(*ptep) & _PAGE_RW) == 0)
273                 return;
274
275         pte_update(mm, addr, ptep, _PAGE_RW, 0, 0);
276 }
277
278 #define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT
279 static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
280                                            unsigned long addr, pte_t *ptep)
281 {
282         if ((pte_val(*ptep) & _PAGE_RW) == 0)
283                 return;
284
285         pte_update(mm, addr, ptep, _PAGE_RW, 0, 1);
286 }
287
288 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
289 #define ptep_clear_flush_young(__vma, __address, __ptep)                \
290 ({                                                                      \
291         int __young = __ptep_test_and_clear_young((__vma)->vm_mm, __address, \
292                                                   __ptep);              \
293         __young;                                                        \
294 })
295
296 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
297 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
298                                        unsigned long addr, pte_t *ptep)
299 {
300         unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
301         return __pte(old);
302 }
303
304 static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
305                              pte_t * ptep)
306 {
307         pte_update(mm, addr, ptep, ~0UL, 0, 0);
308 }
309
310
311 /* Set the dirty and/or accessed bits atomically in a linux PTE */
312 static inline void __ptep_set_access_flags(struct vm_area_struct *vma,
313                                            pte_t *ptep, pte_t entry,
314                                            unsigned long address,
315                                            int psize)
316 {
317         unsigned long bits = pte_val(entry) &
318                 (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);
319
320 #ifdef PTE_ATOMIC_UPDATES
321         unsigned long old, tmp;
322
323         __asm__ __volatile__(
324         "1:     ldarx   %0,0,%4\n\
325                 or      %0,%3,%0\n\
326                 stdcx.  %0,0,%4\n\
327                 bne-    1b"
328         :"=&r" (old), "=&r" (tmp), "=m" (*ptep)
329         :"r" (bits), "r" (ptep), "m" (*ptep)
330         :"cc");
331 #else
332         unsigned long old = pte_val(*ptep);
333         *ptep = __pte(old | bits);
334 #endif
335
336         flush_tlb_page(vma, address);
337 }
338
339 #define __HAVE_ARCH_PTE_SAME
340 #define pte_same(A,B)   ((pte_val(A) ^ pte_val(B)) == 0)
341
342 #define pte_ERROR(e) \
343         pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
344 #define pmd_ERROR(e) \
345         pr_err("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
346 #define pgd_ERROR(e) \
347         pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
348
349 /* Encode and de-code a swap entry */
350 #define MAX_SWAPFILES_CHECK() do { \
351         BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS); \
352         } while (0)
353
354 #define SWP_TYPE_BITS 5
355 #define __swp_type(x)           (((x).val >> _PAGE_BIT_SWAP_TYPE) \
356                                 & ((1UL << SWP_TYPE_BITS) - 1))
357 #define __swp_offset(x)         ((x).val >> PTE_RPN_SHIFT)
358 #define __swp_entry(type, offset)       ((swp_entry_t) { \
359                                         ((type) << _PAGE_BIT_SWAP_TYPE) \
360                                         | ((offset) << PTE_RPN_SHIFT) })
361
362 #define __pte_to_swp_entry(pte)         ((swp_entry_t) { pte_val((pte)) })
363 #define __swp_entry_to_pte(x)           __pte((x).val)
364
365 int map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot);
366 extern int __meminit vmemmap_create_mapping(unsigned long start,
367                                             unsigned long page_size,
368                                             unsigned long phys);
369 extern void vmemmap_remove_mapping(unsigned long start,
370                                    unsigned long page_size);
371 #endif /* __ASSEMBLY__ */
372
373 #endif /* _ASM_POWERPC_NOHASH_64_PGTABLE_H */