xref: /linux/arch/csky/include/asm/pgtable.h (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3 
4 #ifndef __ASM_CSKY_PGTABLE_H
5 #define __ASM_CSKY_PGTABLE_H
6 
7 #include <asm/fixmap.h>
8 #include <asm/addrspace.h>
9 #include <abi/pgtable-bits.h>
10 #include <asm-generic/pgtable-nopmd.h>
11 
12 #define PGDIR_SHIFT		22
13 #define PGDIR_SIZE		(1UL << PGDIR_SHIFT)
14 #define PGDIR_MASK		(~(PGDIR_SIZE-1))
15 
16 #define USER_PTRS_PER_PGD	(0x80000000UL/PGDIR_SIZE)
17 #define FIRST_USER_ADDRESS	0UL
18 
19 #define PKMAP_BASE		(0xff800000)
20 
21 #define VMALLOC_START		(0xc0008000)
22 #define VMALLOC_END		(PKMAP_BASE - 2*PAGE_SIZE)
23 
24 /*
25  * C-SKY is two-level paging structure:
26  */
27 #define PGD_ORDER	0
28 #define PTE_ORDER	0
29 
30 #define PTRS_PER_PGD	((PAGE_SIZE << PGD_ORDER) / sizeof(pgd_t))
31 #define PTRS_PER_PMD	1
32 #define PTRS_PER_PTE	((PAGE_SIZE << PTE_ORDER) / sizeof(pte_t))
33 
34 #define pte_ERROR(e) \
35 	pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, (e).pte_low)
36 #define pgd_ERROR(e) \
37 	pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
38 
39 /* Find an entry in the third-level page table.. */
40 #define __pte_offset_t(address) \
41 	(((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
42 #define pte_offset_kernel(dir, address) \
43 	(pmd_page_vaddr(*(dir)) + __pte_offset_t(address))
44 #define pte_offset_map(dir, address) \
45 	((pte_t *)page_address(pmd_page(*(dir))) + __pte_offset_t(address))
46 #define pmd_page(pmd)	(pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
47 #define pte_clear(mm, addr, ptep)	set_pte((ptep), \
48 	(((unsigned int) addr & PAGE_OFFSET) ? __pte(_PAGE_GLOBAL) : __pte(0)))
49 #define pte_none(pte)		(!(pte_val(pte) & ~_PAGE_GLOBAL))
50 #define pte_present(pte)	(pte_val(pte) & _PAGE_PRESENT)
51 #define pte_pfn(x)	((unsigned long)((x).pte_low >> PAGE_SHIFT))
52 #define pfn_pte(pfn, prot) __pte(((unsigned long long)(pfn) << PAGE_SHIFT) \
53 				| pgprot_val(prot))
54 
55 #define __READABLE	(_PAGE_READ | _PAGE_VALID | _PAGE_ACCESSED)
56 #define __WRITEABLE	(_PAGE_WRITE | _PAGE_DIRTY | _PAGE_MODIFIED)
57 
58 #define _PAGE_CHG_MASK	(PAGE_MASK | _PAGE_ACCESSED | _PAGE_MODIFIED | \
59 			 _CACHE_MASK)
60 
61 #define pte_unmap(pte)	((void)(pte))
62 
63 #define __swp_type(x)			(((x).val >> 4) & 0xff)
64 #define __swp_offset(x)			((x).val >> 12)
65 #define __swp_entry(type, offset)	((swp_entry_t) {((type) << 4) | \
66 					((offset) << 12) })
67 #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) })
68 #define __swp_entry_to_pte(x)		((pte_t) { (x).val })
69 
70 #define pte_page(x)			pfn_to_page(pte_pfn(x))
71 #define __mk_pte(page_nr, pgprot)	__pte(((page_nr) << PAGE_SHIFT) | \
72 					pgprot_val(pgprot))
73 
74 /*
75  * CSKY can't do page protection for execute, and considers that the same like
76  * read. Also, write permissions imply read permissions. This is the closest
77  * we can get by reasonable means..
78  */
79 #define PAGE_NONE	__pgprot(_PAGE_PRESENT | _CACHE_CACHED)
80 #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
81 				_CACHE_CACHED)
82 #define PAGE_COPY	__pgprot(_PAGE_PRESENT | _PAGE_READ | _CACHE_CACHED)
83 #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_READ | _CACHE_CACHED)
84 #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | __READABLE | __WRITEABLE | \
85 				_PAGE_GLOBAL | _CACHE_CACHED)
86 #define PAGE_USERIO	__pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
87 				_CACHE_CACHED)
88 
89 #define _PAGE_IOREMAP \
90 	(_PAGE_PRESENT | __READABLE | __WRITEABLE | _PAGE_GLOBAL | \
91 	 _CACHE_UNCACHED | _PAGE_SO)
92 
93 #define __P000	PAGE_NONE
94 #define __P001	PAGE_READONLY
95 #define __P010	PAGE_COPY
96 #define __P011	PAGE_COPY
97 #define __P100	PAGE_READONLY
98 #define __P101	PAGE_READONLY
99 #define __P110	PAGE_COPY
100 #define __P111	PAGE_COPY
101 
102 #define __S000	PAGE_NONE
103 #define __S001	PAGE_READONLY
104 #define __S010	PAGE_SHARED
105 #define __S011	PAGE_SHARED
106 #define __S100	PAGE_READONLY
107 #define __S101	PAGE_READONLY
108 #define __S110	PAGE_SHARED
109 #define __S111	PAGE_SHARED
110 
111 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
112 #define ZERO_PAGE(vaddr)	(virt_to_page(empty_zero_page))
113 
114 extern void load_pgd(unsigned long pg_dir);
115 extern pte_t invalid_pte_table[PTRS_PER_PTE];
116 
117 static inline int pte_special(pte_t pte) { return 0; }
118 static inline pte_t pte_mkspecial(pte_t pte) { return pte; }
119 
120 static inline void set_pte(pte_t *p, pte_t pte)
121 {
122 	*p = pte;
123 #if defined(CONFIG_CPU_NEED_TLBSYNC)
124 	dcache_wb_line((u32)p);
125 #endif
126 	/* prevent out of order excution */
127 	smp_mb();
128 }
129 #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
130 
131 static inline pte_t *pmd_page_vaddr(pmd_t pmd)
132 {
133 	unsigned long ptr;
134 
135 	ptr = pmd_val(pmd);
136 
137 	return __va(ptr);
138 }
139 
140 #define pmd_phys(pmd) pmd_val(pmd)
141 
142 static inline void set_pmd(pmd_t *p, pmd_t pmd)
143 {
144 	*p = pmd;
145 #if defined(CONFIG_CPU_NEED_TLBSYNC)
146 	dcache_wb_line((u32)p);
147 #endif
148 	/* prevent specul excute */
149 	smp_mb();
150 }
151 
152 
153 static inline int pmd_none(pmd_t pmd)
154 {
155 	return pmd_val(pmd) == __pa(invalid_pte_table);
156 }
157 
158 #define pmd_bad(pmd)	(pmd_val(pmd) & ~PAGE_MASK)
159 
160 static inline int pmd_present(pmd_t pmd)
161 {
162 	return (pmd_val(pmd) != __pa(invalid_pte_table));
163 }
164 
165 static inline void pmd_clear(pmd_t *p)
166 {
167 	pmd_val(*p) = (__pa(invalid_pte_table));
168 #if defined(CONFIG_CPU_NEED_TLBSYNC)
169 	dcache_wb_line((u32)p);
170 #endif
171 }
172 
173 /*
174  * The following only work if pte_present() is true.
175  * Undefined behaviour if not..
176  */
177 static inline int pte_read(pte_t pte)
178 {
179 	return pte.pte_low & _PAGE_READ;
180 }
181 
182 static inline int pte_write(pte_t pte)
183 {
184 	return (pte).pte_low & _PAGE_WRITE;
185 }
186 
187 static inline int pte_dirty(pte_t pte)
188 {
189 	return (pte).pte_low & _PAGE_MODIFIED;
190 }
191 
192 static inline int pte_young(pte_t pte)
193 {
194 	return (pte).pte_low & _PAGE_ACCESSED;
195 }
196 
197 static inline pte_t pte_wrprotect(pte_t pte)
198 {
199 	pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_DIRTY);
200 	return pte;
201 }
202 
203 static inline pte_t pte_mkclean(pte_t pte)
204 {
205 	pte_val(pte) &= ~(_PAGE_MODIFIED|_PAGE_DIRTY);
206 	return pte;
207 }
208 
209 static inline pte_t pte_mkold(pte_t pte)
210 {
211 	pte_val(pte) &= ~(_PAGE_ACCESSED|_PAGE_VALID);
212 	return pte;
213 }
214 
215 static inline pte_t pte_mkwrite(pte_t pte)
216 {
217 	pte_val(pte) |= _PAGE_WRITE;
218 	if (pte_val(pte) & _PAGE_MODIFIED)
219 		pte_val(pte) |= _PAGE_DIRTY;
220 	return pte;
221 }
222 
223 static inline pte_t pte_mkdirty(pte_t pte)
224 {
225 	pte_val(pte) |= _PAGE_MODIFIED;
226 	if (pte_val(pte) & _PAGE_WRITE)
227 		pte_val(pte) |= _PAGE_DIRTY;
228 	return pte;
229 }
230 
231 static inline pte_t pte_mkyoung(pte_t pte)
232 {
233 	pte_val(pte) |= _PAGE_ACCESSED;
234 	if (pte_val(pte) & _PAGE_READ)
235 		pte_val(pte) |= _PAGE_VALID;
236 	return pte;
237 }
238 
239 #define __pgd_offset(address)	pgd_index(address)
240 #define __pud_offset(address)	(((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
241 #define __pmd_offset(address)	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
242 
243 /* to find an entry in a kernel page-table-directory */
244 #define pgd_offset_k(address)	pgd_offset(&init_mm, address)
245 
246 #define pgd_index(address)	((address) >> PGDIR_SHIFT)
247 
248 #define __HAVE_PHYS_MEM_ACCESS_PROT
249 struct file;
250 extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
251 				     unsigned long size, pgprot_t vma_prot);
252 
253 /*
254  * Macro to make mark a page protection value as "uncacheable".  Note
255  * that "protection" is really a misnomer here as the protection value
256  * contains the memory attribute bits, dirty bits, and various other
257  * bits as well.
258  */
259 #define pgprot_noncached pgprot_noncached
260 
261 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
262 {
263 	unsigned long prot = pgprot_val(_prot);
264 
265 	prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED | _PAGE_SO;
266 
267 	return __pgprot(prot);
268 }
269 
270 #define pgprot_writecombine pgprot_writecombine
271 static inline pgprot_t pgprot_writecombine(pgprot_t _prot)
272 {
273 	unsigned long prot = pgprot_val(_prot);
274 
275 	prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
276 
277 	return __pgprot(prot);
278 }
279 
280 /*
281  * Conversion functions: convert a page and protection to a page entry,
282  * and a page entry and page directory to the page they refer to.
283  */
284 #define mk_pte(page, pgprot)    pfn_pte(page_to_pfn(page), (pgprot))
285 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
286 {
287 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) |
288 		     (pgprot_val(newprot)));
289 }
290 
291 /* to find an entry in a page-table-directory */
292 static inline pgd_t *pgd_offset(struct mm_struct *mm, unsigned long address)
293 {
294 	return mm->pgd + pgd_index(address);
295 }
296 
297 /* Find an entry in the third-level page table.. */
298 static inline pte_t *pte_offset(pmd_t *dir, unsigned long address)
299 {
300 	return (pte_t *) (pmd_page_vaddr(*dir)) +
301 		((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
302 }
303 
304 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
305 extern void paging_init(void);
306 
307 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
308 		      pte_t *pte);
309 
310 /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
311 #define kern_addr_valid(addr)	(1)
312 
313 #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
314 	remap_pfn_range(vma, vaddr, pfn, size, prot)
315 
316 #include <asm-generic/pgtable.h>
317 
318 #endif /* __ASM_CSKY_PGTABLE_H */
319