11297bf2eSDirk Hohndel /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2c078aa2fSThomas Hellstrom /**************************************************************************
3c078aa2fSThomas Hellstrom  *
4c078aa2fSThomas Hellstrom  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5c078aa2fSThomas Hellstrom  * All Rights Reserved.
6c078aa2fSThomas Hellstrom  *
7c078aa2fSThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
8c078aa2fSThomas Hellstrom  * copy of this software and associated documentation files (the
9c078aa2fSThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
10c078aa2fSThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
11c078aa2fSThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
12c078aa2fSThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
13c078aa2fSThomas Hellstrom  * the following conditions:
14c078aa2fSThomas Hellstrom  *
15c078aa2fSThomas Hellstrom  * The above copyright notice and this permission notice (including the
16c078aa2fSThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
17c078aa2fSThomas Hellstrom  * of the Software.
18c078aa2fSThomas Hellstrom  *
19c078aa2fSThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20c078aa2fSThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21c078aa2fSThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22c078aa2fSThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23c078aa2fSThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24c078aa2fSThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25c078aa2fSThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26c078aa2fSThomas Hellstrom  *
27c078aa2fSThomas Hellstrom  **************************************************************************/
28c078aa2fSThomas Hellstrom 
29760285e7SDavid Howells #include <drm/ttm/ttm_execbuf_util.h>
30*a3185f91SChristian König #include <drm/ttm/ttm_bo.h>
31c078aa2fSThomas Hellstrom 
ttm_eu_backoff_reservation_reverse(struct list_head * list,struct ttm_validate_buffer * entry)321f0dc9a5SMaarten Lankhorst static void ttm_eu_backoff_reservation_reverse(struct list_head *list,
331f0dc9a5SMaarten Lankhorst 					      struct ttm_validate_buffer *entry)
34d6ea8886SDave Airlie {
351f0dc9a5SMaarten Lankhorst 	list_for_each_entry_continue_reverse(entry, list, head) {
36d6ea8886SDave Airlie 		struct ttm_buffer_object *bo = entry->bo;
37d6ea8886SDave Airlie 
3852791eeeSChristian König 		dma_resv_unlock(bo->base.resv);
39d6ea8886SDave Airlie 	}
40ecff665fSMaarten Lankhorst }
41d6ea8886SDave Airlie 
ttm_eu_backoff_reservation(struct ww_acquire_ctx * ticket,struct list_head * list)42ecff665fSMaarten Lankhorst void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
43ecff665fSMaarten Lankhorst 				struct list_head *list)
44c078aa2fSThomas Hellstrom {
45c078aa2fSThomas Hellstrom 	struct ttm_validate_buffer *entry;
46c078aa2fSThomas Hellstrom 
4768c4fa31SThomas Hellstrom 	if (list_empty(list))
4868c4fa31SThomas Hellstrom 		return;
49c078aa2fSThomas Hellstrom 
501f0dc9a5SMaarten Lankhorst 	list_for_each_entry(entry, list, head) {
511f0dc9a5SMaarten Lankhorst 		struct ttm_buffer_object *bo = entry->bo;
521f0dc9a5SMaarten Lankhorst 
53a1f091f8SChristian König 		ttm_bo_move_to_lru_tail_unlocked(bo);
5452791eeeSChristian König 		dma_resv_unlock(bo->base.resv);
551f0dc9a5SMaarten Lankhorst 	}
561f0dc9a5SMaarten Lankhorst 
578d17fb44SThomas Hellstrom 	if (ticket)
58ecff665fSMaarten Lankhorst 		ww_acquire_fini(ticket);
59c078aa2fSThomas Hellstrom }
60c078aa2fSThomas Hellstrom EXPORT_SYMBOL(ttm_eu_backoff_reservation);
61c078aa2fSThomas Hellstrom 
62c078aa2fSThomas Hellstrom /*
63c078aa2fSThomas Hellstrom  * Reserve buffers for validation.
64c078aa2fSThomas Hellstrom  *
65c078aa2fSThomas Hellstrom  * If a buffer in the list is marked for CPU access, we back off and
66c078aa2fSThomas Hellstrom  * wait for that buffer to become free for GPU access.
67c078aa2fSThomas Hellstrom  *
68c078aa2fSThomas Hellstrom  * If a buffer is reserved for another validation, the validator with
69c078aa2fSThomas Hellstrom  * the highest validation sequence backs off and waits for that buffer
70c078aa2fSThomas Hellstrom  * to become unreserved. This prevents deadlocks when validating multiple
71c078aa2fSThomas Hellstrom  * buffers in different orders.
72c078aa2fSThomas Hellstrom  */
73c078aa2fSThomas Hellstrom 
ttm_eu_reserve_buffers(struct ww_acquire_ctx * ticket,struct list_head * list,bool intr,struct list_head * dups)74ecff665fSMaarten Lankhorst int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
75aa35071cSChristian König 			   struct list_head *list, bool intr,
769165fb87SChristian König 			   struct list_head *dups)
77c078aa2fSThomas Hellstrom {
78c078aa2fSThomas Hellstrom 	struct ttm_validate_buffer *entry;
79c078aa2fSThomas Hellstrom 	int ret;
80c078aa2fSThomas Hellstrom 
81d6ea8886SDave Airlie 	if (list_empty(list))
82d6ea8886SDave Airlie 		return 0;
83d6ea8886SDave Airlie 
848d17fb44SThomas Hellstrom 	if (ticket)
85ecff665fSMaarten Lankhorst 		ww_acquire_init(ticket, &reservation_ww_class);
861f0dc9a5SMaarten Lankhorst 
87c078aa2fSThomas Hellstrom 	list_for_each_entry(entry, list, head) {
88c078aa2fSThomas Hellstrom 		struct ttm_buffer_object *bo = entry->bo;
89c8d4c18bSChristian König 		unsigned int num_fences;
90c078aa2fSThomas Hellstrom 
9146bca88bSDave Airlie 		ret = ttm_bo_reserve(bo, intr, (ticket == NULL), ticket);
927fb03cc3SChristian König 		if (ret == -EALREADY && dups) {
93aa35071cSChristian König 			struct ttm_validate_buffer *safe = entry;
94aa35071cSChristian König 			entry = list_prev_entry(entry, head);
95aa35071cSChristian König 			list_del(&safe->head);
96aa35071cSChristian König 			list_add(&safe->head, dups);
97aa35071cSChristian König 			continue;
981f0dc9a5SMaarten Lankhorst 		}
991f0dc9a5SMaarten Lankhorst 
100d72dcbe9SChristian König 		num_fences = max(entry->num_shared, 1u);
101ae9c0af2SChristian König 		if (!ret) {
102c8d4c18bSChristian König 			ret = dma_resv_reserve_fences(bo->base.resv,
103c8d4c18bSChristian König 						      num_fences);
1041f0dc9a5SMaarten Lankhorst 			if (!ret)
1051f0dc9a5SMaarten Lankhorst 				continue;
106ae9c0af2SChristian König 		}
1071f0dc9a5SMaarten Lankhorst 
1085e338405SMaarten Lankhorst 		/* uh oh, we lost out, drop every reservation and try
1095e338405SMaarten Lankhorst 		 * to only reserve this buffer, then start over if
1105e338405SMaarten Lankhorst 		 * this succeeds.
1115e338405SMaarten Lankhorst 		 */
1121f0dc9a5SMaarten Lankhorst 		ttm_eu_backoff_reservation_reverse(list, entry);
11358b4d720SMaarten Lankhorst 
11407d48da4STom St Denis 		if (ret == -EDEADLK) {
11546bca88bSDave Airlie 			ret = ttm_bo_reserve_slowpath(bo, intr, ticket);
11607d48da4STom St Denis 		}
1171f0dc9a5SMaarten Lankhorst 
118c8d4c18bSChristian König 		if (!ret)
119c8d4c18bSChristian König 			ret = dma_resv_reserve_fences(bo->base.resv,
120c8d4c18bSChristian König 						      num_fences);
121ae9c0af2SChristian König 
1225e338405SMaarten Lankhorst 		if (unlikely(ret != 0)) {
1231f0dc9a5SMaarten Lankhorst 			if (ticket) {
1241f0dc9a5SMaarten Lankhorst 				ww_acquire_done(ticket);
1251f0dc9a5SMaarten Lankhorst 				ww_acquire_fini(ticket);
1265e338405SMaarten Lankhorst 			}
1271f0dc9a5SMaarten Lankhorst 			return ret;
1281f0dc9a5SMaarten Lankhorst 		}
129ecff665fSMaarten Lankhorst 
1301f0dc9a5SMaarten Lankhorst 		/* move this item to the front of the list,
1311f0dc9a5SMaarten Lankhorst 		 * forces correct iteration of the loop without keeping track
1321f0dc9a5SMaarten Lankhorst 		 */
1331f0dc9a5SMaarten Lankhorst 		list_del(&entry->head);
1341f0dc9a5SMaarten Lankhorst 		list_add(&entry->head, list);
135c078aa2fSThomas Hellstrom 	}
136d6ea8886SDave Airlie 
137c078aa2fSThomas Hellstrom 	return 0;
138c078aa2fSThomas Hellstrom }
139c078aa2fSThomas Hellstrom EXPORT_SYMBOL(ttm_eu_reserve_buffers);
140c078aa2fSThomas Hellstrom 
ttm_eu_fence_buffer_objects(struct ww_acquire_ctx * ticket,struct list_head * list,struct dma_fence * fence)141ecff665fSMaarten Lankhorst void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
142f54d1867SChris Wilson 				 struct list_head *list,
143f54d1867SChris Wilson 				 struct dma_fence *fence)
144c078aa2fSThomas Hellstrom {
145c078aa2fSThomas Hellstrom 	struct ttm_validate_buffer *entry;
146c078aa2fSThomas Hellstrom 
14795762c2bSThomas Hellstrom 	if (list_empty(list))
14895762c2bSThomas Hellstrom 		return;
14995762c2bSThomas Hellstrom 
15095762c2bSThomas Hellstrom 	list_for_each_entry(entry, list, head) {
15197588b5bSChristian König 		struct ttm_buffer_object *bo = entry->bo;
15297588b5bSChristian König 
15373511edfSChristian König 		dma_resv_add_fence(bo->base.resv, fence, entry->num_shared ?
15473511edfSChristian König 				   DMA_RESV_USAGE_READ : DMA_RESV_USAGE_WRITE);
155a1f091f8SChristian König 		ttm_bo_move_to_lru_tail_unlocked(bo);
15652791eeeSChristian König 		dma_resv_unlock(bo->base.resv);
15795762c2bSThomas Hellstrom 	}
1588d17fb44SThomas Hellstrom 	if (ticket)
159ecff665fSMaarten Lankhorst 		ww_acquire_fini(ticket);
160c078aa2fSThomas Hellstrom }
161c078aa2fSThomas Hellstrom EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);
162