| 1 |
/* |
| 2 |
* Copyright (c) 2009-2024 Broadcom. All Rights Reserved. |
| 3 |
* The term “Broadcom” refers to Broadcom Inc. |
| 4 |
* and/or its subsidiaries. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
#include "svga_cmd.h" |
| 10 |
|
| 11 |
#include "util/u_debug.h" |
| 12 |
#include "util/u_memory.h" |
| 13 |
#include "util/u_debug_stack.h" |
| 14 |
#include "util/u_debug_flush.h" |
| 15 |
#include "util/u_hash_table.h" |
| 16 |
#include "util/u_bitmask.h" |
| 17 |
#include "util/u_atomic.h" |
| 18 |
#include "pipebuffer/pb_buffer.h" |
| 19 |
#include "pipebuffer/pb_validate.h" |
| 20 |
|
| 21 |
#include "svga_winsys.h" |
| 22 |
#include "vmw_context.h" |
| 23 |
#include "vmw_screen.h" |
| 24 |
#include "vmw_buffer.h" |
| 25 |
#include "vmw_surface.h" |
| 26 |
#include "vmw_fence.h" |
| 27 |
#include "vmw_shader.h" |
| 28 |
#include "vmw_query.h" |
| 29 |
|
| 30 |
#define VMW_COMMAND_SIZE (64*1024) |
| 31 |
#define VMW_SURFACE_RELOCS (1024) |
| 32 |
#define VMW_SHADER_RELOCS (1024) |
| 33 |
#define VMW_REGION_RELOCS (512) |
| 34 |
|
| 35 |
#define VMW_MUST_FLUSH_STACK 8 |
| 36 |
|
| 37 |
/* |
| 38 |
* A factor applied to the maximum mob memory size to determine |
| 39 |
* the optimial time to preemptively flush the command buffer. |
| 40 |
* The constant is based on some performance trials with SpecViewperf. |
| 41 |
*/ |
| 42 |
#define VMW_MAX_MOB_MEM_FACTOR 2 |
| 43 |
|
| 44 |
/* |
| 45 |
* A factor applied to the maximum surface memory size to determine |
| 46 |
* the optimial time to preemptively flush the command buffer. |
| 47 |
* The constant is based on some performance trials with SpecViewperf. |
| 48 |
*/ |
| 49 |
#define VMW_MAX_SURF_MEM_FACTOR 2 |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
struct vmw_buffer_relocation |
| 54 |
{ |
| 55 |
struct pb_buffer *buffer; |
| 56 |
bool is_mob; |
| 57 |
uint32 offset; |
| 58 |
|
| 59 |
union { |
| 60 |
struct { |
| 61 |
struct SVGAGuestPtr *where; |
| 62 |
} region; |
| 63 |
struct { |
| 64 |
SVGAMobId *id; |
| 65 |
uint32 *offset_into_mob; |
| 66 |
} mob; |
| 67 |
}; |
| 68 |
}; |
| 69 |
|
| 70 |
struct vmw_ctx_validate_item { |
| 71 |
union { |
| 72 |
struct vmw_svga_winsys_surface *vsurf; |
| 73 |
struct vmw_svga_winsys_shader *vshader; |
| 74 |
}; |
| 75 |
bool referenced; |
| 76 |
}; |
| 77 |
|
| 78 |
struct vmw_svga_winsys_context |
| 79 |
{ |
| 80 |
struct svga_winsys_context base; |
| 81 |
|
| 82 |
struct vmw_winsys_screen *vws; |
| 83 |
struct hash_table *hash; |
| 84 |
|
| 85 |
#if MESA_DEBUG |
| 86 |
bool must_flush; |
| 87 |
struct debug_stack_frame must_flush_stack[VMW_MUST_FLUSH_STACK]; |
| 88 |
struct debug_flush_ctx *fctx; |
| 89 |
#endif |
| 90 |
|
| 91 |
struct { |
| 92 |
uint8_t buffer[VMW_COMMAND_SIZE]; |
| 93 |
uint32_t size; |
| 94 |
uint32_t used; |
| 95 |
uint32_t reserved; |
| 96 |
} command; |
| 97 |
|
| 98 |
struct { |
| 99 |
struct vmw_ctx_validate_item items[VMW_SURFACE_RELOCS]; |
| 100 |
uint32_t size; |
| 101 |
uint32_t used; |
| 102 |
uint32_t staged; |
| 103 |
uint32_t reserved; |
| 104 |
} surface; |
| 105 |
|
| 106 |
struct { |
| 107 |
struct vmw_buffer_relocation relocs[VMW_REGION_RELOCS]; |
| 108 |
uint32_t size; |
| 109 |
uint32_t used; |
| 110 |
uint32_t staged; |
| 111 |
uint32_t reserved; |
| 112 |
} region; |
| 113 |
|
| 114 |
struct { |
| 115 |
struct vmw_ctx_validate_item items[VMW_SHADER_RELOCS]; |
| 116 |
uint32_t size; |
| 117 |
uint32_t used; |
| 118 |
uint32_t staged; |
| 119 |
uint32_t reserved; |
| 120 |
} shader; |
| 121 |
|
| 122 |
struct pb_validate *validate; |
| 123 |
|
| 124 |
/** |
| 125 |
* The amount of surface, GMR or MOB memory that is referred by the commands |
| 126 |
* currently batched in the context command buffer. |
| 127 |
*/ |
| 128 |
uint64_t seen_surfaces; |
| 129 |
uint64_t seen_regions; |
| 130 |
uint64_t seen_mobs; |
| 131 |
|
| 132 |
int32_t refcount; |
| 133 |
|
| 134 |
/* Bitmask of userspace managed surfaces */ |
| 135 |
struct util_bitmask *surface_id_bm; |
| 136 |
|
| 137 |
/** |
| 138 |
* Whether this context should fail to reserve more commands, not because it |
| 139 |
* ran out of command space, but because a substantial ammount of GMR was |
| 140 |
* referred. |
| 141 |
*/ |
| 142 |
bool preemptive_flush; |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
static inline struct vmw_svga_winsys_context * |
| 147 |
vmw_svga_winsys_context(struct svga_winsys_context *swc) |
| 148 |
{ |
| 149 |
assert(swc); |
| 150 |
return (struct vmw_svga_winsys_context *)swc; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
static inline enum pb_usage_flags |
| 155 |
vmw_translate_to_pb_flags(unsigned flags) |
| 156 |
{ |
| 157 |
enum pb_usage_flags f = 0; |
| 158 |
if (flags & SVGA_RELOC_READ) |
| 159 |
f |= PB_USAGE_GPU_READ; |
| 160 |
|
| 161 |
if (flags & SVGA_RELOC_WRITE) |
| 162 |
f |= PB_USAGE_GPU_WRITE; |
| 163 |
|
| 164 |
return f; |
| 165 |
} |
| 166 |
|
| 167 |
static enum pipe_error |
| 168 |
vmw_swc_flush(struct svga_winsys_context *swc, |
| 169 |
struct pipe_fence_handle **pfence) |
| 170 |
{ |
| 171 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 172 |
struct vmw_winsys_screen *vws = vswc->vws; |
| 173 |
struct pipe_fence_handle *fence = NULL; |
| 174 |
unsigned i; |
| 175 |
enum pipe_error ret; |
| 176 |
|
| 177 |
/* |
| 178 |
* If we hit a retry, lock the mutex and retry immediately. |
| 179 |
* If we then still hit a retry, sleep until another thread |
| 180 |
* wakes us up after it has released its buffers from the |
| 181 |
* validate list. |
| 182 |
* |
| 183 |
* If we hit another error condition, we still need to broadcast since |
| 184 |
* pb_validate_validate releases validated buffers in its error path. |
| 185 |
*/ |
| 186 |
|
| 187 |
ret = pb_validate_validate(vswc->validate); |
| 188 |
if (ret != PIPE_OK) { |
| 189 |
mtx_lock(&vws->cs_mutex); |
| 190 |
while (ret == PIPE_ERROR_RETRY) { |
| 191 |
ret = pb_validate_validate(vswc->validate); |
| 192 |
if (ret == PIPE_ERROR_RETRY) { |
| 193 |
cnd_wait(&vws->cs_cond, &vws->cs_mutex); |
| 194 |
} |
| 195 |
} |
| 196 |
if (ret != PIPE_OK) { |
| 197 |
cnd_broadcast(&vws->cs_cond); |
| 198 |
} |
| 199 |
mtx_unlock(&vws->cs_mutex); |
| 200 |
} |
| 201 |
|
| 202 |
assert(ret == PIPE_OK); |
| 203 |
if (ret == PIPE_OK) { |
| 204 |
/* Apply relocations */ |
| 205 |
for (i = 0; i < vswc->region.used; ++i) { |
| 206 |
struct vmw_buffer_relocation *reloc = &vswc->region.relocs[i]; |
| 207 |
struct SVGAGuestPtr ptr; |
| 208 |
|
| 209 |
if (!vmw_dma_bufmgr_region_ptr(reloc->buffer, &ptr)) |
| 210 |
assert(0); |
| 211 |
|
| 212 |
ptr.offset += reloc->offset; |
| 213 |
|
| 214 |
if (reloc->is_mob) { |
| 215 |
if (reloc->mob.id) { |
| 216 |
*reloc->mob.id = ptr.gmrId; |
| 217 |
} |
| 218 |
if (reloc->mob.offset_into_mob) { |
| 219 |
*reloc->mob.offset_into_mob = ptr.offset; |
| 220 |
} else { |
| 221 |
assert(ptr.offset == 0); |
| 222 |
} |
| 223 |
} else { |
| 224 |
*reloc->region.where = ptr; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (vswc->command.used || pfence != NULL) |
| 229 |
vmw_ioctl_command(vws, |
| 230 |
vswc->base.cid, |
| 231 |
0, |
| 232 |
vswc->command.buffer, |
| 233 |
vswc->command.used, |
| 234 |
&fence, |
| 235 |
vswc->base.imported_fence_fd, |
| 236 |
vswc->base.hints); |
| 237 |
|
| 238 |
pb_validate_fence(vswc->validate, fence); |
| 239 |
mtx_lock(&vws->cs_mutex); |
| 240 |
cnd_broadcast(&vws->cs_cond); |
| 241 |
mtx_unlock(&vws->cs_mutex); |
| 242 |
} |
| 243 |
|
| 244 |
vswc->command.used = 0; |
| 245 |
vswc->command.reserved = 0; |
| 246 |
|
| 247 |
for (i = 0; i < vswc->surface.used + vswc->surface.staged; ++i) { |
| 248 |
struct vmw_ctx_validate_item *isurf = &vswc->surface.items[i]; |
| 249 |
if (isurf->referenced) |
| 250 |
p_atomic_dec(&isurf->vsurf->validated); |
| 251 |
vmw_svga_winsys_surface_reference(&isurf->vsurf, NULL); |
| 252 |
} |
| 253 |
|
| 254 |
_mesa_hash_table_clear(vswc->hash, NULL); |
| 255 |
vswc->surface.used = 0; |
| 256 |
vswc->surface.reserved = 0; |
| 257 |
|
| 258 |
for (i = 0; i < vswc->shader.used + vswc->shader.staged; ++i) { |
| 259 |
struct vmw_ctx_validate_item *ishader = &vswc->shader.items[i]; |
| 260 |
if (ishader->referenced) |
| 261 |
p_atomic_dec(&ishader->vshader->validated); |
| 262 |
vmw_svga_winsys_shader_reference(&ishader->vshader, NULL); |
| 263 |
} |
| 264 |
|
| 265 |
vswc->shader.used = 0; |
| 266 |
vswc->shader.reserved = 0; |
| 267 |
|
| 268 |
vswc->region.used = 0; |
| 269 |
vswc->region.reserved = 0; |
| 270 |
|
| 271 |
#if MESA_DEBUG |
| 272 |
vswc->must_flush = false; |
| 273 |
debug_flush_flush(vswc->fctx); |
| 274 |
#endif |
| 275 |
swc->hints &= ~SVGA_HINT_FLAG_CAN_PRE_FLUSH; |
| 276 |
swc->hints &= ~SVGA_HINT_FLAG_EXPORT_FENCE_FD; |
| 277 |
vswc->preemptive_flush = false; |
| 278 |
vswc->seen_surfaces = 0; |
| 279 |
vswc->seen_regions = 0; |
| 280 |
vswc->seen_mobs = 0; |
| 281 |
|
| 282 |
if (vswc->base.imported_fence_fd != -1) { |
| 283 |
close(vswc->base.imported_fence_fd); |
| 284 |
vswc->base.imported_fence_fd = -1; |
| 285 |
} |
| 286 |
|
| 287 |
if (pfence) |
| 288 |
vmw_fence_reference(vswc->vws, pfence, fence); |
| 289 |
|
| 290 |
vmw_fence_reference(vswc->vws, &fence, NULL); |
| 291 |
|
| 292 |
return ret; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
static void * |
| 297 |
vmw_swc_reserve(struct svga_winsys_context *swc, |
| 298 |
uint32_t nr_bytes, uint32_t nr_relocs ) |
| 299 |
{ |
| 300 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 301 |
|
| 302 |
#if MESA_DEBUG |
| 303 |
/* Check if somebody forgot to check the previous failure */ |
| 304 |
if (vswc->must_flush) { |
| 305 |
debug_printf("Forgot to flush:\n"); |
| 306 |
debug_backtrace_dump(vswc->must_flush_stack, VMW_MUST_FLUSH_STACK); |
| 307 |
assert(!vswc->must_flush); |
| 308 |
} |
| 309 |
debug_flush_might_flush(vswc->fctx); |
| 310 |
#endif |
| 311 |
|
| 312 |
assert(nr_bytes <= vswc->command.size); |
| 313 |
if (nr_bytes > vswc->command.size) |
| 314 |
return NULL; |
| 315 |
|
| 316 |
if (vswc->preemptive_flush || |
| 317 |
vswc->command.used + nr_bytes > vswc->command.size || |
| 318 |
vswc->surface.used + nr_relocs > vswc->surface.size || |
| 319 |
vswc->shader.used + nr_relocs > vswc->shader.size || |
| 320 |
vswc->region.used + nr_relocs > vswc->region.size) { |
| 321 |
#if MESA_DEBUG |
| 322 |
vswc->must_flush = true; |
| 323 |
debug_backtrace_capture(vswc->must_flush_stack, 1, |
| 324 |
VMW_MUST_FLUSH_STACK); |
| 325 |
#endif |
| 326 |
return NULL; |
| 327 |
} |
| 328 |
|
| 329 |
assert(vswc->command.used + nr_bytes <= vswc->command.size); |
| 330 |
assert(vswc->surface.used + nr_relocs <= vswc->surface.size); |
| 331 |
assert(vswc->shader.used + nr_relocs <= vswc->shader.size); |
| 332 |
assert(vswc->region.used + nr_relocs <= vswc->region.size); |
| 333 |
|
| 334 |
vswc->command.reserved = nr_bytes; |
| 335 |
vswc->surface.reserved = nr_relocs; |
| 336 |
vswc->surface.staged = 0; |
| 337 |
vswc->shader.reserved = nr_relocs; |
| 338 |
vswc->shader.staged = 0; |
| 339 |
vswc->region.reserved = nr_relocs; |
| 340 |
vswc->region.staged = 0; |
| 341 |
|
| 342 |
return vswc->command.buffer + vswc->command.used; |
| 343 |
} |
| 344 |
|
| 345 |
static unsigned |
| 346 |
vmw_swc_get_command_buffer_size(struct svga_winsys_context *swc) |
| 347 |
{ |
| 348 |
const struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 349 |
return vswc->command.used; |
| 350 |
} |
| 351 |
|
| 352 |
static void |
| 353 |
vmw_swc_context_relocation(struct svga_winsys_context *swc, |
| 354 |
uint32 *cid) |
| 355 |
{ |
| 356 |
*cid = swc->cid; |
| 357 |
} |
| 358 |
|
| 359 |
static bool |
| 360 |
vmw_swc_add_validate_buffer(struct vmw_svga_winsys_context *vswc, |
| 361 |
struct pb_buffer *pb_buf, |
| 362 |
unsigned flags) |
| 363 |
{ |
| 364 |
ASSERTED enum pipe_error ret; |
| 365 |
unsigned translated_flags; |
| 366 |
bool already_present; |
| 367 |
|
| 368 |
translated_flags = vmw_translate_to_pb_flags(flags); |
| 369 |
ret = pb_validate_add_buffer(vswc->validate, pb_buf, translated_flags, |
| 370 |
vswc->hash, &already_present); |
| 371 |
assert(ret == PIPE_OK); |
| 372 |
return !already_present; |
| 373 |
} |
| 374 |
|
| 375 |
static void |
| 376 |
vmw_swc_region_relocation(struct svga_winsys_context *swc, |
| 377 |
struct SVGAGuestPtr *where, |
| 378 |
struct svga_winsys_buffer *buffer, |
| 379 |
uint32 offset, |
| 380 |
unsigned flags) |
| 381 |
{ |
| 382 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 383 |
struct vmw_buffer_relocation *reloc; |
| 384 |
|
| 385 |
assert(vswc->region.staged < vswc->region.reserved); |
| 386 |
|
| 387 |
reloc = &vswc->region.relocs[vswc->region.used + vswc->region.staged]; |
| 388 |
reloc->region.where = where; |
| 389 |
|
| 390 |
/* |
| 391 |
* pb_validate holds a refcount to the buffer, so no need to |
| 392 |
* refcount it again in the relocation. |
| 393 |
*/ |
| 394 |
reloc->buffer = vmw_pb_buffer(buffer); |
| 395 |
reloc->offset = offset; |
| 396 |
reloc->is_mob = false; |
| 397 |
++vswc->region.staged; |
| 398 |
|
| 399 |
if (vmw_swc_add_validate_buffer(vswc, reloc->buffer, flags)) { |
| 400 |
vswc->seen_regions += reloc->buffer->base.size; |
| 401 |
if ((swc->hints & SVGA_HINT_FLAG_CAN_PRE_FLUSH) && |
| 402 |
vswc->seen_regions >= VMW_GMR_POOL_SIZE/5) |
| 403 |
vswc->preemptive_flush = true; |
| 404 |
} |
| 405 |
|
| 406 |
#if MESA_DEBUG |
| 407 |
if (!(flags & SVGA_RELOC_INTERNAL)) |
| 408 |
debug_flush_cb_reference(vswc->fctx, vmw_debug_flush_buf(buffer)); |
| 409 |
#endif |
| 410 |
} |
| 411 |
|
| 412 |
static void |
| 413 |
vmw_swc_mob_relocation(struct svga_winsys_context *swc, |
| 414 |
SVGAMobId *id, |
| 415 |
uint32 *offset_into_mob, |
| 416 |
struct svga_winsys_buffer *buffer, |
| 417 |
uint32 offset, |
| 418 |
unsigned flags) |
| 419 |
{ |
| 420 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 421 |
struct vmw_buffer_relocation *reloc; |
| 422 |
struct pb_buffer *pb_buffer = vmw_pb_buffer(buffer); |
| 423 |
|
| 424 |
if (id) { |
| 425 |
assert(vswc->region.staged < vswc->region.reserved); |
| 426 |
|
| 427 |
reloc = &vswc->region.relocs[vswc->region.used + vswc->region.staged]; |
| 428 |
reloc->mob.id = id; |
| 429 |
reloc->mob.offset_into_mob = offset_into_mob; |
| 430 |
|
| 431 |
/* |
| 432 |
* pb_validate holds a refcount to the buffer, so no need to |
| 433 |
* refcount it again in the relocation. |
| 434 |
*/ |
| 435 |
reloc->buffer = pb_buffer; |
| 436 |
reloc->offset = offset; |
| 437 |
reloc->is_mob = true; |
| 438 |
++vswc->region.staged; |
| 439 |
} |
| 440 |
|
| 441 |
if (vmw_swc_add_validate_buffer(vswc, pb_buffer, flags)) { |
| 442 |
vswc->seen_mobs += pb_buffer->base.size; |
| 443 |
|
| 444 |
if ((swc->hints & SVGA_HINT_FLAG_CAN_PRE_FLUSH) && |
| 445 |
vswc->seen_mobs >= |
| 446 |
vswc->vws->ioctl.max_mob_memory / VMW_MAX_MOB_MEM_FACTOR) |
| 447 |
vswc->preemptive_flush = true; |
| 448 |
} |
| 449 |
|
| 450 |
#if MESA_DEBUG |
| 451 |
if (!(flags & SVGA_RELOC_INTERNAL)) |
| 452 |
debug_flush_cb_reference(vswc->fctx, vmw_debug_flush_buf(buffer)); |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
/** |
| 458 |
* vmw_swc_surface_clear_reference - Clear referenced info for a surface |
| 459 |
* |
| 460 |
* @swc: Pointer to an svga_winsys_context |
| 461 |
* @vsurf: Pointer to a vmw_svga_winsys_surface, the referenced info of which |
| 462 |
* we want to clear |
| 463 |
* |
| 464 |
* This is primarily used by a discard surface map to indicate that the |
| 465 |
* surface data is no longer referenced by a draw call, and mapping it |
| 466 |
* should therefore no longer cause a flush. |
| 467 |
*/ |
| 468 |
void |
| 469 |
vmw_swc_surface_clear_reference(struct svga_winsys_context *swc, |
| 470 |
struct vmw_svga_winsys_surface *vsurf) |
| 471 |
{ |
| 472 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 473 |
struct vmw_ctx_validate_item *isrf = |
| 474 |
util_hash_table_get(vswc->hash, vsurf); |
| 475 |
|
| 476 |
if (isrf && isrf->referenced) { |
| 477 |
isrf->referenced = false; |
| 478 |
p_atomic_dec(&vsurf->validated); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
vmw_swc_surface_only_relocation(struct svga_winsys_context *swc, |
| 484 |
uint32 *where, |
| 485 |
struct vmw_svga_winsys_surface *vsurf, |
| 486 |
unsigned flags) |
| 487 |
{ |
| 488 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 489 |
struct vmw_ctx_validate_item *isrf; |
| 490 |
|
| 491 |
assert(vswc->surface.staged < vswc->surface.reserved); |
| 492 |
isrf = util_hash_table_get(vswc->hash, vsurf); |
| 493 |
|
| 494 |
if (isrf == NULL) { |
| 495 |
isrf = &vswc->surface.items[vswc->surface.used + vswc->surface.staged]; |
| 496 |
vmw_svga_winsys_surface_reference(&isrf->vsurf, vsurf); |
| 497 |
isrf->referenced = false; |
| 498 |
|
| 499 |
_mesa_hash_table_insert(vswc->hash, vsurf, isrf); |
| 500 |
++vswc->surface.staged; |
| 501 |
|
| 502 |
vswc->seen_surfaces += vsurf->size; |
| 503 |
if ((swc->hints & SVGA_HINT_FLAG_CAN_PRE_FLUSH) && |
| 504 |
vswc->seen_surfaces >= |
| 505 |
vswc->vws->ioctl.max_surface_memory / VMW_MAX_SURF_MEM_FACTOR) |
| 506 |
vswc->preemptive_flush = true; |
| 507 |
} |
| 508 |
|
| 509 |
if (!(flags & SVGA_RELOC_INTERNAL) && !isrf->referenced) { |
| 510 |
isrf->referenced = true; |
| 511 |
p_atomic_inc(&vsurf->validated); |
| 512 |
} |
| 513 |
|
| 514 |
if (where) |
| 515 |
*where = vsurf->sid; |
| 516 |
} |
| 517 |
|
| 518 |
static void |
| 519 |
vmw_swc_surface_relocation(struct svga_winsys_context *swc, |
| 520 |
uint32 *where, |
| 521 |
uint32 *mobid, |
| 522 |
struct svga_winsys_surface *surface, |
| 523 |
unsigned flags) |
| 524 |
{ |
| 525 |
struct vmw_svga_winsys_surface *vsurf; |
| 526 |
|
| 527 |
assert(swc->have_gb_objects || mobid == NULL); |
| 528 |
|
| 529 |
if (!surface) { |
| 530 |
*where = SVGA3D_INVALID_ID; |
| 531 |
if (mobid) |
| 532 |
*mobid = SVGA3D_INVALID_ID; |
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
vsurf = vmw_svga_winsys_surface(surface); |
| 537 |
vmw_swc_surface_only_relocation(swc, where, vsurf, flags); |
| 538 |
|
| 539 |
if (swc->have_gb_objects && vsurf->buf != NULL) { |
| 540 |
|
| 541 |
/* |
| 542 |
* Make sure backup buffer ends up fenced. |
| 543 |
*/ |
| 544 |
|
| 545 |
mtx_lock(&vsurf->mutex); |
| 546 |
assert(vsurf->buf != NULL); |
| 547 |
|
| 548 |
/* |
| 549 |
* An internal reloc means that the surface transfer direction |
| 550 |
* is opposite to the MOB transfer direction... |
| 551 |
*/ |
| 552 |
if ((flags & SVGA_RELOC_INTERNAL) && |
| 553 |
(flags & (SVGA_RELOC_READ | SVGA_RELOC_WRITE)) != |
| 554 |
(SVGA_RELOC_READ | SVGA_RELOC_WRITE)) |
| 555 |
flags ^= (SVGA_RELOC_READ | SVGA_RELOC_WRITE); |
| 556 |
vmw_swc_mob_relocation(swc, mobid, NULL, (struct svga_winsys_buffer *) |
| 557 |
vsurf->buf, 0, flags); |
| 558 |
mtx_unlock(&vsurf->mutex); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
static void |
| 563 |
vmw_swc_shader_relocation(struct svga_winsys_context *swc, |
| 564 |
uint32 *shid, |
| 565 |
uint32 *mobid, |
| 566 |
uint32 *offset, |
| 567 |
struct svga_winsys_gb_shader *shader, |
| 568 |
unsigned flags) |
| 569 |
{ |
| 570 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 571 |
struct vmw_winsys_screen *vws = vswc->vws; |
| 572 |
struct vmw_svga_winsys_shader *vshader; |
| 573 |
struct vmw_ctx_validate_item *ishader; |
| 574 |
|
| 575 |
if (!shader) { |
| 576 |
*shid = SVGA3D_INVALID_ID; |
| 577 |
return; |
| 578 |
} |
| 579 |
|
| 580 |
vshader = vmw_svga_winsys_shader(shader); |
| 581 |
|
| 582 |
if (!vws->base.have_vgpu10) { |
| 583 |
assert(vswc->shader.staged < vswc->shader.reserved); |
| 584 |
ishader = util_hash_table_get(vswc->hash, vshader); |
| 585 |
|
| 586 |
if (ishader == NULL) { |
| 587 |
ishader = &vswc->shader.items[vswc->shader.used + vswc->shader.staged]; |
| 588 |
vmw_svga_winsys_shader_reference(&ishader->vshader, vshader); |
| 589 |
ishader->referenced = false; |
| 590 |
|
| 591 |
_mesa_hash_table_insert(vswc->hash, vshader, ishader); |
| 592 |
++vswc->shader.staged; |
| 593 |
} |
| 594 |
|
| 595 |
if (!ishader->referenced) { |
| 596 |
ishader->referenced = true; |
| 597 |
p_atomic_inc(&vshader->validated); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
if (shid) |
| 602 |
*shid = vshader->shid; |
| 603 |
|
| 604 |
if (vshader->buf) |
| 605 |
vmw_swc_mob_relocation(swc, mobid, offset, vshader->buf, |
| 606 |
0, SVGA_RELOC_READ); |
| 607 |
} |
| 608 |
|
| 609 |
static void |
| 610 |
vmw_swc_query_relocation(struct svga_winsys_context *swc, |
| 611 |
SVGAMobId *id, |
| 612 |
struct svga_winsys_gb_query *query) |
| 613 |
{ |
| 614 |
/* Queries are backed by one big MOB */ |
| 615 |
vmw_swc_mob_relocation(swc, id, NULL, query->buf, 0, |
| 616 |
SVGA_RELOC_READ | SVGA_RELOC_WRITE); |
| 617 |
} |
| 618 |
|
| 619 |
static void |
| 620 |
vmw_swc_commit(struct svga_winsys_context *swc) |
| 621 |
{ |
| 622 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 623 |
|
| 624 |
assert(vswc->command.used + vswc->command.reserved <= vswc->command.size); |
| 625 |
vswc->command.used += vswc->command.reserved; |
| 626 |
vswc->command.reserved = 0; |
| 627 |
|
| 628 |
assert(vswc->surface.staged <= vswc->surface.reserved); |
| 629 |
assert(vswc->surface.used + vswc->surface.staged <= vswc->surface.size); |
| 630 |
vswc->surface.used += vswc->surface.staged; |
| 631 |
vswc->surface.staged = 0; |
| 632 |
vswc->surface.reserved = 0; |
| 633 |
|
| 634 |
assert(vswc->shader.staged <= vswc->shader.reserved); |
| 635 |
assert(vswc->shader.used + vswc->shader.staged <= vswc->shader.size); |
| 636 |
vswc->shader.used += vswc->shader.staged; |
| 637 |
vswc->shader.staged = 0; |
| 638 |
vswc->shader.reserved = 0; |
| 639 |
|
| 640 |
assert(vswc->region.staged <= vswc->region.reserved); |
| 641 |
assert(vswc->region.used + vswc->region.staged <= vswc->region.size); |
| 642 |
vswc->region.used += vswc->region.staged; |
| 643 |
vswc->region.staged = 0; |
| 644 |
vswc->region.reserved = 0; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
static void |
| 649 |
vmw_swc_destroy(struct svga_winsys_context *swc) |
| 650 |
{ |
| 651 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 652 |
unsigned i; |
| 653 |
|
| 654 |
for (i = 0; i < vswc->surface.used; ++i) { |
| 655 |
struct vmw_ctx_validate_item *isurf = &vswc->surface.items[i]; |
| 656 |
if (isurf->referenced) |
| 657 |
p_atomic_dec(&isurf->vsurf->validated); |
| 658 |
vmw_svga_winsys_surface_reference(&isurf->vsurf, NULL); |
| 659 |
} |
| 660 |
|
| 661 |
for (i = 0; i < vswc->shader.used; ++i) { |
| 662 |
struct vmw_ctx_validate_item *ishader = &vswc->shader.items[i]; |
| 663 |
if (ishader->referenced) |
| 664 |
p_atomic_dec(&ishader->vshader->validated); |
| 665 |
vmw_svga_winsys_shader_reference(&ishader->vshader, NULL); |
| 666 |
} |
| 667 |
|
| 668 |
if (vmw_has_userspace_surface(vswc->vws)) |
| 669 |
util_bitmask_destroy(vswc->surface_id_bm); |
| 670 |
_mesa_hash_table_destroy(vswc->hash, NULL); |
| 671 |
pb_validate_destroy(vswc->validate); |
| 672 |
vmw_ioctl_context_destroy(vswc->vws, swc->cid); |
| 673 |
if (vswc->vws->swc == swc) |
| 674 |
vswc->vws->swc = NULL; |
| 675 |
#if MESA_DEBUG |
| 676 |
debug_flush_ctx_destroy(vswc->fctx); |
| 677 |
#endif |
| 678 |
FREE(vswc); |
| 679 |
} |
| 680 |
|
| 681 |
void |
| 682 |
vmw_swc_unref(struct svga_winsys_context *swc) |
| 683 |
{ |
| 684 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 685 |
if (p_atomic_dec_zero(&vswc->refcount)) { |
| 686 |
vmw_swc_destroy(swc); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
/** |
| 691 |
* vmw_svga_winsys_vgpu10_shader_screate - The winsys shader_crate callback |
| 692 |
* |
| 693 |
* @swc: The winsys context. |
| 694 |
* @shaderId: Previously allocated shader id. |
| 695 |
* @shaderType: The shader type. |
| 696 |
* @bytecode: The shader bytecode |
| 697 |
* @bytecodelen: The length of the bytecode. |
| 698 |
* |
| 699 |
* Creates an svga_winsys_gb_shader structure and allocates a buffer for the |
| 700 |
* shader code and copies the shader code into the buffer. Shader |
| 701 |
* resource creation is not done. |
| 702 |
*/ |
| 703 |
static struct svga_winsys_gb_shader * |
| 704 |
vmw_svga_winsys_vgpu10_shader_create(struct svga_winsys_context *swc, |
| 705 |
uint32 shaderId, |
| 706 |
SVGA3dShaderType shaderType, |
| 707 |
const uint32 *bytecode, |
| 708 |
uint32 bytecodeLen, |
| 709 |
const SVGA3dDXShaderSignatureHeader *sgnInfo, |
| 710 |
uint32 sgnLen) |
| 711 |
{ |
| 712 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 713 |
struct vmw_svga_winsys_shader *shader; |
| 714 |
shader = vmw_svga_shader_create(&vswc->vws->base, shaderType, bytecode, |
| 715 |
bytecodeLen, sgnInfo, sgnLen); |
| 716 |
if (!shader) |
| 717 |
return NULL; |
| 718 |
|
| 719 |
shader->shid = shaderId; |
| 720 |
return svga_winsys_shader(shader); |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* vmw_svga_winsys_vgpu10_shader_destroy - The winsys shader_destroy callback. |
| 725 |
* |
| 726 |
* @swc: The winsys context. |
| 727 |
* @shader: A shader structure previously allocated by shader_create. |
| 728 |
* |
| 729 |
* Frees the shader structure and the buffer holding the shader code. |
| 730 |
*/ |
| 731 |
static void |
| 732 |
vmw_svga_winsys_vgpu10_shader_destroy(struct svga_winsys_context *swc, |
| 733 |
struct svga_winsys_gb_shader *shader) |
| 734 |
{ |
| 735 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 736 |
|
| 737 |
vmw_svga_winsys_shader_destroy(&vswc->vws->base, shader); |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* vmw_svga_winsys_resource_rebind - The winsys resource_rebind callback |
| 742 |
* |
| 743 |
* @swc: The winsys context. |
| 744 |
* @surface: The surface to be referenced. |
| 745 |
* @shader: The shader to be referenced. |
| 746 |
* @flags: Relocation flags. |
| 747 |
* |
| 748 |
* This callback is needed because shader backing buffers are sub-allocated, and |
| 749 |
* hence the kernel fencing is not sufficient. The buffers need to be put on |
| 750 |
* the context's validation list and fenced after command submission to avoid |
| 751 |
* reuse of busy shader buffers. In addition, surfaces need to be put on the |
| 752 |
* validation list in order for the driver to regard them as referenced |
| 753 |
* by the command stream. |
| 754 |
*/ |
| 755 |
static enum pipe_error |
| 756 |
vmw_svga_winsys_resource_rebind(struct svga_winsys_context *swc, |
| 757 |
struct svga_winsys_surface *surface, |
| 758 |
struct svga_winsys_gb_shader *shader, |
| 759 |
unsigned flags) |
| 760 |
{ |
| 761 |
/** |
| 762 |
* Need to reserve one validation item for either the surface or |
| 763 |
* the shader. |
| 764 |
*/ |
| 765 |
if (!vmw_swc_reserve(swc, 0, 1)) |
| 766 |
return PIPE_ERROR_OUT_OF_MEMORY; |
| 767 |
|
| 768 |
if (surface) |
| 769 |
vmw_swc_surface_relocation(swc, NULL, NULL, surface, flags); |
| 770 |
else if (shader) |
| 771 |
vmw_swc_shader_relocation(swc, NULL, NULL, NULL, shader, flags); |
| 772 |
|
| 773 |
vmw_swc_commit(swc); |
| 774 |
|
| 775 |
return PIPE_OK; |
| 776 |
} |
| 777 |
|
| 778 |
struct svga_winsys_context * |
| 779 |
vmw_svga_winsys_context_create(struct svga_winsys_screen *sws) |
| 780 |
{ |
| 781 |
struct vmw_winsys_screen *vws = vmw_winsys_screen(sws); |
| 782 |
struct vmw_svga_winsys_context *vswc; |
| 783 |
|
| 784 |
vswc = CALLOC_STRUCT(vmw_svga_winsys_context); |
| 785 |
if (!vswc) |
| 786 |
return NULL; |
| 787 |
|
| 788 |
vswc->base.destroy = vmw_swc_unref; |
| 789 |
vswc->base.reserve = vmw_swc_reserve; |
| 790 |
vswc->base.get_command_buffer_size = vmw_swc_get_command_buffer_size; |
| 791 |
vswc->base.surface_relocation = vmw_swc_surface_relocation; |
| 792 |
vswc->base.region_relocation = vmw_swc_region_relocation; |
| 793 |
vswc->base.mob_relocation = vmw_swc_mob_relocation; |
| 794 |
vswc->base.query_relocation = vmw_swc_query_relocation; |
| 795 |
vswc->base.query_bind = vmw_swc_query_bind; |
| 796 |
vswc->base.context_relocation = vmw_swc_context_relocation; |
| 797 |
vswc->base.shader_relocation = vmw_swc_shader_relocation; |
| 798 |
vswc->base.commit = vmw_swc_commit; |
| 799 |
vswc->base.flush = vmw_swc_flush; |
| 800 |
vswc->base.surface_map = vmw_svga_winsys_surface_map; |
| 801 |
vswc->base.surface_unmap = vmw_svga_winsys_surface_unmap; |
| 802 |
|
| 803 |
vswc->base.shader_create = vmw_svga_winsys_vgpu10_shader_create; |
| 804 |
vswc->base.shader_destroy = vmw_svga_winsys_vgpu10_shader_destroy; |
| 805 |
|
| 806 |
vswc->base.resource_rebind = vmw_svga_winsys_resource_rebind; |
| 807 |
|
| 808 |
if (sws->have_vgpu10) |
| 809 |
vswc->base.cid = vmw_ioctl_extended_context_create(vws, sws->have_vgpu10); |
| 810 |
else |
| 811 |
vswc->base.cid = vmw_ioctl_context_create(vws); |
| 812 |
|
| 813 |
if (vswc->base.cid == -1) |
| 814 |
goto out_no_context; |
| 815 |
|
| 816 |
vswc->base.imported_fence_fd = -1; |
| 817 |
|
| 818 |
vswc->base.have_gb_objects = sws->have_gb_objects; |
| 819 |
|
| 820 |
vswc->vws = vws; |
| 821 |
|
| 822 |
vswc->command.size = VMW_COMMAND_SIZE; |
| 823 |
vswc->surface.size = VMW_SURFACE_RELOCS; |
| 824 |
vswc->shader.size = VMW_SHADER_RELOCS; |
| 825 |
vswc->region.size = VMW_REGION_RELOCS; |
| 826 |
|
| 827 |
vswc->validate = pb_validate_create(); |
| 828 |
if (!vswc->validate) |
| 829 |
goto out_no_validate; |
| 830 |
|
| 831 |
vswc->hash = util_hash_table_create_ptr_keys(); |
| 832 |
if (!vswc->hash) |
| 833 |
goto out_no_hash; |
| 834 |
|
| 835 |
if (vmw_has_userspace_surface(vws)) { |
| 836 |
if (!(vswc->surface_id_bm = util_bitmask_create())) |
| 837 |
goto out_no_user_srf; |
| 838 |
/** |
| 839 |
* First id assigned is 0 which is invalid for surface id. Consume the |
| 840 |
* first id. |
| 841 |
*/ |
| 842 |
vmw_swc_surface_add_userspace_id(&vswc->base); |
| 843 |
} |
| 844 |
|
| 845 |
/** |
| 846 |
* The context refcount is initialized to 2, one reference is for the context |
| 847 |
* itself and the other is for vws screen. One unref is done when context |
| 848 |
* destroy is called and the other when the either vws screen is destroyed |
| 849 |
* or it initializes another context. |
| 850 |
* This ensures that a screen always has access to its last created context. |
| 851 |
* The vws screen needs this context to submit surface commands for userspace |
| 852 |
* managed surfaces. |
| 853 |
*/ |
| 854 |
p_atomic_set(&vswc->refcount, 1); |
| 855 |
if (vws->swc) |
| 856 |
vmw_swc_unref(vws->swc); |
| 857 |
vws->swc = &vswc->base; |
| 858 |
p_atomic_inc(&vswc->refcount); |
| 859 |
|
| 860 |
#if MESA_DEBUG |
| 861 |
vswc->fctx = debug_flush_ctx_create(true, VMW_DEBUG_FLUSH_STACK); |
| 862 |
#endif |
| 863 |
|
| 864 |
vswc->base.force_coherent = vws->force_coherent; |
| 865 |
return &vswc->base; |
| 866 |
|
| 867 |
out_no_user_srf: |
| 868 |
_mesa_hash_table_destroy(vswc->hash, NULL); |
| 869 |
out_no_hash: |
| 870 |
pb_validate_destroy(vswc->validate); |
| 871 |
out_no_validate: |
| 872 |
vmw_ioctl_context_destroy(vws, vswc->base.cid); |
| 873 |
out_no_context: |
| 874 |
FREE(vswc); |
| 875 |
return NULL; |
| 876 |
} |
| 877 |
|
| 878 |
void |
| 879 |
vmw_swc_surface_clear_userspace_id(struct svga_winsys_context *swc, |
| 880 |
uint32 sid) |
| 881 |
{ |
| 882 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 883 |
util_bitmask_clear(vswc->surface_id_bm, sid); |
| 884 |
} |
| 885 |
|
| 886 |
uint32_t |
| 887 |
vmw_swc_surface_add_userspace_id(struct svga_winsys_context *swc) |
| 888 |
{ |
| 889 |
struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc); |
| 890 |
return util_bitmask_add(vswc->surface_id_bm); |
| 891 |
} |