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