blob: 39c44e301c721af19b3cd40e68459d9324cac23c [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>
David Howells760285e2012-10-02 18:01:07 +010042#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>
Laura Abbotted3ba072017-05-08 15:58:17 -070046#ifdef CONFIG_X86
47#include <asm/set_memory.h>
48#endif
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020050/**
51 * Allocates storage for pointers to the pages that back the ttm.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020052 */
Tom St Denis5b4262d2018-01-25 13:24:03 -050053static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020054{
Michal Hocko20981052017-05-17 14:23:12 +020055 ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
56 GFP_KERNEL | __GFP_ZERO);
Tom St Denis5b4262d2018-01-25 13:24:03 -050057 if (!ttm->pages)
58 return -ENOMEM;
59 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020060}
61
Tom St Denis5b4262d2018-01-25 13:24:03 -050062static int ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020063{
Michal Hocko20981052017-05-17 14:23:12 +020064 ttm->ttm.pages = kvmalloc_array(ttm->ttm.num_pages,
Alexandre Courbot3d50d4d2014-08-04 18:28:54 +090065 sizeof(*ttm->ttm.pages) +
Michal Hocko20981052017-05-17 14:23:12 +020066 sizeof(*ttm->dma_address),
67 GFP_KERNEL | __GFP_ZERO);
Tom St Denis5b4262d2018-01-25 13:24:03 -050068 if (!ttm->ttm.pages)
69 return -ENOMEM;
Alexandre Courbotaf1f85d2016-09-16 18:32:26 +090070 ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
Tom St Denis5b4262d2018-01-25 13:24:03 -050071 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020072}
73
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020074#ifdef CONFIG_X86
75static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100076 enum ttm_caching_state c_old,
77 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020078{
Francisco Jerezdb78e272010-01-12 18:49:43 +010079 int ret = 0;
80
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020081 if (PageHighMem(p))
82 return 0;
83
Francisco Jerezf0e2f382010-02-20 07:30:15 +100084 if (c_old != tt_cached) {
Francisco Jerezdb78e272010-01-12 18:49:43 +010085 /* p isn't in the default caching state, set it to
86 * writeback first to free its current memtype. */
87
88 ret = set_pages_wb(p, 1);
89 if (ret)
90 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020091 }
Francisco Jerezdb78e272010-01-12 18:49:43 +010092
Francisco Jerezf0e2f382010-02-20 07:30:15 +100093 if (c_new == tt_wc)
Francisco Jerezdb78e272010-01-12 18:49:43 +010094 ret = set_memory_wc((unsigned long) page_address(p), 1);
Francisco Jerezf0e2f382010-02-20 07:30:15 +100095 else if (c_new == tt_uncached)
Francisco Jerezdb78e272010-01-12 18:49:43 +010096 ret = set_pages_uc(p, 1);
97
98 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020099}
100#else /* CONFIG_X86 */
101static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000102 enum ttm_caching_state c_old,
103 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200104{
105 return 0;
106}
107#endif /* CONFIG_X86 */
108
109/*
110 * Change caching policy for the linear kernel map
111 * for range of pages in a ttm.
112 */
113
114static int ttm_tt_set_caching(struct ttm_tt *ttm,
115 enum ttm_caching_state c_state)
116{
117 int i, j;
118 struct page *cur_page;
119 int ret;
120
121 if (ttm->caching_state == c_state)
122 return 0;
123
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000124 if (ttm->state == tt_unpopulated) {
125 /* Change caching but don't populate */
126 ttm->caching_state = c_state;
127 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200128 }
129
130 if (ttm->caching_state == tt_cached)
Dave Airliec9c97b82009-08-27 09:53:47 +1000131 drm_clflush_pages(ttm->pages, ttm->num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200132
133 for (i = 0; i < ttm->num_pages; ++i) {
134 cur_page = ttm->pages[i];
135 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000136 ret = ttm_tt_set_page_caching(cur_page,
137 ttm->caching_state,
138 c_state);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200139 if (unlikely(ret != 0))
140 goto out_err;
141 }
142 }
143
144 ttm->caching_state = c_state;
145
146 return 0;
147
148out_err:
149 for (j = 0; j < i; ++j) {
150 cur_page = ttm->pages[j];
151 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000152 (void)ttm_tt_set_page_caching(cur_page, c_state,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200153 ttm->caching_state);
154 }
155 }
156
157 return ret;
158}
159
160int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
161{
162 enum ttm_caching_state state;
163
164 if (placement & TTM_PL_FLAG_WC)
165 state = tt_wc;
166 else if (placement & TTM_PL_FLAG_UNCACHED)
167 state = tt_uncached;
168 else
169 state = tt_cached;
170
171 return ttm_tt_set_caching(ttm, state);
172}
Dave Airliedf67bed2009-10-30 13:31:26 +1000173EXPORT_SYMBOL(ttm_tt_set_placement_caching);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175void ttm_tt_destroy(struct ttm_tt *ttm)
176{
Christian König4279cb12016-06-06 10:17:51 +0200177 if (ttm == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200178 return;
179
Christian König2ff2bf12016-07-21 12:18:19 +0200180 ttm_tt_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100182 if (ttm->state == tt_unbound)
183 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200184
Jan Engelhardt5df23972011-04-04 01:25:18 +0200185 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186 ttm->swap_storage)
187 fput(ttm->swap_storage);
188
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400189 ttm->swap_storage = NULL;
190 ttm->func->destroy(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200191}
192
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400193int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
194 unsigned long size, uint32_t page_flags,
195 struct page *dummy_read_page)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196{
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400197 ttm->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200198 ttm->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200199 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200200 ttm->caching_state = tt_cached;
201 ttm->page_flags = page_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200202 ttm->dummy_read_page = dummy_read_page;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400203 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500204 ttm->swap_storage = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200205
Tom St Denis5b4262d2018-01-25 13:24:03 -0500206 if (ttm_tt_alloc_page_directory(ttm)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700208 pr_err("Failed allocating page table\n");
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400209 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200210 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400211 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200212}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400213EXPORT_SYMBOL(ttm_tt_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200214
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500215void ttm_tt_fini(struct ttm_tt *ttm)
216{
Michal Hocko20981052017-05-17 14:23:12 +0200217 kvfree(ttm->pages);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500218 ttm->pages = NULL;
219}
220EXPORT_SYMBOL(ttm_tt_fini);
221
222int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
223 unsigned long size, uint32_t page_flags,
224 struct page *dummy_read_page)
225{
226 struct ttm_tt *ttm = &ttm_dma->ttm;
227
228 ttm->bdev = bdev;
229 ttm->glob = bdev->glob;
230 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
231 ttm->caching_state = tt_cached;
232 ttm->page_flags = page_flags;
233 ttm->dummy_read_page = dummy_read_page;
234 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500235 ttm->swap_storage = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500236
237 INIT_LIST_HEAD(&ttm_dma->pages_list);
Tom St Denis5b4262d2018-01-25 13:24:03 -0500238 if (ttm_dma_tt_alloc_page_directory(ttm_dma)) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500239 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700240 pr_err("Failed allocating page table\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500241 return -ENOMEM;
242 }
243 return 0;
244}
245EXPORT_SYMBOL(ttm_dma_tt_init);
246
247void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
248{
249 struct ttm_tt *ttm = &ttm_dma->ttm;
250
Michal Hocko20981052017-05-17 14:23:12 +0200251 kvfree(ttm->pages);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500252 ttm->pages = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500253 ttm_dma->dma_address = NULL;
254}
255EXPORT_SYMBOL(ttm_dma_tt_fini);
256
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200257void ttm_tt_unbind(struct ttm_tt *ttm)
258{
259 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200260
261 if (ttm->state == tt_bound) {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400262 ret = ttm->func->unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263 BUG_ON(ret);
264 ttm->state = tt_unbound;
265 }
266}
267
Roger He993baf12017-12-21 17:42:51 +0800268int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
269 struct ttm_operation_ctx *ctx)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200270{
271 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200272
273 if (!ttm)
274 return -EINVAL;
275
276 if (ttm->state == tt_bound)
277 return 0;
278
Christian König25893a12018-02-01 14:39:29 +0100279 ret = ttm_tt_populate(ttm, ctx);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280 if (ret)
281 return ret;
282
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400283 ret = ttm->func->bind(ttm, bo_mem);
Thomas Hellstrom7dcebb52010-10-29 10:46:49 +0200284 if (unlikely(ret != 0))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200286
287 ttm->state = tt_bound;
288
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289 return 0;
290}
291EXPORT_SYMBOL(ttm_tt_bind);
292
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400293int ttm_tt_swapin(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200294{
295 struct address_space *swap_space;
296 struct file *swap_storage;
297 struct page *from_page;
298 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200299 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100300 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200302 swap_storage = ttm->swap_storage;
303 BUG_ON(swap_storage == NULL);
304
Al Viro93c76a32015-12-04 23:45:44 -0500305 swap_space = swap_storage->f_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200306
307 for (i = 0; i < ttm->num_pages; ++i) {
Andrey Grodzovskycb5f1a52017-12-22 08:12:40 -0500308 gfp_t gfp_mask = mapping_gfp_mask(swap_space);
309
310 gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
311 from_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
312
Maarten Maathuis290e55052010-02-20 03:22:21 +0100313 if (IS_ERR(from_page)) {
314 ret = PTR_ERR(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200315 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100316 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400317 to_page = ttm->pages[i];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200318 if (unlikely(to_page == NULL))
319 goto out_err;
320
Akinobu Mita259a2902012-09-25 11:57:02 +0000321 copy_highpage(to_page, from_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300322 put_page(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323 }
324
Jan Engelhardt5df23972011-04-04 01:25:18 +0200325 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 fput(swap_storage);
327 ttm->swap_storage = NULL;
328 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
329
330 return 0;
331out_err:
Maarten Maathuis290e55052010-02-20 03:22:21 +0100332 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333}
334
Jan Engelhardt5df23972011-04-04 01:25:18 +0200335int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200336{
337 struct address_space *swap_space;
338 struct file *swap_storage;
339 struct page *from_page;
340 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100342 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343
344 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
345 BUG_ON(ttm->caching_state != tt_cached);
346
Jan Engelhardt5df23972011-04-04 01:25:18 +0200347 if (!persistent_swap_storage) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200348 swap_storage = shmem_file_setup("ttm swap",
349 ttm->num_pages << PAGE_SHIFT,
350 0);
Viresh Kumar55579cf2015-07-31 14:08:24 +0530351 if (IS_ERR(swap_storage)) {
Joe Perches25d04792012-03-16 21:43:50 -0700352 pr_err("Failed allocating swap storage\n");
Maarten Maathuis290e55052010-02-20 03:22:21 +0100353 return PTR_ERR(swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200354 }
Tom St Denisddde9852018-01-25 13:29:42 -0500355 } else {
Jan Engelhardt5df23972011-04-04 01:25:18 +0200356 swap_storage = persistent_swap_storage;
Tom St Denisddde9852018-01-25 13:29:42 -0500357 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358
Al Viro93c76a32015-12-04 23:45:44 -0500359 swap_space = swap_storage->f_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360
361 for (i = 0; i < ttm->num_pages; ++i) {
Andrey Grodzovskycb5f1a52017-12-22 08:12:40 -0500362 gfp_t gfp_mask = mapping_gfp_mask(swap_space);
363
364 gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
365
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200366 from_page = ttm->pages[i];
367 if (unlikely(from_page == NULL))
368 continue;
Andrey Grodzovskycb5f1a52017-12-22 08:12:40 -0500369
370 to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
Viresh Kumar55579cf2015-07-31 14:08:24 +0530371 if (IS_ERR(to_page)) {
Maarten Maathuis290e55052010-02-20 03:22:21 +0100372 ret = PTR_ERR(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200373 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100374 }
Akinobu Mita259a2902012-09-25 11:57:02 +0000375 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200376 set_page_dirty(to_page);
377 mark_page_accessed(to_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300378 put_page(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200379 }
380
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100381 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200382 ttm->swap_storage = swap_storage;
383 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200384 if (persistent_swap_storage)
385 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386
387 return 0;
388out_err:
Jan Engelhardt5df23972011-04-04 01:25:18 +0200389 if (!persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 fput(swap_storage);
391
Maarten Maathuis290e55052010-02-20 03:22:21 +0100392 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200393}
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100394
Christian König25893a12018-02-01 14:39:29 +0100395int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
396{
397 if (ttm->state != tt_unpopulated)
398 return 0;
399
400 return ttm->bdev->driver->ttm_tt_populate(ttm, ctx);
401}
402
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100403static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
404{
405 pgoff_t i;
406 struct page **page = ttm->pages;
407
Thomas Hellstrom1b76af52014-02-05 09:18:26 +0100408 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
409 return;
410
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100411 for (i = 0; i < ttm->num_pages; ++i) {
412 (*page)->mapping = NULL;
413 (*page++)->index = 0;
414 }
415}
416
417void ttm_tt_unpopulate(struct ttm_tt *ttm)
418{
419 if (ttm->state == tt_unpopulated)
420 return;
421
422 ttm_tt_clear_mapping(ttm);
423 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
424}