iris: Fix compute scratch pinning
[mesa.git] / src / gallium / drivers / iris / iris_state.c
index e4e42f50d8268525c118cb8b317938e315e48484..a8d644eeae6b5bb3b0188321ac73861c018799fe 100644 (file)
@@ -161,7 +161,14 @@ __gen_combine_address(struct iris_batch *batch, void *location,
 #include "genxml/gen_macros.h"
 #include "genxml/genX_bits.h"
 
-#define MOCS_WB (2 << 1)
+#define MOCS_PTE (1 << 1)
+#define MOCS_WB  (2 << 1)
+
+static uint32_t
+mocs(struct iris_bo *bo)
+{
+   return bo && bo->external ? MOCS_PTE : MOCS_WB;
+}
 
 /**
  * Statically assert that PIPE_* enums match the hardware packets.
@@ -576,15 +583,12 @@ init_state_base_address(struct iris_batch *batch)
     * updated occasionally.  See iris_binder.c for the details there.
     */
    iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
-   #if 0
-   // XXX: MOCS is stupid for this.
-      sba.GeneralStateMemoryObjectControlState            = MOCS_WB;
-      sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
-      sba.DynamicStateMemoryObjectControlState            = MOCS_WB;
-      sba.IndirectObjectMemoryObjectControlState          = MOCS_WB;
-      sba.InstructionMemoryObjectControlState             = MOCS_WB;
-      sba.BindlessSurfaceStateMemoryObjectControlState    = MOCS_WB;
-   #endif
+      sba.GeneralStateMOCS            = MOCS_WB;
+      sba.StatelessDataPortAccessMOCS = MOCS_WB;
+      sba.DynamicStateMOCS            = MOCS_WB;
+      sba.IndirectObjectMOCS          = MOCS_WB;
+      sba.InstructionMOCS             = MOCS_WB;
+      sba.BindlessSurfaceStateMOCS    = MOCS_WB;
 
       sba.GeneralStateBaseAddressModifyEnable   = true;
       sba.DynamicStateBaseAddressModifyEnable   = true;
@@ -1513,7 +1517,7 @@ fill_buffer_surface_state(struct isl_device *isl_dev,
                          .size_B = final_size,
                          .format = format,
                          .stride_B = cpp,
-                         .mocs = MOCS_WB);
+                         .mocs = mocs(bo));
 }
 
 /**
@@ -1541,7 +1545,7 @@ fill_surface_state(struct isl_device *isl_dev,
    struct isl_surf_fill_state_info f = {
       .surf = &res->surf,
       .view = view,
-      .mocs = MOCS_WB,
+      .mocs = mocs(res->bo),
       .address = res->bo->gtt_offset,
    };
 
@@ -2090,10 +2094,7 @@ iris_set_framebuffer_state(struct pipe_context *ctx,
       .swizzle = ISL_SWIZZLE_IDENTITY,
    };
 
-   struct isl_depth_stencil_hiz_emit_info info = {
-      .view = &view,
-      .mocs = MOCS_WB,
-   };
+   struct isl_depth_stencil_hiz_emit_info info = { .view = &view };
 
    if (cso->zsbuf) {
       iris_get_depth_stencil_resources(cso->zsbuf->texture, &zres,
@@ -2109,7 +2110,7 @@ iris_set_framebuffer_state(struct pipe_context *ctx,
 
          info.depth_surf = &zres->surf;
          info.depth_address = zres->bo->gtt_offset;
-         info.hiz_usage = ISL_AUX_USAGE_NONE;
+         info.mocs = mocs(zres->bo);
 
          view.format = zres->surf.format;
       }
@@ -2118,8 +2119,10 @@ iris_set_framebuffer_state(struct pipe_context *ctx,
          view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
          info.stencil_surf = &stencil_res->surf;
          info.stencil_address = stencil_res->bo->gtt_offset;
-         if (!zres)
+         if (!zres) {
             view.format = stencil_res->surf.format;
+            info.mocs = mocs(stencil_res->bo);
+         }
       }
    }
 
@@ -2189,7 +2192,7 @@ upload_ubo_surf_state(struct iris_context *ice,
                                         res->bo->size - cbuf->data.offset),
                          .format = ISL_FORMAT_R32G32B32A32_FLOAT,
                          .stride_B = 1,
-                         .mocs = MOCS_WB)
+                         .mocs = mocs(res->bo))
 }
 
 /**
@@ -2337,7 +2340,7 @@ iris_set_shader_buffers(struct pipe_context *ctx,
                                        res->bo->size - buffer->buffer_offset),
                                .format = ISL_FORMAT_RAW,
                                .stride_B = 1,
-                               .mocs = MOCS_WB);
+                               .mocs = mocs(res->bo));
       } else {
          pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
          pipe_resource_reference(&shs->ssbo_surface_state[start_slot + i].res,
@@ -2391,13 +2394,13 @@ iris_set_vertex_buffers(struct pipe_context *ctx,
 
       iris_pack_state(GENX(VERTEX_BUFFER_STATE), state->state, vb) {
          vb.VertexBufferIndex = start_slot + i;
-         vb.MOCS = MOCS_WB;
          vb.AddressModifyEnable = true;
          vb.BufferPitch = buffer->stride;
          if (res) {
             vb.BufferSize = res->bo->size;
             vb.BufferStartingAddress =
                ro_bo(NULL, res->bo->gtt_offset + (int) buffer->buffer_offset);
+            vb.MOCS = mocs(res->bo);
          } else {
             vb.NullVertexBuffer = true;
          }
@@ -2628,7 +2631,7 @@ iris_set_stream_output_targets(struct pipe_context *ctx,
          sob.SOBufferEnable = true;
          sob.StreamOffsetWriteEnable = true;
          sob.StreamOutputBufferOffsetAddressEnable = true;
-         sob.MOCS = MOCS_WB; // XXX: MOCS
+         sob.MOCS = mocs(res->bo);
 
          sob.SurfaceSize = MAX2(tgt->base.buffer_size / 4, 1) - 1;
 
@@ -2852,8 +2855,6 @@ iris_emit_sbe_swiz(struct iris_batch *batch,
 
    /* XXX: this should be generated when putting programs in place */
 
