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