ttm_tt.c 8.3 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
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 30 31
/**************************************************************************
 *
 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS 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: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
 */

J
Joe Perches 已提交
32 33
#define pr_fmt(fmt) "[TTM] " fmt

34 35
#include <linux/sched.h>
#include <linux/pagemap.h>
36
#include <linux/shmem_fs.h>
37
#include <linux/file.h>
38 39
#include <drm/drm_cache.h>
#include <drm/ttm/ttm_bo_driver.h>
40

41
/*
42 43 44 45 46 47 48
 * Allocates a ttm structure for the given BO.
 */
int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
{
	struct ttm_bo_device *bdev = bo->bdev;
	uint32_t page_flags = 0;

49
	dma_resv_assert_held(bo->base.resv);
50

51 52 53
	if (bo->ttm)
		return 0;

54 55 56 57
	switch (bo->type) {
	case ttm_bo_type_device:
		if (zero_alloc)
			page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
58
		break;
59 60 61
	case ttm_bo_type_kernel:
		break;
	case ttm_bo_type_sg:
62
		page_flags |= TTM_PAGE_FLAG_SG;
63 64 65
		break;
	default:
		pr_err("Illegal buffer object type\n");
66
		return -EINVAL;
67 68
	}

69
	bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
70 71 72 73
	if (unlikely(bo->ttm == NULL))
		return -ENOMEM;

	return 0;
74 75
}

76
/*
77 78
 * Allocates storage for pointers to the pages that back the ttm.
 */
79
static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
80
{
M
Michal Hocko 已提交
81 82
	ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
			GFP_KERNEL | __GFP_ZERO);
83 84 85
	if (!ttm->pages)
		return -ENOMEM;
	return 0;
86 87
}

88
static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
89
{
90 91 92 93 94
	ttm->pages = kvmalloc_array(ttm->num_pages,
				    sizeof(*ttm->pages) +
				    sizeof(*ttm->dma_address),
				    GFP_KERNEL | __GFP_ZERO);
	if (!ttm->pages)
95
		return -ENOMEM;
96 97

	ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
98
	return 0;
99 100
}

101
static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
C
Christian König 已提交
102
{
103
	ttm->dma_address = kvmalloc_array(ttm->num_pages,
C
Christian König 已提交
104 105 106 107 108 109 110
					  sizeof(*ttm->dma_address),
					  GFP_KERNEL | __GFP_ZERO);
	if (!ttm->dma_address)
		return -ENOMEM;
	return 0;
}

D
Dave Airlie 已提交
111
void ttm_tt_destroy_common(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
112
{
113
	ttm_tt_unpopulate(bdev, ttm);
114

115
	if (ttm->swap_storage)
116 117
		fput(ttm->swap_storage);

118
	ttm->swap_storage = NULL;
D
Dave Airlie 已提交
119 120 121 122 123
}
EXPORT_SYMBOL(ttm_tt_destroy_common);

void ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
{
124
	bdev->driver->ttm_tt_destroy(bdev, ttm);
125 126
}

127 128
static void ttm_tt_init_fields(struct ttm_tt *ttm,
			       struct ttm_buffer_object *bo,
129 130
			       uint32_t page_flags,
			       enum ttm_caching caching)
131
{
132
	ttm->num_pages = bo->num_pages;
133
	ttm->caching = ttm_cached;
134
	ttm->page_flags = page_flags;
135
	ttm->dma_address = NULL;
J
Jerome Glisse 已提交
136
	ttm->swap_storage = NULL;
137
	ttm->sg = bo->sg;
138
	ttm->caching = caching;
C
Christian König 已提交
139 140
}

141
int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
142
		uint32_t page_flags, enum ttm_caching caching)
C
Christian König 已提交
143
{
144
	ttm_tt_init_fields(ttm, bo, page_flags, caching);
145

146
	if (ttm_tt_alloc_page_directory(ttm)) {
J
Joe Perches 已提交
147
		pr_err("Failed allocating page table\n");
148
		return -ENOMEM;
149
	}
150
	return 0;
151
}
152
EXPORT_SYMBOL(ttm_tt_init);
153

154 155
void ttm_tt_fini(struct ttm_tt *ttm)
{
156 157 158 159
	if (ttm->pages)
		kvfree(ttm->pages);
	else
		kvfree(ttm->dma_address);
160
	ttm->pages = NULL;
161
	ttm->dma_address = NULL;
162 163 164
}
EXPORT_SYMBOL(ttm_tt_fini);

165
int ttm_dma_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
166
		    uint32_t page_flags, enum ttm_caching caching)
167
{
168
	ttm_tt_init_fields(ttm, bo, page_flags, caching);
169

170
	if (ttm_dma_tt_alloc_page_directory(ttm)) {
J
Joe Perches 已提交
171
		pr_err("Failed allocating page table\n");
172 173 174 175 176 177
		return -ENOMEM;
	}
	return 0;
}
EXPORT_SYMBOL(ttm_dma_tt_init);

178
int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
179
		   uint32_t page_flags, enum ttm_caching caching)
