1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2011-2023 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include <linux/sched/signal.h>
30 #include "vmwgfx_drv.h"
32 #define VMW_FENCE_WRAP (1 << 31)
34 struct vmw_fence_manager {
35 struct vmw_private *dev_priv;
37 struct list_head fence_list;
38 struct work_struct work;
40 struct list_head cleanup_list;
41 uint32_t pending_actions[VMW_ACTION_MAX];
42 struct mutex goal_irq_mutex;
43 bool goal_irq_on; /* Protected by @goal_irq_mutex */
44 bool seqno_valid; /* Protected by @lock, and may not be set to true
45 without the @goal_irq_mutex held. */
49 struct vmw_user_fence {
50 struct ttm_base_object base;
51 struct vmw_fence_obj fence;
55 * struct vmw_event_fence_action - fence action that delivers a drm event.
57 * @action: A struct vmw_fence_action to hook up to a fence.
58 * @event: A pointer to the pending event.
59 * @fence: A referenced pointer to the fence to keep it alive while @action
61 * @dev: Pointer to a struct drm_device so we can access the event stuff.
62 * @tv_sec: If non-null, the variable pointed to will be assigned
63 * current time tv_sec val when the fence signals.
64 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
65 * be assigned the current time tv_usec val when the fence signals.
67 struct vmw_event_fence_action {
68 struct vmw_fence_action action;
70 struct drm_pending_event *event;
71 struct vmw_fence_obj *fence;
72 struct drm_device *dev;
78 static struct vmw_fence_manager *
79 fman_from_fence(struct vmw_fence_obj *fence)
81 return container_of(fence->base.lock, struct vmw_fence_manager, lock);
84 static u32 vmw_fence_goal_read(struct vmw_private *vmw)
86 if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
87 return vmw_read(vmw, SVGA_REG_FENCE_GOAL);
89 return vmw_fifo_mem_read(vmw, SVGA_FIFO_FENCE_GOAL);
92 static void vmw_fence_goal_write(struct vmw_private *vmw, u32 value)
94 if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
95 vmw_write(vmw, SVGA_REG_FENCE_GOAL, value);
97 vmw_fifo_mem_write(vmw, SVGA_FIFO_FENCE_GOAL, value);
101 * Note on fencing subsystem usage of irqs:
102 * Typically the vmw_fences_update function is called
104 * a) When a new fence seqno has been submitted by the fifo code.
105 * b) On-demand when we have waiters. Sleeping waiters will switch on the
106 * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE
107 * irq is received. When the last fence waiter is gone, that IRQ is masked
110 * In situations where there are no waiters and we don't submit any new fences,
111 * fence objects may not be signaled. This is perfectly OK, since there are
112 * no consumers of the signaled data, but that is NOT ok when there are fence
113 * actions attached to a fence. The fencing subsystem then makes use of the
114 * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence
115 * which has an action attached, and each time vmw_fences_update is called,
116 * the subsystem makes sure the fence goal seqno is updated.
118 * The fence goal seqno irq is on as long as there are unsignaled fence
119 * objects with actions attached to them.
122 static void vmw_fence_obj_destroy(struct dma_fence *f)
124 struct vmw_fence_obj *fence =
125 container_of(f, struct vmw_fence_obj, base);
126 struct vmw_fence_manager *fman = fman_from_fence(fence);
128 if (!list_empty(&fence->head)) {
129 spin_lock(&fman->lock);
130 list_del_init(&fence->head);
131 spin_unlock(&fman->lock);
133 fence->destroy(fence);
136 static const char *vmw_fence_get_driver_name(struct dma_fence *f)
141 static const char *vmw_fence_get_timeline_name(struct dma_fence *f)
146 static bool vmw_fence_enable_signaling(struct dma_fence *f)
148 struct vmw_fence_obj *fence =
149 container_of(f, struct vmw_fence_obj, base);
151 struct vmw_fence_manager *fman = fman_from_fence(fence);
152 struct vmw_private *dev_priv = fman->dev_priv;
154 u32 seqno = vmw_fence_read(dev_priv);
155 if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
161 struct vmwgfx_wait_cb {
162 struct dma_fence_cb base;
163 struct task_struct *task;
167 vmwgfx_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
169 struct vmwgfx_wait_cb *wait =
170 container_of(cb, struct vmwgfx_wait_cb, base);
172 wake_up_process(wait->task);
175 static void __vmw_fences_update(struct vmw_fence_manager *fman);
177 static long vmw_fence_wait(struct dma_fence *f, bool intr, signed long timeout)
179 struct vmw_fence_obj *fence =
180 container_of(f, struct vmw_fence_obj, base);
182 struct vmw_fence_manager *fman = fman_from_fence(fence);
183 struct vmw_private *dev_priv = fman->dev_priv;
184 struct vmwgfx_wait_cb cb;
187 if (likely(vmw_fence_obj_signaled(fence)))
190 vmw_seqno_waiter_add(dev_priv);
194 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags))
197 if (intr && signal_pending(current)) {
202 cb.base.func = vmwgfx_wait_cb;
204 list_add(&cb.base.node, &f->cb_list);
207 __vmw_fences_update(fman);
210 * We can use the barrier free __set_current_state() since
211 * DMA_FENCE_FLAG_SIGNALED_BIT + wakeup is protected by the
215 __set_current_state(TASK_INTERRUPTIBLE);
217 __set_current_state(TASK_UNINTERRUPTIBLE);
219 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags)) {
220 if (ret == 0 && timeout > 0)
225 if (intr && signal_pending(current)) {
233 spin_unlock(f->lock);
235 ret = schedule_timeout(ret);
239 __set_current_state(TASK_RUNNING);
240 if (!list_empty(&cb.base.node))
241 list_del(&cb.base.node);
244 spin_unlock(f->lock);
246 vmw_seqno_waiter_remove(dev_priv);
251 static const struct dma_fence_ops vmw_fence_ops = {
252 .get_driver_name = vmw_fence_get_driver_name,
253 .get_timeline_name = vmw_fence_get_timeline_name,
254 .enable_signaling = vmw_fence_enable_signaling,
255 .wait = vmw_fence_wait,
256 .release = vmw_fence_obj_destroy,
260 * Execute signal actions on fences recently signaled.
261 * This is done from a workqueue so we don't have to execute
262 * signal actions from atomic context.
265 static void vmw_fence_work_func(struct work_struct *work)
267 struct vmw_fence_manager *fman =
268 container_of(work, struct vmw_fence_manager, work);
269 struct list_head list;
270 struct vmw_fence_action *action, *next_action;
274 INIT_LIST_HEAD(&list);
275 mutex_lock(&fman->goal_irq_mutex);
277 spin_lock(&fman->lock);
278 list_splice_init(&fman->cleanup_list, &list);
279 seqno_valid = fman->seqno_valid;
280 spin_unlock(&fman->lock);
282 if (!seqno_valid && fman->goal_irq_on) {
283 fman->goal_irq_on = false;
284 vmw_goal_waiter_remove(fman->dev_priv);
286 mutex_unlock(&fman->goal_irq_mutex);
288 if (list_empty(&list))
292 * At this point, only we should be able to manipulate the
293 * list heads of the actions we have on the private list.
294 * hence fman::lock not held.
297 list_for_each_entry_safe(action, next_action, &list, head) {
298 list_del_init(&action->head);
300 action->cleanup(action);
305 struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
307 struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
312 fman->dev_priv = dev_priv;
313 spin_lock_init(&fman->lock);
314 INIT_LIST_HEAD(&fman->fence_list);
315 INIT_LIST_HEAD(&fman->cleanup_list);
316 INIT_WORK(&fman->work, &vmw_fence_work_func);
317 fman->fifo_down = true;
318 mutex_init(&fman->goal_irq_mutex);
319 fman->ctx = dma_fence_context_alloc(1);
324 void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
328 (void) cancel_work_sync(&fman->work);
330 spin_lock(&fman->lock);
331 lists_empty = list_empty(&fman->fence_list) &&
332 list_empty(&fman->cleanup_list);
333 spin_unlock(&fman->lock);
335 BUG_ON(!lists_empty);
339 static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
340 struct vmw_fence_obj *fence, u32 seqno,
341 void (*destroy) (struct vmw_fence_obj *fence))
345 dma_fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
347 INIT_LIST_HEAD(&fence->seq_passed_actions);
348 fence->destroy = destroy;
350 spin_lock(&fman->lock);
351 if (unlikely(fman->fifo_down)) {
355 list_add_tail(&fence->head, &fman->fence_list);
358 spin_unlock(&fman->lock);
363 static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
364 struct list_head *list)
366 struct vmw_fence_action *action, *next_action;
368 list_for_each_entry_safe(action, next_action, list, head) {
369 list_del_init(&action->head);
370 fman->pending_actions[action->type]--;
371 if (action->seq_passed != NULL)
372 action->seq_passed(action);
375 * Add the cleanup action to the cleanup list so that
376 * it will be performed by a worker task.
379 list_add_tail(&action->head, &fman->cleanup_list);
384 * vmw_fence_goal_new_locked - Figure out a new device fence goal
387 * @fman: Pointer to a fence manager.
388 * @passed_seqno: The seqno the device currently signals as passed.
390 * This function should be called with the fence manager lock held.
391 * It is typically called when we have a new passed_seqno, and
392 * we might need to update the fence goal. It checks to see whether
393 * the current fence goal has already passed, and, in that case,
394 * scans through all unsignaled fences to get the next fence object with an
395 * action attached, and sets the seqno of that fence as a new fence goal.
397 * returns true if the device goal seqno was updated. False otherwise.
399 static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
403 struct vmw_fence_obj *fence, *next_fence;
405 if (likely(!fman->seqno_valid))
408 goal_seqno = vmw_fence_goal_read(fman->dev_priv);
409 if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
412 fman->seqno_valid = false;
413 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
414 if (!list_empty(&fence->seq_passed_actions)) {
415 fman->seqno_valid = true;
416 vmw_fence_goal_write(fman->dev_priv,
427 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
430 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
431 * considered as a device fence goal.
433 * This function should be called with the fence manager lock held.
434 * It is typically called when an action has been attached to a fence to
435 * check whether the seqno of that fence should be used for a fence
436 * goal interrupt. This is typically needed if the current fence goal is
437 * invalid, or has a higher seqno than that of the current fence object.
439 * returns true if the device goal seqno was updated. False otherwise.
441 static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
443 struct vmw_fence_manager *fman = fman_from_fence(fence);
446 if (dma_fence_is_signaled_locked(&fence->base))
449 goal_seqno = vmw_fence_goal_read(fman->dev_priv);
450 if (likely(fman->seqno_valid &&
451 goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
454 vmw_fence_goal_write(fman->dev_priv, fence->base.seqno);
455 fman->seqno_valid = true;
460 static void __vmw_fences_update(struct vmw_fence_manager *fman)
462 struct vmw_fence_obj *fence, *next_fence;
463 struct list_head action_list;
465 uint32_t seqno, new_seqno;
467 seqno = vmw_fence_read(fman->dev_priv);
469 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
470 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
471 list_del_init(&fence->head);
472 dma_fence_signal_locked(&fence->base);
473 INIT_LIST_HEAD(&action_list);
474 list_splice_init(&fence->seq_passed_actions,
476 vmw_fences_perform_actions(fman, &action_list);
482 * Rerun if the fence goal seqno was updated, and the
483 * hardware might have raced with that update, so that
484 * we missed a fence_goal irq.
487 needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
488 if (unlikely(needs_rerun)) {
489 new_seqno = vmw_fence_read(fman->dev_priv);
490 if (new_seqno != seqno) {
496 if (!list_empty(&fman->cleanup_list))
497 (void) schedule_work(&fman->work);
500 void vmw_fences_update(struct vmw_fence_manager *fman)
502 spin_lock(&fman->lock);
503 __vmw_fences_update(fman);
504 spin_unlock(&fman->lock);
507 bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
509 struct vmw_fence_manager *fman = fman_from_fence(fence);
511 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
514 vmw_fences_update(fman);
516 return dma_fence_is_signaled(&fence->base);
519 int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
520 bool interruptible, unsigned long timeout)
522 long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout);
532 static void vmw_fence_destroy(struct vmw_fence_obj *fence)
534 dma_fence_free(&fence->base);
537 int vmw_fence_create(struct vmw_fence_manager *fman,
539 struct vmw_fence_obj **p_fence)
541 struct vmw_fence_obj *fence;
544 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
545 if (unlikely(!fence))
548 ret = vmw_fence_obj_init(fman, fence, seqno,
550 if (unlikely(ret != 0))
562 static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
564 struct vmw_user_fence *ufence =
565 container_of(fence, struct vmw_user_fence, fence);
567 ttm_base_object_kfree(ufence, base);
570 static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
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;
578 vmw_fence_obj_unreference(&fence);
581 int vmw_user_fence_create(struct drm_file *file_priv,
582 struct vmw_fence_manager *fman,
584 struct vmw_fence_obj **p_fence,
587 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
588 struct vmw_user_fence *ufence;
589 struct vmw_fence_obj *tmp;
592 ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
593 if (unlikely(!ufence)) {
598 ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
599 vmw_user_fence_destroy);
600 if (unlikely(ret != 0)) {
606 * The base object holds a reference which is freed in
607 * vmw_user_fence_base_release.
609 tmp = vmw_fence_obj_reference(&ufence->fence);
611 ret = ttm_base_object_init(tfile, &ufence->base, false,
613 &vmw_user_fence_base_release);
616 if (unlikely(ret != 0)) {
618 * Free the base object's reference
620 vmw_fence_obj_unreference(&tmp);
624 *p_fence = &ufence->fence;
625 *p_handle = ufence->base.handle;
629 tmp = &ufence->fence;
630 vmw_fence_obj_unreference(&tmp);
636 * vmw_fence_fifo_down - signal all unsignaled fence objects.
639 void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
641 struct list_head action_list;
645 * The list may be altered while we traverse it, so always
646 * restart when we've released the fman->lock.
649 spin_lock(&fman->lock);
650 fman->fifo_down = true;
651 while (!list_empty(&fman->fence_list)) {
652 struct vmw_fence_obj *fence =
653 list_entry(fman->fence_list.prev, struct vmw_fence_obj,
655 dma_fence_get(&fence->base);
656 spin_unlock(&fman->lock);
658 ret = vmw_fence_obj_wait(fence, false, false,
659 VMW_FENCE_WAIT_TIMEOUT);
661 if (unlikely(ret != 0)) {
662 list_del_init(&fence->head);
663 dma_fence_signal(&fence->base);
664 INIT_LIST_HEAD(&action_list);
665 list_splice_init(&fence->seq_passed_actions,
667 vmw_fences_perform_actions(fman, &action_list);
670 BUG_ON(!list_empty(&fence->head));
671 dma_fence_put(&fence->base);
672 spin_lock(&fman->lock);
674 spin_unlock(&fman->lock);
677 void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
679 spin_lock(&fman->lock);
680 fman->fifo_down = false;
681 spin_unlock(&fman->lock);
686 * vmw_fence_obj_lookup - Look up a user-space fence object
688 * @tfile: A struct ttm_object_file identifying the caller.
689 * @handle: A handle identifying the fence object.
690 * @return: A struct vmw_user_fence base ttm object on success or
691 * an error pointer on failure.
693 * The fence object is looked up and type-checked. The caller needs
694 * to have opened the fence object first, but since that happens on
695 * creation and fence objects aren't shareable, that's not an
698 static struct ttm_base_object *
699 vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
701 struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
704 pr_err("Invalid fence object handle 0x%08lx.\n",
705 (unsigned long)handle);
706 return ERR_PTR(-EINVAL);
709 if (base->refcount_release != vmw_user_fence_base_release) {
710 pr_err("Invalid fence object handle 0x%08lx.\n",
711 (unsigned long)handle);
712 ttm_base_object_unref(&base);
713 return ERR_PTR(-EINVAL);
720 int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
721 struct drm_file *file_priv)
723 struct drm_vmw_fence_wait_arg *arg =
724 (struct drm_vmw_fence_wait_arg *)data;
725 unsigned long timeout;
726 struct ttm_base_object *base;
727 struct vmw_fence_obj *fence;
728 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
730 uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
733 * 64-bit division not present on 32-bit systems, so do an
734 * approximation. (Divide by 1000000).
737 wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
738 (wait_timeout >> 26);
740 if (!arg->cookie_valid) {
741 arg->cookie_valid = 1;
742 arg->kernel_cookie = jiffies + wait_timeout;
745 base = vmw_fence_obj_lookup(tfile, arg->handle);
747 return PTR_ERR(base);
749 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
752 if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
753 ret = ((vmw_fence_obj_signaled(fence)) ?
758 timeout = (unsigned long)arg->kernel_cookie - timeout;
760 ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
763 ttm_base_object_unref(&base);
766 * Optionally unref the fence object.
769 if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
770 return ttm_ref_object_base_unref(tfile, arg->handle);
774 int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
775 struct drm_file *file_priv)
777 struct drm_vmw_fence_signaled_arg *arg =
778 (struct drm_vmw_fence_signaled_arg *) data;
779 struct ttm_base_object *base;
780 struct vmw_fence_obj *fence;
781 struct vmw_fence_manager *fman;
782 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
783 struct vmw_private *dev_priv = vmw_priv(dev);
785 base = vmw_fence_obj_lookup(tfile, arg->handle);
787 return PTR_ERR(base);
789 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
790 fman = fman_from_fence(fence);
792 arg->signaled = vmw_fence_obj_signaled(fence);
794 arg->signaled_flags = arg->flags;
795 spin_lock(&fman->lock);
796 arg->passed_seqno = dev_priv->last_read_seqno;
797 spin_unlock(&fman->lock);
799 ttm_base_object_unref(&base);
805 int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
806 struct drm_file *file_priv)
808 struct drm_vmw_fence_arg *arg =
809 (struct drm_vmw_fence_arg *) data;
811 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
816 * vmw_event_fence_action_seq_passed
818 * @action: The struct vmw_fence_action embedded in a struct
819 * vmw_event_fence_action.
821 * This function is called when the seqno of the fence where @action is
822 * attached has passed. It queues the event on the submitter's event list.
823 * This function is always called from atomic context.
825 static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
827 struct vmw_event_fence_action *eaction =
828 container_of(action, struct vmw_event_fence_action, action);
829 struct drm_device *dev = eaction->dev;
830 struct drm_pending_event *event = eaction->event;
832 if (unlikely(event == NULL))
835 spin_lock_irq(&dev->event_lock);
837 if (likely(eaction->tv_sec != NULL)) {
838 struct timespec64 ts;
841 /* monotonic time, so no y2038 overflow */
842 *eaction->tv_sec = ts.tv_sec;
843 *eaction->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
846 drm_send_event_locked(dev, eaction->event);
847 eaction->event = NULL;
848 spin_unlock_irq(&dev->event_lock);
852 * vmw_event_fence_action_cleanup
854 * @action: The struct vmw_fence_action embedded in a struct
855 * vmw_event_fence_action.
857 * This function is the struct vmw_fence_action destructor. It's typically
858 * called from a workqueue.
860 static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
862 struct vmw_event_fence_action *eaction =
863 container_of(action, struct vmw_event_fence_action, action);
865 vmw_fence_obj_unreference(&eaction->fence);
871 * vmw_fence_obj_add_action - Add an action to a fence object.
873 * @fence: The fence object.
874 * @action: The action to add.
876 * Note that the action callbacks may be executed before this function
879 static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
880 struct vmw_fence_action *action)
882 struct vmw_fence_manager *fman = fman_from_fence(fence);
883 bool run_update = false;
885 mutex_lock(&fman->goal_irq_mutex);
886 spin_lock(&fman->lock);
888 fman->pending_actions[action->type]++;
889 if (dma_fence_is_signaled_locked(&fence->base)) {
890 struct list_head action_list;
892 INIT_LIST_HEAD(&action_list);
893 list_add_tail(&action->head, &action_list);
894 vmw_fences_perform_actions(fman, &action_list);
896 list_add_tail(&action->head, &fence->seq_passed_actions);
899 * This function may set fman::seqno_valid, so it must
900 * be run with the goal_irq_mutex held.
902 run_update = vmw_fence_goal_check_locked(fence);
905 spin_unlock(&fman->lock);
908 if (!fman->goal_irq_on) {
909 fman->goal_irq_on = true;
910 vmw_goal_waiter_add(fman->dev_priv);
912 vmw_fences_update(fman);
914 mutex_unlock(&fman->goal_irq_mutex);
919 * vmw_event_fence_action_queue - Post an event for sending when a fence
920 * object seqno has passed.
922 * @file_priv: The file connection on which the event should be posted.
923 * @fence: The fence object on which to post the event.
924 * @event: Event to be posted. This event should've been alloced
925 * using k[mz]alloc, and should've been completely initialized.
926 * @tv_sec: If non-null, the variable pointed to will be assigned
927 * current time tv_sec val when the fence signals.
928 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
929 * be assigned the current time tv_usec val when the fence signals.
930 * @interruptible: Interruptible waits if possible.
932 * As a side effect, the object pointed to by @event may have been
933 * freed when this function returns. If this function returns with
934 * an error code, the caller needs to free that object.
937 int vmw_event_fence_action_queue(struct drm_file *file_priv,
938 struct vmw_fence_obj *fence,
939 struct drm_pending_event *event,
944 struct vmw_event_fence_action *eaction;
945 struct vmw_fence_manager *fman = fman_from_fence(fence);
947 eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
948 if (unlikely(!eaction))
951 eaction->event = event;
953 eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
954 eaction->action.cleanup = vmw_event_fence_action_cleanup;
955 eaction->action.type = VMW_ACTION_EVENT;
957 eaction->fence = vmw_fence_obj_reference(fence);
958 eaction->dev = &fman->dev_priv->drm;
959 eaction->tv_sec = tv_sec;
960 eaction->tv_usec = tv_usec;
962 vmw_fence_obj_add_action(fence, &eaction->action);
967 struct vmw_event_fence_pending {
968 struct drm_pending_event base;
969 struct drm_vmw_event_fence event;
972 static int vmw_event_fence_action_create(struct drm_file *file_priv,
973 struct vmw_fence_obj *fence,
978 struct vmw_event_fence_pending *event;
979 struct vmw_fence_manager *fman = fman_from_fence(fence);
980 struct drm_device *dev = &fman->dev_priv->drm;
983 event = kzalloc(sizeof(*event), GFP_KERNEL);
984 if (unlikely(!event)) {
985 DRM_ERROR("Failed to allocate an event.\n");
990 event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
991 event->event.base.length = sizeof(event->event);
992 event->event.user_data = user_data;
994 ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
996 if (unlikely(ret != 0)) {
997 DRM_ERROR("Failed to allocate event space for this file.\n");
1002 if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
1003 ret = vmw_event_fence_action_queue(file_priv, fence,
1005 &event->event.tv_sec,
1006 &event->event.tv_usec,
1009 ret = vmw_event_fence_action_queue(file_priv, fence,
1020 drm_event_cancel_free(dev, &event->base);
1025 int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
1026 struct drm_file *file_priv)
1028 struct vmw_private *dev_priv = vmw_priv(dev);
1029 struct drm_vmw_fence_event_arg *arg =
1030 (struct drm_vmw_fence_event_arg *) data;
1031 struct vmw_fence_obj *fence = NULL;
1032 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1033 struct ttm_object_file *tfile = vmw_fp->tfile;
1034 struct drm_vmw_fence_rep __user *user_fence_rep =
1035 (struct drm_vmw_fence_rep __user *)(unsigned long)
1041 * Look up an existing fence object,
1042 * and if user-space wants a new reference,
1046 struct ttm_base_object *base =
1047 vmw_fence_obj_lookup(tfile, arg->handle);
1050 return PTR_ERR(base);
1052 fence = &(container_of(base, struct vmw_user_fence,
1054 (void) vmw_fence_obj_reference(fence);
1056 if (user_fence_rep != NULL) {
1057 ret = ttm_ref_object_add(vmw_fp->tfile, base,
1059 if (unlikely(ret != 0)) {
1060 DRM_ERROR("Failed to reference a fence "
1062 goto out_no_ref_obj;
1064 handle = base->handle;
1066 ttm_base_object_unref(&base);
1070 * Create a new fence object.
1073 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1077 if (unlikely(ret != 0)) {
1078 DRM_ERROR("Fence event failed to create fence.\n");
1083 BUG_ON(fence == NULL);
1085 ret = vmw_event_fence_action_create(file_priv, fence,
1089 if (unlikely(ret != 0)) {
1090 if (ret != -ERESTARTSYS)
1091 DRM_ERROR("Failed to attach event to fence.\n");
1095 vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
1097 vmw_fence_obj_unreference(&fence);
1100 if (user_fence_rep != NULL)
1101 ttm_ref_object_base_unref(tfile, handle);
1103 vmw_fence_obj_unreference(&fence);