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