blob: de3ce1eec2ece4d222a2293bfa325fb585104433 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Russell King674c0452005-10-28 14:25:28 +010029#include <asm/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/tlbflush.h>
32
33static inline void
34remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
35 unsigned long phys_addr, pgprot_t pgprot)
36{
37 unsigned long end;
38
39 address &= ~PMD_MASK;
40 end = address + size;
41 if (end > PMD_SIZE)
42 end = PMD_SIZE;
43 BUG_ON(address >= end);
44 do {
45 if (!pte_none(*pte))
46 goto bad;
47
48 set_pte(pte, pfn_pte(phys_addr >> PAGE_SHIFT, pgprot));
49 address += PAGE_SIZE;
50 phys_addr += PAGE_SIZE;
51 pte++;
52 } while (address && (address < end));
53 return;
54
55 bad:
56 printk("remap_area_pte: page already exists\n");
57 BUG();
58}
59
60static inline int
61remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
62 unsigned long phys_addr, unsigned long flags)
63{
64 unsigned long end;
65 pgprot_t pgprot;
66
67 address &= ~PGDIR_MASK;
68 end = address + size;
69
70 if (end > PGDIR_SIZE)
71 end = PGDIR_SIZE;
72
73 phys_addr -= address;
74 BUG_ON(address >= end);
75
76 pgprot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_WRITE | flags);
77 do {
Hugh Dickins872fec12005-10-29 18:16:21 -070078 pte_t * pte = pte_alloc_kernel(pmd, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!pte)
80 return -ENOMEM;
81 remap_area_pte(pte, address, end - address, address + phys_addr, pgprot);
82 address = (address + PMD_SIZE) & PMD_MASK;
83 pmd++;
84 } while (address && (address < end));
85 return 0;
86}
87
88static int
Deepak Saxena9d4ae722006-01-09 19:23:11 +000089remap_area_pages(unsigned long start, unsigned long pfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long size, unsigned long flags)
91{
92 unsigned long address = start;
93 unsigned long end = start + size;
Deepak Saxena9d4ae722006-01-09 19:23:11 +000094 unsigned long phys_addr = __pfn_to_phys(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int err = 0;
96 pgd_t * dir;
97
98 phys_addr -= address;
99 dir = pgd_offset(&init_mm, address);
100 BUG_ON(address >= end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 do {
102 pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
103 if (!pmd) {
104 err = -ENOMEM;
105 break;
106 }
107 if (remap_area_pmd(pmd, address, end - address,
108 phys_addr + address, flags)) {
109 err = -ENOMEM;
110 break;
111 }
112
113 address = (address + PGDIR_SIZE) & PGDIR_MASK;
114 dir++;
115 } while (address && (address < end));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 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 *
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000134__ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
135 unsigned long flags)
136{
137 unsigned long addr;
138 struct vm_struct * area;
139
140 area = get_vm_area(size, VM_IOREMAP);
141 if (!area)
142 return NULL;
143 addr = (unsigned long)area->addr;
144 if (remap_area_pages(addr, pfn, size, flags)) {
145 vfree(addr);
146 return NULL;
147 }
148 return (void __iomem *) (offset + (char *)addr);
149}
150EXPORT_SYMBOL(__ioremap_pfn);
151
152void __iomem *
Russell King67a19012005-11-17 16:48:00 +0000153__ioremap(unsigned long phys_addr, size_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000155 unsigned long last_addr;
156 unsigned long offset = phys_addr & ~PAGE_MASK;
157 unsigned long pfn = __phys_to_pfn(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000159 /*
160 * Don't allow wraparound or zero size
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 last_addr = phys_addr + size - 1;
163 if (!size || last_addr < phys_addr)
164 return NULL;
165
166 /*
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000167 * Page align the mapping size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
170
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000171 return __ioremap_pfn(pfn, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173EXPORT_SYMBOL(__ioremap);
174
175void __iounmap(void __iomem *addr)
176{
177 vfree((void *) (PAGE_MASK & (unsigned long) addr));
178}
179EXPORT_SYMBOL(__iounmap);
Russell King09f05512005-06-20 18:44:37 +0100180
181#ifdef __io
182void __iomem *ioport_map(unsigned long port, unsigned int nr)
183{
184 return __io(port);
185}
186EXPORT_SYMBOL(ioport_map);
187
188void ioport_unmap(void __iomem *addr)
189{
190}
191EXPORT_SYMBOL(ioport_unmap);
192#endif
193
194#ifdef CONFIG_PCI
195#include <linux/pci.h>
196#include <linux/ioport.h>
197
198void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
199{
200 unsigned long start = pci_resource_start(dev, bar);
201 unsigned long len = pci_resource_len(dev, bar);
202 unsigned long flags = pci_resource_flags(dev, bar);
203
204 if (!len || !start)
205 return NULL;
206 if (maxlen && len > maxlen)
207 len = maxlen;
208 if (flags & IORESOURCE_IO)
209 return ioport_map(start, len);
210 if (flags & IORESOURCE_MEM) {
211 if (flags & IORESOURCE_CACHEABLE)
212 return ioremap(start, len);
213 return ioremap_nocache(start, len);
214 }
215 return NULL;
216}
217EXPORT_SYMBOL(pci_iomap);
218
219void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
220{
221 if ((unsigned long)addr >= VMALLOC_START &&
222 (unsigned long)addr < VMALLOC_END)
223 iounmap(addr);
224}
225EXPORT_SYMBOL(pci_iounmap);
226#endif