amdgpu_vram_mgr.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_bo_invisible_size - CPU invisible BO size
  94. *
  95. * @bo: &amdgpu_bo buffer object (must be in VRAM)
  96. *
  97. * Returns:
  98. * How much of the given &amdgpu_bo buffer object lies in CPU invisible VRAM.
  99. */
  100. u64 amdgpu_vram_mgr_bo_invisible_size(struct amdgpu_bo *bo)
  101. {
  102. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  103. return amdgpu_bo_size(bo);
  104. return 0;
  105. }
  106. /**
  107. * amdgpu_vram_mgr_new - allocate new ranges
  108. *
  109. * @man: TTM memory type manager
  110. * @tbo: TTM BO we need this range for
  111. * @place: placement flags and restrictions
  112. * @mem: the resulting mem object
  113. *
  114. * Allocate VRAM for the given BO.
  115. */
  116. static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
  117. struct ttm_buffer_object *tbo,
  118. const struct ttm_place *place,
  119. struct ttm_mem_reg *mem)
  120. {
  121. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  122. struct amdgpu_vram_mgr *mgr = man->priv;
  123. struct drm_mm *mm = &mgr->mm;
  124. struct drm_mm_node *nodes;
  125. enum drm_mm_insert_mode mode;
  126. unsigned long lpfn, num_nodes, pages_per_node, pages_left;
  127. uint64_t usage = 0, vis_usage = 0;
  128. unsigned i;
  129. int r;
  130. lpfn = place->lpfn;
  131. if (!lpfn)
  132. lpfn = man->size;
  133. if (place->flags & TTM_PL_FLAG_CONTIGUOUS ||
  134. amdgpu_vram_page_split == -1) {
  135. pages_per_node = ~0ul;
  136. num_nodes = 1;
  137. } else {
  138. pages_per_node = max((uint32_t)amdgpu_vram_page_split,
  139. mem->page_alignment);
  140. num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
  141. }
  142. nodes = kvmalloc_array(num_nodes, sizeof(*nodes),
  143. GFP_KERNEL | __GFP_ZERO);
  144. if (!nodes)
  145. return -ENOMEM;
  146. mode = DRM_MM_INSERT_BEST;
  147. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  148. mode = DRM_MM_INSERT_HIGH;
  149. mem->start = 0;
  150. pages_left = mem->num_pages;
  151. spin_lock(&mgr->lock);
  152. for (i = 0; i < num_nodes; ++i) {
  153. unsigned long pages = min(pages_left, pages_per_node);
  154. uint32_t alignment = mem->page_alignment;
  155. unsigned long start;
  156. if (pages == pages_per_node)
  157. alignment = pages_per_node;
  158. r = drm_mm_insert_node_in_range(mm, &nodes[i],
  159. pages, alignment, 0,
  160. place->fpfn, lpfn,
  161. mode);
  162. if (unlikely(r))
  163. goto error;
  164. usage += nodes[i].size << PAGE_SHIFT;
  165. vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
  166. /* Calculate a virtual BO start address to easily check if
  167. * everything is CPU accessible.
  168. */
  169. start = nodes[i].start + nodes[i].size;
  170. if (start > mem->num_pages)
  171. start -= mem->num_pages;
  172. else
  173. start = 0;
  174. mem->start = max(mem->start, start);
  175. pages_left -= pages;
  176. }
  177. spin_unlock(&mgr->lock);
  178. atomic64_add(usage, &mgr->usage);
  179. atomic64_add(vis_usage, &mgr->vis_usage);
  180. mem->mm_node = nodes;
  181. return 0;
  182. error:
  183. while (i--)
  184. drm_mm_remove_node(&nodes[i]);
  185. spin_unlock(&mgr->lock);
  186. kvfree(nodes);
  187. return r == -ENOSPC ? 0 : r;
  188. }
  189. /**
  190. * amdgpu_vram_mgr_del - free ranges
  191. *
  192. * @man: TTM memory type manager
  193. * @tbo: TTM BO we need this range for
  194. * @place: placement flags and restrictions
  195. * @mem: TTM memory object
  196. *
  197. * Free the allocated VRAM again.
  198. */
  199. static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
  200. struct ttm_mem_reg *mem)
  201. {
  202. struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
  203. struct amdgpu_vram_mgr *mgr = man->priv;
  204. struct drm_mm_node *nodes = mem->mm_node;
  205. uint64_t usage = 0, vis_usage = 0;
  206. unsigned pages = mem->num_pages;
  207. if (!mem->mm_node)
  208. return;
  209. spin_lock(&mgr->lock);
  210. while (pages) {
  211. pages -= nodes->size;
  212. drm_mm_remove_node(nodes);
  213. usage += nodes->size << PAGE_SHIFT;
  214. vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
  215. ++nodes;
  216. }
  217. spin_unlock(&mgr->lock);
  218. atomic64_sub(usage, &mgr->usage);
  219. atomic64_sub(vis_usage, &mgr->vis_usage);
  220. kvfree(mem->mm_node);
  221. mem->mm_node = NULL;
  222. }
  223. /**
  224. * amdgpu_vram_mgr_usage - how many bytes are used in this domain
  225. *
  226. * @man: TTM memory type manager
  227. *
  228. * Returns how many bytes are used in this domain.
  229. */
  230. uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man)
  231. {
  232. struct amdgpu_vram_mgr *mgr = man->priv;
  233. return atomic64_read(&mgr->usage);
  234. }
  235. /**
  236. * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
  237. *
  238. * @man: TTM memory type manager
  239. *
  240. * Returns how many bytes are used in the visible part of VRAM
  241. */
  242. uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man)
  243. {
  244. struct amdgpu_vram_mgr *mgr = man->priv;
  245. return atomic64_read(&mgr->vis_usage);
  246. }
  247. /**
  248. * amdgpu_vram_mgr_debug - dump VRAM table
  249. *
  250. * @man: TTM memory type manager
  251. * @printer: DRM printer to use
  252. *
  253. * Dump the table content using printk.
  254. */
  255. static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
  256. struct drm_printer *printer)
  257. {
  258. struct amdgpu_vram_mgr *mgr = man->priv;
  259. spin_lock(&mgr->lock);
  260. drm_mm_print(&mgr->mm, printer);
  261. spin_unlock(&mgr->lock);
  262. drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
  263. man->size, amdgpu_vram_mgr_usage(man) >> 20,
  264. amdgpu_vram_mgr_vis_usage(man) >> 20);
  265. }
  266. const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
  267. .init = amdgpu_vram_mgr_init,
  268. .takedown = amdgpu_vram_mgr_fini,
  269. .get_node = amdgpu_vram_mgr_new,
  270. .put_node = amdgpu_vram_mgr_del,
  271. .debug = amdgpu_vram_mgr_debug
  272. };