blob: 6783937edbeba2885ccc16e22a3328665f494370 [file] [log] [blame]
Greentime Hu2e1aecb2017-10-24 14:53:31 +08001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2005-2017 Andes Technology Corporation
3
4#ifndef _ASMNDS32_PGTABLE_H
5#define _ASMNDS32_PGTABLE_H
6
7#define __PAGETABLE_PMD_FOLDED
8#include <asm-generic/4level-fixup.h>
9#include <asm-generic/sizes.h>
10
11#include <asm/memory.h>
12#include <asm/nds32.h>
13#ifndef __ASSEMBLY__
14#include <asm/fixmap.h>
15#include <asm/io.h>
16#include <nds32_intrinsic.h>
17#endif
18
19#ifdef CONFIG_ANDES_PAGE_SIZE_4KB
20#define PGDIR_SHIFT 22
21#define PTRS_PER_PGD 1024
22#define PMD_SHIFT 22
23#define PTRS_PER_PMD 1
24#define PTRS_PER_PTE 1024
25#endif
26
27#ifdef CONFIG_ANDES_PAGE_SIZE_8KB
28#define PGDIR_SHIFT 24
29#define PTRS_PER_PGD 256
30#define PMD_SHIFT 24
31#define PTRS_PER_PMD 1
32#define PTRS_PER_PTE 2048
33#endif
34
35#ifndef __ASSEMBLY__
36extern void __pte_error(const char *file, int line, unsigned long val);
37extern void __pmd_error(const char *file, int line, unsigned long val);
38extern void __pgd_error(const char *file, int line, unsigned long val);
39
40#define pte_ERROR(pte) __pte_error(__FILE__, __LINE__, pte_val(pte))
41#define pmd_ERROR(pmd) __pmd_error(__FILE__, __LINE__, pmd_val(pmd))
42#define pgd_ERROR(pgd) __pgd_error(__FILE__, __LINE__, pgd_val(pgd))
43#endif /* !__ASSEMBLY__ */
44
45#define PMD_SIZE (1UL << PMD_SHIFT)
46#define PMD_MASK (~(PMD_SIZE-1))
47#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
48#define PGDIR_MASK (~(PGDIR_SIZE-1))
49
50/*
51 * This is the lowest virtual address we can permit any user space
52 * mapping to be mapped at. This is particularly important for
53 * non-high vector CPUs.
54 */
55#define FIRST_USER_ADDRESS 0x8000
56
57#ifdef CONFIG_HIGHMEM
58#define CONSISTENT_BASE ((PKMAP_BASE) - (SZ_2M))
59#define CONSISTENT_END (PKMAP_BASE)
60#else
61#define CONSISTENT_BASE (FIXADDR_START - SZ_2M)
62#define CONSISTENT_END (FIXADDR_START)
63#endif
64#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
65
66#ifdef CONFIG_HIGHMEM
67#ifndef __ASSEMBLY__
68#include <asm/highmem.h>
69#endif
70#endif
71
72#define VMALLOC_RESERVE SZ_128M
73#define VMALLOC_END (CONSISTENT_BASE - PAGE_SIZE)
74#define VMALLOC_START ((VMALLOC_END) - VMALLOC_RESERVE)
75#define VMALLOC_VMADDR(x) ((unsigned long)(x))
76#define MAXMEM __pa(VMALLOC_START)
77#define MAXMEM_PFN PFN_DOWN(MAXMEM)
78
79#define FIRST_USER_PGD_NR 0
80#define USER_PTRS_PER_PGD ((TASK_SIZE/PGDIR_SIZE) + FIRST_USER_PGD_NR)
81
82/* L2 PTE */
83#define _PAGE_V (1UL << 0)
84
85#define _PAGE_M_XKRW (0UL << 1)
86#define _PAGE_M_UR_KR (1UL << 1)
87#define _PAGE_M_UR_KRW (2UL << 1)
88#define _PAGE_M_URW_KRW (3UL << 1)
89#define _PAGE_M_KR (5UL << 1)
90#define _PAGE_M_KRW (7UL << 1)
91
92#define _PAGE_D (1UL << 4)
93#define _PAGE_E (1UL << 5)
94#define _PAGE_A (1UL << 6)
95#define _PAGE_G (1UL << 7)
96
97#define _PAGE_C_DEV (0UL << 8)
98#define _PAGE_C_DEV_WB (1UL << 8)
99#define _PAGE_C_MEM (2UL << 8)
100#define _PAGE_C_MEM_SHRD_WB (4UL << 8)
101#define _PAGE_C_MEM_SHRD_WT (5UL << 8)
102#define _PAGE_C_MEM_WB (6UL << 8)
103#define _PAGE_C_MEM_WT (7UL << 8)
104
105#define _PAGE_L (1UL << 11)
106
107#define _HAVE_PAGE_L (_PAGE_L)
108#define _PAGE_FILE (1UL << 1)
109#define _PAGE_YOUNG 0
110#define _PAGE_M_MASK _PAGE_M_KRW
111#define _PAGE_C_MASK _PAGE_C_MEM_WT
112
113#ifdef CONFIG_SMP
114#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
115#define _PAGE_CACHE_SHRD _PAGE_C_MEM_SHRD_WT
116#else
117#define _PAGE_CACHE_SHRD _PAGE_C_MEM_SHRD_WB
118#endif
119#else
120#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
121#define _PAGE_CACHE_SHRD _PAGE_C_MEM_WT
122#else
123#define _PAGE_CACHE_SHRD _PAGE_C_MEM_WB
124#endif
125#endif
126
127#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
128#define _PAGE_CACHE _PAGE_C_MEM_WT
129#else
130#define _PAGE_CACHE _PAGE_C_MEM_WB
131#endif
132
133/*
134 * + Level 1 descriptor (PMD)
135 */
136#define PMD_TYPE_TABLE 0
137
138#ifndef __ASSEMBLY__
139
140#define _PAGE_USER_TABLE PMD_TYPE_TABLE
141#define _PAGE_KERNEL_TABLE PMD_TYPE_TABLE
142
143#define PAGE_EXEC __pgprot(_PAGE_V | _PAGE_M_XKRW | _PAGE_E)
144#define PAGE_NONE __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_A)
145#define PAGE_READ __pgprot(_PAGE_V | _PAGE_M_UR_KR)
146#define PAGE_RDWR __pgprot(_PAGE_V | _PAGE_M_URW_KRW | _PAGE_D)
147#define PAGE_COPY __pgprot(_PAGE_V | _PAGE_M_UR_KR)
148
149#define PAGE_UXKRWX_V1 __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
150#define PAGE_UXKRWX_V2 __pgprot(_PAGE_V | _PAGE_M_XKRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
151#define PAGE_URXKRWX_V2 __pgprot(_PAGE_V | _PAGE_M_UR_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
152#define PAGE_CACHE_L1 __pgprot(_HAVE_PAGE_L | _PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE)
153#define PAGE_MEMORY __pgprot(_HAVE_PAGE_L | _PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
154#define PAGE_KERNEL __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
155#define PAGE_DEVICE __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_G | _PAGE_C_DEV)
156#endif /* __ASSEMBLY__ */
157
158/* xwr */
159#define __P000 (PAGE_NONE | _PAGE_CACHE_SHRD)
160#define __P001 (PAGE_READ | _PAGE_CACHE_SHRD)
161#define __P010 (PAGE_COPY | _PAGE_CACHE_SHRD)
162#define __P011 (PAGE_COPY | _PAGE_CACHE_SHRD)
163#define __P100 (PAGE_EXEC | _PAGE_CACHE_SHRD)
164#define __P101 (PAGE_READ | _PAGE_E | _PAGE_CACHE_SHRD)
165#define __P110 (PAGE_COPY | _PAGE_E | _PAGE_CACHE_SHRD)
166#define __P111 (PAGE_COPY | _PAGE_E | _PAGE_CACHE_SHRD)
167
168#define __S000 (PAGE_NONE | _PAGE_CACHE_SHRD)
169#define __S001 (PAGE_READ | _PAGE_CACHE_SHRD)
170#define __S010 (PAGE_RDWR | _PAGE_CACHE_SHRD)
171#define __S011 (PAGE_RDWR | _PAGE_CACHE_SHRD)
172#define __S100 (PAGE_EXEC | _PAGE_CACHE_SHRD)
173#define __S101 (PAGE_READ | _PAGE_E | _PAGE_CACHE_SHRD)
174#define __S110 (PAGE_RDWR | _PAGE_E | _PAGE_CACHE_SHRD)
175#define __S111 (PAGE_RDWR | _PAGE_E | _PAGE_CACHE_SHRD)
176
177#ifndef __ASSEMBLY__
178/*
179 * ZERO_PAGE is a global shared page that is always zero: used
180 * for zero-mapped memory areas etc..
181 */
182extern struct page *empty_zero_page;
183extern void paging_init(void);
184#define ZERO_PAGE(vaddr) (empty_zero_page)
185
186#define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
187#define pfn_pte(pfn,prot) (__pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot)))
188
189#define pte_none(pte) !(pte_val(pte))
190#define pte_clear(mm,addr,ptep) set_pte_at((mm),(addr),(ptep), __pte(0))
191#define pte_page(pte) (pfn_to_page(pte_pfn(pte)))
192
193#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
194#define pte_offset_kernel(dir, address) ((pte_t *)pmd_page_kernel(*(dir)) + pte_index(address))
195#define pte_offset_map(dir, address) ((pte_t *)page_address(pmd_page(*(dir))) + pte_index(address))
196#define pte_offset_map_nested(dir, address) pte_offset_map(dir, address)
197#define pmd_page_kernel(pmd) ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
198
199#define pte_unmap(pte) do { } while (0)
200#define pte_unmap_nested(pte) do { } while (0)
201
202#define pmd_off_k(address) pmd_offset(pgd_offset_k(address), address)
203
204#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
205/*
206 * Set a level 1 translation table entry, and clean it out of
207 * any caches such that the MMUs can load it correctly.
208 */
209static inline void set_pmd(pmd_t * pmdp, pmd_t pmd)
210{
211
212 *pmdp = pmd;
213#if !defined(CONFIG_CPU_DCACHE_DISABLE) && !defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
214 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (pmdp):"memory");
215 __nds32__msync_all();
216 __nds32__dsb();
217#endif
218}
219
220/*
221 * Set a PTE and flush it out
222 */
223static inline void set_pte(pte_t * ptep, pte_t pte)
224{
225
226 *ptep = pte;
227#if !defined(CONFIG_CPU_DCACHE_DISABLE) && !defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
228 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (ptep):"memory");
229 __nds32__msync_all();
230 __nds32__dsb();
231#endif
232}
233
234/*
235 * The following only work if pte_present() is true.
236 * Undefined behaviour if not..
237 */
238
239/*
240 * pte_write: this page is writeable for user mode
241 * pte_read: this page is readable for user mode
242 * pte_kernel_write: this page is writeable for kernel mode
243 *
244 * We don't have pte_kernel_read because kernel always can read.
245 *
246 * */
247
248#define pte_present(pte) (pte_val(pte) & _PAGE_V)
249#define pte_write(pte) ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_URW_KRW)
250#define pte_read(pte) (((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_UR_KR) || \
251 ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_UR_KRW) || \
252 ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_URW_KRW))
253#define pte_kernel_write(pte) (((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_URW_KRW) || \
254 ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_UR_KRW) || \
255 ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_KRW) || \
256 (((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_XKRW) && pte_exec(pte)))
257#define pte_exec(pte) (pte_val(pte) & _PAGE_E)
258#define pte_dirty(pte) (pte_val(pte) & _PAGE_D)
259#define pte_young(pte) (pte_val(pte) & _PAGE_YOUNG)
260
261/*
262 * The following only works if pte_present() is not true.
263 */
264#define pte_file(pte) (pte_val(pte) & _PAGE_FILE)
265#define pte_to_pgoff(x) (pte_val(x) >> 2)
266#define pgoff_to_pte(x) __pte(((x) << 2) | _PAGE_FILE)
267
268#define PTE_FILE_MAX_BITS 29
269
270#define PTE_BIT_FUNC(fn,op) \
271static inline pte_t pte_##fn(pte_t pte) { pte_val(pte) op; return pte; }
272
273static inline pte_t pte_wrprotect(pte_t pte)
274{
275 pte_val(pte) = pte_val(pte) & ~_PAGE_M_MASK;
276 pte_val(pte) = pte_val(pte) | _PAGE_M_UR_KR;
277 return pte;
278}
279
280static inline pte_t pte_mkwrite(pte_t pte)
281{
282 pte_val(pte) = pte_val(pte) & ~_PAGE_M_MASK;
283 pte_val(pte) = pte_val(pte) | _PAGE_M_URW_KRW;
284 return pte;
285}
286
287PTE_BIT_FUNC(exprotect, &=~_PAGE_E);
288PTE_BIT_FUNC(mkexec, |=_PAGE_E);
289PTE_BIT_FUNC(mkclean, &=~_PAGE_D);
290PTE_BIT_FUNC(mkdirty, |=_PAGE_D);
291PTE_BIT_FUNC(mkold, &=~_PAGE_YOUNG);
292PTE_BIT_FUNC(mkyoung, |=_PAGE_YOUNG);
293static inline int pte_special(pte_t pte)
294{
295 return 0;
296}
297
298static inline pte_t pte_mkspecial(pte_t pte)
299{
300 return pte;
301}
302
303/*
304 * Mark the prot value as uncacheable and unbufferable.
305 */
306#define pgprot_noncached(prot) __pgprot((pgprot_val(prot)&~_PAGE_C_MASK) | _PAGE_C_DEV)
307#define pgprot_writecombine(prot) __pgprot((pgprot_val(prot)&~_PAGE_C_MASK) | _PAGE_C_DEV_WB)
308
309#define pmd_none(pmd) (pmd_val(pmd)&0x1)
310#define pmd_present(pmd) (!pmd_none(pmd))
311#define pmd_bad(pmd) pmd_none(pmd)
312
313#define copy_pmd(pmdpd,pmdps) set_pmd((pmdpd), *(pmdps))
314#define pmd_clear(pmdp) set_pmd((pmdp), __pmd(1))
315
316static inline pmd_t __mk_pmd(pte_t * ptep, unsigned long prot)
317{
318 unsigned long ptr = (unsigned long)ptep;
319 pmd_t pmd;
320
321 /*
322 * The pmd must be loaded with the physical
323 * address of the PTE table
324 */
325
326 pmd_val(pmd) = __virt_to_phys(ptr) | prot;
327 return pmd;
328}
329
330#define pmd_page(pmd) virt_to_page(__va(pmd_val(pmd)))
331
332/*
333 * Permanent address of a page. We never have highmem, so this is trivial.
334 */
335#define pages_to_mb(x) ((x) >> (20 - PAGE_SHIFT))
336
337/*
338 * Conversion functions: convert a page and protection to a page entry,
339 * and a page entry and page directory to the page they refer to.
340 */
341#define mk_pte(page,prot) pfn_pte(page_to_pfn(page),prot)
342
343/*
344 * The "pgd_xxx()" functions here are trivial for a folded two-level
345 * setup: the pgd is never bad, and a pmd always exists (as it's folded
346 * into the pgd entry)
347 */
348#define pgd_none(pgd) (0)
349#define pgd_bad(pgd) (0)
350#define pgd_present(pgd) (1)
351#define pgd_clear(pgdp) do { } while (0)
352
353#define page_pte_prot(page,prot) mk_pte(page, prot)
354#define page_pte(page) mk_pte(page, __pgprot(0))
355/*
356 * L1PTE = $mr1 + ((virt >> PMD_SHIFT) << 2);
357 * L2PTE = (((virt >> PAGE_SHIFT) & (PTRS_PER_PTE -1 )) << 2);
358 * PPN = (phys & 0xfffff000);
359 *
360*/
361
362/* to find an entry in a page-table-directory */
363#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
364#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
365/* to find an entry in a kernel page-table-directory */
366#define pgd_offset_k(addr) pgd_offset(&init_mm, addr)
367
368/* Find an entry in the second-level page table.. */
369#define pmd_offset(dir, addr) ((pmd_t *)(dir))
370
371static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
372{
373 const unsigned long mask = 0xfff;
374 pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
375 return pte;
376}
377
378extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
379
380/* Encode and decode a swap entry.
381 *
382 * We support up to 32GB of swap on 4k machines
383 */
384#define __swp_type(x) (((x).val >> 2) & 0x7f)
385#define __swp_offset(x) ((x).val >> 9)
386#define __swp_entry(type,offset) ((swp_entry_t) { ((type) << 2) | ((offset) << 9) })
387#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
388#define __swp_entry_to_pte(swp) ((pte_t) { (swp).val })
389
390/* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
391#define kern_addr_valid(addr) (1)
392
393#include <asm-generic/pgtable.h>
394
395/*
396 * We provide our own arch_get_unmapped_area to cope with VIPT caches.
397 */
398#define HAVE_ARCH_UNMAPPED_AREA
399
400/*
401 * remap a physical address `phys' of size `size' with page protection `prot'
402 * into virtual address `from'
403 */
404
405#define pgtable_cache_init() do { } while (0)
406
407#endif /* !__ASSEMBLY__ */
408
409#endif /* _ASMNDS32_PGTABLE_H */