blob: 3e199b9cbbfdbb4daeafea0108df8426db10e137 [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
Roel Kluinf7a75f02007-10-16 23:30:25 -070039static DEFINE_SPINLOCK(slice_convert_lock);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100040
41#ifdef DEBUG
42int _slice_debug = 1;
43
Nicholas Piggin830fd2d2018-03-07 11:37:11 +100044static void slice_print_mask(const char *label, const struct slice_mask *mask)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100045{
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100046 if (!_slice_debug)
47 return;
Nicholas Piggin830fd2d2018-03-07 11:37:11 +100048 pr_devel("%s low_slice: %*pbl\n", label,
49 (int)SLICE_NUM_LOW, &mask->low_slices);
50 pr_devel("%s high_slice: %*pbl\n", label,
51 (int)SLICE_NUM_HIGH, mask->high_slices);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100052}
53
Aneesh Kumar K.V302413c2017-03-22 09:06:52 +053054#define slice_dbg(fmt...) do { if (_slice_debug) pr_devel(fmt); } while (0)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100055
56#else
57
Nicholas Piggin830fd2d2018-03-07 11:37:11 +100058static void slice_print_mask(const char *label, const struct slice_mask *mask) {}
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100059#define slice_dbg(fmt...)
60
61#endif
62
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +053063static void slice_range_to_mask(unsigned long start, unsigned long len,
64 struct slice_mask *ret)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100065{
66 unsigned long end = start + len - 1;
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +053067
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +053068 ret->low_slices = 0;
Christophe Leroydb3a5282018-02-22 15:27:24 +010069 if (SLICE_NUM_HIGH)
70 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100071
72 if (start < SLICE_LOW_TOP) {
Christophe Leroydb3a5282018-02-22 15:27:24 +010073 unsigned long mend = min(end,
74 (unsigned long)(SLICE_LOW_TOP - 1));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100075
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +053076 ret->low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
Aneesh Kumar K.V98beda72017-03-21 22:59:52 +053077 - (1u << GET_LOW_SLICE_INDEX(start));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100078 }
79
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +053080 if ((start + len) > SLICE_LOW_TOP) {
81 unsigned long start_index = GET_HIGH_SLICE_INDEX(start);
82 unsigned long align_end = ALIGN(end, (1UL << SLICE_HIGH_SHIFT));
83 unsigned long count = GET_HIGH_SLICE_INDEX(align_end) - start_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100084
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +053085 bitmap_set(ret->high_slices, start_index, count);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +053086 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100087}
88
89static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
90 unsigned long len)
91{
92 struct vm_area_struct *vma;
93
Nicholas Piggin47224762017-11-10 04:27:40 +110094 if ((mm->context.slb_addr_limit - len) < addr)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100095 return 0;
96 vma = find_vma(mm, addr);
Hugh Dickins1be71072017-06-19 04:03:24 -070097 return (!vma || (addr + len) <= vm_start_gap(vma));
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100098}
99
100static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
101{
102 return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
103 1ul << SLICE_LOW_SHIFT);
104}
105
106static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
107{
108 unsigned long start = slice << SLICE_HIGH_SHIFT;
109 unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
110
Christophe Leroydb3a5282018-02-22 15:27:24 +0100111#ifdef CONFIG_PPC64
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000112 /* Hack, so that each addresses is controlled by exactly one
113 * of the high or low area bitmaps, the first high area starts
114 * at 4GB, not 0 */
115 if (start == 0)
116 start = SLICE_LOW_TOP;
Christophe Leroydb3a5282018-02-22 15:27:24 +0100117#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000118
119 return !slice_area_is_free(mm, start, end - start);
120}
121
Aneesh Kumar K.V7a06c662017-11-10 10:25:07 +0530122static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret,
123 unsigned long high_limit)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000124{
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000125 unsigned long i;
126
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530127 ret->low_slices = 0;
Christophe Leroydb3a5282018-02-22 15:27:24 +0100128 if (SLICE_NUM_HIGH)
129 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530130
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000131 for (i = 0; i < SLICE_NUM_LOW; i++)
132 if (!slice_low_has_vma(mm, i))
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530133 ret->low_slices |= 1u << i;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000134
Aneesh Kumar K.V7a06c662017-11-10 10:25:07 +0530135 if (high_limit <= SLICE_LOW_TOP)
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530136 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000137
Aneesh Kumar K.V7a06c662017-11-10 10:25:07 +0530138 for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000139 if (!slice_high_has_vma(mm, i))
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530140 __set_bit(i, ret->high_slices);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000141}
142
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000143#ifdef CONFIG_PPC_BOOK3S_64
144static struct slice_mask *slice_mask_for_size(struct mm_struct *mm, int psize)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000145{
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000146#ifdef CONFIG_PPC_64K_PAGES
147 if (psize == MMU_PAGE_64K)
148 return &mm->context.mask_64k;
149#endif
150 if (psize == MMU_PAGE_4K)
151 return &mm->context.mask_4k;
152#ifdef CONFIG_HUGETLB_PAGE
153 if (psize == MMU_PAGE_16M)
154 return &mm->context.mask_16m;
155 if (psize == MMU_PAGE_16G)
156 return &mm->context.mask_16g;
157#endif
158 BUG();
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000159}
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000160#elif defined(CONFIG_PPC_8xx)
161static struct slice_mask *slice_mask_for_size(struct mm_struct *mm, int psize)
162{
163 if (psize == mmu_virtual_psize)
164 return &mm->context.mask_base_psize;
165#ifdef CONFIG_HUGETLB_PAGE
166 if (psize == MMU_PAGE_512K)
167 return &mm->context.mask_512k;
168 if (psize == MMU_PAGE_8M)
169 return &mm->context.mask_8m;
170#endif
171 BUG();
172}
173#else
174#error "Must define the slice masks for page sizes supported by the platform"
175#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000176
Aneesh Kumar K.V957b7782017-03-22 09:06:58 +0530177static int slice_check_fit(struct mm_struct *mm,
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000178 const struct slice_mask *mask,
179 const struct slice_mask *available)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000180{
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530181 DECLARE_BITMAP(result, SLICE_NUM_HIGH);
Aneesh Kumar K.V7a06c662017-11-10 10:25:07 +0530182 /*
183 * Make sure we just do bit compare only to the max
184 * addr limit and not the full bit map size.
185 */
Nicholas Piggin47224762017-11-10 04:27:40 +1100186 unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530187
Christophe Leroydb3a5282018-02-22 15:27:24 +0100188 if (!SLICE_NUM_HIGH)
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000189 return (mask->low_slices & available->low_slices) ==
190 mask->low_slices;
Christophe Leroydb3a5282018-02-22 15:27:24 +0100191
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000192 bitmap_and(result, mask->high_slices,
193 available->high_slices, slice_count);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530194
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000195 return (mask->low_slices & available->low_slices) == mask->low_slices &&
196 bitmap_equal(result, mask->high_slices, slice_count);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000197}
198
199static void slice_flush_segments(void *parm)
200{
Christophe Leroydb3a5282018-02-22 15:27:24 +0100201#ifdef CONFIG_PPC64
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000202 struct mm_struct *mm = parm;
203 unsigned long flags;
204
205 if (mm != current->active_mm)
206 return;
207
Aneesh Kumar K.V52b1e662017-03-22 09:06:49 +0530208 copy_mm_to_paca(current->active_mm);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000209
210 local_irq_save(flags);
211 slb_flush_and_rebolt();
212 local_irq_restore(flags);
Christophe Leroydb3a5282018-02-22 15:27:24 +0100213#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000214}
215
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000216static void slice_convert(struct mm_struct *mm,
217 const struct slice_mask *mask, int psize)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000218{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000219 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000220 /* Write the new slice psize bits */
Christophe Leroy15472422018-02-22 15:27:28 +0100221 unsigned char *hpsizes, *lpsizes;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000222 struct slice_mask *psize_mask, *old_mask;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000223 unsigned long i, flags;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000224 int old_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000225
226 slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
227 slice_print_mask(" mask", mask);
228
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000229 psize_mask = slice_mask_for_size(mm, psize);
230
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000231 /* We need to use a spinlock here to protect against
232 * concurrent 64k -> 4k demotion ...
233 */
234 spin_lock_irqsave(&slice_convert_lock, flags);
235
236 lpsizes = mm->context.low_slices_psize;
Nicholas Piggin5a807e02018-03-07 11:37:10 +1000237 for (i = 0; i < SLICE_NUM_LOW; i++) {
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000238 if (!(mask->low_slices & (1u << i)))
Nicholas Piggin5a807e02018-03-07 11:37:10 +1000239 continue;
240
241 mask_index = i & 0x1;
242 index = i >> 1;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000243
244 /* Update the slice_mask */
245 old_psize = (lpsizes[index] >> (mask_index * 4)) & 0xf;
246 old_mask = slice_mask_for_size(mm, old_psize);
247 old_mask->low_slices &= ~(1u << i);
248 psize_mask->low_slices |= 1u << i;
249
250 /* Update the sizes array */
Nicholas Piggin5a807e02018-03-07 11:37:10 +1000251 lpsizes[index] = (lpsizes[index] & ~(0xf << (mask_index * 4))) |
Christophe Leroy15472422018-02-22 15:27:28 +0100252 (((unsigned long)psize) << (mask_index * 4));
Nicholas Piggin5a807e02018-03-07 11:37:10 +1000253 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000254
255 hpsizes = mm->context.high_slices_psize;
Nicholas Piggin47224762017-11-10 04:27:40 +1100256 for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000257 if (!test_bit(i, mask->high_slices))
Nicholas Piggin5a807e02018-03-07 11:37:10 +1000258 continue;
259
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000260 mask_index = i & 0x1;
261 index = i >> 1;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000262
263 /* Update the slice_mask */
264 old_psize = (hpsizes[index] >> (mask_index * 4)) & 0xf;
265 old_mask = slice_mask_for_size(mm, old_psize);
266 __clear_bit(i, old_mask->high_slices);
267 __set_bit(i, psize_mask->high_slices);
268
269 /* Update the sizes array */
Nicholas Piggin5a807e02018-03-07 11:37:10 +1000270 hpsizes[index] = (hpsizes[index] & ~(0xf << (mask_index * 4))) |
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000271 (((unsigned long)psize) << (mask_index * 4));
272 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000273
274 slice_dbg(" lsps=%lx, hsps=%lx\n",
Aneesh Kumar K.V302413c2017-03-22 09:06:52 +0530275 (unsigned long)mm->context.low_slices_psize,
276 (unsigned long)mm->context.high_slices_psize);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000277
278 spin_unlock_irqrestore(&slice_convert_lock, flags);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000279
Ian Munsiebe3ebfe2014-10-08 19:54:52 +1100280 copro_flush_all_slbs(mm);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000281}
282
Michel Lespinassefba23692013-04-29 11:53:53 -0700283/*
284 * Compute which slice addr is part of;
285 * set *boundary_addr to the start or end boundary of that slice
286 * (depending on 'end' parameter);
287 * return boolean indicating if the slice is marked as available in the
288 * 'available' slice_mark.
289 */
290static bool slice_scan_available(unsigned long addr,
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000291 const struct slice_mask *available,
292 int end, unsigned long *boundary_addr)
Michel Lespinassefba23692013-04-29 11:53:53 -0700293{
294 unsigned long slice;
295 if (addr < SLICE_LOW_TOP) {
296 slice = GET_LOW_SLICE_INDEX(addr);
297 *boundary_addr = (slice + end) << SLICE_LOW_SHIFT;
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000298 return !!(available->low_slices & (1u << slice));
Michel Lespinassefba23692013-04-29 11:53:53 -0700299 } else {
300 slice = GET_HIGH_SLICE_INDEX(addr);
301 *boundary_addr = (slice + end) ?
302 ((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP;
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000303 return !!test_bit(slice, available->high_slices);
Michel Lespinassefba23692013-04-29 11:53:53 -0700304 }
305}
306
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000307static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
308 unsigned long len,
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000309 const struct slice_mask *available,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530310 int psize, unsigned long high_limit)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000311{
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000312 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
Michel Lespinassefba23692013-04-29 11:53:53 -0700313 unsigned long addr, found, next_end;
314 struct vm_unmapped_area_info info;
315
316 info.flags = 0;
317 info.length = len;
318 info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
319 info.align_offset = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000320
Michel Lespinasse34d07172013-04-29 11:53:52 -0700321 addr = TASK_UNMAPPED_BASE;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530322 /*
323 * Check till the allow max value for this mmap request
324 */
325 while (addr < high_limit) {
Michel Lespinassefba23692013-04-29 11:53:53 -0700326 info.low_limit = addr;
327 if (!slice_scan_available(addr, available, 1, &addr))
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000328 continue;
Michel Lespinassefba23692013-04-29 11:53:53 -0700329
330 next_slice:
331 /*
332 * At this point [info.low_limit; addr) covers
333 * available slices only and ends at a slice boundary.
334 * Check if we need to reduce the range, or if we can
335 * extend it to cover the next available slice.
336 */
Aneesh Kumar K.Vbe77e992017-04-14 00:48:21 +0530337 if (addr >= high_limit)
338 addr = high_limit;
Michel Lespinassefba23692013-04-29 11:53:53 -0700339 else if (slice_scan_available(addr, available, 1, &next_end)) {
340 addr = next_end;
341 goto next_slice;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000342 }
Michel Lespinassefba23692013-04-29 11:53:53 -0700343 info.high_limit = addr;
344
345 found = vm_unmapped_area(&info);
346 if (!(found & ~PAGE_MASK))
347 return found;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000348 }
349
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000350 return -ENOMEM;
351}
352
353static unsigned long slice_find_area_topdown(struct mm_struct *mm,
354 unsigned long len,
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000355 const struct slice_mask *available,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530356 int psize, unsigned long high_limit)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000357{
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000358 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
Michel Lespinassefba23692013-04-29 11:53:53 -0700359 unsigned long addr, found, prev;
360 struct vm_unmapped_area_info info;
361
362 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
363 info.length = len;
364 info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
365 info.align_offset = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000366
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000367 addr = mm->mmap_base;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530368 /*
369 * If we are trying to allocate above DEFAULT_MAP_WINDOW
370 * Add the different to the mmap_base.
371 * Only for that request for which high_limit is above
372 * DEFAULT_MAP_WINDOW we should apply this.
373 */
Nicholas Piggin47224762017-11-10 04:27:40 +1100374 if (high_limit > DEFAULT_MAP_WINDOW)
375 addr += mm->context.slb_addr_limit - DEFAULT_MAP_WINDOW;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530376
Michel Lespinassefba23692013-04-29 11:53:53 -0700377 while (addr > PAGE_SIZE) {
378 info.high_limit = addr;
379 if (!slice_scan_available(addr - 1, available, 0, &addr))
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000380 continue;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000381
Michel Lespinassefba23692013-04-29 11:53:53 -0700382 prev_slice:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000383 /*
Michel Lespinassefba23692013-04-29 11:53:53 -0700384 * At this point [addr; info.high_limit) covers
385 * available slices only and starts at a slice boundary.
386 * Check if we need to reduce the range, or if we can
387 * extend it to cover the previous available slice.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000388 */
Michel Lespinassefba23692013-04-29 11:53:53 -0700389 if (addr < PAGE_SIZE)
390 addr = PAGE_SIZE;
391 else if (slice_scan_available(addr - 1, available, 0, &prev)) {
392 addr = prev;
393 goto prev_slice;
394 }
395 info.low_limit = addr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000396
Michel Lespinassefba23692013-04-29 11:53:53 -0700397 found = vm_unmapped_area(&info);
398 if (!(found & ~PAGE_MASK))
399 return found;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000400 }
401
402 /*
403 * A failed mmap() very likely causes application failure,
404 * so fall back to the bottom-up function here. This scenario
405 * can happen with large stack limits and large mmap()
406 * allocations.
407 */
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530408 return slice_find_area_bottomup(mm, len, available, psize, high_limit);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000409}
410
411
412static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000413 const struct slice_mask *mask, int psize,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530414 int topdown, unsigned long high_limit)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000415{
416 if (topdown)
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530417 return slice_find_area_topdown(mm, len, mask, psize, high_limit);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000418 else
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530419 return slice_find_area_bottomup(mm, len, mask, psize, high_limit);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000420}
421
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000422static inline void slice_or_mask(struct slice_mask *dst,
423 const struct slice_mask *src)
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530424{
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530425 dst->low_slices |= src->low_slices;
Christophe Leroydb3a5282018-02-22 15:27:24 +0100426 if (!SLICE_NUM_HIGH)
427 return;
Christophe Leroy326691a2018-02-22 15:27:20 +0100428 bitmap_or(dst->high_slices, dst->high_slices, src->high_slices,
429 SLICE_NUM_HIGH);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530430}
431
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000432static inline void slice_andnot_mask(struct slice_mask *dst,
433 const struct slice_mask *src)
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530434{
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530435 dst->low_slices &= ~src->low_slices;
436
Christophe Leroydb3a5282018-02-22 15:27:24 +0100437 if (!SLICE_NUM_HIGH)
438 return;
Christophe Leroy326691a2018-02-22 15:27:20 +0100439 bitmap_andnot(dst->high_slices, dst->high_slices, src->high_slices,
440 SLICE_NUM_HIGH);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530441}
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000442
443#ifdef CONFIG_PPC_64K_PAGES
444#define MMU_PAGE_BASE MMU_PAGE_64K
445#else
446#define MMU_PAGE_BASE MMU_PAGE_4K
447#endif
448
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000449unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
450 unsigned long flags, unsigned int psize,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700451 int topdown)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000452{
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530453 struct slice_mask mask;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000454 struct slice_mask good_mask;
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530455 struct slice_mask potential_mask;
456 struct slice_mask compat_mask;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000457 int fixed = (flags & MAP_FIXED);
458 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
Nicholas Piggin6a72dc02017-11-10 04:27:36 +1100459 unsigned long page_size = 1UL << pshift;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000460 struct mm_struct *mm = current->mm;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000461 unsigned long newaddr;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530462 unsigned long high_limit;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000463
Nicholas Piggin6a72dc02017-11-10 04:27:36 +1100464 high_limit = DEFAULT_MAP_WINDOW;
Nicholas Piggin35602f82017-11-10 04:27:38 +1100465 if (addr >= high_limit || (fixed && (addr + len > high_limit)))
Nicholas Piggin6a72dc02017-11-10 04:27:36 +1100466 high_limit = TASK_SIZE;
467
468 if (len > high_limit)
469 return -ENOMEM;
470 if (len & (page_size - 1))
471 return -EINVAL;
472 if (fixed) {
473 if (addr & (page_size - 1))
474 return -EINVAL;
475 if (addr > high_limit - len)
476 return -ENOMEM;
477 }
478
Nicholas Piggin47224762017-11-10 04:27:40 +1100479 if (high_limit > mm->context.slb_addr_limit) {
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000480 /*
481 * Increasing the slb_addr_limit does not require
482 * slice mask cache to be recalculated because it should
483 * be already initialised beyond the old address limit.
484 */
Nicholas Piggin47224762017-11-10 04:27:40 +1100485 mm->context.slb_addr_limit = high_limit;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000486
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530487 on_each_cpu(slice_flush_segments, mm, 1);
488 }
Nicholas Piggin6a72dc02017-11-10 04:27:36 +1100489
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530490 /*
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530491 * init different masks
492 */
493 mask.low_slices = 0;
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530494
495 /* silence stupid warning */;
496 potential_mask.low_slices = 0;
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530497
498 compat_mask.low_slices = 0;
Christophe Leroydb3a5282018-02-22 15:27:24 +0100499
500 if (SLICE_NUM_HIGH) {
501 bitmap_zero(mask.high_slices, SLICE_NUM_HIGH);
502 bitmap_zero(potential_mask.high_slices, SLICE_NUM_HIGH);
503 bitmap_zero(compat_mask.high_slices, SLICE_NUM_HIGH);
504 }
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530505
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000506 /* Sanity checks */
507 BUG_ON(mm->task_size == 0);
Nicholas Piggin47224762017-11-10 04:27:40 +1100508 BUG_ON(mm->context.slb_addr_limit == 0);
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000509 VM_BUG_ON(radix_enabled());
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000510
511 slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
Michel Lespinasse34d07172013-04-29 11:53:52 -0700512 slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
513 addr, len, flags, topdown);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000514
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000515 /* If hint, make sure it matches our alignment restrictions */
516 if (!fixed && addr) {
Nicholas Piggin6a72dc02017-11-10 04:27:36 +1100517 addr = _ALIGN_UP(addr, page_size);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000518 slice_dbg(" aligned addr=%lx\n", addr);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000519 /* Ignore hint if it's too large or overlaps a VMA */
Nicholas Piggin6a72dc02017-11-10 04:27:36 +1100520 if (addr > high_limit - len ||
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000521 !slice_area_is_free(mm, addr, len))
522 addr = 0;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000523 }
524
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000525 /* First make up a "good" mask of slices that have the right size
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000526 * already
527 */
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000528 good_mask = *slice_mask_for_size(mm, psize);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000529 slice_print_mask(" good_mask", &good_mask);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000530
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000531 /*
532 * Here "good" means slices that are already the right page size,
533 * "compat" means slices that have a compatible page size (i.e.
534 * 4k in a 64k pagesize kernel), and "free" means slices without
535 * any VMAs.
536 *
537 * If MAP_FIXED:
538 * check if fits in good | compat => OK
539 * check if fits in good | compat | free => convert free
540 * else bad
541 * If have hint:
542 * check if hint fits in good => OK
543 * check if hint fits in good | free => convert free
544 * Otherwise:
545 * search in good, found => OK
546 * search in good | free, found => convert free
547 * search in good | compat | free, found => convert free.
548 */
549
550#ifdef CONFIG_PPC_64K_PAGES
551 /* If we support combo pages, we can allow 64k pages in 4k slices */
552 if (psize == MMU_PAGE_64K) {
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000553 compat_mask = *slice_mask_for_size(mm, MMU_PAGE_4K);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000554 if (fixed)
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530555 slice_or_mask(&good_mask, &compat_mask);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000556 }
557#endif
558
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000559 /* First check hint if it's valid or if we have MAP_FIXED */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000560 if (addr != 0 || fixed) {
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000561 /* Build a mask for the requested range */
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530562 slice_range_to_mask(addr, len, &mask);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000563 slice_print_mask(" mask", &mask);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000564
565 /* Check if we fit in the good mask. If we do, we just return,
566 * nothing else to do
567 */
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000568 if (slice_check_fit(mm, &mask, &good_mask)) {
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000569 slice_dbg(" fits good !\n");
570 return addr;
571 }
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000572 } else {
573 /* Now let's see if we can find something in the existing
574 * slices for that size
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000575 */
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000576 newaddr = slice_find_area(mm, len, &good_mask,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530577 psize, topdown, high_limit);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000578 if (newaddr != -ENOMEM) {
579 /* Found within the good mask, we don't have to setup,
580 * we thus return directly
581 */
582 slice_dbg(" found area at 0x%lx\n", newaddr);
583 return newaddr;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000584 }
585 }
Aneesh Kumar K.V7a06c662017-11-10 10:25:07 +0530586 /*
587 * We don't fit in the good mask, check what other slices are
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000588 * empty and thus can be converted
589 */
Aneesh Kumar K.V7a06c662017-11-10 10:25:07 +0530590 slice_mask_for_free(mm, &potential_mask, high_limit);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530591 slice_or_mask(&potential_mask, &good_mask);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000592 slice_print_mask(" potential", &potential_mask);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000593
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000594 if ((addr != 0 || fixed) &&
595 slice_check_fit(mm, &mask, &potential_mask)) {
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000596 slice_dbg(" fits potential !\n");
597 goto convert;
598 }
599
600 /* If we have MAP_FIXED and failed the above steps, then error out */
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000601 if (fixed)
602 return -EBUSY;
603
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000604 slice_dbg(" search...\n");
605
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000606 /* If we had a hint that didn't work out, see if we can fit
607 * anywhere in the good area.
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000608 */
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000609 if (addr) {
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000610 addr = slice_find_area(mm, len, &good_mask,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530611 psize, topdown, high_limit);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000612 if (addr != -ENOMEM) {
613 slice_dbg(" found area at 0x%lx\n", addr);
614 return addr;
615 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000616 }
617
618 /* Now let's see if we can find something in the existing slices
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000619 * for that size plus free slices
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000620 */
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000621 addr = slice_find_area(mm, len, &potential_mask,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530622 psize, topdown, high_limit);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000623
624#ifdef CONFIG_PPC_64K_PAGES
625 if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
626 /* retry the search with 4k-page slices included */
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530627 slice_or_mask(&potential_mask, &compat_mask);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000628 addr = slice_find_area(mm, len, &potential_mask,
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530629 psize, topdown, high_limit);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000630 }
631#endif
632
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000633 if (addr == -ENOMEM)
634 return -ENOMEM;
635
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530636 slice_range_to_mask(addr, len, &mask);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000637 slice_dbg(" found potential area at 0x%lx\n", addr);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000638 slice_print_mask(" mask", &mask);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000639
640 convert:
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530641 slice_andnot_mask(&mask, &good_mask);
642 slice_andnot_mask(&mask, &compat_mask);
Christophe Leroydb3a5282018-02-22 15:27:24 +0100643 if (mask.low_slices ||
644 (SLICE_NUM_HIGH &&
645 !bitmap_empty(mask.high_slices, SLICE_NUM_HIGH))) {
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000646 slice_convert(mm, &mask, psize);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000647 if (psize > MMU_PAGE_BASE)
Benjamin Herrenschmidt84c3d4a2008-07-16 11:07:59 +1000648 on_each_cpu(slice_flush_segments, mm, 1);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000649 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000650 return addr;
651
652}
653EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
654
655unsigned long arch_get_unmapped_area(struct file *filp,
656 unsigned long addr,
657 unsigned long len,
658 unsigned long pgoff,
659 unsigned long flags)
660{
661 return slice_get_unmapped_area(addr, len, flags,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700662 current->mm->context.user_psize, 0);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000663}
664
665unsigned long arch_get_unmapped_area_topdown(struct file *filp,
666 const unsigned long addr0,
667 const unsigned long len,
668 const unsigned long pgoff,
669 const unsigned long flags)
670{
671 return slice_get_unmapped_area(addr0, len, flags,
Michel Lespinasse34d07172013-04-29 11:53:52 -0700672 current->mm->context.user_psize, 1);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000673}
674
675unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
676{
Christophe Leroy15472422018-02-22 15:27:28 +0100677 unsigned char *psizes;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000678 int index, mask_index;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000679
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000680 /*
681 * Radix doesn't use slice, but can get enabled along with MMU_SLICE
682 */
683 if (radix_enabled()) {
684#ifdef CONFIG_PPC_64K_PAGES
685 return MMU_PAGE_64K;
686#else
687 return MMU_PAGE_4K;
688#endif
689 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000690 if (addr < SLICE_LOW_TOP) {
Christophe Leroy15472422018-02-22 15:27:28 +0100691 psizes = mm->context.low_slices_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000692 index = GET_LOW_SLICE_INDEX(addr);
Christophe Leroy15472422018-02-22 15:27:28 +0100693 } else {
694 psizes = mm->context.high_slices_psize;
695 index = GET_HIGH_SLICE_INDEX(addr);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000696 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000697 mask_index = index & 0x1;
Christophe Leroy15472422018-02-22 15:27:28 +0100698 return (psizes[index >> 1] >> (mask_index * 4)) & 0xf;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000699}
700EXPORT_SYMBOL_GPL(get_slice_psize);
701
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000702void slice_init_new_context_exec(struct mm_struct *mm)
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000703{
Christophe Leroy15472422018-02-22 15:27:28 +0100704 unsigned char *hpsizes, *lpsizes;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000705 struct slice_mask *mask;
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000706 unsigned int psize = mmu_virtual_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000707
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000708 slice_dbg("slice_init_new_context_exec(mm=%p)\n", mm);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000709
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000710 /*
711 * In the case of exec, use the default limit. In the
712 * case of fork it is just inherited from the mm being
713 * duplicated.
714 */
715#ifdef CONFIG_PPC64
716 mm->context.slb_addr_limit = DEFAULT_MAP_WINDOW_USER64;
717#else
718 mm->context.slb_addr_limit = DEFAULT_MAP_WINDOW;
719#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000720
721 mm->context.user_psize = psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000722
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000723 /*
724 * Set all slice psizes to the default.
725 */
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000726 lpsizes = mm->context.low_slices_psize;
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000727 memset(lpsizes, (psize << 4) | psize, SLICE_NUM_LOW >> 1);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000728
729 hpsizes = mm->context.high_slices_psize;
Nicholas Piggin1753dd12018-03-07 11:37:09 +1000730 memset(hpsizes, (psize << 4) | psize, SLICE_NUM_HIGH >> 1);
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000731
732 /*
733 * Slice mask cache starts zeroed, fill the default size cache.
734 */
735 mask = slice_mask_for_size(mm, psize);
736 mask->low_slices = ~0UL;
737 if (SLICE_NUM_HIGH)
738 bitmap_fill(mask->high_slices, SLICE_NUM_HIGH);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000739}
740
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000741void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
742 unsigned long len, unsigned int psize)
743{
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530744 struct slice_mask mask;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000745
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000746 VM_BUG_ON(radix_enabled());
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530747
748 slice_range_to_mask(start, len, &mask);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000749 slice_convert(mm, &mask, psize);
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000750}
751
Aneesh Kumar K.V66437732014-10-21 14:25:38 +1100752#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000753/*
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400754 * is_hugepage_only_range() is used by generic code to verify whether
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000755 * a normal mmap mapping (non hugetlbfs) is valid on a given area.
756 *
757 * until the generic code provides a more generic hook and/or starts
758 * calling arch get_unmapped_area for MAP_FIXED (which our implementation
759 * here knows how to deal with), we hijack it to keep standard mappings
760 * away from us.
761 *
762 * because of that generic code limitation, MAP_FIXED mapping cannot
763 * "convert" back a slice with no VMAs to the standard page size, only
764 * get_unmapped_area() can. It would be possible to fix it here but I
765 * prefer working on fixing the generic code instead.
766 *
767 * WARNING: This will not work if hugetlbfs isn't enabled since the
768 * generic code will redefine that function as 0 in that. This is ok
769 * for now as we only use slices with hugetlbfs enabled. This should
770 * be fixed as the generic code gets fixed.
771 */
772int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
773 unsigned long len)
774{
775 struct slice_mask mask, available;
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000776 unsigned int psize = mm->context.user_psize;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000777
Aneesh Kumar K.V764041e2016-04-29 23:26:09 +1000778 if (radix_enabled())
779 return 0;
780
Aneesh Kumar K.Va4d362152017-03-22 09:06:48 +0530781 slice_range_to_mask(addr, len, &mask);
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000782 available = *slice_mask_for_size(mm, psize);
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000783#ifdef CONFIG_PPC_64K_PAGES
784 /* We need to account for 4k slices too */
785 if (psize == MMU_PAGE_64K) {
786 struct slice_mask compat_mask;
Nicholas Piggin5709f7c2018-03-07 11:37:12 +1000787 compat_mask = *slice_mask_for_size(mm, MMU_PAGE_4K);
Aneesh Kumar K.Vf3207c12017-03-22 09:06:47 +0530788 slice_or_mask(&available, &compat_mask);
Dave Kleikamp9ba0fdb2009-01-14 09:09:34 +0000789 }
790#endif
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000791
792#if 0 /* too verbose */
793 slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
794 mm, addr, len);
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000795 slice_print_mask(" mask", &mask);
796 slice_print_mask(" available", &available);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000797#endif
Nicholas Piggin830fd2d2018-03-07 11:37:11 +1000798 return !slice_check_fit(mm, &mask, &available);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000799}
Aneesh Kumar K.V66437732014-10-21 14:25:38 +1100800#endif