xref: /linux/arch/hexagon/include/asm/pgtable.h (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Page table support for the Hexagon architecture
4  *
5  * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
6  */
7 
8 #ifndef _ASM_PGTABLE_H
9 #define _ASM_PGTABLE_H
10 
11 /*
12  * Page table definitions for Qualcomm Hexagon processor.
13  */
14 #include <asm/page.h>
15 #include <asm-generic/pgtable-nopmd.h>
16 
17 /*
18  * The PTE model described here is that of the Hexagon Virtual Machine,
19  * which autonomously walks 2-level page tables.  At a lower level, we
20  * also describe the RISCish software-loaded TLB entry structure of
21  * the underlying Hexagon processor. A kernel built to run on the
22  * virtual machine has no need to know about the underlying hardware.
23  */
24 #include <asm/vm_mmu.h>
25 
26 /*
27  * To maximize the comfort level for the PTE manipulation macros,
28  * define the "well known" architecture-specific bits.
29  */
30 #define _PAGE_READ	__HVM_PTE_R
31 #define _PAGE_WRITE	__HVM_PTE_W
32 #define _PAGE_EXECUTE	__HVM_PTE_X
33 #define _PAGE_USER	__HVM_PTE_U
34 
35 /*
36  * We have a total of 4 "soft" bits available in the abstract PTE.
37  * The two mandatory software bits are Dirty and Accessed.
38  * To make nonlinear swap work according to the more recent
39  * model, we want a low order "Present" bit to indicate whether
40  * the PTE describes MMU programming or swap space.
41  */
42 #define _PAGE_PRESENT	(1<<0)
43 #define _PAGE_DIRTY	(1<<1)
44 #define _PAGE_ACCESSED	(1<<2)
45 
46 /*
47  * For now, let's say that Valid and Present are the same thing.
48  * Alternatively, we could say that it's the "or" of R, W, and X
49  * permissions.
50  */
51 #define _PAGE_VALID	_PAGE_PRESENT
52 
53 /*
54  * We're not defining _PAGE_GLOBAL here, since there's no concept
55  * of global pages or ASIDs exposed to the Hexagon Virtual Machine,
56  * and we want to use the same page table structures and macros in
57  * the native kernel as we do in the virtual machine kernel.
58  * So we'll put up with a bit of inefficiency for now...
59  */
60 
61 /* We borrow bit 6 to store the exclusive marker in swap PTEs. */
62 #define _PAGE_SWP_EXCLUSIVE	(1<<6)
63 
64 /*
65  * Top "FOURTH" level (pgd), which for the Hexagon VM is really
66  * only the second from the bottom, pgd and pud both being collapsed.
67  * Each entry represents 4MB of virtual address space, 4K of table
68  * thus maps the full 4GB.
69  */
70 #define PGDIR_SHIFT 22
71 #define PTRS_PER_PGD 1024
72 
73 #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
74 #define PGDIR_MASK (~(PGDIR_SIZE-1))
75 
76 #ifdef CONFIG_PAGE_SIZE_4KB
77 #define PTRS_PER_PTE 1024
78 #endif
79 
80 #ifdef CONFIG_PAGE_SIZE_16KB
81 #define PTRS_PER_PTE 256
82 #endif
83 
84 #ifdef CONFIG_PAGE_SIZE_64KB
85 #define PTRS_PER_PTE 64
86 #endif
87 
88 #ifdef CONFIG_PAGE_SIZE_256KB
89 #define PTRS_PER_PTE 16
90 #endif
91 
92 #ifdef CONFIG_PAGE_SIZE_1MB
93 #define PTRS_PER_PTE 4
94 #endif
95 
96 /*  Any bigger and the PTE disappears.  */
97 
98 /*
99  * Page Protection Constants. Includes (in this variant) cache attributes.
100  */
101 extern unsigned long _dflt_cache_att;
102 
103 #define PAGE_NONE	__pgprot(_PAGE_PRESENT | _PAGE_USER | \
104 				_dflt_cache_att)
105 #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_USER | \
106 				_PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)
107 #define PAGE_COPY	PAGE_READONLY
108 #define PAGE_EXEC	__pgprot(_PAGE_PRESENT | _PAGE_USER | \
109 				_PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)
110 #define PAGE_COPY_EXEC	PAGE_EXEC
111 #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | \
112 				_PAGE_EXECUTE | _PAGE_WRITE | _dflt_cache_att)
113 #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | _PAGE_READ | \
114 				_PAGE_WRITE | _PAGE_EXECUTE | _dflt_cache_att)
115 
116 
117 /*
118  * Aliases for mapping mmap() protection bits to page protections.
119  * These get used for static initialization, so using the _dflt_cache_att
120  * variable for the default cache attribute isn't workable. If the
121  * default gets changed at boot time, the boot option code has to
122  * update data structures like the protaction_map[] array.
123  */
124 #define CACHEDEF	(CACHE_DEFAULT << 6)
125 
126 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];  /* located in head.S */
127 
128 /*  HUGETLB not working currently  */
129 #ifdef CONFIG_HUGETLB_PAGE
130 #define pte_mkhuge(pte) __pte((pte_val(pte) & ~0x3) | HVM_HUGEPAGE_SIZE)
131 #endif
132 
133 /*
134  * For now, assume that higher-level code will do TLB/MMU invalidations
135  * and don't insert that overhead into this low-level function.
136  */
137 extern void sync_icache_dcache(pte_t pte);
138 
139 #define pte_present_exec_user(pte) \
140 	((pte_val(pte) & (_PAGE_EXECUTE | _PAGE_USER)) == \
141 	(_PAGE_EXECUTE | _PAGE_USER))
142 
143 static inline void set_pte(pte_t *ptep, pte_t pteval)
144 {
145 	/*  should really be using pte_exec, if it weren't declared later. */
146 	if (pte_present_exec_user(pteval))
147 		sync_icache_dcache(pteval);
148 
149 	*ptep = pteval;
150 }
151 
152 /*
153  * For the Hexagon Virtual Machine MMU (or its emulation), a null/invalid
154  * L1 PTE (PMD/PGD) has 7 in the least significant bits. For the L2 PTE
155  * (Linux PTE), the key is to have bits 11..9 all zero.  We'd use 0x7
156  * as a universal null entry, but some of those least significant bits
157  * are interpreted by software.
158  */
159 #define _NULL_PMD	0x7
160 #define _NULL_PTE	0x0
161 
162 static inline void pmd_clear(pmd_t *pmd_entry_ptr)
163 {
164 	 pmd_val(*pmd_entry_ptr) = _NULL_PMD;
165 }
166 
167 /*
168  * Conveniently, a null PTE value is invalid.
169  */
170 static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
171 				pte_t *ptep)
172 {
173 	pte_val(*ptep) = _NULL_PTE;
174 }
175 
176 /**
177  * pmd_none - check if pmd_entry is mapped
178  * @pmd_entry:  pmd entry
179  *
180  * MIPS checks it against that "invalid pte table" thing.
181  */
182 static inline int pmd_none(pmd_t pmd)
183 {
184 	return pmd_val(pmd) == _NULL_PMD;
185 }
186 
187 /**
188  * pmd_present - is there a page table behind this?
189  * Essentially the inverse of pmd_none.  We maybe
190  * save an inline instruction by defining it this
191  * way, instead of simply "!pmd_none".
192  */
193 static inline int pmd_present(pmd_t pmd)
194 {
195 	return pmd_val(pmd) != (unsigned long)_NULL_PMD;
196 }
197 
198 /**
199  * pmd_bad - check if a PMD entry is "bad". That might mean swapped out.
200  * As we have no known cause of badness, it's null, as it is for many
201  * architectures.
202  */
203 static inline int pmd_bad(pmd_t pmd)
204 {
205 	return 0;
206 }
207 
208 /*
209  * pmd_pfn - converts a PMD entry to a page frame number
210  */
211 #define pmd_pfn(pmd)  (pmd_val(pmd) >> PAGE_SHIFT)
212 
213 /*
214  * pmd_page - converts a PMD entry to a page pointer
215  */
216 #define pmd_page(pmd)  (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
217 
218 /**
219  * pte_none - check if pte is mapped
220  * @pte: pte_t entry
221  */
222 static inline int pte_none(pte_t pte)
223 {
224 	return pte_val(pte) == _NULL_PTE;
225 };
226 
227 /*
228  * pte_present - check if page is present
229  */
230 static inline int pte_present(pte_t pte)
231 {
232 	return pte_val(pte) & _PAGE_PRESENT;
233 }
234 
235 /* pte_page - returns a page (frame pointer/descriptor?) based on a PTE */
236 #define pte_page(x) pfn_to_page(pte_pfn(x))
237 
238 /* pte_mkold - mark PTE as not recently accessed */
239 static inline pte_t pte_mkold(pte_t pte)
240 {
241 	pte_val(pte) &= ~_PAGE_ACCESSED;
242 	return pte;
243 }
244 
245 /* pte_mkyoung - mark PTE as recently accessed */
246 static inline pte_t pte_mkyoung(pte_t pte)
247 {
248 	pte_val(pte) |= _PAGE_ACCESSED;
249 	return pte;
250 }
251 
252 /* pte_mkclean - mark page as in sync with backing store */
253 static inline pte_t pte_mkclean(pte_t pte)
254 {
255 	pte_val(pte) &= ~_PAGE_DIRTY;
256 	return pte;
257 }
258 
259 /* pte_mkdirty - mark page as modified */
260 static inline pte_t pte_mkdirty(pte_t pte)
261 {
262 	pte_val(pte) |= _PAGE_DIRTY;
263 	return pte;
264 }
265 
266 /* pte_young - "is PTE marked as accessed"? */
267 static inline int pte_young(pte_t pte)
268 {
269 	return pte_val(pte) & _PAGE_ACCESSED;
270 }
271 
272 /* pte_dirty - "is PTE dirty?" */
273 static inline int pte_dirty(pte_t pte)
274 {
275 	return pte_val(pte) & _PAGE_DIRTY;
276 }
277 
278 /* pte_modify - set protection bits on PTE */
279 static inline pte_t pte_modify(pte_t pte, pgprot_t prot)
280 {
281 	pte_val(pte) &= PAGE_MASK;
282 	pte_val(pte) |= pgprot_val(prot);
283 	return pte;
284 }
285 
286 /* pte_wrprotect - mark page as not writable */
287 static inline pte_t pte_wrprotect(pte_t pte)
288 {
289 	pte_val(pte) &= ~_PAGE_WRITE;
290 	return pte;
291 }
292 
293 /* pte_mkwrite - mark page as writable */
294 static inline pte_t pte_mkwrite_novma(pte_t pte)
295 {
296 	pte_val(pte) |= _PAGE_WRITE;
297 	return pte;
298 }
299 
300 /* pte_mkexec - mark PTE as executable */
301 static inline pte_t pte_mkexec(pte_t pte)
302 {
303 	pte_val(pte) |= _PAGE_EXECUTE;
304 	return pte;
305 }
306 
307 /* pte_read - "is PTE marked as readable?" */
308 static inline int pte_read(pte_t pte)
309 {
310 	return pte_val(pte) & _PAGE_READ;
311 }
312 
313 /* pte_write - "is PTE marked as writable?" */
314 static inline int pte_write(pte_t pte)
315 {
316 	return pte_val(pte) & _PAGE_WRITE;
317 }
318 
319 
320 /* pte_exec - "is PTE marked as executable?" */
321 static inline int pte_exec(pte_t pte)
322 {
323 	return pte_val(pte) & _PAGE_EXECUTE;
324 }
325 
326 /* __pte_to_swp_entry - extract swap entry from PTE */
327 #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
328 
329 /* __swp_entry_to_pte - extract PTE from swap entry */
330 #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
331 
332 #define PFN_PTE_SHIFT	PAGE_SHIFT
333 /* pfn_pte - convert page number and protection value to page table entry */
334 #define pfn_pte(pfn, pgprot) __pte((pfn << PAGE_SHIFT) | pgprot_val(pgprot))
335 
336 /* pte_pfn - convert pte to page frame number */
337 #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
338 #define set_pmd(pmdptr, pmdval) (*(pmdptr) = (pmdval))
339 
340 static inline unsigned long pmd_page_vaddr(pmd_t pmd)
341 {
342 	return (unsigned long)__va(pmd_val(pmd) & PAGE_MASK);
343 }
344 
345 /*
346  * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
347  * are !pte_none() && !pte_present().
348  *
349  * Swap/file PTE definitions.  If _PAGE_PRESENT is zero, the rest of the PTE is
350  * interpreted as swap information.  The remaining free bits are interpreted as
351  * listed below.  Rather than have the TLB fill handler test
352  * _PAGE_PRESENT, we're going to reserve the permissions bits and set them to
353  * all zeros for swap entries, which speeds up the miss handler at the cost of
354  * 3 bits of offset.  That trade-off can be revisited if necessary, but Hexagon
355  * processor architecture and target applications suggest a lot of TLB misses
356  * and not much swap space.
357  *
358  * Format of swap PTE:
359  *	bit	0:	Present (zero)
360  *	bits	1-5:	swap type (arch independent layer uses 5 bits max)
361  *	bit	6:	exclusive marker
362  *	bits	7-9:	bits 2:0 of offset
363  *	bits	10-12:	effectively _PAGE_PROTNONE (all zero)
364  *	bits	13-31:  bits 21:3 of swap offset
365  *
366  * The split offset makes some of the following macros a little gnarly,
367  * but there's plenty of precedent for this sort of thing.
368  */
369 
370 /* Used for swap PTEs */
371 #define __swp_type(swp_pte)		(((swp_pte).val >> 1) & 0x1f)
372 
373 #define __swp_offset(swp_pte) \
374 	((((swp_pte).val >> 7) & 0x7) | (((swp_pte).val >> 10) & 0x3ffff8))
375 
376 #define __swp_entry(type, offset) \
377 	((swp_entry_t)	{ \
378 		(((type & 0x1f) << 1) | \
379 		 ((offset & 0x3ffff8) << 10) | ((offset & 0x7) << 7)) })
380 
381 static inline bool pte_swp_exclusive(pte_t pte)
382 {
383 	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
384 }
385 
386 static inline pte_t pte_swp_mkexclusive(pte_t pte)
387 {
388 	pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
389 	return pte;
390 }
391 
392 static inline pte_t pte_swp_clear_exclusive(pte_t pte)
393 {
394 	pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
395 	return pte;
396 }
397 
398 #endif
399