ttm_device.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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/export.h>
  29. #include <linux/mm.h>
  30. #include <drm/ttm/ttm_allocation.h>
  31. #include <drm/ttm/ttm_bo.h>
  32. #include <drm/ttm/ttm_device.h>
  33. #include <drm/ttm/ttm_tt.h>
  34. #include <drm/ttm/ttm_placement.h>
  35. #include "ttm_module.h"
  36. #include "ttm_bo_internal.h"
  37. /*
  38. * ttm_global_mutex - protecting the global state
  39. */
  40. static DEFINE_MUTEX(ttm_global_mutex);
  41. static unsigned ttm_glob_use_count;
  42. struct ttm_global ttm_glob;
  43. EXPORT_SYMBOL(ttm_glob);
  44. struct dentry *ttm_debugfs_root;
  45. static void ttm_global_release(void)
  46. {
  47. struct ttm_global *glob = &ttm_glob;
  48. mutex_lock(&ttm_global_mutex);
  49. if (--ttm_glob_use_count > 0)
  50. goto out;
  51. ttm_pool_mgr_fini();
  52. debugfs_remove(ttm_debugfs_root);
  53. __free_page(glob->dummy_read_page);
  54. memset(glob, 0, sizeof(*glob));
  55. out:
  56. mutex_unlock(&ttm_global_mutex);
  57. }
  58. static int ttm_global_init(void)
  59. {
  60. struct ttm_global *glob = &ttm_glob;
  61. unsigned long num_pages, num_dma32;
  62. struct sysinfo si;
  63. int ret = 0;
  64. mutex_lock(&ttm_global_mutex);
  65. if (++ttm_glob_use_count > 1)
  66. goto out;
  67. si_meminfo(&si);
  68. ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
  69. if (IS_ERR(ttm_debugfs_root)) {
  70. ttm_debugfs_root = NULL;
  71. }
  72. /* Limit the number of pages in the pool to about 50% of the total
  73. * system memory.
  74. */
  75. num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
  76. num_pages /= 2;
  77. /* But for DMA32 we limit ourself to only use 2GiB maximum. */
  78. num_dma32 = (u64)(si.totalram - si.totalhigh) * si.mem_unit
  79. >> PAGE_SHIFT;
  80. num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT));
  81. ttm_pool_mgr_init(num_pages);
  82. ttm_tt_mgr_init(num_pages, num_dma32);
  83. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32 |
  84. __GFP_NOWARN);
  85. /* Retry without GFP_DMA32 for platforms DMA32 is not available */
  86. if (unlikely(glob->dummy_read_page == NULL)) {
  87. glob->dummy_read_page = alloc_page(__GFP_ZERO);
  88. if (unlikely(glob->dummy_read_page == NULL)) {
  89. ret = -ENOMEM;
  90. goto out;
  91. }
  92. pr_warn("Using GFP_DMA32 fallback for dummy_read_page\n");
  93. }
  94. INIT_LIST_HEAD(&glob->device_list);
  95. atomic_set(&glob->bo_count, 0);
  96. debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
  97. &glob->bo_count);
  98. out:
  99. if (ret && ttm_debugfs_root)
  100. debugfs_remove(ttm_debugfs_root);
  101. if (ret)
  102. --ttm_glob_use_count;
  103. mutex_unlock(&ttm_global_mutex);
  104. return ret;
  105. }
  106. /**
  107. * ttm_device_prepare_hibernation - move GTT BOs to shmem for hibernation.
  108. *
  109. * @bdev: A pointer to a struct ttm_device to prepare hibernation for.
  110. *
  111. * Return: 0 on success, negative number on failure.
  112. */
  113. int ttm_device_prepare_hibernation(struct ttm_device *bdev)
  114. {
  115. struct ttm_operation_ctx ctx = { };
  116. int ret;
  117. do {
  118. ret = ttm_device_swapout(bdev, &ctx, GFP_KERNEL);
  119. } while (ret > 0);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL(ttm_device_prepare_hibernation);
  123. /*
  124. * A buffer object shrink method that tries to swap out the first
  125. * buffer object on the global::swap_lru list.
  126. */
  127. int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
  128. {
  129. struct ttm_global *glob = &ttm_glob;
  130. struct ttm_device *bdev;
  131. int ret = 0;
  132. mutex_lock(&ttm_global_mutex);
  133. list_for_each_entry(bdev, &glob->device_list, device_list) {
  134. ret = ttm_device_swapout(bdev, ctx, gfp_flags);
  135. if (ret > 0) {
  136. list_move_tail(&bdev->device_list, &glob->device_list);
  137. break;
  138. }
  139. }
  140. mutex_unlock(&ttm_global_mutex);
  141. return ret;
  142. }
  143. int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  144. gfp_t gfp_flags)
  145. {
  146. struct ttm_resource_manager *man;
  147. unsigned i;
  148. s64 lret;
  149. for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
  150. man = ttm_manager_type(bdev, i);
  151. if (!man || !man->use_tt)
  152. continue;
  153. lret = ttm_bo_swapout(bdev, ctx, man, gfp_flags, 1);
  154. /* Can be both positive (num_pages) and negative (error) */
  155. if (lret)
  156. return lret;
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(ttm_device_swapout);
  161. /**
  162. * ttm_device_init
  163. *
  164. * @bdev: A pointer to a struct ttm_device to initialize.
  165. * @funcs: Function table for the device.
  166. * @dev: The core kernel device pointer for DMA mappings and allocations.
  167. * @mapping: The address space to use for this bo.
  168. * @vma_manager: A pointer to a vma manager.
  169. * @alloc_flags: TTM_ALLOCATION_* flags.
  170. *
  171. * Initializes a struct ttm_device:
  172. * Returns:
  173. * !0: Failure.
  174. */
  175. int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
  176. struct device *dev, struct address_space *mapping,
  177. struct drm_vma_offset_manager *vma_manager,
  178. unsigned int alloc_flags)
  179. {
  180. struct ttm_global *glob = &ttm_glob;
  181. int ret, nid;
  182. if (WARN_ON(vma_manager == NULL))
  183. return -EINVAL;
  184. ret = ttm_global_init();
  185. if (ret)
  186. return ret;
  187. bdev->wq = alloc_workqueue("ttm",
  188. WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16);
  189. if (!bdev->wq) {
  190. ttm_global_release();
  191. return -ENOMEM;
  192. }
  193. bdev->alloc_flags = alloc_flags;
  194. bdev->funcs = funcs;
  195. ttm_sys_man_init(bdev);
  196. if (dev)
  197. nid = dev_to_node(dev);
  198. else
  199. nid = NUMA_NO_NODE;
  200. ttm_pool_init(&bdev->pool, dev, nid, alloc_flags);
  201. bdev->vma_manager = vma_manager;
  202. spin_lock_init(&bdev->lru_lock);
  203. INIT_LIST_HEAD(&bdev->unevictable);
  204. bdev->dev_mapping = mapping;
  205. mutex_lock(&ttm_global_mutex);
  206. list_add_tail(&bdev->device_list, &glob->device_list);
  207. mutex_unlock(&ttm_global_mutex);
  208. return 0;
  209. }
  210. EXPORT_SYMBOL(ttm_device_init);
  211. void ttm_device_fini(struct ttm_device *bdev)
  212. {
  213. struct ttm_resource_manager *man;
  214. unsigned i;
  215. mutex_lock(&ttm_global_mutex);
  216. list_del(&bdev->device_list);
  217. mutex_unlock(&ttm_global_mutex);
  218. drain_workqueue(bdev->wq);
  219. destroy_workqueue(bdev->wq);
  220. man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
  221. ttm_resource_manager_set_used(man, false);
  222. ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
  223. spin_lock(&bdev->lru_lock);
  224. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  225. if (list_empty(&man->lru[0]))
  226. pr_debug("Swap list %d was clean\n", i);
  227. spin_unlock(&bdev->lru_lock);
  228. ttm_pool_fini(&bdev->pool);
  229. ttm_global_release();
  230. }
  231. EXPORT_SYMBOL(ttm_device_fini);
  232. static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
  233. struct list_head *list)
  234. {
  235. struct ttm_resource *res;
  236. spin_lock(&bdev->lru_lock);
  237. while ((res = ttm_lru_first_res_or_null(list))) {
  238. struct ttm_buffer_object *bo = res->bo;
  239. /* Take ref against racing releases once lru_lock is unlocked */
  240. if (!ttm_bo_get_unless_zero(bo))
  241. continue;
  242. list_del_init(&bo->resource->lru.link);
  243. spin_unlock(&bdev->lru_lock);
  244. if (bo->ttm)
  245. ttm_tt_unpopulate(bo->bdev, bo->ttm);
  246. ttm_bo_put(bo);
  247. spin_lock(&bdev->lru_lock);
  248. }
  249. spin_unlock(&bdev->lru_lock);
  250. }
  251. void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
  252. {
  253. struct ttm_resource_manager *man;
  254. unsigned int i, j;
  255. ttm_device_clear_lru_dma_mappings(bdev, &bdev->unevictable);
  256. for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
  257. man = ttm_manager_type(bdev, i);
  258. if (!man || !man->use_tt)
  259. continue;
  260. for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
  261. ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
  262. }
  263. }
  264. EXPORT_SYMBOL(ttm_device_clear_dma_mappings);