2 * Copyright (C) 2004-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #include <linux/vmalloc.h>
9 #include <linux/module.h>
12 #include <asm/pgtable.h>
13 #include <asm/cacheflush.h>
14 #include <asm/tlbflush.h>
15 #include <asm/addrspace.h>
17 static inline int remap_area_pte(pte_t
*pte
, unsigned long address
,
18 unsigned long end
, unsigned long phys_addr
,
23 pfn
= phys_addr
>> PAGE_SHIFT
;
25 WARN_ON(!pte_none(*pte
));
27 set_pte(pte
, pfn_pte(pfn
, prot
));
31 } while (address
&& (address
< end
));
36 static inline int remap_area_pmd(pmd_t
*pmd
, unsigned long address
,
37 unsigned long end
, unsigned long phys_addr
,
45 pte_t
*pte
= pte_alloc_kernel(pmd
, address
);
49 next
= (address
+ PMD_SIZE
) & PMD_MASK
;
50 if (remap_area_pte(pte
, address
, next
,
51 address
+ phys_addr
, prot
))
56 } while (address
&& (address
< end
));
60 static int remap_area_pud(pud_t
*pud
, unsigned long address
,
61 unsigned long end
, unsigned long phys_addr
,
69 pmd_t
*pmd
= pmd_alloc(&init_mm
, pud
, address
);
72 next
= (address
+ PUD_SIZE
) & PUD_MASK
;
73 if (remap_area_pmd(pmd
, address
, next
,
74 phys_addr
+ address
, prot
))
79 } while (address
&& address
< end
);
84 static int remap_area_pages(unsigned long address
, unsigned long phys_addr
,
85 size_t size
, pgprot_t prot
)
87 unsigned long end
= address
+ size
;
94 pgd
= pgd_offset_k(address
);
96 BUG_ON(address
>= end
);
98 spin_lock(&init_mm
.page_table_lock
);
100 pud_t
*pud
= pud_alloc(&init_mm
, pgd
, address
);
106 next
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
107 if (next
< address
|| next
> end
)
109 err
= remap_area_pud(pud
, address
, next
,
110 phys_addr
+ address
, prot
);
116 } while (address
&& (address
< end
));
118 spin_unlock(&init_mm
.page_table_lock
);
124 * Re-map an arbitrary physical address space into the kernel virtual
125 * address space. Needed when the kernel wants to access physical
128 void __iomem
*__ioremap(unsigned long phys_addr
, size_t size
,
132 struct vm_struct
*area
;
133 unsigned long offset
, last_addr
;
137 * Check if we can simply use the P4 segment. This area is
138 * uncacheable, so if caching/buffering is requested, we can't
141 if ((phys_addr
>= P4SEG
) && (flags
== 0))
142 return (void __iomem
*)phys_addr
;
144 /* Don't allow wraparound or zero size */
145 last_addr
= phys_addr
+ size
- 1;
146 if (!size
|| last_addr
< phys_addr
)
150 * XXX: When mapping regular RAM, we'd better make damn sure
151 * it's never used for anything else. But this is really the
152 * caller's responsibility...
154 if (PHYSADDR(P2SEGADDR(phys_addr
)) == phys_addr
)
155 return (void __iomem
*)P2SEGADDR(phys_addr
);
157 /* Mappings have to be page-aligned */
158 offset
= phys_addr
& ~PAGE_MASK
;
159 phys_addr
&= PAGE_MASK
;
160 size
= PAGE_ALIGN(last_addr
+ 1) - phys_addr
;
162 prot
= __pgprot(_PAGE_PRESENT
| _PAGE_RW
| _PAGE_DIRTY
163 | _PAGE_ACCESSED
| _PAGE_TYPE_SMALL
| flags
);
168 area
= get_vm_area(size
, VM_IOREMAP
);
171 area
->phys_addr
= phys_addr
;
173 if (remap_area_pages((unsigned long)addr
, phys_addr
, size
, prot
)) {
178 return (void __iomem
*)(offset
+ (char *)addr
);
180 EXPORT_SYMBOL(__ioremap
);
182 void __iounmap(void __iomem
*addr
)
186 if ((unsigned long)addr
>= P4SEG
)
189 p
= remove_vm_area((void *)(PAGE_MASK
& (unsigned long __force
)addr
));
191 printk (KERN_ERR
"iounmap: bad address %p\n", addr
);
197 EXPORT_SYMBOL(__iounmap
);