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/sched.h>
32 #include <linux/highmem.h>
33 #include <linux/pagemap.h>
34 #include <linux/file.h>
35 #include <linux/swap.h>
36 #include "drm_cache.h"
37 #include "drm_mem_util.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 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
49 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(*ttm->pages));
52 static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
54 drm_free_large(ttm->pages);
58 static struct page *ttm_tt_alloc_page(unsigned page_flags)
60 gfp_t gfp_flags = GFP_USER;
62 if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
63 gfp_flags |= __GFP_ZERO;
65 if (page_flags & TTM_PAGE_FLAG_DMA32)
66 gfp_flags |= __GFP_DMA32;
68 gfp_flags |= __GFP_HIGHMEM;
70 return alloc_page(gfp_flags);
73 static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
79 struct ttm_backend *be = ttm->be;
81 BUG_ON(!(ttm->page_flags & TTM_PAGE_FLAG_USER));
82 write = ((ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0);
83 dirty = ((ttm->page_flags & TTM_PAGE_FLAG_USER_DIRTY) != 0);
88 for (i = 0; i < ttm->num_pages; ++i) {
93 if (page == ttm->dummy_read_page) {
98 if (write && dirty && !PageReserved(page))
99 set_page_dirty_lock(page);
101 ttm->pages[i] = NULL;
102 ttm_mem_global_free(ttm->glob->mem_glob, PAGE_SIZE);
105 ttm->state = tt_unpopulated;
106 ttm->first_himem_page = ttm->num_pages;
107 ttm->last_lomem_page = -1;
110 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
113 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
116 while (NULL == (p = ttm->pages[index])) {
117 p = ttm_tt_alloc_page(ttm->page_flags);
122 ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
123 if (unlikely(ret != 0))
127 ttm->pages[--ttm->first_himem_page] = p;
129 ttm->pages[++ttm->last_lomem_page] = p;
137 struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
141 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
142 ret = ttm_tt_swapin(ttm);
143 if (unlikely(ret != 0))
146 return __ttm_tt_get_page(ttm, index);
149 int ttm_tt_populate(struct ttm_tt *ttm)
153 struct ttm_backend *be;
156 if (ttm->state != tt_unpopulated)
159 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
160 ret = ttm_tt_swapin(ttm);
161 if (unlikely(ret != 0))
167 for (i = 0; i < ttm->num_pages; ++i) {
168 page = __ttm_tt_get_page(ttm, i);
173 be->func->populate(be, ttm->num_pages, ttm->pages,
174 ttm->dummy_read_page);
175 ttm->state = tt_unbound;
178 EXPORT_SYMBOL(ttm_tt_populate);
181 static inline int ttm_tt_set_page_caching(struct page *p,
182 enum ttm_caching_state c_old,
183 enum ttm_caching_state c_new)
190 if (c_old != tt_cached) {
191 /* p isn't in the default caching state, set it to
192 * writeback first to free its current memtype. */
194 ret = set_pages_wb(p, 1);
200 ret = set_memory_wc((unsigned long) page_address(p), 1);
201 else if (c_new == tt_uncached)
202 ret = set_pages_uc(p, 1);
206 #else /* CONFIG_X86 */
207 static inline int ttm_tt_set_page_caching(struct page *p,
208 enum ttm_caching_state c_old,
209 enum ttm_caching_state c_new)
213 #endif /* CONFIG_X86 */
216 * Change caching policy for the linear kernel map
217 * for range of pages in a ttm.
220 static int ttm_tt_set_caching(struct ttm_tt *ttm,
221 enum ttm_caching_state c_state)
224 struct page *cur_page;
227 if (ttm->caching_state == c_state)
230 if (c_state != tt_cached) {
231 ret = ttm_tt_populate(ttm);
232 if (unlikely(ret != 0))
236 if (ttm->caching_state == tt_cached)
237 drm_clflush_pages(ttm->pages, ttm->num_pages);
239 for (i = 0; i < ttm->num_pages; ++i) {
240 cur_page = ttm->pages[i];
241 if (likely(cur_page != NULL)) {
242 ret = ttm_tt_set_page_caching(cur_page,
245 if (unlikely(ret != 0))
250 ttm->caching_state = c_state;
255 for (j = 0; j < i; ++j) {
256 cur_page = ttm->pages[j];
257 if (likely(cur_page != NULL)) {
258 (void)ttm_tt_set_page_caching(cur_page, c_state,
266 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
268 enum ttm_caching_state state;
270 if (placement & TTM_PL_FLAG_WC)
272 else if (placement & TTM_PL_FLAG_UNCACHED)
277 return ttm_tt_set_caching(ttm, state);
279 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
281 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
284 struct page *cur_page;
285 struct ttm_backend *be = ttm->be;
289 (void)ttm_tt_set_caching(ttm, tt_cached);
290 for (i = 0; i < ttm->num_pages; ++i) {
291 cur_page = ttm->pages[i];
292 ttm->pages[i] = NULL;
294 if (page_count(cur_page) != 1)
295 printk(KERN_ERR TTM_PFX
296 "Erroneous page count. "
298 ttm_mem_global_free_page(ttm->glob->mem_glob,
300 __free_page(cur_page);
303 ttm->state = tt_unpopulated;
304 ttm->first_himem_page = ttm->num_pages;
305 ttm->last_lomem_page = -1;
308 void ttm_tt_destroy(struct ttm_tt *ttm)
310 struct ttm_backend *be;
312 if (unlikely(ttm == NULL))
316 if (likely(be != NULL)) {
317 be->func->destroy(be);
321 if (likely(ttm->pages != NULL)) {
322 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
323 ttm_tt_free_user_pages(ttm);
325 ttm_tt_free_alloced_pages(ttm);
327 ttm_tt_free_page_directory(ttm);
330 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
332 fput(ttm->swap_storage);
337 int ttm_tt_set_user(struct ttm_tt *ttm,
338 struct task_struct *tsk,
339 unsigned long start, unsigned long num_pages)
341 struct mm_struct *mm = tsk->mm;
343 int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
344 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
346 BUG_ON(num_pages != ttm->num_pages);
347 BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
350 * Account user pages as lowmem pages for now.
353 ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
355 if (unlikely(ret != 0))
358 down_read(&mm->mmap_sem);
359 ret = get_user_pages(tsk, mm, start, num_pages,
360 write, 0, ttm->pages, NULL);
361 up_read(&mm->mmap_sem);
363 if (ret != num_pages && write) {
364 ttm_tt_free_user_pages(ttm);
365 ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
371 ttm->state = tt_unbound;
376 struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
377 uint32_t page_flags, struct page *dummy_read_page)
379 struct ttm_bo_driver *bo_driver = bdev->driver;
385 ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
389 ttm->glob = bdev->glob;
390 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
391 ttm->first_himem_page = ttm->num_pages;
392 ttm->last_lomem_page = -1;
393 ttm->caching_state = tt_cached;
394 ttm->page_flags = page_flags;
396 ttm->dummy_read_page = dummy_read_page;
398 ttm_tt_alloc_page_directory(ttm);
401 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
404 ttm->be = bo_driver->create_ttm_backend_entry(bdev);
407 printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
410 ttm->state = tt_unpopulated;
414 void ttm_tt_unbind(struct ttm_tt *ttm)
417 struct ttm_backend *be = ttm->be;
419 if (ttm->state == tt_bound) {
420 ret = be->func->unbind(be);
422 ttm->state = tt_unbound;
426 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
429 struct ttm_backend *be;
434 if (ttm->state == tt_bound)
439 ret = ttm_tt_populate(ttm);
443 ret = be->func->bind(be, bo_mem);
445 printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
449 ttm->state = tt_bound;
451 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
452 ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
455 EXPORT_SYMBOL(ttm_tt_bind);
457 static int ttm_tt_swapin(struct ttm_tt *ttm)
459 struct address_space *swap_space;
460 struct file *swap_storage;
461 struct page *from_page;
462 struct page *to_page;
468 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
469 ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
471 if (unlikely(ret != 0))
474 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
478 swap_storage = ttm->swap_storage;
479 BUG_ON(swap_storage == NULL);
481 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
483 for (i = 0; i < ttm->num_pages; ++i) {
484 from_page = read_mapping_page(swap_space, i, NULL);
485 if (IS_ERR(from_page)) {
486 ret = PTR_ERR(from_page);
489 to_page = __ttm_tt_get_page(ttm, i);
490 if (unlikely(to_page == NULL))
494 from_virtual = kmap_atomic(from_page, KM_USER0);
495 to_virtual = kmap_atomic(to_page, KM_USER1);
496 memcpy(to_virtual, from_virtual, PAGE_SIZE);
497 kunmap_atomic(to_virtual, KM_USER1);
498 kunmap_atomic(from_virtual, KM_USER0);
500 page_cache_release(from_page);
503 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
505 ttm->swap_storage = NULL;
506 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
510 ttm_tt_free_alloced_pages(ttm);
514 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage)
516 struct address_space *swap_space;
517 struct file *swap_storage;
518 struct page *from_page;
519 struct page *to_page;
525 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
526 BUG_ON(ttm->caching_state != tt_cached);
529 * For user buffers, just unpin the pages, as there should be
533 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
534 ttm_tt_free_user_pages(ttm);
535 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
536 ttm->swap_storage = NULL;
540 if (!persistant_swap_storage) {
541 swap_storage = shmem_file_setup("ttm swap",
542 ttm->num_pages << PAGE_SHIFT,
544 if (unlikely(IS_ERR(swap_storage))) {
545 printk(KERN_ERR "Failed allocating swap storage.\n");
546 return PTR_ERR(swap_storage);
549 swap_storage = persistant_swap_storage;
551 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
553 for (i = 0; i < ttm->num_pages; ++i) {
554 from_page = ttm->pages[i];
555 if (unlikely(from_page == NULL))
557 to_page = read_mapping_page(swap_space, i, NULL);
558 if (unlikely(IS_ERR(to_page))) {
559 ret = PTR_ERR(to_page);
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);
569 set_page_dirty(to_page);
570 mark_page_accessed(to_page);
571 page_cache_release(to_page);
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;
582 if (!persistant_swap_storage)