xref: /linux/drivers/gpu/drm/ttm/ttm_tt.c (revision 0ad53fe3ae82443c74ff8cfd7bd13377cc1134a3)
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/shmem_fs.h>
36 #include <linux/file.h>
37 #include <drm/drm_cache.h>
38 #include <drm/ttm/ttm_bo_driver.h>
39 
40 #include "ttm_module.h"
41 
42 static unsigned long ttm_pages_limit;
43 
44 MODULE_PARM_DESC(pages_limit, "Limit for the allocated pages");
45 module_param_named(pages_limit, ttm_pages_limit, ulong, 0644);
46 
47 static unsigned long ttm_dma32_pages_limit;
48 
49 MODULE_PARM_DESC(dma32_pages_limit, "Limit for the allocated DMA32 pages");
50 module_param_named(dma32_pages_limit, ttm_dma32_pages_limit, ulong, 0644);
51 
52 static atomic_long_t ttm_pages_allocated;
53 static atomic_long_t ttm_dma32_pages_allocated;
54 
55 /*
56  * Allocates a ttm structure for the given BO.
57  */
58 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
59 {
60 	struct ttm_device *bdev = bo->bdev;
61 	uint32_t page_flags = 0;
62 
63 	dma_resv_assert_held(bo->base.resv);
64 
65 	if (bo->ttm)
66 		return 0;
67 
68 	switch (bo->type) {
69 	case ttm_bo_type_device:
70 		if (zero_alloc)
71 			page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
72 		break;
73 	case ttm_bo_type_kernel:
74 		break;
75 	case ttm_bo_type_sg:
76 		page_flags |= TTM_PAGE_FLAG_SG;
77 		break;
78 	default:
79 		pr_err("Illegal buffer object type\n");
80 		return -EINVAL;
81 	}
82 
83 	bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
84 	if (unlikely(bo->ttm == NULL))
85 		return -ENOMEM;
86 
87 	return 0;
88 }
89 
90 /*
91  * Allocates storage for pointers to the pages that back the ttm.
92  */
93 static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
94 {
95 	ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
96 			GFP_KERNEL | __GFP_ZERO);
97 	if (!ttm->pages)
98 		return -ENOMEM;
99 	return 0;
100 }
101 
102 static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
103 {
104 	ttm->pages = kvmalloc_array(ttm->num_pages,
105 				    sizeof(*ttm->pages) +
106 				    sizeof(*ttm->dma_address),
107 				    GFP_KERNEL | __GFP_ZERO);
108 	if (!ttm->pages)
109 		return -ENOMEM;
110 
111 	ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
112 	return 0;
113 }
114 
115 static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
116 {
117 	ttm->dma_address = kvmalloc_array(ttm->num_pages,
118 					  sizeof(*ttm->dma_address),
119 					  GFP_KERNEL | __GFP_ZERO);
120 	if (!ttm->dma_address)
121 		return -ENOMEM;
122 	return 0;
123 }
124 
125 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
126 {
127 	bdev->funcs->ttm_tt_destroy(bdev, ttm);
128 }
129 
130 static void ttm_tt_init_fields(struct ttm_tt *ttm,
131 			       struct ttm_buffer_object *bo,
132 			       uint32_t page_flags,
133 			       enum ttm_caching caching)
134 {
135 	ttm->num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
136 	ttm->caching = ttm_cached;
137 	ttm->page_flags = page_flags;
138 	ttm->dma_address = NULL;
139 	ttm->swap_storage = NULL;
140 	ttm->sg = bo->sg;
141 	ttm->caching = caching;
142 }
143 
144 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
145 		uint32_t page_flags, enum ttm_caching caching)
146 {
147 	ttm_tt_init_fields(ttm, bo, page_flags, caching);
148 
149 	if (ttm_tt_alloc_page_directory(ttm)) {
150 		pr_err("Failed allocating page table\n");
151 		return -ENOMEM;
152 	}
153 	return 0;
154 }
155 EXPORT_SYMBOL(ttm_tt_init);
156 
157 void ttm_tt_fini(struct ttm_tt *ttm)
158 {
159 	WARN_ON(ttm->page_flags & TTM_PAGE_FLAG_PRIV_POPULATED);
160 
161 	if (ttm->swap_storage)
162 		fput(ttm->swap_storage);
163 	ttm->swap_storage = NULL;
164 
165 	if (ttm->pages)
166 		kvfree(ttm->pages);
167 	else
168 		kvfree(ttm->dma_address);
169 	ttm->pages = NULL;
170 	ttm->dma_address = NULL;
171 }
172 EXPORT_SYMBOL(ttm_tt_fini);
173 
174 int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
175 		   uint32_t page_flags, enum ttm_caching caching)
176 {
177 	int ret;
178 
179 	ttm_tt_init_fields(ttm, bo, page_flags, caching);
180 
181 	if (page_flags & TTM_PAGE_FLAG_SG)
182 		ret = ttm_sg_tt_alloc_page_directory(ttm);
183 	else
184 		ret = ttm_dma_tt_alloc_page_directory(ttm);
185 	if (ret) {
186 		pr_err("Failed allocating page table\n");
187 		return -ENOMEM;
188 	}
189 	return 0;
190 }
191 EXPORT_SYMBOL(ttm_sg_tt_init);
192 
193 int ttm_tt_swapin(struct ttm_tt *ttm)
194 {
195 	struct address_space *swap_space;
196 	struct file *swap_storage;
197 	struct page *from_page;
198 	struct page *to_page;
199 	gfp_t gfp_mask;
200 	int i, ret;
201 
202 	swap_storage = ttm->swap_storage;
203 	BUG_ON(swap_storage == NULL);
204 
205 	swap_space = swap_storage->f_mapping;
206 	gfp_mask = mapping_gfp_mask(swap_space);
207 
208 	for (i = 0; i < ttm->num_pages; ++i) {
209 		from_page = shmem_read_mapping_page_gfp(swap_space, i,
210 							gfp_mask);
211 		if (IS_ERR(from_page)) {
212 			ret = PTR_ERR(from_page);
213 			goto out_err;
214 		}
215 		to_page = ttm->pages[i];
216 		if (unlikely(to_page == NULL)) {
217 			ret = -ENOMEM;
218 			goto out_err;
219 		}
220 
221 		copy_highpage(to_page, from_page);
222 		put_page(from_page);
223 	}
224 
225 	fput(swap_storage);
226 	ttm->swap_storage = NULL;
227 	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
228 
229 	return 0;
230 
231 out_err:
232 	return ret;
233 }
234 
235 /**
236  * ttm_tt_swapout - swap out tt object
237  *
238  * @bdev: TTM device structure.
239  * @ttm: The struct ttm_tt.
240  * @gfp_flags: Flags to use for memory allocation.
241  *
242  * Swapout a TT object to a shmem_file, return number of pages swapped out or
243  * negative error code.
244  */
245 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
246 		   gfp_t gfp_flags)
247 {
248 	loff_t size = (loff_t)ttm->num_pages << PAGE_SHIFT;
249 	struct address_space *swap_space;
250 	struct file *swap_storage;
251 	struct page *from_page;
252 	struct page *to_page;
253 	int i, ret;
254 
255 	swap_storage = shmem_file_setup("ttm swap", size, 0);
256 	if (IS_ERR(swap_storage)) {
257 		pr_err("Failed allocating swap storage\n");
258 		return PTR_ERR(swap_storage);
259 	}
260 
261 	swap_space = swap_storage->f_mapping;
262 	gfp_flags &= mapping_gfp_mask(swap_space);
263 
264 	for (i = 0; i < ttm->num_pages; ++i) {
265 		from_page = ttm->pages[i];
266 		if (unlikely(from_page == NULL))
267 			continue;
268 
269 		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_flags);
270 		if (IS_ERR(to_page)) {
271 			ret = PTR_ERR(to_page);
272 			goto out_err;
273 		}
274 		copy_highpage(to_page, from_page);
275 		set_page_dirty(to_page);
276 		mark_page_accessed(to_page);
277 		put_page(to_page);
278 	}
279 
280 	ttm_tt_unpopulate(bdev, ttm);
281 	ttm->swap_storage = swap_storage;
282 	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
283 
284 	return ttm->num_pages;
285 
286 out_err:
287 	fput(swap_storage);
288 
289 	return ret;
290 }
291 
292 static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
293 {
294 	pgoff_t i;
295 
296 	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
297 		return;
298 
299 	for (i = 0; i < ttm->num_pages; ++i)
300 		ttm->pages[i]->mapping = bdev->dev_mapping;
301 }
302 
303 int ttm_tt_populate(struct ttm_device *bdev,
304 		    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
305 {
306 	int ret;
307 
308 	if (!ttm)
309 		return -EINVAL;
310 
311 	if (ttm_tt_is_populated(ttm))
312 		return 0;
313 
314 	if (!(ttm->page_flags & TTM_PAGE_FLAG_SG)) {
315 		atomic_long_add(ttm->num_pages, &ttm_pages_allocated);
316 		if (bdev->pool.use_dma32)
317 			atomic_long_add(ttm->num_pages,
318 					&ttm_dma32_pages_allocated);
319 	}
320 
321 	while (atomic_long_read(&ttm_pages_allocated) > ttm_pages_limit ||
322 	       atomic_long_read(&ttm_dma32_pages_allocated) >
323 	       ttm_dma32_pages_limit) {
324 
325 		ret = ttm_global_swapout(ctx, GFP_KERNEL);
326 		if (ret == 0)
327 			break;
328 		if (ret < 0)
329 			goto error;
330 	}
331 
332 	if (bdev->funcs->ttm_tt_populate)
333 		ret = bdev->funcs->ttm_tt_populate(bdev, ttm, ctx);
334 	else
335 		ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
336 	if (ret)
337 		goto error;
338 
339 	ttm_tt_add_mapping(bdev, ttm);
340 	ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
341 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
342 		ret = ttm_tt_swapin(ttm);
343 		if (unlikely(ret != 0)) {
344 			ttm_tt_unpopulate(bdev, ttm);
345 			return ret;
346 		}
347 	}
348 
349 	return 0;
350 
351 error:
352 	if (!(ttm->page_flags & TTM_PAGE_FLAG_SG)) {
353 		atomic_long_sub(ttm->num_pages, &ttm_pages_allocated);
354 		if (bdev->pool.use_dma32)
355 			atomic_long_sub(ttm->num_pages,
356 					&ttm_dma32_pages_allocated);
357 	}
358 	return ret;
359 }
360 EXPORT_SYMBOL(ttm_tt_populate);
361 
362 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
363 {
364 	pgoff_t i;
365 	struct page **page = ttm->pages;
366 
367 	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
368 		return;
369 
370 	for (i = 0; i < ttm->num_pages; ++i) {
371 		(*page)->mapping = NULL;
372 		(*page++)->index = 0;
373 	}
374 }
375 
376 void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
377 {
378 	if (!ttm_tt_is_populated(ttm))
379 		return;
380 
381 	ttm_tt_clear_mapping(ttm);
382 	if (bdev->funcs->ttm_tt_unpopulate)
383 		bdev->funcs->ttm_tt_unpopulate(bdev, ttm);
384 	else
385 		ttm_pool_free(&bdev->pool, ttm);
386 
387 	if (!(ttm->page_flags & TTM_PAGE_FLAG_SG)) {
388 		atomic_long_sub(ttm->num_pages, &ttm_pages_allocated);
389 		if (bdev->pool.use_dma32)
390 			atomic_long_sub(ttm->num_pages,
391 					&ttm_dma32_pages_allocated);
392 	}
393 
394 	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
395 }
396 
397 #ifdef CONFIG_DEBUG_FS
398 
399 /* Test the shrinker functions and dump the result */
400 static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
401 {
402 	struct ttm_operation_ctx ctx = { false, false };
403 
404 	seq_printf(m, "%d\n", ttm_global_swapout(&ctx, GFP_KERNEL));
405 	return 0;
406 }
407 DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
408 
409 #endif
410 
411 
412 /*
413  * ttm_tt_mgr_init - register with the MM shrinker
414  *
415  * Register with the MM shrinker for swapping out BOs.
416  */
417 void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages)
418 {
419 #ifdef CONFIG_DEBUG_FS
420 	debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
421 			    &ttm_tt_debugfs_shrink_fops);
422 #endif
423 
424 	if (!ttm_pages_limit)
425 		ttm_pages_limit = num_pages;
426 
427 	if (!ttm_dma32_pages_limit)
428 		ttm_dma32_pages_limit = num_dma32_pages;
429 }
430 
431 static void ttm_kmap_iter_tt_map_local(struct ttm_kmap_iter *iter,
432 				       struct dma_buf_map *dmap,
433 				       pgoff_t i)
434 {
435 	struct ttm_kmap_iter_tt *iter_tt =
436 		container_of(iter, typeof(*iter_tt), base);
437 
438 	dma_buf_map_set_vaddr(dmap, kmap_local_page_prot(iter_tt->tt->pages[i],
439 							 iter_tt->prot));
440 }
441 
442 static void ttm_kmap_iter_tt_unmap_local(struct ttm_kmap_iter *iter,
443 					 struct dma_buf_map *map)
444 {
445 	kunmap_local(map->vaddr);
446 }
447 
448 static const struct ttm_kmap_iter_ops ttm_kmap_iter_tt_ops = {
449 	.map_local = ttm_kmap_iter_tt_map_local,
450 	.unmap_local = ttm_kmap_iter_tt_unmap_local,
451 	.maps_tt = true,
452 };
453 
454 /**
455  * ttm_kmap_iter_tt_init - Initialize a struct ttm_kmap_iter_tt
456  * @iter_tt: The struct ttm_kmap_iter_tt to initialize.
457  * @tt: Struct ttm_tt holding page pointers of the struct ttm_resource.
458  *
459  * Return: Pointer to the embedded struct ttm_kmap_iter.
460  */
461 struct ttm_kmap_iter *
462 ttm_kmap_iter_tt_init(struct ttm_kmap_iter_tt *iter_tt,
463 		      struct ttm_tt *tt)
464 {
465 	iter_tt->base.ops = &ttm_kmap_iter_tt_ops;
466 	iter_tt->tt = tt;
467 	if (tt)
468 		iter_tt->prot = ttm_prot_from_caching(tt->caching, PAGE_KERNEL);
469 	else
470 		iter_tt->prot = PAGE_KERNEL;
471 
472 	return &iter_tt->base;
473 }
474 EXPORT_SYMBOL(ttm_kmap_iter_tt_init);
475