blob: fc2c96f0a1fd6bd532b233da431ca7dc4f6db5ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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, 2002 Ralf Baechle
8 */
Haavard Skinnemoen8e087922006-12-08 02:38:04 -08009#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <asm/addrspace.h>
12#include <asm/byteorder.h>
13
14#include <linux/vmalloc.h>
Haavard Skinnemoen8e087922006-12-08 02:38:04 -080015#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Generic mapping function (not visible outside):
19 */
20
21/*
22 * Remap an arbitrary physical address space into the kernel virtual
23 * address space. Needed when the kernel wants to access high addresses
24 * directly.
25 *
26 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
27 * have to convert them into an offset in a page-aligned mapping, but the
28 * caller shouldn't need to know that small detail.
29 */
30
31#define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
32
Maciej W. Rozyckic3455b02005-06-30 10:48:40 +000033void __iomem * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct vm_struct * area;
36 unsigned long offset;
37 phys_t last_addr;
38 void * addr;
Haavard Skinnemoen8e087922006-12-08 02:38:04 -080039 pgprot_t pgprot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 phys_addr = fixup_bigphys_addr(phys_addr, size);
42
43 /* Don't allow wraparound or zero size */
44 last_addr = phys_addr + size - 1;
45 if (!size || last_addr < phys_addr)
46 return NULL;
47
48 /*
49 * Map uncached objects in the low 512mb of address space using KSEG1,
50 * otherwise map using page tables.
51 */
52 if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
53 flags == _CACHE_UNCACHED)
Maciej W. Rozyckic3455b02005-06-30 10:48:40 +000054 return (void __iomem *) CKSEG1ADDR(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 /*
57 * Don't allow anybody to remap normal RAM that we're using..
58 */
59 if (phys_addr < virt_to_phys(high_memory)) {
60 char *t_addr, *t_end;
61 struct page *page;
62
63 t_addr = __va(phys_addr);
64 t_end = t_addr + (size - 1);
65
66 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
67 if(!PageReserved(page))
68 return NULL;
69 }
70
Haavard Skinnemoen8e087922006-12-08 02:38:04 -080071 pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
72 | __WRITEABLE | flags);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /*
75 * Mappings have to be page-aligned
76 */
77 offset = phys_addr & ~PAGE_MASK;
78 phys_addr &= PAGE_MASK;
79 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
80
81 /*
82 * Ok, go for it..
83 */
84 area = get_vm_area(size, VM_IOREMAP);
85 if (!area)
86 return NULL;
87 addr = area->addr;
Haavard Skinnemoen8e087922006-12-08 02:38:04 -080088 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
89 phys_addr, pgprot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 vunmap(addr);
91 return NULL;
92 }
93
Maciej W. Rozyckic3455b02005-06-30 10:48:40 +000094 return (void __iomem *) (offset + (char *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Ralf Baechlec6e8b582005-02-10 12:19:59 +000097#define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Ralf Baechled89e36d2006-10-19 14:21:47 +010099void __iounmap(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct vm_struct *p;
102
103 if (IS_KSEG1(addr))
104 return;
105
106 p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000107 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 printk(KERN_ERR "iounmap: bad address %p\n", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 kfree(p);
111}
112
113EXPORT_SYMBOL(__ioremap);
114EXPORT_SYMBOL(__iounmap);