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