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>
24 #include <linux/pci.h>
28 #include "amdgpu_smu.h"
29 #include "smu_internal.h"
30 #include "soc15_common.h"
31 #include "smu_v11_0.h"
32 #include "smu_v12_0.h"
35 #include "vega20_ppt.h"
36 #include "arcturus_ppt.h"
37 #include "navi10_ppt.h"
38 #include "renoir_ppt.h"
40 #undef __SMU_DUMMY_MAP
41 #define __SMU_DUMMY_MAP(type) #type
42 static const char* __smu_message_names[] = {
46 const char *smu_get_message_name(struct smu_context *smu, enum smu_message_type type)
48 if (type < 0 || type >= SMU_MSG_MAX_COUNT)
49 return "unknown smu message";
50 return __smu_message_names[type];
53 #undef __SMU_DUMMY_MAP
54 #define __SMU_DUMMY_MAP(fea) #fea
55 static const char* __smu_feature_names[] = {
59 const char *smu_get_feature_name(struct smu_context *smu, enum smu_feature_mask feature)
61 if (feature < 0 || feature >= SMU_FEATURE_COUNT)
62 return "unknown smu feature";
63 return __smu_feature_names[feature];
66 size_t smu_sys_get_pp_feature_mask(struct smu_context *smu, char *buf)
70 uint32_t feature_mask[2] = { 0 };
71 int32_t feature_index = 0;
73 uint32_t sort_feature[SMU_FEATURE_COUNT];
74 uint64_t hw_feature_count = 0;
76 mutex_lock(&smu->mutex);
78 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
82 size = sprintf(buf + size, "features high: 0x%08x low: 0x%08x\n",
83 feature_mask[1], feature_mask[0]);
85 for (i = 0; i < SMU_FEATURE_COUNT; i++) {
86 feature_index = smu_feature_get_index(smu, i);
87 if (feature_index < 0)
89 sort_feature[feature_index] = i;
93 for (i = 0; i < hw_feature_count; i++) {
94 size += sprintf(buf + size, "%02d. %-20s (%2d) : %s\n",
96 smu_get_feature_name(smu, sort_feature[i]),
98 !!smu_feature_is_enabled(smu, sort_feature[i]) ?
99 "enabled" : "disabled");
103 mutex_unlock(&smu->mutex);
108 static int smu_feature_update_enable_state(struct smu_context *smu,
109 uint64_t feature_mask,
112 struct smu_feature *feature = &smu->smu_feature;
113 uint32_t feature_low = 0, feature_high = 0;
116 if (!smu->pm_enabled)
119 feature_low = (feature_mask >> 0 ) & 0xffffffff;
120 feature_high = (feature_mask >> 32) & 0xffffffff;
123 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_EnableSmuFeaturesLow,
127 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_EnableSmuFeaturesHigh,
132 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_DisableSmuFeaturesLow,
136 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_DisableSmuFeaturesHigh,
142 mutex_lock(&feature->mutex);
144 bitmap_or(feature->enabled, feature->enabled,
145 (unsigned long *)(&feature_mask), SMU_FEATURE_MAX);
147 bitmap_andnot(feature->enabled, feature->enabled,
148 (unsigned long *)(&feature_mask), SMU_FEATURE_MAX);
149 mutex_unlock(&feature->mutex);
154 int smu_sys_set_pp_feature_mask(struct smu_context *smu, uint64_t new_mask)
157 uint32_t feature_mask[2] = { 0 };
158 uint64_t feature_2_enabled = 0;
159 uint64_t feature_2_disabled = 0;
160 uint64_t feature_enables = 0;
162 mutex_lock(&smu->mutex);
164 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
168 feature_enables = ((uint64_t)feature_mask[1] << 32 | (uint64_t)feature_mask[0]);
170 feature_2_enabled = ~feature_enables & new_mask;
171 feature_2_disabled = feature_enables & ~new_mask;
173 if (feature_2_enabled) {
174 ret = smu_feature_update_enable_state(smu, feature_2_enabled, true);
178 if (feature_2_disabled) {
179 ret = smu_feature_update_enable_state(smu, feature_2_disabled, false);
185 mutex_unlock(&smu->mutex);
190 int smu_get_smc_version(struct smu_context *smu, uint32_t *if_version, uint32_t *smu_version)
194 if (!if_version && !smu_version)
198 ret = smu_send_smc_msg(smu, SMU_MSG_GetDriverIfVersion);
202 ret = smu_read_smc_arg(smu, if_version);
208 ret = smu_send_smc_msg(smu, SMU_MSG_GetSmuVersion);
212 ret = smu_read_smc_arg(smu, smu_version);
220 int smu_set_soft_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
221 uint32_t min, uint32_t max)
225 if (min < 0 && max < 0)
228 if (!smu_clk_dpm_is_enabled(smu, clk_type))
231 ret = smu_set_soft_freq_limited_range(smu, clk_type, min, max);
235 int smu_set_hard_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
236 uint32_t min, uint32_t max)
238 int ret = 0, clk_id = 0;
241 if (min <= 0 && max <= 0)
244 if (!smu_clk_dpm_is_enabled(smu, clk_type))
247 clk_id = smu_clk_get_index(smu, clk_type);
252 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
253 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMaxByFreq,
260 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
261 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
271 int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
272 uint32_t *min, uint32_t *max, bool lock_needed)
274 uint32_t clock_limit;
281 mutex_lock(&smu->mutex);
283 if (!smu_clk_dpm_is_enabled(smu, clk_type)) {
287 clock_limit = smu->smu_table.boot_values.uclk;
291 clock_limit = smu->smu_table.boot_values.gfxclk;
294 clock_limit = smu->smu_table.boot_values.socclk;
301 /* clock in Mhz unit */
303 *min = clock_limit / 100;
305 *max = clock_limit / 100;
308 * Todo: Use each asic(ASIC_ppt funcs) control the callbacks exposed to the
309 * core driver and then have helpers for stuff that is common(SMU_v11_x | SMU_v12_x funcs).
311 ret = smu_get_dpm_ultimate_freq(smu, clk_type, min, max);
315 mutex_unlock(&smu->mutex);
320 int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
321 uint16_t level, uint32_t *value)
323 int ret = 0, clk_id = 0;
329 if (!smu_clk_dpm_is_enabled(smu, clk_type))
332 clk_id = smu_clk_get_index(smu, clk_type);
336 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
338 ret = smu_send_smc_msg_with_param(smu,SMU_MSG_GetDpmFreqByIndex,
343 ret = smu_read_smc_arg(smu, ¶m);
347 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
348 * now, we un-support it */
349 *value = param & 0x7fffffff;
354 int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
357 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
360 int smu_get_dpm_level_range(struct smu_context *smu, enum smu_clk_type clk_type,
361 uint32_t *min_value, uint32_t *max_value)
364 uint32_t level_count = 0;
366 if (!min_value && !max_value)
370 /* by default, level 0 clock value as min value */
371 ret = smu_get_dpm_freq_by_index(smu, clk_type, 0, min_value);
377 ret = smu_get_dpm_level_count(smu, clk_type, &level_count);
381 ret = smu_get_dpm_freq_by_index(smu, clk_type, level_count - 1, max_value);
389 bool smu_clk_dpm_is_enabled(struct smu_context *smu, enum smu_clk_type clk_type)
391 enum smu_feature_mask feature_id = 0;
396 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
400 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
403 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
409 if(!smu_feature_is_enabled(smu, feature_id)) {
417 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
419 * @smu: smu_context pointer
420 * @block_type: the IP block to power gate/ungate
421 * @gate: to power gate if true, ungate otherwise
423 * This API uses no smu->mutex lock protection due to:
424 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
425 * This is guarded to be race condition free by the caller.
426 * 2. Or get called on user setting request of power_dpm_force_performance_level.
427 * Under this case, the smu->mutex lock protection is already enforced on
428 * the parent API smu_force_performance_level of the call path.
430 int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
435 switch (block_type) {
436 case AMD_IP_BLOCK_TYPE_UVD:
437 ret = smu_dpm_set_uvd_enable(smu, !gate);
439 case AMD_IP_BLOCK_TYPE_VCE:
440 ret = smu_dpm_set_vce_enable(smu, !gate);
442 case AMD_IP_BLOCK_TYPE_GFX:
443 ret = smu_gfx_off_control(smu, gate);
445 case AMD_IP_BLOCK_TYPE_SDMA:
446 ret = smu_powergate_sdma(smu, gate);
448 case AMD_IP_BLOCK_TYPE_JPEG:
449 ret = smu_dpm_set_jpeg_enable(smu, !gate);
458 int smu_get_power_num_states(struct smu_context *smu,
459 struct pp_states_info *state_info)
464 /* not support power state */
465 memset(state_info, 0, sizeof(struct pp_states_info));
466 state_info->nums = 1;
467 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
472 int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
473 void *data, uint32_t *size)
475 struct smu_power_context *smu_power = &smu->smu_power;
476 struct smu_power_gate *power_gate = &smu_power->power_gate;
483 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
484 *((uint32_t *)data) = smu->pstate_sclk;
487 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
488 *((uint32_t *)data) = smu->pstate_mclk;
491 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
492 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
495 case AMDGPU_PP_SENSOR_UVD_POWER:
496 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
499 case AMDGPU_PP_SENSOR_VCE_POWER:
500 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
503 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
504 *(uint32_t *)data = power_gate->vcn_gated ? 0 : 1;
518 int smu_update_table(struct smu_context *smu, enum smu_table_id table_index, int argument,
519 void *table_data, bool drv2smu)
521 struct smu_table_context *smu_table = &smu->smu_table;
522 struct amdgpu_device *adev = smu->adev;
523 struct smu_table *table = &smu_table->driver_table;
524 int table_id = smu_table_get_index(smu, table_index);
528 if (!table_data || table_id >= SMU_TABLE_COUNT || table_id < 0)
531 table_size = smu_table->tables[table_index].size;
534 memcpy(table->cpu_addr, table_data, table_size);
536 * Flush hdp cache: to guard the content seen by
537 * GPU is consitent with CPU.
539 amdgpu_asic_flush_hdp(adev, NULL);
542 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
543 SMU_MSG_TransferTableDram2Smu :
544 SMU_MSG_TransferTableSmu2Dram,
545 table_id | ((argument & 0xFFFF) << 16));
550 amdgpu_asic_flush_hdp(adev, NULL);
551 memcpy(table_data, table->cpu_addr, table_size);
557 bool is_support_sw_smu(struct amdgpu_device *adev)
559 if (adev->asic_type == CHIP_VEGA20)
560 return (amdgpu_dpm == 2) ? true : false;
561 else if (adev->asic_type >= CHIP_ARCTURUS) {
562 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
570 bool is_support_sw_smu_xgmi(struct amdgpu_device *adev)
572 if (!is_support_sw_smu(adev))
575 if (adev->asic_type == CHIP_VEGA20)
581 int smu_sys_get_pp_table(struct smu_context *smu, void **table)
583 struct smu_table_context *smu_table = &smu->smu_table;
584 uint32_t powerplay_table_size;
586 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
589 mutex_lock(&smu->mutex);
591 if (smu_table->hardcode_pptable)
592 *table = smu_table->hardcode_pptable;
594 *table = smu_table->power_play_table;
596 powerplay_table_size = smu_table->power_play_table_size;
598 mutex_unlock(&smu->mutex);
600 return powerplay_table_size;
603 int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
605 struct smu_table_context *smu_table = &smu->smu_table;
606 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
609 if (!smu->pm_enabled)
611 if (header->usStructureSize != size) {
612 pr_err("pp table size not matched !\n");
616 mutex_lock(&smu->mutex);
617 if (!smu_table->hardcode_pptable)
618 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
619 if (!smu_table->hardcode_pptable) {
624 memcpy(smu_table->hardcode_pptable, buf, size);
625 smu_table->power_play_table = smu_table->hardcode_pptable;
626 smu_table->power_play_table_size = size;
629 * Special hw_fini action(for Navi1x, the DPMs disablement will be
630 * skipped) may be needed for custom pptable uploading.
632 smu->uploading_custom_pp_table = true;
634 ret = smu_reset(smu);
636 pr_info("smu reset failed, ret = %d\n", ret);
638 smu->uploading_custom_pp_table = false;
641 mutex_unlock(&smu->mutex);
645 int smu_feature_init_dpm(struct smu_context *smu)
647 struct smu_feature *feature = &smu->smu_feature;
649 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
651 if (!smu->pm_enabled)
653 mutex_lock(&feature->mutex);
654 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
655 mutex_unlock(&feature->mutex);
657 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
662 mutex_lock(&feature->mutex);
663 bitmap_or(feature->allowed, feature->allowed,
664 (unsigned long *)allowed_feature_mask,
665 feature->feature_num);
666 mutex_unlock(&feature->mutex);
672 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
674 struct smu_feature *feature = &smu->smu_feature;
681 feature_id = smu_feature_get_index(smu, mask);
685 WARN_ON(feature_id > feature->feature_num);
687 mutex_lock(&feature->mutex);
688 ret = test_bit(feature_id, feature->enabled);
689 mutex_unlock(&feature->mutex);
694 int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
697 struct smu_feature *feature = &smu->smu_feature;
700 feature_id = smu_feature_get_index(smu, mask);
704 WARN_ON(feature_id > feature->feature_num);
706 return smu_feature_update_enable_state(smu,
711 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
713 struct smu_feature *feature = &smu->smu_feature;
717 feature_id = smu_feature_get_index(smu, mask);
721 WARN_ON(feature_id > feature->feature_num);
723 mutex_lock(&feature->mutex);
724 ret = test_bit(feature_id, feature->supported);
725 mutex_unlock(&feature->mutex);
730 int smu_feature_set_supported(struct smu_context *smu,
731 enum smu_feature_mask mask,
734 struct smu_feature *feature = &smu->smu_feature;
738 feature_id = smu_feature_get_index(smu, mask);
742 WARN_ON(feature_id > feature->feature_num);
744 mutex_lock(&feature->mutex);
746 test_and_set_bit(feature_id, feature->supported);
748 test_and_clear_bit(feature_id, feature->supported);
749 mutex_unlock(&feature->mutex);
754 static int smu_set_funcs(struct amdgpu_device *adev)
756 struct smu_context *smu = &adev->smu;
758 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
759 smu->od_enabled = true;
761 switch (adev->asic_type) {
763 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
764 vega20_set_ppt_funcs(smu);
769 navi10_set_ppt_funcs(smu);
772 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
773 arcturus_set_ppt_funcs(smu);
774 /* OD is not supported on Arcturus */
775 smu->od_enabled =false;
778 renoir_set_ppt_funcs(smu);
787 static int smu_early_init(void *handle)
789 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
790 struct smu_context *smu = &adev->smu;
793 smu->pm_enabled = !!amdgpu_dpm;
795 mutex_init(&smu->mutex);
797 return smu_set_funcs(adev);
800 static int smu_late_init(void *handle)
802 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
803 struct smu_context *smu = &adev->smu;
805 if (!smu->pm_enabled)
808 smu_handle_task(&adev->smu,
809 smu->smu_dpm.dpm_level,
810 AMD_PP_TASK_COMPLETE_INIT,
816 int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
817 uint16_t *size, uint8_t *frev, uint8_t *crev,
820 struct amdgpu_device *adev = smu->adev;
823 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
824 size, frev, crev, &data_start))
827 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
832 static int smu_initialize_pptable(struct smu_context *smu)
838 static int smu_smc_table_sw_init(struct smu_context *smu)
842 ret = smu_initialize_pptable(smu);
844 pr_err("Failed to init smu_initialize_pptable!\n");
849 * Create smu_table structure, and init smc tables such as
850 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
852 ret = smu_init_smc_tables(smu);
854 pr_err("Failed to init smc tables!\n");
859 * Create smu_power_context structure, and allocate smu_dpm_context and
860 * context size to fill the smu_power_context data.
862 ret = smu_init_power(smu);
864 pr_err("Failed to init smu_init_power!\n");
871 static int smu_smc_table_sw_fini(struct smu_context *smu)
875 ret = smu_fini_smc_tables(smu);
877 pr_err("Failed to smu_fini_smc_tables!\n");
884 static int smu_sw_init(void *handle)
886 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
887 struct smu_context *smu = &adev->smu;
890 smu->pool_size = adev->pm.smu_prv_buffer_size;
891 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
892 mutex_init(&smu->smu_feature.mutex);
893 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
894 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
895 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
897 mutex_init(&smu->smu_baco.mutex);
898 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
899 smu->smu_baco.platform_support = false;
901 mutex_init(&smu->sensor_lock);
902 mutex_init(&smu->metrics_lock);
904 smu->watermarks_bitmap = 0;
905 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
906 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
908 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
909 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
910 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
911 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
912 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
913 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
914 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
915 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
917 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
918 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
919 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
920 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
921 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
922 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
923 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
924 smu->display_config = &adev->pm.pm_display_cfg;
926 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
927 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
928 ret = smu_init_microcode(smu);
930 pr_err("Failed to load smu firmware!\n");
934 ret = smu_smc_table_sw_init(smu);
936 pr_err("Failed to sw init smc table!\n");
940 ret = smu_register_irq_handler(smu);
942 pr_err("Failed to register smc irq handler!\n");
949 static int smu_sw_fini(void *handle)
951 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
952 struct smu_context *smu = &adev->smu;
955 kfree(smu->irq_source);
956 smu->irq_source = NULL;
958 ret = smu_smc_table_sw_fini(smu);
960 pr_err("Failed to sw fini smc table!\n");
964 ret = smu_fini_power(smu);
966 pr_err("Failed to init smu_fini_power!\n");
973 static int smu_init_fb_allocations(struct smu_context *smu)
975 struct amdgpu_device *adev = smu->adev;
976 struct smu_table_context *smu_table = &smu->smu_table;
977 struct smu_table *tables = smu_table->tables;
978 struct smu_table *driver_table = &(smu_table->driver_table);
979 uint32_t max_table_size = 0;
982 /* VRAM allocation for tool table */
983 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
984 ret = amdgpu_bo_create_kernel(adev,
985 tables[SMU_TABLE_PMSTATUSLOG].size,
986 tables[SMU_TABLE_PMSTATUSLOG].align,
987 tables[SMU_TABLE_PMSTATUSLOG].domain,
988 &tables[SMU_TABLE_PMSTATUSLOG].bo,
989 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
990 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
992 pr_err("VRAM allocation for tool table failed!\n");
997 /* VRAM allocation for driver table */
998 for (i = 0; i < SMU_TABLE_COUNT; i++) {
999 if (tables[i].size == 0)
1002 if (i == SMU_TABLE_PMSTATUSLOG)
1005 if (max_table_size < tables[i].size)
1006 max_table_size = tables[i].size;
1009 driver_table->size = max_table_size;
1010 driver_table->align = PAGE_SIZE;
1011 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
1013 ret = amdgpu_bo_create_kernel(adev,
1015 driver_table->align,
1016 driver_table->domain,
1018 &driver_table->mc_address,
1019 &driver_table->cpu_addr);
1021 pr_err("VRAM allocation for driver table failed!\n");
1022 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
1023 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
1024 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
1025 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
1031 static int smu_fini_fb_allocations(struct smu_context *smu)
1033 struct smu_table_context *smu_table = &smu->smu_table;
1034 struct smu_table *tables = smu_table->tables;
1035 struct smu_table *driver_table = &(smu_table->driver_table);
1040 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
1041 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
1042 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
1043 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
1045 amdgpu_bo_free_kernel(&driver_table->bo,
1046 &driver_table->mc_address,
1047 &driver_table->cpu_addr);
1052 static int smu_smc_table_hw_init(struct smu_context *smu,
1055 struct amdgpu_device *adev = smu->adev;
1058 if (smu_is_dpm_running(smu) && adev->in_suspend) {
1059 pr_info("dpm has been enabled\n");
1063 if (adev->asic_type != CHIP_ARCTURUS) {
1064 ret = smu_init_display_count(smu, 0);
1070 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1071 ret = smu_get_vbios_bootup_values(smu);
1075 ret = smu_setup_pptable(smu);
1079 ret = smu_get_clk_info_from_vbios(smu);
1084 * check if the format_revision in vbios is up to pptable header
1085 * version, and the structure size is not 0.
1087 ret = smu_check_pptable(smu);
1092 * allocate vram bos to store smc table contents.
1094 ret = smu_init_fb_allocations(smu);
1099 * Parse pptable format and fill PPTable_t smc_pptable to
1100 * smu_table_context structure. And read the smc_dpm_table from vbios,
1101 * then fill it into smc_pptable.
1103 ret = smu_parse_pptable(smu);
1108 * Send msg GetDriverIfVersion to check if the return value is equal
1109 * with DRIVER_IF_VERSION of smc header.
1111 ret = smu_check_fw_version(smu);
1116 /* smu_dump_pptable(smu); */
1117 if (!amdgpu_sriov_vf(adev)) {
1118 ret = smu_set_driver_table_location(smu);
1123 * Copy pptable bo in the vram to smc with SMU MSGs such as
1124 * SetDriverDramAddr and TransferTableDram2Smu.
1126 ret = smu_write_pptable(smu);
1130 /* issue Run*Btc msg */
1131 ret = smu_run_btc(smu);
1134 ret = smu_feature_set_allowed_mask(smu);
1138 ret = smu_system_features_control(smu, true);
1142 if (adev->asic_type == CHIP_NAVI10) {
1143 if ((adev->pdev->device == 0x731f && (adev->pdev->revision == 0xc2 ||
1144 adev->pdev->revision == 0xc3 ||
1145 adev->pdev->revision == 0xca ||
1146 adev->pdev->revision == 0xcb)) ||
1147 (adev->pdev->device == 0x66af && (adev->pdev->revision == 0xf3 ||
1148 adev->pdev->revision == 0xf4 ||
1149 adev->pdev->revision == 0xf5 ||
1150 adev->pdev->revision == 0xf6))) {
1151 ret = smu_disable_umc_cdr_12gbps_workaround(smu);
1153 pr_err("Workaround failed to disable UMC CDR feature on 12Gbps SKU!\n");
1159 if (adev->asic_type != CHIP_ARCTURUS) {
1160 ret = smu_notify_display_change(smu);
1165 * Set min deep sleep dce fclk with bootup value from vbios via
1166 * SetMinDeepSleepDcefclk MSG.
1168 ret = smu_set_min_dcef_deep_sleep(smu);
1174 * Set initialized values (get from vbios) to dpm tables context such as
1175 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1179 ret = smu_populate_smc_tables(smu);
1183 ret = smu_init_max_sustainable_clocks(smu);
1188 if (adev->asic_type != CHIP_ARCTURUS) {
1189 ret = smu_override_pcie_parameters(smu);
1194 ret = smu_set_default_od_settings(smu, initialize);
1199 ret = smu_populate_umd_state_clk(smu);
1203 ret = smu_get_power_limit(smu, &smu->default_power_limit, false, false);
1209 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1211 if (!amdgpu_sriov_vf(adev)) {
1212 ret = smu_set_tool_table_location(smu);
1214 if (!smu_is_dpm_running(smu))
1215 pr_info("dpm has been disabled\n");
1221 * smu_alloc_memory_pool - allocate memory pool in the system memory
1223 * @smu: amdgpu_device pointer
1225 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
1226 * and DramLogSetDramAddr can notify it changed.
1228 * Returns 0 on success, error on failure.
1230 static int smu_alloc_memory_pool(struct smu_context *smu)
1232 struct amdgpu_device *adev = smu->adev;
1233 struct smu_table_context *smu_table = &smu->smu_table;
1234 struct smu_table *memory_pool = &smu_table->memory_pool;
1235 uint64_t pool_size = smu->pool_size;
1238 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
1241 memory_pool->size = pool_size;
1242 memory_pool->align = PAGE_SIZE;
1243 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
1245 switch (pool_size) {
1246 case SMU_MEMORY_POOL_SIZE_256_MB:
1247 case SMU_MEMORY_POOL_SIZE_512_MB:
1248 case SMU_MEMORY_POOL_SIZE_1_GB:
1249 case SMU_MEMORY_POOL_SIZE_2_GB:
1250 ret = amdgpu_bo_create_kernel(adev,
1253 memory_pool->domain,
1255 &memory_pool->mc_address,
1256 &memory_pool->cpu_addr);
1265 static int smu_free_memory_pool(struct smu_context *smu)
1267 struct smu_table_context *smu_table = &smu->smu_table;
1268 struct smu_table *memory_pool = &smu_table->memory_pool;
1270 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1273 amdgpu_bo_free_kernel(&memory_pool->bo,
1274 &memory_pool->mc_address,
1275 &memory_pool->cpu_addr);
1277 memset(memory_pool, 0, sizeof(struct smu_table));
1282 static int smu_start_smc_engine(struct smu_context *smu)
1284 struct amdgpu_device *adev = smu->adev;
1287 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1288 if (adev->asic_type < CHIP_NAVI10) {
1289 if (smu->ppt_funcs->load_microcode) {
1290 ret = smu->ppt_funcs->load_microcode(smu);
1297 if (smu->ppt_funcs->check_fw_status) {
1298 ret = smu->ppt_funcs->check_fw_status(smu);
1300 pr_err("SMC is not ready\n");
1306 static int smu_hw_init(void *handle)
1309 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1310 struct smu_context *smu = &adev->smu;
1312 ret = smu_start_smc_engine(smu);
1314 pr_err("SMU is not ready yet!\n");
1319 smu_powergate_sdma(&adev->smu, false);
1320 smu_powergate_vcn(&adev->smu, false);
1321 smu_powergate_jpeg(&adev->smu, false);
1322 smu_set_gfx_cgpg(&adev->smu, true);
1325 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1328 if (!smu->pm_enabled)
1331 ret = smu_feature_init_dpm(smu);
1335 ret = smu_smc_table_hw_init(smu, true);
1339 ret = smu_alloc_memory_pool(smu);
1344 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1347 ret = smu_notify_memory_pool_location(smu);
1351 ret = smu_start_thermal_control(smu);
1355 if (!smu->pm_enabled)
1356 adev->pm.dpm_enabled = false;
1358 adev->pm.dpm_enabled = true; /* TODO: will set dpm_enabled flag while VCN and DAL DPM is workable */
1360 pr_info("SMU is initialized successfully!\n");
1368 static int smu_stop_dpms(struct smu_context *smu)
1370 return smu_system_features_control(smu, false);
1373 static int smu_hw_fini(void *handle)
1375 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1376 struct smu_context *smu = &adev->smu;
1377 struct smu_table_context *table_context = &smu->smu_table;
1380 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1384 smu_powergate_sdma(&adev->smu, true);
1385 smu_powergate_vcn(&adev->smu, true);
1386 smu_powergate_jpeg(&adev->smu, true);
1389 if (!smu->pm_enabled)
1392 if (!amdgpu_sriov_vf(adev)){
1393 ret = smu_stop_thermal_control(smu);
1395 pr_warn("Fail to stop thermal control!\n");
1400 * For custom pptable uploading, skip the DPM features
1401 * disable process on Navi1x ASICs.
1402 * - As the gfx related features are under control of
1403 * RLC on those ASICs. RLC reinitialization will be
1404 * needed to reenable them. That will cost much more
1407 * - SMU firmware can handle the DPM reenablement
1410 if (!smu->uploading_custom_pp_table ||
1411 !((adev->asic_type >= CHIP_NAVI10) &&
1412 (adev->asic_type <= CHIP_NAVI12))) {
1413 ret = smu_stop_dpms(smu);
1415 pr_warn("Fail to stop Dpms!\n");
1421 kfree(table_context->driver_pptable);
1422 table_context->driver_pptable = NULL;
1424 kfree(table_context->max_sustainable_clocks);
1425 table_context->max_sustainable_clocks = NULL;
1427 kfree(table_context->overdrive_table);
1428 table_context->overdrive_table = NULL;
1430 ret = smu_fini_fb_allocations(smu);
1434 ret = smu_free_memory_pool(smu);
1441 int smu_reset(struct smu_context *smu)
1443 struct amdgpu_device *adev = smu->adev;
1446 ret = smu_hw_fini(adev);
1450 ret = smu_hw_init(adev);
1457 static int smu_suspend(void *handle)
1460 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1461 struct smu_context *smu = &adev->smu;
1462 bool baco_feature_is_enabled = false;
1464 if (!smu->pm_enabled)
1468 baco_feature_is_enabled = smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT);
1470 ret = smu_system_features_control(smu, false);
1474 if (baco_feature_is_enabled) {
1475 ret = smu_feature_set_enabled(smu, SMU_FEATURE_BACO_BIT, true);
1477 pr_warn("set BACO feature enabled failed, return %d\n", ret);
1482 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1484 if (adev->asic_type >= CHIP_NAVI10 &&
1485 adev->gfx.rlc.funcs->stop)
1486 adev->gfx.rlc.funcs->stop(adev);
1488 smu_set_gfx_cgpg(&adev->smu, false);
1493 static int smu_resume(void *handle)
1496 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1497 struct smu_context *smu = &adev->smu;
1499 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1502 if (!smu->pm_enabled)
1505 pr_info("SMU is resuming...\n");
1507 ret = smu_start_smc_engine(smu);
1509 pr_err("SMU is not ready yet!\n");
1513 ret = smu_smc_table_hw_init(smu, false);
1517 ret = smu_start_thermal_control(smu);
1522 smu_set_gfx_cgpg(&adev->smu, true);
1524 smu->disable_uclk_switch = 0;
1526 pr_info("SMU is resumed successfully!\n");
1534 int smu_display_configuration_change(struct smu_context *smu,
1535 const struct amd_pp_display_configuration *display_config)
1538 int num_of_active_display = 0;
1540 if (!smu->pm_enabled || !is_support_sw_smu(smu->adev))
1543 if (!display_config)
1546 mutex_lock(&smu->mutex);
1548 if (smu->ppt_funcs->set_deep_sleep_dcefclk)
1549 smu->ppt_funcs->set_deep_sleep_dcefclk(smu,
1550 display_config->min_dcef_deep_sleep_set_clk / 100);
1552 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1553 if (display_config->displays[index].controller_id != 0)
1554 num_of_active_display++;
1557 smu_set_active_display_count(smu, num_of_active_display);
1559 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1560 display_config->cpu_cc6_disable,
1561 display_config->cpu_pstate_disable,
1562 display_config->nb_pstate_switch_disable);
1564 mutex_unlock(&smu->mutex);
1569 static int smu_get_clock_info(struct smu_context *smu,
1570 struct smu_clock_info *clk_info,
1571 enum smu_perf_level_designation designation)
1574 struct smu_performance_level level = {0};
1579 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1583 clk_info->min_mem_clk = level.memory_clock;
1584 clk_info->min_eng_clk = level.core_clock;
1585 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1587 ret = smu_get_perf_level(smu, designation, &level);
1591 clk_info->min_mem_clk = level.memory_clock;
1592 clk_info->min_eng_clk = level.core_clock;
1593 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1598 int smu_get_current_clocks(struct smu_context *smu,
1599 struct amd_pp_clock_info *clocks)
1601 struct amd_pp_simple_clock_info simple_clocks = {0};
1602 struct smu_clock_info hw_clocks;
1605 if (!is_support_sw_smu(smu->adev))
1608 mutex_lock(&smu->mutex);
1610 smu_get_dal_power_level(smu, &simple_clocks);
1612 if (smu->support_power_containment)
1613 ret = smu_get_clock_info(smu, &hw_clocks,
1614 PERF_LEVEL_POWER_CONTAINMENT);
1616 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1619 pr_err("Error in smu_get_clock_info\n");
1623 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1624 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1625 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1626 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1627 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1628 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1629 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1630 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1632 if (simple_clocks.level == 0)
1633 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1635 clocks->max_clocks_state = simple_clocks.level;
1637 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1638 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1639 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1643 mutex_unlock(&smu->mutex);
1647 static int smu_set_clockgating_state(void *handle,
1648 enum amd_clockgating_state state)
1653 static int smu_set_powergating_state(void *handle,
1654 enum amd_powergating_state state)
1659 static int smu_enable_umd_pstate(void *handle,
1660 enum amd_dpm_forced_level *level)
1662 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1663 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1664 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1665 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1667 struct smu_context *smu = (struct smu_context*)(handle);
1668 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1670 if (!smu->is_apu && (!smu->pm_enabled || !smu_dpm_ctx->dpm_context))
1673 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1674 /* enter umd pstate, save current level, disable gfx cg*/
1675 if (*level & profile_mode_mask) {
1676 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1677 smu_dpm_ctx->enable_umd_pstate = true;
1678 amdgpu_device_ip_set_clockgating_state(smu->adev,
1679 AMD_IP_BLOCK_TYPE_GFX,
1680 AMD_CG_STATE_UNGATE);
1681 amdgpu_device_ip_set_powergating_state(smu->adev,
1682 AMD_IP_BLOCK_TYPE_GFX,
1683 AMD_PG_STATE_UNGATE);
1686 /* exit umd pstate, restore level, enable gfx cg*/
1687 if (!(*level & profile_mode_mask)) {
1688 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1689 *level = smu_dpm_ctx->saved_dpm_level;
1690 smu_dpm_ctx->enable_umd_pstate = false;
1691 amdgpu_device_ip_set_clockgating_state(smu->adev,
1692 AMD_IP_BLOCK_TYPE_GFX,
1694 amdgpu_device_ip_set_powergating_state(smu->adev,
1695 AMD_IP_BLOCK_TYPE_GFX,
1703 int smu_adjust_power_state_dynamic(struct smu_context *smu,
1704 enum amd_dpm_forced_level level,
1705 bool skip_display_settings)
1710 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1712 if (!smu->pm_enabled)
1715 if (!skip_display_settings) {
1716 ret = smu_display_config_changed(smu);
1718 pr_err("Failed to change display config!");
1723 ret = smu_apply_clocks_adjust_rules(smu);
1725 pr_err("Failed to apply clocks adjust rules!");
1729 if (!skip_display_settings) {
1730 ret = smu_notify_smc_display_config(smu);
1732 pr_err("Failed to notify smc display config!");
1737 if (smu_dpm_ctx->dpm_level != level) {
1738 ret = smu_asic_set_performance_level(smu, level);
1740 pr_err("Failed to set performance level!");
1744 /* update the saved copy */
1745 smu_dpm_ctx->dpm_level = level;
1748 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1749 index = fls(smu->workload_mask);
1750 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1751 workload = smu->workload_setting[index];
1753 if (smu->power_profile_mode != workload)
1754 smu_set_power_profile_mode(smu, &workload, 0, false);
1760 int smu_handle_task(struct smu_context *smu,
1761 enum amd_dpm_forced_level level,
1762 enum amd_pp_task task_id,
1768 mutex_lock(&smu->mutex);
1771 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1772 ret = smu_pre_display_config_changed(smu);
1775 ret = smu_set_cpu_power_state(smu);
1778 ret = smu_adjust_power_state_dynamic(smu, level, false);
1780 case AMD_PP_TASK_COMPLETE_INIT:
1781 case AMD_PP_TASK_READJUST_POWER_STATE:
1782 ret = smu_adjust_power_state_dynamic(smu, level, true);
1790 mutex_unlock(&smu->mutex);
1795 int smu_switch_power_profile(struct smu_context *smu,
1796 enum PP_SMC_POWER_PROFILE type,
1799 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1803 if (!smu->pm_enabled)
1806 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1809 mutex_lock(&smu->mutex);
1812 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1813 index = fls(smu->workload_mask);
1814 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1815 workload = smu->workload_setting[index];
1817 smu->workload_mask |= (1 << smu->workload_prority[type]);
1818 index = fls(smu->workload_mask);
1819 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1820 workload = smu->workload_setting[index];
1823 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1824 smu_set_power_profile_mode(smu, &workload, 0, false);
1826 mutex_unlock(&smu->mutex);
1831 enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1833 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1834 enum amd_dpm_forced_level level;
1836 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1839 mutex_lock(&(smu->mutex));
1840 level = smu_dpm_ctx->dpm_level;
1841 mutex_unlock(&(smu->mutex));
1846 int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1848 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1851 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1854 mutex_lock(&smu->mutex);
1856 ret = smu_enable_umd_pstate(smu, &level);
1858 mutex_unlock(&smu->mutex);
1862 ret = smu_handle_task(smu, level,
1863 AMD_PP_TASK_READJUST_POWER_STATE,
1866 mutex_unlock(&smu->mutex);
1871 int smu_set_display_count(struct smu_context *smu, uint32_t count)
1875 mutex_lock(&smu->mutex);
1876 ret = smu_init_display_count(smu, count);
1877 mutex_unlock(&smu->mutex);
1882 int smu_force_clk_levels(struct smu_context *smu,
1883 enum smu_clk_type clk_type,
1887 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1890 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1891 pr_debug("force clock level is for dpm manual mode only.\n");
1896 mutex_lock(&smu->mutex);
1898 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels)
1899 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1902 mutex_unlock(&smu->mutex);
1907 int smu_set_mp1_state(struct smu_context *smu,
1908 enum pp_mp1_state mp1_state)
1914 * The SMC is not fully ready. That may be
1915 * expected as the IP may be masked.
1916 * So, just return without error.
1918 if (!smu->pm_enabled)
1921 mutex_lock(&smu->mutex);
1923 switch (mp1_state) {
1924 case PP_MP1_STATE_SHUTDOWN:
1925 msg = SMU_MSG_PrepareMp1ForShutdown;
1927 case PP_MP1_STATE_UNLOAD:
1928 msg = SMU_MSG_PrepareMp1ForUnload;
1930 case PP_MP1_STATE_RESET:
1931 msg = SMU_MSG_PrepareMp1ForReset;
1933 case PP_MP1_STATE_NONE:
1935 mutex_unlock(&smu->mutex);
1939 /* some asics may not support those messages */
1940 if (smu_msg_get_index(smu, msg) < 0) {
1941 mutex_unlock(&smu->mutex);
1945 ret = smu_send_smc_msg(smu, msg);
1947 pr_err("[PrepareMp1] Failed!\n");
1949 mutex_unlock(&smu->mutex);
1954 int smu_set_df_cstate(struct smu_context *smu,
1955 enum pp_df_cstate state)
1960 * The SMC is not fully ready. That may be
1961 * expected as the IP may be masked.
1962 * So, just return without error.
1964 if (!smu->pm_enabled)
1967 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
1970 mutex_lock(&smu->mutex);
1972 ret = smu->ppt_funcs->set_df_cstate(smu, state);
1974 pr_err("[SetDfCstate] failed!\n");
1976 mutex_unlock(&smu->mutex);
1981 int smu_write_watermarks_table(struct smu_context *smu)
1983 void *watermarks_table = smu->smu_table.watermarks_table;
1985 if (!watermarks_table)
1988 return smu_update_table(smu,
1989 SMU_TABLE_WATERMARKS,
1995 int smu_set_watermarks_for_clock_ranges(struct smu_context *smu,
1996 struct dm_pp_wm_sets_with_clock_ranges_soc15 *clock_ranges)
1998 void *table = smu->smu_table.watermarks_table;
2003 mutex_lock(&smu->mutex);
2005 if (!smu->disable_watermark &&
2006 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_DCEFCLK_BIT) &&
2007 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT)) {
2008 smu_set_watermarks_table(smu, table, clock_ranges);
2010 if (!(smu->watermarks_bitmap & WATERMARKS_EXIST)) {
2011 smu->watermarks_bitmap |= WATERMARKS_EXIST;
2012 smu->watermarks_bitmap &= ~WATERMARKS_LOADED;
2016 mutex_unlock(&smu->mutex);
2021 const struct amd_ip_funcs smu_ip_funcs = {
2023 .early_init = smu_early_init,
2024 .late_init = smu_late_init,
2025 .sw_init = smu_sw_init,
2026 .sw_fini = smu_sw_fini,
2027 .hw_init = smu_hw_init,
2028 .hw_fini = smu_hw_fini,
2029 .suspend = smu_suspend,
2030 .resume = smu_resume,
2032 .check_soft_reset = NULL,
2033 .wait_for_idle = NULL,
2035 .set_clockgating_state = smu_set_clockgating_state,
2036 .set_powergating_state = smu_set_powergating_state,
2037 .enable_umd_pstate = smu_enable_umd_pstate,
2040 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
2042 .type = AMD_IP_BLOCK_TYPE_SMC,
2046 .funcs = &smu_ip_funcs,
2049 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
2051 .type = AMD_IP_BLOCK_TYPE_SMC,
2055 .funcs = &smu_ip_funcs,
2058 int smu_load_microcode(struct smu_context *smu)
2062 mutex_lock(&smu->mutex);
2064 if (smu->ppt_funcs->load_microcode)
2065 ret = smu->ppt_funcs->load_microcode(smu);
2067 mutex_unlock(&smu->mutex);
2072 int smu_check_fw_status(struct smu_context *smu)
2076 mutex_lock(&smu->mutex);
2078 if (smu->ppt_funcs->check_fw_status)
2079 ret = smu->ppt_funcs->check_fw_status(smu);
2081 mutex_unlock(&smu->mutex);
2086 int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2090 mutex_lock(&smu->mutex);
2092 if (smu->ppt_funcs->set_gfx_cgpg)
2093 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2095 mutex_unlock(&smu->mutex);
2100 int smu_set_fan_speed_rpm(struct smu_context *smu, uint32_t speed)
2104 mutex_lock(&smu->mutex);
2106 if (smu->ppt_funcs->set_fan_speed_rpm)
2107 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2109 mutex_unlock(&smu->mutex);
2114 int smu_get_power_limit(struct smu_context *smu,
2122 mutex_lock(&smu->mutex);
2124 if (smu->ppt_funcs->get_power_limit)
2125 ret = smu->ppt_funcs->get_power_limit(smu, limit, def);
2128 mutex_unlock(&smu->mutex);
2133 int smu_set_power_limit(struct smu_context *smu, uint32_t limit)
2137 mutex_lock(&smu->mutex);
2139 if (smu->ppt_funcs->set_power_limit)
2140 ret = smu->ppt_funcs->set_power_limit(smu, limit);
2142 mutex_unlock(&smu->mutex);
2147 int smu_print_clk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2151 mutex_lock(&smu->mutex);
2153 if (smu->ppt_funcs->print_clk_levels)
2154 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2156 mutex_unlock(&smu->mutex);
2161 int smu_get_od_percentage(struct smu_context *smu, enum smu_clk_type type)
2165 mutex_lock(&smu->mutex);
2167 if (smu->ppt_funcs->get_od_percentage)
2168 ret = smu->ppt_funcs->get_od_percentage(smu, type);
2170 mutex_unlock(&smu->mutex);
2175 int smu_set_od_percentage(struct smu_context *smu, enum smu_clk_type type, uint32_t value)
2179 mutex_lock(&smu->mutex);
2181 if (smu->ppt_funcs->set_od_percentage)
2182 ret = smu->ppt_funcs->set_od_percentage(smu, type, value);
2184 mutex_unlock(&smu->mutex);
2189 int smu_od_edit_dpm_table(struct smu_context *smu,
2190 enum PP_OD_DPM_TABLE_COMMAND type,
2191 long *input, uint32_t size)
2195 mutex_lock(&smu->mutex);
2197 if (smu->ppt_funcs->od_edit_dpm_table)
2198 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2200 mutex_unlock(&smu->mutex);
2205 int smu_read_sensor(struct smu_context *smu,
2206 enum amd_pp_sensors sensor,
2207 void *data, uint32_t *size)
2211 mutex_lock(&smu->mutex);
2213 if (smu->ppt_funcs->read_sensor)
2214 ret = smu->ppt_funcs->read_sensor(smu, sensor, data, size);
2216 mutex_unlock(&smu->mutex);
2221 int smu_get_power_profile_mode(struct smu_context *smu, char *buf)
2225 mutex_lock(&smu->mutex);
2227 if (smu->ppt_funcs->get_power_profile_mode)
2228 ret = smu->ppt_funcs->get_power_profile_mode(smu, buf);
2230 mutex_unlock(&smu->mutex);
2235 int smu_set_power_profile_mode(struct smu_context *smu,
2237 uint32_t param_size,
2243 mutex_lock(&smu->mutex);
2245 if (smu->ppt_funcs->set_power_profile_mode)
2246 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
2249 mutex_unlock(&smu->mutex);
2255 int smu_get_fan_control_mode(struct smu_context *smu)
2259 mutex_lock(&smu->mutex);
2261 if (smu->ppt_funcs->get_fan_control_mode)
2262 ret = smu->ppt_funcs->get_fan_control_mode(smu);
2264 mutex_unlock(&smu->mutex);
2269 int smu_set_fan_control_mode(struct smu_context *smu, int value)
2273 mutex_lock(&smu->mutex);
2275 if (smu->ppt_funcs->set_fan_control_mode)
2276 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2278 mutex_unlock(&smu->mutex);
2283 int smu_get_fan_speed_percent(struct smu_context *smu, uint32_t *speed)
2287 mutex_lock(&smu->mutex);
2289 if (smu->ppt_funcs->get_fan_speed_percent)
2290 ret = smu->ppt_funcs->get_fan_speed_percent(smu, speed);
2292 mutex_unlock(&smu->mutex);
2297 int smu_set_fan_speed_percent(struct smu_context *smu, uint32_t speed)
2301 mutex_lock(&smu->mutex);
2303 if (smu->ppt_funcs->set_fan_speed_percent)
2304 ret = smu->ppt_funcs->set_fan_speed_percent(smu, speed);
2306 mutex_unlock(&smu->mutex);
2311 int smu_get_fan_speed_rpm(struct smu_context *smu, uint32_t *speed)
2315 mutex_lock(&smu->mutex);
2317 if (smu->ppt_funcs->get_fan_speed_rpm)
2318 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2320 mutex_unlock(&smu->mutex);
2325 int smu_set_deep_sleep_dcefclk(struct smu_context *smu, int clk)
2329 mutex_lock(&smu->mutex);
2331 if (smu->ppt_funcs->set_deep_sleep_dcefclk)
2332 ret = smu->ppt_funcs->set_deep_sleep_dcefclk(smu, clk);
2334 mutex_unlock(&smu->mutex);
2339 int smu_set_active_display_count(struct smu_context *smu, uint32_t count)
2343 if (smu->ppt_funcs->set_active_display_count)
2344 ret = smu->ppt_funcs->set_active_display_count(smu, count);
2349 int smu_get_clock_by_type(struct smu_context *smu,
2350 enum amd_pp_clock_type type,
2351 struct amd_pp_clocks *clocks)
2355 mutex_lock(&smu->mutex);
2357 if (smu->ppt_funcs->get_clock_by_type)
2358 ret = smu->ppt_funcs->get_clock_by_type(smu, type, clocks);
2360 mutex_unlock(&smu->mutex);
2365 int smu_get_max_high_clocks(struct smu_context *smu,
2366 struct amd_pp_simple_clock_info *clocks)
2370 mutex_lock(&smu->mutex);
2372 if (smu->ppt_funcs->get_max_high_clocks)
2373 ret = smu->ppt_funcs->get_max_high_clocks(smu, clocks);
2375 mutex_unlock(&smu->mutex);
2380 int smu_get_clock_by_type_with_latency(struct smu_context *smu,
2381 enum smu_clk_type clk_type,
2382 struct pp_clock_levels_with_latency *clocks)
2386 mutex_lock(&smu->mutex);
2388 if (smu->ppt_funcs->get_clock_by_type_with_latency)
2389 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2391 mutex_unlock(&smu->mutex);
2396 int smu_get_clock_by_type_with_voltage(struct smu_context *smu,
2397 enum amd_pp_clock_type type,
2398 struct pp_clock_levels_with_voltage *clocks)
2402 mutex_lock(&smu->mutex);
2404 if (smu->ppt_funcs->get_clock_by_type_with_voltage)
2405 ret = smu->ppt_funcs->get_clock_by_type_with_voltage(smu, type, clocks);
2407 mutex_unlock(&smu->mutex);
2413 int smu_display_clock_voltage_request(struct smu_context *smu,
2414 struct pp_display_clock_request *clock_req)
2418 mutex_lock(&smu->mutex);
2420 if (smu->ppt_funcs->display_clock_voltage_request)
2421 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2423 mutex_unlock(&smu->mutex);
2429 int smu_display_disable_memory_clock_switch(struct smu_context *smu, bool disable_memory_clock_switch)
2433 mutex_lock(&smu->mutex);
2435 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2436 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2438 mutex_unlock(&smu->mutex);
2443 int smu_notify_smu_enable_pwe(struct smu_context *smu)
2447 mutex_lock(&smu->mutex);
2449 if (smu->ppt_funcs->notify_smu_enable_pwe)
2450 ret = smu->ppt_funcs->notify_smu_enable_pwe(smu);
2452 mutex_unlock(&smu->mutex);
2457 int smu_set_xgmi_pstate(struct smu_context *smu,
2462 mutex_lock(&smu->mutex);
2464 if (smu->ppt_funcs->set_xgmi_pstate)
2465 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2467 mutex_unlock(&smu->mutex);
2472 int smu_set_azalia_d3_pme(struct smu_context *smu)
2476 mutex_lock(&smu->mutex);
2478 if (smu->ppt_funcs->set_azalia_d3_pme)
2479 ret = smu->ppt_funcs->set_azalia_d3_pme(smu);
2481 mutex_unlock(&smu->mutex);
2486 bool smu_baco_is_support(struct smu_context *smu)
2490 mutex_lock(&smu->mutex);
2492 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2493 ret = smu->ppt_funcs->baco_is_support(smu);
2495 mutex_unlock(&smu->mutex);
2500 int smu_baco_get_state(struct smu_context *smu, enum smu_baco_state *state)
2502 if (smu->ppt_funcs->baco_get_state)
2505 mutex_lock(&smu->mutex);
2506 *state = smu->ppt_funcs->baco_get_state(smu);
2507 mutex_unlock(&smu->mutex);
2512 int smu_baco_enter(struct smu_context *smu)
2516 mutex_lock(&smu->mutex);
2518 if (smu->ppt_funcs->baco_enter)
2519 ret = smu->ppt_funcs->baco_enter(smu);
2521 mutex_unlock(&smu->mutex);
2526 int smu_baco_exit(struct smu_context *smu)
2530 mutex_lock(&smu->mutex);
2532 if (smu->ppt_funcs->baco_exit)
2533 ret = smu->ppt_funcs->baco_exit(smu);
2535 mutex_unlock(&smu->mutex);
2540 int smu_mode2_reset(struct smu_context *smu)
2544 mutex_lock(&smu->mutex);
2546 if (smu->ppt_funcs->mode2_reset)
2547 ret = smu->ppt_funcs->mode2_reset(smu);
2549 mutex_unlock(&smu->mutex);
2554 int smu_get_max_sustainable_clocks_by_dc(struct smu_context *smu,
2555 struct pp_smu_nv_clock_table *max_clocks)
2559 mutex_lock(&smu->mutex);
2561 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2562 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2564 mutex_unlock(&smu->mutex);
2569 int smu_get_uclk_dpm_states(struct smu_context *smu,
2570 unsigned int *clock_values_in_khz,
2571 unsigned int *num_states)
2575 mutex_lock(&smu->mutex);
2577 if (smu->ppt_funcs->get_uclk_dpm_states)
2578 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2580 mutex_unlock(&smu->mutex);
2585 enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
2587 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2589 mutex_lock(&smu->mutex);
2591 if (smu->ppt_funcs->get_current_power_state)
2592 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2594 mutex_unlock(&smu->mutex);
2599 int smu_get_dpm_clock_table(struct smu_context *smu,
2600 struct dpm_clocks *clock_table)
2604 mutex_lock(&smu->mutex);
2606 if (smu->ppt_funcs->get_dpm_clock_table)
2607 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2609 mutex_unlock(&smu->mutex);
2614 uint32_t smu_get_pptable_power_limit(struct smu_context *smu)
2618 if (smu->ppt_funcs->get_pptable_power_limit)
2619 ret = smu->ppt_funcs->get_pptable_power_limit(smu);
2624 int smu_send_smc_msg(struct smu_context *smu,
2625 enum smu_message_type msg)
2629 ret = smu_send_smc_msg_with_param(smu, msg, 0);