sparc32: Un-btfixup pmd_{bad,present}().
[cascardo/linux.git] / arch / sparc / include / asm / pgtable_32.h
1 #ifndef _SPARC_PGTABLE_H
2 #define _SPARC_PGTABLE_H
3
4 /*  asm/pgtable.h:  Defines and functions used to work
5  *                        with Sparc page tables.
6  *
7  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
8  *  Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9  */
10
11 #include <linux/const.h>
12
13 #ifndef __ASSEMBLY__
14 #include <asm-generic/4level-fixup.h>
15
16 #include <linux/spinlock.h>
17 #include <linux/swap.h>
18 #include <asm/types.h>
19 #include <asm/pgtsrmmu.h>
20 #include <asm/oplib.h>
21 #include <asm/btfixup.h>
22 #include <asm/cpu_type.h>
23
24
25 struct vm_area_struct;
26 struct page;
27
28 extern void load_mmu(void);
29 extern unsigned long calc_highpages(void);
30
31 #define pte_ERROR(e)   __builtin_trap()
32 #define pmd_ERROR(e)   __builtin_trap()
33 #define pgd_ERROR(e)   __builtin_trap()
34
35 #define PMD_SHIFT               22
36 #define PMD_SIZE                (1UL << PMD_SHIFT)
37 #define PMD_MASK                (~(PMD_SIZE-1))
38 #define PMD_ALIGN(__addr)       (((__addr) + ~PMD_MASK) & PMD_MASK)
39 #define PGDIR_SHIFT             SRMMU_PGDIR_SHIFT
40 #define PGDIR_SIZE              SRMMU_PGDIR_SIZE
41 #define PGDIR_MASK              SRMMU_PGDIR_MASK
42 #define PTRS_PER_PTE            1024
43 #define PTRS_PER_PMD            SRMMU_PTRS_PER_PMD
44 #define PTRS_PER_PGD            SRMMU_PTRS_PER_PGD
45 #define USER_PTRS_PER_PGD       PAGE_OFFSET / SRMMU_PGDIR_SIZE
46 #define FIRST_USER_ADDRESS      0
47 #define PTE_SIZE                (PTRS_PER_PTE*4)
48
49 #define PAGE_NONE       SRMMU_PAGE_NONE
50 #define PAGE_SHARED     SRMMU_PAGE_SHARED
51 #define PAGE_COPY       SRMMU_PAGE_COPY
52 #define PAGE_READONLY   SRMMU_PAGE_RDONLY
53 #define PAGE_KERNEL     SRMMU_PAGE_KERNEL
54
55 /* Top-level page directory */
56 extern pgd_t swapper_pg_dir[1024];
57
58 extern void paging_init(void);
59
60 extern unsigned long ptr_in_current_pgd;
61
62 /*         xwr */
63 #define __P000  PAGE_NONE
64 #define __P001  PAGE_READONLY
65 #define __P010  PAGE_COPY
66 #define __P011  PAGE_COPY
67 #define __P100  PAGE_READONLY
68 #define __P101  PAGE_READONLY
69 #define __P110  PAGE_COPY
70 #define __P111  PAGE_COPY
71
72 #define __S000  PAGE_NONE
73 #define __S001  PAGE_READONLY
74 #define __S010  PAGE_SHARED
75 #define __S011  PAGE_SHARED
76 #define __S100  PAGE_READONLY
77 #define __S101  PAGE_READONLY
78 #define __S110  PAGE_SHARED
79 #define __S111  PAGE_SHARED
80
81 extern int num_contexts;
82
83 /* First physical page can be anywhere, the following is needed so that
84  * va-->pa and vice versa conversions work properly without performance
85  * hit for all __pa()/__va() operations.
86  */
87 extern unsigned long phys_base;
88 extern unsigned long pfn_base;
89
90 /*
91  * BAD_PAGETABLE is used when we need a bogus page-table, while
92  * BAD_PAGE is used for a bogus page.
93  *
94  * ZERO_PAGE is a global shared page that is always zero: used
95  * for zero-mapped memory areas etc..
96  */
97 extern pte_t * __bad_pagetable(void);
98 extern pte_t __bad_page(void);
99 extern unsigned long empty_zero_page;
100
101 #define BAD_PAGETABLE __bad_pagetable()
102 #define BAD_PAGE __bad_page()
103 #define ZERO_PAGE(vaddr) (virt_to_page(&empty_zero_page))
104
105 /*
106  * In general all page table modifications should use the V8 atomic
107  * swap instruction.  This insures the mmu and the cpu are in sync
108  * with respect to ref/mod bits in the page tables.
109  */
110 static inline unsigned long srmmu_swap(unsigned long *addr, unsigned long value)
111 {
112         __asm__ __volatile__("swap [%2], %0" : "=&r" (value) : "0" (value), "r" (addr));
113         return value;
114 }
115
116 static inline void srmmu_set_pte(pte_t *ptep, pte_t pteval)
117 {
118         srmmu_swap((unsigned long *)ptep, pte_val(pteval));
119 }
120
121 static inline int srmmu_device_memory(unsigned long x)
122 {
123         return ((x & 0xF0000000) != 0);
124 }
125
126 static inline struct page *pmd_page(pmd_t pmd)
127 {
128         if (srmmu_device_memory(pmd_val(pmd)))
129                 BUG();
130         return pfn_to_page((pmd_val(pmd) & SRMMU_PTD_PMASK) >> (PAGE_SHIFT-4));
131 }
132
133 BTFIXUPDEF_CALL_CONST(unsigned long, pgd_page_vaddr, pgd_t)
134
135 #define pgd_page_vaddr(pgd) BTFIXUP_CALL(pgd_page_vaddr)(pgd)
136
137 BTFIXUPDEF_CALL_CONST(int, pte_present, pte_t)
138
139 static inline int pte_none(pte_t pte)
140 {
141         return !pte_val(pte);
142 }
143
144 #define pte_present(pte) BTFIXUP_CALL(pte_present)(pte)
145
146 static inline void __pte_clear(pte_t *ptep)
147 {
148         srmmu_set_pte(ptep, __pte(0));
149 }
150
151 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
152 {
153         __pte_clear(ptep);
154 }
155
156 static inline int pmd_bad(pmd_t pmd)
157 {
158         return (pmd_val(pmd) & SRMMU_ET_MASK) != SRMMU_ET_PTD;
159 }
160
161 static inline int pmd_present(pmd_t pmd)
162 {
163         return ((pmd_val(pmd) & SRMMU_ET_MASK) == SRMMU_ET_PTD);
164 }
165
166 static inline int pmd_none(pmd_t pmd)
167 {
168         return !pmd_val(pmd);
169 }
170
171 static inline void pmd_clear(pmd_t *pmdp)
172 {
173         int i;
174         for (i = 0; i < PTRS_PER_PTE/SRMMU_REAL_PTRS_PER_PTE; i++)
175                 srmmu_set_pte((pte_t *)&pmdp->pmdv[i], __pte(0));
176 }
177
178 static inline int pgd_none(pgd_t pgd)          
179 {
180         return !(pgd_val(pgd) & 0xFFFFFFF);
181 }
182
183 static inline int pgd_bad(pgd_t pgd)
184 {
185         return (pgd_val(pgd) & SRMMU_ET_MASK) != SRMMU_ET_PTD;
186 }
187
188 static inline int pgd_present(pgd_t pgd)
189 {
190         return ((pgd_val(pgd) & SRMMU_ET_MASK) == SRMMU_ET_PTD);
191 }
192
193 static inline void pgd_clear(pgd_t *pgdp)
194 {
195         srmmu_set_pte((pte_t *)pgdp, __pte(0));
196 }
197
198 /*
199  * The following only work if pte_present() is true.
200  * Undefined behaviour if not..
201  */
202 BTFIXUPDEF_HALF(pte_writei)
203 BTFIXUPDEF_HALF(pte_dirtyi)
204 BTFIXUPDEF_HALF(pte_youngi)
205
206 static int pte_write(pte_t pte) __attribute_const__;
207 static inline int pte_write(pte_t pte)
208 {
209         return pte_val(pte) & BTFIXUP_HALF(pte_writei);
210 }
211
212 static int pte_dirty(pte_t pte) __attribute_const__;
213 static inline int pte_dirty(pte_t pte)
214 {
215         return pte_val(pte) & BTFIXUP_HALF(pte_dirtyi);
216 }
217
218 static int pte_young(pte_t pte) __attribute_const__;
219 static inline int pte_young(pte_t pte)
220 {
221         return pte_val(pte) & BTFIXUP_HALF(pte_youngi);
222 }
223
224 /*
225  * The following only work if pte_present() is not true.
226  */
227 BTFIXUPDEF_HALF(pte_filei)
228
229 static int pte_file(pte_t pte) __attribute_const__;
230 static inline int pte_file(pte_t pte)
231 {
232         return pte_val(pte) & BTFIXUP_HALF(pte_filei);
233 }
234
235 static inline int pte_special(pte_t pte)
236 {
237         return 0;
238 }
239
240 /*
241  */
242 BTFIXUPDEF_HALF(pte_wrprotecti)
243 BTFIXUPDEF_HALF(pte_mkcleani)
244 BTFIXUPDEF_HALF(pte_mkoldi)
245
246 static pte_t pte_wrprotect(pte_t pte) __attribute_const__;
247 static inline pte_t pte_wrprotect(pte_t pte)
248 {
249         return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_wrprotecti));
250 }
251
252 static pte_t pte_mkclean(pte_t pte) __attribute_const__;
253 static inline pte_t pte_mkclean(pte_t pte)
254 {
255         return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_mkcleani));
256 }
257
258 static pte_t pte_mkold(pte_t pte) __attribute_const__;
259 static inline pte_t pte_mkold(pte_t pte)
260 {
261         return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_mkoldi));
262 }
263
264 BTFIXUPDEF_CALL_CONST(pte_t, pte_mkwrite, pte_t)
265 BTFIXUPDEF_CALL_CONST(pte_t, pte_mkdirty, pte_t)
266 BTFIXUPDEF_CALL_CONST(pte_t, pte_mkyoung, pte_t)
267
268 #define pte_mkwrite(pte) BTFIXUP_CALL(pte_mkwrite)(pte)
269 #define pte_mkdirty(pte) BTFIXUP_CALL(pte_mkdirty)(pte)
270 #define pte_mkyoung(pte) BTFIXUP_CALL(pte_mkyoung)(pte)
271
272 #define pte_mkspecial(pte)    (pte)
273
274 #define pfn_pte(pfn, prot)              mk_pte(pfn_to_page(pfn), prot)
275
276 static inline unsigned long pte_pfn(pte_t pte)
277 {
278         if (srmmu_device_memory(pte_val(pte))) {
279                 /* Just return something that will cause
280                  * pfn_valid() to return false.  This makes
281                  * copy_one_pte() to just directly copy to
282                  * PTE over.
283                  */
284                 return ~0UL;
285         }
286         return (pte_val(pte) & SRMMU_PTE_PMASK) >> (PAGE_SHIFT-4);
287 }
288
289 #define pte_page(pte)   pfn_to_page(pte_pfn(pte))
290
291 /*
292  * Conversion functions: convert a page and protection to a page entry,
293  * and a page entry and page directory to the page they refer to.
294  */
295 BTFIXUPDEF_CALL_CONST(pte_t, mk_pte, struct page *, pgprot_t)
296
297 BTFIXUPDEF_CALL_CONST(pte_t, mk_pte_phys, unsigned long, pgprot_t)
298 BTFIXUPDEF_CALL_CONST(pte_t, mk_pte_io, unsigned long, pgprot_t, int)
299
300 #define mk_pte(page,pgprot) BTFIXUP_CALL(mk_pte)(page,pgprot)
301 #define mk_pte_phys(page,pgprot) BTFIXUP_CALL(mk_pte_phys)(page,pgprot)
302 #define mk_pte_io(page,pgprot,space) BTFIXUP_CALL(mk_pte_io)(page,pgprot,space)
303
304 #define pgprot_noncached pgprot_noncached
305 static inline pgprot_t pgprot_noncached(pgprot_t prot)
306 {
307         prot &= ~__pgprot(SRMMU_CACHE);
308         return prot;
309 }
310
311 BTFIXUPDEF_INT(pte_modify_mask)
312
313 static pte_t pte_modify(pte_t pte, pgprot_t newprot) __attribute_const__;
314 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
315 {
316         return __pte((pte_val(pte) & BTFIXUP_INT(pte_modify_mask)) |
317                 pgprot_val(newprot));
318 }
319
320 #define pgd_index(address) ((address) >> PGDIR_SHIFT)
321
322 /* to find an entry in a page-table-directory */
323 #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
324
325 /* to find an entry in a kernel page-table-directory */
326 #define pgd_offset_k(address) pgd_offset(&init_mm, address)
327
328 /* Find an entry in the second-level page table.. */
329 BTFIXUPDEF_CALL(pmd_t *, pmd_offset, pgd_t *, unsigned long)
330 #define pmd_offset(dir,addr) BTFIXUP_CALL(pmd_offset)(dir,addr)
331
332 /* Find an entry in the third-level page table.. */
333 BTFIXUPDEF_CALL(pte_t *, pte_offset_kernel, pmd_t *, unsigned long)
334 #define pte_offset_kernel(dir,addr) BTFIXUP_CALL(pte_offset_kernel)(dir,addr)
335
336 /*
337  * This shortcut works on sun4m (and sun4d) because the nocache area is static.
338  */
339 #define pte_offset_map(d, a)            pte_offset_kernel(d,a)
340 #define pte_unmap(pte)          do{}while(0)
341
342 /* Certain architectures need to do special things when pte's
343  * within a page table are directly modified.  Thus, the following
344  * hook is made available.
345  */
346
347 BTFIXUPDEF_CALL(void, set_pte, pte_t *, pte_t)
348
349 #define set_pte(ptep,pteval) BTFIXUP_CALL(set_pte)(ptep,pteval)
350 #define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
351
352 struct seq_file;
353 BTFIXUPDEF_CALL(void, mmu_info, struct seq_file *)
354
355 #define mmu_info(p) BTFIXUP_CALL(mmu_info)(p)
356
357 /* Fault handler stuff... */
358 #define FAULT_CODE_PROT     0x1
359 #define FAULT_CODE_WRITE    0x2
360 #define FAULT_CODE_USER     0x4
361
362 BTFIXUPDEF_CALL(void, update_mmu_cache, struct vm_area_struct *, unsigned long, pte_t *)
363
364 #define update_mmu_cache(vma,addr,ptep) BTFIXUP_CALL(update_mmu_cache)(vma,addr,ptep)
365
366 BTFIXUPDEF_CALL(void, sparc_mapiorange, unsigned int, unsigned long,
367     unsigned long, unsigned int)
368 BTFIXUPDEF_CALL(void, sparc_unmapiorange, unsigned long, unsigned int)
369 #define sparc_mapiorange(bus,pa,va,len) BTFIXUP_CALL(sparc_mapiorange)(bus,pa,va,len)
370 #define sparc_unmapiorange(va,len) BTFIXUP_CALL(sparc_unmapiorange)(va,len)
371
372 extern int invalid_segment;
373
374 /* Encode and de-code a swap entry */
375 BTFIXUPDEF_CALL(unsigned long, __swp_type, swp_entry_t)
376 BTFIXUPDEF_CALL(unsigned long, __swp_offset, swp_entry_t)
377 BTFIXUPDEF_CALL(swp_entry_t, __swp_entry, unsigned long, unsigned long)
378
379 #define __swp_type(__x)                 BTFIXUP_CALL(__swp_type)(__x)
380 #define __swp_offset(__x)               BTFIXUP_CALL(__swp_offset)(__x)
381 #define __swp_entry(__type,__off)       BTFIXUP_CALL(__swp_entry)(__type,__off)
382
383 #define __pte_to_swp_entry(pte)         ((swp_entry_t) { pte_val(pte) })
384 #define __swp_entry_to_pte(x)           ((pte_t) { (x).val })
385
386 /* file-offset-in-pte helpers */
387 static inline unsigned long pte_to_pgoff(pte_t pte)
388 {
389         return pte_val(pte) >> SRMMU_PTE_FILE_SHIFT;
390 }
391
392 static inline pte_t pgoff_to_pte(unsigned long pgoff)
393 {
394         return __pte((pgoff << SRMMU_PTE_FILE_SHIFT) | SRMMU_FILE);
395 }
396
397 /*
398  * This is made a constant because mm/fremap.c required a constant.
399  */
400 #define PTE_FILE_MAX_BITS 24
401
402 /*
403  */
404 struct ctx_list {
405         struct ctx_list *next;
406         struct ctx_list *prev;
407         unsigned int ctx_number;
408         struct mm_struct *ctx_mm;
409 };
410
411 extern struct ctx_list *ctx_list_pool;  /* Dynamically allocated */
412 extern struct ctx_list ctx_free;        /* Head of free list */
413 extern struct ctx_list ctx_used;        /* Head of used contexts list */
414
415 #define NO_CONTEXT     -1
416
417 static inline void remove_from_ctx_list(struct ctx_list *entry)
418 {
419         entry->next->prev = entry->prev;
420         entry->prev->next = entry->next;
421 }
422
423 static inline void add_to_ctx_list(struct ctx_list *head, struct ctx_list *entry)
424 {
425         entry->next = head;
426         (entry->prev = head->prev)->next = entry;
427         head->prev = entry;
428 }
429 #define add_to_free_ctxlist(entry) add_to_ctx_list(&ctx_free, entry)
430 #define add_to_used_ctxlist(entry) add_to_ctx_list(&ctx_used, entry)
431
432 static inline unsigned long
433 __get_phys (unsigned long addr)
434 {
435         switch (sparc_cpu_model){
436         case sun4m:
437         case sun4d:
438                 return ((srmmu_get_pte (addr) & 0xffffff00) << 4);
439         default:
440                 return 0;
441         }
442 }
443
444 static inline int
445 __get_iospace (unsigned long addr)
446 {
447         switch (sparc_cpu_model){
448         case sun4m:
449         case sun4d:
450                 return (srmmu_get_pte (addr) >> 28);
451         default:
452                 return -1;
453         }
454 }
455
456 extern unsigned long *sparc_valid_addr_bitmap;
457
458 /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
459 #define kern_addr_valid(addr) \
460         (test_bit(__pa((unsigned long)(addr))>>20, sparc_valid_addr_bitmap))
461
462 /*
463  * For sparc32&64, the pfn in io_remap_pfn_range() carries <iospace> in
464  * its high 4 bits.  These macros/functions put it there or get it from there.
465  */
466 #define MK_IOSPACE_PFN(space, pfn)      (pfn | (space << (BITS_PER_LONG - 4)))
467 #define GET_IOSPACE(pfn)                (pfn >> (BITS_PER_LONG - 4))
468 #define GET_PFN(pfn)                    (pfn & 0x0fffffffUL)
469
470 extern int remap_pfn_range(struct vm_area_struct *, unsigned long, unsigned long,
471                            unsigned long, pgprot_t);
472
473 static inline int io_remap_pfn_range(struct vm_area_struct *vma,
474                                      unsigned long from, unsigned long pfn,
475                                      unsigned long size, pgprot_t prot)
476 {
477         unsigned long long offset, space, phys_base;
478
479         offset = ((unsigned long long) GET_PFN(pfn)) << PAGE_SHIFT;
480         space = GET_IOSPACE(pfn);
481         phys_base = offset | (space << 32ULL);
482
483         return remap_pfn_range(vma, from, phys_base >> PAGE_SHIFT, size, prot);
484 }
485
486 #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
487 #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
488 ({                                                                        \
489         int __changed = !pte_same(*(__ptep), __entry);                    \
490         if (__changed) {                                                  \
491                 set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
492                 flush_tlb_page(__vma, __address);                         \
493         }                                                                 \
494         __changed;                                                        \
495 })
496
497 #include <asm-generic/pgtable.h>
498
499 #endif /* !(__ASSEMBLY__) */
500
501 #define VMALLOC_START           _AC(0xfe600000,UL)
502 #define VMALLOC_END             _AC(0xffc00000,UL)
503
504 /* We provide our own get_unmapped_area to cope with VA holes for userland */
505 #define HAVE_ARCH_UNMAPPED_AREA
506
507 /*
508  * No page table caches to initialise
509  */
510 #define pgtable_cache_init()    do { } while (0)
511
512 #endif /* !(_SPARC_PGTABLE_H) */