pandora: defconfig: update
[pandora-kernel.git] / arch / powerpc / mm / slice.c
1 /*
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>
32 #include <linux/export.h>
33 #include <asm/mman.h>
34 #include <asm/mmu.h>
35 #include <asm/spu.h>
36
37 static DEFINE_SPINLOCK(slice_convert_lock);
38
39
40 #ifdef DEBUG
41 int _slice_debug = 1;
42
43 static void slice_print_mask(const char *label, struct slice_mask mask)
44 {
45         char    *p, buf[16 + 3 + 16 + 1];
46         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++)
57                 *(p++) = (mask.high_slices & (1 << i)) ? '1' : '0';
58         *(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
67 static void slice_print_mask(const char *label, struct slice_mask mask) {}
68 #define slice_dbg(fmt...)
69
70 #endif
71
72 static 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)
87                 ret.high_slices = (1u << (GET_HIGH_SLICE_INDEX(end) + 1))
88                         - (1u << GET_HIGH_SLICE_INDEX(start));
89
90         return ret;
91 }
92
93 static 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) <= vm_start_gap(vma));
102 }
103
104 static 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
110 static 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
124 static 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))
138                         ret.high_slices |= 1u << i;
139
140         return ret;
141 }
142
143 static struct slice_mask slice_mask_for_size(struct mm_struct *mm, int psize)
144 {
145         struct slice_mask ret = { 0, 0 };
146         unsigned long i;
147         u64 psizes;
148
149         psizes = mm->context.low_slices_psize;
150         for (i = 0; i < SLICE_NUM_LOW; i++)
151                 if (((psizes >> (i * 4)) & 0xf) == psize)
152                         ret.low_slices |= 1u << i;
153
154         psizes = mm->context.high_slices_psize;
155         for (i = 0; i < SLICE_NUM_HIGH; i++)
156                 if (((psizes >> (i * 4)) & 0xf) == psize)
157                         ret.high_slices |= 1u << i;
158
159         return ret;
160 }
161
162 static int slice_check_fit(struct slice_mask mask, struct slice_mask available)
163 {
164         return (mask.low_slices & available.low_slices) == mask.low_slices &&
165                 (mask.high_slices & available.high_slices) == mask.high_slices;
166 }
167
168 static void slice_flush_segments(void *parm)
169 {
170         struct mm_struct *mm = parm;
171         unsigned long flags;
172
173         if (mm != current->active_mm)
174                 return;
175
176         /* update the paca copy of the context struct */
177         get_paca()->context = current->active_mm->context;
178
179         local_irq_save(flags);
180         slb_flush_and_rebolt();
181         local_irq_restore(flags);
182 }
183
184 static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
185 {
186         /* Write the new slice psize bits */
187         u64 lpsizes, hpsizes;
188         unsigned long i, flags;
189
190         slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
191         slice_print_mask(" mask", mask);
192
193         /* We need to use a spinlock here to protect against
194          * concurrent 64k -> 4k demotion ...
195          */
196         spin_lock_irqsave(&slice_convert_lock, flags);
197
198         lpsizes = mm->context.low_slices_psize;
199         for (i = 0; i < SLICE_NUM_LOW; i++)
200                 if (mask.low_slices & (1u << i))
201                         lpsizes = (lpsizes & ~(0xful << (i * 4))) |
202                                 (((unsigned long)psize) << (i * 4));
203
204         hpsizes = mm->context.high_slices_psize;
205         for (i = 0; i < SLICE_NUM_HIGH; i++)
206                 if (mask.high_slices & (1u << i))
207                         hpsizes = (hpsizes & ~(0xful << (i * 4))) |
208                                 (((unsigned long)psize) << (i * 4));
209
210         mm->context.low_slices_psize = lpsizes;
211         mm->context.high_slices_psize = hpsizes;
212
213         slice_dbg(" lsps=%lx, hsps=%lx\n",
214                   mm->context.low_slices_psize,
215                   mm->context.high_slices_psize);
216
217         spin_unlock_irqrestore(&slice_convert_lock, flags);
218
219 #ifdef CONFIG_SPU_BASE
220         spu_flush_all_slbs(mm);
221 #endif
222 }
223
224 static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
225                                               unsigned long len,
226                                               struct slice_mask available,
227                                               int psize, int use_cache)
228 {
229         struct vm_area_struct *vma;
230         unsigned long start_addr, addr, vm_start;
231         struct slice_mask mask;
232         int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
233
234         if (use_cache) {
235                 if (len <= mm->cached_hole_size) {
236                         start_addr = addr = TASK_UNMAPPED_BASE;
237                         mm->cached_hole_size = 0;
238                 } else
239                         start_addr = addr = mm->free_area_cache;
240         } else
241                 start_addr = addr = TASK_UNMAPPED_BASE;
242
243 full_search:
244         for (;;) {
245                 addr = _ALIGN_UP(addr, 1ul << pshift);
246                 if ((TASK_SIZE - len) < addr)
247                         break;
248                 vma = find_vma(mm, addr);
249                 BUG_ON(vma && (addr >= vma->vm_end));
250
251                 mask = slice_range_to_mask(addr, len);
252                 if (!slice_check_fit(mask, available)) {
253                         if (addr < SLICE_LOW_TOP)
254                                 addr = _ALIGN_UP(addr + 1,  1ul << SLICE_LOW_SHIFT);
255                         else
256                                 addr = _ALIGN_UP(addr + 1,  1ul << SLICE_HIGH_SHIFT);
257                         continue;
258                 }
259                 if (vma)
260                         vm_start = vm_start_gap(vma);
261                 if (!vma || addr + len <= vm_start) {
262                         /*
263                          * Remember the place where we stopped the search:
264                          */
265                         if (use_cache)
266                                 mm->free_area_cache = addr + len;
267                         return addr;
268                 }
269                 if (use_cache && (addr + mm->cached_hole_size) < vm_start)
270                         mm->cached_hole_size = vm_start - addr;
271                 addr = vma->vm_end;
272         }
273
274         /* Make sure we didn't miss any holes */
275         if (use_cache && start_addr != TASK_UNMAPPED_BASE) {
276                 start_addr = addr = TASK_UNMAPPED_BASE;
277                 mm->cached_hole_size = 0;
278                 goto full_search;
279         }
280         return -ENOMEM;
281 }
282
283 static unsigned long slice_find_area_topdown(struct mm_struct *mm,
284                                              unsigned long len,
285                                              struct slice_mask available,
286                                              int psize, int use_cache)
287 {
288         struct vm_area_struct *vma;
289         unsigned long addr, vm_start;
290         struct slice_mask mask;
291         int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
292
293         /* check if free_area_cache is useful for us */
294         if (use_cache) {
295                 if (len <= mm->cached_hole_size) {
296                         mm->cached_hole_size = 0;
297                         mm->free_area_cache = mm->mmap_base;
298                 }
299
300                 /* either no address requested or can't fit in requested
301                  * address hole
302                  */
303                 addr = mm->free_area_cache;
304
305                 /* make sure it can fit in the remaining address space */
306                 if (addr > len) {
307                         addr = _ALIGN_DOWN(addr - len, 1ul << pshift);
308                         mask = slice_range_to_mask(addr, len);
309                         if (slice_check_fit(mask, available) &&
310                             slice_area_is_free(mm, addr, len))
311                                         /* remember the address as a hint for
312                                          * next time
313                                          */
314                                         return (mm->free_area_cache = addr);
315                 }
316         }
317
318         addr = mm->mmap_base;
319         while (addr > len) {
320                 /* Go down by chunk size */
321                 addr = _ALIGN_DOWN(addr - len, 1ul << pshift);
322
323                 /* Check for hit with different page size */
324                 mask = slice_range_to_mask(addr, len);
325                 if (!slice_check_fit(mask, available)) {
326                         if (addr < SLICE_LOW_TOP)
327                                 addr = _ALIGN_DOWN(addr, 1ul << SLICE_LOW_SHIFT);
328                         else if (addr < (1ul << SLICE_HIGH_SHIFT))
329                                 addr = SLICE_LOW_TOP;
330                         else
331                                 addr = _ALIGN_DOWN(addr, 1ul << SLICE_HIGH_SHIFT);
332                         continue;
333                 }
334
335                 /*
336                  * Lookup failure means no vma is above this address,
337                  * else if new region fits below vma->vm_start,
338                  * return with success:
339                  */
340                 vma = find_vma(mm, addr);
341                 if (vma)
342                         vm_start = vm_start_gap(vma);
343                 if (!vma || (addr + len) <= vm_start) {
344                         /* remember the address as a hint for next time */
345                         if (use_cache)
346                                 mm->free_area_cache = addr;
347                         return addr;
348                 }
349
350                 /* remember the largest hole we saw so far */
351                 if (use_cache && (addr + mm->cached_hole_size) < vm_start)
352                         mm->cached_hole_size = vm_start - addr;
353
354                 /* try just below the current vma->vm_start */
355                 addr = vm_start;
356         }
357
358         /*
359          * A failed mmap() very likely causes application failure,
360          * so fall back to the bottom-up function here. This scenario
361          * can happen with large stack limits and large mmap()
362          * allocations.
363          */
364         addr = slice_find_area_bottomup(mm, len, available, psize, 0);
365
366         /*
367          * Restore the topdown base:
368          */
369         if (use_cache) {
370                 mm->free_area_cache = mm->mmap_base;
371                 mm->cached_hole_size = ~0UL;
372         }
373
374         return addr;
375 }
376
377
378 static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
379                                      struct slice_mask mask, int psize,
380                                      int topdown, int use_cache)
381 {
382         if (topdown)
383                 return slice_find_area_topdown(mm, len, mask, psize, use_cache);
384         else
385                 return slice_find_area_bottomup(mm, len, mask, psize, use_cache);
386 }
387
388 #define or_mask(dst, src)       do {                    \
389         (dst).low_slices |= (src).low_slices;           \
390         (dst).high_slices |= (src).high_slices;         \
391 } while (0)
392
393 #define andnot_mask(dst, src)   do {                    \
394         (dst).low_slices &= ~(src).low_slices;          \
395         (dst).high_slices &= ~(src).high_slices;        \
396 } while (0)
397
398 #ifdef CONFIG_PPC_64K_PAGES
399 #define MMU_PAGE_BASE   MMU_PAGE_64K
400 #else
401 #define MMU_PAGE_BASE   MMU_PAGE_4K
402 #endif
403
404 unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
405                                       unsigned long flags, unsigned int psize,
406                                       int topdown, int use_cache)
407 {
408         struct slice_mask mask = {0, 0};
409         struct slice_mask good_mask;
410         struct slice_mask potential_mask = {0,0} /* silence stupid warning */;
411         struct slice_mask compat_mask = {0, 0};
412         int fixed = (flags & MAP_FIXED);
413         int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
414         struct mm_struct *mm = current->mm;
415         unsigned long newaddr;
416
417         /* Sanity checks */
418         BUG_ON(mm->task_size == 0);
419
420         slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
421         slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d, use_cache=%d\n",
422                   addr, len, flags, topdown, use_cache);
423
424         if (len > mm->task_size)
425                 return -ENOMEM;
426         if (len & ((1ul << pshift) - 1))
427                 return -EINVAL;
428         if (fixed && (addr & ((1ul << pshift) - 1)))
429                 return -EINVAL;
430         if (fixed && addr > (mm->task_size - len))
431                 return -EINVAL;
432
433         /* If hint, make sure it matches our alignment restrictions */
434         if (!fixed && addr) {
435                 addr = _ALIGN_UP(addr, 1ul << pshift);
436                 slice_dbg(" aligned addr=%lx\n", addr);
437                 /* Ignore hint if it's too large or overlaps a VMA */
438                 if (addr > mm->task_size - len ||
439                     !slice_area_is_free(mm, addr, len))
440                         addr = 0;
441         }
442
443         /* First make up a "good" mask of slices that have the right size
444          * already
445          */
446         good_mask = slice_mask_for_size(mm, psize);
447         slice_print_mask(" good_mask", good_mask);
448
449         /*
450          * Here "good" means slices that are already the right page size,
451          * "compat" means slices that have a compatible page size (i.e.
452          * 4k in a 64k pagesize kernel), and "free" means slices without
453          * any VMAs.
454          *
455          * If MAP_FIXED:
456          *      check if fits in good | compat => OK
457          *      check if fits in good | compat | free => convert free
458          *      else bad
459          * If have hint:
460          *      check if hint fits in good => OK
461          *      check if hint fits in good | free => convert free
462          * Otherwise:
463          *      search in good, found => OK
464          *      search in good | free, found => convert free
465          *      search in good | compat | free, found => convert free.
466          */
467
468 #ifdef CONFIG_PPC_64K_PAGES
469         /* If we support combo pages, we can allow 64k pages in 4k slices */
470         if (psize == MMU_PAGE_64K) {
471                 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
472                 if (fixed)
473                         or_mask(good_mask, compat_mask);
474         }
475 #endif
476
477         /* First check hint if it's valid or if we have MAP_FIXED */
478         if (addr != 0 || fixed) {
479                 /* Build a mask for the requested range */
480                 mask = slice_range_to_mask(addr, len);
481                 slice_print_mask(" mask", mask);
482
483                 /* Check if we fit in the good mask. If we do, we just return,
484                  * nothing else to do
485                  */
486                 if (slice_check_fit(mask, good_mask)) {
487                         slice_dbg(" fits good !\n");
488                         return addr;
489                 }
490         } else {
491                 /* Now let's see if we can find something in the existing
492                  * slices for that size
493                  */
494                 newaddr = slice_find_area(mm, len, good_mask, psize, topdown,
495                                           use_cache);
496                 if (newaddr != -ENOMEM) {
497                         /* Found within the good mask, we don't have to setup,
498                          * we thus return directly
499                          */
500                         slice_dbg(" found area at 0x%lx\n", newaddr);
501                         return newaddr;
502                 }
503         }
504
505         /* We don't fit in the good mask, check what other slices are
506          * empty and thus can be converted
507          */
508         potential_mask = slice_mask_for_free(mm);
509         or_mask(potential_mask, good_mask);
510         slice_print_mask(" potential", potential_mask);
511
512         if ((addr != 0 || fixed) && slice_check_fit(mask, potential_mask)) {
513                 slice_dbg(" fits potential !\n");
514                 goto convert;
515         }
516
517         /* If we have MAP_FIXED and failed the above steps, then error out */
518         if (fixed)
519                 return -EBUSY;
520
521         slice_dbg(" search...\n");
522
523         /* If we had a hint that didn't work out, see if we can fit
524          * anywhere in the good area.
525          */
526         if (addr) {
527                 addr = slice_find_area(mm, len, good_mask, psize, topdown,
528                                        use_cache);
529                 if (addr != -ENOMEM) {
530                         slice_dbg(" found area at 0x%lx\n", addr);
531                         return addr;
532                 }
533         }
534
535         /* Now let's see if we can find something in the existing slices
536          * for that size plus free slices
537          */
538         addr = slice_find_area(mm, len, potential_mask, psize, topdown,
539                                use_cache);
540
541 #ifdef CONFIG_PPC_64K_PAGES
542         if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
543                 /* retry the search with 4k-page slices included */
544                 or_mask(potential_mask, compat_mask);
545                 addr = slice_find_area(mm, len, potential_mask, psize,
546                                        topdown, use_cache);
547         }
548 #endif
549
550         if (addr == -ENOMEM)
551                 return -ENOMEM;
552
553         mask = slice_range_to_mask(addr, len);
554         slice_dbg(" found potential area at 0x%lx\n", addr);
555         slice_print_mask(" mask", mask);
556
557  convert:
558         andnot_mask(mask, good_mask);
559         andnot_mask(mask, compat_mask);
560         if (mask.low_slices || mask.high_slices) {
561                 slice_convert(mm, mask, psize);
562                 if (psize > MMU_PAGE_BASE)
563                         on_each_cpu(slice_flush_segments, mm, 1);
564         }
565         return addr;
566
567 }
568 EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
569
570 unsigned long arch_get_unmapped_area(struct file *filp,
571                                      unsigned long addr,
572                                      unsigned long len,
573                                      unsigned long pgoff,
574                                      unsigned long flags)
575 {
576         return slice_get_unmapped_area(addr, len, flags,
577                                        current->mm->context.user_psize,
578                                        0, 1);
579 }
580
581 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
582                                              const unsigned long addr0,
583                                              const unsigned long len,
584                                              const unsigned long pgoff,
585                                              const unsigned long flags)
586 {
587         return slice_get_unmapped_area(addr0, len, flags,
588                                        current->mm->context.user_psize,
589                                        1, 1);
590 }
591
592 unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
593 {
594         u64 psizes;
595         int index;
596
597         if (addr < SLICE_LOW_TOP) {
598                 psizes = mm->context.low_slices_psize;
599                 index = GET_LOW_SLICE_INDEX(addr);
600         } else {
601                 psizes = mm->context.high_slices_psize;
602                 index = GET_HIGH_SLICE_INDEX(addr);
603         }
604
605         return (psizes >> (index * 4)) & 0xf;
606 }
607 EXPORT_SYMBOL_GPL(get_slice_psize);
608
609 /*
610  * This is called by hash_page when it needs to do a lazy conversion of
611  * an address space from real 64K pages to combo 4K pages (typically
612  * when hitting a non cacheable mapping on a processor or hypervisor
613  * that won't allow them for 64K pages).
614  *
615  * This is also called in init_new_context() to change back the user
616  * psize from whatever the parent context had it set to
617  * N.B. This may be called before mm->context.id has been set.
618  *
619  * This function will only change the content of the {low,high)_slice_psize
620  * masks, it will not flush SLBs as this shall be handled lazily by the
621  * caller.
622  */
623 void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
624 {
625         unsigned long flags, lpsizes, hpsizes;
626         unsigned int old_psize;
627         int i;
628
629         slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
630
631         spin_lock_irqsave(&slice_convert_lock, flags);
632
633         old_psize = mm->context.user_psize;
634         slice_dbg(" old_psize=%d\n", old_psize);
635         if (old_psize == psize)
636                 goto bail;
637
638         mm->context.user_psize = psize;
639         wmb();
640
641         lpsizes = mm->context.low_slices_psize;
642         for (i = 0; i < SLICE_NUM_LOW; i++)
643                 if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
644                         lpsizes = (lpsizes & ~(0xful << (i * 4))) |
645                                 (((unsigned long)psize) << (i * 4));
646
647         hpsizes = mm->context.high_slices_psize;
648         for (i = 0; i < SLICE_NUM_HIGH; i++)
649                 if (((hpsizes >> (i * 4)) & 0xf) == old_psize)
650                         hpsizes = (hpsizes & ~(0xful << (i * 4))) |
651                                 (((unsigned long)psize) << (i * 4));
652
653         mm->context.low_slices_psize = lpsizes;
654         mm->context.high_slices_psize = hpsizes;
655
656         slice_dbg(" lsps=%lx, hsps=%lx\n",
657                   mm->context.low_slices_psize,
658                   mm->context.high_slices_psize);
659
660  bail:
661         spin_unlock_irqrestore(&slice_convert_lock, flags);
662 }
663
664 void slice_set_psize(struct mm_struct *mm, unsigned long address,
665                      unsigned int psize)
666 {
667         unsigned long i, flags;
668         u64 *p;
669
670         spin_lock_irqsave(&slice_convert_lock, flags);
671         if (address < SLICE_LOW_TOP) {
672                 i = GET_LOW_SLICE_INDEX(address);
673                 p = &mm->context.low_slices_psize;
674         } else {
675                 i = GET_HIGH_SLICE_INDEX(address);
676                 p = &mm->context.high_slices_psize;
677         }
678         *p = (*p & ~(0xful << (i * 4))) | ((unsigned long) psize << (i * 4));
679         spin_unlock_irqrestore(&slice_convert_lock, flags);
680
681 #ifdef CONFIG_SPU_BASE
682         spu_flush_all_slbs(mm);
683 #endif
684 }
685
686 void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
687                            unsigned long len, unsigned int psize)
688 {
689         struct slice_mask mask = slice_range_to_mask(start, len);
690
691         slice_convert(mm, mask, psize);
692 }
693
694 /*
695  * is_hugepage_only_range() is used by generic code to verify wether
696  * a normal mmap mapping (non hugetlbfs) is valid on a given area.
697  *
698  * until the generic code provides a more generic hook and/or starts
699  * calling arch get_unmapped_area for MAP_FIXED (which our implementation
700  * here knows how to deal with), we hijack it to keep standard mappings
701  * away from us.
702  *
703  * because of that generic code limitation, MAP_FIXED mapping cannot
704  * "convert" back a slice with no VMAs to the standard page size, only
705  * get_unmapped_area() can. It would be possible to fix it here but I
706  * prefer working on fixing the generic code instead.
707  *
708  * WARNING: This will not work if hugetlbfs isn't enabled since the
709  * generic code will redefine that function as 0 in that. This is ok
710  * for now as we only use slices with hugetlbfs enabled. This should
711  * be fixed as the generic code gets fixed.
712  */
713 int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
714                            unsigned long len)
715 {
716         struct slice_mask mask, available;
717         unsigned int psize = mm->context.user_psize;
718
719         mask = slice_range_to_mask(addr, len);
720         available = slice_mask_for_size(mm, psize);
721 #ifdef CONFIG_PPC_64K_PAGES
722         /* We need to account for 4k slices too */
723         if (psize == MMU_PAGE_64K) {
724                 struct slice_mask compat_mask;
725                 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
726                 or_mask(available, compat_mask);
727         }
728 #endif
729
730 #if 0 /* too verbose */
731         slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
732                  mm, addr, len);
733         slice_print_mask(" mask", mask);
734         slice_print_mask(" available", available);
735 #endif
736         return !slice_check_fit(mask, available);
737 }
738