lscsa_alloc.c 4.7 KB

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