blob: f379b8d675585eac6e6884887fc1a1a9b5230624 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/i386/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 * This is needed for high PCI addresses that aren't mapped in the
6 * 640k-1MB IO memory area on PC's
7 *
8 * (C) Copyright 1995 1996 Linus Torvalds
9 */
10
11#include <linux/vmalloc.h>
12#include <linux/init.h>
13#include <linux/slab.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/io.h>
16#include <asm/fixmap.h>
17#include <asm/cacheflush.h>
18#include <asm/tlbflush.h>
19#include <asm/pgtable.h>
20
21#define ISA_START_ADDRESS 0xa0000
22#define ISA_END_ADDRESS 0x100000
23
24static int ioremap_pte_range(pmd_t *pmd, unsigned long addr,
25 unsigned long end, unsigned long phys_addr, unsigned long flags)
26{
27 pte_t *pte;
28 unsigned long pfn;
29
30 pfn = phys_addr >> PAGE_SHIFT;
31 pte = pte_alloc_kernel(&init_mm, pmd, addr);
32 if (!pte)
33 return -ENOMEM;
34 do {
35 BUG_ON(!pte_none(*pte));
36 set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW |
37 _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
38 pfn++;
39 } while (pte++, addr += PAGE_SIZE, addr != end);
40 return 0;
41}
42
43static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr,
44 unsigned long end, unsigned long phys_addr, unsigned long flags)
45{
46 pmd_t *pmd;
47 unsigned long next;
48
49 phys_addr -= addr;
50 pmd = pmd_alloc(&init_mm, pud, addr);
51 if (!pmd)
52 return -ENOMEM;
53 do {
54 next = pmd_addr_end(addr, end);
55 if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, flags))
56 return -ENOMEM;
57 } while (pmd++, addr = next, addr != end);
58 return 0;
59}
60
61static inline int ioremap_pud_range(pgd_t *pgd, unsigned long addr,
62 unsigned long end, unsigned long phys_addr, unsigned long flags)
63{
64 pud_t *pud;
65 unsigned long next;
66
67 phys_addr -= addr;
68 pud = pud_alloc(&init_mm, pgd, addr);
69 if (!pud)
70 return -ENOMEM;
71 do {
72 next = pud_addr_end(addr, end);
73 if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, flags))
74 return -ENOMEM;
75 } while (pud++, addr = next, addr != end);
76 return 0;
77}
78
79static int ioremap_page_range(unsigned long addr,
80 unsigned long end, unsigned long phys_addr, unsigned long flags)
81{
82 pgd_t *pgd;
83 unsigned long next;
84 int err;
85
86 BUG_ON(addr >= end);
87 flush_cache_all();
88 phys_addr -= addr;
89 pgd = pgd_offset_k(addr);
90 spin_lock(&init_mm.page_table_lock);
91 do {
92 next = pgd_addr_end(addr, end);
93 err = ioremap_pud_range(pgd, addr, next, phys_addr+addr, flags);
94 if (err)
95 break;
96 } while (pgd++, addr = next, addr != end);
97 spin_unlock(&init_mm.page_table_lock);
98 flush_tlb_all();
99 return err;
100}
101
102/*
103 * Generic mapping function (not visible outside):
104 */
105
106/*
107 * Remap an arbitrary physical address space into the kernel virtual
108 * address space. Needed when the kernel wants to access high addresses
109 * directly.
110 *
111 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
112 * have to convert them into an offset in a page-aligned mapping, but the
113 * caller shouldn't need to know that small detail.
114 */
115void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
116{
117 void __iomem * addr;
118 struct vm_struct * area;
119 unsigned long offset, last_addr;
120
121 /* Don't allow wraparound or zero size */
122 last_addr = phys_addr + size - 1;
123 if (!size || last_addr < phys_addr)
124 return NULL;
125
126 /*
127 * Don't remap the low PCI/ISA area, it's always mapped..
128 */
129 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
130 return (void __iomem *) phys_to_virt(phys_addr);
131
132 /*
133 * Don't allow anybody to remap normal RAM that we're using..
134 */
135 if (phys_addr <= virt_to_phys(high_memory - 1)) {
136 char *t_addr, *t_end;
137 struct page *page;
138
139 t_addr = __va(phys_addr);
140 t_end = t_addr + (size - 1);
141
142 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
143 if(!PageReserved(page))
144 return NULL;
145 }
146
147 /*
148 * Mappings have to be page-aligned
149 */
150 offset = phys_addr & ~PAGE_MASK;
151 phys_addr &= PAGE_MASK;
152 size = PAGE_ALIGN(last_addr+1) - phys_addr;
153
154 /*
155 * Ok, go for it..
156 */
157 area = get_vm_area(size, VM_IOREMAP | (flags << 20));
158 if (!area)
159 return NULL;
160 area->phys_addr = phys_addr;
161 addr = (void __iomem *) area->addr;
162 if (ioremap_page_range((unsigned long) addr,
163 (unsigned long) addr + size, phys_addr, flags)) {
164 vunmap((void __force *) addr);
165 return NULL;
166 }
167 return (void __iomem *) (offset + (char __iomem *)addr);
168}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700169EXPORT_SYMBOL(__ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/**
172 * ioremap_nocache - map bus memory into CPU space
173 * @offset: bus address of the memory
174 * @size: size of the resource to map
175 *
176 * ioremap_nocache performs a platform specific sequence of operations to
177 * make bus memory CPU accessible via the readb/readw/readl/writeb/
178 * writew/writel functions and the other mmio helpers. The returned
179 * address is not guaranteed to be usable directly as a virtual
180 * address.
181 *
182 * This version of ioremap ensures that the memory is marked uncachable
183 * on the CPU as well as honouring existing caching rules from things like
184 * the PCI bus. Note that there are other caches and buffers on many
185 * busses. In particular driver authors should read up on PCI writes
186 *
187 * It's useful if some control registers are in such an area and
188 * write combining or read caching is not desirable:
189 *
190 * Must be freed with iounmap.
191 */
192
193void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
194{
195 unsigned long last_addr;
196 void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
197 if (!p)
198 return p;
199
200 /* Guaranteed to be > phys_addr, as per __ioremap() */
201 last_addr = phys_addr + size - 1;
202
203 if (last_addr < virt_to_phys(high_memory) - 1) {
204 struct page *ppage = virt_to_page(__va(phys_addr));
205 unsigned long npages;
206
207 phys_addr &= PAGE_MASK;
208
209 /* This might overflow and become zero.. */
210 last_addr = PAGE_ALIGN(last_addr);
211
212 /* .. but that's ok, because modulo-2**n arithmetic will make
213 * the page-aligned "last - first" come out right.
214 */
215 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
216
217 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
218 iounmap(p);
219 p = NULL;
220 }
221 global_flush_tlb();
222 }
223
224 return p;
225}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700226EXPORT_SYMBOL(ioremap_nocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228void iounmap(volatile void __iomem *addr)
229{
230 struct vm_struct *p;
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700231
232 if ((void __force *)addr <= high_memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return;
234
235 /*
236 * __ioremap special-cases the PCI/ISA range by not instantiating a
237 * vm_area and by simply returning an address into the kernel mapping
238 * of ISA space. So handle that here.
239 */
240 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
241 addr < phys_to_virt(ISA_END_ADDRESS))
242 return;
243
Andi Kleen40579232005-05-20 14:27:57 -0700244 write_lock(&vmlist_lock);
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700245 p = __remove_vm_area((void *)(PAGE_MASK & (unsigned long __force)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!p) {
Christophe Lucasee48dd52005-06-25 14:59:24 -0700247 printk(KERN_WARNING "iounmap: bad address %p\n", addr);
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700248 dump_stack();
Andi Kleen40579232005-05-20 14:27:57 -0700249 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 if ((p->flags >> 20) && p->phys_addr < virt_to_phys(high_memory) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 change_page_attr(virt_to_page(__va(p->phys_addr)),
254 p->size >> PAGE_SHIFT,
255 PAGE_KERNEL);
256 global_flush_tlb();
257 }
Andi Kleen40579232005-05-20 14:27:57 -0700258out_unlock:
259 write_unlock(&vmlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 kfree(p);
261}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700262EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
265{
266 unsigned long offset, last_addr;
267 unsigned int nrpages;
268 enum fixed_addresses idx;
269
270 /* Don't allow wraparound or zero size */
271 last_addr = phys_addr + size - 1;
272 if (!size || last_addr < phys_addr)
273 return NULL;
274
275 /*
276 * Don't remap the low PCI/ISA area, it's always mapped..
277 */
278 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
279 return phys_to_virt(phys_addr);
280
281 /*
282 * Mappings have to be page-aligned
283 */
284 offset = phys_addr & ~PAGE_MASK;
285 phys_addr &= PAGE_MASK;
286 size = PAGE_ALIGN(last_addr) - phys_addr;
287
288 /*
289 * Mappings have to fit in the FIX_BTMAP area.
290 */
291 nrpages = size >> PAGE_SHIFT;
292 if (nrpages > NR_FIX_BTMAPS)
293 return NULL;
294
295 /*
296 * Ok, go for it..
297 */
298 idx = FIX_BTMAP_BEGIN;
299 while (nrpages > 0) {
300 set_fixmap(idx, phys_addr);
301 phys_addr += PAGE_SIZE;
302 --idx;
303 --nrpages;
304 }
305 return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
306}
307
308void __init bt_iounmap(void *addr, unsigned long size)
309{
310 unsigned long virt_addr;
311 unsigned long offset;
312 unsigned int nrpages;
313 enum fixed_addresses idx;
314
315 virt_addr = (unsigned long)addr;
316 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
317 return;
318 offset = virt_addr & ~PAGE_MASK;
319 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
320
321 idx = FIX_BTMAP_BEGIN;
322 while (nrpages > 0) {
323 clear_fixmap(idx);
324 --idx;
325 --nrpages;
326 }
327}