gallium: Remove remnants of reference counting internals outside of p_refcnt.h.
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_buffer_fenced.c
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
62 struct 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 */
81 struct 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
105 static INLINE struct fenced_buffer *
106 fenced_buffer(struct pb_buffer *buf)
107 {
108 assert(buf);
109 return (struct fenced_buffer *)buf;
110 }
111
112
113 static 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(pipe_is_referenced(&fenced_buf->base.base.reference));
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 */
135 static 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(!pipe_is_referenced(&fenced_buf->base.base.reference));
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
156 static 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 /**
181 * FIXME!!!
182 */
183
184 if(!pipe_is_referenced(&fenced_buf->base.base.reference))
185 _fenced_buffer_destroy(fenced_buf);
186 }
187
188
189 static INLINE enum pipe_error
190 _fenced_buffer_finish(struct fenced_buffer *fenced_buf)
191 {
192 struct fenced_buffer_list *fenced_list = fenced_buf->list;
193 struct pb_fence_ops *ops = fenced_list->ops;
194
195 #if 0
196 debug_warning("waiting for GPU");
197 #endif
198
199 assert(fenced_buf->fence);
200 if(fenced_buf->fence) {
201 if(ops->fence_finish(ops, fenced_buf->fence, 0) != 0) {
202 return PIPE_ERROR;
203 }
204 /* Remove from the fenced list */
205 /* TODO: remove consequents */
206 _fenced_buffer_remove(fenced_list, fenced_buf);
207 }
208
209 fenced_buf->flags &= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE;
210 return PIPE_OK;
211 }
212
213
214 /**
215 * Free as many fenced buffers from the list head as possible.
216 */
217 static void
218 _fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list,
219 int wait)
220 {
221 struct pb_fence_ops *ops = fenced_list->ops;
222 struct list_head *curr, *next;
223 struct fenced_buffer *fenced_buf;
224 struct pipe_fence_handle *prev_fence = NULL;
225
226 curr = fenced_list->delayed.next;
227 next = curr->next;
228 while(curr != &fenced_list->delayed) {
229 fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
230
231 if(fenced_buf->fence != prev_fence) {
232 int signaled;
233 if (wait)
234 signaled = ops->fence_finish(ops, fenced_buf->fence, 0);
235 else
236 signaled = ops->fence_signalled(ops, fenced_buf->fence, 0);
237 if (signaled != 0)
238 break;
239 prev_fence = fenced_buf->fence;
240 }
241 else {
242 assert(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0);
243 }
244
245 _fenced_buffer_remove(fenced_list, fenced_buf);
246
247 curr = next;
248 next = curr->next;
249 }
250 }
251
252
253 static void
254 fenced_buffer_destroy(struct pb_buffer *buf)
255 {
256 struct fenced_buffer *fenced_buf = fenced_buffer(buf);
257 struct fenced_buffer_list *fenced_list = fenced_buf->list;
258
259 pipe_mutex_lock(fenced_list->mutex);
260 assert(!pipe_is_referenced(&fenced_buf->base.base.reference));
261 if (fenced_buf->fence) {
262 struct pb_fence_ops *ops = fenced_list->ops;
263 if(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0) {
264 struct list_head *curr, *prev;
265 curr = &fenced_buf->head;
266 prev = curr->prev;
267 do {
268 fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
269 assert(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0);
270 _fenced_buffer_remove(fenced_list, fenced_buf);
271 curr = prev;
272 prev = curr->prev;
273 } while (curr != &fenced_list->delayed);
274 }
275 else {
276 /* delay destruction */
277 }
278 }
279 else {
280 _fenced_buffer_destroy(fenced_buf);
281 }
282 pipe_mutex_unlock(fenced_list->mutex);
283 }
284
285
286 static void *
287 fenced_buffer_map(struct pb_buffer *buf,
288 unsigned flags)
289 {
290 struct fenced_buffer *fenced_buf = fenced_buffer(buf);
291 struct fenced_buffer_list *fenced_list = fenced_buf->list;
292 struct pb_fence_ops *ops = fenced_list->ops;
293 void *map;
294
295 assert(!(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE));
296
297 /* Serialize writes */
298 if((fenced_buf->flags & PIPE_BUFFER_USAGE_GPU_WRITE) ||
299 ((fenced_buf->flags & PIPE_BUFFER_USAGE_GPU_READ) && (flags & PIPE_BUFFER_USAGE_CPU_WRITE))) {
300 if(flags & PIPE_BUFFER_USAGE_DONTBLOCK) {
301 /* Don't wait for the GPU to finish writing */
302 if(ops->fence_signalled(ops, fenced_buf->fence, 0) == 0)
303 _fenced_buffer_remove(fenced_list, fenced_buf);
304 else
305 return NULL;
306 }
307 else {
308 /* Wait for the GPU to finish writing */
309 _fenced_buffer_finish(fenced_buf);
310 }
311 }
312
313 #if 0
314 /* Check for CPU write access (read is OK) */
315 if(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE) {
316 /* this is legal -- just for debugging */
317 debug_warning("concurrent CPU writes");
318 }
319 #endif
320
321 map = pb_map(fenced_buf->buffer, flags);
322 if(map) {
323 ++fenced_buf->mapcount;
324 fenced_buf->flags |= flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE;
325 }
326
327 return map;
328 }
329
330
331 static void
332 fenced_buffer_unmap(struct pb_buffer *buf)
333 {
334 struct fenced_buffer *fenced_buf = fenced_buffer(buf);
335 assert(fenced_buf->mapcount);
336 if(fenced_buf->mapcount) {
337 pb_unmap(fenced_buf->buffer);
338 --fenced_buf->mapcount;
339 if(!fenced_buf->mapcount)
340 fenced_buf->flags &= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE;
341 }
342 }
343
344
345 static enum pipe_error
346 fenced_buffer_validate(struct pb_buffer *buf,
347 struct pb_validate *vl,
348 unsigned flags)
349 {
350 struct fenced_buffer *fenced_buf = fenced_buffer(buf);
351 enum pipe_error ret;
352
353 if(!vl) {
354 /* invalidate */
355 fenced_buf->vl = NULL;
356 fenced_buf->validation_flags = 0;
357 return PIPE_OK;
358 }
359
360 assert(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE);
361 assert(!(flags & ~PIPE_BUFFER_USAGE_GPU_READ_WRITE));
362 flags &= PIPE_BUFFER_USAGE_GPU_READ_WRITE;
363
364 /* Buffer cannot be validated in two different lists */
365 if(fenced_buf->vl && fenced_buf->vl != vl)
366 return PIPE_ERROR_RETRY;
367
368 /* Do not validate if buffer is still mapped */
369 if(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE) {
370 /* TODO: wait for the thread that mapped the buffer to unmap it */
371 return PIPE_ERROR_RETRY;
372 }
373
374 if(fenced_buf->vl == vl &&
375 (fenced_buf->validation_flags & flags) == flags) {
376 /* Nothing to do -- buffer already validated */
377 return PIPE_OK;
378 }
379
380 /* Final sanity checking */
381 assert(!(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE));
382 assert(!fenced_buf->mapcount);
383
384 ret = pb_validate(fenced_buf->buffer, vl, flags);
385 if (ret != PIPE_OK)
386 return ret;
387
388 fenced_buf->vl = vl;
389 fenced_buf->validation_flags |= flags;
390
391 return PIPE_OK;
392 }
393
394
395 static void
396 fenced_buffer_fence(struct pb_buffer *buf,
397 struct pipe_fence_handle *fence)
398 {
399 struct fenced_buffer *fenced_buf;
400 struct fenced_buffer_list *fenced_list;
401 struct pb_fence_ops *ops;
402
403 fenced_buf = fenced_buffer(buf);
404 fenced_list = fenced_buf->list;
405 ops = fenced_list->ops;
406
407 if(fence == fenced_buf->fence) {
408 /* Nothing to do */
409 return;
410 }
411
412 assert(fenced_buf->vl);
413 assert(fenced_buf->validation_flags);
414
415 pipe_mutex_lock(fenced_list->mutex);
416 if (fenced_buf->fence)
417 _fenced_buffer_remove(fenced_list, fenced_buf);
418 if (fence) {
419 ops->fence_reference(ops, &fenced_buf->fence, fence);
420 fenced_buf->flags |= fenced_buf->validation_flags;
421 _fenced_buffer_add(fenced_buf);
422 }
423 pipe_mutex_unlock(fenced_list->mutex);
424
425 pb_fence(fenced_buf->buffer, fence);
426
427 fenced_buf->vl = NULL;
428 fenced_buf->validation_flags = 0;
429 }
430
431
432 static void
433 fenced_buffer_get_base_buffer(struct pb_buffer *buf,
434 struct pb_buffer **base_buf,
435 unsigned *offset)
436 {
437 struct fenced_buffer *fenced_buf = fenced_buffer(buf);
438 pb_get_base_buffer(fenced_buf->buffer, base_buf, offset);
439 }
440
441
442 static const struct pb_vtbl
443 fenced_buffer_vtbl = {
444 fenced_buffer_destroy,
445 fenced_buffer_map,
446 fenced_buffer_unmap,
447 fenced_buffer_validate,
448 fenced_buffer_fence,
449 fenced_buffer_get_base_buffer
450 };
451
452
453 struct pb_buffer *
454 fenced_buffer_create(struct fenced_buffer_list *fenced_list,
455 struct pb_buffer *buffer)
456 {
457 struct fenced_buffer *buf;
458
459 if(!buffer)
460 return NULL;
461
462 buf = CALLOC_STRUCT(fenced_buffer);
463 if(!buf) {
464 pb_reference(&buffer, NULL);
465 return NULL;
466 }
467
468 pipe_reference_init(&buf->base.base.reference, 1);
469 buf->base.base.alignment = buffer->base.alignment;
470 buf->base.base.usage = buffer->base.usage;
471 buf->base.base.size = buffer->base.size;
472
473 buf->base.vtbl = &fenced_buffer_vtbl;
474 buf->buffer = buffer;
475 buf->list = fenced_list;
476
477 #ifdef DEBUG
478 pipe_mutex_lock(fenced_list->mutex);
479 LIST_ADDTAIL(&buf->head, &fenced_list->unfenced);
480 ++fenced_list->numUnfenced;
481 pipe_mutex_unlock(fenced_list->mutex);
482 #endif
483
484 return &buf->base;
485 }
486
487
488 struct fenced_buffer_list *
489 fenced_buffer_list_create(struct pb_fence_ops *ops)
490 {
491 struct fenced_buffer_list *fenced_list;
492
493 fenced_list = CALLOC_STRUCT(fenced_buffer_list);
494 if (!fenced_list)
495 return NULL;
496
497 fenced_list->ops = ops;
498
499 LIST_INITHEAD(&fenced_list->delayed);
500 fenced_list->numDelayed = 0;
501
502 #ifdef DEBUG
503 LIST_INITHEAD(&fenced_list->unfenced);
504 fenced_list->numUnfenced = 0;
505 #endif
506
507 pipe_mutex_init(fenced_list->mutex);
508
509 return fenced_list;
510 }
511
512
513 void
514 fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list,
515 int wait)
516 {
517 pipe_mutex_lock(fenced_list->mutex);
518 _fenced_buffer_list_check_free(fenced_list, wait);
519 pipe_mutex_unlock(fenced_list->mutex);
520 }
521
522
523 #ifdef DEBUG
524 void
525 fenced_buffer_list_dump(struct fenced_buffer_list *fenced_list)
526 {
527 struct pb_fence_ops *ops = fenced_list->ops;
528 struct list_head *curr, *next;
529 struct fenced_buffer *fenced_buf;
530
531 pipe_mutex_lock(fenced_list->mutex);
532
533 debug_printf("%10s %7s %10s %s\n",
534 "buffer", "reference.count", "fence", "signalled");
535
536 curr = fenced_list->unfenced.next;
537 next = curr->next;
538 while(curr != &fenced_list->unfenced) {
539 fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
540 assert(!fenced_buf->fence);
541 debug_printf("%10p %7u\n",
542 fenced_buf,
543 fenced_buf->base.base.reference.count);
544 curr = next;
545 next = curr->next;
546 }
547
548 curr = fenced_list->delayed.next;
549 next = curr->next;
550 while(curr != &fenced_list->delayed) {
551 int signaled;
552 fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
553 signaled = ops->fence_signalled(ops, fenced_buf->fence, 0);
554 debug_printf("%10p %7u %10p %s\n",
555 fenced_buf,
556 fenced_buf->base.base.reference.count,
557 fenced_buf->fence,
558 signaled == 0 ? "y" : "n");
559 curr = next;
560 next = curr->next;
561 }
562
563 pipe_mutex_unlock(fenced_list->mutex);
564 }
565 #endif
566
567
568 void
569 fenced_buffer_list_destroy(struct fenced_buffer_list *fenced_list)
570 {
571 pipe_mutex_lock(fenced_list->mutex);
572
573 /* Wait on outstanding fences */
574 while (fenced_list->numDelayed) {
575 pipe_mutex_unlock(fenced_list->mutex);
576 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD)
577 sched_yield();
578 #endif
579 _fenced_buffer_list_check_free(fenced_list, 1);
580 pipe_mutex_lock(fenced_list->mutex);
581 }
582
583 #ifdef DEBUG
584 //assert(!fenced_list->numUnfenced);
585 #endif
586
587 pipe_mutex_unlock(fenced_list->mutex);
588
589 fenced_list->ops->destroy(fenced_list->ops);
590
591 FREE(fenced_list);
592 }
593
594