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
6 * arch/sh64/mm/ioremap.c
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 * Copyright (C) 2003, 2004 Paul Mundt
11 * Mostly derived from arch/sh/mm/ioremap.c which, in turn is mostly
12 * derived from arch/i386/mm/ioremap.c .
14 * (C) Copyright 1995 1996 Linus Torvalds
16 #include <linux/vmalloc.h>
17 #include <linux/ioport.h>
18 #include <linux/module.h>
22 #include <asm/pgalloc.h>
23 #include <asm/addrspace.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
28 static void shmedia_mapioaddr(unsigned long, unsigned long);
29 static unsigned long shmedia_ioremap(struct resource *, u32, int);
32 * Generic mapping function (not visible outside):
36 * Remap an arbitrary physical address space into the kernel virtual
37 * address space. Needed when the kernel wants to access high addresses
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.
44 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
47 struct vm_struct * area;
48 unsigned long offset, last_addr;
51 /* Don't allow wraparound or zero size */
52 last_addr = phys_addr + size - 1;
53 if (!size || last_addr < phys_addr)
56 pgprot = __pgprot(_PAGE_PRESENT | _PAGE_READ |
57 _PAGE_WRITE | _PAGE_DIRTY |
58 _PAGE_ACCESSED | _PAGE_SHARED | flags);
61 * Mappings have to be page-aligned
63 offset = phys_addr & ~PAGE_MASK;
64 phys_addr &= PAGE_MASK;
65 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
70 area = get_vm_area(size, VM_IOREMAP);
71 pr_debug("Get vm_area returns %p addr %p\n",area,area->addr);
74 area->phys_addr = phys_addr;
76 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
81 return (void *) (offset + (char *)addr);
83 EXPORT_SYMBOL(__ioremap);
85 void iounmap(void *addr)
87 struct vm_struct *area;
89 vfree((void *) (PAGE_MASK & (unsigned long) addr));
90 area = remove_vm_area((void *) (PAGE_MASK & (unsigned long) addr));
92 printk(KERN_ERR "iounmap: bad address %p\n", addr);
98 EXPORT_SYMBOL(iounmap);
100 static struct resource shmedia_iomap = {
101 .name = "shmedia_iomap",
102 .start = IOBASE_VADDR + PAGE_SIZE,
103 .end = IOBASE_END - 1,
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);
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.
121 struct resource xres; /* Must be first */
122 int xflag; /* 1 == used */
126 static struct xresource xresv[XNRES];
128 static struct xresource *xres_alloc(void)
130 struct xresource *xrp;
134 for (n = 0; n < XNRES; n++) {
135 if (xrp->xflag == 0) {
144 static void xres_free(struct xresource *xrp)
149 static struct resource *shmedia_find_resource(struct resource *root,
152 struct resource *res;
154 for (res = root->child; res; res = res->sibling)
155 if (res->start <= vaddr && res->end >= vaddr)
161 static unsigned long shmedia_alloc_io(unsigned long phys, unsigned long size,
164 static int printed_full = 0;
165 struct xresource *xres;
166 struct resource *res;
170 if (name == NULL) name = "???";
172 if ((xres = xres_alloc()) != 0) {
177 printk("%s: done with statics, switching to kmalloc\n",
182 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
185 memset(tack, 0, sizeof(struct resource));
186 res = (struct resource *) tack;
187 tack += sizeof (struct resource);
190 strncpy(tack, name, XNMLN);
194 return shmedia_ioremap(res, phys, size);
197 static unsigned long shmedia_ioremap(struct resource *res, u32 pa, int sz)
199 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
200 unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
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: "???");
214 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
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);
221 for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
222 shmedia_mapioaddr(pa, va);
227 res->start += offset;
228 res->end = res->start + sz - 1; /* not strictly necessary.. */
233 static void shmedia_free_io(struct resource *res)
235 unsigned long len = res->end - res->start + 1;
237 BUG_ON((len & (PAGE_SIZE - 1)) != 0);
241 shmedia_unmapioaddr(res->start + len);
244 release_resource(res);
247 static __init_refok void *sh64_get_page(void)
249 extern int after_bootmem;
253 page = (void *)get_zeroed_page(GFP_ATOMIC);
255 page = alloc_bootmem_pages(PAGE_SIZE);
258 if (!page || ((unsigned long)page & ~PAGE_MASK))
259 panic("sh64_get_page: Out of memory already?\n");
264 static void shmedia_mapioaddr(unsigned long pa, unsigned long va)
270 unsigned long flags = 1; /* 1 = CB0-1 device */
272 pr_debug("shmedia_mapiopage pa %08lx va %08lx\n", pa, va);
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));
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));
286 prot = __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE |
287 _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SHARED | flags);
289 pte = pfn_pte(pa >> PAGE_SHIFT, prot);
290 ptep = pte_offset_kernel(pmdp, va);
292 if (!pte_none(*ptep) &&
293 pte_val(*ptep) != pte_val(pte))
298 flush_tlb_kernel_range(va, PAGE_SIZE);
301 static void shmedia_unmapioaddr(unsigned long vaddr)
307 pgdp = pgd_offset_k(vaddr);
308 pmdp = pmd_offset(pgdp, vaddr);
310 if (pmd_none(*pmdp) || pmd_bad(*pmdp))
313 ptep = pte_offset_kernel(pmdp, vaddr);
315 if (pte_none(*ptep) || !pte_present(*ptep))
318 clear_page((void *)ptep);
319 pte_clear(&init_mm, vaddr, ptep);
322 unsigned long onchip_remap(unsigned long phys, unsigned long size, const char *name)
324 if (size < PAGE_SIZE)
327 return shmedia_alloc_io(phys, size, name);
330 void onchip_unmap(unsigned long vaddr)
332 struct resource *res;
335 res = shmedia_find_resource(&shmedia_iomap, vaddr);
337 printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
338 __FUNCTION__, vaddr);
342 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
344 printk(KERN_DEBUG "unmapioaddr: %6s [%2d page%s] freed\n",
345 res->name, psz, psz == 1 ? " " : "s");
347 shmedia_free_io(res);
349 if ((char *)res >= (char *)xresv &&
350 (char *)res < (char *)&xresv[XNRES]) {
351 xres_free((struct xresource *)res);
357 #ifdef CONFIG_PROC_FS
359 ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
362 char *p = buf, *e = buf + length;
366 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
367 if (p + 32 >= e) /* Better than nothing */
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);
377 #endif /* CONFIG_PROC_FS */
379 static int __init register_proc_onchip(void)
381 #ifdef CONFIG_PROC_FS
382 create_proc_read_entry("io_map",0,0, ioremap_proc_info, &shmedia_iomap);
387 __initcall(register_proc_onchip);