blob: aee3c00f836e723fed991843c22eea5ca0b974d0 [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{
Alexandre Courbot3d50d4d2014-08-04 18:28:54 +090058 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages,
59 sizeof(*ttm->ttm.pages) +
Alexandre Courbotaf1f85d2016-09-16 18:32:26 +090060 sizeof(*ttm->dma_address));
61 ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020062}
63
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020064#ifdef CONFIG_X86
65static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100066 enum ttm_caching_state c_old,
67 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020068{
Francisco Jerezdb78e272010-01-12 18:49:43 +010069 int ret = 0;
70
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020071 if (PageHighMem(p))
72 return 0;
73
Francisco Jerezf0e2f382010-02-20 07:30:15 +100074 if (c_old != tt_cached) {
Francisco Jerezdb78e272010-01-12 18:49:43 +010075 /* p isn't in the default caching state, set it to
76 * writeback first to free its current memtype. */
77
78 ret = set_pages_wb(p, 1);
79 if (ret)
80 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020081 }
Francisco Jerezdb78e272010-01-12 18:49:43 +010082
Francisco Jerezf0e2f382010-02-20 07:30:15 +100083 if (c_new == tt_wc)
Francisco Jerezdb78e272010-01-12 18:49:43 +010084 ret = set_memory_wc((unsigned long) page_address(p), 1);
Francisco Jerezf0e2f382010-02-20 07:30:15 +100085 else if (c_new == tt_uncached)
Francisco Jerezdb78e272010-01-12 18:49:43 +010086 ret = set_pages_uc(p, 1);
87
88 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020089}
90#else /* CONFIG_X86 */
91static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100092 enum ttm_caching_state c_old,
93 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020094{
95 return 0;
96}
97#endif /* CONFIG_X86 */
98
99/*
100 * Change caching policy for the linear kernel map
101 * for range of pages in a ttm.
102 */
103
104static int ttm_tt_set_caching(struct ttm_tt *ttm,
105 enum ttm_caching_state c_state)
106{
107 int i, j;
108 struct page *cur_page;
109 int ret;
110
111 if (ttm->caching_state == c_state)
112 return 0;
113
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000114 if (ttm->state == tt_unpopulated) {
115 /* Change caching but don't populate */
116 ttm->caching_state = c_state;
117 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200118 }
119
120 if (ttm->caching_state == tt_cached)
Dave Airliec9c97b82009-08-27 09:53:47 +1000121 drm_clflush_pages(ttm->pages, ttm->num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200122
123 for (i = 0; i < ttm->num_pages; ++i) {
124 cur_page = ttm->pages[i];
125 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000126 ret = ttm_tt_set_page_caching(cur_page,
127 ttm->caching_state,
128 c_state);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129 if (unlikely(ret != 0))
130 goto out_err;
131 }
132 }
133
134 ttm->caching_state = c_state;
135
136 return 0;
137
138out_err:
139 for (j = 0; j < i; ++j) {
140 cur_page = ttm->pages[j];
141 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000142 (void)ttm_tt_set_page_caching(cur_page, c_state,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200143 ttm->caching_state);
144 }
145 }
146
147 return ret;
148}
149
150int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
151{
152 enum ttm_caching_state state;
153
154 if (placement & TTM_PL_FLAG_WC)
155 state = tt_wc;
156 else if (placement & TTM_PL_FLAG_UNCACHED)
157 state = tt_uncached;
158 else
159 state = tt_cached;
160
161 return ttm_tt_set_caching(ttm, state);
162}
Dave Airliedf67bed2009-10-30 13:31:26 +1000163EXPORT_SYMBOL(ttm_tt_set_placement_caching);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165void ttm_tt_destroy(struct ttm_tt *ttm)
166{
Christian König4279cb12016-06-06 10:17:51 +0200167 if (ttm == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168 return;
169
Christian König2ff2bf12016-07-21 12:18:19 +0200170 ttm_tt_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100172 if (ttm->state == tt_unbound)
173 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174
Jan Engelhardt5df23972011-04-04 01:25:18 +0200175 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200176 ttm->swap_storage)
177 fput(ttm->swap_storage);
178
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400179 ttm->swap_storage = NULL;
180 ttm->func->destroy(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181}
182
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400183int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
184 unsigned long size, uint32_t page_flags,
185 struct page *dummy_read_page)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186{
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400187 ttm->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200188 ttm->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 ttm->caching_state = tt_cached;
191 ttm->page_flags = page_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200192 ttm->dummy_read_page = dummy_read_page;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400193 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500194 ttm->swap_storage = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200195
196 ttm_tt_alloc_page_directory(ttm);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500197 if (!ttm->pages) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200198 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700199 pr_err("Failed allocating page table\n");
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400200 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200201 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400202 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200203}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400204EXPORT_SYMBOL(ttm_tt_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200205
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500206void ttm_tt_fini(struct ttm_tt *ttm)
207{
208 drm_free_large(ttm->pages);
209 ttm->pages = NULL;
210}
211EXPORT_SYMBOL(ttm_tt_fini);
212
213int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
214 unsigned long size, uint32_t page_flags,
215 struct page *dummy_read_page)
216{
217 struct ttm_tt *ttm = &ttm_dma->ttm;
218
219 ttm->bdev = bdev;
220 ttm->glob = bdev->glob;
221 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
222 ttm->caching_state = tt_cached;
223 ttm->page_flags = page_flags;
224 ttm->dummy_read_page = dummy_read_page;
225 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500226 ttm->swap_storage = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500227
228 INIT_LIST_HEAD(&ttm_dma->pages_list);
229 ttm_dma_tt_alloc_page_directory(ttm_dma);
Alexandre Courbot3d50d4d2014-08-04 18:28:54 +0900230 if (!ttm->pages) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500231 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700232 pr_err("Failed allocating page table\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500233 return -ENOMEM;
234 }
235 return 0;
236}
237EXPORT_SYMBOL(ttm_dma_tt_init);
238
239void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
240{
241 struct ttm_tt *ttm = &ttm_dma->ttm;
242
243 drm_free_large(ttm->pages);
244 ttm->pages = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500245 ttm_dma->dma_address = NULL;
246}
247EXPORT_SYMBOL(ttm_dma_tt_fini);
248
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200249void ttm_tt_unbind(struct ttm_tt *ttm)
250{
251 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200252
253 if (ttm->state == tt_bound) {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400254 ret = ttm->func->unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200255 BUG_ON(ret);
256 ttm->state = tt_unbound;
257 }
258}
259
260int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
261{
262 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263
264 if (!ttm)
265 return -EINVAL;
266
267 if (ttm->state == tt_bound)
268 return 0;
269
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400270 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200271 if (ret)
272 return ret;
273
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400274 ret = ttm->func->bind(ttm, bo_mem);
Thomas Hellstrom7dcebb52010-10-29 10:46:49 +0200275 if (unlikely(ret != 0))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200276 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200277
278 ttm->state = tt_bound;
279
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280 return 0;
281}
282EXPORT_SYMBOL(ttm_tt_bind);
283
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400284int ttm_tt_swapin(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285{
286 struct address_space *swap_space;
287 struct file *swap_storage;
288 struct page *from_page;
289 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200290 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100291 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200293 swap_storage = ttm->swap_storage;
294 BUG_ON(swap_storage == NULL);
295
Al Viro93c76a32015-12-04 23:45:44 -0500296 swap_space = swap_storage->f_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200297
298 for (i = 0; i < ttm->num_pages; ++i) {
Hugh Dickins3142b652011-06-27 16:18:17 -0700299 from_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100300 if (IS_ERR(from_page)) {
301 ret = PTR_ERR(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200302 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100303 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400304 to_page = ttm->pages[i];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305 if (unlikely(to_page == NULL))
306 goto out_err;
307
Akinobu Mita259a2902012-09-25 11:57:02 +0000308 copy_highpage(to_page, from_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300309 put_page(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310 }
311
Jan Engelhardt5df23972011-04-04 01:25:18 +0200312 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313 fput(swap_storage);
314 ttm->swap_storage = NULL;
315 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
316
317 return 0;
318out_err:
Maarten Maathuis290e55052010-02-20 03:22:21 +0100319 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200320}
321
Jan Engelhardt5df23972011-04-04 01:25:18 +0200322int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323{
324 struct address_space *swap_space;
325 struct file *swap_storage;
326 struct page *from_page;
327 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200328 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100329 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200330
331 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
332 BUG_ON(ttm->caching_state != tt_cached);
333
Jan Engelhardt5df23972011-04-04 01:25:18 +0200334 if (!persistent_swap_storage) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335 swap_storage = shmem_file_setup("ttm swap",
336 ttm->num_pages << PAGE_SHIFT,
337 0);
Viresh Kumar55579cf2015-07-31 14:08:24 +0530338 if (IS_ERR(swap_storage)) {
Joe Perches25d04792012-03-16 21:43:50 -0700339 pr_err("Failed allocating swap storage\n");
Maarten Maathuis290e55052010-02-20 03:22:21 +0100340 return PTR_ERR(swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 }
342 } else
Jan Engelhardt5df23972011-04-04 01:25:18 +0200343 swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344
Al Viro93c76a32015-12-04 23:45:44 -0500345 swap_space = swap_storage->f_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346
347 for (i = 0; i < ttm->num_pages; ++i) {
348 from_page = ttm->pages[i];
349 if (unlikely(from_page == NULL))
350 continue;
Hugh Dickins3142b652011-06-27 16:18:17 -0700351 to_page = shmem_read_mapping_page(swap_space, i);
Viresh Kumar55579cf2015-07-31 14:08:24 +0530352 if (IS_ERR(to_page)) {
Maarten Maathuis290e55052010-02-20 03:22:21 +0100353 ret = PTR_ERR(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200354 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100355 }
Akinobu Mita259a2902012-09-25 11:57:02 +0000356 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 set_page_dirty(to_page);
358 mark_page_accessed(to_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300359 put_page(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 }
361
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100362 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200363 ttm->swap_storage = swap_storage;
364 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200365 if (persistent_swap_storage)
366 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200367
368 return 0;
369out_err:
Jan Engelhardt5df23972011-04-04 01:25:18 +0200370 if (!persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200371 fput(swap_storage);
372
Maarten Maathuis290e55052010-02-20 03:22:21 +0100373 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374}
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100375
376static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
377{
378 pgoff_t i;
379 struct page **page = ttm->pages;
380
Thomas Hellstrom1b76af52014-02-05 09:18:26 +0100381 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
382 return;
383
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100384 for (i = 0; i < ttm->num_pages; ++i) {
385 (*page)->mapping = NULL;
386 (*page++)->index = 0;
387 }
388}
389
390void ttm_tt_unpopulate(struct ttm_tt *ttm)
391{
392 if (ttm->state == tt_unpopulated)
393 return;
394
395 ttm_tt_clear_mapping(ttm);
396 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
397}