1 /**************************************************************************
3 * Copyright 2007-2009 VMware, Inc.
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 VMWARE 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) || defined(PIPE_OS_SOLARIS)
44 #include "pipe/p_compiler.h"
45 #include "pipe/p_defines.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.
87 struct pb_buffer base
;
88 struct pb_buffer
*buffer
;
89 struct fenced_buffer_list
*list
;
92 * Protected by fenced_buffer_list::mutex
94 struct list_head head
;
97 * Following members are mutable and protected by this mutex.
99 * You may lock this mutex alone, or lock it with fenced_buffer_list::mutex
100 * held, but in order to prevent deadlocks you must never lock
101 * fenced_buffer_list::mutex with this mutex held.
106 * A bitmask of PIPE_BUFFER_USAGE_CPU/GPU_READ/WRITE describing the current
112 struct pb_validate
*vl
;
113 unsigned validation_flags
;
114 struct pipe_fence_handle
*fence
;
118 static INLINE
struct fenced_buffer
*
119 fenced_buffer(struct pb_buffer
*buf
)
122 return (struct fenced_buffer
*)buf
;
127 * Add the buffer to the fenced list.
129 * fenced_buffer_list::mutex and fenced_buffer::mutex must be held, in this
130 * order, before calling this function.
132 * Reference count should be incremented before calling this function.
135 fenced_buffer_add_locked(struct fenced_buffer_list
*fenced_list
,
136 struct fenced_buffer
*fenced_buf
)
138 assert(pipe_is_referenced(&fenced_buf
->base
.base
.reference
));
139 assert(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
140 assert(fenced_buf
->fence
);
142 /* TODO: Move the reference count increment here */
145 LIST_DEL(&fenced_buf
->head
);
146 assert(fenced_list
->numUnfenced
);
147 --fenced_list
->numUnfenced
;
149 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->delayed
);
150 ++fenced_list
->numDelayed
;
155 * Remove the buffer from the fenced list.
157 * fenced_buffer_list::mutex and fenced_buffer::mutex must be held, in this
158 * order before calling this function.
160 * Reference count should be decremented after calling this function.
163 fenced_buffer_remove_locked(struct fenced_buffer_list
*fenced_list
,
164 struct fenced_buffer
*fenced_buf
)
166 struct pb_fence_ops
*ops
= fenced_list
->ops
;
168 assert(fenced_buf
->fence
);
169 assert(fenced_buf
->list
== fenced_list
);
171 ops
->fence_reference(ops
, &fenced_buf
->fence
, NULL
);
172 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
174 assert(fenced_buf
->head
.prev
);
175 assert(fenced_buf
->head
.next
);
177 LIST_DEL(&fenced_buf
->head
);
178 assert(fenced_list
->numDelayed
);
179 --fenced_list
->numDelayed
;
182 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->unfenced
);
183 ++fenced_list
->numUnfenced
;
186 /* TODO: Move the reference count decrement and destruction here */
191 * Wait for the fence to expire, and remove it from the fenced list.
193 * fenced_buffer::mutex must be held. fenced_buffer_list::mutex must not be
194 * held -- it will be acquired internally.
196 static INLINE
enum pipe_error
197 fenced_buffer_finish_locked(struct fenced_buffer_list
*fenced_list
,
198 struct fenced_buffer
*fenced_buf
)
200 struct pb_fence_ops
*ops
= fenced_list
->ops
;
201 enum pipe_error ret
= PIPE_ERROR
;
204 debug_warning("waiting for GPU");
207 assert(pipe_is_referenced(&fenced_buf
->base
.base
.reference
));
208 assert(fenced_buf
->fence
);
211 * Acquire the global lock. Must release buffer mutex first to preserve
214 pipe_mutex_unlock(fenced_buf
->mutex
);
215 pipe_mutex_lock(fenced_list
->mutex
);
216 pipe_mutex_lock(fenced_buf
->mutex
);
218 if(fenced_buf
->fence
) {
219 if(ops
->fence_finish(ops
, fenced_buf
->fence
, 0) == 0) {
220 /* Remove from the fenced list */
221 /* TODO: remove consequents */
222 fenced_buffer_remove_locked(fenced_list
, fenced_buf
);
224 p_atomic_dec(&fenced_buf
->base
.base
.reference
.count
);
225 assert(pipe_is_referenced(&fenced_buf
->base
.base
.reference
));
227 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
233 pipe_mutex_unlock(fenced_list
->mutex
);
240 * Free as many fenced buffers from the list head as possible.
243 fenced_buffer_list_check_free_locked(struct fenced_buffer_list
*fenced_list
,
246 struct pb_fence_ops
*ops
= fenced_list
->ops
;
247 struct list_head
*curr
, *next
;
248 struct fenced_buffer
*fenced_buf
;
249 struct pb_buffer
*pb_buf
;
250 struct pipe_fence_handle
*prev_fence
= NULL
;
252 curr
= fenced_list
->delayed
.next
;
254 while(curr
!= &fenced_list
->delayed
) {
255 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
257 pipe_mutex_lock(fenced_buf
->mutex
);
259 if(fenced_buf
->fence
!= prev_fence
) {
262 signaled
= ops
->fence_finish(ops
, fenced_buf
->fence
, 0);
264 signaled
= ops
->fence_signalled(ops
, fenced_buf
->fence
, 0);
266 pipe_mutex_unlock(fenced_buf
->mutex
);
269 prev_fence
= fenced_buf
->fence
;
272 assert(ops
->fence_signalled(ops
, fenced_buf
->fence
, 0) == 0);
275 fenced_buffer_remove_locked(fenced_list
, fenced_buf
);
276 pipe_mutex_unlock(fenced_buf
->mutex
);
278 pb_buf
= &fenced_buf
->base
;
279 pb_reference(&pb_buf
, NULL
);
288 fenced_buffer_destroy(struct pb_buffer
*buf
)
290 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
291 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
293 assert(!pipe_is_referenced(&fenced_buf
->base
.base
.reference
));
294 assert(!fenced_buf
->fence
);
297 pipe_mutex_lock(fenced_list
->mutex
);
298 assert(fenced_buf
->head
.prev
);
299 assert(fenced_buf
->head
.next
);
300 LIST_DEL(&fenced_buf
->head
);
301 assert(fenced_list
->numUnfenced
);
302 --fenced_list
->numUnfenced
;
303 pipe_mutex_unlock(fenced_list
->mutex
);
308 pb_reference(&fenced_buf
->buffer
, NULL
);
310 pipe_mutex_destroy(fenced_buf
->mutex
);
316 fenced_buffer_map(struct pb_buffer
*buf
,
319 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
320 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
321 struct pb_fence_ops
*ops
= fenced_list
->ops
;
324 pipe_mutex_lock(fenced_buf
->mutex
);
326 assert(!(flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
));
328 /* Serialize writes */
329 if((fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_WRITE
) ||
330 ((fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_READ
) && (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
))) {
331 if((flags
& PIPE_BUFFER_USAGE_DONTBLOCK
) &&
332 ops
->fence_signalled(ops
, fenced_buf
->fence
, 0) == 0) {
333 /* Don't wait for the GPU to finish writing */
337 /* Wait for the GPU to finish writing */
338 fenced_buffer_finish_locked(fenced_list
, fenced_buf
);
342 /* Check for CPU write access (read is OK) */
343 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
344 /* this is legal -- just for debugging */
345 debug_warning("concurrent CPU writes");
349 map
= pb_map(fenced_buf
->buffer
, flags
);
351 ++fenced_buf
->mapcount
;
352 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
356 pipe_mutex_unlock(fenced_buf
->mutex
);
363 fenced_buffer_unmap(struct pb_buffer
*buf
)
365 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
367 pipe_mutex_lock(fenced_buf
->mutex
);
369 assert(fenced_buf
->mapcount
);
370 if(fenced_buf
->mapcount
) {
371 pb_unmap(fenced_buf
->buffer
);
372 --fenced_buf
->mapcount
;
373 if(!fenced_buf
->mapcount
)
374 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
377 pipe_mutex_unlock(fenced_buf
->mutex
);
381 static enum pipe_error
382 fenced_buffer_validate(struct pb_buffer
*buf
,
383 struct pb_validate
*vl
,
386 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
389 pipe_mutex_lock(fenced_buf
->mutex
);
393 fenced_buf
->vl
= NULL
;
394 fenced_buf
->validation_flags
= 0;
399 assert(flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
400 assert(!(flags
& ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
));
401 flags
&= PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
403 /* Buffer cannot be validated in two different lists */
404 if(fenced_buf
->vl
&& fenced_buf
->vl
!= vl
) {
405 ret
= PIPE_ERROR_RETRY
;
410 /* Do not validate if buffer is still mapped */
411 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
412 /* TODO: wait for the thread that mapped the buffer to unmap it */
413 ret
= PIPE_ERROR_RETRY
;
416 /* Final sanity checking */
417 assert(!(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
418 assert(!fenced_buf
->mapcount
);
421 if(fenced_buf
->vl
== vl
&&
422 (fenced_buf
->validation_flags
& flags
) == flags
) {
423 /* Nothing to do -- buffer already validated */
428 ret
= pb_validate(fenced_buf
->buffer
, vl
, flags
);
433 fenced_buf
->validation_flags
|= flags
;
436 pipe_mutex_unlock(fenced_buf
->mutex
);
443 fenced_buffer_fence(struct pb_buffer
*buf
,
444 struct pipe_fence_handle
*fence
)
446 struct fenced_buffer
*fenced_buf
;
447 struct fenced_buffer_list
*fenced_list
;
448 struct pb_fence_ops
*ops
;
450 fenced_buf
= fenced_buffer(buf
);
451 fenced_list
= fenced_buf
->list
;
452 ops
= fenced_list
->ops
;
454 pipe_mutex_lock(fenced_list
->mutex
);
455 pipe_mutex_lock(fenced_buf
->mutex
);
457 assert(pipe_is_referenced(&fenced_buf
->base
.base
.reference
));
459 if(fence
!= fenced_buf
->fence
) {
460 assert(fenced_buf
->vl
);
461 assert(fenced_buf
->validation_flags
);
463 if (fenced_buf
->fence
) {
464 fenced_buffer_remove_locked(fenced_list
, fenced_buf
);
465 p_atomic_dec(&fenced_buf
->base
.base
.reference
.count
);
466 assert(pipe_is_referenced(&fenced_buf
->base
.base
.reference
));
469 ops
->fence_reference(ops
, &fenced_buf
->fence
, fence
);
470 fenced_buf
->flags
|= fenced_buf
->validation_flags
;
471 p_atomic_inc(&fenced_buf
->base
.base
.reference
.count
);
472 fenced_buffer_add_locked(fenced_list
, fenced_buf
);
475 pb_fence(fenced_buf
->buffer
, fence
);
477 fenced_buf
->vl
= NULL
;
478 fenced_buf
->validation_flags
= 0;
481 pipe_mutex_unlock(fenced_buf
->mutex
);
482 pipe_mutex_unlock(fenced_list
->mutex
);
487 fenced_buffer_get_base_buffer(struct pb_buffer
*buf
,
488 struct pb_buffer
**base_buf
,
491 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
492 /* NOTE: accesses immutable members only -- mutex not necessary */
493 pb_get_base_buffer(fenced_buf
->buffer
, base_buf
, offset
);
497 static const struct pb_vtbl
498 fenced_buffer_vtbl
= {
499 fenced_buffer_destroy
,
502 fenced_buffer_validate
,
504 fenced_buffer_get_base_buffer
509 fenced_buffer_create(struct fenced_buffer_list
*fenced_list
,
510 struct pb_buffer
*buffer
)
512 struct fenced_buffer
*buf
;
517 buf
= CALLOC_STRUCT(fenced_buffer
);
519 pb_reference(&buffer
, NULL
);
523 pipe_reference_init(&buf
->base
.base
.reference
, 1);
524 buf
->base
.base
.alignment
= buffer
->base
.alignment
;
525 buf
->base
.base
.usage
= buffer
->base
.usage
;
526 buf
->base
.base
.size
= buffer
->base
.size
;
528 buf
->base
.vtbl
= &fenced_buffer_vtbl
;
529 buf
->buffer
= buffer
;
530 buf
->list
= fenced_list
;
532 pipe_mutex_init(buf
->mutex
);
535 pipe_mutex_lock(fenced_list
->mutex
);
536 LIST_ADDTAIL(&buf
->head
, &fenced_list
->unfenced
);
537 ++fenced_list
->numUnfenced
;
538 pipe_mutex_unlock(fenced_list
->mutex
);
545 struct fenced_buffer_list
*
546 fenced_buffer_list_create(struct pb_fence_ops
*ops
)
548 struct fenced_buffer_list
*fenced_list
;
550 fenced_list
= CALLOC_STRUCT(fenced_buffer_list
);
554 fenced_list
->ops
= ops
;
556 LIST_INITHEAD(&fenced_list
->delayed
);
557 fenced_list
->numDelayed
= 0;
560 LIST_INITHEAD(&fenced_list
->unfenced
);
561 fenced_list
->numUnfenced
= 0;
564 pipe_mutex_init(fenced_list
->mutex
);
571 fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
574 pipe_mutex_lock(fenced_list
->mutex
);
575 fenced_buffer_list_check_free_locked(fenced_list
, wait
);
576 pipe_mutex_unlock(fenced_list
->mutex
);
582 fenced_buffer_list_dump(struct fenced_buffer_list
*fenced_list
)
584 struct pb_fence_ops
*ops
= fenced_list
->ops
;
585 struct list_head
*curr
, *next
;
586 struct fenced_buffer
*fenced_buf
;
588 pipe_mutex_lock(fenced_list
->mutex
);
590 debug_printf("%10s %7s %7s %10s %s\n",
591 "buffer", "size", "refcount", "fence", "signalled");
593 curr
= fenced_list
->unfenced
.next
;
595 while(curr
!= &fenced_list
->unfenced
) {
596 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
597 pipe_mutex_lock(fenced_buf
->mutex
);
598 assert(!fenced_buf
->fence
);
599 debug_printf("%10p %7u %7u\n",
601 fenced_buf
->base
.base
.size
,
602 p_atomic_read(&fenced_buf
->base
.base
.reference
.count
));
603 pipe_mutex_unlock(fenced_buf
->mutex
);
608 curr
= fenced_list
->delayed
.next
;
610 while(curr
!= &fenced_list
->delayed
) {
612 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
613 pipe_mutex_lock(fenced_buf
->mutex
);
614 signaled
= ops
->fence_signalled(ops
, fenced_buf
->fence
, 0);
615 debug_printf("%10p %7u %7u %10p %s\n",
617 fenced_buf
->base
.base
.size
,
618 p_atomic_read(&fenced_buf
->base
.base
.reference
.count
),
619 (void *) fenced_buf
->fence
,
620 signaled
== 0 ? "y" : "n");
621 pipe_mutex_unlock(fenced_buf
->mutex
);
626 pipe_mutex_unlock(fenced_list
->mutex
);
632 fenced_buffer_list_destroy(struct fenced_buffer_list
*fenced_list
)
634 pipe_mutex_lock(fenced_list
->mutex
);
636 /* Wait on outstanding fences */
637 while (fenced_list
->numDelayed
) {
638 pipe_mutex_unlock(fenced_list
->mutex
);
639 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
642 pipe_mutex_lock(fenced_list
->mutex
);
643 fenced_buffer_list_check_free_locked(fenced_list
, 1);
647 /*assert(!fenced_list->numUnfenced);*/
650 pipe_mutex_unlock(fenced_list
->mutex
);
651 pipe_mutex_destroy(fenced_list
->mutex
);
653 fenced_list
->ops
->destroy(fenced_list
->ops
);