2 * arch/sh/mm/ioremap_64.c
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003 - 2007 Paul Mundt
7 * Mostly derived from arch/sh/mm/ioremap.c which, in turn is mostly
8 * derived from arch/i386/mm/ioremap.c .
10 * (C) Copyright 1995 1996 Linus Torvalds
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
16 #include <linux/vmalloc.h>
17 #include <linux/ioport.h>
18 #include <linux/module.h>
21 #include <linux/bootmem.h>
22 #include <linux/proc_fs.h>
24 #include <asm/pgalloc.h>
25 #include <asm/addrspace.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
30 static struct resource shmedia_iomap = {
31 .name = "shmedia_iomap",
32 .start = IOBASE_VADDR + PAGE_SIZE,
33 .end = IOBASE_END - 1,
36 static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
38 static void shmedia_unmapioaddr(unsigned long vaddr);
39 static void __iomem *shmedia_ioremap(struct resource *res, u32 pa,
40 int sz, unsigned long flags);
43 * We have the same problem as the SPARC, so lets have the same comment:
44 * Our mini-allocator...
45 * Boy this is gross! We need it because we must map I/O for
46 * timers and interrupt controller before the kmalloc is available.
53 struct resource xres; /* Must be first */
54 int xflag; /* 1 == used */
58 static struct xresource xresv[XNRES];
60 static struct xresource *xres_alloc(void)
62 struct xresource *xrp;
66 for (n = 0; n < XNRES; n++) {
67 if (xrp->xflag == 0) {
76 static void xres_free(struct xresource *xrp)
81 static struct resource *shmedia_find_resource(struct resource *root,
86 for (res = root->child; res; res = res->sibling)
87 if (res->start <= vaddr && res->end >= vaddr)
93 static void __iomem *shmedia_alloc_io(unsigned long phys, unsigned long size,
94 const char *name, unsigned long flags)
96 static int printed_full;
97 struct xresource *xres;
111 printk(KERN_NOTICE "%s: done with statics, "
112 "switching to kmalloc\n", __func__);
116 tack = kmalloc(sizeof(struct resource) + tlen + 1, GFP_KERNEL);
119 memset(tack, 0, sizeof(struct resource));
120 res = (struct resource *) tack;
121 tack += sizeof(struct resource);
124 strncpy(tack, name, XNMLN);
128 return shmedia_ioremap(res, phys, size, flags);
131 static void __iomem *shmedia_ioremap(struct resource *res, u32 pa, int sz,
134 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
135 unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
139 if (allocate_resource(&shmedia_iomap, res, round_sz,
140 shmedia_iomap.start, shmedia_iomap.end,
141 PAGE_SIZE, NULL, NULL) != 0) {
142 panic("alloc_io_res(%s): cannot occupy\n",
143 (res->name != NULL) ? res->name : "???");
149 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
151 for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
152 shmedia_mapioaddr(pa, va, flags);
157 return (void __iomem *)(unsigned long)(res->start + offset);
160 static void shmedia_free_io(struct resource *res)
162 unsigned long len = res->end - res->start + 1;
164 BUG_ON((len & (PAGE_SIZE - 1)) != 0);
168 shmedia_unmapioaddr(res->start + len);
171 release_resource(res);
174 static __init_refok void *sh64_get_page(void)
179 page = (void *)get_zeroed_page(GFP_KERNEL);
181 page = alloc_bootmem_pages(PAGE_SIZE);
183 if (!page || ((unsigned long)page & ~PAGE_MASK))
184 panic("sh64_get_page: Out of memory already?\n");
189 static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
198 pr_debug("shmedia_mapiopage pa %08lx va %08lx\n", pa, va);
201 flags = 1; /* 1 = CB0-1 device */
203 pgdp = pgd_offset_k(va);
204 if (pgd_none(*pgdp) || !pgd_present(*pgdp)) {
205 pudp = (pud_t *)sh64_get_page();
206 set_pgd(pgdp, __pgd((unsigned long)pudp | _KERNPG_TABLE));
209 pudp = pud_offset(pgdp, va);
210 if (pud_none(*pudp) || !pud_present(*pudp)) {
211 pmdp = (pmd_t *)sh64_get_page();
212 set_pud(pudp, __pud((unsigned long)pmdp | _KERNPG_TABLE));
215 pmdp = pmd_offset(pudp, va);
216 if (pmd_none(*pmdp) || !pmd_present(*pmdp)) {
217 ptep = (pte_t *)sh64_get_page();
218 set_pmd(pmdp, __pmd((unsigned long)ptep + _PAGE_TABLE));
221 prot = __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE |
222 _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SHARED | flags);
224 pte = pfn_pte(pa >> PAGE_SHIFT, prot);
225 ptep = pte_offset_kernel(pmdp, va);
227 if (!pte_none(*ptep) &&
228 pte_val(*ptep) != pte_val(pte))
233 flush_tlb_kernel_range(va, PAGE_SIZE);
236 static void shmedia_unmapioaddr(unsigned long vaddr)
243 pgdp = pgd_offset_k(vaddr);
244 if (pgd_none(*pgdp) || pgd_bad(*pgdp))
247 pudp = pud_offset(pgdp, vaddr);
248 if (pud_none(*pudp) || pud_bad(*pudp))
251 pmdp = pmd_offset(pudp, vaddr);
252 if (pmd_none(*pmdp) || pmd_bad(*pmdp))
255 ptep = pte_offset_kernel(pmdp, vaddr);
257 if (pte_none(*ptep) || !pte_present(*ptep))
260 clear_page((void *)ptep);
261 pte_clear(&init_mm, vaddr, ptep);
264 void __iomem *__ioremap(unsigned long offset, unsigned long size,
269 sprintf(name, "phys_%08x", (u32)offset);
270 return shmedia_alloc_io(offset, size, name, flags);
272 EXPORT_SYMBOL(__ioremap);
274 void __iounmap(void __iomem *virtual)
276 unsigned long vaddr = (unsigned long)virtual & PAGE_MASK;
277 struct resource *res;
280 res = shmedia_find_resource(&shmedia_iomap, vaddr);
282 printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
287 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
289 shmedia_free_io(res);
291 if ((char *)res >= (char *)xresv &&
292 (char *)res < (char *)&xresv[XNRES]) {
293 xres_free((struct xresource *)res);
298 EXPORT_SYMBOL(__iounmap);
301 ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
304 char *p = buf, *e = buf + length;
308 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
309 if (p + 32 >= e) /* Better than nothing */
315 p += sprintf(p, "%08lx-%08lx: %s\n",
316 (unsigned long)r->start,
317 (unsigned long)r->end, nm);
323 static int __init register_proc_onchip(void)
325 create_proc_read_entry("io_map", 0, 0, ioremap_proc_info,
329 late_initcall(register_proc_onchip);