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_resource_buffer.h"
38 #include "svga_resource_buffer_upload.h"
39 #include "svga_winsys.h"
40 #include "svga_debug.h"
44 * Allocate a winsys_buffer (ie. DMA, aka GMR memory).
46 * It will flush and retry in case the first attempt to create a DMA buffer
47 * fails, so it should not be called from any function involved in flushing
50 struct svga_winsys_buffer
*
51 svga_winsys_buffer_create( struct svga_context
*svga
,
56 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
57 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
58 struct svga_winsys_buffer
*buf
;
61 buf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
64 SVGA_DBG(DEBUG_DMA
|DEBUG_PERF
, "flushing screen to find %d bytes GMR\n",
67 /* Try flushing all pending DMAs */
68 svga_context_flush(svga
, NULL
);
69 buf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
77 svga_buffer_destroy_hw_storage(struct svga_screen
*ss
, struct svga_buffer
*sbuf
)
79 struct svga_winsys_screen
*sws
= ss
->sws
;
81 assert(!sbuf
->map
.count
);
84 sws
->buffer_destroy(sws
, sbuf
->hwbuf
);
92 * Allocate DMA'ble storage for the buffer.
94 * Called before mapping a buffer.
97 svga_buffer_create_hw_storage(struct svga_screen
*ss
,
98 struct svga_buffer
*sbuf
)
103 struct svga_winsys_screen
*sws
= ss
->sws
;
104 unsigned alignment
= 16;
106 unsigned size
= sbuf
->b
.b
.width0
;
108 sbuf
->hwbuf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
110 return PIPE_ERROR_OUT_OF_MEMORY
;
112 assert(!sbuf
->dma
.pending
);
121 svga_buffer_create_host_surface(struct svga_screen
*ss
,
122 struct svga_buffer
*sbuf
)
127 sbuf
->key
.format
= SVGA3D_BUFFER
;
128 if(sbuf
->b
.b
.bind
& PIPE_BIND_VERTEX_BUFFER
)
129 sbuf
->key
.flags
|= SVGA3D_SURFACE_HINT_VERTEXBUFFER
;
130 if(sbuf
->b
.b
.bind
& PIPE_BIND_INDEX_BUFFER
)
131 sbuf
->key
.flags
|= SVGA3D_SURFACE_HINT_INDEXBUFFER
;
133 sbuf
->key
.size
.width
= sbuf
->b
.b
.width0
;
134 sbuf
->key
.size
.height
= 1;
135 sbuf
->key
.size
.depth
= 1;
137 sbuf
->key
.numFaces
= 1;
138 sbuf
->key
.numMipLevels
= 1;
139 sbuf
->key
.cachable
= 1;
141 SVGA_DBG(DEBUG_DMA
, "surface_create for buffer sz %d\n", sbuf
->b
.b
.width0
);
143 sbuf
->handle
= svga_screen_surface_create(ss
, &sbuf
->key
);
145 return PIPE_ERROR_OUT_OF_MEMORY
;
147 /* Always set the discard flag on the first time the buffer is written
148 * as svga_screen_surface_create might have passed a recycled host
151 sbuf
->dma
.flags
.discard
= TRUE
;
153 SVGA_DBG(DEBUG_DMA
, " --> got sid %p sz %d (buffer)\n", sbuf
->handle
, sbuf
->b
.b
.width0
);
161 svga_buffer_destroy_host_surface(struct svga_screen
*ss
,
162 struct svga_buffer
*sbuf
)
165 SVGA_DBG(DEBUG_DMA
, " ungrab sid %p sz %d\n", sbuf
->handle
, sbuf
->b
.b
.width0
);
166 svga_screen_surface_destroy(ss
, &sbuf
->key
, &sbuf
->handle
);
172 * Variant of SVGA3D_BufferDMA which leaves the copy box temporarily in blank.
174 static enum pipe_error
175 svga_buffer_upload_command(struct svga_context
*svga
,
176 struct svga_buffer
*sbuf
)
178 struct svga_winsys_context
*swc
= svga
->swc
;
179 struct svga_winsys_buffer
*guest
= sbuf
->hwbuf
;
180 struct svga_winsys_surface
*host
= sbuf
->handle
;
181 SVGA3dTransferType transfer
= SVGA3D_WRITE_HOST_VRAM
;
182 SVGA3dCmdSurfaceDMA
*cmd
;
183 uint32 numBoxes
= sbuf
->map
.num_ranges
;
184 SVGA3dCopyBox
*boxes
;
185 SVGA3dCmdSurfaceDMASuffix
*pSuffix
;
186 unsigned region_flags
;
187 unsigned surface_flags
;
188 struct pipe_resource
*dummy
;
190 if(transfer
== SVGA3D_WRITE_HOST_VRAM
) {
191 region_flags
= SVGA_RELOC_READ
;
192 surface_flags
= SVGA_RELOC_WRITE
;
194 else if(transfer
== SVGA3D_READ_HOST_VRAM
) {
195 region_flags
= SVGA_RELOC_WRITE
;
196 surface_flags
= SVGA_RELOC_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
->dma
.boxes
= (SVGA3dCopyBox
*)&cmd
[1];
222 sbuf
->dma
.svga
= svga
;
224 /* Increment reference count */
226 pipe_resource_reference(&dummy
, &sbuf
->b
.b
);
228 pSuffix
= (SVGA3dCmdSurfaceDMASuffix
*)((uint8_t*)cmd
+ sizeof *cmd
+ numBoxes
* sizeof *boxes
);
229 pSuffix
->suffixSize
= sizeof *pSuffix
;
230 pSuffix
->maximumOffset
= sbuf
->b
.b
.width0
;
231 pSuffix
->flags
= sbuf
->dma
.flags
;
233 SVGA_FIFOCommitAll(swc
);
235 sbuf
->dma
.flags
.discard
= FALSE
;
242 * Patch up the upload DMA command reserved by svga_buffer_upload_command
243 * with the final ranges.
246 svga_buffer_upload_flush(struct svga_context
*svga
,
247 struct svga_buffer
*sbuf
)
249 SVGA3dCopyBox
*boxes
;
251 struct pipe_resource
*dummy
;
253 assert(sbuf
->handle
);
255 assert(sbuf
->map
.num_ranges
);
256 assert(sbuf
->dma
.svga
== svga
);
257 assert(sbuf
->dma
.boxes
);
260 * Patch the DMA command with the final copy box.
263 SVGA_DBG(DEBUG_DMA
, "dma to sid %p\n", sbuf
->handle
);
265 boxes
= sbuf
->dma
.boxes
;
266 for(i
= 0; i
< sbuf
->map
.num_ranges
; ++i
) {
267 SVGA_DBG(DEBUG_DMA
, " bytes %u - %u\n",
268 sbuf
->map
.ranges
[i
].start
, sbuf
->map
.ranges
[i
].end
);
270 boxes
[i
].x
= sbuf
->map
.ranges
[i
].start
;
273 boxes
[i
].w
= sbuf
->map
.ranges
[i
].end
- sbuf
->map
.ranges
[i
].start
;
276 boxes
[i
].srcx
= sbuf
->map
.ranges
[i
].start
;
281 sbuf
->map
.num_ranges
= 0;
283 assert(sbuf
->head
.prev
&& sbuf
->head
.next
);
284 LIST_DEL(&sbuf
->head
);
286 sbuf
->head
.next
= sbuf
->head
.prev
= NULL
;
288 sbuf
->dma
.pending
= FALSE
;
290 sbuf
->dma
.svga
= NULL
;
291 sbuf
->dma
.boxes
= NULL
;
293 /* Decrement reference count (and potentially destroy) */
295 pipe_resource_reference(&dummy
, NULL
);
301 * Note a dirty range.
303 * This function only notes the range down. It doesn't actually emit a DMA
304 * upload command. That only happens when a context tries to refer to this
305 * buffer, and the DMA upload command is added to that context's command buffer.
307 * We try to lump as many contiguous DMA transfers together as possible.
310 svga_buffer_add_range(struct svga_buffer
*sbuf
,
315 unsigned nearest_range
;
316 unsigned nearest_dist
;
320 if (sbuf
->map
.num_ranges
< SVGA_BUFFER_MAX_RANGES
) {
321 nearest_range
= sbuf
->map
.num_ranges
;
324 nearest_range
= SVGA_BUFFER_MAX_RANGES
- 1;
329 * Try to grow one of the ranges.
331 * Note that it is not this function task to care about overlapping ranges,
332 * as the GMR was already given so it is too late to do anything. Situations
333 * where overlapping ranges may pose a problem should be detected via
334 * pipe_context::is_resource_referenced and the context that refers to the
335 * buffer should be flushed.
338 for(i
= 0; i
< sbuf
->map
.num_ranges
; ++i
) {
343 left_dist
= start
- sbuf
->map
.ranges
[i
].end
;
344 right_dist
= sbuf
->map
.ranges
[i
].start
- end
;
345 dist
= MAX2(left_dist
, right_dist
);
349 * Ranges are contiguous or overlapping -- extend this one and return.
352 sbuf
->map
.ranges
[i
].start
= MIN2(sbuf
->map
.ranges
[i
].start
, start
);
353 sbuf
->map
.ranges
[i
].end
= MAX2(sbuf
->map
.ranges
[i
].end
, end
);
358 * Discontiguous ranges -- keep track of the nearest range.
361 if (dist
< nearest_dist
) {
369 * We cannot add a new range to an existing DMA command, so patch-up the
370 * pending DMA upload and start clean.
373 if(sbuf
->dma
.pending
)
374 svga_buffer_upload_flush(sbuf
->dma
.svga
, sbuf
);
376 assert(!sbuf
->dma
.pending
);
377 assert(!sbuf
->dma
.svga
);
378 assert(!sbuf
->dma
.boxes
);
380 if (sbuf
->map
.num_ranges
< SVGA_BUFFER_MAX_RANGES
) {
385 sbuf
->map
.ranges
[sbuf
->map
.num_ranges
].start
= start
;
386 sbuf
->map
.ranges
[sbuf
->map
.num_ranges
].end
= end
;
387 ++sbuf
->map
.num_ranges
;
390 * Everything else failed, so just extend the nearest range.
392 * It is OK to do this because we always keep a local copy of the
393 * host buffer data, for SW TNL, and the host never modifies the buffer.
396 assert(nearest_range
< SVGA_BUFFER_MAX_RANGES
);
397 assert(nearest_range
< sbuf
->map
.num_ranges
);
398 sbuf
->map
.ranges
[nearest_range
].start
= MIN2(sbuf
->map
.ranges
[nearest_range
].start
, start
);
399 sbuf
->map
.ranges
[nearest_range
].end
= MAX2(sbuf
->map
.ranges
[nearest_range
].end
, end
);
406 * Copy the contents of the malloc buffer to a hardware buffer.
408 static INLINE
enum pipe_error
409 svga_buffer_update_hw(struct svga_screen
*ss
, struct svga_buffer
*sbuf
)
420 ret
= svga_buffer_create_hw_storage(ss
, sbuf
);
424 pipe_mutex_lock(ss
->swc_mutex
);
425 map
= ss
->sws
->buffer_map(ss
->sws
, sbuf
->hwbuf
, PIPE_TRANSFER_WRITE
);
428 pipe_mutex_unlock(ss
->swc_mutex
);
429 svga_buffer_destroy_hw_storage(ss
, sbuf
);
433 memcpy(map
, sbuf
->swbuf
, sbuf
->b
.b
.width0
);
434 ss
->sws
->buffer_unmap(ss
->sws
, sbuf
->hwbuf
);
436 /* This user/malloc buffer is now indistinguishable from a gpu buffer */
437 assert(!sbuf
->map
.count
);
438 if(!sbuf
->map
.count
) {
442 align_free(sbuf
->swbuf
);
446 pipe_mutex_unlock(ss
->swc_mutex
);
454 * Upload the buffer to the host in a piecewise fashion.
456 * Used when the buffer is too big to fit in the GMR aperture.
458 static INLINE
enum pipe_error
459 svga_buffer_upload_piecewise(struct svga_screen
*ss
,
460 struct svga_context
*svga
,
461 struct svga_buffer
*sbuf
)
463 struct svga_winsys_screen
*sws
= ss
->sws
;
464 const unsigned alignment
= sizeof(void *);
465 const unsigned usage
= 0;
468 assert(sbuf
->map
.num_ranges
);
469 assert(!sbuf
->dma
.pending
);
471 SVGA_DBG(DEBUG_DMA
, "dma to sid %p\n", sbuf
->handle
);
473 for (i
= 0; i
< sbuf
->map
.num_ranges
; ++i
) {
474 struct svga_buffer_range
*range
= &sbuf
->map
.ranges
[i
];
475 unsigned offset
= range
->start
;
476 unsigned size
= range
->end
- range
->start
;
478 while (offset
< range
->end
) {
479 struct svga_winsys_buffer
*hwbuf
;
483 if (offset
+ size
> range
->end
)
484 size
= range
->end
- offset
;
486 hwbuf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
490 return PIPE_ERROR_OUT_OF_MEMORY
;
491 hwbuf
= sws
->buffer_create(sws
, alignment
, usage
, size
);
494 SVGA_DBG(DEBUG_DMA
, " bytes %u - %u\n",
495 offset
, offset
+ size
);
497 map
= sws
->buffer_map(sws
, hwbuf
,
498 PIPE_TRANSFER_WRITE
|
499 PIPE_TRANSFER_DISCARD
);
502 memcpy(map
, sbuf
->swbuf
, size
);
503 sws
->buffer_unmap(sws
, hwbuf
);
506 ret
= SVGA3D_BufferDMA(svga
->swc
,
508 SVGA3D_WRITE_HOST_VRAM
,
509 size
, 0, offset
, sbuf
->dma
.flags
);
511 svga_context_flush(svga
, NULL
);
512 ret
= SVGA3D_BufferDMA(svga
->swc
,
514 SVGA3D_WRITE_HOST_VRAM
,
515 size
, 0, offset
, sbuf
->dma
.flags
);
516 assert(ret
== PIPE_OK
);
519 sbuf
->dma
.flags
.discard
= FALSE
;
521 sws
->buffer_destroy(sws
, hwbuf
);
527 sbuf
->map
.num_ranges
= 0;
535 /* Get (or create/upload) the winsys surface handle so that we can
536 * refer to this buffer in fifo commands.
538 struct svga_winsys_surface
*
539 svga_buffer_handle(struct svga_context
*svga
,
540 struct pipe_resource
*buf
)
542 struct pipe_screen
*screen
= svga
->pipe
.screen
;
543 struct svga_screen
*ss
= svga_screen(screen
);
544 struct svga_buffer
*sbuf
;
550 sbuf
= svga_buffer(buf
);
552 assert(!sbuf
->map
.count
);
556 ret
= svga_buffer_create_host_surface(ss
, sbuf
);
561 assert(sbuf
->handle
);
563 if (sbuf
->map
.num_ranges
) {
564 if (!sbuf
->dma
.pending
) {
566 * No pending DMA upload yet, so insert a DMA upload command now.
570 * Migrate the data from swbuf -> hwbuf if necessary.
572 ret
= svga_buffer_update_hw(ss
, sbuf
);
573 if (ret
== PIPE_OK
) {
575 * Queue a dma command.
578 ret
= svga_buffer_upload_command(svga
, sbuf
);
579 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
580 svga_context_flush(svga
, NULL
);
581 ret
= svga_buffer_upload_command(svga
, sbuf
);
582 assert(ret
== PIPE_OK
);
584 if (ret
== PIPE_OK
) {
585 sbuf
->dma
.pending
= TRUE
;
586 assert(!sbuf
->head
.prev
&& !sbuf
->head
.next
);
587 LIST_ADDTAIL(&sbuf
->head
, &svga
->dirty_buffers
);
590 else if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
592 * The buffer is too big to fit in the GMR aperture, so break it in
595 ret
= svga_buffer_upload_piecewise(ss
, svga
, sbuf
);
598 if (ret
!= PIPE_OK
) {
600 * Something unexpected happened above. There is very little that
601 * we can do other than proceeding while ignoring the dirty ranges.
604 sbuf
->map
.num_ranges
= 0;
609 * There a pending dma already. Make sure it is from this context.
611 assert(sbuf
->dma
.svga
== svga
);
615 assert(!sbuf
->map
.num_ranges
|| sbuf
->dma
.pending
);
623 svga_context_flush_buffers(struct svga_context
*svga
)
625 struct list_head
*curr
, *next
;
626 struct svga_buffer
*sbuf
;
628 curr
= svga
->dirty_buffers
.next
;
630 while(curr
!= &svga
->dirty_buffers
) {
631 sbuf
= LIST_ENTRY(struct svga_buffer
, curr
, head
);
633 assert(p_atomic_read(&sbuf
->b
.b
.reference
.count
) != 0);
634 assert(sbuf
->dma
.pending
);
636 svga_buffer_upload_flush(svga
, sbuf
);