9b2248a80cffa4d5f1b92c4534c5759db5b0550f
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / ttm / ttm_tt.c
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 "ttm/ttm_module.h"
38 #include "ttm/ttm_bo_driver.h"
39 #include "ttm/ttm_placement.h"
40
41 static int ttm_tt_swapin(struct ttm_tt *ttm);
42
43 #if defined(CONFIG_X86)
44 static void ttm_tt_clflush_page(struct page *page)
45 {
46 uint8_t *page_virtual;
47 unsigned int i;
48
49 if (unlikely(page == NULL))
50 return;
51
52 page_virtual = kmap_atomic(page, KM_USER0);
53
54 for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
55 clflush(page_virtual + i);
56
57 kunmap_atomic(page_virtual, KM_USER0);
58 }
59
60 static void ttm_tt_cache_flush_clflush(struct page *pages[],
61 unsigned long num_pages)
62 {
63 unsigned long i;
64
65 mb();
66 for (i = 0; i < num_pages; ++i)
67 ttm_tt_clflush_page(*pages++);
68 mb();
69 }
70 #elif !defined(__powerpc__)
71 static void ttm_tt_ipi_handler(void *null)
72 {
73 ;
74 }
75 #endif
76
77 void ttm_tt_cache_flush(struct page *pages[], unsigned long num_pages)
78 {
79
80 #if defined(CONFIG_X86)
81 if (cpu_has_clflush) {
82 ttm_tt_cache_flush_clflush(pages, num_pages);
83 return;
84 }
85 #elif defined(__powerpc__)
86 unsigned long i;
87
88 for (i = 0; i < num_pages; ++i) {
89 struct page *page = pages[i];
90 void *page_virtual;
91
92 if (unlikely(page == NULL))
93 continue;
94
95 page_virtual = kmap_atomic(page, KM_USER0);
96 flush_dcache_range((unsigned long) page_virtual,
97 (unsigned long) page_virtual + PAGE_SIZE);
98 kunmap_atomic(page_virtual, KM_USER0);
99 }
100 #else
101 if (on_each_cpu(ttm_tt_ipi_handler, NULL, 1) != 0)
102 printk(KERN_ERR TTM_PFX
103 "Timed out waiting for drm cache flush.\n");
104 #endif
105 }
106
107 /**
108 * Allocates storage for pointers to the pages that back the ttm.
109 *
110 * Uses kmalloc if possible. Otherwise falls back to vmalloc.
111 */
112 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
113 {
114 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
115 ttm->pages = NULL;
116
117 if (size <= PAGE_SIZE)
118 ttm->pages = kzalloc(size, GFP_KERNEL);
119
120 if (!ttm->pages) {
121 ttm->pages = vmalloc_user(size);
122 if (ttm->pages)
123 ttm->page_flags |= TTM_PAGE_FLAG_VMALLOC;
124 }
125 }
126
127 static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
128 {
129 if (ttm->page_flags & TTM_PAGE_FLAG_VMALLOC) {
130 vfree(ttm->pages);
131 ttm->page_flags &= ~TTM_PAGE_FLAG_VMALLOC;
132 } else {
133 kfree(ttm->pages);
134 }
135 ttm->pages = NULL;
136 }
137
138 static struct page *ttm_tt_alloc_page(unsigned page_flags)
139 {
140 gfp_t gfp_flags = GFP_HIGHUSER;
141
142 if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
143 gfp_flags |= __GFP_ZERO;
144
145 if (page_flags & TTM_PAGE_FLAG_DMA32)
146 gfp_flags |= __GFP_DMA32;
147
148 return alloc_page(gfp_flags);
149 }
150
151 static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
152 {
153 int write;
154 int dirty;
155 struct page *page;
156 int i;
157 struct ttm_backend *be = ttm->be;
158
159 BUG_ON(!(ttm->page_flags & TTM_PAGE_FLAG_USER));
160 write = ((ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0);
161 dirty = ((ttm->page_flags & TTM_PAGE_FLAG_USER_DIRTY) != 0);
162
163 if (be)
164 be->func->clear(be);
165
166 for (i = 0; i < ttm->num_pages; ++i) {
167 page = ttm->pages[i];
168 if (page == NULL)
169 continue;
170
171 if (page == ttm->dummy_read_page) {
172 BUG_ON(write);
173 continue;
174 }
175
176 if (write && dirty && !PageReserved(page))
177 set_page_dirty_lock(page);
178
179 ttm->pages[i] = NULL;
180 ttm_mem_global_free(ttm->bdev->mem_glob, PAGE_SIZE, false);
181 put_page(page);
182 }
183 ttm->state = tt_unpopulated;
184 ttm->first_himem_page = ttm->num_pages;
185 ttm->last_lomem_page = -1;
186 }
187
188 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
189 {
190 struct page *p;
191 struct ttm_bo_device *bdev = ttm->bdev;
192 struct ttm_mem_global *mem_glob = bdev->mem_glob;
193 int ret;
194
195 while (NULL == (p = ttm->pages[index])) {
196 p = ttm_tt_alloc_page(ttm->page_flags);
197
198 if (!p)
199 return NULL;
200
201 if (PageHighMem(p)) {
202 ret =
203 ttm_mem_global_alloc(mem_glob, PAGE_SIZE,
204 false, false, true);
205 if (unlikely(ret != 0))
206 goto out_err;
207 ttm->pages[--ttm->first_himem_page] = p;
208 } else {
209 ret =
210 ttm_mem_global_alloc(mem_glob, PAGE_SIZE,
211 false, false, false);
212 if (unlikely(ret != 0))
213 goto out_err;
214 ttm->pages[++ttm->last_lomem_page] = p;
215 }
216 }
217 return p;
218 out_err:
219 put_page(p);
220 return NULL;
221 }
222
223 struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
224 {
225 int ret;
226
227 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
228 ret = ttm_tt_swapin(ttm);
229 if (unlikely(ret != 0))
230 return NULL;
231 }
232 return __ttm_tt_get_page(ttm, index);
233 }
234
235 int ttm_tt_populate(struct ttm_tt *ttm)
236 {
237 struct page *page;
238 unsigned long i;
239 struct ttm_backend *be;
240 int ret;
241
242 if (ttm->state != tt_unpopulated)
243 return 0;
244
245 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
246 ret = ttm_tt_swapin(ttm);
247 if (unlikely(ret != 0))
248 return ret;
249 }
250
251 be = ttm->be;
252
253 for (i = 0; i < ttm->num_pages; ++i) {
254 page = __ttm_tt_get_page(ttm, i);
255 if (!page)
256 return -ENOMEM;
257 }
258
259 be->func->populate(be, ttm->num_pages, ttm->pages,
260 ttm->dummy_read_page);
261 ttm->state = tt_unbound;
262 return 0;
263 }
264
265 #ifdef CONFIG_X86
266 static inline int ttm_tt_set_page_caching(struct page *p,
267 enum ttm_caching_state c_state)
268 {
269 if (PageHighMem(p))
270 return 0;
271
272 switch (c_state) {
273 case tt_cached:
274 return set_pages_wb(p, 1);
275 case tt_wc:
276 return set_memory_wc((unsigned long) page_address(p), 1);
277 default:
278 return set_pages_uc(p, 1);
279 }
280 }
281 #else /* CONFIG_X86 */
282 static inline int ttm_tt_set_page_caching(struct page *p,
283 enum ttm_caching_state c_state)
284 {
285 return 0;
286 }
287 #endif /* CONFIG_X86 */
288
289 /*
290 * Change caching policy for the linear kernel map
291 * for range of pages in a ttm.
292 */
293
294 static int ttm_tt_set_caching(struct ttm_tt *ttm,
295 enum ttm_caching_state c_state)
296 {
297 int i, j;
298 struct page *cur_page;
299 int ret;
300
301 if (ttm->caching_state == c_state)
302 return 0;
303
304 if (c_state != tt_cached) {
305 ret = ttm_tt_populate(ttm);
306 if (unlikely(ret != 0))
307 return ret;
308 }
309
310 if (ttm->caching_state == tt_cached)
311 ttm_tt_cache_flush(ttm->pages, ttm->num_pages);
312
313 for (i = 0; i < ttm->num_pages; ++i) {
314 cur_page = ttm->pages[i];
315 if (likely(cur_page != NULL)) {
316 ret = ttm_tt_set_page_caching(cur_page, c_state);
317 if (unlikely(ret != 0))
318 goto out_err;
319 }
320 }
321
322 ttm->caching_state = c_state;
323
324 return 0;
325
326 out_err:
327 for (j = 0; j < i; ++j) {
328 cur_page = ttm->pages[j];
329 if (likely(cur_page != NULL)) {
330 (void)ttm_tt_set_page_caching(cur_page,
331 ttm->caching_state);
332 }
333 }
334
335 return ret;
336 }
337
338 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
339 {
340 enum ttm_caching_state state;
341
342 if (placement & TTM_PL_FLAG_WC)
343 state = tt_wc;
344 else if (placement & TTM_PL_FLAG_UNCACHED)
345 state = tt_uncached;
346 else
347 state = tt_cached;
348
349 return ttm_tt_set_caching(ttm, state);
350 }
351
352 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
353 {
354 int i;
355 struct page *cur_page;
356 struct ttm_backend *be = ttm->be;
357
358 if (be)
359 be->func->clear(be);
360 (void)ttm_tt_set_caching(ttm, tt_cached);
361 for (i = 0; i < ttm->num_pages; ++i) {
362 cur_page = ttm->pages[i];
363 ttm->pages[i] = NULL;
364 if (cur_page) {
365 if (page_count(cur_page) != 1)
366 printk(KERN_ERR TTM_PFX
367 "Erroneous page count. "
368 "Leaking pages.\n");
369 ttm_mem_global_free(ttm->bdev->mem_glob, PAGE_SIZE,
370 PageHighMem(cur_page));
371 __free_page(cur_page);
372 }
373 }
374 ttm->state = tt_unpopulated;
375 ttm->first_himem_page = ttm->num_pages;
376 ttm->last_lomem_page = -1;
377 }
378
379 void ttm_tt_destroy(struct ttm_tt *ttm)
380 {
381 struct ttm_backend *be;
382
383 if (unlikely(ttm == NULL))
384 return;
385
386 be = ttm->be;
387 if (likely(be != NULL)) {
388 be->func->destroy(be);
389 ttm->be = NULL;
390 }
391
392 if (likely(ttm->pages != NULL)) {
393 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
394 ttm_tt_free_user_pages(ttm);
395 else
396 ttm_tt_free_alloced_pages(ttm);
397
398 ttm_tt_free_page_directory(ttm);
399 }
400
401 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
402 ttm->swap_storage)
403 fput(ttm->swap_storage);
404
405 kfree(ttm);
406 }
407
408 int ttm_tt_set_user(struct ttm_tt *ttm,
409 struct task_struct *tsk,
410 unsigned long start, unsigned long num_pages)
411 {
412 struct mm_struct *mm = tsk->mm;
413 int ret;
414 int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
415 struct ttm_mem_global *mem_glob = ttm->bdev->mem_glob;
416
417 BUG_ON(num_pages != ttm->num_pages);
418 BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
419
420 /**
421 * Account user pages as lowmem pages for now.
422 */
423
424 ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
425 false, false, false);
426 if (unlikely(ret != 0))
427 return ret;
428
429 down_read(&mm->mmap_sem);
430 ret = get_user_pages(tsk, mm, start, num_pages,
431 write, 0, ttm->pages, NULL);
432 up_read(&mm->mmap_sem);
433
434 if (ret != num_pages && write) {
435 ttm_tt_free_user_pages(ttm);
436 ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE, false);
437 return -ENOMEM;
438 }
439
440 ttm->tsk = tsk;
441 ttm->start = start;
442 ttm->state = tt_unbound;
443
444 return 0;
445 }
446
447 struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
448 uint32_t page_flags, struct page *dummy_read_page)
449 {
450 struct ttm_bo_driver *bo_driver = bdev->driver;
451 struct ttm_tt *ttm;
452
453 if (!bo_driver)
454 return NULL;
455
456 ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
457 if (!ttm)
458 return NULL;
459
460 ttm->bdev = bdev;
461
462 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
463 ttm->first_himem_page = ttm->num_pages;
464 ttm->last_lomem_page = -1;
465 ttm->caching_state = tt_cached;
466 ttm->page_flags = page_flags;
467
468 ttm->dummy_read_page = dummy_read_page;
469
470 ttm_tt_alloc_page_directory(ttm);
471 if (!ttm->pages) {
472 ttm_tt_destroy(ttm);
473 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
474 return NULL;
475 }
476 ttm->be = bo_driver->create_ttm_backend_entry(bdev);
477 if (!ttm->be) {
478 ttm_tt_destroy(ttm);
479 printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
480 return NULL;
481 }
482 ttm->state = tt_unpopulated;
483 return ttm;
484 }
485
486 void ttm_tt_unbind(struct ttm_tt *ttm)
487 {
488 int ret;
489 struct ttm_backend *be = ttm->be;
490
491 if (ttm->state == tt_bound) {
492 ret = be->func->unbind(be);
493 BUG_ON(ret);
494 ttm->state = tt_unbound;
495 }
496 }
497
498 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
499 {
500 int ret = 0;
501 struct ttm_backend *be;
502
503 if (!ttm)
504 return -EINVAL;
505
506 if (ttm->state == tt_bound)
507 return 0;
508
509 be = ttm->be;
510
511 ret = ttm_tt_populate(ttm);
512 if (ret)
513 return ret;
514
515 ret = be->func->bind(be, bo_mem);
516 if (ret) {
517 printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
518 return ret;
519 }
520
521 ttm->state = tt_bound;
522
523 if (ttm->page_flags & TTM_PAGE_FLAG_USER)
524 ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
525 return 0;
526 }
527 EXPORT_SYMBOL(ttm_tt_bind);
528
529 static int ttm_tt_swapin(struct ttm_tt *ttm)
530 {
531 struct address_space *swap_space;
532 struct file *swap_storage;
533 struct page *from_page;
534 struct page *to_page;
535 void *from_virtual;
536 void *to_virtual;
537 int i;
538 int ret;
539
540 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
541 ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
542 ttm->num_pages);
543 if (unlikely(ret != 0))
544 return ret;
545
546 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
547 return 0;
548 }
549
550 swap_storage = ttm->swap_storage;
551 BUG_ON(swap_storage == NULL);
552
553 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
554
555 for (i = 0; i < ttm->num_pages; ++i) {
556 from_page = read_mapping_page(swap_space, i, NULL);
557 if (IS_ERR(from_page))
558 goto out_err;
559 to_page = __ttm_tt_get_page(ttm, i);
560 if (unlikely(to_page == NULL))
561 goto out_err;
562
563 preempt_disable();
564 from_virtual = kmap_atomic(from_page, KM_USER0);
565 to_virtual = kmap_atomic(to_page, KM_USER1);
566 memcpy(to_virtual, from_virtual, PAGE_SIZE);
567 kunmap_atomic(to_virtual, KM_USER1);
568 kunmap_atomic(from_virtual, KM_USER0);
569 preempt_enable();
570 page_cache_release(from_page);
571 }
572
573 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
574 fput(swap_storage);
575 ttm->swap_storage = NULL;
576 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
577
578 return 0;
579 out_err:
580 ttm_tt_free_alloced_pages(ttm);
581 return -ENOMEM;
582 }
583
584 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage)
585 {
586 struct address_space *swap_space;
587 struct file *swap_storage;
588 struct page *from_page;
589 struct page *to_page;
590 void *from_virtual;
591 void *to_virtual;
592 int i;
593
594 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
595 BUG_ON(ttm->caching_state != tt_cached);
596
597 /*
598 * For user buffers, just unpin the pages, as there should be
599 * vma references.
600 */
601
602 if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
603 ttm_tt_free_user_pages(ttm);
604 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
605 ttm->swap_storage = NULL;
606 return 0;
607 }
608
609 if (!persistant_swap_storage) {
610 swap_storage = shmem_file_setup("ttm swap",
611 ttm->num_pages << PAGE_SHIFT,
612 0);
613 if (unlikely(IS_ERR(swap_storage))) {
614 printk(KERN_ERR "Failed allocating swap storage.\n");
615 return -ENOMEM;
616 }
617 } else
618 swap_storage = persistant_swap_storage;
619
620 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
621
622 for (i = 0; i < ttm->num_pages; ++i) {
623 from_page = ttm->pages[i];
624 if (unlikely(from_page == NULL))
625 continue;
626 to_page = read_mapping_page(swap_space, i, NULL);
627 if (unlikely(to_page == NULL))
628 goto out_err;
629
630 preempt_disable();
631 from_virtual = kmap_atomic(from_page, KM_USER0);
632 to_virtual = kmap_atomic(to_page, KM_USER1);
633 memcpy(to_virtual, from_virtual, PAGE_SIZE);
634 kunmap_atomic(to_virtual, KM_USER1);
635 kunmap_atomic(from_virtual, KM_USER0);
636 preempt_enable();
637 set_page_dirty(to_page);
638 mark_page_accessed(to_page);
639 page_cache_release(to_page);
640 }
641
642 ttm_tt_free_alloced_pages(ttm);
643 ttm->swap_storage = swap_storage;
644 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
645 if (persistant_swap_storage)
646 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP;
647
648 return 0;
649 out_err:
650 if (!persistant_swap_storage)
651 fput(swap_storage);
652
653 return -ENOMEM;
654 }