[SCSI] ibmvscsi: convert kmalloc + memset to kcalloc
[wrt350n-kernel.git] / arch / arm / mm / ioremap.c
blob25e0ca3e598cfc97ac0140aaa383effaf5e76b77
1 /*
2 * linux/arch/arm/mm/ioremap.c
4 * Re-map IO memory to kernel address space so that we can access it.
6 * (C) Copyright 1995 1996 Linus Torvalds
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
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.
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.)
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/mm.h>
26 #include <linux/vmalloc.h>
28 #include <asm/cacheflush.h>
29 #include <asm/io.h>
30 #include <asm/tlbflush.h>
32 static inline void
33 remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
34 unsigned long phys_addr, pgprot_t pgprot)
36 unsigned long end;
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;
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;
54 bad:
55 printk("remap_area_pte: page already exists\n");
56 BUG();
59 static inline int
60 remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
61 unsigned long phys_addr, unsigned long flags)
63 unsigned long end;
64 pgprot_t pgprot;
66 address &= ~PGDIR_MASK;
67 end = address + size;
69 if (end > PGDIR_SIZE)
70 end = PGDIR_SIZE;
72 phys_addr -= address;
73 BUG_ON(address >= end);
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(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;
87 static int
88 remap_area_pages(unsigned long start, unsigned long pfn,
89 unsigned long size, unsigned long flags)
91 unsigned long address = start;
92 unsigned long end = start + size;
93 unsigned long phys_addr = __pfn_to_phys(pfn);
94 int err = 0;
95 pgd_t * dir;
97 phys_addr -= address;
98 dir = pgd_offset(&init_mm, address);
99 BUG_ON(address >= end);
100 do {
101 pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
102 if (!pmd) {
103 err = -ENOMEM;
104 break;
106 if (remap_area_pmd(pmd, address, end - address,
107 phys_addr + address, flags)) {
108 err = -ENOMEM;
109 break;
112 address = (address + PGDIR_SIZE) & PGDIR_MASK;
113 dir++;
114 } while (address && (address < end));
116 flush_cache_vmap(start, end);
117 return err;
121 * Remap an arbitrary physical address space into the kernel virtual
122 * address space. Needed when the kernel wants to access high addresses
123 * directly.
125 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
126 * have to convert them into an offset in a page-aligned mapping, but the
127 * caller shouldn't need to know that small detail.
129 * 'flags' are the extra L_PTE_ flags that you want to specify for this
130 * mapping. See include/asm-arm/proc-armv/pgtable.h for more information.
132 void __iomem *
133 __ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
134 unsigned long flags)
136 unsigned long addr;
137 struct vm_struct * area;
139 area = get_vm_area(size, VM_IOREMAP);
140 if (!area)
141 return NULL;
142 addr = (unsigned long)area->addr;
143 if (remap_area_pages(addr, pfn, size, flags)) {
144 vfree((void *)addr);
145 return NULL;
147 return (void __iomem *) (offset + (char *)addr);
149 EXPORT_SYMBOL(__ioremap_pfn);
151 void __iomem *
152 __ioremap(unsigned long phys_addr, size_t size, unsigned long flags)
154 unsigned long last_addr;
155 unsigned long offset = phys_addr & ~PAGE_MASK;
156 unsigned long pfn = __phys_to_pfn(phys_addr);
159 * Don't allow wraparound or zero size
161 last_addr = phys_addr + size - 1;
162 if (!size || last_addr < phys_addr)
163 return NULL;
166 * Page align the mapping size
168 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
170 return __ioremap_pfn(pfn, offset, size, flags);
172 EXPORT_SYMBOL(__ioremap);
174 void __iounmap(void __iomem *addr)
176 vfree((void *) (PAGE_MASK & (unsigned long) addr));
178 EXPORT_SYMBOL(__iounmap);
180 #ifdef __io
181 void __iomem *ioport_map(unsigned long port, unsigned int nr)
183 return __io(port);
185 EXPORT_SYMBOL(ioport_map);
187 void ioport_unmap(void __iomem *addr)
190 EXPORT_SYMBOL(ioport_unmap);
191 #endif
193 #ifdef CONFIG_PCI
194 #include <linux/pci.h>
195 #include <linux/ioport.h>
197 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
199 unsigned long start = pci_resource_start(dev, bar);
200 unsigned long len = pci_resource_len(dev, bar);
201 unsigned long flags = pci_resource_flags(dev, bar);
203 if (!len || !start)
204 return NULL;
205 if (maxlen && len > maxlen)
206 len = maxlen;
207 if (flags & IORESOURCE_IO)
208 return ioport_map(start, len);
209 if (flags & IORESOURCE_MEM) {
210 if (flags & IORESOURCE_CACHEABLE)
211 return ioremap(start, len);
212 return ioremap_nocache(start, len);
214 return NULL;
216 EXPORT_SYMBOL(pci_iomap);
218 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
220 if ((unsigned long)addr >= VMALLOC_START &&
221 (unsigned long)addr < VMALLOC_END)
222 iounmap(addr);
224 EXPORT_SYMBOL(pci_iounmap);
225 #endif