ttm_tt.c 9.3 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 30
/**************************************************************************
 *
 * 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 已提交
31 32
#define pr_fmt(fmt) "[TTM] " fmt

33 34 35
#include <linux/sched.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>
36
#include <linux/shmem_fs.h>
37 38
#include <linux/file.h>
#include <linux/swap.h>
39
#include <linux/slab.h>
40
#include <linux/export.h>
41 42 43 44 45 46
#include <drm/drm_cache.h>
#include <drm/drm_mem_util.h>
#include <drm/ttm/ttm_module.h>
#include <drm/ttm/ttm_bo_driver.h>
#include <drm/ttm/ttm_placement.h>
#include <drm/ttm/ttm_page_alloc.h>
47 48 49 50 51 52

/**
 * Allocates storage for pointers to the pages that back the ttm.
 */
static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
{
53
	ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
54 55
}

56
static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
57
{
58 59 60
	ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
	ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
					    sizeof(*ttm->dma_address));
61 62 63 64
}

#ifdef CONFIG_X86
static inline int ttm_tt_set_page_caching(struct page *p,
65 66
					  enum ttm_caching_state c_old,
					  enum ttm_caching_state c_new)
67
{
68 69
	int ret = 0;

70 71 72
	if (PageHighMem(p))
		return 0;

73
	if (c_old != tt_cached) {
74 75 76 77 78 79
		/* p isn't in the default caching state, set it to
		 * writeback first to free its current memtype. */

		ret = set_pages_wb(p, 1);
		if (ret)
			return ret;
80
	}
81

82
	if (c_new == tt_wc)
83
		ret = set_memory_wc((unsigned long) page_address(p), 1);
84
	else if (c_new == tt_uncached)
85 86 87
		ret = set_pages_uc(p, 1);

	return ret;
88 89 90
}
#else /* CONFIG_X86 */
static inline int ttm_tt_set_page_caching(struct page *p,
91 92
					  enum ttm_caching_state c_old,
					  enum ttm_caching_state c_new)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
	return 0;
}
#endif /* CONFIG_X86 */

/*
 * Change caching policy for the linear kernel map
 * for range of pages in a ttm.
 */

static int ttm_tt_set_caching(struct ttm_tt *ttm,
			      enum ttm_caching_state c_state)
{
	int i, j;
	struct page *cur_page;
	int ret;

	if (ttm->caching_state == c_state)
		return 0;

113 114 115 116
	if (ttm->state == tt_unpopulated) {
		/* Change caching but don't populate */
		ttm->caching_state = c_state;
		return 0;
117 118 119
	}

	if (ttm->caching_state == tt_cached)
120
		drm_clflush_pages(ttm->pages, ttm->num_pages);
121 122 123 124

	for (i = 0; i < ttm->num_pages; ++i) {
		cur_page = ttm->pages[i];
		if (likely(cur_page != NULL)) {
125 126 127
			ret = ttm_tt_set_page_caching(cur_page,
						      ttm->caching_state,
						      c_state);
128 129 130 131 132 133 134 135 136 137 138 139 140
			if (unlikely(ret != 0))
				goto out_err;
		}
	}

	ttm->caching_state = c_state;

	return 0;

out_err:
	for (j = 0; j < i; ++j) {
		cur_page = ttm->pages[j];
		if (likely(cur_page != NULL)) {
141
			(void)ttm_tt_set_page_caching(cur_page, c_state,
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
						      ttm->caching_state);
		}
	}

	return ret;
}

int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
{
	enum ttm_caching_state state;

	if (placement & TTM_PL_FLAG_WC)
		state = tt_wc;
	else if (placement & TTM_PL_FLAG_UNCACHED)
		state = tt_uncached;
	else
		state = tt_cached;

	return ttm_tt_set_caching(ttm, state);
}
162
EXPORT_SYMBOL(ttm_tt_set_placement_caching);
163 164 165 166 167 168

void ttm_tt_destroy(struct ttm_tt *ttm)
{
	if (unlikely(ttm == NULL))
		return;

169 170
	if (ttm->state == tt_bound) {
		ttm_tt_unbind(ttm);
171 172
	}

173 174
	if (ttm->state == tt_unbound)
		ttm_tt_unpopulate(ttm);
175

J
Jan Engelhardt 已提交
176
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
177 178 179
	    ttm->swap_storage)
		fput(ttm->swap_storage);

180 181
	ttm->swap_storage = NULL;
	ttm->func->destroy(ttm);
182 183
}

184 185 186
int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
		unsigned long size, uint32_t page_flags,
		struct page *dummy_read_page)
187
{
188
	ttm->bdev = bdev;
189
	ttm->glob = bdev->glob;
190 191 192 193
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
	ttm->caching_state = tt_cached;
	ttm->page_flags = page_flags;
	ttm->dummy_read_page = dummy_read_page;
194
	ttm->state = tt_unpopulated;
J
Jerome Glisse 已提交
195
	ttm->swap_storage = NULL;
196 197

	ttm_tt_alloc_page_directory(ttm);
198
	if (!ttm->pages) {
199
		ttm_tt_destroy(ttm);
J
Joe Perches 已提交
200
		pr_err("Failed allocating page table\n");
201
		return -ENOMEM;
202
	}
203
	return 0;
204
}
205
EXPORT_SYMBOL(ttm_tt_init);
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
void ttm_tt_fini(struct ttm_tt *ttm)
{
	drm_free_large(ttm->pages);
	ttm->pages = NULL;
}
EXPORT_SYMBOL(ttm_tt_fini);

