Loading...
1/**************************************************************************
2 *
3 * Copyright © 2011-2014 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
28#include <drm/drmP.h>
29#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;
37 struct list_head fence_list;
38 struct work_struct work;
39 u32 user_fence_size;
40 u32 fence_size;
41 u32 event_fence_action_size;
42 bool fifo_down;
43 struct list_head cleanup_list;
44 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. */
49 unsigned ctx;
50};
51
52struct vmw_user_fence {
53 struct ttm_base_object base;
54 struct vmw_fence_obj fence;
55};
56
57/**
58 * struct vmw_event_fence_action - fence action that delivers a drm event.
59 *
60 * @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 {
73 struct vmw_fence_action action;
74
75 struct drm_pending_event *event;
76 struct vmw_fence_obj *fence;
77 struct drm_device *dev;
78
79 uint32_t *tv_sec;
80 uint32_t *tv_usec;
81};
82
83static 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
89/**
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.
109 */
110
111static void vmw_fence_obj_destroy(struct fence *f)
112{
113 struct vmw_fence_obj *fence =
114 container_of(f, struct vmw_fence_obj, base);
115
116 struct vmw_fence_manager *fman = fman_from_fence(fence);
117 unsigned long irq_flags;
118
119 spin_lock_irqsave(&fman->lock, irq_flags);
120 list_del_init(&fence->head);
121 --fman->num_fence_objects;
122 spin_unlock_irqrestore(&fman->lock, irq_flags);
123 fence->destroy(fence);
124}
125
126static const char *vmw_fence_get_driver_name(struct fence *f)
127{
128 return "vmwgfx";
129}
130
131static const char *vmw_fence_get_timeline_name(struct fence *f)
132{
133 return "svga";
134}
135
136static bool vmw_fence_enable_signaling(struct fence *f)
137{
138 struct vmw_fence_obj *fence =
139 container_of(f, struct vmw_fence_obj, base);
140
141 struct vmw_fence_manager *fman = fman_from_fence(fence);
142 struct vmw_private *dev_priv = fman->dev_priv;
143
144 u32 *fifo_mem = dev_priv->mmio_virt;
145 u32 seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
146 if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
147 return false;
148
149 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
150
151 return true;
152}
153
154struct vmwgfx_wait_cb {
155 struct fence_cb base;
156 struct task_struct *task;
157};
158
159static void
160vmwgfx_wait_cb(struct fence *fence, struct fence_cb *cb)
161{
162 struct vmwgfx_wait_cb *wait =
163 container_of(cb, struct vmwgfx_wait_cb, base);
164
165 wake_up_process(wait->task);
166}
167
168static void __vmw_fences_update(struct vmw_fence_manager *fman);
169
170static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
171{
172 struct vmw_fence_obj *fence =
173 container_of(f, struct vmw_fence_obj, base);
174
175 struct vmw_fence_manager *fman = fman_from_fence(fence);
176 struct vmw_private *dev_priv = fman->dev_priv;
177 struct vmwgfx_wait_cb cb;
178 long ret = timeout;
179 unsigned long irq_flags;
180
181 if (likely(vmw_fence_obj_signaled(fence)))
182 return timeout;
183
184 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
185 vmw_seqno_waiter_add(dev_priv);
186
187 spin_lock_irqsave(f->lock, irq_flags);
188
189 if (intr && signal_pending(current)) {
190 ret = -ERESTARTSYS;
191 goto out;
192 }
193
194 cb.base.func = vmwgfx_wait_cb;
195 cb.task = current;
196 list_add(&cb.base.node, &f->cb_list);
197
198 while (ret > 0) {
199 __vmw_fences_update(fman);
200 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &f->flags))
201 break;
202
203 if (intr)
204 __set_current_state(TASK_INTERRUPTIBLE);
205 else
206 __set_current_state(TASK_UNINTERRUPTIBLE);
207 spin_unlock_irqrestore(f->lock, irq_flags);
208
209 ret = schedule_timeout(ret);
210
211 spin_lock_irqsave(f->lock, irq_flags);
212 if (ret > 0 && intr && signal_pending(current))
213 ret = -ERESTARTSYS;
214 }
215
216 if (!list_empty(&cb.base.node))
217 list_del(&cb.base.node);
218 __set_current_state(TASK_RUNNING);
219
220out:
221 spin_unlock_irqrestore(f->lock, irq_flags);
222
223 vmw_seqno_waiter_remove(dev_priv);
224
225 return ret;
226}
227
228static struct fence_ops vmw_fence_ops = {
229 .get_driver_name = vmw_fence_get_driver_name,
230 .get_timeline_name = vmw_fence_get_timeline_name,
231 .enable_signaling = vmw_fence_enable_signaling,
232 .wait = vmw_fence_wait,
233 .release = vmw_fence_obj_destroy,
234};
235
236
237/**
238 * Execute signal actions on fences recently signaled.
239 * This is done from a workqueue so we don't have to execute
240 * signal actions from atomic context.
241 */
242
243static void vmw_fence_work_func(struct work_struct *work)
244{
245 struct vmw_fence_manager *fman =
246 container_of(work, struct vmw_fence_manager, work);
247 struct list_head list;
248 struct vmw_fence_action *action, *next_action;
249 bool seqno_valid;
250
251 do {
252 INIT_LIST_HEAD(&list);
253 mutex_lock(&fman->goal_irq_mutex);
254
255 spin_lock_irq(&fman->lock);
256 list_splice_init(&fman->cleanup_list, &list);
257 seqno_valid = fman->seqno_valid;
258 spin_unlock_irq(&fman->lock);
259
260 if (!seqno_valid && fman->goal_irq_on) {
261 fman->goal_irq_on = false;
262 vmw_goal_waiter_remove(fman->dev_priv);
263 }
264 mutex_unlock(&fman->goal_irq_mutex);
265
266 if (list_empty(&list))
267 return;
268
269 /*
270 * At this point, only we should be able to manipulate the
271 * list heads of the actions we have on the private list.
272 * hence fman::lock not held.
273 */
274
275 list_for_each_entry_safe(action, next_action, &list, head) {
276 list_del_init(&action->head);
277 if (action->cleanup)
278 action->cleanup(action);
279 }
280 } while (1);
281}
282
283struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
284{
285 struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
286
287 if (unlikely(fman == NULL))
288 return NULL;
289
290 fman->dev_priv = dev_priv;
291 spin_lock_init(&fman->lock);
292 INIT_LIST_HEAD(&fman->fence_list);
293 INIT_LIST_HEAD(&fman->cleanup_list);
294 INIT_WORK(&fman->work, &vmw_fence_work_func);
295 fman->fifo_down = true;
296 fman->user_fence_size = ttm_round_pot(sizeof(struct vmw_user_fence));
297 fman->fence_size = ttm_round_pot(sizeof(struct vmw_fence_obj));
298 fman->event_fence_action_size =
299 ttm_round_pot(sizeof(struct vmw_event_fence_action));
300 mutex_init(&fman->goal_irq_mutex);
301 fman->ctx = fence_context_alloc(1);
302
303 return fman;
304}
305
306void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
307{
308 unsigned long irq_flags;
309 bool lists_empty;
310
311 (void) cancel_work_sync(&fman->work);
312
313 spin_lock_irqsave(&fman->lock, irq_flags);
314 lists_empty = list_empty(&fman->fence_list) &&
315 list_empty(&fman->cleanup_list);
316 spin_unlock_irqrestore(&fman->lock, irq_flags);
317
318 BUG_ON(!lists_empty);
319 kfree(fman);
320}
321
322static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
323 struct vmw_fence_obj *fence, u32 seqno,
324 void (*destroy) (struct vmw_fence_obj *fence))
325{
326 unsigned long irq_flags;
327 int ret = 0;
328
329 fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
330 fman->ctx, seqno);
331 INIT_LIST_HEAD(&fence->seq_passed_actions);
332 fence->destroy = destroy;
333
334 spin_lock_irqsave(&fman->lock, irq_flags);
335 if (unlikely(fman->fifo_down)) {
336 ret = -EBUSY;
337 goto out_unlock;
338 }
339 list_add_tail(&fence->head, &fman->fence_list);
340 ++fman->num_fence_objects;
341
342out_unlock:
343 spin_unlock_irqrestore(&fman->lock, irq_flags);
344 return ret;
345
346}
347
348static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
349 struct list_head *list)
350{
351 struct vmw_fence_action *action, *next_action;
352
353 list_for_each_entry_safe(action, next_action, list, head) {
354 list_del_init(&action->head);
355 fman->pending_actions[action->type]--;
356 if (action->seq_passed != NULL)
357 action->seq_passed(action);
358
359 /*
360 * Add the cleanup action to the cleanup list so that
361 * it will be performed by a worker task.
362 */
363
364 list_add_tail(&action->head, &fman->cleanup_list);
365 }
366}
367
368/**
369 * vmw_fence_goal_new_locked - Figure out a new device fence goal
370 * seqno if needed.
371 *
372 * @fman: Pointer to a fence manager.
373 * @passed_seqno: The seqno the device currently signals as passed.
374 *
375 * This function should be called with the fence manager lock held.
376 * It is typically called when we have a new passed_seqno, and
377 * we might need to update the fence goal. It checks to see whether
378 * the current fence goal has already passed, and, in that case,
379 * scans through all unsignaled fences to get the next fence object with an
380 * action attached, and sets the seqno of that fence as a new fence goal.
381 *
382 * returns true if the device goal seqno was updated. False otherwise.
383 */
384static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
385 u32 passed_seqno)
386{
387 u32 goal_seqno;
388 u32 *fifo_mem;
389 struct vmw_fence_obj *fence;
390
391 if (likely(!fman->seqno_valid))
392 return false;
393
394 fifo_mem = fman->dev_priv->mmio_virt;
395 goal_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE_GOAL);
396 if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
397 return false;
398
399 fman->seqno_valid = false;
400 list_for_each_entry(fence, &fman->fence_list, head) {
401 if (!list_empty(&fence->seq_passed_actions)) {
402 fman->seqno_valid = true;
403 vmw_mmio_write(fence->base.seqno,
404 fifo_mem + SVGA_FIFO_FENCE_GOAL);
405 break;
406 }
407 }
408
409 return true;
410}
411
412
413/**
414 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
415 * needed.
416 *
417 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
418 * considered as a device fence goal.
419 *
420 * This function should be called with the fence manager lock held.
421 * It is typically called when an action has been attached to a fence to
422 * check whether the seqno of that fence should be used for a fence
423 * goal interrupt. This is typically needed if the current fence goal is
424 * invalid, or has a higher seqno than that of the current fence object.
425 *
426 * returns true if the device goal seqno was updated. False otherwise.
427 */
428static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
429{
430 struct vmw_fence_manager *fman = fman_from_fence(fence);
431 u32 goal_seqno;
432 u32 *fifo_mem;
433
434 if (fence_is_signaled_locked(&fence->base))
435 return false;
436
437 fifo_mem = fman->dev_priv->mmio_virt;
438 goal_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE_GOAL);
439 if (likely(fman->seqno_valid &&
440 goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
441 return false;
442
443 vmw_mmio_write(fence->base.seqno, fifo_mem + SVGA_FIFO_FENCE_GOAL);
444 fman->seqno_valid = true;
445
446 return true;
447}
448
449static void __vmw_fences_update(struct vmw_fence_manager *fman)
450{
451 struct vmw_fence_obj *fence, *next_fence;
452 struct list_head action_list;
453 bool needs_rerun;
454 uint32_t seqno, new_seqno;
455 u32 *fifo_mem = fman->dev_priv->mmio_virt;
456
457 seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
458rerun:
459 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
460 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
461 list_del_init(&fence->head);
462 fence_signal_locked(&fence->base);
463 INIT_LIST_HEAD(&action_list);
464 list_splice_init(&fence->seq_passed_actions,
465 &action_list);
466 vmw_fences_perform_actions(fman, &action_list);
467 } else
468 break;
469 }
470
471 /*
472 * Rerun if the fence goal seqno was updated, and the
473 * hardware might have raced with that update, so that
474 * we missed a fence_goal irq.
475 */
476
477 needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
478 if (unlikely(needs_rerun)) {
479 new_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
480 if (new_seqno != seqno) {
481 seqno = new_seqno;
482 goto rerun;
483 }
484 }
485
486 if (!list_empty(&fman->cleanup_list))
487 (void) schedule_work(&fman->work);
488}
489
490void vmw_fences_update(struct vmw_fence_manager *fman)
491{
492 unsigned long irq_flags;
493
494 spin_lock_irqsave(&fman->lock, irq_flags);
495 __vmw_fences_update(fman);
496 spin_unlock_irqrestore(&fman->lock, irq_flags);
497}
498
499bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
500{
501 struct vmw_fence_manager *fman = fman_from_fence(fence);
502
503 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
504 return 1;
505
506 vmw_fences_update(fman);
507
508 return fence_is_signaled(&fence->base);
509}
510
511int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
512 bool interruptible, unsigned long timeout)
513{
514 long ret = fence_wait_timeout(&fence->base, interruptible, timeout);
515
516 if (likely(ret > 0))
517 return 0;
518 else if (ret == 0)
519 return -EBUSY;
520 else
521 return ret;
522}
523
524void vmw_fence_obj_flush(struct vmw_fence_obj *fence)
525{
526 struct vmw_private *dev_priv = fman_from_fence(fence)->dev_priv;
527
528 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
529}
530
531static void vmw_fence_destroy(struct vmw_fence_obj *fence)
532{
533 fence_free(&fence->base);
534}
535
536int vmw_fence_create(struct vmw_fence_manager *fman,
537 uint32_t seqno,
538 struct vmw_fence_obj **p_fence)
539{
540 struct vmw_fence_obj *fence;
541 int ret;
542
543 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
544 if (unlikely(fence == NULL))
545 return -ENOMEM;
546
547 ret = vmw_fence_obj_init(fman, fence, seqno,
548 vmw_fence_destroy);
549 if (unlikely(ret != 0))
550 goto out_err_init;
551
552 *p_fence = fence;
553 return 0;
554
555out_err_init:
556 kfree(fence);
557 return ret;
558}
559
560
561static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
562{
563 struct vmw_user_fence *ufence =
564 container_of(fence, struct vmw_user_fence, fence);
565 struct vmw_fence_manager *fman = fman_from_fence(fence);
566
567 ttm_base_object_kfree(ufence, base);
568 /*
569 * Free kernel space accounting.
570 */
571 ttm_mem_global_free(vmw_mem_glob(fman->dev_priv),
572 fman->user_fence_size);
573}
574
575static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
576{
577 struct ttm_base_object *base = *p_base;
578 struct vmw_user_fence *ufence =
579 container_of(base, struct vmw_user_fence, base);
580 struct vmw_fence_obj *fence = &ufence->fence;
581
582 *p_base = NULL;
583 vmw_fence_obj_unreference(&fence);
584}
585
586int vmw_user_fence_create(struct drm_file *file_priv,
587 struct vmw_fence_manager *fman,
588 uint32_t seqno,
589 struct vmw_fence_obj **p_fence,
590 uint32_t *p_handle)
591{
592 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
593 struct vmw_user_fence *ufence;
594 struct vmw_fence_obj *tmp;
595 struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
596 int ret;
597
598 /*
599 * Kernel memory space accounting, since this object may
600 * be created by a user-space request.
601 */
602
603 ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
604 false, false);
605 if (unlikely(ret != 0))
606 return ret;
607
608 ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
609 if (unlikely(ufence == NULL)) {
610 ret = -ENOMEM;
611 goto out_no_object;
612 }
613
614 ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
615 vmw_user_fence_destroy);
616 if (unlikely(ret != 0)) {
617 kfree(ufence);
618 goto out_no_object;
619 }
620
621 /*
622 * The base object holds a reference which is freed in
623 * vmw_user_fence_base_release.
624 */
625 tmp = vmw_fence_obj_reference(&ufence->fence);
626 ret = ttm_base_object_init(tfile, &ufence->base, false,
627 VMW_RES_FENCE,
628 &vmw_user_fence_base_release, NULL);
629
630
631 if (unlikely(ret != 0)) {
632 /*
633 * Free the base object's reference
634 */
635 vmw_fence_obj_unreference(&tmp);
636 goto out_err;
637 }
638
639 *p_fence = &ufence->fence;
640 *p_handle = ufence->base.hash.key;
641
642 return 0;
643out_err:
644 tmp = &ufence->fence;
645 vmw_fence_obj_unreference(&tmp);
646out_no_object:
647 ttm_mem_global_free(mem_glob, fman->user_fence_size);
648 return ret;
649}
650
651
652/**
653 * vmw_fence_fifo_down - signal all unsignaled fence objects.
654 */
655
656void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
657{
658 struct list_head action_list;
659 int ret;
660
661 /*
662 * The list may be altered while we traverse it, so always
663 * restart when we've released the fman->lock.
664 */
665
666 spin_lock_irq(&fman->lock);
667 fman->fifo_down = true;
668 while (!list_empty(&fman->fence_list)) {
669 struct vmw_fence_obj *fence =
670 list_entry(fman->fence_list.prev, struct vmw_fence_obj,
671 head);
672 fence_get(&fence->base);
673 spin_unlock_irq(&fman->lock);
674
675 ret = vmw_fence_obj_wait(fence, false, false,
676 VMW_FENCE_WAIT_TIMEOUT);
677
678 if (unlikely(ret != 0)) {
679 list_del_init(&fence->head);
680 fence_signal(&fence->base);
681 INIT_LIST_HEAD(&action_list);
682 list_splice_init(&fence->seq_passed_actions,
683 &action_list);
684 vmw_fences_perform_actions(fman, &action_list);
685 }
686
687 BUG_ON(!list_empty(&fence->head));
688 fence_put(&fence->base);
689 spin_lock_irq(&fman->lock);
690 }
691 spin_unlock_irq(&fman->lock);
692}
693
694void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
695{
696 unsigned long irq_flags;
697
698 spin_lock_irqsave(&fman->lock, irq_flags);
699 fman->fifo_down = false;
700 spin_unlock_irqrestore(&fman->lock, irq_flags);
701}
702
703
704int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
705 struct drm_file *file_priv)
706{
707 struct drm_vmw_fence_wait_arg *arg =
708 (struct drm_vmw_fence_wait_arg *)data;
709 unsigned long timeout;
710 struct ttm_base_object *base;
711 struct vmw_fence_obj *fence;
712 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
713 int ret;
714 uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
715
716 /*
717 * 64-bit division not present on 32-bit systems, so do an
718 * approximation. (Divide by 1000000).
719 */
720
721 wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
722 (wait_timeout >> 26);
723
724 if (!arg->cookie_valid) {
725 arg->cookie_valid = 1;
726 arg->kernel_cookie = jiffies + wait_timeout;
727 }
728
729 base = ttm_base_object_lookup(tfile, arg->handle);
730 if (unlikely(base == NULL)) {
731 printk(KERN_ERR "Wait invalid fence object handle "
732 "0x%08lx.\n",
733 (unsigned long)arg->handle);
734 return -EINVAL;
735 }
736
737 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
738
739 timeout = jiffies;
740 if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
741 ret = ((vmw_fence_obj_signaled(fence)) ?
742 0 : -EBUSY);
743 goto out;
744 }
745
746 timeout = (unsigned long)arg->kernel_cookie - timeout;
747
748 ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
749
750out:
751 ttm_base_object_unref(&base);
752
753 /*
754 * Optionally unref the fence object.
755 */
756
757 if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
758 return ttm_ref_object_base_unref(tfile, arg->handle,
759 TTM_REF_USAGE);
760 return ret;
761}
762
763int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
764 struct drm_file *file_priv)
765{
766 struct drm_vmw_fence_signaled_arg *arg =
767 (struct drm_vmw_fence_signaled_arg *) data;
768 struct ttm_base_object *base;
769 struct vmw_fence_obj *fence;
770 struct vmw_fence_manager *fman;
771 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
772 struct vmw_private *dev_priv = vmw_priv(dev);
773
774 base = ttm_base_object_lookup(tfile, arg->handle);
775 if (unlikely(base == NULL)) {
776 printk(KERN_ERR "Fence signaled invalid fence object handle "
777 "0x%08lx.\n",
778 (unsigned long)arg->handle);
779 return -EINVAL;
780 }
781
782 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
783 fman = fman_from_fence(fence);
784
785 arg->signaled = vmw_fence_obj_signaled(fence);
786
787 arg->signaled_flags = arg->flags;
788 spin_lock_irq(&fman->lock);
789 arg->passed_seqno = dev_priv->last_read_seqno;
790 spin_unlock_irq(&fman->lock);
791
792 ttm_base_object_unref(&base);
793
794 return 0;
795}
796
797
798int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
799 struct drm_file *file_priv)
800{
801 struct drm_vmw_fence_arg *arg =
802 (struct drm_vmw_fence_arg *) data;
803
804 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
805 arg->handle,
806 TTM_REF_USAGE);
807}
808
809/**
810 * vmw_event_fence_action_seq_passed
811 *
812 * @action: The struct vmw_fence_action embedded in a struct
813 * vmw_event_fence_action.
814 *
815 * This function is called when the seqno of the fence where @action is
816 * attached has passed. It queues the event on the submitter's event list.
817 * This function is always called from atomic context, and may be called
818 * from irq context.
819 */
820static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
821{
822 struct vmw_event_fence_action *eaction =
823 container_of(action, struct vmw_event_fence_action, action);
824 struct drm_device *dev = eaction->dev;
825 struct drm_pending_event *event = eaction->event;
826 struct drm_file *file_priv;
827 unsigned long irq_flags;
828
829 if (unlikely(event == NULL))
830 return;
831
832 file_priv = event->file_priv;
833 spin_lock_irqsave(&dev->event_lock, irq_flags);
834
835 if (likely(eaction->tv_sec != NULL)) {
836 struct timeval tv;
837
838 do_gettimeofday(&tv);
839 *eaction->tv_sec = tv.tv_sec;
840 *eaction->tv_usec = tv.tv_usec;
841 }
842
843 drm_send_event_locked(dev, eaction->event);
844 eaction->event = NULL;
845 spin_unlock_irqrestore(&dev->event_lock, irq_flags);
846}
847
848/**
849 * vmw_event_fence_action_cleanup
850 *
851 * @action: The struct vmw_fence_action embedded in a struct
852 * vmw_event_fence_action.
853 *
854 * This function is the struct vmw_fence_action destructor. It's typically
855 * called from a workqueue.
856 */
857static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
858{
859 struct vmw_event_fence_action *eaction =
860 container_of(action, struct vmw_event_fence_action, action);
861
862 vmw_fence_obj_unreference(&eaction->fence);
863 kfree(eaction);
864}
865
866
867/**
868 * vmw_fence_obj_add_action - Add an action to a fence object.
869 *
870 * @fence - The fence object.
871 * @action - The action to add.
872 *
873 * Note that the action callbacks may be executed before this function
874 * returns.
875 */
876static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
877 struct vmw_fence_action *action)
878{
879 struct vmw_fence_manager *fman = fman_from_fence(fence);
880 unsigned long irq_flags;
881 bool run_update = false;
882
883 mutex_lock(&fman->goal_irq_mutex);
884 spin_lock_irqsave(&fman->lock, irq_flags);
885
886 fman->pending_actions[action->type]++;
887 if (fence_is_signaled_locked(&fence->base)) {
888 struct list_head action_list;
889
890 INIT_LIST_HEAD(&action_list);
891 list_add_tail(&action->head, &action_list);
892 vmw_fences_perform_actions(fman, &action_list);
893 } else {
894 list_add_tail(&action->head, &fence->seq_passed_actions);
895
896 /*
897 * This function may set fman::seqno_valid, so it must
898 * be run with the goal_irq_mutex held.
899 */
900 run_update = vmw_fence_goal_check_locked(fence);
901 }
902
903 spin_unlock_irqrestore(&fman->lock, irq_flags);
904
905 if (run_update) {
906 if (!fman->goal_irq_on) {
907 fman->goal_irq_on = true;
908 vmw_goal_waiter_add(fman->dev_priv);
909 }
910 vmw_fences_update(fman);
911 }
912 mutex_unlock(&fman->goal_irq_mutex);
913
914}
915
916/**
917 * vmw_event_fence_action_create - Post an event for sending when a fence
918 * object seqno has passed.
919 *
920 * @file_priv: The file connection on which the event should be posted.
921 * @fence: The fence object on which to post the event.
922 * @event: Event to be posted. This event should've been alloced
923 * using k[mz]alloc, and should've been completely initialized.
924 * @interruptible: Interruptible waits if possible.
925 *
926 * As a side effect, the object pointed to by @event may have been
927 * freed when this function returns. If this function returns with
928 * an error code, the caller needs to free that object.
929 */
930
931int vmw_event_fence_action_queue(struct drm_file *file_priv,
932 struct vmw_fence_obj *fence,
933 struct drm_pending_event *event,
934 uint32_t *tv_sec,
935 uint32_t *tv_usec,
936 bool interruptible)
937{
938 struct vmw_event_fence_action *eaction;
939 struct vmw_fence_manager *fman = fman_from_fence(fence);
940
941 eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
942 if (unlikely(eaction == NULL))
943 return -ENOMEM;
944
945 eaction->event = event;
946
947 eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
948 eaction->action.cleanup = vmw_event_fence_action_cleanup;
949 eaction->action.type = VMW_ACTION_EVENT;
950
951 eaction->fence = vmw_fence_obj_reference(fence);
952 eaction->dev = fman->dev_priv->dev;
953 eaction->tv_sec = tv_sec;
954 eaction->tv_usec = tv_usec;
955
956 vmw_fence_obj_add_action(fence, &eaction->action);
957
958 return 0;
959}
960
961struct vmw_event_fence_pending {
962 struct drm_pending_event base;
963 struct drm_vmw_event_fence event;
964};
965
966static int vmw_event_fence_action_create(struct drm_file *file_priv,
967 struct vmw_fence_obj *fence,
968 uint32_t flags,
969 uint64_t user_data,
970 bool interruptible)
971{
972 struct vmw_event_fence_pending *event;
973 struct vmw_fence_manager *fman = fman_from_fence(fence);
974 struct drm_device *dev = fman->dev_priv->dev;
975 int ret;
976
977 event = kzalloc(sizeof(*event), GFP_KERNEL);
978 if (unlikely(event == NULL)) {
979 DRM_ERROR("Failed to allocate an event.\n");
980 ret = -ENOMEM;
981 goto out_no_space;
982 }
983
984 event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
985 event->event.base.length = sizeof(*event);
986 event->event.user_data = user_data;
987
988 ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
989
990 if (unlikely(ret != 0)) {
991 DRM_ERROR("Failed to allocate event space for this file.\n");
992 kfree(event);
993 goto out_no_space;
994 }
995
996 if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
997 ret = vmw_event_fence_action_queue(file_priv, fence,
998 &event->base,
999 &event->event.tv_sec,
1000 &event->event.tv_usec,
1001 interruptible);
1002 else
1003 ret = vmw_event_fence_action_queue(file_priv, fence,
1004 &event->base,
1005 NULL,
1006 NULL,
1007 interruptible);
1008 if (ret != 0)
1009 goto out_no_queue;
1010
1011 return 0;
1012
1013out_no_queue:
1014 drm_event_cancel_free(dev, &event->base);
1015out_no_space:
1016 return ret;
1017}
1018
1019int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
1020 struct drm_file *file_priv)
1021{
1022 struct vmw_private *dev_priv = vmw_priv(dev);
1023 struct drm_vmw_fence_event_arg *arg =
1024 (struct drm_vmw_fence_event_arg *) data;
1025 struct vmw_fence_obj *fence = NULL;
1026 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1027 struct drm_vmw_fence_rep __user *user_fence_rep =
1028 (struct drm_vmw_fence_rep __user *)(unsigned long)
1029 arg->fence_rep;
1030 uint32_t handle;
1031 int ret;
1032
1033 /*
1034 * Look up an existing fence object,
1035 * and if user-space wants a new reference,
1036 * add one.
1037 */
1038 if (arg->handle) {
1039 struct ttm_base_object *base =
1040 ttm_base_object_lookup_for_ref(dev_priv->tdev,
1041 arg->handle);
1042
1043 if (unlikely(base == NULL)) {
1044 DRM_ERROR("Fence event invalid fence object handle "
1045 "0x%08lx.\n",
1046 (unsigned long)arg->handle);
1047 return -EINVAL;
1048 }
1049 fence = &(container_of(base, struct vmw_user_fence,
1050 base)->fence);
1051 (void) vmw_fence_obj_reference(fence);
1052
1053 if (user_fence_rep != NULL) {
1054 bool existed;
1055
1056 ret = ttm_ref_object_add(vmw_fp->tfile, base,
1057 TTM_REF_USAGE, &existed);
1058 if (unlikely(ret != 0)) {
1059 DRM_ERROR("Failed to reference a fence "
1060 "object.\n");
1061 goto out_no_ref_obj;
1062 }
1063 handle = base->hash.key;
1064 }
1065 ttm_base_object_unref(&base);
1066 }
1067
1068 /*
1069 * Create a new fence object.
1070 */
1071 if (!fence) {
1072 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1073 &fence,
1074 (user_fence_rep) ?
1075 &handle : NULL);
1076 if (unlikely(ret != 0)) {
1077 DRM_ERROR("Fence event failed to create fence.\n");
1078 return ret;
1079 }
1080 }
1081
1082 BUG_ON(fence == NULL);
1083
1084 ret = vmw_event_fence_action_create(file_priv, fence,
1085 arg->flags,
1086 arg->user_data,
1087 true);
1088 if (unlikely(ret != 0)) {
1089 if (ret != -ERESTARTSYS)
1090 DRM_ERROR("Failed to attach event to fence.\n");
1091 goto out_no_create;
1092 }
1093
1094 vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
1095 handle);
1096 vmw_fence_obj_unreference(&fence);
1097 return 0;
1098out_no_create:
1099 if (user_fence_rep != NULL)
1100 ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
1101 handle, TTM_REF_USAGE);
1102out_no_ref_obj:
1103 vmw_fence_obj_unreference(&fence);
1104 return ret;
1105}
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2/**************************************************************************
3 *
4 * Copyright 2011-2014 VMware, Inc., Palo Alto, CA., USA
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
28#include <linux/sched/signal.h>
29
30#include "vmwgfx_drv.h"
31
32#define VMW_FENCE_WRAP (1 << 31)
33
34struct vmw_fence_manager {
35 int num_fence_objects;
36 struct vmw_private *dev_priv;
37 spinlock_t lock;
38 struct list_head fence_list;
39 struct work_struct work;
40 bool fifo_down;
41 struct list_head cleanup_list;
42 uint32_t pending_actions[VMW_ACTION_MAX];
43 struct mutex goal_irq_mutex;
44 bool goal_irq_on; /* Protected by @goal_irq_mutex */
45 bool seqno_valid; /* Protected by @lock, and may not be set to true
46 without the @goal_irq_mutex held. */
47 u64 ctx;
48};
49
50struct vmw_user_fence {
51 struct ttm_base_object base;
52 struct vmw_fence_obj fence;
53};
54
55/**
56 * struct vmw_event_fence_action - fence action that delivers a drm event.
57 *
58 * @action: A struct vmw_fence_action to hook up to a fence.
59 * @event: A pointer to the pending event.
60 * @fence: A referenced pointer to the fence to keep it alive while @action
61 * hangs on it.
62 * @dev: Pointer to a struct drm_device so we can access the event stuff.
63 * @tv_sec: If non-null, the variable pointed to will be assigned
64 * current time tv_sec val when the fence signals.
65 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
66 * be assigned the current time tv_usec val when the fence signals.
67 */
68struct vmw_event_fence_action {
69 struct vmw_fence_action action;
70
71 struct drm_pending_event *event;
72 struct vmw_fence_obj *fence;
73 struct drm_device *dev;
74
75 uint32_t *tv_sec;
76 uint32_t *tv_usec;
77};
78
79static struct vmw_fence_manager *
80fman_from_fence(struct vmw_fence_obj *fence)
81{
82 return container_of(fence->base.lock, struct vmw_fence_manager, lock);
83}
84
85static u32 vmw_fence_goal_read(struct vmw_private *vmw)
86{
87 if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
88 return vmw_read(vmw, SVGA_REG_FENCE_GOAL);
89 else
90 return vmw_fifo_mem_read(vmw, SVGA_FIFO_FENCE_GOAL);
91}
92
93static void vmw_fence_goal_write(struct vmw_private *vmw, u32 value)
94{
95 if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
96 vmw_write(vmw, SVGA_REG_FENCE_GOAL, value);
97 else
98 vmw_fifo_mem_write(vmw, SVGA_FIFO_FENCE_GOAL, value);
99}
100
101/*
102 * Note on fencing subsystem usage of irqs:
103 * Typically the vmw_fences_update function is called
104 *
105 * a) When a new fence seqno has been submitted by the fifo code.
106 * b) On-demand when we have waiters. Sleeping waiters will switch on the
107 * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE
108 * irq is received. When the last fence waiter is gone, that IRQ is masked
109 * away.
110 *
111 * In situations where there are no waiters and we don't submit any new fences,
112 * fence objects may not be signaled. This is perfectly OK, since there are
113 * no consumers of the signaled data, but that is NOT ok when there are fence
114 * actions attached to a fence. The fencing subsystem then makes use of the
115 * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence
116 * which has an action attached, and each time vmw_fences_update is called,
117 * the subsystem makes sure the fence goal seqno is updated.
118 *
119 * The fence goal seqno irq is on as long as there are unsignaled fence
120 * objects with actions attached to them.
121 */
122
123static void vmw_fence_obj_destroy(struct dma_fence *f)
124{
125 struct vmw_fence_obj *fence =
126 container_of(f, struct vmw_fence_obj, base);
127
128 struct vmw_fence_manager *fman = fman_from_fence(fence);
129
130 spin_lock(&fman->lock);
131 list_del_init(&fence->head);
132 --fman->num_fence_objects;
133 spin_unlock(&fman->lock);
134 fence->destroy(fence);
135}
136
137static const char *vmw_fence_get_driver_name(struct dma_fence *f)
138{
139 return "vmwgfx";
140}
141
142static const char *vmw_fence_get_timeline_name(struct dma_fence *f)
143{
144 return "svga";
145}
146
147static bool vmw_fence_enable_signaling(struct dma_fence *f)
148{
149 struct vmw_fence_obj *fence =
150 container_of(f, struct vmw_fence_obj, base);
151
152 struct vmw_fence_manager *fman = fman_from_fence(fence);
153 struct vmw_private *dev_priv = fman->dev_priv;
154
155 u32 seqno = vmw_fence_read(dev_priv);
156 if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
157 return false;
158
159 return true;
160}
161
162struct vmwgfx_wait_cb {
163 struct dma_fence_cb base;
164 struct task_struct *task;
165};
166
167static void
168vmwgfx_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
169{
170 struct vmwgfx_wait_cb *wait =
171 container_of(cb, struct vmwgfx_wait_cb, base);
172
173 wake_up_process(wait->task);
174}
175
176static void __vmw_fences_update(struct vmw_fence_manager *fman);
177
178static long vmw_fence_wait(struct dma_fence *f, bool intr, signed long timeout)
179{
180 struct vmw_fence_obj *fence =
181 container_of(f, struct vmw_fence_obj, base);
182
183 struct vmw_fence_manager *fman = fman_from_fence(fence);
184 struct vmw_private *dev_priv = fman->dev_priv;
185 struct vmwgfx_wait_cb cb;
186 long ret = timeout;
187
188 if (likely(vmw_fence_obj_signaled(fence)))
189 return timeout;
190
191 vmw_seqno_waiter_add(dev_priv);
192
193 spin_lock(f->lock);
194
195 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags))
196 goto out;
197
198 if (intr && signal_pending(current)) {
199 ret = -ERESTARTSYS;
200 goto out;
201 }
202
203 cb.base.func = vmwgfx_wait_cb;
204 cb.task = current;
205 list_add(&cb.base.node, &f->cb_list);
206
207 for (;;) {
208 __vmw_fences_update(fman);
209
210 /*
211 * We can use the barrier free __set_current_state() since
212 * DMA_FENCE_FLAG_SIGNALED_BIT + wakeup is protected by the
213 * fence spinlock.
214 */
215 if (intr)
216 __set_current_state(TASK_INTERRUPTIBLE);
217 else
218 __set_current_state(TASK_UNINTERRUPTIBLE);
219
220 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags)) {
221 if (ret == 0 && timeout > 0)
222 ret = 1;
223 break;
224 }
225
226 if (intr && signal_pending(current)) {
227 ret = -ERESTARTSYS;
228 break;
229 }
230
231 if (ret == 0)
232 break;
233
234 spin_unlock(f->lock);
235
236 ret = schedule_timeout(ret);
237
238 spin_lock(f->lock);
239 }
240 __set_current_state(TASK_RUNNING);
241 if (!list_empty(&cb.base.node))
242 list_del(&cb.base.node);
243
244out:
245 spin_unlock(f->lock);
246
247 vmw_seqno_waiter_remove(dev_priv);
248
249 return ret;
250}
251
252static const struct dma_fence_ops vmw_fence_ops = {
253 .get_driver_name = vmw_fence_get_driver_name,
254 .get_timeline_name = vmw_fence_get_timeline_name,
255 .enable_signaling = vmw_fence_enable_signaling,
256 .wait = vmw_fence_wait,
257 .release = vmw_fence_obj_destroy,
258};
259
260
261/*
262 * Execute signal actions on fences recently signaled.
263 * This is done from a workqueue so we don't have to execute
264 * signal actions from atomic context.
265 */
266
267static void vmw_fence_work_func(struct work_struct *work)
268{
269 struct vmw_fence_manager *fman =
270 container_of(work, struct vmw_fence_manager, work);
271 struct list_head list;
272 struct vmw_fence_action *action, *next_action;
273 bool seqno_valid;
274
275 do {
276 INIT_LIST_HEAD(&list);
277 mutex_lock(&fman->goal_irq_mutex);
278
279 spin_lock(&fman->lock);
280 list_splice_init(&fman->cleanup_list, &list);
281 seqno_valid = fman->seqno_valid;
282 spin_unlock(&fman->lock);
283
284 if (!seqno_valid && fman->goal_irq_on) {
285 fman->goal_irq_on = false;
286 vmw_goal_waiter_remove(fman->dev_priv);
287 }
288 mutex_unlock(&fman->goal_irq_mutex);
289
290 if (list_empty(&list))
291 return;
292
293 /*
294 * At this point, only we should be able to manipulate the
295 * list heads of the actions we have on the private list.
296 * hence fman::lock not held.
297 */
298
299 list_for_each_entry_safe(action, next_action, &list, head) {
300 list_del_init(&action->head);
301 if (action->cleanup)
302 action->cleanup(action);
303 }
304 } while (1);
305}
306
307struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
308{
309 struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
310
311 if (unlikely(!fman))
312 return NULL;
313
314 fman->dev_priv = dev_priv;
315 spin_lock_init(&fman->lock);
316 INIT_LIST_HEAD(&fman->fence_list);
317 INIT_LIST_HEAD(&fman->cleanup_list);
318 INIT_WORK(&fman->work, &vmw_fence_work_func);
319 fman->fifo_down = true;
320 mutex_init(&fman->goal_irq_mutex);
321 fman->ctx = dma_fence_context_alloc(1);
322
323 return fman;
324}
325
326void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
327{
328 bool lists_empty;
329
330 (void) cancel_work_sync(&fman->work);
331
332 spin_lock(&fman->lock);
333 lists_empty = list_empty(&fman->fence_list) &&
334 list_empty(&fman->cleanup_list);
335 spin_unlock(&fman->lock);
336
337 BUG_ON(!lists_empty);
338 kfree(fman);
339}
340
341static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
342 struct vmw_fence_obj *fence, u32 seqno,
343 void (*destroy) (struct vmw_fence_obj *fence))
344{
345 int ret = 0;
346
347 dma_fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
348 fman->ctx, seqno);
349 INIT_LIST_HEAD(&fence->seq_passed_actions);
350 fence->destroy = destroy;
351
352 spin_lock(&fman->lock);
353 if (unlikely(fman->fifo_down)) {
354 ret = -EBUSY;
355 goto out_unlock;
356 }
357 list_add_tail(&fence->head, &fman->fence_list);
358 ++fman->num_fence_objects;
359
360out_unlock:
361 spin_unlock(&fman->lock);
362 return ret;
363
364}
365
366static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
367 struct list_head *list)
368{
369 struct vmw_fence_action *action, *next_action;
370
371 list_for_each_entry_safe(action, next_action, list, head) {
372 list_del_init(&action->head);
373 fman->pending_actions[action->type]--;
374 if (action->seq_passed != NULL)
375 action->seq_passed(action);
376
377 /*
378 * Add the cleanup action to the cleanup list so that
379 * it will be performed by a worker task.
380 */
381
382 list_add_tail(&action->head, &fman->cleanup_list);
383 }
384}
385
386/**
387 * vmw_fence_goal_new_locked - Figure out a new device fence goal
388 * seqno if needed.
389 *
390 * @fman: Pointer to a fence manager.
391 * @passed_seqno: The seqno the device currently signals as passed.
392 *
393 * This function should be called with the fence manager lock held.
394 * It is typically called when we have a new passed_seqno, and
395 * we might need to update the fence goal. It checks to see whether
396 * the current fence goal has already passed, and, in that case,
397 * scans through all unsignaled fences to get the next fence object with an
398 * action attached, and sets the seqno of that fence as a new fence goal.
399 *
400 * returns true if the device goal seqno was updated. False otherwise.
401 */
402static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
403 u32 passed_seqno)
404{
405 u32 goal_seqno;
406 struct vmw_fence_obj *fence;
407
408 if (likely(!fman->seqno_valid))
409 return false;
410
411 goal_seqno = vmw_fence_goal_read(fman->dev_priv);
412 if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
413 return false;
414
415 fman->seqno_valid = false;
416 list_for_each_entry(fence, &fman->fence_list, head) {
417 if (!list_empty(&fence->seq_passed_actions)) {
418 fman->seqno_valid = true;
419 vmw_fence_goal_write(fman->dev_priv,
420 fence->base.seqno);
421 break;
422 }
423 }
424
425 return true;
426}
427
428
429/**
430 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
431 * needed.
432 *
433 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
434 * considered as a device fence goal.
435 *
436 * This function should be called with the fence manager lock held.
437 * It is typically called when an action has been attached to a fence to
438 * check whether the seqno of that fence should be used for a fence
439 * goal interrupt. This is typically needed if the current fence goal is
440 * invalid, or has a higher seqno than that of the current fence object.
441 *
442 * returns true if the device goal seqno was updated. False otherwise.
443 */
444static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
445{
446 struct vmw_fence_manager *fman = fman_from_fence(fence);
447 u32 goal_seqno;
448
449 if (dma_fence_is_signaled_locked(&fence->base))
450 return false;
451
452 goal_seqno = vmw_fence_goal_read(fman->dev_priv);
453 if (likely(fman->seqno_valid &&
454 goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
455 return false;
456
457 vmw_fence_goal_write(fman->dev_priv, fence->base.seqno);
458 fman->seqno_valid = true;
459
460 return true;
461}
462
463static void __vmw_fences_update(struct vmw_fence_manager *fman)
464{
465 struct vmw_fence_obj *fence, *next_fence;
466 struct list_head action_list;
467 bool needs_rerun;
468 uint32_t seqno, new_seqno;
469
470 seqno = vmw_fence_read(fman->dev_priv);
471rerun:
472 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
473 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
474 list_del_init(&fence->head);
475 dma_fence_signal_locked(&fence->base);
476 INIT_LIST_HEAD(&action_list);
477 list_splice_init(&fence->seq_passed_actions,
478 &action_list);
479 vmw_fences_perform_actions(fman, &action_list);
480 } else
481 break;
482 }
483
484 /*
485 * Rerun if the fence goal seqno was updated, and the
486 * hardware might have raced with that update, so that
487 * we missed a fence_goal irq.
488 */
489
490 needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
491 if (unlikely(needs_rerun)) {
492 new_seqno = vmw_fence_read(fman->dev_priv);
493 if (new_seqno != seqno) {
494 seqno = new_seqno;
495 goto rerun;
496 }
497 }
498
499 if (!list_empty(&fman->cleanup_list))
500 (void) schedule_work(&fman->work);
501}
502
503void vmw_fences_update(struct vmw_fence_manager *fman)
504{
505 spin_lock(&fman->lock);
506 __vmw_fences_update(fman);
507 spin_unlock(&fman->lock);
508}
509
510bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
511{
512 struct vmw_fence_manager *fman = fman_from_fence(fence);
513
514 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
515 return true;
516
517 vmw_fences_update(fman);
518
519 return dma_fence_is_signaled(&fence->base);
520}
521
522int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
523 bool interruptible, unsigned long timeout)
524{
525 long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout);
526
527 if (likely(ret > 0))
528 return 0;
529 else if (ret == 0)
530 return -EBUSY;
531 else
532 return ret;
533}
534
535static void vmw_fence_destroy(struct vmw_fence_obj *fence)
536{
537 dma_fence_free(&fence->base);
538}
539
540int vmw_fence_create(struct vmw_fence_manager *fman,
541 uint32_t seqno,
542 struct vmw_fence_obj **p_fence)
543{
544 struct vmw_fence_obj *fence;
545 int ret;
546
547 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
548 if (unlikely(!fence))
549 return -ENOMEM;
550
551 ret = vmw_fence_obj_init(fman, fence, seqno,
552 vmw_fence_destroy);
553 if (unlikely(ret != 0))
554 goto out_err_init;
555
556 *p_fence = fence;
557 return 0;
558
559out_err_init:
560 kfree(fence);
561 return ret;
562}
563
564
565static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
566{
567 struct vmw_user_fence *ufence =
568 container_of(fence, struct vmw_user_fence, fence);
569
570 ttm_base_object_kfree(ufence, base);
571}
572
573static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
574{
575 struct ttm_base_object *base = *p_base;
576 struct vmw_user_fence *ufence =
577 container_of(base, struct vmw_user_fence, base);
578 struct vmw_fence_obj *fence = &ufence->fence;
579
580 *p_base = NULL;
581 vmw_fence_obj_unreference(&fence);
582}
583
584int vmw_user_fence_create(struct drm_file *file_priv,
585 struct vmw_fence_manager *fman,
586 uint32_t seqno,
587 struct vmw_fence_obj **p_fence,
588 uint32_t *p_handle)
589{
590 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
591 struct vmw_user_fence *ufence;
592 struct vmw_fence_obj *tmp;
593 int ret;
594
595 ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
596 if (unlikely(!ufence)) {
597 ret = -ENOMEM;
598 goto out_no_object;
599 }
600
601 ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
602 vmw_user_fence_destroy);
603 if (unlikely(ret != 0)) {
604 kfree(ufence);
605 goto out_no_object;
606 }
607
608 /*
609 * The base object holds a reference which is freed in
610 * vmw_user_fence_base_release.
611 */
612 tmp = vmw_fence_obj_reference(&ufence->fence);
613
614 ret = ttm_base_object_init(tfile, &ufence->base, false,
615 VMW_RES_FENCE,
616 &vmw_user_fence_base_release);
617
618
619 if (unlikely(ret != 0)) {
620 /*
621 * Free the base object's reference
622 */
623 vmw_fence_obj_unreference(&tmp);
624 goto out_err;
625 }
626
627 *p_fence = &ufence->fence;
628 *p_handle = ufence->base.handle;
629
630 return 0;
631out_err:
632 tmp = &ufence->fence;
633 vmw_fence_obj_unreference(&tmp);
634out_no_object:
635 return ret;
636}
637
638/*
639 * vmw_fence_fifo_down - signal all unsignaled fence objects.
640 */
641
642void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
643{
644 struct list_head action_list;
645 int ret;
646
647 /*
648 * The list may be altered while we traverse it, so always
649 * restart when we've released the fman->lock.
650 */
651
652 spin_lock(&fman->lock);
653 fman->fifo_down = true;
654 while (!list_empty(&fman->fence_list)) {
655 struct vmw_fence_obj *fence =
656 list_entry(fman->fence_list.prev, struct vmw_fence_obj,
657 head);
658 dma_fence_get(&fence->base);
659 spin_unlock(&fman->lock);
660
661 ret = vmw_fence_obj_wait(fence, false, false,
662 VMW_FENCE_WAIT_TIMEOUT);
663
664 if (unlikely(ret != 0)) {
665 list_del_init(&fence->head);
666 dma_fence_signal(&fence->base);
667 INIT_LIST_HEAD(&action_list);
668 list_splice_init(&fence->seq_passed_actions,
669 &action_list);
670 vmw_fences_perform_actions(fman, &action_list);
671 }
672
673 BUG_ON(!list_empty(&fence->head));
674 dma_fence_put(&fence->base);
675 spin_lock(&fman->lock);
676 }
677 spin_unlock(&fman->lock);
678}
679
680void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
681{
682 spin_lock(&fman->lock);
683 fman->fifo_down = false;
684 spin_unlock(&fman->lock);
685}
686
687
688/**
689 * vmw_fence_obj_lookup - Look up a user-space fence object
690 *
691 * @tfile: A struct ttm_object_file identifying the caller.
692 * @handle: A handle identifying the fence object.
693 * @return: A struct vmw_user_fence base ttm object on success or
694 * an error pointer on failure.
695 *
696 * The fence object is looked up and type-checked. The caller needs
697 * to have opened the fence object first, but since that happens on
698 * creation and fence objects aren't shareable, that's not an
699 * issue currently.
700 */
701static struct ttm_base_object *
702vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
703{
704 struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
705
706 if (!base) {
707 pr_err("Invalid fence object handle 0x%08lx.\n",
708 (unsigned long)handle);
709 return ERR_PTR(-EINVAL);
710 }
711
712 if (base->refcount_release != vmw_user_fence_base_release) {
713 pr_err("Invalid fence object handle 0x%08lx.\n",
714 (unsigned long)handle);
715 ttm_base_object_unref(&base);
716 return ERR_PTR(-EINVAL);
717 }
718
719 return base;
720}
721
722
723int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
724 struct drm_file *file_priv)
725{
726 struct drm_vmw_fence_wait_arg *arg =
727 (struct drm_vmw_fence_wait_arg *)data;
728 unsigned long timeout;
729 struct ttm_base_object *base;
730 struct vmw_fence_obj *fence;
731 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
732 int ret;
733 uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
734
735 /*
736 * 64-bit division not present on 32-bit systems, so do an
737 * approximation. (Divide by 1000000).
738 */
739
740 wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
741 (wait_timeout >> 26);
742
743 if (!arg->cookie_valid) {
744 arg->cookie_valid = 1;
745 arg->kernel_cookie = jiffies + wait_timeout;
746 }
747
748 base = vmw_fence_obj_lookup(tfile, arg->handle);
749 if (IS_ERR(base))
750 return PTR_ERR(base);
751
752 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
753
754 timeout = jiffies;
755 if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
756 ret = ((vmw_fence_obj_signaled(fence)) ?
757 0 : -EBUSY);
758 goto out;
759 }
760
761 timeout = (unsigned long)arg->kernel_cookie - timeout;
762
763 ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
764
765out:
766 ttm_base_object_unref(&base);
767
768 /*
769 * Optionally unref the fence object.
770 */
771
772 if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
773 return ttm_ref_object_base_unref(tfile, arg->handle);
774 return ret;
775}
776
777int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
778 struct drm_file *file_priv)
779{
780 struct drm_vmw_fence_signaled_arg *arg =
781 (struct drm_vmw_fence_signaled_arg *) data;
782 struct ttm_base_object *base;
783 struct vmw_fence_obj *fence;
784 struct vmw_fence_manager *fman;
785 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
786 struct vmw_private *dev_priv = vmw_priv(dev);
787
788 base = vmw_fence_obj_lookup(tfile, arg->handle);
789 if (IS_ERR(base))
790 return PTR_ERR(base);
791
792 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
793 fman = fman_from_fence(fence);
794
795 arg->signaled = vmw_fence_obj_signaled(fence);
796
797 arg->signaled_flags = arg->flags;
798 spin_lock(&fman->lock);
799 arg->passed_seqno = dev_priv->last_read_seqno;
800 spin_unlock(&fman->lock);
801
802 ttm_base_object_unref(&base);
803
804 return 0;
805}
806
807
808int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
809 struct drm_file *file_priv)
810{
811 struct drm_vmw_fence_arg *arg =
812 (struct drm_vmw_fence_arg *) data;
813
814 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
815 arg->handle);
816}
817
818/**
819 * vmw_event_fence_action_seq_passed
820 *
821 * @action: The struct vmw_fence_action embedded in a struct
822 * vmw_event_fence_action.
823 *
824 * This function is called when the seqno of the fence where @action is
825 * attached has passed. It queues the event on the submitter's event list.
826 * This function is always called from atomic context.
827 */
828static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
829{
830 struct vmw_event_fence_action *eaction =
831 container_of(action, struct vmw_event_fence_action, action);
832 struct drm_device *dev = eaction->dev;
833 struct drm_pending_event *event = eaction->event;
834
835 if (unlikely(event == NULL))
836 return;
837
838 spin_lock_irq(&dev->event_lock);
839
840 if (likely(eaction->tv_sec != NULL)) {
841 struct timespec64 ts;
842
843 ktime_get_ts64(&ts);
844 /* monotonic time, so no y2038 overflow */
845 *eaction->tv_sec = ts.tv_sec;
846 *eaction->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
847 }
848
849 drm_send_event_locked(dev, eaction->event);
850 eaction->event = NULL;
851 spin_unlock_irq(&dev->event_lock);
852}
853
854/**
855 * vmw_event_fence_action_cleanup
856 *
857 * @action: The struct vmw_fence_action embedded in a struct
858 * vmw_event_fence_action.
859 *
860 * This function is the struct vmw_fence_action destructor. It's typically
861 * called from a workqueue.
862 */
863static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
864{
865 struct vmw_event_fence_action *eaction =
866 container_of(action, struct vmw_event_fence_action, action);
867
868 vmw_fence_obj_unreference(&eaction->fence);
869 kfree(eaction);
870}
871
872
873/**
874 * vmw_fence_obj_add_action - Add an action to a fence object.
875 *
876 * @fence: The fence object.
877 * @action: The action to add.
878 *
879 * Note that the action callbacks may be executed before this function
880 * returns.
881 */
882static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
883 struct vmw_fence_action *action)
884{
885 struct vmw_fence_manager *fman = fman_from_fence(fence);
886 bool run_update = false;
887
888 mutex_lock(&fman->goal_irq_mutex);
889 spin_lock(&fman->lock);
890
891 fman->pending_actions[action->type]++;
892 if (dma_fence_is_signaled_locked(&fence->base)) {
893 struct list_head action_list;
894
895 INIT_LIST_HEAD(&action_list);
896 list_add_tail(&action->head, &action_list);
897 vmw_fences_perform_actions(fman, &action_list);
898 } else {
899 list_add_tail(&action->head, &fence->seq_passed_actions);
900
901 /*
902 * This function may set fman::seqno_valid, so it must
903 * be run with the goal_irq_mutex held.
904 */
905 run_update = vmw_fence_goal_check_locked(fence);
906 }
907
908 spin_unlock(&fman->lock);
909
910 if (run_update) {
911 if (!fman->goal_irq_on) {
912 fman->goal_irq_on = true;
913 vmw_goal_waiter_add(fman->dev_priv);
914 }
915 vmw_fences_update(fman);
916 }
917 mutex_unlock(&fman->goal_irq_mutex);
918
919}
920
921/**
922 * vmw_event_fence_action_queue - Post an event for sending when a fence
923 * object seqno has passed.
924 *
925 * @file_priv: The file connection on which the event should be posted.
926 * @fence: The fence object on which to post the event.
927 * @event: Event to be posted. This event should've been alloced
928 * using k[mz]alloc, and should've been completely initialized.
929 * @tv_sec: If non-null, the variable pointed to will be assigned
930 * current time tv_sec val when the fence signals.
931 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
932 * be assigned the current time tv_usec val when the fence signals.
933 * @interruptible: Interruptible waits if possible.
934 *
935 * As a side effect, the object pointed to by @event may have been
936 * freed when this function returns. If this function returns with
937 * an error code, the caller needs to free that object.
938 */
939
940int vmw_event_fence_action_queue(struct drm_file *file_priv,
941 struct vmw_fence_obj *fence,
942 struct drm_pending_event *event,
943 uint32_t *tv_sec,
944 uint32_t *tv_usec,
945 bool interruptible)
946{
947 struct vmw_event_fence_action *eaction;
948 struct vmw_fence_manager *fman = fman_from_fence(fence);
949
950 eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
951 if (unlikely(!eaction))
952 return -ENOMEM;
953
954 eaction->event = event;
955
956 eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
957 eaction->action.cleanup = vmw_event_fence_action_cleanup;
958 eaction->action.type = VMW_ACTION_EVENT;
959
960 eaction->fence = vmw_fence_obj_reference(fence);
961 eaction->dev = &fman->dev_priv->drm;
962 eaction->tv_sec = tv_sec;
963 eaction->tv_usec = tv_usec;
964
965 vmw_fence_obj_add_action(fence, &eaction->action);
966
967 return 0;
968}
969
970struct vmw_event_fence_pending {
971 struct drm_pending_event base;
972 struct drm_vmw_event_fence event;
973};
974
975static int vmw_event_fence_action_create(struct drm_file *file_priv,
976 struct vmw_fence_obj *fence,
977 uint32_t flags,
978 uint64_t user_data,
979 bool interruptible)
980{
981 struct vmw_event_fence_pending *event;
982 struct vmw_fence_manager *fman = fman_from_fence(fence);
983 struct drm_device *dev = &fman->dev_priv->drm;
984 int ret;
985
986 event = kzalloc(sizeof(*event), GFP_KERNEL);
987 if (unlikely(!event)) {
988 DRM_ERROR("Failed to allocate an event.\n");
989 ret = -ENOMEM;
990 goto out_no_space;
991 }
992
993 event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
994 event->event.base.length = sizeof(*event);
995 event->event.user_data = user_data;
996
997 ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
998
999 if (unlikely(ret != 0)) {
1000 DRM_ERROR("Failed to allocate event space for this file.\n");
1001 kfree(event);
1002 goto out_no_space;
1003 }
1004
1005 if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
1006 ret = vmw_event_fence_action_queue(file_priv, fence,
1007 &event->base,
1008 &event->event.tv_sec,
1009 &event->event.tv_usec,
1010 interruptible);
1011 else
1012 ret = vmw_event_fence_action_queue(file_priv, fence,
1013 &event->base,
1014 NULL,
1015 NULL,
1016 interruptible);
1017 if (ret != 0)
1018 goto out_no_queue;
1019
1020 return 0;
1021
1022out_no_queue:
1023 drm_event_cancel_free(dev, &event->base);
1024out_no_space:
1025 return ret;
1026}
1027
1028int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
1029 struct drm_file *file_priv)
1030{
1031 struct vmw_private *dev_priv = vmw_priv(dev);
1032 struct drm_vmw_fence_event_arg *arg =
1033 (struct drm_vmw_fence_event_arg *) data;
1034 struct vmw_fence_obj *fence = NULL;
1035 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1036 struct ttm_object_file *tfile = vmw_fp->tfile;
1037 struct drm_vmw_fence_rep __user *user_fence_rep =
1038 (struct drm_vmw_fence_rep __user *)(unsigned long)
1039 arg->fence_rep;
1040 uint32_t handle;
1041 int ret;
1042
1043 /*
1044 * Look up an existing fence object,
1045 * and if user-space wants a new reference,
1046 * add one.
1047 */
1048 if (arg->handle) {
1049 struct ttm_base_object *base =
1050 vmw_fence_obj_lookup(tfile, arg->handle);
1051
1052 if (IS_ERR(base))
1053 return PTR_ERR(base);
1054
1055 fence = &(container_of(base, struct vmw_user_fence,
1056 base)->fence);
1057 (void) vmw_fence_obj_reference(fence);
1058
1059 if (user_fence_rep != NULL) {
1060 ret = ttm_ref_object_add(vmw_fp->tfile, base,
1061 NULL, false);
1062 if (unlikely(ret != 0)) {
1063 DRM_ERROR("Failed to reference a fence "
1064 "object.\n");
1065 goto out_no_ref_obj;
1066 }
1067 handle = base->handle;
1068 }
1069 ttm_base_object_unref(&base);
1070 }
1071
1072 /*
1073 * Create a new fence object.
1074 */
1075 if (!fence) {
1076 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1077 &fence,
1078 (user_fence_rep) ?
1079 &handle : NULL);
1080 if (unlikely(ret != 0)) {
1081 DRM_ERROR("Fence event failed to create fence.\n");
1082 return ret;
1083 }
1084 }
1085
1086 BUG_ON(fence == NULL);
1087
1088 ret = vmw_event_fence_action_create(file_priv, fence,
1089 arg->flags,
1090 arg->user_data,
1091 true);
1092 if (unlikely(ret != 0)) {
1093 if (ret != -ERESTARTSYS)
1094 DRM_ERROR("Failed to attach event to fence.\n");
1095 goto out_no_create;
1096 }
1097
1098 vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
1099 handle, -1);
1100 vmw_fence_obj_unreference(&fence);
1101 return 0;
1102out_no_create:
1103 if (user_fence_rep != NULL)
1104 ttm_ref_object_base_unref(tfile, handle);
1105out_no_ref_obj:
1106 vmw_fence_obj_unreference(&fence);
1107 return ret;
1108}