1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
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"
42 static int ttm_tt_swapin(struct ttm_tt *ttm);
45 * Allocates storage for pointers to the pages that back the ttm.
47 * Uses kmalloc if possible. Otherwise falls back to vmalloc.
49 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
51 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
54 if (size <= PAGE_SIZE)
55 ttm->pages = kzalloc(size, GFP_KERNEL);
58 ttm->pages = vmalloc_user(size);
60 ttm->page_flags |= TTM_PAGE_FLAG_VMALLOC;
64 static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
66 if (ttm->page_flags & TTM_PAGE_FLAG_VMALLOC) {
68 ttm->page_flags &= ~TTM_PAGE_FLAG_VMALLOC;
75 static struct page *ttm_tt_alloc_page(unsigned page_flags)
77 gfp_t gfp_flags = GFP_USER;
79 if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
80 gfp_flags |= __GFP_ZERO;
82 if (page_flags & TTM_PAGE_FLAG_DMA32)
83 gfp_flags |= __GFP_DMA32;
85 gfp_flags |= __GFP_HIGHMEM;
87 return alloc_page(gfp_flags);
90 static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
96 struct ttm_backend *be = ttm->be;
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);
105 for (i = 0; i < ttm->num_pages; ++i) {
106 page = ttm->pages[i];
110 if (page == ttm->dummy_read_page) {
115 if (write && dirty && !PageReserved(page))
116 set_page_dirty_lock(page);
118 ttm->pages[i] = NULL;
119 ttm_mem_global_free(ttm->glob->mem_glob, PAGE_SIZE);
122 ttm->state = tt_unpopulated;
123 ttm->first_himem_page = ttm->num_pages;
124 ttm->last_lomem_page = -1;
127 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
130 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
133 while (NULL == (p = ttm->pages[index])) {
134 p = ttm_tt_alloc_page(ttm->page_flags);
139 ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
140 if (unlikely(ret != 0))
144 ttm->pages[--ttm->first_himem_page] = p;
146 ttm->pages[++ttm->last_lomem_page] = p;
154 struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
158 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
159 ret = ttm_tt_swapin(ttm);
160 if (unlikely(ret != 0))
163 return __ttm_tt_get_page(ttm, index);
166 int ttm_tt_populate(struct ttm_tt *ttm)
170 struct ttm_backend *be;
173 if (ttm->state != tt_unpopulated)
176 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
177 ret = ttm_tt_swapin(ttm);
178 if (unlikely(ret != 0))
184 for (i = 0; i < ttm->num_pages; ++i) {
185 page = __ttm_tt_get_page(ttm, i);
190 be->func->populate(be, ttm->num_pages, ttm->pages,
191 ttm->dummy_read_page);
192 ttm->state = tt_unbound;
195 EXPORT_SYMBOL(ttm_tt_populate);
198 static inline int ttm_tt_set_page_caching(struct page *p,
199 enum ttm_caching_state c_state)
206 if (get_page_memtype(p) != -1) {
207 /* p isn't in the default caching state, set it to
208 * writeback first to free its current memtype. */
210 ret = set_pages_wb(p, 1);
215 if (c_state == tt_wc)
216 ret = set_memory_wc((unsigned long) page_address(p), 1);
217 else if (c_state == tt_uncached)
218 ret = set_pages_uc(p, 1);
222 #else /* CONFIG_X86 */
223 static inline int ttm_tt_set_page_caching(struct page *p,
224 enum ttm_caching_state c_state)
228 #endif /* CONFIG_X86 */
231 * Change caching policy for the linear kernel map
232 * for range of pages in a ttm.
235 static int ttm_tt_set_caching(struct ttm_tt *ttm,
236 enum ttm_caching_state c_state)
239 struct page *cur_page;
242 if (ttm->caching_state == c_state)
245 if (c_state != tt_cached) {
246 ret = ttm_tt_populate(ttm);
247 if (unlikely(ret != 0))
251 if (ttm->caching_state == tt_cached)
252 drm_clflush_pages(ttm->pages, ttm->num_pages);
254 for (i = 0; i < ttm->num_pages; ++i) {
255 cur_page = ttm->pages[i];
256 if (likely(cur_page != NULL)) {
257 ret = ttm_tt_set_page_caching(cur_page, c_state);
258 if (unlikely(ret != 0))
263 ttm->caching_state = c_state;
268 for (j = 0; j < i; ++j) {
269 cur_page = ttm->pages[j];
270 if (likely(cur_page != NULL)) {
271 (void)ttm_tt_set_page_caching(cur_page,
279 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
281 enum ttm_caching_state state;
283 if (placement & TTM_PL_FLAG_WC)
285 else if (placement & TTM_PL_FLAG_UNCACHED)
290 return ttm_tt_set_caching(ttm, state);
292 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
294 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
297 struct page *cur_page;
298 struct ttm_backend *be = ttm->be;
302 (void)ttm_tt_set_caching(ttm, tt_cached);
303 for (i = 0; i < ttm->num_pages; ++i) {
304 cur_page = ttm->pages[i];
305 ttm->pages[i] = NULL;
307 if (page_count(cur_page) != 1)
308 printk(KERN_ERR TTM_PFX
309 "Erroneous page count. "
311 ttm_mem_global_free_page(ttm->glob->mem_glob,
313 __free_page(cur_page);
316 ttm->state = tt_unpopulated;
317 ttm->first_himem_page = ttm->num_pages;
318 ttm->last_lomem_page = -1;
321 void ttm_tt_destroy(struct ttm_tt *ttm)
323 struct ttm_backend *be;
325 if (unlikely(ttm == NULL))
329 if (likely(be != NULL)) {
330 be->func->destroy(be);
334 if (likely(ttm->pages != NULL)) {
335 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
336 ttm_tt_free_user_pages(ttm);
338 ttm_tt_free_alloced_pages(ttm);
340 ttm_tt_free_page_directory(ttm);
343 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
345 fput(ttm->swap_storage);
350 int ttm_tt_set_user(struct ttm_tt *ttm,
351 struct task_struct *tsk,
352 unsigned long start, unsigned long num_pages)
354 struct mm_struct *mm = tsk->mm;
356 int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
357 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
359 BUG_ON(num_pages != ttm->num_pages);
360 BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
363 * Account user pages as lowmem pages for now.
366 ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
368 if (unlikely(ret != 0))
371 down_read(&mm->mmap_sem);
372 ret = get_user_pages(tsk, mm, start, num_pages,
373 write, 0, ttm->pages, NULL);
374 up_read(&mm->mmap_sem);
376 if (ret != num_pages && write) {
377 ttm_tt_free_user_pages(ttm);
378 ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
384 ttm->state = tt_unbound;
389 struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
390 uint32_t page_flags, struct page *dummy_read_page)
392 struct ttm_bo_driver *bo_driver = bdev->driver;
398 ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
402 ttm->glob = bdev->glob;
403 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
404 ttm->first_himem_page = ttm->num_pages;
405 ttm->last_lomem_page = -1;
406 ttm->caching_state = tt_cached;
407 ttm->page_flags = page_flags;
409 ttm->dummy_read_page = dummy_read_page;
411 ttm_tt_alloc_page_directory(ttm);
414 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
417 ttm->be = bo_driver->create_ttm_backend_entry(bdev);
420 printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
423 ttm->state = tt_unpopulated;
427 void ttm_tt_unbind(struct ttm_tt *ttm)
430 struct ttm_backend *be = ttm->be;
432 if (ttm->state == tt_bound) {
433 ret = be->func->unbind(be);
435 ttm->state = tt_unbound;
439 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
442 struct ttm_backend *be;
447 if (ttm->state == tt_bound)
452 ret = ttm_tt_populate(ttm);
456 ret = be->func->bind(be, bo_mem);
458 printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
462 ttm->state = tt_bound;
464 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
465 ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
468 EXPORT_SYMBOL(ttm_tt_bind);
470 static int ttm_tt_swapin(struct ttm_tt *ttm)
472 struct address_space *swap_space;
473 struct file *swap_storage;
474 struct page *from_page;
475 struct page *to_page;
481 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
482 ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
484 if (unlikely(ret != 0))
487 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
491 swap_storage = ttm->swap_storage;
492 BUG_ON(swap_storage == NULL);
494 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
496 for (i = 0; i < ttm->num_pages; ++i) {
497 from_page = read_mapping_page(swap_space, i, NULL);
498 if (IS_ERR(from_page))
500 to_page = __ttm_tt_get_page(ttm, i);
501 if (unlikely(to_page == NULL))
505 from_virtual = kmap_atomic(from_page, KM_USER0);
506 to_virtual = kmap_atomic(to_page, KM_USER1);
507 memcpy(to_virtual, from_virtual, PAGE_SIZE);
508 kunmap_atomic(to_virtual, KM_USER1);
509 kunmap_atomic(from_virtual, KM_USER0);
511 page_cache_release(from_page);
514 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
516 ttm->swap_storage = NULL;
517 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
521 ttm_tt_free_alloced_pages(ttm);
525 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage)
527 struct address_space *swap_space;
528 struct file *swap_storage;
529 struct page *from_page;
530 struct page *to_page;
535 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
536 BUG_ON(ttm->caching_state != tt_cached);
539 * For user buffers, just unpin the pages, as there should be
543 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
544 ttm_tt_free_user_pages(ttm);
545 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
546 ttm->swap_storage = NULL;
550 if (!persistant_swap_storage) {
551 swap_storage = shmem_file_setup("ttm swap",
552 ttm->num_pages << PAGE_SHIFT,
554 if (unlikely(IS_ERR(swap_storage))) {
555 printk(KERN_ERR "Failed allocating swap storage.\n");
559 swap_storage = persistant_swap_storage;
561 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
563 for (i = 0; i < ttm->num_pages; ++i) {
564 from_page = ttm->pages[i];
565 if (unlikely(from_page == NULL))
567 to_page = read_mapping_page(swap_space, i, NULL);
568 if (unlikely(to_page == NULL))
572 from_virtual = kmap_atomic(from_page, KM_USER0);
573 to_virtual = kmap_atomic(to_page, KM_USER1);
574 memcpy(to_virtual, from_virtual, PAGE_SIZE);
575 kunmap_atomic(to_virtual, KM_USER1);
576 kunmap_atomic(from_virtual, KM_USER0);
578 set_page_dirty(to_page);
579 mark_page_accessed(to_page);
580 page_cache_release(to_page);
583 ttm_tt_free_alloced_pages(ttm);
584 ttm->swap_storage = swap_storage;
585 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
586 if (persistant_swap_storage)
587 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP;
591 if (!persistant_swap_storage)