blob: 5e93a52d4f2c7a6183a36e70c976d8a1bbf9871e [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
173 if (likely(ttm->pages != NULL)) {
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400174 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175 }
176
Jan Engelhardt5df23972011-04-04 01:25:18 +0200177 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200178 ttm->swap_storage)
179 fput(ttm->swap_storage);
180
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400181 ttm->swap_storage = NULL;
182 ttm->func->destroy(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183}
184
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400185int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
186 unsigned long size, uint32_t page_flags,
187 struct page *dummy_read_page)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188{
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400189 ttm->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200190 ttm->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200191 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200192 ttm->caching_state = tt_cached;
193 ttm->page_flags = page_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200194 ttm->dummy_read_page = dummy_read_page;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400195 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500196 ttm->swap_storage = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200197
198 ttm_tt_alloc_page_directory(ttm);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500199 if (!ttm->pages) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200200 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700201 pr_err("Failed allocating page table\n");
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400202 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200203 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400204 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200205}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400206EXPORT_SYMBOL(ttm_tt_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500208void ttm_tt_fini(struct ttm_tt *ttm)
209{
210 drm_free_large(ttm->pages);
211 ttm->pages = NULL;
212}
213EXPORT_SYMBOL(ttm_tt_fini);
214
215int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
216 unsigned long size, uint32_t page_flags,
217 struct page *dummy_read_page)
218{
219 struct ttm_tt *ttm = &ttm_dma->ttm;
220
221 ttm->bdev = bdev;
222 ttm->glob = bdev->glob;
223 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
224 ttm->caching_state = tt_cached;
225 ttm->page_flags = page_flags;
226 ttm->dummy_read_page = dummy_read_page;
227 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500228 ttm->swap_storage = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500229
230 INIT_LIST_HEAD(&ttm_dma->pages_list);
231 ttm_dma_tt_alloc_page_directory(ttm_dma);
232 if (!ttm->pages || !ttm_dma->dma_address) {
233 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700234 pr_err("Failed allocating page table\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500235 return -ENOMEM;
236 }
237 return 0;
238}
239EXPORT_SYMBOL(ttm_dma_tt_init);
240
241void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
242{
243 struct ttm_tt *ttm = &ttm_dma->ttm;
244
245 drm_free_large(ttm->pages);
246 ttm->pages = NULL;
247 drm_free_large(ttm_dma->dma_address);
248 ttm_dma->dma_address = NULL;
249}
250EXPORT_SYMBOL(ttm_dma_tt_fini);
251
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200252void ttm_tt_unbind(struct ttm_tt *ttm)
253{
254 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200255
256 if (ttm->state == tt_bound) {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400257 ret = ttm->func->unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200258 BUG_ON(ret);
259 ttm->state = tt_unbound;
260 }
261}
262
263int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
264{
265 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200266
267 if (!ttm)
268 return -EINVAL;
269
270 if (ttm->state == tt_bound)
271 return 0;
272
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400273 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200274 if (ret)
275 return ret;
276
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400277 ret = ttm->func->bind(ttm, bo_mem);
Thomas Hellstrom7dcebb52010-10-29 10:46:49 +0200278 if (unlikely(ret != 0))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200279 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280
281 ttm->state = tt_bound;
282
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200283 return 0;
284}
285EXPORT_SYMBOL(ttm_tt_bind);
286
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400287int ttm_tt_swapin(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288{
289 struct address_space *swap_space;
290 struct file *swap_storage;
291 struct page *from_page;
292 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200293 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100294 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296 swap_storage = ttm->swap_storage;
297 BUG_ON(swap_storage == NULL);
298
Al Viro496ad9a2013-01-23 17:07:38 -0500299 swap_space = file_inode(swap_storage)->i_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300
301 for (i = 0; i < ttm->num_pages; ++i) {
Hugh Dickins3142b652011-06-27 16:18:17 -0700302 from_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100303 if (IS_ERR(from_page)) {
304 ret = PTR_ERR(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100306 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400307 to_page = ttm->pages[i];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200308 if (unlikely(to_page == NULL))
309 goto out_err;
310
Akinobu Mita259a2902012-09-25 11:57:02 +0000311 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312 page_cache_release(from_page);
313 }
314
Jan Engelhardt5df23972011-04-04 01:25:18 +0200315 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200316 fput(swap_storage);
317 ttm->swap_storage = NULL;
318 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
319
320 return 0;
321out_err:
Maarten Maathuis290e55052010-02-20 03:22:21 +0100322 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323}
324
Jan Engelhardt5df23972011-04-04 01:25:18 +0200325int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326{
327 struct address_space *swap_space;
328 struct file *swap_storage;
329 struct page *from_page;
330 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200331 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100332 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333
334 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
335 BUG_ON(ttm->caching_state != tt_cached);
336
Jan Engelhardt5df23972011-04-04 01:25:18 +0200337 if (!persistent_swap_storage) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200338 swap_storage = shmem_file_setup("ttm swap",
339 ttm->num_pages << PAGE_SHIFT,
340 0);
341 if (unlikely(IS_ERR(swap_storage))) {
Joe Perches25d04792012-03-16 21:43:50 -0700342 pr_err("Failed allocating swap storage\n");
Maarten Maathuis290e55052010-02-20 03:22:21 +0100343 return PTR_ERR(swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344 }
345 } else
Jan Engelhardt5df23972011-04-04 01:25:18 +0200346 swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200347
Al Viro496ad9a2013-01-23 17:07:38 -0500348 swap_space = file_inode(swap_storage)->i_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349
350 for (i = 0; i < ttm->num_pages; ++i) {
351 from_page = ttm->pages[i];
352 if (unlikely(from_page == NULL))
353 continue;
Hugh Dickins3142b652011-06-27 16:18:17 -0700354 to_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100355 if (unlikely(IS_ERR(to_page))) {
356 ret = PTR_ERR(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100358 }
Akinobu Mita259a2902012-09-25 11:57:02 +0000359 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 set_page_dirty(to_page);
361 mark_page_accessed(to_page);
362 page_cache_release(to_page);
363 }
364
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400365 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200366 ttm->swap_storage = swap_storage;
367 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200368 if (persistent_swap_storage)
369 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200370
371 return 0;
372out_err:
Jan Engelhardt5df23972011-04-04 01:25:18 +0200373 if (!persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374 fput(swap_storage);
375
Maarten Maathuis290e55052010-02-20 03:22:21 +0100376 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200377}