slice.c 20 KB

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