2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <asm/div64.h>
25 #include "smu7_thermal.h"
26 #include "smu7_hwmgr.h"
27 #include "smu7_common.h"
29 int smu7_fan_ctrl_get_fan_speed_info(struct pp_hwmgr
*hwmgr
,
30 struct phm_fan_speed_info
*fan_speed_info
)
32 if (hwmgr
->thermal_controller
.fanInfo
.bNoFan
)
35 fan_speed_info
->supports_percent_read
= true;
36 fan_speed_info
->supports_percent_write
= true;
37 fan_speed_info
->min_percent
= 0;
38 fan_speed_info
->max_percent
= 100;
40 if (PP_CAP(PHM_PlatformCaps_FanSpeedInTableIsRPM
) &&
41 hwmgr
->thermal_controller
.fanInfo
.ucTachometerPulsesPerRevolution
) {
42 fan_speed_info
->supports_rpm_read
= true;
43 fan_speed_info
->supports_rpm_write
= true;
44 fan_speed_info
->min_rpm
= hwmgr
->thermal_controller
.fanInfo
.ulMinRPM
;
45 fan_speed_info
->max_rpm
= hwmgr
->thermal_controller
.fanInfo
.ulMaxRPM
;
47 fan_speed_info
->min_rpm
= 0;
48 fan_speed_info
->max_rpm
= 0;
54 int smu7_fan_ctrl_get_fan_speed_percent(struct pp_hwmgr
*hwmgr
,
61 if (hwmgr
->thermal_controller
.fanInfo
.bNoFan
)
64 duty100
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
65 CG_FDO_CTRL1
, FMAX_DUTY100
);
66 duty
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
67 CG_THERMAL_STATUS
, FDO_PWM_DUTY
);
73 tmp64
= (uint64_t)duty
* 100;
74 do_div(tmp64
, duty100
);
75 *speed
= (uint32_t)tmp64
;
83 int smu7_fan_ctrl_get_fan_speed_rpm(struct pp_hwmgr
*hwmgr
, uint32_t *speed
)
86 uint32_t crystal_clock_freq
;
88 if (hwmgr
->thermal_controller
.fanInfo
.bNoFan
||
89 !hwmgr
->thermal_controller
.fanInfo
.ucTachometerPulsesPerRevolution
)
92 tach_period
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
93 CG_TACH_STATUS
, TACH_PERIOD
);
98 crystal_clock_freq
= amdgpu_asic_get_xclk((struct amdgpu_device
*)hwmgr
->adev
);
100 *speed
= 60 * crystal_clock_freq
* 10000 / tach_period
;
106 * Set Fan Speed Control to static mode, so that the user can decide what speed to use.
107 * @param hwmgr the address of the powerplay hardware manager.
108 * mode the fan control mode, 0 default, 1 by percent, 5, by RPM
109 * @exception Should always succeed.
111 int smu7_fan_ctrl_set_static_mode(struct pp_hwmgr
*hwmgr
, uint32_t mode
)
113 if (hwmgr
->fan_ctrl_is_in_default_mode
) {
114 hwmgr
->fan_ctrl_default_mode
=
115 PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
116 CG_FDO_CTRL2
, FDO_PWM_MODE
);
118 PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
120 hwmgr
->fan_ctrl_is_in_default_mode
= false;
123 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
124 CG_FDO_CTRL2
, TMIN
, 0);
125 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
126 CG_FDO_CTRL2
, FDO_PWM_MODE
, mode
);
132 * Reset Fan Speed Control to default mode.
133 * @param hwmgr the address of the powerplay hardware manager.
134 * @exception Should always succeed.
136 int smu7_fan_ctrl_set_default_mode(struct pp_hwmgr
*hwmgr
)
138 if (!hwmgr
->fan_ctrl_is_in_default_mode
) {
139 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
140 CG_FDO_CTRL2
, FDO_PWM_MODE
, hwmgr
->fan_ctrl_default_mode
);
141 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
142 CG_FDO_CTRL2
, TMIN
, hwmgr
->tmin
);
143 hwmgr
->fan_ctrl_is_in_default_mode
= true;
149 int smu7_fan_ctrl_start_smc_fan_control(struct pp_hwmgr
*hwmgr
)
153 if (PP_CAP(PHM_PlatformCaps_ODFuzzyFanControlSupport
)) {
154 cgs_write_register(hwmgr
->device
, mmSMC_MSG_ARG_0
, FAN_CONTROL_FUZZY
);
155 result
= smum_send_msg_to_smc(hwmgr
, PPSMC_StartFanControl
);
157 if (PP_CAP(PHM_PlatformCaps_FanSpeedInTableIsRPM
))
158 hwmgr
->hwmgr_func
->set_max_fan_rpm_output(hwmgr
,
159 hwmgr
->thermal_controller
.
160 advanceFanControlParameters
.usMaxFanRPM
);
162 hwmgr
->hwmgr_func
->set_max_fan_pwm_output(hwmgr
,
163 hwmgr
->thermal_controller
.
164 advanceFanControlParameters
.usMaxFanPWM
);
167 cgs_write_register(hwmgr
->device
, mmSMC_MSG_ARG_0
, FAN_CONTROL_TABLE
);
168 result
= smum_send_msg_to_smc(hwmgr
, PPSMC_StartFanControl
);
171 if (!result
&& hwmgr
->thermal_controller
.
172 advanceFanControlParameters
.ucTargetTemperature
)
173 result
= smum_send_msg_to_smc_with_parameter(hwmgr
,
174 PPSMC_MSG_SetFanTemperatureTarget
,
175 hwmgr
->thermal_controller
.
176 advanceFanControlParameters
.ucTargetTemperature
);
177 hwmgr
->fan_ctrl_enabled
= true;
183 int smu7_fan_ctrl_stop_smc_fan_control(struct pp_hwmgr
*hwmgr
)
185 hwmgr
->fan_ctrl_enabled
= false;
186 return smum_send_msg_to_smc(hwmgr
, PPSMC_StopFanControl
);
190 * Set Fan Speed in percent.
191 * @param hwmgr the address of the powerplay hardware manager.
192 * @param speed is the percentage value (0% - 100%) to be set.
193 * @exception Fails is the 100% setting appears to be 0.
195 int smu7_fan_ctrl_set_fan_speed_percent(struct pp_hwmgr
*hwmgr
,
202 if (hwmgr
->thermal_controller
.fanInfo
.bNoFan
)
208 if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl
))
209 smu7_fan_ctrl_stop_smc_fan_control(hwmgr
);
211 duty100
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
212 CG_FDO_CTRL1
, FMAX_DUTY100
);
217 tmp64
= (uint64_t)speed
* duty100
;
219 duty
= (uint32_t)tmp64
;
221 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
222 CG_FDO_CTRL0
, FDO_STATIC_DUTY
, duty
);
224 return smu7_fan_ctrl_set_static_mode(hwmgr
, FDO_PWM_MODE_STATIC
);
228 * Reset Fan Speed to default.
229 * @param hwmgr the address of the powerplay hardware manager.
230 * @exception Always succeeds.
232 int smu7_fan_ctrl_reset_fan_speed_to_default(struct pp_hwmgr
*hwmgr
)
236 if (hwmgr
->thermal_controller
.fanInfo
.bNoFan
)
239 if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl
)) {
240 result
= smu7_fan_ctrl_set_static_mode(hwmgr
, FDO_PWM_MODE_STATIC
);
242 result
= smu7_fan_ctrl_start_smc_fan_control(hwmgr
);
244 result
= smu7_fan_ctrl_set_default_mode(hwmgr
);
250 * Set Fan Speed in RPM.
251 * @param hwmgr the address of the powerplay hardware manager.
252 * @param speed is the percentage value (min - max) to be set.
253 * @exception Fails is the speed not lie between min and max.
255 int smu7_fan_ctrl_set_fan_speed_rpm(struct pp_hwmgr
*hwmgr
, uint32_t speed
)
257 uint32_t tach_period
;
258 uint32_t crystal_clock_freq
;
260 if (hwmgr
->thermal_controller
.fanInfo
.bNoFan
||
261 (hwmgr
->thermal_controller
.fanInfo
.
262 ucTachometerPulsesPerRevolution
== 0) ||
263 (speed
< hwmgr
->thermal_controller
.fanInfo
.ulMinRPM
) ||
264 (speed
> hwmgr
->thermal_controller
.fanInfo
.ulMaxRPM
))
267 if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl
))
268 smu7_fan_ctrl_stop_smc_fan_control(hwmgr
);
270 crystal_clock_freq
= amdgpu_asic_get_xclk((struct amdgpu_device
*)hwmgr
->adev
);
272 tach_period
= 60 * crystal_clock_freq
* 10000 / (8 * speed
);
274 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
275 CG_TACH_STATUS
, TACH_PERIOD
, tach_period
);
277 return smu7_fan_ctrl_set_static_mode(hwmgr
, FDO_PWM_MODE_STATIC_RPM
);
281 * Reads the remote temperature from the SIslands thermal controller.
283 * @param hwmgr The address of the hardware manager.
285 int smu7_thermal_get_temperature(struct pp_hwmgr
*hwmgr
)
289 temp
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
290 CG_MULT_THERMAL_STATUS
, CTF_TEMP
);
292 /* Bit 9 means the reading is lower than the lowest usable value. */
294 temp
= SMU7_THERMAL_MAXIMUM_TEMP_READING
;
298 temp
*= PP_TEMPERATURE_UNITS_PER_CENTIGRADES
;
304 * Set the requested temperature range for high and low alert signals
306 * @param hwmgr The address of the hardware manager.
307 * @param range Temperature range to be programmed for high and low alert signals
308 * @exception PP_Result_BadInput if the input data is not valid.
310 static int smu7_thermal_set_temperature_range(struct pp_hwmgr
*hwmgr
,
311 int low_temp
, int high_temp
)
313 int low
= SMU7_THERMAL_MINIMUM_ALERT_TEMP
*
314 PP_TEMPERATURE_UNITS_PER_CENTIGRADES
;
315 int high
= SMU7_THERMAL_MAXIMUM_ALERT_TEMP
*
316 PP_TEMPERATURE_UNITS_PER_CENTIGRADES
;
320 if (high
> high_temp
)
326 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
327 CG_THERMAL_INT
, DIG_THERM_INTH
,
328 (high
/ PP_TEMPERATURE_UNITS_PER_CENTIGRADES
));
329 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
330 CG_THERMAL_INT
, DIG_THERM_INTL
,
331 (low
/ PP_TEMPERATURE_UNITS_PER_CENTIGRADES
));
332 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
333 CG_THERMAL_CTRL
, DIG_THERM_DPM
,
334 (high
/ PP_TEMPERATURE_UNITS_PER_CENTIGRADES
));
340 * Programs thermal controller one-time setting registers
342 * @param hwmgr The address of the hardware manager.
344 static int smu7_thermal_initialize(struct pp_hwmgr
*hwmgr
)
346 if (hwmgr
->thermal_controller
.fanInfo
.ucTachometerPulsesPerRevolution
)
347 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
348 CG_TACH_CTRL
, EDGE_PER_REV
,
349 hwmgr
->thermal_controller
.fanInfo
.
350 ucTachometerPulsesPerRevolution
- 1);
352 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
353 CG_FDO_CTRL2
, TACH_PWM_RESP_RATE
, 0x28);
359 * Enable thermal alerts on the RV770 thermal controller.
361 * @param hwmgr The address of the hardware manager.
363 static void smu7_thermal_enable_alert(struct pp_hwmgr
*hwmgr
)
367 alert
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
368 CG_THERMAL_INT
, THERM_INT_MASK
);
369 alert
&= ~(SMU7_THERMAL_HIGH_ALERT_MASK
| SMU7_THERMAL_LOW_ALERT_MASK
);
370 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
371 CG_THERMAL_INT
, THERM_INT_MASK
, alert
);
373 /* send message to SMU to enable internal thermal interrupts */
374 smum_send_msg_to_smc(hwmgr
, PPSMC_MSG_Thermal_Cntl_Enable
);
378 * Disable thermal alerts on the RV770 thermal controller.
379 * @param hwmgr The address of the hardware manager.
381 int smu7_thermal_disable_alert(struct pp_hwmgr
*hwmgr
)
385 alert
= PHM_READ_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
386 CG_THERMAL_INT
, THERM_INT_MASK
);
387 alert
|= (SMU7_THERMAL_HIGH_ALERT_MASK
| SMU7_THERMAL_LOW_ALERT_MASK
);
388 PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr
->device
, CGS_IND_REG__SMC
,
389 CG_THERMAL_INT
, THERM_INT_MASK
, alert
);
391 /* send message to SMU to disable internal thermal interrupts */
392 return smum_send_msg_to_smc(hwmgr
, PPSMC_MSG_Thermal_Cntl_Disable
);
396 * Uninitialize the thermal controller.
397 * Currently just disables alerts.
398 * @param hwmgr The address of the hardware manager.
400 int smu7_thermal_stop_thermal_controller(struct pp_hwmgr
*hwmgr
)
402 int result
= smu7_thermal_disable_alert(hwmgr
);
404 if (!hwmgr
->thermal_controller
.fanInfo
.bNoFan
)
405 smu7_fan_ctrl_set_default_mode(hwmgr
);
411 * Start the fan control on the SMC.
412 * @param hwmgr the address of the powerplay hardware manager.
413 * @param pInput the pointer to input data
414 * @param pOutput the pointer to output data
415 * @param pStorage the pointer to temporary storage
416 * @param Result the last failure code
417 * @return result from set temperature range routine
419 static int smu7_thermal_start_smc_fan_control(struct pp_hwmgr
*hwmgr
)
421 /* If the fantable setup has failed we could have disabled
422 * PHM_PlatformCaps_MicrocodeFanControl even after
423 * this function was included in the table.
424 * Make sure that we still think controlling the fan is OK.
426 if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl
)) {
427 smu7_fan_ctrl_start_smc_fan_control(hwmgr
);
428 smu7_fan_ctrl_set_static_mode(hwmgr
, FDO_PWM_MODE_STATIC
);
434 int smu7_start_thermal_controller(struct pp_hwmgr
*hwmgr
,
435 struct PP_TemperatureRange
*range
)
442 smu7_thermal_initialize(hwmgr
);
443 ret
= smu7_thermal_set_temperature_range(hwmgr
, range
->min
, range
->max
);
446 smu7_thermal_enable_alert(hwmgr
);
447 ret
= smum_thermal_avfs_enable(hwmgr
);
451 /* We should restrict performance levels to low before we halt the SMC.
452 * On the other hand we are still in boot state when we do this
453 * so it would be pointless.
454 * If this assumption changes we have to revisit this table.
456 smum_thermal_setup_fan_table(hwmgr
);
457 smu7_thermal_start_smc_fan_control(hwmgr
);
463 int smu7_thermal_ctrl_uninitialize_thermal_controller(struct pp_hwmgr
*hwmgr
)
465 if (!hwmgr
->thermal_controller
.fanInfo
.bNoFan
)
466 smu7_fan_ctrl_set_default_mode(hwmgr
);