int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
		unsigned long size, uint32_t page_flags,
		struct page *dummy_read_page)
{
	struct ttm_tt *ttm = &ttm_dma->ttm;

	ttm->bdev = bdev;
	ttm->glob = bdev->glob;
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
	ttm->caching_state = tt_cached;
	ttm->page_flags = page_flags;
	ttm->dummy_read_page = dummy_read_page;
	ttm->state = tt_unpopulated;
J
Jerome Glisse 已提交
227
	ttm->swap_storage = NULL;
228 229 230 231 232

	INIT_LIST_HEAD(&ttm_dma->pages_list);
	ttm_dma_tt_alloc_page_directory(ttm_dma);
	if (!ttm->pages || !ttm_dma->dma_address) {
		ttm_tt_destroy(ttm);
J
Joe Perches 已提交
233
		pr_err("Failed allocating page table\n");
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		return -ENOMEM;
	}
	return 0;
}
EXPORT_SYMBOL(ttm_dma_tt_init);

void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
{
	struct ttm_tt *ttm = &ttm_dma->ttm;

	drm_free_large(ttm->pages);
	ttm->pages = NULL;
	drm_free_large(ttm_dma->dma_address);
	ttm_dma->dma_address = NULL;
}
EXPORT_SYMBOL(ttm_dma_tt_fini);

251 252 253 254 255
void ttm_tt_unbind(struct ttm_tt *ttm)
{
	int ret;

	if (ttm->state == tt_bound) {
256
		ret = ttm->func->unbind(ttm);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		BUG_ON(ret);
		ttm->state = tt_unbound;
	}
}

int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
{
	int ret = 0;

	if (!ttm)
		return -EINVAL;

	if (ttm->state == tt_bound)
		return 0;

272
	ret = ttm->bdev->driver->ttm_tt_populate(ttm);
273 274 275
	if (ret)
		return ret;

276
	ret = ttm->func->bind(ttm, bo_mem);
277
	if (unlikely(ret != 0))
278 279 280 281 282 283 284 285
		return ret;

	ttm->state = tt_bound;

	return 0;
}
EXPORT_SYMBOL(ttm_tt_bind);

286
int ttm_tt_swapin(struct ttm_tt *ttm)
287 288 289 290 291 292
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
	int i;
293
	int ret = -ENOMEM;
294 295 296 297

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

A
Al Viro 已提交
298
	swap_space = file_inode(swap_storage)->i_mapping;
299 300

	for (i = 0; i < ttm->num_pages; ++i) {
301
		from_page = shmem_read_mapping_page(swap_space, i);
302 303
		if (IS_ERR(from_page)) {
			ret = PTR_ERR(from_page);
304
			goto out_err;
305
		}
306
		to_page = ttm->pages[i];
307 308 309
		if (unlikely(to_page == NULL))
			goto out_err;

A
Akinobu Mita 已提交
310
		copy_highpage(to_page, from_page);
311 312 313
		page_cache_release(from_page);
	}

J
Jan Engelhardt 已提交
314
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
315 316 317 318 319 320
		fput(swap_storage);
	ttm->swap_storage = NULL;
	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;

	return 0;
out_err:
321
	return ret;
322 323
}

J
Jan Engelhardt 已提交
324
int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
325 326 327 328 329 330
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
	int i;
331
	int ret = -ENOMEM;
332 333 334 335

	BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
	BUG_ON(ttm->caching_state != tt_cached);

J
Jan Engelhardt 已提交
336
	if (!persistent_swap_storage) {
337 338 339 340
		swap_storage = shmem_file_setup("ttm swap",
						ttm->num_pages << PAGE_SHIFT,
						0);
		if (unlikely(IS_ERR(swap_storage))) {
J
Joe Perches 已提交
341
			pr_err("Failed allocating swap storage\n");
342
			return PTR_ERR(swap_storage);
343 344
		}
	} else
J
Jan Engelhardt 已提交
345
		swap_storage = persistent_swap_storage;
346

A
Al Viro 已提交
347
	swap_space = file_inode(swap_storage)->i_mapping;
348 349 350 351 352

	for (i = 0; i < ttm->num_pages; ++i) {
		from_page = ttm->pages[i];
		if (unlikely(from_page == NULL))
			continue;
353
		to_page = shmem_read_mapping_page(swap_space, i);
354 355
		if (unlikely(IS_ERR(to_page))) {
			ret = PTR_ERR(to_page);
356
			goto out_err;
357
		}
A
Akinobu Mita 已提交
358
		copy_highpage(to_page, from_page);
359 360 361 362 363
		set_page_dirty(to_page);
		mark_page_accessed(to_page);
		page_cache_release(to_page);
	}

364
	ttm_tt_unpopulate(ttm);
365 366
	ttm->swap_storage = swap_storage;
	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
J
Jan Engelhardt 已提交
367 368
	if (persistent_swap_storage)
		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
369 370 371

	return 0;
out_err:
J
Jan Engelhardt 已提交
372
	if (!persistent_swap_storage)
373 374
		fput(swap_storage);

375
	return ret;
376
}
377 378 379 380 381 382

static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
{
	pgoff_t i;
	struct page **page = ttm->pages;

383 384 385
	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
		return;

386 387 388 389 390 391 392 393 394 395 396 397 398 399
	for (i = 0; i < ttm->num_pages; ++i) {
		(*page)->mapping = NULL;
		(*page++)->index = 0;
	}
}

void ttm_tt_unpopulate(struct ttm_tt *ttm)
{
	if (ttm->state == tt_unpopulated)
		return;

	ttm_tt_clear_mapping(ttm);
	ttm->bdev->driver->ttm_tt_unpopulate(ttm);
}