svga: add a few more resource updates HUD query
[mesa.git] / src / gallium / drivers / svga / svga_resource_buffer_upload.c
index a657a8bc2240ed0c1e4832e9dc095f0c8268fea8..1121b780af1ca4e1e4e8d79b74404571fe5cbfbb 100644 (file)
  *
  **********************************************************/
 
-#include "svga_cmd.h"
 
+#include "os/os_thread.h"
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
 #include "util/u_inlines.h"
-#include "os/os_thread.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 
+#include "svga_cmd.h"
 #include "svga_context.h"
-#include "svga_screen.h"
+#include "svga_debug.h"
 #include "svga_resource_buffer.h"
 #include "svga_resource_buffer_upload.h"
+#include "svga_screen.h"
 #include "svga_winsys.h"
-#include "svga_debug.h"
+
+/**
+ * Describes a complete SVGA_3D_CMD_UPDATE_GB_IMAGE command
+ *
+ */
+struct svga_3d_update_gb_image {
+   SVGA3dCmdHeader header;
+   SVGA3dCmdUpdateGBImage body;
+};
+
+struct svga_3d_invalidate_gb_image {
+   SVGA3dCmdHeader header;
+   SVGA3dCmdInvalidateGBImage body;
+};
 
 
 /**
  */
 struct svga_winsys_buffer *
 svga_winsys_buffer_create( struct svga_context *svga,
-                           unsigned alignment, 
+                           unsigned alignment,
                            unsigned usage,
                            unsigned size )
 {
    struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
    struct svga_winsys_screen *sws = svgascreen->sws;
    struct svga_winsys_buffer *buf;
-   
+
    /* Just try */
    buf = sws->buffer_create(sws, alignment, usage, size);
    if (!buf) {
       SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "flushing context to find %d bytes GMR\n",
-               size); 
-      
+               size);
+
       /* Try flushing all pending DMAs */
       svga_context_flush(svga, NULL);
       buf = sws->buffer_create(sws, alignment, usage, size);
    }
-   
+
    return buf;
 }
 
 
