<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Reviewed-by: Alex Deucher <alexander.deucher@amd.com><br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> amd-gfx <amd-gfx-bounces@lists.freedesktop.org> on behalf of Evan Quan <evan.quan@amd.com><br>
<b>Sent:</b> Wednesday, July 24, 2019 10:39 PM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Quan, Evan <Evan.Quan@amd.com><br>
<b>Subject:</b> [PATCH] drm/amd/powerplay: minor fixes around SW SMU power and fan setting</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Add checking for possible invalid input and null pointer. And<br>
drop redundant code.<br>
<br>
Change-Id: I6ebd6acd944e821fb19af77ed1eaa8c4b1d407ce<br>
Signed-off-by: Evan Quan <evan.quan@amd.com><br>
---<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c    | 22 ++++++++++-----------<br>
 drivers/gpu/drm/amd/powerplay/smu_v11_0.c | 24 +++++++++++------------<br>
 2 files changed, 21 insertions(+), 25 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c<br>
index bdf537d3f459..9aa00d67e61d 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c<br>
@@ -1667,20 +1667,16 @@ static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,<br>
              (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))<br>
                 return -EINVAL;<br>
 <br>
-       if (is_support_sw_smu(adev)) {<br>
-               err = kstrtoint(buf, 10, &value);<br>
-               if (err)<br>
-                       return err;<br>
+       err = kstrtoint(buf, 10, &value);<br>
+       if (err)<br>
+               return err;<br>
 <br>
+       if (is_support_sw_smu(adev)) {<br>
                 smu_set_fan_control_mode(&adev->smu, value);<br>
         } else {<br>
                 if (!adev->powerplay.pp_funcs->set_fan_control_mode)<br>
                         return -EINVAL;<br>
 <br>
-               err = kstrtoint(buf, 10, &value);<br>
-               if (err)<br>
-                       return err;<br>
-<br>
                 amdgpu_dpm_set_fan_control_mode(adev, value);<br>
         }<br>
 <br>
@@ -2100,16 +2096,18 @@ static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,<br>
                 return err;<br>
 <br>
         value = value / 1000000; /* convert to Watt */<br>
+<br>
         if (is_support_sw_smu(adev)) {<br>
-               adev->smu.funcs->set_power_limit(&adev->smu, value);<br>
+               err = smu_set_power_limit(&adev->smu, value);<br>
         } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {<br>
                 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);<br>
-               if (err)<br>
-                       return err;<br>
         } else {<br>
-               return -EINVAL;<br>
+               err = -EINVAL;<br>
         }<br>
 <br>
+       if (err)<br>
+               return err;<br>
+<br>
         return count;<br>
 }<br>
 <br>
diff --git a/drivers/gpu/drm/amd/powerplay/smu_v11_0.c b/drivers/gpu/drm/amd/powerplay/smu_v11_0.c<br>
index d1486c3e1357..8ac9acabebf8 100644<br>
--- a/drivers/gpu/drm/amd/powerplay/smu_v11_0.c<br>
+++ b/drivers/gpu/drm/amd/powerplay/smu_v11_0.c<br>
@@ -1092,6 +1092,8 @@ static int smu_v11_0_set_power_limit(struct smu_context *smu, uint32_t n)<br>
                 max_power_limit *= (100 + smu->smu_table.TDPODLimit);<br>
                 max_power_limit /= 100;<br>
         }<br>
+       if (n > max_power_limit)<br>
+               return -EINVAL;<br>
 <br>
         if (smu_feature_is_enabled(smu, SMU_FEATURE_PPT_BIT))<br>
                 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetPptLimit, n);<br>
@@ -1407,17 +1409,17 @@ smu_v11_0_get_fan_control_mode(struct smu_context *smu)<br>
 }<br>
 <br>
 static int<br>
