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 #define pr_fmt(fmt) "[TTM] " fmt
33 #include <linux/sched.h>
34 #include <linux/highmem.h>
35 #include <linux/pagemap.h>
36 #include <linux/shmem_fs.h>
37 #include <linux/file.h>
38 #include <linux/swap.h>
39 #include <linux/slab.h>
40 #include <linux/export.h>
41 #include <drm/drm_mem_util.h>
42 #include <drm/ttm/ttm_module.h>
43 #include <drm/ttm/ttm_bo_driver.h>
44 #include <drm/ttm/ttm_placement.h>
45 #include <drm/ttm/ttm_page_alloc.h>
48 * Allocates storage for pointers to the pages that back the ttm.
50 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
52 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
55 static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
57 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages,
58 sizeof(*ttm->ttm.pages) +
59 sizeof(*ttm->dma_address));
60 ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
64 static inline int ttm_tt_set_page_caching(struct page *p,
65 enum ttm_caching_state c_old,
66 enum ttm_caching_state c_new)
75 if (c_old != tt_cached) {
76 /* p isn't in the default caching state, set it to
77 * writeback first to free its current memtype. */
79 ret = set_pages_wb(p, 1);
85 pmap_page_set_memattr((struct vm_page *)p, VM_MEMATTR_WRITE_COMBINING);
86 else if (c_new == tt_uncached)
87 pmap_page_set_memattr((struct vm_page *)p, VM_MEMATTR_UNCACHEABLE);
91 #else /* CONFIG_X86 */
92 static inline int ttm_tt_set_page_caching(struct page *p,
93 enum ttm_caching_state c_old,
94 enum ttm_caching_state c_new)
98 #endif /* CONFIG_X86 */
101 * Change caching policy for the linear kernel map
102 * for range of pages in a ttm.
105 static int ttm_tt_set_caching(struct ttm_tt *ttm,
106 enum ttm_caching_state c_state)
109 struct page *cur_page;
112 if (ttm->caching_state == c_state)
115 if (ttm->state == tt_unpopulated) {
116 /* Change caching but don't populate */
117 ttm->caching_state = c_state;
121 if (ttm->caching_state == tt_cached)
122 drm_clflush_pages(ttm->pages, ttm->num_pages);
124 for (i = 0; i < ttm->num_pages; ++i) {
125 cur_page = ttm->pages[i];
126 if (likely(cur_page != NULL)) {
127 ret = ttm_tt_set_page_caching(cur_page,
130 if (unlikely(ret != 0))
135 ttm->caching_state = c_state;
140 for (j = 0; j < i; ++j) {
141 cur_page = ttm->pages[j];
142 if (likely(cur_page != NULL)) {
143 (void)ttm_tt_set_page_caching(cur_page, c_state,
151 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
153 enum ttm_caching_state state;
155 if (placement & TTM_PL_FLAG_WC)
157 else if (placement & TTM_PL_FLAG_UNCACHED)
162 return ttm_tt_set_caching(ttm, state);
164 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
166 void ttm_tt_destroy(struct ttm_tt *ttm)
173 if (ttm->state == tt_unbound)
174 ttm_tt_unpopulate(ttm);
176 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
178 vm_object_deallocate(ttm->swap_storage);
180 ttm->swap_storage = NULL;
181 ttm->func->destroy(ttm);
184 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
185 unsigned long size, uint32_t page_flags,
186 struct page *dummy_read_page)
189 ttm->glob = bdev->glob;
190 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
191 ttm->caching_state = tt_cached;
192 ttm->page_flags = page_flags;
193 ttm->dummy_read_page = dummy_read_page;
194 ttm->state = tt_unpopulated;
195 ttm->swap_storage = NULL;
197 ttm_tt_alloc_page_directory(ttm);
200 pr_err("Failed allocating page table\n");
205 EXPORT_SYMBOL(ttm_tt_init);
207 void ttm_tt_fini(struct ttm_tt *ttm)
209 drm_free_large(ttm->pages);
212 EXPORT_SYMBOL(ttm_tt_fini);
214 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
215 unsigned long size, uint32_t page_flags,
216 struct page *dummy_read_page)
218 struct ttm_tt *ttm = &ttm_dma->ttm;
221 ttm->glob = bdev->glob;
222 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
223 ttm->caching_state = tt_cached;
224 ttm->page_flags = page_flags;
225 ttm->dummy_read_page = dummy_read_page;
226 ttm->state = tt_unpopulated;
227 ttm->swap_storage = NULL;
229 INIT_LIST_HEAD(&ttm_dma->pages_list);
230 ttm_dma_tt_alloc_page_directory(ttm_dma);
233 pr_err("Failed allocating page table\n");
238 EXPORT_SYMBOL(ttm_dma_tt_init);
240 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
242 struct ttm_tt *ttm = &ttm_dma->ttm;
244 drm_free_large(ttm->pages);
246 ttm_dma->dma_address = NULL;
248 EXPORT_SYMBOL(ttm_dma_tt_fini);
250 void ttm_tt_unbind(struct ttm_tt *ttm)
254 if (ttm->state == tt_bound) {
255 ret = ttm->func->unbind(ttm);
257 ttm->state = tt_unbound;
261 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
268 if (ttm->state == tt_bound)
271 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
275 ret = ttm->func->bind(ttm, bo_mem);
276 if (unlikely(ret != 0))
279 ttm->state = tt_bound;
283 EXPORT_SYMBOL(ttm_tt_bind);
285 int ttm_tt_swapin(struct ttm_tt *ttm)
288 struct page *from_page;
289 struct page *to_page;
293 obj = ttm->swap_storage;
296 vm_object_pip_add(obj, 1);
297 for (i = 0; i < ttm->num_pages; ++i) {
298 from_page = (struct page *)vm_page_grab(obj, i, VM_ALLOC_NORMAL |
300 if (((struct vm_page *)from_page)->valid != VM_PAGE_BITS_ALL) {
301 if (vm_pager_has_page(obj, i)) {
302 if (vm_pager_get_page(obj, (struct vm_page **)&from_page, 1) != VM_PAGER_OK) {
303 vm_page_free((struct vm_page *)from_page);
308 vm_page_zero_invalid((struct vm_page *)from_page, TRUE);
311 to_page = ttm->pages[i];
312 if (unlikely(to_page == NULL)) {
313 vm_page_wakeup((struct vm_page *)from_page);
317 pmap_copy_page(VM_PAGE_TO_PHYS((struct vm_page *)from_page),
318 VM_PAGE_TO_PHYS((struct vm_page *)to_page));
319 vm_page_wakeup((struct vm_page *)from_page);
321 vm_object_pip_wakeup(obj);
322 VM_OBJECT_UNLOCK(obj);
324 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
325 vm_object_deallocate(obj);
326 ttm->swap_storage = NULL;
327 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
331 vm_object_pip_wakeup(obj);
332 VM_OBJECT_UNLOCK(obj);
336 int ttm_tt_swapout(struct ttm_tt *ttm, vm_object_t persistent_swap_storage)
339 vm_page_t from_page, to_page;
342 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
343 BUG_ON(ttm->caching_state != tt_cached);
345 if (!persistent_swap_storage) {
346 obj = swap_pager_alloc(NULL,
347 IDX_TO_OFF(ttm->num_pages), VM_PROT_DEFAULT, 0);
349 pr_err("Failed allocating swap storage\n");
353 obj = persistent_swap_storage;
356 vm_object_pip_add(obj, 1);
357 for (i = 0; i < ttm->num_pages; ++i) {
358 from_page = (struct vm_page *)ttm->pages[i];
359 if (unlikely(from_page == NULL))
361 to_page = vm_page_grab(obj, i, VM_ALLOC_NORMAL |
363 pmap_copy_page(VM_PAGE_TO_PHYS(from_page),
364 VM_PAGE_TO_PHYS(to_page));
365 to_page->valid = VM_PAGE_BITS_ALL;
366 vm_page_dirty(to_page);
367 vm_page_wakeup(to_page);
369 vm_object_pip_wakeup(obj);
370 VM_OBJECT_UNLOCK(obj);
372 ttm_tt_unpopulate(ttm);
373 ttm->swap_storage = obj;
374 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
375 if (persistent_swap_storage)
376 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
381 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
385 struct page **page = ttm->pages;
387 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
390 for (i = 0; i < ttm->num_pages; ++i) {
391 (*page)->mapping = NULL;
392 (*page++)->index = 0;
397 void ttm_tt_unpopulate(struct ttm_tt *ttm)
399 if (ttm->state == tt_unpopulated)
402 ttm_tt_clear_mapping(ttm);
403 ttm->bdev->driver->ttm_tt_unpopulate(ttm);