lscsa_alloc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "spufs.h"
  28. static int spu_alloc_lscsa_std(struct spu_state *csa)
  29. {
  30. struct spu_lscsa *lscsa;
  31. unsigned char *p;
  32. lscsa = vmalloc(sizeof(struct spu_lscsa));
  33. if (!lscsa)
  34. return -ENOMEM;
  35. memset(lscsa, 0, sizeof(struct spu_lscsa));
  36. csa->lscsa = lscsa;
  37. /* Set LS pages reserved to allow for user-space mapping. */
  38. for (p = lscsa->ls; p < lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  39. SetPageReserved(vmalloc_to_page(p));
  40. return 0;
  41. }
  42. static void spu_free_lscsa_std(struct spu_state *csa)
  43. {
  44. /* Clear reserved bit before vfree. */
  45. unsigned char *p;
  46. if (csa->lscsa == NULL)
  47. return;
  48. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  49. ClearPageReserved(vmalloc_to_page(p));
  50. vfree(csa->lscsa);
  51. }
  52. #ifdef CONFIG_SPU_FS_64K_LS
  53. #define SPU_64K_PAGE_SHIFT 16
  54. #define SPU_64K_PAGE_ORDER (SPU_64K_PAGE_SHIFT - PAGE_SHIFT)
  55. #define SPU_64K_PAGE_COUNT (1ul << SPU_64K_PAGE_ORDER)
  56. int spu_alloc_lscsa(struct spu_state *csa)
  57. {
  58. struct page **pgarray;
  59. unsigned char *p;
  60. int i, j, n_4k;
  61. /* Check availability of 64K pages */
  62. if (!spu_64k_pages_available())
  63. goto fail;
  64. csa->use_big_pages = 1;
  65. pr_debug("spu_alloc_lscsa(csa=0x%p), trying to allocate 64K pages\n",
  66. csa);
  67. /* First try to allocate our 64K pages. We need 5 of them
  68. * with the current implementation. In the future, we should try
  69. * to separate the lscsa with the actual local store image, thus
  70. * allowing us to require only 4 64K pages per context
  71. */
  72. for (i = 0; i < SPU_LSCSA_NUM_BIG_PAGES; i++) {
  73. /* XXX This is likely to fail, we should use a special pool
  74. * similiar to what hugetlbfs does.
  75. */
  76. csa->lscsa_pages[i] = alloc_pages(GFP_KERNEL,
  77. SPU_64K_PAGE_ORDER);
  78. if (csa->lscsa_pages[i] == NULL)
  79. goto fail;
  80. }
  81. pr_debug(" success ! creating vmap...\n");
  82. /* Now we need to create a vmalloc mapping of these for the kernel
  83. * and SPU context switch code to use. Currently, we stick to a
  84. * normal kernel vmalloc mapping, which in our case will be 4K
  85. */
  86. n_4k = SPU_64K_PAGE_COUNT * SPU_LSCSA_NUM_BIG_PAGES;
  87. pgarray = kmalloc(sizeof(struct page *) * n_4k, GFP_KERNEL);
  88. if (pgarray == NULL)
  89. goto fail;
  90. for (i = 0; i < SPU_LSCSA_NUM_BIG_PAGES; i++)
  91. for (j = 0; j < SPU_64K_PAGE_COUNT; j++)
  92. /* We assume all the struct page's are contiguous
  93. * which should be hopefully the case for an order 4
  94. * allocation..
  95. */
  96. pgarray[i * SPU_64K_PAGE_COUNT + j] =
  97. csa->lscsa_pages[i] + j;
  98. csa->lscsa = vmap(pgarray, n_4k, VM_USERMAP, PAGE_KERNEL);
  99. kfree(pgarray);
  100. if (csa->lscsa == NULL)
  101. goto fail;
  102. memset(csa->lscsa, 0, sizeof(struct spu_lscsa));
  103. /* Set LS pages reserved to allow for user-space mapping.
  104. *
  105. * XXX isn't that a bit obsolete ? I think we should just
  106. * make sure the page count is high enough. Anyway, won't harm
  107. * for now
  108. */
  109. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  110. SetPageReserved(vmalloc_to_page(p));
  111. pr_debug(" all good !\n");
  112. return 0;
  113. fail:
  114. pr_debug("spufs: failed to allocate lscsa 64K pages, falling back\n");
  115. spu_free_lscsa(csa);
  116. return spu_alloc_lscsa_std(csa);
  117. }
  118. void spu_free_lscsa(struct spu_state *csa)
  119. {
  120. unsigned char *p;
  121. int i;
  122. if (!csa->use_big_pages) {
  123. spu_free_lscsa_std(csa);
  124. return;
  125. }
  126. csa->use_big_pages = 0;
  127. if (csa->lscsa == NULL)
  128. goto free_pages;
  129. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  130. ClearPageReserved(vmalloc_to_page(p));
  131. vunmap(csa->lscsa);
  132. csa->lscsa = NULL;
  133. free_pages:
  134. for (i = 0; i < SPU_LSCSA_NUM_BIG_PAGES; i++)
  135. if (csa->lscsa_pages[i])
  136. __free_pages(csa->lscsa_pages[i], SPU_64K_PAGE_ORDER);
  137. }
  138. #else /* CONFIG_SPU_FS_64K_LS */
  139. int spu_alloc_lscsa(struct spu_state *csa)
  140. {
  141. return spu_alloc_lscsa_std(csa);
  142. }
  143. void spu_free_lscsa(struct spu_state *csa)
  144. {
  145. spu_free_lscsa_std(csa);
  146. }
  147. #endif /* !defined(CONFIG_SPU_FS_64K_LS) */