pb_buffer_fenced.c revision dbab39c6caacb974062ac574b365254412aea412
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 void *map; 288 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; 292 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); 297 } 298 299#if 0 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"); 304 } 305#endif 306 307 map = pb_map(fenced_buf->buffer, flags); 308 if(map) { 309 ++fenced_buf->mapcount; 310 fenced_buf->flags |= flags; 311 } 312 313 return map; 314} 315 316 317static void 318fenced_buffer_unmap(struct pb_buffer *buf) 319{ 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; 327 } 328} 329 330 331static enum pipe_error 332fenced_buffer_validate(struct pb_buffer *buf, 333 struct pb_validate *vl, 334 unsigned flags) 335{ 336 struct fenced_buffer *fenced_buf = fenced_buffer(buf); 337 enum pipe_error ret; 338 339 if(!vl) { 340 /* invalidate */ 341 fenced_buf->vl = NULL; 342 fenced_buf->validation_flags = 0; 343 return PIPE_OK; 344 } 345 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; 349 350 /* Buffer cannot be validated in two different lists */ 351 if(fenced_buf->vl && fenced_buf->vl != vl) 352 return PIPE_ERROR_RETRY; 353 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; 358 } 359 360 if(fenced_buf->vl == vl && 361 (fenced_buf->validation_flags & flags) == flags) { 362 /* Nothing to do -- buffer already validated */ 363 return PIPE_OK; 364 } 365 366 /* Final sanity checking */ 367 assert(!(fenced_buf->flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE)); 368 assert(!fenced_buf->mapcount); 369 370 ret = pb_validate(fenced_buf->buffer, vl, flags); 371 if (ret != PIPE_OK) 372 return ret; 373 374 fenced_buf->vl = vl; 375 fenced_buf->validation_flags |= flags; 376 377 return PIPE_OK; 378} 379 380 381static void 382fenced_buffer_fence(struct pb_buffer *buf, 383 struct pipe_fence_handle *fence) 384{ 385 struct fenced_buffer *fenced_buf; 386 struct fenced_buffer_list *fenced_list; 387 struct pb_fence_ops *ops; 388 389 fenced_buf = fenced_buffer(buf); 390 fenced_list = fenced_buf->list; 391 ops = fenced_list->ops; 392 393 if(fence == fenced_buf->fence) { 394 /* Nothing to do */ 395 return; 396 } 397 398 assert(fenced_buf->vl); 399 assert(fenced_buf->validation_flags); 400 401 pipe_mutex_lock(fenced_list->mutex); 402 if (fenced_buf->fence) 403 _fenced_buffer_remove(fenced_list, fenced_buf); 404 if (fence) { 405 ops->fence_reference(ops, &fenced_buf->fence, fence); 406 fenced_buf->flags |= fenced_buf->validation_flags; 407 _fenced_buffer_add(fenced_buf); 408 } 409 pipe_mutex_unlock(fenced_list->mutex); 410 411 pb_fence(fenced_buf->buffer, fence); 412 413 fenced_buf->vl = NULL; 414 fenced_buf->validation_flags = 0; 415} 416 417 418static void 419fenced_buffer_get_base_buffer(struct pb_buffer *buf, 420 struct pb_buffer **base_buf, 421 unsigned *offset) 422{ 423 struct fenced_buffer *fenced_buf = fenced_buffer(buf); 424 pb_get_base_buffer(fenced_buf->buffer, base_buf, offset); 425} 426 427 428static const struct pb_vtbl 429fenced_buffer_vtbl = { 430 fenced_buffer_destroy, 431 fenced_buffer_map, 432 fenced_buffer_unmap, 433 fenced_buffer_validate, 434 fenced_buffer_fence, 435 fenced_buffer_get_base_buffer 436}; 437 438 439struct pb_buffer * 440fenced_buffer_create(struct fenced_buffer_list *fenced_list, 441 struct pb_buffer *buffer) 442{ 443 struct fenced_buffer *buf; 444 445 if(!buffer) 446 return NULL; 447 448 buf = CALLOC_STRUCT(fenced_buffer); 449 if(!buf) { 450 pb_reference(&buffer, NULL); 451 return NULL; 452 } 453 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; 458 459 buf->base.vtbl = &fenced_buffer_vtbl; 460 buf->buffer = buffer; 461 buf->list = fenced_list; 462 463#ifdef DEBUG 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); 468#endif 469 470 return &buf->base; 471} 472 473 474struct fenced_buffer_list * 475fenced_buffer_list_create(struct pb_fence_ops *ops) 476{ 477 struct fenced_buffer_list *fenced_list; 478 479 fenced_list = CALLOC_STRUCT(fenced_buffer_list); 480 if (!fenced_list) 481 return NULL; 482 483 fenced_list->ops = ops; 484 485 LIST_INITHEAD(&fenced_list->delayed); 486 fenced_list->numDelayed = 0; 487 488#ifdef DEBUG 489 LIST_INITHEAD(&fenced_list->unfenced); 490 fenced_list->numUnfenced = 0; 491#endif 492 493 pipe_mutex_init(fenced_list->mutex); 494 495 return fenced_list; 496} 497 498 499void 500fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list, 501 int wait) 502{ 503 pipe_mutex_lock(fenced_list->mutex); 504 _fenced_buffer_list_check_free(fenced_list, wait); 505 pipe_mutex_unlock(fenced_list->mutex); 506} 507 508 509#ifdef DEBUG 510void 511fenced_buffer_list_dump(struct fenced_buffer_list *fenced_list) 512{ 513 struct pb_fence_ops *ops = fenced_list->ops; 514 struct list_head *curr, *next; 515 struct fenced_buffer *fenced_buf; 516 517 pipe_mutex_lock(fenced_list->mutex); 518 519 debug_printf("%10s %7s %10s %s\n", 520 "buffer", "refcount", "fence", "signalled"); 521 522 curr = fenced_list->unfenced.next; 523 next = curr->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", 528 fenced_buf, 529 fenced_buf->base.base.refcount); 530 curr = next; 531 next = curr->next; 532 } 533 534 curr = fenced_list->delayed.next; 535 next = curr->next; 536 while(curr != &fenced_list->delayed) { 537 int signaled; 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", 541 fenced_buf, 542 fenced_buf->base.base.refcount, 543 fenced_buf->fence, 544 signaled == 0 ? "y" : "n"); 545 curr = next; 546 next = curr->next; 547 } 548 549 pipe_mutex_unlock(fenced_list->mutex); 550} 551#endif 552 553 554void 555fenced_buffer_list_destroy(struct fenced_buffer_list *fenced_list) 556{ 557 pipe_mutex_lock(fenced_list->mutex); 558 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) 563 sched_yield(); 564#endif 565 _fenced_buffer_list_check_free(fenced_list, 1); 566 pipe_mutex_lock(fenced_list->mutex); 567 } 568 569#ifdef DEBUG 570 //assert(!fenced_list->numUnfenced); 571#endif 572 573 pipe_mutex_unlock(fenced_list->mutex); 574 575 fenced_list->ops->destroy(fenced_list->ops); 576 577 FREE(fenced_list); 578} 579 580 581