1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #define pr_fmt(fmt) "[TTM] " fmt
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>
41 #include "ttm_module.h"
43 static struct shrinker mm_shrinker;
44 static atomic_long_t swapable_pages;
47 * Allocates a ttm structure for the given BO.
49 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
51 struct ttm_device *bdev = bo->bdev;
52 uint32_t page_flags = 0;
54 dma_resv_assert_held(bo->base.resv);
60 case ttm_bo_type_device:
62 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
64 case ttm_bo_type_kernel:
67 page_flags |= TTM_PAGE_FLAG_SG;
70 pr_err("Illegal buffer object type\n");
74 bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
75 if (unlikely(bo->ttm == NULL))
82 * Allocates storage for pointers to the pages that back the ttm.
84 static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
86 ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
87 GFP_KERNEL | __GFP_ZERO);
93 static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
95 ttm->pages = kvmalloc_array(ttm->num_pages,
97 sizeof(*ttm->dma_address),
98 GFP_KERNEL | __GFP_ZERO);
102 ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
106 static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
108 ttm->dma_address = kvmalloc_array(ttm->num_pages,
109 sizeof(*ttm->dma_address),
110 GFP_KERNEL | __GFP_ZERO);
111 if (!ttm->dma_address)
116 void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm)
118 ttm_tt_unpopulate(bdev, ttm);
120 if (ttm->swap_storage)
121 fput(ttm->swap_storage);
123 ttm->swap_storage = NULL;
125 EXPORT_SYMBOL(ttm_tt_destroy_common);
127 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
129 bdev->funcs->ttm_tt_destroy(bdev, ttm);
132 static void ttm_tt_init_fields(struct ttm_tt *ttm,
133 struct ttm_buffer_object *bo,
135 enum ttm_caching caching)
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;
143 ttm->caching = caching;
146 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
147 uint32_t page_flags, enum ttm_caching caching)
149 ttm_tt_init_fields(ttm, bo, page_flags, caching);
151 if (ttm_tt_alloc_page_directory(ttm)) {
152 pr_err("Failed allocating page table\n");
157 EXPORT_SYMBOL(ttm_tt_init);
159 void ttm_tt_fini(struct ttm_tt *ttm)
164 kvfree(ttm->dma_address);
166 ttm->dma_address = NULL;
168 EXPORT_SYMBOL(ttm_tt_fini);
170 int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
171 uint32_t page_flags, enum ttm_caching caching)
175 ttm_tt_init_fields(ttm, bo, page_flags, caching);
177 if (page_flags & TTM_PAGE_FLAG_SG)
178 ret = ttm_sg_tt_alloc_page_directory(ttm);
180 ret = ttm_dma_tt_alloc_page_directory(ttm);
182 pr_err("Failed allocating page table\n");
187 EXPORT_SYMBOL(ttm_sg_tt_init);
189 int ttm_tt_swapin(struct ttm_tt *ttm)
191 struct address_space *swap_space;
192 struct file *swap_storage;
193 struct page *from_page;
194 struct page *to_page;
198 swap_storage = ttm->swap_storage;
199 BUG_ON(swap_storage == NULL);
201 swap_space = swap_storage->f_mapping;
202 gfp_mask = mapping_gfp_mask(swap_space);
204 for (i = 0; i < ttm->num_pages; ++i) {
205 from_page = shmem_read_mapping_page_gfp(swap_space, i,
207 if (IS_ERR(from_page)) {
208 ret = PTR_ERR(from_page);
211 to_page = ttm->pages[i];
212 if (unlikely(to_page == NULL)) {
217 copy_highpage(to_page, from_page);
222 ttm->swap_storage = NULL;
223 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
232 * ttm_tt_swapout - swap out tt object
234 * @bdev: TTM device structure.
235 * @ttm: The struct ttm_tt.
236 * @gfp_flags: Flags to use for memory allocation.
238 * Swapout a TT object to a shmem_file, return number of pages swapped out or
239 * negative error code.
241 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
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;
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);
257 swap_space = swap_storage->f_mapping;
258 gfp_flags &= mapping_gfp_mask(swap_space);
260 for (i = 0; i < ttm->num_pages; ++i) {
261 from_page = ttm->pages[i];
262 if (unlikely(from_page == NULL))
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);
270 copy_highpage(to_page, from_page);
271 set_page_dirty(to_page);
272 mark_page_accessed(to_page);
276 ttm_tt_unpopulate(bdev, ttm);
277 ttm->swap_storage = swap_storage;
278 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
280 return ttm->num_pages;
288 static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
292 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
295 for (i = 0; i < ttm->num_pages; ++i)
296 ttm->pages[i]->mapping = bdev->dev_mapping;
298 atomic_long_add(ttm->num_pages, &swapable_pages);
301 int ttm_tt_populate(struct ttm_device *bdev,
302 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
309 if (ttm_tt_is_populated(ttm))
312 if (bdev->funcs->ttm_tt_populate)
313 ret = bdev->funcs->ttm_tt_populate(bdev, ttm, ctx);
315 ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
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);
331 EXPORT_SYMBOL(ttm_tt_populate);
333 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
336 struct page **page = ttm->pages;
338 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
341 for (i = 0; i < ttm->num_pages; ++i) {
342 (*page)->mapping = NULL;
343 (*page++)->index = 0;
346 atomic_long_sub(ttm->num_pages, &swapable_pages);
349 void ttm_tt_unpopulate(struct ttm_device *bdev,
352 if (!ttm_tt_is_populated(ttm))
355 ttm_tt_clear_mapping(ttm);
356 if (bdev->funcs->ttm_tt_unpopulate)
357 bdev->funcs->ttm_tt_unpopulate(bdev, ttm);
359 ttm_pool_free(&bdev->pool, ttm);
360 ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
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)
367 struct ttm_operation_ctx ctx = {
372 ret = ttm_global_swapout(&ctx, GFP_NOFS);
373 return ret < 0 ? SHRINK_EMPTY : ret;
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)
380 unsigned long num_pages;
382 num_pages = atomic_long_read(&swapable_pages);
383 return num_pages ? num_pages : SHRINK_EMPTY;
386 #ifdef CONFIG_DEBUG_FS
388 /* Test the shrinker functions and dump the result */
389 static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
391 struct shrink_control sc = { .gfp_mask = GFP_KERNEL };
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);
400 DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
407 * ttm_tt_mgr_init - register with the MM shrinker
409 * Register with the MM shrinker for swapping out BOs.
411 int ttm_tt_mgr_init(void)
413 #ifdef CONFIG_DEBUG_FS
414 debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
415 &ttm_tt_debugfs_shrink_fops);
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);
425 * ttm_tt_mgr_fini - unregister our MM shrinker
427 * Unregisters the MM shrinker.
429 void ttm_tt_mgr_fini(void)
431 unregister_shrinker(&mm_shrinker);