VC4 is affected by the HW erratum GFXH-16: the QPUs can get stuck in a
loop where instruction cache loads are evicted before they are used.

The QPUs of a slice share one instruction cache. Shader BOs are page
aligned and the code always starts at offset 0, so instructions at the
same position in different shaders end up mapping to the same cache sets.
When the four QPUs of a slice run four different shaders, their fills
evict each other, no instruction executes and the hangcheck resets the
GPU.

Place each shader's code at one of four offsets, 256 bytes apart, inside
its BO, and add the offset when relocating the shader record. Shader code
is position independent, as the validator only accepts relative branches.
The offsets are tracked per client, so shaders a given client creates in
sequence (which tend to run together) start in different sets.

Notice that this approach makes the livelock unlikely, but not impossible.
The next commit will address the remaining cases.

On a Raspberry Pi 3 running the issue's reproducer, GPU resets went from
dozens in an hour to none.

Cc: [email protected]
Fixes: 463873d57014 ("drm/vc4: Add an API for creating GPU shaders in GEM BOs.")
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/16652
Signed-off-by: Maíra Canal <[email protected]>
---
 drivers/gpu/drm/vc4/vc4_bo.c               | 43 +++++++++++++++++++++++-------
 drivers/gpu/drm/vc4/vc4_drv.h              | 10 ++++++-
 drivers/gpu/drm/vc4/vc4_validate.c         |  3 ++-
 drivers/gpu/drm/vc4/vc4_validate_shaders.c |  7 ++---
 4 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/drivers/gpu/drm/vc4/vc4_bo.c b/drivers/gpu/drm/vc4/vc4_bo.c
index da4b11b7ce9f..2ab7c158b7ff 100644
--- a/drivers/gpu/drm/vc4/vc4_bo.c
+++ b/drivers/gpu/drm/vc4/vc4_bo.c
@@ -17,6 +17,7 @@
  */
 
 #include <linux/dma-buf.h>
+#include <linux/overflow.h>
 
 #include <drm/drm_fourcc.h>
 #include <drm/drm_print.h>
@@ -836,6 +837,19 @@ int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
        return 0;
 }
 
+/*
+ * QPUs sharing a slice's instruction cache can livelock evicting each
+ * other's fills. Considering that shader BOs are page aligned, any code
+ * at offset 0 maps every shader to the same cache sets. This makes a
+ * livelock more likely to happen due to GFXH-16.
+ *
+ * Start each shader at one of four offsets, cycled per client, to spread
+ * them across sets. The use of four different offsets is motivated by the
+ * number of QPUs per slice (4 QPUs).
+ */
+#define VC4_SHADER_OFFSET_STEP 256
+#define VC4_SHADER_OFFSET_NUM  4
+
 int
 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
                           struct drm_file *file_priv)
@@ -844,6 +858,7 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void 
*data,
        struct vc4_file *vc4file = file_priv->driver_priv;
        struct vc4_dev *vc4 = to_vc4_dev(dev);
        struct vc4_bo *bo = NULL;
+       u32 offset, size;
        int ret;
 
        if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
@@ -865,29 +880,37 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void 
*data,
                return -EINVAL;
        }
 
+       offset = ((u32)atomic_inc_return(&vc4file->shader_offset_idx) %
+                 VC4_SHADER_OFFSET_NUM) * VC4_SHADER_OFFSET_STEP;
+
+       if (check_add_overflow(offset, args->size, &size))
+               return -EINVAL;
+
        ret = vc4_grab_bin_bo(vc4, vc4file);
        if (ret)
                return ret;
 
-       bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
+       bo = vc4_bo_create(dev, size, true, VC4_BO_TYPE_V3D_SHADER);
        if (IS_ERR(bo))
                return PTR_ERR(bo);
 
        bo->madv = VC4_MADV_WILLNEED;
+       bo->shader_offset = offset;
 
