slice.c 21 KB

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