Path: blob/master/drivers/gpu/drm/amd/pm/swsmu/amdgpu_smu.c
29294 views
/*1* Copyright 2019 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*/2122#define SWSMU_CODE_LAYER_L12324#include <linux/firmware.h>25#include <linux/pci.h>26#include <linux/power_supply.h>27#include <linux/reboot.h>2829#include "amdgpu.h"30#include "amdgpu_smu.h"31#include "smu_internal.h"32#include "atom.h"33#include "arcturus_ppt.h"34#include "navi10_ppt.h"35#include "sienna_cichlid_ppt.h"36#include "renoir_ppt.h"37#include "vangogh_ppt.h"38#include "aldebaran_ppt.h"39#include "yellow_carp_ppt.h"40#include "cyan_skillfish_ppt.h"41#include "smu_v13_0_0_ppt.h"42#include "smu_v13_0_4_ppt.h"43#include "smu_v13_0_5_ppt.h"44#include "smu_v13_0_6_ppt.h"45#include "smu_v13_0_7_ppt.h"46#include "smu_v14_0_0_ppt.h"47#include "smu_v14_0_2_ppt.h"48#include "amd_pcie.h"4950/*51* DO NOT use these for err/warn/info/debug messages.52* Use dev_err, dev_warn, dev_info and dev_dbg instead.53* They are more MGPU friendly.54*/55#undef pr_err56#undef pr_warn57#undef pr_info58#undef pr_debug5960static const struct amd_pm_funcs swsmu_pm_funcs;61static int smu_force_smuclk_levels(struct smu_context *smu,62enum smu_clk_type clk_type,63uint32_t mask);64static int smu_handle_task(struct smu_context *smu,65enum amd_dpm_forced_level level,66enum amd_pp_task task_id);67static int smu_reset(struct smu_context *smu);68static int smu_set_fan_speed_pwm(void *handle, u32 speed);69static int smu_set_fan_control_mode(void *handle, u32 value);70static int smu_set_power_limit(void *handle, uint32_t limit);71static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);72static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);73static int smu_set_mp1_state(void *handle, enum pp_mp1_state mp1_state);74static void smu_power_profile_mode_get(struct smu_context *smu,75enum PP_SMC_POWER_PROFILE profile_mode);76static void smu_power_profile_mode_put(struct smu_context *smu,77enum PP_SMC_POWER_PROFILE profile_mode);78static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type);79static int smu_od_edit_dpm_table(void *handle,80enum PP_OD_DPM_TABLE_COMMAND type,81long *input, uint32_t size);8283static int smu_sys_get_pp_feature_mask(void *handle,84char *buf)85{86struct smu_context *smu = handle;8788if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)89return -EOPNOTSUPP;9091return smu_get_pp_feature_mask(smu, buf);92}9394static int smu_sys_set_pp_feature_mask(void *handle,95uint64_t new_mask)96{97struct smu_context *smu = handle;9899if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)100return -EOPNOTSUPP;101102return smu_set_pp_feature_mask(smu, new_mask);103}104105int smu_set_residency_gfxoff(struct smu_context *smu, bool value)106{107if (!smu->ppt_funcs->set_gfx_off_residency)108return -EINVAL;109110return smu_set_gfx_off_residency(smu, value);111}112113int smu_get_residency_gfxoff(struct smu_context *smu, u32 *value)114{115if (!smu->ppt_funcs->get_gfx_off_residency)116return -EINVAL;117118return smu_get_gfx_off_residency(smu, value);119}120121int smu_get_entrycount_gfxoff(struct smu_context *smu, u64 *value)122{123if (!smu->ppt_funcs->get_gfx_off_entrycount)124return -EINVAL;125126return smu_get_gfx_off_entrycount(smu, value);127}128129int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value)130{131if (!smu->ppt_funcs->get_gfx_off_status)132return -EINVAL;133134*value = smu_get_gfx_off_status(smu);135136return 0;137}138139int smu_set_soft_freq_range(struct smu_context *smu,140enum pp_clock_type type,141uint32_t min,142uint32_t max)143{144enum smu_clk_type clk_type;145int ret = 0;146147clk_type = smu_convert_to_smuclk(type);148if (clk_type == SMU_CLK_COUNT)149return -EINVAL;150151if (smu->ppt_funcs->set_soft_freq_limited_range)152ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,153clk_type,154min,155max,156false);157158return ret;159}160161int smu_get_dpm_freq_range(struct smu_context *smu,162enum smu_clk_type clk_type,163uint32_t *min,164uint32_t *max)165{166int ret = -ENOTSUPP;167168if (!min && !max)169return -EINVAL;170171if (smu->ppt_funcs->get_dpm_ultimate_freq)172ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,173clk_type,174min,175max);176177return ret;178}179180int smu_set_gfx_power_up_by_imu(struct smu_context *smu)181{182int ret = 0;183struct amdgpu_device *adev = smu->adev;184185if (smu->ppt_funcs->set_gfx_power_up_by_imu) {186ret = smu->ppt_funcs->set_gfx_power_up_by_imu(smu);187if (ret)188dev_err(adev->dev, "Failed to enable gfx imu!\n");189}190return ret;191}192193static u32 smu_get_mclk(void *handle, bool low)194{195struct smu_context *smu = handle;196uint32_t clk_freq;197int ret = 0;198199ret = smu_get_dpm_freq_range(smu, SMU_UCLK,200low ? &clk_freq : NULL,201!low ? &clk_freq : NULL);202if (ret)203return 0;204return clk_freq * 100;205}206207static u32 smu_get_sclk(void *handle, bool low)208{209struct smu_context *smu = handle;210uint32_t clk_freq;211int ret = 0;212213ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,214low ? &clk_freq : NULL,215!low ? &clk_freq : NULL);216if (ret)217return 0;218return clk_freq * 100;219}220221static int smu_set_gfx_imu_enable(struct smu_context *smu)222{223struct amdgpu_device *adev = smu->adev;224225if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)226return 0;227228if (amdgpu_in_reset(smu->adev) || adev->in_s0ix)229return 0;230231return smu_set_gfx_power_up_by_imu(smu);232}233234static bool is_vcn_enabled(struct amdgpu_device *adev)235{236int i;237238for (i = 0; i < adev->num_ip_blocks; i++) {239if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_VCN ||240adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_JPEG) &&241!adev->ip_blocks[i].status.valid)242return false;243}244245return true;246}247248static int smu_dpm_set_vcn_enable(struct smu_context *smu,249bool enable,250int inst)251{252struct smu_power_context *smu_power = &smu->smu_power;253struct smu_power_gate *power_gate = &smu_power->power_gate;254int ret = 0;255256/*257* don't poweron vcn/jpeg when they are skipped.258*/259if (!is_vcn_enabled(smu->adev))260return 0;261262if (!smu->ppt_funcs->dpm_set_vcn_enable)263return 0;264265if (atomic_read(&power_gate->vcn_gated[inst]) ^ enable)266return 0;267268ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable, inst);269if (!ret)270atomic_set(&power_gate->vcn_gated[inst], !enable);271272return ret;273}274275static int smu_dpm_set_jpeg_enable(struct smu_context *smu,276bool enable)277{278struct smu_power_context *smu_power = &smu->smu_power;279struct smu_power_gate *power_gate = &smu_power->power_gate;280int ret = 0;281282if (!is_vcn_enabled(smu->adev))283return 0;284285if (!smu->ppt_funcs->dpm_set_jpeg_enable)286return 0;287288if (atomic_read(&power_gate->jpeg_gated) ^ enable)289return 0;290291ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);292if (!ret)293atomic_set(&power_gate->jpeg_gated, !enable);294295return ret;296}297298static int smu_dpm_set_vpe_enable(struct smu_context *smu,299bool enable)300{301struct smu_power_context *smu_power = &smu->smu_power;302struct smu_power_gate *power_gate = &smu_power->power_gate;303int ret = 0;304305if (!smu->ppt_funcs->dpm_set_vpe_enable)306return 0;307308if (atomic_read(&power_gate->vpe_gated) ^ enable)309return 0;310311ret = smu->ppt_funcs->dpm_set_vpe_enable(smu, enable);312if (!ret)313atomic_set(&power_gate->vpe_gated, !enable);314315return ret;316}317318static int smu_dpm_set_isp_enable(struct smu_context *smu,319bool enable)320{321struct smu_power_context *smu_power = &smu->smu_power;322struct smu_power_gate *power_gate = &smu_power->power_gate;323int ret;324325if (!smu->ppt_funcs->dpm_set_isp_enable)326return 0;327328if (atomic_read(&power_gate->isp_gated) ^ enable)329return 0;330331ret = smu->ppt_funcs->dpm_set_isp_enable(smu, enable);332if (!ret)333atomic_set(&power_gate->isp_gated, !enable);334335return ret;336}337338static int smu_dpm_set_umsch_mm_enable(struct smu_context *smu,339bool enable)340{341struct smu_power_context *smu_power = &smu->smu_power;342struct smu_power_gate *power_gate = &smu_power->power_gate;343int ret = 0;344345if (!smu->adev->enable_umsch_mm)346return 0;347348if (!smu->ppt_funcs->dpm_set_umsch_mm_enable)349return 0;350351if (atomic_read(&power_gate->umsch_mm_gated) ^ enable)352return 0;353354ret = smu->ppt_funcs->dpm_set_umsch_mm_enable(smu, enable);355if (!ret)356atomic_set(&power_gate->umsch_mm_gated, !enable);357358return ret;359}360361static int smu_set_mall_enable(struct smu_context *smu)362{363int ret = 0;364365if (!smu->ppt_funcs->set_mall_enable)366return 0;367368ret = smu->ppt_funcs->set_mall_enable(smu);369370return ret;371}372373/**374* smu_dpm_set_power_gate - power gate/ungate the specific IP block375*376* @handle: smu_context pointer377* @block_type: the IP block to power gate/ungate378* @gate: to power gate if true, ungate otherwise379* @inst: the instance of the IP block to power gate/ungate380*381* This API uses no smu->mutex lock protection due to:382* 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).383* This is guarded to be race condition free by the caller.384* 2. Or get called on user setting request of power_dpm_force_performance_level.385* Under this case, the smu->mutex lock protection is already enforced on386* the parent API smu_force_performance_level of the call path.387*/388static int smu_dpm_set_power_gate(void *handle,389uint32_t block_type,390bool gate,391int inst)392{393struct smu_context *smu = handle;394int ret = 0;395396if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) {397dev_WARN(smu->adev->dev,398"SMU uninitialized but power %s requested for %u!\n",399gate ? "gate" : "ungate", block_type);400return -EOPNOTSUPP;401}402403switch (block_type) {404/*405* Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses406* AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.407*/408case AMD_IP_BLOCK_TYPE_UVD:409case AMD_IP_BLOCK_TYPE_VCN:410ret = smu_dpm_set_vcn_enable(smu, !gate, inst);411if (ret)412dev_err(smu->adev->dev, "Failed to power %s VCN instance %d!\n",413gate ? "gate" : "ungate", inst);414break;415case AMD_IP_BLOCK_TYPE_GFX:416ret = smu_gfx_off_control(smu, gate);417if (ret)418dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",419gate ? "enable" : "disable");420break;421case AMD_IP_BLOCK_TYPE_SDMA:422ret = smu_powergate_sdma(smu, gate);423if (ret)424dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",425gate ? "gate" : "ungate");426break;427case AMD_IP_BLOCK_TYPE_JPEG:428ret = smu_dpm_set_jpeg_enable(smu, !gate);429if (ret)430dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",431gate ? "gate" : "ungate");432break;433case AMD_IP_BLOCK_TYPE_VPE:434ret = smu_dpm_set_vpe_enable(smu, !gate);435if (ret)436dev_err(smu->adev->dev, "Failed to power %s VPE!\n",437gate ? "gate" : "ungate");438break;439case AMD_IP_BLOCK_TYPE_ISP:440ret = smu_dpm_set_isp_enable(smu, !gate);441if (ret)442dev_err(smu->adev->dev, "Failed to power %s ISP!\n",443gate ? "gate" : "ungate");444break;445default:446dev_err(smu->adev->dev, "Unsupported block type!\n");447return -EINVAL;448}449450return ret;451}452453/**454* smu_set_user_clk_dependencies - set user profile clock dependencies455*456* @smu: smu_context pointer457* @clk: enum smu_clk_type type458*459* Enable/Disable the clock dependency for the @clk type.460*/461static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)462{463if (smu->adev->in_suspend)464return;465466if (clk == SMU_MCLK) {467smu->user_dpm_profile.clk_dependency = 0;468smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);469} else if (clk == SMU_FCLK) {470/* MCLK takes precedence over FCLK */471if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))472return;473474smu->user_dpm_profile.clk_dependency = 0;475smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);476} else if (clk == SMU_SOCCLK) {477/* MCLK takes precedence over SOCCLK */478if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))479return;480481smu->user_dpm_profile.clk_dependency = 0;482smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);483} else484/* Add clk dependencies here, if any */485return;486}487488/**489* smu_restore_dpm_user_profile - reinstate user dpm profile490*491* @smu: smu_context pointer492*493* Restore the saved user power configurations include power limit,494* clock frequencies, fan control mode and fan speed.495*/496static void smu_restore_dpm_user_profile(struct smu_context *smu)497{498struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);499int ret = 0;500501if (!smu->adev->in_suspend)502return;503504if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)505return;506507/* Enable restore flag */508smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;509510/* set the user dpm power limit */511if (smu->user_dpm_profile.power_limit) {512ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);513if (ret)514dev_err(smu->adev->dev, "Failed to set power limit value\n");515}516517/* set the user dpm clock configurations */518if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {519enum smu_clk_type clk_type;520521for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {522/*523* Iterate over smu clk type and force the saved user clk524* configs, skip if clock dependency is enabled525*/526if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&527smu->user_dpm_profile.clk_mask[clk_type]) {528ret = smu_force_smuclk_levels(smu, clk_type,529smu->user_dpm_profile.clk_mask[clk_type]);530if (ret)531dev_err(smu->adev->dev,532"Failed to set clock type = %d\n", clk_type);533}534}535}536537/* set the user dpm fan configurations */538if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||539smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {540ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);541if (ret != -EOPNOTSUPP) {542smu->user_dpm_profile.fan_speed_pwm = 0;543smu->user_dpm_profile.fan_speed_rpm = 0;544smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;545dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");546}547548if (smu->user_dpm_profile.fan_speed_pwm) {549ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);550if (ret != -EOPNOTSUPP)551dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");552}553554if (smu->user_dpm_profile.fan_speed_rpm) {555ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);556if (ret != -EOPNOTSUPP)557dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");558}559}560561/* Restore user customized OD settings */562if (smu->user_dpm_profile.user_od) {563if (smu->ppt_funcs->restore_user_od_settings) {564ret = smu->ppt_funcs->restore_user_od_settings(smu);565if (ret)566dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");567}568}569570/* Disable restore flag */571smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;572}573574static int smu_get_power_num_states(void *handle,575struct pp_states_info *state_info)576{577if (!state_info)578return -EINVAL;579580/* not support power state */581memset(state_info, 0, sizeof(struct pp_states_info));582state_info->nums = 1;583state_info->states[0] = POWER_STATE_TYPE_DEFAULT;584585return 0;586}587588bool is_support_sw_smu(struct amdgpu_device *adev)589{590/* vega20 is 11.0.2, but it's supported via the powerplay code */591if (adev->asic_type == CHIP_VEGA20)592return false;593594if ((amdgpu_ip_version(adev, MP1_HWIP, 0) >= IP_VERSION(11, 0, 0)) &&595amdgpu_device_ip_is_valid(adev, AMD_IP_BLOCK_TYPE_SMC))596return true;597598return false;599}600601bool is_support_cclk_dpm(struct amdgpu_device *adev)602{603struct smu_context *smu = adev->powerplay.pp_handle;604605if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))606return false;607608return true;609}610611612static int smu_sys_get_pp_table(void *handle,613char **table)614{615struct smu_context *smu = handle;616struct smu_table_context *smu_table = &smu->smu_table;617618if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)619return -EOPNOTSUPP;620621if (!smu_table->power_play_table && !smu_table->hardcode_pptable)622return -EINVAL;623624if (smu_table->hardcode_pptable)625*table = smu_table->hardcode_pptable;626else627*table = smu_table->power_play_table;628629return smu_table->power_play_table_size;630}631632static int smu_sys_set_pp_table(void *handle,633const char *buf,634size_t size)635{636struct smu_context *smu = handle;637struct smu_table_context *smu_table = &smu->smu_table;638ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;639int ret = 0;640641if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)642return -EOPNOTSUPP;643644if (header->usStructureSize != size) {645dev_err(smu->adev->dev, "pp table size not matched !\n");646return -EIO;647}648649if (!smu_table->hardcode_pptable || smu_table->power_play_table_size < size) {650kfree(smu_table->hardcode_pptable);651smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);652if (!smu_table->hardcode_pptable)653return -ENOMEM;654}655656memcpy(smu_table->hardcode_pptable, buf, size);657smu_table->power_play_table = smu_table->hardcode_pptable;658smu_table->power_play_table_size = size;659660/*661* Special hw_fini action(for Navi1x, the DPMs disablement will be662* skipped) may be needed for custom pptable uploading.663*/664smu->uploading_custom_pp_table = true;665666ret = smu_reset(smu);667if (ret)668dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);669670smu->uploading_custom_pp_table = false;671672return ret;673}674675static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)676{677struct smu_feature *feature = &smu->smu_feature;678uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];679int ret = 0;680681/*682* With SCPM enabled, the allowed featuremasks setting(via683* PPSMC_MSG_SetAllowedFeaturesMaskLow/High) is not permitted.684* That means there is no way to let PMFW knows the settings below.685* Thus, we just assume all the features are allowed under686* such scenario.687*/688if (smu->adev->scpm_enabled) {689bitmap_fill(feature->allowed, SMU_FEATURE_MAX);690return 0;691}692693bitmap_zero(feature->allowed, SMU_FEATURE_MAX);694695ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,696SMU_FEATURE_MAX/32);697if (ret)698return ret;699700bitmap_or(feature->allowed, feature->allowed,701(unsigned long *)allowed_feature_mask,702feature->feature_num);703704return ret;705}706707static int smu_set_funcs(struct amdgpu_device *adev)708{709struct smu_context *smu = adev->powerplay.pp_handle;710711if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)712smu->od_enabled = true;713714switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {715case IP_VERSION(11, 0, 0):716case IP_VERSION(11, 0, 5):717case IP_VERSION(11, 0, 9):718navi10_set_ppt_funcs(smu);719break;720case IP_VERSION(11, 0, 7):721case IP_VERSION(11, 0, 11):722case IP_VERSION(11, 0, 12):723case IP_VERSION(11, 0, 13):724sienna_cichlid_set_ppt_funcs(smu);725break;726case IP_VERSION(12, 0, 0):727case IP_VERSION(12, 0, 1):728renoir_set_ppt_funcs(smu);729break;730case IP_VERSION(11, 5, 0):731case IP_VERSION(11, 5, 2):732vangogh_set_ppt_funcs(smu);733break;734case IP_VERSION(13, 0, 1):735case IP_VERSION(13, 0, 3):736case IP_VERSION(13, 0, 8):737yellow_carp_set_ppt_funcs(smu);738break;739case IP_VERSION(13, 0, 4):740case IP_VERSION(13, 0, 11):741smu_v13_0_4_set_ppt_funcs(smu);742break;743case IP_VERSION(13, 0, 5):744smu_v13_0_5_set_ppt_funcs(smu);745break;746case IP_VERSION(11, 0, 8):747cyan_skillfish_set_ppt_funcs(smu);748break;749case IP_VERSION(11, 0, 2):750adev->pm.pp_feature &= ~PP_GFXOFF_MASK;751arcturus_set_ppt_funcs(smu);752/* OD is not supported on Arcturus */753smu->od_enabled = false;754break;755case IP_VERSION(13, 0, 2):756aldebaran_set_ppt_funcs(smu);757/* Enable pp_od_clk_voltage node */758smu->od_enabled = true;759break;760case IP_VERSION(13, 0, 0):761case IP_VERSION(13, 0, 10):762smu_v13_0_0_set_ppt_funcs(smu);763break;764case IP_VERSION(13, 0, 6):765case IP_VERSION(13, 0, 14):766case IP_VERSION(13, 0, 12):767smu_v13_0_6_set_ppt_funcs(smu);768/* Enable pp_od_clk_voltage node */769smu->od_enabled = true;770break;771case IP_VERSION(13, 0, 7):772smu_v13_0_7_set_ppt_funcs(smu);773break;774case IP_VERSION(14, 0, 0):775case IP_VERSION(14, 0, 1):776case IP_VERSION(14, 0, 4):777case IP_VERSION(14, 0, 5):778smu_v14_0_0_set_ppt_funcs(smu);779break;780case IP_VERSION(14, 0, 2):781case IP_VERSION(14, 0, 3):782smu_v14_0_2_set_ppt_funcs(smu);783break;784default:785return -EINVAL;786}787788return 0;789}790791static int smu_early_init(struct amdgpu_ip_block *ip_block)792{793struct amdgpu_device *adev = ip_block->adev;794struct smu_context *smu;795int r;796797smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL);798if (!smu)799return -ENOMEM;800801smu->adev = adev;802smu->pm_enabled = !!amdgpu_dpm;803smu->is_apu = false;804smu->smu_baco.state = SMU_BACO_STATE_NONE;805smu->smu_baco.platform_support = false;806smu->smu_baco.maco_support = false;807smu->user_dpm_profile.fan_mode = -1;808smu->power_profile_mode = PP_SMC_POWER_PROFILE_UNKNOWN;809810mutex_init(&smu->message_lock);811812adev->powerplay.pp_handle = smu;813adev->powerplay.pp_funcs = &swsmu_pm_funcs;814815r = smu_set_funcs(adev);816if (r)817return r;818return smu_init_microcode(smu);819}820821static int smu_set_default_dpm_table(struct smu_context *smu)822{823struct amdgpu_device *adev = smu->adev;824struct smu_power_context *smu_power = &smu->smu_power;825struct smu_power_gate *power_gate = &smu_power->power_gate;826int vcn_gate[AMDGPU_MAX_VCN_INSTANCES], jpeg_gate, i;827int ret = 0;828829if (!smu->ppt_funcs->set_default_dpm_table)830return 0;831832if (adev->pg_flags & AMD_PG_SUPPORT_VCN) {833for (i = 0; i < adev->vcn.num_vcn_inst; i++)834vcn_gate[i] = atomic_read(&power_gate->vcn_gated[i]);835}836if (adev->pg_flags & AMD_PG_SUPPORT_JPEG)837jpeg_gate = atomic_read(&power_gate->jpeg_gated);838839if (adev->pg_flags & AMD_PG_SUPPORT_VCN) {840for (i = 0; i < adev->vcn.num_vcn_inst; i++) {841ret = smu_dpm_set_vcn_enable(smu, true, i);842if (ret)843return ret;844}845}846847if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {848ret = smu_dpm_set_jpeg_enable(smu, true);849if (ret)850goto err_out;851}852853ret = smu->ppt_funcs->set_default_dpm_table(smu);854if (ret)855dev_err(smu->adev->dev,856"Failed to setup default dpm clock tables!\n");857858if (adev->pg_flags & AMD_PG_SUPPORT_JPEG)859smu_dpm_set_jpeg_enable(smu, !jpeg_gate);860err_out:861if (adev->pg_flags & AMD_PG_SUPPORT_VCN) {862for (i = 0; i < adev->vcn.num_vcn_inst; i++)863smu_dpm_set_vcn_enable(smu, !vcn_gate[i], i);864}865866return ret;867}868869static int smu_apply_default_config_table_settings(struct smu_context *smu)870{871struct amdgpu_device *adev = smu->adev;872int ret = 0;873874ret = smu_get_default_config_table_settings(smu,875&adev->pm.config_table);876if (ret)877return ret;878879return smu_set_config_table(smu, &adev->pm.config_table);880}881882static int smu_late_init(struct amdgpu_ip_block *ip_block)883{884struct amdgpu_device *adev = ip_block->adev;885struct smu_context *smu = adev->powerplay.pp_handle;886int ret = 0;887888smu_set_fine_grain_gfx_freq_parameters(smu);889890if (!smu->pm_enabled)891return 0;892893ret = smu_post_init(smu);894if (ret) {895dev_err(adev->dev, "Failed to post smu init!\n");896return ret;897}898899/*900* Explicitly notify PMFW the power mode the system in. Since901* the PMFW may boot the ASIC with a different mode.902* For those supporting ACDC switch via gpio, PMFW will903* handle the switch automatically. Driver involvement904* is unnecessary.905*/906adev->pm.ac_power = power_supply_is_system_supplied() > 0;907smu_set_ac_dc(smu);908909if ((amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 1)) ||910(amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 3)))911return 0;912913if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {914ret = smu_set_default_od_settings(smu);915if (ret) {916dev_err(adev->dev, "Failed to setup default OD settings!\n");917return ret;918}919}920921ret = smu_populate_umd_state_clk(smu);922if (ret) {923dev_err(adev->dev, "Failed to populate UMD state clocks!\n");924return ret;925}926927ret = smu_get_asic_power_limits(smu,928&smu->current_power_limit,929&smu->default_power_limit,930&smu->max_power_limit,931&smu->min_power_limit);932if (ret) {933dev_err(adev->dev, "Failed to get asic power limits!\n");934return ret;935}936937if (!amdgpu_sriov_vf(adev))938smu_get_unique_id(smu);939940smu_get_fan_parameters(smu);941942smu_handle_task(smu,943smu->smu_dpm.dpm_level,944AMD_PP_TASK_COMPLETE_INIT);945946ret = smu_apply_default_config_table_settings(smu);947if (ret && (ret != -EOPNOTSUPP)) {948dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n");949return ret;950}951952smu_restore_dpm_user_profile(smu);953954return 0;955}956957static int smu_init_fb_allocations(struct smu_context *smu)958{959struct amdgpu_device *adev = smu->adev;960struct smu_table_context *smu_table = &smu->smu_table;961struct smu_table *tables = smu_table->tables;962struct smu_table *driver_table = &(smu_table->driver_table);963uint32_t max_table_size = 0;964int ret, i;965966/* VRAM allocation for tool table */967if (tables[SMU_TABLE_PMSTATUSLOG].size) {968ret = amdgpu_bo_create_kernel(adev,969tables[SMU_TABLE_PMSTATUSLOG].size,970tables[SMU_TABLE_PMSTATUSLOG].align,971tables[SMU_TABLE_PMSTATUSLOG].domain,972&tables[SMU_TABLE_PMSTATUSLOG].bo,973&tables[SMU_TABLE_PMSTATUSLOG].mc_address,974&tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);975if (ret) {976dev_err(adev->dev, "VRAM allocation for tool table failed!\n");977return ret;978}979}980981driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT;982/* VRAM allocation for driver table */983for (i = 0; i < SMU_TABLE_COUNT; i++) {984if (tables[i].size == 0)985continue;986987/* If one of the tables has VRAM domain restriction, keep it in988* VRAM989*/990if ((tables[i].domain &991(AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) ==992AMDGPU_GEM_DOMAIN_VRAM)993driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;994995if (i == SMU_TABLE_PMSTATUSLOG)996continue;997998if (max_table_size < tables[i].size)999max_table_size = tables[i].size;1000}10011002driver_table->size = max_table_size;1003driver_table->align = PAGE_SIZE;10041005ret = amdgpu_bo_create_kernel(adev,1006driver_table->size,1007driver_table->align,1008driver_table->domain,1009&driver_table->bo,1010&driver_table->mc_address,1011&driver_table->cpu_addr);1012if (ret) {1013dev_err(adev->dev, "VRAM allocation for driver table failed!\n");1014if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)1015amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,1016&tables[SMU_TABLE_PMSTATUSLOG].mc_address,1017&tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);1018}10191020return ret;1021}10221023static int smu_fini_fb_allocations(struct smu_context *smu)1024{1025struct smu_table_context *smu_table = &smu->smu_table;1026struct smu_table *tables = smu_table->tables;1027struct smu_table *driver_table = &(smu_table->driver_table);10281029if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)1030amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,1031&tables[SMU_TABLE_PMSTATUSLOG].mc_address,1032&tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);10331034amdgpu_bo_free_kernel(&driver_table->bo,1035&driver_table->mc_address,1036&driver_table->cpu_addr);10371038return 0;1039}10401041static void smu_update_gpu_addresses(struct smu_context *smu)1042{1043struct smu_table_context *smu_table = &smu->smu_table;1044struct smu_table *pm_status_table = smu_table->tables + SMU_TABLE_PMSTATUSLOG;1045struct smu_table *driver_table = &(smu_table->driver_table);1046struct smu_table *dummy_read_1_table = &smu_table->dummy_read_1_table;10471048if (pm_status_table->bo)1049pm_status_table->mc_address = amdgpu_bo_fb_aper_addr(pm_status_table->bo);1050if (driver_table->bo)1051driver_table->mc_address = amdgpu_bo_fb_aper_addr(driver_table->bo);1052if (dummy_read_1_table->bo)1053dummy_read_1_table->mc_address = amdgpu_bo_fb_aper_addr(dummy_read_1_table->bo);1054}10551056/**1057* smu_alloc_memory_pool - allocate memory pool in the system memory1058*1059* @smu: amdgpu_device pointer1060*1061* This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr1062* and DramLogSetDramAddr can notify it changed.1063*1064* Returns 0 on success, error on failure.1065*/1066static int smu_alloc_memory_pool(struct smu_context *smu)1067{1068struct amdgpu_device *adev = smu->adev;1069struct smu_table_context *smu_table = &smu->smu_table;1070struct smu_table *memory_pool = &smu_table->memory_pool;1071uint64_t pool_size = smu->pool_size;1072int ret = 0;10731074if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)1075return ret;10761077memory_pool->size = pool_size;1078memory_pool->align = PAGE_SIZE;1079memory_pool->domain =1080(adev->pm.smu_debug_mask & SMU_DEBUG_POOL_USE_VRAM) ?1081AMDGPU_GEM_DOMAIN_VRAM :1082AMDGPU_GEM_DOMAIN_GTT;10831084switch (pool_size) {1085case SMU_MEMORY_POOL_SIZE_256_MB:1086case SMU_MEMORY_POOL_SIZE_512_MB:1087case SMU_MEMORY_POOL_SIZE_1_GB:1088case SMU_MEMORY_POOL_SIZE_2_GB:1089ret = amdgpu_bo_create_kernel(adev,1090memory_pool->size,1091memory_pool->align,1092memory_pool->domain,1093&memory_pool->bo,1094&memory_pool->mc_address,1095&memory_pool->cpu_addr);1096if (ret)1097dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");1098break;1099default:1100break;1101}11021103return ret;1104}11051106static int smu_free_memory_pool(struct smu_context *smu)1107{1108struct smu_table_context *smu_table = &smu->smu_table;1109struct smu_table *memory_pool = &smu_table->memory_pool;11101111if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)1112return 0;11131114amdgpu_bo_free_kernel(&memory_pool->bo,1115&memory_pool->mc_address,1116&memory_pool->cpu_addr);11171118memset(memory_pool, 0, sizeof(struct smu_table));11191120return 0;1121}11221123static int smu_alloc_dummy_read_table(struct smu_context *smu)1124{1125struct smu_table_context *smu_table = &smu->smu_table;1126struct smu_table *dummy_read_1_table =1127&smu_table->dummy_read_1_table;1128struct amdgpu_device *adev = smu->adev;1129int ret = 0;11301131if (!dummy_read_1_table->size)1132return 0;11331134ret = amdgpu_bo_create_kernel(adev,1135dummy_read_1_table->size,1136dummy_read_1_table->align,1137dummy_read_1_table->domain,1138&dummy_read_1_table->bo,1139&dummy_read_1_table->mc_address,1140&dummy_read_1_table->cpu_addr);1141if (ret)1142dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");11431144return ret;1145}11461147static void smu_free_dummy_read_table(struct smu_context *smu)1148{1149struct smu_table_context *smu_table = &smu->smu_table;1150struct smu_table *dummy_read_1_table =1151&smu_table->dummy_read_1_table;115211531154amdgpu_bo_free_kernel(&dummy_read_1_table->bo,1155&dummy_read_1_table->mc_address,1156&dummy_read_1_table->cpu_addr);11571158memset(dummy_read_1_table, 0, sizeof(struct smu_table));1159}11601161static int smu_smc_table_sw_init(struct smu_context *smu)1162{1163int ret;11641165/**1166* Create smu_table structure, and init smc tables such as1167* TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.1168*/1169ret = smu_init_smc_tables(smu);1170if (ret) {1171dev_err(smu->adev->dev, "Failed to init smc tables!\n");1172return ret;1173}11741175/**1176* Create smu_power_context structure, and allocate smu_dpm_context and1177* context size to fill the smu_power_context data.1178*/1179ret = smu_init_power(smu);1180if (ret) {1181dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");1182return ret;1183}11841185/*1186* allocate vram bos to store smc table contents.1187*/1188ret = smu_init_fb_allocations(smu);1189if (ret)1190return ret;11911192ret = smu_alloc_memory_pool(smu);1193if (ret)1194return ret;11951196ret = smu_alloc_dummy_read_table(smu);1197if (ret)1198return ret;11991200ret = smu_i2c_init(smu);1201if (ret)1202return ret;12031204return 0;1205}12061207static int smu_smc_table_sw_fini(struct smu_context *smu)1208{1209int ret;12101211smu_i2c_fini(smu);12121213smu_free_dummy_read_table(smu);12141215ret = smu_free_memory_pool(smu);1216if (ret)1217return ret;12181219ret = smu_fini_fb_allocations(smu);1220if (ret)1221return ret;12221223ret = smu_fini_power(smu);1224if (ret) {1225dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");1226return ret;1227}12281229ret = smu_fini_smc_tables(smu);1230if (ret) {1231dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");1232return ret;1233}12341235return 0;1236}12371238static void smu_throttling_logging_work_fn(struct work_struct *work)1239{1240struct smu_context *smu = container_of(work, struct smu_context,1241throttling_logging_work);12421243smu_log_thermal_throttling(smu);1244}12451246static void smu_interrupt_work_fn(struct work_struct *work)1247{1248struct smu_context *smu = container_of(work, struct smu_context,1249interrupt_work);12501251if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)1252smu->ppt_funcs->interrupt_work(smu);1253}12541255static void smu_swctf_delayed_work_handler(struct work_struct *work)1256{1257struct smu_context *smu =1258container_of(work, struct smu_context, swctf_delayed_work.work);1259struct smu_temperature_range *range =1260&smu->thermal_range;1261struct amdgpu_device *adev = smu->adev;1262uint32_t hotspot_tmp, size;12631264/*1265* If the hotspot temperature is confirmed as below SW CTF setting point1266* after the delay enforced, nothing will be done.1267* Otherwise, a graceful shutdown will be performed to prevent further damage.1268*/1269if (range->software_shutdown_temp &&1270smu->ppt_funcs->read_sensor &&1271!smu->ppt_funcs->read_sensor(smu,1272AMDGPU_PP_SENSOR_HOTSPOT_TEMP,1273&hotspot_tmp,1274&size) &&1275hotspot_tmp / 1000 < range->software_shutdown_temp)1276return;12771278dev_emerg(adev->dev, "ERROR: GPU over temperature range(SW CTF) detected!\n");1279dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU SW CTF!\n");1280orderly_poweroff(true);1281}12821283static void smu_init_xgmi_plpd_mode(struct smu_context *smu)1284{1285struct smu_dpm_context *dpm_ctxt = &(smu->smu_dpm);1286struct smu_dpm_policy_ctxt *policy_ctxt;1287struct smu_dpm_policy *policy;12881289policy = smu_get_pm_policy(smu, PP_PM_POLICY_XGMI_PLPD);1290if (amdgpu_ip_version(smu->adev, MP1_HWIP, 0) == IP_VERSION(11, 0, 2)) {1291if (policy)1292policy->current_level = XGMI_PLPD_DEFAULT;1293return;1294}12951296/* PMFW put PLPD into default policy after enabling the feature */1297if (smu_feature_is_enabled(smu,1298SMU_FEATURE_XGMI_PER_LINK_PWR_DWN_BIT)) {1299if (policy)1300policy->current_level = XGMI_PLPD_DEFAULT;1301} else {1302policy_ctxt = dpm_ctxt->dpm_policies;1303if (policy_ctxt)1304policy_ctxt->policy_mask &=1305~BIT(PP_PM_POLICY_XGMI_PLPD);1306}1307}13081309static void smu_init_power_profile(struct smu_context *smu)1310{1311if (smu->power_profile_mode == PP_SMC_POWER_PROFILE_UNKNOWN)1312smu->power_profile_mode =1313PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;1314smu_power_profile_mode_get(smu, smu->power_profile_mode);1315}13161317void smu_feature_cap_set(struct smu_context *smu, enum smu_feature_cap_id fea_id)1318{1319struct smu_feature_cap *fea_cap = &smu->fea_cap;13201321if (fea_id >= SMU_FEATURE_CAP_ID__COUNT)1322return;13231324set_bit(fea_id, fea_cap->cap_map);1325}13261327bool smu_feature_cap_test(struct smu_context *smu, enum smu_feature_cap_id fea_id)1328{1329struct smu_feature_cap *fea_cap = &smu->fea_cap;13301331if (fea_id >= SMU_FEATURE_CAP_ID__COUNT)1332return false;13331334return test_bit(fea_id, fea_cap->cap_map);1335}13361337static void smu_feature_cap_init(struct smu_context *smu)1338{1339struct smu_feature_cap *fea_cap = &smu->fea_cap;13401341bitmap_zero(fea_cap->cap_map, SMU_FEATURE_CAP_ID__COUNT);1342}13431344static int smu_sw_init(struct amdgpu_ip_block *ip_block)1345{1346struct amdgpu_device *adev = ip_block->adev;1347struct smu_context *smu = adev->powerplay.pp_handle;1348int i, ret;13491350smu->pool_size = adev->pm.smu_prv_buffer_size;1351smu->smu_feature.feature_num = SMU_FEATURE_MAX;1352bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);1353bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);13541355INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);1356INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);1357atomic64_set(&smu->throttle_int_counter, 0);1358smu->watermarks_bitmap = 0;13591360for (i = 0; i < adev->vcn.num_vcn_inst; i++)1361atomic_set(&smu->smu_power.power_gate.vcn_gated[i], 1);1362atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);1363atomic_set(&smu->smu_power.power_gate.vpe_gated, 1);1364atomic_set(&smu->smu_power.power_gate.isp_gated, 1);1365atomic_set(&smu->smu_power.power_gate.umsch_mm_gated, 1);13661367smu_init_power_profile(smu);1368smu->display_config = &adev->pm.pm_display_cfg;13691370smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;1371smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;13721373INIT_DELAYED_WORK(&smu->swctf_delayed_work,1374smu_swctf_delayed_work_handler);13751376smu_feature_cap_init(smu);13771378ret = smu_smc_table_sw_init(smu);1379if (ret) {1380dev_err(adev->dev, "Failed to sw init smc table!\n");1381return ret;1382}13831384/* get boot_values from vbios to set revision, gfxclk, and etc. */1385ret = smu_get_vbios_bootup_values(smu);1386if (ret) {1387dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");1388return ret;1389}13901391ret = smu_init_pptable_microcode(smu);1392if (ret) {1393dev_err(adev->dev, "Failed to setup pptable firmware!\n");1394return ret;1395}13961397ret = smu_register_irq_handler(smu);1398if (ret) {1399dev_err(adev->dev, "Failed to register smc irq handler!\n");1400return ret;1401}14021403/* If there is no way to query fan control mode, fan control is not supported */1404if (!smu->ppt_funcs->get_fan_control_mode)1405smu->adev->pm.no_fan = true;14061407return 0;1408}14091410static int smu_sw_fini(struct amdgpu_ip_block *ip_block)1411{1412struct amdgpu_device *adev = ip_block->adev;1413struct smu_context *smu = adev->powerplay.pp_handle;1414int ret;14151416ret = smu_smc_table_sw_fini(smu);1417if (ret) {1418dev_err(adev->dev, "Failed to sw fini smc table!\n");1419return ret;1420}14211422if (smu->custom_profile_params) {1423kfree(smu->custom_profile_params);1424smu->custom_profile_params = NULL;1425}14261427smu_fini_microcode(smu);14281429return 0;1430}14311432static int smu_get_thermal_temperature_range(struct smu_context *smu)1433{1434struct amdgpu_device *adev = smu->adev;1435struct smu_temperature_range *range =1436&smu->thermal_range;1437int ret = 0;14381439if (!smu->ppt_funcs->get_thermal_temperature_range)1440return 0;14411442ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);1443if (ret)1444return ret;14451446adev->pm.dpm.thermal.min_temp = range->min;1447adev->pm.dpm.thermal.max_temp = range->max;1448adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;1449adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;1450adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;1451adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;1452adev->pm.dpm.thermal.min_mem_temp = range->mem_min;1453adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;1454adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;14551456return ret;1457}14581459/**1460* smu_wbrf_handle_exclusion_ranges - consume the wbrf exclusion ranges1461*1462* @smu: smu_context pointer1463*1464* Retrieve the wbrf exclusion ranges and send them to PMFW for proper handling.1465* Returns 0 on success, error on failure.1466*/1467static int smu_wbrf_handle_exclusion_ranges(struct smu_context *smu)1468{1469struct wbrf_ranges_in_out wbrf_exclusion = {0};1470struct freq_band_range *wifi_bands = wbrf_exclusion.band_list;1471struct amdgpu_device *adev = smu->adev;1472uint32_t num_of_wbrf_ranges = MAX_NUM_OF_WBRF_RANGES;1473uint64_t start, end;1474int ret, i, j;14751476ret = amd_wbrf_retrieve_freq_band(adev->dev, &wbrf_exclusion);1477if (ret) {1478dev_err(adev->dev, "Failed to retrieve exclusion ranges!\n");1479return ret;1480}14811482/*1483* The exclusion ranges array we got might be filled with holes and duplicate1484* entries. For example:1485* {(2400, 2500), (0, 0), (6882, 6962), (2400, 2500), (0, 0), (6117, 6189), (0, 0)...}1486* We need to do some sortups to eliminate those holes and duplicate entries.1487* Expected output: {(2400, 2500), (6117, 6189), (6882, 6962), (0, 0)...}1488*/1489for (i = 0; i < num_of_wbrf_ranges; i++) {1490start = wifi_bands[i].start;1491end = wifi_bands[i].end;14921493/* get the last valid entry to fill the intermediate hole */1494if (!start && !end) {1495for (j = num_of_wbrf_ranges - 1; j > i; j--)1496if (wifi_bands[j].start && wifi_bands[j].end)1497break;14981499/* no valid entry left */1500if (j <= i)1501break;15021503start = wifi_bands[i].start = wifi_bands[j].start;1504end = wifi_bands[i].end = wifi_bands[j].end;1505wifi_bands[j].start = 0;1506wifi_bands[j].end = 0;1507num_of_wbrf_ranges = j;1508}15091510/* eliminate duplicate entries */1511for (j = i + 1; j < num_of_wbrf_ranges; j++) {1512if ((wifi_bands[j].start == start) && (wifi_bands[j].end == end)) {1513wifi_bands[j].start = 0;1514wifi_bands[j].end = 0;1515}1516}1517}15181519/* Send the sorted wifi_bands to PMFW */1520ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);1521/* Try to set the wifi_bands again */1522if (unlikely(ret == -EBUSY)) {1523mdelay(5);1524ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);1525}15261527return ret;1528}15291530/**1531* smu_wbrf_event_handler - handle notify events1532*1533* @nb: notifier block1534* @action: event type1535* @_arg: event data1536*1537* Calls relevant amdgpu function in response to wbrf event1538* notification from kernel.1539*/1540static int smu_wbrf_event_handler(struct notifier_block *nb,1541unsigned long action, void *_arg)1542{1543struct smu_context *smu = container_of(nb, struct smu_context, wbrf_notifier);15441545switch (action) {1546case WBRF_CHANGED:1547schedule_delayed_work(&smu->wbrf_delayed_work,1548msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));1549break;1550default:1551return NOTIFY_DONE;1552}15531554return NOTIFY_OK;1555}15561557/**1558* smu_wbrf_delayed_work_handler - callback on delayed work timer expired1559*1560* @work: struct work_struct pointer1561*1562* Flood is over and driver will consume the latest exclusion ranges.1563*/1564static void smu_wbrf_delayed_work_handler(struct work_struct *work)1565{1566struct smu_context *smu = container_of(work, struct smu_context, wbrf_delayed_work.work);15671568smu_wbrf_handle_exclusion_ranges(smu);1569}15701571/**1572* smu_wbrf_support_check - check wbrf support1573*1574* @smu: smu_context pointer1575*1576* Verifies the ACPI interface whether wbrf is supported.1577*/1578static void smu_wbrf_support_check(struct smu_context *smu)1579{1580struct amdgpu_device *adev = smu->adev;15811582smu->wbrf_supported = smu_is_asic_wbrf_supported(smu) && amdgpu_wbrf &&1583acpi_amd_wbrf_supported_consumer(adev->dev);15841585if (smu->wbrf_supported)1586dev_info(adev->dev, "RF interference mitigation is supported\n");1587}15881589/**1590* smu_wbrf_init - init driver wbrf support1591*1592* @smu: smu_context pointer1593*1594* Verifies the AMD ACPI interfaces and registers with the wbrf1595* notifier chain if wbrf feature is supported.1596* Returns 0 on success, error on failure.1597*/1598static int smu_wbrf_init(struct smu_context *smu)1599{1600int ret;16011602if (!smu->wbrf_supported)1603return 0;16041605INIT_DELAYED_WORK(&smu->wbrf_delayed_work, smu_wbrf_delayed_work_handler);16061607smu->wbrf_notifier.notifier_call = smu_wbrf_event_handler;1608ret = amd_wbrf_register_notifier(&smu->wbrf_notifier);1609if (ret)1610return ret;16111612/*1613* Some wifiband exclusion ranges may be already there1614* before our driver loaded. To make sure our driver1615* is awared of those exclusion ranges.1616*/1617schedule_delayed_work(&smu->wbrf_delayed_work,1618msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));16191620return 0;1621}16221623/**1624* smu_wbrf_fini - tear down driver wbrf support1625*1626* @smu: smu_context pointer1627*1628* Unregisters with the wbrf notifier chain.1629*/1630static void smu_wbrf_fini(struct smu_context *smu)1631{1632if (!smu->wbrf_supported)1633return;16341635amd_wbrf_unregister_notifier(&smu->wbrf_notifier);16361637cancel_delayed_work_sync(&smu->wbrf_delayed_work);1638}16391640static int smu_smc_hw_setup(struct smu_context *smu)1641{1642struct smu_feature *feature = &smu->smu_feature;1643struct amdgpu_device *adev = smu->adev;1644uint8_t pcie_gen = 0, pcie_width = 0;1645uint64_t features_supported;1646int ret = 0;16471648switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {1649case IP_VERSION(11, 0, 7):1650case IP_VERSION(11, 0, 11):1651case IP_VERSION(11, 5, 0):1652case IP_VERSION(11, 5, 2):1653case IP_VERSION(11, 0, 12):1654if (adev->in_suspend && smu_is_dpm_running(smu)) {1655dev_info(adev->dev, "dpm has been enabled\n");1656ret = smu_system_features_control(smu, true);1657if (ret)1658dev_err(adev->dev, "Failed system features control!\n");1659return ret;1660}1661break;1662default:1663break;1664}16651666ret = smu_init_display_count(smu, 0);1667if (ret) {1668dev_info(adev->dev, "Failed to pre-set display count as 0!\n");1669return ret;1670}16711672ret = smu_set_driver_table_location(smu);1673if (ret) {1674dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");1675return ret;1676}16771678/*1679* Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.1680*/1681ret = smu_set_tool_table_location(smu);1682if (ret) {1683dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");1684return ret;1685}16861687/*1688* Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify1689* pool location.1690*/1691ret = smu_notify_memory_pool_location(smu);1692if (ret) {1693dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");1694return ret;1695}16961697/*1698* It is assumed the pptable used before runpm is same as1699* the one used afterwards. Thus, we can reuse the stored1700* copy and do not need to resetup the pptable again.1701*/1702if (!adev->in_runpm) {1703ret = smu_setup_pptable(smu);1704if (ret) {1705dev_err(adev->dev, "Failed to setup pptable!\n");1706return ret;1707}1708}17091710/* smu_dump_pptable(smu); */17111712/*1713* With SCPM enabled, PSP is responsible for the PPTable transferring1714* (to SMU). Driver involvement is not needed and permitted.1715*/1716if (!adev->scpm_enabled) {1717/*1718* Copy pptable bo in the vram to smc with SMU MSGs such as1719* SetDriverDramAddr and TransferTableDram2Smu.1720*/1721ret = smu_write_pptable(smu);1722if (ret) {1723dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");1724return ret;1725}1726}17271728/* issue Run*Btc msg */1729ret = smu_run_btc(smu);1730if (ret)1731return ret;17321733/* Enable UclkShadow on wbrf supported */1734if (smu->wbrf_supported) {1735ret = smu_enable_uclk_shadow(smu, true);1736if (ret) {1737dev_err(adev->dev, "Failed to enable UclkShadow feature to support wbrf!\n");1738return ret;1739}1740}17411742/*1743* With SCPM enabled, these actions(and relevant messages) are1744* not needed and permitted.1745*/1746if (!adev->scpm_enabled) {1747ret = smu_feature_set_allowed_mask(smu);1748if (ret) {1749dev_err(adev->dev, "Failed to set driver allowed features mask!\n");1750return ret;1751}1752}17531754if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5)1755pcie_gen = 4;1756else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)1757pcie_gen = 3;1758else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)1759pcie_gen = 2;1760else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)1761pcie_gen = 1;1762else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)1763pcie_gen = 0;17641765/* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM11766* Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN41767* Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x321768*/1769if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X32)1770pcie_width = 7;1771else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)1772pcie_width = 6;1773else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)1774pcie_width = 5;1775else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)1776pcie_width = 4;1777else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)1778pcie_width = 3;1779else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)1780pcie_width = 2;1781else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)1782pcie_width = 1;1783ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);1784if (ret) {1785dev_err(adev->dev, "Attempt to override pcie params failed!\n");1786return ret;1787}17881789ret = smu_system_features_control(smu, true);1790if (ret) {1791dev_err(adev->dev, "Failed to enable requested dpm features!\n");1792return ret;1793}17941795smu_init_xgmi_plpd_mode(smu);17961797ret = smu_feature_get_enabled_mask(smu, &features_supported);1798if (ret) {1799dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");1800return ret;1801}1802bitmap_copy(feature->supported,1803(unsigned long *)&features_supported,1804feature->feature_num);18051806if (!smu_is_dpm_running(smu))1807dev_info(adev->dev, "dpm has been disabled\n");18081809/*1810* Set initialized values (get from vbios) to dpm tables context such as1811* gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each1812* type of clks.1813*/1814ret = smu_set_default_dpm_table(smu);1815if (ret) {1816dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");1817return ret;1818}18191820ret = smu_get_thermal_temperature_range(smu);1821if (ret) {1822dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");1823return ret;1824}18251826ret = smu_enable_thermal_alert(smu);1827if (ret) {1828dev_err(adev->dev, "Failed to enable thermal alert!\n");1829return ret;1830}18311832ret = smu_notify_display_change(smu);1833if (ret) {1834dev_err(adev->dev, "Failed to notify display change!\n");1835return ret;1836}18371838/*1839* Set min deep sleep dce fclk with bootup value from vbios via1840* SetMinDeepSleepDcefclk MSG.1841*/1842ret = smu_set_min_dcef_deep_sleep(smu,1843smu->smu_table.boot_values.dcefclk / 100);1844if (ret) {1845dev_err(adev->dev, "Error setting min deepsleep dcefclk\n");1846return ret;1847}18481849/* Init wbrf support. Properly setup the notifier */1850ret = smu_wbrf_init(smu);1851if (ret)1852dev_err(adev->dev, "Error during wbrf init call\n");18531854return ret;1855}18561857static int smu_start_smc_engine(struct smu_context *smu)1858{1859struct amdgpu_device *adev = smu->adev;1860int ret = 0;18611862if (amdgpu_virt_xgmi_migrate_enabled(adev))1863smu_update_gpu_addresses(smu);18641865smu->smc_fw_state = SMU_FW_INIT;18661867if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {1868if (amdgpu_ip_version(adev, MP1_HWIP, 0) < IP_VERSION(11, 0, 0)) {1869if (smu->ppt_funcs->load_microcode) {1870ret = smu->ppt_funcs->load_microcode(smu);1871if (ret)1872return ret;1873}1874}1875}18761877if (smu->ppt_funcs->check_fw_status) {1878ret = smu->ppt_funcs->check_fw_status(smu);1879if (ret) {1880dev_err(adev->dev, "SMC is not ready\n");1881return ret;1882}1883}18841885/*1886* Send msg GetDriverIfVersion to check if the return value is equal1887* with DRIVER_IF_VERSION of smc header.1888*/1889ret = smu_check_fw_version(smu);1890if (ret)1891return ret;18921893return ret;1894}18951896static int smu_hw_init(struct amdgpu_ip_block *ip_block)1897{1898int i, ret;1899struct amdgpu_device *adev = ip_block->adev;1900struct smu_context *smu = adev->powerplay.pp_handle;19011902if (amdgpu_sriov_multi_vf_mode(adev)) {1903smu->pm_enabled = false;1904return 0;1905}19061907ret = smu_start_smc_engine(smu);1908if (ret) {1909dev_err(adev->dev, "SMC engine is not correctly up!\n");1910return ret;1911}19121913/*1914* Check whether wbrf is supported. This needs to be done1915* before SMU setup starts since part of SMU configuration1916* relies on this.1917*/1918smu_wbrf_support_check(smu);19191920if (smu->is_apu) {1921ret = smu_set_gfx_imu_enable(smu);1922if (ret)1923return ret;1924for (i = 0; i < adev->vcn.num_vcn_inst; i++)1925smu_dpm_set_vcn_enable(smu, true, i);1926smu_dpm_set_jpeg_enable(smu, true);1927smu_dpm_set_umsch_mm_enable(smu, true);1928smu_set_mall_enable(smu);1929smu_set_gfx_cgpg(smu, true);1930}19311932if (!smu->pm_enabled)1933return 0;19341935ret = smu_get_driver_allowed_feature_mask(smu);1936if (ret)1937return ret;19381939ret = smu_smc_hw_setup(smu);1940if (ret) {1941dev_err(adev->dev, "Failed to setup smc hw!\n");1942return ret;1943}19441945/*1946* Move maximum sustainable clock retrieving here considering1947* 1. It is not needed on resume(from S3).1948* 2. DAL settings come between .hw_init and .late_init of SMU.1949* And DAL needs to know the maximum sustainable clocks. Thus1950* it cannot be put in .late_init().1951*/1952ret = smu_init_max_sustainable_clocks(smu);1953if (ret) {1954dev_err(adev->dev, "Failed to init max sustainable clocks!\n");1955return ret;1956}19571958adev->pm.dpm_enabled = true;19591960dev_info(adev->dev, "SMU is initialized successfully!\n");19611962return 0;1963}19641965static int smu_disable_dpms(struct smu_context *smu)1966{1967struct amdgpu_device *adev = smu->adev;1968int ret = 0;1969bool use_baco = !smu->is_apu &&1970((amdgpu_in_reset(adev) &&1971(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||1972((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));19731974/*1975* For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others)1976* properly on suspend/reset/unload. Driver involvement may cause some unexpected issues.1977*/1978switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {1979case IP_VERSION(13, 0, 0):1980case IP_VERSION(13, 0, 7):1981case IP_VERSION(13, 0, 10):1982case IP_VERSION(14, 0, 2):1983case IP_VERSION(14, 0, 3):1984return 0;1985default:1986break;1987}19881989/*1990* For custom pptable uploading, skip the DPM features1991* disable process on Navi1x ASICs.1992* - As the gfx related features are under control of1993* RLC on those ASICs. RLC reinitialization will be1994* needed to reenable them. That will cost much more1995* efforts.1996*1997* - SMU firmware can handle the DPM reenablement1998* properly.1999*/2000if (smu->uploading_custom_pp_table) {2001switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {2002case IP_VERSION(11, 0, 0):2003case IP_VERSION(11, 0, 5):2004case IP_VERSION(11, 0, 9):2005case IP_VERSION(11, 0, 7):2006case IP_VERSION(11, 0, 11):2007case IP_VERSION(11, 5, 0):2008case IP_VERSION(11, 5, 2):2009case IP_VERSION(11, 0, 12):2010case IP_VERSION(11, 0, 13):2011return 0;2012default:2013break;2014}2015}20162017/*2018* For Sienna_Cichlid, PMFW will handle the features disablement properly2019* on BACO in. Driver involvement is unnecessary.2020*/2021if (use_baco) {2022switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {2023case IP_VERSION(11, 0, 7):2024case IP_VERSION(11, 0, 0):2025case IP_VERSION(11, 0, 5):2026case IP_VERSION(11, 0, 9):2027case IP_VERSION(13, 0, 7):2028return 0;2029default:2030break;2031}2032}20332034/*2035* For GFX11 and subsequent APUs, PMFW will handle the features disablement properly2036* for gpu reset and S0i3 cases. Driver involvement is unnecessary.2037*/2038if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) >= 11 &&2039smu->is_apu && (amdgpu_in_reset(adev) || adev->in_s0ix))2040return 0;20412042/*2043* For gpu reset, runpm and hibernation through BACO,2044* BACO feature has to be kept enabled.2045*/2046if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {2047ret = smu_disable_all_features_with_exception(smu,2048SMU_FEATURE_BACO_BIT);2049if (ret)2050dev_err(adev->dev, "Failed to disable smu features except BACO.\n");2051} else {2052/* DisableAllSmuFeatures message is not permitted with SCPM enabled */2053if (!adev->scpm_enabled) {2054ret = smu_system_features_control(smu, false);2055if (ret)2056dev_err(adev->dev, "Failed to disable smu features.\n");2057}2058}20592060/* Notify SMU RLC is going to be off, stop RLC and SMU interaction.2061* otherwise SMU will hang while interacting with RLC if RLC is halted2062* this is a WA for Vangogh asic which fix the SMU hang issue.2063*/2064ret = smu_notify_rlc_state(smu, false);2065if (ret) {2066dev_err(adev->dev, "Fail to notify rlc status!\n");2067return ret;2068}20692070if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 2) &&2071!((adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs) &&2072!amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop)2073adev->gfx.rlc.funcs->stop(adev);20742075return ret;2076}20772078static int smu_smc_hw_cleanup(struct smu_context *smu)2079{2080struct amdgpu_device *adev = smu->adev;2081int ret = 0;20822083smu_wbrf_fini(smu);20842085cancel_work_sync(&smu->throttling_logging_work);2086cancel_work_sync(&smu->interrupt_work);20872088ret = smu_disable_thermal_alert(smu);2089if (ret) {2090dev_err(adev->dev, "Fail to disable thermal alert!\n");2091return ret;2092}20932094cancel_delayed_work_sync(&smu->swctf_delayed_work);20952096ret = smu_disable_dpms(smu);2097if (ret) {2098dev_err(adev->dev, "Fail to disable dpm features!\n");2099return ret;2100}21012102return 0;2103}21042105static int smu_reset_mp1_state(struct smu_context *smu)2106{2107struct amdgpu_device *adev = smu->adev;2108int ret = 0;21092110if ((!adev->in_runpm) && (!adev->in_suspend) &&2111(!amdgpu_in_reset(adev)) && amdgpu_ip_version(adev, MP1_HWIP, 0) ==2112IP_VERSION(13, 0, 10) &&2113!amdgpu_device_has_display_hardware(adev))2114ret = smu_set_mp1_state(smu, PP_MP1_STATE_UNLOAD);21152116return ret;2117}21182119static int smu_hw_fini(struct amdgpu_ip_block *ip_block)2120{2121struct amdgpu_device *adev = ip_block->adev;2122struct smu_context *smu = adev->powerplay.pp_handle;2123int i, ret;21242125if (amdgpu_sriov_multi_vf_mode(adev))2126return 0;21272128for (i = 0; i < adev->vcn.num_vcn_inst; i++) {2129smu_dpm_set_vcn_enable(smu, false, i);2130adev->vcn.inst[i].cur_state = AMD_PG_STATE_GATE;2131}2132smu_dpm_set_jpeg_enable(smu, false);2133adev->jpeg.cur_state = AMD_PG_STATE_GATE;2134smu_dpm_set_umsch_mm_enable(smu, false);21352136if (!smu->pm_enabled)2137return 0;21382139adev->pm.dpm_enabled = false;21402141ret = smu_smc_hw_cleanup(smu);2142if (ret)2143return ret;21442145ret = smu_reset_mp1_state(smu);2146if (ret)2147return ret;21482149return 0;2150}21512152static void smu_late_fini(struct amdgpu_ip_block *ip_block)2153{2154struct amdgpu_device *adev = ip_block->adev;2155struct smu_context *smu = adev->powerplay.pp_handle;21562157kfree(smu);2158}21592160static int smu_reset(struct smu_context *smu)2161{2162struct amdgpu_device *adev = smu->adev;2163struct amdgpu_ip_block *ip_block;2164int ret;21652166ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_SMC);2167if (!ip_block)2168return -EINVAL;21692170ret = smu_hw_fini(ip_block);2171if (ret)2172return ret;21732174ret = smu_hw_init(ip_block);2175if (ret)2176return ret;21772178ret = smu_late_init(ip_block);2179if (ret)2180return ret;21812182return 0;2183}21842185static int smu_suspend(struct amdgpu_ip_block *ip_block)2186{2187struct amdgpu_device *adev = ip_block->adev;2188struct smu_context *smu = adev->powerplay.pp_handle;2189int ret;2190uint64_t count;21912192if (amdgpu_sriov_multi_vf_mode(adev))2193return 0;21942195if (!smu->pm_enabled)2196return 0;21972198adev->pm.dpm_enabled = false;21992200ret = smu_smc_hw_cleanup(smu);2201if (ret)2202return ret;22032204smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);22052206smu_set_gfx_cgpg(smu, false);22072208/*2209* pwfw resets entrycount when device is suspended, so we save the2210* last value to be used when we resume to keep it consistent2211*/2212ret = smu_get_entrycount_gfxoff(smu, &count);2213if (!ret)2214adev->gfx.gfx_off_entrycount = count;22152216/* clear this on suspend so it will get reprogrammed on resume */2217smu->workload_mask = 0;22182219return 0;2220}22212222static int smu_resume(struct amdgpu_ip_block *ip_block)2223{2224int ret;2225struct amdgpu_device *adev = ip_block->adev;2226struct smu_context *smu = adev->powerplay.pp_handle;2227struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);22282229if (amdgpu_sriov_multi_vf_mode(adev))2230return 0;22312232if (!smu->pm_enabled)2233return 0;22342235dev_info(adev->dev, "SMU is resuming...\n");22362237ret = smu_start_smc_engine(smu);2238if (ret) {2239dev_err(adev->dev, "SMC engine is not correctly up!\n");2240return ret;2241}22422243ret = smu_smc_hw_setup(smu);2244if (ret) {2245dev_err(adev->dev, "Failed to setup smc hw!\n");2246return ret;2247}22482249ret = smu_set_gfx_imu_enable(smu);2250if (ret)2251return ret;22522253smu_set_gfx_cgpg(smu, true);22542255smu->disable_uclk_switch = 0;22562257adev->pm.dpm_enabled = true;22582259if (smu->current_power_limit) {2260ret = smu_set_power_limit(smu, smu->current_power_limit);2261if (ret && ret != -EOPNOTSUPP)2262return ret;2263}22642265if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL && smu->od_enabled) {2266ret = smu_od_edit_dpm_table(smu, PP_OD_COMMIT_DPM_TABLE, NULL, 0);2267if (ret)2268return ret;2269}22702271dev_info(adev->dev, "SMU is resumed successfully!\n");22722273return 0;2274}22752276static int smu_display_configuration_change(void *handle,2277const struct amd_pp_display_configuration *display_config)2278{2279struct smu_context *smu = handle;22802281if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2282return -EOPNOTSUPP;22832284if (!display_config)2285return -EINVAL;22862287smu_set_min_dcef_deep_sleep(smu,2288display_config->min_dcef_deep_sleep_set_clk / 100);22892290return 0;2291}22922293static int smu_set_clockgating_state(struct amdgpu_ip_block *ip_block,2294enum amd_clockgating_state state)2295{2296return 0;2297}22982299static int smu_set_powergating_state(struct amdgpu_ip_block *ip_block,2300enum amd_powergating_state state)2301{2302return 0;2303}23042305static int smu_enable_umd_pstate(void *handle,2306enum amd_dpm_forced_level *level)2307{2308uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |2309AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |2310AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |2311AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;23122313struct smu_context *smu = (struct smu_context*)(handle);2314struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);23152316if (!smu->is_apu && !smu_dpm_ctx->dpm_context)2317return -EINVAL;23182319if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {2320/* enter umd pstate, save current level, disable gfx cg*/2321if (*level & profile_mode_mask) {2322smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;2323smu_gpo_control(smu, false);2324smu_gfx_ulv_control(smu, false);2325smu_deep_sleep_control(smu, false);2326amdgpu_asic_update_umd_stable_pstate(smu->adev, true);2327}2328} else {2329/* exit umd pstate, restore level, enable gfx cg*/2330if (!(*level & profile_mode_mask)) {2331if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)2332*level = smu_dpm_ctx->saved_dpm_level;2333amdgpu_asic_update_umd_stable_pstate(smu->adev, false);2334smu_deep_sleep_control(smu, true);2335smu_gfx_ulv_control(smu, true);2336smu_gpo_control(smu, true);2337}2338}23392340return 0;2341}23422343static int smu_bump_power_profile_mode(struct smu_context *smu,2344long *custom_params,2345u32 custom_params_max_idx)2346{2347u32 workload_mask = 0;2348int i, ret = 0;23492350for (i = 0; i < PP_SMC_POWER_PROFILE_COUNT; i++) {2351if (smu->workload_refcount[i])2352workload_mask |= 1 << i;2353}23542355if (smu->workload_mask == workload_mask)2356return 0;23572358if (smu->ppt_funcs->set_power_profile_mode)2359ret = smu->ppt_funcs->set_power_profile_mode(smu, workload_mask,2360custom_params,2361custom_params_max_idx);23622363if (!ret)2364smu->workload_mask = workload_mask;23652366return ret;2367}23682369static void smu_power_profile_mode_get(struct smu_context *smu,2370enum PP_SMC_POWER_PROFILE profile_mode)2371{2372smu->workload_refcount[profile_mode]++;2373}23742375static void smu_power_profile_mode_put(struct smu_context *smu,2376enum PP_SMC_POWER_PROFILE profile_mode)2377{2378if (smu->workload_refcount[profile_mode])2379smu->workload_refcount[profile_mode]--;2380}23812382static int smu_adjust_power_state_dynamic(struct smu_context *smu,2383enum amd_dpm_forced_level level,2384bool skip_display_settings)2385{2386int ret = 0;2387struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);23882389if (!skip_display_settings) {2390ret = smu_display_config_changed(smu);2391if (ret) {2392dev_err(smu->adev->dev, "Failed to change display config!");2393return ret;2394}2395}23962397ret = smu_apply_clocks_adjust_rules(smu);2398if (ret) {2399dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");2400return ret;2401}24022403if (!skip_display_settings) {2404ret = smu_notify_smc_display_config(smu);2405if (ret) {2406dev_err(smu->adev->dev, "Failed to notify smc display config!");2407return ret;2408}2409}24102411if (smu_dpm_ctx->dpm_level != level) {2412ret = smu_asic_set_performance_level(smu, level);2413if (ret) {2414if (ret == -EOPNOTSUPP)2415dev_info(smu->adev->dev, "set performance level %d not supported",2416level);2417else2418dev_err(smu->adev->dev, "Failed to set performance level %d",2419level);2420return ret;2421}24222423/* update the saved copy */2424smu_dpm_ctx->dpm_level = level;2425}24262427if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&2428smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)2429smu_bump_power_profile_mode(smu, NULL, 0);24302431return ret;2432}24332434static int smu_handle_task(struct smu_context *smu,2435enum amd_dpm_forced_level level,2436enum amd_pp_task task_id)2437{2438int ret = 0;24392440if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2441return -EOPNOTSUPP;24422443switch (task_id) {2444case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:2445ret = smu_pre_display_config_changed(smu);2446if (ret)2447return ret;2448ret = smu_adjust_power_state_dynamic(smu, level, false);2449break;2450case AMD_PP_TASK_COMPLETE_INIT:2451ret = smu_adjust_power_state_dynamic(smu, level, true);2452break;2453case AMD_PP_TASK_READJUST_POWER_STATE:2454ret = smu_adjust_power_state_dynamic(smu, level, true);2455break;2456default:2457break;2458}24592460return ret;2461}24622463static int smu_handle_dpm_task(void *handle,2464enum amd_pp_task task_id,2465enum amd_pm_state_type *user_state)2466{2467struct smu_context *smu = handle;2468struct smu_dpm_context *smu_dpm = &smu->smu_dpm;24692470return smu_handle_task(smu, smu_dpm->dpm_level, task_id);24712472}24732474static int smu_switch_power_profile(void *handle,2475enum PP_SMC_POWER_PROFILE type,2476bool enable)2477{2478struct smu_context *smu = handle;2479struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2480int ret;24812482if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2483return -EOPNOTSUPP;24842485if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))2486return -EINVAL;24872488if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&2489smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {2490if (enable)2491smu_power_profile_mode_get(smu, type);2492else2493smu_power_profile_mode_put(smu, type);2494/* don't switch the active workload when paused */2495if (smu->pause_workload)2496ret = 0;2497else2498ret = smu_bump_power_profile_mode(smu, NULL, 0);2499if (ret) {2500if (enable)2501smu_power_profile_mode_put(smu, type);2502else2503smu_power_profile_mode_get(smu, type);2504return ret;2505}2506}25072508return 0;2509}25102511static int smu_pause_power_profile(void *handle,2512bool pause)2513{2514struct smu_context *smu = handle;2515struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2516u32 workload_mask = 1 << PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;2517int ret;25182519if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2520return -EOPNOTSUPP;25212522if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&2523smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {2524smu->pause_workload = pause;25252526/* force to bootup default profile */2527if (smu->pause_workload && smu->ppt_funcs->set_power_profile_mode)2528ret = smu->ppt_funcs->set_power_profile_mode(smu,2529workload_mask,2530NULL,25310);2532else2533ret = smu_bump_power_profile_mode(smu, NULL, 0);2534return ret;2535}25362537return 0;2538}25392540static enum amd_dpm_forced_level smu_get_performance_level(void *handle)2541{2542struct smu_context *smu = handle;2543struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);25442545if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2546return -EOPNOTSUPP;25472548if (!smu->is_apu && !smu_dpm_ctx->dpm_context)2549return -EINVAL;25502551return smu_dpm_ctx->dpm_level;2552}25532554static int smu_force_performance_level(void *handle,2555enum amd_dpm_forced_level level)2556{2557struct smu_context *smu = handle;2558struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2559int ret = 0;25602561if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2562return -EOPNOTSUPP;25632564if (!smu->is_apu && !smu_dpm_ctx->dpm_context)2565return -EINVAL;25662567ret = smu_enable_umd_pstate(smu, &level);2568if (ret)2569return ret;25702571ret = smu_handle_task(smu, level,2572AMD_PP_TASK_READJUST_POWER_STATE);25732574/* reset user dpm clock state */2575if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {2576memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));2577smu->user_dpm_profile.clk_dependency = 0;2578}25792580return ret;2581}25822583static int smu_set_display_count(void *handle, uint32_t count)2584{2585struct smu_context *smu = handle;25862587if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2588return -EOPNOTSUPP;25892590return smu_init_display_count(smu, count);2591}25922593static int smu_force_smuclk_levels(struct smu_context *smu,2594enum smu_clk_type clk_type,2595uint32_t mask)2596{2597struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2598int ret = 0;25992600if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2601return -EOPNOTSUPP;26022603if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {2604dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");2605return -EINVAL;2606}26072608if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {2609ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);2610if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {2611smu->user_dpm_profile.clk_mask[clk_type] = mask;2612smu_set_user_clk_dependencies(smu, clk_type);2613}2614}26152616return ret;2617}26182619static int smu_force_ppclk_levels(void *handle,2620enum pp_clock_type type,2621uint32_t mask)2622{2623struct smu_context *smu = handle;2624enum smu_clk_type clk_type;26252626switch (type) {2627case PP_SCLK:2628clk_type = SMU_SCLK; break;2629case PP_MCLK:2630clk_type = SMU_MCLK; break;2631case PP_PCIE:2632clk_type = SMU_PCIE; break;2633case PP_SOCCLK:2634clk_type = SMU_SOCCLK; break;2635case PP_FCLK:2636clk_type = SMU_FCLK; break;2637case PP_DCEFCLK:2638clk_type = SMU_DCEFCLK; break;2639case PP_VCLK:2640clk_type = SMU_VCLK; break;2641case PP_VCLK1:2642clk_type = SMU_VCLK1; break;2643case PP_DCLK:2644clk_type = SMU_DCLK; break;2645case PP_DCLK1:2646clk_type = SMU_DCLK1; break;2647case OD_SCLK:2648clk_type = SMU_OD_SCLK; break;2649case OD_MCLK:2650clk_type = SMU_OD_MCLK; break;2651case OD_VDDC_CURVE:2652clk_type = SMU_OD_VDDC_CURVE; break;2653case OD_RANGE:2654clk_type = SMU_OD_RANGE; break;2655default:2656return -EINVAL;2657}26582659return smu_force_smuclk_levels(smu, clk_type, mask);2660}26612662/*2663* On system suspending or resetting, the dpm_enabled2664* flag will be cleared. So that those SMU services which2665* are not supported will be gated.2666* However, the mp1 state setting should still be granted2667* even if the dpm_enabled cleared.2668*/2669static int smu_set_mp1_state(void *handle,2670enum pp_mp1_state mp1_state)2671{2672struct smu_context *smu = handle;2673int ret = 0;26742675if (!smu->pm_enabled)2676return -EOPNOTSUPP;26772678if (smu->ppt_funcs &&2679smu->ppt_funcs->set_mp1_state)2680ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);26812682return ret;2683}26842685static int smu_set_df_cstate(void *handle,2686enum pp_df_cstate state)2687{2688struct smu_context *smu = handle;2689int ret = 0;26902691if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2692return -EOPNOTSUPP;26932694if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)2695return 0;26962697ret = smu->ppt_funcs->set_df_cstate(smu, state);2698if (ret)2699dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");27002701return ret;2702}27032704int smu_write_watermarks_table(struct smu_context *smu)2705{2706if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2707return -EOPNOTSUPP;27082709return smu_set_watermarks_table(smu, NULL);2710}27112712static int smu_set_watermarks_for_clock_ranges(void *handle,2713struct pp_smu_wm_range_sets *clock_ranges)2714{2715struct smu_context *smu = handle;27162717if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2718return -EOPNOTSUPP;27192720if (smu->disable_watermark)2721return 0;27222723return smu_set_watermarks_table(smu, clock_ranges);2724}27252726int smu_set_ac_dc(struct smu_context *smu)2727{2728int ret = 0;27292730if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2731return -EOPNOTSUPP;27322733/* controlled by firmware */2734if (smu->dc_controlled_by_gpio)2735return 0;27362737ret = smu_set_power_source(smu,2738smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :2739SMU_POWER_SOURCE_DC);2740if (ret)2741dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",2742smu->adev->pm.ac_power ? "AC" : "DC");27432744return ret;2745}27462747const struct amd_ip_funcs smu_ip_funcs = {2748.name = "smu",2749.early_init = smu_early_init,2750.late_init = smu_late_init,2751.sw_init = smu_sw_init,2752.sw_fini = smu_sw_fini,2753.hw_init = smu_hw_init,2754.hw_fini = smu_hw_fini,2755.late_fini = smu_late_fini,2756.suspend = smu_suspend,2757.resume = smu_resume,2758.is_idle = NULL,2759.check_soft_reset = NULL,2760.wait_for_idle = NULL,2761.soft_reset = NULL,2762.set_clockgating_state = smu_set_clockgating_state,2763.set_powergating_state = smu_set_powergating_state,2764};27652766const struct amdgpu_ip_block_version smu_v11_0_ip_block = {2767.type = AMD_IP_BLOCK_TYPE_SMC,2768.major = 11,2769.minor = 0,2770.rev = 0,2771.funcs = &smu_ip_funcs,2772};27732774const struct amdgpu_ip_block_version smu_v12_0_ip_block = {2775.type = AMD_IP_BLOCK_TYPE_SMC,2776.major = 12,2777.minor = 0,2778.rev = 0,2779.funcs = &smu_ip_funcs,2780};27812782const struct amdgpu_ip_block_version smu_v13_0_ip_block = {2783.type = AMD_IP_BLOCK_TYPE_SMC,2784.major = 13,2785.minor = 0,2786.rev = 0,2787.funcs = &smu_ip_funcs,2788};27892790const struct amdgpu_ip_block_version smu_v14_0_ip_block = {2791.type = AMD_IP_BLOCK_TYPE_SMC,2792.major = 14,2793.minor = 0,2794.rev = 0,2795.funcs = &smu_ip_funcs,2796};27972798static int smu_load_microcode(void *handle)2799{2800struct smu_context *smu = handle;2801struct amdgpu_device *adev = smu->adev;2802int ret = 0;28032804if (!smu->pm_enabled)2805return -EOPNOTSUPP;28062807/* This should be used for non PSP loading */2808if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)2809return 0;28102811if (smu->ppt_funcs->load_microcode) {2812ret = smu->ppt_funcs->load_microcode(smu);2813if (ret) {2814dev_err(adev->dev, "Load microcode failed\n");2815return ret;2816}2817}28182819if (smu->ppt_funcs->check_fw_status) {2820ret = smu->ppt_funcs->check_fw_status(smu);2821if (ret) {2822dev_err(adev->dev, "SMC is not ready\n");2823return ret;2824}2825}28262827return ret;2828}28292830static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)2831{2832int ret = 0;28332834if (smu->ppt_funcs->set_gfx_cgpg)2835ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);28362837return ret;2838}28392840static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)2841{2842struct smu_context *smu = handle;2843int ret = 0;28442845if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2846return -EOPNOTSUPP;28472848if (!smu->ppt_funcs->set_fan_speed_rpm)2849return -EOPNOTSUPP;28502851if (speed == U32_MAX)2852return -EINVAL;28532854ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);2855if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {2856smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;2857smu->user_dpm_profile.fan_speed_rpm = speed;28582859/* Override custom PWM setting as they cannot co-exist */2860smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;2861smu->user_dpm_profile.fan_speed_pwm = 0;2862}28632864return ret;2865}28662867/**2868* smu_get_power_limit - Request one of the SMU Power Limits2869*2870* @handle: pointer to smu context2871* @limit: requested limit is written back to this variable2872* @pp_limit_level: &pp_power_limit_level which limit of the power to return2873* @pp_power_type: &pp_power_type type of power2874* Return: 0 on success, <0 on error2875*2876*/2877int smu_get_power_limit(void *handle,2878uint32_t *limit,2879enum pp_power_limit_level pp_limit_level,2880enum pp_power_type pp_power_type)2881{2882struct smu_context *smu = handle;2883struct amdgpu_device *adev = smu->adev;2884enum smu_ppt_limit_level limit_level;2885uint32_t limit_type;2886int ret = 0;28872888if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2889return -EOPNOTSUPP;28902891switch (pp_power_type) {2892case PP_PWR_TYPE_SUSTAINED:2893limit_type = SMU_DEFAULT_PPT_LIMIT;2894break;2895case PP_PWR_TYPE_FAST:2896limit_type = SMU_FAST_PPT_LIMIT;2897break;2898default:2899return -EOPNOTSUPP;2900}29012902switch (pp_limit_level) {2903case PP_PWR_LIMIT_CURRENT:2904limit_level = SMU_PPT_LIMIT_CURRENT;2905break;2906case PP_PWR_LIMIT_DEFAULT:2907limit_level = SMU_PPT_LIMIT_DEFAULT;2908break;2909case PP_PWR_LIMIT_MAX:2910limit_level = SMU_PPT_LIMIT_MAX;2911break;2912case PP_PWR_LIMIT_MIN:2913limit_level = SMU_PPT_LIMIT_MIN;2914break;2915default:2916return -EOPNOTSUPP;2917}29182919if (limit_type != SMU_DEFAULT_PPT_LIMIT) {2920if (smu->ppt_funcs->get_ppt_limit)2921ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);2922} else {2923switch (limit_level) {2924case SMU_PPT_LIMIT_CURRENT:2925switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {2926case IP_VERSION(13, 0, 2):2927case IP_VERSION(13, 0, 6):2928case IP_VERSION(13, 0, 12):2929case IP_VERSION(13, 0, 14):2930case IP_VERSION(11, 0, 7):2931case IP_VERSION(11, 0, 11):2932case IP_VERSION(11, 0, 12):2933case IP_VERSION(11, 0, 13):2934ret = smu_get_asic_power_limits(smu,2935&smu->current_power_limit,2936NULL, NULL, NULL);2937break;2938default:2939break;2940}2941*limit = smu->current_power_limit;2942break;2943case SMU_PPT_LIMIT_DEFAULT:2944*limit = smu->default_power_limit;2945break;2946case SMU_PPT_LIMIT_MAX:2947*limit = smu->max_power_limit;2948break;2949case SMU_PPT_LIMIT_MIN:2950*limit = smu->min_power_limit;2951break;2952default:2953return -EINVAL;2954}2955}29562957return ret;2958}29592960static int smu_set_power_limit(void *handle, uint32_t limit)2961{2962struct smu_context *smu = handle;2963uint32_t limit_type = limit >> 24;2964int ret = 0;29652966if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2967return -EOPNOTSUPP;29682969limit &= (1<<24)-1;2970if (limit_type != SMU_DEFAULT_PPT_LIMIT)2971if (smu->ppt_funcs->set_power_limit)2972return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);29732974if ((limit > smu->max_power_limit) || (limit < smu->min_power_limit)) {2975dev_err(smu->adev->dev,2976"New power limit (%d) is out of range [%d,%d]\n",2977limit, smu->min_power_limit, smu->max_power_limit);2978return -EINVAL;2979}29802981if (!limit)2982limit = smu->current_power_limit;29832984if (smu->ppt_funcs->set_power_limit) {2985ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);2986if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))2987smu->user_dpm_profile.power_limit = limit;2988}29892990return ret;2991}29922993static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)2994{2995int ret = 0;29962997if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2998return -EOPNOTSUPP;29993000if (smu->ppt_funcs->print_clk_levels)3001ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);30023003return ret;3004}30053006static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)3007{3008enum smu_clk_type clk_type;30093010switch (type) {3011case PP_SCLK:3012clk_type = SMU_SCLK; break;3013case PP_MCLK:3014clk_type = SMU_MCLK; break;3015case PP_PCIE:3016clk_type = SMU_PCIE; break;3017case PP_SOCCLK:3018clk_type = SMU_SOCCLK; break;3019case PP_FCLK:3020clk_type = SMU_FCLK; break;3021case PP_DCEFCLK:3022clk_type = SMU_DCEFCLK; break;3023case PP_VCLK:3024clk_type = SMU_VCLK; break;3025case PP_VCLK1:3026clk_type = SMU_VCLK1; break;3027case PP_DCLK:3028clk_type = SMU_DCLK; break;3029case PP_DCLK1:3030clk_type = SMU_DCLK1; break;3031case PP_ISPICLK:3032clk_type = SMU_ISPICLK;3033break;3034case PP_ISPXCLK:3035clk_type = SMU_ISPXCLK;3036break;3037case OD_SCLK:3038clk_type = SMU_OD_SCLK; break;3039case OD_MCLK:3040clk_type = SMU_OD_MCLK; break;3041case OD_VDDC_CURVE:3042clk_type = SMU_OD_VDDC_CURVE; break;3043case OD_RANGE:3044clk_type = SMU_OD_RANGE; break;3045case OD_VDDGFX_OFFSET:3046clk_type = SMU_OD_VDDGFX_OFFSET; break;3047case OD_CCLK:3048clk_type = SMU_OD_CCLK; break;3049case OD_FAN_CURVE:3050clk_type = SMU_OD_FAN_CURVE; break;3051case OD_ACOUSTIC_LIMIT:3052clk_type = SMU_OD_ACOUSTIC_LIMIT; break;3053case OD_ACOUSTIC_TARGET:3054clk_type = SMU_OD_ACOUSTIC_TARGET; break;3055case OD_FAN_TARGET_TEMPERATURE:3056clk_type = SMU_OD_FAN_TARGET_TEMPERATURE; break;3057case OD_FAN_MINIMUM_PWM:3058clk_type = SMU_OD_FAN_MINIMUM_PWM; break;3059case OD_FAN_ZERO_RPM_ENABLE:3060clk_type = SMU_OD_FAN_ZERO_RPM_ENABLE; break;3061case OD_FAN_ZERO_RPM_STOP_TEMP:3062clk_type = SMU_OD_FAN_ZERO_RPM_STOP_TEMP; break;3063default:3064clk_type = SMU_CLK_COUNT; break;3065}30663067return clk_type;3068}30693070static int smu_print_ppclk_levels(void *handle,3071enum pp_clock_type type,3072char *buf)3073{3074struct smu_context *smu = handle;3075enum smu_clk_type clk_type;30763077clk_type = smu_convert_to_smuclk(type);3078if (clk_type == SMU_CLK_COUNT)3079return -EINVAL;30803081return smu_print_smuclk_levels(smu, clk_type, buf);3082}30833084static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)3085{3086struct smu_context *smu = handle;3087enum smu_clk_type clk_type;30883089clk_type = smu_convert_to_smuclk(type);3090if (clk_type == SMU_CLK_COUNT)3091return -EINVAL;30923093if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3094return -EOPNOTSUPP;30953096if (!smu->ppt_funcs->emit_clk_levels)3097return -ENOENT;30983099return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);31003101}31023103static int smu_od_edit_dpm_table(void *handle,3104enum PP_OD_DPM_TABLE_COMMAND type,3105long *input, uint32_t size)3106{3107struct smu_context *smu = handle;3108int ret = 0;31093110if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3111return -EOPNOTSUPP;31123113if (smu->ppt_funcs->od_edit_dpm_table) {3114ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);3115}31163117return ret;3118}31193120static int smu_read_sensor(void *handle,3121int sensor,3122void *data,3123int *size_arg)3124{3125struct smu_context *smu = handle;3126struct amdgpu_device *adev = smu->adev;3127struct smu_umd_pstate_table *pstate_table =3128&smu->pstate_table;3129int i, ret = 0;3130uint32_t *size, size_val;31313132if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3133return -EOPNOTSUPP;31343135if (!data || !size_arg)3136return -EINVAL;31373138size_val = *size_arg;3139size = &size_val;31403141if (smu->ppt_funcs->read_sensor)3142if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))3143goto unlock;31443145switch (sensor) {3146case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:3147*((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;3148*size = 4;3149break;3150case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:3151*((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;3152*size = 4;3153break;3154case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:3155*((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100;3156*size = 4;3157break;3158case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:3159*((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100;3160*size = 4;3161break;3162case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:3163ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);3164*size = 8;3165break;3166case AMDGPU_PP_SENSOR_UVD_POWER:3167*(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;3168*size = 4;3169break;3170case AMDGPU_PP_SENSOR_VCE_POWER:3171*(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;3172*size = 4;3173break;3174case AMDGPU_PP_SENSOR_VCN_POWER_STATE:3175*(uint32_t *)data = 0;3176for (i = 0; i < adev->vcn.num_vcn_inst; i++) {3177if (!atomic_read(&smu->smu_power.power_gate.vcn_gated[i])) {3178*(uint32_t *)data = 1;3179break;3180}3181}3182*size = 4;3183break;3184case AMDGPU_PP_SENSOR_MIN_FAN_RPM:3185*(uint32_t *)data = 0;3186*size = 4;3187break;3188default:3189*size = 0;3190ret = -EOPNOTSUPP;3191break;3192}31933194unlock:3195// assign uint32_t to int3196*size_arg = size_val;31973198return ret;3199}32003201static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit)3202{3203int ret = -EOPNOTSUPP;3204struct smu_context *smu = handle;32053206if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit)3207ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit);32083209return ret;3210}32113212static int smu_set_apu_thermal_limit(void *handle, uint32_t limit)3213{3214int ret = -EOPNOTSUPP;3215struct smu_context *smu = handle;32163217if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit)3218ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit);32193220return ret;3221}32223223static int smu_get_power_profile_mode(void *handle, char *buf)3224{3225struct smu_context *smu = handle;32263227if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||3228!smu->ppt_funcs->get_power_profile_mode)3229return -EOPNOTSUPP;3230if (!buf)3231return -EINVAL;32323233return smu->ppt_funcs->get_power_profile_mode(smu, buf);3234}32353236static int smu_set_power_profile_mode(void *handle,3237long *param,3238uint32_t param_size)3239{3240struct smu_context *smu = handle;3241bool custom = false;3242int ret = 0;32433244if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||3245!smu->ppt_funcs->set_power_profile_mode)3246return -EOPNOTSUPP;32473248if (param[param_size] == PP_SMC_POWER_PROFILE_CUSTOM) {3249custom = true;3250/* clear frontend mask so custom changes propogate */3251smu->workload_mask = 0;3252}32533254if ((param[param_size] != smu->power_profile_mode) || custom) {3255/* clear the old user preference */3256smu_power_profile_mode_put(smu, smu->power_profile_mode);3257/* set the new user preference */3258smu_power_profile_mode_get(smu, param[param_size]);3259ret = smu_bump_power_profile_mode(smu,3260custom ? param : NULL,3261custom ? param_size : 0);3262if (ret)3263smu_power_profile_mode_put(smu, param[param_size]);3264else3265/* store the user's preference */3266smu->power_profile_mode = param[param_size];3267}32683269return ret;3270}32713272static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)3273{3274struct smu_context *smu = handle;32753276if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3277return -EOPNOTSUPP;32783279if (!smu->ppt_funcs->get_fan_control_mode)3280return -EOPNOTSUPP;32813282if (!fan_mode)3283return -EINVAL;32843285*fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);32863287return 0;3288}32893290static int smu_set_fan_control_mode(void *handle, u32 value)3291{3292struct smu_context *smu = handle;3293int ret = 0;32943295if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3296return -EOPNOTSUPP;32973298if (!smu->ppt_funcs->set_fan_control_mode)3299return -EOPNOTSUPP;33003301if (value == U32_MAX)3302return -EINVAL;33033304ret = smu->ppt_funcs->set_fan_control_mode(smu, value);3305if (ret)3306goto out;33073308if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {3309smu->user_dpm_profile.fan_mode = value;33103311/* reset user dpm fan speed */3312if (value != AMD_FAN_CTRL_MANUAL) {3313smu->user_dpm_profile.fan_speed_pwm = 0;3314smu->user_dpm_profile.fan_speed_rpm = 0;3315smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);3316}3317}33183319out:3320return ret;3321}33223323static int smu_get_fan_speed_pwm(void *handle, u32 *speed)3324{3325struct smu_context *smu = handle;3326int ret = 0;33273328if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3329return -EOPNOTSUPP;33303331if (!smu->ppt_funcs->get_fan_speed_pwm)3332return -EOPNOTSUPP;33333334if (!speed)3335return -EINVAL;33363337ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);33383339return ret;3340}33413342static int smu_set_fan_speed_pwm(void *handle, u32 speed)3343{3344struct smu_context *smu = handle;3345int ret = 0;33463347if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3348return -EOPNOTSUPP;33493350if (!smu->ppt_funcs->set_fan_speed_pwm)3351return -EOPNOTSUPP;33523353if (speed == U32_MAX)3354return -EINVAL;33553356ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);3357if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {3358smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;3359smu->user_dpm_profile.fan_speed_pwm = speed;33603361/* Override custom RPM setting as they cannot co-exist */3362smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;3363smu->user_dpm_profile.fan_speed_rpm = 0;3364}33653366return ret;3367}33683369static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)3370{3371struct smu_context *smu = handle;3372int ret = 0;33733374if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3375return -EOPNOTSUPP;33763377if (!smu->ppt_funcs->get_fan_speed_rpm)3378return -EOPNOTSUPP;33793380if (!speed)3381return -EINVAL;33823383ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);33843385return ret;3386}33873388static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)3389{3390struct smu_context *smu = handle;33913392if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3393return -EOPNOTSUPP;33943395return smu_set_min_dcef_deep_sleep(smu, clk);3396}33973398static int smu_get_clock_by_type_with_latency(void *handle,3399enum amd_pp_clock_type type,3400struct pp_clock_levels_with_latency *clocks)3401{3402struct smu_context *smu = handle;3403enum smu_clk_type clk_type;3404int ret = 0;34053406if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3407return -EOPNOTSUPP;34083409if (smu->ppt_funcs->get_clock_by_type_with_latency) {3410switch (type) {3411case amd_pp_sys_clock:3412clk_type = SMU_GFXCLK;3413break;3414case amd_pp_mem_clock:3415clk_type = SMU_MCLK;3416break;3417case amd_pp_dcef_clock:3418clk_type = SMU_DCEFCLK;3419break;3420case amd_pp_disp_clock:3421clk_type = SMU_DISPCLK;3422break;3423default:3424dev_err(smu->adev->dev, "Invalid clock type!\n");3425return -EINVAL;3426}34273428ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);3429}34303431return ret;3432}34333434static int smu_display_clock_voltage_request(void *handle,3435struct pp_display_clock_request *clock_req)3436{3437struct smu_context *smu = handle;3438int ret = 0;34393440if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3441return -EOPNOTSUPP;34423443if (smu->ppt_funcs->display_clock_voltage_request)3444ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);34453446return ret;3447}344834493450static int smu_display_disable_memory_clock_switch(void *handle,3451bool disable_memory_clock_switch)3452{3453struct smu_context *smu = handle;3454int ret = -EINVAL;34553456if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3457return -EOPNOTSUPP;34583459if (smu->ppt_funcs->display_disable_memory_clock_switch)3460ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);34613462return ret;3463}34643465static int smu_set_xgmi_pstate(void *handle,3466uint32_t pstate)3467{3468struct smu_context *smu = handle;3469int ret = 0;34703471if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3472return -EOPNOTSUPP;34733474if (smu->ppt_funcs->set_xgmi_pstate)3475ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);34763477if (ret)3478dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");34793480return ret;3481}34823483static int smu_get_baco_capability(void *handle)3484{3485struct smu_context *smu = handle;34863487if (!smu->pm_enabled)3488return false;34893490if (!smu->ppt_funcs || !smu->ppt_funcs->get_bamaco_support)3491return false;34923493return smu->ppt_funcs->get_bamaco_support(smu);3494}34953496static int smu_baco_set_state(void *handle, int state)3497{3498struct smu_context *smu = handle;3499int ret = 0;35003501if (!smu->pm_enabled)3502return -EOPNOTSUPP;35033504if (state == 0) {3505if (smu->ppt_funcs->baco_exit)3506ret = smu->ppt_funcs->baco_exit(smu);3507} else if (state == 1) {3508if (smu->ppt_funcs->baco_enter)3509ret = smu->ppt_funcs->baco_enter(smu);3510} else {3511return -EINVAL;3512}35133514if (ret)3515dev_err(smu->adev->dev, "Failed to %s BACO state!\n",3516(state)?"enter":"exit");35173518return ret;3519}35203521bool smu_mode1_reset_is_support(struct smu_context *smu)3522{3523bool ret = false;35243525if (!smu->pm_enabled)3526return false;35273528if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)3529ret = smu->ppt_funcs->mode1_reset_is_support(smu);35303531return ret;3532}35333534bool smu_link_reset_is_support(struct smu_context *smu)3535{3536if (!smu->pm_enabled)3537return false;35383539return smu_feature_cap_test(smu, SMU_FEATURE_CAP_ID__LINK_RESET);3540}35413542int smu_mode1_reset(struct smu_context *smu)3543{3544int ret = 0;35453546if (!smu->pm_enabled)3547return -EOPNOTSUPP;35483549if (smu->ppt_funcs->mode1_reset)3550ret = smu->ppt_funcs->mode1_reset(smu);35513552return ret;3553}35543555static int smu_mode2_reset(void *handle)3556{3557struct smu_context *smu = handle;3558int ret = 0;35593560if (!smu->pm_enabled)3561return -EOPNOTSUPP;35623563if (smu->ppt_funcs->mode2_reset)3564ret = smu->ppt_funcs->mode2_reset(smu);35653566if (ret)3567dev_err(smu->adev->dev, "Mode2 reset failed!\n");35683569return ret;3570}35713572int smu_link_reset(struct smu_context *smu)3573{3574int ret = 0;35753576if (!smu->pm_enabled)3577return -EOPNOTSUPP;35783579if (smu->ppt_funcs->link_reset)3580ret = smu->ppt_funcs->link_reset(smu);35813582return ret;3583}35843585static int smu_enable_gfx_features(void *handle)3586{3587struct smu_context *smu = handle;3588int ret = 0;35893590if (!smu->pm_enabled)3591return -EOPNOTSUPP;35923593if (smu->ppt_funcs->enable_gfx_features)3594ret = smu->ppt_funcs->enable_gfx_features(smu);35953596if (ret)3597dev_err(smu->adev->dev, "enable gfx features failed!\n");35983599return ret;3600}36013602static int smu_get_max_sustainable_clocks_by_dc(void *handle,3603struct pp_smu_nv_clock_table *max_clocks)3604{3605struct smu_context *smu = handle;3606int ret = 0;36073608if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3609return -EOPNOTSUPP;36103611if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)3612ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);36133614return ret;3615}36163617static int smu_get_uclk_dpm_states(void *handle,3618unsigned int *clock_values_in_khz,3619unsigned int *num_states)3620{3621struct smu_context *smu = handle;3622int ret = 0;36233624if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3625return -EOPNOTSUPP;36263627if (smu->ppt_funcs->get_uclk_dpm_states)3628ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);36293630return ret;3631}36323633static enum amd_pm_state_type smu_get_current_power_state(void *handle)3634{3635struct smu_context *smu = handle;3636enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;36373638if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3639return -EOPNOTSUPP;36403641if (smu->ppt_funcs->get_current_power_state)3642pm_state = smu->ppt_funcs->get_current_power_state(smu);36433644return pm_state;3645}36463647static int smu_get_dpm_clock_table(void *handle,3648struct dpm_clocks *clock_table)3649{3650struct smu_context *smu = handle;3651int ret = 0;36523653if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3654return -EOPNOTSUPP;36553656if (smu->ppt_funcs->get_dpm_clock_table)3657ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);36583659return ret;3660}36613662static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)3663{3664struct smu_context *smu = handle;36653666if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3667return -EOPNOTSUPP;36683669if (!smu->ppt_funcs->get_gpu_metrics)3670return -EOPNOTSUPP;36713672return smu->ppt_funcs->get_gpu_metrics(smu, table);3673}36743675static ssize_t smu_sys_get_pm_metrics(void *handle, void *pm_metrics,3676size_t size)3677{3678struct smu_context *smu = handle;36793680if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3681return -EOPNOTSUPP;36823683if (!smu->ppt_funcs->get_pm_metrics)3684return -EOPNOTSUPP;36853686return smu->ppt_funcs->get_pm_metrics(smu, pm_metrics, size);3687}36883689static int smu_enable_mgpu_fan_boost(void *handle)3690{3691struct smu_context *smu = handle;3692int ret = 0;36933694if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3695return -EOPNOTSUPP;36963697if (smu->ppt_funcs->enable_mgpu_fan_boost)3698ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);36993700return ret;3701}37023703static int smu_gfx_state_change_set(void *handle,3704uint32_t state)3705{3706struct smu_context *smu = handle;3707int ret = 0;37083709if (smu->ppt_funcs->gfx_state_change_set)3710ret = smu->ppt_funcs->gfx_state_change_set(smu, state);37113712return ret;3713}37143715int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)3716{3717int ret = 0;37183719if (smu->ppt_funcs->smu_handle_passthrough_sbr)3720ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);37213722return ret;3723}37243725int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)3726{3727int ret = -EOPNOTSUPP;37283729if (smu->ppt_funcs &&3730smu->ppt_funcs->get_ecc_info)3731ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);37323733return ret;37343735}37363737static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)3738{3739struct smu_context *smu = handle;3740struct smu_table_context *smu_table = &smu->smu_table;3741struct smu_table *memory_pool = &smu_table->memory_pool;37423743if (!addr || !size)3744return -EINVAL;37453746*addr = NULL;3747*size = 0;3748if (memory_pool->bo) {3749*addr = memory_pool->cpu_addr;3750*size = memory_pool->size;3751}37523753return 0;3754}37553756static void smu_print_dpm_policy(struct smu_dpm_policy *policy, char *sysbuf,3757size_t *size)3758{3759size_t offset = *size;3760int level;37613762for_each_set_bit(level, &policy->level_mask, PP_POLICY_MAX_LEVELS) {3763if (level == policy->current_level)3764offset += sysfs_emit_at(sysbuf, offset,3765"%d : %s*\n", level,3766policy->desc->get_desc(policy, level));3767else3768offset += sysfs_emit_at(sysbuf, offset,3769"%d : %s\n", level,3770policy->desc->get_desc(policy, level));3771}37723773*size = offset;3774}37753776ssize_t smu_get_pm_policy_info(struct smu_context *smu,3777enum pp_pm_policy p_type, char *sysbuf)3778{3779struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;3780struct smu_dpm_policy_ctxt *policy_ctxt;3781struct smu_dpm_policy *dpm_policy;3782size_t offset = 0;37833784policy_ctxt = dpm_ctxt->dpm_policies;3785if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||3786!policy_ctxt->policy_mask)3787return -EOPNOTSUPP;37883789if (p_type == PP_PM_POLICY_NONE)3790return -EINVAL;37913792dpm_policy = smu_get_pm_policy(smu, p_type);3793if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->desc)3794return -ENOENT;37953796if (!sysbuf)3797return -EINVAL;37983799smu_print_dpm_policy(dpm_policy, sysbuf, &offset);38003801return offset;3802}38033804struct smu_dpm_policy *smu_get_pm_policy(struct smu_context *smu,3805enum pp_pm_policy p_type)3806{3807struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;3808struct smu_dpm_policy_ctxt *policy_ctxt;3809int i;38103811policy_ctxt = dpm_ctxt->dpm_policies;3812if (!policy_ctxt)3813return NULL;38143815for (i = 0; i < hweight32(policy_ctxt->policy_mask); ++i) {3816if (policy_ctxt->policies[i].policy_type == p_type)3817return &policy_ctxt->policies[i];3818}38193820return NULL;3821}38223823int smu_set_pm_policy(struct smu_context *smu, enum pp_pm_policy p_type,3824int level)3825{3826struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;3827struct smu_dpm_policy *dpm_policy = NULL;3828struct smu_dpm_policy_ctxt *policy_ctxt;3829int ret = -EOPNOTSUPP;38303831policy_ctxt = dpm_ctxt->dpm_policies;3832if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||3833!policy_ctxt->policy_mask)3834return ret;38353836if (level < 0 || level >= PP_POLICY_MAX_LEVELS)3837return -EINVAL;38383839dpm_policy = smu_get_pm_policy(smu, p_type);38403841if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->set_policy)3842return ret;38433844if (dpm_policy->current_level == level)3845return 0;38463847ret = dpm_policy->set_policy(smu, level);38483849if (!ret)3850dpm_policy->current_level = level;38513852return ret;3853}38543855static ssize_t smu_sys_get_temp_metrics(void *handle, enum smu_temp_metric_type type, void *table)3856{3857struct smu_context *smu = handle;3858struct smu_table_context *smu_table = &smu->smu_table;3859struct smu_table *tables = smu_table->tables;3860enum smu_table_id table_id;38613862if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3863return -EOPNOTSUPP;38643865if (!smu->smu_temp.temp_funcs || !smu->smu_temp.temp_funcs->get_temp_metrics)3866return -EOPNOTSUPP;38673868table_id = smu_metrics_get_temp_table_id(type);38693870if (table_id == SMU_TABLE_COUNT)3871return -EINVAL;38723873/* If the request is to get size alone, return the cached table size */3874if (!table && tables[table_id].cache.size)3875return tables[table_id].cache.size;38763877if (smu_table_cache_is_valid(&tables[table_id])) {3878memcpy(table, tables[table_id].cache.buffer,3879tables[table_id].cache.size);3880return tables[table_id].cache.size;3881}38823883return smu->smu_temp.temp_funcs->get_temp_metrics(smu, type, table);3884}38853886static bool smu_temp_metrics_is_supported(void *handle, enum smu_temp_metric_type type)3887{3888struct smu_context *smu = handle;3889bool ret = false;38903891if (!smu->pm_enabled)3892return false;38933894if (smu->smu_temp.temp_funcs && smu->smu_temp.temp_funcs->temp_metrics_is_supported)3895ret = smu->smu_temp.temp_funcs->temp_metrics_is_supported(smu, type);38963897return ret;3898}38993900static ssize_t smu_sys_get_xcp_metrics(void *handle, int xcp_id, void *table)3901{3902struct smu_context *smu = handle;39033904if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3905return -EOPNOTSUPP;39063907if (!smu->adev->xcp_mgr || !smu->ppt_funcs->get_xcp_metrics)3908return -EOPNOTSUPP;39093910return smu->ppt_funcs->get_xcp_metrics(smu, xcp_id, table);3911}39123913static const struct amd_pm_funcs swsmu_pm_funcs = {3914/* export for sysfs */3915.set_fan_control_mode = smu_set_fan_control_mode,3916.get_fan_control_mode = smu_get_fan_control_mode,3917.set_fan_speed_pwm = smu_set_fan_speed_pwm,3918.get_fan_speed_pwm = smu_get_fan_speed_pwm,3919.force_clock_level = smu_force_ppclk_levels,3920.print_clock_levels = smu_print_ppclk_levels,3921.emit_clock_levels = smu_emit_ppclk_levels,3922.force_performance_level = smu_force_performance_level,3923.read_sensor = smu_read_sensor,3924.get_apu_thermal_limit = smu_get_apu_thermal_limit,3925.set_apu_thermal_limit = smu_set_apu_thermal_limit,3926.get_performance_level = smu_get_performance_level,3927.get_current_power_state = smu_get_current_power_state,3928.get_fan_speed_rpm = smu_get_fan_speed_rpm,3929.set_fan_speed_rpm = smu_set_fan_speed_rpm,3930.get_pp_num_states = smu_get_power_num_states,3931.get_pp_table = smu_sys_get_pp_table,3932.set_pp_table = smu_sys_set_pp_table,3933.switch_power_profile = smu_switch_power_profile,3934.pause_power_profile = smu_pause_power_profile,3935/* export to amdgpu */3936.dispatch_tasks = smu_handle_dpm_task,3937.load_firmware = smu_load_microcode,3938.set_powergating_by_smu = smu_dpm_set_power_gate,3939.set_power_limit = smu_set_power_limit,3940.get_power_limit = smu_get_power_limit,3941.get_power_profile_mode = smu_get_power_profile_mode,3942.set_power_profile_mode = smu_set_power_profile_mode,3943.odn_edit_dpm_table = smu_od_edit_dpm_table,3944.set_mp1_state = smu_set_mp1_state,3945.gfx_state_change_set = smu_gfx_state_change_set,3946/* export to DC */3947.get_sclk = smu_get_sclk,3948.get_mclk = smu_get_mclk,3949.display_configuration_change = smu_display_configuration_change,3950.get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,3951.display_clock_voltage_request = smu_display_clock_voltage_request,3952.enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,3953.set_active_display_count = smu_set_display_count,3954.set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,3955.get_asic_baco_capability = smu_get_baco_capability,3956.set_asic_baco_state = smu_baco_set_state,3957.get_ppfeature_status = smu_sys_get_pp_feature_mask,3958.set_ppfeature_status = smu_sys_set_pp_feature_mask,3959.asic_reset_mode_2 = smu_mode2_reset,3960.asic_reset_enable_gfx_features = smu_enable_gfx_features,3961.set_df_cstate = smu_set_df_cstate,3962.set_xgmi_pstate = smu_set_xgmi_pstate,3963.get_gpu_metrics = smu_sys_get_gpu_metrics,3964.get_pm_metrics = smu_sys_get_pm_metrics,3965.set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,3966.display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,3967.get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,3968.get_uclk_dpm_states = smu_get_uclk_dpm_states,3969.get_dpm_clock_table = smu_get_dpm_clock_table,3970.get_smu_prv_buf_details = smu_get_prv_buffer_details,3971.get_xcp_metrics = smu_sys_get_xcp_metrics,3972.get_temp_metrics = smu_sys_get_temp_metrics,3973.temp_metrics_is_supported = smu_temp_metrics_is_supported,3974};39753976int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,3977uint64_t event_arg)3978{3979int ret = -EINVAL;39803981if (smu->ppt_funcs->wait_for_event)3982ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);39833984return ret;3985}39863987int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)3988{39893990if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)3991return -EOPNOTSUPP;39923993/* Confirm the buffer allocated is of correct size */3994if (size != smu->stb_context.stb_buf_size)3995return -EINVAL;39963997/*3998* No need to lock smu mutex as we access STB directly through MMIO3999* and not going through SMU messaging route (for now at least).4000* For registers access rely on implementation internal locking.4001*/4002return smu->ppt_funcs->stb_collect_info(smu, buf, size);4003}40044005#if defined(CONFIG_DEBUG_FS)40064007static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)4008{4009struct amdgpu_device *adev = filp->f_inode->i_private;4010struct smu_context *smu = adev->powerplay.pp_handle;4011unsigned char *buf;4012int r;40134014buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);4015if (!buf)4016return -ENOMEM;40174018r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);4019if (r)4020goto out;40214022filp->private_data = buf;40234024return 0;40254026out:4027kvfree(buf);4028return r;4029}40304031static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,4032loff_t *pos)4033{4034struct amdgpu_device *adev = filp->f_inode->i_private;4035struct smu_context *smu = adev->powerplay.pp_handle;403640374038if (!filp->private_data)4039return -EINVAL;40404041return simple_read_from_buffer(buf,4042size,4043pos, filp->private_data,4044smu->stb_context.stb_buf_size);4045}40464047static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)4048{4049kvfree(filp->private_data);4050filp->private_data = NULL;40514052return 0;4053}40544055/*4056* We have to define not only read method but also4057* open and release because .read takes up to PAGE_SIZE4058* data each time so and so is invoked multiple times.4059* We allocate the STB buffer in .open and release it4060* in .release4061*/4062static const struct file_operations smu_stb_debugfs_fops = {4063.owner = THIS_MODULE,4064.open = smu_stb_debugfs_open,4065.read = smu_stb_debugfs_read,4066.release = smu_stb_debugfs_release,4067.llseek = default_llseek,4068};40694070#endif40714072void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)4073{4074#if defined(CONFIG_DEBUG_FS)40754076struct smu_context *smu = adev->powerplay.pp_handle;40774078if (!smu || (!smu->stb_context.stb_buf_size))4079return;40804081debugfs_create_file_size("amdgpu_smu_stb_dump",4082S_IRUSR,4083adev_to_drm(adev)->primary->debugfs_root,4084adev,4085&smu_stb_debugfs_fops,4086smu->stb_context.stb_buf_size);4087#endif4088}40894090int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)4091{4092int ret = 0;40934094if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)4095ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);40964097return ret;4098}40994100int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)4101{4102int ret = 0;41034104if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)4105ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);41064107return ret;4108}41094110int smu_send_rma_reason(struct smu_context *smu)4111{4112int ret = 0;41134114if (smu->ppt_funcs && smu->ppt_funcs->send_rma_reason)4115ret = smu->ppt_funcs->send_rma_reason(smu);41164117return ret;4118}41194120/**4121* smu_reset_sdma_is_supported - Check if SDMA reset is supported by SMU4122* @smu: smu_context pointer4123*4124* This function checks if the SMU supports resetting the SDMA engine.4125* It returns true if supported, false otherwise.4126*/4127bool smu_reset_sdma_is_supported(struct smu_context *smu)4128{4129return smu_feature_cap_test(smu, SMU_FEATURE_CAP_ID__SDMA_RESET);4130}41314132int smu_reset_sdma(struct smu_context *smu, uint32_t inst_mask)4133{4134int ret = 0;41354136if (smu->ppt_funcs && smu->ppt_funcs->reset_sdma)4137ret = smu->ppt_funcs->reset_sdma(smu, inst_mask);41384139return ret;4140}41414142bool smu_reset_vcn_is_supported(struct smu_context *smu)4143{4144return smu_feature_cap_test(smu, SMU_FEATURE_CAP_ID__VCN_RESET);4145}41464147int smu_reset_vcn(struct smu_context *smu, uint32_t inst_mask)4148{4149if (smu->ppt_funcs && smu->ppt_funcs->dpm_reset_vcn)4150smu->ppt_funcs->dpm_reset_vcn(smu, inst_mask);41514152return 0;4153}415441554156