GNU Linux-libre 4.4.288-gnu1
[releases.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/module.h>
10 #include <asm/addrspace.h>
11 #include <asm/byteorder.h>
12 #include <linux/ioport.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <asm/cacheflush.h>
17 #include <asm/io.h>
18 #include <asm/tlbflush.h>
19
20 static inline void remap_area_pte(pte_t * pte, unsigned long address,
21         phys_addr_t size, phys_addr_t phys_addr, unsigned long flags)
22 {
23         phys_addr_t end;
24         unsigned long pfn;
25         pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
26                                    | __WRITEABLE | flags);
27
28         address &= ~PMD_MASK;
29         end = address + size;
30         if (end > PMD_SIZE)
31                 end = PMD_SIZE;
32         BUG_ON(address >= end);
33         pfn = phys_addr >> PAGE_SHIFT;
34         do {
35                 if (!pte_none(*pte)) {
36                         printk("remap_area_pte: page already exists\n");
37                         BUG();
38                 }
39                 set_pte(pte, pfn_pte(pfn, pgprot));
40                 address += PAGE_SIZE;
41                 pfn++;
42                 pte++;
43         } while (address && (address < end));
44 }
45
46 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
47         phys_addr_t size, phys_addr_t phys_addr, unsigned long flags)
48 {
49         phys_addr_t end;
50
51         address &= ~PGDIR_MASK;
52         end = address + size;
53         if (end > PGDIR_SIZE)
54                 end = PGDIR_SIZE;
55         phys_addr -= address;
56         BUG_ON(address >= end);
57         do {
58                 pte_t * pte = pte_alloc_kernel(pmd, address);
59                 if (!pte)
60                         return -ENOMEM;
61                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
62                 address = (address + PMD_SIZE) & PMD_MASK;
63                 pmd++;
64         } while (address && (address < end));
65         return 0;
66 }
67
68 static int remap_area_pages(unsigned long address, phys_addr_t phys_addr,
69         phys_addr_t size, unsigned long flags)
70 {
71         int error;
72         pgd_t * dir;
73         unsigned long end = address + size;
74
75         phys_addr -= address;
76         dir = pgd_offset(&init_mm, address);
77         flush_cache_all();
78         BUG_ON(address >= end);
79         do {
80                 pud_t *pud;
81                 pmd_t *pmd;
82
83                 error = -ENOMEM;
84                 pud = pud_alloc(&init_mm, dir, address);
85                 if (!pud)
86                         break;
87                 pmd = pmd_alloc(&init_mm, pud, address);
88                 if (!pmd)
89                         break;
90                 if (remap_area_pmd(pmd, address, end - address,
91                                          phys_addr + address, flags))
92                         break;
93                 error = 0;
94                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
95                 dir++;
96         } while (address && (address < end));
97         flush_tlb_all();
98         return error;
99 }
100
101 static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages,
102                                void *arg)
103 {
104         unsigned long i;
105
106         for (i = 0; i < nr_pages; i++) {
107                 if (pfn_valid(start_pfn + i) &&
108                     !PageReserved(pfn_to_page(start_pfn + i)))
109                         return 1;
110         }
111
112         return 0;
113 }
114
115 /*
116  * Generic mapping function (not visible outside):
117  */
118
119 /*
120  * Remap an arbitrary physical address space into the kernel virtual
121  * address space. Needed when the kernel wants to access high addresses
122  * directly.
123  *
124  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
125  * have to convert them into an offset in a page-aligned mapping, but the
126  * caller shouldn't need to know that small detail.
127  */
128
129 #define IS_LOW512(addr) (!((phys_addr_t)(addr) & (phys_addr_t) ~0x1fffffffULL))
130
131 void __iomem * __ioremap(phys_addr_t phys_addr, phys_addr_t size, unsigned long flags)
132 {
133         unsigned long offset, pfn, last_pfn;
134         struct vm_struct * area;
135         phys_addr_t last_addr;
136         void * addr;
137
138         phys_addr = fixup_bigphys_addr(phys_addr, size);
139
140         /* Don't allow wraparound or zero size */
141         last_addr = phys_addr + size - 1;
142         if (!size || last_addr < phys_addr)
143                 return NULL;
144
145         /*
146          * Map uncached objects in the low 512mb of address space using KSEG1,
147          * otherwise map using page tables.
148          */
149         if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
150             flags == _CACHE_UNCACHED)
151                 return (void __iomem *) CKSEG1ADDR(phys_addr);
152
153         /*
154          * Don't allow anybody to remap RAM that may be allocated by the page
155          * allocator, since that could lead to races & data clobbering.
156          */
157         pfn = PFN_DOWN(phys_addr);
158         last_pfn = PFN_DOWN(last_addr);
159         if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL,
160                                   __ioremap_check_ram) == 1) {
161                 WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
162                           &phys_addr, &last_addr);
163                 return NULL;
164         }
165
166         /*
167          * Mappings have to be page-aligned
168          */
169         offset = phys_addr & ~PAGE_MASK;
170         phys_addr &= PAGE_MASK;
171         size = PAGE_ALIGN(last_addr + 1) - phys_addr;
172
173         /*
174          * Ok, go for it..
175          */
176         area = get_vm_area(size, VM_IOREMAP);
177         if (!area)
178                 return NULL;
179         addr = area->addr;
180         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
181                 vunmap(addr);
182                 return NULL;
183         }
184
185         return (void __iomem *) (offset + (char *)addr);
186 }
187
188 #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
189
190 void __iounmap(const volatile void __iomem *addr)
191 {
192         struct vm_struct *p;
193
194         if (IS_KSEG1(addr))
195                 return;
196
197         p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
198         if (!p)
199                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
200
201         kfree(p);
202 }
203
204 EXPORT_SYMBOL(__ioremap);
205 EXPORT_SYMBOL(__iounmap);