-smu_v11_0_smc_fan_control(struct smu_context *smu, bool start)<br>
+smu_v11_0_auto_fan_control(struct smu_context *smu, bool auto_fan_control)<br>
 {<br>
         int ret = 0;<br>
 <br>
         if (smu_feature_is_supported(smu, SMU_FEATURE_FAN_CONTROL_BIT))<br>
                 return 0;<br>
 <br>
-       ret = smu_feature_set_enabled(smu, SMU_FEATURE_FAN_CONTROL_BIT, start);<br>
+       ret = smu_feature_set_enabled(smu, SMU_FEATURE_FAN_CONTROL_BIT, auto_fan_control);<br>
         if (ret)<br>
                 pr_err("[%s]%s smc FAN CONTROL feature failed!",<br>
-                      __func__, (start ? "Start" : "Stop"));<br>
+                      __func__, (auto_fan_control ? "Start" : "Stop"));<br>
 <br>
         return ret;<br>
 }<br>
@@ -1441,16 +1443,15 @@ static int<br>
 smu_v11_0_set_fan_speed_percent(struct smu_context *smu, uint32_t speed)<br>
 {<br>
         struct amdgpu_device *adev = smu->adev;<br>
-       uint32_t duty100;<br>
-       uint32_t duty;<br>
+       uint32_t duty100, duty;<br>
         uint64_t tmp64;<br>
-       bool stop = 0;<br>
 <br>
         if (speed > 100)<br>
                 speed = 100;<br>
 <br>
-       if (smu_v11_0_smc_fan_control(smu, stop))<br>
+       if (smu_v11_0_auto_fan_control(smu, 0))<br>
                 return -EINVAL;<br>
+<br>
         duty100 = REG_GET_FIELD(RREG32_SOC15(THM, 0, mmCG_FDO_CTRL1),<br>
                                 CG_FDO_CTRL1, FMAX_DUTY100);<br>
         if (!duty100)<br>
@@ -1472,18 +1473,16 @@ smu_v11_0_set_fan_control_mode(struct smu_context *smu,<br>
                                uint32_t mode)<br>
 {<br>
         int ret = 0;<br>
-       bool start = 1;<br>
-       bool stop  = 0;<br>
 <br>
         switch (mode) {<br>
         case AMD_FAN_CTRL_NONE:<br>
                 ret = smu_v11_0_set_fan_speed_percent(smu, 100);<br>
                 break;<br>
         case AMD_FAN_CTRL_MANUAL:<br>
-               ret = smu_v11_0_smc_fan_control(smu, stop);<br>
+               ret = smu_v11_0_auto_fan_control(smu, 0);<br>
                 break;<br>
         case AMD_FAN_CTRL_AUTO:<br>
-               ret = smu_v11_0_smc_fan_control(smu, start);<br>
+               ret = smu_v11_0_auto_fan_control(smu, 1);<br>
                 break;<br>
         default:<br>
                 break;<br>
@@ -1503,13 +1502,12 @@ static int smu_v11_0_set_fan_speed_rpm(struct smu_context *smu,<br>
         struct amdgpu_device *adev = smu->adev;<br>
         int ret;<br>
         uint32_t tach_period, crystal_clock_freq;<br>
-       bool stop = 0;<br>
 <br>
         if (!speed)<br>
                 return -EINVAL;<br>
 <br>
         mutex_lock(&(smu->mutex));<br>
-       ret = smu_v11_0_smc_fan_control(smu, stop);<br>
+       ret = smu_v11_0_auto_fan_control(smu, 0);<br>
         if (ret)<br>
                 goto set_fan_speed_rpm_failed;<br>
 <br>
-- <br>
2.22.0<br>
<br>
_______________________________________________<br>
amd-gfx mailing list<br>
amd-gfx@lists.freedesktop.org<br>
<a href="https://lists.freedesktop.org/mailman/listinfo/amd-gfx">https://lists.freedesktop.org/mailman/listinfo/amd-gfx</a></div>
</span></font></div>
</body>
</html>