xref: /OK3568_Linux_fs/kernel/arch/xtensa/include/asm/pgtable.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * include/asm-xtensa/pgtable.h
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2001 - 2013 Tensilica Inc.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef _XTENSA_PGTABLE_H
9*4882a593Smuzhiyun #define _XTENSA_PGTABLE_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <asm/page.h>
12*4882a593Smuzhiyun #include <asm/kmem_layout.h>
13*4882a593Smuzhiyun #include <asm-generic/pgtable-nopmd.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * We only use two ring levels, user and kernel space.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #ifdef CONFIG_MMU
20*4882a593Smuzhiyun #define USER_RING		1	/* user ring level */
21*4882a593Smuzhiyun #else
22*4882a593Smuzhiyun #define USER_RING		0
23*4882a593Smuzhiyun #endif
24*4882a593Smuzhiyun #define KERNEL_RING		0	/* kernel ring level */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * The Xtensa architecture port of Linux has a two-level page table system,
28*4882a593Smuzhiyun  * i.e. the logical three-level Linux page table layout is folded.
29*4882a593Smuzhiyun  * Each task has the following memory page tables:
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *   PGD table (page directory), ie. 3rd-level page table:
32*4882a593Smuzhiyun  *	One page (4 kB) of 1024 (PTRS_PER_PGD) pointers to PTE tables
33*4882a593Smuzhiyun  *	(Architectures that don't have the PMD folded point to the PMD tables)
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  *	The pointer to the PGD table for a given task can be retrieved from
36*4882a593Smuzhiyun  *	the task structure (struct task_struct*) t, e.g. current():
37*4882a593Smuzhiyun  *	  (t->mm ? t->mm : t->active_mm)->pgd
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  *   PMD tables (page middle-directory), ie. 2nd-level page tables:
40*4882a593Smuzhiyun  *	Absent for the Xtensa architecture (folded, PTRS_PER_PMD == 1).
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  *   PTE tables (page table entry), ie. 1st-level page tables:
43*4882a593Smuzhiyun  *	One page (4 kB) of 1024 (PTRS_PER_PTE) PTEs with a special PTE
44*4882a593Smuzhiyun  *	invalid_pte_table for absent mappings.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * The individual pages are 4 kB big with special pages for the empty_zero_page.
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define PGDIR_SHIFT	22
50*4882a593Smuzhiyun #define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
51*4882a593Smuzhiyun #define PGDIR_MASK	(~(PGDIR_SIZE-1))
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * Entries per page directory level: we use two-level, so
55*4882a593Smuzhiyun  * we don't really have any PMD directory physically.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun #define PTRS_PER_PTE		1024
58*4882a593Smuzhiyun #define PTRS_PER_PTE_SHIFT	10
59*4882a593Smuzhiyun #define PTRS_PER_PGD		1024
60*4882a593Smuzhiyun #define PGD_ORDER		0
61*4882a593Smuzhiyun #define USER_PTRS_PER_PGD	(TASK_SIZE/PGDIR_SIZE)
62*4882a593Smuzhiyun #define FIRST_USER_ADDRESS	0UL
63*4882a593Smuzhiyun #define FIRST_USER_PGD_NR	(FIRST_USER_ADDRESS >> PGDIR_SHIFT)
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifdef CONFIG_MMU
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * Virtual memory area. We keep a distance to other memory regions to be
68*4882a593Smuzhiyun  * on the safe side. We also use this area for cache aliasing.
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun #define VMALLOC_START		(XCHAL_KSEG_CACHED_VADDR - 0x10000000)
71*4882a593Smuzhiyun #define VMALLOC_END		(VMALLOC_START + 0x07FEFFFF)
72*4882a593Smuzhiyun #define TLBTEMP_BASE_1		(VMALLOC_START + 0x08000000)
73*4882a593Smuzhiyun #define TLBTEMP_BASE_2		(TLBTEMP_BASE_1 + DCACHE_WAY_SIZE)
74*4882a593Smuzhiyun #if 2 * DCACHE_WAY_SIZE > ICACHE_WAY_SIZE
75*4882a593Smuzhiyun #define TLBTEMP_SIZE		(2 * DCACHE_WAY_SIZE)
76*4882a593Smuzhiyun #else
77*4882a593Smuzhiyun #define TLBTEMP_SIZE		ICACHE_WAY_SIZE
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun #else
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define VMALLOC_START		__XTENSA_UL_CONST(0)
83*4882a593Smuzhiyun #define VMALLOC_END		__XTENSA_UL_CONST(0xffffffff)
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #endif
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  * For the Xtensa architecture, the PTE layout is as follows:
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  *		31------12  11  10-9   8-6  5-4  3-2  1-0
91*4882a593Smuzhiyun  *		+-----------------------------------------+
92*4882a593Smuzhiyun  *		|           |   Software   |   HARDWARE   |
93*4882a593Smuzhiyun  *		|    PPN    |          ADW | RI |Attribute|
94*4882a593Smuzhiyun  *		+-----------------------------------------+
95*4882a593Smuzhiyun  *   pte_none	|             MBZ          | 01 | 11 | 00 |
96*4882a593Smuzhiyun  *		+-----------------------------------------+
97*4882a593Smuzhiyun  *   present	|    PPN    | 0 | 00 | ADW | RI | CA | wx |
98*4882a593Smuzhiyun  *		+- - - - - - - - - - - - - - - - - - - - -+
99*4882a593Smuzhiyun  *   (PAGE_NONE)|    PPN    | 0 | 00 | ADW | 01 | 11 | 11 |
100*4882a593Smuzhiyun  *		+-----------------------------------------+
101*4882a593Smuzhiyun  *   swap	|     index     |   type   | 01 | 11 | 00 |
102*4882a593Smuzhiyun  *		+-----------------------------------------+
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * For T1050 hardware and earlier the layout differs for present and (PAGE_NONE)
105*4882a593Smuzhiyun  *		+-----------------------------------------+
106*4882a593Smuzhiyun  *   present	|    PPN    | 0 | 00 | ADW | RI | CA | w1 |
107*4882a593Smuzhiyun  *		+-----------------------------------------+
108*4882a593Smuzhiyun  *   (PAGE_NONE)|    PPN    | 0 | 00 | ADW | 01 | 01 | 00 |
109*4882a593Smuzhiyun  *		+-----------------------------------------+
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  *  Legend:
112*4882a593Smuzhiyun  *   PPN        Physical Page Number
113*4882a593Smuzhiyun  *   ADW	software: accessed (young) / dirty / writable
114*4882a593Smuzhiyun  *   RI         ring (0=privileged, 1=user, 2 and 3 are unused)
115*4882a593Smuzhiyun  *   CA		cache attribute: 00 bypass, 01 writeback, 10 writethrough
116*4882a593Smuzhiyun  *		(11 is invalid and used to mark pages that are not present)
117*4882a593Smuzhiyun  *   w		page is writable (hw)
118*4882a593Smuzhiyun  *   x		page is executable (hw)
119*4882a593Smuzhiyun  *   index      swap offset / PAGE_SIZE (bit 11-31: 21 bits -> 8 GB)
120*4882a593Smuzhiyun  *		(note that the index is always non-zero)
121*4882a593Smuzhiyun  *   type       swap type (5 bits -> 32 types)
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  *  Notes:
124*4882a593Smuzhiyun  *   - (PROT_NONE) is a special case of 'present' but causes an exception for
125*4882a593Smuzhiyun  *     any access (read, write, and execute).
126*4882a593Smuzhiyun  *   - 'multihit-exception' has the highest priority of all MMU exceptions,
127*4882a593Smuzhiyun  *     so the ring must be set to 'RING_USER' even for 'non-present' pages.
128*4882a593Smuzhiyun  *   - on older hardware, the exectuable flag was not supported and
129*4882a593Smuzhiyun  *     used as a 'valid' flag, so it needs to be always set.
130*4882a593Smuzhiyun  *   - we need to keep track of certain flags in software (dirty and young)
131*4882a593Smuzhiyun  *     to do this, we use write exceptions and have a separate software w-flag.
132*4882a593Smuzhiyun  *   - attribute value 1101 (and 1111 on T1050 and earlier) is reserved
133*4882a593Smuzhiyun  */
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun #define _PAGE_ATTRIB_MASK	0xf
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #define _PAGE_HW_EXEC		(1<<0)	/* hardware: page is executable */
138*4882a593Smuzhiyun #define _PAGE_HW_WRITE		(1<<1)	/* hardware: page is writable */
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun #define _PAGE_CA_BYPASS		(0<<2)	/* bypass, non-speculative */
141*4882a593Smuzhiyun #define _PAGE_CA_WB		(1<<2)	/* write-back */
142*4882a593Smuzhiyun #define _PAGE_CA_WT		(2<<2)	/* write-through */
143*4882a593Smuzhiyun #define _PAGE_CA_MASK		(3<<2)
144*4882a593Smuzhiyun #define _PAGE_CA_INVALID	(3<<2)
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /* We use invalid attribute values to distinguish special pte entries */
147*4882a593Smuzhiyun #if XCHAL_HW_VERSION_MAJOR < 2000
148*4882a593Smuzhiyun #define _PAGE_HW_VALID		0x01	/* older HW needed this bit set */
149*4882a593Smuzhiyun #define _PAGE_NONE		0x04
150*4882a593Smuzhiyun #else
151*4882a593Smuzhiyun #define _PAGE_HW_VALID		0x00
152*4882a593Smuzhiyun #define _PAGE_NONE		0x0f
153*4882a593Smuzhiyun #endif
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun #define _PAGE_USER		(1<<4)	/* user access (ring=1) */
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* Software */
158*4882a593Smuzhiyun #define _PAGE_WRITABLE_BIT	6
159*4882a593Smuzhiyun #define _PAGE_WRITABLE		(1<<6)	/* software: page writable */
160*4882a593Smuzhiyun #define _PAGE_DIRTY		(1<<7)	/* software: page dirty */
161*4882a593Smuzhiyun #define _PAGE_ACCESSED		(1<<8)	/* software: page accessed (read) */
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun #ifdef CONFIG_MMU
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun #define _PAGE_CHG_MASK	   (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
166*4882a593Smuzhiyun #define _PAGE_PRESENT	   (_PAGE_HW_VALID | _PAGE_CA_WB | _PAGE_ACCESSED)
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun #define PAGE_NONE	   __pgprot(_PAGE_NONE | _PAGE_USER)
169*4882a593Smuzhiyun #define PAGE_COPY	   __pgprot(_PAGE_PRESENT | _PAGE_USER)
170*4882a593Smuzhiyun #define PAGE_COPY_EXEC	   __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
171*4882a593Smuzhiyun #define PAGE_READONLY	   __pgprot(_PAGE_PRESENT | _PAGE_USER)
172*4882a593Smuzhiyun #define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
173*4882a593Smuzhiyun #define PAGE_SHARED	   __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE)
174*4882a593Smuzhiyun #define PAGE_SHARED_EXEC \
175*4882a593Smuzhiyun 	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE | _PAGE_HW_EXEC)
176*4882a593Smuzhiyun #define PAGE_KERNEL	   __pgprot(_PAGE_PRESENT | _PAGE_HW_WRITE)
177*4882a593Smuzhiyun #define PAGE_KERNEL_RO	   __pgprot(_PAGE_PRESENT)
178*4882a593Smuzhiyun #define PAGE_KERNEL_EXEC   __pgprot(_PAGE_PRESENT|_PAGE_HW_WRITE|_PAGE_HW_EXEC)
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun #if (DCACHE_WAY_SIZE > PAGE_SIZE)
181*4882a593Smuzhiyun # define _PAGE_DIRECTORY   (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_BYPASS)
182*4882a593Smuzhiyun #else
183*4882a593Smuzhiyun # define _PAGE_DIRECTORY   (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_WB)
184*4882a593Smuzhiyun #endif
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun #else /* no mmu */
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun # define _PAGE_CHG_MASK  (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
189*4882a593Smuzhiyun # define PAGE_NONE       __pgprot(0)
190*4882a593Smuzhiyun # define PAGE_SHARED     __pgprot(0)
191*4882a593Smuzhiyun # define PAGE_COPY       __pgprot(0)
192*4882a593Smuzhiyun # define PAGE_READONLY   __pgprot(0)
193*4882a593Smuzhiyun # define PAGE_KERNEL     __pgprot(0)
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun #endif
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun  * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
199*4882a593Smuzhiyun  * the MMU can't do page protection for execute, and considers that the same as
200*4882a593Smuzhiyun  * read.  Also, write permissions may imply read permissions.
201*4882a593Smuzhiyun  * What follows is the closest we can get by reasonable means..
202*4882a593Smuzhiyun  * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
203*4882a593Smuzhiyun  */
204*4882a593Smuzhiyun #define __P000	PAGE_NONE		/* private --- */
205*4882a593Smuzhiyun #define __P001	PAGE_READONLY		/* private --r */
206*4882a593Smuzhiyun #define __P010	PAGE_COPY		/* private -w- */
207*4882a593Smuzhiyun #define __P011	PAGE_COPY		/* private -wr */
208*4882a593Smuzhiyun #define __P100	PAGE_READONLY_EXEC	/* private x-- */
209*4882a593Smuzhiyun #define __P101	PAGE_READONLY_EXEC	/* private x-r */
210*4882a593Smuzhiyun #define __P110	PAGE_COPY_EXEC		/* private xw- */
211*4882a593Smuzhiyun #define __P111	PAGE_COPY_EXEC		/* private xwr */
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun #define __S000	PAGE_NONE		/* shared  --- */
214*4882a593Smuzhiyun #define __S001	PAGE_READONLY		/* shared  --r */
215*4882a593Smuzhiyun #define __S010	PAGE_SHARED		/* shared  -w- */
216*4882a593Smuzhiyun #define __S011	PAGE_SHARED		/* shared  -wr */
217*4882a593Smuzhiyun #define __S100	PAGE_READONLY_EXEC	/* shared  x-- */
218*4882a593Smuzhiyun #define __S101	PAGE_READONLY_EXEC	/* shared  x-r */
219*4882a593Smuzhiyun #define __S110	PAGE_SHARED_EXEC	/* shared  xw- */
220*4882a593Smuzhiyun #define __S111	PAGE_SHARED_EXEC	/* shared  xwr */
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun #ifndef __ASSEMBLY__
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun #define pte_ERROR(e) \
225*4882a593Smuzhiyun 	printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
226*4882a593Smuzhiyun #define pgd_ERROR(e) \
227*4882a593Smuzhiyun 	printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun extern unsigned long empty_zero_page[1024];
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun #ifdef CONFIG_MMU
234*4882a593Smuzhiyun extern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
235*4882a593Smuzhiyun extern void paging_init(void);
236*4882a593Smuzhiyun #else
237*4882a593Smuzhiyun # define swapper_pg_dir NULL
paging_init(void)238*4882a593Smuzhiyun static inline void paging_init(void) { }
239*4882a593Smuzhiyun #endif
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun  * The pmd contains the kernel virtual address of the pte page.
243*4882a593Smuzhiyun  */
244*4882a593Smuzhiyun #define pmd_page_vaddr(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
245*4882a593Smuzhiyun #define pmd_page(pmd) virt_to_page(pmd_val(pmd))
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun  * pte status.
249*4882a593Smuzhiyun  */
250*4882a593Smuzhiyun # define pte_none(pte)	 (pte_val(pte) == (_PAGE_CA_INVALID | _PAGE_USER))
251*4882a593Smuzhiyun #if XCHAL_HW_VERSION_MAJOR < 2000
252*4882a593Smuzhiyun # define pte_present(pte) ((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID)
253*4882a593Smuzhiyun #else
254*4882a593Smuzhiyun # define pte_present(pte)						\
255*4882a593Smuzhiyun 	(((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID)		\
256*4882a593Smuzhiyun 	 || ((pte_val(pte) & _PAGE_ATTRIB_MASK) == _PAGE_NONE))
257*4882a593Smuzhiyun #endif
258*4882a593Smuzhiyun #define pte_clear(mm,addr,ptep)						\
259*4882a593Smuzhiyun 	do { update_pte(ptep, __pte(_PAGE_CA_INVALID | _PAGE_USER)); } while (0)
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun #define pmd_none(pmd)	 (!pmd_val(pmd))
262*4882a593Smuzhiyun #define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
263*4882a593Smuzhiyun #define pmd_bad(pmd)	 (pmd_val(pmd) & ~PAGE_MASK)
264*4882a593Smuzhiyun #define pmd_clear(pmdp)	 do { set_pmd(pmdp, __pmd(0)); } while (0)
265*4882a593Smuzhiyun 
pte_write(pte_t pte)266*4882a593Smuzhiyun static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITABLE; }
pte_dirty(pte_t pte)267*4882a593Smuzhiyun static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
pte_young(pte_t pte)268*4882a593Smuzhiyun static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
269*4882a593Smuzhiyun 
pte_wrprotect(pte_t pte)270*4882a593Smuzhiyun static inline pte_t pte_wrprotect(pte_t pte)
271*4882a593Smuzhiyun 	{ pte_val(pte) &= ~(_PAGE_WRITABLE | _PAGE_HW_WRITE); return pte; }
pte_mkclean(pte_t pte)272*4882a593Smuzhiyun static inline pte_t pte_mkclean(pte_t pte)
273*4882a593Smuzhiyun 	{ pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HW_WRITE); return pte; }
pte_mkold(pte_t pte)274*4882a593Smuzhiyun static inline pte_t pte_mkold(pte_t pte)
275*4882a593Smuzhiyun 	{ pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
pte_mkdirty(pte_t pte)276*4882a593Smuzhiyun static inline pte_t pte_mkdirty(pte_t pte)
277*4882a593Smuzhiyun 	{ pte_val(pte) |= _PAGE_DIRTY; return pte; }
pte_mkyoung(pte_t pte)278*4882a593Smuzhiyun static inline pte_t pte_mkyoung(pte_t pte)
279*4882a593Smuzhiyun 	{ pte_val(pte) |= _PAGE_ACCESSED; return pte; }
pte_mkwrite(pte_t pte)280*4882a593Smuzhiyun static inline pte_t pte_mkwrite(pte_t pte)
281*4882a593Smuzhiyun 	{ pte_val(pte) |= _PAGE_WRITABLE; return pte; }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun #define pgprot_noncached(prot) (__pgprot(pgprot_val(prot) & ~_PAGE_CA_MASK))
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun /*
286*4882a593Smuzhiyun  * Conversion functions: convert a page and protection to a page entry,
287*4882a593Smuzhiyun  * and a page entry and page directory to the page they refer to.
288*4882a593Smuzhiyun  */
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun #define pte_pfn(pte)		(pte_val(pte) >> PAGE_SHIFT)
291*4882a593Smuzhiyun #define pte_same(a,b)		(pte_val(a) == pte_val(b))
292*4882a593Smuzhiyun #define pte_page(x)		pfn_to_page(pte_pfn(x))
293*4882a593Smuzhiyun #define pfn_pte(pfn, prot)	__pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
294*4882a593Smuzhiyun #define mk_pte(page, prot)	pfn_pte(page_to_pfn(page), prot)
295*4882a593Smuzhiyun 
pte_modify(pte_t pte,pgprot_t newprot)296*4882a593Smuzhiyun static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /*
302*4882a593Smuzhiyun  * Certain architectures need to do special things when pte's
303*4882a593Smuzhiyun  * within a page table are directly modified.  Thus, the following
304*4882a593Smuzhiyun  * hook is made available.
305*4882a593Smuzhiyun  */
update_pte(pte_t * ptep,pte_t pteval)306*4882a593Smuzhiyun static inline void update_pte(pte_t *ptep, pte_t pteval)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	*ptep = pteval;
309*4882a593Smuzhiyun #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
310*4882a593Smuzhiyun 	__asm__ __volatile__ ("dhwb %0, 0" :: "a" (ptep));
311*4882a593Smuzhiyun #endif
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun struct mm_struct;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun static inline void
set_pte_at(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pteval)318*4882a593Smuzhiyun set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	update_pte(ptep, pteval);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
set_pte(pte_t * ptep,pte_t pteval)323*4882a593Smuzhiyun static inline void set_pte(pte_t *ptep, pte_t pteval)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	update_pte(ptep, pteval);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun static inline void
set_pmd(pmd_t * pmdp,pmd_t pmdval)329*4882a593Smuzhiyun set_pmd(pmd_t *pmdp, pmd_t pmdval)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	*pmdp = pmdval;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun struct vm_area_struct;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun static inline int
ptep_test_and_clear_young(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)337*4882a593Smuzhiyun ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
338*4882a593Smuzhiyun 			  pte_t *ptep)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	pte_t pte = *ptep;
341*4882a593Smuzhiyun 	if (!pte_young(pte))
342*4882a593Smuzhiyun 		return 0;
343*4882a593Smuzhiyun 	update_pte(ptep, pte_mkold(pte));
344*4882a593Smuzhiyun 	return 1;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun static inline pte_t
ptep_get_and_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)348*4882a593Smuzhiyun ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	pte_t pte = *ptep;
351*4882a593Smuzhiyun 	pte_clear(mm, addr, ptep);
352*4882a593Smuzhiyun 	return pte;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun static inline void
ptep_set_wrprotect(struct mm_struct * mm,unsigned long addr,pte_t * ptep)356*4882a593Smuzhiyun ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	pte_t pte = *ptep;
359*4882a593Smuzhiyun 	update_pte(ptep, pte_wrprotect(pte));
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun /*
363*4882a593Smuzhiyun  * Encode and decode a swap and file entry.
364*4882a593Smuzhiyun  */
365*4882a593Smuzhiyun #define SWP_TYPE_BITS		5
366*4882a593Smuzhiyun #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun #define __swp_type(entry)	(((entry).val >> 6) & 0x1f)
369*4882a593Smuzhiyun #define __swp_offset(entry)	((entry).val >> 11)
370*4882a593Smuzhiyun #define __swp_entry(type,offs)	\
371*4882a593Smuzhiyun 	((swp_entry_t){((type) << 6) | ((offs) << 11) | \
372*4882a593Smuzhiyun 	 _PAGE_CA_INVALID | _PAGE_USER})
373*4882a593Smuzhiyun #define __pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
374*4882a593Smuzhiyun #define __swp_entry_to_pte(x)	((pte_t) { (x).val })
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun #endif /*  !defined (__ASSEMBLY__) */
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun #ifdef __ASSEMBLY__
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /* Assembly macro _PGD_INDEX is the same as C pgd_index(unsigned long),
382*4882a593Smuzhiyun  *                _PGD_OFFSET as C pgd_offset(struct mm_struct*, unsigned long),
383*4882a593Smuzhiyun  *                _PMD_OFFSET as C pmd_offset(pgd_t*, unsigned long)
384*4882a593Smuzhiyun  *                _PTE_OFFSET as C pte_offset(pmd_t*, unsigned long)
385*4882a593Smuzhiyun  *
386*4882a593Smuzhiyun  * Note: We require an additional temporary register which can be the same as
387*4882a593Smuzhiyun  *       the register that holds the address.
388*4882a593Smuzhiyun  *
389*4882a593Smuzhiyun  * ((pte_t*) ((unsigned long)(pmd_val(*pmd) & PAGE_MASK)) + pte_index(addr))
390*4882a593Smuzhiyun  *
391*4882a593Smuzhiyun  */
392*4882a593Smuzhiyun #define _PGD_INDEX(rt,rs)	extui	rt, rs, PGDIR_SHIFT, 32-PGDIR_SHIFT
393*4882a593Smuzhiyun #define _PTE_INDEX(rt,rs)	extui	rt, rs, PAGE_SHIFT, PTRS_PER_PTE_SHIFT
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun #define _PGD_OFFSET(mm,adr,tmp)		l32i	mm, mm, MM_PGD;		\
396*4882a593Smuzhiyun 					_PGD_INDEX(tmp, adr);		\
397*4882a593Smuzhiyun 					addx4	mm, tmp, mm
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun #define _PTE_OFFSET(pmd,adr,tmp)	_PTE_INDEX(tmp, adr);		\
400*4882a593Smuzhiyun 					srli	pmd, pmd, PAGE_SHIFT;	\
401*4882a593Smuzhiyun 					slli	pmd, pmd, PAGE_SHIFT;	\
402*4882a593Smuzhiyun 					addx4	pmd, tmp, pmd
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun #else
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun #define kern_addr_valid(addr)	(1)
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun extern  void update_mmu_cache(struct vm_area_struct * vma,
409*4882a593Smuzhiyun 			      unsigned long address, pte_t *ptep);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun typedef pte_t *pte_addr_t;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun #endif /* !defined (__ASSEMBLY__) */
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
416*4882a593Smuzhiyun #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
417*4882a593Smuzhiyun #define __HAVE_ARCH_PTEP_SET_WRPROTECT
418*4882a593Smuzhiyun #define __HAVE_ARCH_PTEP_MKDIRTY
419*4882a593Smuzhiyun #define __HAVE_ARCH_PTE_SAME
420*4882a593Smuzhiyun /* We provide our own get_unmapped_area to cope with
421*4882a593Smuzhiyun  * SHM area cache aliasing for userland.
422*4882a593Smuzhiyun  */
423*4882a593Smuzhiyun #define HAVE_ARCH_UNMAPPED_AREA
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun #endif /* _XTENSA_PGTABLE_H */
426