2 * linux/arch/m68k/mm/kmap.c
4 * Copyright (C) 1997 Roman Hodek
6 * 10/01/99 cleaned up the code and changing to the same interface
7 * used by other architectures /Roman Zippel
10 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
18 #include <asm/setup.h>
19 #include <asm/segment.h>
21 #include <asm/pgalloc.h>
23 #include <asm/system.h>
27 #define PTRTREESIZE (256*1024)
30 * For 040/060 we can use the virtual memory area like other architectures,
31 * but for 020/030 we want to use early termination page descriptor and we
32 * can't mix this with normal page descriptors, so we have to copy that code
33 * (mm/vmalloc.c) and return appriorate aligned addresses.
36 #ifdef CPU_M68040_OR_M68060_ONLY
38 #define IO_SIZE PAGE_SIZE
40 static inline struct vm_struct
*get_io_area(unsigned long size
)
42 return get_vm_area(size
, VM_IOREMAP
);
46 static inline void free_io_area(void *addr
)
48 vfree((void *)(PAGE_MASK
& (unsigned long)addr
));
53 #define IO_SIZE (256*1024)
55 static struct vm_struct
*iolist
;
57 static struct vm_struct
*get_io_area(unsigned long size
)
60 struct vm_struct
**p
, *tmp
, *area
;
62 area
= kmalloc(sizeof(*area
), GFP_KERNEL
);
66 for (p
= &iolist
; (tmp
= *p
) ; p
= &tmp
->next
) {
67 if (size
+ addr
< (unsigned long)tmp
->addr
)
69 if (addr
> KMAP_END
-size
) {
73 addr
= tmp
->size
+ (unsigned long)tmp
->addr
;
75 area
->addr
= (void *)addr
;
76 area
->size
= size
+ IO_SIZE
;
82 static inline void free_io_area(void *addr
)
84 struct vm_struct
**p
, *tmp
;
88 addr
= (void *)((unsigned long)addr
& -IO_SIZE
);
89 for (p
= &iolist
; (tmp
= *p
) ; p
= &tmp
->next
) {
90 if (tmp
->addr
== addr
) {
92 __iounmap(tmp
->addr
, tmp
->size
);
102 * Map some physical address range into the kernel address space.
104 /* Rewritten by Andreas Schwab to remove all races. */
106 void __iomem
*__ioremap(unsigned long physaddr
, unsigned long size
, int cacheflag
)
108 struct vm_struct
*area
;
109 unsigned long virtaddr
, retaddr
;
116 * Don't allow mappings that wrap..
118 if (!size
|| physaddr
> (unsigned long)(-size
))
123 if ((physaddr
>= 0x40000000) && (physaddr
+ size
< 0x60000000)
124 && (cacheflag
== IOMAP_NOCACHE_SER
))
125 return (void __iomem
*)physaddr
;
130 printk("ioremap: 0x%lx,0x%lx(%d) - ", physaddr
, size
, cacheflag
);
133 * Mappings have to be aligned
135 offset
= physaddr
& (IO_SIZE
- 1);
136 physaddr
&= -IO_SIZE
;
137 size
= (size
+ offset
+ IO_SIZE
- 1) & -IO_SIZE
;
142 area
= get_io_area(size
);
146 virtaddr
= (unsigned long)area
->addr
;
147 retaddr
= virtaddr
+ offset
;
149 printk("0x%lx,0x%lx,0x%lx", physaddr
, virtaddr
, retaddr
);
153 * add cache and table flags to physical address
155 if (CPU_IS_040_OR_060
) {
156 physaddr
|= (_PAGE_PRESENT
| _PAGE_GLOBAL040
|
157 _PAGE_ACCESSED
| _PAGE_DIRTY
);
159 case IOMAP_FULL_CACHING
:
160 physaddr
|= _PAGE_CACHE040
;
162 case IOMAP_NOCACHE_SER
:
164 physaddr
|= _PAGE_NOCACHE_S
;
166 case IOMAP_NOCACHE_NONSER
:
167 physaddr
|= _PAGE_NOCACHE
;
169 case IOMAP_WRITETHROUGH
:
170 physaddr
|= _PAGE_CACHE040W
;
174 physaddr
|= (_PAGE_PRESENT
| _PAGE_ACCESSED
| _PAGE_DIRTY
);
176 case IOMAP_NOCACHE_SER
:
177 case IOMAP_NOCACHE_NONSER
:
179 physaddr
|= _PAGE_NOCACHE030
;
181 case IOMAP_FULL_CACHING
:
182 case IOMAP_WRITETHROUGH
:
187 while ((long)size
> 0) {
189 if (!(virtaddr
& (PTRTREESIZE
-1)))
190 printk ("\npa=%#lx va=%#lx ", physaddr
, virtaddr
);
192 pgd_dir
= pgd_offset_k(virtaddr
);
193 pmd_dir
= pmd_alloc(&init_mm
, pgd_dir
, virtaddr
);
195 printk("ioremap: no mem for pmd_dir\n");
199 if (CPU_IS_020_OR_030
) {
200 pmd_dir
->pmd
[(virtaddr
/PTRTREESIZE
) & 15] = physaddr
;
201 physaddr
+= PTRTREESIZE
;
202 virtaddr
+= PTRTREESIZE
;
205 pte_dir
= pte_alloc_kernel(pmd_dir
, virtaddr
);
207 printk("ioremap: no mem for pte_dir\n");
211 pte_val(*pte_dir
) = physaddr
;
212 virtaddr
+= PAGE_SIZE
;
213 physaddr
+= PAGE_SIZE
;
222 return (void __iomem
*)retaddr
;
224 EXPORT_SYMBOL(__ioremap
);
227 * Unmap a ioremap()ed region again
229 void iounmap(void __iomem
*addr
)
232 if ((!MACH_IS_AMIGA
) ||
233 (((unsigned long)addr
< 0x40000000) ||
234 ((unsigned long)addr
> 0x60000000)))
235 free_io_area((__force
void *)addr
);
237 free_io_area((__force
void *)addr
);
240 EXPORT_SYMBOL(iounmap
);
243 * __iounmap unmaps nearly everything, so be careful
244 * it doesn't free currently pointer/page tables anymore but it
245 * wans't used anyway and might be added later.
247 void __iounmap(void *addr
, unsigned long size
)
249 unsigned long virtaddr
= (unsigned long)addr
;
254 while ((long)size
> 0) {
255 pgd_dir
= pgd_offset_k(virtaddr
);
256 if (pgd_bad(*pgd_dir
)) {
257 printk("iounmap: bad pgd(%08lx)\n", pgd_val(*pgd_dir
));
261 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
263 if (CPU_IS_020_OR_030
) {
264 int pmd_off
= (virtaddr
/PTRTREESIZE
) & 15;
265 int pmd_type
= pmd_dir
->pmd
[pmd_off
] & _DESCTYPE_MASK
;
267 if (pmd_type
== _PAGE_PRESENT
) {
268 pmd_dir
->pmd
[pmd_off
] = 0;
269 virtaddr
+= PTRTREESIZE
;
272 } else if (pmd_type
== 0)
276 if (pmd_bad(*pmd_dir
)) {
277 printk("iounmap: bad pmd (%08lx)\n", pmd_val(*pmd_dir
));
281 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
283 pte_val(*pte_dir
) = 0;
284 virtaddr
+= PAGE_SIZE
;
292 * Set new cache mode for some kernel address space.
293 * The caller must push data for that range itself, if such data may already
296 void kernel_set_cachemode(void *addr
, unsigned long size
, int cmode
)
298 unsigned long virtaddr
= (unsigned long)addr
;
303 if (CPU_IS_040_OR_060
) {
305 case IOMAP_FULL_CACHING
:
306 cmode
= _PAGE_CACHE040
;
308 case IOMAP_NOCACHE_SER
:
310 cmode
= _PAGE_NOCACHE_S
;
312 case IOMAP_NOCACHE_NONSER
:
313 cmode
= _PAGE_NOCACHE
;
315 case IOMAP_WRITETHROUGH
:
316 cmode
= _PAGE_CACHE040W
;
321 case IOMAP_NOCACHE_SER
:
322 case IOMAP_NOCACHE_NONSER
:
324 cmode
= _PAGE_NOCACHE030
;
326 case IOMAP_FULL_CACHING
:
327 case IOMAP_WRITETHROUGH
:
332 while ((long)size
> 0) {
333 pgd_dir
= pgd_offset_k(virtaddr
);
334 if (pgd_bad(*pgd_dir
)) {
335 printk("iocachemode: bad pgd(%08lx)\n", pgd_val(*pgd_dir
));
339 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
341 if (CPU_IS_020_OR_030
) {
342 int pmd_off
= (virtaddr
/PTRTREESIZE
) & 15;
344 if ((pmd_dir
->pmd
[pmd_off
] & _DESCTYPE_MASK
) == _PAGE_PRESENT
) {
345 pmd_dir
->pmd
[pmd_off
] = (pmd_dir
->pmd
[pmd_off
] &
346 _CACHEMASK040
) | cmode
;
347 virtaddr
+= PTRTREESIZE
;
353 if (pmd_bad(*pmd_dir
)) {
354 printk("iocachemode: bad pmd (%08lx)\n", pmd_val(*pmd_dir
));
358 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
360 pte_val(*pte_dir
) = (pte_val(*pte_dir
) & _CACHEMASK040
) | cmode
;
361 virtaddr
+= PAGE_SIZE
;
367 EXPORT_SYMBOL(kernel_set_cachemode
);