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 <linux/sizes.h>
10 
11 #include <asm/nohash/64/pgtable-4k.h>
12 #include <asm/barrier.h>
13 #include <asm/asm-const.h>
14 
15 /*
16  * Size of EA range mapped by our pagetables.
17  */
18 #define PGTABLE_EADDR_SIZE (PTE_INDEX_SIZE + PMD_INDEX_SIZE + \
19 			    PUD_INDEX_SIZE + PGD_INDEX_SIZE + PAGE_SHIFT)
20 #define PGTABLE_RANGE (ASM_CONST(1) << PGTABLE_EADDR_SIZE)
21 
22 #define PMD_CACHE_INDEX	PMD_INDEX_SIZE
23 #define PUD_CACHE_INDEX PUD_INDEX_SIZE
24 
25 /*
26  * Define the address range of the kernel non-linear virtual area
27  */
28 #define KERN_VIRT_START ASM_CONST(0xc000100000000000)
29 #define KERN_VIRT_SIZE	ASM_CONST(0x0000100000000000)
30 
31 /*
32  * The vmalloc space starts at the beginning of that region, and
33  * occupies a quarter of it on Book3E
34  * (we keep a quarter for the virtual memmap)
35  */
36 #define VMALLOC_START	KERN_VIRT_START
37 #define VMALLOC_SIZE	(KERN_VIRT_SIZE >> 2)
38 #define VMALLOC_END	(VMALLOC_START + VMALLOC_SIZE)
39 
40 /*
41  * The second half of the kernel virtual space is used for IO mappings,
42  * it's itself carved into the PIO region (ISA and PHB IO space) and
43  * the ioremap space
44  *
45  *  ISA_IO_BASE = KERN_IO_START, 64K reserved area
46  *  PHB_IO_BASE = ISA_IO_BASE + 64K to ISA_IO_BASE + 2G, PHB IO spaces
47  * IOREMAP_BASE = ISA_IO_BASE + 2G to VMALLOC_START + PGTABLE_RANGE
48  */
49 #define KERN_IO_START	(KERN_VIRT_START + (KERN_VIRT_SIZE >> 1))
50 #define FULL_IO_SIZE	0x80000000ul
51 #define  ISA_IO_BASE	(KERN_IO_START)
52 #define  ISA_IO_END	(KERN_IO_START + 0x10000ul)
53 #define  PHB_IO_BASE	(ISA_IO_END)
54 #define  PHB_IO_END	(KERN_IO_START + FULL_IO_SIZE)
55 #define IOREMAP_BASE	(PHB_IO_END)
56 #define IOREMAP_START	(ioremap_bot)
57 #define IOREMAP_END	(KERN_VIRT_START + KERN_VIRT_SIZE - FIXADDR_SIZE)
58 #define FIXADDR_SIZE	SZ_32M
59 
60 /*
61  * Defines the address of the vmemap area, in its own region on
62  * after the vmalloc space on Book3E
63  */
64 #define VMEMMAP_BASE		VMALLOC_END
65 #define VMEMMAP_END		KERN_IO_START
66 #define vmemmap			((struct page *)VMEMMAP_BASE)
67 
68 
69 /*
70  * Include the PTE bits definitions
71  */
72 #include <asm/nohash/pte-book3e.h>
73 
74 #define PTE_RPN_MASK	(~((1UL << PTE_RPN_SHIFT) - 1))
75 
76 /*
77  * _PAGE_CHG_MASK masks of bits that are to be preserved across
78  * pgprot changes.
79  */
80 #define _PAGE_CHG_MASK	(PTE_RPN_MASK | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SPECIAL)
81 
82 #define H_PAGE_4K_PFN 0
83 
84 #ifndef __ASSEMBLY__
85 /* pte_clear moved to later in this file */
86 
87 static inline pte_t pte_mkwrite(pte_t pte)
88 {
89 	return __pte(pte_val(pte) | _PAGE_RW);
90 }
91 
92 static inline pte_t pte_mkdirty(pte_t pte)
93 {
94 	return __pte(pte_val(pte) | _PAGE_DIRTY);
95 }
96 
97 static inline pte_t pte_mkyoung(pte_t pte)
98 {
99 	return __pte(pte_val(pte) | _PAGE_ACCESSED);
100 }
101 
102 static inline pte_t pte_wrprotect(pte_t pte)
103 {
104 	return __pte(pte_val(pte) & ~_PAGE_RW);
105 }
106 
107 #define PMD_BAD_BITS		(PTE_TABLE_SIZE-1)
108 #define PUD_BAD_BITS		(PMD_TABLE_SIZE-1)
109 
110 static inline void pmd_set(pmd_t *pmdp, unsigned long val)
111 {
112 	*pmdp = __pmd(val);
113 }
114 
115 static inline void pmd_clear(pmd_t *pmdp)
116 {
117 	*pmdp = __pmd(0);
118 }
119 
120 static inline pte_t pmd_pte(pmd_t pmd)
121 {
122 	return __pte(pmd_val(pmd));
123 }
124 
125 #define pmd_none(pmd)		(!pmd_val(pmd))
126 #define	pmd_bad(pmd)		(!is_kernel_addr(pmd_val(pmd)) \
127 				 || (pmd_val(pmd) & PMD_BAD_BITS))
128 #define	pmd_present(pmd)	(!pmd_none(pmd))
129 #define pmd_page_vaddr(pmd)	(pmd_val(pmd) & ~PMD_MASKED_BITS)
130 extern struct page *pmd_page(pmd_t pmd);
131 #define pmd_pfn(pmd)		(page_to_pfn(pmd_page(pmd)))
132 
133 static inline void pud_set(pud_t *pudp, unsigned long val)
134 {
135 	*pudp = __pud(val);
136 }
137 
138 static inline void pud_clear(pud_t *pudp)
139 {
140 	*pudp = __pud(0);
141 }
142 
143 #define pud_none(pud)		(!pud_val(pud))
144 #define	pud_bad(pud)		(!is_kernel_addr(pud_val(pud)) \
145 				 || (pud_val(pud) & PUD_BAD_BITS))
146 #define pud_present(pud)	(pud_val(pud) != 0)
147 
148 static inline pmd_t *pud_pgtable(pud_t pud)
149 {
150 	return (pmd_t *)(pud_val(pud) & ~PUD_MASKED_BITS);
151 }
152 
153 extern struct page *pud_page(pud_t pud);
154 
155 static inline pte_t pud_pte(pud_t pud)
156 {
157 	return __pte(pud_val(pud));
158 }
159 
160 static inline pud_t pte_pud(pte_t pte)
161 {
162 	return __pud(pte_val(pte));
163 }
164 #define pud_write(pud)		pte_write(pud_pte(pud))
165 #define p4d_write(pgd)		pte_write(p4d_pte(p4d))
166 
167 static inline void p4d_set(p4d_t *p4dp, unsigned long val)
168 {
169 	*p4dp = __p4d(val);
170 }
171 
172 /* Atomic PTE updates */
173 static inline unsigned long pte_update(struct mm_struct *mm,
174 				       unsigned long addr,
175 				       pte_t *ptep, unsigned long clr,
176 				       unsigned long set,
177 				       int huge)
178 {
179 	unsigned long old = pte_val(*ptep);
180 	*ptep = __pte((old & ~clr) | set);
181 
182 	/* huge pages use the old page table lock */
183 	if (!huge)
184 		assert_pte_locked(mm, addr);
185 
186 	return old;
187 }
188 
189 static inline int pte_young(pte_t pte)
190 {
191 	return pte_val(pte) & _PAGE_ACCESSED;
192 }
193 
194 static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
195 					      unsigned long addr, pte_t *ptep)
196 {
197 	unsigned long old;
198 
199 	if (pte_young(*ptep))
200 		return 0;
201 	old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);
202 	return (old & _PAGE_ACCESSED) != 0;
203 }
204 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
205 #define ptep_test_and_clear_young(__vma, __addr, __ptep)		   \
206 ({									   \
207 	int __r;							   \
208 	__r = __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep); \
209 	__r;								   \
210 })
211 
212 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
213 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
214 				      pte_t *ptep)
215 {
216 
217 	if ((pte_val(*ptep) & _PAGE_RW) == 0)
218 		return;
219 
220 	pte_update(mm, addr, ptep, _PAGE_RW, 0, 0);
221 }
222 
223 #define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT
224 static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
225 					   unsigned long addr, pte_t *ptep)
226 {
227 	if ((pte_val(*ptep) & _PAGE_RW) == 0)
228 		return;
229 
230 	pte_update(mm, addr, ptep, _PAGE_RW, 0, 1);
231 }
232 
233 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
234 #define ptep_clear_flush_young(__vma, __address, __ptep)		\
235 ({									\
236 	int __young = __ptep_test_and_clear_young((__vma)->vm_mm, __address, \
237 						  __ptep);		\
238 	__young;							\
239 })
240 
241 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
242 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
243 				       unsigned long addr, pte_t *ptep)
244 {
245 	unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
246 	return __pte(old);
247 }
248 
249 static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
250 			     pte_t * ptep)
251 {
252 	pte_update(mm, addr, ptep, ~0UL, 0, 0);
253 }
254 
255 
256 /* Set the dirty and/or accessed bits atomically in a linux PTE */
257 static inline void __ptep_set_access_flags(struct vm_area_struct *vma,
258 					   pte_t *ptep, pte_t entry,
259 					   unsigned long address,
260 					   int psize)
261 {
262 	unsigned long bits = pte_val(entry) &
263 		(_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);
264 
265 	unsigned long old = pte_val(*ptep);
266 	*ptep = __pte(old | bits);
267 
268 	flush_tlb_page(vma, address);
269 }
270 
271 #define pte_ERROR(e) \
272 	pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
273 #define pmd_ERROR(e) \
274 	pr_err("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
275 #define pgd_ERROR(e) \
276 	pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
277 
278 /* Encode and de-code a swap entry */
279 #define MAX_SWAPFILES_CHECK() do { \
280 	BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS); \
281 	} while (0)
282 
283 #define SWP_TYPE_BITS 5
284 #define __swp_type(x)		(((x).val >> _PAGE_BIT_SWAP_TYPE) \
285 				& ((1UL << SWP_TYPE_BITS) - 1))
286 #define __swp_offset(x)		((x).val >> PTE_RPN_SHIFT)
287 #define __swp_entry(type, offset)	((swp_entry_t) { \
288 					((type) << _PAGE_BIT_SWAP_TYPE) \
289 					| ((offset) << PTE_RPN_SHIFT) })
290 
291 #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val((pte)) })
292 #define __swp_entry_to_pte(x)		__pte((x).val)
293 
294 int map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot);
295 void unmap_kernel_page(unsigned long va);
296 extern int __meminit vmemmap_create_mapping(unsigned long start,
297 					    unsigned long page_size,
298 					    unsigned long phys);
299 extern void vmemmap_remove_mapping(unsigned long start,
300 				   unsigned long page_size);
301 void __patch_exception(int exc, unsigned long addr);
302 #define patch_exception(exc, name) do { \
303 	extern unsigned int name; \
304 	__patch_exception((exc), (unsigned long)&name); \
305 } while (0)
306 
307 #endif /* __ASSEMBLY__ */
308 
309 #endif /* _ASM_POWERPC_NOHASH_64_PGTABLE_H */
310