vserver 1.9.5.x5
[linux-2.6.git] / arch / sparc64 / mm / generic.c
1 /* $Id: generic.c,v 1.18 2001/12/21 04:56:15 davem Exp $
2  * generic.c: Generic Sparc mm routines that are not dependent upon
3  *            MMU type but are Sparc specific.
4  *
5  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/swap.h>
11 #include <linux/pagemap.h>
12
13 #include <asm/pgalloc.h>
14 #include <asm/pgtable.h>
15 #include <asm/page.h>
16 #include <asm/tlbflush.h>
17
18 /* Remap IO memory, the same way as remap_pfn_range(), but use
19  * the obio memory space.
20  *
21  * They use a pgprot that sets PAGE_IO and does not check the
22  * mem_map table as this is independent of normal memory.
23  *
24  * As a special hack if the lowest bit of offset is set the
25  * side-effect bit will be turned off.  This is used as a
26  * performance improvement on FFB/AFB. -DaveM
27  */
28 static inline void io_remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
29         unsigned long offset, pgprot_t prot, int space)
30 {
31         unsigned long end;
32
33         address &= ~PMD_MASK;
34         end = address + size;
35         if (end > PMD_SIZE)
36                 end = PMD_SIZE;
37         do {
38                 pte_t entry;
39                 unsigned long curend = address + PAGE_SIZE;
40                 
41                 entry = mk_pte_io((offset & ~(0x1UL)), prot, space);
42                 if (!(address & 0xffff)) {
43                         if (!(address & 0x3fffff) && !(offset & 0x3ffffe) && end >= address + 0x400000) {
44                                 entry = mk_pte_io((offset & ~(0x1UL)),
45                                                   __pgprot(pgprot_val (prot) | _PAGE_SZ4MB),
46                                                   space);
47                                 curend = address + 0x400000;
48                                 offset += 0x400000;
49                         } else if (!(address & 0x7ffff) && !(offset & 0x7fffe) && end >= address + 0x80000) {
50                                 entry = mk_pte_io((offset & ~(0x1UL)),
51                                                   __pgprot(pgprot_val (prot) | _PAGE_SZ512K),
52                                                   space);
53                                 curend = address + 0x80000;
54                                 offset += 0x80000;
55                         } else if (!(offset & 0xfffe) && end >= address + 0x10000) {
56                                 entry = mk_pte_io((offset & ~(0x1UL)),
57                                                   __pgprot(pgprot_val (prot) | _PAGE_SZ64K),
58                                                   space);
59                                 curend = address + 0x10000;
60                                 offset += 0x10000;
61                         } else
62                                 offset += PAGE_SIZE;
63                 } else
64                         offset += PAGE_SIZE;
65
66                 if (offset & 0x1UL)
67                         pte_val(entry) &= ~(_PAGE_E);
68                 do {
69                         BUG_ON(!pte_none(*pte));
70                         set_pte(pte, entry);
71                         address += PAGE_SIZE;
72                         pte++;
73                 } while (address < curend);
74         } while (address < end);
75 }
76
77 static inline int io_remap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size,
78         unsigned long offset, pgprot_t prot, int space)
79 {
80         unsigned long end;
81
82         address &= ~PGDIR_MASK;
83         end = address + size;
84         if (end > PGDIR_SIZE)
85                 end = PGDIR_SIZE;
86         offset -= address;
87         do {
88                 pte_t * pte = pte_alloc_map(current->mm, pmd, address);
89                 if (!pte)
90                         return -ENOMEM;
91                 io_remap_pte_range(pte, address, end - address, address + offset, prot, space);
92                 pte_unmap(pte);
93                 address = (address + PMD_SIZE) & PMD_MASK;
94                 pmd++;
95         } while (address < end);
96         return 0;
97 }
98
99 static inline int io_remap_pud_range(pud_t * pud, unsigned long address, unsigned long size,
100         unsigned long offset, pgprot_t prot, int space)
101 {
102         unsigned long end;
103
104         address &= ~PUD_MASK;
105         end = address + size;
106         if (end > PUD_SIZE)
107                 end = PUD_SIZE;
108         offset -= address;
109         do {
110                 pmd_t *pmd = pmd_alloc(current->mm, pud, address);
111                 if (!pud)
112                         return -ENOMEM;
113                 io_remap_pmd_range(pmd, address, end - address, address + offset, prot, space);
114                 address = (address + PUD_SIZE) & PUD_MASK;
115                 pud++;
116         } while (address < end);
117         return 0;
118 }
119
120 int io_remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long offset, unsigned long size, pgprot_t prot, int space)
121 {
122         int error = 0;
123         pgd_t * dir;
124         unsigned long beg = from;
125         unsigned long end = from + size;
126         struct mm_struct *mm = vma->vm_mm;
127
128         prot = __pgprot(pg_iobits);
129         offset -= from;
130         dir = pgd_offset(mm, from);
131         flush_cache_range(vma, beg, end);
132
133         spin_lock(&mm->page_table_lock);
134         while (from < end) {
135                 pud_t *pud = pud_alloc(current->mm, dir, from);
136                 error = -ENOMEM;
137                 if (!pud)
138                         break;
139                 error = io_remap_pud_range(pud, from, end - from, offset + from, prot, space);
140                 if (error)
141                         break;
142                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
143                 dir++;
144         }
145         flush_tlb_range(vma, beg, end);
146         spin_unlock(&mm->page_table_lock);
147
148         return error;
149 }