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