Pull video into test branch
[linux-drm-fsl-dcu.git] / arch / mips / mm / ioremap.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  * (C) Copyright 1995 1996 Linus Torvalds
7  * (C) Copyright 2001, 2002 Ralf Baechle
8  */
9 #include <linux/mm.h>
10 #include <linux/module.h>
11 #include <asm/addrspace.h>
12 #include <asm/byteorder.h>
13
14 #include <linux/vmalloc.h>
15 #include <linux/io.h>
16
17 /*
18  * Generic mapping function (not visible outside):
19  */
20
21 /*
22  * Remap an arbitrary physical address space into the kernel virtual
23  * address space. Needed when the kernel wants to access high addresses
24  * directly.
25  *
26  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
27  * have to convert them into an offset in a page-aligned mapping, but the
28  * caller shouldn't need to know that small detail.
29  */
30
31 #define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
32
33 void __iomem * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
34 {
35         struct vm_struct * area;
36         unsigned long offset;
37         phys_t last_addr;
38         void * addr;
39         pgprot_t pgprot;
40
41         phys_addr = fixup_bigphys_addr(phys_addr, size);
42
43         /* Don't allow wraparound or zero size */
44         last_addr = phys_addr + size - 1;
45         if (!size || last_addr < phys_addr)
46                 return NULL;
47
48         /*
49          * Map uncached objects in the low 512mb of address space using KSEG1,
50          * otherwise map using page tables.
51          */
52         if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
53             flags == _CACHE_UNCACHED)
54                 return (void __iomem *) CKSEG1ADDR(phys_addr);
55
56         /*
57          * Don't allow anybody to remap normal RAM that we're using..
58          */
59         if (phys_addr < virt_to_phys(high_memory)) {
60                 char *t_addr, *t_end;
61                 struct page *page;
62
63                 t_addr = __va(phys_addr);
64                 t_end = t_addr + (size - 1);
65
66                 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
67                         if(!PageReserved(page))
68                                 return NULL;
69         }
70
71         pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
72                           | __WRITEABLE | flags);
73
74         /*
75          * Mappings have to be page-aligned
76          */
77         offset = phys_addr & ~PAGE_MASK;
78         phys_addr &= PAGE_MASK;
79         size = PAGE_ALIGN(last_addr + 1) - phys_addr;
80
81         /*
82          * Ok, go for it..
83          */
84         area = get_vm_area(size, VM_IOREMAP);
85         if (!area)
86                 return NULL;
87         addr = area->addr;
88         if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
89                                phys_addr, pgprot)) {
90                 vunmap(addr);
91                 return NULL;
92         }
93
94         return (void __iomem *) (offset + (char *)addr);
95 }
96
97 #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
98
99 void __iounmap(const volatile void __iomem *addr)
100 {
101         struct vm_struct *p;
102
103         if (IS_KSEG1(addr))
104                 return;
105
106         p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
107         if (!p)
108                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
109
110         kfree(p);
111 }
112
113 EXPORT_SYMBOL(__ioremap);
114 EXPORT_SYMBOL(__iounmap);