blob: cea224c3e49b6049b54edd6d5a6e782a02728dc2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundt249cfea2007-11-19 18:26:19 +09002 * arch/sh/mm/ioremap_64.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000, 2001 Paolo Alberelli
Paul Mundt249cfea2007-11-19 18:26:19 +09005 * Copyright (C) 2003 - 2007 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Paul Mundt249cfea2007-11-19 18:26:19 +090011 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ioport.h>
Paul Mundt749c8492007-10-01 17:36:47 +090018#include <linux/module.h>
Paul Mundtad81eb92007-11-11 18:43:33 +090019#include <linux/mm.h>
20#include <linux/io.h>
Paul Mundt249cfea2007-11-19 18:26:19 +090021#include <linux/bootmem.h>
Paul Mundt2b6a8d42007-11-19 14:26:09 +090022#include <linux/proc_fs.h>
Paul Mundtad81eb92007-11-11 18:43:33 +090023#include <asm/page.h>
Paul Mundt749c8492007-10-01 17:36:47 +090024#include <asm/pgalloc.h>
Paul Mundtad81eb92007-11-11 18:43:33 +090025#include <asm/addrspace.h>
26#include <asm/cacheflush.h>
Paul Mundt749c8492007-10-01 17:36:47 +090027#include <asm/tlbflush.h>
Paul Mundtad81eb92007-11-11 18:43:33 +090028#include <asm/mmu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static void shmedia_mapioaddr(unsigned long, unsigned long);
31static unsigned long shmedia_ioremap(struct resource *, u32, int);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
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 */
Paul Mundt249cfea2007-11-19 18:26:19 +090046void *__ioremap(unsigned long phys_addr, unsigned long size,
47 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 void * addr;
50 struct vm_struct * area;
51 unsigned long offset, last_addr;
Haavard Skinnemoen10731b82006-12-08 02:38:08 -080052 pgprot_t pgprot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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
Haavard Skinnemoen10731b82006-12-08 02:38:08 -080059 pgprot = __pgprot(_PAGE_PRESENT | _PAGE_READ |
60 _PAGE_WRITE | _PAGE_DIRTY |
61 _PAGE_ACCESSED | _PAGE_SHARED | flags);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /*
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 pr_debug("Get vm_area returns %p addr %p\n",area,area->addr);
75 if (!area)
76 return NULL;
77 area->phys_addr = phys_addr;
78 addr = area->addr;
Haavard Skinnemoen10731b82006-12-08 02:38:08 -080079 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
80 phys_addr, pgprot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 vunmap(addr);
82 return NULL;
83 }
84 return (void *) (offset + (char *)addr);
85}
Paul Mundt749c8492007-10-01 17:36:47 +090086EXPORT_SYMBOL(__ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Paul Mundt249cfea2007-11-19 18:26:19 +090088void __iounmap(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
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}
Paul Mundt249cfea2007-11-19 18:26:19 +0900101EXPORT_SYMBOL(__iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static struct resource shmedia_iomap = {
104 .name = "shmedia_iomap",
105 .start = IOBASE_VADDR + PAGE_SIZE,
106 .end = IOBASE_END - 1,
107};
108
109static void shmedia_mapioaddr(unsigned long pa, unsigned long va);
110static void shmedia_unmapioaddr(unsigned long vaddr);
111static 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
123struct xresource {
124 struct resource xres; /* Must be first */
125 int xflag; /* 1 == used */
126 char xname[XNMLN+1];
127};
128
129static struct xresource xresv[XNRES];
130
131static 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
147static void xres_free(struct xresource *xrp)
148{
149 xrp->xflag = 0;
150}
151
152static 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
164static 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",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800181 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 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
200static 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
236static 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
Paul Mundtfad9e7d2007-07-20 17:46:42 +0900250static __init_refok void *sh64_get_page(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
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
267static void shmedia_mapioaddr(unsigned long pa, unsigned long va)
268{
269 pgd_t *pgdp;
Paul Mundt249cfea2007-11-19 18:26:19 +0900270 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 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)) {
Paul Mundt249cfea2007-11-19 18:26:19 +0900280 pudp = (pud_t *)sh64_get_page();
281 set_pgd(pgdp, __pgd((unsigned long)pudp | _KERNPG_TABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
Paul Mundt249cfea2007-11-19 18:26:19 +0900284 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 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
311static void shmedia_unmapioaddr(unsigned long vaddr)
312{
313 pgd_t *pgdp;
Paul Mundt249cfea2007-11-19 18:26:19 +0900314 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 pmd_t *pmdp;
316 pte_t *ptep;
317
318 pgdp = pgd_offset_k(vaddr);
Paul Mundt249cfea2007-11-19 18:26:19 +0900319 if (pgd_none(*pgdp) || pgd_bad(*pgdp))
320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Paul Mundt249cfea2007-11-19 18:26:19 +0900322 pudp = pud_offset(pgdp, vaddr);
323 if (pud_none(*pudp) || pud_bad(*pudp))
324 return;
325
326 pmdp = pmd_offset(pudp, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 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
339unsigned 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
347void onchip_unmap(unsigned long vaddr)
348{
349 struct resource *res;
350 unsigned int psz;
351
352 res = shmedia_find_resource(&shmedia_iomap, vaddr);
353 if (!res) {
354 printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800355 __func__, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357 }
358
359 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
360
361 printk(KERN_DEBUG "unmapioaddr: %6s [%2d page%s] freed\n",
362 res->name, psz, psz == 1 ? " " : "s");
363
364 shmedia_free_io(res);
365
366 if ((char *)res >= (char *)xresv &&
367 (char *)res < (char *)&xresv[XNRES]) {
368 xres_free((struct xresource *)res);
369 } else {
370 kfree(res);
371 }
372}
373
374#ifdef CONFIG_PROC_FS
375static int
376ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
377 void *data)
378{
379 char *p = buf, *e = buf + length;
380 struct resource *r;
381 const char *nm;
382
383 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
384 if (p + 32 >= e) /* Better than nothing */
385 break;
386 if ((nm = r->name) == 0) nm = "???";
Paul Mundt21264132006-09-12 14:36:46 +0900387 p += sprintf(p, "%08lx-%08lx: %s\n",
388 (unsigned long)r->start,
389 (unsigned long)r->end, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 return p-buf;
393}
394#endif /* CONFIG_PROC_FS */
395
396static int __init register_proc_onchip(void)
397{
398#ifdef CONFIG_PROC_FS
399 create_proc_read_entry("io_map",0,0, ioremap_proc_info, &shmedia_iomap);
400#endif
401 return 0;
402}
403
404__initcall(register_proc_onchip);