ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-cris / pgtable.h
1 /*
2  * CRIS pgtable.h - macros and functions to manipulate page tables.
3  */
4
5 #ifndef _CRIS_PGTABLE_H
6 #define _CRIS_PGTABLE_H
7
8 #include <linux/config.h>
9 #include <linux/sched.h>
10 #include <asm/mmu.h>
11 #include <asm/arch/pgtable.h>
12
13 /*
14  * The Linux memory management assumes a three-level page table setup. On
15  * CRIS, we use that, but "fold" the mid level into the top-level page
16  * table. Since the MMU TLB is software loaded through an interrupt, it
17  * supports any page table structure, so we could have used a three-level
18  * setup, but for the amounts of memory we normally use, a two-level is
19  * probably more efficient.
20  *
21  * This file contains the functions and defines necessary to modify and use
22  * the CRIS page table tree.
23  */
24
25 extern void paging_init(void);
26
27 /* Certain architectures need to do special things when pte's
28  * within a page table are directly modified.  Thus, the following
29  * hook is made available.
30  */
31 #define set_pte(pteptr, pteval) ((*(pteptr)) = (pteval))
32 /*
33  * (pmds are folded into pgds so this doesn't get actually called,
34  * but the define is needed for a generic inline function.)
35  */
36 #define set_pmd(pmdptr, pmdval) (*(pmdptr) = pmdval)
37 #define set_pgd(pgdptr, pgdval) (*(pgdptr) = pgdval)
38
39 /* PMD_SHIFT determines the size of the area a second-level page table can
40  * map. It is equal to the page size times the number of PTE's that fit in
41  * a PMD page. A PTE is 4-bytes in CRIS. Hence the following number.
42  */
43
44 #define PMD_SHIFT       (PAGE_SHIFT + (PAGE_SHIFT-2))
45 #define PMD_SIZE        (1UL << PMD_SHIFT)
46 #define PMD_MASK        (~(PMD_SIZE-1))
47
48 /* PGDIR_SHIFT determines what a third-level page table entry can map.
49  * Since we fold into a two-level structure, this is the same as PMD_SHIFT.
50  */
51
52 #define PGDIR_SHIFT     PMD_SHIFT
53 #define PGDIR_SIZE      (1UL << PGDIR_SHIFT)
54 #define PGDIR_MASK      (~(PGDIR_SIZE-1))
55
56 /*
57  * entries per page directory level: we use a two-level, so
58  * we don't really have any PMD directory physically.
59  * pointers are 4 bytes so we can use the page size and 
60  * divide it by 4 (shift by 2).
61  */
62 #define PTRS_PER_PTE    (1UL << (PAGE_SHIFT-2))
63 #define PTRS_PER_PMD    1
64 #define PTRS_PER_PGD    (1UL << (PAGE_SHIFT-2))
65
66 /* calculate how many PGD entries a user-level program can use
67  * the first mappable virtual address is 0
68  * (TASK_SIZE is the maximum virtual address space)
69  */
70
71 #define USER_PTRS_PER_PGD       (TASK_SIZE/PGDIR_SIZE)
72 #define FIRST_USER_PGD_NR       0
73
74 /* zero page used for uninitialized stuff */
75 extern unsigned long empty_zero_page;
76 #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
77
78 /* number of bits that fit into a memory pointer */
79 #define BITS_PER_PTR                    (8*sizeof(unsigned long))
80
81 /* to align the pointer to a pointer address */
82 #define PTR_MASK                        (~(sizeof(void*)-1))
83
84 /* sizeof(void*)==1<<SIZEOF_PTR_LOG2 */
85 /* 64-bit machines, beware!  SRB. */
86 #define SIZEOF_PTR_LOG2                 2
87
88 /* to find an entry in a page-table */
89 #define PAGE_PTR(address) \
90 ((unsigned long)(address)>>(PAGE_SHIFT-SIZEOF_PTR_LOG2)&PTR_MASK&~PAGE_MASK)
91
92 /* to set the page-dir */
93 #define SET_PAGE_DIR(tsk,pgdir)
94
95 #define pte_none(x)     (!pte_val(x))
96 #define pte_present(x)  (pte_val(x) & _PAGE_PRESENT)
97 #define pte_clear(xp)   do { pte_val(*(xp)) = 0; } while (0)
98
99 #define pmd_none(x)     (!pmd_val(x))
100 /* by removing the _PAGE_KERNEL bit from the comparision, the same pmd_bad
101  * works for both _PAGE_TABLE and _KERNPG_TABLE pmd entries.
102  */
103 #define pmd_bad(x)      ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_KERNEL)) != _PAGE_TABLE)
104 #define pmd_present(x)  (pmd_val(x) & _PAGE_PRESENT)
105 #define pmd_clear(xp)   do { pmd_val(*(xp)) = 0; } while (0)
106
107 /*
108  * The "pgd_xxx()" functions here are trivial for a folded two-level
109  * setup: the pgd is never bad, and a pmd always exists (as it's folded
110  * into the pgd entry)
111  */
112 extern inline int pgd_none(pgd_t pgd)           { return 0; }
113 extern inline int pgd_bad(pgd_t pgd)            { return 0; }
114 extern inline int pgd_present(pgd_t pgd)        { return 1; }
115 extern inline void pgd_clear(pgd_t * pgdp)      { }
116
117 /*
118  * The following only work if pte_present() is true.
119  * Undefined behaviour if not..
120  */
121
122 extern inline int pte_read(pte_t pte)           { return pte_val(pte) & _PAGE_READ; }
123 extern inline int pte_write(pte_t pte)          { return pte_val(pte) & _PAGE_WRITE; }
124 extern inline int pte_exec(pte_t pte)           { return pte_val(pte) & _PAGE_READ; }
125 extern inline int pte_dirty(pte_t pte)          { return pte_val(pte) & _PAGE_MODIFIED; }
126 extern inline int pte_young(pte_t pte)          { return pte_val(pte) & _PAGE_ACCESSED; }
127 extern inline int pte_file(pte_t pte)           { return pte_val(pte) & _PAGE_FILE; }
128
129 extern inline pte_t pte_wrprotect(pte_t pte)
130 {
131         pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
132         return pte;
133 }
134
135 extern inline pte_t pte_rdprotect(pte_t pte)
136 {
137         pte_val(pte) &= ~(_PAGE_READ | _PAGE_SILENT_READ);
138         return pte;
139 }
140
141 extern inline pte_t pte_exprotect(pte_t pte)
142 {
143         pte_val(pte) &= ~(_PAGE_READ | _PAGE_SILENT_READ);
144         return pte;
145 }
146
147 extern inline pte_t pte_mkclean(pte_t pte)
148 {
149         pte_val(pte) &= ~(_PAGE_MODIFIED | _PAGE_SILENT_WRITE); 
150         return pte; 
151 }
152
153 extern inline pte_t pte_mkold(pte_t pte)
154 {
155         pte_val(pte) &= ~(_PAGE_ACCESSED | _PAGE_SILENT_READ);
156         return pte;
157 }
158
159 extern inline pte_t pte_mkwrite(pte_t pte)
160 {
161         pte_val(pte) |= _PAGE_WRITE;
162         if (pte_val(pte) & _PAGE_MODIFIED)
163                 pte_val(pte) |= _PAGE_SILENT_WRITE;
164         return pte;
165 }
166
167 extern inline pte_t pte_mkread(pte_t pte)
168 {
169         pte_val(pte) |= _PAGE_READ;
170         if (pte_val(pte) & _PAGE_ACCESSED)
171                 pte_val(pte) |= _PAGE_SILENT_READ;
172         return pte;
173 }
174
175 extern inline pte_t pte_mkexec(pte_t pte)
176 {
177         pte_val(pte) |= _PAGE_READ;
178         if (pte_val(pte) & _PAGE_ACCESSED)
179                 pte_val(pte) |= _PAGE_SILENT_READ;
180         return pte;
181 }
182
183 extern inline pte_t pte_mkdirty(pte_t pte)
184 {
185         pte_val(pte) |= _PAGE_MODIFIED;
186         if (pte_val(pte) & _PAGE_WRITE)
187                 pte_val(pte) |= _PAGE_SILENT_WRITE;
188         return pte;
189 }
190
191 extern inline pte_t pte_mkyoung(pte_t pte)
192 {
193         pte_val(pte) |= _PAGE_ACCESSED;
194         if (pte_val(pte) & _PAGE_READ)
195         {
196                 pte_val(pte) |= _PAGE_SILENT_READ;
197                 if ((pte_val(pte) & (_PAGE_WRITE | _PAGE_MODIFIED)) ==
198                     (_PAGE_WRITE | _PAGE_MODIFIED))
199                         pte_val(pte) |= _PAGE_SILENT_WRITE;
200         }
201         return pte;
202 }
203
204 /*
205  * Conversion functions: convert a page and protection to a page entry,
206  * and a page entry and page directory to the page they refer to.
207  */
208
209 /* What actually goes as arguments to the various functions is less than
210  * obvious, but a rule of thumb is that struct page's goes as struct page *,
211  * really physical DRAM addresses are unsigned long's, and DRAM "virtual"
212  * addresses (the 0xc0xxxxxx's) goes as void *'s.
213  */
214
215 extern inline pte_t __mk_pte(void * page, pgprot_t pgprot)
216 {
217         pte_t pte;
218         /* the PTE needs a physical address */
219         pte_val(pte) = __pa(page) | pgprot_val(pgprot);
220         return pte;
221 }
222
223 #define mk_pte(page, pgprot) __mk_pte(page_address(page), (pgprot))
224
225 #define mk_pte_phys(physpage, pgprot) \
226 ({                                                                      \
227         pte_t __pte;                                                    \
228                                                                         \
229         pte_val(__pte) = (physpage) + pgprot_val(pgprot);               \
230         __pte;                                                          \
231 })
232
233 extern inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
234 { pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot); return pte; }
235
236
237 /* pte_val refers to a page in the 0x4xxxxxxx physical DRAM interval
238  * __pte_page(pte_val) refers to the "virtual" DRAM interval
239  * pte_pagenr refers to the page-number counted starting from the virtual DRAM start
240  */
241
242 extern inline unsigned long __pte_page(pte_t pte)
243 {
244         /* the PTE contains a physical address */
245         return (unsigned long)__va(pte_val(pte) & PAGE_MASK);
246 }
247
248 #define pte_pagenr(pte)         ((__pte_page(pte) - PAGE_OFFSET) >> PAGE_SHIFT)
249
250 /* permanent address of a page */
251
252 #define __page_address(page)    (PAGE_OFFSET + (((page) - mem_map) << PAGE_SHIFT))
253 #define pte_page(pte)           (mem_map+pte_pagenr(pte))
254
255 /* only the pte's themselves need to point to physical DRAM (see above)
256  * the pagetable links are purely handled within the kernel SW and thus
257  * don't need the __pa and __va transformations.
258  */
259
260 extern inline void pmd_set(pmd_t * pmdp, pte_t * ptep)
261 { pmd_val(*pmdp) = _PAGE_TABLE | (unsigned long) ptep; }
262
263 #define pmd_page(pmd)           (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
264 #define pmd_page_kernel(pmd)    ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
265
266 /* to find an entry in a page-table-directory. */
267 #define pgd_index(address) ((address >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
268
269 /* to find an entry in a page-table-directory */
270 extern inline pgd_t * pgd_offset(struct mm_struct * mm, unsigned long address)
271 {
272         return mm->pgd + pgd_index(address);
273 }
274
275 /* to find an entry in a kernel page-table-directory */
276 #define pgd_offset_k(address) pgd_offset(&init_mm, address)
277
278 /* Find an entry in the second-level page table.. */
279 extern inline pmd_t * pmd_offset(pgd_t * dir, unsigned long address)
280 {
281         return (pmd_t *) dir;
282 }
283
284 /* Find an entry in the third-level page table.. */
285 #define __pte_offset(address) \
286         (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
287 #define pte_offset_kernel(dir, address) \
288         ((pte_t *) pmd_page_kernel(*(dir)) +  __pte_offset(address))
289 #define pte_offset_map(dir, address) \
290         ((pte_t *)page_address(pmd_page(*(dir))) + __pte_offset(address))
291 #define pte_offset_map_nested(dir, address) pte_offset_map(dir, address)
292
293 #define pte_unmap(pte) do { } while (0)
294 #define pte_unmap_nested(pte) do { } while (0)
295 #define pte_pfn(x)              ((unsigned long)(__va((x).pte)) >> PAGE_SHIFT)
296 #define pfn_pte(pfn, prot)      __pte((__pa((pfn) << PAGE_SHIFT)) | pgprot_val(prot))
297
298 #define pte_ERROR(e) \
299         printk("%s:%d: bad pte %p(%08lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
300 #define pmd_ERROR(e) \
301         printk("%s:%d: bad pmd %p(%08lx).\n", __FILE__, __LINE__, &(e), pmd_val(e))
302 #define pgd_ERROR(e) \
303         printk("%s:%d: bad pgd %p(%08lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
304
305
306 extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* defined in head.S */
307
308 /*
309  * CRIS doesn't have any external MMU info: the kernel page
310  * tables contain all the necessary information.
311  * 
312  * Actually I am not sure on what this could be used for.
313  */
314 extern inline void update_mmu_cache(struct vm_area_struct * vma,
315         unsigned long address, pte_t pte)
316 {
317 }
318
319 /* Encode and de-code a swap entry (must be !pte_none(e) && !pte_present(e)) */
320 /* Since the PAGE_PRESENT bit is bit 4, we can use the bits above */
321
322 #define __swp_type(x)                   (((x).val >> 5) & 0x7f)
323 #define __swp_offset(x)                 ((x).val >> 12)
324 #define __swp_entry(type, offset)       ((swp_entry_t) { ((type) << 5) | ((offset) << 12) })
325 #define __pte_to_swp_entry(pte)         ((swp_entry_t) { pte_val(pte) })
326 #define __swp_entry_to_pte(x)           ((pte_t) { (x).val })
327
328 #define kern_addr_valid(addr)   (1)
329
330 #include <asm-generic/pgtable.h>
331
332 /*
333  * No page table caches to initialise
334  */
335 #define pgtable_cache_init()   do { } while (0)
336
337 #define pte_to_pgoff(x) (pte_val(x) >> 6)
338 #define pgoff_to_pte(x) __pte(((x) << 6) | _PAGE_FILE)
339
340 typedef pte_t *pte_addr_t;
341
342 #endif /* _CRIS_PGTABLE_H */