First Support on Ginger and OMAP TI
[linux-ginger.git] / arch / m68k / mm / kmap.c
blobdf620ac2a296b7a7da77a87cf9e00c06ee0786e5
1 /*
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
8 */
10 #include <linux/module.h>
11 #include <linux/mm.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>
20 #include <asm/page.h>
21 #include <asm/pgalloc.h>
22 #include <asm/io.h>
23 #include <asm/system.h>
25 #undef DEBUG
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));
51 #else
53 #define IO_SIZE (256*1024)
55 static struct vm_struct *iolist;
57 static struct vm_struct *get_io_area(unsigned long size)
59 unsigned long addr;
60 struct vm_struct **p, *tmp, *area;
62 area = kmalloc(sizeof(*area), GFP_KERNEL);
63 if (!area)
64 return NULL;
65 addr = KMAP_START;
66 for (p = &iolist; (tmp = *p) ; p = &tmp->next) {
67 if (size + addr < (unsigned long)tmp->addr)
68 break;
69 if (addr > KMAP_END-size) {
70 kfree(area);
71 return NULL;
73 addr = tmp->size + (unsigned long)tmp->addr;
75 area->addr = (void *)addr;
76 area->size = size + IO_SIZE;
77 area->next = *p;
78 *p = area;
79 return area;
82 static inline void free_io_area(void *addr)
84 struct vm_struct **p, *tmp;
86 if (!addr)
87 return;
88 addr = (void *)((unsigned long)addr & -IO_SIZE);
89 for (p = &iolist ; (tmp = *p) ; p = &tmp->next) {
90 if (tmp->addr == addr) {
91 *p = tmp->next;
92 __iounmap(tmp->addr, tmp->size);
93 kfree(tmp);
94 return;
99 #endif
102 * Map some physical address range into the kernel address space. The
103 * code is copied and adapted from map_chunk().
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;
111 long offset;
112 pgd_t *pgd_dir;
113 pmd_t *pmd_dir;
114 pte_t *pte_dir;
117 * Don't allow mappings that wrap..
119 if (!size || size > physaddr + size)
120 return NULL;
122 #ifdef CONFIG_AMIGA
123 if (MACH_IS_AMIGA) {
124 if ((physaddr >= 0x40000000) && (physaddr + size < 0x60000000)
125 && (cacheflag == IOMAP_NOCACHE_SER))
126 return (void __iomem *)physaddr;
128 #endif
130 #ifdef DEBUG
131 printk("ioremap: 0x%lx,0x%lx(%d) - ", physaddr, size, cacheflag);
132 #endif
134 * Mappings have to be aligned
136 offset = physaddr & (IO_SIZE - 1);
137 physaddr &= -IO_SIZE;
138 size = (size + offset + IO_SIZE - 1) & -IO_SIZE;
141 * Ok, go for it..
143 area = get_io_area(size);
144 if (!area)
145 return NULL;
147 virtaddr = (unsigned long)area->addr;
148 retaddr = virtaddr + offset;
149 #ifdef DEBUG
150 printk("0x%lx,0x%lx,0x%lx", physaddr, virtaddr, retaddr);
151 #endif
154 * add cache and table flags to physical address
156 if (CPU_IS_040_OR_060) {
157 physaddr |= (_PAGE_PRESENT | _PAGE_GLOBAL040 |
158 _PAGE_ACCESSED | _PAGE_DIRTY);
159 switch (cacheflag) {
160 case IOMAP_FULL_CACHING:
161 physaddr |= _PAGE_CACHE040;
162 break;
163 case IOMAP_NOCACHE_SER:
164 default:
165 physaddr |= _PAGE_NOCACHE_S;
166 break;
167 case IOMAP_NOCACHE_NONSER:
168 physaddr |= _PAGE_NOCACHE;
169 break;
170 case IOMAP_WRITETHROUGH:
171 physaddr |= _PAGE_CACHE040W;
172 break;
174 } else {
175 physaddr |= (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
176 switch (cacheflag) {
177 case IOMAP_NOCACHE_SER:
178 case IOMAP_NOCACHE_NONSER:
179 default:
180 physaddr |= _PAGE_NOCACHE030;
181 break;
182 case IOMAP_FULL_CACHING:
183 case IOMAP_WRITETHROUGH:
184 break;
188 while ((long)size > 0) {
189 #ifdef DEBUG
190 if (!(virtaddr & (PTRTREESIZE-1)))
191 printk ("\npa=%#lx va=%#lx ", physaddr, virtaddr);
192 #endif
193 pgd_dir = pgd_offset_k(virtaddr);
194 pmd_dir = pmd_alloc(&init_mm, pgd_dir, virtaddr);
195 if (!pmd_dir) {
196 printk("ioremap: no mem for pmd_dir\n");
197 return NULL;
200 if (CPU_IS_020_OR_030) {
201 pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
202 physaddr += PTRTREESIZE;
203 virtaddr += PTRTREESIZE;
204 size -= PTRTREESIZE;
205 } else {
206 pte_dir = pte_alloc_kernel(pmd_dir, virtaddr);
207 if (!pte_dir) {
208 printk("ioremap: no mem for pte_dir\n");
209 return NULL;
212 pte_val(*pte_dir) = physaddr;
213 virtaddr += PAGE_SIZE;
214 physaddr += PAGE_SIZE;
215 size -= PAGE_SIZE;
218 #ifdef DEBUG
219 printk("\n");
220 #endif
221 flush_tlb_all();
223 return (void __iomem *)retaddr;
225 EXPORT_SYMBOL(__ioremap);
228 * Unmap a ioremap()ed region again
230 void iounmap(void __iomem *addr)
232 #ifdef CONFIG_AMIGA
233 if ((!MACH_IS_AMIGA) ||
234 (((unsigned long)addr < 0x40000000) ||
235 ((unsigned long)addr > 0x60000000)))
236 free_io_area((__force void *)addr);
237 #else
238 free_io_area((__force void *)addr);
239 #endif
241 EXPORT_SYMBOL(iounmap);
244 * __iounmap unmaps nearly everything, so be careful
245 * it doesn't free currently pointer/page tables anymore but it
246 * wans't used anyway and might be added later.
248 void __iounmap(void *addr, unsigned long size)
250 unsigned long virtaddr = (unsigned long)addr;
251 pgd_t *pgd_dir;
252 pmd_t *pmd_dir;
253 pte_t *pte_dir;
255 while ((long)size > 0) {
256 pgd_dir = pgd_offset_k(virtaddr);
257 if (pgd_bad(*pgd_dir)) {
258 printk("iounmap: bad pgd(%08lx)\n", pgd_val(*pgd_dir));
259 pgd_clear(pgd_dir);
260 return;
262 pmd_dir = pmd_offset(pgd_dir, virtaddr);
264 if (CPU_IS_020_OR_030) {
265 int pmd_off = (virtaddr/PTRTREESIZE) & 15;
266 int pmd_type = pmd_dir->pmd[pmd_off] & _DESCTYPE_MASK;
268 if (pmd_type == _PAGE_PRESENT) {
269 pmd_dir->pmd[pmd_off] = 0;
270 virtaddr += PTRTREESIZE;
271 size -= PTRTREESIZE;
272 continue;
273 } else if (pmd_type == 0)
274 continue;
277 if (pmd_bad(*pmd_dir)) {
278 printk("iounmap: bad pmd (%08lx)\n", pmd_val(*pmd_dir));
279 pmd_clear(pmd_dir);
280 return;
282 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
284 pte_val(*pte_dir) = 0;
285 virtaddr += PAGE_SIZE;
286 size -= PAGE_SIZE;
289 flush_tlb_all();
293 * Set new cache mode for some kernel address space.
294 * The caller must push data for that range itself, if such data may already
295 * be in the cache.
297 void kernel_set_cachemode(void *addr, unsigned long size, int cmode)
299 unsigned long virtaddr = (unsigned long)addr;
300 pgd_t *pgd_dir;
301 pmd_t *pmd_dir;
302 pte_t *pte_dir;
304 if (CPU_IS_040_OR_060) {
305 switch (cmode) {
306 case IOMAP_FULL_CACHING:
307 cmode = _PAGE_CACHE040;
308 break;
309 case IOMAP_NOCACHE_SER:
310 default:
311 cmode = _PAGE_NOCACHE_S;
312 break;
313 case IOMAP_NOCACHE_NONSER:
314 cmode = _PAGE_NOCACHE;
315 break;
316 case IOMAP_WRITETHROUGH:
317 cmode = _PAGE_CACHE040W;
318 break;
320 } else {
321 switch (cmode) {
322 case IOMAP_NOCACHE_SER:
323 case IOMAP_NOCACHE_NONSER:
324 default:
325 cmode = _PAGE_NOCACHE030;
326 break;
327 case IOMAP_FULL_CACHING:
328 case IOMAP_WRITETHROUGH:
329 cmode = 0;
333 while ((long)size > 0) {
334 pgd_dir = pgd_offset_k(virtaddr);
335 if (pgd_bad(*pgd_dir)) {
336 printk("iocachemode: bad pgd(%08lx)\n", pgd_val(*pgd_dir));
337 pgd_clear(pgd_dir);
338 return;
340 pmd_dir = pmd_offset(pgd_dir, virtaddr);
342 if (CPU_IS_020_OR_030) {
343 int pmd_off = (virtaddr/PTRTREESIZE) & 15;
345 if ((pmd_dir->pmd[pmd_off] & _DESCTYPE_MASK) == _PAGE_PRESENT) {
346 pmd_dir->pmd[pmd_off] = (pmd_dir->pmd[pmd_off] &
347 _CACHEMASK040) | cmode;
348 virtaddr += PTRTREESIZE;
349 size -= PTRTREESIZE;
350 continue;
354 if (pmd_bad(*pmd_dir)) {
355 printk("iocachemode: bad pmd (%08lx)\n", pmd_val(*pmd_dir));
356 pmd_clear(pmd_dir);
357 return;
359 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
361 pte_val(*pte_dir) = (pte_val(*pte_dir) & _CACHEMASK040) | cmode;
362 virtaddr += PAGE_SIZE;
363 size -= PAGE_SIZE;
366 flush_tlb_all();
368 EXPORT_SYMBOL(kernel_set_cachemode);