patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-sparc / pgtable.h
1 /* $Id: pgtable.h,v 1.110 2001/12/21 04:56:17 davem Exp $ */
2 #ifndef _SPARC_PGTABLE_H
3 #define _SPARC_PGTABLE_H
4
5 /*  asm-sparc/pgtable.h:  Defines and functions used to work
6  *                        with Sparc page tables.
7  *
8  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
9  *  Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
10  */
11
12 #include <linux/config.h>
13 #include <linux/spinlock.h>
14 #include <linux/swap.h>
15 #include <asm/types.h>
16 #ifdef CONFIG_SUN4
17 #include <asm/pgtsun4.h>
18 #else
19 #include <asm/pgtsun4c.h>
20 #endif
21 #include <asm/pgtsrmmu.h>
22 #include <asm/vac-ops.h>
23 #include <asm/oplib.h>
24 #include <asm/sbus.h>
25 #include <asm/btfixup.h>
26 #include <asm/system.h>
27
28 #ifndef __ASSEMBLY__
29
30 struct vm_area_struct;
31 struct page;
32
33 extern void load_mmu(void);
34 extern unsigned long calc_highpages(void);
35
36 /* Routines for data transfer buffers. */
37 BTFIXUPDEF_CALL(char *, mmu_lockarea, char *, unsigned long)
38 BTFIXUPDEF_CALL(void,   mmu_unlockarea, char *, unsigned long)
39
40 #define mmu_lockarea(vaddr,len) BTFIXUP_CALL(mmu_lockarea)(vaddr,len)
41 #define mmu_unlockarea(vaddr,len) BTFIXUP_CALL(mmu_unlockarea)(vaddr,len)
42
43 /* These are implementations for sbus_map_sg/sbus_unmap_sg... collapse later */
44 BTFIXUPDEF_CALL(__u32, mmu_get_scsi_one, char *, unsigned long, struct sbus_bus *sbus)
45 BTFIXUPDEF_CALL(void,  mmu_get_scsi_sgl, struct scatterlist *, int, struct sbus_bus *sbus)
46 BTFIXUPDEF_CALL(void,  mmu_release_scsi_one, __u32, unsigned long, struct sbus_bus *sbus)
47 BTFIXUPDEF_CALL(void,  mmu_release_scsi_sgl, struct scatterlist *, int, struct sbus_bus *sbus)
48
49 #define mmu_get_scsi_one(vaddr,len,sbus) BTFIXUP_CALL(mmu_get_scsi_one)(vaddr,len,sbus)
50 #define mmu_get_scsi_sgl(sg,sz,sbus) BTFIXUP_CALL(mmu_get_scsi_sgl)(sg,sz,sbus)
51 #define mmu_release_scsi_one(vaddr,len,sbus) BTFIXUP_CALL(mmu_release_scsi_one)(vaddr,len,sbus)
52 #define mmu_release_scsi_sgl(sg,sz,sbus) BTFIXUP_CALL(mmu_release_scsi_sgl)(sg,sz,sbus)
53
54 /*
55  * mmu_map/unmap are provided by iommu/iounit; Invalid to call on IIep.
56  *
57  * The mmu_map_dma_area establishes two mappings in one go.
58  * These mappings point to pages normally mapped at 'va' (linear address).
59  * First mapping is for CPU visible address at 'a', uncached.
60  * This is an alias, but it works because it is an uncached mapping.
61  * Second mapping is for device visible address, or "bus" address.
62  * The bus address is returned at '*pba'.
63  *
64  * These functions seem distinct, but are hard to split. On sun4c,
65  * at least for now, 'a' is equal to bus address, and retured in *pba.
66  * On sun4m, page attributes depend on the CPU type, so we have to
67  * know if we are mapping RAM or I/O, so it has to be an additional argument
68  * to a separate mapping function for CPU visible mappings.
69  */
70 BTFIXUPDEF_CALL(int,  mmu_map_dma_area, dma_addr_t *, unsigned long, unsigned long, int len)
71 BTFIXUPDEF_CALL(struct page *, mmu_translate_dvma, unsigned long busa)
72 BTFIXUPDEF_CALL(void,  mmu_unmap_dma_area, unsigned long busa, int len)
73
74 #define mmu_map_dma_area(pba,va,a,len) BTFIXUP_CALL(mmu_map_dma_area)(pba,va,a,len)
75 #define mmu_unmap_dma_area(ba,len) BTFIXUP_CALL(mmu_unmap_dma_area)(ba,len)
76 #define mmu_translate_dvma(ba)     BTFIXUP_CALL(mmu_translate_dvma)(ba)
77
78 BTFIXUPDEF_SIMM13(pgdir_shift)
79 BTFIXUPDEF_SETHI(pgdir_size)
80 BTFIXUPDEF_SETHI(pgdir_mask)
81
82 BTFIXUPDEF_SIMM13(ptrs_per_pmd)
83 BTFIXUPDEF_SIMM13(ptrs_per_pgd)
84 BTFIXUPDEF_SIMM13(user_ptrs_per_pgd)
85
86 #define pte_ERROR(e)   __builtin_trap()
87 #define pmd_ERROR(e)   __builtin_trap()
88 #define pgd_ERROR(e)   __builtin_trap()
89
90 BTFIXUPDEF_INT(page_none)
91 BTFIXUPDEF_INT(page_shared)
92 BTFIXUPDEF_INT(page_copy)
93 BTFIXUPDEF_INT(page_readonly)
94 BTFIXUPDEF_INT(page_kernel)
95
96 #define PMD_SHIFT               SUN4C_PMD_SHIFT
97 #define PMD_SIZE                (1UL << PMD_SHIFT)
98 #define PMD_MASK                (~(PMD_SIZE-1))
99 #define PMD_ALIGN(__addr)       (((__addr) + ~PMD_MASK) & PMD_MASK)
100 #define PGDIR_SHIFT             BTFIXUP_SIMM13(pgdir_shift)
101 #define PGDIR_SIZE              BTFIXUP_SETHI(pgdir_size)
102 #define PGDIR_MASK              BTFIXUP_SETHI(pgdir_mask)
103 #define PTRS_PER_PTE            1024
104 #define PTRS_PER_PMD            BTFIXUP_SIMM13(ptrs_per_pmd)
105 #define PTRS_PER_PGD            BTFIXUP_SIMM13(ptrs_per_pgd)
106 #define USER_PTRS_PER_PGD       BTFIXUP_SIMM13(user_ptrs_per_pgd)
107 #define FIRST_USER_PGD_NR       0
108 #define PTE_SIZE                (PTRS_PER_PTE*4)
109
110 #define PAGE_NONE      __pgprot(BTFIXUP_INT(page_none))
111 #define PAGE_SHARED    __pgprot(BTFIXUP_INT(page_shared))
112 #define PAGE_COPY      __pgprot(BTFIXUP_INT(page_copy))
113 #define PAGE_READONLY  __pgprot(BTFIXUP_INT(page_readonly))
114
115 extern unsigned long page_kernel;
116
117 #ifdef MODULE
118 #define PAGE_KERNEL     page_kernel
119 #else
120 #define PAGE_KERNEL    __pgprot(BTFIXUP_INT(page_kernel))
121 #endif
122
123 /* Top-level page directory */
124 extern pgd_t swapper_pg_dir[1024];
125
126 /* Page table for 0-4MB for everybody, on the Sparc this
127  * holds the same as on the i386.
128  */
129 extern pte_t pg0[1024];
130 extern pte_t pg1[1024];
131 extern pte_t pg2[1024];
132 extern pte_t pg3[1024];
133
134 extern unsigned long ptr_in_current_pgd;
135
136 /* Here is a trick, since mmap.c need the initializer elements for
137  * protection_map[] to be constant at compile time, I set the following
138  * to all zeros.  I set it to the real values after I link in the
139  * appropriate MMU page table routines at boot time.
140  */
141 #define __P000  __pgprot(0)
142 #define __P001  __pgprot(0)
143 #define __P010  __pgprot(0)
144 #define __P011  __pgprot(0)
145 #define __P100  __pgprot(0)
146 #define __P101  __pgprot(0)
147 #define __P110  __pgprot(0)
148 #define __P111  __pgprot(0)
149
150 #define __S000  __pgprot(0)
151 #define __S001  __pgprot(0)
152 #define __S010  __pgprot(0)
153 #define __S011  __pgprot(0)
154 #define __S100  __pgprot(0)
155 #define __S101  __pgprot(0)
156 #define __S110  __pgprot(0)
157 #define __S111  __pgprot(0)
158
159 extern int num_contexts;
160
161 /* First physical page can be anywhere, the following is needed so that
162  * va-->pa and vice versa conversions work properly without performance
163  * hit for all __pa()/__va() operations.
164  */
165 extern unsigned long phys_base;
166 extern unsigned long pfn_base;
167
168 /*
169  * BAD_PAGETABLE is used when we need a bogus page-table, while
170  * BAD_PAGE is used for a bogus page.
171  *
172  * ZERO_PAGE is a global shared page that is always zero: used
173  * for zero-mapped memory areas etc..
174  */
175 extern pte_t * __bad_pagetable(void);
176 extern pte_t __bad_page(void);
177 extern unsigned long empty_zero_page;
178
179 #define BAD_PAGETABLE __bad_pagetable()
180 #define BAD_PAGE __bad_page()
181 #define ZERO_PAGE(vaddr) (virt_to_page(&empty_zero_page))
182
183 /*
184  */
185 BTFIXUPDEF_CALL_CONST(struct page *, pmd_page, pmd_t)
186 BTFIXUPDEF_CALL_CONST(unsigned long, pgd_page, pgd_t)
187
188 #define pmd_page(pmd) BTFIXUP_CALL(pmd_page)(pmd)
189 #define pgd_page(pgd) BTFIXUP_CALL(pgd_page)(pgd)
190
191 BTFIXUPDEF_SETHI(none_mask)
192 BTFIXUPDEF_CALL_CONST(int, pte_present, pte_t)
193 BTFIXUPDEF_CALL(void, pte_clear, pte_t *)
194
195 extern __inline__ int pte_none(pte_t pte)
196 {
197         return !(pte_val(pte) & ~BTFIXUP_SETHI(none_mask));
198 }
199
200 #define pte_present(pte) BTFIXUP_CALL(pte_present)(pte)
201 #define pte_clear(pte) BTFIXUP_CALL(pte_clear)(pte)
202
203 BTFIXUPDEF_CALL_CONST(int, pmd_bad, pmd_t)
204 BTFIXUPDEF_CALL_CONST(int, pmd_present, pmd_t)
205 BTFIXUPDEF_CALL(void, pmd_clear, pmd_t *)
206
207 extern __inline__ int pmd_none(pmd_t pmd)
208 {
209         return !(pmd_val(pmd) & ~BTFIXUP_SETHI(none_mask));
210 }
211
212 #define pmd_bad(pmd) BTFIXUP_CALL(pmd_bad)(pmd)
213 #define pmd_present(pmd) BTFIXUP_CALL(pmd_present)(pmd)
214 #define pmd_clear(pmd) BTFIXUP_CALL(pmd_clear)(pmd)
215
216 BTFIXUPDEF_CALL_CONST(int, pgd_none, pgd_t)
217 BTFIXUPDEF_CALL_CONST(int, pgd_bad, pgd_t)
218 BTFIXUPDEF_CALL_CONST(int, pgd_present, pgd_t)
219 BTFIXUPDEF_CALL(void, pgd_clear, pgd_t *)
220
221 #define pgd_none(pgd) BTFIXUP_CALL(pgd_none)(pgd)
222 #define pgd_bad(pgd) BTFIXUP_CALL(pgd_bad)(pgd)
223 #define pgd_present(pgd) BTFIXUP_CALL(pgd_present)(pgd)
224 #define pgd_clear(pgd) BTFIXUP_CALL(pgd_clear)(pgd)
225
226 /*
227  * The following only work if pte_present() is true.
228  * Undefined behaviour if not..
229  */
230 BTFIXUPDEF_HALF(pte_writei)
231 BTFIXUPDEF_HALF(pte_dirtyi)
232 BTFIXUPDEF_HALF(pte_youngi)
233
234 extern int pte_write(pte_t pte) __attribute_const__;
235 extern __inline__ int pte_write(pte_t pte)
236 {
237         return pte_val(pte) & BTFIXUP_HALF(pte_writei);
238 }
239
240 extern int pte_dirty(pte_t pte) __attribute_const__;
241 extern __inline__ int pte_dirty(pte_t pte)
242 {
243         return pte_val(pte) & BTFIXUP_HALF(pte_dirtyi);
244 }
245
246 extern int pte_young(pte_t pte) __attribute_const__;
247 extern __inline__ int pte_young(pte_t pte)
248 {
249         return pte_val(pte) & BTFIXUP_HALF(pte_youngi);
250 }
251
252 /*
253  * The following only work if pte_present() is not true.
254  */
255 BTFIXUPDEF_HALF(pte_filei)
256
257 extern int pte_file(pte_t pte) __attribute_const__;
258 extern __inline__ int pte_file(pte_t pte)
259 {
260         return pte_val(pte) & BTFIXUP_HALF(pte_filei);
261 }
262
263 /*
264  */
265 BTFIXUPDEF_HALF(pte_wrprotecti)
266 BTFIXUPDEF_HALF(pte_mkcleani)
267 BTFIXUPDEF_HALF(pte_mkoldi)
268
269 extern pte_t pte_wrprotect(pte_t pte) __attribute_const__;
270 extern __inline__ pte_t pte_wrprotect(pte_t pte)
271 {
272         return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_wrprotecti));
273 }
274
275 extern pte_t pte_mkclean(pte_t pte) __attribute_const__;
276 extern __inline__ pte_t pte_mkclean(pte_t pte)
277 {
278         return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_mkcleani));
279 }
280
281 extern pte_t pte_mkold(pte_t pte) __attribute_const__;
282 extern __inline__ pte_t pte_mkold(pte_t pte)
283 {
284         return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_mkoldi));
285 }
286
287 BTFIXUPDEF_CALL_CONST(pte_t, pte_mkwrite, pte_t)
288 BTFIXUPDEF_CALL_CONST(pte_t, pte_mkdirty, pte_t)
289 BTFIXUPDEF_CALL_CONST(pte_t, pte_mkyoung, pte_t)
290
291 #define pte_mkwrite(pte) BTFIXUP_CALL(pte_mkwrite)(pte)
292 #define pte_mkdirty(pte) BTFIXUP_CALL(pte_mkdirty)(pte)
293 #define pte_mkyoung(pte) BTFIXUP_CALL(pte_mkyoung)(pte)
294
295 #define page_pte_prot(page, prot)       mk_pte(page, prot)
296 #define page_pte(page)                  mk_pte(page, __pgprot(0))
297 #define pfn_pte(pfn, prot)              mk_pte(pfn_to_page(pfn), prot)
298
299 BTFIXUPDEF_CALL(unsigned long,   pte_pfn, pte_t)
300 #define pte_pfn(pte) BTFIXUP_CALL(pte_pfn)(pte)
301 #define pte_page(pte)   pfn_to_page(pte_pfn(pte))
302
303 /*
304  * Conversion functions: convert a page and protection to a page entry,
305  * and a page entry and page directory to the page they refer to.
306  */
307 BTFIXUPDEF_CALL_CONST(pte_t, mk_pte, struct page *, pgprot_t)
308
309 BTFIXUPDEF_CALL_CONST(pte_t, mk_pte_phys, unsigned long, pgprot_t)
310 BTFIXUPDEF_CALL_CONST(pte_t, mk_pte_io, unsigned long, pgprot_t, int)
311
312 #define mk_pte(page,pgprot) BTFIXUP_CALL(mk_pte)(page,pgprot)
313 #define mk_pte_phys(page,pgprot) BTFIXUP_CALL(mk_pte_phys)(page,pgprot)
314 #define mk_pte_io(page,pgprot,space) BTFIXUP_CALL(mk_pte_io)(page,pgprot,space)
315
316 BTFIXUPDEF_INT(pte_modify_mask)
317
318 extern pte_t pte_modify(pte_t pte, pgprot_t newprot) __attribute_const__;
319 extern __inline__ pte_t pte_modify(pte_t pte, pgprot_t newprot)
320 {
321         return __pte((pte_val(pte) & BTFIXUP_INT(pte_modify_mask)) |
322                 pgprot_val(newprot));
323 }
324
325 #define pgd_index(address) ((address) >> PGDIR_SHIFT)
326
327 /* to find an entry in a page-table-directory */
328 #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
329
330 /* to find an entry in a kernel page-table-directory */
331 #define pgd_offset_k(address) pgd_offset(&init_mm, address)
332
333 /* Find an entry in the second-level page table.. */
334 BTFIXUPDEF_CALL(pmd_t *, pmd_offset, pgd_t *, unsigned long)
335 #define pmd_offset(dir,addr) BTFIXUP_CALL(pmd_offset)(dir,addr)
336
337 /* Find an entry in the third-level page table.. */ 
338 BTFIXUPDEF_CALL(pte_t *, pte_offset_kernel, pmd_t *, unsigned long)
339 #define pte_offset_kernel(dir,addr) BTFIXUP_CALL(pte_offset_kernel)(dir,addr)
340
341 /*
342  * This shortcut works on sun4m (and sun4d) because the nocache area is static,
343  * and sun4c is guaranteed to have no highmem anyway.
344  */
345 #define pte_offset_map(d, a)            pte_offset_kernel(d,a)
346 #define pte_offset_map_nested(d, a)     pte_offset_kernel(d,a)
347
348 #define pte_unmap(pte)          do{}while(0)
349 #define pte_unmap_nested(pte)   do{}while(0)
350
351 /* The permissions for pgprot_val to make a page mapped on the obio space */
352 extern unsigned int pg_iobits;
353
354 /* Certain architectures need to do special things when pte's
355  * within a page table are directly modified.  Thus, the following
356  * hook is made available.
357  */
358
359 BTFIXUPDEF_CALL(void, set_pte, pte_t *, pte_t)
360
361 #define set_pte(ptep,pteval) BTFIXUP_CALL(set_pte)(ptep,pteval)
362
363 struct seq_file;
364 BTFIXUPDEF_CALL(void, mmu_info, struct seq_file *)
365
366 #define mmu_info(p) BTFIXUP_CALL(mmu_info)(p)
367
368 /* Fault handler stuff... */
369 #define FAULT_CODE_PROT     0x1
370 #define FAULT_CODE_WRITE    0x2
371 #define FAULT_CODE_USER     0x4
372
373 BTFIXUPDEF_CALL(void, update_mmu_cache, struct vm_area_struct *, unsigned long, pte_t)
374
375 #define update_mmu_cache(vma,addr,pte) BTFIXUP_CALL(update_mmu_cache)(vma,addr,pte)
376
377 BTFIXUPDEF_CALL(void, sparc_mapiorange, unsigned int, unsigned long,
378     unsigned long, unsigned int)
379 BTFIXUPDEF_CALL(void, sparc_unmapiorange, unsigned long, unsigned int)
380 #define sparc_mapiorange(bus,pa,va,len) BTFIXUP_CALL(sparc_mapiorange)(bus,pa,va,len)
381 #define sparc_unmapiorange(va,len) BTFIXUP_CALL(sparc_unmapiorange)(va,len)
382
383 extern int invalid_segment;
384
385 /* Encode and de-code a swap entry */
386 BTFIXUPDEF_CALL(unsigned long, __swp_type, swp_entry_t)
387 BTFIXUPDEF_CALL(unsigned long, __swp_offset, swp_entry_t)
388 BTFIXUPDEF_CALL(swp_entry_t, __swp_entry, unsigned long, unsigned long)
389
390 #define __swp_type(__x)                 BTFIXUP_CALL(__swp_type)(__x)
391 #define __swp_offset(__x)               BTFIXUP_CALL(__swp_offset)(__x)
392 #define __swp_entry(__type,__off)       BTFIXUP_CALL(__swp_entry)(__type,__off)
393
394 #define __pte_to_swp_entry(pte)         ((swp_entry_t) { pte_val(pte) })
395 #define __swp_entry_to_pte(x)           ((pte_t) { (x).val })
396
397 /* file-offset-in-pte helpers */
398 BTFIXUPDEF_CALL(unsigned long, pte_to_pgoff, pte_t pte);
399 BTFIXUPDEF_CALL(pte_t, pgoff_to_pte, unsigned long pgoff);
400
401 #define pte_to_pgoff(pte) BTFIXUP_CALL(pte_to_pgoff)(pte)
402 #define pgoff_to_pte(off) BTFIXUP_CALL(pgoff_to_pte)(off)
403
404 /*
405  * This is made a constant because mm/fremap.c required a constant.
406  * Note that layout of these bits is different between sun4c.c and srmmu.c.
407  */
408 #define PTE_FILE_MAX_BITS 24
409
410 /*
411  */
412 struct ctx_list {
413         struct ctx_list *next;
414         struct ctx_list *prev;
415         unsigned int ctx_number;
416         struct mm_struct *ctx_mm;
417 };
418
419 extern struct ctx_list *ctx_list_pool;  /* Dynamically allocated */
420 extern struct ctx_list ctx_free;        /* Head of free list */
421 extern struct ctx_list ctx_used;        /* Head of used contexts list */
422
423 #define NO_CONTEXT     -1
424
425 extern __inline__ void remove_from_ctx_list(struct ctx_list *entry)
426 {
427         entry->next->prev = entry->prev;
428         entry->prev->next = entry->next;
429 }
430
431 extern __inline__ void add_to_ctx_list(struct ctx_list *head, struct ctx_list *entry)
432 {
433         entry->next = head;
434         (entry->prev = head->prev)->next = entry;
435         head->prev = entry;
436 }
437 #define add_to_free_ctxlist(entry) add_to_ctx_list(&ctx_free, entry)
438 #define add_to_used_ctxlist(entry) add_to_ctx_list(&ctx_used, entry)
439
440 extern __inline__ unsigned long
441 __get_phys (unsigned long addr)
442 {
443         switch (sparc_cpu_model){
444         case sun4:
445         case sun4c:
446                 return sun4c_get_pte (addr) << PAGE_SHIFT;
447         case sun4m:
448         case sun4d:
449                 return ((srmmu_get_pte (addr) & 0xffffff00) << 4);
450         default:
451                 return 0;
452         }
453 }
454
455 extern __inline__ int
456 __get_iospace (unsigned long addr)
457 {
458         switch (sparc_cpu_model){
459         case sun4:
460         case sun4c:
461                 return -1; /* Don't check iospace on sun4c */
462         case sun4m:
463         case sun4d:
464                 return (srmmu_get_pte (addr) >> 28);
465         default:
466                 return -1;
467         }
468 }
469
470 extern unsigned long *sparc_valid_addr_bitmap;
471
472 /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
473 #define kern_addr_valid(addr) \
474         (test_bit(__pa((unsigned long)(addr))>>20, sparc_valid_addr_bitmap))
475
476 extern int io_remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long to,
477                                unsigned long size, pgprot_t prot, int space);
478
479 #include <asm-generic/pgtable.h>
480
481 #endif /* !(__ASSEMBLY__) */
482
483 /* We provide our own get_unmapped_area to cope with VA holes for userland */
484 #define HAVE_ARCH_UNMAPPED_AREA
485
486 /*
487  * No page table caches to initialise
488  */
489 #define pgtable_cache_init()    do { } while (0)
490
491 #endif /* !(_SPARC_PGTABLE_H) */