+/**
+ * Destroy HW storage if separate from the host surface.
+ * In the GB case, the HW storage is associated with the host surface
+ * and is therefore a No-op.
+ */
 void
 svga_buffer_destroy_hw_storage(struct svga_screen *ss, struct svga_buffer *sbuf)
 {
@@ -79,7 +98,7 @@ svga_buffer_destroy_hw_storage(struct svga_screen *ss, struct svga_buffer *sbuf)
 
    assert(!sbuf->map.count);
    assert(sbuf->hwbuf);
-   if(sbuf->hwbuf) {
+   if (sbuf->hwbuf) {
       sws->buffer_destroy(sws, sbuf->hwbuf);
       sbuf->hwbuf = NULL;
    }
@@ -88,8 +107,8 @@ svga_buffer_destroy_hw_storage(struct svga_screen *ss, struct svga_buffer *sbuf)
 
 
 /**
- * Allocate DMA'ble storage for the buffer. 
- * 
+ * Allocate DMA'ble or Updatable storage for the buffer.
+ *
  * Called before mapping a buffer.
  */
 enum pipe_error
@@ -98,19 +117,23 @@ svga_buffer_create_hw_storage(struct svga_screen *ss,
 {
    assert(!sbuf->user);
 
-   if(!sbuf->hwbuf) {
+   if (ss->sws->have_gb_objects) {
+      assert(sbuf->handle || !sbuf->dma.pending);
+      return svga_buffer_create_host_surface(ss, sbuf);
+   }
+   if (!sbuf->hwbuf) {
       struct svga_winsys_screen *sws = ss->sws;
       unsigned alignment = 16;
       unsigned usage = 0;
       unsigned size = sbuf->b.b.width0;
-      
+
       sbuf->hwbuf = sws->buffer_create(sws, alignment, usage, size);
-      if(!sbuf->hwbuf)
+      if (!sbuf->hwbuf)
          return PIPE_ERROR_OUT_OF_MEMORY;
-      
+
       assert(!sbuf->dma.pending);
    }
-   
+
    return PIPE_OK;
 }
 
@@ -122,29 +145,43 @@ svga_buffer_create_host_surface(struct svga_screen *ss,
 {
    assert(!sbuf->user);
 
-   if(!sbuf->handle) {
+   if (!sbuf->handle) {
       sbuf->key.flags = 0;
-      
+
       sbuf->key.format = SVGA3D_BUFFER;
-      if(sbuf->b.b.bind & PIPE_BIND_VERTEX_BUFFER)
+      if (sbuf->bind_flags & PIPE_BIND_VERTEX_BUFFER) {
          sbuf->key.flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
-      if(sbuf->b.b.bind & PIPE_BIND_INDEX_BUFFER)
+         sbuf->key.flags |= SVGA3D_SURFACE_BIND_VERTEX_BUFFER;
+      }
+      if (sbuf->bind_flags & PIPE_BIND_INDEX_BUFFER) {
          sbuf->key.flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
-      
+         sbuf->key.flags |= SVGA3D_SURFACE_BIND_INDEX_BUFFER;
+      }
+      if (sbuf->bind_flags & PIPE_BIND_CONSTANT_BUFFER)
+         sbuf->key.flags |= SVGA3D_SURFACE_BIND_CONSTANT_BUFFER;
+
+      if (sbuf->bind_flags & PIPE_BIND_STREAM_OUTPUT)
+         sbuf->key.flags |= SVGA3D_SURFACE_BIND_STREAM_OUTPUT;
+
+      if (sbuf->bind_flags & PIPE_BIND_SAMPLER_VIEW)
+         sbuf->key.flags |= SVGA3D_SURFACE_BIND_SHADER_RESOURCE;
+
       sbuf->key.size.width = sbuf->b.b.width0;
       sbuf->key.size.height = 1;
       sbuf->key.size.depth = 1;
-      
+
       sbuf->key.numFaces = 1;
       sbuf->key.numMipLevels = 1;
       sbuf->key.cachable = 1;
-      
+      sbuf->key.arraySize = 1;
+
       SVGA_DBG(DEBUG_DMA, "surface_create for buffer sz %d\n", sbuf->b.b.width0);
 
-      sbuf->handle = svga_screen_surface_create(ss, &sbuf->key);
-      if(!sbuf->handle)
+      sbuf->handle = svga_screen_surface_create(ss, sbuf->b.b.bind,
+                                                sbuf->b.b.usage, &sbuf->key);
+      if (!sbuf->handle)
          return PIPE_ERROR_OUT_OF_MEMORY;
-   
+
       /* Always set the discard flag on the first time the buffer is written
        * as svga_screen_surface_create might have passed a recycled host
        * buffer.
@@ -153,20 +190,131 @@ svga_buffer_create_host_surface(struct svga_screen *ss,
 
       SVGA_DBG(DEBUG_DMA, "   --> got sid %p sz %d (buffer)\n", sbuf->handle, sbuf->b.b.width0);
    }
-   
+
    return PIPE_OK;
-}   
+}
 
 
 void
 svga_buffer_destroy_host_surface(struct svga_screen *ss,
                                  struct svga_buffer *sbuf)
 {
-   if(sbuf->handle) {
+   if (sbuf->handle) {
       SVGA_DBG(DEBUG_DMA, " ungrab sid %p sz %d\n", sbuf->handle, sbuf->b.b.width0);
       svga_screen_surface_destroy(ss, &sbuf->key, &sbuf->handle);
    }
-}   
+}
+
+
+/**
+ * Insert a number of preliminary UPDATE_GB_IMAGE commands in the
+ * command buffer, equal to the current number of mapped ranges.
+ * The UPDATE_GB_IMAGE commands will be patched with the
+ * actual ranges just before flush.
+ */
+static enum pipe_error
+svga_buffer_upload_gb_command(struct svga_context *svga,
+                             struct svga_buffer *sbuf)
+{
+   struct svga_winsys_context *swc = svga->swc;
+   SVGA3dCmdUpdateGBImage *update_cmd;
+   struct svga_3d_update_gb_image *whole_update_cmd = NULL;
+   uint32 numBoxes = sbuf->map.num_ranges;
+   struct pipe_resource *dummy;
+   unsigned i;
+
+   assert(numBoxes);
+   assert(sbuf->dma.updates == NULL);
+
+   if (sbuf->dma.flags.discard) {
+      struct svga_3d_invalidate_gb_image *cicmd = NULL;
+      SVGA3dCmdInvalidateGBImage *invalidate_cmd;
+      const unsigned total_commands_size =
+         sizeof(*invalidate_cmd) + numBoxes * sizeof(*whole_update_cmd);
+
+      /* Allocate FIFO space for one INVALIDATE_GB_IMAGE command followed by
+       * 'numBoxes' UPDATE_GB_IMAGE commands.  Allocate all at once rather
+       * than with separate commands because we need to properly deal with
+       * filling the command buffer.
+       */
+      invalidate_cmd = SVGA3D_FIFOReserve(swc,
+                                          SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
+                                          total_commands_size, 1 + numBoxes);
+      if (!invalidate_cmd)
+        return PIPE_ERROR_OUT_OF_MEMORY;
+
+      cicmd = container_of(invalidate_cmd, cicmd, body);
+      cicmd->header.size = sizeof(*invalidate_cmd);
+      swc->surface_relocation(swc, &invalidate_cmd->image.sid, NULL, sbuf->handle,
+                              (SVGA_RELOC_WRITE |
+                               SVGA_RELOC_INTERNAL |
+                               SVGA_RELOC_DMA));
+      invalidate_cmd->image.face = 0;
+      invalidate_cmd->image.mipmap = 0;
+
+      /* The whole_update_command is a SVGA3dCmdHeader plus the
+       * SVGA3dCmdUpdateGBImage command.
+       */
+      whole_update_cmd = (struct svga_3d_update_gb_image *) &invalidate_cmd[1];
+      /* initialize the first UPDATE_GB_IMAGE command */
+      whole_update_cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
+      update_cmd = &whole_update_cmd->body;
+
+   } else {
+      /* Allocate FIFO space for 'numBoxes' UPDATE_GB_IMAGE commands */
+      const unsigned total_commands_size =
+         sizeof(*update_cmd) + (numBoxes - 1) * sizeof(*whole_update_cmd);
+
+      update_cmd = SVGA3D_FIFOReserve(swc,
+                                      SVGA_3D_CMD_UPDATE_GB_IMAGE,
+                                      total_commands_size, numBoxes);
+      if (!update_cmd)
+        return PIPE_ERROR_OUT_OF_MEMORY;
+
+      /* The whole_update_command is a SVGA3dCmdHeader plus the
+       * SVGA3dCmdUpdateGBImage command.
+       */
+      whole_update_cmd = container_of(update_cmd, whole_update_cmd, body);
+   }
+
+   /* Init the first UPDATE_GB_IMAGE command */
+   whole_update_cmd->header.size = sizeof(*update_cmd);
+   swc->surface_relocation(swc, &update_cmd->image.sid, NULL, sbuf->handle,
+                          SVGA_RELOC_WRITE | SVGA_RELOC_INTERNAL);
+   update_cmd->image.face = 0;
+   update_cmd->image.mipmap = 0;
+
+   /* Save pointer to the first UPDATE_GB_IMAGE command so that we can
+    * fill in the box info below.
+    */
+   sbuf->dma.updates = whole_update_cmd;
+
+   /*
+    * Copy the face, mipmap, etc. info to all subsequent commands.
+    * Also do the surface relocation for each subsequent command.
+    */
+   for (i = 1; i < numBoxes; ++i) {
+      whole_update_cmd++;
+      memcpy(whole_update_cmd, sbuf->dma.updates, sizeof(*whole_update_cmd));
+
+      swc->surface_relocation(swc, &whole_update_cmd->body.image.sid, NULL,
+                              sbuf->handle,
+                              SVGA_RELOC_WRITE | SVGA_RELOC_INTERNAL);
+   }
+
+   /* Increment reference count */
+   sbuf->dma.svga = svga;
+   dummy = NULL;
+   pipe_resource_reference(&dummy, &sbuf->b.b);
+   SVGA_FIFOCommitAll(swc);
+
+   swc->hints |= SVGA_HINT_FLAG_CAN_PRE_FLUSH;
+   sbuf->dma.flags.discard = FALSE;
+
+   svga->hud.num_resource_updates++;
+
+   return PIPE_OK;
+}
 
 
 /**
@@ -188,11 +336,14 @@ svga_buffer_upload_command(struct svga_context *svga,
    unsigned surface_flags;
    struct pipe_resource *dummy;
 
-   if(transfer == SVGA3D_WRITE_HOST_VRAM) {
+   if (svga_have_gb_objects(svga))
+      return svga_buffer_upload_gb_command(svga, sbuf);
+
+   if (transfer == SVGA3D_WRITE_HOST_VRAM) {
       region_flags = SVGA_RELOC_READ;
       surface_flags = SVGA_RELOC_WRITE;
    }
-   else if(transfer == SVGA3D_READ_HOST_VRAM) {
+   else if (transfer == SVGA3D_READ_HOST_VRAM) {
       region_flags = SVGA_RELOC_WRITE;
       surface_flags = SVGA_RELOC_READ;
    }
@@ -207,13 +358,13 @@ svga_buffer_upload_command(struct svga_context *svga,
                             SVGA_3D_CMD_SURFACE_DMA,
                             sizeof *cmd + numBoxes * sizeof *boxes + sizeof *pSuffix,
                             2);
-   if(!cmd)
+   if (!cmd)
       return PIPE_ERROR_OUT_OF_MEMORY;
 
    swc->region_relocation(swc, &cmd->guest.ptr, guest, 0, region_flags);
    cmd->guest.pitch = 0;
 
-   swc->surface_relocation(swc, &cmd->host.sid, host, surface_flags);
+   swc->surface_relocation(swc, &cmd->host.sid, NULL, host, surface_flags);
    cmd->host.face = 0;
    cmd->host.mipmap = 0;
 
@@ -233,8 +384,11 @@ svga_buffer_upload_command(struct svga_context *svga,
 
    SVGA_FIFOCommitAll(swc);
 
+   swc->hints |= SVGA_HINT_FLAG_CAN_PRE_FLUSH;
    sbuf->dma.flags.discard = FALSE;
 
+   svga->hud.num_buffer_uploads++;
+
    return PIPE_OK;
 }
 
@@ -245,50 +399,84 @@ svga_buffer_upload_command(struct svga_context *svga,
  */
 void
 svga_buffer_upload_flush(struct svga_context *svga,
-                         struct svga_buffer *sbuf)
+                        struct svga_buffer *sbuf)
 {
-   SVGA3dCopyBox *boxes;
    unsigned i;
    struct pipe_resource *dummy;
 
    if (!sbuf->dma.pending) {
+      //debug_printf("no dma pending on buffer\n");
       return;
    }
 
-   assert(sbuf->handle); 
-   assert(sbuf->hwbuf);
+   assert(sbuf->handle);
    assert(sbuf->map.num_ranges);
    assert(sbuf->dma.svga == svga);
-   assert(sbuf->dma.boxes);
-   
+
    /*
-    * Patch the DMA command with the final copy box.
+    * Patch the DMA/update command with the final copy box.
     */
+   if (svga_have_gb_objects(svga)) {
+      struct svga_3d_update_gb_image *update = sbuf->dma.updates;
+      assert(update);
 
-   SVGA_DBG(DEBUG_DMA, "dma to sid %p\n", sbuf->handle);
+      for (i = 0; i < sbuf->map.num_ranges; ++i, ++update) {
+         SVGA3dBox *box = &update->body.box;
+
+         SVGA_DBG(DEBUG_DMA, "  bytes %u - %u\n",
+                  sbuf->map.ranges[i].start, sbuf->map.ranges[i].end);
+
+         box->x = sbuf->map.ranges[i].start;
+         box->y = 0;
+         box->z = 0;
+         box->w = sbuf->map.ranges[i].end - sbuf->map.ranges[i].start;
+         box->h = 1;
+         box->d = 1;
+
+         assert(box->x <= sbuf->b.b.width0);
+         assert(box->x + box->w <= sbuf->b.b.width0);
 
-   boxes = sbuf->dma.boxes;
-   for(i = 0; i < sbuf->map.num_ranges; ++i) {
-      SVGA_DBG(DEBUG_DMA, "  bytes %u - %u\n",
+         svga->hud.num_bytes_uploaded += box->w;
+         svga->hud.num_buffer_uploads++;
+      }
+   }
+   else {
+      assert(sbuf->hwbuf);
+      assert(sbuf->dma.boxes);
+      SVGA_DBG(DEBUG_DMA, "dma to sid %p\n", sbuf->handle);
+
+      for (i = 0; i < sbuf->map.num_ranges; ++i) {
+         SVGA3dCopyBox *box = sbuf->dma.boxes + i;
+
+         SVGA_DBG(DEBUG_DMA, "  bytes %u - %u\n",
                sbuf->map.ranges[i].start, sbuf->map.ranges[i].end);
 
-      boxes[i].x = sbuf->map.ranges[i].start;
-      boxes[i].y = 0;
-      boxes[i].z = 0;
-      boxes[i].w = sbuf->map.ranges[i].end - sbuf->map.ranges[i].start;
-      boxes[i].h = 1;
-      boxes[i].d = 1;
-      boxes[i].srcx = sbuf->map.ranges[i].start;
-      boxes[i].srcy = 0;
-      boxes[i].srcz = 0;
+         box->x = sbuf->map.ranges[i].start;
+         box->y = 0;
+         box->z = 0;
+         box->w = sbuf->map.ranges[i].end - sbuf->map.ranges[i].start;
+         box->h = 1;
+         box->d = 1;
+         box->srcx = sbuf->map.ranges[i].start;
+         box->srcy = 0;
+         box->srcz = 0;
+
+         assert(box->x <= sbuf->b.b.width0);
+         assert(box->x + box->w <= sbuf->b.b.width0);
+
+         svga->hud.num_bytes_uploaded += box->w;
+         svga->hud.num_buffer_uploads++;
+      }
    }
 
+   /* Reset sbuf for next use/upload */
+
    sbuf->map.num_ranges = 0;
 
    assert(sbuf->head.prev && sbuf->head.next);
-   LIST_DEL(&sbuf->head);
+   LIST_DEL(&sbuf->head);  /* remove from svga->dirty_buffers list */
 #ifdef DEBUG
-   sbuf->head.next = sbuf->head.prev = NULL; 
+   sbuf->head.next = sbuf->head.prev = NULL;
 #endif
    sbuf->dma.pending = FALSE;
    sbuf->dma.flags.discard = FALSE;
@@ -296,6 +484,7 @@ svga_buffer_upload_flush(struct svga_context *svga,
 
    sbuf->dma.svga = NULL;
    sbuf->dma.boxes = NULL;
+   sbuf->dma.updates = NULL;
 
    /* Decrement reference count (and potentially destroy) */
    dummy = &sbuf->b.b;
@@ -308,8 +497,9 @@ svga_buffer_upload_flush(struct svga_context *svga,
  *
  * This function only notes the range down. It doesn't actually emit a DMA
  * upload command. That only happens when a context tries to refer to this
- * buffer, and the DMA upload command is added to that context's command buffer.
- * 
+ * buffer, and the DMA upload command is added to that context's command
+ * buffer.
+ *
  * We try to lump as many contiguous DMA transfers together as possible.
  */
 void
@@ -322,7 +512,7 @@ svga_buffer_add_range(struct svga_buffer *sbuf,
    unsigned nearest_dist;
 
    assert(end > start);
-   
+
    if (sbuf->map.num_ranges < SVGA_BUFFER_MAX_RANGES) {
       nearest_range = sbuf->map.num_ranges;
       nearest_dist = ~0;
@@ -335,7 +525,7 @@ svga_buffer_add_range(struct svga_buffer *sbuf,
     * Try to grow one of the ranges.
     */
 
-   for(i = 0; i < sbuf->map.num_ranges; ++i) {
+   for (i = 0; i < sbuf->map.num_ranges; ++i) {
       int left_dist;
       int right_dist;
       int dist;
@@ -409,47 +599,51 @@ svga_buffer_add_range(struct svga_buffer *sbuf,
 /**
  * Copy the contents of the malloc buffer to a hardware buffer.
  */
-static INLINE enum pipe_error
-svga_buffer_update_hw(struct svga_screen *ss, struct svga_buffer *sbuf)
+static enum pipe_error
+svga_buffer_update_hw(struct svga_context *svga, struct svga_buffer *sbuf)
 {
    assert(!sbuf->user);
-   if(!sbuf->hwbuf) {
+   if (!svga_buffer_has_hw_storage(sbuf)) {
+      struct svga_screen *ss = svga_screen(sbuf->b.b.screen);
       enum pipe_error ret;
+      boolean retry;
       void *map;
-      
+
       assert(sbuf->swbuf);
-      if(!sbuf->swbuf)
+      if (!sbuf->swbuf)
          return PIPE_ERROR;
-      
-      ret = svga_buffer_create_hw_storage(ss, sbuf);
-      if(ret != PIPE_OK)
+
+      ret = svga_buffer_create_hw_storage(svga_screen(sbuf->b.b.screen),
+                                         sbuf);
+      if (ret != PIPE_OK)
          return ret;
 
       pipe_mutex_lock(ss->swc_mutex);
-      map = ss->sws->buffer_map(ss->sws, sbuf->hwbuf, PIPE_TRANSFER_WRITE);
+      map = svga_buffer_hw_storage_map(svga, sbuf, PIPE_TRANSFER_WRITE, &retry);
       assert(map);
-      if(!map) {
+      assert(!retry);
+      if (!map) {
         pipe_mutex_unlock(ss->swc_mutex);
          svga_buffer_destroy_hw_storage(ss, sbuf);
          return PIPE_ERROR;
       }
 
       memcpy(map, sbuf->swbuf, sbuf->b.b.width0);
-      ss->sws->buffer_unmap(ss->sws, sbuf->hwbuf);
+      svga_buffer_hw_storage_unmap(svga, sbuf);
 
       /* This user/malloc buffer is now indistinguishable from a gpu buffer */
       assert(!sbuf->map.count);
-      if(!sbuf->map.count) {
-         if(sbuf->user)
+      if (!sbuf->map.count) {
+         if (sbuf->user)
             sbuf->user = FALSE;
          else
             align_free(sbuf->swbuf);
          sbuf->swbuf = NULL;
       }
-      
+
       pipe_mutex_unlock(ss->swc_mutex);
    }
-   
+
    return PIPE_OK;
 }
 
@@ -458,8 +652,11 @@ svga_buffer_update_hw(struct svga_screen *ss, struct svga_buffer *sbuf)
  * Upload the buffer to the host in a piecewise fashion.
  *
  * Used when the buffer is too big to fit in the GMR aperture.
+ * This function should never get called in the guest-backed case
+ * since we always have a full-sized hardware storage backing the
+ * host surface.
  */
-static INLINE enum pipe_error
+static enum pipe_error
 svga_buffer_upload_piecewise(struct svga_screen *ss,
                              struct svga_context *svga,
                              struct svga_buffer *sbuf)
@@ -471,6 +668,7 @@ svga_buffer_upload_piecewise(struct svga_screen *ss,
 
    assert(sbuf->map.num_ranges);
    assert(!sbuf->dma.pending);
+   assert(!svga_have_gb_objects(svga));
 
    SVGA_DBG(DEBUG_DMA, "dma to sid %p\n", sbuf->handle);
 
@@ -500,10 +698,10 @@ svga_buffer_upload_piecewise(struct svga_screen *ss,
 
          map = sws->buffer_map(sws, hwbuf,
                                PIPE_TRANSFER_WRITE |
-                               PIPE_TRANSFER_DISCARD);
+                               PIPE_TRANSFER_DISCARD_RANGE);
          assert(map);
          if (map) {
-            memcpy(map, sbuf->swbuf, size);
+            memcpy(map, (const char *) sbuf->swbuf + offset, size);
             sws->buffer_unmap(sws, hwbuf);
          }
 
@@ -511,7 +709,7 @@ svga_buffer_upload_piecewise(struct svga_screen *ss,
                                 hwbuf, sbuf->handle,
                                 SVGA3D_WRITE_HOST_VRAM,
                                 size, 0, offset, sbuf->dma.flags);
-         if(ret != PIPE_OK) {
+         if (ret != PIPE_OK) {
             svga_context_flush(svga, NULL);
             ret =  SVGA3D_BufferDMA(svga->swc,
                                     hwbuf, sbuf->handle,
@@ -534,10 +732,12 @@ svga_buffer_upload_piecewise(struct svga_screen *ss,
 }
 
 
-
-
-/* Get (or create/upload) the winsys surface handle so that we can
+/**
+ * Get (or create/upload) the winsys surface handle so that we can
  * refer to this buffer in fifo commands.
+ * This function will create the host surface, and in the GB case also the
+ * hardware storage. In the non-GB case, the hardware storage will be created
+ * if there are mapped ranges and the data is currently in a malloc'ed buffer.
  */
 struct svga_winsys_surface *
 svga_buffer_handle(struct svga_context *svga,
@@ -548,17 +748,21 @@ svga_buffer_handle(struct svga_context *svga,
    struct svga_buffer *sbuf;
    enum pipe_error ret;
 
-   if(!buf)
+   if (!buf)
       return NULL;
 
    sbuf = svga_buffer(buf);
-   
-   assert(!sbuf->map.count);
+
    assert(!sbuf->user);
-   
-   if(!sbuf->handle) {
-      ret = svga_buffer_create_host_surface(ss, sbuf);
-      if(ret != PIPE_OK)
+
+   if (!sbuf->handle) {
+      /* This call will set sbuf->handle */
+      if (svga_have_gb_objects(svga)) {
+        ret = svga_buffer_update_hw(svga, sbuf);
+      } else {
+        ret = svga_buffer_create_host_surface(ss, sbuf);
+      }
+      if (ret != PIPE_OK)
         return NULL;
    }
 
@@ -573,7 +777,7 @@ svga_buffer_handle(struct svga_context *svga,
          /*
           * Migrate the data from swbuf -> hwbuf if necessary.
           */
-         ret = svga_buffer_update_hw(ss, sbuf);
+         ret = svga_buffer_update_hw(svga, sbuf);
          if (ret == PIPE_OK) {
             /*
              * Queue a dma command.
@@ -636,31 +840,10 @@ svga_context_flush_buffers(struct svga_context *svga)
 
       assert(p_atomic_read(&sbuf->b.b.reference.count) != 0);
       assert(sbuf->dma.pending);
-      
+
       svga_buffer_upload_flush(svga, sbuf);
 
-      curr = next; 
+      curr = next;
       next = curr->next;
    }
 }
-
-
-void
-svga_redefine_user_buffer(struct pipe_context *pipe,
-                          struct pipe_resource *resource,
-                          unsigned offset,
-                          unsigned size)
-{
-   struct svga_buffer *sbuf = svga_buffer(resource);
-
-   assert(sbuf->user);
-   assert(!sbuf->dma.pending);
-   assert(!sbuf->handle);
-   assert(!sbuf->hwbuf);
-
-   /*
-    * We always treat the contents of user-buffers as volatile,
-    * so no particular action needed here.
-    */
-
-}