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