Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / arch / sh / mm / mmap.c
1 /*
2  * arch/sh/mm/mmap.c
3  *
4  * Copyright (C) 2008 - 2009  Paul Mundt
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 #include <linux/io.h>
11 #include <linux/mm.h>
12 #include <linux/mman.h>
13 #include <linux/module.h>
14 #include <asm/page.h>
15 #include <asm/processor.h>
16
17 unsigned long shm_align_mask = PAGE_SIZE - 1;   /* Sane caches */
18 EXPORT_SYMBOL(shm_align_mask);
19
20 #ifdef CONFIG_MMU
21 /*
22  * To avoid cache aliases, we map the shared page with same color.
23  */
24 static inline unsigned long COLOUR_ALIGN(unsigned long addr,
25                                          unsigned long pgoff)
26 {
27         unsigned long base = (addr + shm_align_mask) & ~shm_align_mask;
28         unsigned long off = (pgoff << PAGE_SHIFT) & shm_align_mask;
29
30         return base + off;
31 }
32
33 static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
34                                               unsigned long pgoff)
35 {
36         unsigned long base = addr & ~shm_align_mask;
37         unsigned long off = (pgoff << PAGE_SHIFT) & shm_align_mask;
38
39         if (base + off <= addr)
40                 return base + off;
41
42         return base - off;
43 }
44
45 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
46         unsigned long len, unsigned long pgoff, unsigned long flags)
47 {
48         struct mm_struct *mm = current->mm;
49         struct vm_area_struct *vma;
50         unsigned long start_addr, vm_start;
51         int do_colour_align;
52
53         if (flags & MAP_FIXED) {
54                 /* We do not accept a shared mapping if it would violate
55                  * cache aliasing constraints.
56                  */
57                 if ((flags & MAP_SHARED) &&
58                     ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
59                         return -EINVAL;
60                 return addr;
61         }
62
63         if (unlikely(len > TASK_SIZE))
64                 return -ENOMEM;
65
66         do_colour_align = 0;
67         if (filp || (flags & MAP_SHARED))
68                 do_colour_align = 1;
69
70         if (addr) {
71                 if (do_colour_align)
72                         addr = COLOUR_ALIGN(addr, pgoff);
73                 else
74                         addr = PAGE_ALIGN(addr);
75
76                 vma = find_vma(mm, addr);
77                 if (TASK_SIZE - len >= addr &&
78                     (!vma || addr + len <= vm_start_gap(vma)))
79                         return addr;
80         }
81
82         if (len > mm->cached_hole_size) {
83                 start_addr = addr = mm->free_area_cache;
84         } else {
85                 mm->cached_hole_size = 0;
86                 start_addr = addr = TASK_UNMAPPED_BASE;
87         }
88
89 full_search:
90         if (do_colour_align)
91                 addr = COLOUR_ALIGN(addr, pgoff);
92         else
93                 addr = PAGE_ALIGN(mm->free_area_cache);
94
95         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
96                 /* At this point:  (!vma || addr < vma->vm_end). */
97                 if (unlikely(TASK_SIZE - len < addr)) {
98                         /*
99                          * Start a new search - just in case we missed
100                          * some holes.
101                          */
102                         if (start_addr != TASK_UNMAPPED_BASE) {
103                                 start_addr = addr = TASK_UNMAPPED_BASE;
104                                 mm->cached_hole_size = 0;
105                                 goto full_search;
106                         }
107                         return -ENOMEM;
108                 }
109                 if (vma)
110                         vm_start = vm_start_gap(vma);
111                 if (likely(!vma || addr + len <= vm_start)) {
112                         /*
113                          * Remember the place where we stopped the search:
114                          */
115                         mm->free_area_cache = addr + len;
116                         return addr;
117                 }
118                 if (addr + mm->cached_hole_size < vm_start)
119                         mm->cached_hole_size = vm_start - addr;
120
121                 addr = vma->vm_end;
122                 if (do_colour_align)
123                         addr = COLOUR_ALIGN(addr, pgoff);
124         }
125 }
126
127 unsigned long
128 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
129                           const unsigned long len, const unsigned long pgoff,
130                           const unsigned long flags)
131 {
132         struct vm_area_struct *vma;
133         struct mm_struct *mm = current->mm;
134         unsigned long addr = addr0;
135         unsigned long vm_start;
136         int do_colour_align;
137
138         if (flags & MAP_FIXED) {
139                 /* We do not accept a shared mapping if it would violate
140                  * cache aliasing constraints.
141                  */
142                 if ((flags & MAP_SHARED) &&
143                     ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
144                         return -EINVAL;
145                 return addr;
146         }
147
148         if (unlikely(len > TASK_SIZE))
149                 return -ENOMEM;
150
151         do_colour_align = 0;
152         if (filp || (flags & MAP_SHARED))
153                 do_colour_align = 1;
154
155         /* requesting a specific address */
156         if (addr) {
157                 if (do_colour_align)
158                         addr = COLOUR_ALIGN(addr, pgoff);
159                 else
160                         addr = PAGE_ALIGN(addr);
161
162                 vma = find_vma(mm, addr);
163                 if (TASK_SIZE - len >= addr &&
164                     (!vma || addr + len <= vm_start_gap(vma)))
165                         return addr;
166         }
167
168         /* check if free_area_cache is useful for us */
169         if (len <= mm->cached_hole_size) {
170                 mm->cached_hole_size = 0;
171                 mm->free_area_cache = mm->mmap_base;
172         }
173
174         /* either no address requested or can't fit in requested address hole */
175         addr = mm->free_area_cache;
176         if (do_colour_align) {
177                 unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
178
179                 addr = base + len;
180         }
181
182         /* make sure it can fit in the remaining address space */
183         if (likely(addr > len)) {
184                 vma = find_vma(mm, addr-len);
185                 if (!vma || addr <= vm_start_gap(vma)) {
186                         /* remember the address as a hint for next time */
187                         return (mm->free_area_cache = addr-len);
188                 }
189         }
190
191         if (unlikely(mm->mmap_base < len))
192                 goto bottomup;
193
194         addr = mm->mmap_base-len;
195         if (do_colour_align)
196                 addr = COLOUR_ALIGN_DOWN(addr, pgoff);
197
198         do {
199                 /*
200                  * Lookup failure means no vma is above this address,
201                  * else if new region fits below vma->vm_start,
202                  * return with success:
203                  */
204                 vma = find_vma(mm, addr);
205                 if (vma)
206                         vm_start = vm_start_gap(vma);
207                 if (likely(!vma || addr + len <= vm_start)) {
208                         /* remember the address as a hint for next time */
209                         return (mm->free_area_cache = addr);
210                 }
211
212                 /* remember the largest hole we saw so far */
213                 if (addr + mm->cached_hole_size < vm_start)
214                         mm->cached_hole_size = vm_start - addr;
215
216                 /* try just below the current vma->vm_start */
217                 addr = vm_start-len;
218                 if (do_colour_align)
219                         addr = COLOUR_ALIGN_DOWN(addr, pgoff);
220         } while (likely(len < vm_start));
221
222 bottomup:
223         /*
224          * A failed mmap() very likely causes application failure,
225          * so fall back to the bottom-up function here. This scenario
226          * can happen with large stack limits and large mmap()
227          * allocations.
228          */
229         mm->cached_hole_size = ~0UL;
230         mm->free_area_cache = TASK_UNMAPPED_BASE;
231         addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
232         /*
233          * Restore the topdown base:
234          */
235         mm->free_area_cache = mm->mmap_base;
236         mm->cached_hole_size = ~0UL;
237
238         return addr;
239 }
240 #endif /* CONFIG_MMU */
241
242 /*
243  * You really shouldn't be using read() or write() on /dev/mem.  This
244  * might go away in the future.
245  */
246 int valid_phys_addr_range(unsigned long addr, size_t count)
247 {
248         if (addr < __MEMORY_START)
249                 return 0;
250         if (addr + count > __pa(high_memory))
251                 return 0;
252
253         return 1;
254 }
255
256 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
257 {
258         return 1;
259 }