xref: /linux/drivers/gpu/drm/ttm/ttm_bo_util.c (revision 33b4e4fcd2980ee5fd754731ca9b0325f0344f04)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2007-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 #include <linux/export.h>
33 #include <linux/swap.h>
34 #include <linux/vmalloc.h>
35 
36 #include <drm/ttm/ttm_bo.h>
37 #include <drm/ttm/ttm_placement.h>
38 #include <drm/ttm/ttm_tt.h>
39 
40 #include <drm/drm_cache.h>
41 
42 #include "ttm_bo_internal.h"
43 
44 struct ttm_transfer_obj {
45 	struct ttm_buffer_object base;
46 	struct ttm_buffer_object *bo;
47 };
48 
49 int ttm_mem_io_reserve(struct ttm_device *bdev,
50 		       struct ttm_resource *mem)
51 {
52 	if (mem->bus.offset || mem->bus.addr)
53 		return 0;
54 
55 	mem->bus.is_iomem = false;
56 	if (!bdev->funcs->io_mem_reserve)
57 		return 0;
58 
59 	return bdev->funcs->io_mem_reserve(bdev, mem);
60 }
61 
62 void ttm_mem_io_free(struct ttm_device *bdev,
63 		     struct ttm_resource *mem)
64 {
65 	if (!mem)
66 		return;
67 
68 	if (!mem->bus.offset && !mem->bus.addr)
69 		return;
70 
71 	if (bdev->funcs->io_mem_free)
72 		bdev->funcs->io_mem_free(bdev, mem);
73 
74 	mem->bus.offset = 0;
75 	mem->bus.addr = NULL;
76 }
77 
78 /**
79  * ttm_move_memcpy - Helper to perform a memcpy ttm move operation.
80  * @clear: Whether to clear rather than copy.
81  * @num_pages: Number of pages of the operation.
82  * @dst_iter: A struct ttm_kmap_iter representing the destination resource.
83  * @src_iter: A struct ttm_kmap_iter representing the source resource.
84  *
85  * This function is intended to be able to move out async under a
86  * dma-fence if desired.
87  */
88 void ttm_move_memcpy(bool clear,
89 		     u32 num_pages,
90 		     struct ttm_kmap_iter *dst_iter,
91 		     struct ttm_kmap_iter *src_iter)
92 {
93 	const struct ttm_kmap_iter_ops *dst_ops = dst_iter->ops;
94 	const struct ttm_kmap_iter_ops *src_ops = src_iter->ops;
95 	struct iosys_map src_map, dst_map;
96 	pgoff_t i;
97 
98 	/* Single TTM move. NOP */
99 	if (dst_ops->maps_tt && src_ops->maps_tt)
100 		return;
101 
102 	/* Don't move nonexistent data. Clear destination instead. */
103 	if (clear) {
104 		for (i = 0; i < num_pages; ++i) {
105 			dst_ops->map_local(dst_iter, &dst_map, i);
106 			if (dst_map.is_iomem)
107 				memset_io(dst_map.vaddr_iomem, 0, PAGE_SIZE);
108 			else
109 				memset(dst_map.vaddr, 0, PAGE_SIZE);
110 			if (dst_ops->unmap_local)
111 				dst_ops->unmap_local(dst_iter, &dst_map);
112 		}
113 		return;
114 	}
115 
116 	for (i = 0; i < num_pages; ++i) {
117 		dst_ops->map_local(dst_iter, &dst_map, i);
118 		src_ops->map_local(src_iter, &src_map, i);
119 
120 		drm_memcpy_from_wc(&dst_map, &src_map, PAGE_SIZE);
121 
122 		if (src_ops->unmap_local)
123 			src_ops->unmap_local(src_iter, &src_map);
124 		if (dst_ops->unmap_local)
125 			dst_ops->unmap_local(dst_iter, &dst_map);
126 	}
127 }
128 EXPORT_SYMBOL(ttm_move_memcpy);
129 
130 /**
131  * ttm_bo_move_memcpy
132  *
133  * @bo: A pointer to a struct ttm_buffer_object.
134  * @ctx: operation context
135  * @dst_mem: struct ttm_resource indicating where to move.
136  *
137  * Fallback move function for a mappable buffer object in mappable memory.
138  * The function will, if successful,
139  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
140  * and update the (@bo)->mem placement flags. If unsuccessful, the old
141  * data remains untouched, and it's up to the caller to free the
142  * memory space indicated by @new_mem.
143  * Returns:
144  * !0: Failure.
145  */
146 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
147 		       struct ttm_operation_ctx *ctx,
148 		       struct ttm_resource *dst_mem)
149 {
150 	struct ttm_device *bdev = bo->bdev;
151 	struct ttm_resource_manager *dst_man =
152 		ttm_manager_type(bo->bdev, dst_mem->mem_type);
153 	struct ttm_tt *ttm = bo->ttm;
154 	struct ttm_resource *src_mem = bo->resource;
155 	struct ttm_resource_manager *src_man;
156 	union {
157 		struct ttm_kmap_iter_tt tt;
158 		struct ttm_kmap_iter_linear_io io;
159 	} _dst_iter, _src_iter;
160 	struct ttm_kmap_iter *dst_iter, *src_iter;
161 	bool clear;
162 	int ret = 0;
163 
164 	if (WARN_ON(!src_mem))
165 		return -EINVAL;
166 
167 	src_man = ttm_manager_type(bdev, src_mem->mem_type);
168 	if (ttm && ((ttm->page_flags & TTM_TT_FLAG_SWAPPED) ||
169 		    dst_man->use_tt)) {
170 		ret = ttm_bo_populate(bo, ctx);
171 		if (ret)
172 			return ret;
173 	}
174 
175 	dst_iter = ttm_kmap_iter_linear_io_init(&_dst_iter.io, bdev, dst_mem);
176 	if (PTR_ERR(dst_iter) == -EINVAL && dst_man->use_tt)
177 		dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, bo->ttm);
178 	if (IS_ERR(dst_iter))
179 		return PTR_ERR(dst_iter);
180 
181 	src_iter = ttm_kmap_iter_linear_io_init(&_src_iter.io, bdev, src_mem);
182 	if (PTR_ERR(src_iter) == -EINVAL && src_man->use_tt)
183 		src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm);
184 	if (IS_ERR(src_iter)) {
185 		ret = PTR_ERR(src_iter);
186 		goto out_src_iter;
187 	}
188 
189 	clear = src_iter->ops->maps_tt && (!ttm || !ttm_tt_is_populated(ttm));
190 	if (!(clear && ttm && !(ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC)))
191 		ttm_move_memcpy(clear, PFN_UP(dst_mem->size), dst_iter, src_iter);
192 
193 	if (!src_iter->ops->maps_tt)
194 		ttm_kmap_iter_linear_io_fini(&_src_iter.io, bdev, src_mem);
195 	ttm_bo_move_sync_cleanup(bo, dst_mem);
196 
197 out_src_iter:
198 	if (!dst_iter->ops->maps_tt)
199 		ttm_kmap_iter_linear_io_fini(&_dst_iter.io, bdev, dst_mem);
200 
201 	return ret;
202 }
203 EXPORT_SYMBOL(ttm_bo_move_memcpy);
204 
205 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
206 {
207 	struct ttm_transfer_obj *fbo;
208 
209 	fbo = container_of(bo, struct ttm_transfer_obj, base);
210 	dma_resv_fini(&fbo->base.base._resv);
211 	ttm_bo_put(fbo->bo);
212 	kfree(fbo);
213 }
214 
215 /**
216  * ttm_buffer_object_transfer
217  *
218  * @bo: A pointer to a struct ttm_buffer_object.
219  * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
220  * holding the data of @bo with the old placement.
221  *
222  * This is a utility function that may be called after an accelerated move
223  * has been scheduled. A new buffer object is created as a placeholder for
224  * the old data while it's being copied. When that buffer object is idle,
225  * it can be destroyed, releasing the space of the old placement.
226  * Returns:
227  * !0: Failure.
228  */
229 
230 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
231 				      struct ttm_buffer_object **new_obj)
232 {
233 	struct ttm_transfer_obj *fbo;
234 	int ret;
235 
236 	fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
237 	if (!fbo)
238 		return -ENOMEM;
239 
240 	fbo->base = *bo;
241 
242 	/**
243 	 * Fix up members that we shouldn't copy directly:
244 	 * TODO: Explicit member copy would probably be better here.
245 	 */
246 
247 	atomic_inc(&ttm_glob.bo_count);
248 	drm_vma_node_reset(&fbo->base.base.vma_node);
249 
250 	kref_init(&fbo->base.kref);
251 	fbo->base.destroy = &ttm_transfered_destroy;
252 	fbo->base.pin_count = 0;
253 	if (bo->type != ttm_bo_type_sg)
254 		fbo->base.base.resv = &fbo->base.base._resv;
255 
256 	dma_resv_init(&fbo->base.base._resv);
257 	fbo->base.base.dev = NULL;
258 	ret = dma_resv_trylock(&fbo->base.base._resv);
259 	WARN_ON(!ret);
260 
261 	if (fbo->base.resource) {
262 		ttm_resource_set_bo(fbo->base.resource, &fbo->base);
263 		bo->resource = NULL;
264 		ttm_bo_set_bulk_move(&fbo->base, NULL);
265 	} else {
266 		fbo->base.bulk_move = NULL;
267 	}
268 
269 	ret = dma_resv_reserve_fences(&fbo->base.base._resv, 1);
270 	if (ret) {
271 		kfree(fbo);
272 		return ret;
273 	}
274 
275 	ttm_bo_get(bo);
276 	fbo->bo = bo;
277 
278 	ttm_bo_move_to_lru_tail_unlocked(&fbo->base);
279 
280 	*new_obj = &fbo->base;
281 	return 0;
282 }
283 
284 /**
285  * ttm_io_prot
286  *
287  * @bo: ttm buffer object
288  * @res: ttm resource object
289  * @tmp: Page protection flag for a normal, cached mapping.
290  *
291  * Utility function that returns the pgprot_t that should be used for
292  * setting up a PTE with the caching model indicated by @c_state.
293  */
294 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
295 		     pgprot_t tmp)
296 {
297 	struct ttm_resource_manager *man;
298 	enum ttm_caching caching;
299 
300 	man = ttm_manager_type(bo->bdev, res->mem_type);
301 	if (man->use_tt) {
302 		caching = bo->ttm->caching;
303 		if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
304 			tmp = pgprot_decrypted(tmp);
305 	} else  {
306 		caching = res->bus.caching;
307 	}
308 
309 	return ttm_prot_from_caching(caching, tmp);
310 }
311 EXPORT_SYMBOL(ttm_io_prot);
312 
313 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
314 			  unsigned long offset,
315 			  unsigned long size,
316 			  struct ttm_bo_kmap_obj *map)
317 {
318 	struct ttm_resource *mem = bo->resource;
319 
320 	if (bo->resource->bus.addr) {
321 		map->bo_kmap_type = ttm_bo_map_premapped;
322 		map->virtual = ((u8 *)bo->resource->bus.addr) + offset;
323 	} else {
324 		resource_size_t res = bo->resource->bus.offset + offset;
325 
326 		map->bo_kmap_type = ttm_bo_map_iomap;
327 		if (mem->bus.caching == ttm_write_combined)
328 			map->virtual = ioremap_wc(res, size);
329 #ifdef CONFIG_X86
330 		else if (mem->bus.caching == ttm_cached)
331 			map->virtual = ioremap_cache(res, size);
332 #endif
333 		else
334 			map->virtual = ioremap(res, size);
335 	}
336 	return (!map->virtual) ? -ENOMEM : 0;
337 }
338 
339 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
340 			   unsigned long start_page,
341 			   unsigned long num_pages,
342 			   struct ttm_bo_kmap_obj *map)
343 {
344 	struct ttm_resource *mem = bo->resource;
345 	struct ttm_operation_ctx ctx = {
346 		.interruptible = false,
347 		.no_wait_gpu = false
348 	};
349 	struct ttm_tt *ttm = bo->ttm;
350 	struct ttm_resource_manager *man =
351 			ttm_manager_type(bo->bdev, bo->resource->mem_type);
352 	pgprot_t prot;
353 	int ret;
354 
355 	BUG_ON(!ttm);
356 
357 	ret = ttm_bo_populate(bo, &ctx);
358 	if (ret)
359 		return ret;
360 
361 	if (num_pages == 1 && ttm->caching == ttm_cached &&
362 	    !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
363 		/*
364 		 * We're mapping a single page, and the desired
365 		 * page protection is consistent with the bo.
366 		 */
367 
368 		map->bo_kmap_type = ttm_bo_map_kmap;
369 		map->page = ttm->pages[start_page];
370 		map->virtual = kmap(map->page);
371 	} else {
372 		/*
373 		 * We need to use vmap to get the desired page protection
374 		 * or to make the buffer object look contiguous.
375 		 */
376 		prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
377 		map->bo_kmap_type = ttm_bo_map_vmap;
378 		map->virtual = vmap(ttm->pages + start_page, num_pages,
379 				    0, prot);
380 	}
381 	return (!map->virtual) ? -ENOMEM : 0;
382 }
383 
384 /**
385  * ttm_bo_kmap
386  *
387  * @bo: The buffer object.
388  * @start_page: The first page to map.
389  * @num_pages: Number of pages to map.
390  * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
391  *
392  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
393  * data in the buffer object. The ttm_kmap_obj_virtual function can then be
394  * used to obtain a virtual address to the data.
395  *
396  * Returns
397  * -ENOMEM: Out of memory.
398  * -EINVAL: Invalid range.
399  */
400 int ttm_bo_kmap(struct ttm_buffer_object *bo,
401 		unsigned long start_page, unsigned long num_pages,
402 		struct ttm_bo_kmap_obj *map)
403 {
404 	unsigned long offset, size;
405 	int ret;
406 
407 	map->virtual = NULL;
408 	map->bo = bo;
409 	if (num_pages > PFN_UP(bo->resource->size))
410 		return -EINVAL;
411 	if ((start_page + num_pages) > PFN_UP(bo->resource->size))
412 		return -EINVAL;
413 
414 	ret = ttm_mem_io_reserve(bo->bdev, bo->resource);
415 	if (ret)
416 		return ret;
417 	if (!bo->resource->bus.is_iomem) {
418 		return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
419 	} else {
420 		offset = start_page << PAGE_SHIFT;
421 		size = num_pages << PAGE_SHIFT;
422 		return ttm_bo_ioremap(bo, offset, size, map);
423 	}
424 }
425 EXPORT_SYMBOL(ttm_bo_kmap);
426 
427 /**
428  * ttm_bo_kunmap
429  *
430  * @map: Object describing the map to unmap.
431  *
432  * Unmaps a kernel map set up by ttm_bo_kmap.
433  */
434 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
435 {
436 	if (!map->virtual)
437 		return;
438 	switch (map->bo_kmap_type) {
439 	case ttm_bo_map_iomap:
440 		iounmap(map->virtual);
441 		break;
442 	case ttm_bo_map_vmap:
443 		vunmap(map->virtual);
444 		break;
445 	case ttm_bo_map_kmap:
446 		kunmap(map->page);
447 		break;
448 	case ttm_bo_map_premapped:
449 		break;
450 	default:
451 		BUG();
452 	}
453 	ttm_mem_io_free(map->bo->bdev, map->bo->resource);
454 	map->virtual = NULL;
455 	map->page = NULL;
456 }
457 EXPORT_SYMBOL(ttm_bo_kunmap);
458 
459 /**
460  * ttm_bo_vmap
461  *
462  * @bo: The buffer object.
463  * @map: pointer to a struct iosys_map representing the map.
464  *
465  * Sets up a kernel virtual mapping, using ioremap or vmap to the
466  * data in the buffer object. The parameter @map returns the virtual
467  * address as struct iosys_map. Unmap the buffer with ttm_bo_vunmap().
468  *
469  * Returns
470  * -ENOMEM: Out of memory.
471  * -EINVAL: Invalid range.
472  */
473 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map)
474 {
475 	struct ttm_resource *mem = bo->resource;
476 	int ret;
477 
478 	dma_resv_assert_held(bo->base.resv);
479 
480 	ret = ttm_mem_io_reserve(bo->bdev, mem);
481 	if (ret)
482 		return ret;
483 
484 	if (mem->bus.is_iomem) {
485 		void __iomem *vaddr_iomem;
486 
487 		if (mem->bus.addr)
488 			vaddr_iomem = (void __iomem *)mem->bus.addr;
489 		else if (mem->bus.caching == ttm_write_combined)
490 			vaddr_iomem = ioremap_wc(mem->bus.offset,
491 						 bo->base.size);
492 #ifdef CONFIG_X86
493 		else if (mem->bus.caching == ttm_cached)
494 			vaddr_iomem = ioremap_cache(mem->bus.offset,
495 						  bo->base.size);
496 #endif
497 		else
498 			vaddr_iomem = ioremap(mem->bus.offset, bo->base.size);
499 
500 		if (!vaddr_iomem)
501 			return -ENOMEM;
502 
503 		iosys_map_set_vaddr_iomem(map, vaddr_iomem);
504 
505 	} else {
506 		struct ttm_operation_ctx ctx = {
507 			.interruptible = false,
508 			.no_wait_gpu = false
509 		};
510 		struct ttm_tt *ttm = bo->ttm;
511 		pgprot_t prot;
512 		void *vaddr;
513 
514 		ret = ttm_bo_populate(bo, &ctx);
515 		if (ret)
516 			return ret;
517 
518 		/*
519 		 * We need to use vmap to get the desired page protection
520 		 * or to make the buffer object look contiguous.
521 		 */
522 		prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
523 		vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot);
524 		if (!vaddr)
525 			return -ENOMEM;
526 
527 		iosys_map_set_vaddr(map, vaddr);
528 	}
529 
530 	return 0;
531 }
532 EXPORT_SYMBOL(ttm_bo_vmap);
533 
534 /**
535  * ttm_bo_vunmap
536  *
537  * @bo: The buffer object.
538  * @map: Object describing the map to unmap.
539  *
540  * Unmaps a kernel map set up by ttm_bo_vmap().
541  */
542 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map)
543 {
544 	struct ttm_resource *mem = bo->resource;
545 
546 	dma_resv_assert_held(bo->base.resv);
547 
548 	if (iosys_map_is_null(map))
549 		return;
550 
551 	if (!map->is_iomem)
552 		vunmap(map->vaddr);
553 	else if (!mem->bus.addr)
554 		iounmap(map->vaddr_iomem);
555 	iosys_map_clear(map);
556 
557 	ttm_mem_io_free(bo->bdev, bo->resource);
558 }
559 EXPORT_SYMBOL(ttm_bo_vunmap);
560 
561 static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
562 				 bool dst_use_tt)
563 {
564 	long ret;
565 
566 	ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
567 				    false, 15 * HZ);
568 	if (ret == 0)
569 		return -EBUSY;
570 	if (ret < 0)
571 		return ret;
572 
573 	if (!dst_use_tt)
574 		ttm_bo_tt_destroy(bo);
575 	ttm_resource_free(bo, &bo->resource);
576 	return 0;
577 }
578 
579 static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
580 				struct dma_fence *fence,
581 				bool dst_use_tt)
582 {
583 	struct ttm_buffer_object *ghost_obj;
584 	int ret;
585 
586 	/**
587 	 * This should help pipeline ordinary buffer moves.
588 	 *
589 	 * Hang old buffer memory on a new buffer object,
590 	 * and leave it to be released when the GPU
591 	 * operation has completed.
592 	 */
593 
594 	ret = ttm_buffer_object_transfer(bo, &ghost_obj);
595 	if (ret)
596 		return ret;
597 
598 	dma_resv_add_fence(&ghost_obj->base._resv, fence,
599 			   DMA_RESV_USAGE_KERNEL);
600 
601 	/**
602 	 * If we're not moving to fixed memory, the TTM object
603 	 * needs to stay alive. Otherwhise hang it on the ghost
604 	 * bo to be unbound and destroyed.
605 	 */
606 
607 	if (dst_use_tt)
608 		ghost_obj->ttm = NULL;
609 	else
610 		bo->ttm = NULL;
611 
612 	dma_resv_unlock(&ghost_obj->base._resv);
613 	ttm_bo_put(ghost_obj);
614 	return 0;
615 }
616 
617 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
618 				       struct dma_fence *fence)
619 {
620 	struct ttm_device *bdev = bo->bdev;
621 	struct ttm_resource_manager *from;
622 
623 	from = ttm_manager_type(bdev, bo->resource->mem_type);
624 
625 	/**
626 	 * BO doesn't have a TTM we need to bind/unbind. Just remember
627 	 * this eviction and free up the allocation
628 	 */
629 	spin_lock(&from->move_lock);
630 	if (!from->move || dma_fence_is_later(fence, from->move)) {
631 		dma_fence_put(from->move);
632 		from->move = dma_fence_get(fence);
633 	}
634 	spin_unlock(&from->move_lock);
635 
636 	ttm_resource_free(bo, &bo->resource);
637 }
638 
639 /**
640  * ttm_bo_move_accel_cleanup - cleanup helper for hw copies
641  *
642  * @bo: A pointer to a struct ttm_buffer_object.
643  * @fence: A fence object that signals when moving is complete.
644  * @evict: This is an evict move. Don't return until the buffer is idle.
645  * @pipeline: evictions are to be pipelined.
646  * @new_mem: struct ttm_resource indicating where to move.
647  *
648  * Accelerated move function to be called when an accelerated move
649  * has been scheduled. The function will create a new temporary buffer object
650  * representing the old placement, and put the sync object on both buffer
651  * objects. After that the newly created buffer object is unref'd to be
652  * destroyed when the move is complete. This will help pipeline
653  * buffer moves.
654  */
655 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
656 			      struct dma_fence *fence,
657 			      bool evict,
658 			      bool pipeline,
659 			      struct ttm_resource *new_mem)
660 {
661 	struct ttm_device *bdev = bo->bdev;
662 	struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->resource->mem_type);
663 	struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
664 	int ret = 0;
665 
666 	dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
667 	if (!evict)
668 		ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
669 	else if (!from->use_tt && pipeline)
670 		ttm_bo_move_pipeline_evict(bo, fence);
671 	else
672 		ret = ttm_bo_wait_free_node(bo, man->use_tt);
673 
674 	if (ret)
675 		return ret;
676 
677 	ttm_bo_assign_mem(bo, new_mem);
678 
679 	return 0;
680 }
681 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
682 
683 /**
684  * ttm_bo_move_sync_cleanup - cleanup by waiting for the move to finish
685  *
686  * @bo: A pointer to a struct ttm_buffer_object.
687  * @new_mem: struct ttm_resource indicating where to move.
688  *
689  * Special case of ttm_bo_move_accel_cleanup where the bo is guaranteed
690  * by the caller to be idle. Typically used after memcpy buffer moves.
691  */
692 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
693 			      struct ttm_resource *new_mem)
694 {
695 	struct ttm_device *bdev = bo->bdev;
696 	struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
697 	int ret;
698 
699 	ret = ttm_bo_wait_free_node(bo, man->use_tt);
700 	if (WARN_ON(ret))
701 		return;
702 
703 	ttm_bo_assign_mem(bo, new_mem);
704 }
705 EXPORT_SYMBOL(ttm_bo_move_sync_cleanup);
706 
707 /**
708  * ttm_bo_pipeline_gutting - purge the contents of a bo
709  * @bo: The buffer object
710  *
711  * Purge the contents of a bo, async if the bo is not idle.
712  * After a successful call, the bo is left unpopulated in
713  * system placement. The function may wait uninterruptible
714  * for idle on OOM.
715  *
716  * Return: 0 if successful, negative error code on failure.
717  */
718 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
719 {
720 	struct ttm_buffer_object *ghost;
721 	struct ttm_tt *ttm;
722 	int ret;
723 
724 	/* If already idle, no need for ghost object dance. */
725 	if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP)) {
726 		if (!bo->ttm) {
727 			/* See comment below about clearing. */
728 			ret = ttm_tt_create(bo, true);
729 			if (ret)
730 				return ret;
731 		} else {
732 			ttm_tt_unpopulate(bo->bdev, bo->ttm);
733 			if (bo->type == ttm_bo_type_device)
734 				ttm_tt_mark_for_clear(bo->ttm);
735 		}
736 		ttm_resource_free(bo, &bo->resource);
737 		return 0;
738 	}
739 
740 	/*
741 	 * We need an unpopulated ttm_tt after giving our current one,
742 	 * if any, to the ghost object. And we can't afford to fail
743 	 * creating one *after* the operation. If the bo subsequently gets
744 	 * resurrected, make sure it's cleared (if ttm_bo_type_device)
745 	 * to avoid leaking sensitive information to user-space.
746 	 */
747 
748 	ttm = bo->ttm;
749 	bo->ttm = NULL;
750 	ret = ttm_tt_create(bo, true);
751 	swap(bo->ttm, ttm);
752 	if (ret)
753 		return ret;
754 
755 	ret = ttm_buffer_object_transfer(bo, &ghost);
756 	if (ret)
757 		goto error_destroy_tt;
758 
759 	ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
760 	/* Last resort, wait for the BO to be idle when we are OOM */
761 	if (ret) {
762 		dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
763 				      false, MAX_SCHEDULE_TIMEOUT);
764 	}
765 
766 	dma_resv_unlock(&ghost->base._resv);
767 	ttm_bo_put(ghost);
768 	bo->ttm = ttm;
769 	return 0;
770 
771 error_destroy_tt:
772 	ttm_tt_destroy(bo->bdev, ttm);
773 	return ret;
774 }
775 
776 static bool ttm_lru_walk_trylock(struct ttm_operation_ctx *ctx,
777 				 struct ttm_buffer_object *bo,
778 				 bool *needs_unlock)
779 {
780 	*needs_unlock = false;
781 
782 	if (dma_resv_trylock(bo->base.resv)) {
783 		*needs_unlock = true;
784 		return true;
785 	}
786 
787 	if (bo->base.resv == ctx->resv && ctx->allow_res_evict) {
788 		dma_resv_assert_held(bo->base.resv);
789 		return true;
790 	}
791 
792 	return false;
793 }
794 
795 static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk,
796 				   struct ttm_buffer_object *bo,
797 				   bool *needs_unlock)
798 {
799 	struct dma_resv *resv = bo->base.resv;
800 	int ret;
801 
802 	if (walk->ctx->interruptible)
803 		ret = dma_resv_lock_interruptible(resv, walk->ticket);
804 	else
805 		ret = dma_resv_lock(resv, walk->ticket);
806 
807 	if (!ret) {
808 		*needs_unlock = true;
809 		/*
810 		 * Only a single ticketlock per loop. Ticketlocks are prone
811 		 * to return -EDEADLK causing the eviction to fail, so
812 		 * after waiting for the ticketlock, revert back to
813 		 * trylocking for this walk.
814 		 */
815 		walk->ticket = NULL;
816 	} else if (ret == -EDEADLK) {
817 		/* Caller needs to exit the ww transaction. */
818 		ret = -ENOSPC;
819 	}
820 
821 	return ret;
822 }
823 
824 static void ttm_lru_walk_unlock(struct ttm_buffer_object *bo, bool locked)
825 {
826 	if (locked)
827 		dma_resv_unlock(bo->base.resv);
828 }
829 
830 /**
831  * ttm_lru_walk_for_evict() - Perform a LRU list walk, with actions taken on
832  * valid items.
833  * @walk: describe the walks and actions taken
834  * @bdev: The TTM device.
835  * @man: The struct ttm_resource manager whose LRU lists we're walking.
836  * @target: The end condition for the walk.
837  *
838  * The LRU lists of @man are walk, and for each struct ttm_resource encountered,
839  * the corresponding ttm_buffer_object is locked and taken a reference on, and
840  * the LRU lock is dropped. the LRU lock may be dropped before locking and, in
841  * that case, it's verified that the item actually remains on the LRU list after
842  * the lock, and that the buffer object didn't switch resource in between.
843  *
844  * With a locked object, the actions indicated by @walk->process_bo are
845  * performed, and after that, the bo is unlocked, the refcount dropped and the
846  * next struct ttm_resource is processed. Here, the walker relies on
847  * TTM's restartable LRU list implementation.
848  *
849  * Typically @walk->process_bo() would return the number of pages evicted,
850  * swapped or shrunken, so that when the total exceeds @target, or when the
851  * LRU list has been walked in full, iteration is terminated. It's also terminated
852  * on error. Note that the definition of @target is done by the caller, it
853  * could have a different meaning than the number of pages.
854  *
855  * Note that the way dma_resv individualization is done, locking needs to be done
856  * either with the LRU lock held (trylocking only) or with a reference on the
857  * object.
858  *
859  * Return: The progress made towards target or negative error code on error.
860  */
861 s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
862 			   struct ttm_resource_manager *man, s64 target)
863 {
864 	struct ttm_resource_cursor cursor;
865 	struct ttm_resource *res;
866 	s64 progress = 0;
867 	s64 lret;
868 
869 	spin_lock(&bdev->lru_lock);
870 	ttm_resource_cursor_init(&cursor, man);
871 	ttm_resource_manager_for_each_res(&cursor, res) {
872 		struct ttm_buffer_object *bo = res->bo;
873 		bool bo_needs_unlock = false;
874 		bool bo_locked = false;
875 		int mem_type;
876 
877 		/*
878 		 * Attempt a trylock before taking a reference on the bo,
879 		 * since if we do it the other way around, and the trylock fails,
880 		 * we need to drop the lru lock to put the bo.
881 		 */
882 		if (ttm_lru_walk_trylock(walk->ctx, bo, &bo_needs_unlock))
883 			bo_locked = true;
884 		else if (!walk->ticket || walk->ctx->no_wait_gpu ||
885 			 walk->trylock_only)
886 			continue;
887 
888 		if (!ttm_bo_get_unless_zero(bo)) {
889 			ttm_lru_walk_unlock(bo, bo_needs_unlock);
890 			continue;
891 		}
892 
893 		mem_type = res->mem_type;
894 		spin_unlock(&bdev->lru_lock);
895 
896 		lret = 0;
897 		if (!bo_locked)
898 			lret = ttm_lru_walk_ticketlock(walk, bo, &bo_needs_unlock);
899 
900 		/*
901 		 * Note that in between the release of the lru lock and the
902 		 * ticketlock, the bo may have switched resource,
903 		 * and also memory type, since the resource may have been
904 		 * freed and allocated again with a different memory type.
905 		 * In that case, just skip it.
906 		 */
907 		if (!lret && bo->resource && bo->resource->mem_type == mem_type)
908 			lret = walk->ops->process_bo(walk, bo);
909 
910 		ttm_lru_walk_unlock(bo, bo_needs_unlock);
911 		ttm_bo_put(bo);
912 		if (lret == -EBUSY || lret == -EALREADY)
913 			lret = 0;
914 		progress = (lret < 0) ? lret : progress + lret;
915 
916 		spin_lock(&bdev->lru_lock);
917 		if (progress < 0 || progress >= target)
918 			break;
919 	}
920 	ttm_resource_cursor_fini(&cursor);
921 	spin_unlock(&bdev->lru_lock);
922 
923 	return progress;
924 }
925 EXPORT_SYMBOL(ttm_lru_walk_for_evict);
926 
927 static void ttm_bo_lru_cursor_cleanup_bo(struct ttm_bo_lru_cursor *curs)
928 {
929 	struct ttm_buffer_object *bo = curs->bo;
930 
931 	if (bo) {
932 		if (curs->needs_unlock)
933 			dma_resv_unlock(bo->base.resv);
934 		ttm_bo_put(bo);
935 		curs->bo = NULL;
936 	}
937 }
938 
939 /**
940  * ttm_bo_lru_cursor_fini() - Stop using a struct ttm_bo_lru_cursor
941  * and clean up any iteration it was used for.
942  * @curs: The cursor.
943  */
944 void ttm_bo_lru_cursor_fini(struct ttm_bo_lru_cursor *curs)
945 {
946 	spinlock_t *lru_lock = &curs->res_curs.man->bdev->lru_lock;
947 
948 	ttm_bo_lru_cursor_cleanup_bo(curs);
949 	spin_lock(lru_lock);
950 	ttm_resource_cursor_fini(&curs->res_curs);
951 	spin_unlock(lru_lock);
952 }
953 EXPORT_SYMBOL(ttm_bo_lru_cursor_fini);
954 
955 /**
956  * ttm_bo_lru_cursor_init() - Initialize a struct ttm_bo_lru_cursor
957  * @curs: The ttm_bo_lru_cursor to initialize.
958  * @man: The ttm resource_manager whose LRU lists to iterate over.
959  * @ctx: The ttm_operation_ctx to govern the locking.
960  *
961  * Initialize a struct ttm_bo_lru_cursor. Currently only trylocking
962  * or prelocked buffer objects are available as detailed by
963  * @ctx::resv and @ctx::allow_res_evict. Ticketlocking is not
964  * supported.
965  *
966  * Return: Pointer to @curs. The function does not fail.
967  */
968 struct ttm_bo_lru_cursor *
969 ttm_bo_lru_cursor_init(struct ttm_bo_lru_cursor *curs,
970 		       struct ttm_resource_manager *man,
971 		       struct ttm_operation_ctx *ctx)
972 {
973 	memset(curs, 0, sizeof(*curs));
974 	ttm_resource_cursor_init(&curs->res_curs, man);
975 	curs->ctx = ctx;
976 
977 	return curs;
978 }
979 EXPORT_SYMBOL(ttm_bo_lru_cursor_init);
980 
981 static struct ttm_buffer_object *
982 ttm_bo_from_res_reserved(struct ttm_resource *res, struct ttm_bo_lru_cursor *curs)
983 {
984 	struct ttm_buffer_object *bo = res->bo;
985 
986 	if (!ttm_lru_walk_trylock(curs->ctx, bo, &curs->needs_unlock))
987 		return NULL;
988 
989 	if (!ttm_bo_get_unless_zero(bo)) {
990 		if (curs->needs_unlock)
991 			dma_resv_unlock(bo->base.resv);
992 		return NULL;
993 	}
994 
995 	curs->bo = bo;
996 	return bo;
997 }
998 
999 /**
1000  * ttm_bo_lru_cursor_next() - Continue iterating a manager's LRU lists
1001  * to find and lock buffer object.
1002  * @curs: The cursor initialized using ttm_bo_lru_cursor_init() and
1003  * ttm_bo_lru_cursor_first().
1004  *
1005  * Return: A pointer to a locked and reference-counted buffer object,
1006  * or NULL if none could be found and looping should be terminated.
1007  */
1008 struct ttm_buffer_object *ttm_bo_lru_cursor_next(struct ttm_bo_lru_cursor *curs)
1009 {
1010 	spinlock_t *lru_lock = &curs->res_curs.man->bdev->lru_lock;
1011 	struct ttm_resource *res = NULL;
1012 	struct ttm_buffer_object *bo;
1013 
1014 	ttm_bo_lru_cursor_cleanup_bo(curs);
1015 
1016 	spin_lock(lru_lock);
1017 	for (;;) {
1018 		res = ttm_resource_manager_next(&curs->res_curs);
1019 		if (!res)
1020 			break;
1021 
1022 		bo = ttm_bo_from_res_reserved(res, curs);
1023 		if (bo)
1024 			break;
1025 	}
1026 
1027 	spin_unlock(lru_lock);
1028 	return res ? bo : NULL;
1029 }
1030 EXPORT_SYMBOL(ttm_bo_lru_cursor_next);
1031 
1032 /**
1033  * ttm_bo_lru_cursor_first() - Start iterating a manager's LRU lists
1034  * to find and lock buffer object.
1035  * @curs: The cursor initialized using ttm_bo_lru_cursor_init().
1036  *
1037  * Return: A pointer to a locked and reference-counted buffer object,
1038  * or NULL if none could be found and looping should be terminated.
1039  */
1040 struct ttm_buffer_object *ttm_bo_lru_cursor_first(struct ttm_bo_lru_cursor *curs)
1041 {
1042 	spinlock_t *lru_lock = &curs->res_curs.man->bdev->lru_lock;
1043 	struct ttm_buffer_object *bo;
1044 	struct ttm_resource *res;
1045 
1046 	spin_lock(lru_lock);
1047 	res = ttm_resource_manager_first(&curs->res_curs);
1048 	if (!res) {
1049 		spin_unlock(lru_lock);
1050 		return NULL;
1051 	}
1052 
1053 	bo = ttm_bo_from_res_reserved(res, curs);
1054 	spin_unlock(lru_lock);
1055 
1056 	return bo ? bo : ttm_bo_lru_cursor_next(curs);
1057 }
1058 EXPORT_SYMBOL(ttm_bo_lru_cursor_first);
1059 
1060 /**
1061  * ttm_bo_shrink() - Helper to shrink a ttm buffer object.
1062  * @ctx: The struct ttm_operation_ctx used for the shrinking operation.
1063  * @bo: The buffer object.
1064  * @flags: Flags governing the shrinking behaviour.
1065  *
1066  * The function uses the ttm_tt_back_up functionality to back up or
1067  * purge a struct ttm_tt. If the bo is not in system, it's first
1068  * moved there.
1069  *
1070  * Return: The number of pages shrunken or purged, or
1071  * negative error code on failure.
1072  */
1073 long ttm_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
1074 		   const struct ttm_bo_shrink_flags flags)
1075 {
1076 	static const struct ttm_place sys_placement_flags = {
1077 		.fpfn = 0,
1078 		.lpfn = 0,
1079 		.mem_type = TTM_PL_SYSTEM,
1080 		.flags = 0,
1081 	};
1082 	static struct ttm_placement sys_placement = {
1083 		.num_placement = 1,
1084 		.placement = &sys_placement_flags,
1085 	};
1086 	struct ttm_tt *tt = bo->ttm;
1087 	long lret;
1088 
1089 	dma_resv_assert_held(bo->base.resv);
1090 
1091 	if (flags.allow_move && bo->resource->mem_type != TTM_PL_SYSTEM) {
1092 		int ret = ttm_bo_validate(bo, &sys_placement, ctx);
1093 
1094 		/* Consider -ENOMEM and -ENOSPC non-fatal. */
1095 		if (ret) {
1096 			if (ret == -ENOMEM || ret == -ENOSPC)
1097 				ret = -EBUSY;
1098 			return ret;
1099 		}
1100 	}
1101 
1102 	ttm_bo_unmap_virtual(bo);
1103 	lret = ttm_bo_wait_ctx(bo, ctx);
1104 	if (lret < 0)
1105 		return lret;
1106 
1107 	if (bo->bulk_move) {
1108 		spin_lock(&bo->bdev->lru_lock);
1109 		ttm_resource_del_bulk_move(bo->resource, bo);
1110 		spin_unlock(&bo->bdev->lru_lock);
1111 	}
1112 
1113 	lret = ttm_tt_backup(bo->bdev, tt, (struct ttm_backup_flags)
1114 			     {.purge = flags.purge,
1115 			      .writeback = flags.writeback});
1116 
1117 	if (lret <= 0 && bo->bulk_move) {
1118 		spin_lock(&bo->bdev->lru_lock);
1119 		ttm_resource_add_bulk_move(bo->resource, bo);
1120 		spin_unlock(&bo->bdev->lru_lock);
1121 	}
1122 
1123 	if (lret < 0 && lret != -EINTR)
1124 		return -EBUSY;
1125 
1126 	return lret;
1127 }
1128 EXPORT_SYMBOL(ttm_bo_shrink);
1129 
1130 /**
1131  * ttm_bo_shrink_suitable() - Whether a bo is suitable for shinking
1132  * @ctx: The struct ttm_operation_ctx governing the shrinking.
1133  * @bo: The candidate for shrinking.
1134  *
1135  * Check whether the object, given the information available to TTM,
1136  * is suitable for shinking, This function can and should be used
1137  * before attempting to shrink an object.
1138  *
1139  * Return: true if suitable. false if not.
1140  */
1141 bool ttm_bo_shrink_suitable(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
1142 {
1143 	return bo->ttm && ttm_tt_is_populated(bo->ttm) && !bo->pin_count &&
1144 		(!ctx->no_wait_gpu ||
1145 		 dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP));
1146 }
1147 EXPORT_SYMBOL(ttm_bo_shrink_suitable);
1148 
1149 /**
1150  * ttm_bo_shrink_avoid_wait() - Whether to avoid waiting for GPU
1151  * during shrinking
1152  *
1153  * In some situations, like direct reclaim, waiting (in particular gpu waiting)
1154  * should be avoided since it may stall a system that could otherwise make progress
1155  * shrinking something else less time consuming.
1156  *
1157  * Return: true if gpu waiting should be avoided, false if not.
1158  */
1159 bool ttm_bo_shrink_avoid_wait(void)
1160 {
1161 	return !current_is_kswapd();
1162 }
1163 EXPORT_SYMBOL(ttm_bo_shrink_avoid_wait);
1164