Move PTL enums and structures to a unified location in amdgpu_psp.h.
Introduce struct amdgpu_ptl to group PTL related fields, replacing
loose fields in struct psp_context for better code organization.

Signed-off-by: Perry Yuan <[email protected]>
Suggested-by: Harish Kasiviswanathan <[email protected]>
Reviewed-by: Yifan Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 58 ++++++++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    | 18 ++++---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c    | 13 ++---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c   | 49 +++++++++---------
 5 files changed, 74 insertions(+), 66 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index aae2f850b044..827fbdb81011 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -4442,7 +4442,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,
        mutex_init(&adev->virt.vf_errors.lock);
        hash_init(adev->mn_hash);
        mutex_init(&adev->psp.mutex);
-       mutex_init(&adev->psp.ptl_mutex);
+       mutex_init(&adev->psp.ptl.ptl_mutex);
        mutex_init(&adev->notifier_lock);
        mutex_init(&adev->pm.stable_pstate_ctx_lock);
        mutex_init(&adev->benchmark_mutex);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
index f0b1157d7a21..357357523347 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
@@ -1243,6 +1243,7 @@ static int psp_ptl_invoke(struct psp_context *psp, u32 
req_code,
                uint32_t *ptl_state, uint32_t *fmt1, uint32_t *fmt2)
 {
        struct psp_gfx_cmd_resp *cmd;
+       struct amdgpu_ptl *ptl = &psp->ptl;
        int ret;
 
        cmd = acquire_psp_cmd_buf(psp);
@@ -1266,9 +1267,9 @@ static int psp_ptl_invoke(struct psp_context *psp, u32 
req_code,
                break;
        case PSP_PTL_PERF_MON_SET:
                /* Update cached state only on success */
-               psp->ptl_enabled = *ptl_state;
-               psp->ptl_fmt1    = *fmt1;
-               psp->ptl_fmt2    = *fmt2;
+               ptl->ptl_enabled = *ptl_state;
+               ptl->ptl_fmt1    = *fmt1;
+               ptl->ptl_fmt2    = *fmt2;
                break;
        }
 
@@ -1278,10 +1279,13 @@ static int psp_ptl_invoke(struct psp_context *psp, u32 
req_code,
 }
 
 int amdgpu_ptl_perf_monitor_ctrl(struct amdgpu_device *adev, u32 req_code,
-               uint32_t *ptl_state, uint32_t *fmt1, uint32_t *fmt2)
+                               uint32_t *ptl_state,
+                               enum amdgpu_ptl_fmt *fmt1,
+                               enum amdgpu_ptl_fmt *fmt2)
 {
        uint32_t ptl_fmt1, ptl_fmt2;
        struct psp_context *psp;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
 
        if (!adev || !ptl_state || !fmt1 || !fmt2)
                return -EINVAL;
@@ -1304,9 +1308,9 @@ int amdgpu_ptl_perf_monitor_ctrl(struct amdgpu_device 
*adev, u32 req_code,
         * Add check to skip if state and formats are identical to current ones
         */
        if (req_code == PSP_PTL_PERF_MON_SET &&
-                       psp->ptl_enabled == *ptl_state &&
-                       psp->ptl_fmt1 == ptl_fmt1 &&
-                       psp->ptl_fmt2 == ptl_fmt2)
+                       ptl->ptl_enabled == *ptl_state &&
+                       ptl->ptl_fmt1 == ptl_fmt1 &&
+                       ptl->ptl_fmt2 == ptl_fmt2)
                return 0;
 
        return psp_ptl_invoke(psp, req_code, ptl_state, &ptl_fmt1, &ptl_fmt2);
@@ -1345,22 +1349,23 @@ static ssize_t ptl_enable_store(struct device *dev,
        struct drm_device *ddev = dev_get_drvdata(dev);
        struct amdgpu_device *adev = drm_to_adev(ddev);
        struct psp_context *psp = &adev->psp;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
        bool enable, cur_enabled;
        uint32_t ptl_state, fmt1, fmt2;
        int ret;
 
-       mutex_lock(&psp->ptl_mutex);
+       mutex_lock(&ptl->ptl_mutex);
        if (sysfs_streq(buf, "enabled") || sysfs_streq(buf, "1")) {
                enable = true;
        } else if (sysfs_streq(buf, "disabled") || sysfs_streq(buf, "0")) {
                enable = false;
        } else {
-               mutex_unlock(&psp->ptl_mutex);
+               mutex_unlock(&ptl->ptl_mutex);
                return -EINVAL;
        }
 
-       fmt1 = psp->ptl_fmt1;
-       fmt2 = psp->ptl_fmt2;
+       fmt1 = ptl->ptl_fmt1;
+       fmt2 = ptl->ptl_fmt2;
        ptl_state = enable ? 1 : 0;
 
        cur_enabled = READ_ONCE(psp->ptl_enabled);
@@ -1372,10 +1377,10 @@ static ssize_t ptl_enable_store(struct device *dev,
        ret = amdgpu_ptl_perf_monitor_ctrl(adev, PSP_PTL_PERF_MON_SET, 
&ptl_state, &fmt1, &fmt2);
        if (ret) {
                dev_err(adev->dev, "Failed to set PTL err = %d\n", ret);
-               mutex_unlock(&psp->ptl_mutex);
+               mutex_unlock(&ptl->ptl_mutex);
                return ret;
        }
-       mutex_unlock(&psp->ptl_mutex);
+       mutex_unlock(&ptl->ptl_mutex);
 
        return count;
 }
@@ -1386,7 +1391,7 @@ static ssize_t ptl_enable_show(struct device *dev, struct 
device_attribute *attr
        struct amdgpu_device *adev = drm_to_adev(ddev);
        struct psp_context *psp = &adev->psp;
 
-       return sysfs_emit(buf, "%s\n", psp->ptl_enabled ? "enabled" : 
"disabled");
+       return sysfs_emit(buf, "%s\n", psp->ptl.ptl_enabled ? "enabled" : 
"disabled");
 }
 
 static ssize_t ptl_format_store(struct device *dev,
@@ -1398,17 +1403,18 @@ static ssize_t ptl_format_store(struct device *dev,
        struct psp_context *psp = &adev->psp;
        char fmt1_str[8], fmt2_str[8];
        enum amdgpu_ptl_fmt fmt1_enum, fmt2_enum;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
        uint32_t ptl_state, fmt1, fmt2;
        int ret;
 
        /* Only allow format update when PTL is enabled */
-       if (!psp->ptl_enabled)
+       if (!ptl->ptl_enabled)
                return -EPERM;
 
-       mutex_lock(&psp->ptl_mutex);
+       mutex_lock(&ptl->ptl_mutex);
        /* Parse input, expecting "FMT1,FMT2" */
        if (sscanf(buf, "%7[^,],%7s", fmt1_str, fmt2_str) != 2) {
-               mutex_unlock(&psp->ptl_mutex);
+               mutex_unlock(&ptl->ptl_mutex);
                return -EINVAL;
        }
 
@@ -1418,20 +1424,20 @@ static ssize_t ptl_format_store(struct device *dev,
        if (fmt1_enum >= AMDGPU_PTL_FMT_INVALID ||
                        fmt2_enum >= AMDGPU_PTL_FMT_INVALID ||
                        fmt1_enum == fmt2_enum) {
-               mutex_unlock(&psp->ptl_mutex);
+               mutex_unlock(&ptl->ptl_mutex);
                return -EINVAL;
        }
 
-       ptl_state = psp->ptl_enabled;
+       ptl_state = ptl->ptl_enabled;
        fmt1 = fmt1_enum;
        fmt2 = fmt2_enum;
        ret = amdgpu_ptl_perf_monitor_ctrl(adev, PSP_PTL_PERF_MON_SET, 
&ptl_state, &fmt1, &fmt2);
        if (ret) {
                dev_err(adev->dev, "Failed to update PTL err = %d\n", ret);
-               mutex_unlock(&psp->ptl_mutex);
+               mutex_unlock(&ptl->ptl_mutex);
                return ret;
        }
-       mutex_unlock(&psp->ptl_mutex);
+       mutex_unlock(&ptl->ptl_mutex);
 
        return count;
 }
@@ -1443,8 +1449,8 @@ static ssize_t ptl_format_show(struct device *dev, struct 
device_attribute *attr
        struct psp_context *psp = &adev->psp;
 
        return sysfs_emit(buf, "%s,%s\n",
-                       amdgpu_ptl_fmt_str[psp->ptl_fmt1],
-                       amdgpu_ptl_fmt_str[psp->ptl_fmt2]);
+                       amdgpu_ptl_fmt_str[psp->ptl.ptl_fmt1],
+                       amdgpu_ptl_fmt_str[psp->ptl.ptl_fmt2]);
 }
 
 static umode_t amdgpu_ptl_is_visible(struct kobject *kobj, struct attribute 
*attr, int idx)
@@ -1454,7 +1460,7 @@ static umode_t amdgpu_ptl_is_visible(struct kobject 
*kobj, struct attribute *att
        struct amdgpu_device *adev = drm_to_adev(ddev);
 
        /* Only show PTL sysfs files if PTL hardware is supported */
-       if (!adev->psp.ptl_hw_supported)
+       if (!adev->psp.ptl.ptl_hw_supported)
                return 0;
 
        return attr->mode;
@@ -1462,7 +1468,7 @@ static umode_t amdgpu_ptl_is_visible(struct kobject 
*kobj, struct attribute *att
 
 int amdgpu_ptl_sysfs_init(struct amdgpu_device *adev)
 {
-       if (!adev->psp.ptl_hw_supported)
+       if (!adev->psp.ptl.ptl_hw_supported)
                return 0;
 
        return sysfs_create_group(&adev->dev->kobj, &amdgpu_ptl_attr_group);
@@ -1470,7 +1476,7 @@ int amdgpu_ptl_sysfs_init(struct amdgpu_device *adev)
 
 void amdgpu_ptl_sysfs_fini(struct amdgpu_device *adev)
 {
-       if (!adev->psp.ptl_hw_supported)
+       if (!adev->psp.ptl.ptl_hw_supported)
                return;
 
        sysfs_remove_group(&adev->dev->kobj, &amdgpu_ptl_attr_group);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h
index 711f15d21940..d8591602b304 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h
@@ -382,6 +382,16 @@ struct psp_ptl_perf_req {
        uint32_t pref_format2;
 };
 
+struct amdgpu_ptl {
+       enum amdgpu_ptl_fmt             ptl_fmt1;
+       enum amdgpu_ptl_fmt             ptl_fmt2;
+       bool                            ptl_enabled;
+       bool                            ptl_hw_supported;
+       /* PTL disable reference counting */
+       atomic_t                        ptl_disable_ref;
+       struct mutex                    ptl_mutex;
+};
+
 struct psp_context {
        struct amdgpu_device            *adev;
        struct psp_ring                 km_ring;
@@ -472,13 +482,7 @@ struct psp_context {
 #if defined(CONFIG_DEBUG_FS)
        struct spirom_bo *spirom_dump_trip;
 #endif
-       enum amdgpu_ptl_fmt             ptl_fmt1;
-       enum amdgpu_ptl_fmt             ptl_fmt2;
-       bool                            ptl_enabled;
-       bool                            ptl_hw_supported;
-       /* PTL disable reference counting */
-       atomic_t                        ptl_disable_ref;
-       struct mutex                    ptl_mutex;
+       struct amdgpu_ptl               ptl;
 };
 
 struct amdgpu_psp_funcs {
diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c 
b/drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c
index 1682adb1231f..f53934edfd3b 100644
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c
@@ -2374,17 +2374,18 @@ static int gfx_v9_4_3_perf_monitor_ptl_init(struct 
amdgpu_device *adev, bool sta
 {
        uint32_t fmt1, fmt2;
        uint32_t ptl_state = state ? 1 : 0;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
        int r;
 
        if (!adev->psp.funcs)
                return -EOPNOTSUPP;
 
-       if (!adev->psp.ptl_hw_supported) {
+       if (!ptl->ptl_hw_supported) {
                fmt1 = GFX_FTYPE_I8;
                fmt2 = GFX_FTYPE_BF16;
        } else {
-               fmt1 = adev->psp.ptl_fmt1;
-               fmt2 = adev->psp.ptl_fmt2;
+               fmt1 = ptl->ptl_fmt1;
+               fmt2 = ptl->ptl_fmt2;
        }
 
        /* initialize PTL with default formats: GFX_FTYPE_I8 & GFX_FTYPE_BF16 */
@@ -2393,9 +2394,9 @@ static int gfx_v9_4_3_perf_monitor_ptl_init(struct 
amdgpu_device *adev, bool sta
        if (r)
                return r;
 
-       adev->psp.ptl_hw_supported = true;
+       ptl->ptl_hw_supported = true;
 
-       atomic_set(&adev->psp.ptl_disable_ref, 0);
+       atomic_set(&ptl->ptl_disable_ref, 0);
 
        return 0;
 }
@@ -2405,7 +2406,7 @@ static int gfx_v9_4_3_hw_fini(struct amdgpu_ip_block 
*ip_block)
        struct amdgpu_device *adev = ip_block->adev;
        int i, num_xcc;
 
-       if (adev->psp.ptl_hw_supported)
+       if (adev->psp.ptl.ptl_hw_supported)
                gfx_v9_4_3_perf_monitor_ptl_init(adev, 0);
 
        amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index bccb857c81c4..efdd19d6d6da 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1767,12 +1767,13 @@ static int kfd_ioctl_svm(struct file *filep, struct 
kfd_process *p, void *data)
 int kfd_ptl_control(struct kfd_process_device *pdd, bool enable)
 {
        struct amdgpu_device *adev = pdd->dev->adev;
-       enum amdgpu_ptl_fmt pref_format1 = adev->psp.ptl_fmt1;
-       enum amdgpu_ptl_fmt pref_format2 = adev->psp.ptl_fmt2;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
+       enum amdgpu_ptl_fmt pref_format1 = ptl->ptl_fmt1;
+       enum amdgpu_ptl_fmt pref_format2 = ptl->ptl_fmt2;
        uint32_t ptl_state = enable ? 1 : 0;
        int ret;
 
-       if (!adev->psp.ptl_hw_supported)
+       if (!ptl->ptl_hw_supported)
                return -EOPNOTSUPP;
 
        if (!pdd->dev->kfd2kgd || !pdd->dev->kfd2kgd->ptl_ctrl)
@@ -1795,22 +1796,19 @@ int kfd_ptl_control(struct kfd_process_device *pdd, 
bool enable)
 int kfd_ptl_disable_request(struct kfd_process_device *pdd,
                struct kfd_process *p)
 {
-       struct amdgpu_device *adev;
+       struct amdgpu_device *adev = pdd->dev->adev;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
        int ret = 0;
 
-       if (!pdd)
-               return -ENODEV;
-
-       adev = pdd->dev->adev;
-       mutex_lock(&adev->psp.ptl_mutex);
+       mutex_lock(&ptl->ptl_mutex);
 
        if (pdd->ptl_disable_req)
                goto out;
 
-       if (atomic_inc_return(&adev->psp.ptl_disable_ref) == 1) {
+       if (atomic_inc_return(&ptl->ptl_disable_ref) == 1) {
                ret = kfd_ptl_control(pdd, false);
                if (ret) {
-                       atomic_dec(&adev->psp.ptl_disable_ref);
+                       atomic_dec(&ptl->ptl_disable_ref);
                        dev_warn(pdd->dev->adev->dev,
                                        "failed to disable PTL\n");
                        goto out;
@@ -1819,30 +1817,27 @@ int kfd_ptl_disable_request(struct kfd_process_device 
*pdd,
        pdd->ptl_disable_req = true;
 
 out:
-       mutex_unlock(&adev->psp.ptl_mutex);
+       mutex_unlock(&ptl->ptl_mutex);
        return ret;
 }
 
 int kfd_ptl_disable_release(struct kfd_process_device *pdd,
                struct kfd_process *p)
 {
-       struct amdgpu_device *adev;
+       struct amdgpu_device *adev = pdd->dev->adev;
+       struct amdgpu_ptl *ptl = &adev->psp.ptl;
        int ret = 0;
 
-       if (!pdd)
-               return -ENODEV;
+       mutex_lock(&ptl->ptl_mutex);
 
-       adev = pdd->dev->adev;
-       mutex_lock(&adev->psp.ptl_mutex);
        if (!pdd->ptl_disable_req)
                goto out;
 
-       if (atomic_dec_return(&adev->psp.ptl_disable_ref) == 0) {
+       if (atomic_dec_return(&ptl->ptl_disable_ref) == 0) {
                ret = kfd_ptl_control(pdd, true);
                if (ret) {
-                       atomic_inc(&adev->psp.ptl_disable_ref);
-                       dev_warn(pdd->dev->adev->dev,
-                                       "failed to enable PTL\n");
+                       atomic_inc(&ptl->ptl_disable_ref);
+                       dev_warn(adev->dev, "Failed to enable PTL on release: 
%d\n", ret);
                        goto out;
                }
        }
@@ -1850,7 +1845,7 @@ int kfd_ptl_disable_release(struct kfd_process_device 
*pdd,
        pdd->ptl_disable_req = false;
 
 out:
-       mutex_unlock(&adev->psp.ptl_mutex);
+       mutex_unlock(&ptl->ptl_mutex);
        return ret;
 }
 
@@ -1864,8 +1859,8 @@ static int kfd_profiler_ptl_control(struct kfd_process *p,
        pdd = kfd_process_device_data_by_id(p, args->gpu_id);
        mutex_unlock(&p->mutex);
 
-       if (!pdd)
-               return -ENODEV;
+       if (!pdd || !pdd->dev || !pdd->dev->kfd)
+               return -EINVAL;
 
        if (args->enable == 0)
                ret = kfd_ptl_disable_request(pdd, p);
@@ -3322,6 +3317,7 @@ static inline uint32_t profile_lock_device(struct 
kfd_process *p,
        struct kfd_process_device *pdd;
        struct kfd_dev *kfd;
        int status = -EINVAL;
+       struct amdgpu_ptl *ptl;
 
        if (!p)
                return -EINVAL;
@@ -3334,6 +3330,7 @@ static inline uint32_t profile_lock_device(struct 
kfd_process *p,
                return -EINVAL;
 
        kfd = pdd->dev->kfd;
+       ptl = &pdd->dev->adev->psp.ptl;
 
        mutex_lock(&kfd->profiler_lock);
        if (op == 1) {
@@ -3341,7 +3338,7 @@ static inline uint32_t profile_lock_device(struct 
kfd_process *p,
                        kfd->profiler_process = p;
                        status = 0;
                        mutex_unlock(&kfd->profiler_lock);
-                       if (pdd->dev->adev->psp.ptl_hw_supported) {
+                       if (ptl->ptl_hw_supported) {
                                status = kfd_ptl_disable_request(pdd, p);
                                if (status != 0)
                                        dev_err(kfd_device,
@@ -3359,7 +3356,7 @@ static inline uint32_t profile_lock_device(struct 
kfd_process *p,
                status = 0;
                mutex_unlock(&kfd->profiler_lock);
 
-               if (pdd->dev->adev->psp.ptl_hw_supported) {
+               if (ptl->ptl_hw_supported) {
                        status = kfd_ptl_disable_release(pdd, p);
                        if (status)
                                dev_err(kfd_device,
-- 
2.34.1

Reply via email to