1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/m68k/mm/kmap.c
5 * Copyright (C) 1997 Roman Hodek
7 * 10/01/99 cleaned up the code and changing to the same interface
8 * used by other architectures /Roman Zippel
11 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
19 #include <asm/setup.h>
20 #include <asm/segment.h>
22 #include <asm/pgalloc.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 descriptors and we
32 * can't mix this with normal page descriptors, so we have to copy that code
33 * (mm/vmalloc.c) and return appropriately 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 /* remove gap added in get_io_area() */
93 __iounmap(tmp
->addr
, tmp
->size
- IO_SIZE
);
103 * Map some physical address range into the kernel address space.
105 /* Rewritten by Andreas Schwab to remove all races. */
107 void __iomem
*__ioremap(unsigned long physaddr
, unsigned long size
, int cacheflag
)
109 struct vm_struct
*area
;
110 unsigned long virtaddr
, retaddr
;
117 * Don't allow mappings that wrap..
119 if (!size
|| physaddr
> (unsigned long)(-size
))
124 if ((physaddr
>= 0x40000000) && (physaddr
+ size
< 0x60000000)
125 && (cacheflag
== IOMAP_NOCACHE_SER
))
126 return (void __iomem
*)physaddr
;
129 #ifdef CONFIG_COLDFIRE
130 if (__cf_internalio(physaddr
))
131 return (void __iomem
*) physaddr
;
135 printk("ioremap: 0x%lx,0x%lx(%d) - ", physaddr
, size
, cacheflag
);
138 * Mappings have to be aligned
140 offset
= physaddr
& (IO_SIZE
- 1);
141 physaddr
&= -IO_SIZE
;
142 size
= (size
+ offset
+ IO_SIZE
- 1) & -IO_SIZE
;
147 area
= get_io_area(size
);
151 virtaddr
= (unsigned long)area
->addr
;
152 retaddr
= virtaddr
+ offset
;
154 printk("0x%lx,0x%lx,0x%lx", physaddr
, virtaddr
, retaddr
);
158 * add cache and table flags to physical address
160 if (CPU_IS_040_OR_060
) {
161 physaddr
|= (_PAGE_PRESENT
| _PAGE_GLOBAL040
|
162 _PAGE_ACCESSED
| _PAGE_DIRTY
);
164 case IOMAP_FULL_CACHING
:
165 physaddr
|= _PAGE_CACHE040
;
167 case IOMAP_NOCACHE_SER
:
169 physaddr
|= _PAGE_NOCACHE_S
;
171 case IOMAP_NOCACHE_NONSER
:
172 physaddr
|= _PAGE_NOCACHE
;
174 case IOMAP_WRITETHROUGH
:
175 physaddr
|= _PAGE_CACHE040W
;
179 physaddr
|= (_PAGE_PRESENT
| _PAGE_ACCESSED
|
180 _PAGE_DIRTY
| _PAGE_READWRITE
);
182 case IOMAP_NOCACHE_SER
:
183 case IOMAP_NOCACHE_NONSER
:
185 physaddr
|= _PAGE_NOCACHE030
;
187 case IOMAP_FULL_CACHING
:
188 case IOMAP_WRITETHROUGH
:
193 while ((long)size
> 0) {
195 if (!(virtaddr
& (PTRTREESIZE
-1)))
196 printk ("\npa=%#lx va=%#lx ", physaddr
, virtaddr
);
198 pgd_dir
= pgd_offset_k(virtaddr
);
199 pmd_dir
= pmd_alloc(&init_mm
, pgd_dir
, virtaddr
);
201 printk("ioremap: no mem for pmd_dir\n");
205 if (CPU_IS_020_OR_030
) {
206 pmd_dir
->pmd
[(virtaddr
/PTRTREESIZE
) & 15] = physaddr
;
207 physaddr
+= PTRTREESIZE
;
208 virtaddr
+= PTRTREESIZE
;
211 pte_dir
= pte_alloc_kernel(pmd_dir
, virtaddr
);
213 printk("ioremap: no mem for pte_dir\n");
217 pte_val(*pte_dir
) = physaddr
;
218 virtaddr
+= PAGE_SIZE
;
219 physaddr
+= PAGE_SIZE
;
228 return (void __iomem
*)retaddr
;
230 EXPORT_SYMBOL(__ioremap
);
233 * Unmap an ioremap()ed region again
235 void iounmap(void __iomem
*addr
)
238 if ((!MACH_IS_AMIGA
) ||
239 (((unsigned long)addr
< 0x40000000) ||
240 ((unsigned long)addr
> 0x60000000)))
241 free_io_area((__force
void *)addr
);
243 #ifdef CONFIG_COLDFIRE
244 if (cf_internalio(addr
))
247 free_io_area((__force
void *)addr
);
250 EXPORT_SYMBOL(iounmap
);
253 * __iounmap unmaps nearly everything, so be careful
254 * Currently it doesn't free pointer/page tables anymore but this
255 * wasn't used anyway and might be added later.
257 void __iounmap(void *addr
, unsigned long size
)
259 unsigned long virtaddr
= (unsigned long)addr
;
264 while ((long)size
> 0) {
265 pgd_dir
= pgd_offset_k(virtaddr
);
266 if (pgd_bad(*pgd_dir
)) {
267 printk("iounmap: bad pgd(%08lx)\n", pgd_val(*pgd_dir
));
271 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
273 if (CPU_IS_020_OR_030
) {
274 int pmd_off
= (virtaddr
/PTRTREESIZE
) & 15;
275 int pmd_type
= pmd_dir
->pmd
[pmd_off
] & _DESCTYPE_MASK
;
277 if (pmd_type
== _PAGE_PRESENT
) {
278 pmd_dir
->pmd
[pmd_off
] = 0;
279 virtaddr
+= PTRTREESIZE
;
282 } else if (pmd_type
== 0)
286 if (pmd_bad(*pmd_dir
)) {
287 printk("iounmap: bad pmd (%08lx)\n", pmd_val(*pmd_dir
));
291 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
293 pte_val(*pte_dir
) = 0;
294 virtaddr
+= PAGE_SIZE
;
302 * Set new cache mode for some kernel address space.
303 * The caller must push data for that range itself, if such data may already
306 void kernel_set_cachemode(void *addr
, unsigned long size
, int cmode
)
308 unsigned long virtaddr
= (unsigned long)addr
;
313 if (CPU_IS_040_OR_060
) {
315 case IOMAP_FULL_CACHING
:
316 cmode
= _PAGE_CACHE040
;
318 case IOMAP_NOCACHE_SER
:
320 cmode
= _PAGE_NOCACHE_S
;
322 case IOMAP_NOCACHE_NONSER
:
323 cmode
= _PAGE_NOCACHE
;
325 case IOMAP_WRITETHROUGH
:
326 cmode
= _PAGE_CACHE040W
;
331 case IOMAP_NOCACHE_SER
:
332 case IOMAP_NOCACHE_NONSER
:
334 cmode
= _PAGE_NOCACHE030
;
336 case IOMAP_FULL_CACHING
:
337 case IOMAP_WRITETHROUGH
:
342 while ((long)size
> 0) {
343 pgd_dir
= pgd_offset_k(virtaddr
);
344 if (pgd_bad(*pgd_dir
)) {
345 printk("iocachemode: bad pgd(%08lx)\n", pgd_val(*pgd_dir
));
349 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
351 if (CPU_IS_020_OR_030
) {
352 int pmd_off
= (virtaddr
/PTRTREESIZE
) & 15;
354 if ((pmd_dir
->pmd
[pmd_off
] & _DESCTYPE_MASK
) == _PAGE_PRESENT
) {
355 pmd_dir
->pmd
[pmd_off
] = (pmd_dir
->pmd
[pmd_off
] &
356 _CACHEMASK040
) | cmode
;
357 virtaddr
+= PTRTREESIZE
;
363 if (pmd_bad(*pmd_dir
)) {
364 printk("iocachemode: bad pmd (%08lx)\n", pmd_val(*pmd_dir
));
368 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
370 pte_val(*pte_dir
) = (pte_val(*pte_dir
) & _CACHEMASK040
) | cmode
;
371 virtaddr
+= PAGE_SIZE
;
377 EXPORT_SYMBOL(kernel_set_cachemode
);