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