2 * arch/x86_64/mm/ioremap.c
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
11 #include <linux/vmalloc.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
17 #include <asm/pgalloc.h>
18 #include <asm/fixmap.h>
19 #include <asm/tlbflush.h>
20 #include <asm/cacheflush.h>
21 #include <asm/proto.h>
23 unsigned long __phys_addr(unsigned long x)
25 if (x >= __START_KERNEL_map)
26 return x - __START_KERNEL_map + phys_base;
27 return x - PAGE_OFFSET;
29 EXPORT_SYMBOL(__phys_addr);
31 #define ISA_START_ADDRESS 0xa0000
32 #define ISA_END_ADDRESS 0x100000
35 * Fix up the linear direct mapping of the kernel to avoid cache attribute
39 ioremap_change_attr(unsigned long phys_addr, unsigned long size,
43 if (phys_addr + size - 1 < (end_pfn_map << PAGE_SHIFT)) {
44 unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
45 unsigned long vaddr = (unsigned long) __va(phys_addr);
48 * Must use a address here and not struct page because the phys addr
49 * can be a in hole between nodes and not have an memmap entry.
51 err = change_page_attr_addr(vaddr,npages,__pgprot(__PAGE_KERNEL|flags));
59 * Generic mapping function
63 * Remap an arbitrary physical address space into the kernel virtual
64 * address space. Needed when the kernel wants to access high addresses
67 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
68 * have to convert them into an offset in a page-aligned mapping, but the
69 * caller shouldn't need to know that small detail.
71 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
74 struct vm_struct * area;
75 unsigned long offset, last_addr;
78 /* Don't allow wraparound or zero size */
79 last_addr = phys_addr + size - 1;
80 if (!size || last_addr < phys_addr)
84 * Don't remap the low PCI/ISA area, it's always mapped..
86 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
87 return (__force void __iomem *)phys_to_virt(phys_addr);
89 pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_GLOBAL
90 | _PAGE_DIRTY | _PAGE_ACCESSED | flags);
92 * Mappings have to be page-aligned
94 offset = phys_addr & ~PAGE_MASK;
95 phys_addr &= PAGE_MASK;
96 size = PAGE_ALIGN(last_addr+1) - phys_addr;
101 area = get_vm_area(size, VM_IOREMAP | (flags << 20));
104 area->phys_addr = phys_addr;
106 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
107 phys_addr, pgprot)) {
108 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
111 if (flags && ioremap_change_attr(phys_addr, size, flags) < 0) {
112 area->flags &= 0xffffff;
116 return (__force void __iomem *) (offset + (char *)addr);
118 EXPORT_SYMBOL(__ioremap);
121 * ioremap_nocache - map bus memory into CPU space
122 * @offset: bus address of the memory
123 * @size: size of the resource to map
125 * ioremap_nocache performs a platform specific sequence of operations to
126 * make bus memory CPU accessible via the readb/readw/readl/writeb/
127 * writew/writel functions and the other mmio helpers. The returned
128 * address is not guaranteed to be usable directly as a virtual
131 * This version of ioremap ensures that the memory is marked uncachable
132 * on the CPU as well as honouring existing caching rules from things like
133 * the PCI bus. Note that there are other caches and buffers on many
134 * busses. In particular driver authors should read up on PCI writes
136 * It's useful if some control registers are in such an area and
137 * write combining or read caching is not desirable:
139 * Must be freed with iounmap.
142 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
144 return __ioremap(phys_addr, size, _PAGE_PCD);
146 EXPORT_SYMBOL(ioremap_nocache);
149 * iounmap - Free a IO remapping
150 * @addr: virtual address from ioremap_*
152 * Caller must ensure there is only one unmapping for the same pointer.
154 void iounmap(volatile void __iomem *addr)
156 struct vm_struct *p, *o;
158 if (addr <= high_memory)
160 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
161 addr < phys_to_virt(ISA_END_ADDRESS))
164 addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long __force)addr);
165 /* Use the vm area unlocked, assuming the caller
166 ensures there isn't another iounmap for the same address
167 in parallel. Reuse of the virtual address is prevented by
168 leaving it in the global lists until we're done with it.
169 cpa takes care of the direct mappings. */
170 read_lock(&vmlist_lock);
171 for (p = vmlist; p; p = p->next) {
175 read_unlock(&vmlist_lock);
178 printk("iounmap: bad address %p\n", addr);
183 /* Reset the direct mapping. Can block */
185 ioremap_change_attr(p->phys_addr, p->size, 0);
187 /* Finally remove it */
188 o = remove_vm_area((void *)addr);
189 BUG_ON(p != o || o == NULL);
192 EXPORT_SYMBOL(iounmap);