xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_tt.c (revision a09d2831)
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30 
31 #include <linux/vmalloc.h>
32 #include <linux/sched.h>
33 #include <linux/highmem.h>
34 #include <linux/pagemap.h>
35 #include <linux/file.h>
36 #include <linux/swap.h>
37 #include "drm_cache.h"
38 #include "ttm/ttm_module.h"
39 #include "ttm/ttm_bo_driver.h"
40 #include "ttm/ttm_placement.h"
41 
42 static int ttm_tt_swapin(struct ttm_tt *ttm);
43 
44 /**
45  * Allocates storage for pointers to the pages that back the ttm.
46  *
47  * Uses kmalloc if possible. Otherwise falls back to vmalloc.
48  */
49 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
50 {
51 	unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
52 	ttm->pages = NULL;
53 
54 	if (size <= PAGE_SIZE)
55 		ttm->pages = kzalloc(size, GFP_KERNEL);
56 
57 	if (!ttm->pages) {
58 		ttm->pages = vmalloc_user(size);
59 		if (ttm->pages)
60 			ttm->page_flags |= TTM_PAGE_FLAG_VMALLOC;
61 	}
62 }
63 
64 static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
65 {
66 	if (ttm->page_flags & TTM_PAGE_FLAG_VMALLOC) {
67 		vfree(ttm->pages);
68 		ttm->page_flags &= ~TTM_PAGE_FLAG_VMALLOC;
69 	} else {
70 		kfree(ttm->pages);
71 	}
72 	ttm->pages = NULL;
73 }
74 
75 static struct page *ttm_tt_alloc_page(unsigned page_flags)
76 {
77 	gfp_t gfp_flags = GFP_USER;
78 
79 	if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
80 		gfp_flags |= __GFP_ZERO;
81 
82 	if (page_flags & TTM_PAGE_FLAG_DMA32)
83 		gfp_flags |= __GFP_DMA32;
84 	else
85 		gfp_flags |= __GFP_HIGHMEM;
86 
87 	return alloc_page(gfp_flags);
88 }
89 
90 static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
91 {
92 	int write;
93 	int dirty;
94 	struct page *page;
95 	int i;
96 	struct ttm_backend *be = ttm->be;
97 
98 	BUG_ON(!(ttm->page_flags & TTM_PAGE_FLAG_USER));
99 	write = ((ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0);
100 	dirty = ((ttm->page_flags & TTM_PAGE_FLAG_USER_DIRTY) != 0);
101 
102 	if (be)
103 		be->func->clear(be);
104 
105 	for (i = 0; i < ttm->num_pages; ++i) {
106 		page = ttm->pages[i];
107 		if (page == NULL)
108 			continue;
109 
110 		if (page == ttm->dummy_read_page) {
111 			BUG_ON(write);
112 			continue;
113 		}
114 
115 		if (write && dirty && !PageReserved(page))
116 			set_page_dirty_lock(page);
117 
118 		ttm->pages[i] = NULL;
119 		ttm_mem_global_free(ttm->glob->mem_glob, PAGE_SIZE);
120 		put_page(page);
121 	}
122 	ttm->state = tt_unpopulated;
123 	ttm->first_himem_page = ttm->num_pages;
124 	ttm->last_lomem_page = -1;
125 }
126 
127 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
128 {
129 	struct page *p;
130 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
131 	int ret;
132 
133 	while (NULL == (p = ttm->pages[index])) {
134 		p = ttm_tt_alloc_page(ttm->page_flags);
135 
136 		if (!p)
137 			return NULL;
138 
139 		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
140 		if (unlikely(ret != 0))
141 			goto out_err;
142 
143 		if (PageHighMem(p))
144 			ttm->pages[--ttm->first_himem_page] = p;
145 		else
146 			ttm->pages[++ttm->last_lomem_page] = p;
147 	}
148 	return p;
149 out_err:
150 	put_page(p);
151 	return NULL;
152 }
153 
154 struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
155 {
156 	int ret;
157 
158 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
159 		ret = ttm_tt_swapin(ttm);
160 		if (unlikely(ret != 0))
161 			return NULL;
162 	}
163 	return __ttm_tt_get_page(ttm, index);
164 }
165 
166 int ttm_tt_populate(struct ttm_tt *ttm)
167 {
168 	struct page *page;
169 	unsigned long i;
170 	struct ttm_backend *be;
171 	int ret;
172 
173 	if (ttm->state != tt_unpopulated)
174 		return 0;
175 
176 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
177 		ret = ttm_tt_swapin(ttm);
178 		if (unlikely(ret != 0))
179 			return ret;
180 	}
181 
182 	be = ttm->be;
183 
184 	for (i = 0; i < ttm->num_pages; ++i) {
185 		page = __ttm_tt_get_page(ttm, i);
186 		if (!page)
187 			return -ENOMEM;
188 	}
189 
190 	be->func->populate(be, ttm->num_pages, ttm->pages,
191 			   ttm->dummy_read_page);
192 	ttm->state = tt_unbound;
193 	return 0;
194 }
195 EXPORT_SYMBOL(ttm_tt_populate);
196 
197 #ifdef CONFIG_X86
198 static inline int ttm_tt_set_page_caching(struct page *p,
199 					  enum ttm_caching_state c_state)
200 {
201 	if (PageHighMem(p))
202 		return 0;
203 
204 	switch (c_state) {
205 	case tt_cached:
206 		return set_pages_wb(p, 1);
207 	case tt_wc:
208 	    return set_memory_wc((unsigned long) page_address(p), 1);
209 	default:
210 		return set_pages_uc(p, 1);
211 	}
212 }
213 #else /* CONFIG_X86 */
214 static inline int ttm_tt_set_page_caching(struct page *p,
215 					  enum ttm_caching_state c_state)
216 {
217 	return 0;
218 }
219 #endif /* CONFIG_X86 */
220 
221 /*
222  * Change caching policy for the linear kernel map
223  * for range of pages in a ttm.
224  */
225 
226 static int ttm_tt_set_caching(struct ttm_tt *ttm,
227 			      enum ttm_caching_state c_state)
228 {
229 	int i, j;
230 	struct page *cur_page;
231 	int ret;
232 
233 	if (ttm->caching_state == c_state)
234 		return 0;
235 
236 	if (c_state != tt_cached) {
237 		ret = ttm_tt_populate(ttm);
238 		if (unlikely(ret != 0))
239 			return ret;
240 	}
241 
242 	if (ttm->caching_state == tt_cached)
243 		drm_clflush_pages(ttm->pages, ttm->num_pages);
244 
245 	for (i = 0; i < ttm->num_pages; ++i) {
246 		cur_page = ttm->pages[i];
247 		if (likely(cur_page != NULL)) {
248 			ret = ttm_tt_set_page_caching(cur_page, c_state);
249 			if (unlikely(ret != 0))
250 				goto out_err;
251 		}
252 	}
253 
254 	ttm->caching_state = c_state;
255 
256 	return 0;
257 
258 out_err:
259 	for (j = 0; j < i; ++j) {
260 		cur_page = ttm->pages[j];
261 		if (likely(cur_page != NULL)) {
262 			(void)ttm_tt_set_page_caching(cur_page,
263 						      ttm->caching_state);
264 		}
265 	}
266 
267 	return ret;
268 }
269 
270 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
271 {
272 	enum ttm_caching_state state;
273 
274 	if (placement & TTM_PL_FLAG_WC)
275 		state = tt_wc;
276 	else if (placement & TTM_PL_FLAG_UNCACHED)
277 		state = tt_uncached;
278 	else
279 		state = tt_cached;
280 
281 	return ttm_tt_set_caching(ttm, state);
282 }
283 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
284 
285 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
286 {
287 	int i;
288 	struct page *cur_page;
289 	struct ttm_backend *be = ttm->be;
290 
291 	if (be)
292 		be->func->clear(be);
293 	(void)ttm_tt_set_caching(ttm, tt_cached);
294 	for (i = 0; i < ttm->num_pages; ++i) {
295 		cur_page = ttm->pages[i];
296 		ttm->pages[i] = NULL;
297 		if (cur_page) {
298 			if (page_count(cur_page) != 1)
299 				printk(KERN_ERR TTM_PFX
300 				       "Erroneous page count. "
301 				       "Leaking pages.\n");
302 			ttm_mem_global_free_page(ttm->glob->mem_glob,
303 						 cur_page);
304 			__free_page(cur_page);
305 		}
306 	}
307 	ttm->state = tt_unpopulated;
308 	ttm->first_himem_page = ttm->num_pages;
309 	ttm->last_lomem_page = -1;
310 }
311 
312 void ttm_tt_destroy(struct ttm_tt *ttm)
313 {
314 	struct ttm_backend *be;
315 
316 	if (unlikely(ttm == NULL))
317 		return;
318 
319 	be = ttm->be;
320 	if (likely(be != NULL)) {
321 		be->func->destroy(be);
322 		ttm->be = NULL;
323 	}
324 
325 	if (likely(ttm->pages != NULL)) {
326 		if (ttm->page_flags & TTM_PAGE_FLAG_USER)
327 			ttm_tt_free_user_pages(ttm);
328 		else
329 			ttm_tt_free_alloced_pages(ttm);
330 
331 		ttm_tt_free_page_directory(ttm);
332 	}
333 
334 	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
335 	    ttm->swap_storage)
336 		fput(ttm->swap_storage);
337 
338 	kfree(ttm);
339 }
340 
341 int ttm_tt_set_user(struct ttm_tt *ttm,
342 		    struct task_struct *tsk,
343 		    unsigned long start, unsigned long num_pages)
344 {
345 	struct mm_struct *mm = tsk->mm;
346 	int ret;
347 	int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
348 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
349 
350 	BUG_ON(num_pages != ttm->num_pages);
351 	BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
352 
353 	/**
354 	 * Account user pages as lowmem pages for now.
355 	 */
356 
357 	ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
358 				   false, false);
359 	if (unlikely(ret != 0))
360 		return ret;
361 
362 	down_read(&mm->mmap_sem);
363 	ret = get_user_pages(tsk, mm, start, num_pages,
364 			     write, 0, ttm->pages, NULL);
365 	up_read(&mm->mmap_sem);
366 
367 	if (ret != num_pages && write) {
368 		ttm_tt_free_user_pages(ttm);
369 		ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
370 		return -ENOMEM;
371 	}
372 
373 	ttm->tsk = tsk;
374 	ttm->start = start;
375 	ttm->state = tt_unbound;
376 
377 	return 0;
378 }
379 
380 struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
381 			     uint32_t page_flags, struct page *dummy_read_page)
382 {
383 	struct ttm_bo_driver *bo_driver = bdev->driver;
384 	struct ttm_tt *ttm;
385 
386 	if (!bo_driver)
387 		return NULL;
388 
389 	ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
390 	if (!ttm)
391 		return NULL;
392 
393 	ttm->glob = bdev->glob;
394 	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
395 	ttm->first_himem_page = ttm->num_pages;
396 	ttm->last_lomem_page = -1;
397 	ttm->caching_state = tt_cached;
398 	ttm->page_flags = page_flags;
399 
400 	ttm->dummy_read_page = dummy_read_page;
401 
402 	ttm_tt_alloc_page_directory(ttm);
403 	if (!ttm->pages) {
404 		ttm_tt_destroy(ttm);
405 		printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
406 		return NULL;
407 	}
408 	ttm->be = bo_driver->create_ttm_backend_entry(bdev);
409 	if (!ttm->be) {
410 		ttm_tt_destroy(ttm);
411 		printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
412 		return NULL;
413 	}
414 	ttm->state = tt_unpopulated;
415 	return ttm;
416 }
417 
418 void ttm_tt_unbind(struct ttm_tt *ttm)
419 {
420 	int ret;
421 	struct ttm_backend *be = ttm->be;
422 
423 	if (ttm->state == tt_bound) {
424 		ret = be->func->unbind(be);
425 		BUG_ON(ret);
426 		ttm->state = tt_unbound;
427 	}
428 }
429 
430 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
431 {
432 	int ret = 0;
433 	struct ttm_backend *be;
434 
435 	if (!ttm)
436 		return -EINVAL;
437 
438 	if (ttm->state == tt_bound)
439 		return 0;
440 
441 	be = ttm->be;
442 
443 	ret = ttm_tt_populate(ttm);
444 	if (ret)
445 		return ret;
446 
447 	ret = be->func->bind(be, bo_mem);
448 	if (ret) {
449 		printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
450 		return ret;
451 	}
452 
453 	ttm->state = tt_bound;
454 
455 	if (ttm->page_flags & TTM_PAGE_FLAG_USER)
456 		ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
457 	return 0;
458 }
459 EXPORT_SYMBOL(ttm_tt_bind);
460 
461 static int ttm_tt_swapin(struct ttm_tt *ttm)
462 {
463 	struct address_space *swap_space;
464 	struct file *swap_storage;
465 	struct page *from_page;
466 	struct page *to_page;
467 	void *from_virtual;
468 	void *to_virtual;
469 	int i;
470 	int ret;
471 
472 	if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
473 		ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
474 				      ttm->num_pages);
475 		if (unlikely(ret != 0))
476 			return ret;
477 
478 		ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
479 		return 0;
480 	}
481 
482 	swap_storage = ttm->swap_storage;
483 	BUG_ON(swap_storage == NULL);
484 
485 	swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
486 
487 	for (i = 0; i < ttm->num_pages; ++i) {
488 		from_page = read_mapping_page(swap_space, i, NULL);
489 		if (IS_ERR(from_page))
490 			goto out_err;
491 		to_page = __ttm_tt_get_page(ttm, i);
492 		if (unlikely(to_page == NULL))
493 			goto out_err;
494 
495 		preempt_disable();
496 		from_virtual = kmap_atomic(from_page, KM_USER0);
497 		to_virtual = kmap_atomic(to_page, KM_USER1);
498 		memcpy(to_virtual, from_virtual, PAGE_SIZE);
499 		kunmap_atomic(to_virtual, KM_USER1);
500 		kunmap_atomic(from_virtual, KM_USER0);
501 		preempt_enable();
502 		page_cache_release(from_page);
503 	}
504 
505 	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
506 		fput(swap_storage);
507 	ttm->swap_storage = NULL;
508 	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
509 
510 	return 0;
511 out_err:
512 	ttm_tt_free_alloced_pages(ttm);
513 	return -ENOMEM;
514 }
515 
516 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage)
517 {
518 	struct address_space *swap_space;
519 	struct file *swap_storage;
520 	struct page *from_page;
521 	struct page *to_page;
522 	void *from_virtual;
523 	void *to_virtual;
524 	int i;
525 
526 	BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
527 	BUG_ON(ttm->caching_state != tt_cached);
528 
529 	/*
530 	 * For user buffers, just unpin the pages, as there should be
531 	 * vma references.
532 	 */
533 
534 	if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
535 		ttm_tt_free_user_pages(ttm);
536 		ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
537 		ttm->swap_storage = NULL;
538 		return 0;
539 	}
540 
541 	if (!persistant_swap_storage) {
542 		swap_storage = shmem_file_setup("ttm swap",
543 						ttm->num_pages << PAGE_SHIFT,
544 						0);
545 		if (unlikely(IS_ERR(swap_storage))) {
546 			printk(KERN_ERR "Failed allocating swap storage.\n");
547 			return -ENOMEM;
548 		}
549 	} else
550 		swap_storage = persistant_swap_storage;
551 
552 	swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
553 
554 	for (i = 0; i < ttm->num_pages; ++i) {
555 		from_page = ttm->pages[i];
556 		if (unlikely(from_page == NULL))
557 			continue;
558 		to_page = read_mapping_page(swap_space, i, NULL);
559 		if (unlikely(to_page == NULL))
560 			goto out_err;
561 
562 		preempt_disable();
563 		from_virtual = kmap_atomic(from_page, KM_USER0);
564 		to_virtual = kmap_atomic(to_page, KM_USER1);
565 		memcpy(to_virtual, from_virtual, PAGE_SIZE);
566 		kunmap_atomic(to_virtual, KM_USER1);
567 		kunmap_atomic(from_virtual, KM_USER0);
568 		preempt_enable();
569 		set_page_dirty(to_page);
570 		mark_page_accessed(to_page);
571 		page_cache_release(to_page);
572 	}
573 
574 	ttm_tt_free_alloced_pages(ttm);
575 	ttm->swap_storage = swap_storage;
576 	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
577 	if (persistant_swap_storage)
578 		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP;
579 
580 	return 0;
581 out_err:
582 	if (!persistant_swap_storage)
583 		fput(swap_storage);
584 
585 	return -ENOMEM;
586 }
587