CIX Sky1 needs system-cache allocation policies that match the platform
integration. Without these settings, GPU rendering results are displayed
incorrectly.

Read the policies from arm,sysc-alloc instead of hardcoding platform
register values. Validate the GPU architecture, source IDs and supported
read/write policies before saving the configuration. Reject duplicate
source IDs and invalid entries.

Restore the policies before L2 power-on through the common hardware
wrapper so that both legacy and PWR backends apply them after reset and
runtime resume. Leave the registers unchanged when no property is present.

Signed-off-by: Cunyuan Liu <[email protected]>
---
 drivers/gpu/drm/panthor/panthor_gpu.c      | 187 +++++++++++++++++++++
 drivers/gpu/drm/panthor/panthor_gpu.h      |   1 +
 drivers/gpu/drm/panthor/panthor_gpu_regs.h |  58 +++++++
 drivers/gpu/drm/panthor/panthor_hw.c       |  13 ++
 drivers/gpu/drm/panthor/panthor_hw.h       |   5 +-
 5 files changed, 260 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/panthor/panthor_gpu.c 
b/drivers/gpu/drm/panthor/panthor_gpu.c
index a383b04f101e..118e13e56555 100644
--- a/drivers/gpu/drm/panthor/panthor_gpu.c
+++ b/drivers/gpu/drm/panthor/panthor_gpu.c
@@ -12,6 +12,8 @@
 #include <linux/iopoll.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/property.h>
+#include <linux/string.h>
 
 #include <drm/drm_drv.h>
 #include <drm/drm_managed.h>
@@ -46,6 +48,15 @@ struct panthor_gpu {
 
        /** @cache_flush_lock: Lock to serialize cache flushes */
        struct mutex cache_flush_lock;
+
+       /**
+        * @sysc_alloc: System-cache allocation policies for GPU architecture
+        * 11.0.3 and later.
+        */
+       u32 sysc_alloc[GPU_SYSC_ALLOC_COUNT];
+
+       /** @sysc_alloc_enabled: Apply the system-cache allocation policies. */
+       bool sysc_alloc_enabled;
 };
 
 #define GPU_INTERRUPTS_MASK    \
@@ -63,6 +74,178 @@ static void panthor_gpu_coherency_set(struct panthor_device 
*ptdev)
                  ptdev->gpu_info.selected_coherency);
 }
 
