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