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
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
17 #include <asm/setup.h>
18 #include <asm/segment.h>
20 #include <asm/pgalloc.h>
22 #include <asm/system.h>
26 #define PTRTREESIZE (256*1024)
29 * For 040/060 we can use the virtual memory area like other architectures,
30 * but for 020/030 we want to use early termination page descriptor and we
31 * can't mix this with normal page descriptors, so we have to copy that code
32 * (mm/vmalloc.c) and return appriorate aligned addresses.
35 #ifdef CPU_M68040_OR_M68060_ONLY
37 #define IO_SIZE PAGE_SIZE
39 static inline struct vm_struct
*get_io_area(unsigned long size
)
41 return get_vm_area(size
, VM_IOREMAP
);
45 static inline void free_io_area(void *addr
)
47 vfree((void *)(PAGE_MASK
& (unsigned long)addr
));
52 #define IO_SIZE (256*1024)
54 static struct vm_struct
*iolist
;
56 static struct vm_struct
*get_io_area(unsigned long size
)
59 struct vm_struct
**p
, *tmp
, *area
;
61 area
= (struct vm_struct
*)kmalloc(sizeof(*area
), GFP_KERNEL
);
65 for (p
= &iolist
; (tmp
= *p
) ; p
= &tmp
->next
) {
66 if (size
+ addr
< (unsigned long)tmp
->addr
)
68 if (addr
> KMAP_END
-size
)
70 addr
= tmp
->size
+ (unsigned long)tmp
->addr
;
72 area
->addr
= (void *)addr
;
73 area
->size
= size
+ IO_SIZE
;
79 static inline void free_io_area(void *addr
)
81 struct vm_struct
**p
, *tmp
;
85 addr
= (void *)((unsigned long)addr
& -IO_SIZE
);
86 for (p
= &iolist
; (tmp
= *p
) ; p
= &tmp
->next
) {
87 if (tmp
->addr
== addr
) {
89 __iounmap(tmp
->addr
, tmp
->size
);
99 * Map some physical address range into the kernel address space. The
100 * code is copied and adapted from map_chunk().
102 /* Rewritten by Andreas Schwab to remove all races. */
104 void __iomem
*__ioremap(unsigned long physaddr
, unsigned long size
, int cacheflag
)
106 struct vm_struct
*area
;
107 unsigned long virtaddr
, retaddr
;
114 * Don't allow mappings that wrap..
116 if (!size
|| size
> physaddr
+ size
)
121 if ((physaddr
>= 0x40000000) && (physaddr
+ size
< 0x60000000)
122 && (cacheflag
== IOMAP_NOCACHE_SER
))
123 return (void __iomem
*)physaddr
;
128 printk("ioremap: 0x%lx,0x%lx(%d) - ", physaddr
, size
, cacheflag
);
131 * Mappings have to be aligned
133 offset
= physaddr
& (IO_SIZE
- 1);
134 physaddr
&= -IO_SIZE
;
135 size
= (size
+ offset
+ IO_SIZE
- 1) & -IO_SIZE
;
140 area
= get_io_area(size
);
144 virtaddr
= (unsigned long)area
->addr
;
145 retaddr
= virtaddr
+ offset
;
147 printk("0x%lx,0x%lx,0x%lx", physaddr
, virtaddr
, retaddr
);
151 * add cache and table flags to physical address
153 if (CPU_IS_040_OR_060
) {
154 physaddr
|= (_PAGE_PRESENT
| _PAGE_GLOBAL040
|
155 _PAGE_ACCESSED
| _PAGE_DIRTY
);
157 case IOMAP_FULL_CACHING
:
158 physaddr
|= _PAGE_CACHE040
;
160 case IOMAP_NOCACHE_SER
:
162 physaddr
|= _PAGE_NOCACHE_S
;
164 case IOMAP_NOCACHE_NONSER
:
165 physaddr
|= _PAGE_NOCACHE
;
167 case IOMAP_WRITETHROUGH
:
168 physaddr
|= _PAGE_CACHE040W
;
172 physaddr
|= (_PAGE_PRESENT
| _PAGE_ACCESSED
| _PAGE_DIRTY
);
174 case IOMAP_NOCACHE_SER
:
175 case IOMAP_NOCACHE_NONSER
:
177 physaddr
|= _PAGE_NOCACHE030
;
179 case IOMAP_FULL_CACHING
:
180 case IOMAP_WRITETHROUGH
:
185 while ((long)size
> 0) {
187 if (!(virtaddr
& (PTRTREESIZE
-1)))
188 printk ("\npa=%#lx va=%#lx ", physaddr
, virtaddr
);
190 pgd_dir
= pgd_offset_k(virtaddr
);
191 pmd_dir
= pmd_alloc(&init_mm
, pgd_dir
, virtaddr
);
193 printk("ioremap: no mem for pmd_dir\n");
197 if (CPU_IS_020_OR_030
) {
198 pmd_dir
->pmd
[(virtaddr
/PTRTREESIZE
) & 15] = physaddr
;
199 physaddr
+= PTRTREESIZE
;
200 virtaddr
+= PTRTREESIZE
;
203 pte_dir
= pte_alloc_kernel(pmd_dir
, virtaddr
);
205 printk("ioremap: no mem for pte_dir\n");
209 pte_val(*pte_dir
) = physaddr
;
210 virtaddr
+= PAGE_SIZE
;
211 physaddr
+= PAGE_SIZE
;
220 return (void __iomem
*)retaddr
;
224 * Unmap a ioremap()ed region again
226 void iounmap(void __iomem
*addr
)
229 if ((!MACH_IS_AMIGA
) ||
230 (((unsigned long)addr
< 0x40000000) ||
231 ((unsigned long)addr
> 0x60000000)))
232 free_io_area((__force
void *)addr
);
234 free_io_area((__force
void *)addr
);
239 * __iounmap unmaps nearly everything, so be careful
240 * it doesn't free currently pointer/page tables anymore but it
241 * wans't used anyway and might be added later.
243 void __iounmap(void *addr
, unsigned long size
)
245 unsigned long virtaddr
= (unsigned long)addr
;
250 while ((long)size
> 0) {
251 pgd_dir
= pgd_offset_k(virtaddr
);
252 if (pgd_bad(*pgd_dir
)) {
253 printk("iounmap: bad pgd(%08lx)\n", pgd_val(*pgd_dir
));
257 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
259 if (CPU_IS_020_OR_030
) {
260 int pmd_off
= (virtaddr
/PTRTREESIZE
) & 15;
261 int pmd_type
= pmd_dir
->pmd
[pmd_off
] & _DESCTYPE_MASK
;
263 if (pmd_type
== _PAGE_PRESENT
) {
264 pmd_dir
->pmd
[pmd_off
] = 0;
265 virtaddr
+= PTRTREESIZE
;
268 } else if (pmd_type
== 0)
272 if (pmd_bad(*pmd_dir
)) {
273 printk("iounmap: bad pmd (%08lx)\n", pmd_val(*pmd_dir
));
277 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
279 pte_val(*pte_dir
) = 0;
280 virtaddr
+= PAGE_SIZE
;
288 * Set new cache mode for some kernel address space.
289 * The caller must push data for that range itself, if such data may already
292 void kernel_set_cachemode(void *addr
, unsigned long size
, int cmode
)
294 unsigned long virtaddr
= (unsigned long)addr
;
299 if (CPU_IS_040_OR_060
) {
301 case IOMAP_FULL_CACHING
:
302 cmode
= _PAGE_CACHE040
;
304 case IOMAP_NOCACHE_SER
:
306 cmode
= _PAGE_NOCACHE_S
;
308 case IOMAP_NOCACHE_NONSER
:
309 cmode
= _PAGE_NOCACHE
;
311 case IOMAP_WRITETHROUGH
:
312 cmode
= _PAGE_CACHE040W
;
317 case IOMAP_NOCACHE_SER
:
318 case IOMAP_NOCACHE_NONSER
:
320 cmode
= _PAGE_NOCACHE030
;
322 case IOMAP_FULL_CACHING
:
323 case IOMAP_WRITETHROUGH
:
328 while ((long)size
> 0) {
329 pgd_dir
= pgd_offset_k(virtaddr
);
330 if (pgd_bad(*pgd_dir
)) {
331 printk("iocachemode: bad pgd(%08lx)\n", pgd_val(*pgd_dir
));
335 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
337 if (CPU_IS_020_OR_030
) {
338 int pmd_off
= (virtaddr
/PTRTREESIZE
) & 15;
340 if ((pmd_dir
->pmd
[pmd_off
] & _DESCTYPE_MASK
) == _PAGE_PRESENT
) {
341 pmd_dir
->pmd
[pmd_off
] = (pmd_dir
->pmd
[pmd_off
] &
342 _CACHEMASK040
) | cmode
;
343 virtaddr
+= PTRTREESIZE
;
349 if (pmd_bad(*pmd_dir
)) {
350 printk("iocachemode: bad pmd (%08lx)\n", pmd_val(*pmd_dir
));
354 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
356 pte_val(*pte_dir
) = (pte_val(*pte_dir
) & _CACHEMASK040
) | cmode
;
357 virtaddr
+= PAGE_SIZE
;