anv: end command buffer with a potential jump
authorLionel Landwerlin <lionel.g.landwerlin@intel.com>
Wed, 9 Dec 2020 11:19:30 +0000 (13:19 +0200)
committerLionel Landwerlin <lionel.g.landwerlin@intel.com>
Sat, 27 Feb 2021 11:29:07 +0000 (13:29 +0200)
We would like to chain multiple primary command buffer to be submitted
together to i915. For prepare this, add end the command buffers with a
MI_BATCH_BUFFER_START and at submit time, replace it with
MI_BATHC_BUFFER_END if needed.

v2: Don't even consider non softpin platforms

v3: Fix inverted condition

v4: Limit is_chainable() to checking device->use_softpin (Jason)

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2371>

src/intel/vulkan/anv_batch_chain.c
src/intel/vulkan/anv_private.h

index 34e0868..0116f9a 100644 (file)
@@ -614,6 +614,20 @@ cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer *cmd_buffer,
    anv_batch_bo_finish(current_bbo, batch);
 }
 
+static void
+anv_cmd_buffer_record_end_submit(struct anv_cmd_buffer *cmd_buffer)
+{
+   assert(cmd_buffer->device->physical->use_softpin);
+
+   struct anv_batch_bo *last_bbo =
+      list_last_entry(&cmd_buffer->batch_bos, struct anv_batch_bo, link);
+   last_bbo->chained = false;
+
+   uint32_t *batch = cmd_buffer->batch_end;
+   anv_pack_struct(batch, GEN8_MI_BATCH_BUFFER_END,
+                   __anv_cmd_header(GEN8_MI_BATCH_BUFFER_END));
+}
+
 static VkResult
 anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
 {
@@ -919,7 +933,14 @@ anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer)
       /* Save end instruction location to override it later. */
       cmd_buffer->batch_end = cmd_buffer->batch.next;
 
-      anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_END, bbe);
+      /* If we can chain this command buffer to another one, leave some place
+       * for the jump instruction.
+       */
+      batch_bo->chained = anv_cmd_buffer_is_chainable(cmd_buffer);
+      if (batch_bo->chained)
+         emit_batch_buffer_start(cmd_buffer, batch_bo->bo, 0);
+      else
+         anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_END, bbe);
 
       /* Round batch up to an even number of dwords. */
       if ((cmd_buffer->batch.next - cmd_buffer->batch.start) & 4)
@@ -1744,6 +1765,8 @@ anv_queue_execbuf_locked(struct anv_queue *queue,
    }
 
    if (submit->cmd_buffer) {
+      if (!anv_cmd_buffer_is_chainable(submit->cmd_buffer))
+         anv_cmd_buffer_record_end_submit(submit->cmd_buffer);
       result = setup_execbuf_for_cmd_buffer(&execbuf, queue, submit->cmd_buffer);
    } else if (submit->simple_bo) {
       result = anv_execbuf_add_bo(device, &execbuf, submit->simple_bo, NULL, 0);
index 5eb042f..fc62304 100644 (file)
@@ -1587,6 +1587,14 @@ struct anv_batch_bo {
    /* Bytes actually consumed in this batch BO */
    uint32_t                                     length;
 
+   /* When this batch BO is used as part of a primary batch buffer, this
+    * tracked whether it is chained to another primary batch buffer.
+    *
+    * If this is the case, the relocation list's last entry points the
+    * location of the MI_BATCH_BUFFER_START chaining to the next batch.
+    */
+   bool                                         chained;
+
    struct anv_reloc_list                        relocs;
 };
 
@@ -3019,6 +3027,18 @@ struct anv_cmd_buffer {
    uint32_t                                      perf_reloc_idx;
 };
 
+/* Determine whether we can chain a given cmd_buffer to another one. We need
+ * softpin and we also need to make sure that we can edit the end of the batch
+ * to point to next one, which requires the command buffer to not be used
+ * simultaneously.
+ */
+static inline bool
+anv_cmd_buffer_is_chainable(struct anv_cmd_buffer *cmd_buffer)
+{
+   return cmd_buffer->device->physical->use_softpin &&
+      !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT);
+}
+
 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);