blob: bf150557dba82f51e16955bf0e3da598813a059e [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>
Anton Blanchard1217d342014-08-20 08:55:19 +100033#include <linux/hugetlb.h>
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100034#include <asm/mman.h>
35#include <asm/mmu.h>
Ian Munsiebe3ebfe2014-10-08 19:54:52 +110036#include <asm/copro.h>
Anton Blanchard1217d342014-08-20 08:55:19 +100037#include <asm/hugetlb.h>
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100038
Aneesh Kumar K.V78f1dbd2012-09-10 02:52:57 +000039/* some sanity checks */
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +100040#if (H_PGTABLE_RANGE >> 43) > SLICE_MASK_SIZE
41#error H_PGTABLE_RANGE exceeds slice_mask high_slices size
Aneesh Kumar K.V78f1dbd2012-09-10 02:52:57 +000042#endif
43
Roel Kluinf7a75f02007-10-16 23:30:25 -070044static DEFINE_SPINLOCK(slice_convert_lock);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100045
46
47#ifdef DEBUG
48int _slice_debug = 1;
49
50static void slice_print_mask(const char *label, struct slice_mask mask)
51{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +000052 char *p, buf[16 + 3 + 64 + 1];
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100053 int i;
54
55 if (!_slice_debug)
56 return;
57 p = buf;
58 for (i = 0; i < SLICE_NUM_LOW; i++)
59 *(p++) = (mask.low_slices & (1 << i)) ? '1' : '0';
60 *(p++) = ' ';
61 *(p++) = '-';
62 *(p++) = ' ';
63 for (i = 0; i < SLICE_NUM_HIGH; i++)
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +000064 *(p++) = (mask.high_slices & (1ul << i)) ? '1' : '0';
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100065 *(p++) = 0;
66
67 printk(KERN_DEBUG "%s:%s\n", label, buf);
68}
69
70#define slice_dbg(fmt...) do { if (_slice_debug) pr_debug(fmt); } while(0)
71
72#else
73
74static void slice_print_mask(const char *label, struct slice_mask mask) {}
75#define slice_dbg(fmt...)
76
77#endif
78
79static struct slice_mask slice_range_to_mask(unsigned long start,
80 unsigned long len)
81{
82 unsigned long end = start + len - 1;
83 struct slice_mask ret = { 0, 0 };
84
85 if (start < SLICE_LOW_TOP) {
Aneesh Kumar K.V98beda72017-03-21 22:59:52 +053086 unsigned long mend = min(end, (SLICE_LOW_TOP - 1));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100087
88 ret.low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
Aneesh Kumar K.V98beda72017-03-21 22:59:52 +053089 - (1u << GET_LOW_SLICE_INDEX(start));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100090 }
91
92 if ((start + len) > SLICE_LOW_TOP)
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +000093 ret.high_slices = (1ul << (GET_HIGH_SLICE_INDEX(end) + 1))
94 - (1ul << GET_HIGH_SLICE_INDEX(start));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100095
96 return ret;
97}
98
99static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
100 unsigned long len)
101{
102 struct vm_area_struct *vma;
103
104 if ((mm->task_size - len) < addr)
105 return 0;
106 vma = find_vma(mm, addr);
107 return (!vma || (addr + len) <= vma->vm_start);
108}
109
110static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
111{
112 return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
113 1ul << SLICE_LOW_SHIFT);
114}
115
116static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
117{
118 unsigned long start = slice << SLICE_HIGH_SHIFT;
119 unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
120
121 /* Hack, so that each addresses is controlled by exactly one
122 * of the high or low area bitmaps, the first high area starts
123 * at 4GB, not 0 */
124 if (start == 0)
125 start = SLICE_LOW_TOP;
126
127 return !slice_area_is_free(mm, start, end - start);
128}
129
130static struct slice_mask slice_mask_for_free(struct mm_struct *mm)
131{
132 struct slice_mask ret = { 0, 0 };
133 unsigned long i;
134
135 for (i = 0; i < SLICE_NUM_LOW; i++)
136 if (!slice_low_has_vma(mm, i))
137 ret.low_slices |= 1u << i;
138
139 if (mm->task_size <= SLICE_LOW_TOP)
140 return ret;
141
142 for (i = 0; i < SLICE_NUM_HIGH; i++)
143 if (!slice_high_has_vma(mm, i))
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000144 ret.high_slices |= 1ul << i;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000145
146 return ret;
147}
148
149static struct slice_mask slice_mask_for_size(struct mm_struct *mm, int psize)
150{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000151 unsigned char *hpsizes;
152 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000153 struct slice_mask ret = { 0, 0 };
154 unsigned long i;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000155 u64 lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000156
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000157 lpsizes = mm->context.low_slices_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000158 for (i = 0; i < SLICE_NUM_LOW; i++)
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000159 if (((lpsizes >> (i * 4)) & 0xf) == psize)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000160 ret.low_slices |= 1u << i;
161
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000162 hpsizes = mm->context.high_slices_psize;
163 for (i = 0; i < SLICE_NUM_HIGH; i++) {
164 mask_index = i & 0x1;
165 index = i >> 1;
166 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
167 ret.high_slices |= 1ul << i;
168 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000169
170 return ret;
171}
172
173static int slice_check_fit(struct slice_mask mask, struct slice_mask available)
174{
175 return (mask.low_slices & available.low_slices) == mask.low_slices &&
176 (mask.high_slices & available.high_slices) == mask.high_slices;
177}
178
179static void slice_flush_segments(void *parm)
180{
181 struct mm_struct *mm = parm;
182 unsigned long flags;
183
184 if (mm != current->active_mm)
185 return;
186
Michael Neulingc395465da62015-10-28 15:54:06 +1100187 copy_mm_to_paca(&current->active_mm->context);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000188
189 local_irq_save(flags);
190 slb_flush_and_rebolt();
191 local_irq_restore(flags);
192}
193
194static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
195{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000196 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000197 /* Write the new slice psize bits */
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000198 unsigned char *hpsizes;
199 u64 lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000200 unsigned long i, flags;
201
202 slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
203 slice_print_mask(" mask", mask);
204
205 /* We need to use a spinlock here to protect against
206 * concurrent 64k -> 4k demotion ...
207 */
208 spin_lock_irqsave(&slice_convert_lock, flags);
209
210 lpsizes = mm->context.low_slices_psize;
211 for (i = 0; i < SLICE_NUM_LOW; i++)
212 if (mask.low_slices & (1u << i))
213 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
214 (((unsigned long)psize) << (i * 4));
215
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000216 /* Assign the value back */
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000217 mm->context.low_slices_psize = lpsizes;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000218
219 hpsizes = mm->context.high_slices_psize;
220 for (i = 0; i < SLICE_NUM_HIGH; i++) {
221 mask_index = i & 0x1;
222 index = i >> 1;
223 if (mask.high_slices & (1ul << i))
224 hpsizes[index] = (hpsizes[index] &
225 ~(0xf << (mask_index * 4))) |
226 (((unsigned long)psize) << (mask_index * 4));
227 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000228
229 slice_dbg(" lsps=%lx, hsps=%lx\n",
230 mm->context.low_slices_psize,
231 mm->context.high_slices_psize);
232
233 spin_unlock_irqrestore(&slice_convert_lock, flags);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000234
Ian Munsiebe3ebfe2014-10-08 19:54:52 +1100235 copro_flush_all_slbs(mm);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000236}
237
Michel Lespinassefba23692013-04-29 11:53:53 -0700238/*
239 * Compute which slice addr is part of;
240 * set *boundary_addr to the start or end boundary of that slice
241 * (depending on 'end' parameter);
242 * return boolean indicating if the slice is marked as available in the
243 * 'available' slice_mark.
244 */
245static bool slice_scan_available(unsigned long addr,
246 struct slice_mask available,
247 int end,
248 unsigned long *boundary_addr)
249{
250 unsigned long slice;
251 if (addr < SLICE_LOW_TOP) {
252 slice = GET_LOW_SLICE_INDEX(addr);
253 *boundary_addr = (slice + end) << SLICE_LOW_SHIFT;
254 return !!(available.low_slices & (1u << slice));
255 } else {
256 slice = GET_HIGH_SLICE_INDEX(addr);
257 *boundary_addr = (slice + end) ?
258 ((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP;
Anton Blanchard5a049f12013-11-18 14:55:28 +1100259 return !!(available.high_slices & (1ul << slice));
Michel Lespinassefba23692013-04-29 11:53:53 -0700260 }
261}
262
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000263static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
264 unsigned long len,
265 struct slice_mask available,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700266 int psize)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000267{
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000268 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
Michel Lespinassefba23692013-04-29 11:53:53 -0700269 unsigned long addr, found, next_end;
270 struct vm_unmapped_area_info info;
271
272 info.flags = 0;
273 info.length = len;
274 info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
275 info.align_offset = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000276
Michel Lespinasse34d07172013-04-29 11:53:52 -0700277 addr = TASK_UNMAPPED_BASE;
Michel Lespinassefba23692013-04-29 11:53:53 -0700278 while (addr < TASK_SIZE) {
279 info.low_limit = addr;
280 if (!slice_scan_available(addr, available, 1, &addr))
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000281 continue;
Michel Lespinassefba23692013-04-29 11:53:53 -0700282
283 next_slice:
284 /*
285 * At this point [info.low_limit; addr) covers
286 * available slices only and ends at a slice boundary.
287 * Check if we need to reduce the range, or if we can
288 * extend it to cover the next available slice.
289 */
290 if (addr >= TASK_SIZE)
291 addr = TASK_SIZE;
292 else if (slice_scan_available(addr, available, 1, &next_end)) {
293 addr = next_end;
294 goto next_slice;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000295 }
Michel Lespinassefba23692013-04-29 11:53:53 -0700296 info.high_limit = addr;
297
298 found = vm_unmapped_area(&info);
299 if (!(found & ~PAGE_MASK))
300 return found;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000301 }
302
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000303 return -ENOMEM;
304}
305
306static unsigned long slice_find_area_topdown(struct mm_struct *mm,
307 unsigned long len,
308 struct slice_mask available,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700309 int psize)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000310{
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000311 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
Michel Lespinassefba23692013-04-29 11:53:53 -0700312 unsigned long addr, found, prev;
313 struct vm_unmapped_area_info info;
314
315 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
316 info.length = len;
317 info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
318 info.align_offset = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000319
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000320 addr = mm->mmap_base;
Michel Lespinassefba23692013-04-29 11:53:53 -0700321 while (addr > PAGE_SIZE) {
322 info.high_limit = addr;
323 if (!slice_scan_available(addr - 1, available, 0, &addr))
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000324 continue;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000325
Michel Lespinassefba23692013-04-29 11:53:53 -0700326 prev_slice:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000327 /*
Michel Lespinassefba23692013-04-29 11:53:53 -0700328 * At this point [addr; info.high_limit) covers
329 * available slices only and starts at a slice boundary.
330 * Check if we need to reduce the range, or if we can
331 * extend it to cover the previous available slice.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000332 */
Michel Lespinassefba23692013-04-29 11:53:53 -0700333 if (addr < PAGE_SIZE)
334 addr = PAGE_SIZE;
335 else if (slice_scan_available(addr - 1, available, 0, &prev)) {
336 addr = prev;
337 goto prev_slice;
338 }
339 info.low_limit = addr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000340
Michel Lespinassefba23692013-04-29 11:53:53 -0700341 found = vm_unmapped_area(&info);
342 if (!(found & ~PAGE_MASK))
343 return found;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000344 }
345
346 /*
347 * A failed mmap() very likely causes application failure,
348 * so fall back to the bottom-up function here. This scenario
349 * can happen with large stack limits and large mmap()
350 * allocations.
351 */
Michel Lespinasse34d07172013-04-29 11:53:52 -0700352 return slice_find_area_bottomup(mm, len, available, psize);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000353}
354
355
356static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
357 struct slice_mask mask, int psize,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700358 int topdown)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000359{
360 if (topdown)
Michel Lespinasse34d07172013-04-29 11:53:52 -0700361 return slice_find_area_topdown(mm, len, mask, psize);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000362 else
Michel Lespinasse34d07172013-04-29 11:53:52 -0700363 return slice_find_area_bottomup(mm, len, mask, psize);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000364}
365
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000366#define or_mask(dst, src) do { \
367 (dst).low_slices |= (src).low_slices; \
368 (dst).high_slices |= (src).high_slices; \
369} while (0)
370
371#define andnot_mask(dst, src) do { \
372 (dst).low_slices &= ~(src).low_slices; \
373 (dst).high_slices &= ~(src).high_slices; \
374} while (0)
375
376#ifdef CONFIG_PPC_64K_PAGES
377#define MMU_PAGE_BASE MMU_PAGE_64K
378#else
379#define MMU_PAGE_BASE MMU_PAGE_4K
380#endif
381
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000382unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
383 unsigned long flags, unsigned int psize,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700384 int topdown)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000385{
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000386 struct slice_mask mask = {0, 0};
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000387 struct slice_mask good_mask;
388 struct slice_mask potential_mask = {0,0} /* silence stupid warning */;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000389 struct slice_mask compat_mask = {0, 0};
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000390 int fixed = (flags & MAP_FIXED);
391 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
392 struct mm_struct *mm = current->mm;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000393 unsigned long newaddr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000394
395 /* Sanity checks */
396 BUG_ON(mm->task_size == 0);
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000397 VM_BUG_ON(radix_enabled());
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000398
399 slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
Michel Lespinasse34d07172013-04-29 11:53:52 -0700400 slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
401 addr, len, flags, topdown);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000402
403 if (len > mm->task_size)
404 return -ENOMEM;
Benjamin Herrenschmidtd1f5a772007-08-08 15:44:15 +1000405 if (len & ((1ul << pshift) - 1))
406 return -EINVAL;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000407 if (fixed && (addr & ((1ul << pshift) - 1)))
408 return -EINVAL;
409 if (fixed && addr > (mm->task_size - len))
jmarchan@redhat.com19751c02014-01-15 16:27:11 +0100410 return -ENOMEM;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000411
412 /* If hint, make sure it matches our alignment restrictions */
413 if (!fixed && addr) {
414 addr = _ALIGN_UP(addr, 1ul << pshift);
415 slice_dbg(" aligned addr=%lx\n", addr);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000416 /* Ignore hint if it's too large or overlaps a VMA */
417 if (addr > mm->task_size - len ||
418 !slice_area_is_free(mm, addr, len))
419 addr = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000420 }
421
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000422 /* First make up a "good" mask of slices that have the right size
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000423 * already
424 */
425 good_mask = slice_mask_for_size(mm, psize);
426 slice_print_mask(" good_mask", good_mask);
427
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000428 /*
429 * Here "good" means slices that are already the right page size,
430 * "compat" means slices that have a compatible page size (i.e.
431 * 4k in a 64k pagesize kernel), and "free" means slices without
432 * any VMAs.
433 *
434 * If MAP_FIXED:
435 * check if fits in good | compat => OK
436 * check if fits in good | compat | free => convert free
437 * else bad
438 * If have hint:
439 * check if hint fits in good => OK
440 * check if hint fits in good | free => convert free
441 * Otherwise:
442 * search in good, found => OK
443 * search in good | free, found => convert free
444 * search in good | compat | free, found => convert free.
445 */
446
447#ifdef CONFIG_PPC_64K_PAGES
448 /* If we support combo pages, we can allow 64k pages in 4k slices */
449 if (psize == MMU_PAGE_64K) {
450 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
451 if (fixed)
452 or_mask(good_mask, compat_mask);
453 }
454#endif
455
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000456 /* First check hint if it's valid or if we have MAP_FIXED */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000457 if (addr != 0 || fixed) {
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000458 /* Build a mask for the requested range */
459 mask = slice_range_to_mask(addr, len);
460 slice_print_mask(" mask", mask);
461
462 /* Check if we fit in the good mask. If we do, we just return,
463 * nothing else to do
464 */
465 if (slice_check_fit(mask, good_mask)) {
466 slice_dbg(" fits good !\n");
467 return addr;
468 }
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000469 } else {
470 /* Now let's see if we can find something in the existing
471 * slices for that size
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000472 */
Michel Lespinasse34d07172013-04-29 11:53:52 -0700473 newaddr = slice_find_area(mm, len, good_mask, psize, topdown);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000474 if (newaddr != -ENOMEM) {
475 /* Found within the good mask, we don't have to setup,
476 * we thus return directly
477 */
478 slice_dbg(" found area at 0x%lx\n", newaddr);
479 return newaddr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000480 }
481 }
482
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000483 /* We don't fit in the good mask, check what other slices are
484 * empty and thus can be converted
485 */
486 potential_mask = slice_mask_for_free(mm);
487 or_mask(potential_mask, good_mask);
488 slice_print_mask(" potential", potential_mask);
489
490 if ((addr != 0 || fixed) && slice_check_fit(mask, potential_mask)) {
491 slice_dbg(" fits potential !\n");
492 goto convert;
493 }
494
495 /* If we have MAP_FIXED and failed the above steps, then error out */
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000496 if (fixed)
497 return -EBUSY;
498
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000499 slice_dbg(" search...\n");
500
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000501 /* If we had a hint that didn't work out, see if we can fit
502 * anywhere in the good area.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000503 */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000504 if (addr) {
Michel Lespinasse34d07172013-04-29 11:53:52 -0700505 addr = slice_find_area(mm, len, good_mask, psize, topdown);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000506 if (addr != -ENOMEM) {
507 slice_dbg(" found area at 0x%lx\n", addr);
508 return addr;
509 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000510 }
511
512 /* Now let's see if we can find something in the existing slices
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000513 * for that size plus free slices
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000514 */
Michel Lespinasse34d07172013-04-29 11:53:52 -0700515 addr = slice_find_area(mm, len, potential_mask, psize, topdown);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000516
517#ifdef CONFIG_PPC_64K_PAGES
518 if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
519 /* retry the search with 4k-page slices included */
520 or_mask(potential_mask, compat_mask);
521 addr = slice_find_area(mm, len, potential_mask, psize,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700522 topdown);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000523 }
524#endif
525
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000526 if (addr == -ENOMEM)
527 return -ENOMEM;
528
529 mask = slice_range_to_mask(addr, len);
530 slice_dbg(" found potential area at 0x%lx\n", addr);
531 slice_print_mask(" mask", mask);
532
533 convert:
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000534 andnot_mask(mask, good_mask);
535 andnot_mask(mask, compat_mask);
536 if (mask.low_slices || mask.high_slices) {
537 slice_convert(mm, mask, psize);
538 if (psize > MMU_PAGE_BASE)
Benjamin Herrenschmidt84c3d4a2008-07-16 11:07:59 +1000539 on_each_cpu(slice_flush_segments, mm, 1);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000540 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000541 return addr;
542
543}
544EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
545
546unsigned long arch_get_unmapped_area(struct file *filp,
547 unsigned long addr,
548 unsigned long len,
549 unsigned long pgoff,
550 unsigned long flags)
551{
552 return slice_get_unmapped_area(addr, len, flags,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700553 current->mm->context.user_psize, 0);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000554}
555
556unsigned long arch_get_unmapped_area_topdown(struct file *filp,
557 const unsigned long addr0,
558 const unsigned long len,
559 const unsigned long pgoff,
560 const unsigned long flags)
561{
562 return slice_get_unmapped_area(addr0, len, flags,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700563 current->mm->context.user_psize, 1);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000564}
565
566unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
567{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000568 unsigned char *hpsizes;
569 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000570
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000571 /*
572 * Radix doesn't use slice, but can get enabled along with MMU_SLICE
573 */
574 if (radix_enabled()) {
575#ifdef CONFIG_PPC_64K_PAGES
576 return MMU_PAGE_64K;
577#else
578 return MMU_PAGE_4K;
579#endif
580 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000581 if (addr < SLICE_LOW_TOP) {
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000582 u64 lpsizes;
583 lpsizes = mm->context.low_slices_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000584 index = GET_LOW_SLICE_INDEX(addr);
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000585 return (lpsizes >> (index * 4)) & 0xf;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000586 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000587 hpsizes = mm->context.high_slices_psize;
588 index = GET_HIGH_SLICE_INDEX(addr);
589 mask_index = index & 0x1;
590 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000591}
592EXPORT_SYMBOL_GPL(get_slice_psize);
593
594/*
595 * This is called by hash_page when it needs to do a lazy conversion of
596 * an address space from real 64K pages to combo 4K pages (typically
597 * when hitting a non cacheable mapping on a processor or hypervisor
598 * that won't allow them for 64K pages).
599 *
600 * This is also called in init_new_context() to change back the user
601 * psize from whatever the parent context had it set to
Stephen Rothwell9dfe5c532007-08-15 16:33:55 +1000602 * N.B. This may be called before mm->context.id has been set.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000603 *
604 * This function will only change the content of the {low,high)_slice_psize
605 * masks, it will not flush SLBs as this shall be handled lazily by the
606 * caller.
607 */
608void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
609{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000610 int index, mask_index;
611 unsigned char *hpsizes;
612 unsigned long flags, lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000613 unsigned int old_psize;
614 int i;
615
616 slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
617
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000618 VM_BUG_ON(radix_enabled());
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000619 spin_lock_irqsave(&slice_convert_lock, flags);
620
621 old_psize = mm->context.user_psize;
622 slice_dbg(" old_psize=%d\n", old_psize);
623 if (old_psize == psize)
624 goto bail;
625
626 mm->context.user_psize = psize;
627 wmb();
628
629 lpsizes = mm->context.low_slices_psize;
630 for (i = 0; i < SLICE_NUM_LOW; i++)
631 if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
632 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
633 (((unsigned long)psize) << (i * 4));
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000634 /* Assign the value back */
635 mm->context.low_slices_psize = lpsizes;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000636
637 hpsizes = mm->context.high_slices_psize;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000638 for (i = 0; i < SLICE_NUM_HIGH; i++) {
639 mask_index = i & 0x1;
640 index = i >> 1;
641 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize)
642 hpsizes[index] = (hpsizes[index] &
643 ~(0xf << (mask_index * 4))) |
644 (((unsigned long)psize) << (mask_index * 4));
645 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000646
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000647
648
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000649
650 slice_dbg(" lsps=%lx, hsps=%lx\n",
651 mm->context.low_slices_psize,
652 mm->context.high_slices_psize);
653
654 bail:
655 spin_unlock_irqrestore(&slice_convert_lock, flags);
656}
657
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000658void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
659 unsigned long len, unsigned int psize)
660{
661 struct slice_mask mask = slice_range_to_mask(start, len);
662
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000663 VM_BUG_ON(radix_enabled());
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000664 slice_convert(mm, mask, psize);
665}
666
Aneesh Kumar K.V66437732014-10-21 14:25:38 +1100667#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000668/*
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400669 * is_hugepage_only_range() is used by generic code to verify whether
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000670 * a normal mmap mapping (non hugetlbfs) is valid on a given area.
671 *
672 * until the generic code provides a more generic hook and/or starts
673 * calling arch get_unmapped_area for MAP_FIXED (which our implementation
674 * here knows how to deal with), we hijack it to keep standard mappings
675 * away from us.
676 *
677 * because of that generic code limitation, MAP_FIXED mapping cannot
678 * "convert" back a slice with no VMAs to the standard page size, only
679 * get_unmapped_area() can. It would be possible to fix it here but I
680 * prefer working on fixing the generic code instead.
681 *
682 * WARNING: This will not work if hugetlbfs isn't enabled since the
683 * generic code will redefine that function as 0 in that. This is ok
684 * for now as we only use slices with hugetlbfs enabled. This should
685 * be fixed as the generic code gets fixed.
686 */
687int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
688 unsigned long len)
689{
690 struct slice_mask mask, available;
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000691 unsigned int psize = mm->context.user_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000692
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000693 if (radix_enabled())
694 return 0;
695
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000696 mask = slice_range_to_mask(addr, len);
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000697 available = slice_mask_for_size(mm, psize);
698#ifdef CONFIG_PPC_64K_PAGES
699 /* We need to account for 4k slices too */
700 if (psize == MMU_PAGE_64K) {
701 struct slice_mask compat_mask;
702 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
703 or_mask(available, compat_mask);
704 }
705#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000706
707#if 0 /* too verbose */
708 slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
709 mm, addr, len);
710 slice_print_mask(" mask", mask);
711 slice_print_mask(" available", available);
712#endif
713 return !slice_check_fit(mask, available);
714}
Aneesh Kumar K.V66437732014-10-21 14:25:38 +1100715#endif