2 * Copyright 2019 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #define SWSMU_CODE_LAYER_L1
25 #include <linux/firmware.h>
26 #include <linux/pci.h>
27 #include <linux/power_supply.h>
28 #include <linux/reboot.h>
31 #include "amdgpu_smu.h"
32 #include "smu_internal.h"
34 #include "arcturus_ppt.h"
35 #include "navi10_ppt.h"
36 #include "sienna_cichlid_ppt.h"
37 #include "renoir_ppt.h"
38 #include "vangogh_ppt.h"
39 #include "aldebaran_ppt.h"
40 #include "yellow_carp_ppt.h"
41 #include "cyan_skillfish_ppt.h"
42 #include "smu_v13_0_0_ppt.h"
43 #include "smu_v13_0_4_ppt.h"
44 #include "smu_v13_0_5_ppt.h"
45 #include "smu_v13_0_6_ppt.h"
46 #include "smu_v13_0_7_ppt.h"
47 #include "smu_v14_0_0_ppt.h"
51 * DO NOT use these for err/warn/info/debug messages.
52 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
53 * They are more MGPU friendly.
60 static const struct amd_pm_funcs swsmu_pm_funcs;
61 static int smu_force_smuclk_levels(struct smu_context *smu,
62 enum smu_clk_type clk_type,
64 static int smu_handle_task(struct smu_context *smu,
65 enum amd_dpm_forced_level level,
66 enum amd_pp_task task_id);
67 static int smu_reset(struct smu_context *smu);
68 static int smu_set_fan_speed_pwm(void *handle, u32 speed);
69 static int smu_set_fan_control_mode(void *handle, u32 value);
70 static int smu_set_power_limit(void *handle, uint32_t limit);
71 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);
72 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);
73 static int smu_set_mp1_state(void *handle, enum pp_mp1_state mp1_state);
75 static int smu_sys_get_pp_feature_mask(void *handle,
78 struct smu_context *smu = handle;
80 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
83 return smu_get_pp_feature_mask(smu, buf);
86 static int smu_sys_set_pp_feature_mask(void *handle,
89 struct smu_context *smu = handle;
91 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
94 return smu_set_pp_feature_mask(smu, new_mask);
97 int smu_set_residency_gfxoff(struct smu_context *smu, bool value)
99 if (!smu->ppt_funcs->set_gfx_off_residency)
102 return smu_set_gfx_off_residency(smu, value);
105 int smu_get_residency_gfxoff(struct smu_context *smu, u32 *value)
107 if (!smu->ppt_funcs->get_gfx_off_residency)
110 return smu_get_gfx_off_residency(smu, value);
113 int smu_get_entrycount_gfxoff(struct smu_context *smu, u64 *value)
115 if (!smu->ppt_funcs->get_gfx_off_entrycount)
118 return smu_get_gfx_off_entrycount(smu, value);
121 int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value)
123 if (!smu->ppt_funcs->get_gfx_off_status)
126 *value = smu_get_gfx_off_status(smu);
131 int smu_set_soft_freq_range(struct smu_context *smu,
132 enum smu_clk_type clk_type,
138 if (smu->ppt_funcs->set_soft_freq_limited_range)
139 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
147 int smu_get_dpm_freq_range(struct smu_context *smu,
148 enum smu_clk_type clk_type,
157 if (smu->ppt_funcs->get_dpm_ultimate_freq)
158 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
166 int smu_set_gfx_power_up_by_imu(struct smu_context *smu)
169 struct amdgpu_device *adev = smu->adev;
171 if (smu->ppt_funcs->set_gfx_power_up_by_imu) {
172 ret = smu->ppt_funcs->set_gfx_power_up_by_imu(smu);
174 dev_err(adev->dev, "Failed to enable gfx imu!\n");
179 static u32 smu_get_mclk(void *handle, bool low)
181 struct smu_context *smu = handle;
185 ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
186 low ? &clk_freq : NULL,
187 !low ? &clk_freq : NULL);
190 return clk_freq * 100;
193 static u32 smu_get_sclk(void *handle, bool low)
195 struct smu_context *smu = handle;
199 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
200 low ? &clk_freq : NULL,
201 !low ? &clk_freq : NULL);
204 return clk_freq * 100;
207 static int smu_set_gfx_imu_enable(struct smu_context *smu)
209 struct amdgpu_device *adev = smu->adev;
211 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
214 if (amdgpu_in_reset(smu->adev) || adev->in_s0ix)
217 return smu_set_gfx_power_up_by_imu(smu);
220 static bool is_vcn_enabled(struct amdgpu_device *adev)
224 for (i = 0; i < adev->num_ip_blocks; i++) {
225 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_VCN ||
226 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_JPEG) &&
227 !adev->ip_blocks[i].status.valid)
234 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
237 struct smu_power_context *smu_power = &smu->smu_power;
238 struct smu_power_gate *power_gate = &smu_power->power_gate;
242 * don't poweron vcn/jpeg when they are skipped.
244 if (!is_vcn_enabled(smu->adev))
247 if (!smu->ppt_funcs->dpm_set_vcn_enable)
250 if (atomic_read(&power_gate->vcn_gated) ^ enable)
253 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
255 atomic_set(&power_gate->vcn_gated, !enable);
260 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
263 struct smu_power_context *smu_power = &smu->smu_power;
264 struct smu_power_gate *power_gate = &smu_power->power_gate;
267 if (!is_vcn_enabled(smu->adev))
270 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
273 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
276 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
278 atomic_set(&power_gate->jpeg_gated, !enable);
283 static int smu_dpm_set_vpe_enable(struct smu_context *smu,
286 struct smu_power_context *smu_power = &smu->smu_power;
287 struct smu_power_gate *power_gate = &smu_power->power_gate;
290 if (!smu->ppt_funcs->dpm_set_vpe_enable)
293 if (atomic_read(&power_gate->vpe_gated) ^ enable)
296 ret = smu->ppt_funcs->dpm_set_vpe_enable(smu, enable);
298 atomic_set(&power_gate->vpe_gated, !enable);
303 static int smu_dpm_set_umsch_mm_enable(struct smu_context *smu,
306 struct smu_power_context *smu_power = &smu->smu_power;
307 struct smu_power_gate *power_gate = &smu_power->power_gate;
310 if (!smu->adev->enable_umsch_mm)
313 if (!smu->ppt_funcs->dpm_set_umsch_mm_enable)
316 if (atomic_read(&power_gate->umsch_mm_gated) ^ enable)
319 ret = smu->ppt_funcs->dpm_set_umsch_mm_enable(smu, enable);
321 atomic_set(&power_gate->umsch_mm_gated, !enable);
327 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
329 * @handle: smu_context pointer
330 * @block_type: the IP block to power gate/ungate
331 * @gate: to power gate if true, ungate otherwise
333 * This API uses no smu->mutex lock protection due to:
334 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
335 * This is guarded to be race condition free by the caller.
336 * 2. Or get called on user setting request of power_dpm_force_performance_level.
337 * Under this case, the smu->mutex lock protection is already enforced on
338 * the parent API smu_force_performance_level of the call path.
340 static int smu_dpm_set_power_gate(void *handle,
344 struct smu_context *smu = handle;
347 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) {
348 dev_WARN(smu->adev->dev,
349 "SMU uninitialized but power %s requested for %u!\n",
350 gate ? "gate" : "ungate", block_type);
354 switch (block_type) {
356 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
357 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
359 case AMD_IP_BLOCK_TYPE_UVD:
360 case AMD_IP_BLOCK_TYPE_VCN:
361 ret = smu_dpm_set_vcn_enable(smu, !gate);
363 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
364 gate ? "gate" : "ungate");
366 case AMD_IP_BLOCK_TYPE_GFX:
367 ret = smu_gfx_off_control(smu, gate);
369 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
370 gate ? "enable" : "disable");
372 case AMD_IP_BLOCK_TYPE_SDMA:
373 ret = smu_powergate_sdma(smu, gate);
375 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
376 gate ? "gate" : "ungate");
378 case AMD_IP_BLOCK_TYPE_JPEG:
379 ret = smu_dpm_set_jpeg_enable(smu, !gate);
381 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
382 gate ? "gate" : "ungate");
384 case AMD_IP_BLOCK_TYPE_VPE:
385 ret = smu_dpm_set_vpe_enable(smu, !gate);
387 dev_err(smu->adev->dev, "Failed to power %s VPE!\n",
388 gate ? "gate" : "ungate");
391 dev_err(smu->adev->dev, "Unsupported block type!\n");
399 * smu_set_user_clk_dependencies - set user profile clock dependencies
401 * @smu: smu_context pointer
402 * @clk: enum smu_clk_type type
404 * Enable/Disable the clock dependency for the @clk type.
406 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
408 if (smu->adev->in_suspend)
411 if (clk == SMU_MCLK) {
412 smu->user_dpm_profile.clk_dependency = 0;
413 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
414 } else if (clk == SMU_FCLK) {
415 /* MCLK takes precedence over FCLK */
416 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
419 smu->user_dpm_profile.clk_dependency = 0;
420 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
421 } else if (clk == SMU_SOCCLK) {
422 /* MCLK takes precedence over SOCCLK */
423 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
426 smu->user_dpm_profile.clk_dependency = 0;
427 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
429 /* Add clk dependencies here, if any */
434 * smu_restore_dpm_user_profile - reinstate user dpm profile
436 * @smu: smu_context pointer
438 * Restore the saved user power configurations include power limit,
439 * clock frequencies, fan control mode and fan speed.
441 static void smu_restore_dpm_user_profile(struct smu_context *smu)
443 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
446 if (!smu->adev->in_suspend)
449 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
452 /* Enable restore flag */
453 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
455 /* set the user dpm power limit */
456 if (smu->user_dpm_profile.power_limit) {
457 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
459 dev_err(smu->adev->dev, "Failed to set power limit value\n");
462 /* set the user dpm clock configurations */
463 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
464 enum smu_clk_type clk_type;
466 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
468 * Iterate over smu clk type and force the saved user clk
469 * configs, skip if clock dependency is enabled
471 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
472 smu->user_dpm_profile.clk_mask[clk_type]) {
473 ret = smu_force_smuclk_levels(smu, clk_type,
474 smu->user_dpm_profile.clk_mask[clk_type]);
476 dev_err(smu->adev->dev,
477 "Failed to set clock type = %d\n", clk_type);
482 /* set the user dpm fan configurations */
483 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||
484 smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {
485 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
486 if (ret != -EOPNOTSUPP) {
487 smu->user_dpm_profile.fan_speed_pwm = 0;
488 smu->user_dpm_profile.fan_speed_rpm = 0;
489 smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;
490 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
493 if (smu->user_dpm_profile.fan_speed_pwm) {
494 ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);
495 if (ret != -EOPNOTSUPP)
496 dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");
499 if (smu->user_dpm_profile.fan_speed_rpm) {
500 ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);
501 if (ret != -EOPNOTSUPP)
502 dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");
506 /* Restore user customized OD settings */
507 if (smu->user_dpm_profile.user_od) {
508 if (smu->ppt_funcs->restore_user_od_settings) {
509 ret = smu->ppt_funcs->restore_user_od_settings(smu);
511 dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");
515 /* Disable restore flag */
516 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
519 static int smu_get_power_num_states(void *handle,
520 struct pp_states_info *state_info)
525 /* not support power state */
526 memset(state_info, 0, sizeof(struct pp_states_info));
527 state_info->nums = 1;
528 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
533 bool is_support_sw_smu(struct amdgpu_device *adev)
535 /* vega20 is 11.0.2, but it's supported via the powerplay code */
536 if (adev->asic_type == CHIP_VEGA20)
539 if (amdgpu_ip_version(adev, MP1_HWIP, 0) >= IP_VERSION(11, 0, 0))
545 bool is_support_cclk_dpm(struct amdgpu_device *adev)
547 struct smu_context *smu = adev->powerplay.pp_handle;
549 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
556 static int smu_sys_get_pp_table(void *handle,
559 struct smu_context *smu = handle;
560 struct smu_table_context *smu_table = &smu->smu_table;
562 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
565 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
568 if (smu_table->hardcode_pptable)
569 *table = smu_table->hardcode_pptable;
571 *table = smu_table->power_play_table;
573 return smu_table->power_play_table_size;
576 static int smu_sys_set_pp_table(void *handle,
580 struct smu_context *smu = handle;
581 struct smu_table_context *smu_table = &smu->smu_table;
582 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
585 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
588 if (header->usStructureSize != size) {
589 dev_err(smu->adev->dev, "pp table size not matched !\n");
593 if (!smu_table->hardcode_pptable) {
594 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
595 if (!smu_table->hardcode_pptable)
599 memcpy(smu_table->hardcode_pptable, buf, size);
600 smu_table->power_play_table = smu_table->hardcode_pptable;
601 smu_table->power_play_table_size = size;
604 * Special hw_fini action(for Navi1x, the DPMs disablement will be
605 * skipped) may be needed for custom pptable uploading.
607 smu->uploading_custom_pp_table = true;
609 ret = smu_reset(smu);
611 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
613 smu->uploading_custom_pp_table = false;
618 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
620 struct smu_feature *feature = &smu->smu_feature;
621 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
625 * With SCPM enabled, the allowed featuremasks setting(via
626 * PPSMC_MSG_SetAllowedFeaturesMaskLow/High) is not permitted.
627 * That means there is no way to let PMFW knows the settings below.
628 * Thus, we just assume all the features are allowed under
631 if (smu->adev->scpm_enabled) {
632 bitmap_fill(feature->allowed, SMU_FEATURE_MAX);
636 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
638 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
643 bitmap_or(feature->allowed, feature->allowed,
644 (unsigned long *)allowed_feature_mask,
645 feature->feature_num);
650 static int smu_set_funcs(struct amdgpu_device *adev)
652 struct smu_context *smu = adev->powerplay.pp_handle;
654 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
655 smu->od_enabled = true;
657 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
658 case IP_VERSION(11, 0, 0):
659 case IP_VERSION(11, 0, 5):
660 case IP_VERSION(11, 0, 9):
661 navi10_set_ppt_funcs(smu);
663 case IP_VERSION(11, 0, 7):
664 case IP_VERSION(11, 0, 11):
665 case IP_VERSION(11, 0, 12):
666 case IP_VERSION(11, 0, 13):
667 sienna_cichlid_set_ppt_funcs(smu);
669 case IP_VERSION(12, 0, 0):
670 case IP_VERSION(12, 0, 1):
671 renoir_set_ppt_funcs(smu);
673 case IP_VERSION(11, 5, 0):
674 vangogh_set_ppt_funcs(smu);
676 case IP_VERSION(13, 0, 1):
677 case IP_VERSION(13, 0, 3):
678 case IP_VERSION(13, 0, 8):
679 yellow_carp_set_ppt_funcs(smu);
681 case IP_VERSION(13, 0, 4):
682 case IP_VERSION(13, 0, 11):
683 smu_v13_0_4_set_ppt_funcs(smu);
685 case IP_VERSION(13, 0, 5):
686 smu_v13_0_5_set_ppt_funcs(smu);
688 case IP_VERSION(11, 0, 8):
689 cyan_skillfish_set_ppt_funcs(smu);
691 case IP_VERSION(11, 0, 2):
692 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
693 arcturus_set_ppt_funcs(smu);
694 /* OD is not supported on Arcturus */
695 smu->od_enabled = false;
697 case IP_VERSION(13, 0, 2):
698 aldebaran_set_ppt_funcs(smu);
699 /* Enable pp_od_clk_voltage node */
700 smu->od_enabled = true;
702 case IP_VERSION(13, 0, 0):
703 case IP_VERSION(13, 0, 10):
704 smu_v13_0_0_set_ppt_funcs(smu);
706 case IP_VERSION(13, 0, 6):
707 smu_v13_0_6_set_ppt_funcs(smu);
708 /* Enable pp_od_clk_voltage node */
709 smu->od_enabled = true;
711 case IP_VERSION(13, 0, 7):
712 smu_v13_0_7_set_ppt_funcs(smu);
714 case IP_VERSION(14, 0, 0):
715 smu_v14_0_0_set_ppt_funcs(smu);
724 static int smu_early_init(void *handle)
726 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
727 struct smu_context *smu;
730 smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL);
735 smu->pm_enabled = !!amdgpu_dpm;
737 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
738 smu->smu_baco.platform_support = false;
739 smu->user_dpm_profile.fan_mode = -1;
741 mutex_init(&smu->message_lock);
743 adev->powerplay.pp_handle = smu;
744 adev->powerplay.pp_funcs = &swsmu_pm_funcs;
746 r = smu_set_funcs(adev);
749 return smu_init_microcode(smu);
752 static int smu_set_default_dpm_table(struct smu_context *smu)
754 struct smu_power_context *smu_power = &smu->smu_power;
755 struct smu_power_gate *power_gate = &smu_power->power_gate;
756 int vcn_gate, jpeg_gate;
759 if (!smu->ppt_funcs->set_default_dpm_table)
762 vcn_gate = atomic_read(&power_gate->vcn_gated);
763 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
765 ret = smu_dpm_set_vcn_enable(smu, true);
769 ret = smu_dpm_set_jpeg_enable(smu, true);
773 ret = smu->ppt_funcs->set_default_dpm_table(smu);
775 dev_err(smu->adev->dev,
776 "Failed to setup default dpm clock tables!\n");
778 smu_dpm_set_jpeg_enable(smu, !jpeg_gate);
780 smu_dpm_set_vcn_enable(smu, !vcn_gate);
784 static int smu_apply_default_config_table_settings(struct smu_context *smu)
786 struct amdgpu_device *adev = smu->adev;
789 ret = smu_get_default_config_table_settings(smu,
790 &adev->pm.config_table);
794 return smu_set_config_table(smu, &adev->pm.config_table);
797 static int smu_late_init(void *handle)
799 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
800 struct smu_context *smu = adev->powerplay.pp_handle;
803 smu_set_fine_grain_gfx_freq_parameters(smu);
805 if (!smu->pm_enabled)
808 ret = smu_post_init(smu);
810 dev_err(adev->dev, "Failed to post smu init!\n");
815 * Explicitly notify PMFW the power mode the system in. Since
816 * the PMFW may boot the ASIC with a different mode.
817 * For those supporting ACDC switch via gpio, PMFW will
818 * handle the switch automatically. Driver involvement
821 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
824 if ((amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 1)) ||
825 (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 3)))
828 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
829 ret = smu_set_default_od_settings(smu);
831 dev_err(adev->dev, "Failed to setup default OD settings!\n");
836 ret = smu_populate_umd_state_clk(smu);
838 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
842 ret = smu_get_asic_power_limits(smu,
843 &smu->current_power_limit,
844 &smu->default_power_limit,
845 &smu->max_power_limit,
846 &smu->min_power_limit);
848 dev_err(adev->dev, "Failed to get asic power limits!\n");
852 if (!amdgpu_sriov_vf(adev))
853 smu_get_unique_id(smu);
855 smu_get_fan_parameters(smu);
858 smu->smu_dpm.dpm_level,
859 AMD_PP_TASK_COMPLETE_INIT);
861 ret = smu_apply_default_config_table_settings(smu);
862 if (ret && (ret != -EOPNOTSUPP)) {
863 dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n");
867 smu_restore_dpm_user_profile(smu);
872 static int smu_init_fb_allocations(struct smu_context *smu)
874 struct amdgpu_device *adev = smu->adev;
875 struct smu_table_context *smu_table = &smu->smu_table;
876 struct smu_table *tables = smu_table->tables;
877 struct smu_table *driver_table = &(smu_table->driver_table);
878 uint32_t max_table_size = 0;
881 /* VRAM allocation for tool table */
882 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
883 ret = amdgpu_bo_create_kernel(adev,
884 tables[SMU_TABLE_PMSTATUSLOG].size,
885 tables[SMU_TABLE_PMSTATUSLOG].align,
886 tables[SMU_TABLE_PMSTATUSLOG].domain,
887 &tables[SMU_TABLE_PMSTATUSLOG].bo,
888 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
889 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
891 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
896 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT;
897 /* VRAM allocation for driver table */
898 for (i = 0; i < SMU_TABLE_COUNT; i++) {
899 if (tables[i].size == 0)
902 /* If one of the tables has VRAM domain restriction, keep it in
905 if ((tables[i].domain &
906 (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) ==
907 AMDGPU_GEM_DOMAIN_VRAM)
908 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
910 if (i == SMU_TABLE_PMSTATUSLOG)
913 if (max_table_size < tables[i].size)
914 max_table_size = tables[i].size;
917 driver_table->size = max_table_size;
918 driver_table->align = PAGE_SIZE;
920 ret = amdgpu_bo_create_kernel(adev,
923 driver_table->domain,
925 &driver_table->mc_address,
926 &driver_table->cpu_addr);
928 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
929 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
930 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
931 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
932 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
938 static int smu_fini_fb_allocations(struct smu_context *smu)
940 struct smu_table_context *smu_table = &smu->smu_table;
941 struct smu_table *tables = smu_table->tables;
942 struct smu_table *driver_table = &(smu_table->driver_table);
944 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
945 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
946 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
947 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
949 amdgpu_bo_free_kernel(&driver_table->bo,
950 &driver_table->mc_address,
951 &driver_table->cpu_addr);
957 * smu_alloc_memory_pool - allocate memory pool in the system memory
959 * @smu: amdgpu_device pointer
961 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
962 * and DramLogSetDramAddr can notify it changed.
964 * Returns 0 on success, error on failure.
966 static int smu_alloc_memory_pool(struct smu_context *smu)
968 struct amdgpu_device *adev = smu->adev;
969 struct smu_table_context *smu_table = &smu->smu_table;
970 struct smu_table *memory_pool = &smu_table->memory_pool;
971 uint64_t pool_size = smu->pool_size;
974 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
977 memory_pool->size = pool_size;
978 memory_pool->align = PAGE_SIZE;
979 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
982 case SMU_MEMORY_POOL_SIZE_256_MB:
983 case SMU_MEMORY_POOL_SIZE_512_MB:
984 case SMU_MEMORY_POOL_SIZE_1_GB:
985 case SMU_MEMORY_POOL_SIZE_2_GB:
986 ret = amdgpu_bo_create_kernel(adev,
991 &memory_pool->mc_address,
992 &memory_pool->cpu_addr);
994 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
1003 static int smu_free_memory_pool(struct smu_context *smu)
1005 struct smu_table_context *smu_table = &smu->smu_table;
1006 struct smu_table *memory_pool = &smu_table->memory_pool;
1008 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1011 amdgpu_bo_free_kernel(&memory_pool->bo,
1012 &memory_pool->mc_address,
1013 &memory_pool->cpu_addr);
1015 memset(memory_pool, 0, sizeof(struct smu_table));
1020 static int smu_alloc_dummy_read_table(struct smu_context *smu)
1022 struct smu_table_context *smu_table = &smu->smu_table;
1023 struct smu_table *dummy_read_1_table =
1024 &smu_table->dummy_read_1_table;
1025 struct amdgpu_device *adev = smu->adev;
1028 if (!dummy_read_1_table->size)
1031 ret = amdgpu_bo_create_kernel(adev,
1032 dummy_read_1_table->size,
1033 dummy_read_1_table->align,
1034 dummy_read_1_table->domain,
1035 &dummy_read_1_table->bo,
1036 &dummy_read_1_table->mc_address,
1037 &dummy_read_1_table->cpu_addr);
1039 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
1044 static void smu_free_dummy_read_table(struct smu_context *smu)
1046 struct smu_table_context *smu_table = &smu->smu_table;
1047 struct smu_table *dummy_read_1_table =
1048 &smu_table->dummy_read_1_table;
1051 amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
1052 &dummy_read_1_table->mc_address,
1053 &dummy_read_1_table->cpu_addr);
1055 memset(dummy_read_1_table, 0, sizeof(struct smu_table));
1058 static int smu_smc_table_sw_init(struct smu_context *smu)
1063 * Create smu_table structure, and init smc tables such as
1064 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
1066 ret = smu_init_smc_tables(smu);
1068 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
1073 * Create smu_power_context structure, and allocate smu_dpm_context and
1074 * context size to fill the smu_power_context data.
1076 ret = smu_init_power(smu);
1078 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
1083 * allocate vram bos to store smc table contents.
1085 ret = smu_init_fb_allocations(smu);
1089 ret = smu_alloc_memory_pool(smu);
1093 ret = smu_alloc_dummy_read_table(smu);
1097 ret = smu_i2c_init(smu);
1104 static int smu_smc_table_sw_fini(struct smu_context *smu)
1110 smu_free_dummy_read_table(smu);
1112 ret = smu_free_memory_pool(smu);
1116 ret = smu_fini_fb_allocations(smu);
1120 ret = smu_fini_power(smu);
1122 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
1126 ret = smu_fini_smc_tables(smu);
1128 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
1135 static void smu_throttling_logging_work_fn(struct work_struct *work)
1137 struct smu_context *smu = container_of(work, struct smu_context,
1138 throttling_logging_work);
1140 smu_log_thermal_throttling(smu);
1143 static void smu_interrupt_work_fn(struct work_struct *work)
1145 struct smu_context *smu = container_of(work, struct smu_context,
1148 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
1149 smu->ppt_funcs->interrupt_work(smu);
1152 static void smu_swctf_delayed_work_handler(struct work_struct *work)
1154 struct smu_context *smu =
1155 container_of(work, struct smu_context, swctf_delayed_work.work);
1156 struct smu_temperature_range *range =
1157 &smu->thermal_range;
1158 struct amdgpu_device *adev = smu->adev;
1159 uint32_t hotspot_tmp, size;
1162 * If the hotspot temperature is confirmed as below SW CTF setting point
1163 * after the delay enforced, nothing will be done.
1164 * Otherwise, a graceful shutdown will be performed to prevent further damage.
1166 if (range->software_shutdown_temp &&
1167 smu->ppt_funcs->read_sensor &&
1168 !smu->ppt_funcs->read_sensor(smu,
1169 AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1172 hotspot_tmp / 1000 < range->software_shutdown_temp)
1175 dev_emerg(adev->dev, "ERROR: GPU over temperature range(SW CTF) detected!\n");
1176 dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU SW CTF!\n");
1177 orderly_poweroff(true);
1180 static void smu_init_xgmi_plpd_mode(struct smu_context *smu)
1182 if (amdgpu_ip_version(smu->adev, MP1_HWIP, 0) == IP_VERSION(11, 0, 2)) {
1183 smu->plpd_mode = XGMI_PLPD_DEFAULT;
1187 /* PMFW put PLPD into default policy after enabling the feature */
1188 if (smu_feature_is_enabled(smu,
1189 SMU_FEATURE_XGMI_PER_LINK_PWR_DWN_BIT))
1190 smu->plpd_mode = XGMI_PLPD_DEFAULT;
1192 smu->plpd_mode = XGMI_PLPD_NONE;
1195 static int smu_sw_init(void *handle)
1197 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1198 struct smu_context *smu = adev->powerplay.pp_handle;
1201 smu->pool_size = adev->pm.smu_prv_buffer_size;
1202 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
1203 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
1204 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1206 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
1207 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
1208 atomic64_set(&smu->throttle_int_counter, 0);
1209 smu->watermarks_bitmap = 0;
1210 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1211 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1213 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
1214 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
1215 atomic_set(&smu->smu_power.power_gate.vpe_gated, 1);
1216 atomic_set(&smu->smu_power.power_gate.umsch_mm_gated, 1);
1218 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
1219 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
1220 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
1221 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
1222 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
1223 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
1224 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
1225 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
1227 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1228 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1229 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
1230 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
1231 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
1232 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
1233 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
1234 smu->display_config = &adev->pm.pm_display_cfg;
1236 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1237 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1239 INIT_DELAYED_WORK(&smu->swctf_delayed_work,
1240 smu_swctf_delayed_work_handler);
1242 ret = smu_smc_table_sw_init(smu);
1244 dev_err(adev->dev, "Failed to sw init smc table!\n");
1248 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1249 ret = smu_get_vbios_bootup_values(smu);
1251 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1255 ret = smu_init_pptable_microcode(smu);
1257 dev_err(adev->dev, "Failed to setup pptable firmware!\n");
1261 ret = smu_register_irq_handler(smu);
1263 dev_err(adev->dev, "Failed to register smc irq handler!\n");
1267 /* If there is no way to query fan control mode, fan control is not supported */
1268 if (!smu->ppt_funcs->get_fan_control_mode)
1269 smu->adev->pm.no_fan = true;
1274 static int smu_sw_fini(void *handle)
1276 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1277 struct smu_context *smu = adev->powerplay.pp_handle;
1280 ret = smu_smc_table_sw_fini(smu);
1282 dev_err(adev->dev, "Failed to sw fini smc table!\n");
1286 smu_fini_microcode(smu);
1291 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1293 struct amdgpu_device *adev = smu->adev;
1294 struct smu_temperature_range *range =
1295 &smu->thermal_range;
1298 if (!smu->ppt_funcs->get_thermal_temperature_range)
1301 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1305 adev->pm.dpm.thermal.min_temp = range->min;
1306 adev->pm.dpm.thermal.max_temp = range->max;
1307 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1308 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1309 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1310 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1311 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1312 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1313 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1319 * smu_wbrf_handle_exclusion_ranges - consume the wbrf exclusion ranges
1321 * @smu: smu_context pointer
1323 * Retrieve the wbrf exclusion ranges and send them to PMFW for proper handling.
1324 * Returns 0 on success, error on failure.
1326 static int smu_wbrf_handle_exclusion_ranges(struct smu_context *smu)
1328 struct wbrf_ranges_in_out wbrf_exclusion = {0};
1329 struct freq_band_range *wifi_bands = wbrf_exclusion.band_list;
1330 struct amdgpu_device *adev = smu->adev;
1331 uint32_t num_of_wbrf_ranges = MAX_NUM_OF_WBRF_RANGES;
1332 uint64_t start, end;
1335 ret = amd_wbrf_retrieve_freq_band(adev->dev, &wbrf_exclusion);
1337 dev_err(adev->dev, "Failed to retrieve exclusion ranges!\n");
1342 * The exclusion ranges array we got might be filled with holes and duplicate
1343 * entries. For example:
1344 * {(2400, 2500), (0, 0), (6882, 6962), (2400, 2500), (0, 0), (6117, 6189), (0, 0)...}
1345 * We need to do some sortups to eliminate those holes and duplicate entries.
1346 * Expected output: {(2400, 2500), (6117, 6189), (6882, 6962), (0, 0)...}
1348 for (i = 0; i < num_of_wbrf_ranges; i++) {
1349 start = wifi_bands[i].start;
1350 end = wifi_bands[i].end;
1352 /* get the last valid entry to fill the intermediate hole */
1353 if (!start && !end) {
1354 for (j = num_of_wbrf_ranges - 1; j > i; j--)
1355 if (wifi_bands[j].start && wifi_bands[j].end)
1358 /* no valid entry left */
1362 start = wifi_bands[i].start = wifi_bands[j].start;
1363 end = wifi_bands[i].end = wifi_bands[j].end;
1364 wifi_bands[j].start = 0;
1365 wifi_bands[j].end = 0;
1366 num_of_wbrf_ranges = j;
1369 /* eliminate duplicate entries */
1370 for (j = i + 1; j < num_of_wbrf_ranges; j++) {
1371 if ((wifi_bands[j].start == start) && (wifi_bands[j].end == end)) {
1372 wifi_bands[j].start = 0;
1373 wifi_bands[j].end = 0;
1378 /* Send the sorted wifi_bands to PMFW */
1379 ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);
1380 /* Try to set the wifi_bands again */
1381 if (unlikely(ret == -EBUSY)) {
1383 ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);
1390 * smu_wbrf_event_handler - handle notify events
1392 * @nb: notifier block
1393 * @action: event type
1396 * Calls relevant amdgpu function in response to wbrf event
1397 * notification from kernel.
1399 static int smu_wbrf_event_handler(struct notifier_block *nb,
1400 unsigned long action, void *_arg)
1402 struct smu_context *smu = container_of(nb, struct smu_context, wbrf_notifier);
1406 schedule_delayed_work(&smu->wbrf_delayed_work,
1407 msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));
1417 * smu_wbrf_delayed_work_handler - callback on delayed work timer expired
1419 * @work: struct work_struct pointer
1421 * Flood is over and driver will consume the latest exclusion ranges.
1423 static void smu_wbrf_delayed_work_handler(struct work_struct *work)
1425 struct smu_context *smu = container_of(work, struct smu_context, wbrf_delayed_work.work);
1427 smu_wbrf_handle_exclusion_ranges(smu);
1431 * smu_wbrf_support_check - check wbrf support
1433 * @smu: smu_context pointer
1435 * Verifies the ACPI interface whether wbrf is supported.
1437 static void smu_wbrf_support_check(struct smu_context *smu)
1439 struct amdgpu_device *adev = smu->adev;
1441 smu->wbrf_supported = smu_is_asic_wbrf_supported(smu) && amdgpu_wbrf &&
1442 acpi_amd_wbrf_supported_consumer(adev->dev);
1444 if (smu->wbrf_supported)
1445 dev_info(adev->dev, "RF interference mitigation is supported\n");
1449 * smu_wbrf_init - init driver wbrf support
1451 * @smu: smu_context pointer
1453 * Verifies the AMD ACPI interfaces and registers with the wbrf
1454 * notifier chain if wbrf feature is supported.
1455 * Returns 0 on success, error on failure.
1457 static int smu_wbrf_init(struct smu_context *smu)
1461 if (!smu->wbrf_supported)
1464 INIT_DELAYED_WORK(&smu->wbrf_delayed_work, smu_wbrf_delayed_work_handler);
1466 smu->wbrf_notifier.notifier_call = smu_wbrf_event_handler;
1467 ret = amd_wbrf_register_notifier(&smu->wbrf_notifier);
1472 * Some wifiband exclusion ranges may be already there
1473 * before our driver loaded. To make sure our driver
1474 * is awared of those exclusion ranges.
1476 schedule_delayed_work(&smu->wbrf_delayed_work,
1477 msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));
1483 * smu_wbrf_fini - tear down driver wbrf support
1485 * @smu: smu_context pointer
1487 * Unregisters with the wbrf notifier chain.
1489 static void smu_wbrf_fini(struct smu_context *smu)
1491 if (!smu->wbrf_supported)
1494 amd_wbrf_unregister_notifier(&smu->wbrf_notifier);
1496 cancel_delayed_work_sync(&smu->wbrf_delayed_work);
1499 static int smu_smc_hw_setup(struct smu_context *smu)
1501 struct smu_feature *feature = &smu->smu_feature;
1502 struct amdgpu_device *adev = smu->adev;
1503 uint8_t pcie_gen = 0, pcie_width = 0;
1504 uint64_t features_supported;
1507 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1508 case IP_VERSION(11, 0, 7):
1509 case IP_VERSION(11, 0, 11):
1510 case IP_VERSION(11, 5, 0):
1511 case IP_VERSION(11, 0, 12):
1512 if (adev->in_suspend && smu_is_dpm_running(smu)) {
1513 dev_info(adev->dev, "dpm has been enabled\n");
1514 ret = smu_system_features_control(smu, true);
1516 dev_err(adev->dev, "Failed system features control!\n");
1524 ret = smu_init_display_count(smu, 0);
1526 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1530 ret = smu_set_driver_table_location(smu);
1532 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1537 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1539 ret = smu_set_tool_table_location(smu);
1541 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1546 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1549 ret = smu_notify_memory_pool_location(smu);
1551 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1556 * It is assumed the pptable used before runpm is same as
1557 * the one used afterwards. Thus, we can reuse the stored
1558 * copy and do not need to resetup the pptable again.
1560 if (!adev->in_runpm) {
1561 ret = smu_setup_pptable(smu);
1563 dev_err(adev->dev, "Failed to setup pptable!\n");
1568 /* smu_dump_pptable(smu); */
1571 * With SCPM enabled, PSP is responsible for the PPTable transferring
1572 * (to SMU). Driver involvement is not needed and permitted.
1574 if (!adev->scpm_enabled) {
1576 * Copy pptable bo in the vram to smc with SMU MSGs such as
1577 * SetDriverDramAddr and TransferTableDram2Smu.
1579 ret = smu_write_pptable(smu);
1581 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1586 /* issue Run*Btc msg */
1587 ret = smu_run_btc(smu);
1591 /* Enable UclkShadow on wbrf supported */
1592 if (smu->wbrf_supported) {
1593 ret = smu_enable_uclk_shadow(smu, true);
1595 dev_err(adev->dev, "Failed to enable UclkShadow feature to support wbrf!\n");
1601 * With SCPM enabled, these actions(and relevant messages) are
1602 * not needed and permitted.
1604 if (!adev->scpm_enabled) {
1605 ret = smu_feature_set_allowed_mask(smu);
1607 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1612 ret = smu_system_features_control(smu, true);
1614 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1618 smu_init_xgmi_plpd_mode(smu);
1620 ret = smu_feature_get_enabled_mask(smu, &features_supported);
1622 dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");
1625 bitmap_copy(feature->supported,
1626 (unsigned long *)&features_supported,
1627 feature->feature_num);
1629 if (!smu_is_dpm_running(smu))
1630 dev_info(adev->dev, "dpm has been disabled\n");
1633 * Set initialized values (get from vbios) to dpm tables context such as
1634 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1637 ret = smu_set_default_dpm_table(smu);
1639 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1643 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1645 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1647 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1649 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1652 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1653 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1654 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
1656 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1658 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1660 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1662 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1664 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1666 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1668 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1670 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1674 ret = smu_get_thermal_temperature_range(smu);
1676 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1680 ret = smu_enable_thermal_alert(smu);
1682 dev_err(adev->dev, "Failed to enable thermal alert!\n");
1686 ret = smu_notify_display_change(smu);
1688 dev_err(adev->dev, "Failed to notify display change!\n");
1693 * Set min deep sleep dce fclk with bootup value from vbios via
1694 * SetMinDeepSleepDcefclk MSG.
1696 ret = smu_set_min_dcef_deep_sleep(smu,
1697 smu->smu_table.boot_values.dcefclk / 100);
1699 dev_err(adev->dev, "Error setting min deepsleep dcefclk\n");
1703 /* Init wbrf support. Properly setup the notifier */
1704 ret = smu_wbrf_init(smu);
1706 dev_err(adev->dev, "Error during wbrf init call\n");
1711 static int smu_start_smc_engine(struct smu_context *smu)
1713 struct amdgpu_device *adev = smu->adev;
1716 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1717 if (amdgpu_ip_version(adev, MP1_HWIP, 0) < IP_VERSION(11, 0, 0)) {
1718 if (smu->ppt_funcs->load_microcode) {
1719 ret = smu->ppt_funcs->load_microcode(smu);
1726 if (smu->ppt_funcs->check_fw_status) {
1727 ret = smu->ppt_funcs->check_fw_status(smu);
1729 dev_err(adev->dev, "SMC is not ready\n");
1735 * Send msg GetDriverIfVersion to check if the return value is equal
1736 * with DRIVER_IF_VERSION of smc header.
1738 ret = smu_check_fw_version(smu);
1745 static int smu_hw_init(void *handle)
1748 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1749 struct smu_context *smu = adev->powerplay.pp_handle;
1751 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1752 smu->pm_enabled = false;
1756 ret = smu_start_smc_engine(smu);
1758 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1763 * Check whether wbrf is supported. This needs to be done
1764 * before SMU setup starts since part of SMU configuration
1767 smu_wbrf_support_check(smu);
1770 ret = smu_set_gfx_imu_enable(smu);
1773 smu_dpm_set_vcn_enable(smu, true);
1774 smu_dpm_set_jpeg_enable(smu, true);
1775 smu_dpm_set_vpe_enable(smu, true);
1776 smu_dpm_set_umsch_mm_enable(smu, true);
1777 smu_set_gfx_cgpg(smu, true);
1780 if (!smu->pm_enabled)
1783 ret = smu_get_driver_allowed_feature_mask(smu);
1787 ret = smu_smc_hw_setup(smu);
1789 dev_err(adev->dev, "Failed to setup smc hw!\n");
1794 * Move maximum sustainable clock retrieving here considering
1795 * 1. It is not needed on resume(from S3).
1796 * 2. DAL settings come between .hw_init and .late_init of SMU.
1797 * And DAL needs to know the maximum sustainable clocks. Thus
1798 * it cannot be put in .late_init().
1800 ret = smu_init_max_sustainable_clocks(smu);
1802 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1806 adev->pm.dpm_enabled = true;
1808 dev_info(adev->dev, "SMU is initialized successfully!\n");
1813 static int smu_disable_dpms(struct smu_context *smu)
1815 struct amdgpu_device *adev = smu->adev;
1817 bool use_baco = !smu->is_apu &&
1818 ((amdgpu_in_reset(adev) &&
1819 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1820 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1823 * For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others)
1824 * properly on suspend/reset/unload. Driver involvement may cause some unexpected issues.
1826 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1827 case IP_VERSION(13, 0, 0):
1828 case IP_VERSION(13, 0, 7):
1829 case IP_VERSION(13, 0, 10):
1836 * For custom pptable uploading, skip the DPM features
1837 * disable process on Navi1x ASICs.
1838 * - As the gfx related features are under control of
1839 * RLC on those ASICs. RLC reinitialization will be
1840 * needed to reenable them. That will cost much more
1843 * - SMU firmware can handle the DPM reenablement
1846 if (smu->uploading_custom_pp_table) {
1847 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1848 case IP_VERSION(11, 0, 0):
1849 case IP_VERSION(11, 0, 5):
1850 case IP_VERSION(11, 0, 9):
1851 case IP_VERSION(11, 0, 7):
1852 case IP_VERSION(11, 0, 11):
1853 case IP_VERSION(11, 5, 0):
1854 case IP_VERSION(11, 0, 12):
1855 case IP_VERSION(11, 0, 13):
1863 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1864 * on BACO in. Driver involvement is unnecessary.
1867 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1868 case IP_VERSION(11, 0, 7):
1869 case IP_VERSION(11, 0, 0):
1870 case IP_VERSION(11, 0, 5):
1871 case IP_VERSION(11, 0, 9):
1872 case IP_VERSION(13, 0, 7):
1880 * For SMU 13.0.4/11 and 14.0.0, PMFW will handle the features disablement properly
1881 * for gpu reset and S0i3 cases. Driver involvement is unnecessary.
1883 if (amdgpu_in_reset(adev) || adev->in_s0ix) {
1884 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1885 case IP_VERSION(13, 0, 4):
1886 case IP_VERSION(13, 0, 11):
1887 case IP_VERSION(14, 0, 0):
1895 * For gpu reset, runpm and hibernation through BACO,
1896 * BACO feature has to be kept enabled.
1898 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1899 ret = smu_disable_all_features_with_exception(smu,
1900 SMU_FEATURE_BACO_BIT);
1902 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1904 /* DisableAllSmuFeatures message is not permitted with SCPM enabled */
1905 if (!adev->scpm_enabled) {
1906 ret = smu_system_features_control(smu, false);
1908 dev_err(adev->dev, "Failed to disable smu features.\n");
1912 /* Notify SMU RLC is going to be off, stop RLC and SMU interaction.
1913 * otherwise SMU will hang while interacting with RLC if RLC is halted
1914 * this is a WA for Vangogh asic which fix the SMU hang issue.
1916 ret = smu_notify_rlc_state(smu, false);
1918 dev_err(adev->dev, "Fail to notify rlc status!\n");
1922 if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 2) &&
1923 !((adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs) &&
1924 !amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop)
1925 adev->gfx.rlc.funcs->stop(adev);
1930 static int smu_smc_hw_cleanup(struct smu_context *smu)
1932 struct amdgpu_device *adev = smu->adev;
1937 cancel_work_sync(&smu->throttling_logging_work);
1938 cancel_work_sync(&smu->interrupt_work);
1940 ret = smu_disable_thermal_alert(smu);
1942 dev_err(adev->dev, "Fail to disable thermal alert!\n");
1946 cancel_delayed_work_sync(&smu->swctf_delayed_work);
1948 ret = smu_disable_dpms(smu);
1950 dev_err(adev->dev, "Fail to disable dpm features!\n");
1957 static int smu_hw_fini(void *handle)
1959 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1960 struct smu_context *smu = adev->powerplay.pp_handle;
1962 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1965 smu_dpm_set_vcn_enable(smu, false);
1966 smu_dpm_set_jpeg_enable(smu, false);
1967 smu_dpm_set_vpe_enable(smu, false);
1968 smu_dpm_set_umsch_mm_enable(smu, false);
1970 adev->vcn.cur_state = AMD_PG_STATE_GATE;
1971 adev->jpeg.cur_state = AMD_PG_STATE_GATE;
1973 if (!smu->pm_enabled)
1976 adev->pm.dpm_enabled = false;
1978 return smu_smc_hw_cleanup(smu);
1981 static void smu_late_fini(void *handle)
1983 struct amdgpu_device *adev = handle;
1984 struct smu_context *smu = adev->powerplay.pp_handle;
1989 static int smu_reset(struct smu_context *smu)
1991 struct amdgpu_device *adev = smu->adev;
1994 ret = smu_hw_fini(adev);
1998 ret = smu_hw_init(adev);
2002 ret = smu_late_init(adev);
2009 static int smu_suspend(void *handle)
2011 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2012 struct smu_context *smu = adev->powerplay.pp_handle;
2016 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2019 if (!smu->pm_enabled)
2022 adev->pm.dpm_enabled = false;
2024 ret = smu_smc_hw_cleanup(smu);
2028 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
2030 smu_set_gfx_cgpg(smu, false);
2033 * pwfw resets entrycount when device is suspended, so we save the
2034 * last value to be used when we resume to keep it consistent
2036 ret = smu_get_entrycount_gfxoff(smu, &count);
2038 adev->gfx.gfx_off_entrycount = count;
2043 static int smu_resume(void *handle)
2046 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2047 struct smu_context *smu = adev->powerplay.pp_handle;
2049 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
2052 if (!smu->pm_enabled)
2055 dev_info(adev->dev, "SMU is resuming...\n");
2057 ret = smu_start_smc_engine(smu);
2059 dev_err(adev->dev, "SMC engine is not correctly up!\n");
2063 ret = smu_smc_hw_setup(smu);
2065 dev_err(adev->dev, "Failed to setup smc hw!\n");
2069 ret = smu_set_gfx_imu_enable(smu);
2073 smu_set_gfx_cgpg(smu, true);
2075 smu->disable_uclk_switch = 0;
2077 adev->pm.dpm_enabled = true;
2079 dev_info(adev->dev, "SMU is resumed successfully!\n");
2084 static int smu_display_configuration_change(void *handle,
2085 const struct amd_pp_display_configuration *display_config)
2087 struct smu_context *smu = handle;
2089 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2092 if (!display_config)
2095 smu_set_min_dcef_deep_sleep(smu,
2096 display_config->min_dcef_deep_sleep_set_clk / 100);
2101 static int smu_set_clockgating_state(void *handle,
2102 enum amd_clockgating_state state)
2107 static int smu_set_powergating_state(void *handle,
2108 enum amd_powergating_state state)
2113 static int smu_enable_umd_pstate(void *handle,
2114 enum amd_dpm_forced_level *level)
2116 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
2117 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
2118 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
2119 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
2121 struct smu_context *smu = (struct smu_context*)(handle);
2122 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2124 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2127 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
2128 /* enter umd pstate, save current level, disable gfx cg*/
2129 if (*level & profile_mode_mask) {
2130 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
2131 smu_gpo_control(smu, false);
2132 smu_gfx_ulv_control(smu, false);
2133 smu_deep_sleep_control(smu, false);
2134 amdgpu_asic_update_umd_stable_pstate(smu->adev, true);
2137 /* exit umd pstate, restore level, enable gfx cg*/
2138 if (!(*level & profile_mode_mask)) {
2139 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
2140 *level = smu_dpm_ctx->saved_dpm_level;
2141 amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
2142 smu_deep_sleep_control(smu, true);
2143 smu_gfx_ulv_control(smu, true);
2144 smu_gpo_control(smu, true);
2151 static int smu_bump_power_profile_mode(struct smu_context *smu,
2153 uint32_t param_size)
2157 if (smu->ppt_funcs->set_power_profile_mode)
2158 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
2163 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
2164 enum amd_dpm_forced_level level,
2165 bool skip_display_settings)
2170 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2172 if (!skip_display_settings) {
2173 ret = smu_display_config_changed(smu);
2175 dev_err(smu->adev->dev, "Failed to change display config!");
2180 ret = smu_apply_clocks_adjust_rules(smu);
2182 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
2186 if (!skip_display_settings) {
2187 ret = smu_notify_smc_display_config(smu);
2189 dev_err(smu->adev->dev, "Failed to notify smc display config!");
2194 if (smu_dpm_ctx->dpm_level != level) {
2195 ret = smu_asic_set_performance_level(smu, level);
2197 dev_err(smu->adev->dev, "Failed to set performance level!");
2201 /* update the saved copy */
2202 smu_dpm_ctx->dpm_level = level;
2205 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
2206 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
2207 index = fls(smu->workload_mask);
2208 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
2209 workload = smu->workload_setting[index];
2211 if (smu->power_profile_mode != workload)
2212 smu_bump_power_profile_mode(smu, &workload, 0);
2218 static int smu_handle_task(struct smu_context *smu,
2219 enum amd_dpm_forced_level level,
2220 enum amd_pp_task task_id)
2224 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2228 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
2229 ret = smu_pre_display_config_changed(smu);
2232 ret = smu_adjust_power_state_dynamic(smu, level, false);
2234 case AMD_PP_TASK_COMPLETE_INIT:
2235 case AMD_PP_TASK_READJUST_POWER_STATE:
2236 ret = smu_adjust_power_state_dynamic(smu, level, true);
2245 static int smu_handle_dpm_task(void *handle,
2246 enum amd_pp_task task_id,
2247 enum amd_pm_state_type *user_state)
2249 struct smu_context *smu = handle;
2250 struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
2252 return smu_handle_task(smu, smu_dpm->dpm_level, task_id);
2256 static int smu_switch_power_profile(void *handle,
2257 enum PP_SMC_POWER_PROFILE type,
2260 struct smu_context *smu = handle;
2261 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2265 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2268 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
2272 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
2273 index = fls(smu->workload_mask);
2274 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
2275 workload = smu->workload_setting[index];
2277 smu->workload_mask |= (1 << smu->workload_prority[type]);
2278 index = fls(smu->workload_mask);
2279 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
2280 workload = smu->workload_setting[index];
2283 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
2284 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
2285 smu_bump_power_profile_mode(smu, &workload, 0);
2290 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
2292 struct smu_context *smu = handle;
2293 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2295 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2298 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2301 return smu_dpm_ctx->dpm_level;
2304 static int smu_force_performance_level(void *handle,
2305 enum amd_dpm_forced_level level)
2307 struct smu_context *smu = handle;
2308 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2311 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2314 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2317 ret = smu_enable_umd_pstate(smu, &level);
2321 ret = smu_handle_task(smu, level,
2322 AMD_PP_TASK_READJUST_POWER_STATE);
2324 /* reset user dpm clock state */
2325 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
2326 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
2327 smu->user_dpm_profile.clk_dependency = 0;
2333 static int smu_set_display_count(void *handle, uint32_t count)
2335 struct smu_context *smu = handle;
2337 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2340 return smu_init_display_count(smu, count);
2343 static int smu_force_smuclk_levels(struct smu_context *smu,
2344 enum smu_clk_type clk_type,
2347 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2350 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2353 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
2354 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
2358 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
2359 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
2360 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2361 smu->user_dpm_profile.clk_mask[clk_type] = mask;
2362 smu_set_user_clk_dependencies(smu, clk_type);
2369 static int smu_force_ppclk_levels(void *handle,
2370 enum pp_clock_type type,
2373 struct smu_context *smu = handle;
2374 enum smu_clk_type clk_type;
2378 clk_type = SMU_SCLK; break;
2380 clk_type = SMU_MCLK; break;
2382 clk_type = SMU_PCIE; break;
2384 clk_type = SMU_SOCCLK; break;
2386 clk_type = SMU_FCLK; break;
2388 clk_type = SMU_DCEFCLK; break;
2390 clk_type = SMU_VCLK; break;
2392 clk_type = SMU_VCLK1; break;
2394 clk_type = SMU_DCLK; break;
2396 clk_type = SMU_DCLK1; break;
2398 clk_type = SMU_OD_SCLK; break;
2400 clk_type = SMU_OD_MCLK; break;
2402 clk_type = SMU_OD_VDDC_CURVE; break;
2404 clk_type = SMU_OD_RANGE; break;
2409 return smu_force_smuclk_levels(smu, clk_type, mask);
2413 * On system suspending or resetting, the dpm_enabled
2414 * flag will be cleared. So that those SMU services which
2415 * are not supported will be gated.
2416 * However, the mp1 state setting should still be granted
2417 * even if the dpm_enabled cleared.
2419 static int smu_set_mp1_state(void *handle,
2420 enum pp_mp1_state mp1_state)
2422 struct smu_context *smu = handle;
2425 if (!smu->pm_enabled)
2428 if (smu->ppt_funcs &&
2429 smu->ppt_funcs->set_mp1_state)
2430 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
2435 static int smu_set_df_cstate(void *handle,
2436 enum pp_df_cstate state)
2438 struct smu_context *smu = handle;
2441 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2444 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
2447 ret = smu->ppt_funcs->set_df_cstate(smu, state);
2449 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
2454 int smu_write_watermarks_table(struct smu_context *smu)
2456 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2459 return smu_set_watermarks_table(smu, NULL);
2462 static int smu_set_watermarks_for_clock_ranges(void *handle,
2463 struct pp_smu_wm_range_sets *clock_ranges)
2465 struct smu_context *smu = handle;
2467 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2470 if (smu->disable_watermark)
2473 return smu_set_watermarks_table(smu, clock_ranges);
2476 int smu_set_ac_dc(struct smu_context *smu)
2480 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2483 /* controlled by firmware */
2484 if (smu->dc_controlled_by_gpio)
2487 ret = smu_set_power_source(smu,
2488 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
2489 SMU_POWER_SOURCE_DC);
2491 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
2492 smu->adev->pm.ac_power ? "AC" : "DC");
2497 const struct amd_ip_funcs smu_ip_funcs = {
2499 .early_init = smu_early_init,
2500 .late_init = smu_late_init,
2501 .sw_init = smu_sw_init,
2502 .sw_fini = smu_sw_fini,
2503 .hw_init = smu_hw_init,
2504 .hw_fini = smu_hw_fini,
2505 .late_fini = smu_late_fini,
2506 .suspend = smu_suspend,
2507 .resume = smu_resume,
2509 .check_soft_reset = NULL,
2510 .wait_for_idle = NULL,
2512 .set_clockgating_state = smu_set_clockgating_state,
2513 .set_powergating_state = smu_set_powergating_state,
2516 const struct amdgpu_ip_block_version smu_v11_0_ip_block = {
2517 .type = AMD_IP_BLOCK_TYPE_SMC,
2521 .funcs = &smu_ip_funcs,
2524 const struct amdgpu_ip_block_version smu_v12_0_ip_block = {
2525 .type = AMD_IP_BLOCK_TYPE_SMC,
2529 .funcs = &smu_ip_funcs,
2532 const struct amdgpu_ip_block_version smu_v13_0_ip_block = {
2533 .type = AMD_IP_BLOCK_TYPE_SMC,
2537 .funcs = &smu_ip_funcs,
2540 const struct amdgpu_ip_block_version smu_v14_0_ip_block = {
2541 .type = AMD_IP_BLOCK_TYPE_SMC,
2545 .funcs = &smu_ip_funcs,
2548 static int smu_load_microcode(void *handle)
2550 struct smu_context *smu = handle;
2551 struct amdgpu_device *adev = smu->adev;
2554 if (!smu->pm_enabled)
2557 /* This should be used for non PSP loading */
2558 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2561 if (smu->ppt_funcs->load_microcode) {
2562 ret = smu->ppt_funcs->load_microcode(smu);
2564 dev_err(adev->dev, "Load microcode failed\n");
2569 if (smu->ppt_funcs->check_fw_status) {
2570 ret = smu->ppt_funcs->check_fw_status(smu);
2572 dev_err(adev->dev, "SMC is not ready\n");
2580 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2584 if (smu->ppt_funcs->set_gfx_cgpg)
2585 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2590 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2592 struct smu_context *smu = handle;
2595 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2598 if (!smu->ppt_funcs->set_fan_speed_rpm)
2601 if (speed == U32_MAX)
2604 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2605 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2606 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;
2607 smu->user_dpm_profile.fan_speed_rpm = speed;
2609 /* Override custom PWM setting as they cannot co-exist */
2610 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;
2611 smu->user_dpm_profile.fan_speed_pwm = 0;
2618 * smu_get_power_limit - Request one of the SMU Power Limits
2620 * @handle: pointer to smu context
2621 * @limit: requested limit is written back to this variable
2622 * @pp_limit_level: &pp_power_limit_level which limit of the power to return
2623 * @pp_power_type: &pp_power_type type of power
2624 * Return: 0 on success, <0 on error
2627 int smu_get_power_limit(void *handle,
2629 enum pp_power_limit_level pp_limit_level,
2630 enum pp_power_type pp_power_type)
2632 struct smu_context *smu = handle;
2633 struct amdgpu_device *adev = smu->adev;
2634 enum smu_ppt_limit_level limit_level;
2635 uint32_t limit_type;
2638 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2641 switch (pp_power_type) {
2642 case PP_PWR_TYPE_SUSTAINED:
2643 limit_type = SMU_DEFAULT_PPT_LIMIT;
2645 case PP_PWR_TYPE_FAST:
2646 limit_type = SMU_FAST_PPT_LIMIT;
2652 switch (pp_limit_level) {
2653 case PP_PWR_LIMIT_CURRENT:
2654 limit_level = SMU_PPT_LIMIT_CURRENT;
2656 case PP_PWR_LIMIT_DEFAULT:
2657 limit_level = SMU_PPT_LIMIT_DEFAULT;
2659 case PP_PWR_LIMIT_MAX:
2660 limit_level = SMU_PPT_LIMIT_MAX;
2662 case PP_PWR_LIMIT_MIN:
2663 limit_level = SMU_PPT_LIMIT_MIN;
2669 if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2670 if (smu->ppt_funcs->get_ppt_limit)
2671 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2673 switch (limit_level) {
2674 case SMU_PPT_LIMIT_CURRENT:
2675 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
2676 case IP_VERSION(13, 0, 2):
2677 case IP_VERSION(13, 0, 6):
2678 case IP_VERSION(11, 0, 7):
2679 case IP_VERSION(11, 0, 11):
2680 case IP_VERSION(11, 0, 12):
2681 case IP_VERSION(11, 0, 13):
2682 ret = smu_get_asic_power_limits(smu,
2683 &smu->current_power_limit,
2689 *limit = smu->current_power_limit;
2691 case SMU_PPT_LIMIT_DEFAULT:
2692 *limit = smu->default_power_limit;
2694 case SMU_PPT_LIMIT_MAX:
2695 *limit = smu->max_power_limit;
2697 case SMU_PPT_LIMIT_MIN:
2698 *limit = smu->min_power_limit;
2708 static int smu_set_power_limit(void *handle, uint32_t limit)
2710 struct smu_context *smu = handle;
2711 uint32_t limit_type = limit >> 24;
2714 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2718 if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2719 if (smu->ppt_funcs->set_power_limit)
2720 return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2722 if ((limit > smu->max_power_limit) || (limit < smu->min_power_limit)) {
2723 dev_err(smu->adev->dev,
2724 "New power limit (%d) is out of range [%d,%d]\n",
2725 limit, smu->min_power_limit, smu->max_power_limit);
2730 limit = smu->current_power_limit;
2732 if (smu->ppt_funcs->set_power_limit) {
2733 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2734 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2735 smu->user_dpm_profile.power_limit = limit;
2741 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2745 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2748 if (smu->ppt_funcs->print_clk_levels)
2749 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2754 static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)
2756 enum smu_clk_type clk_type;
2760 clk_type = SMU_SCLK; break;
2762 clk_type = SMU_MCLK; break;
2764 clk_type = SMU_PCIE; break;
2766 clk_type = SMU_SOCCLK; break;
2768 clk_type = SMU_FCLK; break;
2770 clk_type = SMU_DCEFCLK; break;
2772 clk_type = SMU_VCLK; break;
2774 clk_type = SMU_VCLK1; break;
2776 clk_type = SMU_DCLK; break;
2778 clk_type = SMU_DCLK1; break;
2780 clk_type = SMU_OD_SCLK; break;
2782 clk_type = SMU_OD_MCLK; break;
2784 clk_type = SMU_OD_VDDC_CURVE; break;
2786 clk_type = SMU_OD_RANGE; break;
2787 case OD_VDDGFX_OFFSET:
2788 clk_type = SMU_OD_VDDGFX_OFFSET; break;
2790 clk_type = SMU_OD_CCLK; break;
2792 clk_type = SMU_OD_FAN_CURVE; break;
2793 case OD_ACOUSTIC_LIMIT:
2794 clk_type = SMU_OD_ACOUSTIC_LIMIT; break;
2795 case OD_ACOUSTIC_TARGET:
2796 clk_type = SMU_OD_ACOUSTIC_TARGET; break;
2797 case OD_FAN_TARGET_TEMPERATURE:
2798 clk_type = SMU_OD_FAN_TARGET_TEMPERATURE; break;
2799 case OD_FAN_MINIMUM_PWM:
2800 clk_type = SMU_OD_FAN_MINIMUM_PWM; break;
2802 clk_type = SMU_CLK_COUNT; break;
2808 static int smu_print_ppclk_levels(void *handle,
2809 enum pp_clock_type type,
2812 struct smu_context *smu = handle;
2813 enum smu_clk_type clk_type;
2815 clk_type = smu_convert_to_smuclk(type);
2816 if (clk_type == SMU_CLK_COUNT)
2819 return smu_print_smuclk_levels(smu, clk_type, buf);
2822 static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)
2824 struct smu_context *smu = handle;
2825 enum smu_clk_type clk_type;
2827 clk_type = smu_convert_to_smuclk(type);
2828 if (clk_type == SMU_CLK_COUNT)
2831 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2834 if (!smu->ppt_funcs->emit_clk_levels)
2837 return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);
2841 static int smu_od_edit_dpm_table(void *handle,
2842 enum PP_OD_DPM_TABLE_COMMAND type,
2843 long *input, uint32_t size)
2845 struct smu_context *smu = handle;
2848 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2851 if (smu->ppt_funcs->od_edit_dpm_table) {
2852 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2858 static int smu_read_sensor(void *handle,
2863 struct smu_context *smu = handle;
2864 struct smu_umd_pstate_table *pstate_table =
2867 uint32_t *size, size_val;
2869 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2872 if (!data || !size_arg)
2875 size_val = *size_arg;
2878 if (smu->ppt_funcs->read_sensor)
2879 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2883 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2884 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2887 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2888 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2891 case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
2892 *((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100;
2895 case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
2896 *((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100;
2899 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2900 ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);
2903 case AMDGPU_PP_SENSOR_UVD_POWER:
2904 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
2907 case AMDGPU_PP_SENSOR_VCE_POWER:
2908 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
2911 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
2912 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0 : 1;
2915 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
2916 *(uint32_t *)data = 0;
2926 // assign uint32_t to int
2927 *size_arg = size_val;
2932 static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit)
2934 int ret = -EOPNOTSUPP;
2935 struct smu_context *smu = handle;
2937 if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit)
2938 ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit);
2943 static int smu_set_apu_thermal_limit(void *handle, uint32_t limit)
2945 int ret = -EOPNOTSUPP;
2946 struct smu_context *smu = handle;
2948 if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit)
2949 ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit);
2954 static int smu_get_power_profile_mode(void *handle, char *buf)
2956 struct smu_context *smu = handle;
2958 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
2959 !smu->ppt_funcs->get_power_profile_mode)
2964 return smu->ppt_funcs->get_power_profile_mode(smu, buf);
2967 static int smu_set_power_profile_mode(void *handle,
2969 uint32_t param_size)
2971 struct smu_context *smu = handle;
2973 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
2974 !smu->ppt_funcs->set_power_profile_mode)
2977 return smu_bump_power_profile_mode(smu, param, param_size);
2980 static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)
2982 struct smu_context *smu = handle;
2984 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2987 if (!smu->ppt_funcs->get_fan_control_mode)
2993 *fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);
2998 static int smu_set_fan_control_mode(void *handle, u32 value)
3000 struct smu_context *smu = handle;
3003 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3006 if (!smu->ppt_funcs->set_fan_control_mode)
3009 if (value == U32_MAX)
3012 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
3016 if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
3017 smu->user_dpm_profile.fan_mode = value;
3019 /* reset user dpm fan speed */
3020 if (value != AMD_FAN_CTRL_MANUAL) {
3021 smu->user_dpm_profile.fan_speed_pwm = 0;
3022 smu->user_dpm_profile.fan_speed_rpm = 0;
3023 smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);
3031 static int smu_get_fan_speed_pwm(void *handle, u32 *speed)
3033 struct smu_context *smu = handle;
3036 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3039 if (!smu->ppt_funcs->get_fan_speed_pwm)
3045 ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);
3050 static int smu_set_fan_speed_pwm(void *handle, u32 speed)
3052 struct smu_context *smu = handle;
3055 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3058 if (!smu->ppt_funcs->set_fan_speed_pwm)
3061 if (speed == U32_MAX)
3064 ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);
3065 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
3066 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;
3067 smu->user_dpm_profile.fan_speed_pwm = speed;
3069 /* Override custom RPM setting as they cannot co-exist */
3070 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;
3071 smu->user_dpm_profile.fan_speed_rpm = 0;
3077 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
3079 struct smu_context *smu = handle;
3082 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3085 if (!smu->ppt_funcs->get_fan_speed_rpm)
3091 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
3096 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
3098 struct smu_context *smu = handle;
3100 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3103 return smu_set_min_dcef_deep_sleep(smu, clk);
3106 static int smu_get_clock_by_type_with_latency(void *handle,
3107 enum amd_pp_clock_type type,
3108 struct pp_clock_levels_with_latency *clocks)
3110 struct smu_context *smu = handle;
3111 enum smu_clk_type clk_type;
3114 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3117 if (smu->ppt_funcs->get_clock_by_type_with_latency) {
3119 case amd_pp_sys_clock:
3120 clk_type = SMU_GFXCLK;
3122 case amd_pp_mem_clock:
3123 clk_type = SMU_MCLK;
3125 case amd_pp_dcef_clock:
3126 clk_type = SMU_DCEFCLK;
3128 case amd_pp_disp_clock:
3129 clk_type = SMU_DISPCLK;
3132 dev_err(smu->adev->dev, "Invalid clock type!\n");
3136 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
3142 static int smu_display_clock_voltage_request(void *handle,
3143 struct pp_display_clock_request *clock_req)
3145 struct smu_context *smu = handle;
3148 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3151 if (smu->ppt_funcs->display_clock_voltage_request)
3152 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
3158 static int smu_display_disable_memory_clock_switch(void *handle,
3159 bool disable_memory_clock_switch)
3161 struct smu_context *smu = handle;
3164 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3167 if (smu->ppt_funcs->display_disable_memory_clock_switch)
3168 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
3173 static int smu_set_xgmi_pstate(void *handle,
3176 struct smu_context *smu = handle;
3179 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3182 if (smu->ppt_funcs->set_xgmi_pstate)
3183 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
3186 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
3191 static bool smu_get_baco_capability(void *handle)
3193 struct smu_context *smu = handle;
3195 if (!smu->pm_enabled)
3198 if (!smu->ppt_funcs || !smu->ppt_funcs->baco_is_support)
3201 return smu->ppt_funcs->baco_is_support(smu);
3204 static int smu_baco_set_state(void *handle, int state)
3206 struct smu_context *smu = handle;
3209 if (!smu->pm_enabled)
3213 if (smu->ppt_funcs->baco_exit)
3214 ret = smu->ppt_funcs->baco_exit(smu);
3215 } else if (state == 1) {
3216 if (smu->ppt_funcs->baco_enter)
3217 ret = smu->ppt_funcs->baco_enter(smu);
3223 dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
3224 (state)?"enter":"exit");
3229 bool smu_mode1_reset_is_support(struct smu_context *smu)
3233 if (!smu->pm_enabled)
3236 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
3237 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
3242 bool smu_mode2_reset_is_support(struct smu_context *smu)
3246 if (!smu->pm_enabled)
3249 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
3250 ret = smu->ppt_funcs->mode2_reset_is_support(smu);
3255 int smu_mode1_reset(struct smu_context *smu)
3259 if (!smu->pm_enabled)
3262 if (smu->ppt_funcs->mode1_reset)
3263 ret = smu->ppt_funcs->mode1_reset(smu);
3268 static int smu_mode2_reset(void *handle)
3270 struct smu_context *smu = handle;
3273 if (!smu->pm_enabled)
3276 if (smu->ppt_funcs->mode2_reset)
3277 ret = smu->ppt_funcs->mode2_reset(smu);
3280 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
3285 static int smu_enable_gfx_features(void *handle)
3287 struct smu_context *smu = handle;
3290 if (!smu->pm_enabled)
3293 if (smu->ppt_funcs->enable_gfx_features)
3294 ret = smu->ppt_funcs->enable_gfx_features(smu);
3297 dev_err(smu->adev->dev, "enable gfx features failed!\n");
3302 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
3303 struct pp_smu_nv_clock_table *max_clocks)
3305 struct smu_context *smu = handle;
3308 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3311 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
3312 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
3317 static int smu_get_uclk_dpm_states(void *handle,
3318 unsigned int *clock_values_in_khz,
3319 unsigned int *num_states)
3321 struct smu_context *smu = handle;
3324 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3327 if (smu->ppt_funcs->get_uclk_dpm_states)
3328 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
3333 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
3335 struct smu_context *smu = handle;
3336 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
3338 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3341 if (smu->ppt_funcs->get_current_power_state)
3342 pm_state = smu->ppt_funcs->get_current_power_state(smu);
3347 static int smu_get_dpm_clock_table(void *handle,
3348 struct dpm_clocks *clock_table)
3350 struct smu_context *smu = handle;
3353 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3356 if (smu->ppt_funcs->get_dpm_clock_table)
3357 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
3362 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
3364 struct smu_context *smu = handle;
3366 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3369 if (!smu->ppt_funcs->get_gpu_metrics)
3372 return smu->ppt_funcs->get_gpu_metrics(smu, table);
3375 static ssize_t smu_sys_get_pm_metrics(void *handle, void *pm_metrics,
3378 struct smu_context *smu = handle;
3380 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3383 if (!smu->ppt_funcs->get_pm_metrics)
3386 return smu->ppt_funcs->get_pm_metrics(smu, pm_metrics, size);
3389 static int smu_enable_mgpu_fan_boost(void *handle)
3391 struct smu_context *smu = handle;
3394 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3397 if (smu->ppt_funcs->enable_mgpu_fan_boost)
3398 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
3403 static int smu_gfx_state_change_set(void *handle,
3406 struct smu_context *smu = handle;
3409 if (smu->ppt_funcs->gfx_state_change_set)
3410 ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
3415 int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)
3419 if (smu->ppt_funcs->smu_handle_passthrough_sbr)
3420 ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);
3425 int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)
3427 int ret = -EOPNOTSUPP;
3429 if (smu->ppt_funcs &&
3430 smu->ppt_funcs->get_ecc_info)
3431 ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);
3437 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
3439 struct smu_context *smu = handle;
3440 struct smu_table_context *smu_table = &smu->smu_table;
3441 struct smu_table *memory_pool = &smu_table->memory_pool;
3448 if (memory_pool->bo) {
3449 *addr = memory_pool->cpu_addr;
3450 *size = memory_pool->size;
3456 int smu_set_xgmi_plpd_mode(struct smu_context *smu,
3457 enum pp_xgmi_plpd_mode mode)
3459 int ret = -EOPNOTSUPP;
3461 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3464 /* PLPD policy is not supported if it's NONE */
3465 if (smu->plpd_mode == XGMI_PLPD_NONE)
3468 if (smu->plpd_mode == mode)
3471 if (smu->ppt_funcs && smu->ppt_funcs->select_xgmi_plpd_policy)
3472 ret = smu->ppt_funcs->select_xgmi_plpd_policy(smu, mode);
3475 smu->plpd_mode = mode;
3480 static const struct amd_pm_funcs swsmu_pm_funcs = {
3481 /* export for sysfs */
3482 .set_fan_control_mode = smu_set_fan_control_mode,
3483 .get_fan_control_mode = smu_get_fan_control_mode,
3484 .set_fan_speed_pwm = smu_set_fan_speed_pwm,
3485 .get_fan_speed_pwm = smu_get_fan_speed_pwm,
3486 .force_clock_level = smu_force_ppclk_levels,
3487 .print_clock_levels = smu_print_ppclk_levels,
3488 .emit_clock_levels = smu_emit_ppclk_levels,
3489 .force_performance_level = smu_force_performance_level,
3490 .read_sensor = smu_read_sensor,
3491 .get_apu_thermal_limit = smu_get_apu_thermal_limit,
3492 .set_apu_thermal_limit = smu_set_apu_thermal_limit,
3493 .get_performance_level = smu_get_performance_level,
3494 .get_current_power_state = smu_get_current_power_state,
3495 .get_fan_speed_rpm = smu_get_fan_speed_rpm,
3496 .set_fan_speed_rpm = smu_set_fan_speed_rpm,
3497 .get_pp_num_states = smu_get_power_num_states,
3498 .get_pp_table = smu_sys_get_pp_table,
3499 .set_pp_table = smu_sys_set_pp_table,
3500 .switch_power_profile = smu_switch_power_profile,
3501 /* export to amdgpu */
3502 .dispatch_tasks = smu_handle_dpm_task,
3503 .load_firmware = smu_load_microcode,
3504 .set_powergating_by_smu = smu_dpm_set_power_gate,
3505 .set_power_limit = smu_set_power_limit,
3506 .get_power_limit = smu_get_power_limit,
3507 .get_power_profile_mode = smu_get_power_profile_mode,
3508 .set_power_profile_mode = smu_set_power_profile_mode,
3509 .odn_edit_dpm_table = smu_od_edit_dpm_table,
3510 .set_mp1_state = smu_set_mp1_state,
3511 .gfx_state_change_set = smu_gfx_state_change_set,
3513 .get_sclk = smu_get_sclk,
3514 .get_mclk = smu_get_mclk,
3515 .display_configuration_change = smu_display_configuration_change,
3516 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,
3517 .display_clock_voltage_request = smu_display_clock_voltage_request,
3518 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,
3519 .set_active_display_count = smu_set_display_count,
3520 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,
3521 .get_asic_baco_capability = smu_get_baco_capability,
3522 .set_asic_baco_state = smu_baco_set_state,
3523 .get_ppfeature_status = smu_sys_get_pp_feature_mask,
3524 .set_ppfeature_status = smu_sys_set_pp_feature_mask,
3525 .asic_reset_mode_2 = smu_mode2_reset,
3526 .asic_reset_enable_gfx_features = smu_enable_gfx_features,
3527 .set_df_cstate = smu_set_df_cstate,
3528 .set_xgmi_pstate = smu_set_xgmi_pstate,
3529 .get_gpu_metrics = smu_sys_get_gpu_metrics,
3530 .get_pm_metrics = smu_sys_get_pm_metrics,
3531 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,
3532 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
3533 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,
3534 .get_uclk_dpm_states = smu_get_uclk_dpm_states,
3535 .get_dpm_clock_table = smu_get_dpm_clock_table,
3536 .get_smu_prv_buf_details = smu_get_prv_buffer_details,
3539 int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,
3544 if (smu->ppt_funcs->wait_for_event)
3545 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
3550 int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)
3553 if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)
3556 /* Confirm the buffer allocated is of correct size */
3557 if (size != smu->stb_context.stb_buf_size)
3561 * No need to lock smu mutex as we access STB directly through MMIO
3562 * and not going through SMU messaging route (for now at least).
3563 * For registers access rely on implementation internal locking.
3565 return smu->ppt_funcs->stb_collect_info(smu, buf, size);
3568 #if defined(CONFIG_DEBUG_FS)
3570 static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)
3572 struct amdgpu_device *adev = filp->f_inode->i_private;
3573 struct smu_context *smu = adev->powerplay.pp_handle;
3577 buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);
3581 r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);
3585 filp->private_data = buf;
3594 static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
3597 struct amdgpu_device *adev = filp->f_inode->i_private;
3598 struct smu_context *smu = adev->powerplay.pp_handle;
3601 if (!filp->private_data)
3604 return simple_read_from_buffer(buf,
3606 pos, filp->private_data,
3607 smu->stb_context.stb_buf_size);
3610 static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)
3612 kvfree(filp->private_data);
3613 filp->private_data = NULL;
3619 * We have to define not only read method but also
3620 * open and release because .read takes up to PAGE_SIZE
3621 * data each time so and so is invoked multiple times.
3622 * We allocate the STB buffer in .open and release it
3625 static const struct file_operations smu_stb_debugfs_fops = {
3626 .owner = THIS_MODULE,
3627 .open = smu_stb_debugfs_open,
3628 .read = smu_stb_debugfs_read,
3629 .release = smu_stb_debugfs_release,
3630 .llseek = default_llseek,
3635 void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)
3637 #if defined(CONFIG_DEBUG_FS)
3639 struct smu_context *smu = adev->powerplay.pp_handle;
3641 if (!smu || (!smu->stb_context.stb_buf_size))
3644 debugfs_create_file_size("amdgpu_smu_stb_dump",
3646 adev_to_drm(adev)->primary->debugfs_root,
3648 &smu_stb_debugfs_fops,
3649 smu->stb_context.stb_buf_size);
3653 int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)
3657 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)
3658 ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);
3663 int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)
3667 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)
3668 ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);