• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22 
23 #define SWSMU_CODE_LAYER_L1
24 
25 #include <linux/firmware.h>
26 #include <linux/pci.h>
27 #include <linux/power_supply.h>
28 #include <linux/reboot.h>
29 
30 #include "amdgpu.h"
31 #include "amdgpu_smu.h"
32 #include "smu_internal.h"
33 #include "atom.h"
34 #include "arcturus_ppt.h"
35 #include "navi10_ppt.h"
36 #include "sienna_cichlid_ppt.h"
37 #include "renoir_ppt.h"
38 #include "vangogh_ppt.h"
39 #include "aldebaran_ppt.h"
40 #include "yellow_carp_ppt.h"
41 #include "cyan_skillfish_ppt.h"
42 #include "smu_v13_0_0_ppt.h"
43 #include "smu_v13_0_4_ppt.h"
44 #include "smu_v13_0_5_ppt.h"
45 #include "smu_v13_0_6_ppt.h"
46 #include "smu_v13_0_7_ppt.h"
47 #include "smu_v14_0_0_ppt.h"
48 #include "smu_v14_0_2_ppt.h"
49 #include "amd_pcie.h"
50 
51 /*
52  * DO NOT use these for err/warn/info/debug messages.
53  * Use dev_err, dev_warn, dev_info and dev_dbg instead.
54  * They are more MGPU friendly.
55  */
56 #undef pr_err
57 #undef pr_warn
58 #undef pr_info
59 #undef pr_debug
60 
61 static const struct amd_pm_funcs swsmu_pm_funcs;
62 static int smu_force_smuclk_levels(struct smu_context *smu,
63 				   enum smu_clk_type clk_type,
64 				   uint32_t mask);
65 static int smu_handle_task(struct smu_context *smu,
66 			   enum amd_dpm_forced_level level,
67 			   enum amd_pp_task task_id);
68 static int smu_reset(struct smu_context *smu);
69 static int smu_set_fan_speed_pwm(void *handle, u32 speed);
70 static int smu_set_fan_control_mode(void *handle, u32 value);
71 static int smu_set_power_limit(void *handle, uint32_t limit);
72 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);
73 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);
74 static int smu_set_mp1_state(void *handle, enum pp_mp1_state mp1_state);
75 static void smu_power_profile_mode_get(struct smu_context *smu,
76 				       enum PP_SMC_POWER_PROFILE profile_mode);
77 static void smu_power_profile_mode_put(struct smu_context *smu,
78 				       enum PP_SMC_POWER_PROFILE profile_mode);
79 
smu_sys_get_pp_feature_mask(void * handle,char * buf)80 static int smu_sys_get_pp_feature_mask(void *handle,
81 				       char *buf)
82 {
83 	struct smu_context *smu = handle;
84 
85 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
86 		return -EOPNOTSUPP;
87 
88 	return smu_get_pp_feature_mask(smu, buf);
89 }
90 
smu_sys_set_pp_feature_mask(void * handle,uint64_t new_mask)91 static int smu_sys_set_pp_feature_mask(void *handle,
92 				       uint64_t new_mask)
93 {
94 	struct smu_context *smu = handle;
95 
96 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
97 		return -EOPNOTSUPP;
98 
99 	return smu_set_pp_feature_mask(smu, new_mask);
100 }
101 
smu_set_residency_gfxoff(struct smu_context * smu,bool value)102 int smu_set_residency_gfxoff(struct smu_context *smu, bool value)
103 {
104 	if (!smu->ppt_funcs->set_gfx_off_residency)
105 		return -EINVAL;
106 
107 	return smu_set_gfx_off_residency(smu, value);
108 }
109 
smu_get_residency_gfxoff(struct smu_context * smu,u32 * value)110 int smu_get_residency_gfxoff(struct smu_context *smu, u32 *value)
111 {
112 	if (!smu->ppt_funcs->get_gfx_off_residency)
113 		return -EINVAL;
114 
115 	return smu_get_gfx_off_residency(smu, value);
116 }
117 
smu_get_entrycount_gfxoff(struct smu_context * smu,u64 * value)118 int smu_get_entrycount_gfxoff(struct smu_context *smu, u64 *value)
119 {
120 	if (!smu->ppt_funcs->get_gfx_off_entrycount)
121 		return -EINVAL;
122 
123 	return smu_get_gfx_off_entrycount(smu, value);
124 }
125 
smu_get_status_gfxoff(struct smu_context * smu,uint32_t * value)126 int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value)
127 {
128 	if (!smu->ppt_funcs->get_gfx_off_status)
129 		return -EINVAL;
130 
131 	*value = smu_get_gfx_off_status(smu);
132 
133 	return 0;
134 }
135 
smu_set_soft_freq_range(struct smu_context * smu,enum smu_clk_type clk_type,uint32_t min,uint32_t max)136 int smu_set_soft_freq_range(struct smu_context *smu,
137 			    enum smu_clk_type clk_type,
138 			    uint32_t min,
139 			    uint32_t max)
140 {
141 	int ret = 0;
142 
143 	if (smu->ppt_funcs->set_soft_freq_limited_range)
144 		ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
145 								  clk_type,
146 								  min,
147 								  max);
148 
149 	return ret;
150 }
151 
smu_get_dpm_freq_range(struct smu_context * smu,enum smu_clk_type clk_type,uint32_t * min,uint32_t * max)152 int smu_get_dpm_freq_range(struct smu_context *smu,
153 			   enum smu_clk_type clk_type,
154 			   uint32_t *min,
155 			   uint32_t *max)
156 {
157 	int ret = -ENOTSUPP;
158 
159 	if (!min && !max)
160 		return -EINVAL;
161 
162 	if (smu->ppt_funcs->get_dpm_ultimate_freq)
163 		ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
164 							    clk_type,
165 							    min,
166 							    max);
167 
168 	return ret;
169 }
170 
smu_set_gfx_power_up_by_imu(struct smu_context * smu)171 int smu_set_gfx_power_up_by_imu(struct smu_context *smu)
172 {
173 	int ret = 0;
174 	struct amdgpu_device *adev = smu->adev;
175 
176 	if (smu->ppt_funcs->set_gfx_power_up_by_imu) {
177 		ret = smu->ppt_funcs->set_gfx_power_up_by_imu(smu);
178 		if (ret)
179 			dev_err(adev->dev, "Failed to enable gfx imu!\n");
180 	}
181 	return ret;
182 }
183 
smu_get_mclk(void * handle,bool low)184 static u32 smu_get_mclk(void *handle, bool low)
185 {
186 	struct smu_context *smu = handle;
187 	uint32_t clk_freq;
188 	int ret = 0;
189 
190 	ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
191 				     low ? &clk_freq : NULL,
192 				     !low ? &clk_freq : NULL);
193 	if (ret)
194 		return 0;
195 	return clk_freq * 100;
196 }
197 
smu_get_sclk(void * handle,bool low)198 static u32 smu_get_sclk(void *handle, bool low)
199 {
200 	struct smu_context *smu = handle;
201 	uint32_t clk_freq;
202 	int ret = 0;
203 
204 	ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
205 				     low ? &clk_freq : NULL,
206 				     !low ? &clk_freq : NULL);
207 	if (ret)
208 		return 0;
209 	return clk_freq * 100;
210 }
211 
smu_set_gfx_imu_enable(struct smu_context * smu)212 static int smu_set_gfx_imu_enable(struct smu_context *smu)
213 {
214 	struct amdgpu_device *adev = smu->adev;
215 
216 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
217 		return 0;
218 
219 	if (amdgpu_in_reset(smu->adev) || adev->in_s0ix)
220 		return 0;
221 
222 	return smu_set_gfx_power_up_by_imu(smu);
223 }
224 
is_vcn_enabled(struct amdgpu_device * adev)225 static bool is_vcn_enabled(struct amdgpu_device *adev)
226 {
227 	int i;
228 
229 	for (i = 0; i < adev->num_ip_blocks; i++) {
230 		if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_VCN ||
231 			adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_JPEG) &&
232 			!adev->ip_blocks[i].status.valid)
233 			return false;
234 	}
235 
236 	return true;
237 }
238 
smu_dpm_set_vcn_enable(struct smu_context * smu,bool enable)239 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
240 				  bool enable)
241 {
242 	struct smu_power_context *smu_power = &smu->smu_power;
243 	struct smu_power_gate *power_gate = &smu_power->power_gate;
244 	int ret = 0;
245 
246 	/*
247 	 * don't poweron vcn/jpeg when they are skipped.
248 	 */
249 	if (!is_vcn_enabled(smu->adev))
250 		return 0;
251 
252 	if (!smu->ppt_funcs->dpm_set_vcn_enable)
253 		return 0;
254 
255 	if (atomic_read(&power_gate->vcn_gated) ^ enable)
256 		return 0;
257 
258 	ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
259 	if (!ret)
260 		atomic_set(&power_gate->vcn_gated, !enable);
261 
262 	return ret;
263 }
264 
smu_dpm_set_jpeg_enable(struct smu_context * smu,bool enable)265 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
266 				   bool enable)
267 {
268 	struct smu_power_context *smu_power = &smu->smu_power;
269 	struct smu_power_gate *power_gate = &smu_power->power_gate;
270 	int ret = 0;
271 
272 	if (!is_vcn_enabled(smu->adev))
273 		return 0;
274 
275 	if (!smu->ppt_funcs->dpm_set_jpeg_enable)
276 		return 0;
277 
278 	if (atomic_read(&power_gate->jpeg_gated) ^ enable)
279 		return 0;
280 
281 	ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
282 	if (!ret)
283 		atomic_set(&power_gate->jpeg_gated, !enable);
284 
285 	return ret;
286 }
287 
smu_dpm_set_vpe_enable(struct smu_context * smu,bool enable)288 static int smu_dpm_set_vpe_enable(struct smu_context *smu,
289 				   bool enable)
290 {
291 	struct smu_power_context *smu_power = &smu->smu_power;
292 	struct smu_power_gate *power_gate = &smu_power->power_gate;
293 	int ret = 0;
294 
295 	if (!smu->ppt_funcs->dpm_set_vpe_enable)
296 		return 0;
297 
298 	if (atomic_read(&power_gate->vpe_gated) ^ enable)
299 		return 0;
300 
301 	ret = smu->ppt_funcs->dpm_set_vpe_enable(smu, enable);
302 	if (!ret)
303 		atomic_set(&power_gate->vpe_gated, !enable);
304 
305 	return ret;
306 }
307 
smu_dpm_set_umsch_mm_enable(struct smu_context * smu,bool enable)308 static int smu_dpm_set_umsch_mm_enable(struct smu_context *smu,
309 				   bool enable)
310 {
311 	struct smu_power_context *smu_power = &smu->smu_power;
312 	struct smu_power_gate *power_gate = &smu_power->power_gate;
313 	int ret = 0;
314 
315 	if (!smu->adev->enable_umsch_mm)
316 		return 0;
317 
318 	if (!smu->ppt_funcs->dpm_set_umsch_mm_enable)
319 		return 0;
320 
321 	if (atomic_read(&power_gate->umsch_mm_gated) ^ enable)
322 		return 0;
323 
324 	ret = smu->ppt_funcs->dpm_set_umsch_mm_enable(smu, enable);
325 	if (!ret)
326 		atomic_set(&power_gate->umsch_mm_gated, !enable);
327 
328 	return ret;
329 }
330 
smu_set_mall_enable(struct smu_context * smu)331 static int smu_set_mall_enable(struct smu_context *smu)
332 {
333 	int ret = 0;
334 
335 	if (!smu->ppt_funcs->set_mall_enable)
336 		return 0;
337 
338 	ret = smu->ppt_funcs->set_mall_enable(smu);
339 
340 	return ret;
341 }
342 
343 /**
344  * smu_dpm_set_power_gate - power gate/ungate the specific IP block
345  *
346  * @handle:        smu_context pointer
347  * @block_type: the IP block to power gate/ungate
348  * @gate:       to power gate if true, ungate otherwise
349  *
350  * This API uses no smu->mutex lock protection due to:
351  * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
352  *    This is guarded to be race condition free by the caller.
353  * 2. Or get called on user setting request of power_dpm_force_performance_level.
354  *    Under this case, the smu->mutex lock protection is already enforced on
355  *    the parent API smu_force_performance_level of the call path.
356  */
smu_dpm_set_power_gate(void * handle,uint32_t block_type,bool gate)357 static int smu_dpm_set_power_gate(void *handle,
358 				  uint32_t block_type,
359 				  bool gate)
360 {
361 	struct smu_context *smu = handle;
362 	int ret = 0;
363 
364 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) {
365 		dev_WARN(smu->adev->dev,
366 			 "SMU uninitialized but power %s requested for %u!\n",
367 			 gate ? "gate" : "ungate", block_type);
368 		return -EOPNOTSUPP;
369 	}
370 
371 	switch (block_type) {
372 	/*
373 	 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
374 	 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
375 	 */
376 	case AMD_IP_BLOCK_TYPE_UVD:
377 	case AMD_IP_BLOCK_TYPE_VCN:
378 		ret = smu_dpm_set_vcn_enable(smu, !gate);
379 		if (ret)
380 			dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
381 				gate ? "gate" : "ungate");
382 		break;
383 	case AMD_IP_BLOCK_TYPE_GFX:
384 		ret = smu_gfx_off_control(smu, gate);
385 		if (ret)
386 			dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
387 				gate ? "enable" : "disable");
388 		break;
389 	case AMD_IP_BLOCK_TYPE_SDMA:
390 		ret = smu_powergate_sdma(smu, gate);
391 		if (ret)
392 			dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
393 				gate ? "gate" : "ungate");
394 		break;
395 	case AMD_IP_BLOCK_TYPE_JPEG:
396 		ret = smu_dpm_set_jpeg_enable(smu, !gate);
397 		if (ret)
398 			dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
399 				gate ? "gate" : "ungate");
400 		break;
401 	case AMD_IP_BLOCK_TYPE_VPE:
402 		ret = smu_dpm_set_vpe_enable(smu, !gate);
403 		if (ret)
404 			dev_err(smu->adev->dev, "Failed to power %s VPE!\n",
405 				gate ? "gate" : "ungate");
406 		break;
407 	default:
408 		dev_err(smu->adev->dev, "Unsupported block type!\n");
409 		return -EINVAL;
410 	}
411 
412 	return ret;
413 }
414 
415 /**
416  * smu_set_user_clk_dependencies - set user profile clock dependencies
417  *
418  * @smu:	smu_context pointer
419  * @clk:	enum smu_clk_type type
420  *
421  * Enable/Disable the clock dependency for the @clk type.
422  */
smu_set_user_clk_dependencies(struct smu_context * smu,enum smu_clk_type clk)423 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
424 {
425 	if (smu->adev->in_suspend)
426 		return;
427 
428 	if (clk == SMU_MCLK) {
429 		smu->user_dpm_profile.clk_dependency = 0;
430 		smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
431 	} else if (clk == SMU_FCLK) {
432 		/* MCLK takes precedence over FCLK */
433 		if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
434 			return;
435 
436 		smu->user_dpm_profile.clk_dependency = 0;
437 		smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
438 	} else if (clk == SMU_SOCCLK) {
439 		/* MCLK takes precedence over SOCCLK */
440 		if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
441 			return;
442 
443 		smu->user_dpm_profile.clk_dependency = 0;
444 		smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
445 	} else
446 		/* Add clk dependencies here, if any */
447 		return;
448 }
449 
450 /**
451  * smu_restore_dpm_user_profile - reinstate user dpm profile
452  *
453  * @smu:	smu_context pointer
454  *
455  * Restore the saved user power configurations include power limit,
456  * clock frequencies, fan control mode and fan speed.
457  */
smu_restore_dpm_user_profile(struct smu_context * smu)458 static void smu_restore_dpm_user_profile(struct smu_context *smu)
459 {
460 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
461 	int ret = 0;
462 
463 	if (!smu->adev->in_suspend)
464 		return;
465 
466 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
467 		return;
468 
469 	/* Enable restore flag */
470 	smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
471 
472 	/* set the user dpm power limit */
473 	if (smu->user_dpm_profile.power_limit) {
474 		ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
475 		if (ret)
476 			dev_err(smu->adev->dev, "Failed to set power limit value\n");
477 	}
478 
479 	/* set the user dpm clock configurations */
480 	if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
481 		enum smu_clk_type clk_type;
482 
483 		for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
484 			/*
485 			 * Iterate over smu clk type and force the saved user clk
486 			 * configs, skip if clock dependency is enabled
487 			 */
488 			if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
489 					smu->user_dpm_profile.clk_mask[clk_type]) {
490 				ret = smu_force_smuclk_levels(smu, clk_type,
491 						smu->user_dpm_profile.clk_mask[clk_type]);
492 				if (ret)
493 					dev_err(smu->adev->dev,
494 						"Failed to set clock type = %d\n", clk_type);
495 			}
496 		}
497 	}
498 
499 	/* set the user dpm fan configurations */
500 	if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||
501 	    smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {
502 		ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
503 		if (ret != -EOPNOTSUPP) {
504 			smu->user_dpm_profile.fan_speed_pwm = 0;
505 			smu->user_dpm_profile.fan_speed_rpm = 0;
506 			smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;
507 			dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
508 		}
509 
510 		if (smu->user_dpm_profile.fan_speed_pwm) {
511 			ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);
512 			if (ret != -EOPNOTSUPP)
513 				dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");
514 		}
515 
516 		if (smu->user_dpm_profile.fan_speed_rpm) {
517 			ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);
518 			if (ret != -EOPNOTSUPP)
519 				dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");
520 		}
521 	}
522 
523 	/* Restore user customized OD settings */
524 	if (smu->user_dpm_profile.user_od) {
525 		if (smu->ppt_funcs->restore_user_od_settings) {
526 			ret = smu->ppt_funcs->restore_user_od_settings(smu);
527 			if (ret)
528 				dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");
529 		}
530 	}
531 
532 	/* Disable restore flag */
533 	smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
534 }
535 
smu_get_power_num_states(void * handle,struct pp_states_info * state_info)536 static int smu_get_power_num_states(void *handle,
537 				    struct pp_states_info *state_info)
538 {
539 	if (!state_info)
540 		return -EINVAL;
541 
542 	/* not support power state */
543 	memset(state_info, 0, sizeof(struct pp_states_info));
544 	state_info->nums = 1;
545 	state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
546 
547 	return 0;
548 }
549 
is_support_sw_smu(struct amdgpu_device * adev)550 bool is_support_sw_smu(struct amdgpu_device *adev)
551 {
552 	/* vega20 is 11.0.2, but it's supported via the powerplay code */
553 	if (adev->asic_type == CHIP_VEGA20)
554 		return false;
555 
556 	if (amdgpu_ip_version(adev, MP1_HWIP, 0) >= IP_VERSION(11, 0, 0))
557 		return true;
558 
559 	return false;
560 }
561 
is_support_cclk_dpm(struct amdgpu_device * adev)562 bool is_support_cclk_dpm(struct amdgpu_device *adev)
563 {
564 	struct smu_context *smu = adev->powerplay.pp_handle;
565 
566 	if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
567 		return false;
568 
569 	return true;
570 }
571 
572 
smu_sys_get_pp_table(void * handle,char ** table)573 static int smu_sys_get_pp_table(void *handle,
574 				char **table)
575 {
576 	struct smu_context *smu = handle;
577 	struct smu_table_context *smu_table = &smu->smu_table;
578 
579 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
580 		return -EOPNOTSUPP;
581 
582 	if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
583 		return -EINVAL;
584 
585 	if (smu_table->hardcode_pptable)
586 		*table = smu_table->hardcode_pptable;
587 	else
588 		*table = smu_table->power_play_table;
589 
590 	return smu_table->power_play_table_size;
591 }
592 
smu_sys_set_pp_table(void * handle,const char * buf,size_t size)593 static int smu_sys_set_pp_table(void *handle,
594 				const char *buf,
595 				size_t size)
596 {
597 	struct smu_context *smu = handle;
598 	struct smu_table_context *smu_table = &smu->smu_table;
599 	ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
600 	int ret = 0;
601 
602 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
603 		return -EOPNOTSUPP;
604 
605 	if (header->usStructureSize != size) {
606 		dev_err(smu->adev->dev, "pp table size not matched !\n");
607 		return -EIO;
608 	}
609 
610 	if (!smu_table->hardcode_pptable || smu_table->power_play_table_size < size) {
611 		kfree(smu_table->hardcode_pptable);
612 		smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
613 		if (!smu_table->hardcode_pptable)
614 			return -ENOMEM;
615 	}
616 
617 	memcpy(smu_table->hardcode_pptable, buf, size);
618 	smu_table->power_play_table = smu_table->hardcode_pptable;
619 	smu_table->power_play_table_size = size;
620 
621 	/*
622 	 * Special hw_fini action(for Navi1x, the DPMs disablement will be
623 	 * skipped) may be needed for custom pptable uploading.
624 	 */
625 	smu->uploading_custom_pp_table = true;
626 
627 	ret = smu_reset(smu);
628 	if (ret)
629 		dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
630 
631 	smu->uploading_custom_pp_table = false;
632 
633 	return ret;
634 }
635 
smu_get_driver_allowed_feature_mask(struct smu_context * smu)636 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
637 {
638 	struct smu_feature *feature = &smu->smu_feature;
639 	uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
640 	int ret = 0;
641 
642 	/*
643 	 * With SCPM enabled, the allowed featuremasks setting(via
644 	 * PPSMC_MSG_SetAllowedFeaturesMaskLow/High) is not permitted.
645 	 * That means there is no way to let PMFW knows the settings below.
646 	 * Thus, we just assume all the features are allowed under
647 	 * such scenario.
648 	 */
649 	if (smu->adev->scpm_enabled) {
650 		bitmap_fill(feature->allowed, SMU_FEATURE_MAX);
651 		return 0;
652 	}
653 
654 	bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
655 
656 	ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
657 					     SMU_FEATURE_MAX/32);
658 	if (ret)
659 		return ret;
660 
661 	bitmap_or(feature->allowed, feature->allowed,
662 		      (unsigned long *)allowed_feature_mask,
663 		      feature->feature_num);
664 
665 	return ret;
666 }
667 
smu_set_funcs(struct amdgpu_device * adev)668 static int smu_set_funcs(struct amdgpu_device *adev)
669 {
670 	struct smu_context *smu = adev->powerplay.pp_handle;
671 
672 	if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
673 		smu->od_enabled = true;
674 
675 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
676 	case IP_VERSION(11, 0, 0):
677 	case IP_VERSION(11, 0, 5):
678 	case IP_VERSION(11, 0, 9):
679 		navi10_set_ppt_funcs(smu);
680 		break;
681 	case IP_VERSION(11, 0, 7):
682 	case IP_VERSION(11, 0, 11):
683 	case IP_VERSION(11, 0, 12):
684 	case IP_VERSION(11, 0, 13):
685 		sienna_cichlid_set_ppt_funcs(smu);
686 		break;
687 	case IP_VERSION(12, 0, 0):
688 	case IP_VERSION(12, 0, 1):
689 		renoir_set_ppt_funcs(smu);
690 		break;
691 	case IP_VERSION(11, 5, 0):
692 		vangogh_set_ppt_funcs(smu);
693 		break;
694 	case IP_VERSION(13, 0, 1):
695 	case IP_VERSION(13, 0, 3):
696 	case IP_VERSION(13, 0, 8):
697 		yellow_carp_set_ppt_funcs(smu);
698 		break;
699 	case IP_VERSION(13, 0, 4):
700 	case IP_VERSION(13, 0, 11):
701 		smu_v13_0_4_set_ppt_funcs(smu);
702 		break;
703 	case IP_VERSION(13, 0, 5):
704 		smu_v13_0_5_set_ppt_funcs(smu);
705 		break;
706 	case IP_VERSION(11, 0, 8):
707 		cyan_skillfish_set_ppt_funcs(smu);
708 		break;
709 	case IP_VERSION(11, 0, 2):
710 		adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
711 		arcturus_set_ppt_funcs(smu);
712 		/* OD is not supported on Arcturus */
713 		smu->od_enabled = false;
714 		break;
715 	case IP_VERSION(13, 0, 2):
716 		aldebaran_set_ppt_funcs(smu);
717 		/* Enable pp_od_clk_voltage node */
718 		smu->od_enabled = true;
719 		break;
720 	case IP_VERSION(13, 0, 0):
721 	case IP_VERSION(13, 0, 10):
722 		smu_v13_0_0_set_ppt_funcs(smu);
723 		break;
724 	case IP_VERSION(13, 0, 6):
725 	case IP_VERSION(13, 0, 14):
726 		smu_v13_0_6_set_ppt_funcs(smu);
727 		/* Enable pp_od_clk_voltage node */
728 		smu->od_enabled = true;
729 		break;
730 	case IP_VERSION(13, 0, 7):
731 		smu_v13_0_7_set_ppt_funcs(smu);
732 		break;
733 	case IP_VERSION(14, 0, 0):
734 	case IP_VERSION(14, 0, 1):
735 	case IP_VERSION(14, 0, 4):
736 		smu_v14_0_0_set_ppt_funcs(smu);
737 		break;
738 	case IP_VERSION(14, 0, 2):
739 	case IP_VERSION(14, 0, 3):
740 		smu_v14_0_2_set_ppt_funcs(smu);
741 		break;
742 	default:
743 		return -EINVAL;
744 	}
745 
746 	return 0;
747 }
748 
smu_early_init(void * handle)749 static int smu_early_init(void *handle)
750 {
751 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
752 	struct smu_context *smu;
753 	int r;
754 
755 	smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL);
756 	if (!smu)
757 		return -ENOMEM;
758 
759 	smu->adev = adev;
760 	smu->pm_enabled = !!amdgpu_dpm;
761 	smu->is_apu = false;
762 	smu->smu_baco.state = SMU_BACO_STATE_NONE;
763 	smu->smu_baco.platform_support = false;
764 	smu->smu_baco.maco_support = false;
765 	smu->user_dpm_profile.fan_mode = -1;
766 
767 	mutex_init(&smu->message_lock);
768 
769 	adev->powerplay.pp_handle = smu;
770 	adev->powerplay.pp_funcs = &swsmu_pm_funcs;
771 
772 	r = smu_set_funcs(adev);
773 	if (r)
774 		return r;
775 	return smu_init_microcode(smu);
776 }
777 
smu_set_default_dpm_table(struct smu_context * smu)778 static int smu_set_default_dpm_table(struct smu_context *smu)
779 {
780 	struct amdgpu_device *adev = smu->adev;
781 	struct smu_power_context *smu_power = &smu->smu_power;
782 	struct smu_power_gate *power_gate = &smu_power->power_gate;
783 	int vcn_gate, jpeg_gate;
784 	int ret = 0;
785 
786 	if (!smu->ppt_funcs->set_default_dpm_table)
787 		return 0;
788 
789 	if (adev->pg_flags & AMD_PG_SUPPORT_VCN)
790 		vcn_gate = atomic_read(&power_gate->vcn_gated);
791 	if (adev->pg_flags & AMD_PG_SUPPORT_JPEG)
792 		jpeg_gate = atomic_read(&power_gate->jpeg_gated);
793 
794 	if (adev->pg_flags & AMD_PG_SUPPORT_VCN) {
795 		ret = smu_dpm_set_vcn_enable(smu, true);
796 		if (ret)
797 			return ret;
798 	}
799 
800 	if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {
801 		ret = smu_dpm_set_jpeg_enable(smu, true);
802 		if (ret)
803 			goto err_out;
804 	}
805 
806 	ret = smu->ppt_funcs->set_default_dpm_table(smu);
807 	if (ret)
808 		dev_err(smu->adev->dev,
809 			"Failed to setup default dpm clock tables!\n");
810 
811 	if (adev->pg_flags & AMD_PG_SUPPORT_JPEG)
812 		smu_dpm_set_jpeg_enable(smu, !jpeg_gate);
813 err_out:
814 	if (adev->pg_flags & AMD_PG_SUPPORT_VCN)
815 		smu_dpm_set_vcn_enable(smu, !vcn_gate);
816 
817 	return ret;
818 }
819 
smu_apply_default_config_table_settings(struct smu_context * smu)820 static int smu_apply_default_config_table_settings(struct smu_context *smu)
821 {
822 	struct amdgpu_device *adev = smu->adev;
823 	int ret = 0;
824 
825 	ret = smu_get_default_config_table_settings(smu,
826 						    &adev->pm.config_table);
827 	if (ret)
828 		return ret;
829 
830 	return smu_set_config_table(smu, &adev->pm.config_table);
831 }
832 
smu_late_init(void * handle)833 static int smu_late_init(void *handle)
834 {
835 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
836 	struct smu_context *smu = adev->powerplay.pp_handle;
837 	int ret = 0;
838 
839 	smu_set_fine_grain_gfx_freq_parameters(smu);
840 
841 	if (!smu->pm_enabled)
842 		return 0;
843 
844 	ret = smu_post_init(smu);
845 	if (ret) {
846 		dev_err(adev->dev, "Failed to post smu init!\n");
847 		return ret;
848 	}
849 
850 	/*
851 	 * Explicitly notify PMFW the power mode the system in. Since
852 	 * the PMFW may boot the ASIC with a different mode.
853 	 * For those supporting ACDC switch via gpio, PMFW will
854 	 * handle the switch automatically. Driver involvement
855 	 * is unnecessary.
856 	 */
857 	adev->pm.ac_power = power_supply_is_system_supplied() > 0;
858 	smu_set_ac_dc(smu);
859 
860 	if ((amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 1)) ||
861 	    (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 3)))
862 		return 0;
863 
864 	if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
865 		ret = smu_set_default_od_settings(smu);
866 		if (ret) {
867 			dev_err(adev->dev, "Failed to setup default OD settings!\n");
868 			return ret;
869 		}
870 	}
871 
872 	ret = smu_populate_umd_state_clk(smu);
873 	if (ret) {
874 		dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
875 		return ret;
876 	}
877 
878 	ret = smu_get_asic_power_limits(smu,
879 					&smu->current_power_limit,
880 					&smu->default_power_limit,
881 					&smu->max_power_limit,
882 					&smu->min_power_limit);
883 	if (ret) {
884 		dev_err(adev->dev, "Failed to get asic power limits!\n");
885 		return ret;
886 	}
887 
888 	if (!amdgpu_sriov_vf(adev))
889 		smu_get_unique_id(smu);
890 
891 	smu_get_fan_parameters(smu);
892 
893 	smu_handle_task(smu,
894 			smu->smu_dpm.dpm_level,
895 			AMD_PP_TASK_COMPLETE_INIT);
896 
897 	ret = smu_apply_default_config_table_settings(smu);
898 	if (ret && (ret != -EOPNOTSUPP)) {
899 		dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n");
900 		return ret;
901 	}
902 
903 	smu_restore_dpm_user_profile(smu);
904 
905 	return 0;
906 }
907 
smu_init_fb_allocations(struct smu_context * smu)908 static int smu_init_fb_allocations(struct smu_context *smu)
909 {
910 	struct amdgpu_device *adev = smu->adev;
911 	struct smu_table_context *smu_table = &smu->smu_table;
912 	struct smu_table *tables = smu_table->tables;
913 	struct smu_table *driver_table = &(smu_table->driver_table);
914 	uint32_t max_table_size = 0;
915 	int ret, i;
916 
917 	/* VRAM allocation for tool table */
918 	if (tables[SMU_TABLE_PMSTATUSLOG].size) {
919 		ret = amdgpu_bo_create_kernel(adev,
920 					      tables[SMU_TABLE_PMSTATUSLOG].size,
921 					      tables[SMU_TABLE_PMSTATUSLOG].align,
922 					      tables[SMU_TABLE_PMSTATUSLOG].domain,
923 					      &tables[SMU_TABLE_PMSTATUSLOG].bo,
924 					      &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
925 					      &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
926 		if (ret) {
927 			dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
928 			return ret;
929 		}
930 	}
931 
932 	driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT;
933 	/* VRAM allocation for driver table */
934 	for (i = 0; i < SMU_TABLE_COUNT; i++) {
935 		if (tables[i].size == 0)
936 			continue;
937 
938 		/* If one of the tables has VRAM domain restriction, keep it in
939 		 * VRAM
940 		 */
941 		if ((tables[i].domain &
942 		    (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) ==
943 			    AMDGPU_GEM_DOMAIN_VRAM)
944 			driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
945 
946 		if (i == SMU_TABLE_PMSTATUSLOG)
947 			continue;
948 
949 		if (max_table_size < tables[i].size)
950 			max_table_size = tables[i].size;
951 	}
952 
953 	driver_table->size = max_table_size;
954 	driver_table->align = PAGE_SIZE;
955 
956 	ret = amdgpu_bo_create_kernel(adev,
957 				      driver_table->size,
958 				      driver_table->align,
959 				      driver_table->domain,
960 				      &driver_table->bo,
961 				      &driver_table->mc_address,
962 				      &driver_table->cpu_addr);
963 	if (ret) {
964 		dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
965 		if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
966 			amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
967 					      &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
968 					      &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
969 	}
970 
971 	return ret;
972 }
973 
smu_fini_fb_allocations(struct smu_context * smu)974 static int smu_fini_fb_allocations(struct smu_context *smu)
975 {
976 	struct smu_table_context *smu_table = &smu->smu_table;
977 	struct smu_table *tables = smu_table->tables;
978 	struct smu_table *driver_table = &(smu_table->driver_table);
979 
980 	if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
981 		amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
982 				      &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
983 				      &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
984 
985 	amdgpu_bo_free_kernel(&driver_table->bo,
986 			      &driver_table->mc_address,
987 			      &driver_table->cpu_addr);
988 
989 	return 0;
990 }
991 
992 /**
993  * smu_alloc_memory_pool - allocate memory pool in the system memory
994  *
995  * @smu: amdgpu_device pointer
996  *
997  * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
998  * and DramLogSetDramAddr can notify it changed.
999  *
1000  * Returns 0 on success, error on failure.
1001  */
smu_alloc_memory_pool(struct smu_context * smu)1002 static int smu_alloc_memory_pool(struct smu_context *smu)
1003 {
1004 	struct amdgpu_device *adev = smu->adev;
1005 	struct smu_table_context *smu_table = &smu->smu_table;
1006 	struct smu_table *memory_pool = &smu_table->memory_pool;
1007 	uint64_t pool_size = smu->pool_size;
1008 	int ret = 0;
1009 
1010 	if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
1011 		return ret;
1012 
1013 	memory_pool->size = pool_size;
1014 	memory_pool->align = PAGE_SIZE;
1015 	memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
1016 
1017 	switch (pool_size) {
1018 	case SMU_MEMORY_POOL_SIZE_256_MB:
1019 	case SMU_MEMORY_POOL_SIZE_512_MB:
1020 	case SMU_MEMORY_POOL_SIZE_1_GB:
1021 	case SMU_MEMORY_POOL_SIZE_2_GB:
1022 		ret = amdgpu_bo_create_kernel(adev,
1023 					      memory_pool->size,
1024 					      memory_pool->align,
1025 					      memory_pool->domain,
1026 					      &memory_pool->bo,
1027 					      &memory_pool->mc_address,
1028 					      &memory_pool->cpu_addr);
1029 		if (ret)
1030 			dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
1031 		break;
1032 	default:
1033 		break;
1034 	}
1035 
1036 	return ret;
1037 }
1038 
smu_free_memory_pool(struct smu_context * smu)1039 static int smu_free_memory_pool(struct smu_context *smu)
1040 {
1041 	struct smu_table_context *smu_table = &smu->smu_table;
1042 	struct smu_table *memory_pool = &smu_table->memory_pool;
1043 
1044 	if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1045 		return 0;
1046 
1047 	amdgpu_bo_free_kernel(&memory_pool->bo,
1048 			      &memory_pool->mc_address,
1049 			      &memory_pool->cpu_addr);
1050 
1051 	memset(memory_pool, 0, sizeof(struct smu_table));
1052 
1053 	return 0;
1054 }
1055 
smu_alloc_dummy_read_table(struct smu_context * smu)1056 static int smu_alloc_dummy_read_table(struct smu_context *smu)
1057 {
1058 	struct smu_table_context *smu_table = &smu->smu_table;
1059 	struct smu_table *dummy_read_1_table =
1060 			&smu_table->dummy_read_1_table;
1061 	struct amdgpu_device *adev = smu->adev;
1062 	int ret = 0;
1063 
1064 	if (!dummy_read_1_table->size)
1065 		return 0;
1066 
1067 	ret = amdgpu_bo_create_kernel(adev,
1068 				      dummy_read_1_table->size,
1069 				      dummy_read_1_table->align,
1070 				      dummy_read_1_table->domain,
1071 				      &dummy_read_1_table->bo,
1072 				      &dummy_read_1_table->mc_address,
1073 				      &dummy_read_1_table->cpu_addr);
1074 	if (ret)
1075 		dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
1076 
1077 	return ret;
1078 }
1079 
smu_free_dummy_read_table(struct smu_context * smu)1080 static void smu_free_dummy_read_table(struct smu_context *smu)
1081 {
1082 	struct smu_table_context *smu_table = &smu->smu_table;
1083 	struct smu_table *dummy_read_1_table =
1084 			&smu_table->dummy_read_1_table;
1085 
1086 
1087 	amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
1088 			      &dummy_read_1_table->mc_address,
1089 			      &dummy_read_1_table->cpu_addr);
1090 
1091 	memset(dummy_read_1_table, 0, sizeof(struct smu_table));
1092 }
1093 
smu_smc_table_sw_init(struct smu_context * smu)1094 static int smu_smc_table_sw_init(struct smu_context *smu)
1095 {
1096 	int ret;
1097 
1098 	/**
1099 	 * Create smu_table structure, and init smc tables such as
1100 	 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
1101 	 */
1102 	ret = smu_init_smc_tables(smu);
1103 	if (ret) {
1104 		dev_err(smu->adev->dev, "Failed to init smc tables!\n");
1105 		return ret;
1106 	}
1107 
1108 	/**
1109 	 * Create smu_power_context structure, and allocate smu_dpm_context and
1110 	 * context size to fill the smu_power_context data.
1111 	 */
1112 	ret = smu_init_power(smu);
1113 	if (ret) {
1114 		dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
1115 		return ret;
1116 	}
1117 
1118 	/*
1119 	 * allocate vram bos to store smc table contents.
1120 	 */
1121 	ret = smu_init_fb_allocations(smu);
1122 	if (ret)
1123 		return ret;
1124 
1125 	ret = smu_alloc_memory_pool(smu);
1126 	if (ret)
1127 		return ret;
1128 
1129 	ret = smu_alloc_dummy_read_table(smu);
1130 	if (ret)
1131 		return ret;
1132 
1133 	ret = smu_i2c_init(smu);
1134 	if (ret)
1135 		return ret;
1136 
1137 	return 0;
1138 }
1139 
smu_smc_table_sw_fini(struct smu_context * smu)1140 static int smu_smc_table_sw_fini(struct smu_context *smu)
1141 {
1142 	int ret;
1143 
1144 	smu_i2c_fini(smu);
1145 
1146 	smu_free_dummy_read_table(smu);
1147 
1148 	ret = smu_free_memory_pool(smu);
1149 	if (ret)
1150 		return ret;
1151 
1152 	ret = smu_fini_fb_allocations(smu);
1153 	if (ret)
1154 		return ret;
1155 
1156 	ret = smu_fini_power(smu);
1157 	if (ret) {
1158 		dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
1159 		return ret;
1160 	}
1161 
1162 	ret = smu_fini_smc_tables(smu);
1163 	if (ret) {
1164 		dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
1165 		return ret;
1166 	}
1167 
1168 	return 0;
1169 }
1170 
smu_throttling_logging_work_fn(struct work_struct * work)1171 static void smu_throttling_logging_work_fn(struct work_struct *work)
1172 {
1173 	struct smu_context *smu = container_of(work, struct smu_context,
1174 					       throttling_logging_work);
1175 
1176 	smu_log_thermal_throttling(smu);
1177 }
1178 
smu_interrupt_work_fn(struct work_struct * work)1179 static void smu_interrupt_work_fn(struct work_struct *work)
1180 {
1181 	struct smu_context *smu = container_of(work, struct smu_context,
1182 					       interrupt_work);
1183 
1184 	if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
1185 		smu->ppt_funcs->interrupt_work(smu);
1186 }
1187 
smu_swctf_delayed_work_handler(struct work_struct * work)1188 static void smu_swctf_delayed_work_handler(struct work_struct *work)
1189 {
1190 	struct smu_context *smu =
1191 		container_of(work, struct smu_context, swctf_delayed_work.work);
1192 	struct smu_temperature_range *range =
1193 				&smu->thermal_range;
1194 	struct amdgpu_device *adev = smu->adev;
1195 	uint32_t hotspot_tmp, size;
1196 
1197 	/*
1198 	 * If the hotspot temperature is confirmed as below SW CTF setting point
1199 	 * after the delay enforced, nothing will be done.
1200 	 * Otherwise, a graceful shutdown will be performed to prevent further damage.
1201 	 */
1202 	if (range->software_shutdown_temp &&
1203 	    smu->ppt_funcs->read_sensor &&
1204 	    !smu->ppt_funcs->read_sensor(smu,
1205 					 AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1206 					 &hotspot_tmp,
1207 					 &size) &&
1208 	    hotspot_tmp / 1000 < range->software_shutdown_temp)
1209 		return;
1210 
1211 	dev_emerg(adev->dev, "ERROR: GPU over temperature range(SW CTF) detected!\n");
1212 	dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU SW CTF!\n");
1213 	orderly_poweroff(true);
1214 }
1215 
smu_init_xgmi_plpd_mode(struct smu_context * smu)1216 static void smu_init_xgmi_plpd_mode(struct smu_context *smu)
1217 {
1218 	struct smu_dpm_context *dpm_ctxt = &(smu->smu_dpm);
1219 	struct smu_dpm_policy_ctxt *policy_ctxt;
1220 	struct smu_dpm_policy *policy;
1221 
1222 	policy = smu_get_pm_policy(smu, PP_PM_POLICY_XGMI_PLPD);
1223 	if (amdgpu_ip_version(smu->adev, MP1_HWIP, 0) == IP_VERSION(11, 0, 2)) {
1224 		if (policy)
1225 			policy->current_level = XGMI_PLPD_DEFAULT;
1226 		return;
1227 	}
1228 
1229 	/* PMFW put PLPD into default policy after enabling the feature */
1230 	if (smu_feature_is_enabled(smu,
1231 				   SMU_FEATURE_XGMI_PER_LINK_PWR_DWN_BIT)) {
1232 		if (policy)
1233 			policy->current_level = XGMI_PLPD_DEFAULT;
1234 	} else {
1235 		policy_ctxt = dpm_ctxt->dpm_policies;
1236 		if (policy_ctxt)
1237 			policy_ctxt->policy_mask &=
1238 				~BIT(PP_PM_POLICY_XGMI_PLPD);
1239 	}
1240 }
1241 
smu_is_workload_profile_available(struct smu_context * smu,u32 profile)1242 static bool smu_is_workload_profile_available(struct smu_context *smu,
1243 					      u32 profile)
1244 {
1245 	if (profile >= PP_SMC_POWER_PROFILE_COUNT)
1246 		return false;
1247 	return smu->workload_map && smu->workload_map[profile].valid_mapping;
1248 }
1249 
smu_sw_init(void * handle)1250 static int smu_sw_init(void *handle)
1251 {
1252 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1253 	struct smu_context *smu = adev->powerplay.pp_handle;
1254 	int ret;
1255 
1256 	smu->pool_size = adev->pm.smu_prv_buffer_size;
1257 	smu->smu_feature.feature_num = SMU_FEATURE_MAX;
1258 	bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
1259 	bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1260 
1261 	INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
1262 	INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
1263 	atomic64_set(&smu->throttle_int_counter, 0);
1264 	smu->watermarks_bitmap = 0;
1265 
1266 	atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
1267 	atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
1268 	atomic_set(&smu->smu_power.power_gate.vpe_gated, 1);
1269 	atomic_set(&smu->smu_power.power_gate.umsch_mm_gated, 1);
1270 
1271 	if (smu->is_apu ||
1272 	    !smu_is_workload_profile_available(smu, PP_SMC_POWER_PROFILE_FULLSCREEN3D))
1273 		smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1274 	else
1275 		smu->power_profile_mode = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1276 	smu_power_profile_mode_get(smu, smu->power_profile_mode);
1277 
1278 	smu->display_config = &adev->pm.pm_display_cfg;
1279 
1280 	smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1281 	smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1282 
1283 	INIT_DELAYED_WORK(&smu->swctf_delayed_work,
1284 			  smu_swctf_delayed_work_handler);
1285 
1286 	ret = smu_smc_table_sw_init(smu);
1287 	if (ret) {
1288 		dev_err(adev->dev, "Failed to sw init smc table!\n");
1289 		return ret;
1290 	}
1291 
1292 	/* get boot_values from vbios to set revision, gfxclk, and etc. */
1293 	ret = smu_get_vbios_bootup_values(smu);
1294 	if (ret) {
1295 		dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1296 		return ret;
1297 	}
1298 
1299 	ret = smu_init_pptable_microcode(smu);
1300 	if (ret) {
1301 		dev_err(adev->dev, "Failed to setup pptable firmware!\n");
1302 		return ret;
1303 	}
1304 
1305 	ret = smu_register_irq_handler(smu);
1306 	if (ret) {
1307 		dev_err(adev->dev, "Failed to register smc irq handler!\n");
1308 		return ret;
1309 	}
1310 
1311 	/* If there is no way to query fan control mode, fan control is not supported */
1312 	if (!smu->ppt_funcs->get_fan_control_mode)
1313 		smu->adev->pm.no_fan = true;
1314 
1315 	return 0;
1316 }
1317 
smu_sw_fini(void * handle)1318 static int smu_sw_fini(void *handle)
1319 {
1320 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1321 	struct smu_context *smu = adev->powerplay.pp_handle;
1322 	int ret;
1323 
1324 	ret = smu_smc_table_sw_fini(smu);
1325 	if (ret) {
1326 		dev_err(adev->dev, "Failed to sw fini smc table!\n");
1327 		return ret;
1328 	}
1329 
1330 	if (smu->custom_profile_params) {
1331 		kfree(smu->custom_profile_params);
1332 		smu->custom_profile_params = NULL;
1333 	}
1334 
1335 	smu_fini_microcode(smu);
1336 
1337 	return 0;
1338 }
1339 
smu_get_thermal_temperature_range(struct smu_context * smu)1340 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1341 {
1342 	struct amdgpu_device *adev = smu->adev;
1343 	struct smu_temperature_range *range =
1344 				&smu->thermal_range;
1345 	int ret = 0;
1346 
1347 	if (!smu->ppt_funcs->get_thermal_temperature_range)
1348 		return 0;
1349 
1350 	ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1351 	if (ret)
1352 		return ret;
1353 
1354 	adev->pm.dpm.thermal.min_temp = range->min;
1355 	adev->pm.dpm.thermal.max_temp = range->max;
1356 	adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1357 	adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1358 	adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1359 	adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1360 	adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1361 	adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1362 	adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1363 
1364 	return ret;
1365 }
1366 
1367 /**
1368  * smu_wbrf_handle_exclusion_ranges - consume the wbrf exclusion ranges
1369  *
1370  * @smu: smu_context pointer
1371  *
1372  * Retrieve the wbrf exclusion ranges and send them to PMFW for proper handling.
1373  * Returns 0 on success, error on failure.
1374  */
smu_wbrf_handle_exclusion_ranges(struct smu_context * smu)1375 static int smu_wbrf_handle_exclusion_ranges(struct smu_context *smu)
1376 {
1377 	struct wbrf_ranges_in_out wbrf_exclusion = {0};
1378 	struct freq_band_range *wifi_bands = wbrf_exclusion.band_list;
1379 	struct amdgpu_device *adev = smu->adev;
1380 	uint32_t num_of_wbrf_ranges = MAX_NUM_OF_WBRF_RANGES;
1381 	uint64_t start, end;
1382 	int ret, i, j;
1383 
1384 	ret = amd_wbrf_retrieve_freq_band(adev->dev, &wbrf_exclusion);
1385 	if (ret) {
1386 		dev_err(adev->dev, "Failed to retrieve exclusion ranges!\n");
1387 		return ret;
1388 	}
1389 
1390 	/*
1391 	 * The exclusion ranges array we got might be filled with holes and duplicate
1392 	 * entries. For example:
1393 	 * {(2400, 2500), (0, 0), (6882, 6962), (2400, 2500), (0, 0), (6117, 6189), (0, 0)...}
1394 	 * We need to do some sortups to eliminate those holes and duplicate entries.
1395 	 * Expected output: {(2400, 2500), (6117, 6189), (6882, 6962), (0, 0)...}
1396 	 */
1397 	for (i = 0; i < num_of_wbrf_ranges; i++) {
1398 		start = wifi_bands[i].start;
1399 		end = wifi_bands[i].end;
1400 
1401 		/* get the last valid entry to fill the intermediate hole */
1402 		if (!start && !end) {
1403 			for (j = num_of_wbrf_ranges - 1; j > i; j--)
1404 				if (wifi_bands[j].start && wifi_bands[j].end)
1405 					break;
1406 
1407 			/* no valid entry left */
1408 			if (j <= i)
1409 				break;
1410 
1411 			start = wifi_bands[i].start = wifi_bands[j].start;
1412 			end = wifi_bands[i].end = wifi_bands[j].end;
1413 			wifi_bands[j].start = 0;
1414 			wifi_bands[j].end = 0;
1415 			num_of_wbrf_ranges = j;
1416 		}
1417 
1418 		/* eliminate duplicate entries */
1419 		for (j = i + 1; j < num_of_wbrf_ranges; j++) {
1420 			if ((wifi_bands[j].start == start) && (wifi_bands[j].end == end)) {
1421 				wifi_bands[j].start = 0;
1422 				wifi_bands[j].end = 0;
1423 			}
1424 		}
1425 	}
1426 
1427 	/* Send the sorted wifi_bands to PMFW */
1428 	ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);
1429 	/* Try to set the wifi_bands again */
1430 	if (unlikely(ret == -EBUSY)) {
1431 		mdelay(5);
1432 		ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);
1433 	}
1434 
1435 	return ret;
1436 }
1437 
1438 /**
1439  * smu_wbrf_event_handler - handle notify events
1440  *
1441  * @nb: notifier block
1442  * @action: event type
1443  * @_arg: event data
1444  *
1445  * Calls relevant amdgpu function in response to wbrf event
1446  * notification from kernel.
1447  */
smu_wbrf_event_handler(struct notifier_block * nb,unsigned long action,void * _arg)1448 static int smu_wbrf_event_handler(struct notifier_block *nb,
1449 				  unsigned long action, void *_arg)
1450 {
1451 	struct smu_context *smu = container_of(nb, struct smu_context, wbrf_notifier);
1452 
1453 	switch (action) {
1454 	case WBRF_CHANGED:
1455 		schedule_delayed_work(&smu->wbrf_delayed_work,
1456 				      msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));
1457 		break;
1458 	default:
1459 		return NOTIFY_DONE;
1460 	}
1461 
1462 	return NOTIFY_OK;
1463 }
1464 
1465 /**
1466  * smu_wbrf_delayed_work_handler - callback on delayed work timer expired
1467  *
1468  * @work: struct work_struct pointer
1469  *
1470  * Flood is over and driver will consume the latest exclusion ranges.
1471  */
smu_wbrf_delayed_work_handler(struct work_struct * work)1472 static void smu_wbrf_delayed_work_handler(struct work_struct *work)
1473 {
1474 	struct smu_context *smu = container_of(work, struct smu_context, wbrf_delayed_work.work);
1475 
1476 	smu_wbrf_handle_exclusion_ranges(smu);
1477 }
1478 
1479 /**
1480  * smu_wbrf_support_check - check wbrf support
1481  *
1482  * @smu: smu_context pointer
1483  *
1484  * Verifies the ACPI interface whether wbrf is supported.
1485  */
smu_wbrf_support_check(struct smu_context * smu)1486 static void smu_wbrf_support_check(struct smu_context *smu)
1487 {
1488 	struct amdgpu_device *adev = smu->adev;
1489 
1490 	smu->wbrf_supported = smu_is_asic_wbrf_supported(smu) && amdgpu_wbrf &&
1491 							acpi_amd_wbrf_supported_consumer(adev->dev);
1492 
1493 	if (smu->wbrf_supported)
1494 		dev_info(adev->dev, "RF interference mitigation is supported\n");
1495 }
1496 
1497 /**
1498  * smu_wbrf_init - init driver wbrf support
1499  *
1500  * @smu: smu_context pointer
1501  *
1502  * Verifies the AMD ACPI interfaces and registers with the wbrf
1503  * notifier chain if wbrf feature is supported.
1504  * Returns 0 on success, error on failure.
1505  */
smu_wbrf_init(struct smu_context * smu)1506 static int smu_wbrf_init(struct smu_context *smu)
1507 {
1508 	int ret;
1509 
1510 	if (!smu->wbrf_supported)
1511 		return 0;
1512 
1513 	INIT_DELAYED_WORK(&smu->wbrf_delayed_work, smu_wbrf_delayed_work_handler);
1514 
1515 	smu->wbrf_notifier.notifier_call = smu_wbrf_event_handler;
1516 	ret = amd_wbrf_register_notifier(&smu->wbrf_notifier);
1517 	if (ret)
1518 		return ret;
1519 
1520 	/*
1521 	 * Some wifiband exclusion ranges may be already there
1522 	 * before our driver loaded. To make sure our driver
1523 	 * is awared of those exclusion ranges.
1524 	 */
1525 	schedule_delayed_work(&smu->wbrf_delayed_work,
1526 			      msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));
1527 
1528 	return 0;
1529 }
1530 
1531 /**
1532  * smu_wbrf_fini - tear down driver wbrf support
1533  *
1534  * @smu: smu_context pointer
1535  *
1536  * Unregisters with the wbrf notifier chain.
1537  */
smu_wbrf_fini(struct smu_context * smu)1538 static void smu_wbrf_fini(struct smu_context *smu)
1539 {
1540 	if (!smu->wbrf_supported)
1541 		return;
1542 
1543 	amd_wbrf_unregister_notifier(&smu->wbrf_notifier);
1544 
1545 	cancel_delayed_work_sync(&smu->wbrf_delayed_work);
1546 }
1547 
smu_smc_hw_setup(struct smu_context * smu)1548 static int smu_smc_hw_setup(struct smu_context *smu)
1549 {
1550 	struct smu_feature *feature = &smu->smu_feature;
1551 	struct amdgpu_device *adev = smu->adev;
1552 	uint8_t pcie_gen = 0, pcie_width = 0;
1553 	uint64_t features_supported;
1554 	int ret = 0;
1555 
1556 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1557 	case IP_VERSION(11, 0, 7):
1558 	case IP_VERSION(11, 0, 11):
1559 	case IP_VERSION(11, 5, 0):
1560 	case IP_VERSION(11, 0, 12):
1561 		if (adev->in_suspend && smu_is_dpm_running(smu)) {
1562 			dev_info(adev->dev, "dpm has been enabled\n");
1563 			ret = smu_system_features_control(smu, true);
1564 			if (ret)
1565 				dev_err(adev->dev, "Failed system features control!\n");
1566 			return ret;
1567 		}
1568 		break;
1569 	default:
1570 		break;
1571 	}
1572 
1573 	ret = smu_init_display_count(smu, 0);
1574 	if (ret) {
1575 		dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1576 		return ret;
1577 	}
1578 
1579 	ret = smu_set_driver_table_location(smu);
1580 	if (ret) {
1581 		dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1582 		return ret;
1583 	}
1584 
1585 	/*
1586 	 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1587 	 */
1588 	ret = smu_set_tool_table_location(smu);
1589 	if (ret) {
1590 		dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1591 		return ret;
1592 	}
1593 
1594 	/*
1595 	 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1596 	 * pool location.
1597 	 */
1598 	ret = smu_notify_memory_pool_location(smu);
1599 	if (ret) {
1600 		dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1601 		return ret;
1602 	}
1603 
1604 	/*
1605 	 * It is assumed the pptable used before runpm is same as
1606 	 * the one used afterwards. Thus, we can reuse the stored
1607 	 * copy and do not need to resetup the pptable again.
1608 	 */
1609 	if (!adev->in_runpm) {
1610 		ret = smu_setup_pptable(smu);
1611 		if (ret) {
1612 			dev_err(adev->dev, "Failed to setup pptable!\n");
1613 			return ret;
1614 		}
1615 	}
1616 
1617 	/* smu_dump_pptable(smu); */
1618 
1619 	/*
1620 	 * With SCPM enabled, PSP is responsible for the PPTable transferring
1621 	 * (to SMU). Driver involvement is not needed and permitted.
1622 	 */
1623 	if (!adev->scpm_enabled) {
1624 		/*
1625 		 * Copy pptable bo in the vram to smc with SMU MSGs such as
1626 		 * SetDriverDramAddr and TransferTableDram2Smu.
1627 		 */
1628 		ret = smu_write_pptable(smu);
1629 		if (ret) {
1630 			dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1631 			return ret;
1632 		}
1633 	}
1634 
1635 	/* issue Run*Btc msg */
1636 	ret = smu_run_btc(smu);
1637 	if (ret)
1638 		return ret;
1639 
1640 	/* Enable UclkShadow on wbrf supported */
1641 	if (smu->wbrf_supported) {
1642 		ret = smu_enable_uclk_shadow(smu, true);
1643 		if (ret) {
1644 			dev_err(adev->dev, "Failed to enable UclkShadow feature to support wbrf!\n");
1645 			return ret;
1646 		}
1647 	}
1648 
1649 	/*
1650 	 * With SCPM enabled, these actions(and relevant messages) are
1651 	 * not needed and permitted.
1652 	 */
1653 	if (!adev->scpm_enabled) {
1654 		ret = smu_feature_set_allowed_mask(smu);
1655 		if (ret) {
1656 			dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1657 			return ret;
1658 		}
1659 	}
1660 
1661 	ret = smu_system_features_control(smu, true);
1662 	if (ret) {
1663 		dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1664 		return ret;
1665 	}
1666 
1667 	smu_init_xgmi_plpd_mode(smu);
1668 
1669 	ret = smu_feature_get_enabled_mask(smu, &features_supported);
1670 	if (ret) {
1671 		dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");
1672 		return ret;
1673 	}
1674 	bitmap_copy(feature->supported,
1675 		    (unsigned long *)&features_supported,
1676 		    feature->feature_num);
1677 
1678 	if (!smu_is_dpm_running(smu))
1679 		dev_info(adev->dev, "dpm has been disabled\n");
1680 
1681 	/*
1682 	 * Set initialized values (get from vbios) to dpm tables context such as
1683 	 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1684 	 * type of clks.
1685 	 */
1686 	ret = smu_set_default_dpm_table(smu);
1687 	if (ret) {
1688 		dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1689 		return ret;
1690 	}
1691 
1692 	if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5)
1693 		pcie_gen = 4;
1694 	else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1695 		pcie_gen = 3;
1696 	else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1697 		pcie_gen = 2;
1698 	else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1699 		pcie_gen = 1;
1700 	else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1701 		pcie_gen = 0;
1702 
1703 	/* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1704 	 * Bit 15:8:  PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1705 	 * Bit 7:0:   PCIE lane width, 1 to 7 corresponds is x1 to x32
1706 	 */
1707 	if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X32)
1708 		pcie_width = 7;
1709 	else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1710 		pcie_width = 6;
1711 	else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1712 		pcie_width = 5;
1713 	else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1714 		pcie_width = 4;
1715 	else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1716 		pcie_width = 3;
1717 	else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1718 		pcie_width = 2;
1719 	else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1720 		pcie_width = 1;
1721 	ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1722 	if (ret) {
1723 		dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1724 		return ret;
1725 	}
1726 
1727 	ret = smu_get_thermal_temperature_range(smu);
1728 	if (ret) {
1729 		dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1730 		return ret;
1731 	}
1732 
1733 	ret = smu_enable_thermal_alert(smu);
1734 	if (ret) {
1735 	  dev_err(adev->dev, "Failed to enable thermal alert!\n");
1736 	  return ret;
1737 	}
1738 
1739 	ret = smu_notify_display_change(smu);
1740 	if (ret) {
1741 		dev_err(adev->dev, "Failed to notify display change!\n");
1742 		return ret;
1743 	}
1744 
1745 	/*
1746 	 * Set min deep sleep dce fclk with bootup value from vbios via
1747 	 * SetMinDeepSleepDcefclk MSG.
1748 	 */
1749 	ret = smu_set_min_dcef_deep_sleep(smu,
1750 					  smu->smu_table.boot_values.dcefclk / 100);
1751 	if (ret) {
1752 		dev_err(adev->dev, "Error setting min deepsleep dcefclk\n");
1753 		return ret;
1754 	}
1755 
1756 	/* Init wbrf support. Properly setup the notifier */
1757 	ret = smu_wbrf_init(smu);
1758 	if (ret)
1759 		dev_err(adev->dev, "Error during wbrf init call\n");
1760 
1761 	return ret;
1762 }
1763 
smu_start_smc_engine(struct smu_context * smu)1764 static int smu_start_smc_engine(struct smu_context *smu)
1765 {
1766 	struct amdgpu_device *adev = smu->adev;
1767 	int ret = 0;
1768 
1769 	smu->smc_fw_state = SMU_FW_INIT;
1770 
1771 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1772 		if (amdgpu_ip_version(adev, MP1_HWIP, 0) < IP_VERSION(11, 0, 0)) {
1773 			if (smu->ppt_funcs->load_microcode) {
1774 				ret = smu->ppt_funcs->load_microcode(smu);
1775 				if (ret)
1776 					return ret;
1777 			}
1778 		}
1779 	}
1780 
1781 	if (smu->ppt_funcs->check_fw_status) {
1782 		ret = smu->ppt_funcs->check_fw_status(smu);
1783 		if (ret) {
1784 			dev_err(adev->dev, "SMC is not ready\n");
1785 			return ret;
1786 		}
1787 	}
1788 
1789 	/*
1790 	 * Send msg GetDriverIfVersion to check if the return value is equal
1791 	 * with DRIVER_IF_VERSION of smc header.
1792 	 */
1793 	ret = smu_check_fw_version(smu);
1794 	if (ret)
1795 		return ret;
1796 
1797 	return ret;
1798 }
1799 
smu_hw_init(void * handle)1800 static int smu_hw_init(void *handle)
1801 {
1802 	int ret;
1803 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1804 	struct smu_context *smu = adev->powerplay.pp_handle;
1805 
1806 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1807 		smu->pm_enabled = false;
1808 		return 0;
1809 	}
1810 
1811 	ret = smu_start_smc_engine(smu);
1812 	if (ret) {
1813 		dev_err(adev->dev, "SMC engine is not correctly up!\n");
1814 		return ret;
1815 	}
1816 
1817 	/*
1818 	 * Check whether wbrf is supported. This needs to be done
1819 	 * before SMU setup starts since part of SMU configuration
1820 	 * relies on this.
1821 	 */
1822 	smu_wbrf_support_check(smu);
1823 
1824 	if (smu->is_apu) {
1825 		ret = smu_set_gfx_imu_enable(smu);
1826 		if (ret)
1827 			return ret;
1828 		smu_dpm_set_vcn_enable(smu, true);
1829 		smu_dpm_set_jpeg_enable(smu, true);
1830 		smu_dpm_set_vpe_enable(smu, true);
1831 		smu_dpm_set_umsch_mm_enable(smu, true);
1832 		smu_set_mall_enable(smu);
1833 		smu_set_gfx_cgpg(smu, true);
1834 	}
1835 
1836 	if (!smu->pm_enabled)
1837 		return 0;
1838 
1839 	ret = smu_get_driver_allowed_feature_mask(smu);
1840 	if (ret)
1841 		return ret;
1842 
1843 	ret = smu_smc_hw_setup(smu);
1844 	if (ret) {
1845 		dev_err(adev->dev, "Failed to setup smc hw!\n");
1846 		return ret;
1847 	}
1848 
1849 	/*
1850 	 * Move maximum sustainable clock retrieving here considering
1851 	 * 1. It is not needed on resume(from S3).
1852 	 * 2. DAL settings come between .hw_init and .late_init of SMU.
1853 	 *    And DAL needs to know the maximum sustainable clocks. Thus
1854 	 *    it cannot be put in .late_init().
1855 	 */
1856 	ret = smu_init_max_sustainable_clocks(smu);
1857 	if (ret) {
1858 		dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1859 		return ret;
1860 	}
1861 
1862 	adev->pm.dpm_enabled = true;
1863 
1864 	dev_info(adev->dev, "SMU is initialized successfully!\n");
1865 
1866 	return 0;
1867 }
1868 
smu_disable_dpms(struct smu_context * smu)1869 static int smu_disable_dpms(struct smu_context *smu)
1870 {
1871 	struct amdgpu_device *adev = smu->adev;
1872 	int ret = 0;
1873 	bool use_baco = !smu->is_apu &&
1874 		((amdgpu_in_reset(adev) &&
1875 		  (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1876 		 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1877 
1878 	/*
1879 	 * For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others)
1880 	 * properly on suspend/reset/unload. Driver involvement may cause some unexpected issues.
1881 	 */
1882 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1883 	case IP_VERSION(13, 0, 0):
1884 	case IP_VERSION(13, 0, 7):
1885 	case IP_VERSION(13, 0, 10):
1886 	case IP_VERSION(14, 0, 2):
1887 	case IP_VERSION(14, 0, 3):
1888 		return 0;
1889 	default:
1890 		break;
1891 	}
1892 
1893 	/*
1894 	 * For custom pptable uploading, skip the DPM features
1895 	 * disable process on Navi1x ASICs.
1896 	 *   - As the gfx related features are under control of
1897 	 *     RLC on those ASICs. RLC reinitialization will be
1898 	 *     needed to reenable them. That will cost much more
1899 	 *     efforts.
1900 	 *
1901 	 *   - SMU firmware can handle the DPM reenablement
1902 	 *     properly.
1903 	 */
1904 	if (smu->uploading_custom_pp_table) {
1905 		switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1906 		case IP_VERSION(11, 0, 0):
1907 		case IP_VERSION(11, 0, 5):
1908 		case IP_VERSION(11, 0, 9):
1909 		case IP_VERSION(11, 0, 7):
1910 		case IP_VERSION(11, 0, 11):
1911 		case IP_VERSION(11, 5, 0):
1912 		case IP_VERSION(11, 0, 12):
1913 		case IP_VERSION(11, 0, 13):
1914 			return 0;
1915 		default:
1916 			break;
1917 		}
1918 	}
1919 
1920 	/*
1921 	 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1922 	 * on BACO in. Driver involvement is unnecessary.
1923 	 */
1924 	if (use_baco) {
1925 		switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1926 		case IP_VERSION(11, 0, 7):
1927 		case IP_VERSION(11, 0, 0):
1928 		case IP_VERSION(11, 0, 5):
1929 		case IP_VERSION(11, 0, 9):
1930 		case IP_VERSION(13, 0, 7):
1931 			return 0;
1932 		default:
1933 			break;
1934 		}
1935 	}
1936 
1937 	/*
1938 	 * For GFX11 and subsequent APUs, PMFW will handle the features disablement properly
1939 	 * for gpu reset and S0i3 cases. Driver involvement is unnecessary.
1940 	 */
1941 	if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) >= 11 &&
1942 	    smu->is_apu && (amdgpu_in_reset(adev) || adev->in_s0ix))
1943 		return 0;
1944 
1945 	/*
1946 	 * For gpu reset, runpm and hibernation through BACO,
1947 	 * BACO feature has to be kept enabled.
1948 	 */
1949 	if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1950 		ret = smu_disable_all_features_with_exception(smu,
1951 							      SMU_FEATURE_BACO_BIT);
1952 		if (ret)
1953 			dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1954 	} else {
1955 		/* DisableAllSmuFeatures message is not permitted with SCPM enabled */
1956 		if (!adev->scpm_enabled) {
1957 			ret = smu_system_features_control(smu, false);
1958 			if (ret)
1959 				dev_err(adev->dev, "Failed to disable smu features.\n");
1960 		}
1961 	}
1962 
1963 	/* Notify SMU RLC is going to be off, stop RLC and SMU interaction.
1964 	 * otherwise SMU will hang while interacting with RLC if RLC is halted
1965 	 * this is a WA for Vangogh asic which fix the SMU hang issue.
1966 	 */
1967 	ret = smu_notify_rlc_state(smu, false);
1968 	if (ret) {
1969 		dev_err(adev->dev, "Fail to notify rlc status!\n");
1970 		return ret;
1971 	}
1972 
1973 	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 2) &&
1974 	    !((adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs) &&
1975 	    !amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop)
1976 		adev->gfx.rlc.funcs->stop(adev);
1977 
1978 	return ret;
1979 }
1980 
smu_smc_hw_cleanup(struct smu_context * smu)1981 static int smu_smc_hw_cleanup(struct smu_context *smu)
1982 {
1983 	struct amdgpu_device *adev = smu->adev;
1984 	int ret = 0;
1985 
1986 	smu_wbrf_fini(smu);
1987 
1988 	cancel_work_sync(&smu->throttling_logging_work);
1989 	cancel_work_sync(&smu->interrupt_work);
1990 
1991 	ret = smu_disable_thermal_alert(smu);
1992 	if (ret) {
1993 		dev_err(adev->dev, "Fail to disable thermal alert!\n");
1994 		return ret;
1995 	}
1996 
1997 	cancel_delayed_work_sync(&smu->swctf_delayed_work);
1998 
1999 	ret = smu_disable_dpms(smu);
2000 	if (ret) {
2001 		dev_err(adev->dev, "Fail to disable dpm features!\n");
2002 		return ret;
2003 	}
2004 
2005 	return 0;
2006 }
2007 
smu_reset_mp1_state(struct smu_context * smu)2008 static int smu_reset_mp1_state(struct smu_context *smu)
2009 {
2010 	struct amdgpu_device *adev = smu->adev;
2011 	int ret = 0;
2012 
2013 	if ((!adev->in_runpm) && (!adev->in_suspend) &&
2014 		(!amdgpu_in_reset(adev)) && amdgpu_ip_version(adev, MP1_HWIP, 0) ==
2015 									IP_VERSION(13, 0, 10) &&
2016 		!amdgpu_device_has_display_hardware(adev))
2017 		ret = smu_set_mp1_state(smu, PP_MP1_STATE_UNLOAD);
2018 
2019 	return ret;
2020 }
2021 
smu_hw_fini(void * handle)2022 static int smu_hw_fini(void *handle)
2023 {
2024 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2025 	struct smu_context *smu = adev->powerplay.pp_handle;
2026 	int ret;
2027 
2028 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2029 		return 0;
2030 
2031 	smu_dpm_set_vcn_enable(smu, false);
2032 	smu_dpm_set_jpeg_enable(smu, false);
2033 	smu_dpm_set_vpe_enable(smu, false);
2034 	smu_dpm_set_umsch_mm_enable(smu, false);
2035 
2036 	adev->vcn.cur_state = AMD_PG_STATE_GATE;
2037 	adev->jpeg.cur_state = AMD_PG_STATE_GATE;
2038 
2039 	if (!smu->pm_enabled)
2040 		return 0;
2041 
2042 	adev->pm.dpm_enabled = false;
2043 
2044 	ret = smu_smc_hw_cleanup(smu);
2045 	if (ret)
2046 		return ret;
2047 
2048 	ret = smu_reset_mp1_state(smu);
2049 	if (ret)
2050 		return ret;
2051 
2052 	return 0;
2053 }
2054 
smu_late_fini(void * handle)2055 static void smu_late_fini(void *handle)
2056 {
2057 	struct amdgpu_device *adev = handle;
2058 	struct smu_context *smu = adev->powerplay.pp_handle;
2059 
2060 	kfree(smu);
2061 }
2062 
smu_reset(struct smu_context * smu)2063 static int smu_reset(struct smu_context *smu)
2064 {
2065 	struct amdgpu_device *adev = smu->adev;
2066 	int ret;
2067 
2068 	ret = smu_hw_fini(adev);
2069 	if (ret)
2070 		return ret;
2071 
2072 	ret = smu_hw_init(adev);
2073 	if (ret)
2074 		return ret;
2075 
2076 	ret = smu_late_init(adev);
2077 	if (ret)
2078 		return ret;
2079 
2080 	return 0;
2081 }
2082 
smu_suspend(void * handle)2083 static int smu_suspend(void *handle)
2084 {
2085 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2086 	struct smu_context *smu = adev->powerplay.pp_handle;
2087 	int ret;
2088 	uint64_t count;
2089 
2090 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2091 		return 0;
2092 
2093 	if (!smu->pm_enabled)
2094 		return 0;
2095 
2096 	adev->pm.dpm_enabled = false;
2097 
2098 	ret = smu_smc_hw_cleanup(smu);
2099 	if (ret)
2100 		return ret;
2101 
2102 	smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
2103 
2104 	smu_set_gfx_cgpg(smu, false);
2105 
2106 	/*
2107 	 * pwfw resets entrycount when device is suspended, so we save the
2108 	 * last value to be used when we resume to keep it consistent
2109 	 */
2110 	ret = smu_get_entrycount_gfxoff(smu, &count);
2111 	if (!ret)
2112 		adev->gfx.gfx_off_entrycount = count;
2113 
2114 	/* clear this on suspend so it will get reprogrammed on resume */
2115 	smu->workload_mask = 0;
2116 
2117 	return 0;
2118 }
2119 
smu_resume(void * handle)2120 static int smu_resume(void *handle)
2121 {
2122 	int ret;
2123 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2124 	struct smu_context *smu = adev->powerplay.pp_handle;
2125 
2126 	if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
2127 		return 0;
2128 
2129 	if (!smu->pm_enabled)
2130 		return 0;
2131 
2132 	dev_info(adev->dev, "SMU is resuming...\n");
2133 
2134 	ret = smu_start_smc_engine(smu);
2135 	if (ret) {
2136 		dev_err(adev->dev, "SMC engine is not correctly up!\n");
2137 		return ret;
2138 	}
2139 
2140 	ret = smu_smc_hw_setup(smu);
2141 	if (ret) {
2142 		dev_err(adev->dev, "Failed to setup smc hw!\n");
2143 		return ret;
2144 	}
2145 
2146 	ret = smu_set_gfx_imu_enable(smu);
2147 	if (ret)
2148 		return ret;
2149 
2150 	smu_set_gfx_cgpg(smu, true);
2151 
2152 	smu->disable_uclk_switch = 0;
2153 
2154 	adev->pm.dpm_enabled = true;
2155 
2156 	if (smu->current_power_limit) {
2157 		ret = smu_set_power_limit(smu, smu->current_power_limit);
2158 		if (ret && ret != -EOPNOTSUPP)
2159 			return ret;
2160 	}
2161 
2162 	dev_info(adev->dev, "SMU is resumed successfully!\n");
2163 
2164 	return 0;
2165 }
2166 
smu_display_configuration_change(void * handle,const struct amd_pp_display_configuration * display_config)2167 static int smu_display_configuration_change(void *handle,
2168 					    const struct amd_pp_display_configuration *display_config)
2169 {
2170 	struct smu_context *smu = handle;
2171 
2172 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2173 		return -EOPNOTSUPP;
2174 
2175 	if (!display_config)
2176 		return -EINVAL;
2177 
2178 	smu_set_min_dcef_deep_sleep(smu,
2179 				    display_config->min_dcef_deep_sleep_set_clk / 100);
2180 
2181 	return 0;
2182 }
2183 
smu_set_clockgating_state(void * handle,enum amd_clockgating_state state)2184 static int smu_set_clockgating_state(void *handle,
2185 				     enum amd_clockgating_state state)
2186 {
2187 	return 0;
2188 }
2189 
smu_set_powergating_state(void * handle,enum amd_powergating_state state)2190 static int smu_set_powergating_state(void *handle,
2191 				     enum amd_powergating_state state)
2192 {
2193 	return 0;
2194 }
2195 
smu_enable_umd_pstate(void * handle,enum amd_dpm_forced_level * level)2196 static int smu_enable_umd_pstate(void *handle,
2197 		      enum amd_dpm_forced_level *level)
2198 {
2199 	uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
2200 					AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
2201 					AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
2202 					AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
2203 
2204 	struct smu_context *smu = (struct smu_context*)(handle);
2205 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2206 
2207 	if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2208 		return -EINVAL;
2209 
2210 	if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
2211 		/* enter umd pstate, save current level, disable gfx cg*/
2212 		if (*level & profile_mode_mask) {
2213 			smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
2214 			smu_gpo_control(smu, false);
2215 			smu_gfx_ulv_control(smu, false);
2216 			smu_deep_sleep_control(smu, false);
2217 			amdgpu_asic_update_umd_stable_pstate(smu->adev, true);
2218 		}
2219 	} else {
2220 		/* exit umd pstate, restore level, enable gfx cg*/
2221 		if (!(*level & profile_mode_mask)) {
2222 			if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
2223 				*level = smu_dpm_ctx->saved_dpm_level;
2224 			amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
2225 			smu_deep_sleep_control(smu, true);
2226 			smu_gfx_ulv_control(smu, true);
2227 			smu_gpo_control(smu, true);
2228 		}
2229 	}
2230 
2231 	return 0;
2232 }
2233 
smu_bump_power_profile_mode(struct smu_context * smu,long * custom_params,u32 custom_params_max_idx)2234 static int smu_bump_power_profile_mode(struct smu_context *smu,
2235 				       long *custom_params,
2236 				       u32 custom_params_max_idx)
2237 {
2238 	u32 workload_mask = 0;
2239 	int i, ret = 0;
2240 
2241 	for (i = 0; i < PP_SMC_POWER_PROFILE_COUNT; i++) {
2242 		if (smu->workload_refcount[i])
2243 			workload_mask |= 1 << i;
2244 	}
2245 
2246 	if (smu->workload_mask == workload_mask)
2247 		return 0;
2248 
2249 	if (smu->ppt_funcs->set_power_profile_mode)
2250 		ret = smu->ppt_funcs->set_power_profile_mode(smu, workload_mask,
2251 							     custom_params,
2252 							     custom_params_max_idx);
2253 
2254 	if (!ret)
2255 		smu->workload_mask = workload_mask;
2256 
2257 	return ret;
2258 }
2259 
smu_power_profile_mode_get(struct smu_context * smu,enum PP_SMC_POWER_PROFILE profile_mode)2260 static void smu_power_profile_mode_get(struct smu_context *smu,
2261 				       enum PP_SMC_POWER_PROFILE profile_mode)
2262 {
2263 	smu->workload_refcount[profile_mode]++;
2264 }
2265 
smu_power_profile_mode_put(struct smu_context * smu,enum PP_SMC_POWER_PROFILE profile_mode)2266 static void smu_power_profile_mode_put(struct smu_context *smu,
2267 				       enum PP_SMC_POWER_PROFILE profile_mode)
2268 {
2269 	if (smu->workload_refcount[profile_mode])
2270 		smu->workload_refcount[profile_mode]--;
2271 }
2272 
smu_adjust_power_state_dynamic(struct smu_context * smu,enum amd_dpm_forced_level level,bool skip_display_settings)2273 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
2274 					  enum amd_dpm_forced_level level,
2275 					  bool skip_display_settings)
2276 {
2277 	int ret = 0;
2278 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2279 
2280 	if (!skip_display_settings) {
2281 		ret = smu_display_config_changed(smu);
2282 		if (ret) {
2283 			dev_err(smu->adev->dev, "Failed to change display config!");
2284 			return ret;
2285 		}
2286 	}
2287 
2288 	ret = smu_apply_clocks_adjust_rules(smu);
2289 	if (ret) {
2290 		dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
2291 		return ret;
2292 	}
2293 
2294 	if (!skip_display_settings) {
2295 		ret = smu_notify_smc_display_config(smu);
2296 		if (ret) {
2297 			dev_err(smu->adev->dev, "Failed to notify smc display config!");
2298 			return ret;
2299 		}
2300 	}
2301 
2302 	if (smu_dpm_ctx->dpm_level != level) {
2303 		ret = smu_asic_set_performance_level(smu, level);
2304 		if (ret) {
2305 			dev_err(smu->adev->dev, "Failed to set performance level!");
2306 			return ret;
2307 		}
2308 
2309 		/* update the saved copy */
2310 		smu_dpm_ctx->dpm_level = level;
2311 	}
2312 
2313 	if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
2314 	    smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
2315 		smu_bump_power_profile_mode(smu, NULL, 0);
2316 
2317 	return ret;
2318 }
2319 
smu_handle_task(struct smu_context * smu,enum amd_dpm_forced_level level,enum amd_pp_task task_id)2320 static int smu_handle_task(struct smu_context *smu,
2321 			   enum amd_dpm_forced_level level,
2322 			   enum amd_pp_task task_id)
2323 {
2324 	int ret = 0;
2325 
2326 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2327 		return -EOPNOTSUPP;
2328 
2329 	switch (task_id) {
2330 	case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
2331 		ret = smu_pre_display_config_changed(smu);
2332 		if (ret)
2333 			return ret;
2334 		ret = smu_adjust_power_state_dynamic(smu, level, false);
2335 		break;
2336 	case AMD_PP_TASK_COMPLETE_INIT:
2337 		ret = smu_adjust_power_state_dynamic(smu, level, true);
2338 		break;
2339 	case AMD_PP_TASK_READJUST_POWER_STATE:
2340 		ret = smu_adjust_power_state_dynamic(smu, level, true);
2341 		break;
2342 	default:
2343 		break;
2344 	}
2345 
2346 	return ret;
2347 }
2348 
smu_handle_dpm_task(void * handle,enum amd_pp_task task_id,enum amd_pm_state_type * user_state)2349 static int smu_handle_dpm_task(void *handle,
2350 			       enum amd_pp_task task_id,
2351 			       enum amd_pm_state_type *user_state)
2352 {
2353 	struct smu_context *smu = handle;
2354 	struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
2355 
2356 	return smu_handle_task(smu, smu_dpm->dpm_level, task_id);
2357 
2358 }
2359 
smu_switch_power_profile(void * handle,enum PP_SMC_POWER_PROFILE type,bool enable)2360 static int smu_switch_power_profile(void *handle,
2361 				    enum PP_SMC_POWER_PROFILE type,
2362 				    bool enable)
2363 {
2364 	struct smu_context *smu = handle;
2365 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2366 	int ret;
2367 
2368 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2369 		return -EOPNOTSUPP;
2370 
2371 	if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
2372 		return -EINVAL;
2373 
2374 	if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
2375 	    smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
2376 		if (enable)
2377 			smu_power_profile_mode_get(smu, type);
2378 		else
2379 			smu_power_profile_mode_put(smu, type);
2380 		ret = smu_bump_power_profile_mode(smu, NULL, 0);
2381 		if (ret) {
2382 			if (enable)
2383 				smu_power_profile_mode_put(smu, type);
2384 			else
2385 				smu_power_profile_mode_get(smu, type);
2386 			return ret;
2387 		}
2388 	}
2389 
2390 	return 0;
2391 }
2392 
smu_get_performance_level(void * handle)2393 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
2394 {
2395 	struct smu_context *smu = handle;
2396 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2397 
2398 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2399 		return -EOPNOTSUPP;
2400 
2401 	if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2402 		return -EINVAL;
2403 
2404 	return smu_dpm_ctx->dpm_level;
2405 }
2406 
smu_force_performance_level(void * handle,enum amd_dpm_forced_level level)2407 static int smu_force_performance_level(void *handle,
2408 				       enum amd_dpm_forced_level level)
2409 {
2410 	struct smu_context *smu = handle;
2411 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2412 	int ret = 0;
2413 
2414 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2415 		return -EOPNOTSUPP;
2416 
2417 	if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2418 		return -EINVAL;
2419 
2420 	ret = smu_enable_umd_pstate(smu, &level);
2421 	if (ret)
2422 		return ret;
2423 
2424 	ret = smu_handle_task(smu, level,
2425 			      AMD_PP_TASK_READJUST_POWER_STATE);
2426 
2427 	/* reset user dpm clock state */
2428 	if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
2429 		memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
2430 		smu->user_dpm_profile.clk_dependency = 0;
2431 	}
2432 
2433 	return ret;
2434 }
2435 
smu_set_display_count(void * handle,uint32_t count)2436 static int smu_set_display_count(void *handle, uint32_t count)
2437 {
2438 	struct smu_context *smu = handle;
2439 
2440 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2441 		return -EOPNOTSUPP;
2442 
2443 	return smu_init_display_count(smu, count);
2444 }
2445 
smu_force_smuclk_levels(struct smu_context * smu,enum smu_clk_type clk_type,uint32_t mask)2446 static int smu_force_smuclk_levels(struct smu_context *smu,
2447 			 enum smu_clk_type clk_type,
2448 			 uint32_t mask)
2449 {
2450 	struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2451 	int ret = 0;
2452 
2453 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2454 		return -EOPNOTSUPP;
2455 
2456 	if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
2457 		dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
2458 		return -EINVAL;
2459 	}
2460 
2461 	if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
2462 		ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
2463 		if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2464 			smu->user_dpm_profile.clk_mask[clk_type] = mask;
2465 			smu_set_user_clk_dependencies(smu, clk_type);
2466 		}
2467 	}
2468 
2469 	return ret;
2470 }
2471 
smu_force_ppclk_levels(void * handle,enum pp_clock_type type,uint32_t mask)2472 static int smu_force_ppclk_levels(void *handle,
2473 				  enum pp_clock_type type,
2474 				  uint32_t mask)
2475 {
2476 	struct smu_context *smu = handle;
2477 	enum smu_clk_type clk_type;
2478 
2479 	switch (type) {
2480 	case PP_SCLK:
2481 		clk_type = SMU_SCLK; break;
2482 	case PP_MCLK:
2483 		clk_type = SMU_MCLK; break;
2484 	case PP_PCIE:
2485 		clk_type = SMU_PCIE; break;
2486 	case PP_SOCCLK:
2487 		clk_type = SMU_SOCCLK; break;
2488 	case PP_FCLK:
2489 		clk_type = SMU_FCLK; break;
2490 	case PP_DCEFCLK:
2491 		clk_type = SMU_DCEFCLK; break;
2492 	case PP_VCLK:
2493 		clk_type = SMU_VCLK; break;
2494 	case PP_VCLK1:
2495 		clk_type = SMU_VCLK1; break;
2496 	case PP_DCLK:
2497 		clk_type = SMU_DCLK; break;
2498 	case PP_DCLK1:
2499 		clk_type = SMU_DCLK1; break;
2500 	case OD_SCLK:
2501 		clk_type = SMU_OD_SCLK; break;
2502 	case OD_MCLK:
2503 		clk_type = SMU_OD_MCLK; break;
2504 	case OD_VDDC_CURVE:
2505 		clk_type = SMU_OD_VDDC_CURVE; break;
2506 	case OD_RANGE:
2507 		clk_type = SMU_OD_RANGE; break;
2508 	default:
2509 		return -EINVAL;
2510 	}
2511 
2512 	return smu_force_smuclk_levels(smu, clk_type, mask);
2513 }
2514 
2515 /*
2516  * On system suspending or resetting, the dpm_enabled
2517  * flag will be cleared. So that those SMU services which
2518  * are not supported will be gated.
2519  * However, the mp1 state setting should still be granted
2520  * even if the dpm_enabled cleared.
2521  */
smu_set_mp1_state(void * handle,enum pp_mp1_state mp1_state)2522 static int smu_set_mp1_state(void *handle,
2523 			     enum pp_mp1_state mp1_state)
2524 {
2525 	struct smu_context *smu = handle;
2526 	int ret = 0;
2527 
2528 	if (!smu->pm_enabled)
2529 		return -EOPNOTSUPP;
2530 
2531 	if (smu->ppt_funcs &&
2532 	    smu->ppt_funcs->set_mp1_state)
2533 		ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
2534 
2535 	return ret;
2536 }
2537 
smu_set_df_cstate(void * handle,enum pp_df_cstate state)2538 static int smu_set_df_cstate(void *handle,
2539 			     enum pp_df_cstate state)
2540 {
2541 	struct smu_context *smu = handle;
2542 	int ret = 0;
2543 
2544 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2545 		return -EOPNOTSUPP;
2546 
2547 	if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
2548 		return 0;
2549 
2550 	ret = smu->ppt_funcs->set_df_cstate(smu, state);
2551 	if (ret)
2552 		dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
2553 
2554 	return ret;
2555 }
2556 
smu_write_watermarks_table(struct smu_context * smu)2557 int smu_write_watermarks_table(struct smu_context *smu)
2558 {
2559 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2560 		return -EOPNOTSUPP;
2561 
2562 	return smu_set_watermarks_table(smu, NULL);
2563 }
2564 
smu_set_watermarks_for_clock_ranges(void * handle,struct pp_smu_wm_range_sets * clock_ranges)2565 static int smu_set_watermarks_for_clock_ranges(void *handle,
2566 					       struct pp_smu_wm_range_sets *clock_ranges)
2567 {
2568 	struct smu_context *smu = handle;
2569 
2570 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2571 		return -EOPNOTSUPP;
2572 
2573 	if (smu->disable_watermark)
2574 		return 0;
2575 
2576 	return smu_set_watermarks_table(smu, clock_ranges);
2577 }
2578 
smu_set_ac_dc(struct smu_context * smu)2579 int smu_set_ac_dc(struct smu_context *smu)
2580 {
2581 	int ret = 0;
2582 
2583 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2584 		return -EOPNOTSUPP;
2585 
2586 	/* controlled by firmware */
2587 	if (smu->dc_controlled_by_gpio)
2588 		return 0;
2589 
2590 	ret = smu_set_power_source(smu,
2591 				   smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
2592 				   SMU_POWER_SOURCE_DC);
2593 	if (ret)
2594 		dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
2595 		       smu->adev->pm.ac_power ? "AC" : "DC");
2596 
2597 	return ret;
2598 }
2599 
2600 const struct amd_ip_funcs smu_ip_funcs = {
2601 	.name = "smu",
2602 	.early_init = smu_early_init,
2603 	.late_init = smu_late_init,
2604 	.sw_init = smu_sw_init,
2605 	.sw_fini = smu_sw_fini,
2606 	.hw_init = smu_hw_init,
2607 	.hw_fini = smu_hw_fini,
2608 	.late_fini = smu_late_fini,
2609 	.suspend = smu_suspend,
2610 	.resume = smu_resume,
2611 	.is_idle = NULL,
2612 	.check_soft_reset = NULL,
2613 	.wait_for_idle = NULL,
2614 	.soft_reset = NULL,
2615 	.set_clockgating_state = smu_set_clockgating_state,
2616 	.set_powergating_state = smu_set_powergating_state,
2617 };
2618 
2619 const struct amdgpu_ip_block_version smu_v11_0_ip_block = {
2620 	.type = AMD_IP_BLOCK_TYPE_SMC,
2621 	.major = 11,
2622 	.minor = 0,
2623 	.rev = 0,
2624 	.funcs = &smu_ip_funcs,
2625 };
2626 
2627 const struct amdgpu_ip_block_version smu_v12_0_ip_block = {
2628 	.type = AMD_IP_BLOCK_TYPE_SMC,
2629 	.major = 12,
2630 	.minor = 0,
2631 	.rev = 0,
2632 	.funcs = &smu_ip_funcs,
2633 };
2634 
2635 const struct amdgpu_ip_block_version smu_v13_0_ip_block = {
2636 	.type = AMD_IP_BLOCK_TYPE_SMC,
2637 	.major = 13,
2638 	.minor = 0,
2639 	.rev = 0,
2640 	.funcs = &smu_ip_funcs,
2641 };
2642 
2643 const struct amdgpu_ip_block_version smu_v14_0_ip_block = {
2644 	.type = AMD_IP_BLOCK_TYPE_SMC,
2645 	.major = 14,
2646 	.minor = 0,
2647 	.rev = 0,
2648 	.funcs = &smu_ip_funcs,
2649 };
2650 
smu_load_microcode(void * handle)2651 static int smu_load_microcode(void *handle)
2652 {
2653 	struct smu_context *smu = handle;
2654 	struct amdgpu_device *adev = smu->adev;
2655 	int ret = 0;
2656 
2657 	if (!smu->pm_enabled)
2658 		return -EOPNOTSUPP;
2659 
2660 	/* This should be used for non PSP loading */
2661 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2662 		return 0;
2663 
2664 	if (smu->ppt_funcs->load_microcode) {
2665 		ret = smu->ppt_funcs->load_microcode(smu);
2666 		if (ret) {
2667 			dev_err(adev->dev, "Load microcode failed\n");
2668 			return ret;
2669 		}
2670 	}
2671 
2672 	if (smu->ppt_funcs->check_fw_status) {
2673 		ret = smu->ppt_funcs->check_fw_status(smu);
2674 		if (ret) {
2675 			dev_err(adev->dev, "SMC is not ready\n");
2676 			return ret;
2677 		}
2678 	}
2679 
2680 	return ret;
2681 }
2682 
smu_set_gfx_cgpg(struct smu_context * smu,bool enabled)2683 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2684 {
2685 	int ret = 0;
2686 
2687 	if (smu->ppt_funcs->set_gfx_cgpg)
2688 		ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2689 
2690 	return ret;
2691 }
2692 
smu_set_fan_speed_rpm(void * handle,uint32_t speed)2693 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2694 {
2695 	struct smu_context *smu = handle;
2696 	int ret = 0;
2697 
2698 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2699 		return -EOPNOTSUPP;
2700 
2701 	if (!smu->ppt_funcs->set_fan_speed_rpm)
2702 		return -EOPNOTSUPP;
2703 
2704 	if (speed == U32_MAX)
2705 		return -EINVAL;
2706 
2707 	ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2708 	if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2709 		smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;
2710 		smu->user_dpm_profile.fan_speed_rpm = speed;
2711 
2712 		/* Override custom PWM setting as they cannot co-exist */
2713 		smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;
2714 		smu->user_dpm_profile.fan_speed_pwm = 0;
2715 	}
2716 
2717 	return ret;
2718 }
2719 
2720 /**
2721  * smu_get_power_limit - Request one of the SMU Power Limits
2722  *
2723  * @handle: pointer to smu context
2724  * @limit: requested limit is written back to this variable
2725  * @pp_limit_level: &pp_power_limit_level which limit of the power to return
2726  * @pp_power_type: &pp_power_type type of power
2727  * Return:  0 on success, <0 on error
2728  *
2729  */
smu_get_power_limit(void * handle,uint32_t * limit,enum pp_power_limit_level pp_limit_level,enum pp_power_type pp_power_type)2730 int smu_get_power_limit(void *handle,
2731 			uint32_t *limit,
2732 			enum pp_power_limit_level pp_limit_level,
2733 			enum pp_power_type pp_power_type)
2734 {
2735 	struct smu_context *smu = handle;
2736 	struct amdgpu_device *adev = smu->adev;
2737 	enum smu_ppt_limit_level limit_level;
2738 	uint32_t limit_type;
2739 	int ret = 0;
2740 
2741 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2742 		return -EOPNOTSUPP;
2743 
2744 	switch (pp_power_type) {
2745 	case PP_PWR_TYPE_SUSTAINED:
2746 		limit_type = SMU_DEFAULT_PPT_LIMIT;
2747 		break;
2748 	case PP_PWR_TYPE_FAST:
2749 		limit_type = SMU_FAST_PPT_LIMIT;
2750 		break;
2751 	default:
2752 		return -EOPNOTSUPP;
2753 	}
2754 
2755 	switch (pp_limit_level) {
2756 	case PP_PWR_LIMIT_CURRENT:
2757 		limit_level = SMU_PPT_LIMIT_CURRENT;
2758 		break;
2759 	case PP_PWR_LIMIT_DEFAULT:
2760 		limit_level = SMU_PPT_LIMIT_DEFAULT;
2761 		break;
2762 	case PP_PWR_LIMIT_MAX:
2763 		limit_level = SMU_PPT_LIMIT_MAX;
2764 		break;
2765 	case PP_PWR_LIMIT_MIN:
2766 		limit_level = SMU_PPT_LIMIT_MIN;
2767 		break;
2768 	default:
2769 		return -EOPNOTSUPP;
2770 	}
2771 
2772 	if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2773 		if (smu->ppt_funcs->get_ppt_limit)
2774 			ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2775 	} else {
2776 		switch (limit_level) {
2777 		case SMU_PPT_LIMIT_CURRENT:
2778 			switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
2779 			case IP_VERSION(13, 0, 2):
2780 			case IP_VERSION(13, 0, 6):
2781 			case IP_VERSION(13, 0, 12):
2782 			case IP_VERSION(13, 0, 14):
2783 			case IP_VERSION(11, 0, 7):
2784 			case IP_VERSION(11, 0, 11):
2785 			case IP_VERSION(11, 0, 12):
2786 			case IP_VERSION(11, 0, 13):
2787 				ret = smu_get_asic_power_limits(smu,
2788 								&smu->current_power_limit,
2789 								NULL, NULL, NULL);
2790 				break;
2791 			default:
2792 				break;
2793 			}
2794 			*limit = smu->current_power_limit;
2795 			break;
2796 		case SMU_PPT_LIMIT_DEFAULT:
2797 			*limit = smu->default_power_limit;
2798 			break;
2799 		case SMU_PPT_LIMIT_MAX:
2800 			*limit = smu->max_power_limit;
2801 			break;
2802 		case SMU_PPT_LIMIT_MIN:
2803 			*limit = smu->min_power_limit;
2804 			break;
2805 		default:
2806 			return -EINVAL;
2807 		}
2808 	}
2809 
2810 	return ret;
2811 }
2812 
smu_set_power_limit(void * handle,uint32_t limit)2813 static int smu_set_power_limit(void *handle, uint32_t limit)
2814 {
2815 	struct smu_context *smu = handle;
2816 	uint32_t limit_type = limit >> 24;
2817 	int ret = 0;
2818 
2819 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2820 		return -EOPNOTSUPP;
2821 
2822 	limit &= (1<<24)-1;
2823 	if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2824 		if (smu->ppt_funcs->set_power_limit)
2825 			return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2826 
2827 	if ((limit > smu->max_power_limit) || (limit < smu->min_power_limit)) {
2828 		dev_err(smu->adev->dev,
2829 			"New power limit (%d) is out of range [%d,%d]\n",
2830 			limit, smu->min_power_limit, smu->max_power_limit);
2831 		return -EINVAL;
2832 	}
2833 
2834 	if (!limit)
2835 		limit = smu->current_power_limit;
2836 
2837 	if (smu->ppt_funcs->set_power_limit) {
2838 		ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2839 		if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2840 			smu->user_dpm_profile.power_limit = limit;
2841 	}
2842 
2843 	return ret;
2844 }
2845 
smu_print_smuclk_levels(struct smu_context * smu,enum smu_clk_type clk_type,char * buf)2846 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2847 {
2848 	int ret = 0;
2849 
2850 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2851 		return -EOPNOTSUPP;
2852 
2853 	if (smu->ppt_funcs->print_clk_levels)
2854 		ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2855 
2856 	return ret;
2857 }
2858 
smu_convert_to_smuclk(enum pp_clock_type type)2859 static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)
2860 {
2861 	enum smu_clk_type clk_type;
2862 
2863 	switch (type) {
2864 	case PP_SCLK:
2865 		clk_type = SMU_SCLK; break;
2866 	case PP_MCLK:
2867 		clk_type = SMU_MCLK; break;
2868 	case PP_PCIE:
2869 		clk_type = SMU_PCIE; break;
2870 	case PP_SOCCLK:
2871 		clk_type = SMU_SOCCLK; break;
2872 	case PP_FCLK:
2873 		clk_type = SMU_FCLK; break;
2874 	case PP_DCEFCLK:
2875 		clk_type = SMU_DCEFCLK; break;
2876 	case PP_VCLK:
2877 		clk_type = SMU_VCLK; break;
2878 	case PP_VCLK1:
2879 		clk_type = SMU_VCLK1; break;
2880 	case PP_DCLK:
2881 		clk_type = SMU_DCLK; break;
2882 	case PP_DCLK1:
2883 		clk_type = SMU_DCLK1; break;
2884 	case OD_SCLK:
2885 		clk_type = SMU_OD_SCLK; break;
2886 	case OD_MCLK:
2887 		clk_type = SMU_OD_MCLK; break;
2888 	case OD_VDDC_CURVE:
2889 		clk_type = SMU_OD_VDDC_CURVE; break;
2890 	case OD_RANGE:
2891 		clk_type = SMU_OD_RANGE; break;
2892 	case OD_VDDGFX_OFFSET:
2893 		clk_type = SMU_OD_VDDGFX_OFFSET; break;
2894 	case OD_CCLK:
2895 		clk_type = SMU_OD_CCLK; break;
2896 	case OD_FAN_CURVE:
2897 		clk_type = SMU_OD_FAN_CURVE; break;
2898 	case OD_ACOUSTIC_LIMIT:
2899 		clk_type = SMU_OD_ACOUSTIC_LIMIT; break;
2900 	case OD_ACOUSTIC_TARGET:
2901 		clk_type = SMU_OD_ACOUSTIC_TARGET; break;
2902 	case OD_FAN_TARGET_TEMPERATURE:
2903 		clk_type = SMU_OD_FAN_TARGET_TEMPERATURE; break;
2904 	case OD_FAN_MINIMUM_PWM:
2905 		clk_type = SMU_OD_FAN_MINIMUM_PWM; break;
2906 	default:
2907 		clk_type = SMU_CLK_COUNT; break;
2908 	}
2909 
2910 	return clk_type;
2911 }
2912 
smu_print_ppclk_levels(void * handle,enum pp_clock_type type,char * buf)2913 static int smu_print_ppclk_levels(void *handle,
2914 				  enum pp_clock_type type,
2915 				  char *buf)
2916 {
2917 	struct smu_context *smu = handle;
2918 	enum smu_clk_type clk_type;
2919 
2920 	clk_type = smu_convert_to_smuclk(type);
2921 	if (clk_type == SMU_CLK_COUNT)
2922 		return -EINVAL;
2923 
2924 	return smu_print_smuclk_levels(smu, clk_type, buf);
2925 }
2926 
smu_emit_ppclk_levels(void * handle,enum pp_clock_type type,char * buf,int * offset)2927 static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)
2928 {
2929 	struct smu_context *smu = handle;
2930 	enum smu_clk_type clk_type;
2931 
2932 	clk_type = smu_convert_to_smuclk(type);
2933 	if (clk_type == SMU_CLK_COUNT)
2934 		return -EINVAL;
2935 
2936 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2937 		return -EOPNOTSUPP;
2938 
2939 	if (!smu->ppt_funcs->emit_clk_levels)
2940 		return -ENOENT;
2941 
2942 	return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);
2943 
2944 }
2945 
smu_od_edit_dpm_table(void * handle,enum PP_OD_DPM_TABLE_COMMAND type,long * input,uint32_t size)2946 static int smu_od_edit_dpm_table(void *handle,
2947 				 enum PP_OD_DPM_TABLE_COMMAND type,
2948 				 long *input, uint32_t size)
2949 {
2950 	struct smu_context *smu = handle;
2951 	int ret = 0;
2952 
2953 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2954 		return -EOPNOTSUPP;
2955 
2956 	if (smu->ppt_funcs->od_edit_dpm_table) {
2957 		ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2958 	}
2959 
2960 	return ret;
2961 }
2962 
smu_read_sensor(void * handle,int sensor,void * data,int * size_arg)2963 static int smu_read_sensor(void *handle,
2964 			   int sensor,
2965 			   void *data,
2966 			   int *size_arg)
2967 {
2968 	struct smu_context *smu = handle;
2969 	struct smu_umd_pstate_table *pstate_table =
2970 				&smu->pstate_table;
2971 	int ret = 0;
2972 	uint32_t *size, size_val;
2973 
2974 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2975 		return -EOPNOTSUPP;
2976 
2977 	if (!data || !size_arg)
2978 		return -EINVAL;
2979 
2980 	size_val = *size_arg;
2981 	size = &size_val;
2982 
2983 	if (smu->ppt_funcs->read_sensor)
2984 		if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2985 			goto unlock;
2986 
2987 	switch (sensor) {
2988 	case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2989 		*((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2990 		*size = 4;
2991 		break;
2992 	case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2993 		*((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2994 		*size = 4;
2995 		break;
2996 	case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
2997 		*((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100;
2998 		*size = 4;
2999 		break;
3000 	case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
3001 		*((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100;
3002 		*size = 4;
3003 		break;
3004 	case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
3005 		ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);
3006 		*size = 8;
3007 		break;
3008 	case AMDGPU_PP_SENSOR_UVD_POWER:
3009 		*(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
3010 		*size = 4;
3011 		break;
3012 	case AMDGPU_PP_SENSOR_VCE_POWER:
3013 		*(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
3014 		*size = 4;
3015 		break;
3016 	case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
3017 		*(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0 : 1;
3018 		*size = 4;
3019 		break;
3020 	case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
3021 		*(uint32_t *)data = 0;
3022 		*size = 4;
3023 		break;
3024 	default:
3025 		*size = 0;
3026 		ret = -EOPNOTSUPP;
3027 		break;
3028 	}
3029 
3030 unlock:
3031 	// assign uint32_t to int
3032 	*size_arg = size_val;
3033 
3034 	return ret;
3035 }
3036 
smu_get_apu_thermal_limit(void * handle,uint32_t * limit)3037 static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit)
3038 {
3039 	int ret = -EOPNOTSUPP;
3040 	struct smu_context *smu = handle;
3041 
3042 	if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit)
3043 		ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit);
3044 
3045 	return ret;
3046 }
3047 
smu_set_apu_thermal_limit(void * handle,uint32_t limit)3048 static int smu_set_apu_thermal_limit(void *handle, uint32_t limit)
3049 {
3050 	int ret = -EOPNOTSUPP;
3051 	struct smu_context *smu = handle;
3052 
3053 	if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit)
3054 		ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit);
3055 
3056 	return ret;
3057 }
3058 
smu_get_power_profile_mode(void * handle,char * buf)3059 static int smu_get_power_profile_mode(void *handle, char *buf)
3060 {
3061 	struct smu_context *smu = handle;
3062 
3063 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
3064 	    !smu->ppt_funcs->get_power_profile_mode)
3065 		return -EOPNOTSUPP;
3066 	if (!buf)
3067 		return -EINVAL;
3068 
3069 	return smu->ppt_funcs->get_power_profile_mode(smu, buf);
3070 }
3071 
smu_set_power_profile_mode(void * handle,long * param,uint32_t param_size)3072 static int smu_set_power_profile_mode(void *handle,
3073 				      long *param,
3074 				      uint32_t param_size)
3075 {
3076 	struct smu_context *smu = handle;
3077 	bool custom = false;
3078 	int ret = 0;
3079 
3080 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
3081 	    !smu->ppt_funcs->set_power_profile_mode)
3082 		return -EOPNOTSUPP;
3083 
3084 	if (param[param_size] == PP_SMC_POWER_PROFILE_CUSTOM) {
3085 		custom = true;
3086 		/* clear frontend mask so custom changes propogate */
3087 		smu->workload_mask = 0;
3088 	}
3089 
3090 	if ((param[param_size] != smu->power_profile_mode) || custom) {
3091 		/* clear the old user preference */
3092 		smu_power_profile_mode_put(smu, smu->power_profile_mode);
3093 		/* set the new user preference */
3094 		smu_power_profile_mode_get(smu, param[param_size]);
3095 		ret = smu_bump_power_profile_mode(smu,
3096 						  custom ? param : NULL,
3097 						  custom ? param_size : 0);
3098 		if (ret)
3099 			smu_power_profile_mode_put(smu, param[param_size]);
3100 		else
3101 			/* store the user's preference */
3102 			smu->power_profile_mode = param[param_size];
3103 	}
3104 
3105 	return ret;
3106 }
3107 
smu_get_fan_control_mode(void * handle,u32 * fan_mode)3108 static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)
3109 {
3110 	struct smu_context *smu = handle;
3111 
3112 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3113 		return -EOPNOTSUPP;
3114 
3115 	if (!smu->ppt_funcs->get_fan_control_mode)
3116 		return -EOPNOTSUPP;
3117 
3118 	if (!fan_mode)
3119 		return -EINVAL;
3120 
3121 	*fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);
3122 
3123 	return 0;
3124 }
3125 
smu_set_fan_control_mode(void * handle,u32 value)3126 static int smu_set_fan_control_mode(void *handle, u32 value)
3127 {
3128 	struct smu_context *smu = handle;
3129 	int ret = 0;
3130 
3131 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3132 		return -EOPNOTSUPP;
3133 
3134 	if (!smu->ppt_funcs->set_fan_control_mode)
3135 		return -EOPNOTSUPP;
3136 
3137 	if (value == U32_MAX)
3138 		return -EINVAL;
3139 
3140 	ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
3141 	if (ret)
3142 		goto out;
3143 
3144 	if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
3145 		smu->user_dpm_profile.fan_mode = value;
3146 
3147 		/* reset user dpm fan speed */
3148 		if (value != AMD_FAN_CTRL_MANUAL) {
3149 			smu->user_dpm_profile.fan_speed_pwm = 0;
3150 			smu->user_dpm_profile.fan_speed_rpm = 0;
3151 			smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);
3152 		}
3153 	}
3154 
3155 out:
3156 	return ret;
3157 }
3158 
smu_get_fan_speed_pwm(void * handle,u32 * speed)3159 static int smu_get_fan_speed_pwm(void *handle, u32 *speed)
3160 {
3161 	struct smu_context *smu = handle;
3162 	int ret = 0;
3163 
3164 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3165 		return -EOPNOTSUPP;
3166 
3167 	if (!smu->ppt_funcs->get_fan_speed_pwm)
3168 		return -EOPNOTSUPP;
3169 
3170 	if (!speed)
3171 		return -EINVAL;
3172 
3173 	ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);
3174 
3175 	return ret;
3176 }
3177 
smu_set_fan_speed_pwm(void * handle,u32 speed)3178 static int smu_set_fan_speed_pwm(void *handle, u32 speed)
3179 {
3180 	struct smu_context *smu = handle;
3181 	int ret = 0;
3182 
3183 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3184 		return -EOPNOTSUPP;
3185 
3186 	if (!smu->ppt_funcs->set_fan_speed_pwm)
3187 		return -EOPNOTSUPP;
3188 
3189 	if (speed == U32_MAX)
3190 		return -EINVAL;
3191 
3192 	ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);
3193 	if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
3194 		smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;
3195 		smu->user_dpm_profile.fan_speed_pwm = speed;
3196 
3197 		/* Override custom RPM setting as they cannot co-exist */
3198 		smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;
3199 		smu->user_dpm_profile.fan_speed_rpm = 0;
3200 	}
3201 
3202 	return ret;
3203 }
3204 
smu_get_fan_speed_rpm(void * handle,uint32_t * speed)3205 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
3206 {
3207 	struct smu_context *smu = handle;
3208 	int ret = 0;
3209 
3210 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3211 		return -EOPNOTSUPP;
3212 
3213 	if (!smu->ppt_funcs->get_fan_speed_rpm)
3214 		return -EOPNOTSUPP;
3215 
3216 	if (!speed)
3217 		return -EINVAL;
3218 
3219 	ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
3220 
3221 	return ret;
3222 }
3223 
smu_set_deep_sleep_dcefclk(void * handle,uint32_t clk)3224 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
3225 {
3226 	struct smu_context *smu = handle;
3227 
3228 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3229 		return -EOPNOTSUPP;
3230 
3231 	return smu_set_min_dcef_deep_sleep(smu, clk);
3232 }
3233 
smu_get_clock_by_type_with_latency(void * handle,enum amd_pp_clock_type type,struct pp_clock_levels_with_latency * clocks)3234 static int smu_get_clock_by_type_with_latency(void *handle,
3235 					      enum amd_pp_clock_type type,
3236 					      struct pp_clock_levels_with_latency *clocks)
3237 {
3238 	struct smu_context *smu = handle;
3239 	enum smu_clk_type clk_type;
3240 	int ret = 0;
3241 
3242 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3243 		return -EOPNOTSUPP;
3244 
3245 	if (smu->ppt_funcs->get_clock_by_type_with_latency) {
3246 		switch (type) {
3247 		case amd_pp_sys_clock:
3248 			clk_type = SMU_GFXCLK;
3249 			break;
3250 		case amd_pp_mem_clock:
3251 			clk_type = SMU_MCLK;
3252 			break;
3253 		case amd_pp_dcef_clock:
3254 			clk_type = SMU_DCEFCLK;
3255 			break;
3256 		case amd_pp_disp_clock:
3257 			clk_type = SMU_DISPCLK;
3258 			break;
3259 		default:
3260 			dev_err(smu->adev->dev, "Invalid clock type!\n");
3261 			return -EINVAL;
3262 		}
3263 
3264 		ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
3265 	}
3266 
3267 	return ret;
3268 }
3269 
smu_display_clock_voltage_request(void * handle,struct pp_display_clock_request * clock_req)3270 static int smu_display_clock_voltage_request(void *handle,
3271 					     struct pp_display_clock_request *clock_req)
3272 {
3273 	struct smu_context *smu = handle;
3274 	int ret = 0;
3275 
3276 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3277 		return -EOPNOTSUPP;
3278 
3279 	if (smu->ppt_funcs->display_clock_voltage_request)
3280 		ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
3281 
3282 	return ret;
3283 }
3284 
3285 
smu_display_disable_memory_clock_switch(void * handle,bool disable_memory_clock_switch)3286 static int smu_display_disable_memory_clock_switch(void *handle,
3287 						   bool disable_memory_clock_switch)
3288 {
3289 	struct smu_context *smu = handle;
3290 	int ret = -EINVAL;
3291 
3292 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3293 		return -EOPNOTSUPP;
3294 
3295 	if (smu->ppt_funcs->display_disable_memory_clock_switch)
3296 		ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
3297 
3298 	return ret;
3299 }
3300 
smu_set_xgmi_pstate(void * handle,uint32_t pstate)3301 static int smu_set_xgmi_pstate(void *handle,
3302 			       uint32_t pstate)
3303 {
3304 	struct smu_context *smu = handle;
3305 	int ret = 0;
3306 
3307 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3308 		return -EOPNOTSUPP;
3309 
3310 	if (smu->ppt_funcs->set_xgmi_pstate)
3311 		ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
3312 
3313 	if (ret)
3314 		dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
3315 
3316 	return ret;
3317 }
3318 
smu_get_baco_capability(void * handle)3319 static int smu_get_baco_capability(void *handle)
3320 {
3321 	struct smu_context *smu = handle;
3322 
3323 	if (!smu->pm_enabled)
3324 		return false;
3325 
3326 	if (!smu->ppt_funcs || !smu->ppt_funcs->get_bamaco_support)
3327 		return false;
3328 
3329 	return smu->ppt_funcs->get_bamaco_support(smu);
3330 }
3331 
smu_baco_set_state(void * handle,int state)3332 static int smu_baco_set_state(void *handle, int state)
3333 {
3334 	struct smu_context *smu = handle;
3335 	int ret = 0;
3336 
3337 	if (!smu->pm_enabled)
3338 		return -EOPNOTSUPP;
3339 
3340 	if (state == 0) {
3341 		if (smu->ppt_funcs->baco_exit)
3342 			ret = smu->ppt_funcs->baco_exit(smu);
3343 	} else if (state == 1) {
3344 		if (smu->ppt_funcs->baco_enter)
3345 			ret = smu->ppt_funcs->baco_enter(smu);
3346 	} else {
3347 		return -EINVAL;
3348 	}
3349 
3350 	if (ret)
3351 		dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
3352 				(state)?"enter":"exit");
3353 
3354 	return ret;
3355 }
3356 
smu_mode1_reset_is_support(struct smu_context * smu)3357 bool smu_mode1_reset_is_support(struct smu_context *smu)
3358 {
3359 	bool ret = false;
3360 
3361 	if (!smu->pm_enabled)
3362 		return false;
3363 
3364 	if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
3365 		ret = smu->ppt_funcs->mode1_reset_is_support(smu);
3366 
3367 	return ret;
3368 }
3369 
smu_mode2_reset_is_support(struct smu_context * smu)3370 bool smu_mode2_reset_is_support(struct smu_context *smu)
3371 {
3372 	bool ret = false;
3373 
3374 	if (!smu->pm_enabled)
3375 		return false;
3376 
3377 	if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
3378 		ret = smu->ppt_funcs->mode2_reset_is_support(smu);
3379 
3380 	return ret;
3381 }
3382 
smu_mode1_reset(struct smu_context * smu)3383 int smu_mode1_reset(struct smu_context *smu)
3384 {
3385 	int ret = 0;
3386 
3387 	if (!smu->pm_enabled)
3388 		return -EOPNOTSUPP;
3389 
3390 	if (smu->ppt_funcs->mode1_reset)
3391 		ret = smu->ppt_funcs->mode1_reset(smu);
3392 
3393 	return ret;
3394 }
3395 
smu_mode2_reset(void * handle)3396 static int smu_mode2_reset(void *handle)
3397 {
3398 	struct smu_context *smu = handle;
3399 	int ret = 0;
3400 
3401 	if (!smu->pm_enabled)
3402 		return -EOPNOTSUPP;
3403 
3404 	if (smu->ppt_funcs->mode2_reset)
3405 		ret = smu->ppt_funcs->mode2_reset(smu);
3406 
3407 	if (ret)
3408 		dev_err(smu->adev->dev, "Mode2 reset failed!\n");
3409 
3410 	return ret;
3411 }
3412 
smu_enable_gfx_features(void * handle)3413 static int smu_enable_gfx_features(void *handle)
3414 {
3415 	struct smu_context *smu = handle;
3416 	int ret = 0;
3417 
3418 	if (!smu->pm_enabled)
3419 		return -EOPNOTSUPP;
3420 
3421 	if (smu->ppt_funcs->enable_gfx_features)
3422 		ret = smu->ppt_funcs->enable_gfx_features(smu);
3423 
3424 	if (ret)
3425 		dev_err(smu->adev->dev, "enable gfx features failed!\n");
3426 
3427 	return ret;
3428 }
3429 
smu_get_max_sustainable_clocks_by_dc(void * handle,struct pp_smu_nv_clock_table * max_clocks)3430 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
3431 						struct pp_smu_nv_clock_table *max_clocks)
3432 {
3433 	struct smu_context *smu = handle;
3434 	int ret = 0;
3435 
3436 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3437 		return -EOPNOTSUPP;
3438 
3439 	if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
3440 		ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
3441 
3442 	return ret;
3443 }
3444 
smu_get_uclk_dpm_states(void * handle,unsigned int * clock_values_in_khz,unsigned int * num_states)3445 static int smu_get_uclk_dpm_states(void *handle,
3446 				   unsigned int *clock_values_in_khz,
3447 				   unsigned int *num_states)
3448 {
3449 	struct smu_context *smu = handle;
3450 	int ret = 0;
3451 
3452 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3453 		return -EOPNOTSUPP;
3454 
3455 	if (smu->ppt_funcs->get_uclk_dpm_states)
3456 		ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
3457 
3458 	return ret;
3459 }
3460 
smu_get_current_power_state(void * handle)3461 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
3462 {
3463 	struct smu_context *smu = handle;
3464 	enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
3465 
3466 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3467 		return -EOPNOTSUPP;
3468 
3469 	if (smu->ppt_funcs->get_current_power_state)
3470 		pm_state = smu->ppt_funcs->get_current_power_state(smu);
3471 
3472 	return pm_state;
3473 }
3474 
smu_get_dpm_clock_table(void * handle,struct dpm_clocks * clock_table)3475 static int smu_get_dpm_clock_table(void *handle,
3476 				   struct dpm_clocks *clock_table)
3477 {
3478 	struct smu_context *smu = handle;
3479 	int ret = 0;
3480 
3481 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3482 		return -EOPNOTSUPP;
3483 
3484 	if (smu->ppt_funcs->get_dpm_clock_table)
3485 		ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
3486 
3487 	return ret;
3488 }
3489 
smu_sys_get_gpu_metrics(void * handle,void ** table)3490 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
3491 {
3492 	struct smu_context *smu = handle;
3493 
3494 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3495 		return -EOPNOTSUPP;
3496 
3497 	if (!smu->ppt_funcs->get_gpu_metrics)
3498 		return -EOPNOTSUPP;
3499 
3500 	return smu->ppt_funcs->get_gpu_metrics(smu, table);
3501 }
3502 
smu_sys_get_pm_metrics(void * handle,void * pm_metrics,size_t size)3503 static ssize_t smu_sys_get_pm_metrics(void *handle, void *pm_metrics,
3504 				      size_t size)
3505 {
3506 	struct smu_context *smu = handle;
3507 
3508 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3509 		return -EOPNOTSUPP;
3510 
3511 	if (!smu->ppt_funcs->get_pm_metrics)
3512 		return -EOPNOTSUPP;
3513 
3514 	return smu->ppt_funcs->get_pm_metrics(smu, pm_metrics, size);
3515 }
3516 
smu_enable_mgpu_fan_boost(void * handle)3517 static int smu_enable_mgpu_fan_boost(void *handle)
3518 {
3519 	struct smu_context *smu = handle;
3520 	int ret = 0;
3521 
3522 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3523 		return -EOPNOTSUPP;
3524 
3525 	if (smu->ppt_funcs->enable_mgpu_fan_boost)
3526 		ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
3527 
3528 	return ret;
3529 }
3530 
smu_gfx_state_change_set(void * handle,uint32_t state)3531 static int smu_gfx_state_change_set(void *handle,
3532 				    uint32_t state)
3533 {
3534 	struct smu_context *smu = handle;
3535 	int ret = 0;
3536 
3537 	if (smu->ppt_funcs->gfx_state_change_set)
3538 		ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
3539 
3540 	return ret;
3541 }
3542 
smu_handle_passthrough_sbr(struct smu_context * smu,bool enable)3543 int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)
3544 {
3545 	int ret = 0;
3546 
3547 	if (smu->ppt_funcs->smu_handle_passthrough_sbr)
3548 		ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);
3549 
3550 	return ret;
3551 }
3552 
smu_get_ecc_info(struct smu_context * smu,void * umc_ecc)3553 int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)
3554 {
3555 	int ret = -EOPNOTSUPP;
3556 
3557 	if (smu->ppt_funcs &&
3558 		smu->ppt_funcs->get_ecc_info)
3559 		ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);
3560 
3561 	return ret;
3562 
3563 }
3564 
smu_get_prv_buffer_details(void * handle,void ** addr,size_t * size)3565 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
3566 {
3567 	struct smu_context *smu = handle;
3568 	struct smu_table_context *smu_table = &smu->smu_table;
3569 	struct smu_table *memory_pool = &smu_table->memory_pool;
3570 
3571 	if (!addr || !size)
3572 		return -EINVAL;
3573 
3574 	*addr = NULL;
3575 	*size = 0;
3576 	if (memory_pool->bo) {
3577 		*addr = memory_pool->cpu_addr;
3578 		*size = memory_pool->size;
3579 	}
3580 
3581 	return 0;
3582 }
3583 
smu_print_dpm_policy(struct smu_dpm_policy * policy,char * sysbuf,size_t * size)3584 static void smu_print_dpm_policy(struct smu_dpm_policy *policy, char *sysbuf,
3585 				 size_t *size)
3586 {
3587 	size_t offset = *size;
3588 	int level;
3589 
3590 	for_each_set_bit(level, &policy->level_mask, PP_POLICY_MAX_LEVELS) {
3591 		if (level == policy->current_level)
3592 			offset += sysfs_emit_at(sysbuf, offset,
3593 				"%d : %s*\n", level,
3594 				policy->desc->get_desc(policy, level));
3595 		else
3596 			offset += sysfs_emit_at(sysbuf, offset,
3597 				"%d : %s\n", level,
3598 				policy->desc->get_desc(policy, level));
3599 	}
3600 
3601 	*size = offset;
3602 }
3603 
smu_get_pm_policy_info(struct smu_context * smu,enum pp_pm_policy p_type,char * sysbuf)3604 ssize_t smu_get_pm_policy_info(struct smu_context *smu,
3605 			       enum pp_pm_policy p_type, char *sysbuf)
3606 {
3607 	struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;
3608 	struct smu_dpm_policy_ctxt *policy_ctxt;
3609 	struct smu_dpm_policy *dpm_policy;
3610 	size_t offset = 0;
3611 
3612 	policy_ctxt = dpm_ctxt->dpm_policies;
3613 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||
3614 	    !policy_ctxt->policy_mask)
3615 		return -EOPNOTSUPP;
3616 
3617 	if (p_type == PP_PM_POLICY_NONE)
3618 		return -EINVAL;
3619 
3620 	dpm_policy = smu_get_pm_policy(smu, p_type);
3621 	if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->desc)
3622 		return -ENOENT;
3623 
3624 	if (!sysbuf)
3625 		return -EINVAL;
3626 
3627 	smu_print_dpm_policy(dpm_policy, sysbuf, &offset);
3628 
3629 	return offset;
3630 }
3631 
smu_get_pm_policy(struct smu_context * smu,enum pp_pm_policy p_type)3632 struct smu_dpm_policy *smu_get_pm_policy(struct smu_context *smu,
3633 					 enum pp_pm_policy p_type)
3634 {
3635 	struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;
3636 	struct smu_dpm_policy_ctxt *policy_ctxt;
3637 	int i;
3638 
3639 	policy_ctxt = dpm_ctxt->dpm_policies;
3640 	if (!policy_ctxt)
3641 		return NULL;
3642 
3643 	for (i = 0; i < hweight32(policy_ctxt->policy_mask); ++i) {
3644 		if (policy_ctxt->policies[i].policy_type == p_type)
3645 			return &policy_ctxt->policies[i];
3646 	}
3647 
3648 	return NULL;
3649 }
3650 
smu_set_pm_policy(struct smu_context * smu,enum pp_pm_policy p_type,int level)3651 int smu_set_pm_policy(struct smu_context *smu, enum pp_pm_policy p_type,
3652 		      int level)
3653 {
3654 	struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;
3655 	struct smu_dpm_policy *dpm_policy = NULL;
3656 	struct smu_dpm_policy_ctxt *policy_ctxt;
3657 	int ret = -EOPNOTSUPP;
3658 
3659 	policy_ctxt = dpm_ctxt->dpm_policies;
3660 	if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||
3661 	    !policy_ctxt->policy_mask)
3662 		return ret;
3663 
3664 	if (level < 0 || level >= PP_POLICY_MAX_LEVELS)
3665 		return -EINVAL;
3666 
3667 	dpm_policy = smu_get_pm_policy(smu, p_type);
3668 
3669 	if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->set_policy)
3670 		return ret;
3671 
3672 	if (dpm_policy->current_level == level)
3673 		return 0;
3674 
3675 	ret = dpm_policy->set_policy(smu, level);
3676 
3677 	if (!ret)
3678 		dpm_policy->current_level = level;
3679 
3680 	return ret;
3681 }
3682 
3683 static const struct amd_pm_funcs swsmu_pm_funcs = {
3684 	/* export for sysfs */
3685 	.set_fan_control_mode    = smu_set_fan_control_mode,
3686 	.get_fan_control_mode    = smu_get_fan_control_mode,
3687 	.set_fan_speed_pwm   = smu_set_fan_speed_pwm,
3688 	.get_fan_speed_pwm   = smu_get_fan_speed_pwm,
3689 	.force_clock_level       = smu_force_ppclk_levels,
3690 	.print_clock_levels      = smu_print_ppclk_levels,
3691 	.emit_clock_levels       = smu_emit_ppclk_levels,
3692 	.force_performance_level = smu_force_performance_level,
3693 	.read_sensor             = smu_read_sensor,
3694 	.get_apu_thermal_limit       = smu_get_apu_thermal_limit,
3695 	.set_apu_thermal_limit       = smu_set_apu_thermal_limit,
3696 	.get_performance_level   = smu_get_performance_level,
3697 	.get_current_power_state = smu_get_current_power_state,
3698 	.get_fan_speed_rpm       = smu_get_fan_speed_rpm,
3699 	.set_fan_speed_rpm       = smu_set_fan_speed_rpm,
3700 	.get_pp_num_states       = smu_get_power_num_states,
3701 	.get_pp_table            = smu_sys_get_pp_table,
3702 	.set_pp_table            = smu_sys_set_pp_table,
3703 	.switch_power_profile    = smu_switch_power_profile,
3704 	/* export to amdgpu */
3705 	.dispatch_tasks          = smu_handle_dpm_task,
3706 	.load_firmware           = smu_load_microcode,
3707 	.set_powergating_by_smu  = smu_dpm_set_power_gate,
3708 	.set_power_limit         = smu_set_power_limit,
3709 	.get_power_limit         = smu_get_power_limit,
3710 	.get_power_profile_mode  = smu_get_power_profile_mode,
3711 	.set_power_profile_mode  = smu_set_power_profile_mode,
3712 	.odn_edit_dpm_table      = smu_od_edit_dpm_table,
3713 	.set_mp1_state           = smu_set_mp1_state,
3714 	.gfx_state_change_set    = smu_gfx_state_change_set,
3715 	/* export to DC */
3716 	.get_sclk                         = smu_get_sclk,
3717 	.get_mclk                         = smu_get_mclk,
3718 	.display_configuration_change     = smu_display_configuration_change,
3719 	.get_clock_by_type_with_latency   = smu_get_clock_by_type_with_latency,
3720 	.display_clock_voltage_request    = smu_display_clock_voltage_request,
3721 	.enable_mgpu_fan_boost            = smu_enable_mgpu_fan_boost,
3722 	.set_active_display_count         = smu_set_display_count,
3723 	.set_min_deep_sleep_dcefclk       = smu_set_deep_sleep_dcefclk,
3724 	.get_asic_baco_capability         = smu_get_baco_capability,
3725 	.set_asic_baco_state              = smu_baco_set_state,
3726 	.get_ppfeature_status             = smu_sys_get_pp_feature_mask,
3727 	.set_ppfeature_status             = smu_sys_set_pp_feature_mask,
3728 	.asic_reset_mode_2                = smu_mode2_reset,
3729 	.asic_reset_enable_gfx_features   = smu_enable_gfx_features,
3730 	.set_df_cstate                    = smu_set_df_cstate,
3731 	.set_xgmi_pstate                  = smu_set_xgmi_pstate,
3732 	.get_gpu_metrics                  = smu_sys_get_gpu_metrics,
3733 	.get_pm_metrics                   = smu_sys_get_pm_metrics,
3734 	.set_watermarks_for_clock_ranges     = smu_set_watermarks_for_clock_ranges,
3735 	.display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
3736 	.get_max_sustainable_clocks_by_dc    = smu_get_max_sustainable_clocks_by_dc,
3737 	.get_uclk_dpm_states              = smu_get_uclk_dpm_states,
3738 	.get_dpm_clock_table              = smu_get_dpm_clock_table,
3739 	.get_smu_prv_buf_details = smu_get_prv_buffer_details,
3740 };
3741 
smu_wait_for_event(struct smu_context * smu,enum smu_event_type event,uint64_t event_arg)3742 int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,
3743 		       uint64_t event_arg)
3744 {
3745 	int ret = -EINVAL;
3746 
3747 	if (smu->ppt_funcs->wait_for_event)
3748 		ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
3749 
3750 	return ret;
3751 }
3752 
smu_stb_collect_info(struct smu_context * smu,void * buf,uint32_t size)3753 int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)
3754 {
3755 
3756 	if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)
3757 		return -EOPNOTSUPP;
3758 
3759 	/* Confirm the buffer allocated is of correct size */
3760 	if (size != smu->stb_context.stb_buf_size)
3761 		return -EINVAL;
3762 
3763 	/*
3764 	 * No need to lock smu mutex as we access STB directly through MMIO
3765 	 * and not going through SMU messaging route (for now at least).
3766 	 * For registers access rely on implementation internal locking.
3767 	 */
3768 	return smu->ppt_funcs->stb_collect_info(smu, buf, size);
3769 }
3770 
3771 #if defined(CONFIG_DEBUG_FS)
3772 
smu_stb_debugfs_open(struct inode * inode,struct file * filp)3773 static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)
3774 {
3775 	struct amdgpu_device *adev = filp->f_inode->i_private;
3776 	struct smu_context *smu = adev->powerplay.pp_handle;
3777 	unsigned char *buf;
3778 	int r;
3779 
3780 	buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);
3781 	if (!buf)
3782 		return -ENOMEM;
3783 
3784 	r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);
3785 	if (r)
3786 		goto out;
3787 
3788 	filp->private_data = buf;
3789 
3790 	return 0;
3791 
3792 out:
3793 	kvfree(buf);
3794 	return r;
3795 }
3796 
smu_stb_debugfs_read(struct file * filp,char __user * buf,size_t size,loff_t * pos)3797 static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
3798 				loff_t *pos)
3799 {
3800 	struct amdgpu_device *adev = filp->f_inode->i_private;
3801 	struct smu_context *smu = adev->powerplay.pp_handle;
3802 
3803 
3804 	if (!filp->private_data)
3805 		return -EINVAL;
3806 
3807 	return simple_read_from_buffer(buf,
3808 				       size,
3809 				       pos, filp->private_data,
3810 				       smu->stb_context.stb_buf_size);
3811 }
3812 
smu_stb_debugfs_release(struct inode * inode,struct file * filp)3813 static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)
3814 {
3815 	kvfree(filp->private_data);
3816 	filp->private_data = NULL;
3817 
3818 	return 0;
3819 }
3820 
3821 /*
3822  * We have to define not only read method but also
3823  * open and release because .read takes up to PAGE_SIZE
3824  * data each time so and so is invoked multiple times.
3825  *  We allocate the STB buffer in .open and release it
3826  *  in .release
3827  */
3828 static const struct file_operations smu_stb_debugfs_fops = {
3829 	.owner = THIS_MODULE,
3830 	.open = smu_stb_debugfs_open,
3831 	.read = smu_stb_debugfs_read,
3832 	.release = smu_stb_debugfs_release,
3833 	.llseek = default_llseek,
3834 };
3835 
3836 #endif
3837 
amdgpu_smu_stb_debug_fs_init(struct amdgpu_device * adev)3838 void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)
3839 {
3840 #if defined(CONFIG_DEBUG_FS)
3841 
3842 	struct smu_context *smu = adev->powerplay.pp_handle;
3843 
3844 	if (!smu || (!smu->stb_context.stb_buf_size))
3845 		return;
3846 
3847 	debugfs_create_file_size("amdgpu_smu_stb_dump",
3848 			    S_IRUSR,
3849 			    adev_to_drm(adev)->primary->debugfs_root,
3850 			    adev,
3851 			    &smu_stb_debugfs_fops,
3852 			    smu->stb_context.stb_buf_size);
3853 #endif
3854 }
3855 
smu_send_hbm_bad_pages_num(struct smu_context * smu,uint32_t size)3856 int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)
3857 {
3858 	int ret = 0;
3859 
3860 	if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)
3861 		ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);
3862 
3863 	return ret;
3864 }
3865 
smu_send_hbm_bad_channel_flag(struct smu_context * smu,uint32_t size)3866 int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)
3867 {
3868 	int ret = 0;
3869 
3870 	if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)
3871 		ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);
3872 
3873 	return ret;
3874 }
3875 
smu_send_rma_reason(struct smu_context * smu)3876 int smu_send_rma_reason(struct smu_context *smu)
3877 {
3878 	int ret = 0;
3879 
3880 	if (smu->ppt_funcs && smu->ppt_funcs->send_rma_reason)
3881 		ret = smu->ppt_funcs->send_rma_reason(smu);
3882 
3883 	return ret;
3884 }
3885