-   // XXX: raster->sprite_coord_enable
-
    for (int fs_attr = 0; fs_attr < VARYING_SLOT_MAX; fs_attr++) {
       const int input_index = wm_prog_data->urb_setup[fs_attr];
       if (input_index < 0 || input_index >= 16)
@@ -3047,7 +3048,6 @@ static void
 iris_populate_fs_key(const struct iris_context *ice,
                      struct brw_wm_prog_key *key)
 {
-   /* XXX: dirty flags? */
    const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
    const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
    const struct iris_rasterizer_state *rast = ice->state.cso_rast;
@@ -3068,8 +3068,6 @@ iris_populate_fs_key(const struct iris_context *ice,
 
    key->coherent_fb_fetch = true;
 
-   // XXX: uint64_t input_slots_valid; - for >16 inputs
-
    // XXX: key->force_dual_color_blend for unigine
    // XXX: respect hint for high_quality_derivatives:1;
 }
@@ -3113,8 +3111,9 @@ KSP(const struct iris_compiled_shader *shader)
    pkt.Enable           = true;                                           \
                                                                           \
    if (prog_data->total_scratch) {                                        \
-      uint32_t scratch_addr =                                             \
+      struct iris_bo *bo =                                                \
          iris_get_scratch_space(ice, prog_data->total_scratch, stage);    \
+      uint32_t scratch_addr = bo->gtt_offset;                             \
       pkt.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;     \
       pkt.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);            \
    }
@@ -3308,9 +3307,10 @@ iris_store_fs_state(struct iris_context *ice,
          KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
 
       if (prog_data->total_scratch) {
-         uint32_t scratch_addr =
+         struct iris_bo *bo =
             iris_get_scratch_space(ice, prog_data->total_scratch,
                                    MESA_SHADER_FRAGMENT);
+         uint32_t scratch_addr = bo->gtt_offset;
          ps.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
          ps.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
       }
@@ -3722,8 +3722,6 @@ iris_restore_render_saved_bos(struct iris_context *ice,
 {
    struct iris_genx_state *genx = ice->state.genx;
 
-   // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
-
    const uint64_t clean = ~ice->state.dirty;
 
    if (clean & IRIS_DIRTY_CC_VIEWPORT) {
@@ -3804,12 +3802,19 @@ iris_restore_render_saved_bos(struct iris_context *ice,
    for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
       if (clean & (IRIS_DIRTY_VS << stage)) {
          struct iris_compiled_shader *shader = ice->shaders.prog[stage];
+
          if (shader) {
             struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
             iris_use_pinned_bo(batch, bo, false);
-         }
 
-         // XXX: scratch buffer
+            struct brw_stage_prog_data *prog_data = shader->prog_data;
+
+            if (prog_data->total_scratch > 0) {
+               struct iris_bo *bo =
+                  iris_get_scratch_space(ice, prog_data->total_scratch, stage);
+               iris_use_pinned_bo(batch, bo, true);
+            }
+         }
       }
    }
 
@@ -3820,11 +3825,14 @@ iris_restore_render_saved_bos(struct iris_context *ice,
          struct iris_resource *zres, *sres;
          iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
                                           &zres, &sres);
-         // XXX: might not be writable...
-         if (zres)
-            iris_use_pinned_bo(batch, zres->bo, true);
-         if (sres)
-            iris_use_pinned_bo(batch, sres->bo, true);
+         if (zres) {
+            iris_use_pinned_bo(batch, zres->bo,
+                               ice->state.depth_writes_enabled);
+         }
+         if (sres) {
+            iris_use_pinned_bo(batch, sres->bo,
+                               ice->state.stencil_writes_enabled);
+         }
       }
    }
 
@@ -3886,12 +3894,19 @@ iris_restore_compute_saved_bos(struct iris_context *ice,
 
    if (clean & IRIS_DIRTY_CS) {
       struct iris_compiled_shader *shader = ice->shaders.prog[stage];
+
       if (shader) {
          struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
          iris_use_pinned_bo(batch, bo, false);
-      }
 
-      // XXX: scratch buffer
+         struct brw_stage_prog_data *prog_data = shader->prog_data;
+
+         if (prog_data->total_scratch > 0) {
+            struct iris_bo *bo =
+               iris_get_scratch_space(ice, prog_data->total_scratch, stage);
+            iris_use_pinned_bo(batch, bo, true);
+         }
+      }
    }
 }
 
@@ -3908,7 +3923,7 @@ iris_update_surface_base_address(struct iris_batch *batch,
    flush_for_state_base_change(batch);
 
    iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
-      // XXX: sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
+      sba.SurfaceStateMOCS = MOCS_WB;
       sba.SurfaceStateBaseAddressModifyEnable = true;
       sba.SurfaceStateBaseAddress = ro_bo(binder->bo, 0);
    }
@@ -4011,9 +4026,6 @@ iris_upload_dirty_render_state(struct iris_context *ice,
       }
    }
 
