1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2009-2015 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci#include <libsync.h>
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/u_memory.h"
28bf215546Sopenharmony_ci#include "util/u_atomic.h"
29bf215546Sopenharmony_ci#include "util/list.h"
30bf215546Sopenharmony_ci#include "os/os_thread.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "pipebuffer/pb_buffer_fenced.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "vmw_screen.h"
35bf215546Sopenharmony_ci#include "vmw_fence.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct vmw_fence_ops
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   /*
40bf215546Sopenharmony_ci    * Immutable members.
41bf215546Sopenharmony_ci    */
42bf215546Sopenharmony_ci   struct pb_fence_ops base;
43bf215546Sopenharmony_ci   struct vmw_winsys_screen *vws;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   mtx_t mutex;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   /*
48bf215546Sopenharmony_ci    * Protected by mutex;
49bf215546Sopenharmony_ci    */
50bf215546Sopenharmony_ci   struct list_head not_signaled;
51bf215546Sopenharmony_ci   uint32_t last_signaled;
52bf215546Sopenharmony_ci   uint32_t last_emitted;
53bf215546Sopenharmony_ci};
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cistruct vmw_fence
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   struct list_head ops_list;
58bf215546Sopenharmony_ci   int32_t refcount;
59bf215546Sopenharmony_ci   uint32_t handle;
60bf215546Sopenharmony_ci   uint32_t mask;
61bf215546Sopenharmony_ci   int32_t signalled;
62bf215546Sopenharmony_ci   uint32_t seqno;
63bf215546Sopenharmony_ci   int32_t fence_fd;
64bf215546Sopenharmony_ci   boolean imported; /* TRUE if imported from another process */
65bf215546Sopenharmony_ci};
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci/**
68bf215546Sopenharmony_ci * vmw_fence_seq_is_signaled - Check whether a fence seqno is
69bf215546Sopenharmony_ci * signaled.
70bf215546Sopenharmony_ci *
71bf215546Sopenharmony_ci * @ops: Pointer to a struct pb_fence_ops.
72bf215546Sopenharmony_ci *
73bf215546Sopenharmony_ci */
74bf215546Sopenharmony_cistatic inline boolean
75bf215546Sopenharmony_civmw_fence_seq_is_signaled(uint32_t seq, uint32_t last, uint32_t cur)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci   return (cur - last <= cur - seq);
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci/**
82bf215546Sopenharmony_ci * vmw_fence_ops - Return the vmw_fence_ops structure backing a
83bf215546Sopenharmony_ci * struct pb_fence_ops pointer.
84bf215546Sopenharmony_ci *
85bf215546Sopenharmony_ci * @ops: Pointer to a struct pb_fence_ops.
86bf215546Sopenharmony_ci *
87bf215546Sopenharmony_ci */
88bf215546Sopenharmony_cistatic inline struct vmw_fence_ops *
89bf215546Sopenharmony_civmw_fence_ops(struct pb_fence_ops *ops)
90bf215546Sopenharmony_ci{
91bf215546Sopenharmony_ci   assert(ops);
92bf215546Sopenharmony_ci   return (struct vmw_fence_ops *)ops;
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci/**
97bf215546Sopenharmony_ci * vmw_fences_release - Release all fences from the not_signaled
98bf215546Sopenharmony_ci * list.
99bf215546Sopenharmony_ci *
100bf215546Sopenharmony_ci * @ops: Pointer to a struct vmw_fence_ops.
101bf215546Sopenharmony_ci *
102bf215546Sopenharmony_ci */
103bf215546Sopenharmony_cistatic void
104bf215546Sopenharmony_civmw_fences_release(struct vmw_fence_ops *ops)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   struct vmw_fence *fence, *n;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   mtx_lock(&ops->mutex);
109bf215546Sopenharmony_ci   LIST_FOR_EACH_ENTRY_SAFE(fence, n, &ops->not_signaled, ops_list)
110bf215546Sopenharmony_ci      list_delinit(&fence->ops_list);
111bf215546Sopenharmony_ci   mtx_unlock(&ops->mutex);
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci/**
115bf215546Sopenharmony_ci * vmw_fences_signal - Traverse the not_signaled list and try to
116bf215546Sopenharmony_ci * signal unsignaled fences.
117bf215546Sopenharmony_ci *
118bf215546Sopenharmony_ci * @ops: Pointer to a struct pb_fence_ops.
119bf215546Sopenharmony_ci * @signaled: Seqno that has signaled.
120bf215546Sopenharmony_ci * @emitted: Last seqno emitted by the kernel.
121bf215546Sopenharmony_ci * @has_emitted: Whether we provide the emitted value.
122bf215546Sopenharmony_ci *
123bf215546Sopenharmony_ci */
124bf215546Sopenharmony_civoid
125bf215546Sopenharmony_civmw_fences_signal(struct pb_fence_ops *fence_ops,
126bf215546Sopenharmony_ci                  uint32_t signaled,
127bf215546Sopenharmony_ci                  uint32_t emitted,
128bf215546Sopenharmony_ci                  boolean has_emitted)
129bf215546Sopenharmony_ci{
130bf215546Sopenharmony_ci   struct vmw_fence_ops *ops = NULL;
131bf215546Sopenharmony_ci   struct vmw_fence *fence, *n;
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   if (fence_ops == NULL)
134bf215546Sopenharmony_ci      return;
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   ops = vmw_fence_ops(fence_ops);
137bf215546Sopenharmony_ci   mtx_lock(&ops->mutex);
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   if (!has_emitted) {
140bf215546Sopenharmony_ci      emitted = ops->last_emitted;
141bf215546Sopenharmony_ci      if (emitted - signaled > (1 << 30))
142bf215546Sopenharmony_ci	emitted = signaled;
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   if (signaled == ops->last_signaled && emitted == ops->last_emitted)
146bf215546Sopenharmony_ci      goto out_unlock;
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   LIST_FOR_EACH_ENTRY_SAFE(fence, n, &ops->not_signaled, ops_list) {
149bf215546Sopenharmony_ci      if (!vmw_fence_seq_is_signaled(fence->seqno, signaled, emitted))
150bf215546Sopenharmony_ci         break;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci      p_atomic_set(&fence->signalled, 1);
153bf215546Sopenharmony_ci      list_delinit(&fence->ops_list);
154bf215546Sopenharmony_ci   }
155bf215546Sopenharmony_ci   ops->last_signaled = signaled;
156bf215546Sopenharmony_ci   ops->last_emitted = emitted;
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ciout_unlock:
159bf215546Sopenharmony_ci   mtx_unlock(&ops->mutex);
160bf215546Sopenharmony_ci}
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci/**
164bf215546Sopenharmony_ci * vmw_fence - return the vmw_fence object identified by a
165bf215546Sopenharmony_ci * struct pipe_fence_handle *
166bf215546Sopenharmony_ci *
167bf215546Sopenharmony_ci * @fence: The opaque pipe fence handle.
168bf215546Sopenharmony_ci */
169bf215546Sopenharmony_cistatic inline struct vmw_fence *
170bf215546Sopenharmony_civmw_fence(struct pipe_fence_handle *fence)
171bf215546Sopenharmony_ci{
172bf215546Sopenharmony_ci   return (struct vmw_fence *) fence;
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci/**
177bf215546Sopenharmony_ci * vmw_fence_create - Create a user-space fence object.
178bf215546Sopenharmony_ci *
179bf215546Sopenharmony_ci * @fence_ops: The fence_ops manager to register with.
180bf215546Sopenharmony_ci * @handle: Handle identifying the kernel fence object.
181bf215546Sopenharmony_ci * @mask: Mask of flags that this fence object may signal.
182bf215546Sopenharmony_ci * @fd: File descriptor to associate with the fence
183bf215546Sopenharmony_ci *
184bf215546Sopenharmony_ci * Returns NULL on failure.
185bf215546Sopenharmony_ci */
186bf215546Sopenharmony_cistruct pipe_fence_handle *
187bf215546Sopenharmony_civmw_fence_create(struct pb_fence_ops *fence_ops, uint32_t handle,
188bf215546Sopenharmony_ci                 uint32_t seqno, uint32_t mask, int32_t fd)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   struct vmw_fence *fence = CALLOC_STRUCT(vmw_fence);
191bf215546Sopenharmony_ci   struct vmw_fence_ops *ops = NULL;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   if (!fence)
194bf215546Sopenharmony_ci      return NULL;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   p_atomic_set(&fence->refcount, 1);
197bf215546Sopenharmony_ci   fence->handle = handle;
198bf215546Sopenharmony_ci   fence->mask = mask;
199bf215546Sopenharmony_ci   fence->seqno = seqno;
200bf215546Sopenharmony_ci   fence->fence_fd = fd;
201bf215546Sopenharmony_ci   p_atomic_set(&fence->signalled, 0);
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci   /*
204bf215546Sopenharmony_ci    * If the fence was not created by our device, then we won't
205bf215546Sopenharmony_ci    * manage it with our ops
206bf215546Sopenharmony_ci    */
207bf215546Sopenharmony_ci   if (!fence_ops) {
208bf215546Sopenharmony_ci      fence->imported = true;
209bf215546Sopenharmony_ci      return (struct pipe_fence_handle *) fence;
210bf215546Sopenharmony_ci   }
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   ops = vmw_fence_ops(fence_ops);
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   mtx_lock(&ops->mutex);
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   if (vmw_fence_seq_is_signaled(seqno, ops->last_signaled, seqno)) {
217bf215546Sopenharmony_ci      p_atomic_set(&fence->signalled, 1);
218bf215546Sopenharmony_ci      list_inithead(&fence->ops_list);
219bf215546Sopenharmony_ci   } else {
220bf215546Sopenharmony_ci      p_atomic_set(&fence->signalled, 0);
221bf215546Sopenharmony_ci      list_addtail(&fence->ops_list, &ops->not_signaled);
222bf215546Sopenharmony_ci   }
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci   mtx_unlock(&ops->mutex);
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci   return (struct pipe_fence_handle *) fence;
227bf215546Sopenharmony_ci}
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci/**
231bf215546Sopenharmony_ci * vmw_fence_destroy - Frees a vmw fence object.
232bf215546Sopenharmony_ci *
233bf215546Sopenharmony_ci * Also closes the file handle associated with the object, if any
234bf215546Sopenharmony_ci */
235bf215546Sopenharmony_cistatic
236bf215546Sopenharmony_civoid vmw_fence_destroy(struct vmw_fence *vfence)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci   if (vfence->fence_fd != -1)
239bf215546Sopenharmony_ci      close(vfence->fence_fd);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   FREE(vfence);
242bf215546Sopenharmony_ci}
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci/**
246bf215546Sopenharmony_ci * vmw_fence_reference - Reference / unreference a vmw fence object.
247bf215546Sopenharmony_ci *
248bf215546Sopenharmony_ci * @vws: Pointer to the winsys screen.
249bf215546Sopenharmony_ci * @ptr: Pointer to reference transfer destination.
250bf215546Sopenharmony_ci * @fence: Pointer to object to reference. May be NULL.
251bf215546Sopenharmony_ci */
252bf215546Sopenharmony_civoid
253bf215546Sopenharmony_civmw_fence_reference(struct vmw_winsys_screen *vws,
254bf215546Sopenharmony_ci		    struct pipe_fence_handle **ptr,
255bf215546Sopenharmony_ci		    struct pipe_fence_handle *fence)
256bf215546Sopenharmony_ci{
257bf215546Sopenharmony_ci   if (*ptr) {
258bf215546Sopenharmony_ci      struct vmw_fence *vfence = vmw_fence(*ptr);
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci      if (p_atomic_dec_zero(&vfence->refcount)) {
261bf215546Sopenharmony_ci         struct vmw_fence_ops *ops = vmw_fence_ops(vws->fence_ops);
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci         if (!vfence->imported) {
264bf215546Sopenharmony_ci            vmw_ioctl_fence_unref(vws, vfence->handle);
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci            mtx_lock(&ops->mutex);
267bf215546Sopenharmony_ci            list_delinit(&vfence->ops_list);
268bf215546Sopenharmony_ci            mtx_unlock(&ops->mutex);
269bf215546Sopenharmony_ci         }
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci         vmw_fence_destroy(vfence);
272bf215546Sopenharmony_ci      }
273bf215546Sopenharmony_ci   }
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci   if (fence) {
276bf215546Sopenharmony_ci      struct vmw_fence *vfence = vmw_fence(fence);
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci      p_atomic_inc(&vfence->refcount);
279bf215546Sopenharmony_ci   }
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   *ptr = fence;
282bf215546Sopenharmony_ci}
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci/**
286bf215546Sopenharmony_ci * vmw_fence_signalled - Check whether a fence object is signalled.
287bf215546Sopenharmony_ci *
288bf215546Sopenharmony_ci * @vws: Pointer to the winsys screen.
289bf215546Sopenharmony_ci * @fence: Handle to the fence object.
290bf215546Sopenharmony_ci * @flag: Fence flags to check. If the fence object can't signal
291bf215546Sopenharmony_ci * a flag, it is assumed to be already signaled.
292bf215546Sopenharmony_ci *
293bf215546Sopenharmony_ci * Returns 0 if the fence object was signaled, nonzero otherwise.
294bf215546Sopenharmony_ci */
295bf215546Sopenharmony_ciint
296bf215546Sopenharmony_civmw_fence_signalled(struct vmw_winsys_screen *vws,
297bf215546Sopenharmony_ci		   struct pipe_fence_handle *fence,
298bf215546Sopenharmony_ci		   unsigned flag)
299bf215546Sopenharmony_ci{
300bf215546Sopenharmony_ci   struct vmw_fence *vfence;
301bf215546Sopenharmony_ci   int32_t vflags = SVGA_FENCE_FLAG_EXEC;
302bf215546Sopenharmony_ci   int ret;
303bf215546Sopenharmony_ci   uint32_t old;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   if (!fence)
306bf215546Sopenharmony_ci      return 0;
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci   vfence = vmw_fence(fence);
309bf215546Sopenharmony_ci   old = p_atomic_read(&vfence->signalled);
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   vflags &= ~vfence->mask;
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   if ((old & vflags) == vflags)
314bf215546Sopenharmony_ci      return 0;
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci   /*
317bf215546Sopenharmony_ci    * Currently we update signaled fences on each execbuf call.
318bf215546Sopenharmony_ci    * That should really be sufficient, and we can avoid
319bf215546Sopenharmony_ci    * a lot of kernel calls this way.
320bf215546Sopenharmony_ci    */
321bf215546Sopenharmony_ci#if 1
322bf215546Sopenharmony_ci   ret = vmw_ioctl_fence_signalled(vws, vfence->handle, vflags);
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   if (ret == 0)
325bf215546Sopenharmony_ci      p_atomic_set(&vfence->signalled, 1);
326bf215546Sopenharmony_ci   return ret;
327bf215546Sopenharmony_ci#else
328bf215546Sopenharmony_ci   (void) ret;
329bf215546Sopenharmony_ci   return -1;
330bf215546Sopenharmony_ci#endif
331bf215546Sopenharmony_ci}
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci/**
334bf215546Sopenharmony_ci * vmw_fence_finish - Wait for a fence object to signal.
335bf215546Sopenharmony_ci *
336bf215546Sopenharmony_ci * @vws: Pointer to the winsys screen.
337bf215546Sopenharmony_ci * @fence: Handle to the fence object.
338bf215546Sopenharmony_ci * @timeout: How long to wait before timing out.
339bf215546Sopenharmony_ci * @flag: Fence flags to wait for. If the fence object can't signal
340bf215546Sopenharmony_ci * a flag, it is assumed to be already signaled.
341bf215546Sopenharmony_ci *
342bf215546Sopenharmony_ci * Returns 0 if the wait succeeded. Nonzero otherwise.
343bf215546Sopenharmony_ci */
344bf215546Sopenharmony_ciint
345bf215546Sopenharmony_civmw_fence_finish(struct vmw_winsys_screen *vws,
346bf215546Sopenharmony_ci		 struct pipe_fence_handle *fence,
347bf215546Sopenharmony_ci		 uint64_t timeout,
348bf215546Sopenharmony_ci		 unsigned flag)
349bf215546Sopenharmony_ci{
350bf215546Sopenharmony_ci   struct vmw_fence *vfence;
351bf215546Sopenharmony_ci   int32_t vflags = SVGA_FENCE_FLAG_EXEC;
352bf215546Sopenharmony_ci   int ret;
353bf215546Sopenharmony_ci   uint32_t old;
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   if (!fence)
356bf215546Sopenharmony_ci      return 0;
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci   vfence = vmw_fence(fence);
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci   if (vfence->imported) {
361bf215546Sopenharmony_ci      ret = sync_wait(vfence->fence_fd, timeout / 1000000);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci      if (!ret)
364bf215546Sopenharmony_ci         p_atomic_set(&vfence->signalled, 1);
365bf215546Sopenharmony_ci
366bf215546Sopenharmony_ci      return !!ret;
367bf215546Sopenharmony_ci   }
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   old = p_atomic_read(&vfence->signalled);
370bf215546Sopenharmony_ci   vflags &= ~vfence->mask;
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci   if ((old & vflags) == vflags)
373bf215546Sopenharmony_ci      return 0;
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci   ret = vmw_ioctl_fence_finish(vws, vfence->handle, vflags);
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci   if (ret == 0) {
378bf215546Sopenharmony_ci      int32_t prev = old;
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci      do {
381bf215546Sopenharmony_ci	 old = prev;
382bf215546Sopenharmony_ci	 prev = p_atomic_cmpxchg(&vfence->signalled, old, old | vflags);
383bf215546Sopenharmony_ci      } while (prev != old);
384bf215546Sopenharmony_ci   }
385bf215546Sopenharmony_ci
386bf215546Sopenharmony_ci   return ret;
387bf215546Sopenharmony_ci}
388bf215546Sopenharmony_ci
389bf215546Sopenharmony_ci/**
390bf215546Sopenharmony_ci * vmw_fence_get_fd
391bf215546Sopenharmony_ci *
392bf215546Sopenharmony_ci * Returns the file descriptor associated with the fence
393bf215546Sopenharmony_ci */
394bf215546Sopenharmony_ciint
395bf215546Sopenharmony_civmw_fence_get_fd(struct pipe_fence_handle *fence)
396bf215546Sopenharmony_ci{
397bf215546Sopenharmony_ci   struct vmw_fence *vfence;
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci   if (!fence)
400bf215546Sopenharmony_ci      return -1;
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_ci   vfence = vmw_fence(fence);
403bf215546Sopenharmony_ci   return vfence->fence_fd;
404bf215546Sopenharmony_ci}
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci/**
408bf215546Sopenharmony_ci * vmw_fence_ops_fence_reference - wrapper for the pb_fence_ops api.
409bf215546Sopenharmony_ci *
410bf215546Sopenharmony_ci * wrapper around vmw_fence_reference.
411bf215546Sopenharmony_ci */
412bf215546Sopenharmony_cistatic void
413bf215546Sopenharmony_civmw_fence_ops_fence_reference(struct pb_fence_ops *ops,
414bf215546Sopenharmony_ci                              struct pipe_fence_handle **ptr,
415bf215546Sopenharmony_ci                              struct pipe_fence_handle *fence)
416bf215546Sopenharmony_ci{
417bf215546Sopenharmony_ci   struct vmw_winsys_screen *vws = vmw_fence_ops(ops)->vws;
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci   vmw_fence_reference(vws, ptr, fence);
420bf215546Sopenharmony_ci}
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci/**
423bf215546Sopenharmony_ci * vmw_fence_ops_fence_signalled - wrapper for the pb_fence_ops api.
424bf215546Sopenharmony_ci *
425bf215546Sopenharmony_ci * wrapper around vmw_fence_signalled.
426bf215546Sopenharmony_ci */
427bf215546Sopenharmony_cistatic int
428bf215546Sopenharmony_civmw_fence_ops_fence_signalled(struct pb_fence_ops *ops,
429bf215546Sopenharmony_ci                              struct pipe_fence_handle *fence,
430bf215546Sopenharmony_ci                              unsigned flag)
431bf215546Sopenharmony_ci{
432bf215546Sopenharmony_ci   struct vmw_winsys_screen *vws = vmw_fence_ops(ops)->vws;
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci   return vmw_fence_signalled(vws, fence, flag);
435bf215546Sopenharmony_ci}
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci/**
439bf215546Sopenharmony_ci * vmw_fence_ops_fence_finish - wrapper for the pb_fence_ops api.
440bf215546Sopenharmony_ci *
441bf215546Sopenharmony_ci * wrapper around vmw_fence_finish.
442bf215546Sopenharmony_ci */
443bf215546Sopenharmony_cistatic int
444bf215546Sopenharmony_civmw_fence_ops_fence_finish(struct pb_fence_ops *ops,
445bf215546Sopenharmony_ci                           struct pipe_fence_handle *fence,
446bf215546Sopenharmony_ci                           unsigned flag)
447bf215546Sopenharmony_ci{
448bf215546Sopenharmony_ci   struct vmw_winsys_screen *vws = vmw_fence_ops(ops)->vws;
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci   return vmw_fence_finish(vws, fence, PIPE_TIMEOUT_INFINITE, flag);
451bf215546Sopenharmony_ci}
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci/**
455bf215546Sopenharmony_ci * vmw_fence_ops_destroy - Destroy a pb_fence_ops function table.
456bf215546Sopenharmony_ci *
457bf215546Sopenharmony_ci * @ops: The function table to destroy.
458bf215546Sopenharmony_ci *
459bf215546Sopenharmony_ci * Part of the pb_fence_ops api.
460bf215546Sopenharmony_ci */
461bf215546Sopenharmony_cistatic void
462bf215546Sopenharmony_civmw_fence_ops_destroy(struct pb_fence_ops *ops)
463bf215546Sopenharmony_ci{
464bf215546Sopenharmony_ci   vmw_fences_release(vmw_fence_ops(ops));
465bf215546Sopenharmony_ci   FREE(ops);
466bf215546Sopenharmony_ci}
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci
469bf215546Sopenharmony_ci/**
470bf215546Sopenharmony_ci * vmw_fence_ops_create - Create a pb_fence_ops function table.
471bf215546Sopenharmony_ci *
472bf215546Sopenharmony_ci * @vws: Pointer to a struct vmw_winsys_screen.
473bf215546Sopenharmony_ci *
474bf215546Sopenharmony_ci * Returns a pointer to a pb_fence_ops function table to interface
475bf215546Sopenharmony_ci * with pipe_buffer. This function is typically called on driver setup.
476bf215546Sopenharmony_ci *
477bf215546Sopenharmony_ci * Returns NULL on failure.
478bf215546Sopenharmony_ci */
479bf215546Sopenharmony_cistruct pb_fence_ops *
480bf215546Sopenharmony_civmw_fence_ops_create(struct vmw_winsys_screen *vws)
481bf215546Sopenharmony_ci{
482bf215546Sopenharmony_ci   struct vmw_fence_ops *ops;
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci   ops = CALLOC_STRUCT(vmw_fence_ops);
485bf215546Sopenharmony_ci   if(!ops)
486bf215546Sopenharmony_ci      return NULL;
487bf215546Sopenharmony_ci
488bf215546Sopenharmony_ci   (void) mtx_init(&ops->mutex, mtx_plain);
489bf215546Sopenharmony_ci   list_inithead(&ops->not_signaled);
490bf215546Sopenharmony_ci   ops->base.destroy = &vmw_fence_ops_destroy;
491bf215546Sopenharmony_ci   ops->base.fence_reference = &vmw_fence_ops_fence_reference;
492bf215546Sopenharmony_ci   ops->base.fence_signalled = &vmw_fence_ops_fence_signalled;
493bf215546Sopenharmony_ci   ops->base.fence_finish = &vmw_fence_ops_fence_finish;
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci   ops->vws = vws;
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci   return &ops->base;
498bf215546Sopenharmony_ci}
499