1 /*
2  * Copyright © 2014 Broadcom
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /** @file vc4_fence.c
25  *
26  * Seqno-based fence management.
27  *
28  * We have two mechanisms for waiting in our kernel API: You can wait on a BO
29  * to have all rendering to from any process to be completed, or wait on a
30  * seqno for that particular seqno to be passed.  The fence API we're
31  * implementing is based on waiting for all rendering in the context to have
32  * completed (with no reference to what other processes might be doing with
33  * the same BOs), so we can just use the seqno of the last rendering we'd
34  * fired off as our fence marker.
35  */
36 
37 #include "util/u_inlines.h"
38 
39 #include "vc4_screen.h"
40 #include "vc4_bufmgr.h"
41 
42 struct vc4_fence {
43         struct pipe_reference reference;
44         uint64_t seqno;
45 };
46 
47 static void
48 vc4_fence_reference(struct pipe_screen *pscreen,
49                     struct pipe_fence_handle **pp,
50                     struct pipe_fence_handle *pf)
51 {
52         struct vc4_fence **p = (struct vc4_fence **)pp;
53         struct vc4_fence *f = (struct vc4_fence *)pf;
54         struct vc4_fence *old = *p;
55 
56         if (pipe_reference(&(*p)->reference, &f->reference)) {
57                 free(old);
58         }
59         *p = f;
60 }
61 
62 static boolean
63 vc4_fence_finish(struct pipe_screen *pscreen,
64 		 struct pipe_context *ctx,
65                  struct pipe_fence_handle *pf,
66                  uint64_t timeout_ns)
67 {
68         struct vc4_screen *screen = vc4_screen(pscreen);
69         struct vc4_fence *f = (struct vc4_fence *)pf;
70 
71         return vc4_wait_seqno(screen, f->seqno, timeout_ns, "fence wait");
72 }
73 
74 struct vc4_fence *
75 vc4_fence_create(struct vc4_screen *screen, uint64_t seqno)
76 {
77         struct vc4_fence *f = calloc(1, sizeof(*f));
78 
79         if (!f)
80                 return NULL;
81 
82         pipe_reference_init(&f->reference, 1);
83         f->seqno = seqno;
84 
85         return f;
86 }
87 
88 void
89 vc4_fence_init(struct vc4_screen *screen)
90 {
91         screen->base.fence_reference = vc4_fence_reference;
92         screen->base.fence_finish = vc4_fence_finish;
93 }
94