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