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
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>
26 #include <linux/vmalloc.h>
28 #include <asm/cacheflush.h>
29 #include <asm/hardware.h>
31 #include <asm/tlbflush.h>
34 remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
35 unsigned long phys_addr, pgprot_t pgprot)
43 BUG_ON(address >= end);
48 set_pte(pte, pfn_pte(phys_addr >> PAGE_SHIFT, pgprot));
50 phys_addr += PAGE_SIZE;
52 } while (address && (address < end));
56 printk("remap_area_pte: page already exists\n");
61 remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
62 unsigned long phys_addr, unsigned long flags)
67 address &= ~PGDIR_MASK;
74 BUG_ON(address >= end);
76 pgprot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_WRITE | flags);
78 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
81 remap_area_pte(pte, address, end - address, address + phys_addr, pgprot);
82 address = (address + PMD_SIZE) & PMD_MASK;
84 } while (address && (address < end));
89 remap_area_pages(unsigned long start, unsigned long phys_addr,
90 unsigned long size, unsigned long flags)
92 unsigned long address = start;
93 unsigned long end = start + size;
98 dir = pgd_offset(&init_mm, address);
99 BUG_ON(address >= end);
100 spin_lock(&init_mm.page_table_lock);
102 pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
107 if (remap_area_pmd(pmd, address, end - address,
108 phys_addr + address, flags)) {
113 address = (address + PGDIR_SIZE) & PGDIR_MASK;
115 } while (address && (address < end));
117 spin_unlock(&init_mm.page_table_lock);
118 flush_cache_vmap(start, end);
123 * Remap an arbitrary physical address space into the kernel virtual
124 * address space. Needed when the kernel wants to access high addresses
127 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
128 * have to convert them into an offset in a page-aligned mapping, but the
129 * caller shouldn't need to know that small detail.
131 * 'flags' are the extra L_PTE_ flags that you want to specify for this
132 * mapping. See include/asm-arm/proc-armv/pgtable.h for more information.
135 __ioremap(unsigned long phys_addr, size_t size, unsigned long flags,
139 struct vm_struct * area;
140 unsigned long offset, last_addr;
142 /* Don't allow wraparound or zero size */
143 last_addr = phys_addr + size - 1;
144 if (!size || last_addr < phys_addr)
148 * Mappings have to be page-aligned
150 offset = phys_addr & ~PAGE_MASK;
151 phys_addr &= PAGE_MASK;
152 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
157 area = get_vm_area(size, VM_IOREMAP);
161 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
165 return (void __iomem *) (offset + (char *)addr);
167 EXPORT_SYMBOL(__ioremap);
169 void __iounmap(void __iomem *addr)
171 vfree((void *) (PAGE_MASK & (unsigned long) addr));
173 EXPORT_SYMBOL(__iounmap);
176 void __iomem *ioport_map(unsigned long port, unsigned int nr)
180 EXPORT_SYMBOL(ioport_map);
182 void ioport_unmap(void __iomem *addr)
185 EXPORT_SYMBOL(ioport_unmap);
189 #include <linux/pci.h>
190 #include <linux/ioport.h>
192 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
194 unsigned long start = pci_resource_start(dev, bar);
195 unsigned long len = pci_resource_len(dev, bar);
196 unsigned long flags = pci_resource_flags(dev, bar);
200 if (maxlen && len > maxlen)
202 if (flags & IORESOURCE_IO)
203 return ioport_map(start, len);
204 if (flags & IORESOURCE_MEM) {
205 if (flags & IORESOURCE_CACHEABLE)
206 return ioremap(start, len);
207 return ioremap_nocache(start, len);
211 EXPORT_SYMBOL(pci_iomap);
213 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
215 if ((unsigned long)addr >= VMALLOC_START &&
216 (unsigned long)addr < VMALLOC_END)
219 EXPORT_SYMBOL(pci_iounmap);