ttm_device.c 7.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* SPDX-License-Identifier: GPL-2.0 OR MIT */

/*
 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
 * Copyright 2020 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Christian König
 */

#define pr_fmt(fmt) "[TTM DEVICE] " fmt

30 31
#include <linux/mm.h>

32
#include <drm/ttm/ttm_bo.h>
33
#include <drm/ttm/ttm_device.h>
34
#include <drm/ttm/ttm_tt.h>
35 36 37 38
#include <drm/ttm/ttm_placement.h>

#include "ttm_module.h"

39
/*
40 41
 * ttm_global_mutex - protecting the global state
 */
42 43
static DEFINE_MUTEX(ttm_global_mutex);
static unsigned ttm_glob_use_count;
44 45 46
struct ttm_global ttm_glob;
EXPORT_SYMBOL(ttm_glob);

47 48
struct dentry *ttm_debugfs_root;

49 50 51 52 53 54 55 56
static void ttm_global_release(void)
{
	struct ttm_global *glob = &ttm_glob;

	mutex_lock(&ttm_global_mutex);
	if (--ttm_glob_use_count > 0)
		goto out;

57
	ttm_pool_mgr_fini();
58
	debugfs_remove(ttm_debugfs_root);
59

60 61 62 63 64 65 66 67 68
	__free_page(glob->dummy_read_page);
	memset(glob, 0, sizeof(*glob));
out:
	mutex_unlock(&ttm_global_mutex);
}

static int ttm_global_init(void)
{
	struct ttm_global *glob = &ttm_glob;
69
	unsigned long num_pages, num_dma32;
70
	struct sysinfo si;
71 72 73 74 75 76
	int ret = 0;

	mutex_lock(&ttm_global_mutex);
	if (++ttm_glob_use_count > 1)
		goto out;

77 78
	si_meminfo(&si);

79 80 81 82 83
	ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
	if (IS_ERR(ttm_debugfs_root)) {
		ttm_debugfs_root = NULL;
	}

84 85 86 87
	/* Limit the number of pages in the pool to about 50% of the total
	 * system memory.
	 */
	num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
88 89 90 91 92 93 94 95 96
	num_pages /= 2;

	/* But for DMA32 we limit ourself to only use 2GiB maximum. */
	num_dma32 = (u64)(si.totalram - si.totalhigh) * si.mem_unit
		>> PAGE_SHIFT;
	num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT));

	ttm_pool_mgr_init(num_pages);
	ttm_tt_mgr_init(num_pages, num_dma32);
97 98 99 100 101 102 103 104 105 106 107 108 109 110

	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);

	if (unlikely(glob->dummy_read_page == NULL)) {
		ret = -ENOMEM;
		goto out;
	}

	INIT_LIST_HEAD(&glob->device_list);
	atomic_set(&glob->bo_count, 0);

	debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
				&glob->bo_count);
out:
111 112
	if (ret && ttm_debugfs_root)
		debugfs_remove(ttm_debugfs_root);
113 114
	if (ret)
		--ttm_glob_use_count;
115 116 117 118
	mutex_unlock(&ttm_global_mutex);
	return ret;
}

119
/*
120 121 122 123 124 125
 * A buffer object shrink method that tries to swap out the first
 * buffer object on the global::swap_lru list.
 */
int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
{
	struct ttm_global *glob = &ttm_glob;
126
	struct ttm_device *bdev;
127
	int ret = 0;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	mutex_lock(&ttm_global_mutex);
	list_for_each_entry(bdev, &glob->device_list, device_list) {
		ret = ttm_device_swapout(bdev, ctx, gfp_flags);
		if (ret > 0) {
			list_move_tail(&bdev->device_list, &glob->device_list);
			break;
		}
	}
	mutex_unlock(&ttm_global_mutex);
	return ret;
}

int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
		       gfp_t gfp_flags)
{
144
	struct ttm_resource_cursor cursor;
145
	struct ttm_resource_manager *man;
146
	struct ttm_resource *res;
147
	unsigned i;
148 149
	int ret;

150
	spin_lock(&bdev->lru_lock);
151 152 153 154 155
	for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
		man = ttm_manager_type(bdev, i);
		if (!man || !man->use_tt)
			continue;

156 157
		ttm_resource_manager_for_each_res(man, &cursor, res) {
			struct ttm_buffer_object *bo = res->bo;
158
			uint32_t num_pages;
159

160
			if (!bo || bo->resource != res)
161 162 163
				continue;

			num_pages = PFN_UP(bo->base.size);
164 165 166 167 168 169
			ret = ttm_bo_swapout(bo, ctx, gfp_flags);
			/* ttm_bo_swapout has dropped the lru_lock */
			if (!ret)
				return num_pages;
			if (ret != -EBUSY)
				return ret;
170 171
		}
	}
