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