blob: 667a6c46b5a12ab6f37a2fb306d6fd5eee0ccca7 [file] [log] [blame]
Chris Zankel9a8fd552005-06-23 22:01:26 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/include/asm-xtensa/pgtable.h
Chris Zankel9a8fd552005-06-23 22:01:26 -07003 *
4 * This program is free software; you can redistribute it and/or modify
Chris Zankel01858d12007-08-06 23:57:57 -07005 * it under the terms of the GNU General Public License version 2 as
Chris Zankel9a8fd552005-06-23 22:01:26 -07006 * published by the Free Software Foundation.
7 *
Chris Zankel01858d12007-08-06 23:57:57 -07008 * Copyright (C) 2001 - 2007 Tensilica Inc.
Chris Zankel9a8fd552005-06-23 22:01:26 -07009 */
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
Chris Zankel9a8fd552005-06-23 22:01:26 -070017/*
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,
Chris Zankel01858d12007-08-06 23:57:57 -070026 * i.e. the logical three-level Linux page table layout is folded.
Chris Zankel9a8fd552005-06-23 22:01:26 -070027 * 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 */
Chris Zankel01858d12007-08-06 23:57:57 -070046
Chris Zankel9a8fd552005-06-23 22:01:26 -070047#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
Chris Zankel9a8fd552005-06-23 22:01:26 -070057#define PTRS_PER_PGD 1024
58#define PGD_ORDER 0
Chris Zankel9a8fd552005-06-23 22:01:26 -070059#define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
Chris Zankel01858d12007-08-06 23:57:57 -070060#define FIRST_USER_ADDRESS 0
Chris Zankel9a8fd552005-06-23 22:01:26 -070061#define FIRST_USER_PGD_NR (FIRST_USER_ADDRESS >> PGDIR_SHIFT)
62
63/* virtual memory area. We keep a distance to other memory regions to be
64 * on the safe side. We also use this area for cache aliasing.
65 */
66
67// FIXME: virtual memory area must be configuration-dependent
68
69#define VMALLOC_START 0xC0000000
70#define VMALLOC_END 0xC7FF0000
71
72/* Xtensa Linux config PTE layout (when present):
73 * 31-12: PPN
74 * 11-6: Software
75 * 5-4: RING
76 * 3-0: CA
77 *
78 * Similar to the Alpha and MIPS ports, we need to keep track of the ref
79 * and mod bits in software. We have a software "you can read
80 * from this page" bit, and a hardware one which actually lets the
81 * process read from the page. On the same token we have a software
82 * writable bit and the real hardware one which actually lets the
83 * process write to the page.
84 *
85 * See further below for PTE layout for swapped-out pages.
86 */
87
Chris Zankel01858d12007-08-06 23:57:57 -070088#define _PAGE_HW_EXEC (1<<0) /* hardware: page is executable */
89#define _PAGE_HW_WRITE (1<<1) /* hardware: page is writable */
90
91#define _PAGE_FILE (1<<1) /* non-linear mapping, if !present */
92#define _PAGE_PROTNONE (3<<0) /* special case for VM_PROT_NONE */
Chris Zankel9a8fd552005-06-23 22:01:26 -070093
94/* None of these cache modes include MP coherency: */
Chris Zankel01858d12007-08-06 23:57:57 -070095#define _PAGE_CA_BYPASS (0<<2) /* bypass, non-speculative */
96#define _PAGE_CA_WB (1<<2) /* write-back */
97#define _PAGE_CA_WT (2<<2) /* write-through */
98#define _PAGE_CA_MASK (3<<2)
99#define _PAGE_INVALID (3<<2)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700100
101#define _PAGE_USER (1<<4) /* user access (ring=1) */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700102
103/* Software */
Chris Zankel01858d12007-08-06 23:57:57 -0700104#define _PAGE_WRITABLE_BIT 6
105#define _PAGE_WRITABLE (1<<6) /* software: page writable */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700106#define _PAGE_DIRTY (1<<7) /* software: page dirty */
107#define _PAGE_ACCESSED (1<<8) /* software: page accessed (read) */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700108
Chris Zankel01858d12007-08-06 23:57:57 -0700109/* On older HW revisions, we always have to set bit 0 */
110#if XCHAL_HW_VERSION_MAJOR < 2000
111# define _PAGE_VALID (1<<0)
112#else
113# define _PAGE_VALID 0
114#endif
115
116#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
117#define _PAGE_PRESENT (_PAGE_VALID | _PAGE_CA_WB | _PAGE_ACCESSED)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700118
119#ifdef CONFIG_MMU
120
Chris Zankel01858d12007-08-06 23:57:57 -0700121#define PAGE_NONE __pgprot(_PAGE_INVALID | _PAGE_USER | _PAGE_PROTNONE)
122#define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER)
123#define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
124#define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER)
125#define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
126#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE)
127#define PAGE_SHARED_EXEC \
128 __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE | _PAGE_HW_EXEC)
129#define PAGE_KERNEL __pgprot(_PAGE_PRESENT)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700130
Chris Zankel01858d12007-08-06 23:57:57 -0700131#if (DCACHE_WAY_SIZE > PAGE_SIZE)
132# define _PAGE_DIRECTORY (_PAGE_VALID | _PAGE_ACCESSED | _PAGE_HW_WRITE)
133#else
134# define _PAGE_DIRECTORY (_PAGE_VALID|_PAGE_ACCESSED|_PAGE_HW_WRITE|_PAGE_CA_WB)
135#endif
Chris Zankel9a8fd552005-06-23 22:01:26 -0700136
137#else /* no mmu */
138
139# define PAGE_NONE __pgprot(0)
140# define PAGE_SHARED __pgprot(0)
141# define PAGE_COPY __pgprot(0)
142# define PAGE_READONLY __pgprot(0)
143# define PAGE_KERNEL __pgprot(0)
144
145#endif
146
147/*
148 * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
149 * the MMU can't do page protection for execute, and considers that the same as
150 * read. Also, write permissions may imply read permissions.
151 * What follows is the closest we can get by reasonable means..
152 * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
153 */
Chris Zankel01858d12007-08-06 23:57:57 -0700154#define __P000 PAGE_NONE /* private --- */
155#define __P001 PAGE_READONLY /* private --r */
156#define __P010 PAGE_COPY /* private -w- */
157#define __P011 PAGE_COPY /* private -wr */
158#define __P100 PAGE_READONLY_EXEC /* private x-- */
159#define __P101 PAGE_READONLY_EXEC /* private x-r */
160#define __P110 PAGE_COPY_EXEC /* private xw- */
161#define __P111 PAGE_COPY_EXEC /* private xwr */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700162
Chris Zankel01858d12007-08-06 23:57:57 -0700163#define __S000 PAGE_NONE /* shared --- */
164#define __S001 PAGE_READONLY /* shared --r */
165#define __S010 PAGE_SHARED /* shared -w- */
166#define __S011 PAGE_SHARED /* shared -wr */
167#define __S100 PAGE_READONLY_EXEC /* shared x-- */
168#define __S101 PAGE_READONLY_EXEC /* shared x-r */
169#define __S110 PAGE_SHARED_EXEC /* shared xw- */
170#define __S111 PAGE_SHARED_EXEC /* shared xwr */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700171
172#ifndef __ASSEMBLY__
173
174#define pte_ERROR(e) \
175 printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
176#define pgd_ERROR(e) \
177 printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
178
179extern unsigned long empty_zero_page[1024];
180
181#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
182
183extern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
184
185/*
186 * The pmd contains the kernel virtual address of the pte page.
187 */
Dave McCracken46a82b22006-09-25 23:31:48 -0700188#define pmd_page_vaddr(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
Chris Zankel9a8fd552005-06-23 22:01:26 -0700189#define pmd_page(pmd) virt_to_page(pmd_val(pmd))
190
191/*
Chris Zankel01858d12007-08-06 23:57:57 -0700192 * pte status.
Chris Zankel9a8fd552005-06-23 22:01:26 -0700193 */
Chris Zankel01858d12007-08-06 23:57:57 -0700194#define pte_none(pte) (pte_val(pte) == _PAGE_INVALID)
195#define pte_present(pte) \
196 (((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_INVALID) \
197 || ((pte_val(pte) & _PAGE_PROTNONE) == _PAGE_PROTNONE))
Chris Zankel9a8fd552005-06-23 22:01:26 -0700198#define pte_clear(mm,addr,ptep) \
Chris Zankel01858d12007-08-06 23:57:57 -0700199 do { update_pte(ptep, __pte(_PAGE_INVALID)); } while(0)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700200
201#define pmd_none(pmd) (!pmd_val(pmd))
202#define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700203#define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK)
Chris Zankel01858d12007-08-06 23:57:57 -0700204#define pmd_clear(pmdp) do { set_pmd(pmdp, __pmd(0)); } while (0)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700205
Chris Zankel01858d12007-08-06 23:57:57 -0700206static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITABLE; }
Chris Zankel9a8fd552005-06-23 22:01:26 -0700207static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
208static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
209static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
Chris Zankel01858d12007-08-06 23:57:57 -0700210static inline pte_t pte_wrprotect(pte_t pte)
211 { pte_val(pte) &= ~(_PAGE_WRITABLE | _PAGE_HW_WRITE); return pte; }
212static inline pte_t pte_mkclean(pte_t pte)
213 { pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HW_WRITE); return pte; }
214static inline pte_t pte_mkold(pte_t pte)
215 { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
216static inline pte_t pte_mkdirty(pte_t pte)
217 { pte_val(pte) |= _PAGE_DIRTY; return pte; }
218static inline pte_t pte_mkyoung(pte_t pte)
219 { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
220static inline pte_t pte_mkwrite(pte_t pte)
221 { pte_val(pte) |= _PAGE_WRITABLE; return pte; }
Chris Zankel9a8fd552005-06-23 22:01:26 -0700222
223/*
224 * Conversion functions: convert a page and protection to a page entry,
225 * and a page entry and page directory to the page they refer to.
226 */
Chris Zankel01858d12007-08-06 23:57:57 -0700227
Chris Zankel9a8fd552005-06-23 22:01:26 -0700228#define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
229#define pte_same(a,b) (pte_val(a) == pte_val(b))
230#define pte_page(x) pfn_to_page(pte_pfn(x))
231#define pfn_pte(pfn, prot) __pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
232#define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot)
233
Adrian Bunkd99cf712005-09-03 15:57:53 -0700234static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700235{
236 return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
237}
238
239/*
240 * Certain architectures need to do special things when pte's
241 * within a page table are directly modified. Thus, the following
242 * hook is made available.
243 */
244static inline void update_pte(pte_t *ptep, pte_t pteval)
245{
246 *ptep = pteval;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700247}
248
Tim Schmielau8c65b4a2005-11-07 00:59:43 -0800249struct mm_struct;
250
Adrian Bunkd99cf712005-09-03 15:57:53 -0700251static inline void
Chris Zankel9a8fd552005-06-23 22:01:26 -0700252set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
253{
254 update_pte(ptep, pteval);
255}
256
257
Adrian Bunkd99cf712005-09-03 15:57:53 -0700258static inline void
Chris Zankel9a8fd552005-06-23 22:01:26 -0700259set_pmd(pmd_t *pmdp, pmd_t pmdval)
260{
261 *pmdp = pmdval;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700262}
263
Tim Schmielau8c65b4a2005-11-07 00:59:43 -0800264struct vm_area_struct;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700265
266static inline int
267ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
268 pte_t *ptep)
269{
270 pte_t pte = *ptep;
271 if (!pte_young(pte))
272 return 0;
273 update_pte(ptep, pte_mkold(pte));
274 return 1;
275}
276
Chris Zankel9a8fd552005-06-23 22:01:26 -0700277static inline pte_t
278ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
279{
280 pte_t pte = *ptep;
281 pte_clear(mm, addr, ptep);
282 return pte;
283}
284
285static inline void
286ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
287{
288 pte_t pte = *ptep;
289 update_pte(ptep, pte_wrprotect(pte));
290}
291
292/* to find an entry in a kernel page-table-directory */
293#define pgd_offset_k(address) pgd_offset(&init_mm, address)
294
295/* to find an entry in a page-table-directory */
296#define pgd_offset(mm,address) ((mm)->pgd + pgd_index(address))
297
298#define pgd_index(address) ((address) >> PGDIR_SHIFT)
299
300/* Find an entry in the second-level page table.. */
301#define pmd_offset(dir,address) ((pmd_t*)(dir))
302
303/* Find an entry in the third-level page table.. */
304#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
305#define pte_offset_kernel(dir,addr) \
Dave McCracken46a82b22006-09-25 23:31:48 -0700306 ((pte_t*) pmd_page_vaddr(*(dir)) + pte_index(addr))
Chris Zankel9a8fd552005-06-23 22:01:26 -0700307#define pte_offset_map(dir,addr) pte_offset_kernel((dir),(addr))
308#define pte_offset_map_nested(dir,addr) pte_offset_kernel((dir),(addr))
309
310#define pte_unmap(pte) do { } while (0)
311#define pte_unmap_nested(pte) do { } while (0)
312
313
314/*
315 * Encode and decode a swap entry.
Chris Zankel9a8fd552005-06-23 22:01:26 -0700316 *
Chris Zankel01858d12007-08-06 23:57:57 -0700317 * Format of swap pte:
318 * bit 0 MBZ
319 * bit 1 page-file (must be zero)
320 * bits 2 - 3 page hw access mode (must be 11: _PAGE_INVALID)
321 * bits 4 - 5 ring protection (must be 01: _PAGE_USER)
322 * bits 6 - 10 swap type (5 bits -> 32 types)
323 * bits 11 - 31 swap offset / PAGE_SIZE (21 bits -> 8GB)
324
325 * Format of file pte:
326 * bit 0 MBZ
327 * bit 1 page-file (must be one: _PAGE_FILE)
328 * bits 2 - 3 page hw access mode (must be 11: _PAGE_INVALID)
329 * bits 4 - 5 ring protection (must be 01: _PAGE_USER)
330 * bits 6 - 31 file offset / PAGE_SIZE
Chris Zankel9a8fd552005-06-23 22:01:26 -0700331 */
332
Chris Zankel01858d12007-08-06 23:57:57 -0700333#define __swp_type(entry) (((entry).val >> 6) & 0x1f)
334#define __swp_offset(entry) ((entry).val >> 11)
335#define __swp_entry(type,offs) \
336 ((swp_entry_t) {((type) << 6) | ((offs) << 11) | _PAGE_INVALID})
Chris Zankel9a8fd552005-06-23 22:01:26 -0700337#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
338#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
339
Chris Zankel01858d12007-08-06 23:57:57 -0700340#define PTE_FILE_MAX_BITS 28
341#define pte_to_pgoff(pte) (pte_val(pte) >> 4)
342#define pgoff_to_pte(off) \
343 ((pte_t) { ((off) << 4) | _PAGE_INVALID | _PAGE_FILE })
Chris Zankel9a8fd552005-06-23 22:01:26 -0700344
345#endif /* !defined (__ASSEMBLY__) */
346
347
348#ifdef __ASSEMBLY__
349
350/* Assembly macro _PGD_INDEX is the same as C pgd_index(unsigned long),
351 * _PGD_OFFSET as C pgd_offset(struct mm_struct*, unsigned long),
352 * _PMD_OFFSET as C pmd_offset(pgd_t*, unsigned long)
353 * _PTE_OFFSET as C pte_offset(pmd_t*, unsigned long)
354 *
355 * Note: We require an additional temporary register which can be the same as
356 * the register that holds the address.
357 *
358 * ((pte_t*) ((unsigned long)(pmd_val(*pmd) & PAGE_MASK)) + pte_index(addr))
359 *
360 */
361#define _PGD_INDEX(rt,rs) extui rt, rs, PGDIR_SHIFT, 32-PGDIR_SHIFT
362#define _PTE_INDEX(rt,rs) extui rt, rs, PAGE_SHIFT, PTRS_PER_PTE_SHIFT
363
364#define _PGD_OFFSET(mm,adr,tmp) l32i mm, mm, MM_PGD; \
365 _PGD_INDEX(tmp, adr); \
366 addx4 mm, tmp, mm
367
368#define _PTE_OFFSET(pmd,adr,tmp) _PTE_INDEX(tmp, adr); \
369 srli pmd, pmd, PAGE_SHIFT; \
370 slli pmd, pmd, PAGE_SHIFT; \
371 addx4 pmd, tmp, pmd
372
373#else
374
375extern void paging_init(void);
376
377#define kern_addr_valid(addr) (1)
378
379extern void update_mmu_cache(struct vm_area_struct * vma,
380 unsigned long address, pte_t pte);
381
382/*
Randy Dunlap33bf5612005-09-13 01:25:50 -0700383 * remap a physical page `pfn' of size `size' with page protection `prot'
Chris Zankel9a8fd552005-06-23 22:01:26 -0700384 * into virtual address `from'
385 */
Randy Dunlap33bf5612005-09-13 01:25:50 -0700386#define io_remap_pfn_range(vma,from,pfn,size,prot) \
387 remap_pfn_range(vma, from, pfn, size, prot)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700388
389
390/* No page table caches to init */
391
392#define pgtable_cache_init() do { } while (0)
393
394typedef pte_t *pte_addr_t;
395
396#endif /* !defined (__ASSEMBLY__) */
397
398#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
Chris Zankel9a8fd552005-06-23 22:01:26 -0700399#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
400#define __HAVE_ARCH_PTEP_SET_WRPROTECT
401#define __HAVE_ARCH_PTEP_MKDIRTY
402#define __HAVE_ARCH_PTE_SAME
403
404#include <asm-generic/pgtable.h>
405
406#endif /* _XTENSA_PGTABLE_H */