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