summaryrefslogtreecommitdiffstats
path: root/src/gallium/winsys/svga/drm/vmw_fence.c
blob: b18d5bf5d5d3b71f3258aebb68982796c9f20cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**********************************************************
 * Copyright 2009-2015 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/
#include "util/u_memory.h"
#include "util/u_atomic.h"
#include "util/list.h"
#include "os/os_thread.h"

#include "pipebuffer/pb_buffer_fenced.h"

#include "vmw_screen.h"
#include "vmw_fence.h"

struct vmw_fence_ops 
{
   /*
    * Immutable members.
    */
   struct pb_fence_ops base;
   struct vmw_winsys_screen *vws;

   mtx_t mutex;

   /*
    * Protected by mutex;
    */
   struct list_head not_signaled;
   uint32_t last_signaled;
   uint32_t last_emitted;
};

struct vmw_fence
{
   struct list_head ops_list;
   int32_t refcount;
   uint32_t handle;
   uint32_t mask;
   int32_t signalled;
   uint32_t seqno;
};

/**
 * vmw_fence_seq_is_signaled - Check whether a fence seqno is
 * signaled.
 *
 * @ops: Pointer to a struct pb_fence_ops.
 *
 */
static inline boolean
vmw_fence_seq_is_signaled(uint32_t seq, uint32_t last, uint32_t cur)
{
   return (cur - last <= cur - seq);
}


/**
 * vmw_fence_ops - Return the vmw_fence_ops structure backing a
 * struct pb_fence_ops pointer.
 *
 * @ops: Pointer to a struct pb_fence_ops.
 *
 */
static inline struct vmw_fence_ops *
vmw_fence_ops(struct pb_fence_ops *ops)
{
   assert(ops);
   return (struct vmw_fence_ops *)ops;
}


/**
 * vmw_fences_release - Release all fences from the not_signaled
 * list.
 *
 * @ops: Pointer to a struct vmw_fence_ops.
 *
 */
static void
vmw_fences_release(struct vmw_fence_ops *ops)
{
   struct vmw_fence *fence, *n;

   pipe_mutex_lock(ops->mutex);
   LIST_FOR_EACH_ENTRY_SAFE(fence, n, &ops->not_signaled, ops_list)
      LIST_DELINIT(&fence->ops_list);
   pipe_mutex_unlock(ops->mutex);
}

/**
 * vmw_fences_signal - Traverse the not_signaled list and try to
 * signal unsignaled fences.
 *
 * @ops: Pointer to a struct pb_fence_ops.
 * @signaled: Seqno that has signaled.
 * @emitted: Last seqno emitted by the kernel.
 * @has_emitted: Whether we provide the emitted value.
 *
 */
void
vmw_fences_signal(struct pb_fence_ops *fence_ops,
                  uint32_t signaled,
                  uint32_t emitted,
                  boolean has_emitted)
{
   struct vmw_fence_ops *ops = NULL;
   struct vmw_fence *fence, *n;

   if (fence_ops == NULL)
      return;

   ops = vmw_fence_ops(fence_ops);
   pipe_mutex_lock(ops->mutex);

   if (!has_emitted) {
      emitted = ops->last_emitted;
      if (emitted - signaled > (1 << 30))
	emitted = signaled;
   }

   if (signaled == ops->last_signaled && emitted == ops->last_emitted)
      goto out_unlock;

   LIST_FOR_EACH_ENTRY_SAFE(fence, n, &ops->not_signaled, ops_list) {
      if (!vmw_fence_seq_is_signaled(fence->seqno, signaled, emitted))
         break;

      p_atomic_set(&fence->signalled, 1);
      LIST_DELINIT(&fence->ops_list);
   }
   ops->last_signaled = signaled;
   ops->last_emitted = emitted;

out_unlock:
   pipe_mutex_unlock(ops->mutex);
}


/**
 * vmw_fence - return the vmw_fence object identified by a
 * struct pipe_fence_handle *
 *
 * @fence: The opaque pipe fence handle.
 */