-   /* XXX: L3 State */
-
-   // XXX: this is only flagged at setup, we assume a static configuration
    if (dirty & IRIS_DIRTY_URB) {
       iris_upload_urb_config(ice, batch);
    }
@@ -4300,7 +4312,6 @@ iris_upload_dirty_render_state(struct iris_context *ice,
 
    }
 
-   /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
    if (dirty & IRIS_DIRTY_WM) {
       struct iris_rasterizer_state *cso = ice->state.cso_rast;
       uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
@@ -4371,11 +4382,15 @@ iris_upload_dirty_render_state(struct iris_context *ice,
          struct iris_resource *zres, *sres;
          iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
                                           &zres, &sres);
-         // XXX: might not be writable...
-         if (zres)
-            iris_use_pinned_bo(batch, zres->bo, true);
-         if (sres)
-            iris_use_pinned_bo(batch, sres->bo, true);
+         if (zres) {
+            iris_use_pinned_bo(batch, zres->bo,
+                               ice->state.depth_writes_enabled);
+         }
+
+         if (sres) {
+            iris_use_pinned_bo(batch, sres->bo,
+                               ice->state.stencil_writes_enabled);
+         }
       }
    }
 
@@ -4543,7 +4558,7 @@ iris_upload_render_state(struct iris_context *ice,
 
       iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
          ib.IndexFormat = draw->index_size >> 1;
-         ib.MOCS = MOCS_WB;
+         ib.MOCS = mocs(bo);
          ib.BufferSize = bo->size;
          ib.BufferStartingAddress = ro_bo(bo, offset);
       }
@@ -4695,11 +4710,11 @@ iris_upload_compute_state(struct iris_context *ice,
 
       iris_emit_cmd(batch, GENX(MEDIA_VFE_STATE), vfe) {
          if (prog_data->total_scratch) {
-            uint32_t scratch_addr =
+            struct iris_bo *bo =
                iris_get_scratch_space(ice, prog_data->total_scratch,
                                       MESA_SHADER_COMPUTE);
             vfe.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
-            vfe.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
+            vfe.ScratchSpaceBasePointer = rw_bo(bo, 0);
          }
 
          vfe.MaximumNumberofThreads =