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/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"
41 static int ttm_tt_swapin(struct ttm_tt
*ttm
);
43 #if defined(CONFIG_X86)
44 static void ttm_tt_clflush_page(struct page
*page
)
46 uint8_t *page_virtual
;
49 if (unlikely(page
== NULL
))
52 page_virtual
= kmap_atomic(page
, KM_USER0
);
54 for (i
= 0; i
< PAGE_SIZE
; i
+= boot_cpu_data
.x86_clflush_size
)
55 clflush(page_virtual
+ i
);
57 kunmap_atomic(page_virtual
, KM_USER0
);
60 static void ttm_tt_cache_flush_clflush(struct page
*pages
[],
61 unsigned long num_pages
)
66 for (i
= 0; i
< num_pages
; ++i
)
67 ttm_tt_clflush_page(*pages
++);
70 #elif !defined(__powerpc__)
71 static void ttm_tt_ipi_handler(void *null
)
77 void ttm_tt_cache_flush(struct page
*pages
[], unsigned long num_pages
)
80 #if defined(CONFIG_X86)
81 if (cpu_has_clflush
) {
82 ttm_tt_cache_flush_clflush(pages
, num_pages
);
85 #elif defined(__powerpc__)
88 for (i
= 0; i
< num_pages
; ++i
) {
89 struct page
*page
= pages
[i
];
92 if (unlikely(page
== NULL
))
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
);
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");
108 * Allocates storage for pointers to the pages that back the ttm.
110 * Uses kmalloc if possible. Otherwise falls back to vmalloc.
112 static void ttm_tt_alloc_page_directory(struct ttm_tt
*ttm
)
114 unsigned long size
= ttm
->num_pages
* sizeof(*ttm
->pages
);
117 if (size
<= PAGE_SIZE
)
118 ttm
->pages
= kzalloc(size
, GFP_KERNEL
);
121 ttm
->pages
= vmalloc_user(size
);
123 ttm
->page_flags
|= TTM_PAGE_FLAG_VMALLOC
;
127 static void ttm_tt_free_page_directory(struct ttm_tt
*ttm
)
129 if (ttm
->page_flags
& TTM_PAGE_FLAG_VMALLOC
) {
131 ttm
->page_flags
&= ~TTM_PAGE_FLAG_VMALLOC
;
138 static struct page
*ttm_tt_alloc_page(unsigned page_flags
)
140 gfp_t gfp_flags
= GFP_USER
;
142 if (page_flags
& TTM_PAGE_FLAG_ZERO_ALLOC
)
143 gfp_flags
|= __GFP_ZERO
;
145 if (page_flags
& TTM_PAGE_FLAG_DMA32
)
146 gfp_flags
|= __GFP_DMA32
;
148 gfp_flags
|= __GFP_HIGHMEM
;
150 return alloc_page(gfp_flags
);
153 static void ttm_tt_free_user_pages(struct ttm_tt
*ttm
)
159 struct ttm_backend
*be
= ttm
->be
;
161 BUG_ON(!(ttm
->page_flags
& TTM_PAGE_FLAG_USER
));
162 write
= ((ttm
->page_flags
& TTM_PAGE_FLAG_WRITE
) != 0);
163 dirty
= ((ttm
->page_flags
& TTM_PAGE_FLAG_USER_DIRTY
) != 0);
168 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
169 page
= ttm
->pages
[i
];
173 if (page
== ttm
->dummy_read_page
) {
178 if (write
&& dirty
&& !PageReserved(page
))
179 set_page_dirty_lock(page
);
181 ttm
->pages
[i
] = NULL
;
182 ttm_mem_global_free(ttm
->bdev
->mem_glob
, PAGE_SIZE
, false);
185 ttm
->state
= tt_unpopulated
;
186 ttm
->first_himem_page
= ttm
->num_pages
;
187 ttm
->last_lomem_page
= -1;
190 static struct page
*__ttm_tt_get_page(struct ttm_tt
*ttm
, int index
)
193 struct ttm_bo_device
*bdev
= ttm
->bdev
;
194 struct ttm_mem_global
*mem_glob
= bdev
->mem_glob
;
197 while (NULL
== (p
= ttm
->pages
[index
])) {
198 p
= ttm_tt_alloc_page(ttm
->page_flags
);
203 if (PageHighMem(p
)) {
205 ttm_mem_global_alloc(mem_glob
, PAGE_SIZE
,
207 if (unlikely(ret
!= 0))
209 ttm
->pages
[--ttm
->first_himem_page
] = p
;
212 ttm_mem_global_alloc(mem_glob
, PAGE_SIZE
,
213 false, false, false);
214 if (unlikely(ret
!= 0))
216 ttm
->pages
[++ttm
->last_lomem_page
] = p
;
225 struct page
*ttm_tt_get_page(struct ttm_tt
*ttm
, int index
)
229 if (unlikely(ttm
->page_flags
& TTM_PAGE_FLAG_SWAPPED
)) {
230 ret
= ttm_tt_swapin(ttm
);
231 if (unlikely(ret
!= 0))
234 return __ttm_tt_get_page(ttm
, index
);
237 int ttm_tt_populate(struct ttm_tt
*ttm
)
241 struct ttm_backend
*be
;
244 if (ttm
->state
!= tt_unpopulated
)
247 if (unlikely(ttm
->page_flags
& TTM_PAGE_FLAG_SWAPPED
)) {
248 ret
= ttm_tt_swapin(ttm
);
249 if (unlikely(ret
!= 0))
255 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
256 page
= __ttm_tt_get_page(ttm
, i
);
261 be
->func
->populate(be
, ttm
->num_pages
, ttm
->pages
,
262 ttm
->dummy_read_page
);
263 ttm
->state
= tt_unbound
;
268 static inline int ttm_tt_set_page_caching(struct page
*p
,
269 enum ttm_caching_state c_state
)
276 return set_pages_wb(p
, 1);
278 return set_memory_wc((unsigned long) page_address(p
), 1);
280 return set_pages_uc(p
, 1);
283 #else /* CONFIG_X86 */
284 static inline int ttm_tt_set_page_caching(struct page
*p
,
285 enum ttm_caching_state c_state
)
289 #endif /* CONFIG_X86 */
292 * Change caching policy for the linear kernel map
293 * for range of pages in a ttm.
296 static int ttm_tt_set_caching(struct ttm_tt
*ttm
,
297 enum ttm_caching_state c_state
)
300 struct page
*cur_page
;
303 if (ttm
->caching_state
== c_state
)
306 if (c_state
!= tt_cached
) {
307 ret
= ttm_tt_populate(ttm
);
308 if (unlikely(ret
!= 0))
312 if (ttm
->caching_state
== tt_cached
)
313 ttm_tt_cache_flush(ttm
->pages
, ttm
->num_pages
);
315 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
316 cur_page
= ttm
->pages
[i
];
317 if (likely(cur_page
!= NULL
)) {
318 ret
= ttm_tt_set_page_caching(cur_page
, c_state
);
319 if (unlikely(ret
!= 0))
324 ttm
->caching_state
= c_state
;
329 for (j
= 0; j
< i
; ++j
) {
330 cur_page
= ttm
->pages
[j
];
331 if (likely(cur_page
!= NULL
)) {
332 (void)ttm_tt_set_page_caching(cur_page
,
340 int ttm_tt_set_placement_caching(struct ttm_tt
*ttm
, uint32_t placement
)
342 enum ttm_caching_state state
;
344 if (placement
& TTM_PL_FLAG_WC
)
346 else if (placement
& TTM_PL_FLAG_UNCACHED
)
351 return ttm_tt_set_caching(ttm
, state
);
354 static void ttm_tt_free_alloced_pages(struct ttm_tt
*ttm
)
357 struct page
*cur_page
;
358 struct ttm_backend
*be
= ttm
->be
;
362 (void)ttm_tt_set_caching(ttm
, tt_cached
);
363 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
364 cur_page
= ttm
->pages
[i
];
365 ttm
->pages
[i
] = NULL
;
367 if (page_count(cur_page
) != 1)
368 printk(KERN_ERR TTM_PFX
369 "Erroneous page count. "
371 ttm_mem_global_free(ttm
->bdev
->mem_glob
, PAGE_SIZE
,
372 PageHighMem(cur_page
));
373 __free_page(cur_page
);
376 ttm
->state
= tt_unpopulated
;
377 ttm
->first_himem_page
= ttm
->num_pages
;
378 ttm
->last_lomem_page
= -1;
381 void ttm_tt_destroy(struct ttm_tt
*ttm
)
383 struct ttm_backend
*be
;
385 if (unlikely(ttm
== NULL
))
389 if (likely(be
!= NULL
)) {
390 be
->func
->destroy(be
);
394 if (likely(ttm
->pages
!= NULL
)) {
395 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
)
396 ttm_tt_free_user_pages(ttm
);
398 ttm_tt_free_alloced_pages(ttm
);
400 ttm_tt_free_page_directory(ttm
);
403 if (!(ttm
->page_flags
& TTM_PAGE_FLAG_PERSISTANT_SWAP
) &&
405 fput(ttm
->swap_storage
);
410 int ttm_tt_set_user(struct ttm_tt
*ttm
,
411 struct task_struct
*tsk
,
412 unsigned long start
, unsigned long num_pages
)
414 struct mm_struct
*mm
= tsk
->mm
;
416 int write
= (ttm
->page_flags
& TTM_PAGE_FLAG_WRITE
) != 0;
417 struct ttm_mem_global
*mem_glob
= ttm
->bdev
->mem_glob
;
419 BUG_ON(num_pages
!= ttm
->num_pages
);
420 BUG_ON((ttm
->page_flags
& TTM_PAGE_FLAG_USER
) == 0);
423 * Account user pages as lowmem pages for now.
426 ret
= ttm_mem_global_alloc(mem_glob
, num_pages
* PAGE_SIZE
,
427 false, false, false);
428 if (unlikely(ret
!= 0))
431 down_read(&mm
->mmap_sem
);
432 ret
= get_user_pages(tsk
, mm
, start
, num_pages
,
433 write
, 0, ttm
->pages
, NULL
);
434 up_read(&mm
->mmap_sem
);
436 if (ret
!= num_pages
&& write
) {
437 ttm_tt_free_user_pages(ttm
);
438 ttm_mem_global_free(mem_glob
, num_pages
* PAGE_SIZE
, false);
444 ttm
->state
= tt_unbound
;
449 struct ttm_tt
*ttm_tt_create(struct ttm_bo_device
*bdev
, unsigned long size
,
450 uint32_t page_flags
, struct page
*dummy_read_page
)
452 struct ttm_bo_driver
*bo_driver
= bdev
->driver
;
458 ttm
= kzalloc(sizeof(*ttm
), GFP_KERNEL
);
464 ttm
->num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
465 ttm
->first_himem_page
= ttm
->num_pages
;
466 ttm
->last_lomem_page
= -1;
467 ttm
->caching_state
= tt_cached
;
468 ttm
->page_flags
= page_flags
;
470 ttm
->dummy_read_page
= dummy_read_page
;
472 ttm_tt_alloc_page_directory(ttm
);
475 printk(KERN_ERR TTM_PFX
"Failed allocating page table\n");
478 ttm
->be
= bo_driver
->create_ttm_backend_entry(bdev
);
481 printk(KERN_ERR TTM_PFX
"Failed creating ttm backend entry\n");
484 ttm
->state
= tt_unpopulated
;
488 void ttm_tt_unbind(struct ttm_tt
*ttm
)
491 struct ttm_backend
*be
= ttm
->be
;
493 if (ttm
->state
== tt_bound
) {
494 ret
= be
->func
->unbind(be
);
496 ttm
->state
= tt_unbound
;
500 int ttm_tt_bind(struct ttm_tt
*ttm
, struct ttm_mem_reg
*bo_mem
)
503 struct ttm_backend
*be
;
508 if (ttm
->state
== tt_bound
)
513 ret
= ttm_tt_populate(ttm
);
517 ret
= be
->func
->bind(be
, bo_mem
);
519 printk(KERN_ERR TTM_PFX
"Couldn't bind backend.\n");
523 ttm
->state
= tt_bound
;
525 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
)
526 ttm
->page_flags
|= TTM_PAGE_FLAG_USER_DIRTY
;
529 EXPORT_SYMBOL(ttm_tt_bind
);
531 static int ttm_tt_swapin(struct ttm_tt
*ttm
)
533 struct address_space
*swap_space
;
534 struct file
*swap_storage
;
535 struct page
*from_page
;
536 struct page
*to_page
;
542 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
) {
543 ret
= ttm_tt_set_user(ttm
, ttm
->tsk
, ttm
->start
,
545 if (unlikely(ret
!= 0))
548 ttm
->page_flags
&= ~TTM_PAGE_FLAG_SWAPPED
;
552 swap_storage
= ttm
->swap_storage
;
553 BUG_ON(swap_storage
== NULL
);
555 swap_space
= swap_storage
->f_path
.dentry
->d_inode
->i_mapping
;
557 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
558 from_page
= read_mapping_page(swap_space
, i
, NULL
);
559 if (IS_ERR(from_page
))
561 to_page
= __ttm_tt_get_page(ttm
, i
);
562 if (unlikely(to_page
== NULL
))
566 from_virtual
= kmap_atomic(from_page
, KM_USER0
);
567 to_virtual
= kmap_atomic(to_page
, KM_USER1
);
568 memcpy(to_virtual
, from_virtual
, PAGE_SIZE
);
569 kunmap_atomic(to_virtual
, KM_USER1
);
570 kunmap_atomic(from_virtual
, KM_USER0
);
572 page_cache_release(from_page
);
575 if (!(ttm
->page_flags
& TTM_PAGE_FLAG_PERSISTANT_SWAP
))
577 ttm
->swap_storage
= NULL
;
578 ttm
->page_flags
&= ~TTM_PAGE_FLAG_SWAPPED
;
582 ttm_tt_free_alloced_pages(ttm
);
586 int ttm_tt_swapout(struct ttm_tt
*ttm
, struct file
*persistant_swap_storage
)
588 struct address_space
*swap_space
;
589 struct file
*swap_storage
;
590 struct page
*from_page
;
591 struct page
*to_page
;
596 BUG_ON(ttm
->state
!= tt_unbound
&& ttm
->state
!= tt_unpopulated
);
597 BUG_ON(ttm
->caching_state
!= tt_cached
);
600 * For user buffers, just unpin the pages, as there should be
604 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
) {
605 ttm_tt_free_user_pages(ttm
);
606 ttm
->page_flags
|= TTM_PAGE_FLAG_SWAPPED
;
607 ttm
->swap_storage
= NULL
;
611 if (!persistant_swap_storage
) {
612 swap_storage
= shmem_file_setup("ttm swap",
613 ttm
->num_pages
<< PAGE_SHIFT
,
615 if (unlikely(IS_ERR(swap_storage
))) {
616 printk(KERN_ERR
"Failed allocating swap storage.\n");
620 swap_storage
= persistant_swap_storage
;
622 swap_space
= swap_storage
->f_path
.dentry
->d_inode
->i_mapping
;
624 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
625 from_page
= ttm
->pages
[i
];
626 if (unlikely(from_page
== NULL
))
628 to_page
= read_mapping_page(swap_space
, i
, NULL
);
629 if (unlikely(to_page
== NULL
))
633 from_virtual
= kmap_atomic(from_page
, KM_USER0
);
634 to_virtual
= kmap_atomic(to_page
, KM_USER1
);
635 memcpy(to_virtual
, from_virtual
, PAGE_SIZE
);
636 kunmap_atomic(to_virtual
, KM_USER1
);
637 kunmap_atomic(from_virtual
, KM_USER0
);
639 set_page_dirty(to_page
);
640 mark_page_accessed(to_page
);
641 page_cache_release(to_page
);
644 ttm_tt_free_alloced_pages(ttm
);
645 ttm
->swap_storage
= swap_storage
;
646 ttm
->page_flags
|= TTM_PAGE_FLAG_SWAPPED
;
647 if (persistant_swap_storage
)
648 ttm
->page_flags
|= TTM_PAGE_FLAG_PERSISTANT_SWAP
;
652 if (!persistant_swap_storage
)