blob: 65976238d24b209b16df56fe5f2c165c858afea7 [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>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020034#include <linux/pagemap.h>
Hugh Dickins3142b652011-06-27 16:18:17 -070035#include <linux/shmem_fs.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/file.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drm_cache.h>
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/ttm/ttm_bo_driver.h>
David Howells760285e2012-10-02 18:01:07 +010039#include <drm/ttm/ttm_page_alloc.h>
Laura Abbotted3ba072017-05-08 15:58:17 -070040#ifdef CONFIG_X86
41#include <asm/set_memory.h>
42#endif
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044/**
Christian König97b7e1b2018-02-22 08:54:57 +010045 * Allocates a ttm structure for the given BO.
46 */
47int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
48{
49 struct ttm_bo_device *bdev = bo->bdev;
Christian König97b7e1b2018-02-22 08:54:57 +010050 uint32_t page_flags = 0;
51
52 reservation_object_assert_held(bo->resv);
Christian König97b7e1b2018-02-22 08:54:57 +010053
54 if (bdev->need_dma32)
55 page_flags |= TTM_PAGE_FLAG_DMA32;
56
57 if (bdev->no_retry)
58 page_flags |= TTM_PAGE_FLAG_NO_RETRY;
59
60 switch (bo->type) {
61 case ttm_bo_type_device:
62 if (zero_alloc)
63 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
Christian König45a9d152018-02-22 08:54:57 +010064 break;
Christian König97b7e1b2018-02-22 08:54:57 +010065 case ttm_bo_type_kernel:
Christian König97b7e1b2018-02-22 08:54:57 +010066 break;
67 case ttm_bo_type_sg:
Christian König45a9d152018-02-22 08:54:57 +010068 page_flags |= TTM_PAGE_FLAG_SG;
Christian König97b7e1b2018-02-22 08:54:57 +010069 break;
70 default:
Christian König45a9d152018-02-22 08:54:57 +010071 bo->ttm = NULL;
Christian König97b7e1b2018-02-22 08:54:57 +010072 pr_err("Illegal buffer object type\n");
Christian König45a9d152018-02-22 08:54:57 +010073 return -EINVAL;
Christian König97b7e1b2018-02-22 08:54:57 +010074 }
75
Christian Königdde5da22018-02-22 10:18:14 +010076 bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
Christian König45a9d152018-02-22 08:54:57 +010077 if (unlikely(bo->ttm == NULL))
78 return -ENOMEM;
79
80 if (bo->type == ttm_bo_type_sg)
81 bo->ttm->sg = bo->sg;
82
83 return 0;
Christian König97b7e1b2018-02-22 08:54:57 +010084}
85
86/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020087 * Allocates storage for pointers to the pages that back the ttm.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020088 */
Tom St Denis5b4262d2018-01-25 13:24:03 -050089static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020090{
Michal Hocko20981052017-05-17 14:23:12 +020091 ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
92 GFP_KERNEL | __GFP_ZERO);
Tom St Denis5b4262d2018-01-25 13:24:03 -050093 if (!ttm->pages)
94 return -ENOMEM;
95 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020096}
97
Tom St Denis5b4262d2018-01-25 13:24:03 -050098static int ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020099{
Michal Hocko20981052017-05-17 14:23:12 +0200100 ttm->ttm.pages = kvmalloc_array(ttm->ttm.num_pages,
Alexandre Courbot3d50d4d2014-08-04 18:28:54 +0900101 sizeof(*ttm->ttm.pages) +
Michal Hocko20981052017-05-17 14:23:12 +0200102 sizeof(*ttm->dma_address),
103 GFP_KERNEL | __GFP_ZERO);
Tom St Denis5b4262d2018-01-25 13:24:03 -0500104 if (!ttm->ttm.pages)
105 return -ENOMEM;
Alexandre Courbotaf1f85d2016-09-16 18:32:26 +0900106 ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
Tom St Denis5b4262d2018-01-25 13:24:03 -0500107 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200108}
109
Christian König75a57662018-02-23 15:12:00 +0100110static int ttm_sg_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
111{
112 ttm->dma_address = kvmalloc_array(ttm->ttm.num_pages,
113 sizeof(*ttm->dma_address),
114 GFP_KERNEL | __GFP_ZERO);
115 if (!ttm->dma_address)
116 return -ENOMEM;
117 return 0;
118}
119
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200120#ifdef CONFIG_X86
121static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000122 enum ttm_caching_state c_old,
123 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200124{
Francisco Jerezdb78e272010-01-12 18:49:43 +0100125 int ret = 0;
126
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200127 if (PageHighMem(p))
128 return 0;
129
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000130 if (c_old != tt_cached) {
Francisco Jerezdb78e272010-01-12 18:49:43 +0100131 /* p isn't in the default caching state, set it to
132 * writeback first to free its current memtype. */
133
134 ret = set_pages_wb(p, 1);
135 if (ret)
136 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200137 }
Francisco Jerezdb78e272010-01-12 18:49:43 +0100138
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000139 if (c_new == tt_wc)
Francisco Jerezdb78e272010-01-12 18:49:43 +0100140 ret = set_memory_wc((unsigned long) page_address(p), 1);
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000141 else if (c_new == tt_uncached)
Francisco Jerezdb78e272010-01-12 18:49:43 +0100142 ret = set_pages_uc(p, 1);
143
144 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200145}
146#else /* CONFIG_X86 */
147static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000148 enum ttm_caching_state c_old,
149 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200150{
151 return 0;
152}
153#endif /* CONFIG_X86 */
154
155/*
156 * Change caching policy for the linear kernel map
157 * for range of pages in a ttm.
158 */
159
160static int ttm_tt_set_caching(struct ttm_tt *ttm,
161 enum ttm_caching_state c_state)
162{
163 int i, j;
164 struct page *cur_page;
165 int ret;
166
167 if (ttm->caching_state == c_state)
168 return 0;
169
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000170 if (ttm->state == tt_unpopulated) {
171 /* Change caching but don't populate */
172 ttm->caching_state = c_state;
173 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174 }
175
176 if (ttm->caching_state == tt_cached)
Dave Airliec9c97b82009-08-27 09:53:47 +1000177 drm_clflush_pages(ttm->pages, ttm->num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200178
179 for (i = 0; i < ttm->num_pages; ++i) {
180 cur_page = ttm->pages[i];
181 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000182 ret = ttm_tt_set_page_caching(cur_page,
183 ttm->caching_state,
184 c_state);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200185 if (unlikely(ret != 0))
186 goto out_err;
187 }
188 }
189
190 ttm->caching_state = c_state;
191
192 return 0;
193
194out_err:
195 for (j = 0; j < i; ++j) {
196 cur_page = ttm->pages[j];
197 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000198 (void)ttm_tt_set_page_caching(cur_page, c_state,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200199 ttm->caching_state);
200 }
201 }
202
203 return ret;
204}
205
206int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
207{
208 enum ttm_caching_state state;
209
210 if (placement & TTM_PL_FLAG_WC)
211 state = tt_wc;
212 else if (placement & TTM_PL_FLAG_UNCACHED)
213 state = tt_uncached;
214 else
215 state = tt_cached;
216
217 return ttm_tt_set_caching(ttm, state);
218}
Dave Airliedf67bed2009-10-30 13:31:26 +1000219EXPORT_SYMBOL(ttm_tt_set_placement_caching);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221void ttm_tt_destroy(struct ttm_tt *ttm)
222{
Christian König4279cb12016-06-06 10:17:51 +0200223 if (ttm == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200224 return;
225
Christian König2ff2bf12016-07-21 12:18:19 +0200226 ttm_tt_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200227
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100228 if (ttm->state == tt_unbound)
229 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200230
Jan Engelhardt5df23972011-04-04 01:25:18 +0200231 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200232 ttm->swap_storage)
233 fput(ttm->swap_storage);
234
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400235 ttm->swap_storage = NULL;
236 ttm->func->destroy(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200237}
238
Christian Königdde5da22018-02-22 10:18:14 +0100239void ttm_tt_init_fields(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
240 uint32_t page_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200241{
Christian Königdde5da22018-02-22 10:18:14 +0100242 ttm->bdev = bo->bdev;
243 ttm->num_pages = bo->num_pages;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200244 ttm->caching_state = tt_cached;
245 ttm->page_flags = page_flags;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400246 ttm->state = tt_unpopulated;
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500247 ttm->swap_storage = NULL;
Christian König75a57662018-02-23 15:12:00 +0100248}
249
Christian Königdde5da22018-02-22 10:18:14 +0100250int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
251 uint32_t page_flags)
Christian König75a57662018-02-23 15:12:00 +0100252{
Christian Königdde5da22018-02-22 10:18:14 +0100253 ttm_tt_init_fields(ttm, bo, page_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254
Tom St Denis5b4262d2018-01-25 13:24:03 -0500255 if (ttm_tt_alloc_page_directory(ttm)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200256 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700257 pr_err("Failed allocating page table\n");
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400258 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200259 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400260 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400262EXPORT_SYMBOL(ttm_tt_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500264void ttm_tt_fini(struct ttm_tt *ttm)
265{
Michal Hocko20981052017-05-17 14:23:12 +0200266 kvfree(ttm->pages);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500267 ttm->pages = NULL;
268}
269EXPORT_SYMBOL(ttm_tt_fini);
270
Christian Königdde5da22018-02-22 10:18:14 +0100271int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
272 uint32_t page_flags)
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500273{
274 struct ttm_tt *ttm = &ttm_dma->ttm;
275
Christian Königdde5da22018-02-22 10:18:14 +0100276 ttm_tt_init_fields(ttm, bo, page_flags);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500277
278 INIT_LIST_HEAD(&ttm_dma->pages_list);
Tom St Denis5b4262d2018-01-25 13:24:03 -0500279 if (ttm_dma_tt_alloc_page_directory(ttm_dma)) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500280 ttm_tt_destroy(ttm);
Joe Perches25d04792012-03-16 21:43:50 -0700281 pr_err("Failed allocating page table\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500282 return -ENOMEM;
283 }
284 return 0;
285}
286EXPORT_SYMBOL(ttm_dma_tt_init);
287
Christian Königdde5da22018-02-22 10:18:14 +0100288int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
289 uint32_t page_flags)
Christian König75a57662018-02-23 15:12:00 +0100290{
291 struct ttm_tt *ttm = &ttm_dma->ttm;
292 int ret;
293
Christian Königdde5da22018-02-22 10:18:14 +0100294 ttm_tt_init_fields(ttm, bo, page_flags);
Christian König75a57662018-02-23 15:12:00 +0100295
296 INIT_LIST_HEAD(&ttm_dma->pages_list);
297 if (page_flags & TTM_PAGE_FLAG_SG)
298 ret = ttm_sg_tt_alloc_page_directory(ttm_dma);
299 else
300 ret = ttm_dma_tt_alloc_page_directory(ttm_dma);
301 if (ret) {
302 ttm_tt_destroy(ttm);
303 pr_err("Failed allocating page table\n");
304 return -ENOMEM;
305 }
306 return 0;
307}
308EXPORT_SYMBOL(ttm_sg_tt_init);
309
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500310void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
311{
312 struct ttm_tt *ttm = &ttm_dma->ttm;
313
Christian König75a57662018-02-23 15:12:00 +0100314 if (ttm->pages)
315 kvfree(ttm->pages);
316 else
317 kvfree(ttm_dma->dma_address);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500318 ttm->pages = NULL;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500319 ttm_dma->dma_address = NULL;
320}
321EXPORT_SYMBOL(ttm_dma_tt_fini);
322
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323void ttm_tt_unbind(struct ttm_tt *ttm)
324{
325 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326
327 if (ttm->state == tt_bound) {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400328 ret = ttm->func->unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200329 BUG_ON(ret);
330 ttm->state = tt_unbound;
331 }
332}
333
Roger He993baf12017-12-21 17:42:51 +0800334int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
335 struct ttm_operation_ctx *ctx)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200336{
337 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200338
339 if (!ttm)
340 return -EINVAL;
341
342 if (ttm->state == tt_bound)
343 return 0;
344
Christian König25893a12018-02-01 14:39:29 +0100345 ret = ttm_tt_populate(ttm, ctx);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346 if (ret)
347 return ret;
348
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400349 ret = ttm->func->bind(ttm, bo_mem);
Thomas Hellstrom7dcebb52010-10-29 10:46:49 +0200350 if (unlikely(ret != 0))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200351 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200352
353 ttm->state = tt_bound;
354
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200355 return 0;
356}
357EXPORT_SYMBOL(ttm_tt_bind);
358
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400359int ttm_tt_swapin(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360{
361 struct address_space *swap_space;
362 struct file *swap_storage;
363 struct page *from_page;
364 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200365 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100366 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200367
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200368 swap_storage = ttm->swap_storage;
369 BUG_ON(swap_storage == NULL);
370
Al Viro93c76a32015-12-04 23:45:44 -0500371 swap_space = swap_storage->f_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200372
373 for (i = 0; i < ttm->num_pages; ++i) {
Andrey Grodzovskycb5f1a52017-12-22 08:12:40 -0500374 gfp_t gfp_mask = mapping_gfp_mask(swap_space);
375
376 gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
377 from_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
378
Maarten Maathuis290e55052010-02-20 03:22:21 +0100379 if (IS_ERR(from_page)) {
380 ret = PTR_ERR(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200381 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100382 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400383 to_page = ttm->pages[i];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200384 if (unlikely(to_page == NULL))
385 goto out_err;
386
Akinobu Mita259a2902012-09-25 11:57:02 +0000387 copy_highpage(to_page, from_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300388 put_page(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200389 }
390
Jan Engelhardt5df23972011-04-04 01:25:18 +0200391 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200392 fput(swap_storage);
393 ttm->swap_storage = NULL;
394 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
395
396 return 0;
397out_err:
Maarten Maathuis290e55052010-02-20 03:22:21 +0100398 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200399}
400
Jan Engelhardt5df23972011-04-04 01:25:18 +0200401int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402{
403 struct address_space *swap_space;
404 struct file *swap_storage;
405 struct page *from_page;
406 struct page *to_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200407 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100408 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200409
410 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
411 BUG_ON(ttm->caching_state != tt_cached);
412
Jan Engelhardt5df23972011-04-04 01:25:18 +0200413 if (!persistent_swap_storage) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200414 swap_storage = shmem_file_setup("ttm swap",
415 ttm->num_pages << PAGE_SHIFT,
416 0);
Viresh Kumar55579cf2015-07-31 14:08:24 +0530417 if (IS_ERR(swap_storage)) {
Joe Perches25d04792012-03-16 21:43:50 -0700418 pr_err("Failed allocating swap storage\n");
Maarten Maathuis290e55052010-02-20 03:22:21 +0100419 return PTR_ERR(swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200420 }
Tom St Denisddde9852018-01-25 13:29:42 -0500421 } else {
Jan Engelhardt5df23972011-04-04 01:25:18 +0200422 swap_storage = persistent_swap_storage;
Tom St Denisddde9852018-01-25 13:29:42 -0500423 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200424
Al Viro93c76a32015-12-04 23:45:44 -0500425 swap_space = swap_storage->f_mapping;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426
427 for (i = 0; i < ttm->num_pages; ++i) {
Andrey Grodzovskycb5f1a52017-12-22 08:12:40 -0500428 gfp_t gfp_mask = mapping_gfp_mask(swap_space);
429
430 gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
431
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432 from_page = ttm->pages[i];
433 if (unlikely(from_page == NULL))
434 continue;
Andrey Grodzovskycb5f1a52017-12-22 08:12:40 -0500435
436 to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
Viresh Kumar55579cf2015-07-31 14:08:24 +0530437 if (IS_ERR(to_page)) {
Maarten Maathuis290e55052010-02-20 03:22:21 +0100438 ret = PTR_ERR(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200439 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100440 }
Akinobu Mita259a2902012-09-25 11:57:02 +0000441 copy_highpage(to_page, from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442 set_page_dirty(to_page);
443 mark_page_accessed(to_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300444 put_page(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200445 }
446
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100447 ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200448 ttm->swap_storage = swap_storage;
449 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200450 if (persistent_swap_storage)
451 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452
453 return 0;
454out_err:
Jan Engelhardt5df23972011-04-04 01:25:18 +0200455 if (!persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456 fput(swap_storage);
457
Maarten Maathuis290e55052010-02-20 03:22:21 +0100458 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200459}
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100460
Christian Königec929372018-02-01 14:52:50 +0100461static void ttm_tt_add_mapping(struct ttm_tt *ttm)
462{
463 pgoff_t i;
464
465 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
466 return;
467
468 for (i = 0; i < ttm->num_pages; ++i)
469 ttm->pages[i]->mapping = ttm->bdev->dev_mapping;
470}
471
Christian König25893a12018-02-01 14:39:29 +0100472int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
473{
Christian Königec929372018-02-01 14:52:50 +0100474 int ret;
475
Christian König25893a12018-02-01 14:39:29 +0100476 if (ttm->state != tt_unpopulated)
477 return 0;
478
Christian Könige44fcf72018-02-22 12:00:05 +0100479 if (ttm->bdev->driver->ttm_tt_populate)
480 ret = ttm->bdev->driver->ttm_tt_populate(ttm, ctx);
481 else
482 ret = ttm_pool_populate(ttm, ctx);
Christian Königec929372018-02-01 14:52:50 +0100483 if (!ret)
484 ttm_tt_add_mapping(ttm);
485 return ret;
Christian König25893a12018-02-01 14:39:29 +0100486}
487
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100488static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
489{
490 pgoff_t i;
491 struct page **page = ttm->pages;
492
Thomas Hellstrom1b76af52014-02-05 09:18:26 +0100493 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
494 return;
495
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100496 for (i = 0; i < ttm->num_pages; ++i) {
497 (*page)->mapping = NULL;
498 (*page++)->index = 0;
499 }
500}
501
502void ttm_tt_unpopulate(struct ttm_tt *ttm)
503{
504 if (ttm->state == tt_unpopulated)
505 return;
506
507 ttm_tt_clear_mapping(ttm);
Christian Könige44fcf72018-02-22 12:00:05 +0100508 if (ttm->bdev->driver->ttm_tt_unpopulate)
509 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
510 else
511 ttm_pool_unpopulate(ttm);
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100512}