amdgpu_vram_mgr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. atomic64_t usage;
  30. atomic64_t vis_usage;
  31. };
  32. /**
  33. * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
  34. *
  35. * @man: TTM memory type manager
  36. * @p_size: maximum size of VRAM
  37. *
  38. * Allocate and initialize the VRAM manager.
  39. */
  40. static int amdgpu_vram_mgr_init(struct ttm_mem_type_manager *man,
  41. unsigned long p_size)
  42. {
  43. struct amdgpu_vram_mgr *mgr;
  44. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  45. if (!mgr)
  46. return -ENOMEM;
  47. drm_mm_init(&mgr->mm, 0, p_size);
  48. spin_lock_init(&mgr->lock);
  49. man->priv = mgr;
  50. return 0;
  51. }
  52. /**
  53. * amdgpu_vram_mgr_fini - free and destroy VRAM manager
  54. *
  55. * @man: TTM memory type manager
  56. *
  57. * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
  58. * allocated inside it.
  59. */
  60. static int amdgpu_vram_mgr_fini(struct ttm_mem_type_manager *man)
  61. {
  62. struct amdgpu_vram_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_vram_mgr_vis_size - Calculate visible node size
  76. *
  77. * @adev: amdgpu device structure
  78. * @node: MM node structure
  79. *
  80. * Calculate how many bytes of the MM node are inside visible VRAM
  81. */
  82. static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
  83. struct drm_mm_node *node)
  84. {
  85. uint64_t start = node->start << PAGE_SHIFT;
  86. uint64_t end = (node->size + node->start) << PAGE_SHIFT;
  87. if (start >= adev->mc.visible_vram_size)
  88. return 0;
  89. return (end > adev->mc.visible_vram_size ?
  90. adev->mc.visible_vram_size : end) - start;
  91. }
  92. /**
  93. * amdgpu_vram_mgr_new - allocate new ranges
  94. *
  95. * @man: TTM memory type manager
  96. * @tbo: TTM BO we need this range for
  97. * @place: placement flags and restrictions
  98. * @mem: the resulting mem object
  99. *
  100. * Allocate VRAM for the given BO.
  101. */
  102. static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
  103. struct ttm_buffer_object *tbo,
  104. const struct ttm_place *place,
  105. struct ttm_mem_reg *mem)
  106. {
  107. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  108. struct amdgpu_vram_mgr *mgr = man->priv;
  109. struct drm_mm *mm = &mgr->mm;
  110. struct drm_mm_node *nodes;
  111. enum drm_mm_insert_mode mode;
  112. unsigned long lpfn, num_nodes, pages_per_node, pages_left;
  113. uint64_t usage = 0, vis_usage = 0;
  114. unsigned i;
  115. int r;
  116. lpfn = place->lpfn;
  117. if (!lpfn)
  118. lpfn = man->size;
  119. if (place->flags & TTM_PL_FLAG_CONTIGUOUS ||
  120. amdgpu_vram_page_split == -1) {
  121. pages_per_node = ~0ul;
  122. num_nodes = 1;
  123. } else {
  124. pages_per_node = max((uint32_t)amdgpu_vram_page_split,
  125. mem->page_alignment);
  126. num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
  127. }
  128. nodes = kcalloc(num_nodes, sizeof(*nodes), GFP_KERNEL);
  129. if (!nodes)
  130. return -ENOMEM;
  131. mode = DRM_MM_INSERT_BEST;
  132. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  133. mode = DRM_MM_INSERT_HIGH;
  134. mem->start = 0;
  135. pages_left = mem->num_pages;
  136. spin_lock(&mgr->lock);
  137. for (i = 0; i < num_nodes; ++i) {
  138. unsigned long pages = min(pages_left, pages_per_node);
  139. uint32_t alignment = mem->page_alignment;
  140. unsigned long start;
  141. if (pages == pages_per_node)
  142. alignment = pages_per_node;
  143. r = drm_mm_insert_node_in_range(mm, &nodes[i],
  144. pages, alignment, 0,
  145. place->fpfn, lpfn,
  146. mode);
  147. if (unlikely(r))
  148. goto error;
  149. usage += nodes[i].size << PAGE_SHIFT;
  150. vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
  151. /* Calculate a virtual BO start address to easily check if
  152. * everything is CPU accessible.
  153. */
  154. start = nodes[i].start + nodes[i].size;
  155. if (start > mem->num_pages)
  156. start -= mem->num_pages;
  157. else
  158. start = 0;
  159. mem->start = max(mem->start, start);
  160. pages_left -= pages;
  161. }
  162. spin_unlock(&mgr->lock);
  163. atomic64_add(usage, &mgr->usage);
  164. atomic64_add(vis_usage, &mgr->vis_usage);
  165. mem->mm_node = nodes;
  166. return 0;
  167. error:
  168. while (i--)
  169. drm_mm_remove_node(&nodes[i]);
  170. spin_unlock(&mgr->lock);
  171. kfree(nodes);
  172. return r == -ENOSPC ? 0 : r;
  173. }
  174. /**
  175. * amdgpu_vram_mgr_del - free ranges
  176. *
  177. * @man: TTM memory type manager
  178. * @tbo: TTM BO we need this range for
  179. * @place: placement flags and restrictions
  180. * @mem: TTM memory object
  181. *
  182. * Free the allocated VRAM again.
  183. */
  184. static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
  185. struct ttm_mem_reg *mem)
  186. {
  187. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  188. struct amdgpu_vram_mgr *mgr = man->priv;
  189. struct drm_mm_node *nodes = mem->mm_node;
  190. uint64_t usage = 0, vis_usage = 0;
  191. unsigned pages = mem->num_pages;
  192. if (!mem->mm_node)
  193. return;
  194. spin_lock(&mgr->lock);
  195. while (pages) {
  196. pages -= nodes->size;
  197. drm_mm_remove_node(nodes);
  198. usage += nodes->size << PAGE_SHIFT;
  199. vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
  200. ++nodes;
  201. }
  202. spin_unlock(&mgr->lock);
  203. atomic64_sub(usage, &mgr->usage);
  204. atomic64_sub(vis_usage, &mgr->vis_usage);
  205. kfree(mem->mm_node);
  206. mem->mm_node = NULL;
  207. }
  208. /**
  209. * amdgpu_vram_mgr_usage - how many bytes are used in this domain
  210. *
  211. * @man: TTM memory type manager
  212. *
  213. * Returns how many bytes are used in this domain.
  214. */
  215. uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man)
  216. {
  217. struct amdgpu_vram_mgr *mgr = man->priv;
  218. return atomic64_read(&mgr->usage);
  219. }
  220. /**
  221. * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
  222. *
  223. * @man: TTM memory type manager
  224. *
  225. * Returns how many bytes are used in the visible part of VRAM
  226. */
  227. uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man)
  228. {
  229. struct amdgpu_vram_mgr *mgr = man->priv;
  230. return atomic64_read(&mgr->vis_usage);
  231. }
  232. /**
  233. * amdgpu_vram_mgr_debug - dump VRAM table
  234. *
  235. * @man: TTM memory type manager
  236. * @printer: DRM printer to use
  237. *
  238. * Dump the table content using printk.
  239. */
  240. static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
  241. struct drm_printer *printer)
  242. {
  243. struct amdgpu_vram_mgr *mgr = man->priv;
  244. spin_lock(&mgr->lock);
  245. drm_mm_print(&mgr->mm, printer);
  246. spin_unlock(&mgr->lock);
  247. drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
  248. man->size, amdgpu_vram_mgr_usage(man) >> 20,
  249. amdgpu_vram_mgr_vis_usage(man) >> 20);
  250. }
  251. const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
  252. .init = amdgpu_vram_mgr_init,
  253. .takedown = amdgpu_vram_mgr_fini,
  254. .get_node = amdgpu_vram_mgr_new,
  255. .put_node = amdgpu_vram_mgr_del,
  256. .debug = amdgpu_vram_mgr_debug
  257. };