svga: Don't advertise pixel shader addr register support.
[mesa.git] / src / gallium / drivers / svga / svga_resource_buffer_upload.c
1 /**********************************************************
2 * Copyright 2008-2009 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 #include "svga_cmd.h"
27
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"
34
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"
41
42
43 /**
44 * Allocate a winsys_buffer (ie. DMA, aka GMR memory).
45 *
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
48 * to avoid recursion.
49 */
50 struct svga_winsys_buffer *
51 svga_winsys_buffer_create( struct svga_context *svga,
52 unsigned alignment,
53 unsigned usage,
54 unsigned size )
55 {
56 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
57 struct svga_winsys_screen *sws = svgascreen->sws;
58 struct svga_winsys_buffer *buf;
59
60 /* Just try */
61 buf = sws->buffer_create(sws, alignment, usage, size);
62 if(!buf) {
63
64 SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "flushing screen to find %d bytes GMR\n",
65 size);
66
67 /* Try flushing all pending DMAs */
68 svga_context_flush(svga, NULL);
69 buf = sws->buffer_create(sws, alignment, usage, size);
70 }
71
72 return buf;
73 }
74
75
76 void
77 svga_buffer_destroy_hw_storage(struct svga_screen *ss, struct svga_buffer *sbuf)
78 {
79 struct svga_winsys_screen *sws = ss->sws;
80
81 assert(!sbuf->map.count);
82 assert(sbuf->hwbuf);
83 if(sbuf->hwbuf) {
84 sws->buffer_destroy(sws, sbuf->hwbuf);
85 sbuf->hwbuf = NULL;
86 }
87 }
88
89
90
91 /**
92 * Allocate DMA'ble storage for the buffer.
93 *
94 * Called before mapping a buffer.
95 */
96 enum pipe_error
97 svga_buffer_create_hw_storage(struct svga_screen *ss,
98 struct svga_buffer *sbuf)
99 {
100 assert(!sbuf->user);
101
102 if(!sbuf->hwbuf) {
103 struct svga_winsys_screen *sws = ss->sws;
104 unsigned alignment = 16;
105 unsigned usage = 0;
106 unsigned size = sbuf->b.b.width0;
107
108 sbuf->hwbuf = sws->buffer_create(sws, alignment, usage, size);
109 if(!sbuf->hwbuf)
110 return PIPE_ERROR_OUT_OF_MEMORY;
111
112 assert(!sbuf->dma.pending);
113 }
114
115 return PIPE_OK;
116 }
117
118
119
120 enum pipe_error
121 svga_buffer_create_host_surface(struct svga_screen *ss,
122 struct svga_buffer *sbuf)
123 {
124 if(!sbuf->handle) {
125 sbuf->key.flags = 0;
126
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;
132
133 sbuf->key.size.width = sbuf->b.b.width0;
134 sbuf->key.size.height = 1;
135 sbuf->key.size.depth = 1;
136
137 sbuf->key.numFaces = 1;
138 sbuf->key.numMipLevels = 1;
139 sbuf->key.cachable = 1;
140
141 SVGA_DBG(DEBUG_DMA, "surface_create for buffer sz %d\n", sbuf->b.b.width0);
142
143 sbuf->handle = svga_screen_surface_create(ss, &sbuf->key);
144 if(!sbuf->handle)
145 return PIPE_ERROR_OUT_OF_MEMORY;
146
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
149 * buffer.
150 */
151 sbuf->dma.flags.discard = TRUE;
152
153 SVGA_DBG(DEBUG_DMA, " --> got sid %p sz %d (buffer)\n", sbuf->handle, sbuf->b.b.width0);
154 }
155
156 return PIPE_OK;
157 }
158
159
160 void
161 svga_buffer_destroy_host_surface(struct svga_screen *ss,
162 struct svga_buffer *sbuf)
163 {
164 if(sbuf->handle) {
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);
167 }
168 }
169
170
171 /**
172 * Variant of SVGA3D_BufferDMA which leaves the copy box temporarily in blank.
173 */
174 static enum pipe_error
175 svga_buffer_upload_command(struct svga_context *svga,
176 struct svga_buffer *sbuf)
177 {
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;
189
190 if(transfer == SVGA3D_WRITE_HOST_VRAM) {
191 region_flags = SVGA_RELOC_READ;
192 surface_flags = SVGA_RELOC_WRITE;
193 }
194 else if(transfer == SVGA3D_READ_HOST_VRAM) {
195 region_flags = SVGA_RELOC_WRITE;
196 surface_flags = SVGA_RELOC_READ;
197 }
198 else {
199 assert(0);
200 return PIPE_ERROR_BAD_INPUT;
201 }
202
203 assert(numBoxes);
204
205 cmd = SVGA3D_FIFOReserve(swc,
206 SVGA_3D_CMD_SURFACE_DMA,
207 sizeof *cmd + numBoxes * sizeof *boxes + sizeof *pSuffix,
208 2);
209 if(!cmd)
210 return PIPE_ERROR_OUT_OF_MEMORY;
211
212 swc->region_relocation(swc, &cmd->guest.ptr, guest, 0, region_flags);
213 cmd->guest.pitch = 0;
214
215 swc->surface_relocation(swc, &cmd->host.sid, host, surface_flags);
216 cmd->host.face = 0;
217 cmd->host.mipmap = 0;
218
219 cmd->transfer = transfer;
220
221 sbuf->dma.boxes = (SVGA3dCopyBox *)&cmd[1];
222 sbuf->dma.svga = svga;
223
224 /* Increment reference count */
225 dummy = NULL;
226 pipe_resource_reference(&dummy, &sbuf->b.b);
227
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;
232
233 SVGA_FIFOCommitAll(swc);
234
235 sbuf->dma.flags.discard = FALSE;
236
237 return PIPE_OK;
238 }
239
240
241 /**
242 * Patch up the upload DMA command reserved by svga_buffer_upload_command
243 * with the final ranges.
244 */
245 static void
246 svga_buffer_upload_flush(struct svga_context *svga,
247 struct svga_buffer *sbuf)
248 {
249 SVGA3dCopyBox *boxes;
250 unsigned i;
251 struct pipe_resource *dummy;
252
253 assert(sbuf->handle);
254 assert(sbuf->hwbuf);
255 assert(sbuf->map.num_ranges);
256 assert(sbuf->dma.svga == svga);
257 assert(sbuf->dma.boxes);
258
259 /*
260 * Patch the DMA command with the final copy box.
261 */
262
263 SVGA_DBG(DEBUG_DMA, "dma to sid %p\n", sbuf->handle);
264
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);
269
270 boxes[i].x = sbuf->map.ranges[i].start;
271 boxes[i].y = 0;
272 boxes[i].z = 0;
273 boxes[i].w = sbuf->map.ranges[i].end - sbuf->map.ranges[i].start;
274 boxes[i].h = 1;
275 boxes[i].d = 1;
276 boxes[i].srcx = sbuf->map.ranges[i].start;
277 boxes[i].srcy = 0;
278 boxes[i].srcz = 0;
279 }
280
281 sbuf->map.num_ranges = 0;
282
283 assert(sbuf->head.prev && sbuf->head.next);
284 LIST_DEL(&sbuf->head);
285 #ifdef DEBUG
286 sbuf->head.next = sbuf->head.prev = NULL;
287 #endif
288 sbuf->dma.pending = FALSE;
289
290 sbuf->dma.svga = NULL;
291 sbuf->dma.boxes = NULL;
292
293 /* Decrement reference count (and potentially destroy) */
294 dummy = &sbuf->b.b;
295 pipe_resource_reference(&dummy, NULL);
296 }
297
298
299
300 /**
301 * Note a dirty range.
302 *
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.
306 *
307 * We try to lump as many contiguous DMA transfers together as possible.
308 */
309 void
310 svga_buffer_add_range(struct svga_buffer *sbuf,
311 unsigned start,
312 unsigned end)
313 {
314 unsigned i;
315 unsigned nearest_range;
316 unsigned nearest_dist;
317
318 assert(end > start);
319
320 if (sbuf->map.num_ranges < SVGA_BUFFER_MAX_RANGES) {
321 nearest_range = sbuf->map.num_ranges;
322 nearest_dist = ~0;
323 } else {
324 nearest_range = SVGA_BUFFER_MAX_RANGES - 1;
325 nearest_dist = 0;
326 }
327
328 /*
329 * Try to grow one of the ranges.
330 *
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.
336 */
337
338 for(i = 0; i < sbuf->map.num_ranges; ++i) {
339 int left_dist;
340 int right_dist;
341 int dist;
342
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);
346
347 if (dist <= 0) {
348 /*
349 * Ranges are contiguous or overlapping -- extend this one and return.
350 */
351
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);
354 return;
355 }
356 else {
357 /*
358 * Discontiguous ranges -- keep track of the nearest range.
359 */
360
361 if (dist < nearest_dist) {
362 nearest_range = i;
363 nearest_dist = dist;
364 }
365 }
366 }
367
368 /*
369 * We cannot add a new range to an existing DMA command, so patch-up the
370 * pending DMA upload and start clean.
371 */
372
373 if(sbuf->dma.pending)
374 svga_buffer_upload_flush(sbuf->dma.svga, sbuf);
375
376 assert(!sbuf->dma.pending);
377 assert(!sbuf->dma.svga);
378 assert(!sbuf->dma.boxes);
379
380 if (sbuf->map.num_ranges < SVGA_BUFFER_MAX_RANGES) {
381 /*
382 * Add a new range.
383 */
384
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;
388 } else {
389 /*
390 * Everything else failed, so just extend the nearest range.
391 *
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.
394 */
395
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);
400 }
401 }
402
403
404
405 /**
406 * Copy the contents of the malloc buffer to a hardware buffer.
407 */
408 static INLINE enum pipe_error
409 svga_buffer_update_hw(struct svga_screen *ss, struct svga_buffer *sbuf)
410 {
411 assert(!sbuf->user);
412 if(!sbuf->hwbuf) {
413 enum pipe_error ret;
414 void *map;
415
416 assert(sbuf->swbuf);
417 if(!sbuf->swbuf)
418 return PIPE_ERROR;
419
420 ret = svga_buffer_create_hw_storage(ss, sbuf);
421 if(ret != PIPE_OK)
422 return ret;
423
424 pipe_mutex_lock(ss->swc_mutex);
425 map = ss->sws->buffer_map(ss->sws, sbuf->hwbuf, PIPE_TRANSFER_WRITE);
426 assert(map);
427 if(!map) {
428 pipe_mutex_unlock(ss->swc_mutex);
429 svga_buffer_destroy_hw_storage(ss, sbuf);
430 return PIPE_ERROR;
431 }
432
433 memcpy(map, sbuf->swbuf, sbuf->b.b.width0);
434 ss->sws->buffer_unmap(ss->sws, sbuf->hwbuf);
435
436 /* This user/malloc buffer is now indistinguishable from a gpu buffer */
437 assert(!sbuf->map.count);
438 if(!sbuf->map.count) {
439 if(sbuf->user)
440 sbuf->user = FALSE;
441 else
442 align_free(sbuf->swbuf);
443 sbuf->swbuf = NULL;
444 }
445
446 pipe_mutex_unlock(ss->swc_mutex);
447 }
448
449 return PIPE_OK;
450 }
451
452
453 /**
454 * Upload the buffer to the host in a piecewise fashion.
455 *
456 * Used when the buffer is too big to fit in the GMR aperture.
457 */
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)
462 {
463 struct svga_winsys_screen *sws = ss->sws;
464 const unsigned alignment = sizeof(void *);
465 const unsigned usage = 0;
466 unsigned i;
467
468 assert(sbuf->map.num_ranges);
469 assert(!sbuf->dma.pending);
470
471 SVGA_DBG(DEBUG_DMA, "dma to sid %p\n", sbuf->handle);
472
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;
477
478 while (offset < range->end) {
479 struct svga_winsys_buffer *hwbuf;
480 uint8_t *map;
481 enum pipe_error ret;
482
483 if (offset + size > range->end)
484 size = range->end - offset;
485
486 hwbuf = sws->buffer_create(sws, alignment, usage, size);
487 while (!hwbuf) {
488 size /= 2;
489 if (!size)
490 return PIPE_ERROR_OUT_OF_MEMORY;
491 hwbuf = sws->buffer_create(sws, alignment, usage, size);
492 }
493
494 SVGA_DBG(DEBUG_DMA, " bytes %u - %u\n",
495 offset, offset + size);
496
497 map = sws->buffer_map(sws, hwbuf,
498 PIPE_TRANSFER_WRITE |
499 PIPE_TRANSFER_DISCARD);
500 assert(map);
501 if (map) {
502 memcpy(map, sbuf->swbuf, size);
503 sws->buffer_unmap(sws, hwbuf);
504 }
505
506 ret = SVGA3D_BufferDMA(svga->swc,
507 hwbuf, sbuf->handle,
508 SVGA3D_WRITE_HOST_VRAM,
509 size, 0, offset, sbuf->dma.flags);
510 if(ret != PIPE_OK) {
511 svga_context_flush(svga, NULL);
512 ret = SVGA3D_BufferDMA(svga->swc,
513 hwbuf, sbuf->handle,
514 SVGA3D_WRITE_HOST_VRAM,
515 size, 0, offset, sbuf->dma.flags);
516 assert(ret == PIPE_OK);
517 }
518
519 sbuf->dma.flags.discard = FALSE;
520
521 sws->buffer_destroy(sws, hwbuf);
522
523 offset += size;
524 }
525 }
526
527 sbuf->map.num_ranges = 0;
528
529 return PIPE_OK;
530 }
531
532
533
534
535 /* Get (or create/upload) the winsys surface handle so that we can
536 * refer to this buffer in fifo commands.
537 */
538 struct svga_winsys_surface *
539 svga_buffer_handle(struct svga_context *svga,
540 struct pipe_resource *buf)
541 {
542 struct pipe_screen *screen = svga->pipe.screen;
543 struct svga_screen *ss = svga_screen(screen);
544 struct svga_buffer *sbuf;
545 enum pipe_error ret;
546
547 if(!buf)
548 return NULL;
549
550 sbuf = svga_buffer(buf);
551
552 assert(!sbuf->map.count);
553 assert(!sbuf->user);
554
555 if(!sbuf->handle) {
556 ret = svga_buffer_create_host_surface(ss, sbuf);
557 if(ret != PIPE_OK)
558 return NULL;
559 }
560
561 assert(sbuf->handle);
562
563 if (sbuf->map.num_ranges) {
564 if (!sbuf->dma.pending) {
565 /*
566 * No pending DMA upload yet, so insert a DMA upload command now.
567 */
568
569 /*
570 * Migrate the data from swbuf -> hwbuf if necessary.
571 */
572 ret = svga_buffer_update_hw(ss, sbuf);
573 if (ret == PIPE_OK) {
574 /*
575 * Queue a dma command.
576 */
577
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);
583 }
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);
588 }
589 }
590 else if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
591 /*
592 * The buffer is too big to fit in the GMR aperture, so break it in
593 * smaller pieces.
594 */
595 ret = svga_buffer_upload_piecewise(ss, svga, sbuf);
596 }
597
598 if (ret != PIPE_OK) {
599 /*
600 * Something unexpected happened above. There is very little that
601 * we can do other than proceeding while ignoring the dirty ranges.
602 */
603 assert(0);
604 sbuf->map.num_ranges = 0;
605 }
606 }
607 else {
608 /*
609 * There a pending dma already. Make sure it is from this context.
610 */
611 assert(sbuf->dma.svga == svga);
612 }
613 }
614
615 assert(!sbuf->map.num_ranges || sbuf->dma.pending);
616
617 return sbuf->handle;
618 }
619
620
621
622 void
623 svga_context_flush_buffers(struct svga_context *svga)
624 {
625 struct list_head *curr, *next;
626 struct svga_buffer *sbuf;
627
628 curr = svga->dirty_buffers.next;
629 next = curr->next;
630 while(curr != &svga->dirty_buffers) {
631 sbuf = LIST_ENTRY(struct svga_buffer, curr, head);
632
633 assert(p_atomic_read(&sbuf->b.b.reference.count) != 0);
634 assert(sbuf->dma.pending);
635
636 svga_buffer_upload_flush(svga, sbuf);
637
638 curr = next;
639 next = curr->next;
640 }
641 }