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>
15 #include <asm/cacheflush.h>
16 #include <asm/tlbflush.h>
17 #include <asm/arch/memmap.h>
19 extern inline void remap_area_pte(pte_t
* pte
, unsigned long address
, unsigned long size
,
20 unsigned long phys_addr
, pgprot_t prot
)
31 if (!pte_none(*pte
)) {
32 printk("remap_area_pte: page already exists\n");
35 set_pte(pte
, mk_pte_phys(phys_addr
, prot
));
37 phys_addr
+= PAGE_SIZE
;
39 } while (address
&& (address
< end
));
42 static inline int remap_area_pmd(pmd_t
* pmd
, unsigned long address
, unsigned long size
,
43 unsigned long phys_addr
, pgprot_t prot
)
47 address
&= ~PGDIR_MASK
;
55 pte_t
* pte
= pte_alloc_kernel(pmd
, address
);
58 remap_area_pte(pte
, address
, end
- address
, address
+ phys_addr
, prot
);
59 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
61 } while (address
&& (address
< end
));
65 static int remap_area_pages(unsigned long address
, unsigned long phys_addr
,
66 unsigned long size
, pgprot_t prot
)
70 unsigned long end
= address
+ size
;
73 dir
= pgd_offset(&init_mm
, address
);
82 pud
= pud_alloc(&init_mm
, dir
, address
);
85 pmd
= pmd_alloc(&init_mm
, pud
, address
);
89 if (remap_area_pmd(pmd
, address
, end
- address
,
90 phys_addr
+ address
, prot
))
93 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
95 } while (address
&& (address
< end
));
101 * Generic mapping function (not visible outside):
105 * Remap an arbitrary physical address space into the kernel virtual
106 * address space. Needed when the kernel wants to access high addresses
109 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
110 * have to convert them into an offset in a page-aligned mapping, but the
111 * caller shouldn't need to know that small detail.
113 void __iomem
* __ioremap_prot(unsigned long phys_addr
, unsigned long size
, pgprot_t prot
)
116 struct vm_struct
* area
;
117 unsigned long offset
, last_addr
;
119 /* Don't allow wraparound or zero size */
120 last_addr
= phys_addr
+ size
- 1;
121 if (!size
|| last_addr
< phys_addr
)
125 * Mappings have to be page-aligned
127 offset
= phys_addr
& ~PAGE_MASK
;
128 phys_addr
&= PAGE_MASK
;
129 size
= PAGE_ALIGN(last_addr
+1) - phys_addr
;
134 area
= get_vm_area(size
, VM_IOREMAP
);
137 addr
= (void __iomem
*)area
->addr
;
138 if (remap_area_pages((unsigned long) addr
, phys_addr
, size
, prot
)) {
139 vfree((void __force
*)addr
);
142 return (void __iomem
*) (offset
+ (char __iomem
*)addr
);
145 void __iomem
* __ioremap(unsigned long phys_addr
, unsigned long size
, unsigned long flags
)
147 return __ioremap_prot(phys_addr
, size
,
148 __pgprot(_PAGE_PRESENT
| __READABLE
|
149 __WRITEABLE
| _PAGE_GLOBAL
|
150 _PAGE_KERNEL
| flags
));
154 * ioremap_nocache - map bus memory into CPU space
155 * @offset: bus address of the memory
156 * @size: size of the resource to map
158 * Must be freed with iounmap.
161 void __iomem
*ioremap_nocache (unsigned long phys_addr
, unsigned long size
)
163 return __ioremap(phys_addr
| MEM_NON_CACHEABLE
, size
, 0);
166 void iounmap(volatile void __iomem
*addr
)
168 if (addr
> high_memory
)
169 return vfree((void *) (PAGE_MASK
& (unsigned long) addr
));