source: src/linux/universal/linux-4.4/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c @ 31859

Last change on this file since 31859 was 31859, checked in by brainslayer, 2 months ago

kernel update

File size: 32.0 KB
Line 
1/**************************************************************************
2 *
3 * Copyright © 2011-2014 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include <drm/drmP.h>
29#include "vmwgfx_drv.h"
30
31#define VMW_FENCE_WRAP (1 << 31)
32
33struct vmw_fence_manager {
34        int num_fence_objects;
35        struct vmw_private *dev_priv;
36        spinlock_t lock;
37        struct list_head fence_list;
38        struct work_struct work;
39        u32 user_fence_size;
40        u32 fence_size;
41        u32 event_fence_action_size;
42        bool fifo_down;
43        struct list_head cleanup_list;
44        uint32_t pending_actions[VMW_ACTION_MAX];
45        struct mutex goal_irq_mutex;
46        bool goal_irq_on; /* Protected by @goal_irq_mutex */
47        bool seqno_valid; /* Protected by @lock, and may not be set to true
48                             without the @goal_irq_mutex held. */
49        unsigned ctx;
50};
51
52struct vmw_user_fence {
53        struct ttm_base_object base;
54        struct vmw_fence_obj fence;
55};
56
57/**
58 * struct vmw_event_fence_action - fence action that delivers a drm event.
59 *
60 * @e: A struct drm_pending_event that controls the event delivery.
61 * @action: A struct vmw_fence_action to hook up to a fence.
62 * @fence: A referenced pointer to the fence to keep it alive while @action
63 * hangs on it.
64 * @dev: Pointer to a struct drm_device so we can access the event stuff.
65 * @kref: Both @e and @action has destructors, so we need to refcount.
66 * @size: Size accounted for this object.
67 * @tv_sec: If non-null, the variable pointed to will be assigned
68 * current time tv_sec val when the fence signals.
69 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
70 * be assigned the current time tv_usec val when the fence signals.
71 */
72struct vmw_event_fence_action {
73        struct vmw_fence_action action;
74        struct list_head fpriv_head;
75
76        struct drm_pending_event *event;
77        struct vmw_fence_obj *fence;
78        struct drm_device *dev;
79
80        uint32_t *tv_sec;
81        uint32_t *tv_usec;
82};
83
84static struct vmw_fence_manager *
85fman_from_fence(struct vmw_fence_obj *fence)
86{
87        return container_of(fence->base.lock, struct vmw_fence_manager, lock);
88}
89
90/**
91 * Note on fencing subsystem usage of irqs:
92 * Typically the vmw_fences_update function is called
93 *
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
98 * away.
99 *
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.
107 *
108 * The fence goal seqno irq is on as long as there are unsignaled fence
109 * objects with actions attached to them.
110 */
111
112static void vmw_fence_obj_destroy(struct fence *f)
113{
114        struct vmw_fence_obj *fence =
115                container_of(f, struct vmw_fence_obj, base);
116
117        struct vmw_fence_manager *fman = fman_from_fence(fence);
118        unsigned long irq_flags;
119
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);
125}
126
127static const char *vmw_fence_get_driver_name(struct fence *f)
128{
129        return "vmwgfx";
130}
131
132static const char *vmw_fence_get_timeline_name(struct fence *f)
133{
134        return "svga";
135}
136
137static bool vmw_fence_enable_signaling(struct fence *f)
138{
139        struct vmw_fence_obj *fence =
140                container_of(f, struct vmw_fence_obj, base);
141
142        struct vmw_fence_manager *fman = fman_from_fence(fence);
143        struct vmw_private *dev_priv = fman->dev_priv;
144
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)
148                return false;
149
150        vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
151
152        return true;
153}
154
155struct vmwgfx_wait_cb {
156        struct fence_cb base;
157        struct task_struct *task;
158};
159
160static void
161vmwgfx_wait_cb(struct fence *fence, struct fence_cb *cb)
162{
163        struct vmwgfx_wait_cb *wait =
164                container_of(cb, struct vmwgfx_wait_cb, base);
165
166        wake_up_process(wait->task);
167}
168
169static void __vmw_fences_update(struct vmw_fence_manager *fman);
170
171static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
172{
173        struct vmw_fence_obj *fence =
174                container_of(f, struct vmw_fence_obj, base);
175
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;
179        long ret = timeout;
180        unsigned long irq_flags;
181
182        if (likely(vmw_fence_obj_signaled(fence)))
183                return timeout;
184
185        vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
186        vmw_seqno_waiter_add(dev_priv);
187
188        spin_lock_irqsave(f->lock, irq_flags);
189
190        if (intr && signal_pending(current)) {
191                ret = -ERESTARTSYS;
192                goto out;
193        }
194
195        cb.base.func = vmwgfx_wait_cb;
196        cb.task = current;
197        list_add(&cb.base.node, &f->cb_list);
198
199        while (ret > 0) {
200                __vmw_fences_update(fman);
201                if (test_bit(FENCE_FLAG_SIGNALED_BIT, &f->flags))
202                        break;
203
204                if (intr)
205                        __set_current_state(TASK_INTERRUPTIBLE);
206                else
207                        __set_current_state(TASK_UNINTERRUPTIBLE);
208                spin_unlock_irqrestore(f->lock, irq_flags);
209
210                ret = schedule_timeout(ret);
211
212                spin_lock_irqsave(f->lock, irq_flags);
213                if (ret > 0 && intr && signal_pending(current))
214                        ret = -ERESTARTSYS;
215        }
216
217        if (!list_empty(&cb.base.node))
218                list_del(&cb.base.node);
219        __set_current_state(TASK_RUNNING);
220
221out:
222        spin_unlock_irqrestore(f->lock, irq_flags);
223
224        vmw_seqno_waiter_remove(dev_priv);
225
226        return ret;
227}
228
229static 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,
235};
236
237
238/**
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.
242 */
243
244static void vmw_fence_work_func(struct work_struct *work)
245{
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;
250        bool seqno_valid;
251
252        do {
253                INIT_LIST_HEAD(&list);
254                mutex_lock(&fman->goal_irq_mutex);
255
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);
260
261                if (!seqno_valid && fman->goal_irq_on) {
262                        fman->goal_irq_on = false;
263                        vmw_goal_waiter_remove(fman->dev_priv);
264                }
265                mutex_unlock(&fman->goal_irq_mutex);
266
267                if (list_empty(&list))
268                        return;
269
270                /*
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.
274                 */
275
276                list_for_each_entry_safe(action, next_action, &list, head) {
277                        list_del_init(&action->head);
278                        if (action->cleanup)
279                                action->cleanup(action);
280                }
281        } while (1);
282}
283
284struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
285{
286        struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
287
288        if (unlikely(fman == NULL))
289                return NULL;
290
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);
303
304        return fman;
305}
306
307void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
308{
309        unsigned long irq_flags;
310        bool lists_empty;
311
312        (void) cancel_work_sync(&fman->work);
313
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);
318
319        BUG_ON(!lists_empty);
320        kfree(fman);
321}
322
323static 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))
326{
327        unsigned long irq_flags;
328        int ret = 0;
329
330        fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
331                   fman->ctx, seqno);
332        INIT_LIST_HEAD(&fence->seq_passed_actions);
333        fence->destroy = destroy;
334
335        spin_lock_irqsave(&fman->lock, irq_flags);
336        if (unlikely(fman->fifo_down)) {
337                ret = -EBUSY;
338                goto out_unlock;
339        }
340        list_add_tail(&fence->head, &fman->fence_list);
341        ++fman->num_fence_objects;
342
343out_unlock:
344        spin_unlock_irqrestore(&fman->lock, irq_flags);
345        return ret;
346
347}
348
349static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
350                                struct list_head *list)
351{
352        struct vmw_fence_action *action, *next_action;
353
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);
359
360                /*
361                 * Add the cleanup action to the cleanup list so that
362                 * it will be performed by a worker task.
363                 */
364
365                list_add_tail(&action->head, &fman->cleanup_list);
366        }
367}
368
369/**
370 * vmw_fence_goal_new_locked - Figure out a new device fence goal
371 * seqno if needed.
372 *
373 * @fman: Pointer to a fence manager.
374 * @passed_seqno: The seqno the device currently signals as passed.
375 *
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.
382 *
383 * returns true if the device goal seqno was updated. False otherwise.
384 */
385static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
386                                      u32 passed_seqno)
387{
388        u32 goal_seqno;
389        u32 *fifo_mem;
390        struct vmw_fence_obj *fence;
391
392        if (likely(!fman->seqno_valid))
393                return false;
394
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))
398                return false;
399
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);
406                        break;
407                }
408        }
409
410        return true;
411}
412
413
414/**
415 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
416 * needed.
417 *
418 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
419 * considered as a device fence goal.
420 *
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.
426 *
427 * returns true if the device goal seqno was updated. False otherwise.
428 */
429static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
430{
431        struct vmw_fence_manager *fman = fman_from_fence(fence);
432        u32 goal_seqno;
433        u32 *fifo_mem;
434
435        if (fence_is_signaled_locked(&fence->base))
436                return false;
437
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))
442                return false;
443
444        vmw_mmio_write(fence->base.seqno, fifo_mem + SVGA_FIFO_FENCE_GOAL);
445        fman->seqno_valid = true;
446
447        return true;
448}
449
450static void __vmw_fences_update(struct vmw_fence_manager *fman)
451{
452        struct vmw_fence_obj *fence, *next_fence;
453        struct list_head action_list;
454        bool needs_rerun;
455        uint32_t seqno, new_seqno;
456        u32 *fifo_mem = fman->dev_priv->mmio_virt;
457
458        seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
459rerun:
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,
466                                         &action_list);
467                        vmw_fences_perform_actions(fman, &action_list);
468                } else
469                        break;
470        }
471
472        /*
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.
476         */
477
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) {
482                        seqno = new_seqno;
483                        goto rerun;
484                }
485        }
486
487        if (!list_empty(&fman->cleanup_list))
488                (void) schedule_work(&fman->work);
489}
490
491void vmw_fences_update(struct vmw_fence_manager *fman)
492{
493        unsigned long irq_flags;
494
495        spin_lock_irqsave(&fman->lock, irq_flags);
496        __vmw_fences_update(fman);
497        spin_unlock_irqrestore(&fman->lock, irq_flags);
498}
499
500bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
501{
502        struct vmw_fence_manager *fman = fman_from_fence(fence);
503
504        if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
505                return 1;
506
507        vmw_fences_update(fman);
508
509        return fence_is_signaled(&fence->base);
510}
511
512int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
513                       bool interruptible, unsigned long timeout)
514{
515        long ret = fence_wait_timeout(&fence->base, interruptible, timeout);
516
517        if (likely(ret > 0))
518                return 0;
519        else if (ret == 0)
520                return -EBUSY;
521        else
522                return ret;
523}
524
525void vmw_fence_obj_flush(struct vmw_fence_obj *fence)
526{
527        struct vmw_private *dev_priv = fman_from_fence(fence)->dev_priv;
528
529        vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
530}
531
532static void vmw_fence_destroy(struct vmw_fence_obj *fence)
533{
534        fence_free(&fence->base);
535}
536
537int vmw_fence_create(struct vmw_fence_manager *fman,
538                     uint32_t seqno,
539                     struct vmw_fence_obj **p_fence)
540{
541        struct vmw_fence_obj *fence;
542        int ret;
543
544        fence = kzalloc(sizeof(*fence), GFP_KERNEL);
545        if (unlikely(fence == NULL))
546                return -ENOMEM;
547
548        ret = vmw_fence_obj_init(fman, fence, seqno,
549                                 vmw_fence_destroy);
550        if (unlikely(ret != 0))
551                goto out_err_init;
552
553        *p_fence = fence;
554        return 0;
555
556out_err_init:
557        kfree(fence);
558        return ret;
559}
560
561
562static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
563{
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);
567
568        ttm_base_object_kfree(ufence, base);
569        /*
570         * Free kernel space accounting.
571         */
572        ttm_mem_global_free(vmw_mem_glob(fman->dev_priv),
573                            fman->user_fence_size);
574}
575
576static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
577{
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;
582
583        *p_base = NULL;
584        vmw_fence_obj_unreference(&fence);
585}
586
587int vmw_user_fence_create(struct drm_file *file_priv,
588                          struct vmw_fence_manager *fman,
589                          uint32_t seqno,
590                          struct vmw_fence_obj **p_fence,
591                          uint32_t *p_handle)
592{
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);
597        int ret;
598
599        /*
600         * Kernel memory space accounting, since this object may
601         * be created by a user-space request.
602         */
603
604        ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
605                                   false, false);
606        if (unlikely(ret != 0))
607                return ret;
608
609        ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
610        if (unlikely(ufence == NULL)) {
611                ret = -ENOMEM;
612                goto out_no_object;
613        }
614
615        ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
616                                 vmw_user_fence_destroy);
617        if (unlikely(ret != 0)) {
618                kfree(ufence);
619                goto out_no_object;
620        }
621
622        /*
623         * The base object holds a reference which is freed in
624         * vmw_user_fence_base_release.
625         */
626        tmp = vmw_fence_obj_reference(&ufence->fence);
627        ret = ttm_base_object_init(tfile, &ufence->base, false,
628                                   VMW_RES_FENCE,
629                                   &vmw_user_fence_base_release, NULL);
630
631
632        if (unlikely(ret != 0)) {
633                /*
634                 * Free the base object's reference
635                 */
636                vmw_fence_obj_unreference(&tmp);
637                goto out_err;
638        }
639
640        *p_fence = &ufence->fence;
641        *p_handle = ufence->base.hash.key;
642
643        return 0;
644out_err:
645        tmp = &ufence->fence;
646        vmw_fence_obj_unreference(&tmp);
647out_no_object:
648        ttm_mem_global_free(mem_glob, fman->user_fence_size);
649        return ret;
650}
651
652
653/**
654 * vmw_fence_fifo_down - signal all unsignaled fence objects.
655 */
656
657void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
658{
659        struct list_head action_list;
660        int ret;
661
662        /*
663         * The list may be altered while we traverse it, so always
664         * restart when we've released the fman->lock.
665         */
666
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,
672                                   head);
673                fence_get(&fence->base);
674                spin_unlock_irq(&fman->lock);
675
676                ret = vmw_fence_obj_wait(fence, false, false,
677                                         VMW_FENCE_WAIT_TIMEOUT);
678
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,
684                                         &action_list);
685                        vmw_fences_perform_actions(fman, &action_list);
686                }
687
688                BUG_ON(!list_empty(&fence->head));
689                fence_put(&fence->base);
690                spin_lock_irq(&fman->lock);
691        }
692        spin_unlock_irq(&fman->lock);
693}
694
695void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
696{
697        unsigned long irq_flags;
698
699        spin_lock_irqsave(&fman->lock, irq_flags);
700        fman->fifo_down = false;
701        spin_unlock_irqrestore(&fman->lock, irq_flags);
702}
703
704
705/**
706 * vmw_fence_obj_lookup - Look up a user-space fence object
707 *
708 * @tfile: A struct ttm_object_file identifying the caller.
709 * @handle: A handle identifying the fence object.
710 * @return: A struct vmw_user_fence base ttm object on success or
711 * an error pointer on failure.
712 *
713 * The fence object is looked up and type-checked. The caller needs
714 * to have opened the fence object first, but since that happens on
715 * creation and fence objects aren't shareable, that's not an
716 * issue currently.
717 */
718static struct ttm_base_object *
719vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
720{
721        struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
722
723        if (!base) {
724                pr_err("Invalid fence object handle 0x%08lx.\n",
725                       (unsigned long)handle);
726                return ERR_PTR(-EINVAL);
727        }
728
729        if (base->refcount_release != vmw_user_fence_base_release) {
730                pr_err("Invalid fence object handle 0x%08lx.\n",
731                       (unsigned long)handle);
732                ttm_base_object_unref(&base);
733                return ERR_PTR(-EINVAL);
734        }
735
736        return base;
737}
738
739
740int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
741                             struct drm_file *file_priv)
742{
743        struct drm_vmw_fence_wait_arg *arg =
744            (struct drm_vmw_fence_wait_arg *)data;
745        unsigned long timeout;
746        struct ttm_base_object *base;
747        struct vmw_fence_obj *fence;
748        struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
749        int ret;
750        uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
751
752        /*
753         * 64-bit division not present on 32-bit systems, so do an
754         * approximation. (Divide by 1000000).
755         */
756
757        wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
758          (wait_timeout >> 26);
759
760        if (!arg->cookie_valid) {
761                arg->cookie_valid = 1;
762                arg->kernel_cookie = jiffies + wait_timeout;
763        }
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
771        timeout = jiffies;
772        if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
773                ret = ((vmw_fence_obj_signaled(fence)) ?
774                       0 : -EBUSY);
775                goto out;
776        }
777
778        timeout = (unsigned long)arg->kernel_cookie - timeout;
779
780        ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
781
782out:
783        ttm_base_object_unref(&base);
784
785        /*
786         * Optionally unref the fence object.
787         */
788
789        if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
790                return ttm_ref_object_base_unref(tfile, arg->handle,
791                                                 TTM_REF_USAGE);
792        return ret;
793}
794
795int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
796                                 struct drm_file *file_priv)
797{
798        struct drm_vmw_fence_signaled_arg *arg =
799                (struct drm_vmw_fence_signaled_arg *) data;
800        struct ttm_base_object *base;
801        struct vmw_fence_obj *fence;
802        struct vmw_fence_manager *fman;
803        struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
804        struct vmw_private *dev_priv = vmw_priv(dev);
805
806        base = vmw_fence_obj_lookup(tfile, arg->handle);
807        if (IS_ERR(base))
808                return PTR_ERR(base);
809
810        fence = &(container_of(base, struct vmw_user_fence, base)->fence);
811        fman = fman_from_fence(fence);
812
813        arg->signaled = vmw_fence_obj_signaled(fence);
814
815        arg->signaled_flags = arg->flags;
816        spin_lock_irq(&fman->lock);
817        arg->passed_seqno = dev_priv->last_read_seqno;
818        spin_unlock_irq(&fman->lock);
819
820        ttm_base_object_unref(&base);
821
822        return 0;
823}
824
825
826int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
827                              struct drm_file *file_priv)
828{
829        struct drm_vmw_fence_arg *arg =
830                (struct drm_vmw_fence_arg *) data;
831
832        return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
833                                         arg->handle,
834                                         TTM_REF_USAGE);
835}
836
837/**
838 * vmw_event_fence_fpriv_gone - Remove references to struct drm_file objects
839 *
840 * @fman: Pointer to a struct vmw_fence_manager
841 * @event_list: Pointer to linked list of struct vmw_event_fence_action objects
842 * with pointers to a struct drm_file object about to be closed.
843 *
844 * This function removes all pending fence events with references to a
845 * specific struct drm_file object about to be closed. The caller is required
846 * to pass a list of all struct vmw_event_fence_action objects with such
847 * events attached. This function is typically called before the
848 * struct drm_file object's event management is taken down.
849 */
850void vmw_event_fence_fpriv_gone(struct vmw_fence_manager *fman,
851                                struct list_head *event_list)
852{
853        struct vmw_event_fence_action *eaction;
854        struct drm_pending_event *event;
855        unsigned long irq_flags;
856
857        while (1) {
858                spin_lock_irqsave(&fman->lock, irq_flags);
859                if (list_empty(event_list))
860                        goto out_unlock;
861                eaction = list_first_entry(event_list,
862                                           struct vmw_event_fence_action,
863                                           fpriv_head);
864                list_del_init(&eaction->fpriv_head);
865                event = eaction->event;
866                eaction->event = NULL;
867                spin_unlock_irqrestore(&fman->lock, irq_flags);
868                event->destroy(event);
869        }
870out_unlock:
871        spin_unlock_irqrestore(&fman->lock, irq_flags);
872}
873
874
875/**
876 * vmw_event_fence_action_seq_passed
877 *
878 * @action: The struct vmw_fence_action embedded in a struct
879 * vmw_event_fence_action.
880 *
881 * This function is called when the seqno of the fence where @action is
882 * attached has passed. It queues the event on the submitter's event list.
883 * This function is always called from atomic context, and may be called
884 * from irq context.
885 */
886static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
887{
888        struct vmw_event_fence_action *eaction =
889                container_of(action, struct vmw_event_fence_action, action);
890        struct drm_device *dev = eaction->dev;
891        struct drm_pending_event *event = eaction->event;
892        struct drm_file *file_priv;
893        unsigned long irq_flags;
894
895        if (unlikely(event == NULL))
896                return;
897
898        file_priv = event->file_priv;
899        spin_lock_irqsave(&dev->event_lock, irq_flags);
900
901        if (likely(eaction->tv_sec != NULL)) {
902                struct timeval tv;
903
904                do_gettimeofday(&tv);
905                *eaction->tv_sec = tv.tv_sec;
906                *eaction->tv_usec = tv.tv_usec;
907        }
908
909        list_del_init(&eaction->fpriv_head);
910        list_add_tail(&eaction->event->link, &file_priv->event_list);
911        eaction->event = NULL;
912        wake_up_all(&file_priv->event_wait);
913        spin_unlock_irqrestore(&dev->event_lock, irq_flags);
914}
915
916/**
917 * vmw_event_fence_action_cleanup
918 *
919 * @action: The struct vmw_fence_action embedded in a struct
920 * vmw_event_fence_action.
921 *
922 * This function is the struct vmw_fence_action destructor. It's typically
923 * called from a workqueue.
924 */
925static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
926{
927        struct vmw_event_fence_action *eaction =
928                container_of(action, struct vmw_event_fence_action, action);
929        struct vmw_fence_manager *fman = fman_from_fence(eaction->fence);
930        unsigned long irq_flags;
931
932        spin_lock_irqsave(&fman->lock, irq_flags);
933        list_del(&eaction->fpriv_head);
934        spin_unlock_irqrestore(&fman->lock, irq_flags);
935
936        vmw_fence_obj_unreference(&eaction->fence);
937        kfree(eaction);
938}
939
940
941/**
942 * vmw_fence_obj_add_action - Add an action to a fence object.
943 *
944 * @fence - The fence object.
945 * @action - The action to add.
946 *
947 * Note that the action callbacks may be executed before this function
948 * returns.
949 */
950static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
951                              struct vmw_fence_action *action)
952{
953        struct vmw_fence_manager *fman = fman_from_fence(fence);
954        unsigned long irq_flags;
955        bool run_update = false;
956
957        mutex_lock(&fman->goal_irq_mutex);
958        spin_lock_irqsave(&fman->lock, irq_flags);
959
960        fman->pending_actions[action->type]++;
961        if (fence_is_signaled_locked(&fence->base)) {
962                struct list_head action_list;
963
964                INIT_LIST_HEAD(&action_list);
965                list_add_tail(&action->head, &action_list);
966                vmw_fences_perform_actions(fman, &action_list);
967        } else {
968                list_add_tail(&action->head, &fence->seq_passed_actions);
969
970                /*
971                 * This function may set fman::seqno_valid, so it must
972                 * be run with the goal_irq_mutex held.
973                 */
974                run_update = vmw_fence_goal_check_locked(fence);
975        }
976
977        spin_unlock_irqrestore(&fman->lock, irq_flags);
978
979        if (run_update) {
980                if (!fman->goal_irq_on) {
981                        fman->goal_irq_on = true;
982                        vmw_goal_waiter_add(fman->dev_priv);
983                }
984                vmw_fences_update(fman);
985        }
986        mutex_unlock(&fman->goal_irq_mutex);
987
988}
989
990/**
991 * vmw_event_fence_action_create - Post an event for sending when a fence
992 * object seqno has passed.
993 *
994 * @file_priv: The file connection on which the event should be posted.
995 * @fence: The fence object on which to post the event.
996 * @event: Event to be posted. This event should've been alloced
997 * using k[mz]alloc, and should've been completely initialized.
998 * @interruptible: Interruptible waits if possible.
999 *
1000 * As a side effect, the object pointed to by @event may have been
1001 * freed when this function returns. If this function returns with
1002 * an error code, the caller needs to free that object.
1003 */
1004
1005int vmw_event_fence_action_queue(struct drm_file *file_priv,
1006                                 struct vmw_fence_obj *fence,
1007                                 struct drm_pending_event *event,
1008                                 uint32_t *tv_sec,
1009                                 uint32_t *tv_usec,
1010                                 bool interruptible)
1011{
1012        struct vmw_event_fence_action *eaction;
1013        struct vmw_fence_manager *fman = fman_from_fence(fence);
1014        struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1015        unsigned long irq_flags;
1016
1017        eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
1018        if (unlikely(eaction == NULL))
1019                return -ENOMEM;
1020
1021        eaction->event = event;
1022
1023        eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
1024        eaction->action.cleanup = vmw_event_fence_action_cleanup;
1025        eaction->action.type = VMW_ACTION_EVENT;
1026
1027        eaction->fence = vmw_fence_obj_reference(fence);
1028        eaction->dev = fman->dev_priv->dev;
1029        eaction->tv_sec = tv_sec;
1030        eaction->tv_usec = tv_usec;
1031
1032        spin_lock_irqsave(&fman->lock, irq_flags);
1033        list_add_tail(&eaction->fpriv_head, &vmw_fp->fence_events);
1034        spin_unlock_irqrestore(&fman->lock, irq_flags);
1035
1036        vmw_fence_obj_add_action(fence, &eaction->action);
1037
1038        return 0;
1039}
1040
1041struct vmw_event_fence_pending {
1042        struct drm_pending_event base;
1043        struct drm_vmw_event_fence event;
1044};
1045
1046static int vmw_event_fence_action_create(struct drm_file *file_priv,
1047                                  struct vmw_fence_obj *fence,
1048                                  uint32_t flags,
1049                                  uint64_t user_data,
1050                                  bool interruptible)
1051{
1052        struct vmw_event_fence_pending *event;
1053        struct vmw_fence_manager *fman = fman_from_fence(fence);
1054        struct drm_device *dev = fman->dev_priv->dev;
1055        unsigned long irq_flags;
1056        int ret;
1057
1058        spin_lock_irqsave(&dev->event_lock, irq_flags);
1059
1060        ret = (file_priv->event_space < sizeof(event->event)) ? -EBUSY : 0;
1061        if (likely(ret == 0))
1062                file_priv->event_space -= sizeof(event->event);
1063
1064        spin_unlock_irqrestore(&dev->event_lock, irq_flags);
1065
1066        if (unlikely(ret != 0)) {
1067                DRM_ERROR("Failed to allocate event space for this file.\n");
1068                goto out_no_space;
1069        }
1070
1071
1072        event = kzalloc(sizeof(*event), GFP_KERNEL);
1073        if (unlikely(event == NULL)) {
1074                DRM_ERROR("Failed to allocate an event.\n");
1075                ret = -ENOMEM;
1076                goto out_no_event;
1077        }
1078
1079        event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
1080        event->event.base.length = sizeof(*event);
1081        event->event.user_data = user_data;
1082
1083        event->base.event = &event->event.base;
1084        event->base.file_priv = file_priv;
1085        event->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1086
1087
1088        if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
1089                ret = vmw_event_fence_action_queue(file_priv, fence,
1090                                                   &event->base,
1091                                                   &event->event.tv_sec,
1092                                                   &event->event.tv_usec,
1093                                                   interruptible);
1094        else
1095                ret = vmw_event_fence_action_queue(file_priv, fence,
1096                                                   &event->base,
1097                                                   NULL,
1098                                                   NULL,
1099                                                   interruptible);
1100        if (ret != 0)
1101                goto out_no_queue;
1102
1103        return 0;
1104
1105out_no_queue:
1106        event->base.destroy(&event->base);
1107out_no_event:
1108        spin_lock_irqsave(&dev->event_lock, irq_flags);
1109        file_priv->event_space += sizeof(*event);
1110        spin_unlock_irqrestore(&dev->event_lock, irq_flags);
1111out_no_space:
1112        return ret;
1113}
1114
1115int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
1116                          struct drm_file *file_priv)
1117{
1118        struct vmw_private *dev_priv = vmw_priv(dev);
1119        struct drm_vmw_fence_event_arg *arg =
1120                (struct drm_vmw_fence_event_arg *) data;
1121        struct vmw_fence_obj *fence = NULL;
1122        struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1123        struct ttm_object_file *tfile = vmw_fp->tfile;
1124        struct drm_vmw_fence_rep __user *user_fence_rep =
1125                (struct drm_vmw_fence_rep __user *)(unsigned long)
1126                arg->fence_rep;
1127        uint32_t handle;
1128        int ret;
1129
1130        /*
1131         * Look up an existing fence object,
1132         * and if user-space wants a new reference,
1133         * add one.
1134         */
1135        if (arg->handle) {
1136                struct ttm_base_object *base =
1137                        vmw_fence_obj_lookup(tfile, arg->handle);
1138
1139                if (IS_ERR(base))
1140                        return PTR_ERR(base);
1141
1142                fence = &(container_of(base, struct vmw_user_fence,
1143                                       base)->fence);
1144                (void) vmw_fence_obj_reference(fence);
1145
1146                if (user_fence_rep != NULL) {
1147                        ret = ttm_ref_object_add(vmw_fp->tfile, base,
1148                                                 TTM_REF_USAGE, NULL, false);
1149                        if (unlikely(ret != 0)) {
1150                                DRM_ERROR("Failed to reference a fence "
1151                                          "object.\n");
1152                                goto out_no_ref_obj;
1153                        }
1154                        handle = base->hash.key;
1155                }
1156                ttm_base_object_unref(&base);
1157        }
1158
1159        /*
1160         * Create a new fence object.
1161         */
1162        if (!fence) {
1163                ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1164                                                 &fence,
1165                                                 (user_fence_rep) ?
1166                                                 &handle : NULL);
1167                if (unlikely(ret != 0)) {
1168                        DRM_ERROR("Fence event failed to create fence.\n");
1169                        return ret;
1170                }
1171        }
1172
1173        BUG_ON(fence == NULL);
1174
1175        ret = vmw_event_fence_action_create(file_priv, fence,
1176                                            arg->flags,
1177                                            arg->user_data,
1178                                            true);
1179        if (unlikely(ret != 0)) {
1180                if (ret != -ERESTARTSYS)
1181                        DRM_ERROR("Failed to attach event to fence.\n");
1182                goto out_no_create;
1183        }
1184
1185        vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
1186                                    handle);
1187        vmw_fence_obj_unreference(&fence);
1188        return 0;
1189out_no_create:
1190        if (user_fence_rep != NULL)
1191                ttm_ref_object_base_unref(tfile, handle, TTM_REF_USAGE);
1192out_no_ref_obj:
1193        vmw_fence_obj_unreference(&fence);
1194        return ret;
1195}
Note: See TracBrowser for help on using the repository browser.