lscsa_alloc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * SPU local store allocation routines
  3. *
  4. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #undef DEBUG
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmalloc.h>
  24. #include <asm/spu.h>
  25. #include <asm/spu_csa.h>
  26. #include <asm/mmu.h>
  27. static int spu_alloc_lscsa_std(struct spu_state *csa)
  28. {
  29. struct spu_lscsa *lscsa;
  30. unsigned char *p;
  31. lscsa = vmalloc(sizeof(struct spu_lscsa));
  32. if (!lscsa)
  33. return -ENOMEM;
  34. memset(lscsa, 0, sizeof(struct spu_lscsa));
  35. csa->lscsa = lscsa;
  36. /* Set LS pages reserved to allow for user-space mapping. */
  37. for (p = lscsa->ls; p < lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  38. SetPageReserved(vmalloc_to_page(p));
  39. return 0;
  40. }
  41. static void spu_free_lscsa_std(struct spu_state *csa)
  42. {
  43. /* Clear reserved bit before vfree. */
  44. unsigned char *p;
  45. if (csa->lscsa == NULL)
  46. return;
  47. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  48. ClearPageReserved(vmalloc_to_page(p));
  49. vfree(csa->lscsa);
  50. }
  51. #ifdef CONFIG_SPU_FS_64K_LS
  52. #define SPU_64K_PAGE_SHIFT 16
  53. #define SPU_64K_PAGE_ORDER (SPU_64K_PAGE_SHIFT - PAGE_SHIFT)
  54. #define SPU_64K_PAGE_COUNT (1ul << SPU_64K_PAGE_ORDER)
  55. int spu_alloc_lscsa(struct spu_state *csa)
  56. {
  57. struct page **pgarray;
  58. unsigned char *p;
  59. int i, j, n_4k;
  60. /* Check availability of 64K pages */
  61. if (mmu_psize_defs[MMU_PAGE_64K].shift == 0)
  62. goto fail;
  63. csa->use_big_pages = 1;
  64. pr_debug("spu_alloc_lscsa(csa=0x%p), trying to allocate 64K pages\n",
  65. csa);
  66. /* First try to allocate our 64K pages. We need 5 of them
  67. * with the current implementation. In the future, we should try
  68. * to separate the lscsa with the actual local store image, thus
  69. * allowing us to require only 4 64K pages per context
  70. */
  71. for (i = 0; i < SPU_LSCSA_NUM_BIG_PAGES; i++) {
  72. /* XXX This is likely to fail, we should use a special pool
  73. * similiar to what hugetlbfs does.
  74. */
  75. csa->lscsa_pages[i] = alloc_pages(GFP_KERNEL,
  76. SPU_64K_PAGE_ORDER);
  77. if (csa->lscsa_pages[i] == NULL)
  78. goto fail;
  79. }
  80. pr_debug(" success ! creating vmap...\n");
  81. /* Now we need to create a vmalloc mapping of these for the kernel
  82. * and SPU context switch code to use. Currently, we stick to a
  83. * normal kernel vmalloc mapping, which in our case will be 4K
  84. */
  85. n_4k = SPU_64K_PAGE_COUNT * SPU_LSCSA_NUM_BIG_PAGES;
  86. pgarray = kmalloc(sizeof(struct page *) * n_4k, GFP_KERNEL);
  87. if (pgarray == NULL)
  88. goto fail;
  89. for (i = 0; i < SPU_LSCSA_NUM_BIG_PAGES; i++)
  90. for (j = 0; j < SPU_64K_PAGE_COUNT; j++)
  91. /* We assume all the struct page's are contiguous
  92. * which should be hopefully the case for an order 4
  93. * allocation..
  94. */
  95. pgarray[i * SPU_64K_PAGE_COUNT + j] =
  96. csa->lscsa_pages[i] + j;
  97. csa->lscsa = vmap(pgarray, n_4k, VM_USERMAP, PAGE_KERNEL);
  98. kfree(pgarray);
  99. if (csa->lscsa == NULL)
  100. goto fail;
  101. memset(csa->lscsa, 0, sizeof(struct spu_lscsa));
  102. /* Set LS pages reserved to allow for user-space mapping.
  103. *
  104. * XXX isn't that a bit obsolete ? I think we should just
  105. * make sure the page count is high enough. Anyway, won't harm
  106. * for now
  107. */
  108. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  109. SetPageReserved(vmalloc_to_page(p));
  110. pr_debug(" all good !\n");
  111. return 0;
  112. fail:
  113. pr_debug("spufs: failed to allocate lscsa 64K pages, falling back\n");
  114. spu_free_lscsa(csa);
  115. return spu_alloc_lscsa_std(csa);
  116. }
  117. void spu_free_lscsa(struct spu_state *csa)
  118. {
  119. unsigned char *p;
  120. int i;
  121. if (!csa->use_big_pages) {
  122. spu_free_lscsa_std(csa);
  123. return;
  124. }
  125. csa->use_big_pages = 0;
  126. if (csa->lscsa == NULL)
  127. goto free_pages;
  128. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  129. ClearPageReserved(vmalloc_to_page(p));
  130. vunmap(csa->lscsa);
  131. csa->lscsa = NULL;
  132. free_pages:
  133. for (i = 0; i < SPU_LSCSA_NUM_BIG_PAGES; i++)
  134. if (csa->lscsa_pages[i])
  135. __free_pages(csa->lscsa_pages[i], SPU_64K_PAGE_ORDER);
  136. }
  137. #else /* CONFIG_SPU_FS_64K_LS */
  138. int spu_alloc_lscsa(struct spu_state *csa)
  139. {
  140. return spu_alloc_lscsa_std(csa);
  141. }
  142. void spu_free_lscsa(struct spu_state *csa)
  143. {
  144. spu_free_lscsa_std(csa);
  145. }
  146. #endif /* !defined(CONFIG_SPU_FS_64K_LS) */