blob: b4e996a398bd8535397cbb383d66a8428015cfb0 [file] [log] [blame]
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001/*
2 * address space "slices" (meta-segments) support
3 *
4 * Copyright (C) 2007 Benjamin Herrenschmidt, IBM Corporation.
5 *
6 * Based on hugetlb implementation
7 *
8 * Copyright (C) 2003 David Gibson, IBM Corporation.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#undef DEBUG
26
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/pagemap.h>
30#include <linux/err.h>
31#include <linux/spinlock.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040032#include <linux/export.h>
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100033#include <asm/mman.h>
34#include <asm/mmu.h>
35#include <asm/spu.h>
36
Roel Kluinf7a75f02007-10-16 23:30:25 -070037static DEFINE_SPINLOCK(slice_convert_lock);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100038
39
40#ifdef DEBUG
41int _slice_debug = 1;
42
43static void slice_print_mask(const char *label, struct slice_mask mask)
44{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +000045 char *p, buf[16 + 3 + 64 + 1];
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100046 int i;
47
48 if (!_slice_debug)
49 return;
50 p = buf;
51 for (i = 0; i < SLICE_NUM_LOW; i++)
52 *(p++) = (mask.low_slices & (1 << i)) ? '1' : '0';
53 *(p++) = ' ';
54 *(p++) = '-';
55 *(p++) = ' ';
56 for (i = 0; i < SLICE_NUM_HIGH; i++)
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +000057 *(p++) = (mask.high_slices & (1ul << i)) ? '1' : '0';
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100058 *(p++) = 0;
59
60 printk(KERN_DEBUG "%s:%s\n", label, buf);
61}
62
63#define slice_dbg(fmt...) do { if (_slice_debug) pr_debug(fmt); } while(0)
64
65#else
66
67static void slice_print_mask(const char *label, struct slice_mask mask) {}
68#define slice_dbg(fmt...)
69
70#endif
71
72static struct slice_mask slice_range_to_mask(unsigned long start,
73 unsigned long len)
74{
75 unsigned long end = start + len - 1;
76 struct slice_mask ret = { 0, 0 };
77
78 if (start < SLICE_LOW_TOP) {
79 unsigned long mend = min(end, SLICE_LOW_TOP);
80 unsigned long mstart = min(start, SLICE_LOW_TOP);
81
82 ret.low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
83 - (1u << GET_LOW_SLICE_INDEX(mstart));
84 }
85
86 if ((start + len) > SLICE_LOW_TOP)
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +000087 ret.high_slices = (1ul << (GET_HIGH_SLICE_INDEX(end) + 1))
88 - (1ul << GET_HIGH_SLICE_INDEX(start));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100089
90 return ret;
91}
92
93static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
94 unsigned long len)
95{
96 struct vm_area_struct *vma;
97
98 if ((mm->task_size - len) < addr)
99 return 0;
100 vma = find_vma(mm, addr);
101 return (!vma || (addr + len) <= vma->vm_start);
102}
103
104static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
105{
106 return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
107 1ul << SLICE_LOW_SHIFT);
108}
109
110static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
111{
112 unsigned long start = slice << SLICE_HIGH_SHIFT;
113 unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
114
115 /* Hack, so that each addresses is controlled by exactly one
116 * of the high or low area bitmaps, the first high area starts
117 * at 4GB, not 0 */
118 if (start == 0)
119 start = SLICE_LOW_TOP;
120
121 return !slice_area_is_free(mm, start, end - start);
122}
123
124static struct slice_mask slice_mask_for_free(struct mm_struct *mm)
125{
126 struct slice_mask ret = { 0, 0 };
127 unsigned long i;
128
129 for (i = 0; i < SLICE_NUM_LOW; i++)
130 if (!slice_low_has_vma(mm, i))
131 ret.low_slices |= 1u << i;
132
133 if (mm->task_size <= SLICE_LOW_TOP)
134 return ret;
135
136 for (i = 0; i < SLICE_NUM_HIGH; i++)
137 if (!slice_high_has_vma(mm, i))
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000138 ret.high_slices |= 1ul << i;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000139
140 return ret;
141}
142
143static struct slice_mask slice_mask_for_size(struct mm_struct *mm, int psize)
144{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000145 unsigned char *hpsizes;
146 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000147 struct slice_mask ret = { 0, 0 };
148 unsigned long i;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000149 u64 lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000150
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000151 lpsizes = mm->context.low_slices_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000152 for (i = 0; i < SLICE_NUM_LOW; i++)
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000153 if (((lpsizes >> (i * 4)) & 0xf) == psize)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000154 ret.low_slices |= 1u << i;
155
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000156 hpsizes = mm->context.high_slices_psize;
157 for (i = 0; i < SLICE_NUM_HIGH; i++) {
158 mask_index = i & 0x1;
159 index = i >> 1;
160 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
161 ret.high_slices |= 1ul << i;
162 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000163
164 return ret;
165}
166
167static int slice_check_fit(struct slice_mask mask, struct slice_mask available)
168{
169 return (mask.low_slices & available.low_slices) == mask.low_slices &&
170 (mask.high_slices & available.high_slices) == mask.high_slices;
171}
172
173static void slice_flush_segments(void *parm)
174{
175 struct mm_struct *mm = parm;
176 unsigned long flags;
177
178 if (mm != current->active_mm)
179 return;
180
181 /* update the paca copy of the context struct */
182 get_paca()->context = current->active_mm->context;
183
184 local_irq_save(flags);
185 slb_flush_and_rebolt();
186 local_irq_restore(flags);
187}
188
189static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
190{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000191 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000192 /* Write the new slice psize bits */
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000193 unsigned char *hpsizes;
194 u64 lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000195 unsigned long i, flags;
196
197 slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
198 slice_print_mask(" mask", mask);
199
200 /* We need to use a spinlock here to protect against
201 * concurrent 64k -> 4k demotion ...
202 */
203 spin_lock_irqsave(&slice_convert_lock, flags);
204
205 lpsizes = mm->context.low_slices_psize;
206 for (i = 0; i < SLICE_NUM_LOW; i++)
207 if (mask.low_slices & (1u << i))
208 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
209 (((unsigned long)psize) << (i * 4));
210
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000211 /* Assign the value back */
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000212 mm->context.low_slices_psize = lpsizes;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000213
214 hpsizes = mm->context.high_slices_psize;
215 for (i = 0; i < SLICE_NUM_HIGH; i++) {
216 mask_index = i & 0x1;
217 index = i >> 1;
218 if (mask.high_slices & (1ul << i))
219 hpsizes[index] = (hpsizes[index] &
220 ~(0xf << (mask_index * 4))) |
221 (((unsigned long)psize) << (mask_index * 4));
222 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000223
224 slice_dbg(" lsps=%lx, hsps=%lx\n",
225 mm->context.low_slices_psize,
226 mm->context.high_slices_psize);
227
228 spin_unlock_irqrestore(&slice_convert_lock, flags);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000229
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000230#ifdef CONFIG_SPU_BASE
231 spu_flush_all_slbs(mm);
232#endif
233}
234
235static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
236 unsigned long len,
237 struct slice_mask available,
238 int psize, int use_cache)
239{
240 struct vm_area_struct *vma;
241 unsigned long start_addr, addr;
242 struct slice_mask mask;
243 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
244
245 if (use_cache) {
246 if (len <= mm->cached_hole_size) {
247 start_addr = addr = TASK_UNMAPPED_BASE;
248 mm->cached_hole_size = 0;
249 } else
250 start_addr = addr = mm->free_area_cache;
251 } else
252 start_addr = addr = TASK_UNMAPPED_BASE;
253
254full_search:
255 for (;;) {
256 addr = _ALIGN_UP(addr, 1ul << pshift);
257 if ((TASK_SIZE - len) < addr)
258 break;
259 vma = find_vma(mm, addr);
260 BUG_ON(vma && (addr >= vma->vm_end));
261
262 mask = slice_range_to_mask(addr, len);
263 if (!slice_check_fit(mask, available)) {
264 if (addr < SLICE_LOW_TOP)
265 addr = _ALIGN_UP(addr + 1, 1ul << SLICE_LOW_SHIFT);
266 else
267 addr = _ALIGN_UP(addr + 1, 1ul << SLICE_HIGH_SHIFT);
268 continue;
269 }
270 if (!vma || addr + len <= vma->vm_start) {
271 /*
272 * Remember the place where we stopped the search:
273 */
274 if (use_cache)
275 mm->free_area_cache = addr + len;
276 return addr;
277 }
278 if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start)
279 mm->cached_hole_size = vma->vm_start - addr;
280 addr = vma->vm_end;
281 }
282
283 /* Make sure we didn't miss any holes */
284 if (use_cache && start_addr != TASK_UNMAPPED_BASE) {
285 start_addr = addr = TASK_UNMAPPED_BASE;
286 mm->cached_hole_size = 0;
287 goto full_search;
288 }
289 return -ENOMEM;
290}
291
292static unsigned long slice_find_area_topdown(struct mm_struct *mm,
293 unsigned long len,
294 struct slice_mask available,
295 int psize, int use_cache)
296{
297 struct vm_area_struct *vma;
298 unsigned long addr;
299 struct slice_mask mask;
300 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
301
302 /* check if free_area_cache is useful for us */
303 if (use_cache) {
304 if (len <= mm->cached_hole_size) {
305 mm->cached_hole_size = 0;
306 mm->free_area_cache = mm->mmap_base;
307 }
308
309 /* either no address requested or can't fit in requested
310 * address hole
311 */
312 addr = mm->free_area_cache;
313
314 /* make sure it can fit in the remaining address space */
315 if (addr > len) {
316 addr = _ALIGN_DOWN(addr - len, 1ul << pshift);
317 mask = slice_range_to_mask(addr, len);
318 if (slice_check_fit(mask, available) &&
319 slice_area_is_free(mm, addr, len))
320 /* remember the address as a hint for
321 * next time
322 */
323 return (mm->free_area_cache = addr);
324 }
325 }
326
327 addr = mm->mmap_base;
328 while (addr > len) {
329 /* Go down by chunk size */
330 addr = _ALIGN_DOWN(addr - len, 1ul << pshift);
331
332 /* Check for hit with different page size */
333 mask = slice_range_to_mask(addr, len);
334 if (!slice_check_fit(mask, available)) {
335 if (addr < SLICE_LOW_TOP)
336 addr = _ALIGN_DOWN(addr, 1ul << SLICE_LOW_SHIFT);
337 else if (addr < (1ul << SLICE_HIGH_SHIFT))
338 addr = SLICE_LOW_TOP;
339 else
340 addr = _ALIGN_DOWN(addr, 1ul << SLICE_HIGH_SHIFT);
341 continue;
342 }
343
344 /*
345 * Lookup failure means no vma is above this address,
346 * else if new region fits below vma->vm_start,
347 * return with success:
348 */
349 vma = find_vma(mm, addr);
350 if (!vma || (addr + len) <= vma->vm_start) {
351 /* remember the address as a hint for next time */
352 if (use_cache)
353 mm->free_area_cache = addr;
354 return addr;
355 }
356
357 /* remember the largest hole we saw so far */
358 if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start)
359 mm->cached_hole_size = vma->vm_start - addr;
360
361 /* try just below the current vma->vm_start */
362 addr = vma->vm_start;
363 }
364
365 /*
366 * A failed mmap() very likely causes application failure,
367 * so fall back to the bottom-up function here. This scenario
368 * can happen with large stack limits and large mmap()
369 * allocations.
370 */
371 addr = slice_find_area_bottomup(mm, len, available, psize, 0);
372
373 /*
374 * Restore the topdown base:
375 */
376 if (use_cache) {
377 mm->free_area_cache = mm->mmap_base;
378 mm->cached_hole_size = ~0UL;
379 }
380
381 return addr;
382}
383
384
385static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
386 struct slice_mask mask, int psize,
387 int topdown, int use_cache)
388{
389 if (topdown)
390 return slice_find_area_topdown(mm, len, mask, psize, use_cache);
391 else
392 return slice_find_area_bottomup(mm, len, mask, psize, use_cache);
393}
394
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000395#define or_mask(dst, src) do { \
396 (dst).low_slices |= (src).low_slices; \
397 (dst).high_slices |= (src).high_slices; \
398} while (0)
399
400#define andnot_mask(dst, src) do { \
401 (dst).low_slices &= ~(src).low_slices; \
402 (dst).high_slices &= ~(src).high_slices; \
403} while (0)
404
405#ifdef CONFIG_PPC_64K_PAGES
406#define MMU_PAGE_BASE MMU_PAGE_64K
407#else
408#define MMU_PAGE_BASE MMU_PAGE_4K
409#endif
410
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000411unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
412 unsigned long flags, unsigned int psize,
413 int topdown, int use_cache)
414{
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000415 struct slice_mask mask = {0, 0};
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000416 struct slice_mask good_mask;
417 struct slice_mask potential_mask = {0,0} /* silence stupid warning */;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000418 struct slice_mask compat_mask = {0, 0};
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000419 int fixed = (flags & MAP_FIXED);
420 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
421 struct mm_struct *mm = current->mm;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000422 unsigned long newaddr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000423
424 /* Sanity checks */
425 BUG_ON(mm->task_size == 0);
426
427 slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
428 slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d, use_cache=%d\n",
429 addr, len, flags, topdown, use_cache);
430
431 if (len > mm->task_size)
432 return -ENOMEM;
Benjamin Herrenschmidtd1f5a772007-08-08 15:44:15 +1000433 if (len & ((1ul << pshift) - 1))
434 return -EINVAL;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000435 if (fixed && (addr & ((1ul << pshift) - 1)))
436 return -EINVAL;
437 if (fixed && addr > (mm->task_size - len))
438 return -EINVAL;
439
440 /* If hint, make sure it matches our alignment restrictions */
441 if (!fixed && addr) {
442 addr = _ALIGN_UP(addr, 1ul << pshift);
443 slice_dbg(" aligned addr=%lx\n", addr);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000444 /* Ignore hint if it's too large or overlaps a VMA */
445 if (addr > mm->task_size - len ||
446 !slice_area_is_free(mm, addr, len))
447 addr = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000448 }
449
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000450 /* First make up a "good" mask of slices that have the right size
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000451 * already
452 */
453 good_mask = slice_mask_for_size(mm, psize);
454 slice_print_mask(" good_mask", good_mask);
455
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000456 /*
457 * Here "good" means slices that are already the right page size,
458 * "compat" means slices that have a compatible page size (i.e.
459 * 4k in a 64k pagesize kernel), and "free" means slices without
460 * any VMAs.
461 *
462 * If MAP_FIXED:
463 * check if fits in good | compat => OK
464 * check if fits in good | compat | free => convert free
465 * else bad
466 * If have hint:
467 * check if hint fits in good => OK
468 * check if hint fits in good | free => convert free
469 * Otherwise:
470 * search in good, found => OK
471 * search in good | free, found => convert free
472 * search in good | compat | free, found => convert free.
473 */
474
475#ifdef CONFIG_PPC_64K_PAGES
476 /* If we support combo pages, we can allow 64k pages in 4k slices */
477 if (psize == MMU_PAGE_64K) {
478 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
479 if (fixed)
480 or_mask(good_mask, compat_mask);
481 }
482#endif
483
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000484 /* First check hint if it's valid or if we have MAP_FIXED */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000485 if (addr != 0 || fixed) {
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000486 /* Build a mask for the requested range */
487 mask = slice_range_to_mask(addr, len);
488 slice_print_mask(" mask", mask);
489
490 /* Check if we fit in the good mask. If we do, we just return,
491 * nothing else to do
492 */
493 if (slice_check_fit(mask, good_mask)) {
494 slice_dbg(" fits good !\n");
495 return addr;
496 }
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000497 } else {
498 /* Now let's see if we can find something in the existing
499 * slices for that size
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000500 */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000501 newaddr = slice_find_area(mm, len, good_mask, psize, topdown,
502 use_cache);
503 if (newaddr != -ENOMEM) {
504 /* Found within the good mask, we don't have to setup,
505 * we thus return directly
506 */
507 slice_dbg(" found area at 0x%lx\n", newaddr);
508 return newaddr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000509 }
510 }
511
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000512 /* We don't fit in the good mask, check what other slices are
513 * empty and thus can be converted
514 */
515 potential_mask = slice_mask_for_free(mm);
516 or_mask(potential_mask, good_mask);
517 slice_print_mask(" potential", potential_mask);
518
519 if ((addr != 0 || fixed) && slice_check_fit(mask, potential_mask)) {
520 slice_dbg(" fits potential !\n");
521 goto convert;
522 }
523
524 /* If we have MAP_FIXED and failed the above steps, then error out */
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000525 if (fixed)
526 return -EBUSY;
527
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000528 slice_dbg(" search...\n");
529
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000530 /* If we had a hint that didn't work out, see if we can fit
531 * anywhere in the good area.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000532 */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000533 if (addr) {
534 addr = slice_find_area(mm, len, good_mask, psize, topdown,
535 use_cache);
536 if (addr != -ENOMEM) {
537 slice_dbg(" found area at 0x%lx\n", addr);
538 return addr;
539 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000540 }
541
542 /* Now let's see if we can find something in the existing slices
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000543 * for that size plus free slices
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000544 */
545 addr = slice_find_area(mm, len, potential_mask, psize, topdown,
546 use_cache);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000547
548#ifdef CONFIG_PPC_64K_PAGES
549 if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
550 /* retry the search with 4k-page slices included */
551 or_mask(potential_mask, compat_mask);
552 addr = slice_find_area(mm, len, potential_mask, psize,
553 topdown, use_cache);
554 }
555#endif
556
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000557 if (addr == -ENOMEM)
558 return -ENOMEM;
559
560 mask = slice_range_to_mask(addr, len);
561 slice_dbg(" found potential area at 0x%lx\n", addr);
562 slice_print_mask(" mask", mask);
563
564 convert:
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000565 andnot_mask(mask, good_mask);
566 andnot_mask(mask, compat_mask);
567 if (mask.low_slices || mask.high_slices) {
568 slice_convert(mm, mask, psize);
569 if (psize > MMU_PAGE_BASE)
Benjamin Herrenschmidt84c3d4a2008-07-16 11:07:59 +1000570 on_each_cpu(slice_flush_segments, mm, 1);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000571 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000572 return addr;
573
574}
575EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
576
577unsigned long arch_get_unmapped_area(struct file *filp,
578 unsigned long addr,
579 unsigned long len,
580 unsigned long pgoff,
581 unsigned long flags)
582{
583 return slice_get_unmapped_area(addr, len, flags,
584 current->mm->context.user_psize,
585 0, 1);
586}
587
588unsigned long arch_get_unmapped_area_topdown(struct file *filp,
589 const unsigned long addr0,
590 const unsigned long len,
591 const unsigned long pgoff,
592 const unsigned long flags)
593{
594 return slice_get_unmapped_area(addr0, len, flags,
595 current->mm->context.user_psize,
596 1, 1);
597}
598
599unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
600{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000601 unsigned char *hpsizes;
602 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000603
604 if (addr < SLICE_LOW_TOP) {
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000605 u64 lpsizes;
606 lpsizes = mm->context.low_slices_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000607 index = GET_LOW_SLICE_INDEX(addr);
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000608 return (lpsizes >> (index * 4)) & 0xf;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000609 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000610 hpsizes = mm->context.high_slices_psize;
611 index = GET_HIGH_SLICE_INDEX(addr);
612 mask_index = index & 0x1;
613 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000614}
615EXPORT_SYMBOL_GPL(get_slice_psize);
616
617/*
618 * This is called by hash_page when it needs to do a lazy conversion of
619 * an address space from real 64K pages to combo 4K pages (typically
620 * when hitting a non cacheable mapping on a processor or hypervisor
621 * that won't allow them for 64K pages).
622 *
623 * This is also called in init_new_context() to change back the user
624 * psize from whatever the parent context had it set to
Stephen Rothwell9dfe5c532007-08-15 16:33:55 +1000625 * N.B. This may be called before mm->context.id has been set.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000626 *
627 * This function will only change the content of the {low,high)_slice_psize
628 * masks, it will not flush SLBs as this shall be handled lazily by the
629 * caller.
630 */
631void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
632{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000633 int index, mask_index;
634 unsigned char *hpsizes;
635 unsigned long flags, lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000636 unsigned int old_psize;
637 int i;
638
639 slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
640
641 spin_lock_irqsave(&slice_convert_lock, flags);
642
643 old_psize = mm->context.user_psize;
644 slice_dbg(" old_psize=%d\n", old_psize);
645 if (old_psize == psize)
646 goto bail;
647
648 mm->context.user_psize = psize;
649 wmb();
650
651 lpsizes = mm->context.low_slices_psize;
652 for (i = 0; i < SLICE_NUM_LOW; i++)
653 if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
654 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
655 (((unsigned long)psize) << (i * 4));
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000656 /* Assign the value back */
657 mm->context.low_slices_psize = lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000658
659 hpsizes = mm->context.high_slices_psize;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000660 for (i = 0; i < SLICE_NUM_HIGH; i++) {
661 mask_index = i & 0x1;
662 index = i >> 1;
663 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize)
664 hpsizes[index] = (hpsizes[index] &
665 ~(0xf << (mask_index * 4))) |
666 (((unsigned long)psize) << (mask_index * 4));
667 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000668
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000669
670
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000671
672 slice_dbg(" lsps=%lx, hsps=%lx\n",
673 mm->context.low_slices_psize,
674 mm->context.high_slices_psize);
675
676 bail:
677 spin_unlock_irqrestore(&slice_convert_lock, flags);
678}
679
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000680void slice_set_psize(struct mm_struct *mm, unsigned long address,
681 unsigned int psize)
682{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000683 unsigned char *hpsizes;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000684 unsigned long i, flags;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000685 u64 *lpsizes;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000686
687 spin_lock_irqsave(&slice_convert_lock, flags);
688 if (address < SLICE_LOW_TOP) {
689 i = GET_LOW_SLICE_INDEX(address);
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000690 lpsizes = &mm->context.low_slices_psize;
691 *lpsizes = (*lpsizes & ~(0xful << (i * 4))) |
692 ((unsigned long) psize << (i * 4));
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000693 } else {
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000694 int index, mask_index;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000695 i = GET_HIGH_SLICE_INDEX(address);
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000696 hpsizes = mm->context.high_slices_psize;
697 mask_index = i & 0x1;
698 index = i >> 1;
699 hpsizes[index] = (hpsizes[index] &
700 ~(0xf << (mask_index * 4))) |
701 (((unsigned long)psize) << (mask_index * 4));
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000702 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000703
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000704 spin_unlock_irqrestore(&slice_convert_lock, flags);
705
706#ifdef CONFIG_SPU_BASE
707 spu_flush_all_slbs(mm);
708#endif
709}
710
711void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
712 unsigned long len, unsigned int psize)
713{
714 struct slice_mask mask = slice_range_to_mask(start, len);
715
716 slice_convert(mm, mask, psize);
717}
718
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000719/*
720 * is_hugepage_only_range() is used by generic code to verify wether
721 * a normal mmap mapping (non hugetlbfs) is valid on a given area.
722 *
723 * until the generic code provides a more generic hook and/or starts
724 * calling arch get_unmapped_area for MAP_FIXED (which our implementation
725 * here knows how to deal with), we hijack it to keep standard mappings
726 * away from us.
727 *
728 * because of that generic code limitation, MAP_FIXED mapping cannot
729 * "convert" back a slice with no VMAs to the standard page size, only
730 * get_unmapped_area() can. It would be possible to fix it here but I
731 * prefer working on fixing the generic code instead.
732 *
733 * WARNING: This will not work if hugetlbfs isn't enabled since the
734 * generic code will redefine that function as 0 in that. This is ok
735 * for now as we only use slices with hugetlbfs enabled. This should
736 * be fixed as the generic code gets fixed.
737 */
738int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
739 unsigned long len)
740{
741 struct slice_mask mask, available;
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000742 unsigned int psize = mm->context.user_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000743
744 mask = slice_range_to_mask(addr, len);
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000745 available = slice_mask_for_size(mm, psize);
746#ifdef CONFIG_PPC_64K_PAGES
747 /* We need to account for 4k slices too */
748 if (psize == MMU_PAGE_64K) {
749 struct slice_mask compat_mask;
750 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
751 or_mask(available, compat_mask);
752 }
753#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000754
755#if 0 /* too verbose */
756 slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
757 mm, addr, len);
758 slice_print_mask(" mask", mask);
759 slice_print_mask(" available", available);
760#endif
761 return !slice_check_fit(mask, available);
762}
763