blob: 9af99084b344413dcb07c2a368916bff647fc119 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
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
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020033#include <linux/sched.h>
34#include <linux/highmem.h>
35#include <linux/pagemap.h>
Hugh Dickins3142b652011-06-27 16:18:17 -070036#include <linux/shmem_fs.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020037#include <linux/file.h>
38#include <linux/swap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040040#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010041#include <drm/drm_cache.h>
42#include <drm/drm_mem_util.h>
43#include <drm/ttm/ttm_module.h>
44#include <drm/ttm/ttm_bo_driver.h>
45#include <drm/ttm/ttm_placement.h>
46#include <drm/ttm/ttm_page_alloc.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020047
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020048/**
49 * Allocates storage for pointers to the pages that back the ttm.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020050 */
51static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
52{
Jerome Glisse8e7e7052011-11-09 17:15:26 -050053 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020054}
55
Jerome Glisse8e7e7052011-11-09 17:15:26 -050056static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020057{
Jerome Glisse8e7e7052011-11-09 17:15:26 -050058 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
59 ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
60 sizeof(*ttm->dma_address));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020061}
62
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020063#ifdef CONFIG_X86
64static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100065 enum ttm_caching_state c_old,
66 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020067{
Francisco Jerezdb78e272010-01-12 18:49:43 +010068 int ret = 0;
69
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020070 if (PageHighMem(p))
71 return 0;
72
Francisco Jerezf0e2f382010-02-20 07:30:15 +100073 if (c_old != tt_cached) {
Francisco Jerezdb78e272010-01-12 18:49:43 +010074 /* p isn't in the default caching state, set it to
75 * writeback first to free its current memtype. */
76
77 ret = set_pages_wb(p, 1);
78 if (ret)
79 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020080 }
Francisco Jerezdb78e272010-01-12 18:49:43 +010081
Francisco Jerezf0e2f382010-02-20 07:30:15 +100082 if (c_new == tt_wc)
Francisco Jerezdb78e272010-01-12 18:49:43 +010083 ret = set_memory_wc((unsigned long) page_address(p), 1);
Francisco Jerezf0e2f382010-02-20 07:30:15 +100084 else if (c_new == tt_uncached)
Francisco Jerezdb78e272010-01-12 18:49:43 +010085 ret = set_pages_uc(p, 1);
86
87 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020088}
89#else /* CONFIG_X86 */
90static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100091 enum ttm_caching_state c_old,
92 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020093{
94 return 0;
95}
96#endif /* CONFIG_X86 */
97
98/*
99 * Change caching policy for the linear kernel map
100 * for range of pages in a ttm.
101 */
102
103static int ttm_tt_set_caching(struct ttm_tt *ttm,
104 enum ttm_caching_state c_state)
105{
106 int i, j;
107 struct page *cur_page;
108 int ret;
109
110 if (ttm->caching_state == c_state)
111 return 0;
112
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000113 if (ttm->state == tt_unpopulated) {
114 /* Change caching but don't populate */
115 ttm->caching_state = c_state;
116 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200117 }
118
119 if (ttm->caching_state == tt_cached)
Dave Airliec9c97b82009-08-27 09:53:47 +1000120 drm_clflush_pages(ttm->pages, ttm->num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200121
122 for (i = 0; i < ttm->num_pages; ++i) {
123 cur_page = ttm->pages[i];
124 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000125 ret = ttm_tt_set_page_caching(cur_page,
126 ttm->caching_state,
127 c_state);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200128 if (unlikely(ret != 0))
129 goto out_err;
130 }
131 }
132
133 ttm->caching_state = c_state;
134
135 return 0;
136
137out_err:
138 for (j = 0; j < i; ++j) {
139 cur_page = ttm->pages[j];
140 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000141 (void)ttm_tt_set_page_caching(cur_page, c_state,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142 ttm->caching_state);
143 }
144 }
145
146 return ret;
147}
148
149int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
150{
151 enum ttm_caching_state state;
152
153 if (placement & TTM_PL_FLAG_WC)
154 state = tt_wc;
155 else if (placement & TTM_PL_FLAG_UNCACHED)
156 state = tt_uncached;
157 else
158 state = tt_cached;
159
160 return ttm_tt_set_caching(ttm, state);
161}
Dave Airliedf67bed2009-10-30 13:31:26 +1000162EXPORT_SYMBOL(ttm_tt_set_placement_caching);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164void ttm_tt_destroy(struct ttm_tt *ttm)
165{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166 if (unlikely(ttm == NULL))
167 return;
168
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400169 if (ttm->state == tt_bound) {
170 ttm_tt_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171 }
172
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100173 if (ttm->state == tt_unbound)
174 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175
Jan Engelhardt5df23972011-04-04 01:25:18 +0200176 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200177 ttm->swap_storage)
178 fput(ttm->swap_storage);
179
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400180 ttm->swap_storage = NULL;
181 ttm->func->destroy(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200182}
183
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400184int 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)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200187{
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400188 ttm->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200189 ttm->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200191 ttm->caching_state = tt_cached;
192 ttm->page_flags = page_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200193 ttm->dummy_read_page = dummy_read_page;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400194 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500195 ttm->swap_storage = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196
197 ttm_tt_alloc_page_directory(ttm);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500198 if (!ttm->pages) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200199 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700200 pr_err("Failed allocating page table\n");
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400201 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200202 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400203 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200204}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400205EXPORT_SYMBOL(ttm_tt_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200206
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500207void ttm_tt_fini(struct ttm_tt *ttm)
208{
209 drm_free_large(ttm->pages);
210 ttm->pages = NULL;
211}
212EXPORT_SYMBOL(ttm_tt_fini);
213
214int 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)
217{
218 struct ttm_tt *ttm = &ttm_dma->ttm;
219
220 ttm->bdev = bdev;
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;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500227 ttm->swap_storage = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500228
229 INIT_LIST_HEAD(&ttm_dma->pages_list);
230 ttm_dma_tt_alloc_page_directory(ttm_dma);
231 if (!ttm->pages || !ttm_dma->dma_address) {
232 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700233 pr_err("Failed allocating page table\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500234 return -ENOMEM;
235 }
236 return 0;
237}
238EXPORT_SYMBOL(ttm_dma_tt_init);
239
240void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
241{
242 struct ttm_tt *ttm = &ttm_dma->ttm;
243
244 drm_free_large(ttm->pages);
245 ttm->pages = NULL;
246 drm_free_large(ttm_dma->dma_address);
247 ttm_dma->dma_address = NULL;
248}
249EXPORT_SYMBOL(ttm_dma_tt_fini);
250
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200251void ttm_tt_unbind(struct ttm_tt *ttm)
252{
253 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254
255 if (ttm->state == tt_bound) {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400256 ret = ttm->func->unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200257 BUG_ON(ret);
258 ttm->state = tt_unbound;
259 }
260}
261
262int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
263{
264 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200265
266 if (!ttm)
267 return -EINVAL;
268
269 if (ttm->state == tt_bound)
270 return 0;
271
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400272 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200273 if (ret)
274 return ret;
275
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400276 ret = ttm->func->bind(ttm, bo_mem);
Thomas Hellstrom7dcebb52010-10-29 10:46:49 +0200277 if (unlikely(ret != 0))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200278 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200279
280 ttm->state = tt_bound;
281
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282 return 0;
283}
284EXPORT_SYMBOL(ttm_tt_bind);
285
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400286int ttm_tt_swapin(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200287{
288 struct address_space *swap_space;
289 struct file *swap_storage;
290 struct page *from_page;
291 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100293 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200294
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295 swap_storage = ttm->swap_storage;
296 BUG_ON(swap_storage == NULL);
297
Al Viro496ad9a2013-01-23 17:07:38 -0500298 swap_space = file_inode(swap_storage)->i_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200299
300 for (i = 0; i < ttm->num_pages; ++i) {
Hugh Dickins3142b652011-06-27 16:18:17 -0700301 from_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100302 if (IS_ERR(from_page)) {
303 ret = PTR_ERR(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200304 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100305 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400306 to_page = ttm->pages[i];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307 if (unlikely(to_page == NULL))
308 goto out_err;
309
Akinobu Mita259a2902012-09-25 11:57:02 +0000310 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311 page_cache_release(from_page);
312 }
313
Jan Engelhardt5df23972011-04-04 01:25:18 +0200314 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200315 fput(swap_storage);
316 ttm->swap_storage = NULL;
317 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
318
319 return 0;
320out_err:
Maarten Maathuis290e55052010-02-20 03:22:21 +0100321 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200322}
323
Jan Engelhardt5df23972011-04-04 01:25:18 +0200324int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200325{
326 struct address_space *swap_space;
327 struct file *swap_storage;
328 struct page *from_page;
329 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200330 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100331 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200332
333 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
334 BUG_ON(ttm->caching_state != tt_cached);
335
Jan Engelhardt5df23972011-04-04 01:25:18 +0200336 if (!persistent_swap_storage) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337 swap_storage = shmem_file_setup("ttm swap",
338 ttm->num_pages << PAGE_SHIFT,
339 0);
340 if (unlikely(IS_ERR(swap_storage))) {
Joe Perches25d04792012-03-16 21:43:50 -0700341 pr_err("Failed allocating swap storage\n");
Maarten Maathuis290e55052010-02-20 03:22:21 +0100342 return PTR_ERR(swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343 }
344 } else
Jan Engelhardt5df23972011-04-04 01:25:18 +0200345 swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346
Al Viro496ad9a2013-01-23 17:07:38 -0500347 swap_space = file_inode(swap_storage)->i_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200348
349 for (i = 0; i < ttm->num_pages; ++i) {
350 from_page = ttm->pages[i];
351 if (unlikely(from_page == NULL))
352 continue;
Hugh Dickins3142b652011-06-27 16:18:17 -0700353 to_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100354 if (unlikely(IS_ERR(to_page))) {
355 ret = PTR_ERR(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200356 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100357 }
Akinobu Mita259a2902012-09-25 11:57:02 +0000358 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200359 set_page_dirty(to_page);
360 mark_page_accessed(to_page);
361 page_cache_release(to_page);
362 }
363
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100364 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200365 ttm->swap_storage = swap_storage;
366 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200367 if (persistent_swap_storage)
368 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200369
370 return 0;
371out_err:
Jan Engelhardt5df23972011-04-04 01:25:18 +0200372 if (!persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200373 fput(swap_storage);
374
Maarten Maathuis290e55052010-02-20 03:22:21 +0100375 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200376}
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100377
378static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
379{
380 pgoff_t i;
381 struct page **page = ttm->pages;
382
383 for (i = 0; i < ttm->num_pages; ++i) {
384 (*page)->mapping = NULL;
385 (*page++)->index = 0;
386 }
387}
388
389void ttm_tt_unpopulate(struct ttm_tt *ttm)
390{
391 if (ttm->state == tt_unpopulated)
392 return;
393
394 ttm_tt_clear_mapping(ttm);
395 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
396}