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