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