amdgpu_gtt_mgr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. uint64_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_gtt_mgr *mgr;
  43. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  44. if (!mgr)
  45. return -ENOMEM;
  46. drm_mm_init(&mgr->mm, 0, p_size);
  47. spin_lock_init(&mgr->lock);
  48. mgr->available = p_size;
  49. man->priv = mgr;
  50. return 0;
  51. }
  52. /**
  53. * amdgpu_gtt_mgr_fini - free and destroy GTT manager
  54. *
  55. * @man: TTM memory type manager
  56. *
  57. * Destroy and free the GTT manager, returns -EBUSY if ranges are still
  58. * allocated inside it.
  59. */
  60. static int amdgpu_gtt_mgr_fini(struct ttm_mem_type_manager *man)
  61. {
  62. struct amdgpu_gtt_mgr *mgr = man->priv;
  63. spin_lock(&mgr->lock);
  64. if (!drm_mm_clean(&mgr->mm)) {
  65. spin_unlock(&mgr->lock);
  66. return -EBUSY;
  67. }
  68. drm_mm_takedown(&mgr->mm);
  69. spin_unlock(&mgr->lock);
  70. kfree(mgr);
  71. man->priv = NULL;
  72. return 0;
  73. }
  74. /**
  75. * amdgpu_gtt_mgr_is_allocated - Check if mem has address space
  76. *
  77. * @mem: the mem object to check
  78. *
  79. * Check if a mem object has already address space allocated.
  80. */
  81. bool amdgpu_gtt_mgr_is_allocated(struct ttm_mem_reg *mem)
  82. {
  83. struct drm_mm_node *node = mem->mm_node;
  84. return (node->start != AMDGPU_BO_INVALID_OFFSET);
  85. }
  86. /**
  87. * amdgpu_gtt_mgr_alloc - allocate new ranges
  88. *
  89. * @man: TTM memory type manager
  90. * @tbo: TTM BO we need this range for
  91. * @place: placement flags and restrictions
  92. * @mem: the resulting mem object
  93. *
  94. * Allocate the address space for a node.
  95. */
  96. int amdgpu_gtt_mgr_alloc(struct ttm_mem_type_manager *man,
  97. struct ttm_buffer_object *tbo,
  98. const struct ttm_place *place,
  99. struct ttm_mem_reg *mem)
  100. {
  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_is_allocated(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 = man->size;
  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. if (&tbo->mem == mem)
  127. tbo->offset = (tbo->mem.start << PAGE_SHIFT) +
  128. tbo->bdev->man[tbo->mem.mem_type].gpu_offset;
  129. }
  130. return r;
  131. }
  132. void amdgpu_gtt_mgr_print(struct seq_file *m, struct ttm_mem_type_manager *man)
  133. {
  134. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  135. struct amdgpu_gtt_mgr *mgr = man->priv;
  136. seq_printf(m, "man size:%llu pages, gtt available:%llu pages, usage:%lluMB\n",
  137. man->size, mgr->available, (u64)atomic64_read(&adev->gtt_usage) >> 20);
  138. }
  139. /**
  140. * amdgpu_gtt_mgr_new - allocate a new node
  141. *
  142. * @man: TTM memory type manager
  143. * @tbo: TTM BO we need this range for
  144. * @place: placement flags and restrictions
  145. * @mem: the resulting mem object
  146. *
  147. * Dummy, allocate the node but no space for it yet.
  148. */
  149. static int amdgpu_gtt_mgr_new(struct ttm_mem_type_manager *man,
  150. struct ttm_buffer_object *tbo,
  151. const struct ttm_place *place,
  152. struct ttm_mem_reg *mem)
  153. {
  154. struct amdgpu_gtt_mgr *mgr = man->priv;
  155. struct drm_mm_node *node;
  156. int r;
  157. spin_lock(&mgr->lock);
  158. if (mgr->available < mem->num_pages) {
  159. spin_unlock(&mgr->lock);
  160. return 0;
  161. }
  162. mgr->available -= mem->num_pages;
  163. spin_unlock(&mgr->lock);
  164. node = kzalloc(sizeof(*node), GFP_KERNEL);
  165. if (!node) {
  166. r = -ENOMEM;
  167. goto err_out;
  168. }
  169. node->start = AMDGPU_BO_INVALID_OFFSET;
  170. node->size = mem->num_pages;
  171. mem->mm_node = node;
  172. if (place->fpfn || place->lpfn || place->flags & TTM_PL_FLAG_TOPDOWN) {
  173. r = amdgpu_gtt_mgr_alloc(man, tbo, place, mem);
  174. if (unlikely(r)) {
  175. kfree(node);
  176. mem->mm_node = NULL;
  177. r = 0;
  178. goto err_out;
  179. }
  180. } else {
  181. mem->start = node->start;
  182. }
  183. return 0;
  184. err_out:
  185. spin_lock(&mgr->lock);
  186. mgr->available += mem->num_pages;
  187. spin_unlock(&mgr->lock);
  188. return r;
  189. }
  190. /**
  191. * amdgpu_gtt_mgr_del - free ranges
  192. *
  193. * @man: TTM memory type manager
  194. * @tbo: TTM BO we need this range for
  195. * @place: placement flags and restrictions
  196. * @mem: TTM memory object
  197. *
  198. * Free the allocated GTT again.
  199. */
  200. static void amdgpu_gtt_mgr_del(struct ttm_mem_type_manager *man,
  201. struct ttm_mem_reg *mem)
  202. {
  203. struct amdgpu_gtt_mgr *mgr = man->priv;
  204. struct drm_mm_node *node = mem->mm_node;
  205. if (!node)
  206. return;
  207. spin_lock(&mgr->lock);
  208. if (node->start != AMDGPU_BO_INVALID_OFFSET)
  209. drm_mm_remove_node(node);
  210. mgr->available += mem->num_pages;
  211. spin_unlock(&mgr->lock);
  212. kfree(node);
  213. mem->mm_node = NULL;
  214. }
  215. /**
  216. * amdgpu_gtt_mgr_debug - dump VRAM table
  217. *
  218. * @man: TTM memory type manager
  219. * @prefix: text prefix
  220. *
  221. * Dump the table content using printk.
  222. */
  223. static void amdgpu_gtt_mgr_debug(struct ttm_mem_type_manager *man,
  224. const char *prefix)
  225. {
  226. struct amdgpu_gtt_mgr *mgr = man->priv;
  227. struct drm_printer p = drm_debug_printer(prefix);
  228. spin_lock(&mgr->lock);
  229. drm_mm_print(&mgr->mm, &p);
  230. spin_unlock(&mgr->lock);
  231. }
  232. const struct ttm_mem_type_manager_func amdgpu_gtt_mgr_func = {
  233. .init = amdgpu_gtt_mgr_init,
  234. .takedown = amdgpu_gtt_mgr_fini,
  235. .get_node = amdgpu_gtt_mgr_new,
  236. .put_node = amdgpu_gtt_mgr_del,
  237. .debug = amdgpu_gtt_mgr_debug
  238. };