<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<p style="font-family:Arial;font-size:10pt;color:#0000FF;margin:15pt;" align="Left">
[AMD Official Use Only]<br>
</p>
<br>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Reviewed-by: Kevin Wang <kevin1.wang@amd.com></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Best Regards,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Kevin</div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Powell, Darren <Darren.Powell@amd.com><br>
<b>Sent:</b> Wednesday, June 23, 2021 10:34 AM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Powell, Darren <Darren.Powell@amd.com><br>
<b>Subject:</b> [PATCH 1/1] amdgpu/pm: remove code duplication in show_power_cap calls</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="PlainText">v2: reorder to check pointers before calling pm_runtime_* functions<br>
<br>
 created generic function and call with enum from<br>
 * amdgpu_hwmon_show_power_cap_max<br>
 * amdgpu_hwmon_show_power_cap<br>
 * amdgpu_hwmon_show_power_cap_default<br>
<br>
=== Test ===<br>
AMDGPU_PCI_ADDR=`lspci -nn | grep "VGA\|Display" | cut -d " " -f 1`<br>
AMDGPU_HWMON=`ls -la /sys/class/hwmon | grep $AMDGPU_PCI_ADDR | cut -d " " -f 10`<br>
HWMON_DIR=/sys/class/hwmon/${AMDGPU_HWMON}<br>
<br>
cp pp_show_power_cap.txt{,.old}<br>
lspci -nn | grep "VGA\|Display" > pp_show_power_cap.test.log<br>
FILES="<br>
power1_cap<br>
power1_cap_max<br>
power1_cap_default "<br>
<br>
for f in $FILES<br>
do<br>
  echo  $f = `cat $HWMON_DIR/$f` >> pp_show_power_cap.test.log<br>
done<br>
<br>
Signed-off-by: Darren Powell <darren.powell@amd.com><br>
---<br>
 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 96 ++++++------------------------<br>
 1 file changed, 19 insertions(+), 77 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/amd/pm/amdgpu_pm.c b/drivers/gpu/drm/amd/pm/amdgpu_pm.c<br>
index b2335a1d3f98..fa63160e6405 100644<br>
--- a/drivers/gpu/drm/amd/pm/amdgpu_pm.c<br>
+++ b/drivers/gpu/drm/amd/pm/amdgpu_pm.c<br>
@@ -2901,14 +2901,14 @@ static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,<br>
         return sprintf(buf, "%i\n", 0);<br>
 }<br>
 <br>
-static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,<br>
-                                        struct device_attribute *attr,<br>
-                                        char *buf)<br>
+static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,<br>
+                                  struct device_attribute *attr,<br>
+                                  char *buf,<br>
+                                  enum pp_power_limit_level pp_limit_level)<br>
 {<br>
         struct amdgpu_device *adev = dev_get_drvdata(dev);<br>
         const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;<br>
         enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;<br>
-       enum pp_power_limit_level pp_limit_level = PP_PWR_LIMIT_MAX;<br>
         uint32_t limit;<br>
         ssize_t size;<br>
         int r;<br>
@@ -2918,17 +2918,17 @@ static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,<br>
         if (adev->in_suspend && !adev->in_runpm)<br>
                 return -EPERM;<br>
 <br>
+       if ( !(pp_funcs && pp_funcs->get_power_limit))<br>
+               return -ENODATA;<br>
+<br>
         r = pm_runtime_get_sync(adev_to_drm(adev)->dev);<br>
         if (r < 0) {<br>
                 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);<br>
                 return r;<br>
         }<br>
 <br>
-       if (pp_funcs && pp_funcs->get_power_limit)<br>
-               r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,<br>
-                                             pp_limit_level, power_type);<br>
-       else<br>
-               r = -ENODATA;<br>
+       r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,<br>
+                                     pp_limit_level, power_type);<br>
 <br>
         if (!r)<br>
                 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);<br>
@@ -2941,85 +2941,27 @@ static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,<br>
         return size;<br>
 }<br>
 <br>
-static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,<br>
+static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,<br>
                                          struct device_attribute *attr,<br>
                                          char *buf)<br>
 {<br>
-       struct amdgpu_device *adev = dev_get_drvdata(dev);<br>
-       const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;<br>
-       enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;<br>
-       enum pp_power_limit_level pp_limit_level = PP_PWR_LIMIT_CURRENT;<br>
-       uint32_t limit;<br>
-       ssize_t size;<br>
-       int r;<br>
-<br>
-       if (amdgpu_in_reset(adev))<br>
-               return -EPERM;<br>
-       if (adev->in_suspend && !adev->in_runpm)<br>
-               return -EPERM;<br>
-<br>
-       r = pm_runtime_get_sync(adev_to_drm(adev)->dev);<br>
-       if (r < 0) {<br>
-               pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);<br>
-               return r;<br>
-       }<br>
-<br>
-       if (pp_funcs && pp_funcs->get_power_limit)<br>
-               r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,<br>
-                                             pp_limit_level, power_type);<br>
-       else<br>
-               r = -ENODATA;<br>
-<br>
-       if (!r)<br>
-               size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);<br>
-       else<br>
-               size = snprintf(buf, PAGE_SIZE, "\n");<br>
-<br>
-       pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);<br>
-       pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);<br>
+       return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);<br>
+}<br>
 <br>
-       return size;<br>
+static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,<br>
+                                        struct device_attribute *attr,<br>
+                                        char *buf)<br>
+{<br>
+       return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);<br>
 }<br>
 <br>
 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,<br>
                                          struct device_attribute *attr,<br>
                                          char *buf)<br>
 {<br>
-       struct amdgpu_device *adev = dev_get_drvdata(dev);<br>
-       const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;<br>
-       enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;<br>
-       enum pp_power_limit_level pp_limit_level = PP_PWR_LIMIT_DEFAULT;<br>
-       uint32_t limit;<br>
-       ssize_t size;<br>
-       int r;<br>
-<br>
-       if (amdgpu_in_reset(adev))<br>
-               return -EPERM;<br>
-       if (adev->in_suspend && !adev->in_runpm)<br>
-               return -EPERM;<br>
-<br>
-       r = pm_runtime_get_sync(adev_to_drm(adev)->dev);<br>
-       if (r < 0) {<br>
-               pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);<br>
-               return r;<br>
-       }<br>
-<br>
-       if (pp_funcs && pp_funcs->get_power_limit)<br>
-               r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,<br>
-                                             pp_limit_level, power_type);<br>
-       else<br>
-               r = -ENODATA;<br>
-<br>
-       if (!r)<br>
-               size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);<br>
-       else<br>
-               size = snprintf(buf, PAGE_SIZE, "\n");<br>
-<br>
-       pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);<br>
-       pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);<br>
-<br>
-       return size;<br>
+       return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);<br>
 }<br>
+<br>
 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,<br>
                                          struct device_attribute *attr,<br>
                                          char *buf)<br>
<br>
base-commit: 1971ad5abcd8575efd12d2e6639fd8f58210a8d8<br>
-- <br>
2.31.1<br>
<br>
</div>
</span></font></div>
</div>
</body>
</html>