SRAM contents persist between jobs including different users. Clear the
SRAM before programming the SRAM region to prevent one client observing
another client's data.

Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: [email protected]
Signed-off-by: Rob Herring (Arm) <[email protected]>
---
v4:
 - New patch
---
 drivers/accel/ethosu/ethosu_device.h |  2 ++
 drivers/accel/ethosu/ethosu_job.c    | 47 +++++++++++++++++++++++++++++++-----
 2 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/drivers/accel/ethosu/ethosu_device.h 
b/drivers/accel/ethosu/ethosu_device.h
index 1a90d0ebe5d9..af1489fe08c4 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -205,6 +205,8 @@ struct ethosu_device {
        spinlock_t fence_lock;
 
        struct drm_gpu_scheduler sched;
+       /* Scheduler entity that last used SRAM. */
+       struct drm_sched_entity *sram_entity;
        /* For ethosu_job_do_push() */
        struct mutex sched_lock;
        struct list_head sched_entities;
diff --git a/drivers/accel/ethosu/ethosu_job.c 
b/drivers/accel/ethosu/ethosu_job.c
index ef95a8abd61e..3976115902e8 100644
--- a/drivers/accel/ethosu/ethosu_job.c
+++ b/drivers/accel/ethosu/ethosu_job.c
@@ -45,11 +45,27 @@ static const struct dma_fence_ops ethosu_fence_ops = {
        .get_timeline_name = ethosu_fence_get_timeline_name,
 };
 
-static void ethosu_job_set_in_flight(struct ethosu_device *dev,
-                                    struct ethosu_job *job)
+static u32 ethosu_job_sram_size(struct ethosu_job *job)
 {
+       struct ethosu_validated_cmdstream_info *cmd_info =
+               to_ethosu_bo(job->cmd_bo)->info;
+
+       return cmd_info->region_size[ETHOSU_SRAM_REGION];
+}
+
+static bool ethosu_job_set_in_flight(struct ethosu_device *dev,
+                                    struct ethosu_job *job, u32 sram_size)
+{
+       bool clear_sram = false;
+
        guard(spinlock)(&dev->job_lock);
+       if (sram_size && dev->sram_entity != job->base.entity) {
+               dev->sram_entity = job->base.entity;
+               clear_sram = true;
+       }
        dev->in_flight_job = job;
+
+       return clear_sram;
 }
 
 static struct ethosu_job *
@@ -64,7 +80,8 @@ ethosu_job_clear_in_flight(struct ethosu_device *dev)
        return job;
 }
 
-static void ethosu_job_hw_submit(struct ethosu_device *dev, struct ethosu_job 
*job)
+static void ethosu_job_hw_submit(struct ethosu_device *dev, struct ethosu_job 
*job,
+                                u32 sram_size, bool clear_sram)
 {
        struct drm_gem_dma_object *cmd_bo = to_drm_gem_dma_obj(job->cmd_bo);
        struct ethosu_validated_cmdstream_info *cmd_info = 
to_ethosu_bo(job->cmd_bo)->info;
@@ -79,7 +96,12 @@ static void ethosu_job_hw_submit(struct ethosu_device *dev, 
struct ethosu_job *j
                dev_dbg(dev->base.dev, "Region %d base addr = %pad\n", region, 
&bo->dma_addr);
        }
 
-       if (job->sram_size) {
+       if (sram_size) {
+               if (clear_sram) {
+                       memset_io(dev->sram, 0, dev->npu_info.sram_size);
+                       /* Order the SRAM clear before starting the NPU. */
+                       wmb();
+               }
                writel_relaxed(lower_32_bits(dev->sramphys),
                               dev->regs + NPU_REG_BASEP(ETHOSU_SRAM_REGION));
                writel_relaxed(upper_32_bits(dev->sramphys),
@@ -263,6 +285,8 @@ static struct dma_fence *ethosu_job_run(struct 
drm_sched_job *sched_job)
        struct ethosu_job *job = to_ethosu_job(sched_job);
        struct ethosu_device *dev = job->dev;
        struct dma_fence *fence = job->done_fence;
+       u32 sram_size;
+       bool clear_sram;
 
        if (unlikely(job->base.s_fence->finished.error))
                return NULL;
@@ -273,8 +297,9 @@ static struct dma_fence *ethosu_job_run(struct 
drm_sched_job *sched_job)
 
        ethosu_switch_perfmon(dev, job);
 
-       ethosu_job_set_in_flight(dev, job);
-       ethosu_job_hw_submit(dev, job);
+       sram_size = ethosu_job_sram_size(job);
+       clear_sram = ethosu_job_set_in_flight(dev, job, sram_size);
+       ethosu_job_hw_submit(dev, job, sram_size, clear_sram);
 
        return fence;
 }
@@ -482,12 +507,22 @@ int ethosu_job_open(struct ethosu_file_priv *ethosu_priv)
        return ret;
 }
 
+static void ethosu_job_clear_sram_entity(struct ethosu_device *dev,
+                                        struct drm_sched_entity *entity)
+{
+       guard(spinlock)(&dev->job_lock);
+       if (dev->sram_entity == entity) {
+               dev->sram_entity = NULL;
+       }
+}
+
 void ethosu_job_close(struct ethosu_file_priv *ethosu_priv)
 {
        struct ethosu_device *dev = ethosu_priv->edev;
        struct drm_sched_entity *entity = &ethosu_priv->sched_entity;
 
        scoped_guard(mutex, &dev->sched_lock) {
+               ethosu_job_clear_sram_entity(dev, entity);
                drm_sched_entity_kill(entity);
                list_del(&ethosu_priv->sched_entity_list);
        }

-- 
2.53.0

Reply via email to