1 /**************************************************************************
3 * Copyright © 2011-2014 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 **************************************************************************/
29 #include "vmwgfx_drv.h"
31 #define VMW_FENCE_WRAP (1 << 31)
33 struct vmw_fence_manager {
34 int num_fence_objects;
35 struct vmw_private *dev_priv;
37 struct list_head fence_list;
38 struct work_struct work;
41 u32 event_fence_action_size;
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. */
52 struct vmw_user_fence {
53 struct ttm_base_object base;
54 struct vmw_fence_obj fence;
58 * struct vmw_event_fence_action - fence action that delivers a drm event.
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
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.
72 struct vmw_event_fence_action {
73 struct vmw_fence_action action;
74 struct list_head fpriv_head;
76 struct drm_pending_event *event;
77 struct vmw_fence_obj *fence;
78 struct drm_device *dev;
84 static struct vmw_fence_manager *
85 fman_from_fence(struct vmw_fence_obj *fence)
87 return container_of(fence->base.lock, struct vmw_fence_manager, lock);
91 * Note on fencing subsystem usage of irqs:
92 * Typically the vmw_fences_update function is called
94 * a) When a new fence seqno has been submitted by the fifo code.
95 * b) On-demand when we have waiters. Sleeping waiters will switch on the
96 * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE
97 * irq is received. When the last fence waiter is gone, that IRQ is masked
100 * In situations where there are no waiters and we don't submit any new fences,
101 * fence objects may not be signaled. This is perfectly OK, since there are
102 * no consumers of the signaled data, but that is NOT ok when there are fence
103 * actions attached to a fence. The fencing subsystem then makes use of the
104 * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence
105 * which has an action attached, and each time vmw_fences_update is called,
106 * the subsystem makes sure the fence goal seqno is updated.
108 * The fence goal seqno irq is on as long as there are unsignaled fence
109 * objects with actions attached to them.
112 static void vmw_fence_obj_destroy(struct fence *f)
114 struct vmw_fence_obj *fence =
115 container_of(f, struct vmw_fence_obj, base);
117 struct vmw_fence_manager *fman = fman_from_fence(fence);
118 unsigned long irq_flags;
120 spin_lock_irqsave(&fman->lock, irq_flags);
121 list_del_init(&fence->head);
122 --fman->num_fence_objects;
123 spin_unlock_irqrestore(&fman->lock, irq_flags);
124 fence->destroy(fence);
127 static const char *vmw_fence_get_driver_name(struct fence *f)
132 static const char *vmw_fence_get_timeline_name(struct fence *f)
137 static bool vmw_fence_enable_signaling(struct fence *f)
139 struct vmw_fence_obj *fence =
140 container_of(f, struct vmw_fence_obj, base);
142 struct vmw_fence_manager *fman = fman_from_fence(fence);
143 struct vmw_private *dev_priv = fman->dev_priv;
145 u32 *fifo_mem = dev_priv->mmio_virt;
146 u32 seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
147 if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
150 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
155 struct vmwgfx_wait_cb {
156 struct fence_cb base;
157 struct task_struct *task;
161 vmwgfx_wait_cb(struct fence *fence, struct fence_cb *cb)
163 struct vmwgfx_wait_cb *wait =
164 container_of(cb, struct vmwgfx_wait_cb, base);
166 wake_up_process(wait->task);
169 static void __vmw_fences_update(struct vmw_fence_manager *fman);
171 static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
173 struct vmw_fence_obj *fence =
174 container_of(f, struct vmw_fence_obj, base);
176 struct vmw_fence_manager *fman = fman_from_fence(fence);
177 struct vmw_private *dev_priv = fman->dev_priv;
178 struct vmwgfx_wait_cb cb;
180 unsigned long irq_flags;
182 if (likely(vmw_fence_obj_signaled(fence)))
185 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
186 vmw_seqno_waiter_add(dev_priv);
188 spin_lock_irqsave(f->lock, irq_flags);
190 if (intr && signal_pending(current)) {
195 cb.base.func = vmwgfx_wait_cb;
197 list_add(&cb.base.node, &f->cb_list);
200 __vmw_fences_update(fman);
201 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &f->flags))
205 __set_current_state(TASK_INTERRUPTIBLE);
207 __set_current_state(TASK_UNINTERRUPTIBLE);
208 spin_unlock_irqrestore(f->lock, irq_flags);
210 ret = schedule_timeout(ret);
212 spin_lock_irqsave(f->lock, irq_flags);
213 if (ret > 0 && intr && signal_pending(current))
217 if (!list_empty(&cb.base.node))
218 list_del(&cb.base.node);
219 __set_current_state(TASK_RUNNING);
222 spin_unlock_irqrestore(f->lock, irq_flags);
224 vmw_seqno_waiter_remove(dev_priv);
229 static struct fence_ops vmw_fence_ops = {
230 .get_driver_name = vmw_fence_get_driver_name,
231 .get_timeline_name = vmw_fence_get_timeline_name,
232 .enable_signaling = vmw_fence_enable_signaling,
233 .wait = vmw_fence_wait,
234 .release = vmw_fence_obj_destroy,
239 * Execute signal actions on fences recently signaled.
240 * This is done from a workqueue so we don't have to execute
241 * signal actions from atomic context.
244 static void vmw_fence_work_func(struct work_struct *work)
246 struct vmw_fence_manager *fman =
247 container_of(work, struct vmw_fence_manager, work);
248 struct list_head list;
249 struct vmw_fence_action *action, *next_action;
253 INIT_LIST_HEAD(&list);
254 mutex_lock(&fman->goal_irq_mutex);
256 spin_lock_irq(&fman->lock);
257 list_splice_init(&fman->cleanup_list, &list);
258 seqno_valid = fman->seqno_valid;
259 spin_unlock_irq(&fman->lock);
261 if (!seqno_valid && fman->goal_irq_on) {
262 fman->goal_irq_on = false;
263 vmw_goal_waiter_remove(fman->dev_priv);
265 mutex_unlock(&fman->goal_irq_mutex);
267 if (list_empty(&list))
271 * At this point, only we should be able to manipulate the
272 * list heads of the actions we have on the private list.
273 * hence fman::lock not held.
276 list_for_each_entry_safe(action, next_action, &list, head) {
277 list_del_init(&action->head);
279 action->cleanup(action);
284 struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
286 struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
288 if (unlikely(fman == NULL))
291 fman->dev_priv = dev_priv;
292 spin_lock_init(&fman->lock);
293 INIT_LIST_HEAD(&fman->fence_list);
294 INIT_LIST_HEAD(&fman->cleanup_list);
295 INIT_WORK(&fman->work, &vmw_fence_work_func);
296 fman->fifo_down = true;
297 fman->user_fence_size = ttm_round_pot(sizeof(struct vmw_user_fence));
298 fman->fence_size = ttm_round_pot(sizeof(struct vmw_fence_obj));
299 fman->event_fence_action_size =
300 ttm_round_pot(sizeof(struct vmw_event_fence_action));
301 mutex_init(&fman->goal_irq_mutex);
302 fman->ctx = fence_context_alloc(1);
307 void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
309 unsigned long irq_flags;
312 (void) cancel_work_sync(&fman->work);
314 spin_lock_irqsave(&fman->lock, irq_flags);
315 lists_empty = list_empty(&fman->fence_list) &&
316 list_empty(&fman->cleanup_list);
317 spin_unlock_irqrestore(&fman->lock, irq_flags);
319 BUG_ON(!lists_empty);
323 static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
324 struct vmw_fence_obj *fence, u32 seqno,
325 void (*destroy) (struct vmw_fence_obj *fence))
327 unsigned long irq_flags;
330 fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
332 INIT_LIST_HEAD(&fence->seq_passed_actions);
333 fence->destroy = destroy;
335 spin_lock_irqsave(&fman->lock, irq_flags);
336 if (unlikely(fman->fifo_down)) {
340 list_add_tail(&fence->head, &fman->fence_list);
341 ++fman->num_fence_objects;
344 spin_unlock_irqrestore(&fman->lock, irq_flags);
349 static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
350 struct list_head *list)
352 struct vmw_fence_action *action, *next_action;
354 list_for_each_entry_safe(action, next_action, list, head) {
355 list_del_init(&action->head);
356 fman->pending_actions[action->type]--;
357 if (action->seq_passed != NULL)
358 action->seq_passed(action);
361 * Add the cleanup action to the cleanup list so that
362 * it will be performed by a worker task.
365 list_add_tail(&action->head, &fman->cleanup_list);
370 * vmw_fence_goal_new_locked - Figure out a new device fence goal
373 * @fman: Pointer to a fence manager.
374 * @passed_seqno: The seqno the device currently signals as passed.
376 * This function should be called with the fence manager lock held.
377 * It is typically called when we have a new passed_seqno, and
378 * we might need to update the fence goal. It checks to see whether
379 * the current fence goal has already passed, and, in that case,
380 * scans through all unsignaled fences to get the next fence object with an
381 * action attached, and sets the seqno of that fence as a new fence goal.
383 * returns true if the device goal seqno was updated. False otherwise.
385 static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
390 struct vmw_fence_obj *fence;
392 if (likely(!fman->seqno_valid))
395 fifo_mem = fman->dev_priv->mmio_virt;
396 goal_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE_GOAL);
397 if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
400 fman->seqno_valid = false;
401 list_for_each_entry(fence, &fman->fence_list, head) {
402 if (!list_empty(&fence->seq_passed_actions)) {
403 fman->seqno_valid = true;
404 vmw_mmio_write(fence->base.seqno,
405 fifo_mem + SVGA_FIFO_FENCE_GOAL);
415 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
418 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
419 * considered as a device fence goal.
421 * This function should be called with the fence manager lock held.
422 * It is typically called when an action has been attached to a fence to
423 * check whether the seqno of that fence should be used for a fence
424 * goal interrupt. This is typically needed if the current fence goal is
425 * invalid, or has a higher seqno than that of the current fence object.
427 * returns true if the device goal seqno was updated. False otherwise.
429 static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
431 struct vmw_fence_manager *fman = fman_from_fence(fence);
435 if (fence_is_signaled_locked(&fence->base))
438 fifo_mem = fman->dev_priv->mmio_virt;
439 goal_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE_GOAL);
440 if (likely(fman->seqno_valid &&
441 goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
444 vmw_mmio_write(fence->base.seqno, fifo_mem + SVGA_FIFO_FENCE_GOAL);
445 fman->seqno_valid = true;
450 static void __vmw_fences_update(struct vmw_fence_manager *fman)
452 struct vmw_fence_obj *fence, *next_fence;
453 struct list_head action_list;
455 uint32_t seqno, new_seqno;
456 u32 *fifo_mem = fman->dev_priv->mmio_virt;
458 seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
460 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
461 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
462 list_del_init(&fence->head);
463 fence_signal_locked(&fence->base);
464 INIT_LIST_HEAD(&action_list);
465 list_splice_init(&fence->seq_passed_actions,
467 vmw_fences_perform_actions(fman, &action_list);
473 * Rerun if the fence goal seqno was updated, and the
474 * hardware might have raced with that update, so that
475 * we missed a fence_goal irq.
478 needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
479 if (unlikely(needs_rerun)) {
480 new_seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
481 if (new_seqno != seqno) {
487 if (!list_empty(&fman->cleanup_list))
488 (void) schedule_work(&fman->work);
491 void vmw_fences_update(struct vmw_fence_manager *fman)
493 unsigned long irq_flags;
495 spin_lock_irqsave(&fman->lock, irq_flags);
496 __vmw_fences_update(fman);
497 spin_unlock_irqrestore(&fman->lock, irq_flags);
500 bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
502 struct vmw_fence_manager *fman = fman_from_fence(fence);
504 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
507 vmw_fences_update(fman);
509 return fence_is_signaled(&fence->base);
512 int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
513 bool interruptible, unsigned long timeout)
515 long ret = fence_wait_timeout(&fence->base, interruptible, timeout);
525 void vmw_fence_obj_flush(struct vmw_fence_obj *fence)
527 struct vmw_private *dev_priv = fman_from_fence(fence)->dev_priv;
529 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
532 static void vmw_fence_destroy(struct vmw_fence_obj *fence)
534 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 == NULL))
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);
566 struct vmw_fence_manager *fman = fman_from_fence(fence);
568 ttm_base_object_kfree(ufence, base);
570 * Free kernel space accounting.
572 ttm_mem_global_free(vmw_mem_glob(fman->dev_priv),
573 fman->user_fence_size);
576 static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
578 struct ttm_base_object *base = *p_base;
579 struct vmw_user_fence *ufence =
580 container_of(base, struct vmw_user_fence, base);
581 struct vmw_fence_obj *fence = &ufence->fence;
584 vmw_fence_obj_unreference(&fence);
587 int vmw_user_fence_create(struct drm_file *file_priv,
588 struct vmw_fence_manager *fman,
590 struct vmw_fence_obj **p_fence,
593 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
594 struct vmw_user_fence *ufence;
595 struct vmw_fence_obj *tmp;
596 struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
600 * Kernel memory space accounting, since this object may
601 * be created by a user-space request.
604 ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
606 if (unlikely(ret != 0))
609 ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
610 if (unlikely(ufence == NULL)) {
615 ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
616 vmw_user_fence_destroy);
617 if (unlikely(ret != 0)) {
623 * The base object holds a reference which is freed in
624 * vmw_user_fence_base_release.
626 tmp = vmw_fence_obj_reference(&ufence->fence);
627 ret = ttm_base_object_init(tfile, &ufence->base, false,
629 &vmw_user_fence_base_release, NULL);
632 if (unlikely(ret != 0)) {
634 * Free the base object's reference
636 vmw_fence_obj_unreference(&tmp);
640 *p_fence = &ufence->fence;
641 *p_handle = ufence->base.hash.key;
645 tmp = &ufence->fence;
646 vmw_fence_obj_unreference(&tmp);
648 ttm_mem_global_free(mem_glob, fman->user_fence_size);
654 * vmw_fence_fifo_down - signal all unsignaled fence objects.
657 void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
659 struct list_head action_list;
663 * The list may be altered while we traverse it, so always
664 * restart when we've released the fman->lock.
667 spin_lock_irq(&fman->lock);
668 fman->fifo_down = true;
669 while (!list_empty(&fman->fence_list)) {
670 struct vmw_fence_obj *fence =
671 list_entry(fman->fence_list.prev, struct vmw_fence_obj,
673 fence_get(&fence->base);
674 spin_unlock_irq(&fman->lock);
676 ret = vmw_fence_obj_wait(fence, false, false,
677 VMW_FENCE_WAIT_TIMEOUT);
679 if (unlikely(ret != 0)) {
680 list_del_init(&fence->head);
681 fence_signal(&fence->base);
682 INIT_LIST_HEAD(&action_list);
683 list_splice_init(&fence->seq_passed_actions,
685 vmw_fences_perform_actions(fman, &action_list);
688 BUG_ON(!list_empty(&fence->head));
689 fence_put(&fence->base);
690 spin_lock_irq(&fman->lock);
692 spin_unlock_irq(&fman->lock);
695 void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
697 unsigned long irq_flags;
699 spin_lock_irqsave(&fman->lock, irq_flags);
700 fman->fifo_down = false;
701 spin_unlock_irqrestore(&fman->lock, irq_flags);
705 int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
706 struct drm_file *file_priv)
708 struct drm_vmw_fence_wait_arg *arg =
709 (struct drm_vmw_fence_wait_arg *)data;
710 unsigned long timeout;
711 struct ttm_base_object *base;
712 struct vmw_fence_obj *fence;
713 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
715 uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
718 * 64-bit division not present on 32-bit systems, so do an
719 * approximation. (Divide by 1000000).
722 wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
723 (wait_timeout >> 26);
725 if (!arg->cookie_valid) {
726 arg->cookie_valid = 1;
727 arg->kernel_cookie = jiffies + wait_timeout;
730 base = ttm_base_object_lookup(tfile, arg->handle);
731 if (unlikely(base == NULL)) {
732 printk(KERN_ERR "Wait invalid fence object handle "
734 (unsigned long)arg->handle);
738 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
741 if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
742 ret = ((vmw_fence_obj_signaled(fence)) ?
747 timeout = (unsigned long)arg->kernel_cookie - timeout;
749 ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
752 ttm_base_object_unref(&base);
755 * Optionally unref the fence object.
758 if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
759 return ttm_ref_object_base_unref(tfile, arg->handle,
764 int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
765 struct drm_file *file_priv)
767 struct drm_vmw_fence_signaled_arg *arg =
768 (struct drm_vmw_fence_signaled_arg *) data;
769 struct ttm_base_object *base;
770 struct vmw_fence_obj *fence;
771 struct vmw_fence_manager *fman;
772 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
773 struct vmw_private *dev_priv = vmw_priv(dev);
775 base = ttm_base_object_lookup(tfile, arg->handle);
776 if (unlikely(base == NULL)) {
777 printk(KERN_ERR "Fence signaled invalid fence object handle "
779 (unsigned long)arg->handle);
783 fence = &(container_of(base, struct vmw_user_fence, base)->fence);
784 fman = fman_from_fence(fence);
786 arg->signaled = vmw_fence_obj_signaled(fence);
788 arg->signaled_flags = arg->flags;
789 spin_lock_irq(&fman->lock);
790 arg->passed_seqno = dev_priv->last_read_seqno;
791 spin_unlock_irq(&fman->lock);
793 ttm_base_object_unref(&base);
799 int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
800 struct drm_file *file_priv)
802 struct drm_vmw_fence_arg *arg =
803 (struct drm_vmw_fence_arg *) data;
805 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
811 * vmw_event_fence_fpriv_gone - Remove references to struct drm_file objects
813 * @fman: Pointer to a struct vmw_fence_manager
814 * @event_list: Pointer to linked list of struct vmw_event_fence_action objects
815 * with pointers to a struct drm_file object about to be closed.
817 * This function removes all pending fence events with references to a
818 * specific struct drm_file object about to be closed. The caller is required
819 * to pass a list of all struct vmw_event_fence_action objects with such
820 * events attached. This function is typically called before the
821 * struct drm_file object's event management is taken down.
823 void vmw_event_fence_fpriv_gone(struct vmw_fence_manager *fman,
824 struct list_head *event_list)
826 struct vmw_event_fence_action *eaction;
827 struct drm_pending_event *event;
828 unsigned long irq_flags;
831 spin_lock_irqsave(&fman->lock, irq_flags);
832 if (list_empty(event_list))
834 eaction = list_first_entry(event_list,
835 struct vmw_event_fence_action,
837 list_del_init(&eaction->fpriv_head);
838 event = eaction->event;
839 eaction->event = NULL;
840 spin_unlock_irqrestore(&fman->lock, irq_flags);
841 event->destroy(event);
844 spin_unlock_irqrestore(&fman->lock, irq_flags);
849 * vmw_event_fence_action_seq_passed
851 * @action: The struct vmw_fence_action embedded in a struct
852 * vmw_event_fence_action.
854 * This function is called when the seqno of the fence where @action is
855 * attached has passed. It queues the event on the submitter's event list.
856 * This function is always called from atomic context, and may be called
859 static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
861 struct vmw_event_fence_action *eaction =
862 container_of(action, struct vmw_event_fence_action, action);
863 struct drm_device *dev = eaction->dev;
864 struct drm_pending_event *event = eaction->event;
865 struct drm_file *file_priv;
866 unsigned long irq_flags;
868 if (unlikely(event == NULL))
871 file_priv = event->file_priv;
872 spin_lock_irqsave(&dev->event_lock, irq_flags);
874 if (likely(eaction->tv_sec != NULL)) {
877 do_gettimeofday(&tv);
878 *eaction->tv_sec = tv.tv_sec;
879 *eaction->tv_usec = tv.tv_usec;
882 list_del_init(&eaction->fpriv_head);
883 list_add_tail(&eaction->event->link, &file_priv->event_list);
884 eaction->event = NULL;
885 wake_up_all(&file_priv->event_wait);
886 spin_unlock_irqrestore(&dev->event_lock, irq_flags);
890 * vmw_event_fence_action_cleanup
892 * @action: The struct vmw_fence_action embedded in a struct
893 * vmw_event_fence_action.
895 * This function is the struct vmw_fence_action destructor. It's typically
896 * called from a workqueue.
898 static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
900 struct vmw_event_fence_action *eaction =
901 container_of(action, struct vmw_event_fence_action, action);
902 struct vmw_fence_manager *fman = fman_from_fence(eaction->fence);
903 unsigned long irq_flags;
905 spin_lock_irqsave(&fman->lock, irq_flags);
906 list_del(&eaction->fpriv_head);
907 spin_unlock_irqrestore(&fman->lock, irq_flags);
909 vmw_fence_obj_unreference(&eaction->fence);
915 * vmw_fence_obj_add_action - Add an action to a fence object.
917 * @fence - The fence object.
918 * @action - The action to add.
920 * Note that the action callbacks may be executed before this function
923 static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
924 struct vmw_fence_action *action)
926 struct vmw_fence_manager *fman = fman_from_fence(fence);
927 unsigned long irq_flags;
928 bool run_update = false;
930 mutex_lock(&fman->goal_irq_mutex);
931 spin_lock_irqsave(&fman->lock, irq_flags);
933 fman->pending_actions[action->type]++;
934 if (fence_is_signaled_locked(&fence->base)) {
935 struct list_head action_list;
937 INIT_LIST_HEAD(&action_list);
938 list_add_tail(&action->head, &action_list);
939 vmw_fences_perform_actions(fman, &action_list);
941 list_add_tail(&action->head, &fence->seq_passed_actions);
944 * This function may set fman::seqno_valid, so it must
945 * be run with the goal_irq_mutex held.
947 run_update = vmw_fence_goal_check_locked(fence);
950 spin_unlock_irqrestore(&fman->lock, irq_flags);
953 if (!fman->goal_irq_on) {
954 fman->goal_irq_on = true;
955 vmw_goal_waiter_add(fman->dev_priv);
957 vmw_fences_update(fman);
959 mutex_unlock(&fman->goal_irq_mutex);
964 * vmw_event_fence_action_create - Post an event for sending when a fence
965 * object seqno has passed.
967 * @file_priv: The file connection on which the event should be posted.
968 * @fence: The fence object on which to post the event.
969 * @event: Event to be posted. This event should've been alloced
970 * using k[mz]alloc, and should've been completely initialized.
971 * @interruptible: Interruptible waits if possible.
973 * As a side effect, the object pointed to by @event may have been
974 * freed when this function returns. If this function returns with
975 * an error code, the caller needs to free that object.
978 int vmw_event_fence_action_queue(struct drm_file *file_priv,
979 struct vmw_fence_obj *fence,
980 struct drm_pending_event *event,
985 struct vmw_event_fence_action *eaction;
986 struct vmw_fence_manager *fman = fman_from_fence(fence);
987 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
988 unsigned long irq_flags;
990 eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
991 if (unlikely(eaction == NULL))
994 eaction->event = event;
996 eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
997 eaction->action.cleanup = vmw_event_fence_action_cleanup;
998 eaction->action.type = VMW_ACTION_EVENT;
1000 eaction->fence = vmw_fence_obj_reference(fence);
1001 eaction->dev = fman->dev_priv->dev;
1002 eaction->tv_sec = tv_sec;
1003 eaction->tv_usec = tv_usec;
1005 spin_lock_irqsave(&fman->lock, irq_flags);
1006 list_add_tail(&eaction->fpriv_head, &vmw_fp->fence_events);
1007 spin_unlock_irqrestore(&fman->lock, irq_flags);
1009 vmw_fence_obj_add_action(fence, &eaction->action);
1014 struct vmw_event_fence_pending {
1015 struct drm_pending_event base;
1016 struct drm_vmw_event_fence event;
1019 static int vmw_event_fence_action_create(struct drm_file *file_priv,
1020 struct vmw_fence_obj *fence,
1025 struct vmw_event_fence_pending *event;
1026 struct vmw_fence_manager *fman = fman_from_fence(fence);
1027 struct drm_device *dev = fman->dev_priv->dev;
1028 unsigned long irq_flags;
1031 spin_lock_irqsave(&dev->event_lock, irq_flags);
1033 ret = (file_priv->event_space < sizeof(event->event)) ? -EBUSY : 0;
1034 if (likely(ret == 0))
1035 file_priv->event_space -= sizeof(event->event);
1037 spin_unlock_irqrestore(&dev->event_lock, irq_flags);
1039 if (unlikely(ret != 0)) {
1040 DRM_ERROR("Failed to allocate event space for this file.\n");
1045 event = kzalloc(sizeof(*event), GFP_KERNEL);
1046 if (unlikely(event == NULL)) {
1047 DRM_ERROR("Failed to allocate an event.\n");
1052 event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
1053 event->event.base.length = sizeof(*event);
1054 event->event.user_data = user_data;
1056 event->base.event = &event->event.base;
1057 event->base.file_priv = file_priv;
1058 event->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1061 if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
1062 ret = vmw_event_fence_action_queue(file_priv, fence,
1064 &event->event.tv_sec,
1065 &event->event.tv_usec,
1068 ret = vmw_event_fence_action_queue(file_priv, fence,
1079 event->base.destroy(&event->base);
1081 spin_lock_irqsave(&dev->event_lock, irq_flags);
1082 file_priv->event_space += sizeof(*event);
1083 spin_unlock_irqrestore(&dev->event_lock, irq_flags);
1088 int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
1089 struct drm_file *file_priv)
1091 struct vmw_private *dev_priv = vmw_priv(dev);
1092 struct drm_vmw_fence_event_arg *arg =
1093 (struct drm_vmw_fence_event_arg *) data;
1094 struct vmw_fence_obj *fence = NULL;
1095 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1096 struct drm_vmw_fence_rep __user *user_fence_rep =
1097 (struct drm_vmw_fence_rep __user *)(unsigned long)
1103 * Look up an existing fence object,
1104 * and if user-space wants a new reference,
1108 struct ttm_base_object *base =
1109 ttm_base_object_lookup_for_ref(dev_priv->tdev,
1112 if (unlikely(base == NULL)) {
1113 DRM_ERROR("Fence event invalid fence object handle "
1115 (unsigned long)arg->handle);
1118 fence = &(container_of(base, struct vmw_user_fence,
1120 (void) vmw_fence_obj_reference(fence);
1122 if (user_fence_rep != NULL) {
1125 ret = ttm_ref_object_add(vmw_fp->tfile, base,
1126 TTM_REF_USAGE, &existed);
1127 if (unlikely(ret != 0)) {
1128 DRM_ERROR("Failed to reference a fence "
1130 goto out_no_ref_obj;
1132 handle = base->hash.key;
1134 ttm_base_object_unref(&base);
1138 * Create a new fence object.
1141 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1145 if (unlikely(ret != 0)) {
1146 DRM_ERROR("Fence event failed to create fence.\n");
1151 BUG_ON(fence == NULL);
1153 ret = vmw_event_fence_action_create(file_priv, fence,
1157 if (unlikely(ret != 0)) {
1158 if (ret != -ERESTARTSYS)
1159 DRM_ERROR("Failed to attach event to fence.\n");
1163 vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
1165 vmw_fence_obj_unreference(&fence);
1168 if (user_fence_rep != NULL)
1169 ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
1170 handle, TTM_REF_USAGE);
1172 vmw_fence_obj_unreference(&fence);