more changes on original files
[linux-2.4.git] / arch / mips / mm / ioremap.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * (C) Copyright 1995 1996 Linus Torvalds
7  * (C) Copyright 2001 Ralf Baechle
8  */
9 #include <linux/module.h>
10 #include <asm/addrspace.h>
11 #include <asm/byteorder.h>
12
13 #include <linux/vmalloc.h>
14 #include <asm/io.h>
15 #include <asm/pgalloc.h>
16
17 static inline void remap_area_pte(pte_t * pte, unsigned long address,
18         phys_t size, phys_t phys_addr, unsigned long flags)
19 {
20         phys_t end;
21         pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
22                                    | __WRITEABLE | flags);
23
24         address &= ~PMD_MASK;
25         end = address + size;
26         if (end > PMD_SIZE)
27                 end = PMD_SIZE;
28         if (address >= end)
29                 BUG();
30         do {
31                 if (!pte_none(*pte)) {
32                         printk("remap_area_pte: page already exists\n");
33                         BUG();
34                 }
35                 set_pte(pte, mk_pte_phys(phys_addr, pgprot));
36                 address += PAGE_SIZE;
37                 phys_addr += PAGE_SIZE;
38                 pte++;
39         } while (address && (address < end));
40 }
41
42 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
43         phys_t size, phys_t phys_addr, unsigned long flags)
44 {
45         phys_t end;
46
47         address &= ~PGDIR_MASK;
48         end = address + size;
49         if (end > PGDIR_SIZE)
50                 end = PGDIR_SIZE;
51         phys_addr -= address;
52         if (address >= end)
53                 BUG();
54         do {
55                 pte_t * pte = pte_alloc(&init_mm, pmd, address);
56                 if (!pte)
57                         return -ENOMEM;
58                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
59                 address = (address + PMD_SIZE) & PMD_MASK;
60                 pmd++;
61         } while (address && (address < end));
62         return 0;
63 }
64
65 static int remap_area_pages(unsigned long address, phys_t phys_addr,
66         phys_t size, unsigned long flags)
67 {
68         int error;
69         pgd_t * dir;
70         unsigned long end = address + size;
71
72         phys_addr -= address;
73         dir = pgd_offset(&init_mm, address);
74         flush_cache_all();
75         if (address >= end)
76                 BUG();
77         spin_lock(&init_mm.page_table_lock);
78         do {
79                 pmd_t *pmd;
80                 pmd = pmd_alloc(&init_mm, dir, address);
81                 error = -ENOMEM;
82                 if (!pmd)
83                         break;
84                 if (remap_area_pmd(pmd, address, end - address,
85                                          phys_addr + address, flags))
86                         break;
87                 error = 0;
88                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
89                 dir++;
90         } while (address && (address < end));
91         spin_unlock(&init_mm.page_table_lock);
92         flush_tlb_all();
93         return error;
94 }
95
96 /*
97  * Allow physical addresses to be fixed up to help 36 bit 
98  * peripherals.
99  */
100 static phys_t def_fixup_bigphys_addr(phys_t phys_addr, phys_t size)
101 {
102         return phys_addr;
103 }
104
105 phys_t (*fixup_bigphys_addr)(phys_t phys_addr, phys_t size) = def_fixup_bigphys_addr;
106
107 /*
108  * Generic mapping function (not visible outside):
109  */
110
111 /*
112  * Remap an arbitrary physical address space into the kernel virtual
113  * address space. Needed when the kernel wants to access high addresses
114  * directly.
115  *
116  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
117  * have to convert them into an offset in a page-aligned mapping, but the
118  * caller shouldn't need to know that small detail.
119  */
120
121 #define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
122
123 void * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
124 {
125         struct vm_struct * area;
126         unsigned long offset;
127         phys_t last_addr;
128         void * addr;
129
130         phys_addr = fixup_bigphys_addr(phys_addr, size);
131
132         /* Don't allow wraparound or zero size */
133         last_addr = phys_addr + size - 1;
134         if (!size || last_addr < phys_addr)
135                 return NULL;
136
137         /*
138          * Map uncached objects in the low 512mb of address space using KSEG1,
139          * otherwise map using page tables.
140          */
141         if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
142             flags == _CACHE_UNCACHED)
143                 return (void *) KSEG1ADDR(phys_addr);
144
145         /*
146          * Don't allow anybody to remap normal RAM that we're using..
147          */
148         if (phys_addr < virt_to_phys(high_memory)) {
149                 char *t_addr, *t_end;
150                 struct page *page;
151
152                 t_addr = __va(phys_addr);
153                 t_end = t_addr + (size - 1);
154
155                 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
156                         if(!PageReserved(page))
157                                 return NULL;
158         }
159
160         /*
161          * Mappings have to be page-aligned
162          */
163         offset = phys_addr & ~PAGE_MASK;
164         phys_addr &= PAGE_MASK;
165         size = PAGE_ALIGN(last_addr + 1) - phys_addr;
166
167         /*
168          * Ok, go for it..
169          */
170         area = get_vm_area(size, VM_IOREMAP);
171         if (!area)
172                 return NULL;
173         addr = area->addr;
174         if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
175                 vfree(addr);
176                 return NULL;
177         }
178
179         return (void *) (offset + (char *)addr);
180 }
181
182 #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
183
184 void iounmap(void *addr)
185 {
186         if (!IS_KSEG1(addr))
187                 return vfree((void *) (PAGE_MASK & (unsigned long) addr));
188 }
189
190 EXPORT_SYMBOL(__ioremap);
191 EXPORT_SYMBOL(iounmap);