drm/amdgpu/jpeg: fix race condition issue for jpeg start
authorJames Zhu <James.Zhu@amd.com>
Wed, 10 Jun 2020 16:10:20 +0000 (12:10 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Wed, 1 Jul 2020 05:59:19 +0000 (01:59 -0400)
Fix race condition issue when multiple jpeg starts are called.

Signed-off-by: James Zhu <James.Zhu@amd.com>
Acked-by: Nirmoy Das <nirmoy.das@amd.com>
Reviewed-by: Leo Liu <leo.liu@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu_jpeg.c
drivers/gpu/drm/amd/amdgpu/amdgpu_jpeg.h

index d31d65e6b0398eaf6c20121f815b8fc4a88a1226..8996cb4ed57a87f568249b0daec623a998045d30 100644 (file)
@@ -37,6 +37,8 @@ static void amdgpu_jpeg_idle_work_handler(struct work_struct *work);
 int amdgpu_jpeg_sw_init(struct amdgpu_device *adev)
 {
        INIT_DELAYED_WORK(&adev->jpeg.idle_work, amdgpu_jpeg_idle_work_handler);
+       mutex_init(&adev->jpeg.jpeg_pg_lock);
+       atomic_set(&adev->jpeg.total_submission_cnt, 0);
 
        return 0;
 }
@@ -54,6 +56,8 @@ int amdgpu_jpeg_sw_fini(struct amdgpu_device *adev)
                amdgpu_ring_fini(&adev->jpeg.inst[i].ring_dec);
        }
 
+       mutex_destroy(&adev->jpeg.jpeg_pg_lock);
+
        return 0;
 }
 
@@ -83,7 +87,7 @@ static void amdgpu_jpeg_idle_work_handler(struct work_struct *work)
                fences += amdgpu_fence_count_emitted(&adev->jpeg.inst[i].ring_dec);
        }
 
-       if (fences == 0)
+       if (!fences && !atomic_read(&adev->jpeg.total_submission_cnt))
                amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
                                                       AMD_PG_STATE_GATE);
        else
@@ -93,15 +97,19 @@ static void amdgpu_jpeg_idle_work_handler(struct work_struct *work)
 void amdgpu_jpeg_ring_begin_use(struct amdgpu_ring *ring)
 {
        struct amdgpu_device *adev = ring->adev;
-       bool set_clocks = !cancel_delayed_work_sync(&adev->jpeg.idle_work);
 
-       if (set_clocks)
-               amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
+       atomic_inc(&adev->jpeg.total_submission_cnt);
+       cancel_delayed_work_sync(&adev->jpeg.idle_work);
+
+       mutex_lock(&adev->jpeg.jpeg_pg_lock);
+       amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
                                                       AMD_PG_STATE_UNGATE);
+       mutex_unlock(&adev->jpeg.jpeg_pg_lock);
 }
 
 void amdgpu_jpeg_ring_end_use(struct amdgpu_ring *ring)
 {
+       atomic_dec(&ring->adev->jpeg.total_submission_cnt);
        schedule_delayed_work(&ring->adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT);
 }
 
index 5131a0a1bc8aa90c8c9dde5482ff466ee0ce8977..55fbff2be761b572b096d8ee9f2ae0372d3595ad 100644 (file)
@@ -46,6 +46,8 @@ struct amdgpu_jpeg {
        unsigned harvest_config;
        struct delayed_work idle_work;
        enum amd_powergating_state cur_state;
+       struct mutex jpeg_pg_lock;
+       atomic_t total_submission_cnt;
 };
 
 int amdgpu_jpeg_sw_init(struct amdgpu_device *adev);