1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Implementation of fenced buffers.
32 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com>
33 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
37 #include "pipe/p_config.h"
39 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD)
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"
51 #include "pb_buffer.h"
52 #include "pb_buffer_fenced.h"
57 * Convenience macro (type safe).
59 #define SUPER(__derived) (&(__derived)->base)
62 struct fenced_buffer_list
66 struct pb_fence_ops
*ops
;
69 struct list_head delayed
;
73 struct list_head unfenced
;
79 * Wrapper around a pipe buffer which adds fencing and reference counting.
83 struct pb_buffer base
;
85 struct pb_buffer
*buffer
;
87 /* FIXME: protect access with mutex */
90 * A bitmask of PIPE_BUFFER_USAGE_CPU/GPU_READ/WRITE describing the current
96 struct pb_validate
*vl
;
97 unsigned validation_flags
;
98 struct pipe_fence_handle
*fence
;
100 struct list_head head
;
101 struct fenced_buffer_list
*list
;
105 static INLINE
struct fenced_buffer
*
106 fenced_buffer(struct pb_buffer
*buf
)
109 return (struct fenced_buffer
*)buf
;
114 _fenced_buffer_add(struct fenced_buffer
*fenced_buf
)
116 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
118 assert(fenced_buf
->base
.base
.refcount
);
119 assert(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
120 assert(fenced_buf
->fence
);
123 LIST_DEL(&fenced_buf
->head
);
124 assert(fenced_list
->numUnfenced
);
125 --fenced_list
->numUnfenced
;
127 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->delayed
);
128 ++fenced_list
->numDelayed
;
133 * Actually destroy the buffer.
136 _fenced_buffer_destroy(struct fenced_buffer
*fenced_buf
)
138 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
140 assert(!fenced_buf
->base
.base
.refcount
);
141 assert(!fenced_buf
->fence
);
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
;
151 pb_reference(&fenced_buf
->buffer
, NULL
);
157 _fenced_buffer_remove(struct fenced_buffer_list
*fenced_list
,
158 struct fenced_buffer
*fenced_buf
)
160 struct pb_fence_ops
*ops
= fenced_list
->ops
;
162 assert(fenced_buf
->fence
);
163 assert(fenced_buf
->list
== fenced_list
);
165 ops
->fence_reference(ops
, &fenced_buf
->fence
, NULL
);
166 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
168 assert(fenced_buf
->head
.prev
);
169 assert(fenced_buf
->head
.next
);
171 LIST_DEL(&fenced_buf
->head
);
172 assert(fenced_list
->numDelayed
);
173 --fenced_list
->numDelayed
;
176 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->unfenced
);
177 ++fenced_list
->numUnfenced
;
180 if(!fenced_buf
->base
.base
.refcount
)
181 _fenced_buffer_destroy(fenced_buf
);
185 static INLINE
enum pipe_error
186 _fenced_buffer_finish(struct fenced_buffer
*fenced_buf
)
188 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
189 struct pb_fence_ops
*ops
= fenced_list
->ops
;
192 debug_warning("waiting for GPU");
195 assert(fenced_buf
->fence
);
196 if(fenced_buf
->fence
) {
197 if(ops
->fence_finish(ops
, fenced_buf
->fence
, 0) != 0) {
200 /* Remove from the fenced list */
201 /* TODO: remove consequents */
202 _fenced_buffer_remove(fenced_list
, fenced_buf
);
205 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
211 * Free as many fenced buffers from the list head as possible.
214 _fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
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
;
222 curr
= fenced_list
->delayed
.next
;
224 while(curr
!= &fenced_list
->delayed
) {
225 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
227 if(fenced_buf
->fence
!= prev_fence
) {
230 signaled
= ops
->fence_finish(ops
, fenced_buf
->fence
, 0);
232 signaled
= ops
->fence_signalled(ops
, fenced_buf
->fence
, 0);
235 prev_fence
= fenced_buf
->fence
;
238 assert(ops
->fence_signalled(ops
, fenced_buf
->fence
, 0) == 0);
241 _fenced_buffer_remove(fenced_list
, fenced_buf
);
250 fenced_buffer_destroy(struct pb_buffer
*buf
)
252 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
253 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
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
;
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
);
269 } while (curr
!= &fenced_list
->delayed
);
272 /* delay destruction */
276 _fenced_buffer_destroy(fenced_buf
);
278 pipe_mutex_unlock(fenced_list
->mutex
);
283 fenced_buffer_map(struct pb_buffer
*buf
,
286 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
289 assert(flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
);
290 assert(!(flags
& ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
291 flags
&= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
293 /* Check for GPU read/write access */
294 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
295 /* Wait for the GPU to finish writing */
296 _fenced_buffer_finish(fenced_buf
);
300 /* Check for CPU write access (read is OK) */
301 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
302 /* this is legal -- just for debugging */
303 debug_warning("concurrent CPU writes");
307 map
= pb_map(fenced_buf
->buffer
, flags
);
309 ++fenced_buf
->mapcount
;
310 fenced_buf
->flags
|= flags
;
318 fenced_buffer_unmap(struct pb_buffer
*buf
)
320 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
321 assert(fenced_buf
->mapcount
);
322 if(fenced_buf
->mapcount
) {
323 pb_unmap(fenced_buf
->buffer
);
324 --fenced_buf
->mapcount
;
325 if(!fenced_buf
->mapcount
)
326 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
331 static enum pipe_error
332 fenced_buffer_validate(struct pb_buffer
*buf
,
333 struct pb_validate
*vl
,
336 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
341 fenced_buf
->vl
= NULL
;
342 fenced_buf
->validation_flags
= 0;
346 assert(flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
347 assert(!(flags
& ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
));
348 flags
&= PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
350 /* Buffer cannot be validated in two different lists */
351 if(fenced_buf
->vl
&& fenced_buf
->vl
!= vl
)
352 return PIPE_ERROR_RETRY
;
354 /* Do not validate if buffer is still mapped */
355 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
356 /* TODO: wait for the thread that mapped the buffer to unmap it */
357 return PIPE_ERROR_RETRY
;
360 if(fenced_buf
->vl
== vl
&&
361 (fenced_buf
->validation_flags
& flags
) == flags
) {
362 /* Nothing to do -- buffer already validated */
366 /* Final sanity checking */
367 assert(!(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
368 assert(!fenced_buf
->mapcount
);
370 ret
= pb_validate(fenced_buf
->buffer
, vl
, flags
);
375 fenced_buf
->validation_flags
|= flags
;
382 fenced_buffer_fence(struct pb_buffer
*buf
,
383 struct pipe_fence_handle
*fence
)
385 struct fenced_buffer
*fenced_buf
;
386 struct fenced_buffer_list
*fenced_list
;
387 struct pb_fence_ops
*ops
;
389 fenced_buf
= fenced_buffer(buf
);
390 fenced_list
= fenced_buf
->list
;
391 ops
= fenced_list
->ops
;
393 if(fence
== fenced_buf
->fence
) {
398 assert(fenced_buf
->vl
);
399 assert(fenced_buf
->validation_flags
);
401 pipe_mutex_lock(fenced_list
->mutex
);
402 if (fenced_buf
->fence
)
403 _fenced_buffer_remove(fenced_list
, fenced_buf
);
405 ops
->fence_reference(ops
, &fenced_buf
->fence
, fence
);
406 fenced_buf
->flags
|= fenced_buf
->validation_flags
;
407 _fenced_buffer_add(fenced_buf
);
409 pipe_mutex_unlock(fenced_list
->mutex
);
411 pb_fence(fenced_buf
->buffer
, fence
);
413 fenced_buf
->vl
= NULL
;
414 fenced_buf
->validation_flags
= 0;
419 fenced_buffer_get_base_buffer(struct pb_buffer
*buf
,
420 struct pb_buffer
**base_buf
,
423 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
424 pb_get_base_buffer(fenced_buf
->buffer
, base_buf
, offset
);
428 static const struct pb_vtbl
429 fenced_buffer_vtbl
= {
430 fenced_buffer_destroy
,
433 fenced_buffer_validate
,
435 fenced_buffer_get_base_buffer
440 fenced_buffer_create(struct fenced_buffer_list
*fenced_list
,
441 struct pb_buffer
*buffer
)
443 struct fenced_buffer
*buf
;
448 buf
= CALLOC_STRUCT(fenced_buffer
);
450 pb_reference(&buffer
, NULL
);
454 buf
->base
.base
.refcount
= 1;
455 buf
->base
.base
.alignment
= buffer
->base
.alignment
;
456 buf
->base
.base
.usage
= buffer
->base
.usage
;
457 buf
->base
.base
.size
= buffer
->base
.size
;
459 buf
->base
.vtbl
= &fenced_buffer_vtbl
;
460 buf
->buffer
= buffer
;
461 buf
->list
= fenced_list
;
464 pipe_mutex_lock(fenced_list
->mutex
);
465 LIST_ADDTAIL(&buf
->head
, &fenced_list
->unfenced
);
466 ++fenced_list
->numUnfenced
;
467 pipe_mutex_unlock(fenced_list
->mutex
);
474 struct fenced_buffer_list
*
475 fenced_buffer_list_create(struct pb_fence_ops
*ops
)
477 struct fenced_buffer_list
*fenced_list
;
479 fenced_list
= CALLOC_STRUCT(fenced_buffer_list
);
483 fenced_list
->ops
= ops
;
485 LIST_INITHEAD(&fenced_list
->delayed
);
486 fenced_list
->numDelayed
= 0;
489 LIST_INITHEAD(&fenced_list
->unfenced
);
490 fenced_list
->numUnfenced
= 0;
493 pipe_mutex_init(fenced_list
->mutex
);
500 fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
503 pipe_mutex_lock(fenced_list
->mutex
);
504 _fenced_buffer_list_check_free(fenced_list
, wait
);
505 pipe_mutex_unlock(fenced_list
->mutex
);
511 fenced_buffer_list_dump(struct fenced_buffer_list
*fenced_list
)
513 struct pb_fence_ops
*ops
= fenced_list
->ops
;
514 struct list_head
*curr
, *next
;
515 struct fenced_buffer
*fenced_buf
;
517 pipe_mutex_lock(fenced_list
->mutex
);
519 debug_printf("%10s %7s %10s %s\n",
520 "buffer", "refcount", "fence", "signalled");
522 curr
= fenced_list
->unfenced
.next
;
524 while(curr
!= &fenced_list
->unfenced
) {
525 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
526 assert(!fenced_buf
->fence
);
527 debug_printf("%10p %7u\n",
529 fenced_buf
->base
.base
.refcount
);
534 curr
= fenced_list
->delayed
.next
;
536 while(curr
!= &fenced_list
->delayed
) {
538 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
539 signaled
= ops
->fence_signalled(ops
, fenced_buf
->fence
, 0);
540 debug_printf("%10p %7u %10p %s\n",
542 fenced_buf
->base
.base
.refcount
,
544 signaled
== 0 ? "y" : "n");
549 pipe_mutex_unlock(fenced_list
->mutex
);
555 fenced_buffer_list_destroy(struct fenced_buffer_list
*fenced_list
)
557 pipe_mutex_lock(fenced_list
->mutex
);
559 /* Wait on outstanding fences */
560 while (fenced_list
->numDelayed
) {
561 pipe_mutex_unlock(fenced_list
->mutex
);
562 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD)
565 _fenced_buffer_list_check_free(fenced_list
, 1);
566 pipe_mutex_lock(fenced_list
->mutex
);
570 //assert(!fenced_list->numUnfenced);
573 pipe_mutex_unlock(fenced_list
->mutex
);
575 fenced_list
->ops
->destroy(fenced_list
->ops
);