amdgpu_vram_mgr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Christian König
  23. */
  24. #include <drm/drmP.h>
  25. #include "amdgpu.h"
  26. struct amdgpu_vram_mgr {
  27. struct drm_mm mm;
  28. spinlock_t lock;
  29. };
  30. /**
  31. * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
  32. *
  33. * @man: TTM memory type manager
  34. * @p_size: maximum size of VRAM
  35. *
  36. * Allocate and initialize the VRAM manager.
  37. */
  38. static int amdgpu_vram_mgr_init(struct ttm_mem_type_manager *man,
  39. unsigned long p_size)
  40. {
  41. struct amdgpu_vram_mgr *mgr;
  42. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  43. if (!mgr)
  44. return -ENOMEM;
  45. drm_mm_init(&mgr->mm, 0, p_size);
  46. spin_lock_init(&mgr->lock);
  47. man->priv = mgr;
  48. return 0;
  49. }
  50. /**
  51. * amdgpu_vram_mgr_fini - free and destroy VRAM manager
  52. *
  53. * @man: TTM memory type manager
  54. *
  55. * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
  56. * allocated inside it.
  57. */
  58. static int amdgpu_vram_mgr_fini(struct ttm_mem_type_manager *man)
  59. {
  60. struct amdgpu_vram_mgr *mgr = man->priv;
  61. spin_lock(&mgr->lock);
  62. if (!drm_mm_clean(&mgr->mm)) {
  63. spin_unlock(&mgr->lock);
  64. return -EBUSY;
  65. }
  66. drm_mm_takedown(&mgr->mm);
  67. spin_unlock(&mgr->lock);
  68. kfree(mgr);
  69. man->priv = NULL;
  70. return 0;
  71. }
  72. /**
  73. * amdgpu_vram_mgr_new - allocate new ranges
  74. *
  75. * @man: TTM memory type manager
  76. * @tbo: TTM BO we need this range for
  77. * @place: placement flags and restrictions
  78. * @mem: the resulting mem object
  79. *
  80. * Allocate VRAM for the given BO.
  81. */
  82. static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
  83. struct ttm_buffer_object *tbo,
  84. const struct ttm_place *place,
  85. struct ttm_mem_reg *mem)
  86. {
  87. struct amdgpu_vram_mgr *mgr = man->priv;
  88. struct drm_mm *mm = &mgr->mm;
  89. struct drm_mm_node *nodes;
  90. enum drm_mm_insert_mode mode;
  91. unsigned long lpfn, num_nodes, pages_per_node, pages_left;
  92. unsigned i;
  93. int r;
  94. lpfn = place->lpfn;
  95. if (!lpfn)
  96. lpfn = man->size;
  97. if (place->flags & TTM_PL_FLAG_CONTIGUOUS ||
  98. amdgpu_vram_page_split == -1) {
  99. pages_per_node = ~0ul;
  100. num_nodes = 1;
  101. } else {
  102. pages_per_node = max((uint32_t)amdgpu_vram_page_split,
  103. mem->page_alignment);
  104. num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
  105. }
  106. nodes = kcalloc(num_nodes, sizeof(*nodes), GFP_KERNEL);
  107. if (!nodes)
  108. return -ENOMEM;
  109. mode = DRM_MM_INSERT_BEST;
  110. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  111. mode = DRM_MM_INSERT_HIGH;
  112. mem->start = 0;
  113. pages_left = mem->num_pages;
  114. spin_lock(&mgr->lock);
  115. for (i = 0; i < num_nodes; ++i) {
  116. unsigned long pages = min(pages_left, pages_per_node);
  117. uint32_t alignment = mem->page_alignment;
  118. unsigned long start;
  119. if (pages == pages_per_node)
  120. alignment = pages_per_node;
  121. r = drm_mm_insert_node_in_range(mm, &nodes[i],
  122. pages, alignment, 0,
  123. place->fpfn, lpfn,
  124. mode);
  125. if (unlikely(r))
  126. goto error;
  127. /* Calculate a virtual BO start address to easily check if
  128. * everything is CPU accessible.
  129. */
  130. start = nodes[i].start + nodes[i].size;
  131. if (start > mem->num_pages)
  132. start -= mem->num_pages;
  133. else
  134. start = 0;
  135. mem->start = max(mem->start, start);
  136. pages_left -= pages;
  137. }
  138. spin_unlock(&mgr->lock);
  139. mem->mm_node = nodes;
  140. return 0;
  141. error:
  142. while (i--)
  143. drm_mm_remove_node(&nodes[i]);
  144. spin_unlock(&mgr->lock);
  145. kfree(nodes);
  146. return r == -ENOSPC ? 0 : r;
  147. }
  148. /**
  149. * amdgpu_vram_mgr_del - free ranges
  150. *
  151. * @man: TTM memory type manager
  152. * @tbo: TTM BO we need this range for
  153. * @place: placement flags and restrictions
  154. * @mem: TTM memory object
  155. *
  156. * Free the allocated VRAM again.
  157. */
  158. static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
  159. struct ttm_mem_reg *mem)
  160. {
  161. struct amdgpu_vram_mgr *mgr = man->priv;
  162. struct drm_mm_node *nodes = mem->mm_node;
  163. unsigned pages = mem->num_pages;
  164. if (!mem->mm_node)
  165. return;
  166. spin_lock(&mgr->lock);
  167. while (pages) {
  168. pages -= nodes->size;
  169. drm_mm_remove_node(nodes);
  170. ++nodes;
  171. }
  172. spin_unlock(&mgr->lock);
  173. kfree(mem->mm_node);
  174. mem->mm_node = NULL;
  175. }
  176. /**
  177. * amdgpu_vram_mgr_debug - dump VRAM table
  178. *
  179. * @man: TTM memory type manager
  180. * @printer: DRM printer to use
  181. *
  182. * Dump the table content using printk.
  183. */
  184. static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
  185. struct drm_printer *printer)
  186. {
  187. struct amdgpu_vram_mgr *mgr = man->priv;
  188. spin_lock(&mgr->lock);
  189. drm_mm_print(&mgr->mm, printer);
  190. spin_unlock(&mgr->lock);
  191. }
  192. const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
  193. .init = amdgpu_vram_mgr_init,
  194. .takedown = amdgpu_vram_mgr_fini,
  195. .get_node = amdgpu_vram_mgr_new,
  196. .put_node = amdgpu_vram_mgr_del,
  197. .debug = amdgpu_vram_mgr_debug
  198. };