]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - arch/xtensa/include/asm/pgtable.h
Merge remote-tracking branches 'spi/topic/mxs', 'spi/topic/pxa', 'spi/topic/rockchip...
[mirror_ubuntu-eoan-kernel.git] / arch / xtensa / include / asm / pgtable.h
CommitLineData
9a8fd558 1/*
6656920b 2 * include/asm-xtensa/pgtable.h
9a8fd558
CZ
3 *
4 * This program is free software; you can redistribute it and/or modify
01858d1b 5 * it under the terms of the GNU General Public License version 2 as
9a8fd558
CZ
6 * published by the Free Software Foundation.
7 *
7711ece9 8 * Copyright (C) 2001 - 2013 Tensilica Inc.
9a8fd558
CZ
9 */
10
11#ifndef _XTENSA_PGTABLE_H
12#define _XTENSA_PGTABLE_H
13
14#include <asm-generic/pgtable-nopmd.h>
15#include <asm/page.h>
16
9a8fd558
CZ
17/*
18 * We only use two ring levels, user and kernel space.
19 */
20
21#define USER_RING 1 /* user ring level */
22#define KERNEL_RING 0 /* kernel ring level */
23
24/*
25 * The Xtensa architecture port of Linux has a two-level page table system,
01858d1b 26 * i.e. the logical three-level Linux page table layout is folded.
9a8fd558
CZ
27 * Each task has the following memory page tables:
28 *
29 * PGD table (page directory), ie. 3rd-level page table:
30 * One page (4 kB) of 1024 (PTRS_PER_PGD) pointers to PTE tables
31 * (Architectures that don't have the PMD folded point to the PMD tables)
32 *
33 * The pointer to the PGD table for a given task can be retrieved from
34 * the task structure (struct task_struct*) t, e.g. current():
35 * (t->mm ? t->mm : t->active_mm)->pgd
36 *
37 * PMD tables (page middle-directory), ie. 2nd-level page tables:
38 * Absent for the Xtensa architecture (folded, PTRS_PER_PMD == 1).
39 *
40 * PTE tables (page table entry), ie. 1st-level page tables:
41 * One page (4 kB) of 1024 (PTRS_PER_PTE) PTEs with a special PTE
42 * invalid_pte_table for absent mappings.
43 *
44 * The individual pages are 4 kB big with special pages for the empty_zero_page.
45 */
01858d1b 46
9a8fd558
CZ
47#define PGDIR_SHIFT 22
48#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
49#define PGDIR_MASK (~(PGDIR_SIZE-1))
50
51/*
52 * Entries per page directory level: we use two-level, so
53 * we don't really have any PMD directory physically.
54 */
55#define PTRS_PER_PTE 1024
56#define PTRS_PER_PTE_SHIFT 10
9a8fd558
CZ
57#define PTRS_PER_PGD 1024
58#define PGD_ORDER 0
9a8fd558 59#define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
01858d1b 60#define FIRST_USER_ADDRESS 0
9a8fd558
CZ
61#define FIRST_USER_PGD_NR (FIRST_USER_ADDRESS >> PGDIR_SHIFT)
62
6656920b
CZ
63/*
64 * Virtual memory area. We keep a distance to other memory regions to be
9a8fd558
CZ
65 * on the safe side. We also use this area for cache aliasing.
66 */
9a8fd558 67#define VMALLOC_START 0xC0000000
3b4a49e2
CZ
68#define VMALLOC_END 0xC7FEFFFF
69#define TLBTEMP_BASE_1 0xC7FF0000
7128039f
MF
70#define TLBTEMP_BASE_2 (TLBTEMP_BASE_1 + DCACHE_WAY_SIZE)
71#if 2 * DCACHE_WAY_SIZE > ICACHE_WAY_SIZE
72#define TLBTEMP_SIZE (2 * DCACHE_WAY_SIZE)
73#else
74#define TLBTEMP_SIZE ICACHE_WAY_SIZE
75#endif
9a8fd558 76
6656920b 77/*
7711ece9
CZ
78 * For the Xtensa architecture, the PTE layout is as follows:
79 *
80 * 31------12 11 10-9 8-6 5-4 3-2 1-0
81 * +-----------------------------------------+
82 * | | Software | HARDWARE |
83 * | PPN | ADW | RI |Attribute|
84 * +-----------------------------------------+
85 * pte_none | MBZ | 01 | 11 | 00 |
86 * +-----------------------------------------+
87 * present | PPN | 0 | 00 | ADW | RI | CA | wx |
88 * +- - - - - - - - - - - - - - - - - - - - -+
89 * (PAGE_NONE)| PPN | 0 | 00 | ADW | 01 | 11 | 11 |
90 * +-----------------------------------------+
91 * swap | index | type | 01 | 11 | 00 |
92 * +- - - - - - - - - - - - - - - - - - - - -+
93 * file | file offset | 01 | 11 | 10 |
94 * +-----------------------------------------+
95 *
96 * For T1050 hardware and earlier the layout differs for present and (PAGE_NONE)
97 * +-----------------------------------------+
98 * present | PPN | 0 | 00 | ADW | RI | CA | w1 |
99 * +-----------------------------------------+
100 * (PAGE_NONE)| PPN | 0 | 00 | ADW | 01 | 01 | 00 |
101 * +-----------------------------------------+
9a8fd558 102 *
7711ece9
CZ
103 * Legend:
104 * PPN Physical Page Number
105 * ADW software: accessed (young) / dirty / writable
106 * RI ring (0=privileged, 1=user, 2 and 3 are unused)
107 * CA cache attribute: 00 bypass, 01 writeback, 10 writethrough
108 * (11 is invalid and used to mark pages that are not present)
109 * w page is writable (hw)
110 * x page is executable (hw)
111 * index swap offset / PAGE_SIZE (bit 11-31: 21 bits -> 8 GB)
112 * (note that the index is always non-zero)
113 * type swap type (5 bits -> 32 types)
114 * file offset 26-bit offset into the file, in increments of PAGE_SIZE
9a8fd558 115 *
7711ece9
CZ
116 * Notes:
117 * - (PROT_NONE) is a special case of 'present' but causes an exception for
118 * any access (read, write, and execute).
119 * - 'multihit-exception' has the highest priority of all MMU exceptions,
120 * so the ring must be set to 'RING_USER' even for 'non-present' pages.
121 * - on older hardware, the exectuable flag was not supported and
122 * used as a 'valid' flag, so it needs to be always set.
123 * - we need to keep track of certain flags in software (dirty and young)
124 * to do this, we use write exceptions and have a separate software w-flag.
125 * - attribute value 1101 (and 1111 on T1050 and earlier) is reserved
9a8fd558
CZ
126 */
127
7711ece9
CZ
128#define _PAGE_ATTRIB_MASK 0xf
129
01858d1b
CZ
130#define _PAGE_HW_EXEC (1<<0) /* hardware: page is executable */
131#define _PAGE_HW_WRITE (1<<1) /* hardware: page is writable */
132
01858d1b
CZ
133#define _PAGE_CA_BYPASS (0<<2) /* bypass, non-speculative */
134#define _PAGE_CA_WB (1<<2) /* write-back */
135#define _PAGE_CA_WT (2<<2) /* write-through */
136#define _PAGE_CA_MASK (3<<2)
7711ece9
CZ
137#define _PAGE_CA_INVALID (3<<2)
138
139/* We use invalid attribute values to distinguish special pte entries */
140#if XCHAL_HW_VERSION_MAJOR < 2000
141#define _PAGE_HW_VALID 0x01 /* older HW needed this bit set */
142#define _PAGE_NONE 0x04
143#else
144#define _PAGE_HW_VALID 0x00
145#define _PAGE_NONE 0x0f
146#endif
147#define _PAGE_FILE (1<<1) /* file mapped page, only if !present */
9a8fd558
CZ
148
149#define _PAGE_USER (1<<4) /* user access (ring=1) */
9a8fd558
CZ
150
151/* Software */
01858d1b
CZ
152#define _PAGE_WRITABLE_BIT 6
153#define _PAGE_WRITABLE (1<<6) /* software: page writable */
9a8fd558
CZ
154#define _PAGE_DIRTY (1<<7) /* software: page dirty */
155#define _PAGE_ACCESSED (1<<8) /* software: page accessed (read) */
9a8fd558 156
9a8fd558
CZ
157#ifdef CONFIG_MMU
158
7711ece9
CZ
159#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
160#define _PAGE_PRESENT (_PAGE_HW_VALID | _PAGE_CA_WB | _PAGE_ACCESSED)
161
162#define PAGE_NONE __pgprot(_PAGE_NONE | _PAGE_USER)
01858d1b
CZ
163#define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER)
164#define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
165#define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER)
166#define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
167#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE)
168#define PAGE_SHARED_EXEC \
169 __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE | _PAGE_HW_EXEC)
6656920b
CZ
170#define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_HW_WRITE)
171#define PAGE_KERNEL_EXEC __pgprot(_PAGE_PRESENT|_PAGE_HW_WRITE|_PAGE_HW_EXEC)
01858d1b
CZ
172
173#if (DCACHE_WAY_SIZE > PAGE_SIZE)
7711ece9 174# define _PAGE_DIRECTORY (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_BYPASS)
01858d1b 175#else
7711ece9 176# define _PAGE_DIRECTORY (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_WB)
01858d1b 177#endif
9a8fd558
CZ
178
179#else /* no mmu */
180
181# define PAGE_NONE __pgprot(0)
182# define PAGE_SHARED __pgprot(0)
183# define PAGE_COPY __pgprot(0)
184# define PAGE_READONLY __pgprot(0)
185# define PAGE_KERNEL __pgprot(0)
186
187#endif
188
189/*
190 * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
191 * the MMU can't do page protection for execute, and considers that the same as
192 * read. Also, write permissions may imply read permissions.
193 * What follows is the closest we can get by reasonable means..
194 * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
195 */
01858d1b
CZ
196#define __P000 PAGE_NONE /* private --- */
197#define __P001 PAGE_READONLY /* private --r */
198#define __P010 PAGE_COPY /* private -w- */
199#define __P011 PAGE_COPY /* private -wr */
200#define __P100 PAGE_READONLY_EXEC /* private x-- */
201#define __P101 PAGE_READONLY_EXEC /* private x-r */
202#define __P110 PAGE_COPY_EXEC /* private xw- */
203#define __P111 PAGE_COPY_EXEC /* private xwr */
204
205#define __S000 PAGE_NONE /* shared --- */
206#define __S001 PAGE_READONLY /* shared --r */
207#define __S010 PAGE_SHARED /* shared -w- */
208#define __S011 PAGE_SHARED /* shared -wr */
209#define __S100 PAGE_READONLY_EXEC /* shared x-- */
210#define __S101 PAGE_READONLY_EXEC /* shared x-r */
211#define __S110 PAGE_SHARED_EXEC /* shared xw- */
212#define __S111 PAGE_SHARED_EXEC /* shared xwr */
9a8fd558
CZ
213
214#ifndef __ASSEMBLY__
215
216#define pte_ERROR(e) \
217 printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
218#define pgd_ERROR(e) \
219 printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
220
221extern unsigned long empty_zero_page[1024];
222
223#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
224
e5083a63 225#ifdef CONFIG_MMU
9a8fd558 226extern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
e5083a63 227extern void paging_init(void);
e5083a63
JW
228#else
229# define swapper_pg_dir NULL
230static inline void paging_init(void) { }
e5083a63 231#endif
f820e280 232static inline void pgtable_cache_init(void) { }
9a8fd558
CZ
233
234/*
235 * The pmd contains the kernel virtual address of the pte page.
236 */
46a82b2d 237#define pmd_page_vaddr(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
9a8fd558
CZ
238#define pmd_page(pmd) virt_to_page(pmd_val(pmd))
239
240/*
01858d1b 241 * pte status.
9a8fd558 242 */
7711ece9
CZ
243# define pte_none(pte) (pte_val(pte) == (_PAGE_CA_INVALID | _PAGE_USER))
244#if XCHAL_HW_VERSION_MAJOR < 2000
245# define pte_present(pte) ((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID)
246#else
247# define pte_present(pte) \
248 (((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID) \
249 || ((pte_val(pte) & _PAGE_ATTRIB_MASK) == _PAGE_NONE))
250#endif
9a8fd558 251#define pte_clear(mm,addr,ptep) \
7711ece9 252 do { update_pte(ptep, __pte(_PAGE_CA_INVALID | _PAGE_USER)); } while (0)
9a8fd558
CZ
253
254#define pmd_none(pmd) (!pmd_val(pmd))
255#define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
9a8fd558 256#define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK)
01858d1b 257#define pmd_clear(pmdp) do { set_pmd(pmdp, __pmd(0)); } while (0)
9a8fd558 258
01858d1b 259static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITABLE; }
9a8fd558
CZ
260static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
261static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
262static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
7e675137
NP
263static inline int pte_special(pte_t pte) { return 0; }
264
01858d1b
CZ
265static inline pte_t pte_wrprotect(pte_t pte)
266 { pte_val(pte) &= ~(_PAGE_WRITABLE | _PAGE_HW_WRITE); return pte; }
267static inline pte_t pte_mkclean(pte_t pte)
268 { pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HW_WRITE); return pte; }
269static inline pte_t pte_mkold(pte_t pte)
270 { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
271static inline pte_t pte_mkdirty(pte_t pte)
272 { pte_val(pte) |= _PAGE_DIRTY; return pte; }
273static inline pte_t pte_mkyoung(pte_t pte)
274 { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
275static inline pte_t pte_mkwrite(pte_t pte)
276 { pte_val(pte) |= _PAGE_WRITABLE; return pte; }
7e675137
NP
277static inline pte_t pte_mkspecial(pte_t pte)
278 { return pte; }
9a8fd558 279
a211276a
MF
280#define pgprot_noncached(prot) (__pgprot(pgprot_val(prot) & ~_PAGE_CA_MASK))
281
9a8fd558
CZ
282/*
283 * Conversion functions: convert a page and protection to a page entry,
284 * and a page entry and page directory to the page they refer to.
285 */
01858d1b 286
9a8fd558
CZ
287#define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
288#define pte_same(a,b) (pte_val(a) == pte_val(b))
289#define pte_page(x) pfn_to_page(pte_pfn(x))
290#define pfn_pte(pfn, prot) __pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
291#define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot)
292
d99cf715 293static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
9a8fd558
CZ
294{
295 return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
296}
297
298/*
299 * Certain architectures need to do special things when pte's
300 * within a page table are directly modified. Thus, the following
301 * hook is made available.
302 */
303static inline void update_pte(pte_t *ptep, pte_t pteval)
304{
305 *ptep = pteval;
6656920b
CZ
306#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
307 __asm__ __volatile__ ("dhwb %0, 0" :: "a" (ptep));
308#endif
309
9a8fd558
CZ
310}
311
8c65b4a6
TS
312struct mm_struct;
313
d99cf715 314static inline void
9a8fd558
CZ
315set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
316{
317 update_pte(ptep, pteval);
318}
319
65559100
MF
320static inline void set_pte(pte_t *ptep, pte_t pteval)
321{
322 update_pte(ptep, pteval);
323}
9a8fd558 324
d99cf715 325static inline void
9a8fd558
CZ
326set_pmd(pmd_t *pmdp, pmd_t pmdval)
327{
328 *pmdp = pmdval;
9a8fd558
CZ
329}
330
8c65b4a6 331struct vm_area_struct;
9a8fd558
CZ
332
333static inline int
334ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
c4c4594b 335 pte_t *ptep)
9a8fd558
CZ
336{
337 pte_t pte = *ptep;
338 if (!pte_young(pte))
339 return 0;
340 update_pte(ptep, pte_mkold(pte));
341 return 1;
342}
343
9a8fd558
CZ
344static inline pte_t
345ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
346{
347 pte_t pte = *ptep;
348 pte_clear(mm, addr, ptep);
349 return pte;
350}
351
352static inline void
353ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
354{
c4c4594b
CZ
355 pte_t pte = *ptep;
356 update_pte(ptep, pte_wrprotect(pte));
9a8fd558
CZ
357}
358
359/* to find an entry in a kernel page-table-directory */
360#define pgd_offset_k(address) pgd_offset(&init_mm, address)
361
362/* to find an entry in a page-table-directory */
363#define pgd_offset(mm,address) ((mm)->pgd + pgd_index(address))
364
365#define pgd_index(address) ((address) >> PGDIR_SHIFT)
366
367/* Find an entry in the second-level page table.. */
368#define pmd_offset(dir,address) ((pmd_t*)(dir))
369
370/* Find an entry in the third-level page table.. */
371#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
372#define pte_offset_kernel(dir,addr) \
46a82b2d 373 ((pte_t*) pmd_page_vaddr(*(dir)) + pte_index(addr))
9a8fd558 374#define pte_offset_map(dir,addr) pte_offset_kernel((dir),(addr))
9a8fd558 375#define pte_unmap(pte) do { } while (0)
9a8fd558
CZ
376
377
378/*
7711ece9 379 * Encode and decode a swap and file entry.
9a8fd558 380 */
7711ece9
CZ
381#define SWP_TYPE_BITS 5
382#define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
9a8fd558 383
01858d1b
CZ
384#define __swp_type(entry) (((entry).val >> 6) & 0x1f)
385#define __swp_offset(entry) ((entry).val >> 11)
386#define __swp_entry(type,offs) \
7711ece9
CZ
387 ((swp_entry_t){((type) << 6) | ((offs) << 11) | \
388 _PAGE_CA_INVALID | _PAGE_USER})
9a8fd558
CZ
389#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
390#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
391
7711ece9
CZ
392#define PTE_FILE_MAX_BITS 26
393#define pte_to_pgoff(pte) (pte_val(pte) >> 6)
01858d1b 394#define pgoff_to_pte(off) \
7711ece9 395 ((pte_t) { ((off) << 6) | _PAGE_CA_INVALID | _PAGE_FILE | _PAGE_USER })
9a8fd558
CZ
396
397#endif /* !defined (__ASSEMBLY__) */
398
399
400#ifdef __ASSEMBLY__
401
402/* Assembly macro _PGD_INDEX is the same as C pgd_index(unsigned long),
403 * _PGD_OFFSET as C pgd_offset(struct mm_struct*, unsigned long),
404 * _PMD_OFFSET as C pmd_offset(pgd_t*, unsigned long)
405 * _PTE_OFFSET as C pte_offset(pmd_t*, unsigned long)
406 *
407 * Note: We require an additional temporary register which can be the same as
408 * the register that holds the address.
409 *
410 * ((pte_t*) ((unsigned long)(pmd_val(*pmd) & PAGE_MASK)) + pte_index(addr))
411 *
412 */
413#define _PGD_INDEX(rt,rs) extui rt, rs, PGDIR_SHIFT, 32-PGDIR_SHIFT
414#define _PTE_INDEX(rt,rs) extui rt, rs, PAGE_SHIFT, PTRS_PER_PTE_SHIFT
415
416#define _PGD_OFFSET(mm,adr,tmp) l32i mm, mm, MM_PGD; \
417 _PGD_INDEX(tmp, adr); \
418 addx4 mm, tmp, mm
419
420#define _PTE_OFFSET(pmd,adr,tmp) _PTE_INDEX(tmp, adr); \
421 srli pmd, pmd, PAGE_SHIFT; \
422 slli pmd, pmd, PAGE_SHIFT; \
423 addx4 pmd, tmp, pmd
424
425#else
426
9a8fd558
CZ
427#define kern_addr_valid(addr) (1)
428
429extern void update_mmu_cache(struct vm_area_struct * vma,
4b3073e1 430 unsigned long address, pte_t *ptep);
9a8fd558 431
9a8fd558
CZ
432typedef pte_t *pte_addr_t;
433
434#endif /* !defined (__ASSEMBLY__) */
435
436#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
9a8fd558
CZ
437#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
438#define __HAVE_ARCH_PTEP_SET_WRPROTECT
439#define __HAVE_ARCH_PTEP_MKDIRTY
440#define __HAVE_ARCH_PTE_SAME
de73b6b1
MF
441/* We provide our own get_unmapped_area to cope with
442 * SHM area cache aliasing for userland.
443 */
444#define HAVE_ARCH_UNMAPPED_AREA
9a8fd558
CZ
445
446#include <asm-generic/pgtable.h>
447
448#endif /* _XTENSA_PGTABLE_H */