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