C
Christian König 已提交
180 181 182
{
	int ret;

183
	ttm_tt_init_fields(ttm, bo, page_flags, caching);
C
Christian König 已提交
184 185

	if (page_flags & TTM_PAGE_FLAG_SG)
186
		ret = ttm_sg_tt_alloc_page_directory(ttm);
C
Christian König 已提交
187
	else
188
		ret = ttm_dma_tt_alloc_page_directory(ttm);
C
Christian König 已提交
189 190 191 192 193 194 195 196
	if (ret) {
		pr_err("Failed allocating page table\n");
		return -ENOMEM;
	}
	return 0;
}
EXPORT_SYMBOL(ttm_sg_tt_init);

197
int ttm_tt_swapin(struct ttm_tt *ttm)
198 199 200 201 202
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
203 204
	gfp_t gfp_mask;
	int i, ret;
205 206 207 208

	swap_storage = ttm->swap_storage;
	BUG_ON(swap_storage == NULL);

209
	swap_space = swap_storage->f_mapping;
210
	gfp_mask = mapping_gfp_mask(swap_space);
211 212

	for (i = 0; i < ttm->num_pages; ++i) {
213 214
		from_page = shmem_read_mapping_page_gfp(swap_space, i,
							gfp_mask);
215 216
		if (IS_ERR(from_page)) {
			ret = PTR_ERR(from_page);
217
			goto out_err;
218
		}
219
		to_page = ttm->pages[i];
220 221
		if (unlikely(to_page == NULL)) {
			ret = -ENOMEM;
222
			goto out_err;
223
		}
224

A
Akinobu Mita 已提交
225
		copy_highpage(to_page, from_page);
226
		put_page(from_page);
227 228
	}

229
	fput(swap_storage);
230 231 232 233
	ttm->swap_storage = NULL;
	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;

	return 0;
234

235
out_err:
236
	return ret;
237 238
}

239
int ttm_tt_swapout(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
240 241 242 243 244
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
245 246 247 248 249 250 251 252 253
	gfp_t gfp_mask;
	int i, ret;

	swap_storage = shmem_file_setup("ttm swap",
					ttm->num_pages << PAGE_SHIFT,
					0);
	if (IS_ERR(swap_storage)) {
		pr_err("Failed allocating swap storage\n");
		return PTR_ERR(swap_storage);
254
	}
255

256
	swap_space = swap_storage->f_mapping;
257
	gfp_mask = mapping_gfp_mask(swap_space);
258 259 260 261 262

	for (i = 0; i < ttm->num_pages; ++i) {
		from_page = ttm->pages[i];
		if (unlikely(from_page == NULL))
			continue;
263 264

		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
265
		if (IS_ERR(to_page)) {
266
			ret = PTR_ERR(to_page);
267
			goto out_err;
268
		}
A
Akinobu Mita 已提交
269
		copy_highpage(to_page, from_page);
270 271
		set_page_dirty(to_page);
		mark_page_accessed(to_page);
272
		put_page(to_page);
273 274
	}

D
Dave Airlie 已提交
275
	ttm_tt_unpopulate(bdev, ttm);
276 277 278 279
	ttm->swap_storage = swap_storage;
	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;

	return 0;
280

281
out_err:
282
	fput(swap_storage);
283

284
	return ret;
285
}
286

D
Dave Airlie 已提交
287
static void ttm_tt_add_mapping(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
288 289 290 291 292 293 294
{
	pgoff_t i;

	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
		return;

	for (i = 0; i < ttm->num_pages; ++i)
D
Dave Airlie 已提交
295
		ttm->pages[i]->mapping = bdev->dev_mapping;
296 297
}

D
Dave Airlie 已提交
298 299
int ttm_tt_populate(struct ttm_bo_device *bdev,
		    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
300
{
301 302
	int ret;

303 304 305
	if (!ttm)
		return -EINVAL;

306
	if (ttm_tt_is_populated(ttm))
307 308
		return 0;

D
Dave Airlie 已提交
309 310
	if (bdev->driver->ttm_tt_populate)
		ret = bdev->driver->ttm_tt_populate(bdev, ttm, ctx);
311
	else
312
		ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
313 314 315 316 317
	if (ret)
		return ret;

	ttm_tt_add_mapping(bdev, ttm);
	ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
318 319 320 321 322 323 324 325
	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
		ret = ttm_tt_swapin(ttm);
		if (unlikely(ret != 0)) {
			ttm_tt_unpopulate(bdev, ttm);
			return ret;
		}
	}

326
	return 0;
327
}
328
EXPORT_SYMBOL(ttm_tt_populate);
329

330 331 332 333 334
static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
{
	pgoff_t i;
	struct page **page = ttm->pages;

335 336 337
	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
		return;

338 339 340 341 342 343
	for (i = 0; i < ttm->num_pages; ++i) {
		(*page)->mapping = NULL;
		(*page++)->index = 0;
	}
}

D
Dave Airlie 已提交
344 345
void ttm_tt_unpopulate(struct ttm_bo_device *bdev,
		       struct ttm_tt *ttm)
346
{
347
	if (!ttm_tt_is_populated(ttm))
348 349 350
		return;

	ttm_tt_clear_mapping(ttm);
D
Dave Airlie 已提交
351 352
	if (bdev->driver->ttm_tt_unpopulate)
		bdev->driver->ttm_tt_unpopulate(bdev, ttm);
353
	else
354
		ttm_pool_free(&bdev->pool, ttm);
355
	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
356
}