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 #include <linux/firmware.h>
27 #include "amdgpu_smu.h"
28 #include "smu_internal.h"
29 #include "soc15_common.h"
30 #include "smu_v11_0.h"
31 #include "smu_v12_0.h"
34 #include "vega20_ppt.h"
35 #include "arcturus_ppt.h"
36 #include "navi10_ppt.h"
37 #include "renoir_ppt.h"
39 #undef __SMU_DUMMY_MAP
40 #define __SMU_DUMMY_MAP(type) #type
41 static const char* __smu_message_names[] = {
45 const char *smu_get_message_name(struct smu_context *smu, enum smu_message_type type)
47 if (type < 0 || type >= SMU_MSG_MAX_COUNT)
48 return "unknown smu message";
49 return __smu_message_names[type];
52 #undef __SMU_DUMMY_MAP
53 #define __SMU_DUMMY_MAP(fea) #fea
54 static const char* __smu_feature_names[] = {
58 const char *smu_get_feature_name(struct smu_context *smu, enum smu_feature_mask feature)
60 if (feature < 0 || feature >= SMU_FEATURE_COUNT)
61 return "unknown smu feature";
62 return __smu_feature_names[feature];
65 size_t smu_sys_get_pp_feature_mask(struct smu_context *smu, char *buf)
69 uint32_t feature_mask[2] = { 0 };
70 int32_t feature_index = 0;
72 uint32_t sort_feature[SMU_FEATURE_COUNT];
73 uint64_t hw_feature_count = 0;
75 mutex_lock(&smu->mutex);
77 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
81 size = sprintf(buf + size, "features high: 0x%08x low: 0x%08x\n",
82 feature_mask[1], feature_mask[0]);
84 for (i = 0; i < SMU_FEATURE_COUNT; i++) {
85 feature_index = smu_feature_get_index(smu, i);
86 if (feature_index < 0)
88 sort_feature[feature_index] = i;
92 for (i = 0; i < hw_feature_count; i++) {
93 size += sprintf(buf + size, "%02d. %-20s (%2d) : %s\n",
95 smu_get_feature_name(smu, sort_feature[i]),
97 !!smu_feature_is_enabled(smu, sort_feature[i]) ?
98 "enabled" : "disabled");
102 mutex_unlock(&smu->mutex);
107 static int smu_feature_update_enable_state(struct smu_context *smu,
108 uint64_t feature_mask,
111 struct smu_feature *feature = &smu->smu_feature;
112 uint32_t feature_low = 0, feature_high = 0;
115 if (!smu->pm_enabled)
118 feature_low = (feature_mask >> 0 ) & 0xffffffff;
119 feature_high = (feature_mask >> 32) & 0xffffffff;
122 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_EnableSmuFeaturesLow,
126 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_EnableSmuFeaturesHigh,
131 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_DisableSmuFeaturesLow,
135 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_DisableSmuFeaturesHigh,
141 mutex_lock(&feature->mutex);
143 bitmap_or(feature->enabled, feature->enabled,
144 (unsigned long *)(&feature_mask), SMU_FEATURE_MAX);
146 bitmap_andnot(feature->enabled, feature->enabled,
147 (unsigned long *)(&feature_mask), SMU_FEATURE_MAX);
148 mutex_unlock(&feature->mutex);
153 int smu_sys_set_pp_feature_mask(struct smu_context *smu, uint64_t new_mask)
156 uint32_t feature_mask[2] = { 0 };
157 uint64_t feature_2_enabled = 0;
158 uint64_t feature_2_disabled = 0;
159 uint64_t feature_enables = 0;
161 mutex_lock(&smu->mutex);
163 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
167 feature_enables = ((uint64_t)feature_mask[1] << 32 | (uint64_t)feature_mask[0]);
169 feature_2_enabled = ~feature_enables & new_mask;
170 feature_2_disabled = feature_enables & ~new_mask;
172 if (feature_2_enabled) {
173 ret = smu_feature_update_enable_state(smu, feature_2_enabled, true);
177 if (feature_2_disabled) {
178 ret = smu_feature_update_enable_state(smu, feature_2_disabled, false);
184 mutex_unlock(&smu->mutex);
189 int smu_get_smc_version(struct smu_context *smu, uint32_t *if_version, uint32_t *smu_version)
193 if (!if_version && !smu_version)
197 ret = smu_send_smc_msg(smu, SMU_MSG_GetDriverIfVersion);
201 ret = smu_read_smc_arg(smu, if_version);
207 ret = smu_send_smc_msg(smu, SMU_MSG_GetSmuVersion);
211 ret = smu_read_smc_arg(smu, smu_version);
219 int smu_set_soft_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
220 uint32_t min, uint32_t max)
224 if (min <= 0 && max <= 0)
227 if (!smu_clk_dpm_is_enabled(smu, clk_type))
230 ret = smu_set_soft_freq_limited_range(smu, clk_type, min, max);
234 int smu_set_hard_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
235 uint32_t min, uint32_t max)
237 int ret = 0, clk_id = 0;
240 if (min <= 0 && max <= 0)
243 if (!smu_clk_dpm_is_enabled(smu, clk_type))
246 clk_id = smu_clk_get_index(smu, clk_type);
251 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
252 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMaxByFreq,
259 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
260 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
270 int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
271 uint32_t *min, uint32_t *max, bool lock_needed)
273 uint32_t clock_limit;
280 mutex_lock(&smu->mutex);
282 if (!smu_clk_dpm_is_enabled(smu, clk_type)) {
286 clock_limit = smu->smu_table.boot_values.uclk;
290 clock_limit = smu->smu_table.boot_values.gfxclk;
293 clock_limit = smu->smu_table.boot_values.socclk;
300 /* clock in Mhz unit */
302 *min = clock_limit / 100;
304 *max = clock_limit / 100;
307 * Todo: Use each asic(ASIC_ppt funcs) control the callbacks exposed to the
308 * core driver and then have helpers for stuff that is common(SMU_v11_x | SMU_v12_x funcs).
310 ret = smu_get_dpm_ultimate_freq(smu, clk_type, min, max);
314 mutex_unlock(&smu->mutex);
319 int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
320 uint16_t level, uint32_t *value)
322 int ret = 0, clk_id = 0;
328 if (!smu_clk_dpm_is_enabled(smu, clk_type))
331 clk_id = smu_clk_get_index(smu, clk_type);
335 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
337 ret = smu_send_smc_msg_with_param(smu,SMU_MSG_GetDpmFreqByIndex,
342 ret = smu_read_smc_arg(smu, ¶m);
346 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
347 * now, we un-support it */
348 *value = param & 0x7fffffff;
353 int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
356 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
359 int smu_get_dpm_level_range(struct smu_context *smu, enum smu_clk_type clk_type,
360 uint32_t *min_value, uint32_t *max_value)
363 uint32_t level_count = 0;
365 if (!min_value && !max_value)
369 /* by default, level 0 clock value as min value */
370 ret = smu_get_dpm_freq_by_index(smu, clk_type, 0, min_value);
376 ret = smu_get_dpm_level_count(smu, clk_type, &level_count);
380 ret = smu_get_dpm_freq_by_index(smu, clk_type, level_count - 1, max_value);
388 bool smu_clk_dpm_is_enabled(struct smu_context *smu, enum smu_clk_type clk_type)
390 enum smu_feature_mask feature_id = 0;
395 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
399 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
402 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
408 if(!smu_feature_is_enabled(smu, feature_id)) {
416 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
418 * @smu: smu_context pointer
419 * @block_type: the IP block to power gate/ungate
420 * @gate: to power gate if true, ungate otherwise
422 * This API uses no smu->mutex lock protection due to:
423 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
424 * This is guarded to be race condition free by the caller.
425 * 2. Or get called on user setting request of power_dpm_force_performance_level.
426 * Under this case, the smu->mutex lock protection is already enforced on
427 * the parent API smu_force_performance_level of the call path.
429 int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
434 switch (block_type) {
435 case AMD_IP_BLOCK_TYPE_UVD:
436 ret = smu_dpm_set_uvd_enable(smu, gate);
438 case AMD_IP_BLOCK_TYPE_VCE:
439 ret = smu_dpm_set_vce_enable(smu, gate);
441 case AMD_IP_BLOCK_TYPE_GFX:
442 ret = smu_gfx_off_control(smu, gate);
444 case AMD_IP_BLOCK_TYPE_SDMA:
445 ret = smu_powergate_sdma(smu, gate);
447 case AMD_IP_BLOCK_TYPE_JPEG:
448 ret = smu_dpm_set_jpeg_enable(smu, gate);
457 int smu_get_power_num_states(struct smu_context *smu,
458 struct pp_states_info *state_info)
463 /* not support power state */
464 memset(state_info, 0, sizeof(struct pp_states_info));
465 state_info->nums = 1;
466 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
471 int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
472 void *data, uint32_t *size)
474 struct smu_power_context *smu_power = &smu->smu_power;
475 struct smu_power_gate *power_gate = &smu_power->power_gate;
482 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
483 *((uint32_t *)data) = smu->pstate_sclk;
486 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
487 *((uint32_t *)data) = smu->pstate_mclk;
490 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
491 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
494 case AMDGPU_PP_SENSOR_UVD_POWER:
495 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
498 case AMDGPU_PP_SENSOR_VCE_POWER:
499 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
502 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
503 *(uint32_t *)data = power_gate->vcn_gated ? 0 : 1;
517 int smu_update_table(struct smu_context *smu, enum smu_table_id table_index, int argument,
518 void *table_data, bool drv2smu)
520 struct smu_table_context *smu_table = &smu->smu_table;
521 struct amdgpu_device *adev = smu->adev;
522 struct smu_table *table = NULL;
524 int table_id = smu_table_get_index(smu, table_index);
526 if (!table_data || table_id >= SMU_TABLE_COUNT || table_id < 0)
529 table = &smu_table->tables[table_index];
532 memcpy(table->cpu_addr, table_data, table->size);
534 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrHigh,
535 upper_32_bits(table->mc_address));
538 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrLow,
539 lower_32_bits(table->mc_address));
542 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
543 SMU_MSG_TransferTableDram2Smu :
544 SMU_MSG_TransferTableSmu2Dram,
545 table_id | ((argument & 0xFFFF) << 16));
549 /* flush hdp cache */
550 adev->nbio.funcs->hdp_flush(adev, NULL);
553 memcpy(table_data, table->cpu_addr, table->size);
558 bool is_support_sw_smu(struct amdgpu_device *adev)
560 if (adev->asic_type == CHIP_VEGA20)
561 return (amdgpu_dpm == 2) ? true : false;
562 else if (adev->asic_type >= CHIP_ARCTURUS) {
563 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
571 bool is_support_sw_smu_xgmi(struct amdgpu_device *adev)
573 if (!is_support_sw_smu(adev))
576 if (adev->asic_type == CHIP_VEGA20)
582 int smu_sys_get_pp_table(struct smu_context *smu, void **table)
584 struct smu_table_context *smu_table = &smu->smu_table;
585 uint32_t powerplay_table_size;
587 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
590 mutex_lock(&smu->mutex);
592 if (smu_table->hardcode_pptable)
593 *table = smu_table->hardcode_pptable;
595 *table = smu_table->power_play_table;
597 powerplay_table_size = smu_table->power_play_table_size;
599 mutex_unlock(&smu->mutex);
601 return powerplay_table_size;
604 int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
606 struct smu_table_context *smu_table = &smu->smu_table;
607 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
610 if (!smu->pm_enabled)
612 if (header->usStructureSize != size) {
613 pr_err("pp table size not matched !\n");
617 mutex_lock(&smu->mutex);
618 if (!smu_table->hardcode_pptable)
619 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
620 if (!smu_table->hardcode_pptable) {
625 memcpy(smu_table->hardcode_pptable, buf, size);
626 smu_table->power_play_table = smu_table->hardcode_pptable;
627 smu_table->power_play_table_size = size;
630 * Special hw_fini action(for Navi1x, the DPMs disablement will be
631 * skipped) may be needed for custom pptable uploading.
633 smu->uploading_custom_pp_table = true;
635 ret = smu_reset(smu);
637 pr_info("smu reset failed, ret = %d\n", ret);
639 smu->uploading_custom_pp_table = false;
642 mutex_unlock(&smu->mutex);
646 int smu_feature_init_dpm(struct smu_context *smu)
648 struct smu_feature *feature = &smu->smu_feature;
650 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
652 if (!smu->pm_enabled)
654 mutex_lock(&feature->mutex);
655 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
656 mutex_unlock(&feature->mutex);
658 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
663 mutex_lock(&feature->mutex);
664 bitmap_or(feature->allowed, feature->allowed,
665 (unsigned long *)allowed_feature_mask,
666 feature->feature_num);
667 mutex_unlock(&feature->mutex);
673 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
675 struct smu_feature *feature = &smu->smu_feature;
682 feature_id = smu_feature_get_index(smu, mask);
686 WARN_ON(feature_id > feature->feature_num);
688 mutex_lock(&feature->mutex);
689 ret = test_bit(feature_id, feature->enabled);
690 mutex_unlock(&feature->mutex);
695 int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
698 struct smu_feature *feature = &smu->smu_feature;
701 feature_id = smu_feature_get_index(smu, mask);
705 WARN_ON(feature_id > feature->feature_num);
707 return smu_feature_update_enable_state(smu,
712 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
714 struct smu_feature *feature = &smu->smu_feature;
718 feature_id = smu_feature_get_index(smu, mask);
722 WARN_ON(feature_id > feature->feature_num);
724 mutex_lock(&feature->mutex);
725 ret = test_bit(feature_id, feature->supported);
726 mutex_unlock(&feature->mutex);
731 int smu_feature_set_supported(struct smu_context *smu,
732 enum smu_feature_mask mask,
735 struct smu_feature *feature = &smu->smu_feature;
739 feature_id = smu_feature_get_index(smu, mask);
743 WARN_ON(feature_id > feature->feature_num);
745 mutex_lock(&feature->mutex);
747 test_and_set_bit(feature_id, feature->supported);
749 test_and_clear_bit(feature_id, feature->supported);
750 mutex_unlock(&feature->mutex);
755 static int smu_set_funcs(struct amdgpu_device *adev)
757 struct smu_context *smu = &adev->smu;
759 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
760 smu->od_enabled = true;
762 switch (adev->asic_type) {
764 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
765 vega20_set_ppt_funcs(smu);
770 navi10_set_ppt_funcs(smu);
773 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
774 arcturus_set_ppt_funcs(smu);
775 /* OD is not supported on Arcturus */
776 smu->od_enabled =false;
779 renoir_set_ppt_funcs(smu);
788 static int smu_early_init(void *handle)
790 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
791 struct smu_context *smu = &adev->smu;
794 smu->pm_enabled = !!amdgpu_dpm;
796 mutex_init(&smu->mutex);
798 return smu_set_funcs(adev);
801 static int smu_late_init(void *handle)
803 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
804 struct smu_context *smu = &adev->smu;
806 if (!smu->pm_enabled)
809 smu_handle_task(&adev->smu,
810 smu->smu_dpm.dpm_level,
811 AMD_PP_TASK_COMPLETE_INIT,
817 int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
818 uint16_t *size, uint8_t *frev, uint8_t *crev,
821 struct amdgpu_device *adev = smu->adev;
824 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
825 size, frev, crev, &data_start))
828 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
833 static int smu_initialize_pptable(struct smu_context *smu)
839 static int smu_smc_table_sw_init(struct smu_context *smu)
843 ret = smu_initialize_pptable(smu);
845 pr_err("Failed to init smu_initialize_pptable!\n");
850 * Create smu_table structure, and init smc tables such as
851 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
853 ret = smu_init_smc_tables(smu);
855 pr_err("Failed to init smc tables!\n");
860 * Create smu_power_context structure, and allocate smu_dpm_context and
861 * context size to fill the smu_power_context data.
863 ret = smu_init_power(smu);
865 pr_err("Failed to init smu_init_power!\n");
872 static int smu_smc_table_sw_fini(struct smu_context *smu)
876 ret = smu_fini_smc_tables(smu);
878 pr_err("Failed to smu_fini_smc_tables!\n");
885 static int smu_sw_init(void *handle)
887 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
888 struct smu_context *smu = &adev->smu;
891 smu->pool_size = adev->pm.smu_prv_buffer_size;
892 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
893 mutex_init(&smu->smu_feature.mutex);
894 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
895 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
896 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
898 mutex_init(&smu->smu_baco.mutex);
899 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
900 smu->smu_baco.platform_support = false;
902 mutex_init(&smu->sensor_lock);
903 mutex_init(&smu->metrics_lock);
905 smu->watermarks_bitmap = 0;
906 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
907 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
909 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
910 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
911 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
912 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
913 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
914 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
915 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
916 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
918 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
919 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
920 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
921 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
922 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
923 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
924 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
925 smu->display_config = &adev->pm.pm_display_cfg;
927 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
928 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
929 ret = smu_init_microcode(smu);
931 pr_err("Failed to load smu firmware!\n");
935 ret = smu_smc_table_sw_init(smu);
937 pr_err("Failed to sw init smc table!\n");
941 ret = smu_register_irq_handler(smu);
943 pr_err("Failed to register smc irq handler!\n");
950 static int smu_sw_fini(void *handle)
952 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
953 struct smu_context *smu = &adev->smu;
956 kfree(smu->irq_source);
957 smu->irq_source = NULL;
959 ret = smu_smc_table_sw_fini(smu);
961 pr_err("Failed to sw fini smc table!\n");
965 ret = smu_fini_power(smu);
967 pr_err("Failed to init smu_fini_power!\n");
974 static int smu_init_fb_allocations(struct smu_context *smu)
976 struct amdgpu_device *adev = smu->adev;
977 struct smu_table_context *smu_table = &smu->smu_table;
978 struct smu_table *tables = smu_table->tables;
981 for (i = 0; i < SMU_TABLE_COUNT; i++) {
982 if (tables[i].size == 0)
984 ret = amdgpu_bo_create_kernel(adev,
989 &tables[i].mc_address,
990 &tables[i].cpu_addr);
998 if (tables[i].size == 0)
1000 amdgpu_bo_free_kernel(&tables[i].bo,
1001 &tables[i].mc_address,
1002 &tables[i].cpu_addr);
1008 static int smu_fini_fb_allocations(struct smu_context *smu)
1010 struct smu_table_context *smu_table = &smu->smu_table;
1011 struct smu_table *tables = smu_table->tables;
1017 for (i = 0; i < SMU_TABLE_COUNT; i++) {
1018 if (tables[i].size == 0)
1020 amdgpu_bo_free_kernel(&tables[i].bo,
1021 &tables[i].mc_address,
1022 &tables[i].cpu_addr);
1028 static int smu_smc_table_hw_init(struct smu_context *smu,
1031 struct amdgpu_device *adev = smu->adev;
1034 if (smu_is_dpm_running(smu) && adev->in_suspend) {
1035 pr_info("dpm has been enabled\n");
1039 if (adev->asic_type != CHIP_ARCTURUS) {
1040 ret = smu_init_display_count(smu, 0);
1046 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1047 ret = smu_get_vbios_bootup_values(smu);
1051 ret = smu_setup_pptable(smu);
1055 ret = smu_get_clk_info_from_vbios(smu);
1060 * check if the format_revision in vbios is up to pptable header
1061 * version, and the structure size is not 0.
1063 ret = smu_check_pptable(smu);
1068 * allocate vram bos to store smc table contents.
1070 ret = smu_init_fb_allocations(smu);
1075 * Parse pptable format and fill PPTable_t smc_pptable to
1076 * smu_table_context structure. And read the smc_dpm_table from vbios,
1077 * then fill it into smc_pptable.
1079 ret = smu_parse_pptable(smu);
1084 * Send msg GetDriverIfVersion to check if the return value is equal
1085 * with DRIVER_IF_VERSION of smc header.
1087 ret = smu_check_fw_version(smu);
1092 /* smu_dump_pptable(smu); */
1093 if (!amdgpu_sriov_vf(adev)) {
1095 * Copy pptable bo in the vram to smc with SMU MSGs such as
1096 * SetDriverDramAddr and TransferTableDram2Smu.
1098 ret = smu_write_pptable(smu);
1102 /* issue Run*Btc msg */
1103 ret = smu_run_btc(smu);
1106 ret = smu_feature_set_allowed_mask(smu);
1110 ret = smu_system_features_control(smu, true);
1114 if (adev->asic_type != CHIP_ARCTURUS) {
1115 ret = smu_notify_display_change(smu);
1120 * Set min deep sleep dce fclk with bootup value from vbios via
1121 * SetMinDeepSleepDcefclk MSG.
1123 ret = smu_set_min_dcef_deep_sleep(smu);
1129 * Set initialized values (get from vbios) to dpm tables context such as
1130 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1134 ret = smu_populate_smc_tables(smu);
1138 ret = smu_init_max_sustainable_clocks(smu);
1143 if (adev->asic_type != CHIP_ARCTURUS) {
1144 ret = smu_override_pcie_parameters(smu);
1149 ret = smu_set_default_od_settings(smu, initialize);
1154 ret = smu_populate_umd_state_clk(smu);
1158 ret = smu_get_power_limit(smu, &smu->default_power_limit, false, false);
1164 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1166 if (!amdgpu_sriov_vf(adev)) {
1167 ret = smu_set_tool_table_location(smu);
1169 if (!smu_is_dpm_running(smu))
1170 pr_info("dpm has been disabled\n");
1176 * smu_alloc_memory_pool - allocate memory pool in the system memory
1178 * @smu: amdgpu_device pointer
1180 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
1181 * and DramLogSetDramAddr can notify it changed.
1183 * Returns 0 on success, error on failure.
1185 static int smu_alloc_memory_pool(struct smu_context *smu)
1187 struct amdgpu_device *adev = smu->adev;
1188 struct smu_table_context *smu_table = &smu->smu_table;
1189 struct smu_table *memory_pool = &smu_table->memory_pool;
1190 uint64_t pool_size = smu->pool_size;
1193 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
1196 memory_pool->size = pool_size;
1197 memory_pool->align = PAGE_SIZE;
1198 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
1200 switch (pool_size) {
1201 case SMU_MEMORY_POOL_SIZE_256_MB:
1202 case SMU_MEMORY_POOL_SIZE_512_MB:
1203 case SMU_MEMORY_POOL_SIZE_1_GB:
1204 case SMU_MEMORY_POOL_SIZE_2_GB:
1205 ret = amdgpu_bo_create_kernel(adev,
1208 memory_pool->domain,
1210 &memory_pool->mc_address,
1211 &memory_pool->cpu_addr);
1220 static int smu_free_memory_pool(struct smu_context *smu)
1222 struct smu_table_context *smu_table = &smu->smu_table;
1223 struct smu_table *memory_pool = &smu_table->memory_pool;
1225 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1228 amdgpu_bo_free_kernel(&memory_pool->bo,
1229 &memory_pool->mc_address,
1230 &memory_pool->cpu_addr);
1232 memset(memory_pool, 0, sizeof(struct smu_table));
1237 static int smu_start_smc_engine(struct smu_context *smu)
1239 struct amdgpu_device *adev = smu->adev;
1242 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1243 if (adev->asic_type < CHIP_NAVI10) {
1244 if (smu->ppt_funcs->load_microcode) {
1245 ret = smu->ppt_funcs->load_microcode(smu);
1252 if (smu->ppt_funcs->check_fw_status) {
1253 ret = smu->ppt_funcs->check_fw_status(smu);
1255 pr_err("SMC is not ready\n");
1261 static int smu_hw_init(void *handle)
1264 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1265 struct smu_context *smu = &adev->smu;
1267 ret = smu_start_smc_engine(smu);
1269 pr_err("SMU is not ready yet!\n");
1274 smu_powergate_sdma(&adev->smu, false);
1275 smu_powergate_vcn(&adev->smu, false);
1276 smu_powergate_jpeg(&adev->smu, false);
1277 smu_set_gfx_cgpg(&adev->smu, true);
1280 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1283 if (!smu->pm_enabled)
1286 ret = smu_feature_init_dpm(smu);
1290 ret = smu_smc_table_hw_init(smu, true);
1294 ret = smu_alloc_memory_pool(smu);
1299 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1302 ret = smu_notify_memory_pool_location(smu);
1306 ret = smu_start_thermal_control(smu);
1310 if (!smu->pm_enabled)
1311 adev->pm.dpm_enabled = false;
1313 adev->pm.dpm_enabled = true; /* TODO: will set dpm_enabled flag while VCN and DAL DPM is workable */
1315 pr_info("SMU is initialized successfully!\n");
1323 static int smu_stop_dpms(struct smu_context *smu)
1325 return smu_system_features_control(smu, false);
1328 static int smu_hw_fini(void *handle)
1330 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1331 struct smu_context *smu = &adev->smu;
1332 struct smu_table_context *table_context = &smu->smu_table;
1335 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1339 smu_powergate_sdma(&adev->smu, true);
1340 smu_powergate_vcn(&adev->smu, true);
1341 smu_powergate_jpeg(&adev->smu, true);
1344 if (!amdgpu_sriov_vf(adev)){
1345 ret = smu_stop_thermal_control(smu);
1347 pr_warn("Fail to stop thermal control!\n");
1352 * For custom pptable uploading, skip the DPM features
1353 * disable process on Navi1x ASICs.
1354 * - As the gfx related features are under control of
1355 * RLC on those ASICs. RLC reinitialization will be
1356 * needed to reenable them. That will cost much more
1359 * - SMU firmware can handle the DPM reenablement
1362 if (!smu->uploading_custom_pp_table ||
1363 !((adev->asic_type >= CHIP_NAVI10) &&
1364 (adev->asic_type <= CHIP_NAVI12))) {
1365 ret = smu_stop_dpms(smu);
1367 pr_warn("Fail to stop Dpms!\n");
1373 kfree(table_context->driver_pptable);
1374 table_context->driver_pptable = NULL;
1376 kfree(table_context->max_sustainable_clocks);
1377 table_context->max_sustainable_clocks = NULL;
1379 kfree(table_context->overdrive_table);
1380 table_context->overdrive_table = NULL;
1382 ret = smu_fini_fb_allocations(smu);
1386 ret = smu_free_memory_pool(smu);
1393 int smu_reset(struct smu_context *smu)
1395 struct amdgpu_device *adev = smu->adev;
1398 ret = smu_hw_fini(adev);
1402 ret = smu_hw_init(adev);
1409 static int smu_suspend(void *handle)
1412 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1413 struct smu_context *smu = &adev->smu;
1414 bool baco_feature_is_enabled = false;
1417 baco_feature_is_enabled = smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT);
1419 ret = smu_system_features_control(smu, false);
1423 if (baco_feature_is_enabled) {
1424 ret = smu_feature_set_enabled(smu, SMU_FEATURE_BACO_BIT, true);
1426 pr_warn("set BACO feature enabled failed, return %d\n", ret);
1431 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1433 if (adev->asic_type >= CHIP_NAVI10 &&
1434 adev->gfx.rlc.funcs->stop)
1435 adev->gfx.rlc.funcs->stop(adev);
1437 smu_set_gfx_cgpg(&adev->smu, false);
1442 static int smu_resume(void *handle)
1445 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1446 struct smu_context *smu = &adev->smu;
1448 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1451 if (!smu->pm_enabled)
1454 pr_info("SMU is resuming...\n");
1456 ret = smu_start_smc_engine(smu);
1458 pr_err("SMU is not ready yet!\n");
1462 ret = smu_smc_table_hw_init(smu, false);
1466 ret = smu_start_thermal_control(smu);
1471 smu_set_gfx_cgpg(&adev->smu, true);
1473 smu->disable_uclk_switch = 0;
1475 pr_info("SMU is resumed successfully!\n");
1483 int smu_display_configuration_change(struct smu_context *smu,
1484 const struct amd_pp_display_configuration *display_config)
1487 int num_of_active_display = 0;
1489 if (!smu->pm_enabled || !is_support_sw_smu(smu->adev))
1492 if (!display_config)
1495 mutex_lock(&smu->mutex);
1497 if (smu->ppt_funcs->set_deep_sleep_dcefclk)
1498 smu->ppt_funcs->set_deep_sleep_dcefclk(smu,
1499 display_config->min_dcef_deep_sleep_set_clk / 100);
1501 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1502 if (display_config->displays[index].controller_id != 0)
1503 num_of_active_display++;
1506 smu_set_active_display_count(smu, num_of_active_display);
1508 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1509 display_config->cpu_cc6_disable,
1510 display_config->cpu_pstate_disable,
1511 display_config->nb_pstate_switch_disable);
1513 mutex_unlock(&smu->mutex);
1518 static int smu_get_clock_info(struct smu_context *smu,
1519 struct smu_clock_info *clk_info,
1520 enum smu_perf_level_designation designation)
1523 struct smu_performance_level level = {0};
1528 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1532 clk_info->min_mem_clk = level.memory_clock;
1533 clk_info->min_eng_clk = level.core_clock;
1534 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1536 ret = smu_get_perf_level(smu, designation, &level);
1540 clk_info->min_mem_clk = level.memory_clock;
1541 clk_info->min_eng_clk = level.core_clock;
1542 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1547 int smu_get_current_clocks(struct smu_context *smu,
1548 struct amd_pp_clock_info *clocks)
1550 struct amd_pp_simple_clock_info simple_clocks = {0};
1551 struct smu_clock_info hw_clocks;
1554 if (!is_support_sw_smu(smu->adev))
1557 mutex_lock(&smu->mutex);
1559 smu_get_dal_power_level(smu, &simple_clocks);
1561 if (smu->support_power_containment)
1562 ret = smu_get_clock_info(smu, &hw_clocks,
1563 PERF_LEVEL_POWER_CONTAINMENT);
1565 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1568 pr_err("Error in smu_get_clock_info\n");
1572 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1573 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1574 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1575 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1576 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1577 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1578 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1579 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1581 if (simple_clocks.level == 0)
1582 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1584 clocks->max_clocks_state = simple_clocks.level;
1586 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1587 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1588 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1592 mutex_unlock(&smu->mutex);
1596 static int smu_set_clockgating_state(void *handle,
1597 enum amd_clockgating_state state)
1602 static int smu_set_powergating_state(void *handle,
1603 enum amd_powergating_state state)
1608 static int smu_enable_umd_pstate(void *handle,
1609 enum amd_dpm_forced_level *level)
1611 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1612 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1613 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1614 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1616 struct smu_context *smu = (struct smu_context*)(handle);
1617 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1619 if (!smu->is_apu && (!smu->pm_enabled || !smu_dpm_ctx->dpm_context))
1622 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1623 /* enter umd pstate, save current level, disable gfx cg*/
1624 if (*level & profile_mode_mask) {
1625 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1626 smu_dpm_ctx->enable_umd_pstate = true;
1627 amdgpu_device_ip_set_clockgating_state(smu->adev,
1628 AMD_IP_BLOCK_TYPE_GFX,
1629 AMD_CG_STATE_UNGATE);
1630 amdgpu_device_ip_set_powergating_state(smu->adev,
1631 AMD_IP_BLOCK_TYPE_GFX,
1632 AMD_PG_STATE_UNGATE);
1635 /* exit umd pstate, restore level, enable gfx cg*/
1636 if (!(*level & profile_mode_mask)) {
1637 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1638 *level = smu_dpm_ctx->saved_dpm_level;
1639 smu_dpm_ctx->enable_umd_pstate = false;
1640 amdgpu_device_ip_set_clockgating_state(smu->adev,
1641 AMD_IP_BLOCK_TYPE_GFX,
1643 amdgpu_device_ip_set_powergating_state(smu->adev,
1644 AMD_IP_BLOCK_TYPE_GFX,
1652 int smu_adjust_power_state_dynamic(struct smu_context *smu,
1653 enum amd_dpm_forced_level level,
1654 bool skip_display_settings)
1659 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1661 if (!smu->pm_enabled)
1664 if (!skip_display_settings) {
1665 ret = smu_display_config_changed(smu);
1667 pr_err("Failed to change display config!");
1672 ret = smu_apply_clocks_adjust_rules(smu);
1674 pr_err("Failed to apply clocks adjust rules!");
1678 if (!skip_display_settings) {
1679 ret = smu_notify_smc_display_config(smu);
1681 pr_err("Failed to notify smc display config!");
1686 if (smu_dpm_ctx->dpm_level != level) {
1687 ret = smu_asic_set_performance_level(smu, level);
1689 pr_err("Failed to set performance level!");
1693 /* update the saved copy */
1694 smu_dpm_ctx->dpm_level = level;
1697 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1698 index = fls(smu->workload_mask);
1699 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1700 workload = smu->workload_setting[index];
1702 if (smu->power_profile_mode != workload)
1703 smu_set_power_profile_mode(smu, &workload, 0, false);
1709 int smu_handle_task(struct smu_context *smu,
1710 enum amd_dpm_forced_level level,
1711 enum amd_pp_task task_id,
1717 mutex_lock(&smu->mutex);
1720 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1721 ret = smu_pre_display_config_changed(smu);
1724 ret = smu_set_cpu_power_state(smu);
1727 ret = smu_adjust_power_state_dynamic(smu, level, false);
1729 case AMD_PP_TASK_COMPLETE_INIT:
1730 case AMD_PP_TASK_READJUST_POWER_STATE:
1731 ret = smu_adjust_power_state_dynamic(smu, level, true);
1739 mutex_unlock(&smu->mutex);
1744 int smu_switch_power_profile(struct smu_context *smu,
1745 enum PP_SMC_POWER_PROFILE type,
1748 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1752 if (!smu->pm_enabled)
1755 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1758 mutex_lock(&smu->mutex);
1761 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1762 index = fls(smu->workload_mask);
1763 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1764 workload = smu->workload_setting[index];
1766 smu->workload_mask |= (1 << smu->workload_prority[type]);
1767 index = fls(smu->workload_mask);
1768 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1769 workload = smu->workload_setting[index];
1772 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1773 smu_set_power_profile_mode(smu, &workload, 0, false);
1775 mutex_unlock(&smu->mutex);
1780 enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1782 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1783 enum amd_dpm_forced_level level;
1785 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1788 mutex_lock(&(smu->mutex));
1789 level = smu_dpm_ctx->dpm_level;
1790 mutex_unlock(&(smu->mutex));
1795 int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1797 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1800 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1803 mutex_lock(&smu->mutex);
1805 ret = smu_enable_umd_pstate(smu, &level);
1807 mutex_unlock(&smu->mutex);
1811 ret = smu_handle_task(smu, level,
1812 AMD_PP_TASK_READJUST_POWER_STATE,
1815 mutex_unlock(&smu->mutex);
1820 int smu_set_display_count(struct smu_context *smu, uint32_t count)
1824 mutex_lock(&smu->mutex);
1825 ret = smu_init_display_count(smu, count);
1826 mutex_unlock(&smu->mutex);
1831 int smu_force_clk_levels(struct smu_context *smu,
1832 enum smu_clk_type clk_type,
1836 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1839 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1840 pr_debug("force clock level is for dpm manual mode only.\n");
1845 mutex_lock(&smu->mutex);
1847 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels)
1848 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1851 mutex_unlock(&smu->mutex);
1856 int smu_set_mp1_state(struct smu_context *smu,
1857 enum pp_mp1_state mp1_state)
1863 * The SMC is not fully ready. That may be
1864 * expected as the IP may be masked.
1865 * So, just return without error.
1867 if (!smu->pm_enabled)
1870 mutex_lock(&smu->mutex);
1872 switch (mp1_state) {
1873 case PP_MP1_STATE_SHUTDOWN:
1874 msg = SMU_MSG_PrepareMp1ForShutdown;
1876 case PP_MP1_STATE_UNLOAD:
1877 msg = SMU_MSG_PrepareMp1ForUnload;
1879 case PP_MP1_STATE_RESET:
1880 msg = SMU_MSG_PrepareMp1ForReset;
1882 case PP_MP1_STATE_NONE:
1884 mutex_unlock(&smu->mutex);
1888 /* some asics may not support those messages */
1889 if (smu_msg_get_index(smu, msg) < 0) {
1890 mutex_unlock(&smu->mutex);
1894 ret = smu_send_smc_msg(smu, msg);
1896 pr_err("[PrepareMp1] Failed!\n");
1898 mutex_unlock(&smu->mutex);
1903 int smu_set_df_cstate(struct smu_context *smu,
1904 enum pp_df_cstate state)
1909 * The SMC is not fully ready. That may be
1910 * expected as the IP may be masked.
1911 * So, just return without error.
1913 if (!smu->pm_enabled)
1916 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
1919 mutex_lock(&smu->mutex);
1921 ret = smu->ppt_funcs->set_df_cstate(smu, state);
1923 pr_err("[SetDfCstate] failed!\n");
1925 mutex_unlock(&smu->mutex);
1930 int smu_write_watermarks_table(struct smu_context *smu)
1932 void *watermarks_table = smu->smu_table.watermarks_table;
1934 if (!watermarks_table)
1937 return smu_update_table(smu,
1938 SMU_TABLE_WATERMARKS,
1944 int smu_set_watermarks_for_clock_ranges(struct smu_context *smu,
1945 struct dm_pp_wm_sets_with_clock_ranges_soc15 *clock_ranges)
1947 void *table = smu->smu_table.watermarks_table;
1952 mutex_lock(&smu->mutex);
1954 if (!smu->disable_watermark &&
1955 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_DCEFCLK_BIT) &&
1956 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT)) {
1957 smu_set_watermarks_table(smu, table, clock_ranges);
1958 smu->watermarks_bitmap |= WATERMARKS_EXIST;
1959 smu->watermarks_bitmap &= ~WATERMARKS_LOADED;
1962 mutex_unlock(&smu->mutex);
1967 const struct amd_ip_funcs smu_ip_funcs = {
1969 .early_init = smu_early_init,
1970 .late_init = smu_late_init,
1971 .sw_init = smu_sw_init,
1972 .sw_fini = smu_sw_fini,
1973 .hw_init = smu_hw_init,
1974 .hw_fini = smu_hw_fini,
1975 .suspend = smu_suspend,
1976 .resume = smu_resume,
1978 .check_soft_reset = NULL,
1979 .wait_for_idle = NULL,
1981 .set_clockgating_state = smu_set_clockgating_state,
1982 .set_powergating_state = smu_set_powergating_state,
1983 .enable_umd_pstate = smu_enable_umd_pstate,
1986 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
1988 .type = AMD_IP_BLOCK_TYPE_SMC,
1992 .funcs = &smu_ip_funcs,
1995 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
1997 .type = AMD_IP_BLOCK_TYPE_SMC,
2001 .funcs = &smu_ip_funcs,
2004 int smu_load_microcode(struct smu_context *smu)
2008 mutex_lock(&smu->mutex);
2010 if (smu->ppt_funcs->load_microcode)
2011 ret = smu->ppt_funcs->load_microcode(smu);
2013 mutex_unlock(&smu->mutex);
2018 int smu_check_fw_status(struct smu_context *smu)
2022 mutex_lock(&smu->mutex);
2024 if (smu->ppt_funcs->check_fw_status)
2025 ret = smu->ppt_funcs->check_fw_status(smu);
2027 mutex_unlock(&smu->mutex);
2032 int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2036 mutex_lock(&smu->mutex);
2038 if (smu->ppt_funcs->set_gfx_cgpg)
2039 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2041 mutex_unlock(&smu->mutex);
2046 int smu_set_fan_speed_rpm(struct smu_context *smu, uint32_t speed)
2050 mutex_lock(&smu->mutex);
2052 if (smu->ppt_funcs->set_fan_speed_rpm)
2053 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2055 mutex_unlock(&smu->mutex);
2060 int smu_get_power_limit(struct smu_context *smu,
2068 mutex_lock(&smu->mutex);
2070 if (smu->ppt_funcs->get_power_limit)
2071 ret = smu->ppt_funcs->get_power_limit(smu, limit, def);
2074 mutex_unlock(&smu->mutex);
2079 int smu_set_power_limit(struct smu_context *smu, uint32_t limit)
2083 mutex_lock(&smu->mutex);
2085 if (smu->ppt_funcs->set_power_limit)
2086 ret = smu->ppt_funcs->set_power_limit(smu, limit);
2088 mutex_unlock(&smu->mutex);
2093 int smu_print_clk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2097 mutex_lock(&smu->mutex);
2099 if (smu->ppt_funcs->print_clk_levels)
2100 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2102 mutex_unlock(&smu->mutex);
2107 int smu_get_od_percentage(struct smu_context *smu, enum smu_clk_type type)
2111 mutex_lock(&smu->mutex);
2113 if (smu->ppt_funcs->get_od_percentage)
2114 ret = smu->ppt_funcs->get_od_percentage(smu, type);
2116 mutex_unlock(&smu->mutex);
2121 int smu_set_od_percentage(struct smu_context *smu, enum smu_clk_type type, uint32_t value)
2125 mutex_lock(&smu->mutex);
2127 if (smu->ppt_funcs->set_od_percentage)
2128 ret = smu->ppt_funcs->set_od_percentage(smu, type, value);
2130 mutex_unlock(&smu->mutex);
2135 int smu_od_edit_dpm_table(struct smu_context *smu,
2136 enum PP_OD_DPM_TABLE_COMMAND type,
2137 long *input, uint32_t size)
2141 mutex_lock(&smu->mutex);
2143 if (smu->ppt_funcs->od_edit_dpm_table)
2144 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2146 mutex_unlock(&smu->mutex);
2151 int smu_read_sensor(struct smu_context *smu,
2152 enum amd_pp_sensors sensor,
2153 void *data, uint32_t *size)
2157 mutex_lock(&smu->mutex);
2159 if (smu->ppt_funcs->read_sensor)
2160 ret = smu->ppt_funcs->read_sensor(smu, sensor, data, size);
2162 mutex_unlock(&smu->mutex);
2167 int smu_get_power_profile_mode(struct smu_context *smu, char *buf)
2171 mutex_lock(&smu->mutex);
2173 if (smu->ppt_funcs->get_power_profile_mode)
2174 ret = smu->ppt_funcs->get_power_profile_mode(smu, buf);
2176 mutex_unlock(&smu->mutex);
2181 int smu_set_power_profile_mode(struct smu_context *smu,
2183 uint32_t param_size,
2189 mutex_lock(&smu->mutex);
2191 if (smu->ppt_funcs->set_power_profile_mode)
2192 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
2195 mutex_unlock(&smu->mutex);
2201 int smu_get_fan_control_mode(struct smu_context *smu)
2205 mutex_lock(&smu->mutex);
2207 if (smu->ppt_funcs->get_fan_control_mode)
2208 ret = smu->ppt_funcs->get_fan_control_mode(smu);
2210 mutex_unlock(&smu->mutex);
2215 int smu_set_fan_control_mode(struct smu_context *smu, int value)
2219 mutex_lock(&smu->mutex);
2221 if (smu->ppt_funcs->set_fan_control_mode)
2222 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2224 mutex_unlock(&smu->mutex);
2229 int smu_get_fan_speed_percent(struct smu_context *smu, uint32_t *speed)
2233 mutex_lock(&smu->mutex);
2235 if (smu->ppt_funcs->get_fan_speed_percent)
2236 ret = smu->ppt_funcs->get_fan_speed_percent(smu, speed);
2238 mutex_unlock(&smu->mutex);
2243 int smu_set_fan_speed_percent(struct smu_context *smu, uint32_t speed)
2247 mutex_lock(&smu->mutex);
2249 if (smu->ppt_funcs->set_fan_speed_percent)
2250 ret = smu->ppt_funcs->set_fan_speed_percent(smu, speed);
2252 mutex_unlock(&smu->mutex);
2257 int smu_get_fan_speed_rpm(struct smu_context *smu, uint32_t *speed)
2261 mutex_lock(&smu->mutex);
2263 if (smu->ppt_funcs->get_fan_speed_rpm)
2264 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2266 mutex_unlock(&smu->mutex);
2271 int smu_set_deep_sleep_dcefclk(struct smu_context *smu, int clk)
2275 mutex_lock(&smu->mutex);
2277 if (smu->ppt_funcs->set_deep_sleep_dcefclk)
2278 ret = smu->ppt_funcs->set_deep_sleep_dcefclk(smu, clk);
2280 mutex_unlock(&smu->mutex);
2285 int smu_set_active_display_count(struct smu_context *smu, uint32_t count)
2289 if (smu->ppt_funcs->set_active_display_count)
2290 ret = smu->ppt_funcs->set_active_display_count(smu, count);
2295 int smu_get_clock_by_type(struct smu_context *smu,
2296 enum amd_pp_clock_type type,
2297 struct amd_pp_clocks *clocks)
2301 mutex_lock(&smu->mutex);
2303 if (smu->ppt_funcs->get_clock_by_type)
2304 ret = smu->ppt_funcs->get_clock_by_type(smu, type, clocks);
2306 mutex_unlock(&smu->mutex);
2311 int smu_get_max_high_clocks(struct smu_context *smu,
2312 struct amd_pp_simple_clock_info *clocks)
2316 mutex_lock(&smu->mutex);
2318 if (smu->ppt_funcs->get_max_high_clocks)
2319 ret = smu->ppt_funcs->get_max_high_clocks(smu, clocks);
2321 mutex_unlock(&smu->mutex);
2326 int smu_get_clock_by_type_with_latency(struct smu_context *smu,
2327 enum smu_clk_type clk_type,
2328 struct pp_clock_levels_with_latency *clocks)
2332 mutex_lock(&smu->mutex);
2334 if (smu->ppt_funcs->get_clock_by_type_with_latency)
2335 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2337 mutex_unlock(&smu->mutex);
2342 int smu_get_clock_by_type_with_voltage(struct smu_context *smu,
2343 enum amd_pp_clock_type type,
2344 struct pp_clock_levels_with_voltage *clocks)
2348 mutex_lock(&smu->mutex);
2350 if (smu->ppt_funcs->get_clock_by_type_with_voltage)
2351 ret = smu->ppt_funcs->get_clock_by_type_with_voltage(smu, type, clocks);
2353 mutex_unlock(&smu->mutex);
2359 int smu_display_clock_voltage_request(struct smu_context *smu,
2360 struct pp_display_clock_request *clock_req)
2364 mutex_lock(&smu->mutex);
2366 if (smu->ppt_funcs->display_clock_voltage_request)
2367 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2369 mutex_unlock(&smu->mutex);
2375 int smu_display_disable_memory_clock_switch(struct smu_context *smu, bool disable_memory_clock_switch)
2379 mutex_lock(&smu->mutex);
2381 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2382 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2384 mutex_unlock(&smu->mutex);
2389 int smu_notify_smu_enable_pwe(struct smu_context *smu)
2393 mutex_lock(&smu->mutex);
2395 if (smu->ppt_funcs->notify_smu_enable_pwe)
2396 ret = smu->ppt_funcs->notify_smu_enable_pwe(smu);
2398 mutex_unlock(&smu->mutex);
2403 int smu_set_xgmi_pstate(struct smu_context *smu,
2408 mutex_lock(&smu->mutex);
2410 if (smu->ppt_funcs->set_xgmi_pstate)
2411 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2413 mutex_unlock(&smu->mutex);
2418 int smu_set_azalia_d3_pme(struct smu_context *smu)
2422 mutex_lock(&smu->mutex);
2424 if (smu->ppt_funcs->set_azalia_d3_pme)
2425 ret = smu->ppt_funcs->set_azalia_d3_pme(smu);
2427 mutex_unlock(&smu->mutex);
2432 bool smu_baco_is_support(struct smu_context *smu)
2436 mutex_lock(&smu->mutex);
2438 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2439 ret = smu->ppt_funcs->baco_is_support(smu);
2441 mutex_unlock(&smu->mutex);
2446 int smu_baco_get_state(struct smu_context *smu, enum smu_baco_state *state)
2448 if (smu->ppt_funcs->baco_get_state)
2451 mutex_lock(&smu->mutex);
2452 *state = smu->ppt_funcs->baco_get_state(smu);
2453 mutex_unlock(&smu->mutex);
2458 int smu_baco_enter(struct smu_context *smu)
2462 mutex_lock(&smu->mutex);
2464 if (smu->ppt_funcs->baco_enter)
2465 ret = smu->ppt_funcs->baco_enter(smu);
2467 mutex_unlock(&smu->mutex);
2472 int smu_baco_exit(struct smu_context *smu)
2476 mutex_lock(&smu->mutex);
2478 if (smu->ppt_funcs->baco_exit)
2479 ret = smu->ppt_funcs->baco_exit(smu);
2481 mutex_unlock(&smu->mutex);
2486 int smu_mode2_reset(struct smu_context *smu)
2490 mutex_lock(&smu->mutex);
2492 if (smu->ppt_funcs->mode2_reset)
2493 ret = smu->ppt_funcs->mode2_reset(smu);
2495 mutex_unlock(&smu->mutex);
2500 int smu_get_max_sustainable_clocks_by_dc(struct smu_context *smu,
2501 struct pp_smu_nv_clock_table *max_clocks)
2505 mutex_lock(&smu->mutex);
2507 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2508 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2510 mutex_unlock(&smu->mutex);
2515 int smu_get_uclk_dpm_states(struct smu_context *smu,
2516 unsigned int *clock_values_in_khz,
2517 unsigned int *num_states)
2521 mutex_lock(&smu->mutex);
2523 if (smu->ppt_funcs->get_uclk_dpm_states)
2524 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2526 mutex_unlock(&smu->mutex);
2531 enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
2533 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2535 mutex_lock(&smu->mutex);
2537 if (smu->ppt_funcs->get_current_power_state)
2538 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2540 mutex_unlock(&smu->mutex);
2545 int smu_get_dpm_clock_table(struct smu_context *smu,
2546 struct dpm_clocks *clock_table)
2550 mutex_lock(&smu->mutex);
2552 if (smu->ppt_funcs->get_dpm_clock_table)
2553 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2555 mutex_unlock(&smu->mutex);
2560 uint32_t smu_get_pptable_power_limit(struct smu_context *smu)
2564 if (smu->ppt_funcs->get_pptable_power_limit)
2565 ret = smu->ppt_funcs->get_pptable_power_limit(smu);
2570 int smu_send_smc_msg(struct smu_context *smu,
2571 enum smu_message_type msg)
2575 ret = smu_send_smc_msg_with_param(smu, msg, 0);