Linux Audio

Check our new training course

Loading...
v6.8
  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 */
 42static DEFINE_MUTEX(ttm_global_mutex);
 43static unsigned ttm_glob_use_count;
 44struct ttm_global ttm_glob;
 45EXPORT_SYMBOL(ttm_glob);
 46
 47struct dentry *ttm_debugfs_root;
 48
 49static 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));
 62out:
 63	mutex_unlock(&ttm_global_mutex);
 64}
 65
 66static 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					   __GFP_NOWARN);
100
101	/* Retry without GFP_DMA32 for platforms DMA32 is not available */
102	if (unlikely(glob->dummy_read_page == NULL)) {
103		glob->dummy_read_page = alloc_page(__GFP_ZERO);
104		if (unlikely(glob->dummy_read_page == NULL)) {
105			ret = -ENOMEM;
106			goto out;
107		}
108		pr_warn("Using GFP_DMA32 fallback for dummy_read_page\n");
109	}
110
111	INIT_LIST_HEAD(&glob->device_list);
112	atomic_set(&glob->bo_count, 0);
113
114	debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
115				&glob->bo_count);
116out:
117	if (ret && ttm_debugfs_root)
118		debugfs_remove(ttm_debugfs_root);
119	if (ret)
120		--ttm_glob_use_count;
121	mutex_unlock(&ttm_global_mutex);
122	return ret;
123}
124
125/*
126 * A buffer object shrink method that tries to swap out the first
127 * buffer object on the global::swap_lru list.
128 */
129int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
130{
131	struct ttm_global *glob = &ttm_glob;
132	struct ttm_device *bdev;
133	int ret = 0;
134
135	mutex_lock(&ttm_global_mutex);
136	list_for_each_entry(bdev, &glob->device_list, device_list) {
137		ret = ttm_device_swapout(bdev, ctx, gfp_flags);
138		if (ret > 0) {
139			list_move_tail(&bdev->device_list, &glob->device_list);
140			break;
141		}
142	}
143	mutex_unlock(&ttm_global_mutex);
144	return ret;
145}
146
147int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
148		       gfp_t gfp_flags)
149{
150	struct ttm_resource_cursor cursor;
151	struct ttm_resource_manager *man;
152	struct ttm_resource *res;
153	unsigned i;
154	int ret;
155
156	spin_lock(&bdev->lru_lock);
157	for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
158		man = ttm_manager_type(bdev, i);
159		if (!man || !man->use_tt)
160			continue;
161
162		ttm_resource_manager_for_each_res(man, &cursor, res) {
163			struct ttm_buffer_object *bo = res->bo;
164			uint32_t num_pages;
165
166			if (!bo || bo->resource != res)
167				continue;
168
169			num_pages = PFN_UP(bo->base.size);
170			ret = ttm_bo_swapout(bo, ctx, gfp_flags);
171			/* ttm_bo_swapout has dropped the lru_lock */
172			if (!ret)
173				return num_pages;
174			if (ret != -EBUSY)
175				return ret;
176		}
177	}
178	spin_unlock(&bdev->lru_lock);
179	return 0;
180}
181EXPORT_SYMBOL(ttm_device_swapout);
182
183/**
184 * ttm_device_init
185 *
186 * @bdev: A pointer to a struct ttm_device to initialize.
187 * @funcs: Function table for the device.
188 * @dev: The core kernel device pointer for DMA mappings and allocations.
189 * @mapping: The address space to use for this bo.
190 * @vma_manager: A pointer to a vma manager.
191 * @use_dma_alloc: If coherent DMA allocation API should be used.
192 * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
193 *
194 * Initializes a struct ttm_device:
195 * Returns:
196 * !0: Failure.
197 */
198int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
199		    struct device *dev, struct address_space *mapping,
200		    struct drm_vma_offset_manager *vma_manager,
201		    bool use_dma_alloc, bool use_dma32)
202{
203	struct ttm_global *glob = &ttm_glob;
204	int ret, nid;
205
206	if (WARN_ON(vma_manager == NULL))
207		return -EINVAL;
208
209	ret = ttm_global_init();
210	if (ret)
211		return ret;
212
213	bdev->wq = alloc_workqueue("ttm",
214				   WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16);
215	if (!bdev->wq) {
216		ttm_global_release();
217		return -ENOMEM;
218	}
219
220	bdev->funcs = funcs;
221
222	ttm_sys_man_init(bdev);
223
224	if (dev)
225		nid = dev_to_node(dev);
226	else
227		nid = NUMA_NO_NODE;
228
229	ttm_pool_init(&bdev->pool, dev, nid, use_dma_alloc, use_dma32);
230
231	bdev->vma_manager = vma_manager;
232	spin_lock_init(&bdev->lru_lock);
233	INIT_LIST_HEAD(&bdev->pinned);
234	bdev->dev_mapping = mapping;
235	mutex_lock(&ttm_global_mutex);
236	list_add_tail(&bdev->device_list, &glob->device_list);
237	mutex_unlock(&ttm_global_mutex);
238
239	return 0;
240}
241EXPORT_SYMBOL(ttm_device_init);
242
243void ttm_device_fini(struct ttm_device *bdev)
244{
245	struct ttm_resource_manager *man;
246	unsigned i;
247
248	mutex_lock(&ttm_global_mutex);
249	list_del(&bdev->device_list);
250	mutex_unlock(&ttm_global_mutex);
251
252	drain_workqueue(bdev->wq);
253	destroy_workqueue(bdev->wq);
254
255	man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
256	ttm_resource_manager_set_used(man, false);
257	ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
258
259	spin_lock(&bdev->lru_lock);
260	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
261		if (list_empty(&man->lru[0]))
262			pr_debug("Swap list %d was clean\n", i);
263	spin_unlock(&bdev->lru_lock);
264
265	ttm_pool_fini(&bdev->pool);
266	ttm_global_release();
267}
268EXPORT_SYMBOL(ttm_device_fini);
269
270static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
271					      struct list_head *list)
272{
273	struct ttm_resource *res;
274
275	spin_lock(&bdev->lru_lock);
276	while ((res = list_first_entry_or_null(list, typeof(*res), lru))) {
277		struct ttm_buffer_object *bo = res->bo;
278
279		/* Take ref against racing releases once lru_lock is unlocked */
280		if (!ttm_bo_get_unless_zero(bo))
281			continue;
282
283		list_del_init(&res->lru);
284		spin_unlock(&bdev->lru_lock);
285
286		if (bo->ttm)
287			ttm_tt_unpopulate(bo->bdev, bo->ttm);
288
289		ttm_bo_put(bo);
290		spin_lock(&bdev->lru_lock);
291	}
292	spin_unlock(&bdev->lru_lock);
293}
294
295void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
296{
297	struct ttm_resource_manager *man;
298	unsigned int i, j;
299
300	ttm_device_clear_lru_dma_mappings(bdev, &bdev->pinned);
301
302	for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
303		man = ttm_manager_type(bdev, i);
304		if (!man || !man->use_tt)
305			continue;
306
307		for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
308			ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
309	}
310}
311EXPORT_SYMBOL(ttm_device_clear_dma_mappings);
v6.13.7
  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 */
 43static DEFINE_MUTEX(ttm_global_mutex);
 44static unsigned ttm_glob_use_count;
 45struct ttm_global ttm_glob;
 46EXPORT_SYMBOL(ttm_glob);
 47
 48struct dentry *ttm_debugfs_root;
 49
 50static 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));
 63out:
 64	mutex_unlock(&ttm_global_mutex);
 65}
 66
 67static 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);
