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;
144 if (!smu_clk_dpm_is_enabled(smu, clk_type))
147 mutex_lock(&smu->mutex);
148 clk_id = smu_clk_get_index(smu, clk_type);
154 param = (clk_id & 0xffff) << 16;
157 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMaxDpmFreq, param);
160 ret = smu_read_smc_arg(smu, max);
166 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_GetMinDpmFreq, param);
169 ret = smu_read_smc_arg(smu, min);
175 mutex_unlock(&smu->mutex);
179 int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
180 uint16_t level, uint32_t *value)
182 int ret = 0, clk_id = 0;
188 if (!smu_clk_dpm_is_enabled(smu, clk_type))
191 clk_id = smu_clk_get_index(smu, clk_type);
195 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
197 ret = smu_send_smc_msg_with_param(smu,SMU_MSG_GetDpmFreqByIndex,
202 ret = smu_read_smc_arg(smu, ¶m);
206 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
207 * now, we un-support it */
208 *value = param & 0x7fffffff;
213 int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
216 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
219 bool smu_clk_dpm_is_enabled(struct smu_context *smu, enum smu_clk_type clk_type)
221 enum smu_feature_mask feature_id = 0;
226 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
230 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
233 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
239 if(!smu_feature_is_enabled(smu, feature_id)) {
240 pr_warn("smu %d clk dpm feature %d is not enabled\n", clk_type, feature_id);
248 int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
253 switch (block_type) {
254 case AMD_IP_BLOCK_TYPE_UVD:
255 ret = smu_dpm_set_uvd_enable(smu, gate);
257 case AMD_IP_BLOCK_TYPE_VCE:
258 ret = smu_dpm_set_vce_enable(smu, gate);
260 case AMD_IP_BLOCK_TYPE_GFX:
261 ret = smu_gfx_off_control(smu, gate);
270 enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
272 /* not support power state */
273 return POWER_STATE_TYPE_DEFAULT;
276 int smu_get_power_num_states(struct smu_context *smu,
277 struct pp_states_info *state_info)
282 /* not support power state */
283 memset(state_info, 0, sizeof(struct pp_states_info));
284 state_info->nums = 0;
289 int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
290 void *data, uint32_t *size)
295 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
296 *((uint32_t *)data) = smu->pstate_sclk;
299 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
300 *((uint32_t *)data) = smu->pstate_mclk;
303 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
304 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
307 case AMDGPU_PP_SENSOR_UVD_POWER:
308 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
311 case AMDGPU_PP_SENSOR_VCE_POWER:
312 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
326 int smu_update_table(struct smu_context *smu, enum smu_table_id table_index, int argument,
327 void *table_data, bool drv2smu)
329 struct smu_table_context *smu_table = &smu->smu_table;
330 struct smu_table *table = NULL;
332 int table_id = smu_table_get_index(smu, table_index);
334 if (!table_data || table_id >= smu_table->table_count)
337 table = &smu_table->tables[table_index];
340 memcpy(table->cpu_addr, table_data, table->size);
342 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrHigh,
343 upper_32_bits(table->mc_address));
346 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrLow,
347 lower_32_bits(table->mc_address));
350 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
351 SMU_MSG_TransferTableDram2Smu :
352 SMU_MSG_TransferTableSmu2Dram,
353 table_id | ((argument & 0xFFFF) << 16));
358 memcpy(table_data, table->cpu_addr, table->size);
363 bool is_support_sw_smu(struct amdgpu_device *adev)
365 if (adev->asic_type == CHIP_VEGA20)
366 return (amdgpu_dpm == 2) ? true : false;
367 else if (adev->asic_type >= CHIP_NAVI10)
373 int smu_sys_get_pp_table(struct smu_context *smu, void **table)
375 struct smu_table_context *smu_table = &smu->smu_table;
377 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
380 if (smu_table->hardcode_pptable)
381 *table = smu_table->hardcode_pptable;
383 *table = smu_table->power_play_table;
385 return smu_table->power_play_table_size;
388 int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
390 struct smu_table_context *smu_table = &smu->smu_table;
391 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
394 if (!smu->pm_enabled)
396 if (header->usStructureSize != size) {
397 pr_err("pp table size not matched !\n");
401 mutex_lock(&smu->mutex);
402 if (!smu_table->hardcode_pptable)
403 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
404 if (!smu_table->hardcode_pptable) {
409 memcpy(smu_table->hardcode_pptable, buf, size);
410 smu_table->power_play_table = smu_table->hardcode_pptable;
411 smu_table->power_play_table_size = size;
412 mutex_unlock(&smu->mutex);
414 ret = smu_reset(smu);
416 pr_info("smu reset failed, ret = %d\n", ret);
421 mutex_unlock(&smu->mutex);
425 int smu_feature_init_dpm(struct smu_context *smu)
427 struct smu_feature *feature = &smu->smu_feature;
429 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
431 if (!smu->pm_enabled)
433 mutex_lock(&feature->mutex);
434 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
435 mutex_unlock(&feature->mutex);
437 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
442 mutex_lock(&feature->mutex);
443 bitmap_or(feature->allowed, feature->allowed,
444 (unsigned long *)allowed_feature_mask,
445 feature->feature_num);
446 mutex_unlock(&feature->mutex);
451 int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
453 struct smu_feature *feature = &smu->smu_feature;
457 feature_id = smu_feature_get_index(smu, mask);
459 WARN_ON(feature_id > feature->feature_num);
461 mutex_lock(&feature->mutex);
462 ret = test_bit(feature_id, feature->enabled);
463 mutex_unlock(&feature->mutex);
468 int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
471 struct smu_feature *feature = &smu->smu_feature;
475 feature_id = smu_feature_get_index(smu, mask);
477 WARN_ON(feature_id > feature->feature_num);
479 mutex_lock(&feature->mutex);
480 ret = smu_feature_update_enable_state(smu, feature_id, enable);
485 test_and_set_bit(feature_id, feature->enabled);
487 test_and_clear_bit(feature_id, feature->enabled);
490 mutex_unlock(&feature->mutex);
495 int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
497 struct smu_feature *feature = &smu->smu_feature;
501 feature_id = smu_feature_get_index(smu, mask);
503 WARN_ON(feature_id > feature->feature_num);
505 mutex_lock(&feature->mutex);
506 ret = test_bit(feature_id, feature->supported);
507 mutex_unlock(&feature->mutex);
512 int smu_feature_set_supported(struct smu_context *smu,
513 enum smu_feature_mask mask,
516 struct smu_feature *feature = &smu->smu_feature;
520 feature_id = smu_feature_get_index(smu, mask);
522 WARN_ON(feature_id > feature->feature_num);
524 mutex_lock(&feature->mutex);
526 test_and_set_bit(feature_id, feature->supported);
528 test_and_clear_bit(feature_id, feature->supported);
529 mutex_unlock(&feature->mutex);
534 static int smu_set_funcs(struct amdgpu_device *adev)
536 struct smu_context *smu = &adev->smu;
538 switch (adev->asic_type) {
541 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
542 smu->od_enabled = true;
543 smu_v11_0_set_smu_funcs(smu);
552 static int smu_early_init(void *handle)
554 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
555 struct smu_context *smu = &adev->smu;
558 smu->pm_enabled = !!amdgpu_dpm;
559 mutex_init(&smu->mutex);
561 return smu_set_funcs(adev);
564 static int smu_late_init(void *handle)
566 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
567 struct smu_context *smu = &adev->smu;
569 if (!smu->pm_enabled)
571 mutex_lock(&smu->mutex);
572 smu_handle_task(&adev->smu,
573 smu->smu_dpm.dpm_level,
574 AMD_PP_TASK_COMPLETE_INIT);
575 mutex_unlock(&smu->mutex);
580 int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
581 uint16_t *size, uint8_t *frev, uint8_t *crev,
584 struct amdgpu_device *adev = smu->adev;
587 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
588 size, frev, crev, &data_start))
591 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
596 static int smu_initialize_pptable(struct smu_context *smu)
602 static int smu_smc_table_sw_init(struct smu_context *smu)
606 ret = smu_initialize_pptable(smu);
608 pr_err("Failed to init smu_initialize_pptable!\n");
613 * Create smu_table structure, and init smc tables such as
614 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
616 ret = smu_init_smc_tables(smu);
618 pr_err("Failed to init smc tables!\n");
623 * Create smu_power_context structure, and allocate smu_dpm_context and
624 * context size to fill the smu_power_context data.
626 ret = smu_init_power(smu);
628 pr_err("Failed to init smu_init_power!\n");
635 static int smu_smc_table_sw_fini(struct smu_context *smu)
639 ret = smu_fini_smc_tables(smu);
641 pr_err("Failed to smu_fini_smc_tables!\n");
648 static int smu_sw_init(void *handle)
650 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
651 struct smu_context *smu = &adev->smu;
654 smu->pool_size = adev->pm.smu_prv_buffer_size;
655 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
656 mutex_init(&smu->smu_feature.mutex);
657 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
658 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
659 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
661 mutex_init(&smu->smu_baco.mutex);
662 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
663 smu->smu_baco.platform_support = false;
665 smu->watermarks_bitmap = 0;
666 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
667 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
669 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
670 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
671 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
672 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
673 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
674 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
675 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
676 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
678 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
679 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
680 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
681 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
682 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
683 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
684 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
685 smu->display_config = &adev->pm.pm_display_cfg;
687 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
688 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
689 ret = smu_init_microcode(smu);
691 pr_err("Failed to load smu firmware!\n");
695 ret = smu_smc_table_sw_init(smu);
697 pr_err("Failed to sw init smc table!\n");
704 static int smu_sw_fini(void *handle)
706 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
707 struct smu_context *smu = &adev->smu;
710 ret = smu_smc_table_sw_fini(smu);
712 pr_err("Failed to sw fini smc table!\n");
716 ret = smu_fini_power(smu);
718 pr_err("Failed to init smu_fini_power!\n");
725 static int smu_init_fb_allocations(struct smu_context *smu)
727 struct amdgpu_device *adev = smu->adev;
728 struct smu_table_context *smu_table = &smu->smu_table;
729 struct smu_table *tables = smu_table->tables;
730 uint32_t table_count = smu_table->table_count;
734 if (table_count <= 0)
737 for (i = 0 ; i < table_count; i++) {
738 if (tables[i].size == 0)
740 ret = amdgpu_bo_create_kernel(adev,
745 &tables[i].mc_address,
746 &tables[i].cpu_addr);
754 if (tables[i].size == 0)
756 amdgpu_bo_free_kernel(&tables[i].bo,
757 &tables[i].mc_address,
758 &tables[i].cpu_addr);
764 static int smu_fini_fb_allocations(struct smu_context *smu)
766 struct smu_table_context *smu_table = &smu->smu_table;
767 struct smu_table *tables = smu_table->tables;
768 uint32_t table_count = smu_table->table_count;
771 if (table_count == 0 || tables == NULL)
774 for (i = 0 ; i < table_count; i++) {
775 if (tables[i].size == 0)
777 amdgpu_bo_free_kernel(&tables[i].bo,
778 &tables[i].mc_address,
779 &tables[i].cpu_addr);
785 static int smu_override_pcie_parameters(struct smu_context *smu)
787 struct amdgpu_device *adev = smu->adev;
788 uint32_t pcie_gen = 0, pcie_width = 0, smu_pcie_arg;
791 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
793 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
795 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
797 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
800 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
801 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
802 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
804 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
806 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
808 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
810 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
812 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
814 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
817 smu_pcie_arg = (1 << 16) | (pcie_gen << 8) | pcie_width;
818 ret = smu_send_smc_msg_with_param(smu,
819 SMU_MSG_OverridePcieParameters,
822 pr_err("[%s] Attempt to override pcie params failed!\n", __func__);
826 static int smu_smc_table_hw_init(struct smu_context *smu,
829 struct amdgpu_device *adev = smu->adev;
832 if (smu_is_dpm_running(smu) && adev->in_suspend) {
833 pr_info("dpm has been enabled\n");
837 ret = smu_init_display_count(smu, 0);
842 /* get boot_values from vbios to set revision, gfxclk, and etc. */
843 ret = smu_get_vbios_bootup_values(smu);
847 ret = smu_setup_pptable(smu);
851 ret = smu_get_clk_info_from_vbios(smu);
856 * check if the format_revision in vbios is up to pptable header
857 * version, and the structure size is not 0.
859 ret = smu_check_pptable(smu);
864 * allocate vram bos to store smc table contents.
866 ret = smu_init_fb_allocations(smu);
871 * Parse pptable format and fill PPTable_t smc_pptable to
872 * smu_table_context structure. And read the smc_dpm_table from vbios,
873 * then fill it into smc_pptable.
875 ret = smu_parse_pptable(smu);
880 * Send msg GetDriverIfVersion to check if the return value is equal
881 * with DRIVER_IF_VERSION of smc header.
883 ret = smu_check_fw_version(smu);
889 * Copy pptable bo in the vram to smc with SMU MSGs such as
890 * SetDriverDramAddr and TransferTableDram2Smu.
892 ret = smu_write_pptable(smu);
896 /* issue RunAfllBtc msg */
897 ret = smu_run_afll_btc(smu);
901 ret = smu_feature_set_allowed_mask(smu);
905 ret = smu_system_features_control(smu, true);
909 ret = smu_override_pcie_parameters(smu);
913 ret = smu_notify_display_change(smu);
918 * Set min deep sleep dce fclk with bootup value from vbios via
919 * SetMinDeepSleepDcefclk MSG.
921 ret = smu_set_min_dcef_deep_sleep(smu);
926 * Set initialized values (get from vbios) to dpm tables context such as
927 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
931 ret = smu_populate_smc_pptable(smu);
935 ret = smu_init_max_sustainable_clocks(smu);
940 ret = smu_set_default_od_settings(smu, initialize);
945 ret = smu_populate_umd_state_clk(smu);
949 ret = smu_get_power_limit(smu, &smu->default_power_limit, false);
955 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
957 ret = smu_set_tool_table_location(smu);
959 if (!smu_is_dpm_running(smu))
960 pr_info("dpm has been disabled\n");
966 * smu_alloc_memory_pool - allocate memory pool in the system memory
968 * @smu: amdgpu_device pointer
970 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
971 * and DramLogSetDramAddr can notify it changed.
973 * Returns 0 on success, error on failure.
975 static int smu_alloc_memory_pool(struct smu_context *smu)
977 struct amdgpu_device *adev = smu->adev;
978 struct smu_table_context *smu_table = &smu->smu_table;
979 struct smu_table *memory_pool = &smu_table->memory_pool;
980 uint64_t pool_size = smu->pool_size;
983 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
986 memory_pool->size = pool_size;
987 memory_pool->align = PAGE_SIZE;
988 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
991 case SMU_MEMORY_POOL_SIZE_256_MB:
992 case SMU_MEMORY_POOL_SIZE_512_MB:
993 case SMU_MEMORY_POOL_SIZE_1_GB:
994 case SMU_MEMORY_POOL_SIZE_2_GB:
995 ret = amdgpu_bo_create_kernel(adev,
1000 &memory_pool->mc_address,
1001 &memory_pool->cpu_addr);
1010 static int smu_free_memory_pool(struct smu_context *smu)
1012 struct smu_table_context *smu_table = &smu->smu_table;
1013 struct smu_table *memory_pool = &smu_table->memory_pool;
1016 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1019 amdgpu_bo_free_kernel(&memory_pool->bo,
1020 &memory_pool->mc_address,
1021 &memory_pool->cpu_addr);
1023 memset(memory_pool, 0, sizeof(struct smu_table));
1028 static int smu_hw_init(void *handle)
1031 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1032 struct smu_context *smu = &adev->smu;
1034 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1035 ret = smu_check_fw_status(smu);
1037 pr_err("SMC firmware status is not correct\n");
1042 ret = smu_feature_init_dpm(smu);
1046 ret = smu_smc_table_hw_init(smu, true);
1050 ret = smu_alloc_memory_pool(smu);
1055 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1058 ret = smu_notify_memory_pool_location(smu);
1062 ret = smu_start_thermal_control(smu);
1066 ret = smu_register_irq_handler(smu);
1070 if (!smu->pm_enabled)
1071 adev->pm.dpm_enabled = false;
1073 adev->pm.dpm_enabled = true; /* TODO: will set dpm_enabled flag while VCN and DAL DPM is workable */
1075 pr_info("SMU is initialized successfully!\n");
1083 static int smu_hw_fini(void *handle)
1085 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1086 struct smu_context *smu = &adev->smu;
1087 struct smu_table_context *table_context = &smu->smu_table;
1090 kfree(table_context->driver_pptable);
1091 table_context->driver_pptable = NULL;
1093 kfree(table_context->max_sustainable_clocks);
1094 table_context->max_sustainable_clocks = NULL;
1096 kfree(table_context->overdrive_table);
1097 table_context->overdrive_table = NULL;
1099 kfree(smu->irq_source);
1100 smu->irq_source = NULL;
1102 ret = smu_fini_fb_allocations(smu);
1106 ret = smu_free_memory_pool(smu);
1113 int smu_reset(struct smu_context *smu)
1115 struct amdgpu_device *adev = smu->adev;
1118 ret = smu_hw_fini(adev);
1122 ret = smu_hw_init(adev);
1129 static int smu_suspend(void *handle)
1132 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1133 struct smu_context *smu = &adev->smu;
1134 bool baco_feature_is_enabled = smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT);
1136 ret = smu_system_features_control(smu, false);
1140 if (adev->in_gpu_reset && baco_feature_is_enabled) {
1141 ret = smu_feature_set_enabled(smu, SMU_FEATURE_BACO_BIT, true);
1143 pr_warn("set BACO feature enabled failed, return %d\n", ret);
1148 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1150 if (adev->asic_type >= CHIP_NAVI10 &&
1151 adev->gfx.rlc.funcs->stop)
1152 adev->gfx.rlc.funcs->stop(adev);
1157 static int smu_resume(void *handle)
1160 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1161 struct smu_context *smu = &adev->smu;
1163 pr_info("SMU is resuming...\n");
1165 mutex_lock(&smu->mutex);
1167 ret = smu_smc_table_hw_init(smu, false);
1171 ret = smu_start_thermal_control(smu);
1175 mutex_unlock(&smu->mutex);
1177 pr_info("SMU is resumed successfully!\n");
1181 mutex_unlock(&smu->mutex);
1185 int smu_display_configuration_change(struct smu_context *smu,
1186 const struct amd_pp_display_configuration *display_config)
1189 int num_of_active_display = 0;
1191 if (!smu->pm_enabled || !is_support_sw_smu(smu->adev))
1194 if (!display_config)
1197 mutex_lock(&smu->mutex);
1199 smu_set_deep_sleep_dcefclk(smu,
1200 display_config->min_dcef_deep_sleep_set_clk / 100);
1202 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1203 if (display_config->displays[index].controller_id != 0)
1204 num_of_active_display++;
1207 smu_set_active_display_count(smu, num_of_active_display);
1209 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1210 display_config->cpu_cc6_disable,
1211 display_config->cpu_pstate_disable,
1212 display_config->nb_pstate_switch_disable);
1214 mutex_unlock(&smu->mutex);
1219 static int smu_get_clock_info(struct smu_context *smu,
1220 struct smu_clock_info *clk_info,
1221 enum smu_perf_level_designation designation)
1224 struct smu_performance_level level = {0};
1229 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1233 clk_info->min_mem_clk = level.memory_clock;
1234 clk_info->min_eng_clk = level.core_clock;
1235 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1237 ret = smu_get_perf_level(smu, designation, &level);
1241 clk_info->min_mem_clk = level.memory_clock;
1242 clk_info->min_eng_clk = level.core_clock;
1243 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1248 int smu_get_current_clocks(struct smu_context *smu,
1249 struct amd_pp_clock_info *clocks)
1251 struct amd_pp_simple_clock_info simple_clocks = {0};
1252 struct smu_clock_info hw_clocks;
1255 if (!is_support_sw_smu(smu->adev))
1258 mutex_lock(&smu->mutex);
1260 smu_get_dal_power_level(smu, &simple_clocks);
1262 if (smu->support_power_containment)
1263 ret = smu_get_clock_info(smu, &hw_clocks,
1264 PERF_LEVEL_POWER_CONTAINMENT);
1266 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1269 pr_err("Error in smu_get_clock_info\n");
1273 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1274 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1275 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1276 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1277 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1278 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1279 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1280 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1282 if (simple_clocks.level == 0)
1283 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1285 clocks->max_clocks_state = simple_clocks.level;
1287 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1288 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1289 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1293 mutex_unlock(&smu->mutex);
1297 static int smu_set_clockgating_state(void *handle,
1298 enum amd_clockgating_state state)
1303 static int smu_set_powergating_state(void *handle,
1304 enum amd_powergating_state state)
1309 static int smu_enable_umd_pstate(void *handle,
1310 enum amd_dpm_forced_level *level)
1312 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1313 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1314 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1315 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1317 struct smu_context *smu = (struct smu_context*)(handle);
1318 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1319 if (!smu->pm_enabled || !smu_dpm_ctx->dpm_context)
1322 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1323 /* enter umd pstate, save current level, disable gfx cg*/
1324 if (*level & profile_mode_mask) {
1325 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1326 smu_dpm_ctx->enable_umd_pstate = true;
1327 amdgpu_device_ip_set_clockgating_state(smu->adev,
1328 AMD_IP_BLOCK_TYPE_GFX,
1329 AMD_CG_STATE_UNGATE);
1330 amdgpu_device_ip_set_powergating_state(smu->adev,
1331 AMD_IP_BLOCK_TYPE_GFX,
1332 AMD_PG_STATE_UNGATE);
1335 /* exit umd pstate, restore level, enable gfx cg*/
1336 if (!(*level & profile_mode_mask)) {
1337 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1338 *level = smu_dpm_ctx->saved_dpm_level;
1339 smu_dpm_ctx->enable_umd_pstate = false;
1340 amdgpu_device_ip_set_clockgating_state(smu->adev,
1341 AMD_IP_BLOCK_TYPE_GFX,
1343 amdgpu_device_ip_set_powergating_state(smu->adev,
1344 AMD_IP_BLOCK_TYPE_GFX,
1352 int smu_adjust_power_state_dynamic(struct smu_context *smu,
1353 enum amd_dpm_forced_level level,
1354 bool skip_display_settings)
1358 uint32_t sclk_mask, mclk_mask, soc_mask;
1360 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1362 if (!smu->pm_enabled)
1364 if (!skip_display_settings) {
1365 ret = smu_display_config_changed(smu);
1367 pr_err("Failed to change display config!");
1372 if (!smu->pm_enabled)
1374 ret = smu_apply_clocks_adjust_rules(smu);
1376 pr_err("Failed to apply clocks adjust rules!");
1380 if (!skip_display_settings) {
1381 ret = smu_notify_smc_dispaly_config(smu);
1383 pr_err("Failed to notify smc display config!");
1388 if (smu_dpm_ctx->dpm_level != level) {
1390 case AMD_DPM_FORCED_LEVEL_HIGH:
1391 ret = smu_force_dpm_limit_value(smu, true);
1393 case AMD_DPM_FORCED_LEVEL_LOW:
1394 ret = smu_force_dpm_limit_value(smu, false);
1397 case AMD_DPM_FORCED_LEVEL_AUTO:
1398 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
1399 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);
1416 case AMD_DPM_FORCED_LEVEL_MANUAL:
1417 case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
1423 smu_dpm_ctx->dpm_level = level;
1426 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1427 index = fls(smu->workload_mask);
1428 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1429 workload = smu->workload_setting[index];
1431 if (smu->power_profile_mode != workload)
1432 smu_set_power_profile_mode(smu, &workload, 0);
1438 int smu_handle_task(struct smu_context *smu,
1439 enum amd_dpm_forced_level level,
1440 enum amd_pp_task task_id)
1445 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1446 ret = smu_pre_display_config_changed(smu);
1449 ret = smu_set_cpu_power_state(smu);
1452 ret = smu_adjust_power_state_dynamic(smu, level, false);
1454 case AMD_PP_TASK_COMPLETE_INIT:
1455 case AMD_PP_TASK_READJUST_POWER_STATE:
1456 ret = smu_adjust_power_state_dynamic(smu, level, true);
1465 enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1467 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1468 enum amd_dpm_forced_level level;
1470 if (!smu_dpm_ctx->dpm_context)
1473 mutex_lock(&(smu->mutex));
1474 level = smu_dpm_ctx->dpm_level;
1475 mutex_unlock(&(smu->mutex));
1480 int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1484 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1486 if (!smu_dpm_ctx->dpm_context)
1489 for (i = 0; i < smu->adev->num_ip_blocks; i++) {
1490 if (smu->adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)
1495 smu->adev->ip_blocks[i].version->funcs->enable_umd_pstate(smu, &level);
1496 ret = smu_handle_task(smu, level,
1497 AMD_PP_TASK_READJUST_POWER_STATE);
1501 mutex_lock(&smu->mutex);
1502 smu_dpm_ctx->dpm_level = level;
1503 mutex_unlock(&smu->mutex);
1508 int smu_set_display_count(struct smu_context *smu, uint32_t count)
1512 mutex_lock(&smu->mutex);
1513 ret = smu_init_display_count(smu, count);
1514 mutex_unlock(&smu->mutex);
1519 const struct amd_ip_funcs smu_ip_funcs = {
1521 .early_init = smu_early_init,
1522 .late_init = smu_late_init,
1523 .sw_init = smu_sw_init,
1524 .sw_fini = smu_sw_fini,
1525 .hw_init = smu_hw_init,
1526 .hw_fini = smu_hw_fini,
1527 .suspend = smu_suspend,
1528 .resume = smu_resume,
1530 .check_soft_reset = NULL,
1531 .wait_for_idle = NULL,
1533 .set_clockgating_state = smu_set_clockgating_state,
1534 .set_powergating_state = smu_set_powergating_state,
1535 .enable_umd_pstate = smu_enable_umd_pstate,
1538 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
1540 .type = AMD_IP_BLOCK_TYPE_SMC,
1544 .funcs = &smu_ip_funcs,