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
;
75 struct drm_pending_event
*event
;
76 struct vmw_fence_obj
*fence
;
77 struct drm_device
*dev
;
83 static struct vmw_fence_manager
*
84 fman_from_fence(struct vmw_fence_obj
*fence
)
86 return container_of(fence
->base
.lock
, struct vmw_fence_manager
, lock
);
90 * Note on fencing subsystem usage of irqs:
91 * Typically the vmw_fences_update function is called
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
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.
107 * The fence goal seqno irq is on as long as there are unsignaled fence
108 * objects with actions attached to them.
111 static void vmw_fence_obj_destroy(struct fence
*f
)
113 struct vmw_fence_obj
*fence
=
114 container_of(f
, struct vmw_fence_obj
, base
);
116 struct vmw_fence_manager
*fman
= fman_from_fence(fence
);
117 unsigned long irq_flags
;
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
);
126 static const char *vmw_fence_get_driver_name(struct fence
*f
)
131 static const char *vmw_fence_get_timeline_name(struct fence
*f
)
136 static bool vmw_fence_enable_signaling(struct fence
*f
)
138 struct vmw_fence_obj
*fence
=
139 container_of(f
, struct vmw_fence_obj
, base
);
141 struct vmw_fence_manager
*fman
= fman_from_fence(fence
);
142 struct vmw_private
*dev_priv
= fman
->dev_priv
;
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
)
149 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
154 struct vmwgfx_wait_cb
{
155 struct fence_cb base
;
156 struct task_struct
*task
;
160 vmwgfx_wait_cb(struct fence
*fence
, struct fence_cb
*cb
)
162 struct vmwgfx_wait_cb
*wait
=
163 container_of(cb
, struct vmwgfx_wait_cb
, base
);
165 wake_up_process(wait
->task
);
168 static void __vmw_fences_update(struct vmw_fence_manager
*fman
);
170 static long vmw_fence_wait(struct fence
*f
, bool intr
, signed long timeout
)
172 struct vmw_fence_obj
*fence
=
173 container_of(f
, struct vmw_fence_obj
, base
);
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
;
179 unsigned long irq_flags
;
181 if (likely(vmw_fence_obj_signaled(fence
)))
184 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
185 vmw_seqno_waiter_add(dev_priv
);
187 spin_lock_irqsave(f
->lock
, irq_flags
);
189 if (intr
&& signal_pending(current
)) {
194 cb
.base
.func
= vmwgfx_wait_cb
;
196 list_add(&cb
.base
.node
, &f
->cb_list
);
199 __vmw_fences_update(fman
);
200 if (test_bit(FENCE_FLAG_SIGNALED_BIT
, &f
->flags
))
204 __set_current_state(TASK_INTERRUPTIBLE
);
206 __set_current_state(TASK_UNINTERRUPTIBLE
);
207 spin_unlock_irqrestore(f
->lock
, irq_flags
);
209 ret
= schedule_timeout(ret
);
211 spin_lock_irqsave(f
->lock
, irq_flags
);
212 if (ret
> 0 && intr
&& signal_pending(current
))
216 if (!list_empty(&cb
.base
.node
))
217 list_del(&cb
.base
.node
);
218 __set_current_state(TASK_RUNNING
);
221 spin_unlock_irqrestore(f
->lock
, irq_flags
);
223 vmw_seqno_waiter_remove(dev_priv
);
228 static 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
,
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.
243 static void vmw_fence_work_func(struct work_struct
*work
)
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
;
252 INIT_LIST_HEAD(&list
);
253 mutex_lock(&fman
->goal_irq_mutex
);
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
);
260 if (!seqno_valid
&& fman
->goal_irq_on
) {
261 fman
->goal_irq_on
= false;
262 vmw_goal_waiter_remove(fman
->dev_priv
);
264 mutex_unlock(&fman
->goal_irq_mutex
);
266 if (list_empty(&list
))
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.
275 list_for_each_entry_safe(action
, next_action
, &list
, head
) {
276 list_del_init(&action
->head
);
278 action
->cleanup(action
);
283 struct vmw_fence_manager
*vmw_fence_manager_init(struct vmw_private
*dev_priv
)
285 struct vmw_fence_manager
*fman
= kzalloc(sizeof(*fman
), GFP_KERNEL
);
287 if (unlikely(fman
== NULL
))
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);
306 void vmw_fence_manager_takedown(struct vmw_fence_manager
*fman
)
308 unsigned long irq_flags
;
311 (void) cancel_work_sync(&fman
->work
);
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
);
318 BUG_ON(!lists_empty
);
322 static 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
))
326 unsigned long irq_flags
;
329 fence_init(&fence
->base
, &vmw_fence_ops
, &fman
->lock
,
331 INIT_LIST_HEAD(&fence
->seq_passed_actions
);
332 fence
->destroy
= destroy
;
334 spin_lock_irqsave(&fman
->lock
, irq_flags
);
335 if (unlikely(fman
->fifo_down
)) {
339 list_add_tail(&fence
->head
, &fman
->fence_list
);
340 ++fman
->num_fence_objects
;
343 spin_unlock_irqrestore(&fman
->lock
, irq_flags
);
348 static void vmw_fences_perform_actions(struct vmw_fence_manager
*fman
,
349 struct list_head
*list
)
351 struct vmw_fence_action
*action
, *next_action
;
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
);
360 * Add the cleanup action to the cleanup list so that
361 * it will be performed by a worker task.
364 list_add_tail(&action
->head
, &fman
->cleanup_list
);
369 * vmw_fence_goal_new_locked - Figure out a new device fence goal
372 * @fman: Pointer to a fence manager.
373 * @passed_seqno: The seqno the device currently signals as passed.
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.
382 * returns true if the device goal seqno was updated. False otherwise.
384 static bool vmw_fence_goal_new_locked(struct vmw_fence_manager
*fman
,
389 struct vmw_fence_obj
*fence
;
391 if (likely(!fman
->seqno_valid
))
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
))
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
);
414 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
417 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
418 * considered as a device fence goal.
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.
426 * returns true if the device goal seqno was updated. False otherwise.
428 static bool vmw_fence_goal_check_locked(struct vmw_fence_obj
*fence
)
430 struct vmw_fence_manager
*fman
= fman_from_fence(fence
);
434 if (fence_is_signaled_locked(&fence
->base
))
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
))
443 vmw_mmio_write(fence
->base
.seqno
, fifo_mem
+ SVGA_FIFO_FENCE_GOAL
);
444 fman
->seqno_valid
= true;
449 static void __vmw_fences_update(struct vmw_fence_manager
*fman
)
451 struct vmw_fence_obj
*fence
, *next_fence
;
452 struct list_head action_list
;
454 uint32_t seqno
, new_seqno
;
455 u32
*fifo_mem
= fman
->dev_priv
->mmio_virt
;
457 seqno
= vmw_mmio_read(fifo_mem
+ SVGA_FIFO_FENCE
);
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
,
466 vmw_fences_perform_actions(fman
, &action_list
);
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.
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
) {
486 if (!list_empty(&fman
->cleanup_list
))
487 (void) schedule_work(&fman
->work
);
490 void vmw_fences_update(struct vmw_fence_manager
*fman
)
492 unsigned long irq_flags
;
494 spin_lock_irqsave(&fman
->lock
, irq_flags
);
495 __vmw_fences_update(fman
);
496 spin_unlock_irqrestore(&fman
->lock
, irq_flags
);
499 bool vmw_fence_obj_signaled(struct vmw_fence_obj
*fence
)
501 struct vmw_fence_manager
*fman
= fman_from_fence(fence
);
503 if (test_bit(FENCE_FLAG_SIGNALED_BIT
, &fence
->base
.flags
))
506 vmw_fences_update(fman
);
508 return fence_is_signaled(&fence
->base
);
511 int vmw_fence_obj_wait(struct vmw_fence_obj
*fence
, bool lazy
,
512 bool interruptible
, unsigned long timeout
)
514 long ret
= fence_wait_timeout(&fence
->base
, interruptible
, timeout
);
524 void vmw_fence_obj_flush(struct vmw_fence_obj
*fence
)
526 struct vmw_private
*dev_priv
= fman_from_fence(fence
)->dev_priv
;
528 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
531 static void vmw_fence_destroy(struct vmw_fence_obj
*fence
)
533 fence_free(&fence
->base
);
536 int vmw_fence_create(struct vmw_fence_manager
*fman
,
538 struct vmw_fence_obj
**p_fence
)
540 struct vmw_fence_obj
*fence
;
543 fence
= kzalloc(sizeof(*fence
), GFP_KERNEL
);
544 if (unlikely(fence
== NULL
))
547 ret
= vmw_fence_obj_init(fman
, fence
, seqno
,
549 if (unlikely(ret
!= 0))
561 static void vmw_user_fence_destroy(struct vmw_fence_obj
*fence
)
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
);
567 ttm_base_object_kfree(ufence
, base
);
569 * Free kernel space accounting.
571 ttm_mem_global_free(vmw_mem_glob(fman
->dev_priv
),
572 fman
->user_fence_size
);
575 static void vmw_user_fence_base_release(struct ttm_base_object
**p_base
)
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
;
583 vmw_fence_obj_unreference(&fence
);
586 int vmw_user_fence_create(struct drm_file
*file_priv
,
587 struct vmw_fence_manager
*fman
,
589 struct vmw_fence_obj
**p_fence
,
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
);
599 * Kernel memory space accounting, since this object may
600 * be created by a user-space request.
603 ret
= ttm_mem_global_alloc(mem_glob
, fman
->user_fence_size
,
605 if (unlikely(ret
!= 0))
608 ufence
= kzalloc(sizeof(*ufence
), GFP_KERNEL
);
609 if (unlikely(ufence
== NULL
)) {
614 ret
= vmw_fence_obj_init(fman
, &ufence
->fence
, seqno
,
615 vmw_user_fence_destroy
);
616 if (unlikely(ret
!= 0)) {
622 * The base object holds a reference which is freed in
623 * vmw_user_fence_base_release.
625 tmp
= vmw_fence_obj_reference(&ufence
->fence
);
626 ret
= ttm_base_object_init(tfile
, &ufence
->base
, false,
628 &vmw_user_fence_base_release
, NULL
);
631 if (unlikely(ret
!= 0)) {
633 * Free the base object's reference
635 vmw_fence_obj_unreference(&tmp
);
639 *p_fence
= &ufence
->fence
;
640 *p_handle
= ufence
->base
.hash
.key
;
644 tmp
= &ufence
->fence
;
645 vmw_fence_obj_unreference(&tmp
);
647 ttm_mem_global_free(mem_glob
, fman
->user_fence_size
);
653 * vmw_fence_fifo_down - signal all unsignaled fence objects.
656 void vmw_fence_fifo_down(struct vmw_fence_manager
*fman
)
658 struct list_head action_list
;
662 * The list may be altered while we traverse it, so always
663 * restart when we've released the fman->lock.
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
,
672 fence_get(&fence
->base
);
673 spin_unlock_irq(&fman
->lock
);
675 ret
= vmw_fence_obj_wait(fence
, false, false,
676 VMW_FENCE_WAIT_TIMEOUT
);
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
,
684 vmw_fences_perform_actions(fman
, &action_list
);
687 BUG_ON(!list_empty(&fence
->head
));
688 fence_put(&fence
->base
);
689 spin_lock_irq(&fman
->lock
);
691 spin_unlock_irq(&fman
->lock
);
694 void vmw_fence_fifo_up(struct vmw_fence_manager
*fman
)
696 unsigned long irq_flags
;
698 spin_lock_irqsave(&fman
->lock
, irq_flags
);
699 fman
->fifo_down
= false;
700 spin_unlock_irqrestore(&fman
->lock
, irq_flags
);
704 int vmw_fence_obj_wait_ioctl(struct drm_device
*dev
, void *data
,
705 struct drm_file
*file_priv
)
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
;
714 uint64_t wait_timeout
= ((uint64_t)arg
->timeout_us
* HZ
);
717 * 64-bit division not present on 32-bit systems, so do an
718 * approximation. (Divide by 1000000).
721 wait_timeout
= (wait_timeout
>> 20) + (wait_timeout
>> 24) -
722 (wait_timeout
>> 26);
724 if (!arg
->cookie_valid
) {
725 arg
->cookie_valid
= 1;
726 arg
->kernel_cookie
= jiffies
+ wait_timeout
;
729 base
= ttm_base_object_lookup(tfile
, arg
->handle
);
730 if (unlikely(base
== NULL
)) {
731 printk(KERN_ERR
"Wait invalid fence object handle "
733 (unsigned long)arg
->handle
);
737 fence
= &(container_of(base
, struct vmw_user_fence
, base
)->fence
);
740 if (time_after_eq(timeout
, (unsigned long)arg
->kernel_cookie
)) {
741 ret
= ((vmw_fence_obj_signaled(fence
)) ?
746 timeout
= (unsigned long)arg
->kernel_cookie
- timeout
;
748 ret
= vmw_fence_obj_wait(fence
, arg
->lazy
, true, timeout
);
751 ttm_base_object_unref(&base
);
754 * Optionally unref the fence object.
757 if (ret
== 0 && (arg
->wait_options
& DRM_VMW_WAIT_OPTION_UNREF
))
758 return ttm_ref_object_base_unref(tfile
, arg
->handle
,
763 int vmw_fence_obj_signaled_ioctl(struct drm_device
*dev
, void *data
,
764 struct drm_file
*file_priv
)
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
);
774 base
= ttm_base_object_lookup(tfile
, arg
->handle
);
775 if (unlikely(base
== NULL
)) {
776 printk(KERN_ERR
"Fence signaled invalid fence object handle "
778 (unsigned long)arg
->handle
);
782 fence
= &(container_of(base
, struct vmw_user_fence
, base
)->fence
);
783 fman
= fman_from_fence(fence
);
785 arg
->signaled
= vmw_fence_obj_signaled(fence
);
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
);
792 ttm_base_object_unref(&base
);
798 int vmw_fence_obj_unref_ioctl(struct drm_device
*dev
, void *data
,
799 struct drm_file
*file_priv
)
801 struct drm_vmw_fence_arg
*arg
=
802 (struct drm_vmw_fence_arg
*) data
;
804 return ttm_ref_object_base_unref(vmw_fpriv(file_priv
)->tfile
,
810 * vmw_event_fence_action_seq_passed
812 * @action: The struct vmw_fence_action embedded in a struct
813 * vmw_event_fence_action.
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
820 static void vmw_event_fence_action_seq_passed(struct vmw_fence_action
*action
)
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
;
829 if (unlikely(event
== NULL
))
832 file_priv
= event
->file_priv
;
833 spin_lock_irqsave(&dev
->event_lock
, irq_flags
);
835 if (likely(eaction
->tv_sec
!= NULL
)) {
838 do_gettimeofday(&tv
);
839 *eaction
->tv_sec
= tv
.tv_sec
;
840 *eaction
->tv_usec
= tv
.tv_usec
;
843 drm_send_event_locked(dev
, eaction
->event
);
844 eaction
->event
= NULL
;
845 spin_unlock_irqrestore(&dev
->event_lock
, irq_flags
);
849 * vmw_event_fence_action_cleanup
851 * @action: The struct vmw_fence_action embedded in a struct
852 * vmw_event_fence_action.
854 * This function is the struct vmw_fence_action destructor. It's typically
855 * called from a workqueue.
857 static void vmw_event_fence_action_cleanup(struct vmw_fence_action
*action
)
859 struct vmw_event_fence_action
*eaction
=
860 container_of(action
, struct vmw_event_fence_action
, action
);
862 vmw_fence_obj_unreference(&eaction
->fence
);
868 * vmw_fence_obj_add_action - Add an action to a fence object.
870 * @fence - The fence object.
871 * @action - The action to add.
873 * Note that the action callbacks may be executed before this function
876 static void vmw_fence_obj_add_action(struct vmw_fence_obj
*fence
,
877 struct vmw_fence_action
*action
)
879 struct vmw_fence_manager
*fman
= fman_from_fence(fence
);
880 unsigned long irq_flags
;
881 bool run_update
= false;
883 mutex_lock(&fman
->goal_irq_mutex
);
884 spin_lock_irqsave(&fman
->lock
, irq_flags
);
886 fman
->pending_actions
[action
->type
]++;
887 if (fence_is_signaled_locked(&fence
->base
)) {
888 struct list_head action_list
;
890 INIT_LIST_HEAD(&action_list
);
891 list_add_tail(&action
->head
, &action_list
);
892 vmw_fences_perform_actions(fman
, &action_list
);
894 list_add_tail(&action
->head
, &fence
->seq_passed_actions
);
897 * This function may set fman::seqno_valid, so it must
898 * be run with the goal_irq_mutex held.
900 run_update
= vmw_fence_goal_check_locked(fence
);
903 spin_unlock_irqrestore(&fman
->lock
, irq_flags
);
906 if (!fman
->goal_irq_on
) {
907 fman
->goal_irq_on
= true;
908 vmw_goal_waiter_add(fman
->dev_priv
);
910 vmw_fences_update(fman
);
912 mutex_unlock(&fman
->goal_irq_mutex
);
917 * vmw_event_fence_action_create - Post an event for sending when a fence
918 * object seqno has passed.
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.
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.
931 int vmw_event_fence_action_queue(struct drm_file
*file_priv
,
932 struct vmw_fence_obj
*fence
,
933 struct drm_pending_event
*event
,
938 struct vmw_event_fence_action
*eaction
;
939 struct vmw_fence_manager
*fman
= fman_from_fence(fence
);
941 eaction
= kzalloc(sizeof(*eaction
), GFP_KERNEL
);
942 if (unlikely(eaction
== NULL
))
945 eaction
->event
= event
;
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
;
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
;
956 vmw_fence_obj_add_action(fence
, &eaction
->action
);
961 struct vmw_event_fence_pending
{
962 struct drm_pending_event base
;
963 struct drm_vmw_event_fence event
;
966 static int vmw_event_fence_action_create(struct drm_file
*file_priv
,
967 struct vmw_fence_obj
*fence
,
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
;
977 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
978 if (unlikely(event
== NULL
)) {
979 DRM_ERROR("Failed to allocate an event.\n");
984 event
->event
.base
.type
= DRM_VMW_EVENT_FENCE_SIGNALED
;
985 event
->event
.base
.length
= sizeof(*event
);
986 event
->event
.user_data
= user_data
;
988 ret
= drm_event_reserve_init(dev
, file_priv
, &event
->base
, &event
->event
.base
);
990 if (unlikely(ret
!= 0)) {
991 DRM_ERROR("Failed to allocate event space for this file.\n");
996 if (flags
& DRM_VMW_FE_FLAG_REQ_TIME
)
997 ret
= vmw_event_fence_action_queue(file_priv
, fence
,
999 &event
->event
.tv_sec
,
1000 &event
->event
.tv_usec
,
1003 ret
= vmw_event_fence_action_queue(file_priv
, fence
,
1014 drm_event_cancel_free(dev
, &event
->base
);
1019 int vmw_fence_event_ioctl(struct drm_device
*dev
, void *data
,
1020 struct drm_file
*file_priv
)
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)
1034 * Look up an existing fence object,
1035 * and if user-space wants a new reference,
1039 struct ttm_base_object
*base
=
1040 ttm_base_object_lookup_for_ref(dev_priv
->tdev
,
1043 if (unlikely(base
== NULL
)) {
1044 DRM_ERROR("Fence event invalid fence object handle "
1046 (unsigned long)arg
->handle
);
1049 fence
= &(container_of(base
, struct vmw_user_fence
,
1051 (void) vmw_fence_obj_reference(fence
);
1053 if (user_fence_rep
!= NULL
) {
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 "
1061 goto out_no_ref_obj
;
1063 handle
= base
->hash
.key
;
1065 ttm_base_object_unref(&base
);
1069 * Create a new fence object.
1072 ret
= vmw_execbuf_fence_commands(file_priv
, dev_priv
,
1076 if (unlikely(ret
!= 0)) {
1077 DRM_ERROR("Fence event failed to create fence.\n");
1082 BUG_ON(fence
== NULL
);
1084 ret
= vmw_event_fence_action_create(file_priv
, fence
,
1088 if (unlikely(ret
!= 0)) {
1089 if (ret
!= -ERESTARTSYS
)
1090 DRM_ERROR("Failed to attach event to fence.\n");
1094 vmw_execbuf_copy_fence_user(dev_priv
, vmw_fp
, 0, user_fence_rep
, fence
,
1096 vmw_fence_obj_unreference(&fence
);
1099 if (user_fence_rep
!= NULL
)
1100 ttm_ref_object_base_unref(vmw_fpriv(file_priv
)->tfile
,
1101 handle
, TTM_REF_USAGE
);
1103 vmw_fence_obj_unreference(&fence
);