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 = (struct vm_struct *)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)
71 addr = tmp->size + (unsigned long)tmp->addr;
73 area->addr = (void *)addr;
74 area->size = size + IO_SIZE;
80 static inline void free_io_area(void *addr)
82 struct vm_struct **p, *tmp;
86 addr = (void *)((unsigned long)addr & -IO_SIZE);
87 for (p = &iolist ; (tmp = *p) ; p = &tmp->next) {
88 if (tmp->addr == addr) {
90 __iounmap(tmp->addr, tmp->size);
100 * Map some physical address range into the kernel address space. The
101 * code is copied and adapted from map_chunk().
103 /* Rewritten by Andreas Schwab to remove all races. */
105 void __iomem *__ioremap(unsigned long physaddr, unsigned long size, int cacheflag)
107 struct vm_struct *area;
108 unsigned long virtaddr, retaddr;
115 * Don't allow mappings that wrap..
117 if (!size || size > physaddr + size)
122 if ((physaddr >= 0x40000000) && (physaddr + size < 0x60000000)
123 && (cacheflag == IOMAP_NOCACHE_SER))
124 return (void __iomem *)physaddr;
129 printk("ioremap: 0x%lx,0x%lx(%d) - ", physaddr, size, cacheflag);
132 * Mappings have to be aligned
134 offset = physaddr & (IO_SIZE - 1);
135 physaddr &= -IO_SIZE;
136 size = (size + offset + IO_SIZE - 1) & -IO_SIZE;
141 area = get_io_area(size);
145 virtaddr = (unsigned long)area->addr;
146 retaddr = virtaddr + offset;
148 printk("0x%lx,0x%lx,0x%lx", physaddr, virtaddr, retaddr);
152 * add cache and table flags to physical address
154 if (CPU_IS_040_OR_060) {
155 physaddr |= (_PAGE_PRESENT | _PAGE_GLOBAL040 |
156 _PAGE_ACCESSED | _PAGE_DIRTY);
158 case IOMAP_FULL_CACHING:
159 physaddr |= _PAGE_CACHE040;
161 case IOMAP_NOCACHE_SER:
163 physaddr |= _PAGE_NOCACHE_S;
165 case IOMAP_NOCACHE_NONSER:
166 physaddr |= _PAGE_NOCACHE;
168 case IOMAP_WRITETHROUGH:
169 physaddr |= _PAGE_CACHE040W;
173 physaddr |= (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
175 case IOMAP_NOCACHE_SER:
176 case IOMAP_NOCACHE_NONSER:
178 physaddr |= _PAGE_NOCACHE030;
180 case IOMAP_FULL_CACHING:
181 case IOMAP_WRITETHROUGH:
186 while ((long)size > 0) {
188 if (!(virtaddr & (PTRTREESIZE-1)))
189 printk ("\npa=%#lx va=%#lx ", physaddr, virtaddr);
191 pgd_dir = pgd_offset_k(virtaddr);
192 pmd_dir = pmd_alloc(&init_mm, pgd_dir, virtaddr);
194 printk("ioremap: no mem for pmd_dir\n");
198 if (CPU_IS_020_OR_030) {
199 pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
200 physaddr += PTRTREESIZE;
201 virtaddr += PTRTREESIZE;
204 pte_dir = pte_alloc_kernel(pmd_dir, virtaddr);
206 printk("ioremap: no mem for pte_dir\n");
210 pte_val(*pte_dir) = physaddr;
211 virtaddr += PAGE_SIZE;
212 physaddr += PAGE_SIZE;
221 return (void __iomem *)retaddr;
223 EXPORT_SYMBOL(__ioremap);
226 * Unmap a ioremap()ed region again
228 void iounmap(void __iomem *addr)
231 if ((!MACH_IS_AMIGA) ||
232 (((unsigned long)addr < 0x40000000) ||
233 ((unsigned long)addr > 0x60000000)))
234 free_io_area((__force void *)addr);
236 free_io_area((__force void *)addr);
239 EXPORT_SYMBOL(iounmap);
242 * __iounmap unmaps nearly everything, so be careful
243 * it doesn't free currently pointer/page tables anymore but it
244 * wans't used anyway and might be added later.
246 void __iounmap(void *addr, unsigned long size)
248 unsigned long virtaddr = (unsigned long)addr;
253 while ((long)size > 0) {
254 pgd_dir = pgd_offset_k(virtaddr);
255 if (pgd_bad(*pgd_dir)) {
256 printk("iounmap: bad pgd(%08lx)\n", pgd_val(*pgd_dir));
260 pmd_dir = pmd_offset(pgd_dir, virtaddr);
262 if (CPU_IS_020_OR_030) {
263 int pmd_off = (virtaddr/PTRTREESIZE) & 15;
264 int pmd_type = pmd_dir->pmd[pmd_off] & _DESCTYPE_MASK;
266 if (pmd_type == _PAGE_PRESENT) {
267 pmd_dir->pmd[pmd_off] = 0;
268 virtaddr += PTRTREESIZE;
271 } else if (pmd_type == 0)
275 if (pmd_bad(*pmd_dir)) {
276 printk("iounmap: bad pmd (%08lx)\n", pmd_val(*pmd_dir));
280 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
282 pte_val(*pte_dir) = 0;
283 virtaddr += PAGE_SIZE;
291 * Set new cache mode for some kernel address space.
292 * The caller must push data for that range itself, if such data may already
295 void kernel_set_cachemode(void *addr, unsigned long size, int cmode)
297 unsigned long virtaddr = (unsigned long)addr;
302 if (CPU_IS_040_OR_060) {
304 case IOMAP_FULL_CACHING:
305 cmode = _PAGE_CACHE040;
307 case IOMAP_NOCACHE_SER:
309 cmode = _PAGE_NOCACHE_S;
311 case IOMAP_NOCACHE_NONSER:
312 cmode = _PAGE_NOCACHE;
314 case IOMAP_WRITETHROUGH:
315 cmode = _PAGE_CACHE040W;
320 case IOMAP_NOCACHE_SER:
321 case IOMAP_NOCACHE_NONSER:
323 cmode = _PAGE_NOCACHE030;
325 case IOMAP_FULL_CACHING:
326 case IOMAP_WRITETHROUGH:
331 while ((long)size > 0) {
332 pgd_dir = pgd_offset_k(virtaddr);
333 if (pgd_bad(*pgd_dir)) {
334 printk("iocachemode: bad pgd(%08lx)\n", pgd_val(*pgd_dir));
338 pmd_dir = pmd_offset(pgd_dir, virtaddr);
340 if (CPU_IS_020_OR_030) {
341 int pmd_off = (virtaddr/PTRTREESIZE) & 15;
343 if ((pmd_dir->pmd[pmd_off] & _DESCTYPE_MASK) == _PAGE_PRESENT) {
344 pmd_dir->pmd[pmd_off] = (pmd_dir->pmd[pmd_off] &
345 _CACHEMASK040) | cmode;
346 virtaddr += PTRTREESIZE;
352 if (pmd_bad(*pmd_dir)) {
353 printk("iocachemode: bad pmd (%08lx)\n", pmd_val(*pmd_dir));
357 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
359 pte_val(*pte_dir) = (pte_val(*pte_dir) & _CACHEMASK040) | cmode;
360 virtaddr += PAGE_SIZE;
366 EXPORT_SYMBOL(kernel_set_cachemode);