static inline struct vmw_fence *
vmw_fence(struct pipe_fence_handle *fence)
{
   return (struct vmw_fence *) fence;
}


/**
 * vmw_fence_create - Create a user-space fence object.
 *
 * @fence_ops: The fence_ops manager to register with.
 * @handle: Handle identifying the kernel fence object.
 * @mask: Mask of flags that this fence object may signal.
 *
 * Returns NULL on failure.
 */
struct pipe_fence_handle *
vmw_fence_create(struct pb_fence_ops *fence_ops, uint32_t handle,
                 uint32_t seqno, uint32_t mask)
{
   struct vmw_fence *fence = CALLOC_STRUCT(vmw_fence);
   struct vmw_fence_ops *ops = vmw_fence_ops(fence_ops);

   if (!fence)
      return NULL;

   p_atomic_set(&fence->refcount, 1);
   fence->handle = handle;
   fence->mask = mask;
   fence->seqno = seqno;
   p_atomic_set(&fence->signalled, 0);
   pipe_mutex_lock(ops->mutex);

   if (vmw_fence_seq_is_signaled(seqno, ops->last_signaled, seqno)) {
      p_atomic_set(&fence->signalled, 1);
      LIST_INITHEAD(&fence->ops_list);
   } else {
      p_atomic_set(&fence->signalled, 0);
      LIST_ADDTAIL(&fence->ops_list, &ops->not_signaled);
   }

   pipe_mutex_unlock(ops->mutex);

   return (struct pipe_fence_handle *) fence;
}


/**
 * vmw_fence_reference - Reference / unreference a vmw fence object.
 *
 * @vws: Pointer to the winsys screen.
 * @ptr: Pointer to reference transfer destination.
 * @fence: Pointer to object to reference. May be NULL.
 */
void
vmw_fence_reference(struct vmw_winsys_screen *vws,
		    struct pipe_fence_handle **ptr,
		    struct pipe_fence_handle *fence)
{
   if (*ptr) {
      struct vmw_fence *vfence = vmw_fence(*ptr);

      if (p_atomic_dec_zero(&vfence->refcount)) {
         struct vmw_fence_ops *ops = vmw_fence_ops(vws->fence_ops);

	 vmw_ioctl_fence_unref(vws, vfence->handle);

         pipe_mutex_lock(ops->mutex);
         LIST_DELINIT(&vfence->ops_list);
         pipe_mutex_unlock(ops->mutex);

	 FREE(vfence);
      }
   }

   if (fence) {
      struct vmw_fence *vfence = vmw_fence(fence);

      p_atomic_inc(&vfence->refcount);
   }

   *ptr = fence;
}


/**
 * vmw_fence_signalled - Check whether a fence object is signalled.
 *
 * @vws: Pointer to the winsys screen.
 * @fence: Handle to the fence object.
 * @flag: Fence flags to check. If the fence object can't signal
 * a flag, it is assumed to be already signaled.
 *
 * Returns 0 if the fence object was signaled, nonzero otherwise.
 */
int
vmw_fence_signalled(struct vmw_winsys_screen *vws,
		   struct pipe_fence_handle *fence,
		   unsigned flag)
{
   struct vmw_fence *vfence;
   int32_t vflags = SVGA_FENCE_FLAG_EXEC;
   int ret;
   uint32_t old;

   if (!fence)
      return 0;

   vfence = vmw_fence(fence);
   old = p_atomic_read(&vfence->signalled);

   vflags &= ~vfence->mask;

   if ((old & vflags) == vflags)
      return 0;

   /*
    * Currently we update signaled fences on each execbuf call.
    * That should really be sufficient, and we can avoid
    * a lot of kernel calls this way.
    */
#if 1
   ret = vmw_ioctl_fence_signalled(vws, vfence->handle, vflags);

   if (ret == 0)
      p_atomic_set(&vfence->signalled, 1);
   return ret;
#else
   (void) ret;
   return -1;
#endif
}

