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/sched.h>
32 #include <linux/highmem.h>
33 #include <linux/pagemap.h>
34 #include <linux/shmem_fs.h>
35 #include <linux/file.h>
36 #include <linux/swap.h>
37 #include <linux/slab.h>
38 #include <linux/export.h>
39 #include "drm_cache.h"
40 #include "drm_mem_util.h"
41 #include "ttm/ttm_module.h"
42 #include "ttm/ttm_bo_driver.h"
43 #include "ttm/ttm_placement.h"
44 #include "ttm/ttm_page_alloc.h"
46 static int ttm_tt_swapin(struct ttm_tt
*ttm
);
49 * Allocates storage for pointers to the pages that back the ttm.
51 static void ttm_tt_alloc_page_directory(struct ttm_tt
*ttm
)
53 ttm
->pages
= drm_calloc_large(ttm
->num_pages
, sizeof(*ttm
->pages
));
54 ttm
->dma_address
= drm_calloc_large(ttm
->num_pages
,
55 sizeof(*ttm
->dma_address
));
58 static void ttm_tt_free_page_directory(struct ttm_tt
*ttm
)
60 drm_free_large(ttm
->pages
);
62 drm_free_large(ttm
->dma_address
);
63 ttm
->dma_address
= NULL
;
66 static void ttm_tt_free_user_pages(struct ttm_tt
*ttm
)
72 struct ttm_backend
*be
= ttm
->be
;
74 BUG_ON(!(ttm
->page_flags
& TTM_PAGE_FLAG_USER
));
75 write
= ((ttm
->page_flags
& TTM_PAGE_FLAG_WRITE
) != 0);
76 dirty
= ((ttm
->page_flags
& TTM_PAGE_FLAG_USER_DIRTY
) != 0);
81 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
86 if (page
== ttm
->dummy_read_page
) {
91 if (write
&& dirty
&& !PageReserved(page
))
92 set_page_dirty_lock(page
);
95 ttm_mem_global_free(ttm
->glob
->mem_glob
, PAGE_SIZE
);
98 ttm
->state
= tt_unpopulated
;
99 ttm
->first_himem_page
= ttm
->num_pages
;
100 ttm
->last_lomem_page
= -1;
103 static struct page
*__ttm_tt_get_page(struct ttm_tt
*ttm
, int index
)
107 struct ttm_mem_global
*mem_glob
= ttm
->glob
->mem_glob
;
110 while (NULL
== (p
= ttm
->pages
[index
])) {
114 ret
= ttm_get_pages(&h
, ttm
->page_flags
, ttm
->caching_state
, 1,
115 &ttm
->dma_address
[index
]);
120 p
= list_first_entry(&h
, struct page
, lru
);
122 ret
= ttm_mem_global_alloc_page(mem_glob
, p
, false, false);
123 if (unlikely(ret
!= 0))
127 ttm
->pages
[--ttm
->first_himem_page
] = p
;
129 ttm
->pages
[++ttm
->last_lomem_page
] = p
;
137 struct page
*ttm_tt_get_page(struct ttm_tt
*ttm
, int index
)
141 if (unlikely(ttm
->page_flags
& TTM_PAGE_FLAG_SWAPPED
)) {
142 ret
= ttm_tt_swapin(ttm
);
143 if (unlikely(ret
!= 0))
146 return __ttm_tt_get_page(ttm
, index
);
149 int ttm_tt_populate(struct ttm_tt
*ttm
)
153 struct ttm_backend
*be
;
156 if (ttm
->state
!= tt_unpopulated
)
159 if (unlikely(ttm
->page_flags
& TTM_PAGE_FLAG_SWAPPED
)) {
160 ret
= ttm_tt_swapin(ttm
);
161 if (unlikely(ret
!= 0))
167 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
168 page
= __ttm_tt_get_page(ttm
, i
);
173 be
->func
->populate(be
, ttm
->num_pages
, ttm
->pages
,
174 ttm
->dummy_read_page
, ttm
->dma_address
);
175 ttm
->state
= tt_unbound
;
178 EXPORT_SYMBOL(ttm_tt_populate
);
181 static inline int ttm_tt_set_page_caching(struct page
*p
,
182 enum ttm_caching_state c_old
,
183 enum ttm_caching_state c_new
)
190 if (c_old
!= tt_cached
) {
191 /* p isn't in the default caching state, set it to
192 * writeback first to free its current memtype. */
194 ret
= set_pages_wb(p
, 1);
200 ret
= set_memory_wc((unsigned long) page_address(p
), 1);
201 else if (c_new
== tt_uncached
)
202 ret
= set_pages_uc(p
, 1);
206 #else /* CONFIG_X86 */
207 static inline int ttm_tt_set_page_caching(struct page
*p
,
208 enum ttm_caching_state c_old
,
209 enum ttm_caching_state c_new
)
213 #endif /* CONFIG_X86 */
216 * Change caching policy for the linear kernel map
217 * for range of pages in a ttm.
220 static int ttm_tt_set_caching(struct ttm_tt
*ttm
,
221 enum ttm_caching_state c_state
)
224 struct page
*cur_page
;
227 if (ttm
->caching_state
== c_state
)
230 if (ttm
->state
== tt_unpopulated
) {
231 /* Change caching but don't populate */
232 ttm
->caching_state
= c_state
;
236 if (ttm
->caching_state
== tt_cached
)
237 drm_clflush_pages(ttm
->pages
, ttm
->num_pages
);
239 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
240 cur_page
= ttm
->pages
[i
];
241 if (likely(cur_page
!= NULL
)) {
242 ret
= ttm_tt_set_page_caching(cur_page
,
245 if (unlikely(ret
!= 0))
250 ttm
->caching_state
= c_state
;
255 for (j
= 0; j
< i
; ++j
) {
256 cur_page
= ttm
->pages
[j
];
257 if (likely(cur_page
!= NULL
)) {
258 (void)ttm_tt_set_page_caching(cur_page
, c_state
,
266 int ttm_tt_set_placement_caching(struct ttm_tt
*ttm
, uint32_t placement
)
268 enum ttm_caching_state state
;
270 if (placement
& TTM_PL_FLAG_WC
)
272 else if (placement
& TTM_PL_FLAG_UNCACHED
)
277 return ttm_tt_set_caching(ttm
, state
);
279 EXPORT_SYMBOL(ttm_tt_set_placement_caching
);
281 static void ttm_tt_free_alloced_pages(struct ttm_tt
*ttm
)
286 struct page
*cur_page
;
287 struct ttm_backend
*be
= ttm
->be
;
293 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
295 cur_page
= ttm
->pages
[i
];
296 ttm
->pages
[i
] = NULL
;
298 if (page_count(cur_page
) != 1)
299 printk(KERN_ERR TTM_PFX
300 "Erroneous page count. "
302 ttm_mem_global_free_page(ttm
->glob
->mem_glob
,
304 list_add(&cur_page
->lru
, &h
);
308 ttm_put_pages(&h
, count
, ttm
->page_flags
, ttm
->caching_state
,
310 ttm
->state
= tt_unpopulated
;
311 ttm
->first_himem_page
= ttm
->num_pages
;
312 ttm
->last_lomem_page
= -1;
315 void ttm_tt_destroy(struct ttm_tt
*ttm
)
317 struct ttm_backend
*be
;
319 if (unlikely(ttm
== NULL
))
323 if (likely(be
!= NULL
)) {
324 be
->func
->destroy(be
);
328 if (likely(ttm
->pages
!= NULL
)) {
329 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
)
330 ttm_tt_free_user_pages(ttm
);
332 ttm_tt_free_alloced_pages(ttm
);
334 ttm_tt_free_page_directory(ttm
);
337 if (!(ttm
->page_flags
& TTM_PAGE_FLAG_PERSISTENT_SWAP
) &&
339 fput(ttm
->swap_storage
);
344 int ttm_tt_set_user(struct ttm_tt
*ttm
,
345 struct task_struct
*tsk
,
346 unsigned long start
, unsigned long num_pages
)
348 struct mm_struct
*mm
= tsk
->mm
;
350 int write
= (ttm
->page_flags
& TTM_PAGE_FLAG_WRITE
) != 0;
351 struct ttm_mem_global
*mem_glob
= ttm
->glob
->mem_glob
;
353 BUG_ON(num_pages
!= ttm
->num_pages
);
354 BUG_ON((ttm
->page_flags
& TTM_PAGE_FLAG_USER
) == 0);
357 * Account user pages as lowmem pages for now.
360 ret
= ttm_mem_global_alloc(mem_glob
, num_pages
* PAGE_SIZE
,
362 if (unlikely(ret
!= 0))
365 down_read(&mm
->mmap_sem
);
366 ret
= get_user_pages(tsk
, mm
, start
, num_pages
,
367 write
, 0, ttm
->pages
, NULL
);
368 up_read(&mm
->mmap_sem
);
370 if (ret
!= num_pages
&& write
) {
371 ttm_tt_free_user_pages(ttm
);
372 ttm_mem_global_free(mem_glob
, num_pages
* PAGE_SIZE
);
378 ttm
->state
= tt_unbound
;
383 struct ttm_tt
*ttm_tt_create(struct ttm_bo_device
*bdev
, unsigned long size
,
384 uint32_t page_flags
, struct page
*dummy_read_page
)
386 struct ttm_bo_driver
*bo_driver
= bdev
->driver
;
392 ttm
= kzalloc(sizeof(*ttm
), GFP_KERNEL
);
396 ttm
->glob
= bdev
->glob
;
397 ttm
->num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
398 ttm
->first_himem_page
= ttm
->num_pages
;
399 ttm
->last_lomem_page
= -1;
400 ttm
->caching_state
= tt_cached
;
401 ttm
->page_flags
= page_flags
;
403 ttm
->dummy_read_page
= dummy_read_page
;
405 ttm_tt_alloc_page_directory(ttm
);
408 printk(KERN_ERR TTM_PFX
"Failed allocating page table\n");
411 ttm
->be
= bo_driver
->create_ttm_backend_entry(bdev
);
414 printk(KERN_ERR TTM_PFX
"Failed creating ttm backend entry\n");
417 ttm
->state
= tt_unpopulated
;
421 void ttm_tt_unbind(struct ttm_tt
*ttm
)
424 struct ttm_backend
*be
= ttm
->be
;
426 if (ttm
->state
== tt_bound
) {
427 ret
= be
->func
->unbind(be
);
429 ttm
->state
= tt_unbound
;
433 int ttm_tt_bind(struct ttm_tt
*ttm
, struct ttm_mem_reg
*bo_mem
)
436 struct ttm_backend
*be
;
441 if (ttm
->state
== tt_bound
)
446 ret
= ttm_tt_populate(ttm
);
450 ret
= be
->func
->bind(be
, bo_mem
);
451 if (unlikely(ret
!= 0))
454 ttm
->state
= tt_bound
;
456 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
)
457 ttm
->page_flags
|= TTM_PAGE_FLAG_USER_DIRTY
;
460 EXPORT_SYMBOL(ttm_tt_bind
);
462 static int ttm_tt_swapin(struct ttm_tt
*ttm
)
464 struct address_space
*swap_space
;
465 struct file
*swap_storage
;
466 struct page
*from_page
;
467 struct page
*to_page
;
473 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
) {
474 ret
= ttm_tt_set_user(ttm
, ttm
->tsk
, ttm
->start
,
476 if (unlikely(ret
!= 0))
479 ttm
->page_flags
&= ~TTM_PAGE_FLAG_SWAPPED
;
483 swap_storage
= ttm
->swap_storage
;
484 BUG_ON(swap_storage
== NULL
);
486 swap_space
= swap_storage
->f_path
.dentry
->d_inode
->i_mapping
;
488 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
489 from_page
= shmem_read_mapping_page(swap_space
, i
);
490 if (IS_ERR(from_page
)) {
491 ret
= PTR_ERR(from_page
);
494 to_page
= __ttm_tt_get_page(ttm
, i
);
495 if (unlikely(to_page
== NULL
))
499 from_virtual
= kmap_atomic(from_page
, KM_USER0
);
500 to_virtual
= kmap_atomic(to_page
, KM_USER1
);
501 memcpy(to_virtual
, from_virtual
, PAGE_SIZE
);
502 kunmap_atomic(to_virtual
, KM_USER1
);
503 kunmap_atomic(from_virtual
, KM_USER0
);
505 page_cache_release(from_page
);
508 if (!(ttm
->page_flags
& TTM_PAGE_FLAG_PERSISTENT_SWAP
))
510 ttm
->swap_storage
= NULL
;
511 ttm
->page_flags
&= ~TTM_PAGE_FLAG_SWAPPED
;
515 ttm_tt_free_alloced_pages(ttm
);
519 int ttm_tt_swapout(struct ttm_tt
*ttm
, struct file
*persistent_swap_storage
)
521 struct address_space
*swap_space
;
522 struct file
*swap_storage
;
523 struct page
*from_page
;
524 struct page
*to_page
;
530 BUG_ON(ttm
->state
!= tt_unbound
&& ttm
->state
!= tt_unpopulated
);
531 BUG_ON(ttm
->caching_state
!= tt_cached
);
534 * For user buffers, just unpin the pages, as there should be
538 if (ttm
->page_flags
& TTM_PAGE_FLAG_USER
) {
539 ttm_tt_free_user_pages(ttm
);
540 ttm
->page_flags
|= TTM_PAGE_FLAG_SWAPPED
;
541 ttm
->swap_storage
= NULL
;
545 if (!persistent_swap_storage
) {
546 swap_storage
= shmem_file_setup("ttm swap",
547 ttm
->num_pages
<< PAGE_SHIFT
,
549 if (unlikely(IS_ERR(swap_storage
))) {
550 printk(KERN_ERR
"Failed allocating swap storage.\n");
551 return PTR_ERR(swap_storage
);
554 swap_storage
= persistent_swap_storage
;
556 swap_space
= swap_storage
->f_path
.dentry
->d_inode
->i_mapping
;
558 for (i
= 0; i
< ttm
->num_pages
; ++i
) {
559 from_page
= ttm
->pages
[i
];
560 if (unlikely(from_page
== NULL
))
562 to_page
= shmem_read_mapping_page(swap_space
, i
);
563 if (unlikely(IS_ERR(to_page
))) {
564 ret
= PTR_ERR(to_page
);
568 from_virtual
= kmap_atomic(from_page
, KM_USER0
);
569 to_virtual
= kmap_atomic(to_page
, KM_USER1
);
570 memcpy(to_virtual
, from_virtual
, PAGE_SIZE
);
571 kunmap_atomic(to_virtual
, KM_USER1
);
572 kunmap_atomic(from_virtual
, KM_USER0
);
574 set_page_dirty(to_page
);
575 mark_page_accessed(to_page
);
576 page_cache_release(to_page
);
579 ttm_tt_free_alloced_pages(ttm
);
580 ttm
->swap_storage
= swap_storage
;
581 ttm
->page_flags
|= TTM_PAGE_FLAG_SWAPPED
;
582 if (persistent_swap_storage
)
583 ttm
->page_flags
|= TTM_PAGE_FLAG_PERSISTENT_SWAP
;
587 if (!persistent_swap_storage
)