2 * Copyright 2019 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include <linux/firmware.h>
27 #include "amdgpu_smu.h"
28 #include "soc15_common.h"
29 #include "smu_v11_0.h"
33 int smu_get_smc_version(struct smu_context *smu, uint32_t *if_version, uint32_t *smu_version)
37 if (!if_version && !smu_version)
41 ret = smu_send_smc_msg(smu, SMU_MSG_GetDriverIfVersion);
45 ret = smu_read_smc_arg(smu, if_version);
51 ret = smu_send_smc_msg(smu, SMU_MSG_GetSmuVersion);
55 ret = smu_read_smc_arg(smu, smu_version);
63 int smu_set_soft_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
64 uint32_t min, uint32_t max)
66 int ret = 0, clk_id = 0;
69 if (min <= 0 && max <= 0)
72 clk_id = smu_clk_get_index(smu, clk_type);
77 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
78 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxByFreq,
85 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
86 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMinByFreq,
96 int smu_set_hard_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
97 uint32_t min, uint32_t max)
99 int ret = 0, clk_id = 0;
102 if (min <= 0 && max <= 0)
105 clk_id = smu_clk_get_index(smu, clk_type);
110 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
111 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMaxByFreq,
118 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
119 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
129 int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
130 uint32_t *min, uint32_t *max)
132 int ret = 0, clk_id = 0;
141 if (!smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UCLK_BIT)) {
142 pr_warn("uclk dpm is not enabled\n");
148 if (!smu_feature_is_enabled(smu, SMU_FEATURE_DPM_GFXCLK_BIT)) {
149 pr_warn("gfxclk dpm is not enabled\n");
157 mutex_lock(&smu->mutex);
158 clk_id = smu_clk_get_index(smu, clk_type);
164 param = (clk_id & 0xffff) << 16;
167 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMaxDpmFreq, param);
170 ret = smu_read_smc_arg(smu, max);
176 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMinDpmFreq, param);
179 ret = smu_read_smc_arg(smu, min);
185 mutex_unlock(&smu->mutex);
189 int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
190 uint16_t level, uint32_t *value)
192 int ret = 0, clk_id = 0;
198 clk_id = smu_clk_get_index(smu, clk_type);
202 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
204 ret = smu_send_smc_msg_with_param(smu,SMU_MSG_GetDpmFreqByIndex,
209 ret = smu_read_smc_arg(smu, ¶m);
213 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
214 * now, we un-support it */
215 *value = param & 0x7fffffff;
220 int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
223 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
226 int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
231 switch (block_type) {
232 case AMD_IP_BLOCK_TYPE_UVD:
233 ret = smu_dpm_set_uvd_enable(smu, gate);
235 case AMD_IP_BLOCK_TYPE_VCE:
236 ret = smu_dpm_set_vce_enable(smu, gate);
238 case AMD_IP_BLOCK_TYPE_GFX:
239 ret = smu_gfx_off_control(smu, gate);
248 enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
250 /* not support power state */
251 return POWER_STATE_TYPE_DEFAULT;
254 int smu_get_power_num_states(struct smu_context *smu,
255 struct pp_states_info *state_info)
260 /* not support power state */
261 memset(state_info, 0, sizeof(struct pp_states_info));
262 state_info->nums = 0;
267 int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
268 void *data, uint32_t *size)
273 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
274 *((uint32_t *)data) = smu->pstate_sclk;
277 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
278 *((uint32_t *)data) = smu->pstate_mclk;
281 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
282 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
285 case AMDGPU_PP_SENSOR_UVD_POWER:
286 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
289 case AMDGPU_PP_SENSOR_VCE_POWER:
290 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
304 int smu_update_table(struct smu_context *smu, enum smu_table_id table_index,
305 void *table_data, bool drv2smu)
307 struct smu_table_context *smu_table = &smu->smu_table;
308 struct smu_table *table = NULL;
310 int table_id = smu_table_get_index(smu, table_index);
312 if (!table_data || table_id >= smu_table->table_count)
315 table = &smu_table->tables[table_index];
318 memcpy(table->cpu_addr, table_data, table->size);
320 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrHigh,
321 upper_32_bits(table->mc_address));
324 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrLow,
325 lower_32_bits(table->mc_address));
328 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
329 SMU_MSG_TransferTableDram2Smu :
330 SMU_MSG_TransferTableSmu2Dram,
336 memcpy(table_data, table->cpu_addr, table->size);
341 bool is_support_sw_smu(struct amdgpu_device *adev)
343 if (adev->asic_type == CHIP_VEGA20)
344 return (amdgpu_dpm == 2) ? true : false;
345 else if (adev->asic_type >= CHIP_NAVI10)
351 int smu_sys_get_pp_table(struct smu_context *smu, void **table)
353 struct smu_table_context *smu_table = &smu->smu_table;
355 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
358 if (smu_table->hardcode_pptable)
359 *table = smu_table->hardcode_pptable;
361 *table = smu_table->power_play_table;
363 return smu_table->power_play_table_size;
366 int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
368 struct smu_table_context *smu_table = &smu->smu_table;
369 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
372 if (!smu->pm_enabled)
374 if (header->usStructureSize != size) {
375 pr_err("pp table size not matched !\n");
379 mutex_lock(&smu->mutex);
380 if (!smu_table->hardcode_pptable)
381 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
382 if (!smu_table->hardcode_pptable) {
387 memcpy(smu_table->hardcode_pptable, buf, size);
388 smu_table->power_play_table = smu_table->hardcode_pptable;
389 smu_table->power_play_table_size = size;
390 mutex_unlock(&smu->mutex);
392 ret = smu_reset(smu);
394 pr_info("smu reset failed, ret = %d\n", ret);
399 mutex_unlock(&smu->mutex);
403 int smu_feature_init_dpm(struct smu_context *smu)
405 struct smu_feature *feature = &smu->smu_feature;
407 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
409 if (!smu->pm_enabled)
411 mutex_lock(&feature->mutex);
412 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
413 mutex_unlock(&feature->mutex);
415 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
420 mutex_lock(&feature->mutex);
421 bitmap_or(feature->allowed, feature->allowed,
422 (unsigned long *)allowed_feature_mask,
423 feature->feature_num);
424 mutex_unlock(&feature->mutex);
429 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
431 struct smu_feature *feature = &smu->smu_feature;
435 feature_id = smu_feature_get_index(smu, mask);
437 WARN_ON(feature_id > feature->feature_num);
439 mutex_lock(&feature->mutex);
440 ret = test_bit(feature_id, feature->enabled);
441 mutex_unlock(&feature->mutex);
446 int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
449 struct smu_feature *feature = &smu->smu_feature;
453 feature_id = smu_feature_get_index(smu, mask);
455 WARN_ON(feature_id > feature->feature_num);
457 mutex_lock(&feature->mutex);
458 ret = smu_feature_update_enable_state(smu, feature_id, enable);
463 test_and_set_bit(feature_id, feature->enabled);
465 test_and_clear_bit(feature_id, feature->enabled);
468 mutex_unlock(&feature->mutex);
473 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
475 struct smu_feature *feature = &smu->smu_feature;
479 feature_id = smu_feature_get_index(smu, mask);
481 WARN_ON(feature_id > feature->feature_num);
483 mutex_lock(&feature->mutex);
484 ret = test_bit(feature_id, feature->supported);
485 mutex_unlock(&feature->mutex);
490 int smu_feature_set_supported(struct smu_context *smu,
491 enum smu_feature_mask mask,
494 struct smu_feature *feature = &smu->smu_feature;
498 feature_id = smu_feature_get_index(smu, mask);
500 WARN_ON(feature_id > feature->feature_num);
502 mutex_lock(&feature->mutex);
504 test_and_set_bit(feature_id, feature->supported);
506 test_and_clear_bit(feature_id, feature->supported);
507 mutex_unlock(&feature->mutex);
512 static int smu_set_funcs(struct amdgpu_device *adev)
514 struct smu_context *smu = &adev->smu;
516 switch (adev->asic_type) {
519 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
520 smu->od_enabled = true;
521 smu_v11_0_set_smu_funcs(smu);
530 static int smu_early_init(void *handle)
532 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
533 struct smu_context *smu = &adev->smu;
536 smu->pm_enabled = !!amdgpu_dpm;
537 mutex_init(&smu->mutex);
539 return smu_set_funcs(adev);
542 static int smu_late_init(void *handle)
544 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
545 struct smu_context *smu = &adev->smu;
547 if (!smu->pm_enabled)
549 mutex_lock(&smu->mutex);
550 smu_handle_task(&adev->smu,
551 smu->smu_dpm.dpm_level,
552 AMD_PP_TASK_COMPLETE_INIT);
553 mutex_unlock(&smu->mutex);
558 int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
559 uint16_t *size, uint8_t *frev, uint8_t *crev,
562 struct amdgpu_device *adev = smu->adev;
565 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
566 size, frev, crev, &data_start))
569 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
574 static int smu_initialize_pptable(struct smu_context *smu)
580 static int smu_smc_table_sw_init(struct smu_context *smu)
584 ret = smu_initialize_pptable(smu);
586 pr_err("Failed to init smu_initialize_pptable!\n");
591 * Create smu_table structure, and init smc tables such as
592 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
594 ret = smu_init_smc_tables(smu);
596 pr_err("Failed to init smc tables!\n");
601 * Create smu_power_context structure, and allocate smu_dpm_context and
602 * context size to fill the smu_power_context data.
604 ret = smu_init_power(smu);
606 pr_err("Failed to init smu_init_power!\n");
613 static int smu_smc_table_sw_fini(struct smu_context *smu)
617 ret = smu_fini_smc_tables(smu);
619 pr_err("Failed to smu_fini_smc_tables!\n");
626 static int smu_sw_init(void *handle)
628 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
629 struct smu_context *smu = &adev->smu;
632 smu->pool_size = adev->pm.smu_prv_buffer_size;
633 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
634 mutex_init(&smu->smu_feature.mutex);
635 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
636 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
637 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
639 mutex_init(&smu->smu_baco.mutex);
640 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
641 smu->smu_baco.platform_support = false;
643 smu->watermarks_bitmap = 0;
644 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
645 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
647 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
648 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
649 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
650 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
651 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
652 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
653 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
654 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
656 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
657 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
658 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
659 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
660 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
661 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
662 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
663 smu->display_config = &adev->pm.pm_display_cfg;
665 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
666 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
667 ret = smu_init_microcode(smu);
669 pr_err("Failed to load smu firmware!\n");
673 ret = smu_smc_table_sw_init(smu);
675 pr_err("Failed to sw init smc table!\n");
682 static int smu_sw_fini(void *handle)
684 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
685 struct smu_context *smu = &adev->smu;
688 ret = smu_smc_table_sw_fini(smu);
690 pr_err("Failed to sw fini smc table!\n");
694 ret = smu_fini_power(smu);
696 pr_err("Failed to init smu_fini_power!\n");
703 static int smu_init_fb_allocations(struct smu_context *smu)
705 struct amdgpu_device *adev = smu->adev;
706 struct smu_table_context *smu_table = &smu->smu_table;
707 struct smu_table *tables = smu_table->tables;
708 uint32_t table_count = smu_table->table_count;
712 if (table_count <= 0)
715 for (i = 0 ; i < table_count; i++) {
716 if (tables[i].size == 0)
718 ret = amdgpu_bo_create_kernel(adev,
723 &tables[i].mc_address,
724 &tables[i].cpu_addr);
732 if (tables[i].size == 0)
734 amdgpu_bo_free_kernel(&tables[i].bo,
735 &tables[i].mc_address,
736 &tables[i].cpu_addr);
742 static int smu_fini_fb_allocations(struct smu_context *smu)
744 struct smu_table_context *smu_table = &smu->smu_table;
745 struct smu_table *tables = smu_table->tables;
746 uint32_t table_count = smu_table->table_count;
749 if (table_count == 0 || tables == NULL)
752 for (i = 0 ; i < table_count; i++) {
753 if (tables[i].size == 0)
755 amdgpu_bo_free_kernel(&tables[i].bo,
756 &tables[i].mc_address,
757 &tables[i].cpu_addr);
763 static int smu_override_pcie_parameters(struct smu_context *smu)
765 struct amdgpu_device *adev = smu->adev;
766 uint32_t pcie_gen = 0, pcie_width = 0, smu_pcie_arg;
769 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
771 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
773 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
775 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
778 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
779 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
780 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
782 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
784 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
786 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
788 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
790 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
792 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
795 smu_pcie_arg = (1 << 16) | (pcie_gen << 8) | pcie_width;
796 ret = smu_send_smc_msg_with_param(smu,
797 SMU_MSG_OverridePcieParameters,
800 pr_err("[%s] Attempt to override pcie params failed!\n", __func__);
804 static int smu_smc_table_hw_init(struct smu_context *smu,
807 struct amdgpu_device *adev = smu->adev;
810 if (smu_is_dpm_running(smu) && adev->in_suspend) {
811 pr_info("dpm has been enabled\n");
815 ret = smu_init_display_count(smu, 0);
820 /* get boot_values from vbios to set revision, gfxclk, and etc. */
821 ret = smu_get_vbios_bootup_values(smu);
825 ret = smu_setup_pptable(smu);
829 ret = smu_get_clk_info_from_vbios(smu);
834 * check if the format_revision in vbios is up to pptable header
835 * version, and the structure size is not 0.
837 ret = smu_check_pptable(smu);
842 * allocate vram bos to store smc table contents.
844 ret = smu_init_fb_allocations(smu);
849 * Parse pptable format and fill PPTable_t smc_pptable to
850 * smu_table_context structure. And read the smc_dpm_table from vbios,
851 * then fill it into smc_pptable.
853 ret = smu_parse_pptable(smu);
858 * Send msg GetDriverIfVersion to check if the return value is equal
859 * with DRIVER_IF_VERSION of smc header.
861 ret = smu_check_fw_version(smu);
867 * Copy pptable bo in the vram to smc with SMU MSGs such as
868 * SetDriverDramAddr and TransferTableDram2Smu.
870 ret = smu_write_pptable(smu);
874 /* issue RunAfllBtc msg */
875 ret = smu_run_afll_btc(smu);
879 ret = smu_feature_set_allowed_mask(smu);
883 ret = smu_system_features_control(smu, true);
887 ret = smu_override_pcie_parameters(smu);
891 ret = smu_notify_display_change(smu);
896 * Set min deep sleep dce fclk with bootup value from vbios via
897 * SetMinDeepSleepDcefclk MSG.
899 ret = smu_set_min_dcef_deep_sleep(smu);
904 * Set initialized values (get from vbios) to dpm tables context such as
905 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
909 ret = smu_populate_smc_pptable(smu);
913 ret = smu_init_max_sustainable_clocks(smu);
918 ret = smu_set_default_od_settings(smu, initialize);
923 ret = smu_populate_umd_state_clk(smu);
927 ret = smu_get_power_limit(smu, &smu->default_power_limit, false);
933 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
935 ret = smu_set_tool_table_location(smu);
937 if (!smu_is_dpm_running(smu))
938 pr_info("dpm has been disabled\n");
944 * smu_alloc_memory_pool - allocate memory pool in the system memory
946 * @smu: amdgpu_device pointer
948 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
949 * and DramLogSetDramAddr can notify it changed.
951 * Returns 0 on success, error on failure.
953 static int smu_alloc_memory_pool(struct smu_context *smu)
955 struct amdgpu_device *adev = smu->adev;
956 struct smu_table_context *smu_table = &smu->smu_table;
957 struct smu_table *memory_pool = &smu_table->memory_pool;
958 uint64_t pool_size = smu->pool_size;
961 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
964 memory_pool->size = pool_size;
965 memory_pool->align = PAGE_SIZE;
966 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
969 case SMU_MEMORY_POOL_SIZE_256_MB:
970 case SMU_MEMORY_POOL_SIZE_512_MB:
971 case SMU_MEMORY_POOL_SIZE_1_GB:
972 case SMU_MEMORY_POOL_SIZE_2_GB:
973 ret = amdgpu_bo_create_kernel(adev,
978 &memory_pool->mc_address,
979 &memory_pool->cpu_addr);
988 static int smu_free_memory_pool(struct smu_context *smu)
990 struct smu_table_context *smu_table = &smu->smu_table;
991 struct smu_table *memory_pool = &smu_table->memory_pool;
994 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
997 amdgpu_bo_free_kernel(&memory_pool->bo,
998 &memory_pool->mc_address,
999 &memory_pool->cpu_addr);
1001 memset(memory_pool, 0, sizeof(struct smu_table));
1006 static int smu_hw_init(void *handle)
1009 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1010 struct smu_context *smu = &adev->smu;
1012 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1013 ret = smu_check_fw_status(smu);
1015 pr_err("SMC firmware status is not correct\n");
1020 ret = smu_feature_init_dpm(smu);
1024 ret = smu_smc_table_hw_init(smu, true);
1028 ret = smu_alloc_memory_pool(smu);
1033 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1036 ret = smu_notify_memory_pool_location(smu);
1040 ret = smu_start_thermal_control(smu);
1044 ret = smu_register_irq_handler(smu);
1048 if (!smu->pm_enabled)
1049 adev->pm.dpm_enabled = false;
1051 adev->pm.dpm_enabled = true; /* TODO: will set dpm_enabled flag while VCN and DAL DPM is workable */
1053 pr_info("SMU is initialized successfully!\n");
1061 static int smu_hw_fini(void *handle)
1063 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1064 struct smu_context *smu = &adev->smu;
1065 struct smu_table_context *table_context = &smu->smu_table;
1068 kfree(table_context->driver_pptable);
1069 table_context->driver_pptable = NULL;
1071 kfree(table_context->max_sustainable_clocks);
1072 table_context->max_sustainable_clocks = NULL;
1074 kfree(table_context->overdrive_table);
1075 table_context->overdrive_table = NULL;
1077 kfree(smu->irq_source);
1078 smu->irq_source = NULL;
1080 ret = smu_fini_fb_allocations(smu);
1084 ret = smu_free_memory_pool(smu);
1091 int smu_reset(struct smu_context *smu)
1093 struct amdgpu_device *adev = smu->adev;
1096 ret = smu_hw_fini(adev);
1100 ret = smu_hw_init(adev);
1107 static int smu_suspend(void *handle)
1110 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1111 struct smu_context *smu = &adev->smu;
1112 bool baco_feature_is_enabled = smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT);
1114 ret = smu_system_features_control(smu, false);
1118 if (adev->in_gpu_reset && baco_feature_is_enabled) {
1119 ret = smu_feature_set_enabled(smu, SMU_FEATURE_BACO_BIT, true);
1121 pr_warn("set BACO feature enabled failed, return %d\n", ret);
1126 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1128 if (adev->asic_type >= CHIP_NAVI10 &&
1129 adev->gfx.rlc.funcs->stop)
1130 adev->gfx.rlc.funcs->stop(adev);
1135 static int smu_resume(void *handle)
1138 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1139 struct smu_context *smu = &adev->smu;
1141 pr_info("SMU is resuming...\n");
1143 mutex_lock(&smu->mutex);
1145 ret = smu_smc_table_hw_init(smu, false);
1149 ret = smu_start_thermal_control(smu);
1153 mutex_unlock(&smu->mutex);
1155 pr_info("SMU is resumed successfully!\n");
1159 mutex_unlock(&smu->mutex);
1163 int smu_display_configuration_change(struct smu_context *smu,
1164 const struct amd_pp_display_configuration *display_config)
1167 int num_of_active_display = 0;
1169 if (!smu->pm_enabled || !is_support_sw_smu(smu->adev))
1172 if (!display_config)
1175 mutex_lock(&smu->mutex);
1177 smu_set_deep_sleep_dcefclk(smu,
1178 display_config->min_dcef_deep_sleep_set_clk / 100);
1180 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1181 if (display_config->displays[index].controller_id != 0)
1182 num_of_active_display++;
1185 smu_set_active_display_count(smu, num_of_active_display);
1187 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1188 display_config->cpu_cc6_disable,
1189 display_config->cpu_pstate_disable,
1190 display_config->nb_pstate_switch_disable);
1192 mutex_unlock(&smu->mutex);
1197 static int smu_get_clock_info(struct smu_context *smu,
1198 struct smu_clock_info *clk_info,
1199 enum smu_perf_level_designation designation)
1202 struct smu_performance_level level = {0};
1207 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1211 clk_info->min_mem_clk = level.memory_clock;
1212 clk_info->min_eng_clk = level.core_clock;
1213 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1215 ret = smu_get_perf_level(smu, designation, &level);
1219 clk_info->min_mem_clk = level.memory_clock;
1220 clk_info->min_eng_clk = level.core_clock;
1221 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1226 int smu_get_current_clocks(struct smu_context *smu,
1227 struct amd_pp_clock_info *clocks)
1229 struct amd_pp_simple_clock_info simple_clocks = {0};
1230 struct smu_clock_info hw_clocks;
1233 if (!is_support_sw_smu(smu->adev))
1236 mutex_lock(&smu->mutex);
1238 smu_get_dal_power_level(smu, &simple_clocks);
1240 if (smu->support_power_containment)
1241 ret = smu_get_clock_info(smu, &hw_clocks,
1242 PERF_LEVEL_POWER_CONTAINMENT);
1244 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1247 pr_err("Error in smu_get_clock_info\n");
1251 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1252 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1253 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1254 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1255 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1256 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1257 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1258 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1260 if (simple_clocks.level == 0)
1261 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1263 clocks->max_clocks_state = simple_clocks.level;
1265 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1266 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1267 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1271 mutex_unlock(&smu->mutex);
1275 static int smu_set_clockgating_state(void *handle,
1276 enum amd_clockgating_state state)
1281 static int smu_set_powergating_state(void *handle,
1282 enum amd_powergating_state state)
1287 static int smu_enable_umd_pstate(void *handle,
1288 enum amd_dpm_forced_level *level)
1290 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1291 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1292 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1293 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1295 struct smu_context *smu = (struct smu_context*)(handle);
1296 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1297 if (!smu->pm_enabled || !smu_dpm_ctx->dpm_context)
1300 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1301 /* enter umd pstate, save current level, disable gfx cg*/
1302 if (*level & profile_mode_mask) {
1303 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1304 smu_dpm_ctx->enable_umd_pstate = true;
1305 amdgpu_device_ip_set_clockgating_state(smu->adev,
1306 AMD_IP_BLOCK_TYPE_GFX,
1307 AMD_CG_STATE_UNGATE);
1308 amdgpu_device_ip_set_powergating_state(smu->adev,
1309 AMD_IP_BLOCK_TYPE_GFX,
1310 AMD_PG_STATE_UNGATE);
1313 /* exit umd pstate, restore level, enable gfx cg*/
1314 if (!(*level & profile_mode_mask)) {
1315 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1316 *level = smu_dpm_ctx->saved_dpm_level;
1317 smu_dpm_ctx->enable_umd_pstate = false;
1318 amdgpu_device_ip_set_clockgating_state(smu->adev,
1319 AMD_IP_BLOCK_TYPE_GFX,
1321 amdgpu_device_ip_set_powergating_state(smu->adev,
1322 AMD_IP_BLOCK_TYPE_GFX,
1330 int smu_adjust_power_state_dynamic(struct smu_context *smu,
1331 enum amd_dpm_forced_level level,
1332 bool skip_display_settings)
1336 uint32_t sclk_mask, mclk_mask, soc_mask;
1338 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1340 if (!smu->pm_enabled)
1342 if (!skip_display_settings) {
1343 ret = smu_display_config_changed(smu);
1345 pr_err("Failed to change display config!");
1350 if (!smu->pm_enabled)
1352 ret = smu_apply_clocks_adjust_rules(smu);
1354 pr_err("Failed to apply clocks adjust rules!");
1358 if (!skip_display_settings) {
1359 ret = smu_notify_smc_dispaly_config(smu);
1361 pr_err("Failed to notify smc display config!");
1366 if (smu_dpm_ctx->dpm_level != level) {
1368 case AMD_DPM_FORCED_LEVEL_HIGH:
1369 ret = smu_force_dpm_limit_value(smu, true);
1371 case AMD_DPM_FORCED_LEVEL_LOW:
1372 ret = smu_force_dpm_limit_value(smu, false);
1375 case AMD_DPM_FORCED_LEVEL_AUTO:
1376 ret = smu_unforce_dpm_levels(smu);
1379 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
1380 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
1381 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
1382 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
1383 ret = smu_get_profiling_clk_mask(smu, level,
1389 smu_force_clk_levels(smu, SMU_SCLK, 1 << sclk_mask);
1390 smu_force_clk_levels(smu, SMU_MCLK, 1 << mclk_mask);
1393 case AMD_DPM_FORCED_LEVEL_MANUAL:
1394 case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
1400 smu_dpm_ctx->dpm_level = level;
1403 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1404 index = fls(smu->workload_mask);
1405 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1406 workload = smu->workload_setting[index];
1408 if (smu->power_profile_mode != workload)
1409 smu_set_power_profile_mode(smu, &workload, 0);
1415 int smu_handle_task(struct smu_context *smu,
1416 enum amd_dpm_forced_level level,
1417 enum amd_pp_task task_id)
1422 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1423 ret = smu_pre_display_config_changed(smu);
1426 ret = smu_set_cpu_power_state(smu);
1429 ret = smu_adjust_power_state_dynamic(smu, level, false);
1431 case AMD_PP_TASK_COMPLETE_INIT:
1432 case AMD_PP_TASK_READJUST_POWER_STATE:
1433 ret = smu_adjust_power_state_dynamic(smu, level, true);
1442 enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1444 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1446 if (!smu_dpm_ctx->dpm_context)
1449 mutex_lock(&(smu->mutex));
1450 if (smu_dpm_ctx->dpm_level != smu_dpm_ctx->saved_dpm_level) {
1451 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1453 mutex_unlock(&(smu->mutex));
1455 return smu_dpm_ctx->dpm_level;
1458 int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1462 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1464 if (!smu_dpm_ctx->dpm_context)
1467 for (i = 0; i < smu->adev->num_ip_blocks; i++) {
1468 if (smu->adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)
1473 smu->adev->ip_blocks[i].version->funcs->enable_umd_pstate(smu, &level);
1474 ret = smu_handle_task(smu, level,
1475 AMD_PP_TASK_READJUST_POWER_STATE);
1479 mutex_lock(&smu->mutex);
1480 smu_dpm_ctx->dpm_level = level;
1481 mutex_unlock(&smu->mutex);
1486 int smu_set_display_count(struct smu_context *smu, uint32_t count)
1490 mutex_lock(&smu->mutex);
1491 ret = smu_init_display_count(smu, count);
1492 mutex_unlock(&smu->mutex);
1497 const struct amd_ip_funcs smu_ip_funcs = {
1499 .early_init = smu_early_init,
1500 .late_init = smu_late_init,
1501 .sw_init = smu_sw_init,
1502 .sw_fini = smu_sw_fini,
1503 .hw_init = smu_hw_init,
1504 .hw_fini = smu_hw_fini,
1505 .suspend = smu_suspend,
1506 .resume = smu_resume,
1508 .check_soft_reset = NULL,
1509 .wait_for_idle = NULL,
1511 .set_clockgating_state = smu_set_clockgating_state,
1512 .set_powergating_state = smu_set_powergating_state,
1513 .enable_umd_pstate = smu_enable_umd_pstate,
1516 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
1518 .type = AMD_IP_BLOCK_TYPE_SMC,
1522 .funcs = &smu_ip_funcs,