/**
 * vmw_fence_finish - Wait for a fence object to signal.
 *
 * @vws: Pointer to the winsys screen.
 * @fence: Handle to the fence object.
 * @flag: Fence flags to wait for. If the fence object can't signal
 * a flag, it is assumed to be already signaled.
 *
 * Returns 0 if the wait succeeded. Nonzero otherwise.
 */
int
vmw_fence_finish(struct vmw_winsys_screen *vws,
		 struct pipe_fence_handle *fence,
		 unsigned flag)
{
   struct vmw_fence *vfence;
   int32_t vflags = SVGA_FENCE_FLAG_EXEC;
   int ret;
   uint32_t old;

   if (!fence)
      return 0;

   vfence = vmw_fence(fence);
   old = p_atomic_read(&vfence->signalled);
   vflags &= ~vfence->mask;

   if ((old & vflags) == vflags)
      return 0;

   ret = vmw_ioctl_fence_finish(vws, vfence->handle, vflags);

   if (ret == 0) {
      int32_t prev = old;

      do {
	 old = prev;
	 prev = p_atomic_cmpxchg(&vfence->signalled, old, old | vflags);
      } while (prev != old);
   }

   return ret;
}


/**
 * vmw_fence_ops_fence_reference - wrapper for the pb_fence_ops api.
 *
 * wrapper around vmw_fence_reference.
 */
static void
vmw_fence_ops_fence_reference(struct pb_fence_ops *ops,
                              struct pipe_fence_handle **ptr,
                              struct pipe_fence_handle *fence)
{
   struct vmw_winsys_screen *vws = vmw_fence_ops(ops)->vws;

   vmw_fence_reference(vws, ptr, fence);
}

/**
 * vmw_fence_ops_fence_signalled - wrapper for the pb_fence_ops api.
 *
 * wrapper around vmw_fence_signalled.
 */
static int
vmw_fence_ops_fence_signalled(struct pb_fence_ops *ops,
                              struct pipe_fence_handle *fence,
                              unsigned flag)
{
   struct vmw_winsys_screen *vws = vmw_fence_ops(ops)->vws;

   return vmw_fence_signalled(vws, fence, flag);
}


/**
 * vmw_fence_ops_fence_finish - wrapper for the pb_fence_ops api.
 *
 * wrapper around vmw_fence_finish.
 */
static int
vmw_fence_ops_fence_finish(struct pb_fence_ops *ops,
                           struct pipe_fence_handle *fence,
                           unsigned flag)
{
   struct vmw_winsys_screen *vws = vmw_fence_ops(ops)->vws;

   return vmw_fence_finish(vws, fence, flag);
}


/**
 * vmw_fence_ops_destroy - Destroy a pb_fence_ops function table.
 *
 * @ops: The function table to destroy.
 *
 * Part of the pb_fence_ops api.
 */
static void
vmw_fence_ops_destroy(struct pb_fence_ops *ops)
{
   vmw_fences_release(vmw_fence_ops(ops));
   FREE(ops);
}


/**
 * vmw_fence_ops_create - Create a pb_fence_ops function table.
 *
 * @vws: Pointer to a struct vmw_winsys_screen.
 *
 * Returns a pointer to a pb_fence_ops function table to interface
 * with pipe_buffer. This function is typically called on driver setup.
 *
 * Returns NULL on failure.
 */
struct pb_fence_ops *
vmw_fence_ops_create(struct vmw_winsys_screen *vws) 
{
   struct vmw_fence_ops *ops;

   ops = CALLOC_STRUCT(vmw_fence_ops);
   if(!ops)
      return NULL;

   (void) mtx_init(&ops->mutex, mtx_plain);
   LIST_INITHEAD(&ops->not_signaled);
   ops->base.destroy = &vmw_fence_ops_destroy;
   ops->base.fence_reference = &vmw_fence_ops_fence_reference;
   ops->base.fence_signalled = &vmw_fence_ops_fence_signalled;
   ops->base.fence_finish = &vmw_fence_ops_fence_finish;

   ops->vws = vws;

   return &ops->base;
}