blob: c812570ff159fb5dd75f30bce4c1f573386a80f1 [file] [log] [blame]
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001/**************************************************************************
2 *
Sinclair Yeh54fbde82015-07-29 12:38:02 -07003 * Copyright © 2011-2014 VMware, Inc., Palo Alto, CA., USA
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004 * 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/drmP.h>
Thomas Hellstromae2a1042011-09-01 20:18:44 +000029#include "vmwgfx_drv.h"
30
31#define VMW_FENCE_WRAP (1 << 31)
32
33struct vmw_fence_manager {
34 int num_fence_objects;
35 struct vmw_private *dev_priv;
36 spinlock_t lock;
Thomas Hellstromae2a1042011-09-01 20:18:44 +000037 struct list_head fence_list;
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -080038 struct work_struct work;
Thomas Hellstromae2a1042011-09-01 20:18:44 +000039 u32 user_fence_size;
40 u32 fence_size;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020041 u32 event_fence_action_size;
Thomas Hellstromae2a1042011-09-01 20:18:44 +000042 bool fifo_down;
43 struct list_head cleanup_list;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020044 uint32_t pending_actions[VMW_ACTION_MAX];
45 struct mutex goal_irq_mutex;
46 bool goal_irq_on; /* Protected by @goal_irq_mutex */
47 bool seqno_valid; /* Protected by @lock, and may not be set to true
48 without the @goal_irq_mutex held. */
Christian König76bf0db2016-06-01 15:10:02 +020049 u64 ctx;
Thomas Hellstromae2a1042011-09-01 20:18:44 +000050};
51
52struct vmw_user_fence {
53 struct ttm_base_object base;
54 struct vmw_fence_obj fence;
55};
56
57/**
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020058 * struct vmw_event_fence_action - fence action that delivers a drm event.
Thomas Hellstromae2a1042011-09-01 20:18:44 +000059 *
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020060 * @e: A struct drm_pending_event that controls the event delivery.
61 * @action: A struct vmw_fence_action to hook up to a fence.
62 * @fence: A referenced pointer to the fence to keep it alive while @action
63 * hangs on it.
64 * @dev: Pointer to a struct drm_device so we can access the event stuff.
65 * @kref: Both @e and @action has destructors, so we need to refcount.
66 * @size: Size accounted for this object.
67 * @tv_sec: If non-null, the variable pointed to will be assigned
68 * current time tv_sec val when the fence signals.
69 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
70 * be assigned the current time tv_usec val when the fence signals.
71 */
72struct vmw_event_fence_action {
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020073 struct vmw_fence_action action;
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +010074
75 struct drm_pending_event *event;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020076 struct vmw_fence_obj *fence;
77 struct drm_device *dev;
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +010078
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020079 uint32_t *tv_sec;
80 uint32_t *tv_usec;
81};
82
Maarten Lankhorst2298e802014-03-26 14:07:44 +010083static struct vmw_fence_manager *
84fman_from_fence(struct vmw_fence_obj *fence)
85{
86 return container_of(fence->base.lock, struct vmw_fence_manager, lock);
87}
88
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +020089/**
90 * Note on fencing subsystem usage of irqs:
91 * Typically the vmw_fences_update function is called
92 *
93 * a) When a new fence seqno has been submitted by the fifo code.
94 * b) On-demand when we have waiters. Sleeping waiters will switch on the
95 * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE
96 * irq is received. When the last fence waiter is gone, that IRQ is masked
97 * away.
98 *
99 * In situations where there are no waiters and we don't submit any new fences,
100 * fence objects may not be signaled. This is perfectly OK, since there are
101 * no consumers of the signaled data, but that is NOT ok when there are fence
102 * actions attached to a fence. The fencing subsystem then makes use of the
103 * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence
104 * which has an action attached, and each time vmw_fences_update is called,
105 * the subsystem makes sure the fence goal seqno is updated.
106 *
107 * The fence goal seqno irq is on as long as there are unsignaled fence
108 * objects with actions attached to them.
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000109 */
110
Chris Wilsonf54d1862016-10-25 13:00:45 +0100111static void vmw_fence_obj_destroy(struct dma_fence *f)
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000112{
113 struct vmw_fence_obj *fence =
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100114 container_of(f, struct vmw_fence_obj, base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000115
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100116 struct vmw_fence_manager *fman = fman_from_fence(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000117
Thomas Hellstromef369902017-08-24 08:06:28 +0200118 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000119 list_del_init(&fence->head);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100120 --fman->num_fence_objects;
Thomas Hellstromef369902017-08-24 08:06:28 +0200121 spin_unlock(&fman->lock);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100122 fence->destroy(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000123}
124
Chris Wilsonf54d1862016-10-25 13:00:45 +0100125static const char *vmw_fence_get_driver_name(struct dma_fence *f)
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100126{
127 return "vmwgfx";
128}
129
Chris Wilsonf54d1862016-10-25 13:00:45 +0100130static const char *vmw_fence_get_timeline_name(struct dma_fence *f)
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100131{
132 return "svga";
133}
134
Chris Wilsonf54d1862016-10-25 13:00:45 +0100135static bool vmw_fence_enable_signaling(struct dma_fence *f)
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100136{
137 struct vmw_fence_obj *fence =
138 container_of(f, struct vmw_fence_obj, base);
139
140 struct vmw_fence_manager *fman = fman_from_fence(fence);
141 struct vmw_private *dev_priv = fman->dev_priv;
142
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100143 u32 *fifo_mem = dev_priv->mmio_virt;
144 u32 seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100145 if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
146 return false;
147
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800148 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100149
150 return true;
151}
152
153struct vmwgfx_wait_cb {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100154 struct dma_fence_cb base;
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100155 struct task_struct *task;
156};
157
158static void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100159vmwgfx_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100160{
161 struct vmwgfx_wait_cb *wait =
162 container_of(cb, struct vmwgfx_wait_cb, base);
163
164 wake_up_process(wait->task);
165}
166
167static void __vmw_fences_update(struct vmw_fence_manager *fman);
168
Chris Wilsonf54d1862016-10-25 13:00:45 +0100169static long vmw_fence_wait(struct dma_fence *f, bool intr, signed long timeout)
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100170{
171 struct vmw_fence_obj *fence =
172 container_of(f, struct vmw_fence_obj, base);
173
174 struct vmw_fence_manager *fman = fman_from_fence(fence);
175 struct vmw_private *dev_priv = fman->dev_priv;
176 struct vmwgfx_wait_cb cb;
177 long ret = timeout;
178 unsigned long irq_flags;
179
180 if (likely(vmw_fence_obj_signaled(fence)))
181 return timeout;
182
183 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
184 vmw_seqno_waiter_add(dev_priv);
185
186 spin_lock_irqsave(f->lock, irq_flags);
187
188 if (intr && signal_pending(current)) {
189 ret = -ERESTARTSYS;
190 goto out;
191 }
192
193 cb.base.func = vmwgfx_wait_cb;
194 cb.task = current;
195 list_add(&cb.base.node, &f->cb_list);
196
197 while (ret > 0) {
198 __vmw_fences_update(fman);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100199 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags))
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100200 break;
201
202 if (intr)
203 __set_current_state(TASK_INTERRUPTIBLE);
204 else
205 __set_current_state(TASK_UNINTERRUPTIBLE);
206 spin_unlock_irqrestore(f->lock, irq_flags);
207
208 ret = schedule_timeout(ret);
209
210 spin_lock_irqsave(f->lock, irq_flags);
211 if (ret > 0 && intr && signal_pending(current))
212 ret = -ERESTARTSYS;
213 }
214
215 if (!list_empty(&cb.base.node))
216 list_del(&cb.base.node);
217 __set_current_state(TASK_RUNNING);
218
219out:
220 spin_unlock_irqrestore(f->lock, irq_flags);
221
222 vmw_seqno_waiter_remove(dev_priv);
223
224 return ret;
225}
226
Chris Wilsonf54d1862016-10-25 13:00:45 +0100227static struct dma_fence_ops vmw_fence_ops = {
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100228 .get_driver_name = vmw_fence_get_driver_name,
229 .get_timeline_name = vmw_fence_get_timeline_name,
230 .enable_signaling = vmw_fence_enable_signaling,
231 .wait = vmw_fence_wait,
232 .release = vmw_fence_obj_destroy,
233};
234
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000235
236/**
237 * Execute signal actions on fences recently signaled.
238 * This is done from a workqueue so we don't have to execute
239 * signal actions from atomic context.
240 */
241
242static void vmw_fence_work_func(struct work_struct *work)
243{
244 struct vmw_fence_manager *fman =
245 container_of(work, struct vmw_fence_manager, work);
246 struct list_head list;
247 struct vmw_fence_action *action, *next_action;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200248 bool seqno_valid;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000249
250 do {
251 INIT_LIST_HEAD(&list);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200252 mutex_lock(&fman->goal_irq_mutex);
253
Thomas Hellstromef369902017-08-24 08:06:28 +0200254 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000255 list_splice_init(&fman->cleanup_list, &list);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200256 seqno_valid = fman->seqno_valid;
Thomas Hellstromef369902017-08-24 08:06:28 +0200257 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000258
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200259 if (!seqno_valid && fman->goal_irq_on) {
260 fman->goal_irq_on = false;
261 vmw_goal_waiter_remove(fman->dev_priv);
262 }
263 mutex_unlock(&fman->goal_irq_mutex);
264
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000265 if (list_empty(&list))
266 return;
267
268 /*
269 * At this point, only we should be able to manipulate the
270 * list heads of the actions we have on the private list.
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200271 * hence fman::lock not held.
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000272 */
273
274 list_for_each_entry_safe(action, next_action, &list, head) {
275 list_del_init(&action->head);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200276 if (action->cleanup)
277 action->cleanup(action);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000278 }
279 } while (1);
280}
281
282struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
283{
284 struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
285
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530286 if (unlikely(!fman))
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000287 return NULL;
288
289 fman->dev_priv = dev_priv;
290 spin_lock_init(&fman->lock);
291 INIT_LIST_HEAD(&fman->fence_list);
292 INIT_LIST_HEAD(&fman->cleanup_list);
293 INIT_WORK(&fman->work, &vmw_fence_work_func);
294 fman->fifo_down = true;
295 fman->user_fence_size = ttm_round_pot(sizeof(struct vmw_user_fence));
296 fman->fence_size = ttm_round_pot(sizeof(struct vmw_fence_obj));
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200297 fman->event_fence_action_size =
298 ttm_round_pot(sizeof(struct vmw_event_fence_action));
299 mutex_init(&fman->goal_irq_mutex);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100300 fman->ctx = dma_fence_context_alloc(1);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000301
302 return fman;
303}
304
305void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
306{
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000307 bool lists_empty;
308
309 (void) cancel_work_sync(&fman->work);
310
Thomas Hellstromef369902017-08-24 08:06:28 +0200311 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000312 lists_empty = list_empty(&fman->fence_list) &&
313 list_empty(&fman->cleanup_list);
Thomas Hellstromef369902017-08-24 08:06:28 +0200314 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000315
316 BUG_ON(!lists_empty);
317 kfree(fman);
318}
319
320static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100321 struct vmw_fence_obj *fence, u32 seqno,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000322 void (*destroy) (struct vmw_fence_obj *fence))
323{
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000324 int ret = 0;
325
Chris Wilsonf54d1862016-10-25 13:00:45 +0100326 dma_fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
327 fman->ctx, seqno);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000328 INIT_LIST_HEAD(&fence->seq_passed_actions);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000329 fence->destroy = destroy;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000330
Thomas Hellstromef369902017-08-24 08:06:28 +0200331 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000332 if (unlikely(fman->fifo_down)) {
333 ret = -EBUSY;
334 goto out_unlock;
335 }
336 list_add_tail(&fence->head, &fman->fence_list);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100337 ++fman->num_fence_objects;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000338
339out_unlock:
Thomas Hellstromef369902017-08-24 08:06:28 +0200340 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000341 return ret;
342
343}
344
Rashika Kheria94844cf2014-01-06 22:21:21 +0530345static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000346 struct list_head *list)
347{
348 struct vmw_fence_action *action, *next_action;
349
350 list_for_each_entry_safe(action, next_action, list, head) {
351 list_del_init(&action->head);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200352 fman->pending_actions[action->type]--;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000353 if (action->seq_passed != NULL)
354 action->seq_passed(action);
355
356 /*
357 * Add the cleanup action to the cleanup list so that
358 * it will be performed by a worker task.
359 */
360
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200361 list_add_tail(&action->head, &fman->cleanup_list);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000362 }
363}
364
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200365/**
366 * vmw_fence_goal_new_locked - Figure out a new device fence goal
367 * seqno if needed.
368 *
369 * @fman: Pointer to a fence manager.
370 * @passed_seqno: The seqno the device currently signals as passed.
371 *
372 * This function should be called with the fence manager lock held.
373 * It is typically called when we have a new passed_seqno, and
374 * we might need to update the fence goal. It checks to see whether
375 * the current fence goal has already passed, and, in that case,
376 * scans through all unsignaled fences to get the next fence object with an
377 * action attached, and sets the seqno of that fence as a new fence goal.
378 *
379 * returns true if the device goal seqno was updated. False otherwise.
380 */
381static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
382 u32 passed_seqno)
383{
384 u32 goal_seqno;
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100385 u32 *fifo_mem;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200386 struct vmw_fence_obj *fence;
387
388 if (likely(!fman->seqno_valid))
389 return false;
390
391 fifo_mem = fman->dev_priv->mmio_virt;
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100392 goal_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE_GOAL);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200393 if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
394 return false;
395
396 fman->seqno_valid = false;
397 list_for_each_entry(fence, &fman->fence_list, head) {
398 if (!list_empty(&fence->seq_passed_actions)) {
399 fman->seqno_valid = true;
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100400 vmw_mmio_write(fence->base.seqno,
401 fifo_mem + SVGA_FIFO_FENCE_GOAL);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200402 break;
403 }
404 }
405
406 return true;
407}
408
409
410/**
411 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
412 * needed.
413 *
414 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
415 * considered as a device fence goal.
416 *
417 * This function should be called with the fence manager lock held.
418 * It is typically called when an action has been attached to a fence to
419 * check whether the seqno of that fence should be used for a fence
420 * goal interrupt. This is typically needed if the current fence goal is
421 * invalid, or has a higher seqno than that of the current fence object.
422 *
423 * returns true if the device goal seqno was updated. False otherwise.
424 */
425static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
426{
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100427 struct vmw_fence_manager *fman = fman_from_fence(fence);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200428 u32 goal_seqno;
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100429 u32 *fifo_mem;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200430
Chris Wilsonf54d1862016-10-25 13:00:45 +0100431 if (dma_fence_is_signaled_locked(&fence->base))
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200432 return false;
433
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100434 fifo_mem = fman->dev_priv->mmio_virt;
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100435 goal_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE_GOAL);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100436 if (likely(fman->seqno_valid &&
437 goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200438 return false;
439
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100440 vmw_mmio_write(fence->base.seqno, fifo_mem + SVGA_FIFO_FENCE_GOAL);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100441 fman->seqno_valid = true;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200442
443 return true;
444}
445
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100446static void __vmw_fences_update(struct vmw_fence_manager *fman)
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000447{
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000448 struct vmw_fence_obj *fence, *next_fence;
449 struct list_head action_list;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200450 bool needs_rerun;
451 uint32_t seqno, new_seqno;
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100452 u32 *fifo_mem = fman->dev_priv->mmio_virt;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000453
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100454 seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200455rerun:
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000456 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100457 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000458 list_del_init(&fence->head);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100459 dma_fence_signal_locked(&fence->base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000460 INIT_LIST_HEAD(&action_list);
461 list_splice_init(&fence->seq_passed_actions,
462 &action_list);
463 vmw_fences_perform_actions(fman, &action_list);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200464 } else
465 break;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000466 }
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200467
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200468 /*
469 * Rerun if the fence goal seqno was updated, and the
470 * hardware might have raced with that update, so that
471 * we missed a fence_goal irq.
472 */
473
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100474 needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200475 if (unlikely(needs_rerun)) {
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100476 new_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200477 if (new_seqno != seqno) {
478 seqno = new_seqno;
479 goto rerun;
480 }
481 }
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100482
483 if (!list_empty(&fman->cleanup_list))
484 (void) schedule_work(&fman->work);
485}
486
487void vmw_fences_update(struct vmw_fence_manager *fman)
488{
Thomas Hellstromef369902017-08-24 08:06:28 +0200489 spin_lock(&fman->lock);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100490 __vmw_fences_update(fman);
Thomas Hellstromef369902017-08-24 08:06:28 +0200491 spin_unlock(&fman->lock);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200492}
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000493
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100494bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000495{
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100496 struct vmw_fence_manager *fman = fman_from_fence(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000497
Chris Wilsonf54d1862016-10-25 13:00:45 +0100498 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000499 return 1;
500
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100501 vmw_fences_update(fman);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000502
Chris Wilsonf54d1862016-10-25 13:00:45 +0100503 return dma_fence_is_signaled(&fence->base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000504}
505
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100506int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000507 bool interruptible, unsigned long timeout)
508{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100509 long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000510
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100511 if (likely(ret > 0))
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000512 return 0;
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100513 else if (ret == 0)
514 return -EBUSY;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000515 else
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100516 return ret;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000517}
518
519void vmw_fence_obj_flush(struct vmw_fence_obj *fence)
520{
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100521 struct vmw_private *dev_priv = fman_from_fence(fence)->dev_priv;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000522
523 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
524}
525
526static void vmw_fence_destroy(struct vmw_fence_obj *fence)
527{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100528 dma_fence_free(&fence->base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000529}
530
531int vmw_fence_create(struct vmw_fence_manager *fman,
532 uint32_t seqno,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000533 struct vmw_fence_obj **p_fence)
534{
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000535 struct vmw_fence_obj *fence;
Thomas Hellstromf7652af2017-03-27 11:09:08 +0200536 int ret;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000537
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000538 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530539 if (unlikely(!fence))
Thomas Hellstrom1f563a62014-12-02 03:32:24 -0800540 return -ENOMEM;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000541
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100542 ret = vmw_fence_obj_init(fman, fence, seqno,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000543 vmw_fence_destroy);
544 if (unlikely(ret != 0))
545 goto out_err_init;
546
547 *p_fence = fence;
548 return 0;
549
550out_err_init:
551 kfree(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000552 return ret;
553}
554
555
556static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
557{
558 struct vmw_user_fence *ufence =
559 container_of(fence, struct vmw_user_fence, fence);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100560 struct vmw_fence_manager *fman = fman_from_fence(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000561
Thomas Hellstrom35f62a52012-11-20 12:16:49 +0000562 ttm_base_object_kfree(ufence, base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000563 /*
564 * Free kernel space accounting.
565 */
566 ttm_mem_global_free(vmw_mem_glob(fman->dev_priv),
567 fman->user_fence_size);
568}
569
570static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
571{
572 struct ttm_base_object *base = *p_base;
573 struct vmw_user_fence *ufence =
574 container_of(base, struct vmw_user_fence, base);
575 struct vmw_fence_obj *fence = &ufence->fence;
576
577 *p_base = NULL;
578 vmw_fence_obj_unreference(&fence);
579}
580
581int vmw_user_fence_create(struct drm_file *file_priv,
582 struct vmw_fence_manager *fman,
583 uint32_t seqno,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000584 struct vmw_fence_obj **p_fence,
585 uint32_t *p_handle)
586{
587 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
588 struct vmw_user_fence *ufence;
589 struct vmw_fence_obj *tmp;
590 struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
591 int ret;
592
593 /*
594 * Kernel memory space accounting, since this object may
595 * be created by a user-space request.
596 */
597
598 ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
599 false, false);
600 if (unlikely(ret != 0))
601 return ret;
602
603 ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530604 if (unlikely(!ufence)) {
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000605 ret = -ENOMEM;
606 goto out_no_object;
607 }
608
609 ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100610 vmw_user_fence_destroy);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000611 if (unlikely(ret != 0)) {
612 kfree(ufence);
613 goto out_no_object;
614 }
615
616 /*
617 * The base object holds a reference which is freed in
618 * vmw_user_fence_base_release.
619 */
620 tmp = vmw_fence_obj_reference(&ufence->fence);
621 ret = ttm_base_object_init(tfile, &ufence->base, false,
622 VMW_RES_FENCE,
623 &vmw_user_fence_base_release, NULL);
624
625
626 if (unlikely(ret != 0)) {
627 /*
628 * Free the base object's reference
629 */
630 vmw_fence_obj_unreference(&tmp);
631 goto out_err;
632 }
633
634 *p_fence = &ufence->fence;
635 *p_handle = ufence->base.hash.key;
636
637 return 0;
638out_err:
639 tmp = &ufence->fence;
640 vmw_fence_obj_unreference(&tmp);
641out_no_object:
642 ttm_mem_global_free(mem_glob, fman->user_fence_size);
643 return ret;
644}
645
646
647/**
648 * vmw_fence_fifo_down - signal all unsignaled fence objects.
649 */
650
651void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
652{
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000653 struct list_head action_list;
654 int ret;
655
656 /*
657 * The list may be altered while we traverse it, so always
658 * restart when we've released the fman->lock.
659 */
660
Thomas Hellstromef369902017-08-24 08:06:28 +0200661 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000662 fman->fifo_down = true;
663 while (!list_empty(&fman->fence_list)) {
664 struct vmw_fence_obj *fence =
665 list_entry(fman->fence_list.prev, struct vmw_fence_obj,
666 head);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100667 dma_fence_get(&fence->base);
Thomas Hellstromef369902017-08-24 08:06:28 +0200668 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000669
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100670 ret = vmw_fence_obj_wait(fence, false, false,
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000671 VMW_FENCE_WAIT_TIMEOUT);
672
673 if (unlikely(ret != 0)) {
674 list_del_init(&fence->head);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100675 dma_fence_signal(&fence->base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000676 INIT_LIST_HEAD(&action_list);
677 list_splice_init(&fence->seq_passed_actions,
678 &action_list);
679 vmw_fences_perform_actions(fman, &action_list);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000680 }
681
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000682 BUG_ON(!list_empty(&fence->head));
Chris Wilsonf54d1862016-10-25 13:00:45 +0100683 dma_fence_put(&fence->base);
Thomas Hellstromef369902017-08-24 08:06:28 +0200684 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000685 }
Thomas Hellstromef369902017-08-24 08:06:28 +0200686 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000687}
688
689void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
690{
Thomas Hellstromef369902017-08-24 08:06:28 +0200691 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000692 fman->fifo_down = false;
Thomas Hellstromef369902017-08-24 08:06:28 +0200693 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000694}
695
696
Thomas Hellstromf7652af2017-03-27 11:09:08 +0200697/**
698 * vmw_fence_obj_lookup - Look up a user-space fence object
699 *
700 * @tfile: A struct ttm_object_file identifying the caller.
701 * @handle: A handle identifying the fence object.
702 * @return: A struct vmw_user_fence base ttm object on success or
703 * an error pointer on failure.
704 *
705 * The fence object is looked up and type-checked. The caller needs
706 * to have opened the fence object first, but since that happens on
707 * creation and fence objects aren't shareable, that's not an
708 * issue currently.
709 */
710static struct ttm_base_object *
711vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
712{
713 struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
714
715 if (!base) {
716 pr_err("Invalid fence object handle 0x%08lx.\n",
717 (unsigned long)handle);
718 return ERR_PTR(-EINVAL);
719 }
720
721 if (base->refcount_release != vmw_user_fence_base_release) {
722 pr_err("Invalid fence object handle 0x%08lx.\n",
723 (unsigned long)handle);
724 ttm_base_object_unref(&base);
725 return ERR_PTR(-EINVAL);
726 }
727
728 return base;
729}
730
731
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000732int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
733 struct drm_file *file_priv)
734{
735 struct drm_vmw_fence_wait_arg *arg =
736 (struct drm_vmw_fence_wait_arg *)data;
737 unsigned long timeout;
738 struct ttm_base_object *base;
739 struct vmw_fence_obj *fence;
740 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
741 int ret;
742 uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
743
744 /*
745 * 64-bit division not present on 32-bit systems, so do an
746 * approximation. (Divide by 1000000).
747 */
748
749 wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
750 (wait_timeout >> 26);
751
752 if (!arg->cookie_valid) {
753 arg->cookie_valid = 1;
754 arg->kernel_cookie = jiffies + wait_timeout;
755 }
756
Thomas Hellstromf7652af2017-03-27 11:09:08 +0200757 base = vmw_fence_obj_lookup(tfile, arg->handle);
758 if (IS_ERR(base))
759 return PTR_ERR(base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000760
761 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
762
763 timeout = jiffies;
764 if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100765 ret = ((vmw_fence_obj_signaled(fence)) ?
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000766 0 : -EBUSY);
767 goto out;
768 }
769
770 timeout = (unsigned long)arg->kernel_cookie - timeout;
771
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100772 ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000773
774out:
775 ttm_base_object_unref(&base);
776
777 /*
778 * Optionally unref the fence object.
779 */
780
781 if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
782 return ttm_ref_object_base_unref(tfile, arg->handle,
783 TTM_REF_USAGE);
784 return ret;
785}
786
787int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
788 struct drm_file *file_priv)
789{
790 struct drm_vmw_fence_signaled_arg *arg =
791 (struct drm_vmw_fence_signaled_arg *) data;
792 struct ttm_base_object *base;
793 struct vmw_fence_obj *fence;
794 struct vmw_fence_manager *fman;
795 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
796 struct vmw_private *dev_priv = vmw_priv(dev);
797
Thomas Hellstromf7652af2017-03-27 11:09:08 +0200798 base = vmw_fence_obj_lookup(tfile, arg->handle);
799 if (IS_ERR(base))
800 return PTR_ERR(base);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000801
802 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100803 fman = fman_from_fence(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000804
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100805 arg->signaled = vmw_fence_obj_signaled(fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000806
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +0100807 arg->signaled_flags = arg->flags;
Thomas Hellstromef369902017-08-24 08:06:28 +0200808 spin_lock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000809 arg->passed_seqno = dev_priv->last_read_seqno;
Thomas Hellstromef369902017-08-24 08:06:28 +0200810 spin_unlock(&fman->lock);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000811
812 ttm_base_object_unref(&base);
813
814 return 0;
815}
816
817
818int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
819 struct drm_file *file_priv)
820{
821 struct drm_vmw_fence_arg *arg =
822 (struct drm_vmw_fence_arg *) data;
823
824 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
825 arg->handle,
826 TTM_REF_USAGE);
827}
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200828
Thomas Hellstrom6b82ef52012-02-09 16:56:42 +0100829/**
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200830 * vmw_event_fence_action_seq_passed
831 *
832 * @action: The struct vmw_fence_action embedded in a struct
833 * vmw_event_fence_action.
834 *
835 * This function is called when the seqno of the fence where @action is
836 * attached has passed. It queues the event on the submitter's event list.
Thomas Hellstromef369902017-08-24 08:06:28 +0200837 * This function is always called from atomic context.
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200838 */
839static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
840{
841 struct vmw_event_fence_action *eaction =
842 container_of(action, struct vmw_event_fence_action, action);
843 struct drm_device *dev = eaction->dev;
Thomas Hellstrom6b82ef52012-02-09 16:56:42 +0100844 struct drm_pending_event *event = eaction->event;
845 struct drm_file *file_priv;
Thomas Hellstromef369902017-08-24 08:06:28 +0200846
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200847
Thomas Hellstrom6b82ef52012-02-09 16:56:42 +0100848 if (unlikely(event == NULL))
849 return;
850
851 file_priv = event->file_priv;
Thomas Hellstromef369902017-08-24 08:06:28 +0200852 spin_lock_irq(&dev->event_lock);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200853
854 if (likely(eaction->tv_sec != NULL)) {
855 struct timeval tv;
856
857 do_gettimeofday(&tv);
858 *eaction->tv_sec = tv.tv_sec;
859 *eaction->tv_usec = tv.tv_usec;
860 }
861
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100862 drm_send_event_locked(dev, eaction->event);
Dan Carpenter15b6b802016-01-28 12:06:46 +0300863 eaction->event = NULL;
Thomas Hellstromef369902017-08-24 08:06:28 +0200864 spin_unlock_irq(&dev->event_lock);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200865}
866
867/**
868 * vmw_event_fence_action_cleanup
869 *
870 * @action: The struct vmw_fence_action embedded in a struct
871 * vmw_event_fence_action.
872 *
873 * This function is the struct vmw_fence_action destructor. It's typically
874 * called from a workqueue.
875 */
876static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
877{
878 struct vmw_event_fence_action *eaction =
879 container_of(action, struct vmw_event_fence_action, action);
880
881 vmw_fence_obj_unreference(&eaction->fence);
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100882 kfree(eaction);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200883}
884
885
886/**
887 * vmw_fence_obj_add_action - Add an action to a fence object.
888 *
889 * @fence - The fence object.
890 * @action - The action to add.
891 *
892 * Note that the action callbacks may be executed before this function
893 * returns.
894 */
Rashika Kheria94844cf2014-01-06 22:21:21 +0530895static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200896 struct vmw_fence_action *action)
897{
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100898 struct vmw_fence_manager *fman = fman_from_fence(fence);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200899 bool run_update = false;
900
901 mutex_lock(&fman->goal_irq_mutex);
Thomas Hellstromef369902017-08-24 08:06:28 +0200902 spin_lock(&fman->lock);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200903
904 fman->pending_actions[action->type]++;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100905 if (dma_fence_is_signaled_locked(&fence->base)) {
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200906 struct list_head action_list;
907
908 INIT_LIST_HEAD(&action_list);
909 list_add_tail(&action->head, &action_list);
910 vmw_fences_perform_actions(fman, &action_list);
911 } else {
912 list_add_tail(&action->head, &fence->seq_passed_actions);
913
914 /*
915 * This function may set fman::seqno_valid, so it must
916 * be run with the goal_irq_mutex held.
917 */
918 run_update = vmw_fence_goal_check_locked(fence);
919 }
920
Thomas Hellstromef369902017-08-24 08:06:28 +0200921 spin_unlock(&fman->lock);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200922
923 if (run_update) {
924 if (!fman->goal_irq_on) {
925 fman->goal_irq_on = true;
926 vmw_goal_waiter_add(fman->dev_priv);
927 }
928 vmw_fences_update(fman);
929 }
930 mutex_unlock(&fman->goal_irq_mutex);
931
932}
933
934/**
935 * vmw_event_fence_action_create - Post an event for sending when a fence
936 * object seqno has passed.
937 *
938 * @file_priv: The file connection on which the event should be posted.
939 * @fence: The fence object on which to post the event.
940 * @event: Event to be posted. This event should've been alloced
941 * using k[mz]alloc, and should've been completely initialized.
942 * @interruptible: Interruptible waits if possible.
943 *
944 * As a side effect, the object pointed to by @event may have been
945 * freed when this function returns. If this function returns with
946 * an error code, the caller needs to free that object.
947 */
948
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100949int vmw_event_fence_action_queue(struct drm_file *file_priv,
950 struct vmw_fence_obj *fence,
951 struct drm_pending_event *event,
952 uint32_t *tv_sec,
953 uint32_t *tv_usec,
954 bool interruptible)
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200955{
Dan Carpenter0c5d3702011-10-18 09:09:45 +0300956 struct vmw_event_fence_action *eaction;
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100957 struct vmw_fence_manager *fman = fman_from_fence(fence);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200958
959 eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530960 if (unlikely(!eaction))
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200961 return -ENOMEM;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200962
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100963 eaction->event = event;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200964
965 eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
966 eaction->action.cleanup = vmw_event_fence_action_cleanup;
967 eaction->action.type = VMW_ACTION_EVENT;
968
969 eaction->fence = vmw_fence_obj_reference(fence);
970 eaction->dev = fman->dev_priv->dev;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200971 eaction->tv_sec = tv_sec;
972 eaction->tv_usec = tv_usec;
973
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200974 vmw_fence_obj_add_action(fence, &eaction->action);
975
976 return 0;
977}
978
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100979struct vmw_event_fence_pending {
980 struct drm_pending_event base;
981 struct drm_vmw_event_fence event;
982};
983
Rashika Kheria94844cf2014-01-06 22:21:21 +0530984static int vmw_event_fence_action_create(struct drm_file *file_priv,
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100985 struct vmw_fence_obj *fence,
986 uint32_t flags,
987 uint64_t user_data,
988 bool interruptible)
989{
990 struct vmw_event_fence_pending *event;
Maarten Lankhorst2298e802014-03-26 14:07:44 +0100991 struct vmw_fence_manager *fman = fman_from_fence(fence);
992 struct drm_device *dev = fman->dev_priv->dev;
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100993 int ret;
994
Dan Carpenter68c4fce2012-09-23 19:33:55 +0300995 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530996 if (unlikely(!event)) {
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +0100997 DRM_ERROR("Failed to allocate an event.\n");
998 ret = -ENOMEM;
Daniel Vetter6d3729a2016-01-11 22:40:58 +0100999 goto out_no_space;
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001000 }
1001
1002 event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
1003 event->event.base.length = sizeof(*event);
1004 event->event.user_data = user_data;
1005
Daniel Vetter6d3729a2016-01-11 22:40:58 +01001006 ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001007
Daniel Vetter6d3729a2016-01-11 22:40:58 +01001008 if (unlikely(ret != 0)) {
1009 DRM_ERROR("Failed to allocate event space for this file.\n");
1010 kfree(event);
1011 goto out_no_space;
1012 }
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001013
1014 if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
1015 ret = vmw_event_fence_action_queue(file_priv, fence,
1016 &event->base,
1017 &event->event.tv_sec,
1018 &event->event.tv_usec,
1019 interruptible);
1020 else
1021 ret = vmw_event_fence_action_queue(file_priv, fence,
1022 &event->base,
1023 NULL,
1024 NULL,
1025 interruptible);
1026 if (ret != 0)
1027 goto out_no_queue;
1028
Thomas Hellstrom89669e72014-12-02 03:36:57 -08001029 return 0;
1030
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001031out_no_queue:
Daniel Vetter6d3729a2016-01-11 22:40:58 +01001032 drm_event_cancel_free(dev, &event->base);
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001033out_no_space:
1034 return ret;
1035}
1036
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001037int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
1038 struct drm_file *file_priv)
1039{
1040 struct vmw_private *dev_priv = vmw_priv(dev);
1041 struct drm_vmw_fence_event_arg *arg =
1042 (struct drm_vmw_fence_event_arg *) data;
1043 struct vmw_fence_obj *fence = NULL;
1044 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
Thomas Hellstromf7652af2017-03-27 11:09:08 +02001045 struct ttm_object_file *tfile = vmw_fp->tfile;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001046 struct drm_vmw_fence_rep __user *user_fence_rep =
1047 (struct drm_vmw_fence_rep __user *)(unsigned long)
1048 arg->fence_rep;
1049 uint32_t handle;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001050 int ret;
1051
1052 /*
1053 * Look up an existing fence object,
1054 * and if user-space wants a new reference,
1055 * add one.
1056 */
1057 if (arg->handle) {
1058 struct ttm_base_object *base =
Thomas Hellstromf7652af2017-03-27 11:09:08 +02001059 vmw_fence_obj_lookup(tfile, arg->handle);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001060
Thomas Hellstromf7652af2017-03-27 11:09:08 +02001061 if (IS_ERR(base))
1062 return PTR_ERR(base);
1063
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001064 fence = &(container_of(base, struct vmw_user_fence,
1065 base)->fence);
1066 (void) vmw_fence_obj_reference(fence);
1067
1068 if (user_fence_rep != NULL) {
Thomas Hellstromfe25deb2017-03-27 11:21:25 +02001069 ret = ttm_ref_object_add(vmw_fp->tfile, base,
1070 TTM_REF_USAGE, NULL, false);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001071 if (unlikely(ret != 0)) {
1072 DRM_ERROR("Failed to reference a fence "
1073 "object.\n");
1074 goto out_no_ref_obj;
1075 }
1076 handle = base->hash.key;
1077 }
1078 ttm_base_object_unref(&base);
1079 }
1080
1081 /*
1082 * Create a new fence object.
1083 */
1084 if (!fence) {
1085 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1086 &fence,
1087 (user_fence_rep) ?
1088 &handle : NULL);
1089 if (unlikely(ret != 0)) {
1090 DRM_ERROR("Fence event failed to create fence.\n");
1091 return ret;
1092 }
1093 }
1094
1095 BUG_ON(fence == NULL);
1096
Thomas Hellstrom89669e72014-12-02 03:36:57 -08001097 ret = vmw_event_fence_action_create(file_priv, fence,
1098 arg->flags,
1099 arg->user_data,
1100 true);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001101 if (unlikely(ret != 0)) {
1102 if (ret != -ERESTARTSYS)
1103 DRM_ERROR("Failed to attach event to fence.\n");
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001104 goto out_no_create;
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001105 }
1106
1107 vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
1108 handle);
1109 vmw_fence_obj_unreference(&fence);
1110 return 0;
Jakob Bornecrantz8b7de6a2012-02-09 16:56:41 +01001111out_no_create:
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001112 if (user_fence_rep != NULL)
Thomas Hellstromf7652af2017-03-27 11:09:08 +02001113 ttm_ref_object_base_unref(tfile, handle, TTM_REF_USAGE);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001114out_no_ref_obj:
1115 vmw_fence_obj_unreference(&fence);
1116 return ret;
1117}