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