blob: efcb734e55439960bc8956ba9f3832d3181627e3 [file] [log] [blame]
Thomas Hellstromc078aa22009-12-06 21:46:26 +01001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/ttm/ttm_execbuf_util.h>
29#include <drm/ttm/ttm_bo_driver.h>
30#include <drm/ttm/ttm_placement.h>
Thomas Hellstromc078aa22009-12-06 21:46:26 +010031#include <linux/wait.h>
32#include <linux/sched.h>
33#include <linux/module.h>
34
Maarten Lankhorstecff6652013-06-27 13:48:17 +020035static void ttm_eu_backoff_reservation_locked(struct list_head *list,
36 struct ww_acquire_ctx *ticket)
Dave Airlied6ea8882010-11-22 13:24:40 +100037{
38 struct ttm_validate_buffer *entry;
39
40 list_for_each_entry(entry, list, head) {
41 struct ttm_buffer_object *bo = entry->bo;
42 if (!entry->reserved)
43 continue;
44
Maarten Lankhorstecff6652013-06-27 13:48:17 +020045 entry->reserved = false;
Dave Airlied6ea8882010-11-22 13:24:40 +100046 if (entry->removed) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +020047 ttm_bo_unreserve_ticket_locked(bo, ticket);
Dave Airlied6ea8882010-11-22 13:24:40 +100048 entry->removed = false;
49
Maarten Lankhorstecff6652013-06-27 13:48:17 +020050 } else {
51 atomic_set(&bo->reserved, 0);
52 wake_up_all(&bo->event_queue);
Dave Airlied6ea8882010-11-22 13:24:40 +100053 }
Dave Airlied6ea8882010-11-22 13:24:40 +100054 }
55}
56
57static void ttm_eu_del_from_lru_locked(struct list_head *list)
58{
59 struct ttm_validate_buffer *entry;
60
61 list_for_each_entry(entry, list, head) {
62 struct ttm_buffer_object *bo = entry->bo;
63 if (!entry->reserved)
64 continue;
65
66 if (!entry->removed) {
67 entry->put_count = ttm_bo_del_from_lru(bo);
68 entry->removed = true;
69 }
70 }
71}
72
73static void ttm_eu_list_ref_sub(struct list_head *list)
74{
75 struct ttm_validate_buffer *entry;
76
77 list_for_each_entry(entry, list, head) {
78 struct ttm_buffer_object *bo = entry->bo;
79
80 if (entry->put_count) {
81 ttm_bo_list_ref_sub(bo, entry->put_count, true);
82 entry->put_count = 0;
83 }
84 }
85}
86
Maarten Lankhorstecff6652013-06-27 13:48:17 +020087void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
88 struct list_head *list)
Thomas Hellstromc078aa22009-12-06 21:46:26 +010089{
90 struct ttm_validate_buffer *entry;
Thomas Hellstrom68c4fa32010-11-17 12:28:27 +000091 struct ttm_bo_global *glob;
Thomas Hellstromc078aa22009-12-06 21:46:26 +010092
Thomas Hellstrom68c4fa32010-11-17 12:28:27 +000093 if (list_empty(list))
94 return;
Thomas Hellstromc078aa22009-12-06 21:46:26 +010095
Thomas Hellstrom68c4fa32010-11-17 12:28:27 +000096 entry = list_first_entry(list, struct ttm_validate_buffer, head);
97 glob = entry->bo->glob;
98 spin_lock(&glob->lru_lock);
Maarten Lankhorstecff6652013-06-27 13:48:17 +020099 ttm_eu_backoff_reservation_locked(list, ticket);
100 ww_acquire_fini(ticket);
Thomas Hellstrom68c4fa32010-11-17 12:28:27 +0000101 spin_unlock(&glob->lru_lock);
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100102}
103EXPORT_SYMBOL(ttm_eu_backoff_reservation);
104
105/*
106 * Reserve buffers for validation.
107 *
108 * If a buffer in the list is marked for CPU access, we back off and
109 * wait for that buffer to become free for GPU access.
110 *
111 * If a buffer is reserved for another validation, the validator with
112 * the highest validation sequence backs off and waits for that buffer
113 * to become unreserved. This prevents deadlocks when validating multiple
114 * buffers in different orders.
115 */
116
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200117int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
118 struct list_head *list)
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100119{
Dave Airlied6ea8882010-11-22 13:24:40 +1000120 struct ttm_bo_global *glob;
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100121 struct ttm_validate_buffer *entry;
122 int ret;
123
Dave Airlied6ea8882010-11-22 13:24:40 +1000124 if (list_empty(list))
125 return 0;
126
127 list_for_each_entry(entry, list, head) {
128 entry->reserved = false;
129 entry->put_count = 0;
130 entry->removed = false;
131 }
132
133 entry = list_first_entry(list, struct ttm_validate_buffer, head);
134 glob = entry->bo->glob;
135
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200136 ww_acquire_init(ticket, &reservation_ww_class);
Dave Airlied6ea8882010-11-22 13:24:40 +1000137 spin_lock(&glob->lru_lock);
Thomas Hellstrom65705962010-11-17 12:28:31 +0000138
Maarten Lankhorstf2d476a2013-01-15 14:57:10 +0100139retry:
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100140 list_for_each_entry(entry, list, head) {
141 struct ttm_buffer_object *bo = entry->bo;
142
Maarten Lankhorstf2d476a2013-01-15 14:57:10 +0100143 /* already slowpath reserved? */
144 if (entry->reserved)
145 continue;
146
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200147 ret = ttm_bo_reserve_nolru(bo, true, true, true, ticket);
Dave Airlied6ea8882010-11-22 13:24:40 +1000148 switch (ret) {
149 case 0:
150 break;
151 case -EBUSY:
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100152 ttm_eu_del_from_lru_locked(list);
153 spin_unlock(&glob->lru_lock);
154 ret = ttm_bo_reserve_nolru(bo, true, false,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200155 true, ticket);
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100156 spin_lock(&glob->lru_lock);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200157
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100158 if (!ret)
159 break;
160
161 if (unlikely(ret != -EAGAIN))
162 goto err;
163
164 /* fallthrough */
Dave Airlied6ea8882010-11-22 13:24:40 +1000165 case -EAGAIN:
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200166 ttm_eu_backoff_reservation_locked(list, ticket);
Dave Airlied6ea8882010-11-22 13:24:40 +1000167 spin_unlock(&glob->lru_lock);
168 ttm_eu_list_ref_sub(list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200169 ret = ttm_bo_reserve_slowpath_nolru(bo, true, ticket);
Dave Airlied6ea8882010-11-22 13:24:40 +1000170 if (unlikely(ret != 0))
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200171 goto err_fini;
172
Maarten Lankhorstf2d476a2013-01-15 14:57:10 +0100173 spin_lock(&glob->lru_lock);
174 entry->reserved = true;
175 if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
176 ret = -EBUSY;
177 goto err;
178 }
Dave Airlied6ea8882010-11-22 13:24:40 +1000179 goto retry;
180 default:
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100181 goto err;
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100182 }
183
184 entry->reserved = true;
185 if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100186 ret = -EBUSY;
187 goto err;
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100188 }
189 }
Dave Airlied6ea8882010-11-22 13:24:40 +1000190
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200191 ww_acquire_done(ticket);
Dave Airlied6ea8882010-11-22 13:24:40 +1000192 ttm_eu_del_from_lru_locked(list);
193 spin_unlock(&glob->lru_lock);
194 ttm_eu_list_ref_sub(list);
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100195 return 0;
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100196
197err:
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200198 ttm_eu_backoff_reservation_locked(list, ticket);
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100199 spin_unlock(&glob->lru_lock);
200 ttm_eu_list_ref_sub(list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200201err_fini:
202 ww_acquire_done(ticket);
203 ww_acquire_fini(ticket);
Maarten Lankhorst7a186302013-01-15 14:56:48 +0100204 return ret;
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100205}
206EXPORT_SYMBOL(ttm_eu_reserve_buffers);
207
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200208void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
209 struct list_head *list, void *sync_obj)
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100210{
211 struct ttm_validate_buffer *entry;
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000212 struct ttm_buffer_object *bo;
213 struct ttm_bo_global *glob;
214 struct ttm_bo_device *bdev;
215 struct ttm_bo_driver *driver;
216
217 if (list_empty(list))
218 return;
219
220 bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
221 bdev = bo->bdev;
222 driver = bdev->driver;
223 glob = bo->glob;
224
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000225 spin_lock(&glob->lru_lock);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100226 spin_lock(&bdev->fence_lock);
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100227
228 list_for_each_entry(entry, list, head) {
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000229 bo = entry->bo;
230 entry->old_sync_obj = bo->sync_obj;
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100231 bo->sync_obj = driver->sync_obj_ref(sync_obj);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200232 ttm_bo_unreserve_ticket_locked(bo, ticket);
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100233 entry->reserved = false;
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000234 }
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000235 spin_unlock(&bdev->fence_lock);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100236 spin_unlock(&glob->lru_lock);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200237 ww_acquire_fini(ticket);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000238
239 list_for_each_entry(entry, list, head) {
240 if (entry->old_sync_obj)
241 driver->sync_obj_unref(&entry->old_sync_obj);
Thomas Hellstromc078aa22009-12-06 21:46:26 +0100242 }
243}
244EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);