amdgpu_vram_mgr.c 5.4 KB

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