1*592ffb21SWarner Losh /**************************************************************************
2*592ffb21SWarner Losh *
3*592ffb21SWarner Losh * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*592ffb21SWarner Losh * All Rights Reserved.
5*592ffb21SWarner Losh *
6*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
7*592ffb21SWarner Losh * copy of this software and associated documentation files (the
8*592ffb21SWarner Losh * "Software"), to deal in the Software without restriction, including
9*592ffb21SWarner Losh * without limitation the rights to use, copy, modify, merge, publish,
10*592ffb21SWarner Losh * distribute, sub license, and/or sell copies of the Software, and to
11*592ffb21SWarner Losh * permit persons to whom the Software is furnished to do so, subject to
12*592ffb21SWarner Losh * the following conditions:
13*592ffb21SWarner Losh *
14*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the
15*592ffb21SWarner Losh * next paragraph) shall be included in all copies or substantial portions
16*592ffb21SWarner Losh * of the Software.
17*592ffb21SWarner Losh *
18*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*592ffb21SWarner Losh * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*592ffb21SWarner Losh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*592ffb21SWarner Losh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*592ffb21SWarner Losh * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*592ffb21SWarner Losh *
26*592ffb21SWarner Losh **************************************************************************/
27*592ffb21SWarner Losh
28*592ffb21SWarner Losh #include <sys/cdefs.h>
29*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
30*592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_execbuf_util.h>
31*592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_bo_driver.h>
32*592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_placement.h>
33*592ffb21SWarner Losh
ttm_eu_backoff_reservation_locked(struct list_head * list)34*592ffb21SWarner Losh static void ttm_eu_backoff_reservation_locked(struct list_head *list)
35*592ffb21SWarner Losh {
36*592ffb21SWarner Losh struct ttm_validate_buffer *entry;
37*592ffb21SWarner Losh
38*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
39*592ffb21SWarner Losh struct ttm_buffer_object *bo = entry->bo;
40*592ffb21SWarner Losh if (!entry->reserved)
41*592ffb21SWarner Losh continue;
42*592ffb21SWarner Losh
43*592ffb21SWarner Losh if (entry->removed) {
44*592ffb21SWarner Losh ttm_bo_add_to_lru(bo);
45*592ffb21SWarner Losh entry->removed = false;
46*592ffb21SWarner Losh
47*592ffb21SWarner Losh }
48*592ffb21SWarner Losh entry->reserved = false;
49*592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
50*592ffb21SWarner Losh wakeup(bo);
51*592ffb21SWarner Losh }
52*592ffb21SWarner Losh }
53*592ffb21SWarner Losh
ttm_eu_del_from_lru_locked(struct list_head * list)54*592ffb21SWarner Losh static void ttm_eu_del_from_lru_locked(struct list_head *list)
55*592ffb21SWarner Losh {
56*592ffb21SWarner Losh struct ttm_validate_buffer *entry;
57*592ffb21SWarner Losh
58*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
59*592ffb21SWarner Losh struct ttm_buffer_object *bo = entry->bo;
60*592ffb21SWarner Losh if (!entry->reserved)
61*592ffb21SWarner Losh continue;
62*592ffb21SWarner Losh
63*592ffb21SWarner Losh if (!entry->removed) {
64*592ffb21SWarner Losh entry->put_count = ttm_bo_del_from_lru(bo);
65*592ffb21SWarner Losh entry->removed = true;
66*592ffb21SWarner Losh }
67*592ffb21SWarner Losh }
68*592ffb21SWarner Losh }
69*592ffb21SWarner Losh
ttm_eu_list_ref_sub(struct list_head * list)70*592ffb21SWarner Losh static void ttm_eu_list_ref_sub(struct list_head *list)
71*592ffb21SWarner Losh {
72*592ffb21SWarner Losh struct ttm_validate_buffer *entry;
73*592ffb21SWarner Losh
74*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
75*592ffb21SWarner Losh struct ttm_buffer_object *bo = entry->bo;
76*592ffb21SWarner Losh
77*592ffb21SWarner Losh if (entry->put_count) {
78*592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, entry->put_count, true);
79*592ffb21SWarner Losh entry->put_count = 0;
80*592ffb21SWarner Losh }
81*592ffb21SWarner Losh }
82*592ffb21SWarner Losh }
83*592ffb21SWarner Losh
ttm_eu_backoff_reservation(struct list_head * list)84*592ffb21SWarner Losh void ttm_eu_backoff_reservation(struct list_head *list)
85*592ffb21SWarner Losh {
86*592ffb21SWarner Losh struct ttm_validate_buffer *entry;
87*592ffb21SWarner Losh struct ttm_bo_global *glob;
88*592ffb21SWarner Losh
89*592ffb21SWarner Losh if (list_empty(list))
90*592ffb21SWarner Losh return;
91*592ffb21SWarner Losh
92*592ffb21SWarner Losh entry = list_first_entry(list, struct ttm_validate_buffer, head);
93*592ffb21SWarner Losh glob = entry->bo->glob;
94*592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
95*592ffb21SWarner Losh ttm_eu_backoff_reservation_locked(list);
96*592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
97*592ffb21SWarner Losh }
98*592ffb21SWarner Losh
99*592ffb21SWarner Losh /*
100*592ffb21SWarner Losh * Reserve buffers for validation.
101*592ffb21SWarner Losh *
102*592ffb21SWarner Losh * If a buffer in the list is marked for CPU access, we back off and
103*592ffb21SWarner Losh * wait for that buffer to become free for GPU access.
104*592ffb21SWarner Losh *
105*592ffb21SWarner Losh * If a buffer is reserved for another validation, the validator with
106*592ffb21SWarner Losh * the highest validation sequence backs off and waits for that buffer
107*592ffb21SWarner Losh * to become unreserved. This prevents deadlocks when validating multiple
108*592ffb21SWarner Losh * buffers in different orders.
109*592ffb21SWarner Losh */
110*592ffb21SWarner Losh
ttm_eu_reserve_buffers(struct list_head * list)111*592ffb21SWarner Losh int ttm_eu_reserve_buffers(struct list_head *list)
112*592ffb21SWarner Losh {
113*592ffb21SWarner Losh struct ttm_bo_global *glob;
114*592ffb21SWarner Losh struct ttm_validate_buffer *entry;
115*592ffb21SWarner Losh int ret;
116*592ffb21SWarner Losh uint32_t val_seq;
117*592ffb21SWarner Losh
118*592ffb21SWarner Losh if (list_empty(list))
119*592ffb21SWarner Losh return 0;
120*592ffb21SWarner Losh
121*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
122*592ffb21SWarner Losh entry->reserved = false;
123*592ffb21SWarner Losh entry->put_count = 0;
124*592ffb21SWarner Losh entry->removed = false;
125*592ffb21SWarner Losh }
126*592ffb21SWarner Losh
127*592ffb21SWarner Losh entry = list_first_entry(list, struct ttm_validate_buffer, head);
128*592ffb21SWarner Losh glob = entry->bo->glob;
129*592ffb21SWarner Losh
130*592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
131*592ffb21SWarner Losh val_seq = entry->bo->bdev->val_seq++;
132*592ffb21SWarner Losh
133*592ffb21SWarner Losh retry_locked:
134*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
135*592ffb21SWarner Losh struct ttm_buffer_object *bo = entry->bo;
136*592ffb21SWarner Losh
137*592ffb21SWarner Losh /* already slowpath reserved? */
138*592ffb21SWarner Losh if (entry->reserved)
139*592ffb21SWarner Losh continue;
140*592ffb21SWarner Losh
141*592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, true, true, true, val_seq);
142*592ffb21SWarner Losh switch (ret) {
143*592ffb21SWarner Losh case 0:
144*592ffb21SWarner Losh break;
145*592ffb21SWarner Losh case -EBUSY:
146*592ffb21SWarner Losh ttm_eu_del_from_lru_locked(list);
147*592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, true, false,
148*592ffb21SWarner Losh true, val_seq);
149*592ffb21SWarner Losh if (!ret)
150*592ffb21SWarner Losh break;
151*592ffb21SWarner Losh
152*592ffb21SWarner Losh if (unlikely(ret != -EAGAIN))
153*592ffb21SWarner Losh goto err;
154*592ffb21SWarner Losh
155*592ffb21SWarner Losh /* fallthrough */
156*592ffb21SWarner Losh case -EAGAIN:
157*592ffb21SWarner Losh ttm_eu_backoff_reservation_locked(list);
158*592ffb21SWarner Losh
159*592ffb21SWarner Losh /*
160*592ffb21SWarner Losh * temporarily increase sequence number every retry,
161*592ffb21SWarner Losh * to prevent us from seeing our old reservation
162*592ffb21SWarner Losh * sequence when someone else reserved the buffer,
163*592ffb21SWarner Losh * but hasn't updated the seq_valid/seqno members yet.
164*592ffb21SWarner Losh */
165*592ffb21SWarner Losh val_seq = entry->bo->bdev->val_seq++;
166*592ffb21SWarner Losh
167*592ffb21SWarner Losh ttm_eu_list_ref_sub(list);
168*592ffb21SWarner Losh ret = ttm_bo_reserve_slowpath_nolru(bo, true, val_seq);
169*592ffb21SWarner Losh if (unlikely(ret != 0)) {
170*592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
171*592ffb21SWarner Losh return ret;
172*592ffb21SWarner Losh }
173*592ffb21SWarner Losh entry->reserved = true;
174*592ffb21SWarner Losh if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
175*592ffb21SWarner Losh ret = -EBUSY;
176*592ffb21SWarner Losh goto err;
177*592ffb21SWarner Losh }
178*592ffb21SWarner Losh goto retry_locked;
179*592ffb21SWarner Losh default:
180*592ffb21SWarner Losh goto err;
181*592ffb21SWarner Losh }
182*592ffb21SWarner Losh
183*592ffb21SWarner Losh entry->reserved = true;
184*592ffb21SWarner Losh if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
185*592ffb21SWarner Losh ret = -EBUSY;
186*592ffb21SWarner Losh goto err;
187*592ffb21SWarner Losh }
188*592ffb21SWarner Losh }
189*592ffb21SWarner Losh
190*592ffb21SWarner Losh ttm_eu_del_from_lru_locked(list);
191*592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
192*592ffb21SWarner Losh ttm_eu_list_ref_sub(list);
193*592ffb21SWarner Losh
194*592ffb21SWarner Losh return 0;
195*592ffb21SWarner Losh
196*592ffb21SWarner Losh err:
197*592ffb21SWarner Losh ttm_eu_backoff_reservation_locked(list);
198*592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
199*592ffb21SWarner Losh ttm_eu_list_ref_sub(list);
200*592ffb21SWarner Losh return ret;
201*592ffb21SWarner Losh }
202*592ffb21SWarner Losh
ttm_eu_fence_buffer_objects(struct list_head * list,void * sync_obj)203*592ffb21SWarner Losh void ttm_eu_fence_buffer_objects(struct list_head *list, void *sync_obj)
204*592ffb21SWarner Losh {
205*592ffb21SWarner Losh struct ttm_validate_buffer *entry;
206*592ffb21SWarner Losh struct ttm_buffer_object *bo;
207*592ffb21SWarner Losh struct ttm_bo_global *glob;
208*592ffb21SWarner Losh struct ttm_bo_device *bdev;
209*592ffb21SWarner Losh struct ttm_bo_driver *driver;
210*592ffb21SWarner Losh
211*592ffb21SWarner Losh if (list_empty(list))
212*592ffb21SWarner Losh return;
213*592ffb21SWarner Losh
214*592ffb21SWarner Losh bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
215*592ffb21SWarner Losh bdev = bo->bdev;
216*592ffb21SWarner Losh driver = bdev->driver;
217*592ffb21SWarner Losh glob = bo->glob;
218*592ffb21SWarner Losh
219*592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
220*592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
221*592ffb21SWarner Losh
222*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
223*592ffb21SWarner Losh bo = entry->bo;
224*592ffb21SWarner Losh entry->old_sync_obj = bo->sync_obj;
225*592ffb21SWarner Losh bo->sync_obj = driver->sync_obj_ref(sync_obj);
226*592ffb21SWarner Losh ttm_bo_unreserve_locked(bo);
227*592ffb21SWarner Losh entry->reserved = false;
228*592ffb21SWarner Losh }
229*592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
230*592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
231*592ffb21SWarner Losh
232*592ffb21SWarner Losh list_for_each_entry(entry, list, head) {
233*592ffb21SWarner Losh if (entry->old_sync_obj)
234*592ffb21SWarner Losh driver->sync_obj_unref(&entry->old_sync_obj);
235*592ffb21SWarner Losh }
236*592ffb21SWarner Losh }
237