ttm_tt.c 11.1 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
31
32
33
/**************************************************************************
 *
 * 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>
 */

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

static int ttm_tt_swapin(struct ttm_tt *ttm);

/**
 * 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(*ttm->pages));
54
55
	ttm->dma_address = drm_calloc_large(ttm->num_pages,
					    sizeof(*ttm->dma_address));
56
57
58
59
}

static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
{
60
	drm_free_large(ttm->pages);
61
	ttm->pages = NULL;
62
63
	drm_free_large(ttm->dma_address);
	ttm->dma_address = NULL;
64
65
66
67
68
}

static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
{
	struct page *p;
69
	struct list_head h;
70
	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
71
72
	int ret;

73
	if (NULL == (p = ttm->pages[index])) {
74

75
76
		INIT_LIST_HEAD(&h);

77
		ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
78
				    &ttm->dma_address[index]);
79
80

		if (ret != 0)
81
82
			return NULL;

83
84
		p = list_first_entry(&h, struct page, lru);

85
86
87
88
		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
		if (unlikely(ret != 0))
			goto out_err;

89
		ttm->pages[index] = p;
90
91
92
	}
	return p;
out_err:
93
94
95
96
	INIT_LIST_HEAD(&h);
	list_add(&p->lru, &h);
	ttm_put_pages(&h, 1, ttm->page_flags,
		      ttm->caching_state, &ttm->dma_address[index]);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	return NULL;
}

struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
{
	int ret;

	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
		ret = ttm_tt_swapin(ttm);
		if (unlikely(ret != 0))
			return NULL;
	}
	return __ttm_tt_get_page(ttm, index);
}

int ttm_tt_populate(struct ttm_tt *ttm)
{
	struct page *page;
	unsigned long i;
	struct ttm_backend *be;
	int ret;

	if (ttm->state != tt_unpopulated)
		return 0;

	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
		ret = ttm_tt_swapin(ttm);
		if (unlikely(ret != 0))
			return ret;
	}

	be = ttm->be;

	for (i = 0; i < ttm->num_pages; ++i) {
		page = __ttm_tt_get_page(ttm, i);
		if (!page)
			return -ENOMEM;
	}

	be->func->populate(be, ttm->num_pages, ttm->pages,
137
			   ttm->dummy_read_page, ttm->dma_address);
138
139
140
	ttm->state = tt_unbound;
	return 0;
}
141
EXPORT_SYMBOL(ttm_tt_populate);
142
143
144

#ifdef CONFIG_X86
static inline int ttm_tt_set_page_caching(struct page *p,
145
146
					  enum ttm_caching_state c_old,
					  enum ttm_caching_state c_new)
147
{
148
149
	int ret = 0;

150
151
152
	if (PageHighMem(p))
		return 0;

153
	if (c_old != tt_cached) {
154
155
156
157
158
159
		/* 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;
160
	}
161

162
	if (c_new == tt_wc)
163
		ret = set_memory_wc((unsigned long) page_address(p), 1);
164
	else if (c_new == tt_uncached)
165
166
167
		ret = set_pages_uc(p, 1);

	return ret;
168
169
170
}
#else /* CONFIG_X86 */
static inline int ttm_tt_set_page_caching(struct page *p,
171
172
					  enum ttm_caching_state c_old,
					  enum ttm_caching_state c_new)
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
	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;

193
194
195
196
	if (ttm->state == tt_unpopulated) {
		/* Change caching but don't populate */
		ttm->caching_state = c_state;
		return 0;
197
198
199
	}

	if (ttm->caching_state == tt_cached)
200
		drm_clflush_pages(ttm->pages, ttm->num_pages);
201
202
203
204

	for (i = 0; i < ttm->num_pages; ++i) {
		cur_page = ttm->pages[i];
		if (likely(cur_page != NULL)) {
205
206
207
			ret = ttm_tt_set_page_caching(cur_page,
						      ttm->caching_state,
						      c_state);
208
209
210
211
212
213
214
215
216
217
218
219
220
			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)) {
221
			(void)ttm_tt_set_page_caching(cur_page, c_state,
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
						      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);
}
242
EXPORT_SYMBOL(ttm_tt_set_placement_caching);
243
244
245
246

static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
{
	int i;
247
248
	unsigned count = 0;
	struct list_head h;
249
250
251
	struct page *cur_page;
	struct ttm_backend *be = ttm->be;

252
253
	INIT_LIST_HEAD(&h);

254
255
256
	if (be)
		be->func->clear(be);
	for (i = 0; i < ttm->num_pages; ++i) {
257

258
259
260
261
262
263
264
		cur_page = ttm->pages[i];
		ttm->pages[i] = NULL;
		if (cur_page) {
			if (page_count(cur_page) != 1)
				printk(KERN_ERR TTM_PFX
				       "Erroneous page count. "
				       "Leaking pages.\n");
265
			ttm_mem_global_free_page(ttm->glob->mem_glob,
266
						 cur_page);
267
268
			list_add(&cur_page->lru, &h);
			count++;
269
270
		}
	}
271
	ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
272
		      ttm->dma_address);
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
	ttm->state = tt_unpopulated;
}

