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