xref: /linux/arch/nios2/include/asm/pgtable.h (revision db10cb9b)
1 /*
2  * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
3  * Copyright (C) 2009 Wind River Systems Inc
4  *
5  * Based on asm/pgtable-32.h from mips which is:
6  *
7  * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2003 Ralf Baechle
8  * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
9  *
10  * This file is subject to the terms and conditions of the GNU General Public
11  * License.  See the file "COPYING" in the main directory of this archive
12  * for more details.
13  */
14 
15 #ifndef _ASM_NIOS2_PGTABLE_H
16 #define _ASM_NIOS2_PGTABLE_H
17 
18 #include <linux/io.h>
19 #include <linux/bug.h>
20 #include <asm/page.h>
21 #include <asm/cacheflush.h>
22 #include <asm/tlbflush.h>
23 
24 #include <asm/pgtable-bits.h>
25 #include <asm-generic/pgtable-nopmd.h>
26 
27 #define VMALLOC_START		CONFIG_NIOS2_KERNEL_MMU_REGION_BASE
28 #define VMALLOC_END		(CONFIG_NIOS2_KERNEL_REGION_BASE - 1)
29 
30 struct mm_struct;
31 
32 /* Helper macro */
33 #define MKP(x, w, r) __pgprot(_PAGE_PRESENT | _PAGE_CACHED |		\
34 				((x) ? _PAGE_EXEC : 0) |		\
35 				((r) ? _PAGE_READ : 0) |		\
36 				((w) ? _PAGE_WRITE : 0))
37 /*
38  * These are the macros that generic kernel code needs
39  * (to populate protection_map[])
40  */
41 
42 /* Remove W bit on private pages for COW support */
43 
44 /* Shared pages can have exact HW mapping */
45 
46 /* Used all over the kernel */
47 #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_CACHED | _PAGE_READ | \
48 			     _PAGE_WRITE | _PAGE_EXEC | _PAGE_GLOBAL)
49 
50 #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_CACHED | _PAGE_READ | \
51 			     _PAGE_WRITE | _PAGE_ACCESSED)
52 
53 #define PAGE_COPY MKP(0, 0, 1)
54 
55 #define PTRS_PER_PGD	(PAGE_SIZE / sizeof(pgd_t))
56 #define PTRS_PER_PTE	(PAGE_SIZE / sizeof(pte_t))
57 
58 #define USER_PTRS_PER_PGD	\
59 	(CONFIG_NIOS2_KERNEL_MMU_REGION_BASE / PGDIR_SIZE)
60 
61 #define PGDIR_SHIFT	22
62 #define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
63 #define PGDIR_MASK	(~(PGDIR_SIZE-1))
64 
65 /*
66  * ZERO_PAGE is a global shared page that is always zero: used
67  * for zero-mapped memory areas etc..
68  */
69 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
70 #define ZERO_PAGE(vaddr)	(virt_to_page(empty_zero_page))
71 
72 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
73 extern pte_t invalid_pte_table[PAGE_SIZE/sizeof(pte_t)];
74 
75 /*
76  * (pmds are folded into puds so this doesn't get actually called,
77  * but the define is needed for a generic inline function.)
78  */
79 static inline void set_pmd(pmd_t *pmdptr, pmd_t pmdval)
80 {
81 	*pmdptr = pmdval;
82 }
83 
84 static inline int pte_write(pte_t pte)		\
85 	{ return pte_val(pte) & _PAGE_WRITE; }
86 static inline int pte_dirty(pte_t pte)		\
87 	{ return pte_val(pte) & _PAGE_DIRTY; }
88 static inline int pte_young(pte_t pte)		\
89 	{ return pte_val(pte) & _PAGE_ACCESSED; }
90 
91 #define pgprot_noncached pgprot_noncached
92 
93 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
94 {
95 	unsigned long prot = pgprot_val(_prot);
96 
97 	prot &= ~_PAGE_CACHED;
98 
99 	return __pgprot(prot);
100 }
101 
102 static inline int pte_none(pte_t pte)
103 {
104 	return !(pte_val(pte) & ~(_PAGE_GLOBAL|0xf));
105 }
106 
107 static inline int pte_present(pte_t pte)	\
108 	{ return pte_val(pte) & _PAGE_PRESENT; }
109 
110 /*
111  * The following only work if pte_present() is true.
112  * Undefined behaviour if not..
113  */
114 static inline pte_t pte_wrprotect(pte_t pte)
115 {
116 	pte_val(pte) &= ~_PAGE_WRITE;
117 	return pte;
118 }
119 
120 static inline pte_t pte_mkclean(pte_t pte)
121 {
122 	pte_val(pte) &= ~_PAGE_DIRTY;
123 	return pte;
124 }
125 
126 static inline pte_t pte_mkold(pte_t pte)
127 {
128 	pte_val(pte) &= ~_PAGE_ACCESSED;
129 	return pte;
130 }
131 
132 static inline pte_t pte_mkwrite_novma(pte_t pte)
133 {
134 	pte_val(pte) |= _PAGE_WRITE;
135 	return pte;
136 }
137 
138 static inline pte_t pte_mkdirty(pte_t pte)
139 {
140 	pte_val(pte) |= _PAGE_DIRTY;
141 	return pte;
142 }
143 
144 static inline pte_t pte_mkyoung(pte_t pte)
145 {
146 	pte_val(pte) |= _PAGE_ACCESSED;
147 	return pte;
148 }
149 
150 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
151 {
152 	const unsigned long mask = _PAGE_READ | _PAGE_WRITE | _PAGE_EXEC;
153 
154 	pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
155 	return pte;
156 }
157 
158 static inline int pmd_present(pmd_t pmd)
159 {
160 	return (pmd_val(pmd) != (unsigned long) invalid_pte_table)
161 			&& (pmd_val(pmd) != 0UL);
162 }
163 
164 static inline void pmd_clear(pmd_t *pmdp)
165 {
166 	pmd_val(*pmdp) = (unsigned long) invalid_pte_table;
167 }
168 
169 #define pte_pfn(pte)		(pte_val(pte) & 0xfffff)
170 #define pfn_pte(pfn, prot)	(__pte(pfn | pgprot_val(prot)))
171 #define pte_page(pte)		(pfn_to_page(pte_pfn(pte)))
172 
173 /*
174  * Store a linux PTE into the linux page table.
175  */
176 static inline void set_pte(pte_t *ptep, pte_t pteval)
177 {
178 	*ptep = pteval;
179 }
180 
181 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
182 		pte_t *ptep, pte_t pte, unsigned int nr)
183 {
184 	unsigned long paddr = (unsigned long)page_to_virt(pte_page(pte));
185 
186 	flush_dcache_range(paddr, paddr + nr * PAGE_SIZE);
187 	for (;;) {
188 		set_pte(ptep, pte);
189 		if (--nr == 0)
190 			break;
191 		ptep++;
192 		pte_val(pte) += 1;
193 	}
194 }
195 #define set_ptes set_ptes
196 
197 static inline int pmd_none(pmd_t pmd)
198 {
199 	return (pmd_val(pmd) ==
200 		(unsigned long) invalid_pte_table) || (pmd_val(pmd) == 0UL);
201 }
202 
203 #define pmd_bad(pmd)	(pmd_val(pmd) & ~PAGE_MASK)
204 
205 static inline void pte_clear(struct mm_struct *mm,
206 				unsigned long addr, pte_t *ptep)
207 {
208 	pte_t null;
209 
210 	pte_val(null) = (addr >> PAGE_SHIFT) & 0xf;
211 
212 	set_pte(ptep, null);
213 }
214 
215 /*
216  * Conversion functions: convert a page and protection to a page entry,
217  * and a page entry and page directory to the page they refer to.
218  */
219 #define mk_pte(page, prot)	(pfn_pte(page_to_pfn(page), prot))
220 
221 /*
222  * Conversion functions: convert a page and protection to a page entry,
223  * and a page entry and page directory to the page they refer to.
224  */
225 #define pmd_phys(pmd)		virt_to_phys((void *)pmd_val(pmd))
226 #define pmd_pfn(pmd)		(pmd_phys(pmd) >> PAGE_SHIFT)
227 #define pmd_page(pmd)		(pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
228 
229 static inline unsigned long pmd_page_vaddr(pmd_t pmd)
230 {
231 	return pmd_val(pmd);
232 }
233 
234 #define pte_ERROR(e) \
235 	pr_err("%s:%d: bad pte %08lx.\n", \
236 		__FILE__, __LINE__, pte_val(e))
237 #define pgd_ERROR(e) \
238 	pr_err("%s:%d: bad pgd %08lx.\n", \
239 		__FILE__, __LINE__, pgd_val(e))
240 
241 /*
242  * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
243  * are !pte_none() && !pte_present().
244  *
245  * Format of swap PTEs:
246  *
247  *   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
248  *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
249  *   E < type -> 0 0 0 0 0 0 <-------------- offset --------------->
250  *
251  *   E is the exclusive marker that is not stored in swap entries.
252  *
253  * Note that the offset field is always non-zero if the swap type is 0, thus
254  * !pte_none() is always true.
255  */
256 #define __swp_type(swp)		(((swp).val >> 26) & 0x1f)
257 #define __swp_offset(swp)	((swp).val & 0xfffff)
258 #define __swp_entry(type, off)	((swp_entry_t) { (((type) & 0x1f) << 26) \
259 						 | ((off) & 0xfffff) })
260 #define __swp_entry_to_pte(swp)	((pte_t) { (swp).val })
261 #define __pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
262 
263 static inline int pte_swp_exclusive(pte_t pte)
264 {
265 	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
266 }
267 
268 static inline pte_t pte_swp_mkexclusive(pte_t pte)
269 {
270 	pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
271 	return pte;
272 }
273 
274 static inline pte_t pte_swp_clear_exclusive(pte_t pte)
275 {
276 	pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
277 	return pte;
278 }
279 
280 extern void __init paging_init(void);
281 extern void __init mmu_init(void);
282 
283 void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
284 		unsigned long address, pte_t *ptep, unsigned int nr);
285 
286 #define update_mmu_cache(vma, addr, ptep) \
287 	update_mmu_cache_range(NULL, vma, addr, ptep, 1)
288 
289 #endif /* _ASM_NIOS2_PGTABLE_H */
290