ttm_device.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * Copyright 2020 Advanced Micro Devices, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Christian König
  25. */
  26. #define pr_fmt(fmt) "[TTM DEVICE] " fmt
  27. #include <linux/debugfs.h>
  28. #include <linux/mm.h>
  29. #include <drm/ttm/ttm_bo.h>
  30. #include <drm/ttm/ttm_device.h>
  31. #include <drm/ttm/ttm_tt.h>
  32. #include <drm/ttm/ttm_placement.h>
  33. #include "ttm_module.h"
  34. /*
  35. * ttm_global_mutex - protecting the global state
  36. */
  37. static DEFINE_MUTEX(ttm_global_mutex);
  38. static unsigned ttm_glob_use_count;
  39. struct ttm_global ttm_glob;
  40. EXPORT_SYMBOL(ttm_glob);
  41. struct dentry *ttm_debugfs_root;
  42. static void ttm_global_release(void)
  43. {
  44. struct ttm_global *glob = &ttm_glob;
  45. mutex_lock(&ttm_global_mutex);
  46. if (--ttm_glob_use_count > 0)
  47. goto out;
  48. ttm_pool_mgr_fini();
  49. debugfs_remove(ttm_debugfs_root);
  50. __free_page(glob->dummy_read_page);
  51. memset(glob, 0, sizeof(*glob));
  52. out:
  53. mutex_unlock(&ttm_global_mutex);
  54. }
  55. static int ttm_global_init(void)
  56. {
  57. struct ttm_global *glob = &ttm_glob;
  58. unsigned long num_pages, num_dma32;
  59. struct sysinfo si;
  60. int ret = 0;
  61. mutex_lock(&ttm_global_mutex);
  62. if (++ttm_glob_use_count > 1)
  63. goto out;
  64. si_meminfo(&si);
  65. ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
  66. if (IS_ERR(ttm_debugfs_root)) {
  67. ttm_debugfs_root = NULL;
  68. }
  69. /* Limit the number of pages in the pool to about 50% of the total
  70. * system memory.
  71. */
  72. num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
  73. num_pages /= 2;
  74. /* But for DMA32 we limit ourself to only use 2GiB maximum. */
  75. num_dma32 = (u64)(si.totalram - si.totalhigh) * si.mem_unit
  76. >> PAGE_SHIFT;
  77. num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT));
  78. ttm_pool_mgr_init(num_pages);
  79. ttm_tt_mgr_init(num_pages, num_dma32);
  80. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32 |
  81. __GFP_NOWARN);
  82. /* Retry without GFP_DMA32 for platforms DMA32 is not available */
  83. if (unlikely(glob->dummy_read_page == NULL)) {
  84. glob->dummy_read_page = alloc_page(__GFP_ZERO);
  85. if (unlikely(glob->dummy_read_page == NULL)) {
  86. ret = -ENOMEM;
  87. goto out;
  88. }
  89. pr_warn("Using GFP_DMA32 fallback for dummy_read_page\n");
  90. }
  91. INIT_LIST_HEAD(&glob->device_list);
  92. atomic_set(&glob->bo_count, 0);
  93. debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
  94. &glob->bo_count);
  95. out:
  96. if (ret && ttm_debugfs_root)
  97. debugfs_remove(ttm_debugfs_root);
  98. if (ret)
  99. --ttm_glob_use_count;
  100. mutex_unlock(&ttm_global_mutex);
  101. return ret;
  102. }
  103. /*
  104. * A buffer object shrink method that tries to swap out the first
  105. * buffer object on the global::swap_lru list.
  106. */
  107. int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
  108. {
  109. struct ttm_global *glob = &ttm_glob;
  110. struct ttm_device *bdev;
  111. int ret = 0;
  112. mutex_lock(&ttm_global_mutex);
  113. list_for_each_entry(bdev, &glob->device_list, device_list) {
  114. ret = ttm_device_swapout(bdev, ctx, gfp_flags);
  115. if (ret > 0) {
  116. list_move_tail(&bdev->device_list, &glob->device_list);
  117. break;
  118. }
  119. }
  120. mutex_unlock(&ttm_global_mutex);
  121. return ret;
  122. }
  123. int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  124. gfp_t gfp_flags)
  125. {
  126. struct ttm_resource_manager *man;
  127. unsigned i;
  128. s64 lret;
  129. for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
  130. man = ttm_manager_type(bdev, i);
  131. if (!man || !man->use_tt)
  132. continue;
  133. lret = ttm_bo_swapout(bdev, ctx, man, gfp_flags, 1);
  134. /* Can be both positive (num_pages) and negative (error) */
  135. if (lret)
  136. return lret;
  137. }
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(ttm_device_swapout);
  141. /**
  142. * ttm_device_init
  143. *
  144. * @bdev: A pointer to a struct ttm_device to initialize.
  145. * @funcs: Function table for the device.
  146. * @dev: The core kernel device pointer for DMA mappings and allocations.
  147. * @mapping: The address space to use for this bo.
  148. * @vma_manager: A pointer to a vma manager.
  149. * @use_dma_alloc: If coherent DMA allocation API should be used.
  150. * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
  151. *
  152. * Initializes a struct ttm_device:
  153. * Returns:
  154. * !0: Failure.
  155. */
  156. int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
  157. struct device *dev, struct address_space *mapping,
  158. struct drm_vma_offset_manager *vma_manager,
  159. bool use_dma_alloc, bool use_dma32)
  160. {
  161. struct ttm_global *glob = &ttm_glob;
  162. int ret, nid;
  163. if (WARN_ON(vma_manager == NULL))
  164. return -EINVAL;
  165. ret = ttm_global_init();
  166. if (ret)
  167. return ret;
  168. bdev->wq = alloc_workqueue("ttm",
  169. WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16);
  170. if (!bdev->wq) {
  171. ttm_global_release();
  172. return -ENOMEM;
  173. }
  174. bdev->funcs = funcs;
  175. ttm_sys_man_init(bdev);
  176. if (dev)
  177. nid = dev_to_node(dev);
  178. else
  179. nid = NUMA_NO_NODE;
  180. ttm_pool_init(&bdev->pool, dev, nid, use_dma_alloc, use_dma32);
  181. bdev->vma_manager = vma_manager;
  182. spin_lock_init(&bdev->lru_lock);
  183. INIT_LIST_HEAD(&bdev->pinned);
  184. bdev->dev_mapping = mapping;
  185. mutex_lock(&ttm_global_mutex);
  186. list_add_tail(&bdev->device_list, &glob->device_list);
  187. mutex_unlock(&ttm_global_mutex);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(ttm_device_init);
  191. void ttm_device_fini(struct ttm_device *bdev)
  192. {
  193. struct ttm_resource_manager *man;
  194. unsigned i;
  195. mutex_lock(&ttm_global_mutex);
  196. list_del(&bdev->device_list);
  197. mutex_unlock(&ttm_global_mutex);
  198. drain_workqueue(bdev->wq);
  199. destroy_workqueue(bdev->wq);
  200. man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
  201. ttm_resource_manager_set_used(man, false);
  202. ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
  203. spin_lock(&bdev->lru_lock);
  204. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  205. if (list_empty(&man->lru[0]))
  206. pr_debug("Swap list %d was clean\n", i);
  207. spin_unlock(&bdev->lru_lock);
  208. ttm_pool_fini(&bdev->pool);
  209. ttm_global_release();
  210. }
  211. EXPORT_SYMBOL(ttm_device_fini);
  212. static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
  213. struct list_head *list)
  214. {
  215. struct ttm_resource *res;
  216. spin_lock(&bdev->lru_lock);
  217. while ((res = ttm_lru_first_res_or_null(list))) {
  218. struct ttm_buffer_object *bo = res->bo;
  219. /* Take ref against racing releases once lru_lock is unlocked */
  220. if (!ttm_bo_get_unless_zero(bo))
  221. continue;
  222. list_del_init(&bo->resource->lru.link);
  223. spin_unlock(&bdev->lru_lock);
  224. if (bo->ttm)
  225. ttm_tt_unpopulate(bo->bdev, bo->ttm);
  226. ttm_bo_put(bo);
  227. spin_lock(&bdev->lru_lock);
  228. }
  229. spin_unlock(&bdev->lru_lock);
  230. }
  231. void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
  232. {
  233. struct ttm_resource_manager *man;
  234. unsigned int i, j;
  235. ttm_device_clear_lru_dma_mappings(bdev, &bdev->pinned);
  236. for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
  237. man = ttm_manager_type(bdev, i);
  238. if (!man || !man->use_tt)
  239. continue;
  240. for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
  241. ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
  242. }
  243. }
  244. EXPORT_SYMBOL(ttm_device_clear_dma_mappings);