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