xref: /linux/arch/sh/include/asm/pgtable_32.h (revision 8b70beac99466b6d164de9fe647b3567e6f17e3a)
1f15cbe6fSPaul Mundt #ifndef __ASM_SH_PGTABLE_32_H
2f15cbe6fSPaul Mundt #define __ASM_SH_PGTABLE_32_H
3f15cbe6fSPaul Mundt 
4f15cbe6fSPaul Mundt /*
5f15cbe6fSPaul Mundt  * Linux PTEL encoding.
6f15cbe6fSPaul Mundt  *
7f15cbe6fSPaul Mundt  * Hardware and software bit definitions for the PTEL value (see below for
8f15cbe6fSPaul Mundt  * notes on SH-X2 MMUs and 64-bit PTEs):
9f15cbe6fSPaul Mundt  *
10f15cbe6fSPaul Mundt  * - Bits 0 and 7 are reserved on SH-3 (_PAGE_WT and _PAGE_SZ1 on SH-4).
11f15cbe6fSPaul Mundt  *
12f15cbe6fSPaul Mundt  * - Bit 1 is the SH-bit, but is unused on SH-3 due to an MMU bug (the
13f15cbe6fSPaul Mundt  *   hardware PTEL value can't have the SH-bit set when MMUCR.IX is set,
14f15cbe6fSPaul Mundt  *   which is the default in cpu-sh3/mmu_context.h:MMU_CONTROL_INIT).
15f15cbe6fSPaul Mundt  *
16f15cbe6fSPaul Mundt  *   In order to keep this relatively clean, do not use these for defining
17f15cbe6fSPaul Mundt  *   SH-3 specific flags until all of the other unused bits have been
18f15cbe6fSPaul Mundt  *   exhausted.
19f15cbe6fSPaul Mundt  *
20f15cbe6fSPaul Mundt  * - Bit 9 is reserved by everyone and used by _PAGE_PROTNONE.
21f15cbe6fSPaul Mundt  *
22f15cbe6fSPaul Mundt  * - Bits 10 and 11 are low bits of the PPN that are reserved on >= 4K pages.
23c0b96cf6SPaul Mundt  *   Bit 10 is used for _PAGE_ACCESSED, and bit 11 is used for _PAGE_SPECIAL.
24f15cbe6fSPaul Mundt  *
25f15cbe6fSPaul Mundt  * - On 29 bit platforms, bits 31 to 29 are used for the space attributes
26f15cbe6fSPaul Mundt  *   and timing control which (together with bit 0) are moved into the
27f15cbe6fSPaul Mundt  *   old-style PTEA on the parts that support it.
28f15cbe6fSPaul Mundt  *
29f15cbe6fSPaul Mundt  * SH-X2 MMUs and extended PTEs
30f15cbe6fSPaul Mundt  *
31f15cbe6fSPaul Mundt  * SH-X2 supports an extended mode TLB with split data arrays due to the
32f15cbe6fSPaul Mundt  * number of bits needed for PR and SZ (now EPR and ESZ) encodings. The PR and
33f15cbe6fSPaul Mundt  * SZ bit placeholders still exist in data array 1, but are implemented as
34f15cbe6fSPaul Mundt  * reserved bits, with the real logic existing in data array 2.
35f15cbe6fSPaul Mundt  *
36f15cbe6fSPaul Mundt  * The downside to this is that we can no longer fit everything in to a 32-bit
37f15cbe6fSPaul Mundt  * PTE encoding, so a 64-bit pte_t is necessary for these parts. On the plus
38f15cbe6fSPaul Mundt  * side, this gives us quite a few spare bits to play with for future usage.
39f15cbe6fSPaul Mundt  */
40f15cbe6fSPaul Mundt /* Legacy and compat mode bits */
41f15cbe6fSPaul Mundt #define	_PAGE_WT	0x001		/* WT-bit on SH-4, 0 on SH-3 */
42f15cbe6fSPaul Mundt #define _PAGE_HW_SHARED	0x002		/* SH-bit  : shared among processes */
43f15cbe6fSPaul Mundt #define _PAGE_DIRTY	0x004		/* D-bit   : page changed */
44f15cbe6fSPaul Mundt #define _PAGE_CACHABLE	0x008		/* C-bit   : cachable */
45f15cbe6fSPaul Mundt #define _PAGE_SZ0	0x010		/* SZ0-bit : Size of page */
46f15cbe6fSPaul Mundt #define _PAGE_RW	0x020		/* PR0-bit : write access allowed */
47f15cbe6fSPaul Mundt #define _PAGE_USER	0x040		/* PR1-bit : user space access allowed*/
48f15cbe6fSPaul Mundt #define _PAGE_SZ1	0x080		/* SZ1-bit : Size of page (on SH-4) */
49f15cbe6fSPaul Mundt #define _PAGE_PRESENT	0x100		/* V-bit   : page is valid */
50f15cbe6fSPaul Mundt #define _PAGE_PROTNONE	0x200		/* software: if not present  */
51f15cbe6fSPaul Mundt #define _PAGE_ACCESSED	0x400		/* software: page referenced */
52c0b96cf6SPaul Mundt #define _PAGE_SPECIAL	0x800		/* software: special page */
53f15cbe6fSPaul Mundt 
54f15cbe6fSPaul Mundt #define _PAGE_SZ_MASK	(_PAGE_SZ0 | _PAGE_SZ1)
55f15cbe6fSPaul Mundt #define _PAGE_PR_MASK	(_PAGE_RW | _PAGE_USER)
56f15cbe6fSPaul Mundt 
57f15cbe6fSPaul Mundt /* Extended mode bits */
58f15cbe6fSPaul Mundt #define _PAGE_EXT_ESZ0		0x0010	/* ESZ0-bit: Size of page */
59f15cbe6fSPaul Mundt #define _PAGE_EXT_ESZ1		0x0020	/* ESZ1-bit: Size of page */
60f15cbe6fSPaul Mundt #define _PAGE_EXT_ESZ2		0x0040	/* ESZ2-bit: Size of page */
61f15cbe6fSPaul Mundt #define _PAGE_EXT_ESZ3		0x0080	/* ESZ3-bit: Size of page */
62f15cbe6fSPaul Mundt 
63f15cbe6fSPaul Mundt #define _PAGE_EXT_USER_EXEC	0x0100	/* EPR0-bit: User space executable */
64f15cbe6fSPaul Mundt #define _PAGE_EXT_USER_WRITE	0x0200	/* EPR1-bit: User space writable */
65f15cbe6fSPaul Mundt #define _PAGE_EXT_USER_READ	0x0400	/* EPR2-bit: User space readable */
66f15cbe6fSPaul Mundt 
67f15cbe6fSPaul Mundt #define _PAGE_EXT_KERN_EXEC	0x0800	/* EPR3-bit: Kernel space executable */
68f15cbe6fSPaul Mundt #define _PAGE_EXT_KERN_WRITE	0x1000	/* EPR4-bit: Kernel space writable */
69f15cbe6fSPaul Mundt #define _PAGE_EXT_KERN_READ	0x2000	/* EPR5-bit: Kernel space readable */
70f15cbe6fSPaul Mundt 
718eda5514SMatt Fleming #define _PAGE_EXT_WIRED		0x4000	/* software: Wire TLB entry */
728eda5514SMatt Fleming 
73f15cbe6fSPaul Mundt /* Wrapper for extended mode pgprot twiddling */
74f15cbe6fSPaul Mundt #define _PAGE_EXT(x)		((unsigned long long)(x) << 32)
75f15cbe6fSPaul Mundt 
76efb3e34bSPaul Mundt #ifdef CONFIG_X2TLB
77efb3e34bSPaul Mundt #define _PAGE_PCC_MASK	0x00000000	/* No legacy PTEA support */
78efb3e34bSPaul Mundt #else
79efb3e34bSPaul Mundt 
80f15cbe6fSPaul Mundt /* software: moves to PTEA.TC (Timing Control) */
81f15cbe6fSPaul Mundt #define _PAGE_PCC_AREA5	0x00000000	/* use BSC registers for area5 */
82f15cbe6fSPaul Mundt #define _PAGE_PCC_AREA6	0x80000000	/* use BSC registers for area6 */
83f15cbe6fSPaul Mundt 
84f15cbe6fSPaul Mundt /* software: moves to PTEA.SA[2:0] (Space Attributes) */
85f15cbe6fSPaul Mundt #define _PAGE_PCC_IODYN 0x00000001	/* IO space, dynamically sized bus */
86f15cbe6fSPaul Mundt #define _PAGE_PCC_IO8	0x20000000	/* IO space, 8 bit bus */
87f15cbe6fSPaul Mundt #define _PAGE_PCC_IO16	0x20000001	/* IO space, 16 bit bus */
88f15cbe6fSPaul Mundt #define _PAGE_PCC_COM8	0x40000000	/* Common Memory space, 8 bit bus */
89f15cbe6fSPaul Mundt #define _PAGE_PCC_COM16	0x40000001	/* Common Memory space, 16 bit bus */
90f15cbe6fSPaul Mundt #define _PAGE_PCC_ATR8	0x60000000	/* Attribute Memory space, 8 bit bus */
91f15cbe6fSPaul Mundt #define _PAGE_PCC_ATR16	0x60000001	/* Attribute Memory space, 6 bit bus */
92f15cbe6fSPaul Mundt 
93efb3e34bSPaul Mundt #define _PAGE_PCC_MASK	0xe0000001
94efb3e34bSPaul Mundt 
956503fe4aSMichael Trimarchi /* copy the ptea attributes */
966503fe4aSMichael Trimarchi static inline unsigned long copy_ptea_attributes(unsigned long x)
976503fe4aSMichael Trimarchi {
986503fe4aSMichael Trimarchi 	return	((x >> 28) & 0xe) | (x & 0x1);
996503fe4aSMichael Trimarchi }
1006503fe4aSMichael Trimarchi #endif
1016503fe4aSMichael Trimarchi 
102f15cbe6fSPaul Mundt /* Mask which drops unused bits from the PTEL value */
103f15cbe6fSPaul Mundt #if defined(CONFIG_CPU_SH3)
104f15cbe6fSPaul Mundt #define _PAGE_CLEAR_FLAGS	(_PAGE_PROTNONE | _PAGE_ACCESSED| \
105*8b70beacSKirill A. Shutemov 				  _PAGE_SZ1	| _PAGE_HW_SHARED)
106f15cbe6fSPaul Mundt #elif defined(CONFIG_X2TLB)
107f15cbe6fSPaul Mundt /* Get rid of the legacy PR/SZ bits when using extended mode */
108f15cbe6fSPaul Mundt #define _PAGE_CLEAR_FLAGS	(_PAGE_PROTNONE | _PAGE_ACCESSED | \
109*8b70beacSKirill A. Shutemov 				 _PAGE_PR_MASK | _PAGE_SZ_MASK)
110f15cbe6fSPaul Mundt #else
111*8b70beacSKirill A. Shutemov #define _PAGE_CLEAR_FLAGS	(_PAGE_PROTNONE | _PAGE_ACCESSED)
112f15cbe6fSPaul Mundt #endif
113f15cbe6fSPaul Mundt 
1141f69b6afSMatt Fleming #define _PAGE_FLAGS_HARDWARE_MASK	(phys_addr_mask() & ~(_PAGE_CLEAR_FLAGS))
115f15cbe6fSPaul Mundt 
116f15cbe6fSPaul Mundt /* Hardware flags, page size encoding */
117f15cbe6fSPaul Mundt #if !defined(CONFIG_MMU)
118f15cbe6fSPaul Mundt # define _PAGE_FLAGS_HARD	0ULL
119f15cbe6fSPaul Mundt #elif defined(CONFIG_X2TLB)
120f15cbe6fSPaul Mundt # if defined(CONFIG_PAGE_SIZE_4KB)
121f15cbe6fSPaul Mundt #  define _PAGE_FLAGS_HARD	_PAGE_EXT(_PAGE_EXT_ESZ0)
122f15cbe6fSPaul Mundt # elif defined(CONFIG_PAGE_SIZE_8KB)
123f15cbe6fSPaul Mundt #  define _PAGE_FLAGS_HARD	_PAGE_EXT(_PAGE_EXT_ESZ1)
124f15cbe6fSPaul Mundt # elif defined(CONFIG_PAGE_SIZE_64KB)
125f15cbe6fSPaul Mundt #  define _PAGE_FLAGS_HARD	_PAGE_EXT(_PAGE_EXT_ESZ2)
126f15cbe6fSPaul Mundt # endif
127f15cbe6fSPaul Mundt #else
128f15cbe6fSPaul Mundt # if defined(CONFIG_PAGE_SIZE_4KB)
129f15cbe6fSPaul Mundt #  define _PAGE_FLAGS_HARD	_PAGE_SZ0
130f15cbe6fSPaul Mundt # elif defined(CONFIG_PAGE_SIZE_64KB)
131f15cbe6fSPaul Mundt #  define _PAGE_FLAGS_HARD	_PAGE_SZ1
132f15cbe6fSPaul Mundt # endif
133f15cbe6fSPaul Mundt #endif
134f15cbe6fSPaul Mundt 
135f15cbe6fSPaul Mundt #if defined(CONFIG_X2TLB)
136f15cbe6fSPaul Mundt # if defined(CONFIG_HUGETLB_PAGE_SIZE_64K)
137f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_EXT_ESZ2)
138f15cbe6fSPaul Mundt # elif defined(CONFIG_HUGETLB_PAGE_SIZE_256K)
139f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_EXT_ESZ0 | _PAGE_EXT_ESZ2)
140f15cbe6fSPaul Mundt # elif defined(CONFIG_HUGETLB_PAGE_SIZE_1MB)
141f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_EXT_ESZ0 | _PAGE_EXT_ESZ1 | _PAGE_EXT_ESZ2)
142f15cbe6fSPaul Mundt # elif defined(CONFIG_HUGETLB_PAGE_SIZE_4MB)
143f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_EXT_ESZ3)
144f15cbe6fSPaul Mundt # elif defined(CONFIG_HUGETLB_PAGE_SIZE_64MB)
145f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_EXT_ESZ2 | _PAGE_EXT_ESZ3)
146f15cbe6fSPaul Mundt # endif
147046581f9SPaul Mundt # define _PAGE_WIRED	(_PAGE_EXT(_PAGE_EXT_WIRED))
148f15cbe6fSPaul Mundt #else
149f15cbe6fSPaul Mundt # if defined(CONFIG_HUGETLB_PAGE_SIZE_64K)
150f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_SZ1)
151f15cbe6fSPaul Mundt # elif defined(CONFIG_HUGETLB_PAGE_SIZE_1MB)
152f15cbe6fSPaul Mundt #  define _PAGE_SZHUGE	(_PAGE_SZ0 | _PAGE_SZ1)
153f15cbe6fSPaul Mundt # endif
154046581f9SPaul Mundt # define _PAGE_WIRED	(0)
155f15cbe6fSPaul Mundt #endif
156f15cbe6fSPaul Mundt 
157f15cbe6fSPaul Mundt /*
158f15cbe6fSPaul Mundt  * Stub out _PAGE_SZHUGE if we don't have a good definition for it,
159f15cbe6fSPaul Mundt  * to make pte_mkhuge() happy.
160f15cbe6fSPaul Mundt  */
161f15cbe6fSPaul Mundt #ifndef _PAGE_SZHUGE
162f15cbe6fSPaul Mundt # define _PAGE_SZHUGE	(_PAGE_FLAGS_HARD)
163f15cbe6fSPaul Mundt #endif
164f15cbe6fSPaul Mundt 
165c0b96cf6SPaul Mundt /*
16625985edcSLucas De Marchi  * Mask of bits that are to be preserved across pgprot changes.
167c0b96cf6SPaul Mundt  */
168f15cbe6fSPaul Mundt #define _PAGE_CHG_MASK \
169c0b96cf6SPaul Mundt 	(PTE_MASK | _PAGE_ACCESSED | _PAGE_CACHABLE | \
170c0b96cf6SPaul Mundt 	 _PAGE_DIRTY | _PAGE_SPECIAL)
171f15cbe6fSPaul Mundt 
172f15cbe6fSPaul Mundt #ifndef __ASSEMBLY__
173f15cbe6fSPaul Mundt 
174f15cbe6fSPaul Mundt #if defined(CONFIG_X2TLB) /* SH-X2 TLB */
175f15cbe6fSPaul Mundt #define PAGE_NONE	__pgprot(_PAGE_PROTNONE | _PAGE_CACHABLE | \
176f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_FLAGS_HARD)
177f15cbe6fSPaul Mundt 
178f15cbe6fSPaul Mundt #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | \
179f15cbe6fSPaul Mundt 				 _PAGE_CACHABLE | _PAGE_FLAGS_HARD | \
180f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_READ  | \
181f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_WRITE | \
182f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_READ  | \
183f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_WRITE))
184f15cbe6fSPaul Mundt 
185f15cbe6fSPaul Mundt #define PAGE_EXECREAD	__pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | \
186f15cbe6fSPaul Mundt 				 _PAGE_CACHABLE | _PAGE_FLAGS_HARD | \
187f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_EXEC | \
188f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_READ | \
189f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_EXEC | \
190f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_READ))
191f15cbe6fSPaul Mundt 
192f15cbe6fSPaul Mundt #define PAGE_COPY	PAGE_EXECREAD
193f15cbe6fSPaul Mundt 
194f15cbe6fSPaul Mundt #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | \
195f15cbe6fSPaul Mundt 				 _PAGE_CACHABLE | _PAGE_FLAGS_HARD | \
196f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_READ | \
197f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_READ))
198f15cbe6fSPaul Mundt 
199f15cbe6fSPaul Mundt #define PAGE_WRITEONLY	__pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | \
200f15cbe6fSPaul Mundt 				 _PAGE_CACHABLE | _PAGE_FLAGS_HARD | \
201f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_WRITE | \
202f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_WRITE))
203f15cbe6fSPaul Mundt 
204f15cbe6fSPaul Mundt #define PAGE_RWX	__pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | \
205f15cbe6fSPaul Mundt 				 _PAGE_CACHABLE | _PAGE_FLAGS_HARD | \
206f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_WRITE | \
207f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_READ  | \
208f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_EXEC  | \
209f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_WRITE | \
210f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_READ  | \
211f15cbe6fSPaul Mundt 					   _PAGE_EXT_USER_EXEC))
212f15cbe6fSPaul Mundt 
213f15cbe6fSPaul Mundt #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | _PAGE_CACHABLE | \
214f15cbe6fSPaul Mundt 				 _PAGE_DIRTY | _PAGE_ACCESSED | \
215f15cbe6fSPaul Mundt 				 _PAGE_HW_SHARED | _PAGE_FLAGS_HARD | \
216f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_READ | \
217f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_WRITE | \
218f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_EXEC))
219f15cbe6fSPaul Mundt 
220f15cbe6fSPaul Mundt #define PAGE_KERNEL_NOCACHE \
221f15cbe6fSPaul Mundt 			__pgprot(_PAGE_PRESENT | _PAGE_DIRTY | \
222f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_HW_SHARED | \
223f15cbe6fSPaul Mundt 				 _PAGE_FLAGS_HARD | \
224f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_READ | \
225f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_WRITE | \
226f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_EXEC))
227f15cbe6fSPaul Mundt 
228f15cbe6fSPaul Mundt #define PAGE_KERNEL_RO	__pgprot(_PAGE_PRESENT | _PAGE_CACHABLE | \
229f15cbe6fSPaul Mundt 				 _PAGE_DIRTY | _PAGE_ACCESSED | \
230f15cbe6fSPaul Mundt 				 _PAGE_HW_SHARED | _PAGE_FLAGS_HARD | \
231f15cbe6fSPaul Mundt 				 _PAGE_EXT(_PAGE_EXT_KERN_READ | \
232f15cbe6fSPaul Mundt 					   _PAGE_EXT_KERN_EXEC))
233f15cbe6fSPaul Mundt 
234f15cbe6fSPaul Mundt #define PAGE_KERNEL_PCC(slot, type) \
23511e1ed6eSPaul Mundt 			__pgprot(0)
236f15cbe6fSPaul Mundt 
237f15cbe6fSPaul Mundt #elif defined(CONFIG_MMU) /* SH-X TLB */
238f15cbe6fSPaul Mundt #define PAGE_NONE	__pgprot(_PAGE_PROTNONE | _PAGE_CACHABLE | \
239f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_FLAGS_HARD)
240f15cbe6fSPaul Mundt 
241f15cbe6fSPaul Mundt #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | \
242f15cbe6fSPaul Mundt 				 _PAGE_CACHABLE | _PAGE_ACCESSED | \
243f15cbe6fSPaul Mundt 				 _PAGE_FLAGS_HARD)
244f15cbe6fSPaul Mundt 
245f15cbe6fSPaul Mundt #define PAGE_COPY	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_CACHABLE | \
246f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_FLAGS_HARD)
247f15cbe6fSPaul Mundt 
248f15cbe6fSPaul Mundt #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_CACHABLE | \
249f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_FLAGS_HARD)
250f15cbe6fSPaul Mundt 
251f15cbe6fSPaul Mundt #define PAGE_EXECREAD	PAGE_READONLY
252f15cbe6fSPaul Mundt #define PAGE_RWX	PAGE_SHARED
253f15cbe6fSPaul Mundt #define PAGE_WRITEONLY	PAGE_SHARED
254f15cbe6fSPaul Mundt 
255f15cbe6fSPaul Mundt #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_CACHABLE | \
256f15cbe6fSPaul Mundt 				 _PAGE_DIRTY | _PAGE_ACCESSED | \
257f15cbe6fSPaul Mundt 				 _PAGE_HW_SHARED | _PAGE_FLAGS_HARD)
258f15cbe6fSPaul Mundt 
259f15cbe6fSPaul Mundt #define PAGE_KERNEL_NOCACHE \
260f15cbe6fSPaul Mundt 			__pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | \
261f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_HW_SHARED | \
262f15cbe6fSPaul Mundt 				 _PAGE_FLAGS_HARD)
263f15cbe6fSPaul Mundt 
264f15cbe6fSPaul Mundt #define PAGE_KERNEL_RO	__pgprot(_PAGE_PRESENT | _PAGE_CACHABLE | \
265f15cbe6fSPaul Mundt 				 _PAGE_DIRTY | _PAGE_ACCESSED | \
266f15cbe6fSPaul Mundt 				 _PAGE_HW_SHARED | _PAGE_FLAGS_HARD)
267f15cbe6fSPaul Mundt 
268f15cbe6fSPaul Mundt #define PAGE_KERNEL_PCC(slot, type) \
269f15cbe6fSPaul Mundt 			__pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | \
270f15cbe6fSPaul Mundt 				 _PAGE_ACCESSED | _PAGE_FLAGS_HARD | \
271f15cbe6fSPaul Mundt 				 (slot ? _PAGE_PCC_AREA5 : _PAGE_PCC_AREA6) | \
272f15cbe6fSPaul Mundt 				 (type))
273f15cbe6fSPaul Mundt #else /* no mmu */
274f15cbe6fSPaul Mundt #define PAGE_NONE		__pgprot(0)
275f15cbe6fSPaul Mundt #define PAGE_SHARED		__pgprot(0)
276f15cbe6fSPaul Mundt #define PAGE_COPY		__pgprot(0)
277f15cbe6fSPaul Mundt #define PAGE_EXECREAD		__pgprot(0)
278f15cbe6fSPaul Mundt #define PAGE_RWX		__pgprot(0)
279f15cbe6fSPaul Mundt #define PAGE_READONLY		__pgprot(0)
280f15cbe6fSPaul Mundt #define PAGE_WRITEONLY		__pgprot(0)
281f15cbe6fSPaul Mundt #define PAGE_KERNEL		__pgprot(0)
282f15cbe6fSPaul Mundt #define PAGE_KERNEL_NOCACHE	__pgprot(0)
283f15cbe6fSPaul Mundt #define PAGE_KERNEL_RO		__pgprot(0)
284f15cbe6fSPaul Mundt 
285f15cbe6fSPaul Mundt #define PAGE_KERNEL_PCC(slot, type) \
286f15cbe6fSPaul Mundt 				__pgprot(0)
287f15cbe6fSPaul Mundt #endif
288f15cbe6fSPaul Mundt 
289f15cbe6fSPaul Mundt #endif /* __ASSEMBLY__ */
290f15cbe6fSPaul Mundt 
291f15cbe6fSPaul Mundt #ifndef __ASSEMBLY__
292f15cbe6fSPaul Mundt 
293f15cbe6fSPaul Mundt /*
294f15cbe6fSPaul Mundt  * Certain architectures need to do special things when PTEs
295f15cbe6fSPaul Mundt  * within a page table are directly modified.  Thus, the following
296f15cbe6fSPaul Mundt  * hook is made available.
297f15cbe6fSPaul Mundt  */
298f15cbe6fSPaul Mundt #ifdef CONFIG_X2TLB
299f15cbe6fSPaul Mundt static inline void set_pte(pte_t *ptep, pte_t pte)
300f15cbe6fSPaul Mundt {
301f15cbe6fSPaul Mundt 	ptep->pte_high = pte.pte_high;
302f15cbe6fSPaul Mundt 	smp_wmb();
303f15cbe6fSPaul Mundt 	ptep->pte_low = pte.pte_low;
304f15cbe6fSPaul Mundt }
305f15cbe6fSPaul Mundt #else
306f15cbe6fSPaul Mundt #define set_pte(pteptr, pteval) (*(pteptr) = pteval)
307f15cbe6fSPaul Mundt #endif
308f15cbe6fSPaul Mundt 
309f15cbe6fSPaul Mundt #define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
310f15cbe6fSPaul Mundt 
311f15cbe6fSPaul Mundt /*
312f15cbe6fSPaul Mundt  * (pmds are folded into pgds so this doesn't get actually called,
313f15cbe6fSPaul Mundt  * but the define is needed for a generic inline function.)
314f15cbe6fSPaul Mundt  */
315f15cbe6fSPaul Mundt #define set_pmd(pmdptr, pmdval) (*(pmdptr) = pmdval)
316f15cbe6fSPaul Mundt 
317f15cbe6fSPaul Mundt #define pfn_pte(pfn, prot) \
318f15cbe6fSPaul Mundt 	__pte(((unsigned long long)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
319f15cbe6fSPaul Mundt #define pfn_pmd(pfn, prot) \
320f15cbe6fSPaul Mundt 	__pmd(((unsigned long long)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
321f15cbe6fSPaul Mundt 
322f15cbe6fSPaul Mundt #define pte_none(x)		(!pte_val(x))
323f15cbe6fSPaul Mundt #define pte_present(x)		((x).pte_low & (_PAGE_PRESENT | _PAGE_PROTNONE))
324f15cbe6fSPaul Mundt 
325f15cbe6fSPaul Mundt #define pte_clear(mm,addr,xp) do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
326f15cbe6fSPaul Mundt 
327f15cbe6fSPaul Mundt #define pmd_none(x)	(!pmd_val(x))
328f15cbe6fSPaul Mundt #define pmd_present(x)	(pmd_val(x))
329f15cbe6fSPaul Mundt #define pmd_clear(xp)	do { set_pmd(xp, __pmd(0)); } while (0)
330f15cbe6fSPaul Mundt #define	pmd_bad(x)	(pmd_val(x) & ~PAGE_MASK)
331f15cbe6fSPaul Mundt 
332f15cbe6fSPaul Mundt #define pages_to_mb(x)	((x) >> (20-PAGE_SHIFT))
333f15cbe6fSPaul Mundt #define pte_page(x)	pfn_to_page(pte_pfn(x))
334f15cbe6fSPaul Mundt 
335f15cbe6fSPaul Mundt /*
336f15cbe6fSPaul Mundt  * The following only work if pte_present() is true.
337f15cbe6fSPaul Mundt  * Undefined behaviour if not..
338f15cbe6fSPaul Mundt  */
339f15cbe6fSPaul Mundt #define pte_not_present(pte)	(!((pte).pte_low & _PAGE_PRESENT))
340f15cbe6fSPaul Mundt #define pte_dirty(pte)		((pte).pte_low & _PAGE_DIRTY)
341f15cbe6fSPaul Mundt #define pte_young(pte)		((pte).pte_low & _PAGE_ACCESSED)
342c0b96cf6SPaul Mundt #define pte_special(pte)	((pte).pte_low & _PAGE_SPECIAL)
343f15cbe6fSPaul Mundt 
344f15cbe6fSPaul Mundt #ifdef CONFIG_X2TLB
345fcb4ebd6SMatt Fleming #define pte_write(pte) \
346fcb4ebd6SMatt Fleming 	((pte).pte_high & (_PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE))
347f15cbe6fSPaul Mundt #else
348f15cbe6fSPaul Mundt #define pte_write(pte)		((pte).pte_low & _PAGE_RW)
349f15cbe6fSPaul Mundt #endif
350f15cbe6fSPaul Mundt 
351f15cbe6fSPaul Mundt #define PTE_BIT_FUNC(h,fn,op) \
352f15cbe6fSPaul Mundt static inline pte_t pte_##fn(pte_t pte) { pte.pte_##h op; return pte; }
353f15cbe6fSPaul Mundt 
354f15cbe6fSPaul Mundt #ifdef CONFIG_X2TLB
355f15cbe6fSPaul Mundt /*
356f15cbe6fSPaul Mundt  * We cheat a bit in the SH-X2 TLB case. As the permission bits are
357f15cbe6fSPaul Mundt  * individually toggled (and user permissions are entirely decoupled from
358f15cbe6fSPaul Mundt  * kernel permissions), we attempt to couple them a bit more sanely here.
359f15cbe6fSPaul Mundt  */
360fcb4ebd6SMatt Fleming PTE_BIT_FUNC(high, wrprotect, &= ~(_PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE));
361f15cbe6fSPaul Mundt PTE_BIT_FUNC(high, mkwrite, |= _PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE);
362f15cbe6fSPaul Mundt PTE_BIT_FUNC(high, mkhuge, |= _PAGE_SZHUGE);
363f15cbe6fSPaul Mundt #else
364f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, wrprotect, &= ~_PAGE_RW);
365f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, mkwrite, |= _PAGE_RW);
366f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, mkhuge, |= _PAGE_SZHUGE);
367f15cbe6fSPaul Mundt #endif
368f15cbe6fSPaul Mundt 
369f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, mkclean, &= ~_PAGE_DIRTY);
370f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, mkdirty, |= _PAGE_DIRTY);
371f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, mkold, &= ~_PAGE_ACCESSED);
372f15cbe6fSPaul Mundt PTE_BIT_FUNC(low, mkyoung, |= _PAGE_ACCESSED);
373c0b96cf6SPaul Mundt PTE_BIT_FUNC(low, mkspecial, |= _PAGE_SPECIAL);
374f15cbe6fSPaul Mundt 
375f15cbe6fSPaul Mundt /*
376f15cbe6fSPaul Mundt  * Macro and implementation to make a page protection as uncachable.
377f15cbe6fSPaul Mundt  */
378f15cbe6fSPaul Mundt #define pgprot_writecombine(prot) \
379f15cbe6fSPaul Mundt 	__pgprot(pgprot_val(prot) & ~_PAGE_CACHABLE)
380f15cbe6fSPaul Mundt 
381f15cbe6fSPaul Mundt #define pgprot_noncached	 pgprot_writecombine
382f15cbe6fSPaul Mundt 
383f15cbe6fSPaul Mundt /*
384f15cbe6fSPaul Mundt  * Conversion functions: convert a page and protection to a page entry,
385f15cbe6fSPaul Mundt  * and a page entry and page directory to the page they refer to.
386f15cbe6fSPaul Mundt  *
387f15cbe6fSPaul Mundt  * extern pte_t mk_pte(struct page *page, pgprot_t pgprot)
388f15cbe6fSPaul Mundt  */
389f15cbe6fSPaul Mundt #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
390f15cbe6fSPaul Mundt 
391f15cbe6fSPaul Mundt static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
392f15cbe6fSPaul Mundt {
393f15cbe6fSPaul Mundt 	pte.pte_low &= _PAGE_CHG_MASK;
394f15cbe6fSPaul Mundt 	pte.pte_low |= pgprot_val(newprot);
395f15cbe6fSPaul Mundt 
396f15cbe6fSPaul Mundt #ifdef CONFIG_X2TLB
397f15cbe6fSPaul Mundt 	pte.pte_high |= pgprot_val(newprot) >> 32;
398f15cbe6fSPaul Mundt #endif
399f15cbe6fSPaul Mundt 
400f15cbe6fSPaul Mundt 	return pte;
401f15cbe6fSPaul Mundt }
402f15cbe6fSPaul Mundt 
403f15cbe6fSPaul Mundt #define pmd_page_vaddr(pmd)	((unsigned long)pmd_val(pmd))
404f15cbe6fSPaul Mundt #define pmd_page(pmd)		(virt_to_page(pmd_val(pmd)))
405f15cbe6fSPaul Mundt 
406f15cbe6fSPaul Mundt /* to find an entry in a page-table-directory. */
407f15cbe6fSPaul Mundt #define pgd_index(address)	(((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
408f15cbe6fSPaul Mundt #define pgd_offset(mm, address)	((mm)->pgd + pgd_index(address))
4090906a3adSPaul Mundt #define __pgd_offset(address)	pgd_index(address)
410f15cbe6fSPaul Mundt 
411f15cbe6fSPaul Mundt /* to find an entry in a kernel page-table-directory */
412f15cbe6fSPaul Mundt #define pgd_offset_k(address)	pgd_offset(&init_mm, address)
413f15cbe6fSPaul Mundt 
4140906a3adSPaul Mundt #define __pud_offset(address)	(((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
4150906a3adSPaul Mundt #define __pmd_offset(address)	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
4160906a3adSPaul Mundt 
417f15cbe6fSPaul Mundt /* Find an entry in the third-level page table.. */
418f15cbe6fSPaul Mundt #define pte_index(address)	((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
4190906a3adSPaul Mundt #define __pte_offset(address)	pte_index(address)
4200906a3adSPaul Mundt 
421f15cbe6fSPaul Mundt #define pte_offset_kernel(dir, address) \
422f15cbe6fSPaul Mundt 	((pte_t *) pmd_page_vaddr(*(dir)) + pte_index(address))
423f15cbe6fSPaul Mundt #define pte_offset_map(dir, address)		pte_offset_kernel(dir, address)
424f15cbe6fSPaul Mundt #define pte_unmap(pte)		do { } while (0)
425f15cbe6fSPaul Mundt 
426f15cbe6fSPaul Mundt #ifdef CONFIG_X2TLB
427f15cbe6fSPaul Mundt #define pte_ERROR(e) \
428f15cbe6fSPaul Mundt 	printk("%s:%d: bad pte %p(%08lx%08lx).\n", __FILE__, __LINE__, \
429f15cbe6fSPaul Mundt 	       &(e), (e).pte_high, (e).pte_low)
430f15cbe6fSPaul Mundt #define pgd_ERROR(e) \
431f15cbe6fSPaul Mundt 	printk("%s:%d: bad pgd %016llx.\n", __FILE__, __LINE__, pgd_val(e))
432f15cbe6fSPaul Mundt #else
433f15cbe6fSPaul Mundt #define pte_ERROR(e) \
434f15cbe6fSPaul Mundt 	printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
435f15cbe6fSPaul Mundt #define pgd_ERROR(e) \
436f15cbe6fSPaul Mundt 	printk("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
437f15cbe6fSPaul Mundt #endif
438f15cbe6fSPaul Mundt 
439f15cbe6fSPaul Mundt /*
440f15cbe6fSPaul Mundt  * Encode and de-code a swap entry
441f15cbe6fSPaul Mundt  *
442f15cbe6fSPaul Mundt  * Constraints:
443f15cbe6fSPaul Mundt  *	_PAGE_PRESENT at bit 8
444f15cbe6fSPaul Mundt  *	_PAGE_PROTNONE at bit 9
445f15cbe6fSPaul Mundt  *
446f15cbe6fSPaul Mundt  * For the normal case, we encode the swap type into bits 0:7 and the
447f15cbe6fSPaul Mundt  * swap offset into bits 10:30. For the 64-bit PTE case, we keep the
448f15cbe6fSPaul Mundt  * preserved bits in the low 32-bits and use the upper 32 as the swap
449f15cbe6fSPaul Mundt  * offset (along with a 5-bit type), following the same approach as x86
450*8b70beacSKirill A. Shutemov  * PAE. This keeps the logic quite simple.
451f15cbe6fSPaul Mundt  *
452f15cbe6fSPaul Mundt  * As is evident by the Alpha code, if we ever get a 64-bit unsigned
453f15cbe6fSPaul Mundt  * long (swp_entry_t) to match up with the 64-bit PTEs, this all becomes
454f15cbe6fSPaul Mundt  * much cleaner..
455f15cbe6fSPaul Mundt  *
456f15cbe6fSPaul Mundt  * NOTE: We should set ZEROs at the position of _PAGE_PRESENT
457f15cbe6fSPaul Mundt  *       and _PAGE_PROTNONE bits
458f15cbe6fSPaul Mundt  */
459f15cbe6fSPaul Mundt #ifdef CONFIG_X2TLB
460f15cbe6fSPaul Mundt #define __swp_type(x)			((x).val & 0x1f)
461f15cbe6fSPaul Mundt #define __swp_offset(x)			((x).val >> 5)
462f15cbe6fSPaul Mundt #define __swp_entry(type, offset)	((swp_entry_t){ (type) | (offset) << 5})
463f15cbe6fSPaul Mundt #define __pte_to_swp_entry(pte)		((swp_entry_t){ (pte).pte_high })
464f15cbe6fSPaul Mundt #define __swp_entry_to_pte(x)		((pte_t){ 0, (x).val })
465f15cbe6fSPaul Mundt 
466f15cbe6fSPaul Mundt #else
467f15cbe6fSPaul Mundt #define __swp_type(x)			((x).val & 0xff)
468f15cbe6fSPaul Mundt #define __swp_offset(x)			((x).val >> 10)
469f15cbe6fSPaul Mundt #define __swp_entry(type, offset)	((swp_entry_t){(type) | (offset) <<10})
470f15cbe6fSPaul Mundt 
471f15cbe6fSPaul Mundt #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) >> 1 })
472f15cbe6fSPaul Mundt #define __swp_entry_to_pte(x)		((pte_t) { (x).val << 1 })
473f15cbe6fSPaul Mundt #endif
474f15cbe6fSPaul Mundt 
475f15cbe6fSPaul Mundt #endif /* __ASSEMBLY__ */
476f15cbe6fSPaul Mundt #endif /* __ASM_SH_PGTABLE_32_H */
477