pb_buffer_fenced.c revision 9a9dc422b0c491f516fd8d80e0ce128e4145698f
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * \file
30 * Implementation of fenced buffers.
31 *
32 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com>
33 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
34 */
35
36
37#include "pipe/p_config.h"
38
39#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD)
40#include <unistd.h>
41#include <sched.h>
42#endif
43
44#include "pipe/p_compiler.h"
45#include "pipe/p_error.h"
46#include "util/u_debug.h"
47#include "pipe/p_thread.h"
48#include "util/u_memory.h"
49#include "util/u_double_list.h"
50
51#include "pb_buffer.h"
52#include "pb_buffer_fenced.h"
53
54
55
56/**
57 * Convenience macro (type safe).
58 */
59#define SUPER(__derived) (&(__derived)->base)
60
61
62struct fenced_buffer_list
63{
64   pipe_mutex mutex;
65
66   struct pb_fence_ops *ops;
67
68   size_t numDelayed;
69   struct list_head delayed;
70
71#ifdef DEBUG
72   size_t numUnfenced;
73   struct list_head unfenced;
74#endif
75};
76
77
78/**
79 * Wrapper around a pipe buffer which adds fencing and reference counting.
80 */
81struct fenced_buffer
82{
83   struct pb_buffer base;
84
85   struct pb_buffer *buffer;
86
87   /* FIXME: protect access with mutex */
88
89   /**
90    * A bitmask of PIPE_BUFFER_USAGE_CPU/GPU_READ/WRITE describing the current
91    * buffer usage.
92    */
93   unsigned flags;
94
95   unsigned mapcount;
96   struct pb_validate *vl;
97   unsigned validation_flags;
98   struct pipe_fence_handle *fence;
99
100   struct list_head head;
101   struct fenced_buffer_list *list;
102};
103
104
105static INLINE struct fenced_buffer *
106fenced_buffer(struct pb_buffer *buf)
107{
108   assert(buf);
109   return (struct fenced_buffer *)buf;
110}
111
112
113static INLINE void
114_fenced_buffer_add(struct fenced_buffer *fenced_buf)
115{
116   struct fenced_buffer_list *fenced_list = fenced_buf->list;
117
118   assert(fenced_buf->base.base.refcount);
119   assert(fenced_buf->flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE);
120   assert(fenced_buf->fence);
121
122#ifdef DEBUG
123   LIST_DEL(&fenced_buf->head);
124   assert(fenced_list->numUnfenced);
125   --fenced_list->numUnfenced;
126#endif
127   LIST_ADDTAIL(&fenced_buf->head, &fenced_list->delayed);
128   ++fenced_list->numDelayed;
129}
130
131
132/**
133 * Actually destroy the buffer.
134 */
135static INLINE void
136_fenced_buffer_destroy(struct fenced_buffer *fenced_buf)
137{
138   struct fenced_buffer_list *fenced_list = fenced_buf->list;
139
140   assert(!fenced_buf->base.base.refcount);
141   assert(!fenced_buf->fence);
142#ifdef DEBUG
143   assert(fenced_buf->head.prev);
144   assert(fenced_buf->head.next);
145   LIST_DEL(&fenced_buf->head);
146   assert(fenced_list->numUnfenced);
147   --fenced_list->numUnfenced;
148#else
149   (void)fenced_list;
150#endif
151   pb_reference(&fenced_buf->buffer, NULL);
152   FREE(fenced_buf);
153}
154
155
156static INLINE void
157_fenced_buffer_remove(struct fenced_buffer_list *fenced_list,
158                      struct fenced_buffer *fenced_buf)
159{
160   struct pb_fence_ops *ops = fenced_list->ops;
161
162   assert(fenced_buf->fence);
163   assert(fenced_buf->list == fenced_list);
164
165   ops->fence_reference(ops, &fenced_buf->fence, NULL);
166   fenced_buf->flags &= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE;
167
168   assert(fenced_buf->head.prev);
169   assert(fenced_buf->head.next);
170
171   LIST_DEL(&fenced_buf->head);
172   assert(fenced_list->numDelayed);
173   --fenced_list->numDelayed;
174
175#ifdef DEBUG
176   LIST_ADDTAIL(&fenced_buf->head, &fenced_list->unfenced);
177   ++fenced_list->numUnfenced;
178#endif
179
180   if(!fenced_buf->base.base.refcount)
181      _fenced_buffer_destroy(fenced_buf);
182}
183
184
185static INLINE enum pipe_error
186_fenced_buffer_finish(struct fenced_buffer *fenced_buf)
187{
188   struct fenced_buffer_list *fenced_list = fenced_buf->list;
189   struct pb_fence_ops *ops = fenced_list->ops;
190
191#if 0
192   debug_warning("waiting for GPU");
193#endif
194
195   assert(fenced_buf->fence);
196   if(fenced_buf->fence) {
197      if(ops->fence_finish(ops, fenced_buf->fence, 0) != 0) {
198	 return PIPE_ERROR;
199      }
200      /* Remove from the fenced list */
201      /* TODO: remove consequents */
202      _fenced_buffer_remove(fenced_list, fenced_buf);
203   }
204
205   fenced_buf->flags &= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE;
206   return PIPE_OK;
207}
208
209
210/**
211 * Free as many fenced buffers from the list head as possible.
212 */
213static void
214_fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list,
215                               int wait)
216{
217   struct pb_fence_ops *ops = fenced_list->ops;
218   struct list_head *curr, *next;
219   struct fenced_buffer *fenced_buf;
220   struct pipe_fence_handle *prev_fence = NULL;
221
222   curr = fenced_list->delayed.next;
223   next = curr->next;
224   while(curr != &fenced_list->delayed) {
225      fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
226
227      if(fenced_buf->fence != prev_fence) {
228	 int signaled;
229	 if (wait)
230	    signaled = ops->fence_finish(ops, fenced_buf->fence, 0);
231	 else
232	    signaled = ops->fence_signalled(ops, fenced_buf->fence, 0);
233	 if (signaled != 0)
234	    break;
235	 prev_fence = fenced_buf->fence;
236      }
237      else {
238	 assert(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0);
239      }
240
241      _fenced_buffer_remove(fenced_list, fenced_buf);
242
243      curr = next;
244      next = curr->next;
245   }
246}
247
248
249static void
250fenced_buffer_destroy(struct pb_buffer *buf)
251{
252   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
253   struct fenced_buffer_list *fenced_list = fenced_buf->list;
254
255   pipe_mutex_lock(fenced_list->mutex);
256   assert(fenced_buf->base.base.refcount == 0);
257   if (fenced_buf->fence) {
258      struct pb_fence_ops *ops = fenced_list->ops;
259      if(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0) {
260	 struct list_head *curr, *prev;
261	 curr = &fenced_buf->head;
262	 prev = curr->prev;
263	 do {
264	    fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
265	    assert(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0);
266	    _fenced_buffer_remove(fenced_list, fenced_buf);
267	    curr = prev;
268	    prev = curr->prev;
269	 } while (curr != &fenced_list->delayed);
270      }
271      else {
272	 /* delay destruction */
273      }
274   }
275   else {
276      _fenced_buffer_destroy(fenced_buf);
277   }
278   pipe_mutex_unlock(fenced_list->mutex);
279}
280
281
282static void *
283fenced_buffer_map(struct pb_buffer *buf,
284                  unsigned flags)
285{
286   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
287   struct fenced_buffer_list *fenced_list = fenced_buf->list;
288   struct pb_fence_ops *ops = fenced_list->ops;
289   void *map;
290
291   assert(!(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE));
292
293   /* Serialize writes */
294   if((fenced_buf->flags & PIPE_BUFFER_USAGE_GPU_WRITE) ||
295      ((fenced_buf->flags & PIPE_BUFFER_USAGE_GPU_READ) && (flags & PIPE_BUFFER_USAGE_CPU_WRITE))) {
296      if(flags & PIPE_BUFFER_USAGE_DONTBLOCK) {
297         /* Don't wait for the GPU to finish writing */
298         if(ops->fence_finish(ops, fenced_buf->fence, 0) == 0)
299            _fenced_buffer_remove(fenced_list, fenced_buf);
300         else
301            return NULL;
302      }
303      else {
304         /* Wait for the GPU to finish writing */
305         _fenced_buffer_finish(fenced_buf);
306      }
307   }
308
309#if 0
310   /* Check for CPU write access (read is OK) */
311   if(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE) {
312      /* this is legal -- just for debugging */
313      debug_warning("concurrent CPU writes");
314   }
315#endif
316
317   map = pb_map(fenced_buf->buffer, flags);
318   if(map) {
319      ++fenced_buf->mapcount;
320      fenced_buf->flags |= flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE;
321   }
322
323   return map;
324}
325
326
327static void
328fenced_buffer_unmap(struct pb_buffer *buf)
329{
330   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
331   assert(fenced_buf->mapcount);
332   if(fenced_buf->mapcount) {
333      pb_unmap(fenced_buf->buffer);
334      --fenced_buf->mapcount;
335      if(!fenced_buf->mapcount)
336	 fenced_buf->flags &= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE;
337   }
338}
339
340
341static enum pipe_error
342fenced_buffer_validate(struct pb_buffer *buf,
343                       struct pb_validate *vl,
344                       unsigned flags)
345{
346   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
347   enum pipe_error ret;
348
349   if(!vl) {
350      /* invalidate */
351      fenced_buf->vl = NULL;
352      fenced_buf->validation_flags = 0;
353      return PIPE_OK;
354   }
355
356   assert(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE);
357   assert(!(flags & ~PIPE_BUFFER_USAGE_GPU_READ_WRITE));
358   flags &= PIPE_BUFFER_USAGE_GPU_READ_WRITE;
359
360   /* Buffer cannot be validated in two different lists */
361   if(fenced_buf->vl && fenced_buf->vl != vl)
362      return PIPE_ERROR_RETRY;
363
364   /* Do not validate if buffer is still mapped */
365   if(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE) {
366      /* TODO: wait for the thread that mapped the buffer to unmap it */
367      return PIPE_ERROR_RETRY;
368   }
369
370   if(fenced_buf->vl == vl &&
371      (fenced_buf->validation_flags & flags) == flags) {
372      /* Nothing to do -- buffer already validated */
373      return PIPE_OK;
374   }
375
376   /* Final sanity checking */
377   assert(!(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE));
378   assert(!fenced_buf->mapcount);
379
380   ret = pb_validate(fenced_buf->buffer, vl, flags);
381   if (ret != PIPE_OK)
382      return ret;
383
384   fenced_buf->vl = vl;
385   fenced_buf->validation_flags |= flags;
386
387   return PIPE_OK;
388}
389
390
391static void
392fenced_buffer_fence(struct pb_buffer *buf,
393                    struct pipe_fence_handle *fence)
394{
395   struct fenced_buffer *fenced_buf;
396   struct fenced_buffer_list *fenced_list;
397   struct pb_fence_ops *ops;
398
399   fenced_buf = fenced_buffer(buf);
400   fenced_list = fenced_buf->list;
401   ops = fenced_list->ops;
402
403   if(fence == fenced_buf->fence) {
404      /* Nothing to do */
405      return;
406   }
407
408   assert(fenced_buf->vl);
409   assert(fenced_buf->validation_flags);
410
411   pipe_mutex_lock(fenced_list->mutex);
412   if (fenced_buf->fence)
413      _fenced_buffer_remove(fenced_list, fenced_buf);
414   if (fence) {
415      ops->fence_reference(ops, &fenced_buf->fence, fence);
416      fenced_buf->flags |= fenced_buf->validation_flags;
417      _fenced_buffer_add(fenced_buf);
418   }
419   pipe_mutex_unlock(fenced_list->mutex);
420
421   pb_fence(fenced_buf->buffer, fence);
422
423   fenced_buf->vl = NULL;
424   fenced_buf->validation_flags = 0;
425}
426
427
428static void
429fenced_buffer_get_base_buffer(struct pb_buffer *buf,
430                              struct pb_buffer **base_buf,
431                              unsigned *offset)
432{
433   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
434   pb_get_base_buffer(fenced_buf->buffer, base_buf, offset);
435}
436
437
438static const struct pb_vtbl
439fenced_buffer_vtbl = {
440      fenced_buffer_destroy,
441      fenced_buffer_map,
442      fenced_buffer_unmap,
443      fenced_buffer_validate,
444      fenced_buffer_fence,
445      fenced_buffer_get_base_buffer
446};
447
448
449struct pb_buffer *
450fenced_buffer_create(struct fenced_buffer_list *fenced_list,
451                     struct pb_buffer *buffer)
452{
453   struct fenced_buffer *buf;
454
455   if(!buffer)
456      return NULL;
457
458   buf = CALLOC_STRUCT(fenced_buffer);
459   if(!buf) {
460      pb_reference(&buffer, NULL);
461      return NULL;
462   }
463
464   buf->base.base.refcount = 1;
465   buf->base.base.alignment = buffer->base.alignment;
466   buf->base.base.usage = buffer->base.usage;
467   buf->base.base.size = buffer->base.size;
468
469   buf->base.vtbl = &fenced_buffer_vtbl;
470   buf->buffer = buffer;
471   buf->list = fenced_list;
472
473#ifdef DEBUG
474   pipe_mutex_lock(fenced_list->mutex);
475   LIST_ADDTAIL(&buf->head, &fenced_list->unfenced);
476   ++fenced_list->numUnfenced;
477   pipe_mutex_unlock(fenced_list->mutex);
478#endif
479
480   return &buf->base;
481}
482
483#if 0
484void
485buffer_fence(struct pb_buffer *buf,
486             struct pipe_fence_handle *fence)
487{
488   struct fenced_buffer *fenced_buf;
489   struct fenced_buffer_list *fenced_list;
490   struct pb_fence_ops *ops;
491   /* FIXME: receive this as a parameter */
492   unsigned flags = fence ? PIPE_BUFFER_USAGE_GPU_READ_WRITE : 0;
493
494   /* This is a public function, so be extra cautious with the buffer passed,
495    * as happens frequently to receive null buffers, or pointer to buffers
496    * other than fenced buffers. */
497   assert(buf);
498   if(!buf)
499      return;
500   assert(buf->vtbl == &fenced_buffer_vtbl);
501   if(buf->vtbl != &fenced_buffer_vtbl)
502      return;
503
504   if(!fence)
505      return;
506
507   fenced_buf = fenced_buffer(buf);
508   fenced_list = fenced_buf->list;
509   ops = fenced_list->ops;
510
511   if(fence == fenced_buf->fence) {
512      fenced_buf->flags |= flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE;
513      return;
514   }
515
516   pipe_mutex_lock(fenced_list->mutex);
517   if (fenced_buf->fence)
518      _fenced_buffer_remove(fenced_list, fenced_buf);
519   ops->fence_reference(ops, &fenced_buf->fence, fence);
520   fenced_buf->flags |= flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE;
521   _fenced_buffer_add(fenced_buf);
522   pipe_mutex_unlock(fenced_list->mutex);
523}
524#endif
525
526
527struct fenced_buffer_list *
528fenced_buffer_list_create(struct pb_fence_ops *ops)
529{
530   struct fenced_buffer_list *fenced_list;
531
532   fenced_list = CALLOC_STRUCT(fenced_buffer_list);
533   if (!fenced_list)
534      return NULL;
535
536   fenced_list->ops = ops;
537
538   LIST_INITHEAD(&fenced_list->delayed);
539   fenced_list->numDelayed = 0;
540
541#ifdef DEBUG
542   LIST_INITHEAD(&fenced_list->unfenced);
543   fenced_list->numUnfenced = 0;
544#endif
545
546   pipe_mutex_init(fenced_list->mutex);
547
548   return fenced_list;
549}
550
551
552void
553fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list,
554                              int wait)
555{
556   pipe_mutex_lock(fenced_list->mutex);
557   _fenced_buffer_list_check_free(fenced_list, wait);
558   pipe_mutex_unlock(fenced_list->mutex);
559}
560
561
562#ifdef DEBUG
563void
564fenced_buffer_list_dump(struct fenced_buffer_list *fenced_list)
565{
566   struct pb_fence_ops *ops = fenced_list->ops;
567   struct list_head *curr, *next;
568   struct fenced_buffer *fenced_buf;
569
570   pipe_mutex_lock(fenced_list->mutex);
571
572   debug_printf("%10s %7s %10s %s\n",
573                "buffer", "refcount", "fence", "signalled");
574
575   curr = fenced_list->unfenced.next;
576   next = curr->next;
577   while(curr != &fenced_list->unfenced) {
578      fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
579      assert(!fenced_buf->fence);
580      debug_printf("%10p %7u\n",
581                   fenced_buf,
582                   fenced_buf->base.base.refcount);
583      curr = next;
584      next = curr->next;
585   }
586
587   curr = fenced_list->delayed.next;
588   next = curr->next;
589   while(curr != &fenced_list->delayed) {
590      int signaled;
591      fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
592      signaled = ops->fence_signalled(ops, fenced_buf->fence, 0);
593      debug_printf("%10p %7u %10p %s\n",
594                   fenced_buf,
595                   fenced_buf->base.base.refcount,
596                   fenced_buf->fence,
597                   signaled == 0 ? "y" : "n");
598      curr = next;
599      next = curr->next;
600   }
601
602   pipe_mutex_unlock(fenced_list->mutex);
603}
604#endif
605
606
607void
608fenced_buffer_list_destroy(struct fenced_buffer_list *fenced_list)
609{
610   pipe_mutex_lock(fenced_list->mutex);
611
612   /* Wait on outstanding fences */
613   while (fenced_list->numDelayed) {
614      pipe_mutex_unlock(fenced_list->mutex);
615#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD)
616      sched_yield();
617#endif
618      _fenced_buffer_list_check_free(fenced_list, 1);
619      pipe_mutex_lock(fenced_list->mutex);
620   }
621
622#ifdef DEBUG
623   //assert(!fenced_list->numUnfenced);
624#endif
625
626   pipe_mutex_unlock(fenced_list->mutex);
627
628   fenced_list->ops->destroy(fenced_list->ops);
629
630   FREE(fenced_list);
631}
632
633
634