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>
15 #include <asm/pgalloc.h>
16 #include <asm/fixmap.h>
17 #include <asm/cacheflush.h>
18 #include <asm/tlbflush.h>
21 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
22 unsigned long phys_addr, unsigned long flags)
33 pfn = phys_addr >> PAGE_SHIFT;
35 if (!pte_none(*pte)) {
36 printk("remap_area_pte: page already exists\n");
39 set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW |
40 _PAGE_GLOBAL | _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
44 } while (address && (address < end));
47 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
48 unsigned long phys_addr, unsigned long flags)
52 address &= ~PGDIR_MASK;
60 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
63 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
64 address = (address + PMD_SIZE) & PMD_MASK;
66 } while (address && (address < end));
70 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
71 unsigned long size, unsigned long flags)
75 unsigned long end = address + size;
78 dir = pgd_offset_k(address);
82 spin_lock(&init_mm.page_table_lock);
85 pmd = pmd_alloc(&init_mm, dir, address);
89 if (remap_area_pmd(pmd, address, end - address,
90 phys_addr + address, flags))
93 address = (address + PGDIR_SIZE) & PGDIR_MASK;
95 } while (address && (address < end));
96 spin_unlock(&init_mm.page_table_lock);
102 * Generic mapping function (not visible outside):
106 * Remap an arbitrary physical address space into the kernel virtual
107 * address space. Needed when the kernel wants to access high addresses
110 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
111 * have to convert them into an offset in a page-aligned mapping, but the
112 * caller shouldn't need to know that small detail.
114 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
117 struct vm_struct * area;
118 unsigned long offset, last_addr;
120 /* Don't allow wraparound or zero size */
121 last_addr = phys_addr + size - 1;
122 if (!size || last_addr < phys_addr)
126 * Don't remap the low PCI/ISA area, it's always mapped..
128 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
129 return (__force void __iomem *)phys_to_virt(phys_addr);
132 * Don't allow anybody to remap normal RAM that we're using..
134 if (phys_addr < virt_to_phys(high_memory)) {
135 #ifndef CONFIG_DISCONTIGMEM
136 char *t_addr, *t_end;
139 t_addr = __va(phys_addr);
140 t_end = t_addr + (size - 1);
142 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
143 if(!PageReserved(page))
149 * Mappings have to be page-aligned
151 offset = phys_addr & ~PAGE_MASK;
152 phys_addr &= PAGE_MASK;
153 size = PAGE_ALIGN(last_addr+1) - phys_addr;
158 area = get_vm_area(size, VM_IOREMAP);
161 area->phys_addr = phys_addr;
163 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
167 return (__force void __iomem *) (offset + (char *)addr);
171 * ioremap_nocache - map bus memory into CPU space
172 * @offset: bus address of the memory
173 * @size: size of the resource to map
175 * ioremap_nocache performs a platform specific sequence of operations to
176 * make bus memory CPU accessible via the readb/readw/readl/writeb/
177 * writew/writel functions and the other mmio helpers. The returned
178 * address is not guaranteed to be usable directly as a virtual
181 * This version of ioremap ensures that the memory is marked uncachable
182 * on the CPU as well as honouring existing caching rules from things like
183 * the PCI bus. Note that there are other caches and buffers on many
184 * busses. In particular driver authors should read up on PCI writes
186 * It's useful if some control registers are in such an area and
187 * write combining or read caching is not desirable:
189 * Must be freed with iounmap.
192 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
194 void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
198 if (phys_addr + size < virt_to_phys(high_memory)) {
199 struct page *ppage = virt_to_page(__va(phys_addr));
200 unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
202 BUG_ON(phys_addr+size > (unsigned long)high_memory);
203 BUG_ON(phys_addr + size < phys_addr);
205 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
215 void iounmap(volatile void __iomem *addr)
218 if (addr <= high_memory)
220 p = remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
222 printk("__iounmap: bad address %p\n", addr);
226 if (p->flags && p->phys_addr < virt_to_phys(high_memory)) {
227 change_page_attr(virt_to_page(__va(p->phys_addr)),
228 p->size >> PAGE_SHIFT,