Merge branch 'upstream'
[linux-2.6] / 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 static inline pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space)
19 {
20         pte_t pte;
21         pte_val(pte) = (((page) | pgprot_val(prot) | _PAGE_E) &
22                         ~(unsigned long)_PAGE_CACHE);
23         pte_val(pte) |= (((unsigned long)space) << 32);
24         return pte;
25 }
26
27 /* Remap IO memory, the same way as remap_pfn_range(), but use
28  * the obio memory space.
29  *
30  * They use a pgprot that sets PAGE_IO and does not check the
31  * mem_map table as this is independent of normal memory.
32  */
33 static inline void io_remap_pte_range(struct mm_struct *mm, pte_t * pte,
34                                       unsigned long address,
35                                       unsigned long size,
36                                       unsigned long offset, pgprot_t prot,
37                                       int space)
38 {
39         unsigned long end;
40
41         /* clear hack bit that was used as a write_combine side-effect flag */
42         offset &= ~0x1UL;
43         address &= ~PMD_MASK;
44         end = address + size;
45         if (end > PMD_SIZE)
46                 end = PMD_SIZE;
47         do {
48                 pte_t entry;
49                 unsigned long curend = address + PAGE_SIZE;
50                 
51                 entry = mk_pte_io(offset, prot, space);
52                 if (!(address & 0xffff)) {
53                         if (!(address & 0x3fffff) && !(offset & 0x3ffffe) && end >= address + 0x400000) {
54                                 entry = mk_pte_io(offset,
55                                                   __pgprot(pgprot_val (prot) | _PAGE_SZ4MB),
56                                                   space);
57                                 curend = address + 0x400000;
58                                 offset += 0x400000;
59                         } else if (!(address & 0x7ffff) && !(offset & 0x7fffe) && end >= address + 0x80000) {
60                                 entry = mk_pte_io(offset,
61                                                   __pgprot(pgprot_val (prot) | _PAGE_SZ512K),
62                                                   space);
63                                 curend = address + 0x80000;
64                                 offset += 0x80000;
65                         } else if (!(offset & 0xfffe) && end >= address + 0x10000) {
66                                 entry = mk_pte_io(offset,
67                                                   __pgprot(pgprot_val (prot) | _PAGE_SZ64K),
68                                                   space);
69                                 curend = address + 0x10000;
70                                 offset += 0x10000;
71                         } else
72                                 offset += PAGE_SIZE;
73                 } else
74                         offset += PAGE_SIZE;
75
76                 do {
77                         BUG_ON(!pte_none(*pte));
78                         set_pte_at(mm, address, pte, entry);
79                         address += PAGE_SIZE;
80                         pte++;
81                 } while (address < curend);
82         } while (address < end);
83 }
84
85 static inline int io_remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
86         unsigned long offset, pgprot_t prot, int space)
87 {
88         unsigned long end;
89
90         address &= ~PGDIR_MASK;
91         end = address + size;
92         if (end > PGDIR_SIZE)
93                 end = PGDIR_SIZE;
94         offset -= address;
95         do {
96                 pte_t * pte = pte_alloc_map(mm, pmd, address);
97                 if (!pte)
98                         return -ENOMEM;
99                 io_remap_pte_range(mm, pte, address, end - address, address + offset, prot, space);
100                 pte_unmap(pte);
101                 address = (address + PMD_SIZE) & PMD_MASK;
102                 pmd++;
103         } while (address < end);
104         return 0;
105 }
106
107 static inline int io_remap_pud_range(struct mm_struct *mm, pud_t * pud, unsigned long address, unsigned long size,
108         unsigned long offset, pgprot_t prot, int space)
109 {
110         unsigned long end;
111
112         address &= ~PUD_MASK;
113         end = address + size;
114         if (end > PUD_SIZE)
115                 end = PUD_SIZE;
116         offset -= address;
117         do {
118                 pmd_t *pmd = pmd_alloc(mm, pud, address);
119                 if (!pud)
120                         return -ENOMEM;
121                 io_remap_pmd_range(mm, pmd, address, end - address, address + offset, prot, space);
122                 address = (address + PUD_SIZE) & PUD_MASK;
123                 pud++;
124         } while (address < end);
125         return 0;
126 }
127
128 int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
129                 unsigned long pfn, unsigned long size, pgprot_t prot)
130 {
131         int error = 0;
132         pgd_t * dir;
133         unsigned long beg = from;
134         unsigned long end = from + size;
135         struct mm_struct *mm = vma->vm_mm;
136         int space = GET_IOSPACE(pfn);
137         unsigned long offset = GET_PFN(pfn) << PAGE_SHIFT;
138         unsigned long phys_base;
139
140         phys_base = offset | (((unsigned long) space) << 32UL);
141
142         /* See comment in mm/memory.c remap_pfn_range */
143         vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
144         vma->vm_pgoff = phys_base >> PAGE_SHIFT;
145
146         prot = __pgprot(pg_iobits);
147         offset -= from;
148         dir = pgd_offset(mm, from);
149         flush_cache_range(vma, beg, end);
150
151         while (from < end) {
152                 pud_t *pud = pud_alloc(mm, dir, from);
153                 error = -ENOMEM;
154                 if (!pud)
155                         break;
156                 error = io_remap_pud_range(mm, pud, from, end - from, offset + from, prot, space);
157                 if (error)
158                         break;
159                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
160                 dir++;
161         }
162
163         flush_tlb_range(vma, beg, end);
164         return error;
165 }