amdgpu_gtt_mgr.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_gtt_mgr {
  27. struct drm_mm mm;
  28. spinlock_t lock;
  29. atomic64_t available;
  30. };
  31. /**
  32. * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
  33. *
  34. * @man: TTM memory type manager
  35. * @p_size: maximum size of GTT
  36. *
  37. * Allocate and initialize the GTT manager.
  38. */
  39. static int amdgpu_gtt_mgr_init(struct ttm_mem_type_manager *man,
  40. unsigned long p_size)
  41. {
  42. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  43. struct amdgpu_gtt_mgr *mgr;
  44. uint64_t start, size;
  45. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  46. if (!mgr)
  47. return -ENOMEM;
  48. start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS;
  49. size = (adev->mc.gart_size >> PAGE_SHIFT) - start;
  50. drm_mm_init(&mgr->mm, start, size);
  51. spin_lock_init(&mgr->lock);
  52. atomic64_set(&mgr->available, p_size);
  53. man->priv = mgr;
  54. return 0;
  55. }
  56. /**
  57. * amdgpu_gtt_mgr_fini - free and destroy GTT manager
  58. *
  59. * @man: TTM memory type manager
  60. *
  61. * Destroy and free the GTT manager, returns -EBUSY if ranges are still
  62. * allocated inside it.
  63. */
  64. static int amdgpu_gtt_mgr_fini(struct ttm_mem_type_manager *man)
  65. {
  66. struct amdgpu_gtt_mgr *mgr = man->priv;
  67. drm_mm_takedown(&mgr->mm);
  68. spin_unlock(&mgr->lock);
  69. kfree(mgr);
  70. man->priv = NULL;
  71. return 0;
  72. }
  73. /**
  74. * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
  75. *
  76. * @mem: the mem object to check
  77. *
  78. * Check if a mem object has already address space allocated.
  79. */
  80. bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_mem_reg *mem)
  81. {
  82. struct drm_mm_node *node = mem->mm_node;
  83. return (node->start != AMDGPU_BO_INVALID_OFFSET);
  84. }
  85. /**
  86. * amdgpu_gtt_mgr_alloc - allocate new ranges
  87. *
  88. * @man: TTM memory type manager
  89. * @tbo: TTM BO we need this range for
  90. * @place: placement flags and restrictions
  91. * @mem: the resulting mem object
  92. *
  93. * Allocate the address space for a node.
  94. */
  95. static int amdgpu_gtt_mgr_alloc(struct ttm_mem_type_manager *man,
  96. struct ttm_buffer_object *tbo,
  97. const struct ttm_place *place,
  98. struct ttm_mem_reg *mem)
  99. {
  100. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  101. struct amdgpu_gtt_mgr *mgr = man->priv;
  102. struct drm_mm_node *node = mem->mm_node;
  103. enum drm_mm_insert_mode mode;
  104. unsigned long fpfn, lpfn;
  105. int r;
  106. if (amdgpu_gtt_mgr_has_gart_addr(mem))
  107. return 0;
  108. if (place)
  109. fpfn = place->fpfn;
  110. else
  111. fpfn = 0;
  112. if (place && place->lpfn)
  113. lpfn = place->lpfn;
  114. else
  115. lpfn = adev->gart.num_cpu_pages;
  116. mode = DRM_MM_INSERT_BEST;
  117. if (place && place->flags & TTM_PL_FLAG_TOPDOWN)
  118. mode = DRM_MM_INSERT_HIGH;
  119. spin_lock(&mgr->lock);
  120. r = drm_mm_insert_node_in_range(&mgr->mm, node,
  121. mem->num_pages, mem->page_alignment, 0,
  122. fpfn, lpfn, mode);
  123. spin_unlock(&mgr->lock);
  124. if (!r)
  125. mem->start = node->start;
  126. return r;
  127. }
  128. /**
  129. * amdgpu_gtt_mgr_new - allocate a new node
  130. *
  131. * @man: TTM memory type manager
  132. * @tbo: TTM BO we need this range for
  133. * @place: placement flags and restrictions
  134. * @mem: the resulting mem object
  135. *
  136. * Dummy, allocate the node but no space for it yet.
  137. */
  138. static int amdgpu_gtt_mgr_new(struct ttm_mem_type_manager *man,
  139. struct ttm_buffer_object *tbo,
  140. const struct ttm_place *place,
  141. struct ttm_mem_reg *mem)
  142. {
  143. struct amdgpu_gtt_mgr *mgr = man->priv;
  144. struct drm_mm_node *node;
  145. int r;
  146. spin_lock(&mgr->lock);
  147. if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) &&
  148. atomic64_read(&mgr->available) < mem->num_pages) {
  149. spin_unlock(&mgr->lock);
  150. return 0;
  151. }
  152. atomic64_sub(mem->num_pages, &mgr->available);
  153. spin_unlock(&mgr->lock);
  154. node = kzalloc(sizeof(*node), GFP_KERNEL);
  155. if (!node) {
  156. r = -ENOMEM;
  157. goto err_out;
  158. }
  159. node->start = AMDGPU_BO_INVALID_OFFSET;
  160. node->size = mem->num_pages;
  161. mem->mm_node = node;
  162. if (place->fpfn || place->lpfn || place->flags & TTM_PL_FLAG_TOPDOWN) {
  163. r = amdgpu_gtt_mgr_alloc(man, tbo, place, mem);
  164. if (unlikely(r)) {
  165. kfree(node);
  166. mem->mm_node = NULL;
  167. r = 0;
  168. goto err_out;
  169. }
  170. } else {
  171. mem->start = node->start;
  172. }
  173. return 0;
  174. err_out:
  175. atomic64_add(mem->num_pages, &mgr->available);
  176. return r;
  177. }
  178. /**
  179. * amdgpu_gtt_mgr_del - free ranges
  180. *
  181. * @man: TTM memory type manager
  182. * @tbo: TTM BO we need this range for
  183. * @place: placement flags and restrictions
  184. * @mem: TTM memory object
  185. *
  186. * Free the allocated GTT again.
  187. */
  188. static void amdgpu_gtt_mgr_del(struct ttm_mem_type_manager *man,
  189. struct ttm_mem_reg *mem)
  190. {
  191. struct amdgpu_gtt_mgr *mgr = man->priv;
  192. struct drm_mm_node *node = mem->mm_node;
  193. if (!node)
  194. return;
  195. spin_lock(&mgr->lock);
  196. if (node->start != AMDGPU_BO_INVALID_OFFSET)
  197. drm_mm_remove_node(node);
  198. spin_unlock(&mgr->lock);
  199. atomic64_add(mem->num_pages, &mgr->available);
  200. kfree(node);
  201. mem->mm_node = NULL;
  202. }
  203. /**
  204. * amdgpu_gtt_mgr_usage - return usage of GTT domain
  205. *
  206. * @man: TTM memory type manager
  207. *
  208. * Return how many bytes are used in the GTT domain
  209. */
  210. uint64_t amdgpu_gtt_mgr_usage(struct ttm_mem_type_manager *man)
  211. {
  212. struct amdgpu_gtt_mgr *mgr = man->priv;
  213. s64 result = man->size - atomic64_read(&mgr->available);
  214. return (result > 0 ? result : 0) * PAGE_SIZE;
  215. }
  216. /**
  217. * amdgpu_gtt_mgr_debug - dump VRAM table
  218. *
  219. * @man: TTM memory type manager
  220. * @printer: DRM printer to use
  221. *
  222. * Dump the table content using printk.
  223. */
  224. static void amdgpu_gtt_mgr_debug(struct ttm_mem_type_manager *man,
  225. struct drm_printer *printer)
  226. {
  227. struct amdgpu_gtt_mgr *mgr = man->priv;
  228. spin_lock(&mgr->lock);
  229. drm_mm_print(&mgr->mm, printer);
  230. spin_unlock(&mgr->lock);
  231. drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n",
  232. man->size, (u64)atomic64_read(&mgr->available),
  233. amdgpu_gtt_mgr_usage(man) >> 20);
  234. }
  235. const struct ttm_mem_type_manager_func amdgpu_gtt_mgr_func = {
  236. .init = amdgpu_gtt_mgr_init,
  237. .takedown = amdgpu_gtt_mgr_fini,
  238. .get_node = amdgpu_gtt_mgr_new,
  239. .put_node = amdgpu_gtt_mgr_del,
  240. .debug = amdgpu_gtt_mgr_debug
  241. };