xref: /freebsd/sys/dev/drm2/ttm/ttm_execbuf_util.c (revision 685dc743)
1592ffb21SWarner Losh /**************************************************************************
2592ffb21SWarner Losh  *
3592ffb21SWarner Losh  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4592ffb21SWarner Losh  * All Rights Reserved.
5592ffb21SWarner Losh  *
6592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
7592ffb21SWarner Losh  * copy of this software and associated documentation files (the
8592ffb21SWarner Losh  * "Software"), to deal in the Software without restriction, including
9592ffb21SWarner Losh  * without limitation the rights to use, copy, modify, merge, publish,
10592ffb21SWarner Losh  * distribute, sub license, and/or sell copies of the Software, and to
11592ffb21SWarner Losh  * permit persons to whom the Software is furnished to do so, subject to
12592ffb21SWarner Losh  * the following conditions:
13592ffb21SWarner Losh  *
14592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the
15592ffb21SWarner Losh  * next paragraph) shall be included in all copies or substantial portions
16592ffb21SWarner Losh  * of the Software.
17592ffb21SWarner Losh  *
18592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21592ffb21SWarner Losh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22592ffb21SWarner Losh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23592ffb21SWarner Losh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24592ffb21SWarner Losh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25592ffb21SWarner Losh  *
26592ffb21SWarner Losh  **************************************************************************/
27592ffb21SWarner Losh 
28592ffb21SWarner Losh #include <sys/cdefs.h>
29592ffb21SWarner Losh #include <dev/drm2/drmP.h>
30592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_execbuf_util.h>
31592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_bo_driver.h>
32592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_placement.h>
33592ffb21SWarner Losh 
ttm_eu_backoff_reservation_locked(struct list_head * list)34592ffb21SWarner Losh static void ttm_eu_backoff_reservation_locked(struct list_head *list)
35592ffb21SWarner Losh {
36592ffb21SWarner Losh 	struct ttm_validate_buffer *entry;
37592ffb21SWarner Losh 
38592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
39592ffb21SWarner Losh 		struct ttm_buffer_object *bo = entry->bo;
40592ffb21SWarner Losh 		if (!entry->reserved)
41592ffb21SWarner Losh 			continue;
42592ffb21SWarner Losh 
43592ffb21SWarner Losh 		if (entry->removed) {
44592ffb21SWarner Losh 			ttm_bo_add_to_lru(bo);
45592ffb21SWarner Losh 			entry->removed = false;
46592ffb21SWarner Losh 
47592ffb21SWarner Losh 		}
48592ffb21SWarner Losh 		entry->reserved = false;
49592ffb21SWarner Losh 		atomic_set(&bo->reserved, 0);
50592ffb21SWarner Losh 		wakeup(bo);
51592ffb21SWarner Losh 	}
52592ffb21SWarner Losh }
53592ffb21SWarner Losh 
ttm_eu_del_from_lru_locked(struct list_head * list)54592ffb21SWarner Losh static void ttm_eu_del_from_lru_locked(struct list_head *list)
55592ffb21SWarner Losh {
56592ffb21SWarner Losh 	struct ttm_validate_buffer *entry;
57592ffb21SWarner Losh 
58592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
59592ffb21SWarner Losh 		struct ttm_buffer_object *bo = entry->bo;
60592ffb21SWarner Losh 		if (!entry->reserved)
61592ffb21SWarner Losh 			continue;
62592ffb21SWarner Losh 
63592ffb21SWarner Losh 		if (!entry->removed) {
64592ffb21SWarner Losh 			entry->put_count = ttm_bo_del_from_lru(bo);
65592ffb21SWarner Losh 			entry->removed = true;
66592ffb21SWarner Losh 		}
67592ffb21SWarner Losh 	}
68592ffb21SWarner Losh }
69592ffb21SWarner Losh 
ttm_eu_list_ref_sub(struct list_head * list)70592ffb21SWarner Losh static void ttm_eu_list_ref_sub(struct list_head *list)
71592ffb21SWarner Losh {
72592ffb21SWarner Losh 	struct ttm_validate_buffer *entry;
73592ffb21SWarner Losh 
74592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
75592ffb21SWarner Losh 		struct ttm_buffer_object *bo = entry->bo;
76592ffb21SWarner Losh 
77592ffb21SWarner Losh 		if (entry->put_count) {
78592ffb21SWarner Losh 			ttm_bo_list_ref_sub(bo, entry->put_count, true);
79592ffb21SWarner Losh 			entry->put_count = 0;
80592ffb21SWarner Losh 		}
81592ffb21SWarner Losh 	}
82592ffb21SWarner Losh }
83592ffb21SWarner Losh 
ttm_eu_backoff_reservation(struct list_head * list)84592ffb21SWarner Losh void ttm_eu_backoff_reservation(struct list_head *list)
85592ffb21SWarner Losh {
86592ffb21SWarner Losh 	struct ttm_validate_buffer *entry;
87592ffb21SWarner Losh 	struct ttm_bo_global *glob;
88592ffb21SWarner Losh 
89592ffb21SWarner Losh 	if (list_empty(list))
90592ffb21SWarner Losh 		return;
91592ffb21SWarner Losh 
92592ffb21SWarner Losh 	entry = list_first_entry(list, struct ttm_validate_buffer, head);
93592ffb21SWarner Losh 	glob = entry->bo->glob;
94592ffb21SWarner Losh 	mtx_lock(&glob->lru_lock);
95592ffb21SWarner Losh 	ttm_eu_backoff_reservation_locked(list);
96592ffb21SWarner Losh 	mtx_unlock(&glob->lru_lock);
97592ffb21SWarner Losh }
98592ffb21SWarner Losh 
99592ffb21SWarner Losh /*
100592ffb21SWarner Losh  * Reserve buffers for validation.
101592ffb21SWarner Losh  *
102592ffb21SWarner Losh  * If a buffer in the list is marked for CPU access, we back off and
103592ffb21SWarner Losh  * wait for that buffer to become free for GPU access.
104592ffb21SWarner Losh  *
105592ffb21SWarner Losh  * If a buffer is reserved for another validation, the validator with
106592ffb21SWarner Losh  * the highest validation sequence backs off and waits for that buffer
107592ffb21SWarner Losh  * to become unreserved. This prevents deadlocks when validating multiple
108592ffb21SWarner Losh  * buffers in different orders.
109592ffb21SWarner Losh  */
110592ffb21SWarner Losh 
ttm_eu_reserve_buffers(struct list_head * list)111592ffb21SWarner Losh int ttm_eu_reserve_buffers(struct list_head *list)
112592ffb21SWarner Losh {
113592ffb21SWarner Losh 	struct ttm_bo_global *glob;
114592ffb21SWarner Losh 	struct ttm_validate_buffer *entry;
115592ffb21SWarner Losh 	int ret;
116592ffb21SWarner Losh 	uint32_t val_seq;
117592ffb21SWarner Losh 
118592ffb21SWarner Losh 	if (list_empty(list))
119592ffb21SWarner Losh 		return 0;
120592ffb21SWarner Losh 
121592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
122592ffb21SWarner Losh 		entry->reserved = false;
123592ffb21SWarner Losh 		entry->put_count = 0;
124592ffb21SWarner Losh 		entry->removed = false;
125592ffb21SWarner Losh 	}
126592ffb21SWarner Losh 
127592ffb21SWarner Losh 	entry = list_first_entry(list, struct ttm_validate_buffer, head);
128592ffb21SWarner Losh 	glob = entry->bo->glob;
129592ffb21SWarner Losh 
130592ffb21SWarner Losh 	mtx_lock(&glob->lru_lock);
131592ffb21SWarner Losh 	val_seq = entry->bo->bdev->val_seq++;
132592ffb21SWarner Losh 
133592ffb21SWarner Losh retry_locked:
134592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
135592ffb21SWarner Losh 		struct ttm_buffer_object *bo = entry->bo;
136592ffb21SWarner Losh 
137592ffb21SWarner Losh 		/* already slowpath reserved? */
138592ffb21SWarner Losh 		if (entry->reserved)
139592ffb21SWarner Losh 			continue;
140592ffb21SWarner Losh 
141592ffb21SWarner Losh 		ret = ttm_bo_reserve_nolru(bo, true, true, true, val_seq);
142592ffb21SWarner Losh 		switch (ret) {
143592ffb21SWarner Losh 		case 0:
144592ffb21SWarner Losh 			break;
145592ffb21SWarner Losh 		case -EBUSY:
146592ffb21SWarner Losh 			ttm_eu_del_from_lru_locked(list);
147592ffb21SWarner Losh 			ret = ttm_bo_reserve_nolru(bo, true, false,
148592ffb21SWarner Losh 						   true, val_seq);
149592ffb21SWarner Losh 			if (!ret)
150592ffb21SWarner Losh 				break;
151592ffb21SWarner Losh 
152592ffb21SWarner Losh 			if (unlikely(ret != -EAGAIN))
153592ffb21SWarner Losh 				goto err;
154592ffb21SWarner Losh 
155592ffb21SWarner Losh 			/* fallthrough */
156592ffb21SWarner Losh 		case -EAGAIN:
157592ffb21SWarner Losh 			ttm_eu_backoff_reservation_locked(list);
158592ffb21SWarner Losh 
159592ffb21SWarner Losh 			/*
160592ffb21SWarner Losh 			 * temporarily increase sequence number every retry,
161592ffb21SWarner Losh 			 * to prevent us from seeing our old reservation
162592ffb21SWarner Losh 			 * sequence when someone else reserved the buffer,
163592ffb21SWarner Losh 			 * but hasn't updated the seq_valid/seqno members yet.
164592ffb21SWarner Losh 			 */
165592ffb21SWarner Losh 			val_seq = entry->bo->bdev->val_seq++;
166592ffb21SWarner Losh 
167592ffb21SWarner Losh 			ttm_eu_list_ref_sub(list);
168592ffb21SWarner Losh 			ret = ttm_bo_reserve_slowpath_nolru(bo, true, val_seq);
169592ffb21SWarner Losh 			if (unlikely(ret != 0)) {
170592ffb21SWarner Losh 				mtx_unlock(&glob->lru_lock);
171592ffb21SWarner Losh 				return ret;
172592ffb21SWarner Losh 			}
173592ffb21SWarner Losh 			entry->reserved = true;
174592ffb21SWarner Losh 			if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
175592ffb21SWarner Losh 				ret = -EBUSY;
176592ffb21SWarner Losh 				goto err;
177592ffb21SWarner Losh 			}
178592ffb21SWarner Losh 			goto retry_locked;
179592ffb21SWarner Losh 		default:
180592ffb21SWarner Losh 			goto err;
181592ffb21SWarner Losh 		}
182592ffb21SWarner Losh 
183592ffb21SWarner Losh 		entry->reserved = true;
184592ffb21SWarner Losh 		if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
185592ffb21SWarner Losh 			ret = -EBUSY;
186592ffb21SWarner Losh 			goto err;
187592ffb21SWarner Losh 		}
188592ffb21SWarner Losh 	}
189592ffb21SWarner Losh 
190592ffb21SWarner Losh 	ttm_eu_del_from_lru_locked(list);
191592ffb21SWarner Losh 	mtx_unlock(&glob->lru_lock);
192592ffb21SWarner Losh 	ttm_eu_list_ref_sub(list);
193592ffb21SWarner Losh 
194592ffb21SWarner Losh 	return 0;
195592ffb21SWarner Losh 
196592ffb21SWarner Losh err:
197592ffb21SWarner Losh 	ttm_eu_backoff_reservation_locked(list);
198592ffb21SWarner Losh 	mtx_unlock(&glob->lru_lock);
199592ffb21SWarner Losh 	ttm_eu_list_ref_sub(list);
200592ffb21SWarner Losh 	return ret;
201592ffb21SWarner Losh }
202592ffb21SWarner Losh 
ttm_eu_fence_buffer_objects(struct list_head * list,void * sync_obj)203592ffb21SWarner Losh void ttm_eu_fence_buffer_objects(struct list_head *list, void *sync_obj)
204592ffb21SWarner Losh {
205592ffb21SWarner Losh 	struct ttm_validate_buffer *entry;
206592ffb21SWarner Losh 	struct ttm_buffer_object *bo;
207592ffb21SWarner Losh 	struct ttm_bo_global *glob;
208592ffb21SWarner Losh 	struct ttm_bo_device *bdev;
209592ffb21SWarner Losh 	struct ttm_bo_driver *driver;
210592ffb21SWarner Losh 
211592ffb21SWarner Losh 	if (list_empty(list))
212592ffb21SWarner Losh 		return;
213592ffb21SWarner Losh 
214592ffb21SWarner Losh 	bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
215592ffb21SWarner Losh 	bdev = bo->bdev;
216592ffb21SWarner Losh 	driver = bdev->driver;
217592ffb21SWarner Losh 	glob = bo->glob;
218592ffb21SWarner Losh 
219592ffb21SWarner Losh 	mtx_lock(&glob->lru_lock);
220592ffb21SWarner Losh 	mtx_lock(&bdev->fence_lock);
221592ffb21SWarner Losh 
222592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
223592ffb21SWarner Losh 		bo = entry->bo;
224592ffb21SWarner Losh 		entry->old_sync_obj = bo->sync_obj;
225592ffb21SWarner Losh 		bo->sync_obj = driver->sync_obj_ref(sync_obj);
226592ffb21SWarner Losh 		ttm_bo_unreserve_locked(bo);
227592ffb21SWarner Losh 		entry->reserved = false;
228592ffb21SWarner Losh 	}
229592ffb21SWarner Losh 	mtx_unlock(&bdev->fence_lock);
230592ffb21SWarner Losh 	mtx_unlock(&glob->lru_lock);
231592ffb21SWarner Losh 
232592ffb21SWarner Losh 	list_for_each_entry(entry, list, head) {
233592ffb21SWarner Losh 		if (entry->old_sync_obj)
234592ffb21SWarner Losh 			driver->sync_obj_unref(&entry->old_sync_obj);
235592ffb21SWarner Losh 	}
236592ffb21SWarner Losh }
237