4 * Re-map IO memory to kernel address space so that we can access it.
5 * This is needed for high PCI addresses that aren't mapped in the
6 * 640k-1MB IO memory area on PC's
8 * (C) Copyright 1995 1996 Linus Torvalds
9 * (C) Copyright 2005, 2006 Paul Mundt
11 * This file is subject to the terms and conditions of the GNU General
12 * Public License. See the file "COPYING" in the main directory of this
13 * archive for more details.
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
18 #include <linux/pci.h>
21 #include <asm/pgalloc.h>
22 #include <asm/addrspace.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
26 static inline void remap_area_pte(pte_t * pte, unsigned long address,
27 unsigned long size, unsigned long phys_addr, unsigned long flags)
31 pgprot_t pgprot = __pgprot(pgprot_val(PAGE_KERNEL_NOCACHE) | flags);
39 pfn = phys_addr >> PAGE_SHIFT;
41 if (!pte_none(*pte)) {
42 printk("remap_area_pte: page already exists\n");
45 set_pte(pte, pfn_pte(pfn, pgprot));
49 } while (address && (address < end));
52 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
53 unsigned long size, unsigned long phys_addr, unsigned long flags)
57 address &= ~PGDIR_MASK;
65 pte_t * pte = pte_alloc_kernel(pmd, address);
68 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
69 address = (address + PMD_SIZE) & PMD_MASK;
71 } while (address && (address < end));
75 int remap_area_pages(unsigned long address, unsigned long phys_addr,
76 unsigned long size, unsigned long flags)
80 unsigned long end = address + size;
83 dir = pgd_offset_k(address);
93 pud = pud_alloc(&init_mm, dir, address);
96 pmd = pmd_alloc(&init_mm, pud, address);
99 if (remap_area_pmd(pmd, address, end - address,
100 phys_addr + address, flags))
103 address = (address + PGDIR_SIZE) & PGDIR_MASK;
105 } while (address && (address < end));
111 * Remap an arbitrary physical address space into the kernel virtual
112 * address space. Needed when the kernel wants to access high addresses
115 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
116 * have to convert them into an offset in a page-aligned mapping, but the
117 * caller shouldn't need to know that small detail.
119 void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
122 struct vm_struct * area;
123 unsigned long offset, last_addr, addr, orig_addr;
125 /* Don't allow wraparound or zero size */
126 last_addr = phys_addr + size - 1;
127 if (!size || last_addr < phys_addr)
131 * Don't remap the low PCI/ISA area, it's always mapped..
133 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
134 return (void __iomem *)phys_to_virt(phys_addr);
137 * If we're on an SH7751 or SH7780 PCI controller, PCI memory is
138 * mapped at the end of the address space (typically 0xfd000000)
139 * in a non-translatable area, so mapping through page tables for
140 * this area is not only pointless, but also fundamentally
141 * broken. Just return the physical address instead.
143 * For boards that map a small PCI memory aperture somewhere in
144 * P1/P2 space, ioremap() will already do the right thing,
145 * and we'll never get this far.
147 if (is_pci_memaddr(phys_addr) && is_pci_memaddr(last_addr))
148 return (void __iomem *)phys_addr;
151 * Don't allow anybody to remap normal RAM that we're using..
153 if (phys_addr < virt_to_phys(high_memory))
157 * Mappings have to be page-aligned
159 offset = phys_addr & ~PAGE_MASK;
160 phys_addr &= PAGE_MASK;
161 size = PAGE_ALIGN(last_addr+1) - phys_addr;
166 area = get_vm_area(size, VM_IOREMAP);
169 area->phys_addr = phys_addr;
170 orig_addr = addr = (unsigned long)area->addr;
174 * First try to remap through the PMB once a valid VMA has been
175 * established. Smaller allocations (or the rest of the size
176 * remaining after a PMB mapping due to the size not being
177 * perfectly aligned on a PMB size boundary) are then mapped
178 * through the UTLB using conventional page tables.
180 * PMB entries are all pre-faulted.
182 if (unlikely(size >= 0x1000000)) {
183 unsigned long mapped = pmb_remap(addr, phys_addr, size, flags);
185 if (likely(mapped)) {
194 if (remap_area_pages(addr, phys_addr, size, flags)) {
195 vunmap((void *)orig_addr);
199 return (void __iomem *)(offset + (char *)orig_addr);
201 EXPORT_SYMBOL(__ioremap);
203 void __iounmap(void __iomem *addr)
205 unsigned long vaddr = (unsigned long __force)addr;
208 if (PXSEG(vaddr) < P3SEG || is_pci_memaddr(vaddr))
213 * Purge any PMB entries that may have been established for this
214 * mapping, then proceed with conventional VMA teardown.
216 * XXX: Note that due to the way that remove_vm_area() does
217 * matching of the resultant VMA, we aren't able to fast-forward
218 * the address past the PMB space until the end of the VMA where
219 * the page tables reside. As such, unmap_vm_area() will be
220 * forced to linearly scan over the area until it finds the page
221 * tables where PTEs that need to be unmapped actually reside,
222 * which is far from optimal. Perhaps we need to use a separate
223 * VMA for the PMB mappings?
229 p = remove_vm_area((void *)(vaddr & PAGE_MASK));
231 printk(KERN_ERR "%s: bad address %p\n", __FUNCTION__, addr);
237 EXPORT_SYMBOL(__iounmap);