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