+enum panthor_sysc_alloc_cell {
+       PANTHOR_SYSC_ALLOC_SOURCE,
+       PANTHOR_SYSC_ALLOC_READ,
+       PANTHOR_SYSC_ALLOC_WRITE,
+       PANTHOR_SYSC_ALLOC_CELLS,
+};
+
+static bool panthor_gpu_has_sysc_alloc(u32 gpu_id)
+{
+       /* SYSC_ALLOC is available from GPU architecture 11.0.3. */
+       return GPU_ARCH_MAJOR(gpu_id) > 11 ||
+              (GPU_ARCH_MAJOR(gpu_id) == 11 &&
+               (GPU_ARCH_MINOR(gpu_id) > 0 || GPU_ARCH_REV(gpu_id) >= 3));
+}
+
+static bool panthor_gpu_sysc_alloc_policy_valid(u32 policy)
+{
+       switch (policy) {
+       case GPU_SYSC_ALLOC_L2_ALLOC:
+       case GPU_SYSC_ALLOC_NEVER_ALLOC:
+       case GPU_SYSC_ALLOC_ALWAYS_ALLOC:
+       case GPU_SYSC_ALLOC_PARTIAL_ALLOC:
+       case GPU_SYSC_ALLOC_L2_PARTIAL_ALLOC:
+               return true;
+       default:
+               return false;
+       }
+}
+
+static bool panthor_gpu_sysc_alloc_read_supported(u32 gpu_id, u32 source)
+{
+       switch (source) {
+       case GPU_SYSC_ALLOC_R_OTHER:
+       case GPU_SYSC_ALLOC_R_CSF:
+       case GPU_SYSC_ALLOC_R_MMU:
+       case GPU_SYSC_ALLOC_R_TILER_VERT:
+       case GPU_SYSC_ALLOC_R_TILER_PTR:
+       case GPU_SYSC_ALLOC_R_TILER_INDEX:
+       case GPU_SYSC_ALLOC_R_TILER_OTHER:
+       case GPU_SYSC_ALLOC_R_IC:
+       case GPU_SYSC_ALLOC_R_ATTR:
+       case GPU_SYSC_ALLOC_R_SCM:
+       case GPU_SYSC_ALLOC_R_FSDC:
+       case GPU_SYSC_ALLOC_R_PLR:
+       case GPU_SYSC_ALLOC_R_TEX:
+       case GPU_SYSC_ALLOC_R_LSC:
+               return true;
+       case GPU_SYSC_ALLOC_R_VL:
+               /* TTIX (architecture 12, product 0) has no VL read policy. */
+               if (GPU_ARCH_MAJOR(gpu_id) == 12 && GPU_PROD_MAJOR(gpu_id) == 0)
+                       return false;
+               return true;
+       case GPU_SYSC_ALLOC_R_RT:
+       case GPU_SYSC_ALLOC_R_NE_A:
+       case GPU_SYSC_ALLOC_R_NE_N:
+               if (GPU_ARCH_MAJOR(gpu_id) < 14)
+                       return false;
+               return true;
+       default:
+               return false;
+       }
+}
+
+static bool panthor_gpu_sysc_alloc_write_supported(u32 gpu_id, u32 source)
+{
+       switch (source) {
+       case GPU_SYSC_ALLOC_W_OTHER:
+       case GPU_SYSC_ALLOC_W_CSF:
+       case GPU_SYSC_ALLOC_W_PCB:
+       case GPU_SYSC_ALLOC_W_TILER_PTR:
+       case GPU_SYSC_ALLOC_W_TILER_VERT_PLIST:
+       case GPU_SYSC_ALLOC_W_TILER_OTHER:
+       case GPU_SYSC_ALLOC_W_L2_EVICT:
+       case GPU_SYSC_ALLOC_W_L2_FLUSH:
+       case GPU_SYSC_ALLOC_W_TIB_COLOR:
+       case GPU_SYSC_ALLOC_W_TIB_COLOR_AFBCH:
+       case GPU_SYSC_ALLOC_W_TIB_COLOR_AFBCB:
+       case GPU_SYSC_ALLOC_W_TIB_CRC:
+       case GPU_SYSC_ALLOC_W_TIB_DS:
+       case GPU_SYSC_ALLOC_W_TIB_DS_AFBCH:
+       case GPU_SYSC_ALLOC_W_TIB_DS_AFBCB:
+       case GPU_SYSC_ALLOC_W_LSC:
+               return true;
+       case GPU_SYSC_ALLOC_W_RT:
+       case GPU_SYSC_ALLOC_W_NE_A:
+       case GPU_SYSC_ALLOC_W_NE_N:
+               if (GPU_ARCH_MAJOR(gpu_id) < 14)
+                       return false;
+               return true;
+       default:
+               return false;
+       }
+}
+
+void panthor_gpu_sysc_config_set(struct panthor_device *ptdev)
+{
+       struct panthor_gpu *gpu = ptdev->gpu;
+       unsigned int i;
+
+       if (!gpu->sysc_alloc_enabled)
+               return;
+
+       for (i = 0; i < ARRAY_SIZE(gpu->sysc_alloc); i++)
+               gpu_write(gpu->iomem, GPU_SYSC_ALLOC(i), gpu->sysc_alloc[i]);
+}
+
+static int panthor_gpu_sysc_config_init(struct panthor_device *ptdev)
+{
+       struct device *dev = ptdev->base.dev;
+       struct panthor_gpu *gpu = ptdev->gpu;
+       u32 entries[GPU_SYSC_ALLOC_SOURCE_COUNT][PANTHOR_SYSC_ALLOC_CELLS];
+       u32 config[GPU_SYSC_ALLOC_COUNT] = {};
+       u32 gpu_id = ptdev->gpu_info.gpu_id;
+       u32 sources = 0;
+       unsigned int i;
+       int count;
+       int ret;
+
+       if (!device_property_present(dev, "arm,sysc-alloc"))
+               return 0;
+
+       if (!panthor_gpu_has_sysc_alloc(gpu_id))
+               return dev_err_probe(dev, -EOPNOTSUPP,
+                                    "SYSC_ALLOC requires GPU architecture 
11.0.3 or later\n");
+
+       count = device_property_count_u32(dev, "arm,sysc-alloc");
+       if (count < 0)
+               return dev_err_probe(dev, count, "Failed to count SYSC_ALLOC 
cells\n");
+       if (!count || count > ARRAY_SIZE(entries) * PANTHOR_SYSC_ALLOC_CELLS ||
+           count % PANTHOR_SYSC_ALLOC_CELLS)
+               return dev_err_probe(dev, -EINVAL, "Invalid SYSC_ALLOC entry 
count\n");
+
+       ret = device_property_read_u32_array(dev, "arm,sysc-alloc", 
&entries[0][0], count);
+       if (ret)
+               return dev_err_probe(dev, ret, "Failed to read SYSC_ALLOC 
entries\n");
+
+       for (i = 0; i < count / PANTHOR_SYSC_ALLOC_CELLS; i++) {
+               u32 source = entries[i][PANTHOR_SYSC_ALLOC_SOURCE];
+               u32 read = entries[i][PANTHOR_SYSC_ALLOC_READ];
+               u32 write = entries[i][PANTHOR_SYSC_ALLOC_WRITE];
+
+               if (source >= GPU_SYSC_ALLOC_SOURCE_COUNT)
+                       return dev_err_probe(dev, -EINVAL,
+                                            "Invalid SYSC_ALLOC source %u\n", 
source);
+               if (sources & BIT(source))
+                       return dev_err_probe(dev, -EINVAL,
+                                            "Duplicate SYSC_ALLOC source 
%u\n", source);
+               sources |= BIT(source);
+
+               if (!panthor_gpu_sysc_alloc_policy_valid(read) ||
+                   !panthor_gpu_sysc_alloc_policy_valid(write))
+                       return dev_err_probe(dev, -EINVAL,
+                                            "Invalid SYSC_ALLOC policies <%u 
%u %u>\n",
+                                            source, read, write);
+
+               if ((read != GPU_SYSC_ALLOC_L2_ALLOC &&
+                    !panthor_gpu_sysc_alloc_read_supported(gpu_id, source)) ||
+                   (write != GPU_SYSC_ALLOC_L2_ALLOC &&
+                    !panthor_gpu_sysc_alloc_write_supported(gpu_id, source)))
+                       return dev_err_probe(dev, -EINVAL,
+                                            "Unsupported SYSC_ALLOC policies 
<%u %u %u>\n",
+                                            source, read, write);
+
+               config[source / GPU_SYSC_ALLOC_SOURCES_PER_REG] |=
+                       GPU_SYSC_ALLOC_POLICY(source, read, write);
+       }
+
+       memcpy(gpu->sysc_alloc, config, sizeof(config));
+       gpu->sysc_alloc_enabled = true;
+       return 0;
+}
+
 static void panthor_gpu_l2_config_set(struct panthor_device *ptdev)
 {
        struct panthor_gpu *gpu = ptdev->gpu;
@@ -164,6 +347,10 @@ int panthor_gpu_init(struct panthor_device *ptdev)
        mutex_init(&gpu->cache_flush_lock);
        ptdev->gpu = gpu;
 
+       ret = panthor_gpu_sysc_config_init(ptdev);
+       if (ret)
+               return ret;
+
        dma_set_max_seg_size(ptdev->base.dev, UINT_MAX);
        pa_bits = GPU_MMU_FEATURES_PA_BITS(ptdev->gpu_info.mmu_features);
        ret = dma_set_mask_and_coherent(ptdev->base.dev, DMA_BIT_MASK(pa_bits));
diff --git a/drivers/gpu/drm/panthor/panthor_gpu.h 
b/drivers/gpu/drm/panthor/panthor_gpu.h
index f615feb05609..935e4af31e0a 100644
--- a/drivers/gpu/drm/panthor/panthor_gpu.h
+++ b/drivers/gpu/drm/panthor/panthor_gpu.h
@@ -13,6 +13,7 @@ int panthor_gpu_init(struct panthor_device *ptdev);
 void panthor_gpu_unplug(struct panthor_device *ptdev);
 void panthor_gpu_suspend(struct panthor_device *ptdev);
 void panthor_gpu_resume(struct panthor_device *ptdev);
+void panthor_gpu_sysc_config_set(struct panthor_device *ptdev);
 
 int panthor_gpu_block_power_on(struct panthor_device *ptdev,
                               const char *blk_name,
diff --git a/drivers/gpu/drm/panthor/panthor_gpu_regs.h 
b/drivers/gpu/drm/panthor/panthor_gpu_regs.h
index 4c5b953796e4..ec4650e7ced5 100644
--- a/drivers/gpu/drm/panthor/panthor_gpu_regs.h
+++ b/drivers/gpu/drm/panthor/panthor_gpu_regs.h
@@ -116,4 +116,62 @@
 #define   GPU_COHERENCY_ACE                            1
 #define   GPU_COHERENCY_NONE                           31
 
+#define GPU_SYSC_ALLOC_COUNT                           8
+#define GPU_SYSC_ALLOC_SOURCES_PER_REG                 4
+#define GPU_SYSC_ALLOC_SOURCE_COUNT                    \
+       (GPU_SYSC_ALLOC_COUNT * GPU_SYSC_ALLOC_SOURCES_PER_REG)
+#define GPU_SYSC_ALLOC(n)                              (0x340 + (n) * 4)
+#define   GPU_SYSC_ALLOC_READ_POLICY                   GENMASK(3, 0)
+#define   GPU_SYSC_ALLOC_WRITE_POLICY                  GENMASK(7, 4)
+#define   GPU_SYSC_ALLOC_POLICY(source, read, write) \
+       ((FIELD_PREP(GPU_SYSC_ALLOC_READ_POLICY, read) | \
+         FIELD_PREP(GPU_SYSC_ALLOC_WRITE_POLICY, write)) << \
+        (((source) % GPU_SYSC_ALLOC_SOURCES_PER_REG) * BITS_PER_BYTE))
+
+#define GPU_SYSC_ALLOC_L2_ALLOC                                0
+#define GPU_SYSC_ALLOC_NEVER_ALLOC                     2
+#define GPU_SYSC_ALLOC_ALWAYS_ALLOC                    3
+#define GPU_SYSC_ALLOC_PARTIAL_ALLOC                   4
+#define GPU_SYSC_ALLOC_L2_PARTIAL_ALLOC                        5
+
+/* Internal source IDs have separate read and write meanings. */
+#define GPU_SYSC_ALLOC_R_OTHER                         0x00
+#define GPU_SYSC_ALLOC_R_CSF                           0x02
+#define GPU_SYSC_ALLOC_R_MMU                           0x04
+#define GPU_SYSC_ALLOC_R_TILER_VERT                    0x08
+#define GPU_SYSC_ALLOC_R_TILER_PTR                     0x09
+#define GPU_SYSC_ALLOC_R_TILER_INDEX                   0x0a
+#define GPU_SYSC_ALLOC_R_TILER_OTHER                   0x0b
+#define GPU_SYSC_ALLOC_R_IC                            0x10
+#define GPU_SYSC_ALLOC_R_ATTR                          0x11
+#define GPU_SYSC_ALLOC_R_SCM                           0x12
+#define GPU_SYSC_ALLOC_R_FSDC                          0x13
+#define GPU_SYSC_ALLOC_R_VL                            0x14
+#define GPU_SYSC_ALLOC_R_PLR                           0x15
+#define GPU_SYSC_ALLOC_R_RT                            0x17
+#define GPU_SYSC_ALLOC_R_TEX                           0x18
+#define GPU_SYSC_ALLOC_R_NE_A                          0x19
+#define GPU_SYSC_ALLOC_R_NE_N                          0x1a
+#define GPU_SYSC_ALLOC_R_LSC                           0x1c
+
+#define GPU_SYSC_ALLOC_W_OTHER                         0x00
+#define GPU_SYSC_ALLOC_W_CSF                           0x02
+#define GPU_SYSC_ALLOC_W_PCB                           0x07
+#define GPU_SYSC_ALLOC_W_TILER_PTR                     0x09
+#define GPU_SYSC_ALLOC_W_TILER_VERT_PLIST              0x0a
+#define GPU_SYSC_ALLOC_W_TILER_OTHER                   0x0b
+#define GPU_SYSC_ALLOC_W_L2_EVICT                      0x0c
+#define GPU_SYSC_ALLOC_W_L2_FLUSH                      0x0d
+#define GPU_SYSC_ALLOC_W_TIB_COLOR                     0x10
+#define GPU_SYSC_ALLOC_W_TIB_COLOR_AFBCH               0x11
+#define GPU_SYSC_ALLOC_W_TIB_COLOR_AFBCB               0x12
+#define GPU_SYSC_ALLOC_W_TIB_CRC                       0x13
+#define GPU_SYSC_ALLOC_W_TIB_DS                                0x14
+#define GPU_SYSC_ALLOC_W_TIB_DS_AFBCH                  0x15
+#define GPU_SYSC_ALLOC_W_TIB_DS_AFBCB                  0x16
+#define GPU_SYSC_ALLOC_W_RT                            0x17
+#define GPU_SYSC_ALLOC_W_NE_A                          0x19
+#define GPU_SYSC_ALLOC_W_NE_N                          0x1a
+#define GPU_SYSC_ALLOC_W_LSC                           0x1c
+
 #endif /* __PANTHOR_GPU_REGS_H__ */
diff --git a/drivers/gpu/drm/panthor/panthor_hw.c 
b/drivers/gpu/drm/panthor/panthor_hw.c
index ea1e222ef121..86de88163e50 100644
--- a/drivers/gpu/drm/panthor/panthor_hw.c
+++ b/drivers/gpu/drm/panthor/panthor_hw.c
@@ -59,6 +59,19 @@ static struct panthor_hw_entry panthor_hw_match[] = {
        },
 };
 
+/**
+ * panthor_hw_l2_power_on() - Restore cache policies and power on L2
+ * @ptdev: Device.
+ *
+ * Return: 0 on success, a negative error code otherwise.
+ */
+int panthor_hw_l2_power_on(struct panthor_device *ptdev)
+{
+       panthor_gpu_sysc_config_set(ptdev);
+
+       return ptdev->hw->ops.l2_power_on(ptdev);
+}
+
 static int panthor_hw_set_power_tracing(struct device *dev, void *data)
 {
        struct panthor_device *ptdev = dev_get_drvdata(dev);
diff --git a/drivers/gpu/drm/panthor/panthor_hw.h 
b/drivers/gpu/drm/panthor/panthor_hw.h
index f797663893b2..fbcd44edd429 100644
--- a/drivers/gpu/drm/panthor/panthor_hw.h
+++ b/drivers/gpu/drm/panthor/panthor_hw.h
@@ -46,10 +46,7 @@ static inline int panthor_hw_soft_reset(struct 
panthor_device *ptdev)
        return ptdev->hw->ops.soft_reset(ptdev);
 }
 
-static inline int panthor_hw_l2_power_on(struct panthor_device *ptdev)
-{
-       return ptdev->hw->ops.l2_power_on(ptdev);
-}
+int panthor_hw_l2_power_on(struct panthor_device *ptdev);
 
 static inline void panthor_hw_l2_power_off(struct panthor_device *ptdev)
 {
-- 
2.53.0

Reply via email to