xref: /openbmc/linux/arch/csky/include/asm/pgtable.h (revision 0d3b051a)
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 __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) })
45 #define __swp_entry_to_pte(x)		((pte_t) { (x).val })
46 
47 #define pte_page(x)			pfn_to_page(pte_pfn(x))
48 #define __mk_pte(page_nr, pgprot)	__pte(((page_nr) << PAGE_SHIFT) | \
49 					pgprot_val(pgprot))
50 
51 /*
52  * C-SKY only has VALID and DIRTY bit in hardware. So we need to use the
53  * two bits emulate PRESENT, READ, WRITE, EXEC, MODIFIED, ACCESSED.
54  */
55 #define _PAGE_BASE	(_PAGE_PRESENT | _PAGE_ACCESSED)
56 
57 #define PAGE_NONE	__pgprot(_PAGE_PROT_NONE)
58 #define PAGE_READ	__pgprot(_PAGE_BASE | _PAGE_READ | \
59 				_CACHE_CACHED)
60 #define PAGE_WRITE	__pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_WRITE | \
61 				_CACHE_CACHED)
62 #define PAGE_SHARED PAGE_WRITE
63 
64 #define PAGE_KERNEL	__pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_VALID | \
65 				_PAGE_WRITE | _PAGE_DIRTY | _PAGE_MODIFIED | \
66 				_PAGE_GLOBAL | \
67 				_CACHE_CACHED)
68 
69 #define _PAGE_IOREMAP		(_PAGE_BASE | _PAGE_READ | _PAGE_VALID | \
70 				_PAGE_WRITE | _PAGE_DIRTY | _PAGE_MODIFIED | \
71 				_PAGE_GLOBAL | \
72 				_CACHE_UNCACHED | _PAGE_SO)
73 
74 #define _PAGE_CHG_MASK	(~(unsigned long) \
75 				(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
76 				_CACHE_MASK | _PAGE_GLOBAL))
77 
78 #define MAX_SWAPFILES_CHECK() \
79 		BUILD_BUG_ON(MAX_SWAPFILES_SHIFT != 5)
80 
81 #define __P000	PAGE_NONE
82 #define __P001	PAGE_READ
83 #define __P010	PAGE_READ
84 #define __P011	PAGE_READ
85 #define __P100	PAGE_READ
86 #define __P101	PAGE_READ
87 #define __P110	PAGE_READ
88 #define __P111	PAGE_READ
89 
90 #define __S000	PAGE_NONE
91 #define __S001	PAGE_READ
92 #define __S010	PAGE_WRITE
93 #define __S011	PAGE_WRITE
94 #define __S100	PAGE_READ
95 #define __S101	PAGE_READ
96 #define __S110	PAGE_WRITE
97 #define __S111	PAGE_WRITE
98 
99 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
100 #define ZERO_PAGE(vaddr)	(virt_to_page(empty_zero_page))
101 
102 extern void load_pgd(unsigned long pg_dir);
103 extern pte_t invalid_pte_table[PTRS_PER_PTE];
104 
105 static inline void set_pte(pte_t *p, pte_t pte)
106 {
107 	*p = pte;
108 #if defined(CONFIG_CPU_NEED_TLBSYNC)
109 	dcache_wb_line((u32)p);
110 #endif
111 	/* prevent out of order excution */
112 	smp_mb();
113 }
114 #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
115 
116 static inline pte_t *pmd_page_vaddr(pmd_t pmd)
117 {
118 	unsigned long ptr;
119 
120 	ptr = pmd_val(pmd);
121 
122 	return __va(ptr);
123 }
124 
125 #define pmd_phys(pmd) pmd_val(pmd)
126 
127 static inline void set_pmd(pmd_t *p, pmd_t pmd)
128 {
129 	*p = pmd;
130 #if defined(CONFIG_CPU_NEED_TLBSYNC)
131 	dcache_wb_line((u32)p);
132 #endif
133 	/* prevent specul excute */
134 	smp_mb();
135 }
136 
137 
138 static inline int pmd_none(pmd_t pmd)
139 {
140 	return pmd_val(pmd) == __pa(invalid_pte_table);
141 }
142 
143 #define pmd_bad(pmd)	(pmd_val(pmd) & ~PAGE_MASK)
144 
145 static inline int pmd_present(pmd_t pmd)
146 {
147 	return (pmd_val(pmd) != __pa(invalid_pte_table));
148 }
149 
150 static inline void pmd_clear(pmd_t *p)
151 {
152 	pmd_val(*p) = (__pa(invalid_pte_table));
153 #if defined(CONFIG_CPU_NEED_TLBSYNC)
154 	dcache_wb_line((u32)p);
155 #endif
156 }
157 
158 /*
159  * The following only work if pte_present() is true.
160  * Undefined behaviour if not..
161  */
162 static inline int pte_read(pte_t pte)
163 {
164 	return pte.pte_low & _PAGE_READ;
165 }
166 
167 static inline int pte_write(pte_t pte)
168 {
169 	return (pte).pte_low & _PAGE_WRITE;
170 }
171 
172 static inline int pte_dirty(pte_t pte)
173 {
174 	return (pte).pte_low & _PAGE_MODIFIED;
175 }
176 
177 static inline int pte_young(pte_t pte)
178 {
179 	return (pte).pte_low & _PAGE_ACCESSED;
180 }
181 
182 static inline pte_t pte_wrprotect(pte_t pte)
183 {
184 	pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_DIRTY);
185 	return pte;
186 }
187 
188 static inline pte_t pte_mkclean(pte_t pte)
189 {
190 	pte_val(pte) &= ~(_PAGE_MODIFIED|_PAGE_DIRTY);
191 	return pte;
192 }
193 
194 static inline pte_t pte_mkold(pte_t pte)
195 {
196 	pte_val(pte) &= ~(_PAGE_ACCESSED|_PAGE_VALID);
197 	return pte;
198 }
199 
200 static inline pte_t pte_mkwrite(pte_t pte)
201 {
202 	pte_val(pte) |= _PAGE_WRITE;
203 	if (pte_val(pte) & _PAGE_MODIFIED)
204 		pte_val(pte) |= _PAGE_DIRTY;
205 	return pte;
206 }
207 
208 static inline pte_t pte_mkdirty(pte_t pte)
209 {
210 	pte_val(pte) |= _PAGE_MODIFIED;
211 	if (pte_val(pte) & _PAGE_WRITE)
212 		pte_val(pte) |= _PAGE_DIRTY;
213 	return pte;
214 }
215 
216 static inline pte_t pte_mkyoung(pte_t pte)
217 {
218 	pte_val(pte) |= _PAGE_ACCESSED;
219 	if (pte_val(pte) & _PAGE_READ)
220 		pte_val(pte) |= _PAGE_VALID;
221 	return pte;
222 }
223 
224 #define __HAVE_PHYS_MEM_ACCESS_PROT
225 struct file;
226 extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
227 				     unsigned long size, pgprot_t vma_prot);
228 
229 /*
230  * Macro to make mark a page protection value as "uncacheable".  Note
231  * that "protection" is really a misnomer here as the protection value
232  * contains the memory attribute bits, dirty bits, and various other
233  * bits as well.
234  */
235 #define pgprot_noncached pgprot_noncached
236 
237 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
238 {
239 	unsigned long prot = pgprot_val(_prot);
240 
241 	prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED | _PAGE_SO;
242 
243 	return __pgprot(prot);
244 }
245 
246 #define pgprot_writecombine pgprot_writecombine
247 static inline pgprot_t pgprot_writecombine(pgprot_t _prot)
248 {
249 	unsigned long prot = pgprot_val(_prot);
250 
251 	prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
252 
253 	return __pgprot(prot);
254 }
255 
256 /*
257  * Conversion functions: convert a page and protection to a page entry,
258  * and a page entry and page directory to the page they refer to.
259  */
260 #define mk_pte(page, pgprot)    pfn_pte(page_to_pfn(page), (pgprot))
261 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
262 {
263 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) |
264 		     (pgprot_val(newprot)));
265 }
266 
267 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
268 extern void paging_init(void);
269 
270 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
271 		      pte_t *pte);
272 
273 /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
274 #define kern_addr_valid(addr)	(1)
275 
276 #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
277 	remap_pfn_range(vma, vaddr, pfn, size, prot)
278 
279 #endif /* __ASM_CSKY_PGTABLE_H */
280