xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/ttm/ttm_execbuf_util.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1*41ec0267Sriastradh /*	$NetBSD: ttm_execbuf_util.c,v 1.6 2021/12/18 23:45:44 riastradh Exp $	*/
2d01ac146Sriastradh 
3*41ec0267Sriastradh /* SPDX-License-Identifier: GPL-2.0 OR MIT */
4fcd0cb28Sriastradh /**************************************************************************
5fcd0cb28Sriastradh  *
6fcd0cb28Sriastradh  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
7fcd0cb28Sriastradh  * All Rights Reserved.
8fcd0cb28Sriastradh  *
9fcd0cb28Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
10fcd0cb28Sriastradh  * copy of this software and associated documentation files (the
11fcd0cb28Sriastradh  * "Software"), to deal in the Software without restriction, including
12fcd0cb28Sriastradh  * without limitation the rights to use, copy, modify, merge, publish,
13fcd0cb28Sriastradh  * distribute, sub license, and/or sell copies of the Software, and to
14fcd0cb28Sriastradh  * permit persons to whom the Software is furnished to do so, subject to
15fcd0cb28Sriastradh  * the following conditions:
16fcd0cb28Sriastradh  *
17fcd0cb28Sriastradh  * The above copyright notice and this permission notice (including the
18fcd0cb28Sriastradh  * next paragraph) shall be included in all copies or substantial portions
19fcd0cb28Sriastradh  * of the Software.
20fcd0cb28Sriastradh  *
21fcd0cb28Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22fcd0cb28Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23fcd0cb28Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
24fcd0cb28Sriastradh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
25fcd0cb28Sriastradh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26fcd0cb28Sriastradh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
27fcd0cb28Sriastradh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
28fcd0cb28Sriastradh  *
29fcd0cb28Sriastradh  **************************************************************************/
30fcd0cb28Sriastradh 
31d01ac146Sriastradh #include <sys/cdefs.h>
32*41ec0267Sriastradh __KERNEL_RCSID(0, "$NetBSD: ttm_execbuf_util.c,v 1.6 2021/12/18 23:45:44 riastradh Exp $");
33d01ac146Sriastradh 
34fcd0cb28Sriastradh #include <drm/ttm/ttm_execbuf_util.h>
35fcd0cb28Sriastradh #include <drm/ttm/ttm_bo_driver.h>
36fcd0cb28Sriastradh #include <drm/ttm/ttm_placement.h>
37fcd0cb28Sriastradh #include <linux/wait.h>
38fcd0cb28Sriastradh #include <linux/sched.h>
39fcd0cb28Sriastradh #include <linux/module.h>
40fcd0cb28Sriastradh 
ttm_eu_backoff_reservation_reverse(struct list_head * list,struct ttm_validate_buffer * entry)41d01ac146Sriastradh static void ttm_eu_backoff_reservation_reverse(struct list_head *list,
42d01ac146Sriastradh 					      struct ttm_validate_buffer *entry)
43fcd0cb28Sriastradh {
44d01ac146Sriastradh 	list_for_each_entry_continue_reverse(entry, list, head) {
45fcd0cb28Sriastradh 		struct ttm_buffer_object *bo = entry->bo;
46fcd0cb28Sriastradh 
47*41ec0267Sriastradh 		dma_resv_unlock(bo->base.resv);
48fcd0cb28Sriastradh 	}
49fcd0cb28Sriastradh }
50fcd0cb28Sriastradh 
ttm_eu_backoff_reservation(struct ww_acquire_ctx * ticket,struct list_head * list)519d20d926Sriastradh void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
529d20d926Sriastradh 				struct list_head *list)
53fcd0cb28Sriastradh {
54fcd0cb28Sriastradh 	struct ttm_validate_buffer *entry;
55fcd0cb28Sriastradh 
56fcd0cb28Sriastradh 	if (list_empty(list))
57fcd0cb28Sriastradh 		return;
58fcd0cb28Sriastradh 
59*41ec0267Sriastradh 	spin_lock(&ttm_bo_glob.lru_lock);
60d01ac146Sriastradh 	list_for_each_entry(entry, list, head) {
61d01ac146Sriastradh 		struct ttm_buffer_object *bo = entry->bo;
62d01ac146Sriastradh 
63*41ec0267Sriastradh 		ttm_bo_move_to_lru_tail(bo, NULL);
64*41ec0267Sriastradh 		dma_resv_unlock(bo->base.resv);
65d01ac146Sriastradh 	}
66*41ec0267Sriastradh 	spin_unlock(&ttm_bo_glob.lru_lock);
67d01ac146Sriastradh 
689d20d926Sriastradh 	if (ticket)
699d20d926Sriastradh 		ww_acquire_fini(ticket);
70fcd0cb28Sriastradh }
71fcd0cb28Sriastradh EXPORT_SYMBOL(ttm_eu_backoff_reservation);
72fcd0cb28Sriastradh 
73fcd0cb28Sriastradh /*
74fcd0cb28Sriastradh  * Reserve buffers for validation.
75fcd0cb28Sriastradh  *
76fcd0cb28Sriastradh  * If a buffer in the list is marked for CPU access, we back off and
77fcd0cb28Sriastradh  * wait for that buffer to become free for GPU access.
78fcd0cb28Sriastradh  *
79fcd0cb28Sriastradh  * If a buffer is reserved for another validation, the validator with
80fcd0cb28Sriastradh  * the highest validation sequence backs off and waits for that buffer
81fcd0cb28Sriastradh  * to become unreserved. This prevents deadlocks when validating multiple
82fcd0cb28Sriastradh  * buffers in different orders.
83fcd0cb28Sriastradh  */
84fcd0cb28Sriastradh 
ttm_eu_reserve_buffers(struct ww_acquire_ctx * ticket,struct list_head * list,bool intr,struct list_head * dups)859d20d926Sriastradh int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
86d01ac146Sriastradh 			   struct list_head *list, bool intr,
87d01ac146Sriastradh 			   struct list_head *dups)
88fcd0cb28Sriastradh {
89fcd0cb28Sriastradh 	struct ttm_validate_buffer *entry;
90fcd0cb28Sriastradh 	int ret;
91fcd0cb28Sriastradh 
92fcd0cb28Sriastradh 	if (list_empty(list))
93fcd0cb28Sriastradh 		return 0;
94fcd0cb28Sriastradh 
959d20d926Sriastradh 	if (ticket)
969d20d926Sriastradh 		ww_acquire_init(ticket, &reservation_ww_class);
97d01ac146Sriastradh 
98fcd0cb28Sriastradh 	list_for_each_entry(entry, list, head) {
99fcd0cb28Sriastradh 		struct ttm_buffer_object *bo = entry->bo;
100fcd0cb28Sriastradh 
101*41ec0267Sriastradh 		ret = __ttm_bo_reserve(bo, intr, (ticket == NULL), ticket);
102*41ec0267Sriastradh 		if (ret == -EALREADY && dups) {
103d01ac146Sriastradh 			struct ttm_validate_buffer *safe = entry;
104d01ac146Sriastradh 			entry = list_prev_entry(entry, head);
105d01ac146Sriastradh 			list_del(&safe->head);
106d01ac146Sriastradh 			list_add(&safe->head, dups);
107d01ac146Sriastradh 			continue;
108d01ac146Sriastradh 		}
109d01ac146Sriastradh 
110d01ac146Sriastradh 		if (!ret) {
111*41ec0267Sriastradh 			if (!entry->num_shared)
1129d20d926Sriastradh 				continue;
1139d20d926Sriastradh 
114*41ec0267Sriastradh 			ret = dma_resv_reserve_shared(bo->base.resv,
115*41ec0267Sriastradh 								entry->num_shared);
116d01ac146Sriastradh 			if (!ret)
117d01ac146Sriastradh 				continue;
118d01ac146Sriastradh 		}
1199d20d926Sriastradh 
1209d20d926Sriastradh 		/* uh oh, we lost out, drop every reservation and try
1219d20d926Sriastradh 		 * to only reserve this buffer, then start over if
1229d20d926Sriastradh 		 * this succeeds.
1239d20d926Sriastradh 		 */
124d01ac146Sriastradh 		ttm_eu_backoff_reservation_reverse(list, entry);
125d01ac146Sriastradh 
126*41ec0267Sriastradh 		if (ret == -EDEADLK) {
127*41ec0267Sriastradh 			if (intr) {
128*41ec0267Sriastradh 				ret = dma_resv_lock_slow_interruptible(bo->base.resv,
1299d20d926Sriastradh 										 ticket);
130*41ec0267Sriastradh 			} else {
131*41ec0267Sriastradh 				dma_resv_lock_slow(bo->base.resv, ticket);
132d01ac146Sriastradh 				ret = 0;
133d01ac146Sriastradh 			}
134*41ec0267Sriastradh 		}
135d01ac146Sriastradh 
136*41ec0267Sriastradh 		if (!ret && entry->num_shared)
137*41ec0267Sriastradh 			ret = dma_resv_reserve_shared(bo->base.resv,
138*41ec0267Sriastradh 								entry->num_shared);
139d01ac146Sriastradh 
140fcd0cb28Sriastradh 		if (unlikely(ret != 0)) {
1419d20d926Sriastradh 			if (ret == -EINTR)
1429d20d926Sriastradh 				ret = -ERESTARTSYS;
143d01ac146Sriastradh 			if (ticket) {
144d01ac146Sriastradh 				ww_acquire_done(ticket);
145d01ac146Sriastradh 				ww_acquire_fini(ticket);
146d01ac146Sriastradh 			}
147d01ac146Sriastradh 			return ret;
148fcd0cb28Sriastradh 		}
149fcd0cb28Sriastradh 
150d01ac146Sriastradh 		/* move this item to the front of the list,
151d01ac146Sriastradh 		 * forces correct iteration of the loop without keeping track
152d01ac146Sriastradh 		 */
153d01ac146Sriastradh 		list_del(&entry->head);
154d01ac146Sriastradh 		list_add(&entry->head, list);
155fcd0cb28Sriastradh 	}
156fcd0cb28Sriastradh 
157fcd0cb28Sriastradh 	return 0;
158fcd0cb28Sriastradh }
159fcd0cb28Sriastradh EXPORT_SYMBOL(ttm_eu_reserve_buffers);
160fcd0cb28Sriastradh 
ttm_eu_fence_buffer_objects(struct ww_acquire_ctx * ticket,struct list_head * list,struct dma_fence * fence)1619d20d926Sriastradh void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
162*41ec0267Sriastradh 				 struct list_head *list,
163*41ec0267Sriastradh 				 struct dma_fence *fence)
164fcd0cb28Sriastradh {
165fcd0cb28Sriastradh 	struct ttm_validate_buffer *entry;
166fcd0cb28Sriastradh 
167fcd0cb28Sriastradh 	if (list_empty(list))
168fcd0cb28Sriastradh 		return;
169fcd0cb28Sriastradh 
170*41ec0267Sriastradh 	spin_lock(&ttm_bo_glob.lru_lock);
171fcd0cb28Sriastradh 	list_for_each_entry(entry, list, head) {
172*41ec0267Sriastradh 		struct ttm_buffer_object *bo = entry->bo;
173*41ec0267Sriastradh 
174*41ec0267Sriastradh 		if (entry->num_shared)
175*41ec0267Sriastradh 			dma_resv_add_shared_fence(bo->base.resv, fence);
176d01ac146Sriastradh 		else
177*41ec0267Sriastradh 			dma_resv_add_excl_fence(bo->base.resv, fence);
178*41ec0267Sriastradh 		ttm_bo_move_to_lru_tail(bo, NULL);
179*41ec0267Sriastradh 		dma_resv_unlock(bo->base.resv);
180fcd0cb28Sriastradh 	}
181*41ec0267Sriastradh 	spin_unlock(&ttm_bo_glob.lru_lock);
1829d20d926Sriastradh 	if (ticket)
1839d20d926Sriastradh 		ww_acquire_fini(ticket);
184fcd0cb28Sriastradh }
185fcd0cb28Sriastradh EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);
186