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
ttm_global_release(void)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
ttm_global_init(void)67 static int ttm_global_init(void)
68 {
69 struct ttm_global *glob = &ttm_glob;
70 unsigned long num_pages, num_dma32;
71 int ret = 0;
72
73 mutex_lock(&ttm_global_mutex);
74 if (++ttm_glob_use_count > 1)
75 goto out;
76
77 ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
78 if (IS_ERR(ttm_debugfs_root)) {
79 ttm_debugfs_root = NULL;
80 }
81
82 /* Limit the number of pages in the pool to about 50% of the total
83 * system memory.
84 */
85 num_pages = physmem;
86 num_pages /= 2;
87
88 /* But for DMA32 we limit ourself to only use 2GiB maximum. */
89 num_dma32 = physmem;
90 num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT));
91
92 ttm_pool_mgr_init(num_pages);
93 ttm_tt_mgr_init(num_pages, num_dma32);
94
95 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32 |
96 __GFP_NOWARN);
97
98 /* Retry without GFP_DMA32 for platforms DMA32 is not available */
99 if (unlikely(glob->dummy_read_page == NULL)) {
100 glob->dummy_read_page = alloc_page(__GFP_ZERO);
101 if (unlikely(glob->dummy_read_page == NULL)) {
102 ret = -ENOMEM;
103 goto out;
104 }
105 pr_warn("Using GFP_DMA32 fallback for dummy_read_page\n");
106 }
107
108 INIT_LIST_HEAD(&glob->device_list);
109 atomic_set(&glob->bo_count, 0);
110
111 debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
112 &glob->bo_count);
113 out:
114 if (ret && ttm_debugfs_root)
115 debugfs_remove(ttm_debugfs_root);
116 if (ret)
117 --ttm_glob_use_count;
118 mutex_unlock(&ttm_global_mutex);
119 return ret;
120 }
121
122 /*
123 * A buffer object shrink method that tries to swap out the first
124 * buffer object on the global::swap_lru list.
125 */
ttm_global_swapout(struct ttm_operation_ctx * ctx,gfp_t gfp_flags)126 int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
127 {
128 struct ttm_global *glob = &ttm_glob;
129 struct ttm_device *bdev;
130 int ret = 0;
131
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
ttm_device_swapout(struct ttm_device * bdev,struct ttm_operation_ctx * ctx,gfp_t gfp_flags)144 int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
145 gfp_t gfp_flags)
146 {
147 struct ttm_resource_manager *man;
148 unsigned i;
149 s64 lret;
150
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 lret = ttm_bo_swapout(bdev, ctx, man, gfp_flags, 1);
157 /* Can be both positive (num_pages) and negative (error) */
158 if (lret)
159 return lret;
160 }
161 return 0;
162 }
163 EXPORT_SYMBOL(ttm_device_swapout);
164
165 /**
166 * ttm_device_init
167 *
168 * @bdev: A pointer to a struct ttm_device to initialize.
169 * @funcs: Function table for the device.
170 * @dev: The core kernel device pointer for DMA mappings and allocations.
171 * @mapping: The address space to use for this bo.
172 * @vma_manager: A pointer to a vma manager.
173 * @use_dma_alloc: If coherent DMA allocation API should be used.
174 * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
175 *
176 * Initializes a struct ttm_device:
177 * Returns:
178 * !0: Failure.
179 */
ttm_device_init(struct ttm_device * bdev,const struct ttm_device_funcs * funcs,struct device * dev,struct address_space * mapping,struct drm_vma_offset_manager * vma_manager,bool use_dma_alloc,bool use_dma32)180 int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
181 struct device *dev, struct address_space *mapping,
182 struct drm_vma_offset_manager *vma_manager,
183 bool use_dma_alloc, bool use_dma32)
184 {
185 struct ttm_global *glob = &ttm_glob;
186 int ret, nid;
187
188 if (WARN_ON(vma_manager == NULL))
189 return -EINVAL;
190
191 ret = ttm_global_init();
192 if (ret)
193 return ret;
194
195 bdev->wq = alloc_workqueue("ttm",
196 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16);
197 if (!bdev->wq) {
198 ttm_global_release();
199 return -ENOMEM;
200 }
201
202 bdev->funcs = funcs;
203
204 ttm_sys_man_init(bdev);
205
206 if (dev)
207 nid = dev_to_node(dev);
208 else
209 nid = NUMA_NO_NODE;
210
211 ttm_pool_init(&bdev->pool, dev, nid, use_dma_alloc, use_dma32);
212
213 bdev->vma_manager = vma_manager;
214 mtx_init(&bdev->lru_lock, IPL_NONE);
215 INIT_LIST_HEAD(&bdev->pinned);
216 bdev->dev_mapping = mapping;
217 mutex_lock(&ttm_global_mutex);
218 list_add_tail(&bdev->device_list, &glob->device_list);
219 mutex_unlock(&ttm_global_mutex);
220
221 return 0;
222 }
223 EXPORT_SYMBOL(ttm_device_init);
224
ttm_device_fini(struct ttm_device * bdev)225 void ttm_device_fini(struct ttm_device *bdev)
226 {
227 struct ttm_resource_manager *man;
228 unsigned i;
229
230 mutex_lock(&ttm_global_mutex);
231 list_del(&bdev->device_list);
232 mutex_unlock(&ttm_global_mutex);
233
234 drain_workqueue(bdev->wq);
235 destroy_workqueue(bdev->wq);
236
237 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
238 ttm_resource_manager_set_used(man, false);
239 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
240
241 spin_lock(&bdev->lru_lock);
242 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
243 if (list_empty(&man->lru[0]))
244 pr_debug("Swap list %d was clean\n", i);
245 spin_unlock(&bdev->lru_lock);
246
247 ttm_pool_fini(&bdev->pool);
248 ttm_global_release();
249 }
250 EXPORT_SYMBOL(ttm_device_fini);
251
ttm_device_clear_lru_dma_mappings(struct ttm_device * bdev,struct list_head * list)252 static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
253 struct list_head *list)
254 {
255 struct ttm_resource *res;
256
257 spin_lock(&bdev->lru_lock);
258 while ((res = ttm_lru_first_res_or_null(list))) {
259 struct ttm_buffer_object *bo = res->bo;
260
261 /* Take ref against racing releases once lru_lock is unlocked */
262 if (!ttm_bo_get_unless_zero(bo))
263 continue;
264
265 list_del_init(&bo->resource->lru.link);
266 spin_unlock(&bdev->lru_lock);
267
268 if (bo->ttm)
269 ttm_tt_unpopulate(bo->bdev, bo->ttm);
270
271 ttm_bo_put(bo);
272 spin_lock(&bdev->lru_lock);
273 }
274 spin_unlock(&bdev->lru_lock);
275 }
276
ttm_device_clear_dma_mappings(struct ttm_device * bdev)277 void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
278 {
279 struct ttm_resource_manager *man;
280 unsigned int i, j;
281
282 ttm_device_clear_lru_dma_mappings(bdev, &bdev->pinned);
283
284 for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
285 man = ttm_manager_type(bdev, i);
286 if (!man || !man->use_tt)
287 continue;
288
289 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
290 ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
291 }
292 }
293 EXPORT_SYMBOL(ttm_device_clear_dma_mappings);
294