First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / gpu / drm / ttm / ttm_tt.c
bloba55ee1a56c167a0dde9c53fc492deee1a61096bc
1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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);
44 /**
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);
52 ttm->pages = NULL;
54 if (size <= PAGE_SIZE)
55 ttm->pages = kzalloc(size, GFP_KERNEL);
57 if (!ttm->pages) {
58 ttm->pages = vmalloc_user(size);
59 if (ttm->pages)
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) {
67 vfree(ttm->pages);
68 ttm->page_flags &= ~TTM_PAGE_FLAG_VMALLOC;
69 } else {
70 kfree(ttm->pages);
72 ttm->pages = NULL;
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;
84 else
85 gfp_flags |= __GFP_HIGHMEM;
87 return alloc_page(gfp_flags);
90 static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
92 int write;
93 int dirty;
94 struct page *page;
95 int i;
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);
102 if (be)
103 be->func->clear(be);
105 for (i = 0; i < ttm->num_pages; ++i) {
106 page = ttm->pages[i];
107 if (page == NULL)
108 continue;
110 if (page == ttm->dummy_read_page) {
111 BUG_ON(write);
112 continue;
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);
120 put_page(page);
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)
129 struct page *p;
130 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
131 int ret;
133 while (NULL == (p = ttm->pages[index])) {
134 p = ttm_tt_alloc_page(ttm->page_flags);
136 if (!p)
137 return NULL;
139 ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
140 if (unlikely(ret != 0))
141 goto out_err;
143 if (PageHighMem(p))
144 ttm->pages[--ttm->first_himem_page] = p;
145 else
146 ttm->pages[++ttm->last_lomem_page] = p;
148 return p;
149 out_err:
150 put_page(p);
151 return NULL;
154 struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
156 int ret;
158 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
159 ret = ttm_tt_swapin(ttm);
160 if (unlikely(ret != 0))
161 return NULL;
163 return __ttm_tt_get_page(ttm, index);
166 int ttm_tt_populate(struct ttm_tt *ttm)
168 struct page *page;
169 unsigned long i;
170 struct ttm_backend *be;
171 int ret;
173 if (ttm->state != tt_unpopulated)
174 return 0;
176 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
177 ret = ttm_tt_swapin(ttm);
178 if (unlikely(ret != 0))
179 return ret;
182 be = ttm->be;
184 for (i = 0; i < ttm->num_pages; ++i) {
185 page = __ttm_tt_get_page(ttm, i);
186 if (!page)
187 return -ENOMEM;
190 be->func->populate(be, ttm->num_pages, ttm->pages,
191 ttm->dummy_read_page);
192 ttm->state = tt_unbound;
193 return 0;
196 #ifdef CONFIG_X86
197 static inline int ttm_tt_set_page_caching(struct page *p,
198 enum ttm_caching_state c_state)
200 if (PageHighMem(p))
201 return 0;
203 switch (c_state) {
204 case tt_cached:
205 return set_pages_wb(p, 1);
206 case tt_wc:
207 return set_memory_wc((unsigned long) page_address(p), 1);
208 default:
209 return set_pages_uc(p, 1);
212 #else /* CONFIG_X86 */
213 static inline int ttm_tt_set_page_caching(struct page *p,
214 enum ttm_caching_state c_state)
216 return 0;
218 #endif /* CONFIG_X86 */
221 * Change caching policy for the linear kernel map
222 * for range of pages in a ttm.
225 static int ttm_tt_set_caching(struct ttm_tt *ttm,
226 enum ttm_caching_state c_state)
228 int i, j;
229 struct page *cur_page;
230 int ret;
232 if (ttm->caching_state == c_state)
233 return 0;
235 if (c_state != tt_cached) {
236 ret = ttm_tt_populate(ttm);
237 if (unlikely(ret != 0))
238 return ret;
241 if (ttm->caching_state == tt_cached)
242 drm_clflush_pages(ttm->pages, ttm->num_pages);
244 for (i = 0; i < ttm->num_pages; ++i) {
245 cur_page = ttm->pages[i];
246 if (likely(cur_page != NULL)) {
247 ret = ttm_tt_set_page_caching(cur_page, c_state);
248 if (unlikely(ret != 0))
249 goto out_err;
253 ttm->caching_state = c_state;
255 return 0;
257 out_err:
258 for (j = 0; j < i; ++j) {
259 cur_page = ttm->pages[j];
260 if (likely(cur_page != NULL)) {
261 (void)ttm_tt_set_page_caching(cur_page,
262 ttm->caching_state);
266 return ret;
269 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
271 enum ttm_caching_state state;
273 if (placement & TTM_PL_FLAG_WC)
274 state = tt_wc;
275 else if (placement & TTM_PL_FLAG_UNCACHED)
276 state = tt_uncached;
277 else
278 state = tt_cached;
280 return ttm_tt_set_caching(ttm, state);
283 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
285 int i;
286 struct page *cur_page;
287 struct ttm_backend *be = ttm->be;
289 if (be)
290 be->func->clear(be);
291 (void)ttm_tt_set_caching(ttm, tt_cached);
292 for (i = 0; i < ttm->num_pages; ++i) {
293 cur_page = ttm->pages[i];
294 ttm->pages[i] = NULL;
295 if (cur_page) {
296 if (page_count(cur_page) != 1)
297 printk(KERN_ERR TTM_PFX
298 "Erroneous page count. "
299 "Leaking pages.\n");
300 ttm_mem_global_free_page(ttm->glob->mem_glob,
301 cur_page);
302 __free_page(cur_page);
305 ttm->state = tt_unpopulated;
306 ttm->first_himem_page = ttm->num_pages;
307 ttm->last_lomem_page = -1;
310 void ttm_tt_destroy(struct ttm_tt *ttm)
312 struct ttm_backend *be;
314 if (unlikely(ttm == NULL))
315 return;
317 be = ttm->be;
318 if (likely(be != NULL)) {
319 be->func->destroy(be);
320 ttm->be = NULL;
323 if (likely(ttm->pages != NULL)) {
324 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
325 ttm_tt_free_user_pages(ttm);
326 else
327 ttm_tt_free_alloced_pages(ttm);
329 ttm_tt_free_page_directory(ttm);
332 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
333 ttm->swap_storage)
334 fput(ttm->swap_storage);
336 kfree(ttm);
339 int ttm_tt_set_user(struct ttm_tt *ttm,
340 struct task_struct *tsk,
341 unsigned long start, unsigned long num_pages)
343 struct mm_struct *mm = tsk->mm;
344 int ret;
345 int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
346 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
348 BUG_ON(num_pages != ttm->num_pages);
349 BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
352 * Account user pages as lowmem pages for now.
355 ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
356 false, false);
357 if (unlikely(ret != 0))
358 return ret;
360 down_read(&mm->mmap_sem);
361 ret = get_user_pages(tsk, mm, start, num_pages,
362 write, 0, ttm->pages, NULL);
363 up_read(&mm->mmap_sem);
365 if (ret != num_pages && write) {
366 ttm_tt_free_user_pages(ttm);
367 ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
368 return -ENOMEM;
371 ttm->tsk = tsk;
372 ttm->start = start;
373 ttm->state = tt_unbound;
375 return 0;
378 struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
379 uint32_t page_flags, struct page *dummy_read_page)
381 struct ttm_bo_driver *bo_driver = bdev->driver;
382 struct ttm_tt *ttm;
384 if (!bo_driver)
385 return NULL;
387 ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
388 if (!ttm)
389 return NULL;
391 ttm->glob = bdev->glob;
392 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
393 ttm->first_himem_page = ttm->num_pages;
394 ttm->last_lomem_page = -1;
395 ttm->caching_state = tt_cached;
396 ttm->page_flags = page_flags;
398 ttm->dummy_read_page = dummy_read_page;
400 ttm_tt_alloc_page_directory(ttm);
401 if (!ttm->pages) {
402 ttm_tt_destroy(ttm);
403 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
404 return NULL;
406 ttm->be = bo_driver->create_ttm_backend_entry(bdev);
407 if (!ttm->be) {
408 ttm_tt_destroy(ttm);
409 printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
410 return NULL;
412 ttm->state = tt_unpopulated;
413 return ttm;
416 void ttm_tt_unbind(struct ttm_tt *ttm)
418 int ret;
419 struct ttm_backend *be = ttm->be;
421 if (ttm->state == tt_bound) {
422 ret = be->func->unbind(be);
423 BUG_ON(ret);
424 ttm->state = tt_unbound;
428 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
430 int ret = 0;
431 struct ttm_backend *be;
433 if (!ttm)
434 return -EINVAL;
436 if (ttm->state == tt_bound)
437 return 0;
439 be = ttm->be;
441 ret = ttm_tt_populate(ttm);
442 if (ret)
443 return ret;
445 ret = be->func->bind(be, bo_mem);
446 if (ret) {
447 printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
448 return ret;
451 ttm->state = tt_bound;
453 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
454 ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
455 return 0;
457 EXPORT_SYMBOL(ttm_tt_bind);
459 static int ttm_tt_swapin(struct ttm_tt *ttm)
461 struct address_space *swap_space;
462 struct file *swap_storage;
463 struct page *from_page;
464 struct page *to_page;
465 void *from_virtual;
466 void *to_virtual;
467 int i;
468 int ret;
470 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
471 ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
472 ttm->num_pages);
473 if (unlikely(ret != 0))
474 return ret;
476 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
477 return 0;
480 swap_storage = ttm->swap_storage;
481 BUG_ON(swap_storage == NULL);
483 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
485 for (i = 0; i < ttm->num_pages; ++i) {
486 from_page = read_mapping_page(swap_space, i, NULL);
487 if (IS_ERR(from_page))
488 goto out_err;
489 to_page = __ttm_tt_get_page(ttm, i);
490 if (unlikely(to_page == NULL))
491 goto out_err;
493 preempt_disable();
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);
499 preempt_enable();
500 page_cache_release(from_page);
503 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
504 fput(swap_storage);
505 ttm->swap_storage = NULL;
506 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
508 return 0;
509 out_err:
510 ttm_tt_free_alloced_pages(ttm);
511 return -ENOMEM;
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;
520 void *from_virtual;
521 void *to_virtual;
522 int i;
524 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
525 BUG_ON(ttm->caching_state != tt_cached);
528 * For user buffers, just unpin the pages, as there should be
529 * vma references.
532 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
533 ttm_tt_free_user_pages(ttm);
534 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
535 ttm->swap_storage = NULL;
536 return 0;
539 if (!persistant_swap_storage) {
540 swap_storage = shmem_file_setup("ttm swap",
541 ttm->num_pages << PAGE_SHIFT,
543 if (unlikely(IS_ERR(swap_storage))) {
544 printk(KERN_ERR "Failed allocating swap storage.\n");
545 return -ENOMEM;
547 } else
548 swap_storage = persistant_swap_storage;
550 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
552 for (i = 0; i < ttm->num_pages; ++i) {
553 from_page = ttm->pages[i];
554 if (unlikely(from_page == NULL))
555 continue;
556 to_page = read_mapping_page(swap_space, i, NULL);
557 if (unlikely(to_page == NULL))
558 goto out_err;
560 preempt_disable();
561 from_virtual = kmap_atomic(from_page, KM_USER0);
562 to_virtual = kmap_atomic(to_page, KM_USER1);
563 memcpy(to_virtual, from_virtual, PAGE_SIZE);
564 kunmap_atomic(to_virtual, KM_USER1);
565 kunmap_atomic(from_virtual, KM_USER0);
566 preempt_enable();
567 set_page_dirty(to_page);
568 mark_page_accessed(to_page);
569 page_cache_release(to_page);
572 ttm_tt_free_alloced_pages(ttm);
573 ttm->swap_storage = swap_storage;
574 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
575 if (persistant_swap_storage)
576 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP;
578 return 0;
579 out_err:
580 if (!persistant_swap_storage)
581 fput(swap_storage);
583 return -ENOMEM;