-       if (copy_from_user(bo->base.vaddr,
-                            (void __user *)(uintptr_t)args->data,
-                            args->size)) {
+       /* Clear the memory around the code, which may come from the BO
+        * cache.
+        */
+       memset(bo->base.vaddr, 0, offset);
+       if (copy_from_user(bo->base.vaddr + offset,
+                          (void __user *)(uintptr_t)args->data,
+                          args->size)) {
                ret = -EFAULT;
                goto fail;
        }
-       /* Clear the rest of the memory from allocating from the BO
-        * cache.
-        */
-       memset(bo->base.vaddr + args->size, 0,
-              bo->base.base.size - args->size);
+       memset(bo->base.vaddr + offset + args->size, 0,
+              bo->base.base.size - offset - args->size);
 
-       bo->validated_shader = vc4_validate_shader(&bo->base);
+       bo->validated_shader = vc4_validate_shader(&bo->base, offset);
        if (!bo->validated_shader) {
                ret = -EINVAL;
                goto fail;
diff --git a/drivers/gpu/drm/vc4/vc4_drv.h b/drivers/gpu/drm/vc4/vc4_drv.h
index f68486be08a6..62ce83322d1b 100644
--- a/drivers/gpu/drm/vc4/vc4_drv.h
+++ b/drivers/gpu/drm/vc4/vc4_drv.h
@@ -255,6 +255,9 @@ struct vc4_bo {
         */
        struct vc4_validated_shader_info *validated_shader;
 
+       /* Byte offset of the shader code in the BO. */
+       u32 shader_offset;
+
        /* One of enum vc4_kernel_bo_type, or VC4_BO_TYPE_COUNT + i
         * for user-allocated labels.
         */
@@ -802,6 +805,11 @@ struct vc4_file {
        struct xarray perfmons;
 
        bool bin_bo_used;
+
+       /* Counter picking the start offset of the next shader BO's code,
+        * see vc4_create_shader_bo_ioctl().
+        */
+       atomic_t shader_offset_idx;
 };
 
 static inline struct vc4_exec_info *
@@ -1089,7 +1097,7 @@ bool vc4_check_tex_size(struct vc4_exec_info *exec,
 
 /* vc4_validate_shader.c */
 struct vc4_validated_shader_info *
-vc4_validate_shader(struct drm_gem_dma_object *shader_obj);
+vc4_validate_shader(struct drm_gem_dma_object *shader_obj, u32 offset);
 
 /* vc4_perfmon.c */
 void vc4_perfmon_get(struct vc4_perfmon *perfmon);
diff --git a/drivers/gpu/drm/vc4/vc4_validate.c 
b/drivers/gpu/drm/vc4/vc4_validate.c
index 240786e7dcd5..2d3b151263e4 100644
--- a/drivers/gpu/drm/vc4/vc4_validate.c
+++ b/drivers/gpu/drm/vc4/vc4_validate.c
@@ -856,7 +856,8 @@ validate_gl_shader_rec(struct drm_device *dev,
                void *uniform_data_u;
                uint32_t tex, uni;
 
-               *(uint32_t *)(pkt_v + o) = bo[i]->dma_addr + src_offset;
+               *(uint32_t *)(pkt_v + o) = bo[i]->dma_addr + src_offset +
+                                          
to_vc4_bo(&bo[i]->base)->shader_offset;
 
                if (src_offset != 0) {
                        DRM_DEBUG("Shaders must be at offset 0 of "
diff --git a/drivers/gpu/drm/vc4/vc4_validate_shaders.c 
b/drivers/gpu/drm/vc4/vc4_validate_shaders.c
index 66502a6a4a8e..a5a1c78eb73d 100644
--- a/drivers/gpu/drm/vc4/vc4_validate_shaders.c
+++ b/drivers/gpu/drm/vc4/vc4_validate_shaders.c
@@ -779,7 +779,7 @@ vc4_handle_branch_target(struct vc4_shader_validation_state 
*validation_state)
 }
 
 struct vc4_validated_shader_info *
-vc4_validate_shader(struct drm_gem_dma_object *shader_obj)
+vc4_validate_shader(struct drm_gem_dma_object *shader_obj, u32 offset)
 {
        struct vc4_dev *vc4 = to_vc4_dev(shader_obj->base.dev);
        bool found_shader_end = false;
@@ -793,8 +793,9 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj)
                return NULL;
 
        memset(&validation_state, 0, sizeof(validation_state));
-       validation_state.shader = shader_obj->vaddr;
-       validation_state.max_ip = shader_obj->base.size / sizeof(uint64_t);
+       validation_state.shader = shader_obj->vaddr + offset;
+       validation_state.max_ip = (shader_obj->base.size - offset) /
+                                 sizeof(uint64_t);
 
        reset_validation_state(&validation_state);
 

-- 
2.56.0

Reply via email to