1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
35 #include "svga_context.h"
36 #include "svga_screen.h"
37 #include "svga_screen_buffer.h"
38 #include "svga_winsys.h"
39 #include "svga_debug.h"
43 * Vertex and index buffers have to be treated slightly differently from
44 * regular guest memory regions because the SVGA device sees them as
45 * surfaces, and the state tracker can create/destroy without the pipe
46 * driver, therefore we must do the uploads from the vws.
49 svga_buffer_needs_hw_storage(unsigned usage
)
51 return usage
& (PIPE_BUFFER_USAGE_VERTEX
| PIPE_BUFFER_USAGE_INDEX
);
55 static INLINE
enum pipe_error
56 svga_buffer_create_host_surface(struct svga_screen
*ss
,
57 struct svga_buffer
*sbuf
)
62 sbuf
->key
.format
= SVGA3D_BUFFER
;
63 if(sbuf
->base
.usage
& PIPE_BUFFER_USAGE_VERTEX
)
64 sbuf
->key
.flags
|= SVGA3D_SURFACE_HINT_VERTEXBUFFER
;
65 if(sbuf
->base
.usage
& PIPE_BUFFER_USAGE_INDEX
)
66 sbuf
->key
.flags
|= SVGA3D_SURFACE_HINT_INDEXBUFFER
;
68 sbuf
->key
.size
.width
= sbuf
->base
.size
;
69 sbuf
->key
.size
.height
= 1;
70 sbuf
->key
.size
.depth
= 1;
72 sbuf
->key
.numFaces
= 1;
73 sbuf
->key
.numMipLevels
= 1;
74 sbuf
->key
.cachable
= 1;
76 SVGA_DBG(DEBUG_DMA
, "surface_create for buffer sz %d\n", sbuf
->base
.size
);
78 sbuf
->handle
= svga_screen_surface_create(ss
, &sbuf
->key
);
80 return PIPE_ERROR_OUT_OF_MEMORY
;
82 /* Always set the discard flag on the first time the buffer is written
83 * as svga_screen_surface_create might have passed a recycled host
86 sbuf
->hw
.flags
.discard
= TRUE
;
88 SVGA_DBG(DEBUG_DMA
, " --> got sid %p sz %d (buffer)\n", sbuf
->handle
, sbuf
->base
.size
);
96 svga_buffer_destroy_host_surface(struct svga_screen
*ss
,
97 struct svga_buffer
*sbuf
)
100 SVGA_DBG(DEBUG_DMA
, " ungrab sid %p sz %d\n", sbuf
->handle
, sbuf
->base
.size
);
101 svga_screen_surface_destroy(ss
, &sbuf
->key
, &sbuf
->handle
);
107 svga_buffer_destroy_hw_storage(struct svga_screen
*ss
, struct svga_buffer
*sbuf
)
109 struct svga_winsys_screen
*sws
= ss
->sws
;
111 assert(!sbuf
->map
.count
);
112 assert(sbuf
->hw
.buf
);
114 sws
->buffer_destroy(sws
, sbuf
->hw
.buf
);
119 struct svga_winsys_buffer
*
120 svga_winsys_buffer_create( struct svga_screen
*ss
,
125 struct svga_winsys_screen
*sws
= ss
->sws
;
126 struct svga_winsys_buffer
*buf
;
129 buf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
132 SVGA_DBG(DEBUG_DMA
|DEBUG_PERF
, "flushing screen to find %d bytes GMR\n",
135 /* Try flushing all pending DMAs */
136 svga_screen_flush(ss
, NULL
);
137 buf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
146 * Allocate DMA'ble storage for the buffer.
148 * Called before mapping a buffer.
150 static INLINE
enum pipe_error
151 svga_buffer_create_hw_storage(struct svga_screen
*ss
,
152 struct svga_buffer
*sbuf
)
155 unsigned alignment
= sbuf
->base
.alignment
;
157 unsigned size
= sbuf
->base
.size
;
159 sbuf
->hw
.buf
= svga_winsys_buffer_create(ss
, alignment
, usage
, size
);
161 return PIPE_ERROR_OUT_OF_MEMORY
;
163 assert(!sbuf
->needs_flush
);
171 * Variant of SVGA3D_BufferDMA which leaves the copy box temporarily in blank.
173 static enum pipe_error
174 svga_buffer_upload_command(struct svga_context
*svga
,
175 struct svga_buffer
*sbuf
)
177 struct svga_winsys_context
*swc
= svga
->swc
;
178 struct svga_winsys_buffer
*guest
= sbuf
->hw
.buf
;
179 struct svga_winsys_surface
*host
= sbuf
->handle
;
180 SVGA3dTransferType transfer
= SVGA3D_WRITE_HOST_VRAM
;
181 SVGA3dSurfaceDMAFlags flags
= sbuf
->hw
.flags
;
182 SVGA3dCmdSurfaceDMA
*cmd
;
183 uint32 numBoxes
= sbuf
->hw
.num_ranges
;
184 SVGA3dCopyBox
*boxes
;
185 SVGA3dCmdSurfaceDMASuffix
*pSuffix
;
186 unsigned region_flags
;
187 unsigned surface_flags
;
188 struct pipe_buffer
*dummy
;
190 if(transfer
== SVGA3D_WRITE_HOST_VRAM
) {
191 region_flags
= PIPE_BUFFER_USAGE_GPU_READ
;
192 surface_flags
= PIPE_BUFFER_USAGE_GPU_WRITE
;
194 else if(transfer
== SVGA3D_READ_HOST_VRAM
) {
195 region_flags
= PIPE_BUFFER_USAGE_GPU_WRITE
;
196 surface_flags
= PIPE_BUFFER_USAGE_GPU_READ
;
200 return PIPE_ERROR_BAD_INPUT
;
205 cmd
= SVGA3D_FIFOReserve(swc
,
206 SVGA_3D_CMD_SURFACE_DMA
,
207 sizeof *cmd
+ numBoxes
* sizeof *boxes
+ sizeof *pSuffix
,
210 return PIPE_ERROR_OUT_OF_MEMORY
;
212 swc
->region_relocation(swc
, &cmd
->guest
.ptr
, guest
, 0, region_flags
);
213 cmd
->guest
.pitch
= 0;
215 swc
->surface_relocation(swc
, &cmd
->host
.sid
, host
, surface_flags
);
217 cmd
->host
.mipmap
= 0;
219 cmd
->transfer
= transfer
;
221 sbuf
->hw
.boxes
= (SVGA3dCopyBox
*)&cmd
[1];
222 sbuf
->hw
.svga
= svga
;
224 /* Increment reference count */
226 pipe_buffer_reference(&dummy
, &sbuf
->base
);
228 pSuffix
= (SVGA3dCmdSurfaceDMASuffix
*)((uint8_t*)cmd
+ sizeof *cmd
+ numBoxes
* sizeof *boxes
);
229 pSuffix
->suffixSize
= sizeof *pSuffix
;
230 pSuffix
->maximumOffset
= sbuf
->base
.size
;
231 pSuffix
->flags
= flags
;
240 * Patch up the upload DMA command reserved by svga_buffer_upload_command
241 * with the final ranges.
244 svga_buffer_upload_flush(struct svga_context
*svga
,
245 struct svga_buffer
*sbuf
)
247 SVGA3dCopyBox
*boxes
;
250 assert(sbuf
->handle
);
251 assert(sbuf
->hw
.buf
);
252 assert(sbuf
->hw
.num_ranges
);
253 assert(sbuf
->hw
.svga
== svga
);
254 assert(sbuf
->hw
.boxes
);
257 * Patch the DMA command with the final copy box.
260 SVGA_DBG(DEBUG_DMA
, "dma to sid %p\n", sbuf
->handle
);
262 boxes
= sbuf
->hw
.boxes
;
263 for(i
= 0; i
< sbuf
->hw
.num_ranges
; ++i
) {
264 SVGA_DBG(DEBUG_DMA
, " bytes %u - %u\n",
265 sbuf
->hw
.ranges
[i
].start
, sbuf
->hw
.ranges
[i
].end
);
267 boxes
[i
].x
= sbuf
->hw
.ranges
[i
].start
;
270 boxes
[i
].w
= sbuf
->hw
.ranges
[i
].end
- sbuf
->hw
.ranges
[i
].start
;
273 boxes
[i
].srcx
= sbuf
->hw
.ranges
[i
].start
;
278 sbuf
->hw
.num_ranges
= 0;
279 memset(&sbuf
->hw
.flags
, 0, sizeof sbuf
->hw
.flags
);
281 assert(sbuf
->head
.prev
&& sbuf
->head
.next
);
282 LIST_DEL(&sbuf
->head
);
284 sbuf
->head
.next
= sbuf
->head
.prev
= NULL
;
286 sbuf
->needs_flush
= FALSE
;
288 sbuf
->hw
.svga
= NULL
;
289 sbuf
->hw
.boxes
= NULL
;
291 sbuf
->host_written
= TRUE
;
293 /* Decrement reference count */
294 pipe_reference(&(sbuf
->base
.reference
), NULL
);
300 * Queue a DMA upload of a range of this buffer to the host.
302 * This function only notes the range down. It doesn't actually emit a DMA
303 * upload command. That only happens when a context tries to refer to this
304 * buffer, and the DMA upload command is added to that context's command buffer.
306 * We try to lump as many contiguous DMA transfers together as possible.
309 svga_buffer_upload_queue(struct svga_buffer
*sbuf
,
315 assert(sbuf
->hw
.buf
);
319 * Try to grow one of the ranges.
321 * Note that it is not this function task to care about overlapping ranges,
322 * as the GMR was already given so it is too late to do anything. Situations
323 * where overlapping ranges may pose a problem should be detected via
324 * pipe_context::is_buffer_referenced and the context that refers to the
325 * buffer should be flushed.
328 for(i
= 0; i
< sbuf
->hw
.num_ranges
; ++i
) {
329 if(start
<= sbuf
->hw
.ranges
[i
].end
&& sbuf
->hw
.ranges
[i
].start
<= end
) {
330 sbuf
->hw
.ranges
[i
].start
= MIN2(sbuf
->hw
.ranges
[i
].start
, start
);
331 sbuf
->hw
.ranges
[i
].end
= MAX2(sbuf
->hw
.ranges
[i
].end
, end
);
337 * We cannot add a new range to an existing DMA command, so patch-up the
338 * pending DMA upload and start clean.
341 if(sbuf
->needs_flush
)
342 svga_buffer_upload_flush(sbuf
->hw
.svga
, sbuf
);
344 assert(!sbuf
->needs_flush
);
345 assert(!sbuf
->hw
.svga
);
346 assert(!sbuf
->hw
.boxes
);
352 sbuf
->hw
.ranges
[sbuf
->hw
.num_ranges
].start
= start
;
353 sbuf
->hw
.ranges
[sbuf
->hw
.num_ranges
].end
= end
;
354 ++sbuf
->hw
.num_ranges
;
359 svga_buffer_map_range( struct pipe_screen
*screen
,
360 struct pipe_buffer
*buf
,
361 unsigned offset
, unsigned length
,
364 struct svga_screen
*ss
= svga_screen(screen
);
365 struct svga_winsys_screen
*sws
= ss
->sws
;
366 struct svga_buffer
*sbuf
= svga_buffer( buf
);
370 /* User/malloc buffer */
375 if(svga_buffer_create_hw_storage(ss
, sbuf
) != PIPE_OK
)
378 /* Populate the hardware storage if the host surface pre-existed */
379 if(sbuf
->host_written
) {
380 SVGA3dSurfaceDMAFlags flags
;
382 struct pipe_fence_handle
*fence
= NULL
;
384 assert(sbuf
->handle
);
386 SVGA_DBG(DEBUG_DMA
|DEBUG_PERF
, "dma from sid %p (buffer), bytes %u - %u\n",
387 sbuf
->handle
, 0, sbuf
->base
.size
);
389 memset(&flags
, 0, sizeof flags
);
391 ret
= SVGA3D_BufferDMA(ss
->swc
,
394 SVGA3D_READ_HOST_VRAM
,
399 ss
->swc
->flush(ss
->swc
, NULL
);
401 ret
= SVGA3D_BufferDMA(ss
->swc
,
404 SVGA3D_READ_HOST_VRAM
,
408 assert(ret
== PIPE_OK
);
411 ss
->swc
->flush(ss
->swc
, &fence
);
412 sws
->fence_finish(sws
, fence
, 0);
413 sws
->fence_reference(sws
, &fence
, NULL
);
417 map
= sws
->buffer_map(sws
, sbuf
->hw
.buf
, usage
);
421 pipe_mutex_lock(ss
->swc_mutex
);
425 if (usage
& PIPE_BUFFER_USAGE_CPU_WRITE
) {
426 assert(sbuf
->map
.count
<= 1);
427 sbuf
->map
.writing
= TRUE
;
428 if (usage
& PIPE_BUFFER_USAGE_FLUSH_EXPLICIT
)
429 sbuf
->map
.flush_explicit
= TRUE
;
432 pipe_mutex_unlock(ss
->swc_mutex
);
439 svga_buffer_flush_mapped_range( struct pipe_screen
*screen
,
440 struct pipe_buffer
*buf
,
441 unsigned offset
, unsigned length
)
443 struct svga_buffer
*sbuf
= svga_buffer( buf
);
444 struct svga_screen
*ss
= svga_screen(screen
);
446 pipe_mutex_lock(ss
->swc_mutex
);
447 assert(sbuf
->map
.writing
);
448 if(sbuf
->map
.writing
) {
449 assert(sbuf
->map
.flush_explicit
);
451 svga_buffer_upload_queue(sbuf
, offset
, offset
+ length
);
453 pipe_mutex_unlock(ss
->swc_mutex
);
457 svga_buffer_unmap( struct pipe_screen
*screen
,
458 struct pipe_buffer
*buf
)
460 struct svga_screen
*ss
= svga_screen(screen
);
461 struct svga_winsys_screen
*sws
= ss
->sws
;
462 struct svga_buffer
*sbuf
= svga_buffer( buf
);
464 pipe_mutex_lock(ss
->swc_mutex
);
466 assert(sbuf
->map
.count
);
471 sws
->buffer_unmap(sws
, sbuf
->hw
.buf
);
473 if(sbuf
->map
.writing
) {
474 if(!sbuf
->map
.flush_explicit
) {
475 /* No mapped range was flushed -- flush the whole buffer */
476 SVGA_DBG(DEBUG_DMA
, "flushing the whole buffer\n");
479 svga_buffer_upload_queue(sbuf
, 0, sbuf
->base
.size
);
482 sbuf
->map
.writing
= FALSE
;
483 sbuf
->map
.flush_explicit
= FALSE
;
486 pipe_mutex_unlock(ss
->swc_mutex
);
490 svga_buffer_destroy( struct pipe_buffer
*buf
)
492 struct svga_screen
*ss
= svga_screen(buf
->screen
);
493 struct svga_buffer
*sbuf
= svga_buffer( buf
);
495 assert(!p_atomic_read(&buf
->reference
.count
));
497 assert(!sbuf
->needs_flush
);
500 svga_buffer_destroy_host_surface(ss
, sbuf
);
503 svga_buffer_destroy_hw_storage(ss
, sbuf
);
505 if(sbuf
->swbuf
&& !sbuf
->user
)
506 align_free(sbuf
->swbuf
);
511 static struct pipe_buffer
*
512 svga_buffer_create(struct pipe_screen
*screen
,
517 struct svga_screen
*ss
= svga_screen(screen
);
518 struct svga_buffer
*sbuf
;
523 sbuf
= CALLOC_STRUCT(svga_buffer
);
527 sbuf
->magic
= SVGA_BUFFER_MAGIC
;
529 pipe_reference_init(&sbuf
->base
.reference
, 1);
530 sbuf
->base
.screen
= screen
;
531 sbuf
->base
.alignment
= alignment
;
532 sbuf
->base
.usage
= usage
;
533 sbuf
->base
.size
= size
;
535 if(svga_buffer_needs_hw_storage(usage
)) {
536 if(svga_buffer_create_host_surface(ss
, sbuf
) != PIPE_OK
)
540 if(alignment
< sizeof(void*))
541 alignment
= sizeof(void*);
543 usage
|= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
545 sbuf
->swbuf
= align_malloc(size
, alignment
);
558 static struct pipe_buffer
*
559 svga_user_buffer_create(struct pipe_screen
*screen
,
563 struct svga_buffer
*sbuf
;
565 sbuf
= CALLOC_STRUCT(svga_buffer
);
569 sbuf
->magic
= SVGA_BUFFER_MAGIC
;
574 pipe_reference_init(&sbuf
->base
.reference
, 1);
575 sbuf
->base
.screen
= screen
;
576 sbuf
->base
.alignment
= 1;
577 sbuf
->base
.usage
= 0;
578 sbuf
->base
.size
= bytes
;
588 svga_screen_init_buffer_functions(struct pipe_screen
*screen
)
590 screen
->buffer_create
= svga_buffer_create
;
591 screen
->user_buffer_create
= svga_user_buffer_create
;
592 screen
->buffer_map_range
= svga_buffer_map_range
;
593 screen
->buffer_flush_mapped_range
= svga_buffer_flush_mapped_range
;
594 screen
->buffer_unmap
= svga_buffer_unmap
;
595 screen
->buffer_destroy
= svga_buffer_destroy
;
600 * Copy the contents of the user buffer / malloc buffer to a hardware buffer.
602 static INLINE
enum pipe_error
603 svga_buffer_update_hw(struct svga_screen
*ss
, struct svga_buffer
*sbuf
)
613 ret
= svga_buffer_create_hw_storage(ss
, sbuf
);
614 assert(ret
== PIPE_OK
);
618 pipe_mutex_lock(ss
->swc_mutex
);
619 map
= ss
->sws
->buffer_map(ss
->sws
, sbuf
->hw
.buf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
622 pipe_mutex_unlock(ss
->swc_mutex
);
623 return PIPE_ERROR_OUT_OF_MEMORY
;
626 memcpy(map
, sbuf
->swbuf
, sbuf
->base
.size
);
627 ss
->sws
->buffer_unmap(ss
->sws
, sbuf
->hw
.buf
);
629 /* This user/malloc buffer is now indistinguishable from a gpu buffer */
630 assert(!sbuf
->map
.count
);
631 if(!sbuf
->map
.count
) {
635 align_free(sbuf
->swbuf
);
639 svga_buffer_upload_queue(sbuf
, 0, sbuf
->base
.size
);
642 pipe_mutex_unlock(ss
->swc_mutex
);
647 struct svga_winsys_surface
*
648 svga_buffer_handle(struct svga_context
*svga
,
649 struct pipe_buffer
*buf
)
651 struct pipe_screen
*screen
= svga
->pipe
.screen
;
652 struct svga_screen
*ss
= svga_screen(screen
);
653 struct svga_buffer
*sbuf
;
659 sbuf
= svga_buffer(buf
);
661 assert(!sbuf
->map
.count
);
664 ret
= svga_buffer_create_host_surface(ss
, sbuf
);
668 ret
= svga_buffer_update_hw(ss
, sbuf
);
673 if(!sbuf
->needs_flush
&& sbuf
->hw
.num_ranges
) {
674 /* Queue the buffer for flushing */
675 ret
= svga_buffer_upload_command(svga
, sbuf
);
677 /* XXX: Should probably have a richer return value */
680 assert(sbuf
->hw
.svga
== svga
);
682 sbuf
->needs_flush
= TRUE
;
683 assert(!sbuf
->head
.prev
&& !sbuf
->head
.next
);
684 LIST_ADDTAIL(&sbuf
->head
, &svga
->dirty_buffers
);
691 svga_screen_buffer_wrap_surface(struct pipe_screen
*screen
,
692 enum SVGA3dSurfaceFormat format
,
693 struct svga_winsys_surface
*srf
)
695 struct pipe_buffer
*buf
;
696 struct svga_buffer
*sbuf
;
697 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
699 buf
= svga_buffer_create(screen
, 0, SVGA_BUFFER_USAGE_WRAPPED
, 0);
703 sbuf
= svga_buffer(buf
);
706 * We are not the creator of this surface and therefore we must not
707 * cache it for reuse. Set the cacheable flag to zero in the key to
710 sbuf
->key
.format
= format
;
711 sbuf
->key
.cachable
= 0;
712 sws
->surface_reference(sws
, &sbuf
->handle
, srf
);
718 struct svga_winsys_surface
*
719 svga_screen_buffer_get_winsys_surface(struct pipe_buffer
*buffer
)
721 struct svga_winsys_screen
*sws
= svga_winsys_screen(buffer
->screen
);
722 struct svga_winsys_surface
*vsurf
= NULL
;
724 assert(svga_buffer(buffer
)->key
.cachable
== 0);
725 svga_buffer(buffer
)->key
.cachable
= 0;
726 sws
->surface_reference(sws
, &vsurf
, svga_buffer(buffer
)->handle
);
731 svga_context_flush_buffers(struct svga_context
*svga
)
733 struct list_head
*curr
, *next
;
734 struct svga_buffer
*sbuf
;
736 curr
= svga
->dirty_buffers
.next
;
738 while(curr
!= &svga
->dirty_buffers
) {
739 sbuf
= LIST_ENTRY(struct svga_buffer
, curr
, head
);
741 assert(p_atomic_read(&sbuf
->base
.reference
.count
) != 0);
742 assert(sbuf
->needs_flush
);
744 svga_buffer_upload_flush(svga
, sbuf
);