172
	spin_unlock(&bdev->lru_lock);
173 174
	return 0;
}
175
EXPORT_SYMBOL(ttm_device_swapout);
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 * ttm_device_init
 *
 * @bdev: A pointer to a struct ttm_device to initialize.
 * @funcs: Function table for the device.
 * @dev: The core kernel device pointer for DMA mappings and allocations.
 * @mapping: The address space to use for this bo.
 * @vma_manager: A pointer to a vma manager.
 * @use_dma_alloc: If coherent DMA allocation API should be used.
 * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
 *
 * Initializes a struct ttm_device:
 * Returns:
 * !0: Failure.
 */
192
int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
193 194 195 196 197 198 199 200 201 202 203 204 205 206
		    struct device *dev, struct address_space *mapping,
		    struct drm_vma_offset_manager *vma_manager,
		    bool use_dma_alloc, bool use_dma32)
{
	struct ttm_global *glob = &ttm_glob;
	int ret;

	if (WARN_ON(vma_manager == NULL))
		return -EINVAL;

	ret = ttm_global_init();
	if (ret)
		return ret;

207 208 209 210 211 212
	bdev->wq = alloc_workqueue("ttm", WQ_MEM_RECLAIM | WQ_HIGHPRI, 16);
	if (!bdev->wq) {
		ttm_global_release();
		return -ENOMEM;
	}

213 214
	bdev->funcs = funcs;

215
	ttm_sys_man_init(bdev);
216 217 218
	ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);

	bdev->vma_manager = vma_manager;
219
	spin_lock_init(&bdev->lru_lock);
220
	INIT_LIST_HEAD(&bdev->pinned);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	bdev->dev_mapping = mapping;
	mutex_lock(&ttm_global_mutex);
	list_add_tail(&bdev->device_list, &glob->device_list);
	mutex_unlock(&ttm_global_mutex);

	return 0;
}
EXPORT_SYMBOL(ttm_device_init);

void ttm_device_fini(struct ttm_device *bdev)
{
	struct ttm_resource_manager *man;
	unsigned i;

	man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
	ttm_resource_manager_set_used(man, false);
	ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);

	mutex_lock(&ttm_global_mutex);
	list_del(&bdev->device_list);
	mutex_unlock(&ttm_global_mutex);

243 244
	drain_workqueue(bdev->wq);
	destroy_workqueue(bdev->wq);
245

246
	spin_lock(&bdev->lru_lock);
247 248 249
	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
		if (list_empty(&man->lru[0]))
			pr_debug("Swap list %d was clean\n", i);
250
	spin_unlock(&bdev->lru_lock);
251 252 253 254 255

	ttm_pool_fini(&bdev->pool);
	ttm_global_release();
}
EXPORT_SYMBOL(ttm_device_fini);
256

257 258
static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
					      struct list_head *list)
259
{
260
	struct ttm_resource *res;
261 262

	spin_lock(&bdev->lru_lock);
263 264 265
	while ((res = list_first_entry_or_null(list, typeof(*res), lru))) {
		struct ttm_buffer_object *bo = res->bo;

266
		/* Take ref against racing releases once lru_lock is unlocked */
267 268
		if (!ttm_bo_get_unless_zero(bo))
			continue;
269

270 271
		list_del_init(&res->lru);
		spin_unlock(&bdev->lru_lock);
272

273 274 275 276 277
		if (bo->ttm)
			ttm_tt_unpopulate(bo->bdev, bo->ttm);

		ttm_bo_put(bo);
		spin_lock(&bdev->lru_lock);
278
	}
279 280 281 282 283 284 285 286 287
	spin_unlock(&bdev->lru_lock);
}

void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
{
	struct ttm_resource_manager *man;
	unsigned int i, j;

	ttm_device_clear_lru_dma_mappings(bdev, &bdev->pinned);
288 289 290 291 292 293

	for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
		man = ttm_manager_type(bdev, i);
		if (!man || !man->use_tt)
			continue;

294 295
		for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
			ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
296 297 298
	}
}
EXPORT_SYMBOL(ttm_device_clear_dma_mappings);