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