2 * arch/cris/mm/ioremap.c
4 * Re-map IO memory to kernel address space so that we can access it.
5 * Needed for memory-mapped I/O devices mapped outside our normal DRAM
6 * window (that is, all memory-mapped I/O devices).
8 * (C) Copyright 1995 1996 Linus Torvalds
9 * CRIS-port by Axis Communications AB
12 #include <linux/vmalloc.h>
14 #include <asm/pgalloc.h>
16 extern inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
17 unsigned long phys_addr, unsigned long flags)
28 if (!pte_none(*pte)) {
29 printk("remap_area_pte: page already exists\n");
32 set_pte(pte, mk_pte_phys(phys_addr, __pgprot(_PAGE_PRESENT | __READABLE |
33 __WRITEABLE | _PAGE_GLOBAL |
34 _PAGE_KERNEL | flags)));
36 phys_addr += PAGE_SIZE;
38 } while (address && (address < end));
41 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
42 unsigned long phys_addr, unsigned long flags)
46 address &= ~PGDIR_MASK;
54 pte_t * pte = pte_alloc(&init_mm, pmd, address);
57 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
58 address = (address + PMD_SIZE) & PMD_MASK;
60 } while (address && (address < end));
64 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
65 unsigned long size, unsigned long flags)
69 unsigned long end = address + size;
72 dir = pgd_offset(&init_mm, address);
76 spin_lock(&init_mm.page_table_lock);
79 pmd = pmd_alloc(&init_mm, dir, address);
83 if (remap_area_pmd(pmd, address, end - address,
84 phys_addr + address, flags))
87 address = (address + PGDIR_SIZE) & PGDIR_MASK;
89 } while (address && (address < end));
90 spin_unlock(&init_mm.page_table_lock);
96 * Generic mapping function (not visible outside):
100 * Remap an arbitrary physical address space into the kernel virtual
101 * address space. Needed when the kernel wants to access high addresses
104 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
105 * have to convert them into an offset in a page-aligned mapping, but the
106 * caller shouldn't need to know that small detail.
108 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
111 struct vm_struct * area;
112 unsigned long offset, last_addr;
114 /* Don't allow wraparound or zero size */
115 last_addr = phys_addr + size - 1;
116 if (!size || last_addr < phys_addr)
120 /* TODO: Here we can put checks for driver-writer abuse... */
123 * Don't remap the low PCI/ISA area, it's always mapped..
125 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
126 return phys_to_virt(phys_addr);
129 * Don't allow anybody to remap normal RAM that we're using..
131 if (phys_addr < virt_to_phys(high_memory)) {
132 char *t_addr, *t_end;
135 t_addr = __va(phys_addr);
136 t_end = t_addr + (size - 1);
138 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
139 if(!PageReserved(page))
145 * Mappings have to be page-aligned
147 offset = phys_addr & ~PAGE_MASK;
148 phys_addr &= PAGE_MASK;
149 size = PAGE_ALIGN(last_addr+1) - phys_addr;
154 area = get_vm_area(size, VM_IOREMAP);
158 if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
162 return (void *) (offset + (char *)addr);
165 void iounmap(void *addr)
167 if (addr > high_memory)
168 return vfree((void *) (PAGE_MASK & (unsigned long) addr));