drm/ttm: move swapout logic around v3
[linux-2.6-microblaze.git] / drivers / gpu / drm / ttm / ttm_tt.c
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  */
31
32 #define pr_fmt(fmt) "[TTM] " fmt
33
34 #include <linux/sched.h>
35 #include <linux/pagemap.h>
36 #include <linux/shmem_fs.h>
37 #include <linux/file.h>
38 #include <drm/drm_cache.h>
39 #include <drm/ttm/ttm_bo_driver.h>
40
41 #include "ttm_module.h"
42
43 static struct shrinker mm_shrinker;
44 static atomic_long_t swapable_pages;
45
46 /*
47  * Allocates a ttm structure for the given BO.
48  */
49 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
50 {
51         struct ttm_device *bdev = bo->bdev;
52         uint32_t page_flags = 0;
53
54         dma_resv_assert_held(bo->base.resv);
55
56         if (bo->ttm)
57                 return 0;
58
59         switch (bo->type) {
60         case ttm_bo_type_device:
61                 if (zero_alloc)
62                         page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
63                 break;
64         case ttm_bo_type_kernel:
65                 break;
66         case ttm_bo_type_sg:
67                 page_flags |= TTM_PAGE_FLAG_SG;
68                 break;
69         default:
70                 pr_err("Illegal buffer object type\n");
71                 return -EINVAL;
72         }
73
74         bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
75         if (unlikely(bo->ttm == NULL))
76                 return -ENOMEM;
77
78         return 0;
79 }
80
81 /*
82  * Allocates storage for pointers to the pages that back the ttm.
83  */
84 static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
85 {
86         ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
87                         GFP_KERNEL | __GFP_ZERO);
88         if (!ttm->pages)
89                 return -ENOMEM;
90         return 0;
91 }
92
93 static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
94 {
95         ttm->pages = kvmalloc_array(ttm->num_pages,
96                                     sizeof(*ttm->pages) +
97                                     sizeof(*ttm->dma_address),
98                                     GFP_KERNEL | __GFP_ZERO);
99         if (!ttm->pages)
100                 return -ENOMEM;
101
102         ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
103         return 0;
104 }
105
106 static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
107 {
108         ttm->dma_address = kvmalloc_array(ttm->num_pages,
109                                           sizeof(*ttm->dma_address),
110                                           GFP_KERNEL | __GFP_ZERO);
111         if (!ttm->dma_address)
112                 return -ENOMEM;
113         return 0;
114 }
115
116 void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm)
117 {
118         ttm_tt_unpopulate(bdev, ttm);
119
120         if (ttm->swap_storage)
121                 fput(ttm->swap_storage);
122
123         ttm->swap_storage = NULL;
124 }
125 EXPORT_SYMBOL(ttm_tt_destroy_common);
126
127 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
128 {
129         bdev->funcs->ttm_tt_destroy(bdev, ttm);
130 }
131
132 static void ttm_tt_init_fields(struct ttm_tt *ttm,
133                                struct ttm_buffer_object *bo,
134                                uint32_t page_flags,
135                                enum ttm_caching caching)
136 {
137         ttm->num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
138         ttm->caching = ttm_cached;
139         ttm->page_flags = page_flags;
140         ttm->dma_address = NULL;
141         ttm->swap_storage = NULL;
142         ttm->sg = bo->sg;
143         ttm->caching = caching;
144 }
145
146 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
147                 uint32_t page_flags, enum ttm_caching caching)
148 {
149         ttm_tt_init_fields(ttm, bo, page_flags, caching);
150
151         if (ttm_tt_alloc_page_directory(ttm)) {
152                 pr_err("Failed allocating page table\n");
153                 return -ENOMEM;
154         }
155         return 0;
156 }
157 EXPORT_SYMBOL(ttm_tt_init);
158
159 void ttm_tt_fini(struct ttm_tt *ttm)
160 {
161         if (ttm->pages)
162                 kvfree(ttm->pages);
163         else
164                 kvfree(ttm->dma_address);
165         ttm->pages = NULL;
166         ttm->dma_address = NULL;
167 }
168 EXPORT_SYMBOL(ttm_tt_fini);
169
170 int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
171                    uint32_t page_flags, enum ttm_caching caching)
172 {
173         int ret;
174
175         ttm_tt_init_fields(ttm, bo, page_flags, caching);
176
177         if (page_flags & TTM_PAGE_FLAG_SG)
178                 ret = ttm_sg_tt_alloc_page_directory(ttm);
179         else
180                 ret = ttm_dma_tt_alloc_page_directory(ttm);
181         if (ret) {
182                 pr_err("Failed allocating page table\n");
183                 return -ENOMEM;
184         }
185         return 0;
186 }
187 EXPORT_SYMBOL(ttm_sg_tt_init);
188
189 int ttm_tt_swapin(struct ttm_tt *ttm)
190 {
191         struct address_space *swap_space;
192         struct file *swap_storage;
193         struct page *from_page;
194         struct page *to_page;
195         gfp_t gfp_mask;
196         int i, ret;
197
198         swap_storage = ttm->swap_storage;
199         BUG_ON(swap_storage == NULL);
200
201         swap_space = swap_storage->f_mapping;
202         gfp_mask = mapping_gfp_mask(swap_space);
203
204         for (i = 0; i < ttm->num_pages; ++i) {
205                 from_page = shmem_read_mapping_page_gfp(swap_space, i,
206                                                         gfp_mask);
207                 if (IS_ERR(from_page)) {
208                         ret = PTR_ERR(from_page);
209                         goto out_err;
210                 }
211                 to_page = ttm->pages[i];
212                 if (unlikely(to_page == NULL)) {
213                         ret = -ENOMEM;
214                         goto out_err;
215                 }
216
217                 copy_highpage(to_page, from_page);
218                 put_page(from_page);
219         }
220
221         fput(swap_storage);
222         ttm->swap_storage = NULL;
223         ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
224
225         return 0;
226
227 out_err:
228         return ret;
229 }
230
231 /**
232  * ttm_tt_swapout - swap out tt object
233  *
234  * @bdev: TTM device structure.
235  * @ttm: The struct ttm_tt.
236  * @gfp_flags: Flags to use for memory allocation.
237  *
238  * Swapout a TT object to a shmem_file, return number of pages swapped out or
239  * negative error code.
240  */
241 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
242                    gfp_t gfp_flags)
243 {
244         loff_t size = (loff_t)ttm->num_pages << PAGE_SHIFT;
245         struct address_space *swap_space;
246         struct file *swap_storage;
247         struct page *from_page;
248         struct page *to_page;
249         int i, ret;
250
251         swap_storage = shmem_file_setup("ttm swap", size, 0);
252         if (IS_ERR(swap_storage)) {
253                 pr_err("Failed allocating swap storage\n");
254                 return PTR_ERR(swap_storage);
255         }
256
257         swap_space = swap_storage->f_mapping;
258         gfp_flags &= mapping_gfp_mask(swap_space);
259
260         for (i = 0; i < ttm->num_pages; ++i) {
261                 from_page = ttm->pages[i];
262                 if (unlikely(from_page == NULL))
263                         continue;
264
265                 to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_flags);
266                 if (IS_ERR(to_page)) {
267                         ret = PTR_ERR(to_page);
268                         goto out_err;
269                 }
270                 copy_highpage(to_page, from_page);
271                 set_page_dirty(to_page);
272                 mark_page_accessed(to_page);
273                 put_page(to_page);
274         }
275
276         ttm_tt_unpopulate(bdev, ttm);
277         ttm->swap_storage = swap_storage;
278         ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
279
280         return ttm->num_pages;
281
282 out_err:
283         fput(swap_storage);
284
285         return ret;
286 }
287
288 static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
289 {
290         pgoff_t i;
291
292         if (ttm->page_flags & TTM_PAGE_FLAG_SG)
293                 return;
294
295         for (i = 0; i < ttm->num_pages; ++i)
296                 ttm->pages[i]->mapping = bdev->dev_mapping;
297
298         atomic_long_add(ttm->num_pages, &swapable_pages);
299 }
300
301 int ttm_tt_populate(struct ttm_device *bdev,
302                     struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
303 {
304         int ret;
305
306         if (!ttm)
307                 return -EINVAL;
308
309         if (ttm_tt_is_populated(ttm))
310                 return 0;
311
312         if (bdev->funcs->ttm_tt_populate)
313                 ret = bdev->funcs->ttm_tt_populate(bdev, ttm, ctx);
314         else
315                 ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
316         if (ret)
317                 return ret;
318
319         ttm_tt_add_mapping(bdev, ttm);
320         ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
321         if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
322                 ret = ttm_tt_swapin(ttm);
323                 if (unlikely(ret != 0)) {
324                         ttm_tt_unpopulate(bdev, ttm);
325                         return ret;
326                 }
327         }
328
329         return 0;
330 }
331 EXPORT_SYMBOL(ttm_tt_populate);
332
333 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
334 {
335         pgoff_t i;
336         struct page **page = ttm->pages;
337
338         if (ttm->page_flags & TTM_PAGE_FLAG_SG)
339                 return;
340
341         for (i = 0; i < ttm->num_pages; ++i) {
342                 (*page)->mapping = NULL;
343                 (*page++)->index = 0;
344         }
345
346         atomic_long_sub(ttm->num_pages, &swapable_pages);
347 }
348
349 void ttm_tt_unpopulate(struct ttm_device *bdev,
350                        struct ttm_tt *ttm)
351 {
352         if (!ttm_tt_is_populated(ttm))
353                 return;
354
355         ttm_tt_clear_mapping(ttm);
356         if (bdev->funcs->ttm_tt_unpopulate)
357                 bdev->funcs->ttm_tt_unpopulate(bdev, ttm);
358         else
359                 ttm_pool_free(&bdev->pool, ttm);
360         ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
361 }
362
363 /* As long as pages are available make sure to release at least one */
364 static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink,
365                                           struct shrink_control *sc)
366 {
367         struct ttm_operation_ctx ctx = {
368                 .no_wait_gpu = false
369         };
370         int ret;
371
372         ret = ttm_global_swapout(&ctx, GFP_NOFS);
373         return ret < 0 ? SHRINK_EMPTY : ret;
374 }
375
376 /* Return the number of pages available or SHRINK_EMPTY if we have none */
377 static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink,
378                                            struct shrink_control *sc)
379 {
380         unsigned long num_pages;
381
382         num_pages = atomic_long_read(&swapable_pages);
383         return num_pages ? num_pages : SHRINK_EMPTY;
384 }
385
386 #ifdef CONFIG_DEBUG_FS
387
388 /* Test the shrinker functions and dump the result */
389 static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
390 {
391         struct shrink_control sc = { .gfp_mask = GFP_KERNEL };
392
393         fs_reclaim_acquire(GFP_KERNEL);
394         seq_printf(m, "%lu/%lu\n", ttm_tt_shrinker_count(&mm_shrinker, &sc),
395                    ttm_tt_shrinker_scan(&mm_shrinker, &sc));
396         fs_reclaim_release(GFP_KERNEL);
397
398         return 0;
399 }
400 DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
401
402 #endif
403
404
405
406 /**
407  * ttm_tt_mgr_init - register with the MM shrinker
408  *
409  * Register with the MM shrinker for swapping out BOs.
410  */
411 int ttm_tt_mgr_init(void)
412 {
413 #ifdef CONFIG_DEBUG_FS
414         debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
415                             &ttm_tt_debugfs_shrink_fops);
416 #endif
417
418         mm_shrinker.count_objects = ttm_tt_shrinker_count;
419         mm_shrinker.scan_objects = ttm_tt_shrinker_scan;
420         mm_shrinker.seeks = 1;
421         return register_shrinker(&mm_shrinker);
422 }
423
424 /**
425  * ttm_tt_mgr_fini - unregister our MM shrinker
426  *
427  * Unregisters the MM shrinker.
428  */
429 void ttm_tt_mgr_fini(void)
430 {
431         unregister_shrinker(&mm_shrinker);
432 }