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>
19 #include <asm/proto.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)
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 inline int remap_area_pud(pud_t * pud, unsigned long address, unsigned long size,
71 unsigned long phys_addr, unsigned long flags)
75 address &= ~PGDIR_MASK;
83 pmd_t * pmd = pmd_alloc(&init_mm, pud, address);
86 remap_area_pmd(pmd, address, end - address, address + phys_addr, flags);
87 address = (address + PUD_SIZE) & PUD_MASK;
89 } while (address && (address < end));
93 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
94 unsigned long size, unsigned long flags)
98 unsigned long end = address + size;
100 phys_addr -= address;
101 pgd = pgd_offset_k(address);
105 spin_lock(&init_mm.page_table_lock);
108 pud = pud_alloc(&init_mm, pgd, address);
112 if (remap_area_pud(pud, address, end - address,
113 phys_addr + address, flags))
116 address = (address + PGDIR_SIZE) & PGDIR_MASK;
118 } while (address && (address < end));
119 spin_unlock(&init_mm.page_table_lock);
125 * Fix up the linear direct mapping of the kernel to avoid cache attribute
129 ioremap_change_attr(unsigned long phys_addr, unsigned long size,
133 if (flags && phys_addr + size - 1 < (end_pfn_map << PAGE_SHIFT)) {
134 unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
135 unsigned long vaddr = (unsigned long) __va(phys_addr);
138 * Must use a address here and not struct page because the phys addr
139 * can be a in hole between nodes and not have an memmap entry.
141 err = change_page_attr_addr(vaddr,npages,__pgprot(__PAGE_KERNEL|flags));
149 * Generic mapping function
153 * Remap an arbitrary physical address space into the kernel virtual
154 * address space. Needed when the kernel wants to access high addresses
157 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
158 * have to convert them into an offset in a page-aligned mapping, but the
159 * caller shouldn't need to know that small detail.
161 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
164 struct vm_struct * area;
165 unsigned long offset, last_addr;
167 /* Don't allow wraparound or zero size */
168 last_addr = phys_addr + size - 1;
169 if (!size || last_addr < phys_addr)
173 * Don't remap the low PCI/ISA area, it's always mapped..
175 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
176 return (__force void __iomem *)phys_to_virt(phys_addr);
178 #ifndef CONFIG_DISCONTIGMEM
180 * Don't allow anybody to remap normal RAM that we're using..
182 if (last_addr < virt_to_phys(high_memory)) {
183 char *t_addr, *t_end;
186 t_addr = __va(phys_addr);
187 t_end = t_addr + (size - 1);
189 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
190 if(!PageReserved(page))
196 * Mappings have to be page-aligned
198 offset = phys_addr & ~PAGE_MASK;
199 phys_addr &= PAGE_MASK;
200 size = PAGE_ALIGN(last_addr+1) - phys_addr;
205 area = get_vm_area(size, VM_IOREMAP | (flags << 20));
208 area->phys_addr = phys_addr;
210 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
211 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
214 if (ioremap_change_attr(phys_addr, size, flags) < 0) {
215 area->flags &= 0xffffff;
219 return (__force void __iomem *) (offset + (char *)addr);
223 * ioremap_nocache - map bus memory into CPU space
224 * @offset: bus address of the memory
225 * @size: size of the resource to map
227 * ioremap_nocache performs a platform specific sequence of operations to
228 * make bus memory CPU accessible via the readb/readw/readl/writeb/
229 * writew/writel functions and the other mmio helpers. The returned
230 * address is not guaranteed to be usable directly as a virtual
233 * This version of ioremap ensures that the memory is marked uncachable
234 * on the CPU as well as honouring existing caching rules from things like
235 * the PCI bus. Note that there are other caches and buffers on many
236 * busses. In particular driver authors should read up on PCI writes
238 * It's useful if some control registers are in such an area and
239 * write combining or read caching is not desirable:
241 * Must be freed with iounmap.
244 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
246 return __ioremap(phys_addr, size, _PAGE_PCD);
249 void iounmap(volatile void __iomem *addr)
251 struct vm_struct *p, **pprev;
253 if (addr <= high_memory)
256 write_lock(&vmlist_lock);
257 for (p = vmlist, pprev = &vmlist; p != NULL; pprev = &p->next, p = *pprev)
258 if (p->addr == (void *)(PAGE_MASK & (unsigned long)addr))
261 printk("__iounmap: bad address %p\n", addr);
266 if ((p->flags >> 20) &&
267 p->phys_addr + p->size - 1 < virt_to_phys(high_memory)) {
268 /* p->size includes the guard page, but cpa doesn't like that */
269 change_page_attr(virt_to_page(__va(p->phys_addr)),
270 (p->size - PAGE_SIZE) >> PAGE_SHIFT,
275 write_unlock(&vmlist_lock);