Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6-block.git] / arch / arm / mm / ioremap.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/arm/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 *
6 * (C) Copyright 1995 1996 Linus Torvalds
7 *
8 * Hacked for ARM by Phil Blundell <philb@gnu.org>
9 * Hacked to allow all architectures to build, and various cleanups
10 * by Russell King
11 *
12 * This allows a driver to remap an arbitrary region of bus memory into
13 * virtual space. One should *only* use readl, writel, memcpy_toio and
14 * so on with such remapped areas.
15 *
16 * Because the ARM only has a 32-bit address space we can't address the
17 * whole of the (physical) PCI space at once. PCI huge-mode addressing
18 * allows us to circumvent this restriction by splitting PCI space into
19 * two 2GB chunks and mapping only one at a time into processor memory.
20 * We use MMU protection domains to trap any attempt to access the bank
21 * that is not currently mapped. (This isn't fully implemented yet.)
22 */
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/mm.h>
26#include <linux/vmalloc.h>
27
28#include <asm/cacheflush.h>
29#include <asm/io.h>
30#include <asm/tlbflush.h>
31
32static inline void
33remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
34 unsigned long phys_addr, pgprot_t pgprot)
35{
36 unsigned long end;
37
38 address &= ~PMD_MASK;
39 end = address + size;
40 if (end > PMD_SIZE)
41 end = PMD_SIZE;
42 BUG_ON(address >= end);
43 do {
44 if (!pte_none(*pte))
45 goto bad;
46
47 set_pte(pte, pfn_pte(phys_addr >> PAGE_SHIFT, pgprot));
48 address += PAGE_SIZE;
49 phys_addr += PAGE_SIZE;
50 pte++;
51 } while (address && (address < end));
52 return;
53
54 bad:
55 printk("remap_area_pte: page already exists\n");
56 BUG();
57}
58
59static inline int
60remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
61 unsigned long phys_addr, unsigned long flags)
62{
63 unsigned long end;
64 pgprot_t pgprot;
65
66 address &= ~PGDIR_MASK;
67 end = address + size;
68
69 if (end > PGDIR_SIZE)
70 end = PGDIR_SIZE;
71
72 phys_addr -= address;
73 BUG_ON(address >= end);
74
75 pgprot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_WRITE | flags);
76 do {
77 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
78 if (!pte)
79 return -ENOMEM;
80 remap_area_pte(pte, address, end - address, address + phys_addr, pgprot);
81 address = (address + PMD_SIZE) & PMD_MASK;
82 pmd++;
83 } while (address && (address < end));
84 return 0;
85}
86
87static int
88remap_area_pages(unsigned long start, unsigned long phys_addr,
89 unsigned long size, unsigned long flags)
90{
91 unsigned long address = start;
92 unsigned long end = start + size;
93 int err = 0;
94 pgd_t * dir;
95
96 phys_addr -= address;
97 dir = pgd_offset(&init_mm, address);
98 BUG_ON(address >= end);
99 spin_lock(&init_mm.page_table_lock);
100 do {
101 pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
102 if (!pmd) {
103 err = -ENOMEM;
104 break;
105 }
106 if (remap_area_pmd(pmd, address, end - address,
107 phys_addr + address, flags)) {
108 err = -ENOMEM;
109 break;
110 }
111
112 address = (address + PGDIR_SIZE) & PGDIR_MASK;
113 dir++;
114 } while (address && (address < end));
115
116 spin_unlock(&init_mm.page_table_lock);
117 flush_cache_vmap(start, end);
118 return err;
119}
120
121/*
122 * Remap an arbitrary physical address space into the kernel virtual
123 * address space. Needed when the kernel wants to access high addresses
124 * directly.
125 *
126 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
127 * have to convert them into an offset in a page-aligned mapping, but the
128 * caller shouldn't need to know that small detail.
129 *
130 * 'flags' are the extra L_PTE_ flags that you want to specify for this
131 * mapping. See include/asm-arm/proc-armv/pgtable.h for more information.
132 */
133void __iomem *
134__ioremap(unsigned long phys_addr, size_t size, unsigned long flags,
135 unsigned long align)
136{
137 void * addr;
138 struct vm_struct * area;
139 unsigned long offset, last_addr;
140
141 /* Don't allow wraparound or zero size */
142 last_addr = phys_addr + size - 1;
143 if (!size || last_addr < phys_addr)
144 return NULL;
145
146 /*
147 * Mappings have to be page-aligned
148 */
149 offset = phys_addr & ~PAGE_MASK;
150 phys_addr &= PAGE_MASK;
151 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
152
153 /*
154 * Ok, go for it..
155 */
156 area = get_vm_area(size, VM_IOREMAP);
157 if (!area)
158 return NULL;
159 addr = area->addr;
160 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
161 vfree(addr);
162 return NULL;
163 }
164 return (void __iomem *) (offset + (char *)addr);
165}
166EXPORT_SYMBOL(__ioremap);
167
168void __iounmap(void __iomem *addr)
169{
170 vfree((void *) (PAGE_MASK & (unsigned long) addr));
171}
172EXPORT_SYMBOL(__iounmap);
09f0551d
RK
173
174#ifdef __io
175void __iomem *ioport_map(unsigned long port, unsigned int nr)
176{
177 return __io(port);
178}
179EXPORT_SYMBOL(ioport_map);
180
181void ioport_unmap(void __iomem *addr)
182{
183}
184EXPORT_SYMBOL(ioport_unmap);
185#endif
186
187#ifdef CONFIG_PCI
188#include <linux/pci.h>
189#include <linux/ioport.h>
190
191void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
192{
193 unsigned long start = pci_resource_start(dev, bar);
194 unsigned long len = pci_resource_len(dev, bar);
195 unsigned long flags = pci_resource_flags(dev, bar);
196
197 if (!len || !start)
198 return NULL;
199 if (maxlen && len > maxlen)
200 len = maxlen;
201 if (flags & IORESOURCE_IO)
202 return ioport_map(start, len);
203 if (flags & IORESOURCE_MEM) {
204 if (flags & IORESOURCE_CACHEABLE)
205 return ioremap(start, len);
206 return ioremap_nocache(start, len);
207 }
208 return NULL;
209}
210EXPORT_SYMBOL(pci_iomap);
211
212void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
213{
214 if ((unsigned long)addr >= VMALLOC_START &&
215 (unsigned long)addr < VMALLOC_END)
216 iounmap(addr);
217}
218EXPORT_SYMBOL(pci_iounmap);
219#endif