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>
27 #include "amdgpu_smu.h"
28 #include "smu_internal.h"
29 #include "smu_v11_0.h"
30 #include "smu_v12_0.h"
32 #include "arcturus_ppt.h"
33 #include "navi10_ppt.h"
34 #include "sienna_cichlid_ppt.h"
35 #include "renoir_ppt.h"
37 #undef __SMU_DUMMY_MAP
38 #define __SMU_DUMMY_MAP(type) #type
39 static const char* __smu_message_names[] = {
43 const char *smu_get_message_name(struct smu_context *smu, enum smu_message_type type)
45 if (type < 0 || type >= SMU_MSG_MAX_COUNT)
46 return "unknown smu message";
47 return __smu_message_names[type];
50 #undef __SMU_DUMMY_MAP
51 #define __SMU_DUMMY_MAP(fea) #fea
52 static const char* __smu_feature_names[] = {
56 const char *smu_get_feature_name(struct smu_context *smu, enum smu_feature_mask feature)
58 if (feature < 0 || feature >= SMU_FEATURE_COUNT)
59 return "unknown smu feature";
60 return __smu_feature_names[feature];
63 size_t smu_sys_get_pp_feature_mask(struct smu_context *smu, char *buf)
67 uint32_t feature_mask[2] = { 0 };
68 int32_t feature_index = 0;
70 uint32_t sort_feature[SMU_FEATURE_COUNT];
71 uint64_t hw_feature_count = 0;
73 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
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;
116 ret = smu_send_smc_msg_with_param(smu,
117 SMU_MSG_EnableSmuFeaturesLow,
118 lower_32_bits(feature_mask),
122 ret = smu_send_smc_msg_with_param(smu,
123 SMU_MSG_EnableSmuFeaturesHigh,
124 upper_32_bits(feature_mask),
129 ret = smu_send_smc_msg_with_param(smu,
130 SMU_MSG_DisableSmuFeaturesLow,
131 lower_32_bits(feature_mask),
135 ret = smu_send_smc_msg_with_param(smu,
136 SMU_MSG_DisableSmuFeaturesHigh,
137 upper_32_bits(feature_mask),
143 mutex_lock(&feature->mutex);
145 bitmap_or(feature->enabled, feature->enabled,
146 (unsigned long *)(&feature_mask), SMU_FEATURE_MAX);
148 bitmap_andnot(feature->enabled, feature->enabled,
149 (unsigned long *)(&feature_mask), SMU_FEATURE_MAX);
150 mutex_unlock(&feature->mutex);
155 int smu_sys_set_pp_feature_mask(struct smu_context *smu, uint64_t new_mask)
158 uint32_t feature_mask[2] = { 0 };
159 uint64_t feature_2_enabled = 0;
160 uint64_t feature_2_disabled = 0;
161 uint64_t feature_enables = 0;
163 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
166 mutex_lock(&smu->mutex);
168 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
172 feature_enables = ((uint64_t)feature_mask[1] << 32 | (uint64_t)feature_mask[0]);
174 feature_2_enabled = ~feature_enables & new_mask;
175 feature_2_disabled = feature_enables & ~new_mask;
177 if (feature_2_enabled) {
178 ret = smu_feature_update_enable_state(smu, feature_2_enabled, true);
182 if (feature_2_disabled) {
183 ret = smu_feature_update_enable_state(smu, feature_2_disabled, false);
189 mutex_unlock(&smu->mutex);
194 int smu_get_smc_version(struct smu_context *smu, uint32_t *if_version, uint32_t *smu_version)
198 if (!if_version && !smu_version)
201 if (smu->smc_fw_if_version && smu->smc_fw_version)
204 *if_version = smu->smc_fw_if_version;
207 *smu_version = smu->smc_fw_version;
213 ret = smu_send_smc_msg(smu, SMU_MSG_GetDriverIfVersion, if_version);
217 smu->smc_fw_if_version = *if_version;
221 ret = smu_send_smc_msg(smu, SMU_MSG_GetSmuVersion, smu_version);
225 smu->smc_fw_version = *smu_version;
231 int smu_set_soft_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
232 uint32_t min, uint32_t max, bool lock_needed)
236 if (!smu_clk_dpm_is_enabled(smu, clk_type))
240 mutex_lock(&smu->mutex);
241 ret = smu_set_soft_freq_limited_range(smu, clk_type, min, max);
243 mutex_unlock(&smu->mutex);
248 int smu_set_hard_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
249 uint32_t min, uint32_t max)
251 int ret = 0, clk_id = 0;
254 if (min <= 0 && max <= 0)
257 if (!smu_clk_dpm_is_enabled(smu, clk_type))
260 clk_id = smu_clk_get_index(smu, clk_type);
265 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
266 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMaxByFreq,
273 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
274 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
284 int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
285 uint32_t *min, uint32_t *max, bool lock_needed)
287 uint32_t clock_limit;
294 mutex_lock(&smu->mutex);
296 if (!smu_clk_dpm_is_enabled(smu, clk_type)) {
300 clock_limit = smu->smu_table.boot_values.uclk;
304 clock_limit = smu->smu_table.boot_values.gfxclk;
307 clock_limit = smu->smu_table.boot_values.socclk;
314 /* clock in Mhz unit */
316 *min = clock_limit / 100;
318 *max = clock_limit / 100;
321 * Todo: Use each asic(ASIC_ppt funcs) control the callbacks exposed to the
322 * core driver and then have helpers for stuff that is common(SMU_v11_x | SMU_v12_x funcs).
324 ret = smu_get_dpm_ultimate_freq(smu, clk_type, min, max);
328 mutex_unlock(&smu->mutex);
333 int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
334 uint16_t level, uint32_t *value)
336 int ret = 0, clk_id = 0;
342 if (!smu_clk_dpm_is_enabled(smu, clk_type))
345 clk_id = smu_clk_get_index(smu, clk_type);
349 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
351 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetDpmFreqByIndex,
356 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
357 * now, we un-support it */
358 *value = *value & 0x7fffffff;
363 int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
366 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
369 int smu_get_dpm_level_range(struct smu_context *smu, enum smu_clk_type clk_type,
370 uint32_t *min_value, uint32_t *max_value)
373 uint32_t level_count = 0;
375 if (!min_value && !max_value)
379 /* by default, level 0 clock value as min value */
380 ret = smu_get_dpm_freq_by_index(smu, clk_type, 0, min_value);
386 ret = smu_get_dpm_level_count(smu, clk_type, &level_count);
390 ret = smu_get_dpm_freq_by_index(smu, clk_type, level_count - 1, max_value);
398 bool smu_clk_dpm_is_enabled(struct smu_context *smu, enum smu_clk_type clk_type)
400 enum smu_feature_mask feature_id = 0;
405 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
409 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
412 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
418 if(!smu_feature_is_enabled(smu, feature_id)) {
426 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
428 * @smu: smu_context pointer
429 * @block_type: the IP block to power gate/ungate
430 * @gate: to power gate if true, ungate otherwise
432 * This API uses no smu->mutex lock protection due to:
433 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
434 * This is guarded to be race condition free by the caller.
435 * 2. Or get called on user setting request of power_dpm_force_performance_level.
436 * Under this case, the smu->mutex lock protection is already enforced on
437 * the parent API smu_force_performance_level of the call path.
439 int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
444 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
447 switch (block_type) {
448 case AMD_IP_BLOCK_TYPE_UVD:
449 ret = smu_dpm_set_uvd_enable(smu, !gate);
451 case AMD_IP_BLOCK_TYPE_VCE:
452 ret = smu_dpm_set_vce_enable(smu, !gate);
454 case AMD_IP_BLOCK_TYPE_GFX:
455 ret = smu_gfx_off_control(smu, gate);
457 case AMD_IP_BLOCK_TYPE_SDMA:
458 ret = smu_powergate_sdma(smu, gate);
460 case AMD_IP_BLOCK_TYPE_JPEG:
461 ret = smu_dpm_set_jpeg_enable(smu, !gate);
470 int smu_get_power_num_states(struct smu_context *smu,
471 struct pp_states_info *state_info)
476 /* not support power state */
477 memset(state_info, 0, sizeof(struct pp_states_info));
478 state_info->nums = 1;
479 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
484 int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
485 void *data, uint32_t *size)
487 struct smu_power_context *smu_power = &smu->smu_power;
488 struct smu_power_gate *power_gate = &smu_power->power_gate;
495 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
496 *((uint32_t *)data) = smu->pstate_sclk;
499 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
500 *((uint32_t *)data) = smu->pstate_mclk;
503 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
504 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
507 case AMDGPU_PP_SENSOR_UVD_POWER:
508 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
511 case AMDGPU_PP_SENSOR_VCE_POWER:
512 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
515 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
516 *(uint32_t *)data = power_gate->vcn_gated ? 0 : 1;
530 int smu_update_table(struct smu_context *smu, enum smu_table_id table_index, int argument,
531 void *table_data, bool drv2smu)
533 struct smu_table_context *smu_table = &smu->smu_table;
534 struct amdgpu_device *adev = smu->adev;
535 struct smu_table *table = &smu_table->driver_table;
536 int table_id = smu_table_get_index(smu, table_index);
539 if (!table_data || table_id >= SMU_TABLE_COUNT || table_id < 0)
542 table_size = smu_table->tables[table_index].size;
545 memcpy(table->cpu_addr, table_data, table_size);
547 * Flush hdp cache: to guard the content seen by
548 * GPU is consitent with CPU.
550 amdgpu_asic_flush_hdp(adev, NULL);
553 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
554 SMU_MSG_TransferTableDram2Smu :
555 SMU_MSG_TransferTableSmu2Dram,
556 table_id | ((argument & 0xFFFF) << 16),
562 amdgpu_asic_flush_hdp(adev, NULL);
563 memcpy(table_data, table->cpu_addr, table_size);
569 bool is_support_sw_smu(struct amdgpu_device *adev)
571 if (adev->asic_type >= CHIP_ARCTURUS)
577 int smu_sys_get_pp_table(struct smu_context *smu, void **table)
579 struct smu_table_context *smu_table = &smu->smu_table;
580 uint32_t powerplay_table_size;
582 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
585 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
588 mutex_lock(&smu->mutex);
590 if (smu_table->hardcode_pptable)
591 *table = smu_table->hardcode_pptable;
593 *table = smu_table->power_play_table;
595 powerplay_table_size = smu_table->power_play_table_size;
597 mutex_unlock(&smu->mutex);
599 return powerplay_table_size;
602 int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
604 struct smu_table_context *smu_table = &smu->smu_table;
605 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
608 if (!smu->pm_enabled || !smu->adev->pm.dpm_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 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
647 struct smu_feature *feature = &smu->smu_feature;
649 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
651 mutex_lock(&feature->mutex);
652 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
653 mutex_unlock(&feature->mutex);
655 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
660 mutex_lock(&feature->mutex);
661 bitmap_or(feature->allowed, feature->allowed,
662 (unsigned long *)allowed_feature_mask,
663 feature->feature_num);
664 mutex_unlock(&feature->mutex);
669 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
671 struct smu_feature *feature = &smu->smu_feature;
677 feature_id = smu_feature_get_index(smu, mask);
681 WARN_ON(feature_id > feature->feature_num);
683 mutex_lock(&feature->mutex);
684 ret = test_bit(feature_id, feature->enabled);
685 mutex_unlock(&feature->mutex);
690 int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
693 struct smu_feature *feature = &smu->smu_feature;
696 feature_id = smu_feature_get_index(smu, mask);
700 WARN_ON(feature_id > feature->feature_num);
702 return smu_feature_update_enable_state(smu,
707 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
709 struct smu_feature *feature = &smu->smu_feature;
713 feature_id = smu_feature_get_index(smu, mask);
717 WARN_ON(feature_id > feature->feature_num);
719 mutex_lock(&feature->mutex);
720 ret = test_bit(feature_id, feature->supported);
721 mutex_unlock(&feature->mutex);
726 static int smu_set_funcs(struct amdgpu_device *adev)
728 struct smu_context *smu = &adev->smu;
730 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
731 smu->od_enabled = true;
733 switch (adev->asic_type) {
737 navi10_set_ppt_funcs(smu);
740 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
741 arcturus_set_ppt_funcs(smu);
742 /* OD is not supported on Arcturus */
743 smu->od_enabled =false;
745 case CHIP_SIENNA_CICHLID:
746 sienna_cichlid_set_ppt_funcs(smu);
749 renoir_set_ppt_funcs(smu);
758 static int smu_early_init(void *handle)
760 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
761 struct smu_context *smu = &adev->smu;
764 smu->pm_enabled = !!amdgpu_dpm;
766 mutex_init(&smu->mutex);
768 return smu_set_funcs(adev);
771 static int smu_late_init(void *handle)
773 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
774 struct smu_context *smu = &adev->smu;
777 if (!smu->pm_enabled)
780 ret = smu_set_default_od_settings(smu);
785 * Set initialized values (get from vbios) to dpm tables context such as
786 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
789 ret = smu_populate_smc_tables(smu);
793 ret = smu_init_max_sustainable_clocks(smu);
797 ret = smu_populate_umd_state_clk(smu);
801 ret = smu_get_power_limit(smu, &smu->default_power_limit, false, false);
805 smu_get_unique_id(smu);
807 smu_handle_task(&adev->smu,
808 smu->smu_dpm.dpm_level,
809 AMD_PP_TASK_COMPLETE_INIT,
815 int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
816 uint16_t *size, uint8_t *frev, uint8_t *crev,
819 struct amdgpu_device *adev = smu->adev;
822 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
823 size, frev, crev, &data_start))
826 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
831 static int smu_init_fb_allocations(struct smu_context *smu)
833 struct amdgpu_device *adev = smu->adev;
834 struct smu_table_context *smu_table = &smu->smu_table;
835 struct smu_table *tables = smu_table->tables;
836 struct smu_table *driver_table = &(smu_table->driver_table);
837 uint32_t max_table_size = 0;
840 /* VRAM allocation for tool table */
841 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
842 ret = amdgpu_bo_create_kernel(adev,
843 tables[SMU_TABLE_PMSTATUSLOG].size,
844 tables[SMU_TABLE_PMSTATUSLOG].align,
845 tables[SMU_TABLE_PMSTATUSLOG].domain,
846 &tables[SMU_TABLE_PMSTATUSLOG].bo,
847 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
848 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
850 pr_err("VRAM allocation for tool table failed!\n");
855 /* VRAM allocation for driver table */
856 for (i = 0; i < SMU_TABLE_COUNT; i++) {
857 if (tables[i].size == 0)
860 if (i == SMU_TABLE_PMSTATUSLOG)
863 if (max_table_size < tables[i].size)
864 max_table_size = tables[i].size;
867 driver_table->size = max_table_size;
868 driver_table->align = PAGE_SIZE;
869 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
871 ret = amdgpu_bo_create_kernel(adev,
874 driver_table->domain,
876 &driver_table->mc_address,
877 &driver_table->cpu_addr);
879 pr_err("VRAM allocation for driver table failed!\n");
880 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
881 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
882 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
883 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
889 static int smu_fini_fb_allocations(struct smu_context *smu)
891 struct smu_table_context *smu_table = &smu->smu_table;
892 struct smu_table *tables = smu_table->tables;
893 struct smu_table *driver_table = &(smu_table->driver_table);
898 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
899 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
900 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
901 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
903 amdgpu_bo_free_kernel(&driver_table->bo,
904 &driver_table->mc_address,
905 &driver_table->cpu_addr);
911 * smu_alloc_memory_pool - allocate memory pool in the system memory
913 * @smu: amdgpu_device pointer
915 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
916 * and DramLogSetDramAddr can notify it changed.
918 * Returns 0 on success, error on failure.
920 static int smu_alloc_memory_pool(struct smu_context *smu)
922 struct amdgpu_device *adev = smu->adev;
923 struct smu_table_context *smu_table = &smu->smu_table;
924 struct smu_table *memory_pool = &smu_table->memory_pool;
925 uint64_t pool_size = smu->pool_size;
928 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
931 memory_pool->size = pool_size;
932 memory_pool->align = PAGE_SIZE;
933 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
936 case SMU_MEMORY_POOL_SIZE_256_MB:
937 case SMU_MEMORY_POOL_SIZE_512_MB:
938 case SMU_MEMORY_POOL_SIZE_1_GB:
939 case SMU_MEMORY_POOL_SIZE_2_GB:
940 ret = amdgpu_bo_create_kernel(adev,
945 &memory_pool->mc_address,
946 &memory_pool->cpu_addr);
955 static int smu_free_memory_pool(struct smu_context *smu)
957 struct smu_table_context *smu_table = &smu->smu_table;
958 struct smu_table *memory_pool = &smu_table->memory_pool;
960 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
963 amdgpu_bo_free_kernel(&memory_pool->bo,
964 &memory_pool->mc_address,
965 &memory_pool->cpu_addr);
967 memset(memory_pool, 0, sizeof(struct smu_table));
972 static int smu_smc_table_sw_init(struct smu_context *smu)
977 * Create smu_table structure, and init smc tables such as
978 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
980 ret = smu_init_smc_tables(smu);
982 pr_err("Failed to init smc tables!\n");
987 * Create smu_power_context structure, and allocate smu_dpm_context and
988 * context size to fill the smu_power_context data.
990 ret = smu_init_power(smu);
992 pr_err("Failed to init smu_init_power!\n");
997 * allocate vram bos to store smc table contents.
999 ret = smu_init_fb_allocations(smu);
1003 ret = smu_alloc_memory_pool(smu);
1010 static int smu_smc_table_sw_fini(struct smu_context *smu)
1014 ret = smu_free_memory_pool(smu);
1018 ret = smu_fini_fb_allocations(smu);
1022 ret = smu_fini_power(smu);
1024 pr_err("Failed to init smu_fini_power!\n");
1028 ret = smu_fini_smc_tables(smu);
1030 pr_err("Failed to smu_fini_smc_tables!\n");
1037 static int smu_sw_init(void *handle)
1039 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1040 struct smu_context *smu = &adev->smu;
1043 smu->pool_size = adev->pm.smu_prv_buffer_size;
1044 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
1045 mutex_init(&smu->smu_feature.mutex);
1046 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
1047 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
1048 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1050 mutex_init(&smu->smu_baco.mutex);
1051 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
1052 smu->smu_baco.platform_support = false;
1054 mutex_init(&smu->sensor_lock);
1055 mutex_init(&smu->metrics_lock);
1056 mutex_init(&smu->message_lock);
1058 smu->watermarks_bitmap = 0;
1059 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1060 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1062 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
1063 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
1064 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
1065 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
1066 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
1067 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
1068 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
1069 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
1071 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1072 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1073 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
1074 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
1075 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
1076 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
1077 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
1078 smu->display_config = &adev->pm.pm_display_cfg;
1080 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1081 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1082 ret = smu_init_microcode(smu);
1084 pr_err("Failed to load smu firmware!\n");
1088 ret = smu_smc_table_sw_init(smu);
1090 pr_err("Failed to sw init smc table!\n");
1094 ret = smu_register_irq_handler(smu);
1096 pr_err("Failed to register smc irq handler!\n");
1103 static int smu_sw_fini(void *handle)
1105 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1106 struct smu_context *smu = &adev->smu;
1109 ret = smu_smc_table_sw_fini(smu);
1111 pr_err("Failed to sw fini smc table!\n");
1115 smu_fini_microcode(smu);
1120 static int smu_smc_hw_setup(struct smu_context *smu)
1122 struct amdgpu_device *adev = smu->adev;
1125 if (smu_is_dpm_running(smu) && adev->in_suspend) {
1126 pr_info("dpm has been enabled\n");
1130 ret = smu_init_display_count(smu, 0);
1134 ret = smu_set_driver_table_location(smu);
1139 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1141 ret = smu_set_tool_table_location(smu);
1146 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1149 ret = smu_notify_memory_pool_location(smu);
1153 /* smu_dump_pptable(smu); */
1155 * Copy pptable bo in the vram to smc with SMU MSGs such as
1156 * SetDriverDramAddr and TransferTableDram2Smu.
1158 ret = smu_write_pptable(smu);
1162 /* issue Run*Btc msg */
1163 ret = smu_run_btc(smu);
1167 ret = smu_feature_set_allowed_mask(smu);
1171 ret = smu_system_features_control(smu, true);
1175 if (!smu_is_dpm_running(smu))
1176 pr_info("dpm has been disabled\n");
1178 ret = smu_override_pcie_parameters(smu);
1182 ret = smu_enable_thermal_alert(smu);
1186 ret = smu_i2c_eeprom_init(smu, &adev->pm.smu_i2c);
1190 ret = smu_disable_umc_cdr_12gbps_workaround(smu);
1192 pr_err("Workaround failed to disable UMC CDR feature on 12Gbps SKU!\n");
1197 * For Navi1X, manually switch it to AC mode as PMFW
1198 * may boot it with DC mode.
1200 ret = smu_set_power_source(smu,
1201 adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
1202 SMU_POWER_SOURCE_DC);
1204 pr_err("Failed to switch to %s mode!\n", adev->pm.ac_power ? "AC" : "DC");
1208 ret = smu_notify_display_change(smu);
1213 * Set min deep sleep dce fclk with bootup value from vbios via
1214 * SetMinDeepSleepDcefclk MSG.
1216 ret = smu_set_min_dcef_deep_sleep(smu);
1223 static int smu_start_smc_engine(struct smu_context *smu)
1225 struct amdgpu_device *adev = smu->adev;
1228 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1229 if (adev->asic_type < CHIP_NAVI10) {
1230 if (smu->ppt_funcs->load_microcode) {
1231 ret = smu->ppt_funcs->load_microcode(smu);
1238 if (smu->ppt_funcs->check_fw_status) {
1239 ret = smu->ppt_funcs->check_fw_status(smu);
1241 pr_err("SMC is not ready\n");
1247 * Send msg GetDriverIfVersion to check if the return value is equal
1248 * with DRIVER_IF_VERSION of smc header.
1250 ret = smu_check_fw_version(smu);
1257 static int smu_hw_init(void *handle)
1260 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1261 struct smu_context *smu = &adev->smu;
1263 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
1266 ret = smu_start_smc_engine(smu);
1268 pr_err("SMU is not ready yet!\n");
1273 smu_powergate_sdma(&adev->smu, false);
1274 smu_powergate_vcn(&adev->smu, false);
1275 smu_powergate_jpeg(&adev->smu, false);
1276 smu_set_gfx_cgpg(&adev->smu, true);
1279 if (!smu->pm_enabled)
1282 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1283 ret = smu_get_vbios_bootup_values(smu);
1287 ret = smu_setup_pptable(smu);
1291 ret = smu_get_driver_allowed_feature_mask(smu);
1295 ret = smu_smc_hw_setup(smu);
1299 adev->pm.dpm_enabled = true;
1301 pr_info("SMU is initialized successfully!\n");
1309 static int smu_disable_dpms(struct smu_context *smu)
1311 struct amdgpu_device *adev = smu->adev;
1312 uint64_t features_to_disable;
1314 bool use_baco = !smu->is_apu &&
1315 ((adev->in_gpu_reset &&
1316 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1317 ((adev->in_runpm || adev->in_hibernate) && amdgpu_asic_supports_baco(adev)));
1320 * For custom pptable uploading, skip the DPM features
1321 * disable process on Navi1x ASICs.
1322 * - As the gfx related features are under control of
1323 * RLC on those ASICs. RLC reinitialization will be
1324 * needed to reenable them. That will cost much more
1327 * - SMU firmware can handle the DPM reenablement
1330 if (smu->uploading_custom_pp_table &&
1331 (adev->asic_type >= CHIP_NAVI10) &&
1332 (adev->asic_type <= CHIP_NAVI12))
1336 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1337 * on BACO in. Driver involvement is unnecessary.
1339 if ((adev->asic_type == CHIP_SIENNA_CICHLID) &&
1344 * For gpu reset, runpm and hibernation through BACO,
1345 * BACO feature has to be kept enabled.
1347 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1348 features_to_disable = U64_MAX &
1349 ~(1ULL << smu_feature_get_index(smu, SMU_FEATURE_BACO_BIT));
1350 ret = smu_feature_update_enable_state(smu,
1351 features_to_disable,
1354 pr_err("Failed to disable smu features except BACO.\n");
1356 ret = smu_system_features_control(smu, false);
1358 pr_err("Failed to disable smu features.\n");
1361 if (adev->asic_type >= CHIP_NAVI10 &&
1362 adev->gfx.rlc.funcs->stop)
1363 adev->gfx.rlc.funcs->stop(adev);
1368 static int smu_smc_hw_cleanup(struct smu_context *smu)
1370 struct amdgpu_device *adev = smu->adev;
1373 smu_i2c_eeprom_fini(smu, &adev->pm.smu_i2c);
1375 ret = smu_disable_thermal_alert(smu);
1377 pr_warn("Fail to stop thermal control!\n");
1381 ret = smu_disable_dpms(smu);
1388 static int smu_hw_fini(void *handle)
1390 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1391 struct smu_context *smu = &adev->smu;
1394 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1398 smu_powergate_sdma(&adev->smu, true);
1399 smu_powergate_vcn(&adev->smu, true);
1400 smu_powergate_jpeg(&adev->smu, true);
1403 if (!smu->pm_enabled)
1406 adev->pm.dpm_enabled = false;
1408 ret = smu_smc_hw_cleanup(smu);
1415 int smu_reset(struct smu_context *smu)
1417 struct amdgpu_device *adev = smu->adev;
1420 ret = smu_hw_fini(adev);
1424 ret = smu_hw_init(adev);
1428 ret = smu_late_init(adev);
1433 static int smu_suspend(void *handle)
1435 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1436 struct smu_context *smu = &adev->smu;
1439 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1442 if (!smu->pm_enabled)
1445 adev->pm.dpm_enabled = false;
1447 ret = smu_smc_hw_cleanup(smu);
1451 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1454 smu_set_gfx_cgpg(&adev->smu, false);
1459 static int smu_resume(void *handle)
1462 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1463 struct smu_context *smu = &adev->smu;
1465 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1468 if (!smu->pm_enabled)
1471 pr_info("SMU is resuming...\n");
1473 ret = smu_start_smc_engine(smu);
1475 pr_err("SMU is not ready yet!\n");
1479 ret = smu_smc_hw_setup(smu);
1484 smu_set_gfx_cgpg(&adev->smu, true);
1486 smu->disable_uclk_switch = 0;
1488 adev->pm.dpm_enabled = true;
1490 pr_info("SMU is resumed successfully!\n");
1498 int smu_display_configuration_change(struct smu_context *smu,
1499 const struct amd_pp_display_configuration *display_config)
1502 int num_of_active_display = 0;
1504 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1507 if (!display_config)
1510 mutex_lock(&smu->mutex);
1512 if (smu->ppt_funcs->set_deep_sleep_dcefclk)
1513 smu->ppt_funcs->set_deep_sleep_dcefclk(smu,
1514 display_config->min_dcef_deep_sleep_set_clk / 100);
1516 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1517 if (display_config->displays[index].controller_id != 0)
1518 num_of_active_display++;
1521 smu_set_active_display_count(smu, num_of_active_display);
1523 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1524 display_config->cpu_cc6_disable,
1525 display_config->cpu_pstate_disable,
1526 display_config->nb_pstate_switch_disable);
1528 mutex_unlock(&smu->mutex);
1533 static int smu_get_clock_info(struct smu_context *smu,
1534 struct smu_clock_info *clk_info,
1535 enum smu_perf_level_designation designation)
1538 struct smu_performance_level level = {0};
1543 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1547 clk_info->min_mem_clk = level.memory_clock;
1548 clk_info->min_eng_clk = level.core_clock;
1549 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1551 ret = smu_get_perf_level(smu, designation, &level);
1555 clk_info->min_mem_clk = level.memory_clock;
1556 clk_info->min_eng_clk = level.core_clock;
1557 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1562 int smu_get_current_clocks(struct smu_context *smu,
1563 struct amd_pp_clock_info *clocks)
1565 struct amd_pp_simple_clock_info simple_clocks = {0};
1566 struct smu_clock_info hw_clocks;
1569 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1572 mutex_lock(&smu->mutex);
1574 smu_get_dal_power_level(smu, &simple_clocks);
1576 if (smu->support_power_containment)
1577 ret = smu_get_clock_info(smu, &hw_clocks,
1578 PERF_LEVEL_POWER_CONTAINMENT);
1580 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1583 pr_err("Error in smu_get_clock_info\n");
1587 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1588 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1589 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1590 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1591 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1592 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1593 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1594 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1596 if (simple_clocks.level == 0)
1597 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1599 clocks->max_clocks_state = simple_clocks.level;
1601 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1602 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1603 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1607 mutex_unlock(&smu->mutex);
1611 static int smu_set_clockgating_state(void *handle,
1612 enum amd_clockgating_state state)
1617 static int smu_set_powergating_state(void *handle,
1618 enum amd_powergating_state state)
1623 static int smu_enable_umd_pstate(void *handle,
1624 enum amd_dpm_forced_level *level)
1626 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1627 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1628 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1629 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1631 struct smu_context *smu = (struct smu_context*)(handle);
1632 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1634 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1637 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1638 /* enter umd pstate, save current level, disable gfx cg*/
1639 if (*level & profile_mode_mask) {
1640 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1641 smu_dpm_ctx->enable_umd_pstate = true;
1642 amdgpu_device_ip_set_powergating_state(smu->adev,
1643 AMD_IP_BLOCK_TYPE_GFX,
1644 AMD_PG_STATE_UNGATE);
1645 amdgpu_device_ip_set_clockgating_state(smu->adev,
1646 AMD_IP_BLOCK_TYPE_GFX,
1647 AMD_CG_STATE_UNGATE);
1650 /* exit umd pstate, restore level, enable gfx cg*/
1651 if (!(*level & profile_mode_mask)) {
1652 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1653 *level = smu_dpm_ctx->saved_dpm_level;
1654 smu_dpm_ctx->enable_umd_pstate = false;
1655 amdgpu_device_ip_set_clockgating_state(smu->adev,
1656 AMD_IP_BLOCK_TYPE_GFX,
1658 amdgpu_device_ip_set_powergating_state(smu->adev,
1659 AMD_IP_BLOCK_TYPE_GFX,
1667 int smu_adjust_power_state_dynamic(struct smu_context *smu,
1668 enum amd_dpm_forced_level level,
1669 bool skip_display_settings)
1674 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1676 if (!skip_display_settings) {
1677 ret = smu_display_config_changed(smu);
1679 pr_err("Failed to change display config!");
1684 ret = smu_apply_clocks_adjust_rules(smu);
1686 pr_err("Failed to apply clocks adjust rules!");
1690 if (!skip_display_settings) {
1691 ret = smu_notify_smc_display_config(smu);
1693 pr_err("Failed to notify smc display config!");
1698 if (smu_dpm_ctx->dpm_level != level) {
1699 ret = smu_asic_set_performance_level(smu, level);
1701 pr_err("Failed to set performance level!");
1705 /* update the saved copy */
1706 smu_dpm_ctx->dpm_level = level;
1709 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1710 index = fls(smu->workload_mask);
1711 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1712 workload = smu->workload_setting[index];
1714 if (smu->power_profile_mode != workload)
1715 smu_set_power_profile_mode(smu, &workload, 0, false);
1721 int smu_handle_task(struct smu_context *smu,
1722 enum amd_dpm_forced_level level,
1723 enum amd_pp_task task_id,
1728 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1732 mutex_lock(&smu->mutex);
1735 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1736 ret = smu_pre_display_config_changed(smu);
1739 ret = smu_set_cpu_power_state(smu);
1742 ret = smu_adjust_power_state_dynamic(smu, level, false);
1744 case AMD_PP_TASK_COMPLETE_INIT:
1745 case AMD_PP_TASK_READJUST_POWER_STATE:
1746 ret = smu_adjust_power_state_dynamic(smu, level, true);
1754 mutex_unlock(&smu->mutex);
1759 int smu_switch_power_profile(struct smu_context *smu,
1760 enum PP_SMC_POWER_PROFILE type,
1763 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1767 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1770 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1773 mutex_lock(&smu->mutex);
1776 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1777 index = fls(smu->workload_mask);
1778 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1779 workload = smu->workload_setting[index];
1781 smu->workload_mask |= (1 << smu->workload_prority[type]);
1782 index = fls(smu->workload_mask);
1783 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1784 workload = smu->workload_setting[index];
1787 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1788 smu_set_power_profile_mode(smu, &workload, 0, false);
1790 mutex_unlock(&smu->mutex);
1795 enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1797 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1798 enum amd_dpm_forced_level level;
1800 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1803 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1806 mutex_lock(&(smu->mutex));
1807 level = smu_dpm_ctx->dpm_level;
1808 mutex_unlock(&(smu->mutex));
1813 int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1815 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1818 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1821 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1824 mutex_lock(&smu->mutex);
1826 ret = smu_enable_umd_pstate(smu, &level);
1828 mutex_unlock(&smu->mutex);
1832 ret = smu_handle_task(smu, level,
1833 AMD_PP_TASK_READJUST_POWER_STATE,
1836 mutex_unlock(&smu->mutex);
1841 int smu_set_display_count(struct smu_context *smu, uint32_t count)
1845 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1848 mutex_lock(&smu->mutex);
1849 ret = smu_init_display_count(smu, count);
1850 mutex_unlock(&smu->mutex);
1855 int smu_force_clk_levels(struct smu_context *smu,
1856 enum smu_clk_type clk_type,
1860 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1863 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1866 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1867 pr_debug("force clock level is for dpm manual mode only.\n");
1872 mutex_lock(&smu->mutex);
1874 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels)
1875 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1878 mutex_unlock(&smu->mutex);
1884 * On system suspending or resetting, the dpm_enabled
1885 * flag will be cleared. So that those SMU services which
1886 * are not supported will be gated.
1887 * However, the mp1 state setting should still be granted
1888 * even if the dpm_enabled cleared.
1890 int smu_set_mp1_state(struct smu_context *smu,
1891 enum pp_mp1_state mp1_state)
1896 if (!smu->pm_enabled)
1899 mutex_lock(&smu->mutex);
1901 switch (mp1_state) {
1902 case PP_MP1_STATE_SHUTDOWN:
1903 msg = SMU_MSG_PrepareMp1ForShutdown;
1905 case PP_MP1_STATE_UNLOAD:
1906 msg = SMU_MSG_PrepareMp1ForUnload;
1908 case PP_MP1_STATE_RESET:
1909 msg = SMU_MSG_PrepareMp1ForReset;
1911 case PP_MP1_STATE_NONE:
1913 mutex_unlock(&smu->mutex);
1917 /* some asics may not support those messages */
1918 if (smu_msg_get_index(smu, msg) < 0) {
1919 mutex_unlock(&smu->mutex);
1923 ret = smu_send_smc_msg(smu, msg, NULL);
1925 pr_err("[PrepareMp1] Failed!\n");
1927 mutex_unlock(&smu->mutex);
1932 int smu_set_df_cstate(struct smu_context *smu,
1933 enum pp_df_cstate state)
1937 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1940 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
1943 mutex_lock(&smu->mutex);
1945 ret = smu->ppt_funcs->set_df_cstate(smu, state);
1947 pr_err("[SetDfCstate] failed!\n");
1949 mutex_unlock(&smu->mutex);
1954 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en)
1958 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1961 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down)
1964 mutex_lock(&smu->mutex);
1966 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en);
1968 pr_err("[AllowXgmiPowerDown] failed!\n");
1970 mutex_unlock(&smu->mutex);
1975 int smu_write_watermarks_table(struct smu_context *smu)
1977 void *watermarks_table = smu->smu_table.watermarks_table;
1979 if (!watermarks_table)
1982 return smu_update_table(smu,
1983 SMU_TABLE_WATERMARKS,
1989 int smu_set_watermarks_for_clock_ranges(struct smu_context *smu,
1990 struct dm_pp_wm_sets_with_clock_ranges_soc15 *clock_ranges)
1992 void *table = smu->smu_table.watermarks_table;
1994 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2000 mutex_lock(&smu->mutex);
2002 if (!smu->disable_watermark &&
2003 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_DCEFCLK_BIT) &&
2004 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT)) {
2005 smu_set_watermarks_table(smu, table, clock_ranges);
2007 if (!(smu->watermarks_bitmap & WATERMARKS_EXIST)) {
2008 smu->watermarks_bitmap |= WATERMARKS_EXIST;
2009 smu->watermarks_bitmap &= ~WATERMARKS_LOADED;
2013 mutex_unlock(&smu->mutex);
2018 int smu_set_ac_dc(struct smu_context *smu)
2022 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2025 /* controlled by firmware */
2026 if (smu->dc_controlled_by_gpio)
2029 mutex_lock(&smu->mutex);
2030 ret = smu_set_power_source(smu,
2031 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
2032 SMU_POWER_SOURCE_DC);
2034 pr_err("Failed to switch to %s mode!\n",
2035 smu->adev->pm.ac_power ? "AC" : "DC");
2036 mutex_unlock(&smu->mutex);
2041 const struct amd_ip_funcs smu_ip_funcs = {
2043 .early_init = smu_early_init,
2044 .late_init = smu_late_init,
2045 .sw_init = smu_sw_init,
2046 .sw_fini = smu_sw_fini,
2047 .hw_init = smu_hw_init,
2048 .hw_fini = smu_hw_fini,
2049 .suspend = smu_suspend,
2050 .resume = smu_resume,
2052 .check_soft_reset = NULL,
2053 .wait_for_idle = NULL,
2055 .set_clockgating_state = smu_set_clockgating_state,
2056 .set_powergating_state = smu_set_powergating_state,
2057 .enable_umd_pstate = smu_enable_umd_pstate,
2060 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
2062 .type = AMD_IP_BLOCK_TYPE_SMC,
2066 .funcs = &smu_ip_funcs,
2069 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
2071 .type = AMD_IP_BLOCK_TYPE_SMC,
2075 .funcs = &smu_ip_funcs,
2078 int smu_load_microcode(struct smu_context *smu)
2082 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2085 mutex_lock(&smu->mutex);
2087 if (smu->ppt_funcs->load_microcode)
2088 ret = smu->ppt_funcs->load_microcode(smu);
2090 mutex_unlock(&smu->mutex);
2095 int smu_check_fw_status(struct smu_context *smu)
2099 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2102 mutex_lock(&smu->mutex);
2104 if (smu->ppt_funcs->check_fw_status)
2105 ret = smu->ppt_funcs->check_fw_status(smu);
2107 mutex_unlock(&smu->mutex);
2112 int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2116 mutex_lock(&smu->mutex);
2118 if (smu->ppt_funcs->set_gfx_cgpg)
2119 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2121 mutex_unlock(&smu->mutex);
2126 int smu_set_fan_speed_rpm(struct smu_context *smu, uint32_t speed)
2130 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2133 mutex_lock(&smu->mutex);
2135 if (smu->ppt_funcs->set_fan_speed_rpm)
2136 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2138 mutex_unlock(&smu->mutex);
2143 int smu_get_power_limit(struct smu_context *smu,
2151 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2154 mutex_lock(&smu->mutex);
2157 if (smu->ppt_funcs->get_power_limit)
2158 ret = smu->ppt_funcs->get_power_limit(smu, limit, def);
2161 mutex_unlock(&smu->mutex);
2166 int smu_set_power_limit(struct smu_context *smu, uint32_t limit)
2170 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2173 mutex_lock(&smu->mutex);
2175 if (smu->ppt_funcs->set_power_limit)
2176 ret = smu->ppt_funcs->set_power_limit(smu, limit);
2178 mutex_unlock(&smu->mutex);
2183 int smu_print_clk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2187 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2190 mutex_lock(&smu->mutex);
2192 if (smu->ppt_funcs->print_clk_levels)
2193 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2195 mutex_unlock(&smu->mutex);
2200 int smu_get_od_percentage(struct smu_context *smu, enum smu_clk_type type)
2204 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2207 mutex_lock(&smu->mutex);
2209 if (smu->ppt_funcs->get_od_percentage)
2210 ret = smu->ppt_funcs->get_od_percentage(smu, type);
2212 mutex_unlock(&smu->mutex);
2217 int smu_set_od_percentage(struct smu_context *smu, enum smu_clk_type type, uint32_t value)
2221 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2224 mutex_lock(&smu->mutex);
2226 if (smu->ppt_funcs->set_od_percentage)
2227 ret = smu->ppt_funcs->set_od_percentage(smu, type, value);
2229 mutex_unlock(&smu->mutex);
2234 int smu_od_edit_dpm_table(struct smu_context *smu,
2235 enum PP_OD_DPM_TABLE_COMMAND type,
2236 long *input, uint32_t size)
2240 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2243 mutex_lock(&smu->mutex);
2245 if (smu->ppt_funcs->od_edit_dpm_table)
2246 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2248 mutex_unlock(&smu->mutex);
2253 int smu_read_sensor(struct smu_context *smu,
2254 enum amd_pp_sensors sensor,
2255 void *data, uint32_t *size)
2259 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2262 mutex_lock(&smu->mutex);
2264 if (smu->ppt_funcs->read_sensor)
2265 ret = smu->ppt_funcs->read_sensor(smu, sensor, data, size);
2267 mutex_unlock(&smu->mutex);
2272 int smu_get_power_profile_mode(struct smu_context *smu, char *buf)
2276 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2279 mutex_lock(&smu->mutex);
2281 if (smu->ppt_funcs->get_power_profile_mode)
2282 ret = smu->ppt_funcs->get_power_profile_mode(smu, buf);
2284 mutex_unlock(&smu->mutex);
2289 int smu_set_power_profile_mode(struct smu_context *smu,
2291 uint32_t param_size,
2296 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2300 mutex_lock(&smu->mutex);
2302 if (smu->ppt_funcs->set_power_profile_mode)
2303 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
2306 mutex_unlock(&smu->mutex);
2312 int smu_get_fan_control_mode(struct smu_context *smu)
2316 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2319 mutex_lock(&smu->mutex);
2321 if (smu->ppt_funcs->get_fan_control_mode)
2322 ret = smu->ppt_funcs->get_fan_control_mode(smu);
2324 mutex_unlock(&smu->mutex);
2329 int smu_set_fan_control_mode(struct smu_context *smu, int value)
2333 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2336 mutex_lock(&smu->mutex);
2338 if (smu->ppt_funcs->set_fan_control_mode)
2339 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2341 mutex_unlock(&smu->mutex);
2346 int smu_get_fan_speed_percent(struct smu_context *smu, uint32_t *speed)
2350 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2353 mutex_lock(&smu->mutex);
2355 if (smu->ppt_funcs->get_fan_speed_percent)
2356 ret = smu->ppt_funcs->get_fan_speed_percent(smu, speed);
2358 mutex_unlock(&smu->mutex);
2363 int smu_set_fan_speed_percent(struct smu_context *smu, uint32_t speed)
2367 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2370 mutex_lock(&smu->mutex);
2372 if (smu->ppt_funcs->set_fan_speed_percent)
2373 ret = smu->ppt_funcs->set_fan_speed_percent(smu, speed);
2375 mutex_unlock(&smu->mutex);
2380 int smu_get_fan_speed_rpm(struct smu_context *smu, uint32_t *speed)
2384 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2387 mutex_lock(&smu->mutex);
2389 if (smu->ppt_funcs->get_fan_speed_rpm)
2390 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2392 mutex_unlock(&smu->mutex);
2397 int smu_set_deep_sleep_dcefclk(struct smu_context *smu, int clk)
2401 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2404 mutex_lock(&smu->mutex);
2406 if (smu->ppt_funcs->set_deep_sleep_dcefclk)
2407 ret = smu->ppt_funcs->set_deep_sleep_dcefclk(smu, clk);
2409 mutex_unlock(&smu->mutex);
2414 int smu_set_active_display_count(struct smu_context *smu, uint32_t count)
2418 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2421 if (smu->ppt_funcs->set_active_display_count)
2422 ret = smu->ppt_funcs->set_active_display_count(smu, count);
2427 int smu_get_clock_by_type(struct smu_context *smu,
2428 enum amd_pp_clock_type type,
2429 struct amd_pp_clocks *clocks)
2433 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2436 mutex_lock(&smu->mutex);
2438 if (smu->ppt_funcs->get_clock_by_type)
2439 ret = smu->ppt_funcs->get_clock_by_type(smu, type, clocks);
2441 mutex_unlock(&smu->mutex);
2446 int smu_get_max_high_clocks(struct smu_context *smu,
2447 struct amd_pp_simple_clock_info *clocks)
2451 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2454 mutex_lock(&smu->mutex);
2456 if (smu->ppt_funcs->get_max_high_clocks)
2457 ret = smu->ppt_funcs->get_max_high_clocks(smu, clocks);
2459 mutex_unlock(&smu->mutex);
2464 int smu_get_clock_by_type_with_latency(struct smu_context *smu,
2465 enum smu_clk_type clk_type,
2466 struct pp_clock_levels_with_latency *clocks)
2470 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2473 mutex_lock(&smu->mutex);
2475 if (smu->ppt_funcs->get_clock_by_type_with_latency)
2476 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2478 mutex_unlock(&smu->mutex);
2483 int smu_get_clock_by_type_with_voltage(struct smu_context *smu,
2484 enum amd_pp_clock_type type,
2485 struct pp_clock_levels_with_voltage *clocks)
2489 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2492 mutex_lock(&smu->mutex);
2494 if (smu->ppt_funcs->get_clock_by_type_with_voltage)
2495 ret = smu->ppt_funcs->get_clock_by_type_with_voltage(smu, type, clocks);
2497 mutex_unlock(&smu->mutex);
2503 int smu_display_clock_voltage_request(struct smu_context *smu,
2504 struct pp_display_clock_request *clock_req)
2508 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2511 mutex_lock(&smu->mutex);
2513 if (smu->ppt_funcs->display_clock_voltage_request)
2514 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2516 mutex_unlock(&smu->mutex);
2522 int smu_display_disable_memory_clock_switch(struct smu_context *smu, bool disable_memory_clock_switch)
2526 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2529 mutex_lock(&smu->mutex);
2531 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2532 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2534 mutex_unlock(&smu->mutex);
2539 int smu_notify_smu_enable_pwe(struct smu_context *smu)
2543 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2546 mutex_lock(&smu->mutex);
2548 if (smu->ppt_funcs->notify_smu_enable_pwe)
2549 ret = smu->ppt_funcs->notify_smu_enable_pwe(smu);
2551 mutex_unlock(&smu->mutex);
2556 int smu_set_xgmi_pstate(struct smu_context *smu,
2561 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2564 mutex_lock(&smu->mutex);
2566 if (smu->ppt_funcs->set_xgmi_pstate)
2567 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2569 mutex_unlock(&smu->mutex);
2574 int smu_set_azalia_d3_pme(struct smu_context *smu)
2578 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2581 mutex_lock(&smu->mutex);
2583 if (smu->ppt_funcs->set_azalia_d3_pme)
2584 ret = smu->ppt_funcs->set_azalia_d3_pme(smu);
2586 mutex_unlock(&smu->mutex);
2592 * On system suspending or resetting, the dpm_enabled
2593 * flag will be cleared. So that those SMU services which
2594 * are not supported will be gated.
2596 * However, the baco/mode1 reset should still be granted
2597 * as they are still supported and necessary.
2599 bool smu_baco_is_support(struct smu_context *smu)
2603 if (!smu->pm_enabled)
2606 mutex_lock(&smu->mutex);
2608 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2609 ret = smu->ppt_funcs->baco_is_support(smu);
2611 mutex_unlock(&smu->mutex);
2616 int smu_baco_get_state(struct smu_context *smu, enum smu_baco_state *state)
2618 if (smu->ppt_funcs->baco_get_state)
2621 mutex_lock(&smu->mutex);
2622 *state = smu->ppt_funcs->baco_get_state(smu);
2623 mutex_unlock(&smu->mutex);
2628 int smu_baco_enter(struct smu_context *smu)
2632 if (!smu->pm_enabled)
2635 mutex_lock(&smu->mutex);
2637 if (smu->ppt_funcs->baco_enter)
2638 ret = smu->ppt_funcs->baco_enter(smu);
2640 mutex_unlock(&smu->mutex);
2645 int smu_baco_exit(struct smu_context *smu)
2649 if (!smu->pm_enabled)
2652 mutex_lock(&smu->mutex);
2654 if (smu->ppt_funcs->baco_exit)
2655 ret = smu->ppt_funcs->baco_exit(smu);
2657 mutex_unlock(&smu->mutex);
2662 int smu_mode2_reset(struct smu_context *smu)
2666 if (!smu->pm_enabled)
2669 mutex_lock(&smu->mutex);
2671 if (smu->ppt_funcs->mode2_reset)
2672 ret = smu->ppt_funcs->mode2_reset(smu);
2674 mutex_unlock(&smu->mutex);
2679 int smu_get_max_sustainable_clocks_by_dc(struct smu_context *smu,
2680 struct pp_smu_nv_clock_table *max_clocks)
2684 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2687 mutex_lock(&smu->mutex);
2689 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2690 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2692 mutex_unlock(&smu->mutex);
2697 int smu_get_uclk_dpm_states(struct smu_context *smu,
2698 unsigned int *clock_values_in_khz,
2699 unsigned int *num_states)
2703 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2706 mutex_lock(&smu->mutex);
2708 if (smu->ppt_funcs->get_uclk_dpm_states)
2709 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2711 mutex_unlock(&smu->mutex);
2716 enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
2718 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2720 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2723 mutex_lock(&smu->mutex);
2725 if (smu->ppt_funcs->get_current_power_state)
2726 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2728 mutex_unlock(&smu->mutex);
2733 int smu_get_dpm_clock_table(struct smu_context *smu,
2734 struct dpm_clocks *clock_table)
2738 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2741 mutex_lock(&smu->mutex);
2743 if (smu->ppt_funcs->get_dpm_clock_table)
2744 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2746 mutex_unlock(&smu->mutex);
2751 uint32_t smu_get_pptable_power_limit(struct smu_context *smu)
2755 if (smu->ppt_funcs->get_pptable_power_limit)
2756 ret = smu->ppt_funcs->get_pptable_power_limit(smu);
2761 int smu_powergate_vcn(struct smu_context *smu, bool gate)
2766 return smu_dpm_set_uvd_enable(smu, !gate);
2769 int smu_powergate_jpeg(struct smu_context *smu, bool gate)
2774 return smu_dpm_set_jpeg_enable(smu, !gate);