1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  */
23*b843c749SSergey Zigachev 
24*b843c749SSergey Zigachev #include <asm/div64.h>
25*b843c749SSergey Zigachev #include "smu7_thermal.h"
26*b843c749SSergey Zigachev #include "smu7_hwmgr.h"
27*b843c749SSergey Zigachev #include "smu7_common.h"
28*b843c749SSergey Zigachev 
smu7_fan_ctrl_get_fan_speed_info(struct pp_hwmgr * hwmgr,struct phm_fan_speed_info * fan_speed_info)29*b843c749SSergey Zigachev int smu7_fan_ctrl_get_fan_speed_info(struct pp_hwmgr *hwmgr,
30*b843c749SSergey Zigachev 		struct phm_fan_speed_info *fan_speed_info)
31*b843c749SSergey Zigachev {
32*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan)
33*b843c749SSergey Zigachev 		return -ENODEV;
34*b843c749SSergey Zigachev 
35*b843c749SSergey Zigachev 	fan_speed_info->supports_percent_read = true;
36*b843c749SSergey Zigachev 	fan_speed_info->supports_percent_write = true;
37*b843c749SSergey Zigachev 	fan_speed_info->min_percent = 0;
38*b843c749SSergey Zigachev 	fan_speed_info->max_percent = 100;
39*b843c749SSergey Zigachev 
40*b843c749SSergey Zigachev 	if (PP_CAP(PHM_PlatformCaps_FanSpeedInTableIsRPM) &&
41*b843c749SSergey Zigachev 	    hwmgr->thermal_controller.fanInfo.ucTachometerPulsesPerRevolution) {
42*b843c749SSergey Zigachev 		fan_speed_info->supports_rpm_read = true;
43*b843c749SSergey Zigachev 		fan_speed_info->supports_rpm_write = true;
44*b843c749SSergey Zigachev 		fan_speed_info->min_rpm = hwmgr->thermal_controller.fanInfo.ulMinRPM;
45*b843c749SSergey Zigachev 		fan_speed_info->max_rpm = hwmgr->thermal_controller.fanInfo.ulMaxRPM;
46*b843c749SSergey Zigachev 	} else {
47*b843c749SSergey Zigachev 		fan_speed_info->min_rpm = 0;
48*b843c749SSergey Zigachev 		fan_speed_info->max_rpm = 0;
49*b843c749SSergey Zigachev 	}
50*b843c749SSergey Zigachev 
51*b843c749SSergey Zigachev 	return 0;
52*b843c749SSergey Zigachev }
53*b843c749SSergey Zigachev 
smu7_fan_ctrl_get_fan_speed_percent(struct pp_hwmgr * hwmgr,uint32_t * speed)54*b843c749SSergey Zigachev int smu7_fan_ctrl_get_fan_speed_percent(struct pp_hwmgr *hwmgr,
55*b843c749SSergey Zigachev 		uint32_t *speed)
56*b843c749SSergey Zigachev {
57*b843c749SSergey Zigachev 	uint32_t duty100;
58*b843c749SSergey Zigachev 	uint32_t duty;
59*b843c749SSergey Zigachev 	uint64_t tmp64;
60*b843c749SSergey Zigachev 
61*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan)
62*b843c749SSergey Zigachev 		return -ENODEV;
63*b843c749SSergey Zigachev 
64*b843c749SSergey Zigachev 	duty100 = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
65*b843c749SSergey Zigachev 			CG_FDO_CTRL1, FMAX_DUTY100);
66*b843c749SSergey Zigachev 	duty = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
67*b843c749SSergey Zigachev 			CG_THERMAL_STATUS, FDO_PWM_DUTY);
68*b843c749SSergey Zigachev 
69*b843c749SSergey Zigachev 	if (duty100 == 0)
70*b843c749SSergey Zigachev 		return -EINVAL;
71*b843c749SSergey Zigachev 
72*b843c749SSergey Zigachev 
73*b843c749SSergey Zigachev 	tmp64 = (uint64_t)duty * 100;
74*b843c749SSergey Zigachev 	do_div(tmp64, duty100);
75*b843c749SSergey Zigachev 	*speed = (uint32_t)tmp64;
76*b843c749SSergey Zigachev 
77*b843c749SSergey Zigachev 	if (*speed > 100)
78*b843c749SSergey Zigachev 		*speed = 100;
79*b843c749SSergey Zigachev 
80*b843c749SSergey Zigachev 	return 0;
81*b843c749SSergey Zigachev }
82*b843c749SSergey Zigachev 
smu7_fan_ctrl_get_fan_speed_rpm(struct pp_hwmgr * hwmgr,uint32_t * speed)83*b843c749SSergey Zigachev int smu7_fan_ctrl_get_fan_speed_rpm(struct pp_hwmgr *hwmgr, uint32_t *speed)
84*b843c749SSergey Zigachev {
85*b843c749SSergey Zigachev 	uint32_t tach_period;
86*b843c749SSergey Zigachev 	uint32_t crystal_clock_freq;
87*b843c749SSergey Zigachev 
88*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan ||
89*b843c749SSergey Zigachev 	    !hwmgr->thermal_controller.fanInfo.ucTachometerPulsesPerRevolution)
90*b843c749SSergey Zigachev 		return -ENODEV;
91*b843c749SSergey Zigachev 
92*b843c749SSergey Zigachev 	tach_period = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
93*b843c749SSergey Zigachev 			CG_TACH_STATUS, TACH_PERIOD);
94*b843c749SSergey Zigachev 
95*b843c749SSergey Zigachev 	if (tach_period == 0)
96*b843c749SSergey Zigachev 		return -EINVAL;
97*b843c749SSergey Zigachev 
98*b843c749SSergey Zigachev 	crystal_clock_freq = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
99*b843c749SSergey Zigachev 
100*b843c749SSergey Zigachev 	*speed = 60 * crystal_clock_freq * 10000 / tach_period;
101*b843c749SSergey Zigachev 
102*b843c749SSergey Zigachev 	return 0;
103*b843c749SSergey Zigachev }
104*b843c749SSergey Zigachev 
105*b843c749SSergey Zigachev /**
106*b843c749SSergey Zigachev * Set Fan Speed Control to static mode, so that the user can decide what speed to use.
107*b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
108*b843c749SSergey Zigachev *           mode    the fan control mode, 0 default, 1 by percent, 5, by RPM
109*b843c749SSergey Zigachev * @exception Should always succeed.
110*b843c749SSergey Zigachev */
smu7_fan_ctrl_set_static_mode(struct pp_hwmgr * hwmgr,uint32_t mode)111*b843c749SSergey Zigachev int smu7_fan_ctrl_set_static_mode(struct pp_hwmgr *hwmgr, uint32_t mode)
112*b843c749SSergey Zigachev {
113*b843c749SSergey Zigachev 	if (hwmgr->fan_ctrl_is_in_default_mode) {
114*b843c749SSergey Zigachev 		hwmgr->fan_ctrl_default_mode =
115*b843c749SSergey Zigachev 				PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
116*b843c749SSergey Zigachev 						CG_FDO_CTRL2, FDO_PWM_MODE);
117*b843c749SSergey Zigachev 		hwmgr->tmin =
118*b843c749SSergey Zigachev 				PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
119*b843c749SSergey Zigachev 						CG_FDO_CTRL2, TMIN);
120*b843c749SSergey Zigachev 		hwmgr->fan_ctrl_is_in_default_mode = false;
121*b843c749SSergey Zigachev 	}
122*b843c749SSergey Zigachev 
123*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
124*b843c749SSergey Zigachev 			CG_FDO_CTRL2, TMIN, 0);
125*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
126*b843c749SSergey Zigachev 			CG_FDO_CTRL2, FDO_PWM_MODE, mode);
127*b843c749SSergey Zigachev 
128*b843c749SSergey Zigachev 	return 0;
129*b843c749SSergey Zigachev }
130*b843c749SSergey Zigachev 
131*b843c749SSergey Zigachev /**
132*b843c749SSergey Zigachev * Reset Fan Speed Control to default mode.
133*b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
134*b843c749SSergey Zigachev * @exception Should always succeed.
135*b843c749SSergey Zigachev */
smu7_fan_ctrl_set_default_mode(struct pp_hwmgr * hwmgr)136*b843c749SSergey Zigachev int smu7_fan_ctrl_set_default_mode(struct pp_hwmgr *hwmgr)
137*b843c749SSergey Zigachev {
138*b843c749SSergey Zigachev 	if (!hwmgr->fan_ctrl_is_in_default_mode) {
139*b843c749SSergey Zigachev 		PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
140*b843c749SSergey Zigachev 				CG_FDO_CTRL2, FDO_PWM_MODE, hwmgr->fan_ctrl_default_mode);
141*b843c749SSergey Zigachev 		PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
142*b843c749SSergey Zigachev 				CG_FDO_CTRL2, TMIN, hwmgr->tmin);
143*b843c749SSergey Zigachev 		hwmgr->fan_ctrl_is_in_default_mode = true;
144*b843c749SSergey Zigachev 	}
145*b843c749SSergey Zigachev 
146*b843c749SSergey Zigachev 	return 0;
147*b843c749SSergey Zigachev }
148*b843c749SSergey Zigachev 
smu7_fan_ctrl_start_smc_fan_control(struct pp_hwmgr * hwmgr)149*b843c749SSergey Zigachev int smu7_fan_ctrl_start_smc_fan_control(struct pp_hwmgr *hwmgr)
150*b843c749SSergey Zigachev {
151*b843c749SSergey Zigachev 	int result;
152*b843c749SSergey Zigachev 
153*b843c749SSergey Zigachev 	if (PP_CAP(PHM_PlatformCaps_ODFuzzyFanControlSupport)) {
154*b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmSMC_MSG_ARG_0, FAN_CONTROL_FUZZY);
155*b843c749SSergey Zigachev 		result = smum_send_msg_to_smc(hwmgr, PPSMC_StartFanControl);
156*b843c749SSergey Zigachev 
157*b843c749SSergey Zigachev 		if (PP_CAP(PHM_PlatformCaps_FanSpeedInTableIsRPM))
158*b843c749SSergey Zigachev 			hwmgr->hwmgr_func->set_max_fan_rpm_output(hwmgr,
159*b843c749SSergey Zigachev 					hwmgr->thermal_controller.
160*b843c749SSergey Zigachev 					advanceFanControlParameters.usMaxFanRPM);
161*b843c749SSergey Zigachev 		else
162*b843c749SSergey Zigachev 			hwmgr->hwmgr_func->set_max_fan_pwm_output(hwmgr,
163*b843c749SSergey Zigachev 					hwmgr->thermal_controller.
164*b843c749SSergey Zigachev 					advanceFanControlParameters.usMaxFanPWM);
165*b843c749SSergey Zigachev 
166*b843c749SSergey Zigachev 	} else {
167*b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmSMC_MSG_ARG_0, FAN_CONTROL_TABLE);
168*b843c749SSergey Zigachev 		result = smum_send_msg_to_smc(hwmgr, PPSMC_StartFanControl);
169*b843c749SSergey Zigachev 	}
170*b843c749SSergey Zigachev 
171*b843c749SSergey Zigachev 	if (!result && hwmgr->thermal_controller.
172*b843c749SSergey Zigachev 			advanceFanControlParameters.ucTargetTemperature)
173*b843c749SSergey Zigachev 		result = smum_send_msg_to_smc_with_parameter(hwmgr,
174*b843c749SSergey Zigachev 				PPSMC_MSG_SetFanTemperatureTarget,
175*b843c749SSergey Zigachev 				hwmgr->thermal_controller.
176*b843c749SSergey Zigachev 				advanceFanControlParameters.ucTargetTemperature);
177*b843c749SSergey Zigachev 	hwmgr->fan_ctrl_enabled = true;
178*b843c749SSergey Zigachev 
179*b843c749SSergey Zigachev 	return result;
180*b843c749SSergey Zigachev }
181*b843c749SSergey Zigachev 
182*b843c749SSergey Zigachev 
smu7_fan_ctrl_stop_smc_fan_control(struct pp_hwmgr * hwmgr)183*b843c749SSergey Zigachev int smu7_fan_ctrl_stop_smc_fan_control(struct pp_hwmgr *hwmgr)
184*b843c749SSergey Zigachev {
185*b843c749SSergey Zigachev 	hwmgr->fan_ctrl_enabled = false;
186*b843c749SSergey Zigachev 	return smum_send_msg_to_smc(hwmgr, PPSMC_StopFanControl);
187*b843c749SSergey Zigachev }
188*b843c749SSergey Zigachev 
189*b843c749SSergey Zigachev /**
190*b843c749SSergey Zigachev * Set Fan Speed in percent.
191*b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
192*b843c749SSergey Zigachev * @param    speed is the percentage value (0% - 100%) to be set.
193*b843c749SSergey Zigachev * @exception Fails is the 100% setting appears to be 0.
194*b843c749SSergey Zigachev */
smu7_fan_ctrl_set_fan_speed_percent(struct pp_hwmgr * hwmgr,uint32_t speed)195*b843c749SSergey Zigachev int smu7_fan_ctrl_set_fan_speed_percent(struct pp_hwmgr *hwmgr,
196*b843c749SSergey Zigachev 		uint32_t speed)
197*b843c749SSergey Zigachev {
198*b843c749SSergey Zigachev 	uint32_t duty100;
199*b843c749SSergey Zigachev 	uint32_t duty;
200*b843c749SSergey Zigachev 	uint64_t tmp64;
201*b843c749SSergey Zigachev 
202*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan)
203*b843c749SSergey Zigachev 		return 0;
204*b843c749SSergey Zigachev 
205*b843c749SSergey Zigachev 	if (speed > 100)
206*b843c749SSergey Zigachev 		speed = 100;
207*b843c749SSergey Zigachev 
208*b843c749SSergey Zigachev 	if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl))
209*b843c749SSergey Zigachev 		smu7_fan_ctrl_stop_smc_fan_control(hwmgr);
210*b843c749SSergey Zigachev 
211*b843c749SSergey Zigachev 	duty100 = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
212*b843c749SSergey Zigachev 			CG_FDO_CTRL1, FMAX_DUTY100);
213*b843c749SSergey Zigachev 
214*b843c749SSergey Zigachev 	if (duty100 == 0)
215*b843c749SSergey Zigachev 		return -EINVAL;
216*b843c749SSergey Zigachev 
217*b843c749SSergey Zigachev 	tmp64 = (uint64_t)speed * duty100;
218*b843c749SSergey Zigachev 	do_div(tmp64, 100);
219*b843c749SSergey Zigachev 	duty = (uint32_t)tmp64;
220*b843c749SSergey Zigachev 
221*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
222*b843c749SSergey Zigachev 			CG_FDO_CTRL0, FDO_STATIC_DUTY, duty);
223*b843c749SSergey Zigachev 
224*b843c749SSergey Zigachev 	return smu7_fan_ctrl_set_static_mode(hwmgr, FDO_PWM_MODE_STATIC);
225*b843c749SSergey Zigachev }
226*b843c749SSergey Zigachev 
227*b843c749SSergey Zigachev /**
228*b843c749SSergey Zigachev * Reset Fan Speed to default.
229*b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
230*b843c749SSergey Zigachev * @exception Always succeeds.
231*b843c749SSergey Zigachev */
smu7_fan_ctrl_reset_fan_speed_to_default(struct pp_hwmgr * hwmgr)232*b843c749SSergey Zigachev int smu7_fan_ctrl_reset_fan_speed_to_default(struct pp_hwmgr *hwmgr)
233*b843c749SSergey Zigachev {
234*b843c749SSergey Zigachev 	int result;
235*b843c749SSergey Zigachev 
236*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan)
237*b843c749SSergey Zigachev 		return 0;
238*b843c749SSergey Zigachev 
239*b843c749SSergey Zigachev 	if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl)) {
240*b843c749SSergey Zigachev 		result = smu7_fan_ctrl_set_static_mode(hwmgr, FDO_PWM_MODE_STATIC);
241*b843c749SSergey Zigachev 		if (!result)
242*b843c749SSergey Zigachev 			result = smu7_fan_ctrl_start_smc_fan_control(hwmgr);
243*b843c749SSergey Zigachev 	} else
244*b843c749SSergey Zigachev 		result = smu7_fan_ctrl_set_default_mode(hwmgr);
245*b843c749SSergey Zigachev 
246*b843c749SSergey Zigachev 	return result;
247*b843c749SSergey Zigachev }
248*b843c749SSergey Zigachev 
249*b843c749SSergey Zigachev /**
250*b843c749SSergey Zigachev * Set Fan Speed in RPM.
251*b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
252*b843c749SSergey Zigachev * @param    speed is the percentage value (min - max) to be set.
253*b843c749SSergey Zigachev * @exception Fails is the speed not lie between min and max.
254*b843c749SSergey Zigachev */
smu7_fan_ctrl_set_fan_speed_rpm(struct pp_hwmgr * hwmgr,uint32_t speed)255*b843c749SSergey Zigachev int smu7_fan_ctrl_set_fan_speed_rpm(struct pp_hwmgr *hwmgr, uint32_t speed)
256*b843c749SSergey Zigachev {
257*b843c749SSergey Zigachev 	uint32_t tach_period;
258*b843c749SSergey Zigachev 	uint32_t crystal_clock_freq;
259*b843c749SSergey Zigachev 
260*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan ||
261*b843c749SSergey Zigachev 			(hwmgr->thermal_controller.fanInfo.
262*b843c749SSergey Zigachev 			ucTachometerPulsesPerRevolution == 0) ||
263*b843c749SSergey Zigachev 			(speed < hwmgr->thermal_controller.fanInfo.ulMinRPM) ||
264*b843c749SSergey Zigachev 			(speed > hwmgr->thermal_controller.fanInfo.ulMaxRPM))
265*b843c749SSergey Zigachev 		return 0;
266*b843c749SSergey Zigachev 
267*b843c749SSergey Zigachev 	if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl))
268*b843c749SSergey Zigachev 		smu7_fan_ctrl_stop_smc_fan_control(hwmgr);
269*b843c749SSergey Zigachev 
270*b843c749SSergey Zigachev 	crystal_clock_freq = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
271*b843c749SSergey Zigachev 
272*b843c749SSergey Zigachev 	tach_period = 60 * crystal_clock_freq * 10000 / (8 * speed);
273*b843c749SSergey Zigachev 
274*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
275*b843c749SSergey Zigachev 				CG_TACH_STATUS, TACH_PERIOD, tach_period);
276*b843c749SSergey Zigachev 
277*b843c749SSergey Zigachev 	return smu7_fan_ctrl_set_static_mode(hwmgr, FDO_PWM_MODE_STATIC_RPM);
278*b843c749SSergey Zigachev }
279*b843c749SSergey Zigachev 
280*b843c749SSergey Zigachev /**
281*b843c749SSergey Zigachev * Reads the remote temperature from the SIslands thermal controller.
282*b843c749SSergey Zigachev *
283*b843c749SSergey Zigachev * @param    hwmgr The address of the hardware manager.
284*b843c749SSergey Zigachev */
smu7_thermal_get_temperature(struct pp_hwmgr * hwmgr)285*b843c749SSergey Zigachev int smu7_thermal_get_temperature(struct pp_hwmgr *hwmgr)
286*b843c749SSergey Zigachev {
287*b843c749SSergey Zigachev 	int temp;
288*b843c749SSergey Zigachev 
289*b843c749SSergey Zigachev 	temp = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
290*b843c749SSergey Zigachev 			CG_MULT_THERMAL_STATUS, CTF_TEMP);
291*b843c749SSergey Zigachev 
292*b843c749SSergey Zigachev 	/* Bit 9 means the reading is lower than the lowest usable value. */
293*b843c749SSergey Zigachev 	if (temp & 0x200)
294*b843c749SSergey Zigachev 		temp = SMU7_THERMAL_MAXIMUM_TEMP_READING;
295*b843c749SSergey Zigachev 	else
296*b843c749SSergey Zigachev 		temp = temp & 0x1ff;
297*b843c749SSergey Zigachev 
298*b843c749SSergey Zigachev 	temp *= PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
299*b843c749SSergey Zigachev 
300*b843c749SSergey Zigachev 	return temp;
301*b843c749SSergey Zigachev }
302*b843c749SSergey Zigachev 
303*b843c749SSergey Zigachev /**
304*b843c749SSergey Zigachev * Set the requested temperature range for high and low alert signals
305*b843c749SSergey Zigachev *
306*b843c749SSergey Zigachev * @param    hwmgr The address of the hardware manager.
307*b843c749SSergey Zigachev * @param    range Temperature range to be programmed for high and low alert signals
308*b843c749SSergey Zigachev * @exception PP_Result_BadInput if the input data is not valid.
309*b843c749SSergey Zigachev */
smu7_thermal_set_temperature_range(struct pp_hwmgr * hwmgr,int low_temp,int high_temp)310*b843c749SSergey Zigachev static int smu7_thermal_set_temperature_range(struct pp_hwmgr *hwmgr,
311*b843c749SSergey Zigachev 		int low_temp, int high_temp)
312*b843c749SSergey Zigachev {
313*b843c749SSergey Zigachev 	int low = SMU7_THERMAL_MINIMUM_ALERT_TEMP *
314*b843c749SSergey Zigachev 			PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
315*b843c749SSergey Zigachev 	int high = SMU7_THERMAL_MAXIMUM_ALERT_TEMP *
316*b843c749SSergey Zigachev 			PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
317*b843c749SSergey Zigachev 
318*b843c749SSergey Zigachev 	if (low < low_temp)
319*b843c749SSergey Zigachev 		low = low_temp;
320*b843c749SSergey Zigachev 	if (high > high_temp)
321*b843c749SSergey Zigachev 		high = high_temp;
322*b843c749SSergey Zigachev 
323*b843c749SSergey Zigachev 	if (low > high)
324*b843c749SSergey Zigachev 		return -EINVAL;
325*b843c749SSergey Zigachev 
326*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
327*b843c749SSergey Zigachev 			CG_THERMAL_INT, DIG_THERM_INTH,
328*b843c749SSergey Zigachev 			(high / PP_TEMPERATURE_UNITS_PER_CENTIGRADES));
329*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
330*b843c749SSergey Zigachev 			CG_THERMAL_INT, DIG_THERM_INTL,
331*b843c749SSergey Zigachev 			(low / PP_TEMPERATURE_UNITS_PER_CENTIGRADES));
332*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
333*b843c749SSergey Zigachev 			CG_THERMAL_CTRL, DIG_THERM_DPM,
334*b843c749SSergey Zigachev 			(high / PP_TEMPERATURE_UNITS_PER_CENTIGRADES));
335*b843c749SSergey Zigachev 
336*b843c749SSergey Zigachev 	return 0;
337*b843c749SSergey Zigachev }
338*b843c749SSergey Zigachev 
339*b843c749SSergey Zigachev /**
340*b843c749SSergey Zigachev * Programs thermal controller one-time setting registers
341*b843c749SSergey Zigachev *
342*b843c749SSergey Zigachev * @param    hwmgr The address of the hardware manager.
343*b843c749SSergey Zigachev */
smu7_thermal_initialize(struct pp_hwmgr * hwmgr)344*b843c749SSergey Zigachev static int smu7_thermal_initialize(struct pp_hwmgr *hwmgr)
345*b843c749SSergey Zigachev {
346*b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.ucTachometerPulsesPerRevolution)
347*b843c749SSergey Zigachev 		PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
348*b843c749SSergey Zigachev 				CG_TACH_CTRL, EDGE_PER_REV,
349*b843c749SSergey Zigachev 				hwmgr->thermal_controller.fanInfo.
350*b843c749SSergey Zigachev 				ucTachometerPulsesPerRevolution - 1);
351*b843c749SSergey Zigachev 
352*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
353*b843c749SSergey Zigachev 			CG_FDO_CTRL2, TACH_PWM_RESP_RATE, 0x28);
354*b843c749SSergey Zigachev 
355*b843c749SSergey Zigachev 	return 0;
356*b843c749SSergey Zigachev }
357*b843c749SSergey Zigachev 
358*b843c749SSergey Zigachev /**
359*b843c749SSergey Zigachev * Enable thermal alerts on the RV770 thermal controller.
360*b843c749SSergey Zigachev *
361*b843c749SSergey Zigachev * @param    hwmgr The address of the hardware manager.
362*b843c749SSergey Zigachev */
smu7_thermal_enable_alert(struct pp_hwmgr * hwmgr)363*b843c749SSergey Zigachev static void smu7_thermal_enable_alert(struct pp_hwmgr *hwmgr)
364*b843c749SSergey Zigachev {
365*b843c749SSergey Zigachev 	uint32_t alert;
366*b843c749SSergey Zigachev 
367*b843c749SSergey Zigachev 	alert = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
368*b843c749SSergey Zigachev 			CG_THERMAL_INT, THERM_INT_MASK);
369*b843c749SSergey Zigachev 	alert &= ~(SMU7_THERMAL_HIGH_ALERT_MASK | SMU7_THERMAL_LOW_ALERT_MASK);
370*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
371*b843c749SSergey Zigachev 			CG_THERMAL_INT, THERM_INT_MASK, alert);
372*b843c749SSergey Zigachev 
373*b843c749SSergey Zigachev 	/* send message to SMU to enable internal thermal interrupts */
374*b843c749SSergey Zigachev 	smum_send_msg_to_smc(hwmgr, PPSMC_MSG_Thermal_Cntl_Enable);
375*b843c749SSergey Zigachev }
376*b843c749SSergey Zigachev 
377*b843c749SSergey Zigachev /**
378*b843c749SSergey Zigachev * Disable thermal alerts on the RV770 thermal controller.
379*b843c749SSergey Zigachev * @param    hwmgr The address of the hardware manager.
380*b843c749SSergey Zigachev */
smu7_thermal_disable_alert(struct pp_hwmgr * hwmgr)381*b843c749SSergey Zigachev int smu7_thermal_disable_alert(struct pp_hwmgr *hwmgr)
382*b843c749SSergey Zigachev {
383*b843c749SSergey Zigachev 	uint32_t alert;
384*b843c749SSergey Zigachev 
385*b843c749SSergey Zigachev 	alert = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
386*b843c749SSergey Zigachev 			CG_THERMAL_INT, THERM_INT_MASK);
387*b843c749SSergey Zigachev 	alert |= (SMU7_THERMAL_HIGH_ALERT_MASK | SMU7_THERMAL_LOW_ALERT_MASK);
388*b843c749SSergey Zigachev 	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
389*b843c749SSergey Zigachev 			CG_THERMAL_INT, THERM_INT_MASK, alert);
390*b843c749SSergey Zigachev 
391*b843c749SSergey Zigachev 	/* send message to SMU to disable internal thermal interrupts */
392*b843c749SSergey Zigachev 	return smum_send_msg_to_smc(hwmgr, PPSMC_MSG_Thermal_Cntl_Disable);
393*b843c749SSergey Zigachev }
394*b843c749SSergey Zigachev 
395*b843c749SSergey Zigachev /**
396*b843c749SSergey Zigachev * Uninitialize the thermal controller.
397*b843c749SSergey Zigachev * Currently just disables alerts.
398*b843c749SSergey Zigachev * @param    hwmgr The address of the hardware manager.
399*b843c749SSergey Zigachev */
smu7_thermal_stop_thermal_controller(struct pp_hwmgr * hwmgr)400*b843c749SSergey Zigachev int smu7_thermal_stop_thermal_controller(struct pp_hwmgr *hwmgr)
401*b843c749SSergey Zigachev {
402*b843c749SSergey Zigachev 	int result = smu7_thermal_disable_alert(hwmgr);
403*b843c749SSergey Zigachev 
404*b843c749SSergey Zigachev 	if (!hwmgr->thermal_controller.fanInfo.bNoFan)
405*b843c749SSergey Zigachev 		smu7_fan_ctrl_set_default_mode(hwmgr);
406*b843c749SSergey Zigachev 
407*b843c749SSergey Zigachev 	return result;
408*b843c749SSergey Zigachev }
409*b843c749SSergey Zigachev 
410*b843c749SSergey Zigachev /**
411*b843c749SSergey Zigachev * Start the fan control on the SMC.
412*b843c749SSergey Zigachev * @param    hwmgr  the address of the powerplay hardware manager.
413*b843c749SSergey Zigachev * @param    pInput the pointer to input data
414*b843c749SSergey Zigachev * @param    pOutput the pointer to output data
415*b843c749SSergey Zigachev * @param    pStorage the pointer to temporary storage
416*b843c749SSergey Zigachev * @param    Result the last failure code
417*b843c749SSergey Zigachev * @return   result from set temperature range routine
418*b843c749SSergey Zigachev */
smu7_thermal_start_smc_fan_control(struct pp_hwmgr * hwmgr)419*b843c749SSergey Zigachev static int smu7_thermal_start_smc_fan_control(struct pp_hwmgr *hwmgr)
420*b843c749SSergey Zigachev {
421*b843c749SSergey Zigachev /* If the fantable setup has failed we could have disabled
422*b843c749SSergey Zigachev  * PHM_PlatformCaps_MicrocodeFanControl even after
423*b843c749SSergey Zigachev  * this function was included in the table.
424*b843c749SSergey Zigachev  * Make sure that we still think controlling the fan is OK.
425*b843c749SSergey Zigachev */
426*b843c749SSergey Zigachev 	if (PP_CAP(PHM_PlatformCaps_MicrocodeFanControl)) {
427*b843c749SSergey Zigachev 		smu7_fan_ctrl_start_smc_fan_control(hwmgr);
428*b843c749SSergey Zigachev 		smu7_fan_ctrl_set_static_mode(hwmgr, FDO_PWM_MODE_STATIC);
429*b843c749SSergey Zigachev 	}
430*b843c749SSergey Zigachev 
431*b843c749SSergey Zigachev 	return 0;
432*b843c749SSergey Zigachev }
433*b843c749SSergey Zigachev 
smu7_start_thermal_controller(struct pp_hwmgr * hwmgr,struct PP_TemperatureRange * range)434*b843c749SSergey Zigachev int smu7_start_thermal_controller(struct pp_hwmgr *hwmgr,
435*b843c749SSergey Zigachev 				struct PP_TemperatureRange *range)
436*b843c749SSergey Zigachev {
437*b843c749SSergey Zigachev 	int ret = 0;
438*b843c749SSergey Zigachev 
439*b843c749SSergey Zigachev 	if (range == NULL)
440*b843c749SSergey Zigachev 		return -EINVAL;
441*b843c749SSergey Zigachev 
442*b843c749SSergey Zigachev 	smu7_thermal_initialize(hwmgr);
443*b843c749SSergey Zigachev 	ret = smu7_thermal_set_temperature_range(hwmgr, range->min, range->max);
444*b843c749SSergey Zigachev 	if (ret)
445*b843c749SSergey Zigachev 		return -EINVAL;
446*b843c749SSergey Zigachev 	smu7_thermal_enable_alert(hwmgr);
447*b843c749SSergey Zigachev 	ret = smum_thermal_avfs_enable(hwmgr);
448*b843c749SSergey Zigachev 	if (ret)
449*b843c749SSergey Zigachev 		return -EINVAL;
450*b843c749SSergey Zigachev 
451*b843c749SSergey Zigachev /* We should restrict performance levels to low before we halt the SMC.
452*b843c749SSergey Zigachev  * On the other hand we are still in boot state when we do this
453*b843c749SSergey Zigachev  * so it would be pointless.
454*b843c749SSergey Zigachev  * If this assumption changes we have to revisit this table.
455*b843c749SSergey Zigachev  */
456*b843c749SSergey Zigachev 	smum_thermal_setup_fan_table(hwmgr);
457*b843c749SSergey Zigachev 	smu7_thermal_start_smc_fan_control(hwmgr);
458*b843c749SSergey Zigachev 	return 0;
459*b843c749SSergey Zigachev }
460*b843c749SSergey Zigachev 
461*b843c749SSergey Zigachev 
462*b843c749SSergey Zigachev 
smu7_thermal_ctrl_uninitialize_thermal_controller(struct pp_hwmgr * hwmgr)463*b843c749SSergey Zigachev int smu7_thermal_ctrl_uninitialize_thermal_controller(struct pp_hwmgr *hwmgr)
464*b843c749SSergey Zigachev {
465*b843c749SSergey Zigachev 	if (!hwmgr->thermal_controller.fanInfo.bNoFan)
466*b843c749SSergey Zigachev 		smu7_fan_ctrl_set_default_mode(hwmgr);
467*b843c749SSergey Zigachev 	return 0;
468*b843c749SSergey Zigachev }
469*b843c749SSergey Zigachev 
470