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 if (!smu_clk_dpm_is_enabled(smu, clk_type))
75 clk_id = smu_clk_get_index(smu, clk_type);
80 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
81 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxByFreq,
88 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
89 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMinByFreq,
99 int smu_set_hard_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
100 uint32_t min, uint32_t max)
102 int ret = 0, clk_id = 0;
105 if (min <= 0 && max <= 0)
108 if (!smu_clk_dpm_is_enabled(smu, clk_type))
111 clk_id = smu_clk_get_index(smu, clk_type);
116 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
117 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMaxByFreq,
124 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
125 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
135 int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
136 uint32_t *min, uint32_t *max)
138 int ret = 0, clk_id = 0;
140 uint32_t clock_limit;
145 if (!smu_clk_dpm_is_enabled(smu, clk_type)) {
149 clock_limit = smu->smu_table.boot_values.uclk;
153 clock_limit = smu->smu_table.boot_values.gfxclk;
156 clock_limit = smu->smu_table.boot_values.socclk;
163 /* clock in Mhz unit */
165 *min = clock_limit / 100;
167 *max = clock_limit / 100;
172 mutex_lock(&smu->mutex);
173 clk_id = smu_clk_get_index(smu, clk_type);
179 param = (clk_id & 0xffff) << 16;
182 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMaxDpmFreq, param);
185 ret = smu_read_smc_arg(smu, max);
191 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMinDpmFreq, param);
194 ret = smu_read_smc_arg(smu, min);
200 mutex_unlock(&smu->mutex);
204 int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
205 uint16_t level, uint32_t *value)
207 int ret = 0, clk_id = 0;
213 if (!smu_clk_dpm_is_enabled(smu, clk_type))
216 clk_id = smu_clk_get_index(smu, clk_type);
220 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
222 ret = smu_send_smc_msg_with_param(smu,SMU_MSG_GetDpmFreqByIndex,
227 ret = smu_read_smc_arg(smu, ¶m);
231 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
232 * now, we un-support it */
233 *value = param & 0x7fffffff;
238 int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
241 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
244 bool smu_clk_dpm_is_enabled(struct smu_context *smu, enum smu_clk_type clk_type)
246 enum smu_feature_mask feature_id = 0;
251 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
255 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
258 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
264 if(!smu_feature_is_enabled(smu, feature_id)) {
265 pr_warn("smu %d clk dpm feature %d is not enabled\n", clk_type, feature_id);
273 int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
278 switch (block_type) {
279 case AMD_IP_BLOCK_TYPE_UVD:
280 ret = smu_dpm_set_uvd_enable(smu, gate);
282 case AMD_IP_BLOCK_TYPE_VCE:
283 ret = smu_dpm_set_vce_enable(smu, gate);
285 case AMD_IP_BLOCK_TYPE_GFX:
286 ret = smu_gfx_off_control(smu, gate);
295 enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
297 /* not support power state */
298 return POWER_STATE_TYPE_DEFAULT;
301 int smu_get_power_num_states(struct smu_context *smu,
302 struct pp_states_info *state_info)
307 /* not support power state */
308 memset(state_info, 0, sizeof(struct pp_states_info));
309 state_info->nums = 1;
310 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
315 int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
316 void *data, uint32_t *size)
318 struct smu_power_context *smu_power = &smu->smu_power;
319 struct smu_power_gate *power_gate = &smu_power->power_gate;
323 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
324 *((uint32_t *)data) = smu->pstate_sclk;
327 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
328 *((uint32_t *)data) = smu->pstate_mclk;
331 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
332 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
335 case AMDGPU_PP_SENSOR_UVD_POWER:
336 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
339 case AMDGPU_PP_SENSOR_VCE_POWER:
340 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
343 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
344 *(uint32_t *)data = power_gate->vcn_gated ? 0 : 1;
358 int smu_update_table(struct smu_context *smu, enum smu_table_id table_index, int argument,
359 void *table_data, bool drv2smu)
361 struct smu_table_context *smu_table = &smu->smu_table;
362 struct smu_table *table = NULL;
364 int table_id = smu_table_get_index(smu, table_index);
366 if (!table_data || table_id >= smu_table->table_count)
369 table = &smu_table->tables[table_index];
372 memcpy(table->cpu_addr, table_data, table->size);
374 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrHigh,
375 upper_32_bits(table->mc_address));
378 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrLow,
379 lower_32_bits(table->mc_address));
382 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
383 SMU_MSG_TransferTableDram2Smu :
384 SMU_MSG_TransferTableSmu2Dram,
385 table_id | ((argument & 0xFFFF) << 16));
390 memcpy(table_data, table->cpu_addr, table->size);
395 bool is_support_sw_smu(struct amdgpu_device *adev)
397 if (adev->asic_type == CHIP_VEGA20)
398 return (amdgpu_dpm == 2) ? true : false;
399 else if (adev->asic_type >= CHIP_NAVI10)
405 int smu_sys_get_pp_table(struct smu_context *smu, void **table)
407 struct smu_table_context *smu_table = &smu->smu_table;
409 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
412 if (smu_table->hardcode_pptable)
413 *table = smu_table->hardcode_pptable;
415 *table = smu_table->power_play_table;
417 return smu_table->power_play_table_size;
420 int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
422 struct smu_table_context *smu_table = &smu->smu_table;
423 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
426 if (!smu->pm_enabled)
428 if (header->usStructureSize != size) {
429 pr_err("pp table size not matched !\n");
433 mutex_lock(&smu->mutex);
434 if (!smu_table->hardcode_pptable)
435 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
436 if (!smu_table->hardcode_pptable) {
441 memcpy(smu_table->hardcode_pptable, buf, size);
442 smu_table->power_play_table = smu_table->hardcode_pptable;
443 smu_table->power_play_table_size = size;
444 mutex_unlock(&smu->mutex);
446 ret = smu_reset(smu);
448 pr_info("smu reset failed, ret = %d\n", ret);
453 mutex_unlock(&smu->mutex);
457 int smu_feature_init_dpm(struct smu_context *smu)
459 struct smu_feature *feature = &smu->smu_feature;
461 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
463 if (!smu->pm_enabled)
465 mutex_lock(&feature->mutex);
466 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
467 mutex_unlock(&feature->mutex);
469 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
474 mutex_lock(&feature->mutex);
475 bitmap_or(feature->allowed, feature->allowed,
476 (unsigned long *)allowed_feature_mask,
477 feature->feature_num);
478 mutex_unlock(&feature->mutex);
483 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
485 struct smu_feature *feature = &smu->smu_feature;
489 feature_id = smu_feature_get_index(smu, mask);
491 WARN_ON(feature_id > feature->feature_num);
493 mutex_lock(&feature->mutex);
494 ret = test_bit(feature_id, feature->enabled);
495 mutex_unlock(&feature->mutex);
500 int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
503 struct smu_feature *feature = &smu->smu_feature;
507 feature_id = smu_feature_get_index(smu, mask);
509 WARN_ON(feature_id > feature->feature_num);
511 mutex_lock(&feature->mutex);
512 ret = smu_feature_update_enable_state(smu, feature_id, enable);
517 test_and_set_bit(feature_id, feature->enabled);
519 test_and_clear_bit(feature_id, feature->enabled);
522 mutex_unlock(&feature->mutex);
527 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
529 struct smu_feature *feature = &smu->smu_feature;
533 feature_id = smu_feature_get_index(smu, mask);
535 WARN_ON(feature_id > feature->feature_num);
537 mutex_lock(&feature->mutex);
538 ret = test_bit(feature_id, feature->supported);
539 mutex_unlock(&feature->mutex);
544 int smu_feature_set_supported(struct smu_context *smu,
545 enum smu_feature_mask mask,
548 struct smu_feature *feature = &smu->smu_feature;
552 feature_id = smu_feature_get_index(smu, mask);
554 WARN_ON(feature_id > feature->feature_num);
556 mutex_lock(&feature->mutex);
558 test_and_set_bit(feature_id, feature->supported);
560 test_and_clear_bit(feature_id, feature->supported);
561 mutex_unlock(&feature->mutex);
566 static int smu_set_funcs(struct amdgpu_device *adev)
568 struct smu_context *smu = &adev->smu;
570 switch (adev->asic_type) {
573 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
574 smu->od_enabled = true;
575 smu_v11_0_set_smu_funcs(smu);
584 static int smu_early_init(void *handle)
586 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
587 struct smu_context *smu = &adev->smu;
590 smu->pm_enabled = !!amdgpu_dpm;
591 mutex_init(&smu->mutex);
593 return smu_set_funcs(adev);
596 static int smu_late_init(void *handle)
598 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
599 struct smu_context *smu = &adev->smu;
601 if (!smu->pm_enabled)
603 mutex_lock(&smu->mutex);
604 smu_handle_task(&adev->smu,
605 smu->smu_dpm.dpm_level,
606 AMD_PP_TASK_COMPLETE_INIT);
607 mutex_unlock(&smu->mutex);
612 int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
613 uint16_t *size, uint8_t *frev, uint8_t *crev,
616 struct amdgpu_device *adev = smu->adev;
619 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
620 size, frev, crev, &data_start))
623 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
628 static int smu_initialize_pptable(struct smu_context *smu)
634 static int smu_smc_table_sw_init(struct smu_context *smu)
638 ret = smu_initialize_pptable(smu);
640 pr_err("Failed to init smu_initialize_pptable!\n");
645 * Create smu_table structure, and init smc tables such as
646 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
648 ret = smu_init_smc_tables(smu);
650 pr_err("Failed to init smc tables!\n");
655 * Create smu_power_context structure, and allocate smu_dpm_context and
656 * context size to fill the smu_power_context data.
658 ret = smu_init_power(smu);
660 pr_err("Failed to init smu_init_power!\n");
667 static int smu_smc_table_sw_fini(struct smu_context *smu)
671 ret = smu_fini_smc_tables(smu);
673 pr_err("Failed to smu_fini_smc_tables!\n");
680 static int smu_sw_init(void *handle)
682 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
683 struct smu_context *smu = &adev->smu;
686 smu->pool_size = adev->pm.smu_prv_buffer_size;
687 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
688 mutex_init(&smu->smu_feature.mutex);
689 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
690 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
691 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
693 mutex_init(&smu->smu_baco.mutex);
694 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
695 smu->smu_baco.platform_support = false;
697 smu->watermarks_bitmap = 0;
698 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
699 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
701 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
702 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
703 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
704 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
705 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
706 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
707 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
708 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
710 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
711 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
712 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
713 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
714 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
715 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
716 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
717 smu->display_config = &adev->pm.pm_display_cfg;
719 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
720 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
721 ret = smu_init_microcode(smu);
723 pr_err("Failed to load smu firmware!\n");
727 ret = smu_smc_table_sw_init(smu);
729 pr_err("Failed to sw init smc table!\n");
733 ret = smu_register_irq_handler(smu);
735 pr_err("Failed to register smc irq handler!\n");
742 static int smu_sw_fini(void *handle)
744 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
745 struct smu_context *smu = &adev->smu;
748 kfree(smu->irq_source);
749 smu->irq_source = NULL;
751 ret = smu_smc_table_sw_fini(smu);
753 pr_err("Failed to sw fini smc table!\n");
757 ret = smu_fini_power(smu);
759 pr_err("Failed to init smu_fini_power!\n");
766 static int smu_init_fb_allocations(struct smu_context *smu)
768 struct amdgpu_device *adev = smu->adev;
769 struct smu_table_context *smu_table = &smu->smu_table;
770 struct smu_table *tables = smu_table->tables;
771 uint32_t table_count = smu_table->table_count;
775 if (table_count <= 0)
778 for (i = 0 ; i < table_count; i++) {
779 if (tables[i].size == 0)
781 ret = amdgpu_bo_create_kernel(adev,
786 &tables[i].mc_address,
787 &tables[i].cpu_addr);
795 if (tables[i].size == 0)
797 amdgpu_bo_free_kernel(&tables[i].bo,
798 &tables[i].mc_address,
799 &tables[i].cpu_addr);
805 static int smu_fini_fb_allocations(struct smu_context *smu)
807 struct smu_table_context *smu_table = &smu->smu_table;
808 struct smu_table *tables = smu_table->tables;
809 uint32_t table_count = smu_table->table_count;
812 if (table_count == 0 || tables == NULL)
815 for (i = 0 ; i < table_count; i++) {
816 if (tables[i].size == 0)
818 amdgpu_bo_free_kernel(&tables[i].bo,
819 &tables[i].mc_address,
820 &tables[i].cpu_addr);
826 static int smu_override_pcie_parameters(struct smu_context *smu)
828 struct amdgpu_device *adev = smu->adev;
829 uint32_t pcie_gen = 0, pcie_width = 0, smu_pcie_arg;
832 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
834 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
836 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
838 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
841 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
842 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
843 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
845 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
847 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
849 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
851 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
853 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
855 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
858 smu_pcie_arg = (1 << 16) | (pcie_gen << 8) | pcie_width;
859 ret = smu_send_smc_msg_with_param(smu,
860 SMU_MSG_OverridePcieParameters,
863 pr_err("[%s] Attempt to override pcie params failed!\n", __func__);
867 static int smu_smc_table_hw_init(struct smu_context *smu,
870 struct amdgpu_device *adev = smu->adev;
873 if (smu_is_dpm_running(smu) && adev->in_suspend) {
874 pr_info("dpm has been enabled\n");
878 ret = smu_init_display_count(smu, 0);
883 /* get boot_values from vbios to set revision, gfxclk, and etc. */
884 ret = smu_get_vbios_bootup_values(smu);
888 ret = smu_setup_pptable(smu);
892 ret = smu_get_clk_info_from_vbios(smu);
897 * check if the format_revision in vbios is up to pptable header
898 * version, and the structure size is not 0.
900 ret = smu_check_pptable(smu);
905 * allocate vram bos to store smc table contents.
907 ret = smu_init_fb_allocations(smu);
912 * Parse pptable format and fill PPTable_t smc_pptable to
913 * smu_table_context structure. And read the smc_dpm_table from vbios,
914 * then fill it into smc_pptable.
916 ret = smu_parse_pptable(smu);
921 * Send msg GetDriverIfVersion to check if the return value is equal
922 * with DRIVER_IF_VERSION of smc header.
924 ret = smu_check_fw_version(smu);
930 * Copy pptable bo in the vram to smc with SMU MSGs such as
931 * SetDriverDramAddr and TransferTableDram2Smu.
933 ret = smu_write_pptable(smu);
937 /* issue RunAfllBtc msg */
938 ret = smu_run_afll_btc(smu);
942 ret = smu_feature_set_allowed_mask(smu);
946 ret = smu_system_features_control(smu, true);
950 ret = smu_override_pcie_parameters(smu);
954 ret = smu_notify_display_change(smu);
959 * Set min deep sleep dce fclk with bootup value from vbios via
960 * SetMinDeepSleepDcefclk MSG.
962 ret = smu_set_min_dcef_deep_sleep(smu);
967 * Set initialized values (get from vbios) to dpm tables context such as
968 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
972 ret = smu_populate_smc_pptable(smu);
976 ret = smu_init_max_sustainable_clocks(smu);
981 ret = smu_set_default_od_settings(smu, initialize);
986 ret = smu_populate_umd_state_clk(smu);
990 ret = smu_get_power_limit(smu, &smu->default_power_limit, false);
996 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
998 ret = smu_set_tool_table_location(smu);
1000 if (!smu_is_dpm_running(smu))
1001 pr_info("dpm has been disabled\n");
1007 * smu_alloc_memory_pool - allocate memory pool in the system memory
1009 * @smu: amdgpu_device pointer
1011 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
1012 * and DramLogSetDramAddr can notify it changed.
1014 * Returns 0 on success, error on failure.
1016 static int smu_alloc_memory_pool(struct smu_context *smu)
1018 struct amdgpu_device *adev = smu->adev;
1019 struct smu_table_context *smu_table = &smu->smu_table;
1020 struct smu_table *memory_pool = &smu_table->memory_pool;
1021 uint64_t pool_size = smu->pool_size;
1024 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
1027 memory_pool->size = pool_size;
1028 memory_pool->align = PAGE_SIZE;
1029 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
1031 switch (pool_size) {
1032 case SMU_MEMORY_POOL_SIZE_256_MB:
1033 case SMU_MEMORY_POOL_SIZE_512_MB:
1034 case SMU_MEMORY_POOL_SIZE_1_GB:
1035 case SMU_MEMORY_POOL_SIZE_2_GB:
1036 ret = amdgpu_bo_create_kernel(adev,
1039 memory_pool->domain,
1041 &memory_pool->mc_address,
1042 &memory_pool->cpu_addr);
1051 static int smu_free_memory_pool(struct smu_context *smu)
1053 struct smu_table_context *smu_table = &smu->smu_table;
1054 struct smu_table *memory_pool = &smu_table->memory_pool;
1057 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1060 amdgpu_bo_free_kernel(&memory_pool->bo,
1061 &memory_pool->mc_address,
1062 &memory_pool->cpu_addr);
1064 memset(memory_pool, 0, sizeof(struct smu_table));
1069 static int smu_hw_init(void *handle)
1072 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1073 struct smu_context *smu = &adev->smu;
1075 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1076 ret = smu_check_fw_status(smu);
1078 pr_err("SMC firmware status is not correct\n");
1083 ret = smu_feature_init_dpm(smu);
1087 ret = smu_smc_table_hw_init(smu, true);
1091 ret = smu_alloc_memory_pool(smu);
1096 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1099 ret = smu_notify_memory_pool_location(smu);
1103 ret = smu_start_thermal_control(smu);
1107 if (!smu->pm_enabled)
1108 adev->pm.dpm_enabled = false;
1110 adev->pm.dpm_enabled = true; /* TODO: will set dpm_enabled flag while VCN and DAL DPM is workable */
1112 pr_info("SMU is initialized successfully!\n");
1120 static int smu_hw_fini(void *handle)
1122 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1123 struct smu_context *smu = &adev->smu;
1124 struct smu_table_context *table_context = &smu->smu_table;
1127 kfree(table_context->driver_pptable);
1128 table_context->driver_pptable = NULL;
1130 kfree(table_context->max_sustainable_clocks);
1131 table_context->max_sustainable_clocks = NULL;
1133 kfree(table_context->overdrive_table);
1134 table_context->overdrive_table = NULL;
1136 ret = smu_fini_fb_allocations(smu);
1140 ret = smu_free_memory_pool(smu);
1147 int smu_reset(struct smu_context *smu)
1149 struct amdgpu_device *adev = smu->adev;
1152 ret = smu_hw_fini(adev);
1156 ret = smu_hw_init(adev);
1163 static int smu_suspend(void *handle)
1166 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1167 struct smu_context *smu = &adev->smu;
1168 bool baco_feature_is_enabled = smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT);
1170 ret = smu_system_features_control(smu, false);
1174 if (adev->in_gpu_reset && baco_feature_is_enabled) {
1175 ret = smu_feature_set_enabled(smu, SMU_FEATURE_BACO_BIT, true);
1177 pr_warn("set BACO feature enabled failed, return %d\n", ret);
1182 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1184 if (adev->asic_type >= CHIP_NAVI10 &&
1185 adev->gfx.rlc.funcs->stop)
1186 adev->gfx.rlc.funcs->stop(adev);
1191 static int smu_resume(void *handle)
1194 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1195 struct smu_context *smu = &adev->smu;
1197 pr_info("SMU is resuming...\n");
1199 mutex_lock(&smu->mutex);
1201 ret = smu_smc_table_hw_init(smu, false);
1205 ret = smu_start_thermal_control(smu);
1209 mutex_unlock(&smu->mutex);
1211 pr_info("SMU is resumed successfully!\n");
1215 mutex_unlock(&smu->mutex);
1219 int smu_display_configuration_change(struct smu_context *smu,
1220 const struct amd_pp_display_configuration *display_config)
1223 int num_of_active_display = 0;
1225 if (!smu->pm_enabled || !is_support_sw_smu(smu->adev))
1228 if (!display_config)
1231 mutex_lock(&smu->mutex);
1233 smu_set_deep_sleep_dcefclk(smu,
1234 display_config->min_dcef_deep_sleep_set_clk / 100);
1236 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1237 if (display_config->displays[index].controller_id != 0)
1238 num_of_active_display++;
1241 smu_set_active_display_count(smu, num_of_active_display);
1243 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1244 display_config->cpu_cc6_disable,
1245 display_config->cpu_pstate_disable,
1246 display_config->nb_pstate_switch_disable);
1248 mutex_unlock(&smu->mutex);
1253 static int smu_get_clock_info(struct smu_context *smu,
1254 struct smu_clock_info *clk_info,
1255 enum smu_perf_level_designation designation)
1258 struct smu_performance_level level = {0};
1263 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1267 clk_info->min_mem_clk = level.memory_clock;
1268 clk_info->min_eng_clk = level.core_clock;
1269 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1271 ret = smu_get_perf_level(smu, designation, &level);
1275 clk_info->min_mem_clk = level.memory_clock;
1276 clk_info->min_eng_clk = level.core_clock;
1277 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1282 int smu_get_current_clocks(struct smu_context *smu,
1283 struct amd_pp_clock_info *clocks)
1285 struct amd_pp_simple_clock_info simple_clocks = {0};
1286 struct smu_clock_info hw_clocks;
1289 if (!is_support_sw_smu(smu->adev))
1292 mutex_lock(&smu->mutex);
1294 smu_get_dal_power_level(smu, &simple_clocks);
1296 if (smu->support_power_containment)
1297 ret = smu_get_clock_info(smu, &hw_clocks,
1298 PERF_LEVEL_POWER_CONTAINMENT);
1300 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1303 pr_err("Error in smu_get_clock_info\n");
1307 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1308 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1309 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1310 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1311 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1312 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1313 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1314 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1316 if (simple_clocks.level == 0)
1317 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1319 clocks->max_clocks_state = simple_clocks.level;
1321 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1322 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1323 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1327 mutex_unlock(&smu->mutex);
1331 static int smu_set_clockgating_state(void *handle,
1332 enum amd_clockgating_state state)
1337 static int smu_set_powergating_state(void *handle,
1338 enum amd_powergating_state state)
1343 static int smu_enable_umd_pstate(void *handle,
1344 enum amd_dpm_forced_level *level)
1346 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1347 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1348 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1349 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1351 struct smu_context *smu = (struct smu_context*)(handle);
1352 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1353 if (!smu->pm_enabled || !smu_dpm_ctx->dpm_context)
1356 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1357 /* enter umd pstate, save current level, disable gfx cg*/
1358 if (*level & profile_mode_mask) {
1359 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1360 smu_dpm_ctx->enable_umd_pstate = true;
1361 amdgpu_device_ip_set_clockgating_state(smu->adev,
1362 AMD_IP_BLOCK_TYPE_GFX,
1363 AMD_CG_STATE_UNGATE);
1364 amdgpu_device_ip_set_powergating_state(smu->adev,
1365 AMD_IP_BLOCK_TYPE_GFX,
1366 AMD_PG_STATE_UNGATE);
1369 /* exit umd pstate, restore level, enable gfx cg*/
1370 if (!(*level & profile_mode_mask)) {
1371 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1372 *level = smu_dpm_ctx->saved_dpm_level;
1373 smu_dpm_ctx->enable_umd_pstate = false;
1374 amdgpu_device_ip_set_clockgating_state(smu->adev,
1375 AMD_IP_BLOCK_TYPE_GFX,
1377 amdgpu_device_ip_set_powergating_state(smu->adev,
1378 AMD_IP_BLOCK_TYPE_GFX,
1386 static int smu_default_set_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1389 uint32_t sclk_mask, mclk_mask, soc_mask;
1392 case AMD_DPM_FORCED_LEVEL_HIGH:
1393 ret = smu_force_dpm_limit_value(smu, true);
1395 case AMD_DPM_FORCED_LEVEL_LOW:
1396 ret = smu_force_dpm_limit_value(smu, false);
1398 case AMD_DPM_FORCED_LEVEL_AUTO:
1399 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
1400 ret = smu_unforce_dpm_levels(smu);
1402 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
1403 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
1404 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
1405 ret = smu_get_profiling_clk_mask(smu, level,
1411 smu_force_clk_levels(smu, SMU_SCLK, 1 << sclk_mask);
1412 smu_force_clk_levels(smu, SMU_MCLK, 1 << mclk_mask);
1413 smu_force_clk_levels(smu, SMU_SOCCLK, 1 << soc_mask);
1415 case AMD_DPM_FORCED_LEVEL_MANUAL:
1416 case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
1423 int smu_adjust_power_state_dynamic(struct smu_context *smu,
1424 enum amd_dpm_forced_level level,
1425 bool skip_display_settings)
1430 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1432 if (!smu->pm_enabled)
1434 if (!skip_display_settings) {
1435 ret = smu_display_config_changed(smu);
1437 pr_err("Failed to change display config!");
1442 if (!smu->pm_enabled)
1444 ret = smu_apply_clocks_adjust_rules(smu);
1446 pr_err("Failed to apply clocks adjust rules!");
1450 if (!skip_display_settings) {
1451 ret = smu_notify_smc_dispaly_config(smu);
1453 pr_err("Failed to notify smc display config!");
1458 if (smu_dpm_ctx->dpm_level != level) {
1459 ret = smu_asic_set_performance_level(smu, level);
1461 ret = smu_default_set_performance_level(smu, level);
1464 smu_dpm_ctx->dpm_level = level;
1467 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1468 index = fls(smu->workload_mask);
1469 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1470 workload = smu->workload_setting[index];
1472 if (smu->power_profile_mode != workload)
1473 smu_set_power_profile_mode(smu, &workload, 0);
1479 int smu_handle_task(struct smu_context *smu,
1480 enum amd_dpm_forced_level level,
1481 enum amd_pp_task task_id)
1486 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1487 ret = smu_pre_display_config_changed(smu);
1490 ret = smu_set_cpu_power_state(smu);
1493 ret = smu_adjust_power_state_dynamic(smu, level, false);
1495 case AMD_PP_TASK_COMPLETE_INIT:
1496 case AMD_PP_TASK_READJUST_POWER_STATE:
1497 ret = smu_adjust_power_state_dynamic(smu, level, true);
1506 enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1508 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1509 enum amd_dpm_forced_level level;
1511 if (!smu_dpm_ctx->dpm_context)
1514 mutex_lock(&(smu->mutex));
1515 level = smu_dpm_ctx->dpm_level;
1516 mutex_unlock(&(smu->mutex));
1521 int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1525 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1527 if (!smu_dpm_ctx->dpm_context)
1530 for (i = 0; i < smu->adev->num_ip_blocks; i++) {
1531 if (smu->adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)
1536 smu->adev->ip_blocks[i].version->funcs->enable_umd_pstate(smu, &level);
1537 ret = smu_handle_task(smu, level,
1538 AMD_PP_TASK_READJUST_POWER_STATE);
1542 mutex_lock(&smu->mutex);
1543 smu_dpm_ctx->dpm_level = level;
1544 mutex_unlock(&smu->mutex);
1549 int smu_set_display_count(struct smu_context *smu, uint32_t count)
1553 mutex_lock(&smu->mutex);
1554 ret = smu_init_display_count(smu, count);
1555 mutex_unlock(&smu->mutex);
1560 const struct amd_ip_funcs smu_ip_funcs = {
1562 .early_init = smu_early_init,
1563 .late_init = smu_late_init,
1564 .sw_init = smu_sw_init,
1565 .sw_fini = smu_sw_fini,
1566 .hw_init = smu_hw_init,
1567 .hw_fini = smu_hw_fini,
1568 .suspend = smu_suspend,
1569 .resume = smu_resume,
1571 .check_soft_reset = NULL,
1572 .wait_for_idle = NULL,
1574 .set_clockgating_state = smu_set_clockgating_state,
1575 .set_powergating_state = smu_set_powergating_state,
1576 .enable_umd_pstate = smu_enable_umd_pstate,
1579 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
1581 .type = AMD_IP_BLOCK_TYPE_SMC,
1585 .funcs = &smu_ip_funcs,