117out:
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 */
130int 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
148int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
149		       gfp_t gfp_flags)
150{
 
151	struct ttm_resource_manager *man;
 
152	unsigned i;
153	s64 lret;
154
 
155	for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
156		man = ttm_manager_type(bdev, i);
157		if (!man || !man->use_tt)
158			continue;
159
160		lret = ttm_bo_swapout(bdev, ctx, man, gfp_flags, 1);
161		/* Can be both positive (num_pages) and negative (error) */
162		if (lret)
163			return lret;
 
 
 
 
 
 
 
 
 
 
 
164	}
 
165	return 0;
166}
167EXPORT_SYMBOL(ttm_device_swapout);
168
169/**
170 * ttm_device_init
171 *
172 * @bdev: A pointer to a struct ttm_device to initialize.
173 * @funcs: Function table for the device.
174 * @dev: The core kernel device pointer for DMA mappings and allocations.
175 * @mapping: The address space to use for this bo.
176 * @vma_manager: A pointer to a vma manager.
177 * @use_dma_alloc: If coherent DMA allocation API should be used.
178 * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
179 *
180 * Initializes a struct ttm_device:
181 * Returns:
182 * !0: Failure.
183 */
184int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
185		    struct device *dev, struct address_space *mapping,
186		    struct drm_vma_offset_manager *vma_manager,
187		    bool use_dma_alloc, bool use_dma32)
188{
189	struct ttm_global *glob = &ttm_glob;
190	int ret, nid;
191
192	if (WARN_ON(vma_manager == NULL))
193		return -EINVAL;
194
195	ret = ttm_global_init();
196	if (ret)
197		return ret;
198
199	bdev->wq = alloc_workqueue("ttm",
200				   WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16);
201	if (!bdev->wq) {
202		ttm_global_release();
203		return -ENOMEM;
204	}
205
206	bdev->funcs = funcs;
207
208	ttm_sys_man_init(bdev);
209
210	if (dev)
211		nid = dev_to_node(dev);
212	else
213		nid = NUMA_NO_NODE;
214
215	ttm_pool_init(&bdev->pool, dev, nid, use_dma_alloc, use_dma32);
216
217	bdev->vma_manager = vma_manager;
218	spin_lock_init(&bdev->lru_lock);
219	INIT_LIST_HEAD(&bdev->unevictable);
220	bdev->dev_mapping = mapping;
221	mutex_lock(&ttm_global_mutex);
222	list_add_tail(&bdev->device_list, &glob->device_list);
223	mutex_unlock(&ttm_global_mutex);
224
225	return 0;
226}
227EXPORT_SYMBOL(ttm_device_init);
228
229void ttm_device_fini(struct ttm_device *bdev)
230{
231	struct ttm_resource_manager *man;
232	unsigned i;
233
234	mutex_lock(&ttm_global_mutex);
235	list_del(&bdev->device_list);
236	mutex_unlock(&ttm_global_mutex);
237
238	drain_workqueue(bdev->wq);
239	destroy_workqueue(bdev->wq);
240
241	man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
242	ttm_resource_manager_set_used(man, false);
243	ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
244
245	spin_lock(&bdev->lru_lock);
246	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
247		if (list_empty(&man->lru[0]))
248			pr_debug("Swap list %d was clean\n", i);
249	spin_unlock(&bdev->lru_lock);
250
251	ttm_pool_fini(&bdev->pool);
252	ttm_global_release();
253}
254EXPORT_SYMBOL(ttm_device_fini);
255
256static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
257					      struct list_head *list)
258{
259	struct ttm_resource *res;
260
261	spin_lock(&bdev->lru_lock);
262	while ((res = ttm_lru_first_res_or_null(list))) {
263		struct ttm_buffer_object *bo = res->bo;
264
265		/* Take ref against racing releases once lru_lock is unlocked */
266		if (!ttm_bo_get_unless_zero(bo))
267			continue;
268
269		list_del_init(&bo->resource->lru.link);
270		spin_unlock(&bdev->lru_lock);
271
272		if (bo->ttm)
273			ttm_tt_unpopulate(bo->bdev, bo->ttm);
274
275		ttm_bo_put(bo);
276		spin_lock(&bdev->lru_lock);
277	}
278	spin_unlock(&bdev->lru_lock);
279}
280
281void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
282{
283	struct ttm_resource_manager *man;
284	unsigned int i, j;
285
286	ttm_device_clear_lru_dma_mappings(bdev, &bdev->unevictable);
287
288	for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
289		man = ttm_manager_type(bdev, i);
290		if (!man || !man->use_tt)
291			continue;
292
293		for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
294			ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
295	}
296}
297EXPORT_SYMBOL(ttm_device_clear_dma_mappings);