Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / arch / sh64 / mm / ioremap.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * arch/sh64/mm/ioremap.c
7  *
8  * Copyright (C) 2000, 2001  Paolo Alberelli
9  * Copyright (C) 2003, 2004  Paul Mundt
10  *
11  * Mostly derived from arch/sh/mm/ioremap.c which, in turn is mostly
12  * derived from arch/i386/mm/ioremap.c .
13  *
14  *   (C) Copyright 1995 1996 Linus Torvalds
15  */
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <asm/io.h>
22 #include <asm/pgalloc.h>
23 #include <asm/tlbflush.h>
24 #include <linux/ioport.h>
25 #include <linux/bootmem.h>
26 #include <linux/proc_fs.h>
27
28 static void shmedia_mapioaddr(unsigned long, unsigned long);
29 static unsigned long shmedia_ioremap(struct resource *, u32, int);
30
31 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
32         unsigned long phys_addr, unsigned long flags)
33 {
34         unsigned long end;
35         unsigned long pfn;
36         pgprot_t pgprot = __pgprot(_PAGE_PRESENT  | _PAGE_READ   |
37                                    _PAGE_WRITE    | _PAGE_DIRTY  |
38                                    _PAGE_ACCESSED | _PAGE_SHARED | flags);
39
40         address &= ~PMD_MASK;
41         end = address + size;
42         if (end > PMD_SIZE)
43                 end = PMD_SIZE;
44         if (address >= end)
45                 BUG();
46
47         pfn = phys_addr >> PAGE_SHIFT;
48
49         pr_debug("    %s: pte %p address %lx size %lx phys_addr %lx\n",
50                  __FUNCTION__,pte,address,size,phys_addr);
51
52         do {
53                 if (!pte_none(*pte)) {
54                         printk("remap_area_pte: page already exists\n");
55                         BUG();
56                 }
57
58                 set_pte(pte, pfn_pte(pfn, pgprot));
59                 address += PAGE_SIZE;
60                 pfn++;
61                 pte++;
62         } while (address && (address < end));
63 }
64
65 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
66         unsigned long phys_addr, unsigned long flags)
67 {
68         unsigned long end;
69
70         address &= ~PGDIR_MASK;
71         end = address + size;
72
73         if (end > PGDIR_SIZE)
74                 end = PGDIR_SIZE;
75
76         phys_addr -= address;
77
78         if (address >= end)
79                 BUG();
80
81         do {
82                 pte_t * pte = pte_alloc_kernel(pmd, address);
83                 if (!pte)
84                         return -ENOMEM;
85                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
86                 address = (address + PMD_SIZE) & PMD_MASK;
87                 pmd++;
88         } while (address && (address < end));
89         return 0;
90 }
91
92 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
93                                  unsigned long size, unsigned long flags)
94 {
95         int error;
96         pgd_t * dir;
97         unsigned long end = address + size;
98
99         phys_addr -= address;
100         dir = pgd_offset_k(address);
101         flush_cache_all();
102         if (address >= end)
103                 BUG();
104         do {
105                 pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
106                 error = -ENOMEM;
107                 if (!pmd)
108                         break;
109                 if (remap_area_pmd(pmd, address, end - address,
110                                    phys_addr + address, flags)) {
111                          break;
112                 }
113                 error = 0;
114                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
115                 dir++;
116         } while (address && (address < end));
117         flush_tlb_all();
118         return 0;
119 }
120
121 /*
122  * Generic mapping function (not visible outside):
123  */
124
125 /*
126  * Remap an arbitrary physical address space into the kernel virtual
127  * address space. Needed when the kernel wants to access high addresses
128  * directly.
129  *
130  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
131  * have to convert them into an offset in a page-aligned mapping, but the
132  * caller shouldn't need to know that small detail.
133  */
134 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
135 {
136         void * addr;
137         struct vm_struct * area;
138         unsigned long offset, last_addr;
139
140         /* Don't allow wraparound or zero size */
141         last_addr = phys_addr + size - 1;
142         if (!size || last_addr < phys_addr)
143                 return NULL;
144
145         /*
146          * Mappings have to be page-aligned
147          */
148         offset = phys_addr & ~PAGE_MASK;
149         phys_addr &= PAGE_MASK;
150         size = PAGE_ALIGN(last_addr + 1) - phys_addr;
151
152         /*
153          * Ok, go for it..
154          */
155         area = get_vm_area(size, VM_IOREMAP);
156         pr_debug("Get vm_area returns %p addr %p\n",area,area->addr);
157         if (!area)
158                 return NULL;
159         area->phys_addr = phys_addr;
160         addr = area->addr;
161         if (remap_area_pages((unsigned long)addr, phys_addr, size, flags)) {
162                 vunmap(addr);
163                 return NULL;
164         }
165         return (void *) (offset + (char *)addr);
166 }
167
168 void iounmap(void *addr)
169 {
170         struct vm_struct *area;
171
172         vfree((void *) (PAGE_MASK & (unsigned long) addr));
173         area = remove_vm_area((void *) (PAGE_MASK & (unsigned long) addr));
174         if (!area) {
175                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
176                 return;
177         }
178
179         kfree(area);
180 }
181
182 static struct resource shmedia_iomap = {
183         .name   = "shmedia_iomap",
184         .start  = IOBASE_VADDR + PAGE_SIZE,
185         .end    = IOBASE_END - 1,
186 };
187
188 static void shmedia_mapioaddr(unsigned long pa, unsigned long va);
189 static void shmedia_unmapioaddr(unsigned long vaddr);
190 static unsigned long shmedia_ioremap(struct resource *res, u32 pa, int sz);
191
192 /*
193  * We have the same problem as the SPARC, so lets have the same comment:
194  * Our mini-allocator...
195  * Boy this is gross! We need it because we must map I/O for
196  * timers and interrupt controller before the kmalloc is available.
197  */
198
199 #define XNMLN  15
200 #define XNRES  10
201
202 struct xresource {
203         struct resource xres;   /* Must be first */
204         int xflag;              /* 1 == used */
205         char xname[XNMLN+1];
206 };
207
208 static struct xresource xresv[XNRES];
209
210 static struct xresource *xres_alloc(void)
211 {
212         struct xresource *xrp;
213         int n;
214
215         xrp = xresv;
216         for (n = 0; n < XNRES; n++) {
217                 if (xrp->xflag == 0) {
218                         xrp->xflag = 1;
219                         return xrp;
220                 }
221                 xrp++;
222         }
223         return NULL;
224 }
225
226 static void xres_free(struct xresource *xrp)
227 {
228         xrp->xflag = 0;
229 }
230
231 static struct resource *shmedia_find_resource(struct resource *root,
232                                               unsigned long vaddr)
233 {
234         struct resource *res;
235
236         for (res = root->child; res; res = res->sibling)
237                 if (res->start <= vaddr && res->end >= vaddr)
238                         return res;
239
240         return NULL;
241 }
242
243 static unsigned long shmedia_alloc_io(unsigned long phys, unsigned long size,
244                                       const char *name)
245 {
246         static int printed_full = 0;
247         struct xresource *xres;
248         struct resource *res;
249         char *tack;
250         int tlen;
251
252         if (name == NULL) name = "???";
253
254         if ((xres = xres_alloc()) != 0) {
255                 tack = xres->xname;
256                 res = &xres->xres;
257         } else {
258                 if (!printed_full) {
259                         printk("%s: done with statics, switching to kmalloc\n",
260                                __FUNCTION__);
261                         printed_full = 1;
262                 }
263                 tlen = strlen(name);
264                 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
265                 if (!tack)
266                         return -ENOMEM;
267                 memset(tack, 0, sizeof(struct resource));
268                 res = (struct resource *) tack;
269                 tack += sizeof (struct resource);
270         }
271
272         strncpy(tack, name, XNMLN);
273         tack[XNMLN] = 0;
274         res->name = tack;
275
276         return shmedia_ioremap(res, phys, size);
277 }
278
279 static unsigned long shmedia_ioremap(struct resource *res, u32 pa, int sz)
280 {
281         unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
282         unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
283         unsigned long va;
284         unsigned int psz;
285
286         if (allocate_resource(&shmedia_iomap, res, round_sz,
287                               shmedia_iomap.start, shmedia_iomap.end,
288                               PAGE_SIZE, NULL, NULL) != 0) {
289                 panic("alloc_io_res(%s): cannot occupy\n",
290                     (res->name != NULL)? res->name: "???");
291         }
292
293         va = res->start;
294         pa &= PAGE_MASK;
295
296         psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
297
298         /* log at boot time ... */
299         printk("mapioaddr: %6s  [%2d page%s]  va 0x%08lx   pa 0x%08x\n",
300                ((res->name != NULL) ? res->name : "???"),
301                psz, psz == 1 ? " " : "s", va, pa);
302
303         for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
304                 shmedia_mapioaddr(pa, va);
305                 va += PAGE_SIZE;
306                 pa += PAGE_SIZE;
307         }
308
309         res->start += offset;
310         res->end = res->start + sz - 1;         /* not strictly necessary.. */
311
312         return res->start;
313 }
314
315 static void shmedia_free_io(struct resource *res)
316 {
317         unsigned long len = res->end - res->start + 1;
318
319         BUG_ON((len & (PAGE_SIZE - 1)) != 0);
320
321         while (len) {
322                 len -= PAGE_SIZE;
323                 shmedia_unmapioaddr(res->start + len);
324         }
325
326         release_resource(res);
327 }
328
329 static void *sh64_get_page(void)
330 {
331         extern int after_bootmem;
332         void *page;
333
334         if (after_bootmem) {
335                 page = (void *)get_zeroed_page(GFP_ATOMIC);
336         } else {
337                 page = alloc_bootmem_pages(PAGE_SIZE);
338         }
339
340         if (!page || ((unsigned long)page & ~PAGE_MASK))
341                 panic("sh64_get_page: Out of memory already?\n");
342
343         return page;
344 }
345
346 static void shmedia_mapioaddr(unsigned long pa, unsigned long va)
347 {
348         pgd_t *pgdp;
349         pmd_t *pmdp;
350         pte_t *ptep, pte;
351         pgprot_t prot;
352         unsigned long flags = 1; /* 1 = CB0-1 device */
353
354         pr_debug("shmedia_mapiopage pa %08lx va %08lx\n",  pa, va);
355
356         pgdp = pgd_offset_k(va);
357         if (pgd_none(*pgdp) || !pgd_present(*pgdp)) {
358                 pmdp = (pmd_t *)sh64_get_page();
359                 set_pgd(pgdp, __pgd((unsigned long)pmdp | _KERNPG_TABLE));
360         }
361
362         pmdp = pmd_offset(pgdp, va);
363         if (pmd_none(*pmdp) || !pmd_present(*pmdp) ) {
364                 ptep = (pte_t *)sh64_get_page();
365                 set_pmd(pmdp, __pmd((unsigned long)ptep + _PAGE_TABLE));
366         }
367
368         prot = __pgprot(_PAGE_PRESENT | _PAGE_READ     | _PAGE_WRITE  |
369                         _PAGE_DIRTY   | _PAGE_ACCESSED | _PAGE_SHARED | flags);
370
371         pte = pfn_pte(pa >> PAGE_SHIFT, prot);
372         ptep = pte_offset_kernel(pmdp, va);
373
374         if (!pte_none(*ptep) &&
375             pte_val(*ptep) != pte_val(pte))
376                 pte_ERROR(*ptep);
377
378         set_pte(ptep, pte);
379
380         flush_tlb_kernel_range(va, PAGE_SIZE);
381 }
382
383 static void shmedia_unmapioaddr(unsigned long vaddr)
384 {
385         pgd_t *pgdp;
386         pmd_t *pmdp;
387         pte_t *ptep;
388
389         pgdp = pgd_offset_k(vaddr);
390         pmdp = pmd_offset(pgdp, vaddr);
391
392         if (pmd_none(*pmdp) || pmd_bad(*pmdp))
393                 return;
394
395         ptep = pte_offset_kernel(pmdp, vaddr);
396
397         if (pte_none(*ptep) || !pte_present(*ptep))
398                 return;
399
400         clear_page((void *)ptep);
401         pte_clear(&init_mm, vaddr, ptep);
402 }
403
404 unsigned long onchip_remap(unsigned long phys, unsigned long size, const char *name)
405 {
406         if (size < PAGE_SIZE)
407                 size = PAGE_SIZE;
408
409         return shmedia_alloc_io(phys, size, name);
410 }
411
412 void onchip_unmap(unsigned long vaddr)
413 {
414         struct resource *res;
415         unsigned int psz;
416
417         res = shmedia_find_resource(&shmedia_iomap, vaddr);
418         if (!res) {
419                 printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
420                        __FUNCTION__, vaddr);
421                 return;
422         }
423
424         psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
425
426         printk(KERN_DEBUG "unmapioaddr: %6s  [%2d page%s] freed\n",
427                res->name, psz, psz == 1 ? " " : "s");
428
429         shmedia_free_io(res);
430
431         if ((char *)res >= (char *)xresv &&
432             (char *)res <  (char *)&xresv[XNRES]) {
433                 xres_free((struct xresource *)res);
434         } else {
435                 kfree(res);
436         }
437 }
438
439 #ifdef CONFIG_PROC_FS
440 static int
441 ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
442                   void *data)
443 {
444         char *p = buf, *e = buf + length;
445         struct resource *r;
446         const char *nm;
447
448         for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
449                 if (p + 32 >= e)        /* Better than nothing */
450                         break;
451                 if ((nm = r->name) == 0) nm = "???";
452                 p += sprintf(p, "%08lx-%08lx: %s\n",
453                              (unsigned long)r->start,
454                              (unsigned long)r->end, nm);
455         }
456
457         return p-buf;
458 }
459 #endif /* CONFIG_PROC_FS */
460
461 static int __init register_proc_onchip(void)
462 {
463 #ifdef CONFIG_PROC_FS
464         create_proc_read_entry("io_map",0,0, ioremap_proc_info, &shmedia_iomap);
465 #endif
466         return 0;
467 }
468
469 __initcall(register_proc_onchip);