void ttm_tt_destroy(struct ttm_tt *ttm)
{
	struct ttm_backend *be;

	if (unlikely(ttm == NULL))
		return;

	be = ttm->be;
	if (likely(be != NULL)) {
		be->func->destroy(be);
		ttm->be = NULL;
	}

	if (likely(ttm->pages != NULL)) {
290
		ttm_tt_free_alloced_pages(ttm);
291
292
293
294

		ttm_tt_free_page_directory(ttm);
	}

Jan Engelhardt's avatar
Jan Engelhardt committed
295
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
	    ttm->swap_storage)
		fput(ttm->swap_storage);

	kfree(ttm);
}

struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
			     uint32_t page_flags, struct page *dummy_read_page)
{
	struct ttm_bo_driver *bo_driver = bdev->driver;
	struct ttm_tt *ttm;

	if (!bo_driver)
		return NULL;

	ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
	if (!ttm)
		return NULL;

315
	ttm->glob = bdev->glob;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	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_tt_alloc_page_directory(ttm);
	if (!ttm->pages) {
		ttm_tt_destroy(ttm);
		printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
		return NULL;
	}
	ttm->be = bo_driver->create_ttm_backend_entry(bdev);
	if (!ttm->be) {
		ttm_tt_destroy(ttm);
		printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
		return NULL;
	}
	ttm->state = tt_unpopulated;
	return ttm;
}

void ttm_tt_unbind(struct ttm_tt *ttm)
{
	int ret;
	struct ttm_backend *be = ttm->be;

	if (ttm->state == tt_bound) {
		ret = be->func->unbind(be);
		BUG_ON(ret);
		ttm->state = tt_unbound;
	}
}

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

	if (!ttm)
		return -EINVAL;

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

	be = ttm->be;

	ret = ttm_tt_populate(ttm);
	if (ret)
		return ret;

	ret = be->func->bind(be, bo_mem);
368
	if (unlikely(ret != 0))
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
		return ret;

	ttm->state = tt_bound;

	return 0;
}
EXPORT_SYMBOL(ttm_tt_bind);

static int ttm_tt_swapin(struct ttm_tt *ttm)
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
	void *from_virtual;
	void *to_virtual;
	int i;
386
	int ret = -ENOMEM;
387
388
389
390
391
392
393

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

	swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;

	for (i = 0; i < ttm->num_pages; ++i) {
394
		from_page = shmem_read_mapping_page(swap_space, i);
395
396
		if (IS_ERR(from_page)) {
			ret = PTR_ERR(from_page);
397
			goto out_err;
398
		}
399
400
401
402
403
404
405
406
407
408
409
410
411
412
		to_page = __ttm_tt_get_page(ttm, i);
		if (unlikely(to_page == NULL))
			goto out_err;

		preempt_disable();
		from_virtual = kmap_atomic(from_page, KM_USER0);
		to_virtual = kmap_atomic(to_page, KM_USER1);
		memcpy(to_virtual, from_virtual, PAGE_SIZE);
		kunmap_atomic(to_virtual, KM_USER1);
		kunmap_atomic(from_virtual, KM_USER0);
		preempt_enable();
		page_cache_release(from_page);
	}

Jan Engelhardt's avatar
Jan Engelhardt committed
413
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
414
415
416
417
418
419
420
		fput(swap_storage);
	ttm->swap_storage = NULL;
	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;

	return 0;
out_err:
	ttm_tt_free_alloced_pages(ttm);
421
	return ret;
422
423
}

Jan Engelhardt's avatar
Jan Engelhardt committed
424
int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
425
426
427
428
429
430
431
432
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
	void *from_virtual;
	void *to_virtual;
	int i;
433
	int ret = -ENOMEM;
434
435
436
437

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

Jan Engelhardt's avatar
Jan Engelhardt committed
438
	if (!persistent_swap_storage) {
439
440
441
442
443
		swap_storage = shmem_file_setup("ttm swap",
						ttm->num_pages << PAGE_SHIFT,
						0);
		if (unlikely(IS_ERR(swap_storage))) {
			printk(KERN_ERR "Failed allocating swap storage.\n");
444
			return PTR_ERR(swap_storage);
445
446
		}
	} else
Jan Engelhardt's avatar
Jan Engelhardt committed
447
		swap_storage = persistent_swap_storage;
448
449
450
451
452
453
454

	swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;

	for (i = 0; i < ttm->num_pages; ++i) {
		from_page = ttm->pages[i];
		if (unlikely(from_page == NULL))
			continue;
455
		to_page = shmem_read_mapping_page(swap_space, i);
456
457
		if (unlikely(IS_ERR(to_page))) {
			ret = PTR_ERR(to_page);
458
			goto out_err;
459
		}
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
		preempt_disable();
		from_virtual = kmap_atomic(from_page, KM_USER0);
		to_virtual = kmap_atomic(to_page, KM_USER1);
		memcpy(to_virtual, from_virtual, PAGE_SIZE);
		kunmap_atomic(to_virtual, KM_USER1);
		kunmap_atomic(from_virtual, KM_USER0);
		preempt_enable();
		set_page_dirty(to_page);
		mark_page_accessed(to_page);
		page_cache_release(to_page);
	}

	ttm_tt_free_alloced_pages(ttm);
	ttm->swap_storage = swap_storage;
	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt's avatar
Jan Engelhardt committed
475
476
	if (persistent_swap_storage)
		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
477
478
479

	return 0;
out_err:
Jan Engelhardt's avatar
Jan Engelhardt committed
480
	if (!persistent_swap_storage)
481
482
		fput(swap_storage);

483
	return ret;
484
}