1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include <drm/ttm/ttm_execbuf_util.h>
29 #include <drm/ttm/ttm_bo_driver.h>
30 #include <drm/ttm/ttm_placement.h>
31 #include <linux/export.h>
32 #include <linux/wait.h>
34 /* XXX this should go to dma-buf driver, for now just to avoid undef */
35 DEFINE_WW_CLASS(reservation_ww_class);
37 static void ttm_eu_backoff_reservation_locked(struct list_head *list,
38 struct ww_acquire_ctx *ticket)
40 struct ttm_validate_buffer *entry;
42 list_for_each_entry(entry, list, head) {
43 struct ttm_buffer_object *bo = entry->bo;
47 entry->reserved = false;
49 ttm_bo_unreserve_ticket_locked(bo, ticket);
50 entry->removed = false;
53 atomic_set(&bo->reserved, 0);
54 wake_up_all(&bo->event_queue);
59 static void ttm_eu_del_from_lru_locked(struct list_head *list)
61 struct ttm_validate_buffer *entry;
63 list_for_each_entry(entry, list, head) {
64 struct ttm_buffer_object *bo = entry->bo;
68 if (!entry->removed) {
69 entry->put_count = ttm_bo_del_from_lru(bo);
70 entry->removed = true;
75 static void ttm_eu_list_ref_sub(struct list_head *list)
77 struct ttm_validate_buffer *entry;
79 list_for_each_entry(entry, list, head) {
80 struct ttm_buffer_object *bo = entry->bo;
82 if (entry->put_count) {
83 ttm_bo_list_ref_sub(bo, entry->put_count, true);
89 void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
90 struct list_head *list)
92 struct ttm_validate_buffer *entry;
93 struct ttm_bo_global *glob;
98 entry = list_first_entry(list, struct ttm_validate_buffer, head);
99 glob = entry->bo->glob;
100 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
101 ttm_eu_backoff_reservation_locked(list, ticket);
102 ww_acquire_fini(ticket);
103 lockmgr(&glob->lru_lock, LK_RELEASE);
105 EXPORT_SYMBOL(ttm_eu_backoff_reservation);
108 * Reserve buffers for validation.
110 * If a buffer in the list is marked for CPU access, we back off and
111 * wait for that buffer to become free for GPU access.
113 * If a buffer is reserved for another validation, the validator with
114 * the highest validation sequence backs off and waits for that buffer
115 * to become unreserved. This prevents deadlocks when validating multiple
116 * buffers in different orders.
119 int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
120 struct list_head *list)
122 struct ttm_bo_global *glob;
123 struct ttm_validate_buffer *entry;
126 if (list_empty(list))
129 list_for_each_entry(entry, list, head) {
130 entry->reserved = false;
131 entry->put_count = 0;
132 entry->removed = false;
135 entry = list_first_entry(list, struct ttm_validate_buffer, head);
136 glob = entry->bo->glob;
138 ww_acquire_init(ticket, &reservation_ww_class);
139 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
142 list_for_each_entry(entry, list, head) {
143 struct ttm_buffer_object *bo = entry->bo;
146 /* already slowpath reserved? */
150 ret = ttm_bo_reserve_nolru(bo, true, true, true, ticket);
155 ttm_eu_del_from_lru_locked(list);
156 owned = lockstatus(&glob->lru_lock, curthread);
157 if (owned == LK_EXCLUSIVE)
158 lockmgr(&glob->lru_lock, LK_RELEASE);
159 ret = ttm_bo_reserve_nolru(bo, true, false,
161 if (owned == LK_EXCLUSIVE)
162 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
166 if (unlikely(ret != -EAGAIN))
171 ttm_eu_backoff_reservation_locked(list, ticket);
172 lockmgr(&glob->lru_lock, LK_RELEASE);
173 ttm_eu_list_ref_sub(list);
174 ret = ttm_bo_reserve_slowpath_nolru(bo, true, ticket);
175 if (unlikely(ret != 0))
178 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
179 entry->reserved = true;
180 if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
189 entry->reserved = true;
190 if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
196 ww_acquire_done(ticket);
197 ttm_eu_del_from_lru_locked(list);
198 lockmgr(&glob->lru_lock, LK_RELEASE);
199 ttm_eu_list_ref_sub(list);
203 ttm_eu_backoff_reservation_locked(list, ticket);
204 lockmgr(&glob->lru_lock, LK_RELEASE);
205 ttm_eu_list_ref_sub(list);
207 ww_acquire_done(ticket);
208 ww_acquire_fini(ticket);
211 EXPORT_SYMBOL(ttm_eu_reserve_buffers);
213 void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
214 struct list_head *list, void *sync_obj)
216 struct ttm_validate_buffer *entry;
217 struct ttm_buffer_object *bo;
218 struct ttm_bo_global *glob;
219 struct ttm_bo_device *bdev;
220 struct ttm_bo_driver *driver;
222 if (list_empty(list))
225 bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
227 driver = bdev->driver;
230 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
231 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
233 list_for_each_entry(entry, list, head) {
235 entry->old_sync_obj = bo->sync_obj;
236 bo->sync_obj = driver->sync_obj_ref(sync_obj);
237 ttm_bo_unreserve_ticket_locked(bo, ticket);
238 entry->reserved = false;
240 lockmgr(&bdev->fence_lock, LK_RELEASE);
241 lockmgr(&glob->lru_lock, LK_RELEASE);
242 ww_acquire_fini(ticket);
244 list_for_each_entry(entry, list, head) {
245 if (entry->old_sync_obj)
246 driver->sync_obj_unref(&entry->old_sync_obj);
249 EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);