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