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