amdgpu_vram_mgr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_bo *bo = container_of(tbo, struct amdgpu_bo, tbo);
  88. struct amdgpu_vram_mgr *mgr = man->priv;
  89. struct drm_mm *mm = &mgr->mm;
  90. struct drm_mm_node *nodes;
  91. enum drm_mm_search_flags sflags = DRM_MM_SEARCH_DEFAULT;
  92. enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT;
  93. unsigned long lpfn, num_nodes, pages_per_node, pages_left;
  94. unsigned i;
  95. int r;
  96. lpfn = place->lpfn;
  97. if (!lpfn)
  98. lpfn = man->size;
  99. if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS ||
  100. place->lpfn || amdgpu_vram_page_split == -1) {
  101. pages_per_node = ~0ul;
  102. num_nodes = 1;
  103. } else {
  104. pages_per_node = max((uint32_t)amdgpu_vram_page_split,
  105. mem->page_alignment);
  106. num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
  107. }
  108. nodes = kcalloc(num_nodes, sizeof(*nodes), GFP_KERNEL);
  109. if (!nodes)
  110. return -ENOMEM;
  111. if (place->flags & TTM_PL_FLAG_TOPDOWN) {
  112. sflags = DRM_MM_SEARCH_BELOW;
  113. aflags = DRM_MM_CREATE_TOP;
  114. }
  115. pages_left = mem->num_pages;
  116. spin_lock(&mgr->lock);
  117. for (i = 0; i < num_nodes; ++i) {
  118. unsigned long pages = min(pages_left, pages_per_node);
  119. uint32_t alignment = mem->page_alignment;
  120. if (pages == pages_per_node)
  121. alignment = pages_per_node;
  122. else
  123. sflags |= DRM_MM_SEARCH_BEST;
  124. r = drm_mm_insert_node_in_range_generic(mm, &nodes[i], pages,
  125. alignment, 0,
  126. place->fpfn, lpfn,
  127. sflags, aflags);
  128. if (unlikely(r))
  129. goto error;
  130. pages_left -= pages;
  131. }
  132. spin_unlock(&mgr->lock);
  133. mem->start = num_nodes == 1 ? nodes[0].start : AMDGPU_BO_INVALID_OFFSET;
  134. mem->mm_node = nodes;
  135. return 0;
  136. error:
  137. while (i--)
  138. drm_mm_remove_node(&nodes[i]);
  139. spin_unlock(&mgr->lock);
  140. kfree(nodes);
  141. return r == -ENOSPC ? 0 : r;
  142. }
  143. /**
  144. * amdgpu_vram_mgr_del - free ranges
  145. *
  146. * @man: TTM memory type manager
  147. * @tbo: TTM BO we need this range for
  148. * @place: placement flags and restrictions
  149. * @mem: TTM memory object
  150. *
  151. * Free the allocated VRAM again.
  152. */
  153. static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
  154. struct ttm_mem_reg *mem)
  155. {
  156. struct amdgpu_vram_mgr *mgr = man->priv;
  157. struct drm_mm_node *nodes = mem->mm_node;
  158. unsigned pages = mem->num_pages;
  159. if (!mem->mm_node)
  160. return;
  161. spin_lock(&mgr->lock);
  162. while (pages) {
  163. pages -= nodes->size;
  164. drm_mm_remove_node(nodes);
  165. ++nodes;
  166. }
  167. spin_unlock(&mgr->lock);
  168. kfree(mem->mm_node);
  169. mem->mm_node = NULL;
  170. }
  171. /**
  172. * amdgpu_vram_mgr_debug - dump VRAM table
  173. *
  174. * @man: TTM memory type manager
  175. * @prefix: text prefix
  176. *
  177. * Dump the table content using printk.
  178. */
  179. static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
  180. const char *prefix)
  181. {
  182. struct amdgpu_vram_mgr *mgr = man->priv;
  183. struct drm_printer p = drm_debug_printer(prefix);
  184. spin_lock(&mgr->lock);
  185. drm_mm_print(&mgr->mm, &p);
  186. spin_unlock(&mgr->lock);
  187. }
  188. const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
  189. amdgpu_vram_mgr_init,
  190. amdgpu_vram_mgr_fini,
  191. amdgpu_vram_mgr_new,
  192. amdgpu_vram_mgr_del,
  193. amdgpu_vram_mgr_debug
  194. };