Path: blob/master/drivers/gpu/drm/amd/pm/swsmu/amdgpu_smu.c
26535 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}13161317static int smu_sw_init(struct amdgpu_ip_block *ip_block)1318{1319struct amdgpu_device *adev = ip_block->adev;1320struct smu_context *smu = adev->powerplay.pp_handle;1321int i, ret;13221323smu->pool_size = adev->pm.smu_prv_buffer_size;1324smu->smu_feature.feature_num = SMU_FEATURE_MAX;1325bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);1326bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);13271328INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);1329INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);1330atomic64_set(&smu->throttle_int_counter, 0);1331smu->watermarks_bitmap = 0;13321333for (i = 0; i < adev->vcn.num_vcn_inst; i++)1334atomic_set(&smu->smu_power.power_gate.vcn_gated[i], 1);1335atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);1336atomic_set(&smu->smu_power.power_gate.vpe_gated, 1);1337atomic_set(&smu->smu_power.power_gate.isp_gated, 1);1338atomic_set(&smu->smu_power.power_gate.umsch_mm_gated, 1);13391340smu_init_power_profile(smu);1341smu->display_config = &adev->pm.pm_display_cfg;13421343smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;1344smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;13451346INIT_DELAYED_WORK(&smu->swctf_delayed_work,1347smu_swctf_delayed_work_handler);13481349ret = smu_smc_table_sw_init(smu);1350if (ret) {1351dev_err(adev->dev, "Failed to sw init smc table!\n");1352return ret;1353}13541355/* get boot_values from vbios to set revision, gfxclk, and etc. */1356ret = smu_get_vbios_bootup_values(smu);1357if (ret) {1358dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");1359return ret;1360}13611362ret = smu_init_pptable_microcode(smu);1363if (ret) {1364dev_err(adev->dev, "Failed to setup pptable firmware!\n");1365return ret;1366}13671368ret = smu_register_irq_handler(smu);1369if (ret) {1370dev_err(adev->dev, "Failed to register smc irq handler!\n");1371return ret;1372}13731374/* If there is no way to query fan control mode, fan control is not supported */1375if (!smu->ppt_funcs->get_fan_control_mode)1376smu->adev->pm.no_fan = true;13771378return 0;1379}13801381static int smu_sw_fini(struct amdgpu_ip_block *ip_block)1382{1383struct amdgpu_device *adev = ip_block->adev;1384struct smu_context *smu = adev->powerplay.pp_handle;1385int ret;13861387ret = smu_smc_table_sw_fini(smu);1388if (ret) {1389dev_err(adev->dev, "Failed to sw fini smc table!\n");1390return ret;1391}13921393if (smu->custom_profile_params) {1394kfree(smu->custom_profile_params);1395smu->custom_profile_params = NULL;1396}13971398smu_fini_microcode(smu);13991400return 0;1401}14021403static int smu_get_thermal_temperature_range(struct smu_context *smu)1404{1405struct amdgpu_device *adev = smu->adev;1406struct smu_temperature_range *range =1407&smu->thermal_range;1408int ret = 0;14091410if (!smu->ppt_funcs->get_thermal_temperature_range)1411return 0;14121413ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);1414if (ret)1415return ret;14161417adev->pm.dpm.thermal.min_temp = range->min;1418adev->pm.dpm.thermal.max_temp = range->max;1419adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;1420adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;1421adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;1422adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;1423adev->pm.dpm.thermal.min_mem_temp = range->mem_min;1424adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;1425adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;14261427return ret;1428}14291430/**1431* smu_wbrf_handle_exclusion_ranges - consume the wbrf exclusion ranges1432*1433* @smu: smu_context pointer1434*1435* Retrieve the wbrf exclusion ranges and send them to PMFW for proper handling.1436* Returns 0 on success, error on failure.1437*/1438static int smu_wbrf_handle_exclusion_ranges(struct smu_context *smu)1439{1440struct wbrf_ranges_in_out wbrf_exclusion = {0};1441struct freq_band_range *wifi_bands = wbrf_exclusion.band_list;1442struct amdgpu_device *adev = smu->adev;1443uint32_t num_of_wbrf_ranges = MAX_NUM_OF_WBRF_RANGES;1444uint64_t start, end;1445int ret, i, j;14461447ret = amd_wbrf_retrieve_freq_band(adev->dev, &wbrf_exclusion);1448if (ret) {1449dev_err(adev->dev, "Failed to retrieve exclusion ranges!\n");1450return ret;1451}14521453/*1454* The exclusion ranges array we got might be filled with holes and duplicate1455* entries. For example:1456* {(2400, 2500), (0, 0), (6882, 6962), (2400, 2500), (0, 0), (6117, 6189), (0, 0)...}1457* We need to do some sortups to eliminate those holes and duplicate entries.1458* Expected output: {(2400, 2500), (6117, 6189), (6882, 6962), (0, 0)...}1459*/1460for (i = 0; i < num_of_wbrf_ranges; i++) {1461start = wifi_bands[i].start;1462end = wifi_bands[i].end;14631464/* get the last valid entry to fill the intermediate hole */1465if (!start && !end) {1466for (j = num_of_wbrf_ranges - 1; j > i; j--)1467if (wifi_bands[j].start && wifi_bands[j].end)1468break;14691470/* no valid entry left */1471if (j <= i)1472break;14731474start = wifi_bands[i].start = wifi_bands[j].start;1475end = wifi_bands[i].end = wifi_bands[j].end;1476wifi_bands[j].start = 0;1477wifi_bands[j].end = 0;1478num_of_wbrf_ranges = j;1479}14801481/* eliminate duplicate entries */1482for (j = i + 1; j < num_of_wbrf_ranges; j++) {1483if ((wifi_bands[j].start == start) && (wifi_bands[j].end == end)) {1484wifi_bands[j].start = 0;1485wifi_bands[j].end = 0;1486}1487}1488}14891490/* Send the sorted wifi_bands to PMFW */1491ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);1492/* Try to set the wifi_bands again */1493if (unlikely(ret == -EBUSY)) {1494mdelay(5);1495ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);1496}14971498return ret;1499}15001501/**1502* smu_wbrf_event_handler - handle notify events1503*1504* @nb: notifier block1505* @action: event type1506* @_arg: event data1507*1508* Calls relevant amdgpu function in response to wbrf event1509* notification from kernel.1510*/1511static int smu_wbrf_event_handler(struct notifier_block *nb,1512unsigned long action, void *_arg)1513{1514struct smu_context *smu = container_of(nb, struct smu_context, wbrf_notifier);15151516switch (action) {1517case WBRF_CHANGED:1518schedule_delayed_work(&smu->wbrf_delayed_work,1519msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));1520break;1521default:1522return NOTIFY_DONE;1523}15241525return NOTIFY_OK;1526}15271528/**1529* smu_wbrf_delayed_work_handler - callback on delayed work timer expired1530*1531* @work: struct work_struct pointer1532*1533* Flood is over and driver will consume the latest exclusion ranges.1534*/1535static void smu_wbrf_delayed_work_handler(struct work_struct *work)1536{1537struct smu_context *smu = container_of(work, struct smu_context, wbrf_delayed_work.work);15381539smu_wbrf_handle_exclusion_ranges(smu);1540}15411542/**1543* smu_wbrf_support_check - check wbrf support1544*1545* @smu: smu_context pointer1546*1547* Verifies the ACPI interface whether wbrf is supported.1548*/1549static void smu_wbrf_support_check(struct smu_context *smu)1550{1551struct amdgpu_device *adev = smu->adev;15521553smu->wbrf_supported = smu_is_asic_wbrf_supported(smu) && amdgpu_wbrf &&1554acpi_amd_wbrf_supported_consumer(adev->dev);15551556if (smu->wbrf_supported)1557dev_info(adev->dev, "RF interference mitigation is supported\n");1558}15591560/**1561* smu_wbrf_init - init driver wbrf support1562*1563* @smu: smu_context pointer1564*1565* Verifies the AMD ACPI interfaces and registers with the wbrf1566* notifier chain if wbrf feature is supported.1567* Returns 0 on success, error on failure.1568*/1569static int smu_wbrf_init(struct smu_context *smu)1570{1571int ret;15721573if (!smu->wbrf_supported)1574return 0;15751576INIT_DELAYED_WORK(&smu->wbrf_delayed_work, smu_wbrf_delayed_work_handler);15771578smu->wbrf_notifier.notifier_call = smu_wbrf_event_handler;1579ret = amd_wbrf_register_notifier(&smu->wbrf_notifier);1580if (ret)1581return ret;15821583/*1584* Some wifiband exclusion ranges may be already there1585* before our driver loaded. To make sure our driver1586* is awared of those exclusion ranges.1587*/1588schedule_delayed_work(&smu->wbrf_delayed_work,1589msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));15901591return 0;1592}15931594/**1595* smu_wbrf_fini - tear down driver wbrf support1596*1597* @smu: smu_context pointer1598*1599* Unregisters with the wbrf notifier chain.1600*/1601static void smu_wbrf_fini(struct smu_context *smu)1602{1603if (!smu->wbrf_supported)1604return;16051606amd_wbrf_unregister_notifier(&smu->wbrf_notifier);16071608cancel_delayed_work_sync(&smu->wbrf_delayed_work);1609}16101611static int smu_smc_hw_setup(struct smu_context *smu)1612{1613struct smu_feature *feature = &smu->smu_feature;1614struct amdgpu_device *adev = smu->adev;1615uint8_t pcie_gen = 0, pcie_width = 0;1616uint64_t features_supported;1617int ret = 0;16181619switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {1620case IP_VERSION(11, 0, 7):1621case IP_VERSION(11, 0, 11):1622case IP_VERSION(11, 5, 0):1623case IP_VERSION(11, 5, 2):1624case IP_VERSION(11, 0, 12):1625if (adev->in_suspend && smu_is_dpm_running(smu)) {1626dev_info(adev->dev, "dpm has been enabled\n");1627ret = smu_system_features_control(smu, true);1628if (ret)1629dev_err(adev->dev, "Failed system features control!\n");1630return ret;1631}1632break;1633default:1634break;1635}16361637ret = smu_init_display_count(smu, 0);1638if (ret) {1639dev_info(adev->dev, "Failed to pre-set display count as 0!\n");1640return ret;1641}16421643ret = smu_set_driver_table_location(smu);1644if (ret) {1645dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");1646return ret;1647}16481649/*1650* Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.1651*/1652ret = smu_set_tool_table_location(smu);1653if (ret) {1654dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");1655return ret;1656}16571658/*1659* Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify1660* pool location.1661*/1662ret = smu_notify_memory_pool_location(smu);1663if (ret) {1664dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");1665return ret;1666}16671668/*1669* It is assumed the pptable used before runpm is same as1670* the one used afterwards. Thus, we can reuse the stored1671* copy and do not need to resetup the pptable again.1672*/1673if (!adev->in_runpm) {1674ret = smu_setup_pptable(smu);1675if (ret) {1676dev_err(adev->dev, "Failed to setup pptable!\n");1677return ret;1678}1679}16801681/* smu_dump_pptable(smu); */16821683/*1684* With SCPM enabled, PSP is responsible for the PPTable transferring1685* (to SMU). Driver involvement is not needed and permitted.1686*/1687if (!adev->scpm_enabled) {1688/*1689* Copy pptable bo in the vram to smc with SMU MSGs such as1690* SetDriverDramAddr and TransferTableDram2Smu.1691*/1692ret = smu_write_pptable(smu);1693if (ret) {1694dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");1695return ret;1696}1697}16981699/* issue Run*Btc msg */1700ret = smu_run_btc(smu);1701if (ret)1702return ret;17031704/* Enable UclkShadow on wbrf supported */1705if (smu->wbrf_supported) {1706ret = smu_enable_uclk_shadow(smu, true);1707if (ret) {1708dev_err(adev->dev, "Failed to enable UclkShadow feature to support wbrf!\n");1709return ret;1710}1711}17121713/*1714* With SCPM enabled, these actions(and relevant messages) are1715* not needed and permitted.1716*/1717if (!adev->scpm_enabled) {1718ret = smu_feature_set_allowed_mask(smu);1719if (ret) {1720dev_err(adev->dev, "Failed to set driver allowed features mask!\n");1721return ret;1722}1723}17241725if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5)1726pcie_gen = 4;1727else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)1728pcie_gen = 3;1729else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)1730pcie_gen = 2;1731else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)1732pcie_gen = 1;1733else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)1734pcie_gen = 0;17351736/* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM11737* Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN41738* Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x321739*/1740if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X32)1741pcie_width = 7;1742else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)1743pcie_width = 6;1744else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)1745pcie_width = 5;1746else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)1747pcie_width = 4;1748else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)1749pcie_width = 3;1750else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)1751pcie_width = 2;1752else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)1753pcie_width = 1;1754ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);1755if (ret) {1756dev_err(adev->dev, "Attempt to override pcie params failed!\n");1757return ret;1758}17591760ret = smu_system_features_control(smu, true);1761if (ret) {1762dev_err(adev->dev, "Failed to enable requested dpm features!\n");1763return ret;1764}17651766smu_init_xgmi_plpd_mode(smu);17671768ret = smu_feature_get_enabled_mask(smu, &features_supported);1769if (ret) {1770dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");1771return ret;1772}1773bitmap_copy(feature->supported,1774(unsigned long *)&features_supported,1775feature->feature_num);17761777if (!smu_is_dpm_running(smu))1778dev_info(adev->dev, "dpm has been disabled\n");17791780/*1781* Set initialized values (get from vbios) to dpm tables context such as1782* gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each1783* type of clks.1784*/1785ret = smu_set_default_dpm_table(smu);1786if (ret) {1787dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");1788return ret;1789}17901791ret = smu_get_thermal_temperature_range(smu);1792if (ret) {1793dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");1794return ret;1795}17961797ret = smu_enable_thermal_alert(smu);1798if (ret) {1799dev_err(adev->dev, "Failed to enable thermal alert!\n");1800return ret;1801}18021803ret = smu_notify_display_change(smu);1804if (ret) {1805dev_err(adev->dev, "Failed to notify display change!\n");1806return ret;1807}18081809/*1810* Set min deep sleep dce fclk with bootup value from vbios via1811* SetMinDeepSleepDcefclk MSG.1812*/1813ret = smu_set_min_dcef_deep_sleep(smu,1814smu->smu_table.boot_values.dcefclk / 100);1815if (ret) {1816dev_err(adev->dev, "Error setting min deepsleep dcefclk\n");1817return ret;1818}18191820/* Init wbrf support. Properly setup the notifier */1821ret = smu_wbrf_init(smu);1822if (ret)1823dev_err(adev->dev, "Error during wbrf init call\n");18241825return ret;1826}18271828static int smu_start_smc_engine(struct smu_context *smu)1829{1830struct amdgpu_device *adev = smu->adev;1831int ret = 0;18321833if (amdgpu_virt_xgmi_migrate_enabled(adev))1834smu_update_gpu_addresses(smu);18351836smu->smc_fw_state = SMU_FW_INIT;18371838if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {1839if (amdgpu_ip_version(adev, MP1_HWIP, 0) < IP_VERSION(11, 0, 0)) {1840if (smu->ppt_funcs->load_microcode) {1841ret = smu->ppt_funcs->load_microcode(smu);1842if (ret)1843return ret;1844}1845}1846}18471848if (smu->ppt_funcs->check_fw_status) {1849ret = smu->ppt_funcs->check_fw_status(smu);1850if (ret) {1851dev_err(adev->dev, "SMC is not ready\n");1852return ret;1853}1854}18551856/*1857* Send msg GetDriverIfVersion to check if the return value is equal1858* with DRIVER_IF_VERSION of smc header.1859*/1860ret = smu_check_fw_version(smu);1861if (ret)1862return ret;18631864return ret;1865}18661867static int smu_hw_init(struct amdgpu_ip_block *ip_block)1868{1869int i, ret;1870struct amdgpu_device *adev = ip_block->adev;1871struct smu_context *smu = adev->powerplay.pp_handle;18721873if (amdgpu_sriov_multi_vf_mode(adev)) {1874smu->pm_enabled = false;1875return 0;1876}18771878ret = smu_start_smc_engine(smu);1879if (ret) {1880dev_err(adev->dev, "SMC engine is not correctly up!\n");1881return ret;1882}18831884/*1885* Check whether wbrf is supported. This needs to be done1886* before SMU setup starts since part of SMU configuration1887* relies on this.1888*/1889smu_wbrf_support_check(smu);18901891if (smu->is_apu) {1892ret = smu_set_gfx_imu_enable(smu);1893if (ret)1894return ret;1895for (i = 0; i < adev->vcn.num_vcn_inst; i++)1896smu_dpm_set_vcn_enable(smu, true, i);1897smu_dpm_set_jpeg_enable(smu, true);1898smu_dpm_set_vpe_enable(smu, true);1899smu_dpm_set_umsch_mm_enable(smu, true);1900smu_set_mall_enable(smu);1901smu_set_gfx_cgpg(smu, true);1902}19031904if (!smu->pm_enabled)1905return 0;19061907ret = smu_get_driver_allowed_feature_mask(smu);1908if (ret)1909return ret;19101911ret = smu_smc_hw_setup(smu);1912if (ret) {1913dev_err(adev->dev, "Failed to setup smc hw!\n");1914return ret;1915}19161917/*1918* Move maximum sustainable clock retrieving here considering1919* 1. It is not needed on resume(from S3).1920* 2. DAL settings come between .hw_init and .late_init of SMU.1921* And DAL needs to know the maximum sustainable clocks. Thus1922* it cannot be put in .late_init().1923*/1924ret = smu_init_max_sustainable_clocks(smu);1925if (ret) {1926dev_err(adev->dev, "Failed to init max sustainable clocks!\n");1927return ret;1928}19291930adev->pm.dpm_enabled = true;19311932dev_info(adev->dev, "SMU is initialized successfully!\n");19331934return 0;1935}19361937static int smu_disable_dpms(struct smu_context *smu)1938{1939struct amdgpu_device *adev = smu->adev;1940int ret = 0;1941bool use_baco = !smu->is_apu &&1942((amdgpu_in_reset(adev) &&1943(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||1944((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));19451946/*1947* For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others)1948* properly on suspend/reset/unload. Driver involvement may cause some unexpected issues.1949*/1950switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {1951case IP_VERSION(13, 0, 0):1952case IP_VERSION(13, 0, 7):1953case IP_VERSION(13, 0, 10):1954case IP_VERSION(14, 0, 2):1955case IP_VERSION(14, 0, 3):1956return 0;1957default:1958break;1959}19601961/*1962* For custom pptable uploading, skip the DPM features1963* disable process on Navi1x ASICs.1964* - As the gfx related features are under control of1965* RLC on those ASICs. RLC reinitialization will be1966* needed to reenable them. That will cost much more1967* efforts.1968*1969* - SMU firmware can handle the DPM reenablement1970* properly.1971*/1972if (smu->uploading_custom_pp_table) {1973switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {1974case IP_VERSION(11, 0, 0):1975case IP_VERSION(11, 0, 5):1976case IP_VERSION(11, 0, 9):1977case IP_VERSION(11, 0, 7):1978case IP_VERSION(11, 0, 11):1979case IP_VERSION(11, 5, 0):1980case IP_VERSION(11, 5, 2):1981case IP_VERSION(11, 0, 12):1982case IP_VERSION(11, 0, 13):1983return 0;1984default:1985break;1986}1987}19881989/*1990* For Sienna_Cichlid, PMFW will handle the features disablement properly1991* on BACO in. Driver involvement is unnecessary.1992*/1993if (use_baco) {1994switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {1995case IP_VERSION(11, 0, 7):1996case IP_VERSION(11, 0, 0):1997case IP_VERSION(11, 0, 5):1998case IP_VERSION(11, 0, 9):1999case IP_VERSION(13, 0, 7):2000return 0;2001default:2002break;2003}2004}20052006/*2007* For GFX11 and subsequent APUs, PMFW will handle the features disablement properly2008* for gpu reset and S0i3 cases. Driver involvement is unnecessary.2009*/2010if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) >= 11 &&2011smu->is_apu && (amdgpu_in_reset(adev) || adev->in_s0ix))2012return 0;20132014/*2015* For gpu reset, runpm and hibernation through BACO,2016* BACO feature has to be kept enabled.2017*/2018if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {2019ret = smu_disable_all_features_with_exception(smu,2020SMU_FEATURE_BACO_BIT);2021if (ret)2022dev_err(adev->dev, "Failed to disable smu features except BACO.\n");2023} else {2024/* DisableAllSmuFeatures message is not permitted with SCPM enabled */2025if (!adev->scpm_enabled) {2026ret = smu_system_features_control(smu, false);2027if (ret)2028dev_err(adev->dev, "Failed to disable smu features.\n");2029}2030}20312032/* Notify SMU RLC is going to be off, stop RLC and SMU interaction.2033* otherwise SMU will hang while interacting with RLC if RLC is halted2034* this is a WA for Vangogh asic which fix the SMU hang issue.2035*/2036ret = smu_notify_rlc_state(smu, false);2037if (ret) {2038dev_err(adev->dev, "Fail to notify rlc status!\n");2039return ret;2040}20412042if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 2) &&2043!((adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs) &&2044!amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop)2045adev->gfx.rlc.funcs->stop(adev);20462047return ret;2048}20492050static int smu_smc_hw_cleanup(struct smu_context *smu)2051{2052struct amdgpu_device *adev = smu->adev;2053int ret = 0;20542055smu_wbrf_fini(smu);20562057cancel_work_sync(&smu->throttling_logging_work);2058cancel_work_sync(&smu->interrupt_work);20592060ret = smu_disable_thermal_alert(smu);2061if (ret) {2062dev_err(adev->dev, "Fail to disable thermal alert!\n");2063return ret;2064}20652066cancel_delayed_work_sync(&smu->swctf_delayed_work);20672068ret = smu_disable_dpms(smu);2069if (ret) {2070dev_err(adev->dev, "Fail to disable dpm features!\n");2071return ret;2072}20732074return 0;2075}20762077static int smu_reset_mp1_state(struct smu_context *smu)2078{2079struct amdgpu_device *adev = smu->adev;2080int ret = 0;20812082if ((!adev->in_runpm) && (!adev->in_suspend) &&2083(!amdgpu_in_reset(adev)) && amdgpu_ip_version(adev, MP1_HWIP, 0) ==2084IP_VERSION(13, 0, 10) &&2085!amdgpu_device_has_display_hardware(adev))2086ret = smu_set_mp1_state(smu, PP_MP1_STATE_UNLOAD);20872088return ret;2089}20902091static int smu_hw_fini(struct amdgpu_ip_block *ip_block)2092{2093struct amdgpu_device *adev = ip_block->adev;2094struct smu_context *smu = adev->powerplay.pp_handle;2095int i, ret;20962097if (amdgpu_sriov_multi_vf_mode(adev))2098return 0;20992100for (i = 0; i < adev->vcn.num_vcn_inst; i++) {2101smu_dpm_set_vcn_enable(smu, false, i);2102adev->vcn.inst[i].cur_state = AMD_PG_STATE_GATE;2103}2104smu_dpm_set_jpeg_enable(smu, false);2105adev->jpeg.cur_state = AMD_PG_STATE_GATE;2106smu_dpm_set_vpe_enable(smu, false);2107smu_dpm_set_umsch_mm_enable(smu, false);21082109if (!smu->pm_enabled)2110return 0;21112112adev->pm.dpm_enabled = false;21132114ret = smu_smc_hw_cleanup(smu);2115if (ret)2116return ret;21172118ret = smu_reset_mp1_state(smu);2119if (ret)2120return ret;21212122return 0;2123}21242125static void smu_late_fini(struct amdgpu_ip_block *ip_block)2126{2127struct amdgpu_device *adev = ip_block->adev;2128struct smu_context *smu = adev->powerplay.pp_handle;21292130kfree(smu);2131}21322133static int smu_reset(struct smu_context *smu)2134{2135struct amdgpu_device *adev = smu->adev;2136struct amdgpu_ip_block *ip_block;2137int ret;21382139ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_SMC);2140if (!ip_block)2141return -EINVAL;21422143ret = smu_hw_fini(ip_block);2144if (ret)2145return ret;21462147ret = smu_hw_init(ip_block);2148if (ret)2149return ret;21502151ret = smu_late_init(ip_block);2152if (ret)2153return ret;21542155return 0;2156}21572158static int smu_suspend(struct amdgpu_ip_block *ip_block)2159{2160struct amdgpu_device *adev = ip_block->adev;2161struct smu_context *smu = adev->powerplay.pp_handle;2162int ret;2163uint64_t count;21642165if (amdgpu_sriov_multi_vf_mode(adev))2166return 0;21672168if (!smu->pm_enabled)2169return 0;21702171adev->pm.dpm_enabled = false;21722173ret = smu_smc_hw_cleanup(smu);2174if (ret)2175return ret;21762177smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);21782179smu_set_gfx_cgpg(smu, false);21802181/*2182* pwfw resets entrycount when device is suspended, so we save the2183* last value to be used when we resume to keep it consistent2184*/2185ret = smu_get_entrycount_gfxoff(smu, &count);2186if (!ret)2187adev->gfx.gfx_off_entrycount = count;21882189/* clear this on suspend so it will get reprogrammed on resume */2190smu->workload_mask = 0;21912192return 0;2193}21942195static int smu_resume(struct amdgpu_ip_block *ip_block)2196{2197int ret;2198struct amdgpu_device *adev = ip_block->adev;2199struct smu_context *smu = adev->powerplay.pp_handle;2200struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);22012202if (amdgpu_sriov_multi_vf_mode(adev))2203return 0;22042205if (!smu->pm_enabled)2206return 0;22072208dev_info(adev->dev, "SMU is resuming...\n");22092210ret = smu_start_smc_engine(smu);2211if (ret) {2212dev_err(adev->dev, "SMC engine is not correctly up!\n");2213return ret;2214}22152216ret = smu_smc_hw_setup(smu);2217if (ret) {2218dev_err(adev->dev, "Failed to setup smc hw!\n");2219return ret;2220}22212222ret = smu_set_gfx_imu_enable(smu);2223if (ret)2224return ret;22252226smu_set_gfx_cgpg(smu, true);22272228smu->disable_uclk_switch = 0;22292230adev->pm.dpm_enabled = true;22312232if (smu->current_power_limit) {2233ret = smu_set_power_limit(smu, smu->current_power_limit);2234if (ret && ret != -EOPNOTSUPP)2235return ret;2236}22372238if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {2239ret = smu_od_edit_dpm_table(smu, PP_OD_COMMIT_DPM_TABLE, NULL, 0);2240if (ret)2241return ret;2242}22432244dev_info(adev->dev, "SMU is resumed successfully!\n");22452246return 0;2247}22482249static int smu_display_configuration_change(void *handle,2250const struct amd_pp_display_configuration *display_config)2251{2252struct smu_context *smu = handle;22532254if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2255return -EOPNOTSUPP;22562257if (!display_config)2258return -EINVAL;22592260smu_set_min_dcef_deep_sleep(smu,2261display_config->min_dcef_deep_sleep_set_clk / 100);22622263return 0;2264}22652266static int smu_set_clockgating_state(struct amdgpu_ip_block *ip_block,2267enum amd_clockgating_state state)2268{2269return 0;2270}22712272static int smu_set_powergating_state(struct amdgpu_ip_block *ip_block,2273enum amd_powergating_state state)2274{2275return 0;2276}22772278static int smu_enable_umd_pstate(void *handle,2279enum amd_dpm_forced_level *level)2280{2281uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |2282AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |2283AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |2284AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;22852286struct smu_context *smu = (struct smu_context*)(handle);2287struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);22882289if (!smu->is_apu && !smu_dpm_ctx->dpm_context)2290return -EINVAL;22912292if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {2293/* enter umd pstate, save current level, disable gfx cg*/2294if (*level & profile_mode_mask) {2295smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;2296smu_gpo_control(smu, false);2297smu_gfx_ulv_control(smu, false);2298smu_deep_sleep_control(smu, false);2299amdgpu_asic_update_umd_stable_pstate(smu->adev, true);2300}2301} else {2302/* exit umd pstate, restore level, enable gfx cg*/2303if (!(*level & profile_mode_mask)) {2304if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)2305*level = smu_dpm_ctx->saved_dpm_level;2306amdgpu_asic_update_umd_stable_pstate(smu->adev, false);2307smu_deep_sleep_control(smu, true);2308smu_gfx_ulv_control(smu, true);2309smu_gpo_control(smu, true);2310}2311}23122313return 0;2314}23152316static int smu_bump_power_profile_mode(struct smu_context *smu,2317long *custom_params,2318u32 custom_params_max_idx)2319{2320u32 workload_mask = 0;2321int i, ret = 0;23222323for (i = 0; i < PP_SMC_POWER_PROFILE_COUNT; i++) {2324if (smu->workload_refcount[i])2325workload_mask |= 1 << i;2326}23272328if (smu->workload_mask == workload_mask)2329return 0;23302331if (smu->ppt_funcs->set_power_profile_mode)2332ret = smu->ppt_funcs->set_power_profile_mode(smu, workload_mask,2333custom_params,2334custom_params_max_idx);23352336if (!ret)2337smu->workload_mask = workload_mask;23382339return ret;2340}23412342static void smu_power_profile_mode_get(struct smu_context *smu,2343enum PP_SMC_POWER_PROFILE profile_mode)2344{2345smu->workload_refcount[profile_mode]++;2346}23472348static void smu_power_profile_mode_put(struct smu_context *smu,2349enum PP_SMC_POWER_PROFILE profile_mode)2350{2351if (smu->workload_refcount[profile_mode])2352smu->workload_refcount[profile_mode]--;2353}23542355static int smu_adjust_power_state_dynamic(struct smu_context *smu,2356enum amd_dpm_forced_level level,2357bool skip_display_settings)2358{2359int ret = 0;2360struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);23612362if (!skip_display_settings) {2363ret = smu_display_config_changed(smu);2364if (ret) {2365dev_err(smu->adev->dev, "Failed to change display config!");2366return ret;2367}2368}23692370ret = smu_apply_clocks_adjust_rules(smu);2371if (ret) {2372dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");2373return ret;2374}23752376if (!skip_display_settings) {2377ret = smu_notify_smc_display_config(smu);2378if (ret) {2379dev_err(smu->adev->dev, "Failed to notify smc display config!");2380return ret;2381}2382}23832384if (smu_dpm_ctx->dpm_level != level) {2385ret = smu_asic_set_performance_level(smu, level);2386if (ret) {2387if (ret == -EOPNOTSUPP)2388dev_info(smu->adev->dev, "set performance level %d not supported",2389level);2390else2391dev_err(smu->adev->dev, "Failed to set performance level %d",2392level);2393return ret;2394}23952396/* update the saved copy */2397smu_dpm_ctx->dpm_level = level;2398}23992400if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&2401smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)2402smu_bump_power_profile_mode(smu, NULL, 0);24032404return ret;2405}24062407static int smu_handle_task(struct smu_context *smu,2408enum amd_dpm_forced_level level,2409enum amd_pp_task task_id)2410{2411int ret = 0;24122413if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2414return -EOPNOTSUPP;24152416switch (task_id) {2417case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:2418ret = smu_pre_display_config_changed(smu);2419if (ret)2420return ret;2421ret = smu_adjust_power_state_dynamic(smu, level, false);2422break;2423case AMD_PP_TASK_COMPLETE_INIT:2424ret = smu_adjust_power_state_dynamic(smu, level, true);2425break;2426case AMD_PP_TASK_READJUST_POWER_STATE:2427ret = smu_adjust_power_state_dynamic(smu, level, true);2428break;2429default:2430break;2431}24322433return ret;2434}24352436static int smu_handle_dpm_task(void *handle,2437enum amd_pp_task task_id,2438enum amd_pm_state_type *user_state)2439{2440struct smu_context *smu = handle;2441struct smu_dpm_context *smu_dpm = &smu->smu_dpm;24422443return smu_handle_task(smu, smu_dpm->dpm_level, task_id);24442445}24462447static int smu_switch_power_profile(void *handle,2448enum PP_SMC_POWER_PROFILE type,2449bool enable)2450{2451struct smu_context *smu = handle;2452struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2453int ret;24542455if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2456return -EOPNOTSUPP;24572458if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))2459return -EINVAL;24602461if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&2462smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {2463if (enable)2464smu_power_profile_mode_get(smu, type);2465else2466smu_power_profile_mode_put(smu, type);2467/* don't switch the active workload when paused */2468if (smu->pause_workload)2469ret = 0;2470else2471ret = smu_bump_power_profile_mode(smu, NULL, 0);2472if (ret) {2473if (enable)2474smu_power_profile_mode_put(smu, type);2475else2476smu_power_profile_mode_get(smu, type);2477return ret;2478}2479}24802481return 0;2482}24832484static int smu_pause_power_profile(void *handle,2485bool pause)2486{2487struct smu_context *smu = handle;2488struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2489u32 workload_mask = 1 << PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;2490int ret;24912492if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2493return -EOPNOTSUPP;24942495if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&2496smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {2497smu->pause_workload = pause;24982499/* force to bootup default profile */2500if (smu->pause_workload && smu->ppt_funcs->set_power_profile_mode)2501ret = smu->ppt_funcs->set_power_profile_mode(smu,2502workload_mask,2503NULL,25040);2505else2506ret = smu_bump_power_profile_mode(smu, NULL, 0);2507return ret;2508}25092510return 0;2511}25122513static enum amd_dpm_forced_level smu_get_performance_level(void *handle)2514{2515struct smu_context *smu = handle;2516struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);25172518if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2519return -EOPNOTSUPP;25202521if (!smu->is_apu && !smu_dpm_ctx->dpm_context)2522return -EINVAL;25232524return smu_dpm_ctx->dpm_level;2525}25262527static int smu_force_performance_level(void *handle,2528enum amd_dpm_forced_level level)2529{2530struct smu_context *smu = handle;2531struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2532int ret = 0;25332534if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2535return -EOPNOTSUPP;25362537if (!smu->is_apu && !smu_dpm_ctx->dpm_context)2538return -EINVAL;25392540ret = smu_enable_umd_pstate(smu, &level);2541if (ret)2542return ret;25432544ret = smu_handle_task(smu, level,2545AMD_PP_TASK_READJUST_POWER_STATE);25462547/* reset user dpm clock state */2548if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {2549memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));2550smu->user_dpm_profile.clk_dependency = 0;2551}25522553return ret;2554}25552556static int smu_set_display_count(void *handle, uint32_t count)2557{2558struct smu_context *smu = handle;25592560if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2561return -EOPNOTSUPP;25622563return smu_init_display_count(smu, count);2564}25652566static int smu_force_smuclk_levels(struct smu_context *smu,2567enum smu_clk_type clk_type,2568uint32_t mask)2569{2570struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);2571int ret = 0;25722573if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2574return -EOPNOTSUPP;25752576if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {2577dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");2578return -EINVAL;2579}25802581if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {2582ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);2583if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {2584smu->user_dpm_profile.clk_mask[clk_type] = mask;2585smu_set_user_clk_dependencies(smu, clk_type);2586}2587}25882589return ret;2590}25912592static int smu_force_ppclk_levels(void *handle,2593enum pp_clock_type type,2594uint32_t mask)2595{2596struct smu_context *smu = handle;2597enum smu_clk_type clk_type;25982599switch (type) {2600case PP_SCLK:2601clk_type = SMU_SCLK; break;2602case PP_MCLK:2603clk_type = SMU_MCLK; break;2604case PP_PCIE:2605clk_type = SMU_PCIE; break;2606case PP_SOCCLK:2607clk_type = SMU_SOCCLK; break;2608case PP_FCLK:2609clk_type = SMU_FCLK; break;2610case PP_DCEFCLK:2611clk_type = SMU_DCEFCLK; break;2612case PP_VCLK:2613clk_type = SMU_VCLK; break;2614case PP_VCLK1:2615clk_type = SMU_VCLK1; break;2616case PP_DCLK:2617clk_type = SMU_DCLK; break;2618case PP_DCLK1:2619clk_type = SMU_DCLK1; break;2620case OD_SCLK:2621clk_type = SMU_OD_SCLK; break;2622case OD_MCLK:2623clk_type = SMU_OD_MCLK; break;2624case OD_VDDC_CURVE:2625clk_type = SMU_OD_VDDC_CURVE; break;2626case OD_RANGE:2627clk_type = SMU_OD_RANGE; break;2628default:2629return -EINVAL;2630}26312632return smu_force_smuclk_levels(smu, clk_type, mask);2633}26342635/*2636* On system suspending or resetting, the dpm_enabled2637* flag will be cleared. So that those SMU services which2638* are not supported will be gated.2639* However, the mp1 state setting should still be granted2640* even if the dpm_enabled cleared.2641*/2642static int smu_set_mp1_state(void *handle,2643enum pp_mp1_state mp1_state)2644{2645struct smu_context *smu = handle;2646int ret = 0;26472648if (!smu->pm_enabled)2649return -EOPNOTSUPP;26502651if (smu->ppt_funcs &&2652smu->ppt_funcs->set_mp1_state)2653ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);26542655return ret;2656}26572658static int smu_set_df_cstate(void *handle,2659enum pp_df_cstate state)2660{2661struct smu_context *smu = handle;2662int ret = 0;26632664if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2665return -EOPNOTSUPP;26662667if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)2668return 0;26692670ret = smu->ppt_funcs->set_df_cstate(smu, state);2671if (ret)2672dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");26732674return ret;2675}26762677int smu_write_watermarks_table(struct smu_context *smu)2678{2679if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2680return -EOPNOTSUPP;26812682return smu_set_watermarks_table(smu, NULL);2683}26842685static int smu_set_watermarks_for_clock_ranges(void *handle,2686struct pp_smu_wm_range_sets *clock_ranges)2687{2688struct smu_context *smu = handle;26892690if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2691return -EOPNOTSUPP;26922693if (smu->disable_watermark)2694return 0;26952696return smu_set_watermarks_table(smu, clock_ranges);2697}26982699int smu_set_ac_dc(struct smu_context *smu)2700{2701int ret = 0;27022703if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2704return -EOPNOTSUPP;27052706/* controlled by firmware */2707if (smu->dc_controlled_by_gpio)2708return 0;27092710ret = smu_set_power_source(smu,2711smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :2712SMU_POWER_SOURCE_DC);2713if (ret)2714dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",2715smu->adev->pm.ac_power ? "AC" : "DC");27162717return ret;2718}27192720const struct amd_ip_funcs smu_ip_funcs = {2721.name = "smu",2722.early_init = smu_early_init,2723.late_init = smu_late_init,2724.sw_init = smu_sw_init,2725.sw_fini = smu_sw_fini,2726.hw_init = smu_hw_init,2727.hw_fini = smu_hw_fini,2728.late_fini = smu_late_fini,2729.suspend = smu_suspend,2730.resume = smu_resume,2731.is_idle = NULL,2732.check_soft_reset = NULL,2733.wait_for_idle = NULL,2734.soft_reset = NULL,2735.set_clockgating_state = smu_set_clockgating_state,2736.set_powergating_state = smu_set_powergating_state,2737};27382739const struct amdgpu_ip_block_version smu_v11_0_ip_block = {2740.type = AMD_IP_BLOCK_TYPE_SMC,2741.major = 11,2742.minor = 0,2743.rev = 0,2744.funcs = &smu_ip_funcs,2745};27462747const struct amdgpu_ip_block_version smu_v12_0_ip_block = {2748.type = AMD_IP_BLOCK_TYPE_SMC,2749.major = 12,2750.minor = 0,2751.rev = 0,2752.funcs = &smu_ip_funcs,2753};27542755const struct amdgpu_ip_block_version smu_v13_0_ip_block = {2756.type = AMD_IP_BLOCK_TYPE_SMC,2757.major = 13,2758.minor = 0,2759.rev = 0,2760.funcs = &smu_ip_funcs,2761};27622763const struct amdgpu_ip_block_version smu_v14_0_ip_block = {2764.type = AMD_IP_BLOCK_TYPE_SMC,2765.major = 14,2766.minor = 0,2767.rev = 0,2768.funcs = &smu_ip_funcs,2769};27702771static int smu_load_microcode(void *handle)2772{2773struct smu_context *smu = handle;2774struct amdgpu_device *adev = smu->adev;2775int ret = 0;27762777if (!smu->pm_enabled)2778return -EOPNOTSUPP;27792780/* This should be used for non PSP loading */2781if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)2782return 0;27832784if (smu->ppt_funcs->load_microcode) {2785ret = smu->ppt_funcs->load_microcode(smu);2786if (ret) {2787dev_err(adev->dev, "Load microcode failed\n");2788return ret;2789}2790}27912792if (smu->ppt_funcs->check_fw_status) {2793ret = smu->ppt_funcs->check_fw_status(smu);2794if (ret) {2795dev_err(adev->dev, "SMC is not ready\n");2796return ret;2797}2798}27992800return ret;2801}28022803static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)2804{2805int ret = 0;28062807if (smu->ppt_funcs->set_gfx_cgpg)2808ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);28092810return ret;2811}28122813static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)2814{2815struct smu_context *smu = handle;2816int ret = 0;28172818if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2819return -EOPNOTSUPP;28202821if (!smu->ppt_funcs->set_fan_speed_rpm)2822return -EOPNOTSUPP;28232824if (speed == U32_MAX)2825return -EINVAL;28262827ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);2828if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {2829smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;2830smu->user_dpm_profile.fan_speed_rpm = speed;28312832/* Override custom PWM setting as they cannot co-exist */2833smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;2834smu->user_dpm_profile.fan_speed_pwm = 0;2835}28362837return ret;2838}28392840/**2841* smu_get_power_limit - Request one of the SMU Power Limits2842*2843* @handle: pointer to smu context2844* @limit: requested limit is written back to this variable2845* @pp_limit_level: &pp_power_limit_level which limit of the power to return2846* @pp_power_type: &pp_power_type type of power2847* Return: 0 on success, <0 on error2848*2849*/2850int smu_get_power_limit(void *handle,2851uint32_t *limit,2852enum pp_power_limit_level pp_limit_level,2853enum pp_power_type pp_power_type)2854{2855struct smu_context *smu = handle;2856struct amdgpu_device *adev = smu->adev;2857enum smu_ppt_limit_level limit_level;2858uint32_t limit_type;2859int ret = 0;28602861if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2862return -EOPNOTSUPP;28632864switch (pp_power_type) {2865case PP_PWR_TYPE_SUSTAINED:2866limit_type = SMU_DEFAULT_PPT_LIMIT;2867break;2868case PP_PWR_TYPE_FAST:2869limit_type = SMU_FAST_PPT_LIMIT;2870break;2871default:2872return -EOPNOTSUPP;2873}28742875switch (pp_limit_level) {2876case PP_PWR_LIMIT_CURRENT:2877limit_level = SMU_PPT_LIMIT_CURRENT;2878break;2879case PP_PWR_LIMIT_DEFAULT:2880limit_level = SMU_PPT_LIMIT_DEFAULT;2881break;2882case PP_PWR_LIMIT_MAX:2883limit_level = SMU_PPT_LIMIT_MAX;2884break;2885case PP_PWR_LIMIT_MIN:2886limit_level = SMU_PPT_LIMIT_MIN;2887break;2888default:2889return -EOPNOTSUPP;2890}28912892if (limit_type != SMU_DEFAULT_PPT_LIMIT) {2893if (smu->ppt_funcs->get_ppt_limit)2894ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);2895} else {2896switch (limit_level) {2897case SMU_PPT_LIMIT_CURRENT:2898switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {2899case IP_VERSION(13, 0, 2):2900case IP_VERSION(13, 0, 6):2901case IP_VERSION(13, 0, 12):2902case IP_VERSION(13, 0, 14):2903case IP_VERSION(11, 0, 7):2904case IP_VERSION(11, 0, 11):2905case IP_VERSION(11, 0, 12):2906case IP_VERSION(11, 0, 13):2907ret = smu_get_asic_power_limits(smu,2908&smu->current_power_limit,2909NULL, NULL, NULL);2910break;2911default:2912break;2913}2914*limit = smu->current_power_limit;2915break;2916case SMU_PPT_LIMIT_DEFAULT:2917*limit = smu->default_power_limit;2918break;2919case SMU_PPT_LIMIT_MAX:2920*limit = smu->max_power_limit;2921break;2922case SMU_PPT_LIMIT_MIN:2923*limit = smu->min_power_limit;2924break;2925default:2926return -EINVAL;2927}2928}29292930return ret;2931}29322933static int smu_set_power_limit(void *handle, uint32_t limit)2934{2935struct smu_context *smu = handle;2936uint32_t limit_type = limit >> 24;2937int ret = 0;29382939if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2940return -EOPNOTSUPP;29412942limit &= (1<<24)-1;2943if (limit_type != SMU_DEFAULT_PPT_LIMIT)2944if (smu->ppt_funcs->set_power_limit)2945return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);29462947if ((limit > smu->max_power_limit) || (limit < smu->min_power_limit)) {2948dev_err(smu->adev->dev,2949"New power limit (%d) is out of range [%d,%d]\n",2950limit, smu->min_power_limit, smu->max_power_limit);2951return -EINVAL;2952}29532954if (!limit)2955limit = smu->current_power_limit;29562957if (smu->ppt_funcs->set_power_limit) {2958ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);2959if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))2960smu->user_dpm_profile.power_limit = limit;2961}29622963return ret;2964}29652966static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)2967{2968int ret = 0;29692970if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)2971return -EOPNOTSUPP;29722973if (smu->ppt_funcs->print_clk_levels)2974ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);29752976return ret;2977}29782979static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)2980{2981enum smu_clk_type clk_type;29822983switch (type) {2984case PP_SCLK:2985clk_type = SMU_SCLK; break;2986case PP_MCLK:2987clk_type = SMU_MCLK; break;2988case PP_PCIE:2989clk_type = SMU_PCIE; break;2990case PP_SOCCLK:2991clk_type = SMU_SOCCLK; break;2992case PP_FCLK:2993clk_type = SMU_FCLK; break;2994case PP_DCEFCLK:2995clk_type = SMU_DCEFCLK; break;2996case PP_VCLK:2997clk_type = SMU_VCLK; break;2998case PP_VCLK1:2999clk_type = SMU_VCLK1; break;3000case PP_DCLK:3001clk_type = SMU_DCLK; break;3002case PP_DCLK1:3003clk_type = SMU_DCLK1; break;3004case PP_ISPICLK:3005clk_type = SMU_ISPICLK;3006break;3007case PP_ISPXCLK:3008clk_type = SMU_ISPXCLK;3009break;3010case OD_SCLK:3011clk_type = SMU_OD_SCLK; break;3012case OD_MCLK:3013clk_type = SMU_OD_MCLK; break;3014case OD_VDDC_CURVE:3015clk_type = SMU_OD_VDDC_CURVE; break;3016case OD_RANGE:3017clk_type = SMU_OD_RANGE; break;3018case OD_VDDGFX_OFFSET:3019clk_type = SMU_OD_VDDGFX_OFFSET; break;3020case OD_CCLK:3021clk_type = SMU_OD_CCLK; break;3022case OD_FAN_CURVE:3023clk_type = SMU_OD_FAN_CURVE; break;3024case OD_ACOUSTIC_LIMIT:3025clk_type = SMU_OD_ACOUSTIC_LIMIT; break;3026case OD_ACOUSTIC_TARGET:3027clk_type = SMU_OD_ACOUSTIC_TARGET; break;3028case OD_FAN_TARGET_TEMPERATURE:3029clk_type = SMU_OD_FAN_TARGET_TEMPERATURE; break;3030case OD_FAN_MINIMUM_PWM:3031clk_type = SMU_OD_FAN_MINIMUM_PWM; break;3032case OD_FAN_ZERO_RPM_ENABLE:3033clk_type = SMU_OD_FAN_ZERO_RPM_ENABLE; break;3034case OD_FAN_ZERO_RPM_STOP_TEMP:3035clk_type = SMU_OD_FAN_ZERO_RPM_STOP_TEMP; break;3036default:3037clk_type = SMU_CLK_COUNT; break;3038}30393040return clk_type;3041}30423043static int smu_print_ppclk_levels(void *handle,3044enum pp_clock_type type,3045char *buf)3046{3047struct smu_context *smu = handle;3048enum smu_clk_type clk_type;30493050clk_type = smu_convert_to_smuclk(type);3051if (clk_type == SMU_CLK_COUNT)3052return -EINVAL;30533054return smu_print_smuclk_levels(smu, clk_type, buf);3055}30563057static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)3058{3059struct smu_context *smu = handle;3060enum smu_clk_type clk_type;30613062clk_type = smu_convert_to_smuclk(type);3063if (clk_type == SMU_CLK_COUNT)3064return -EINVAL;30653066if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3067return -EOPNOTSUPP;30683069if (!smu->ppt_funcs->emit_clk_levels)3070return -ENOENT;30713072return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);30733074}30753076static int smu_od_edit_dpm_table(void *handle,3077enum PP_OD_DPM_TABLE_COMMAND type,3078long *input, uint32_t size)3079{3080struct smu_context *smu = handle;3081int ret = 0;30823083if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3084return -EOPNOTSUPP;30853086if (smu->ppt_funcs->od_edit_dpm_table) {3087ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);3088}30893090return ret;3091}30923093static int smu_read_sensor(void *handle,3094int sensor,3095void *data,3096int *size_arg)3097{3098struct smu_context *smu = handle;3099struct amdgpu_device *adev = smu->adev;3100struct smu_umd_pstate_table *pstate_table =3101&smu->pstate_table;3102int i, ret = 0;3103uint32_t *size, size_val;31043105if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3106return -EOPNOTSUPP;31073108if (!data || !size_arg)3109return -EINVAL;31103111size_val = *size_arg;3112size = &size_val;31133114if (smu->ppt_funcs->read_sensor)3115if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))3116goto unlock;31173118switch (sensor) {3119case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:3120*((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;3121*size = 4;3122break;3123case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:3124*((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;3125*size = 4;3126break;3127case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:3128*((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100;3129*size = 4;3130break;3131case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:3132*((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100;3133*size = 4;3134break;3135case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:3136ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);3137*size = 8;3138break;3139case AMDGPU_PP_SENSOR_UVD_POWER:3140*(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;3141*size = 4;3142break;3143case AMDGPU_PP_SENSOR_VCE_POWER:3144*(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;3145*size = 4;3146break;3147case AMDGPU_PP_SENSOR_VCN_POWER_STATE:3148*(uint32_t *)data = 0;3149for (i = 0; i < adev->vcn.num_vcn_inst; i++) {3150if (!atomic_read(&smu->smu_power.power_gate.vcn_gated[i])) {3151*(uint32_t *)data = 1;3152break;3153}3154}3155*size = 4;3156break;3157case AMDGPU_PP_SENSOR_MIN_FAN_RPM:3158*(uint32_t *)data = 0;3159*size = 4;3160break;3161default:3162*size = 0;3163ret = -EOPNOTSUPP;3164break;3165}31663167unlock:3168// assign uint32_t to int3169*size_arg = size_val;31703171return ret;3172}31733174static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit)3175{3176int ret = -EOPNOTSUPP;3177struct smu_context *smu = handle;31783179if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit)3180ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit);31813182return ret;3183}31843185static int smu_set_apu_thermal_limit(void *handle, uint32_t limit)3186{3187int ret = -EOPNOTSUPP;3188struct smu_context *smu = handle;31893190if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit)3191ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit);31923193return ret;3194}31953196static int smu_get_power_profile_mode(void *handle, char *buf)3197{3198struct smu_context *smu = handle;31993200if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||3201!smu->ppt_funcs->get_power_profile_mode)3202return -EOPNOTSUPP;3203if (!buf)3204return -EINVAL;32053206return smu->ppt_funcs->get_power_profile_mode(smu, buf);3207}32083209static int smu_set_power_profile_mode(void *handle,3210long *param,3211uint32_t param_size)3212{3213struct smu_context *smu = handle;3214bool custom = false;3215int ret = 0;32163217if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||3218!smu->ppt_funcs->set_power_profile_mode)3219return -EOPNOTSUPP;32203221if (param[param_size] == PP_SMC_POWER_PROFILE_CUSTOM) {3222custom = true;3223/* clear frontend mask so custom changes propogate */3224smu->workload_mask = 0;3225}32263227if ((param[param_size] != smu->power_profile_mode) || custom) {3228/* clear the old user preference */3229smu_power_profile_mode_put(smu, smu->power_profile_mode);3230/* set the new user preference */3231smu_power_profile_mode_get(smu, param[param_size]);3232ret = smu_bump_power_profile_mode(smu,3233custom ? param : NULL,3234custom ? param_size : 0);3235if (ret)3236smu_power_profile_mode_put(smu, param[param_size]);3237else3238/* store the user's preference */3239smu->power_profile_mode = param[param_size];3240}32413242return ret;3243}32443245static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)3246{3247struct smu_context *smu = handle;32483249if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3250return -EOPNOTSUPP;32513252if (!smu->ppt_funcs->get_fan_control_mode)3253return -EOPNOTSUPP;32543255if (!fan_mode)3256return -EINVAL;32573258*fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);32593260return 0;3261}32623263static int smu_set_fan_control_mode(void *handle, u32 value)3264{3265struct smu_context *smu = handle;3266int ret = 0;32673268if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3269return -EOPNOTSUPP;32703271if (!smu->ppt_funcs->set_fan_control_mode)3272return -EOPNOTSUPP;32733274if (value == U32_MAX)3275return -EINVAL;32763277ret = smu->ppt_funcs->set_fan_control_mode(smu, value);3278if (ret)3279goto out;32803281if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {3282smu->user_dpm_profile.fan_mode = value;32833284/* reset user dpm fan speed */3285if (value != AMD_FAN_CTRL_MANUAL) {3286smu->user_dpm_profile.fan_speed_pwm = 0;3287smu->user_dpm_profile.fan_speed_rpm = 0;3288smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);3289}3290}32913292out:3293return ret;3294}32953296static int smu_get_fan_speed_pwm(void *handle, u32 *speed)3297{3298struct smu_context *smu = handle;3299int ret = 0;33003301if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3302return -EOPNOTSUPP;33033304if (!smu->ppt_funcs->get_fan_speed_pwm)3305return -EOPNOTSUPP;33063307if (!speed)3308return -EINVAL;33093310ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);33113312return ret;3313}33143315static int smu_set_fan_speed_pwm(void *handle, u32 speed)3316{3317struct smu_context *smu = handle;3318int ret = 0;33193320if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3321return -EOPNOTSUPP;33223323if (!smu->ppt_funcs->set_fan_speed_pwm)3324return -EOPNOTSUPP;33253326if (speed == U32_MAX)3327return -EINVAL;33283329ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);3330if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {3331smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;3332smu->user_dpm_profile.fan_speed_pwm = speed;33333334/* Override custom RPM setting as they cannot co-exist */3335smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;3336smu->user_dpm_profile.fan_speed_rpm = 0;3337}33383339return ret;3340}33413342static int smu_get_fan_speed_rpm(void *handle, uint32_t *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->get_fan_speed_rpm)3351return -EOPNOTSUPP;33523353if (!speed)3354return -EINVAL;33553356ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);33573358return ret;3359}33603361static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)3362{3363struct smu_context *smu = handle;33643365if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3366return -EOPNOTSUPP;33673368return smu_set_min_dcef_deep_sleep(smu, clk);3369}33703371static int smu_get_clock_by_type_with_latency(void *handle,3372enum amd_pp_clock_type type,3373struct pp_clock_levels_with_latency *clocks)3374{3375struct smu_context *smu = handle;3376enum smu_clk_type clk_type;3377int ret = 0;33783379if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3380return -EOPNOTSUPP;33813382if (smu->ppt_funcs->get_clock_by_type_with_latency) {3383switch (type) {3384case amd_pp_sys_clock:3385clk_type = SMU_GFXCLK;3386break;3387case amd_pp_mem_clock:3388clk_type = SMU_MCLK;3389break;3390case amd_pp_dcef_clock:3391clk_type = SMU_DCEFCLK;3392break;3393case amd_pp_disp_clock:3394clk_type = SMU_DISPCLK;3395break;3396default:3397dev_err(smu->adev->dev, "Invalid clock type!\n");3398return -EINVAL;3399}34003401ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);3402}34033404return ret;3405}34063407static int smu_display_clock_voltage_request(void *handle,3408struct pp_display_clock_request *clock_req)3409{3410struct smu_context *smu = handle;3411int ret = 0;34123413if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3414return -EOPNOTSUPP;34153416if (smu->ppt_funcs->display_clock_voltage_request)3417ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);34183419return ret;3420}342134223423static int smu_display_disable_memory_clock_switch(void *handle,3424bool disable_memory_clock_switch)3425{3426struct smu_context *smu = handle;3427int ret = -EINVAL;34283429if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3430return -EOPNOTSUPP;34313432if (smu->ppt_funcs->display_disable_memory_clock_switch)3433ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);34343435return ret;3436}34373438static int smu_set_xgmi_pstate(void *handle,3439uint32_t pstate)3440{3441struct smu_context *smu = handle;3442int ret = 0;34433444if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3445return -EOPNOTSUPP;34463447if (smu->ppt_funcs->set_xgmi_pstate)3448ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);34493450if (ret)3451dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");34523453return ret;3454}34553456static int smu_get_baco_capability(void *handle)3457{3458struct smu_context *smu = handle;34593460if (!smu->pm_enabled)3461return false;34623463if (!smu->ppt_funcs || !smu->ppt_funcs->get_bamaco_support)3464return false;34653466return smu->ppt_funcs->get_bamaco_support(smu);3467}34683469static int smu_baco_set_state(void *handle, int state)3470{3471struct smu_context *smu = handle;3472int ret = 0;34733474if (!smu->pm_enabled)3475return -EOPNOTSUPP;34763477if (state == 0) {3478if (smu->ppt_funcs->baco_exit)3479ret = smu->ppt_funcs->baco_exit(smu);3480} else if (state == 1) {3481if (smu->ppt_funcs->baco_enter)3482ret = smu->ppt_funcs->baco_enter(smu);3483} else {3484return -EINVAL;3485}34863487if (ret)3488dev_err(smu->adev->dev, "Failed to %s BACO state!\n",3489(state)?"enter":"exit");34903491return ret;3492}34933494bool smu_mode1_reset_is_support(struct smu_context *smu)3495{3496bool ret = false;34973498if (!smu->pm_enabled)3499return false;35003501if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)3502ret = smu->ppt_funcs->mode1_reset_is_support(smu);35033504return ret;3505}35063507bool smu_link_reset_is_support(struct smu_context *smu)3508{3509bool ret = false;35103511if (!smu->pm_enabled)3512return false;35133514if (smu->ppt_funcs && smu->ppt_funcs->link_reset_is_support)3515ret = smu->ppt_funcs->link_reset_is_support(smu);35163517return ret;3518}35193520int smu_mode1_reset(struct smu_context *smu)3521{3522int ret = 0;35233524if (!smu->pm_enabled)3525return -EOPNOTSUPP;35263527if (smu->ppt_funcs->mode1_reset)3528ret = smu->ppt_funcs->mode1_reset(smu);35293530return ret;3531}35323533static int smu_mode2_reset(void *handle)3534{3535struct smu_context *smu = handle;3536int ret = 0;35373538if (!smu->pm_enabled)3539return -EOPNOTSUPP;35403541if (smu->ppt_funcs->mode2_reset)3542ret = smu->ppt_funcs->mode2_reset(smu);35433544if (ret)3545dev_err(smu->adev->dev, "Mode2 reset failed!\n");35463547return ret;3548}35493550int smu_link_reset(struct smu_context *smu)3551{3552int ret = 0;35533554if (!smu->pm_enabled)3555return -EOPNOTSUPP;35563557if (smu->ppt_funcs->link_reset)3558ret = smu->ppt_funcs->link_reset(smu);35593560return ret;3561}35623563static int smu_enable_gfx_features(void *handle)3564{3565struct smu_context *smu = handle;3566int ret = 0;35673568if (!smu->pm_enabled)3569return -EOPNOTSUPP;35703571if (smu->ppt_funcs->enable_gfx_features)3572ret = smu->ppt_funcs->enable_gfx_features(smu);35733574if (ret)3575dev_err(smu->adev->dev, "enable gfx features failed!\n");35763577return ret;3578}35793580static int smu_get_max_sustainable_clocks_by_dc(void *handle,3581struct pp_smu_nv_clock_table *max_clocks)3582{3583struct smu_context *smu = handle;3584int ret = 0;35853586if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3587return -EOPNOTSUPP;35883589if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)3590ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);35913592return ret;3593}35943595static int smu_get_uclk_dpm_states(void *handle,3596unsigned int *clock_values_in_khz,3597unsigned int *num_states)3598{3599struct smu_context *smu = handle;3600int ret = 0;36013602if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3603return -EOPNOTSUPP;36043605if (smu->ppt_funcs->get_uclk_dpm_states)3606ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);36073608return ret;3609}36103611static enum amd_pm_state_type smu_get_current_power_state(void *handle)3612{3613struct smu_context *smu = handle;3614enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;36153616if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3617return -EOPNOTSUPP;36183619if (smu->ppt_funcs->get_current_power_state)3620pm_state = smu->ppt_funcs->get_current_power_state(smu);36213622return pm_state;3623}36243625static int smu_get_dpm_clock_table(void *handle,3626struct dpm_clocks *clock_table)3627{3628struct smu_context *smu = handle;3629int ret = 0;36303631if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3632return -EOPNOTSUPP;36333634if (smu->ppt_funcs->get_dpm_clock_table)3635ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);36363637return ret;3638}36393640static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)3641{3642struct smu_context *smu = handle;36433644if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3645return -EOPNOTSUPP;36463647if (!smu->ppt_funcs->get_gpu_metrics)3648return -EOPNOTSUPP;36493650return smu->ppt_funcs->get_gpu_metrics(smu, table);3651}36523653static ssize_t smu_sys_get_pm_metrics(void *handle, void *pm_metrics,3654size_t size)3655{3656struct smu_context *smu = handle;36573658if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3659return -EOPNOTSUPP;36603661if (!smu->ppt_funcs->get_pm_metrics)3662return -EOPNOTSUPP;36633664return smu->ppt_funcs->get_pm_metrics(smu, pm_metrics, size);3665}36663667static int smu_enable_mgpu_fan_boost(void *handle)3668{3669struct smu_context *smu = handle;3670int ret = 0;36713672if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3673return -EOPNOTSUPP;36743675if (smu->ppt_funcs->enable_mgpu_fan_boost)3676ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);36773678return ret;3679}36803681static int smu_gfx_state_change_set(void *handle,3682uint32_t state)3683{3684struct smu_context *smu = handle;3685int ret = 0;36863687if (smu->ppt_funcs->gfx_state_change_set)3688ret = smu->ppt_funcs->gfx_state_change_set(smu, state);36893690return ret;3691}36923693int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)3694{3695int ret = 0;36963697if (smu->ppt_funcs->smu_handle_passthrough_sbr)3698ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);36993700return ret;3701}37023703int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)3704{3705int ret = -EOPNOTSUPP;37063707if (smu->ppt_funcs &&3708smu->ppt_funcs->get_ecc_info)3709ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);37103711return ret;37123713}37143715static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)3716{3717struct smu_context *smu = handle;3718struct smu_table_context *smu_table = &smu->smu_table;3719struct smu_table *memory_pool = &smu_table->memory_pool;37203721if (!addr || !size)3722return -EINVAL;37233724*addr = NULL;3725*size = 0;3726if (memory_pool->bo) {3727*addr = memory_pool->cpu_addr;3728*size = memory_pool->size;3729}37303731return 0;3732}37333734static void smu_print_dpm_policy(struct smu_dpm_policy *policy, char *sysbuf,3735size_t *size)3736{3737size_t offset = *size;3738int level;37393740for_each_set_bit(level, &policy->level_mask, PP_POLICY_MAX_LEVELS) {3741if (level == policy->current_level)3742offset += sysfs_emit_at(sysbuf, offset,3743"%d : %s*\n", level,3744policy->desc->get_desc(policy, level));3745else3746offset += sysfs_emit_at(sysbuf, offset,3747"%d : %s\n", level,3748policy->desc->get_desc(policy, level));3749}37503751*size = offset;3752}37533754ssize_t smu_get_pm_policy_info(struct smu_context *smu,3755enum pp_pm_policy p_type, char *sysbuf)3756{3757struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;3758struct smu_dpm_policy_ctxt *policy_ctxt;3759struct smu_dpm_policy *dpm_policy;3760size_t offset = 0;37613762policy_ctxt = dpm_ctxt->dpm_policies;3763if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||3764!policy_ctxt->policy_mask)3765return -EOPNOTSUPP;37663767if (p_type == PP_PM_POLICY_NONE)3768return -EINVAL;37693770dpm_policy = smu_get_pm_policy(smu, p_type);3771if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->desc)3772return -ENOENT;37733774if (!sysbuf)3775return -EINVAL;37763777smu_print_dpm_policy(dpm_policy, sysbuf, &offset);37783779return offset;3780}37813782struct smu_dpm_policy *smu_get_pm_policy(struct smu_context *smu,3783enum pp_pm_policy p_type)3784{3785struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;3786struct smu_dpm_policy_ctxt *policy_ctxt;3787int i;37883789policy_ctxt = dpm_ctxt->dpm_policies;3790if (!policy_ctxt)3791return NULL;37923793for (i = 0; i < hweight32(policy_ctxt->policy_mask); ++i) {3794if (policy_ctxt->policies[i].policy_type == p_type)3795return &policy_ctxt->policies[i];3796}37973798return NULL;3799}38003801int smu_set_pm_policy(struct smu_context *smu, enum pp_pm_policy p_type,3802int level)3803{3804struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;3805struct smu_dpm_policy *dpm_policy = NULL;3806struct smu_dpm_policy_ctxt *policy_ctxt;3807int ret = -EOPNOTSUPP;38083809policy_ctxt = dpm_ctxt->dpm_policies;3810if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||3811!policy_ctxt->policy_mask)3812return ret;38133814if (level < 0 || level >= PP_POLICY_MAX_LEVELS)3815return -EINVAL;38163817dpm_policy = smu_get_pm_policy(smu, p_type);38183819if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->set_policy)3820return ret;38213822if (dpm_policy->current_level == level)3823return 0;38243825ret = dpm_policy->set_policy(smu, level);38263827if (!ret)3828dpm_policy->current_level = level;38293830return ret;3831}38323833static ssize_t smu_sys_get_xcp_metrics(void *handle, int xcp_id, void *table)3834{3835struct smu_context *smu = handle;38363837if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)3838return -EOPNOTSUPP;38393840if (!smu->adev->xcp_mgr || !smu->ppt_funcs->get_xcp_metrics)3841return -EOPNOTSUPP;38423843return smu->ppt_funcs->get_xcp_metrics(smu, xcp_id, table);3844}38453846static const struct amd_pm_funcs swsmu_pm_funcs = {3847/* export for sysfs */3848.set_fan_control_mode = smu_set_fan_control_mode,3849.get_fan_control_mode = smu_get_fan_control_mode,3850.set_fan_speed_pwm = smu_set_fan_speed_pwm,3851.get_fan_speed_pwm = smu_get_fan_speed_pwm,3852.force_clock_level = smu_force_ppclk_levels,3853.print_clock_levels = smu_print_ppclk_levels,3854.emit_clock_levels = smu_emit_ppclk_levels,3855.force_performance_level = smu_force_performance_level,3856.read_sensor = smu_read_sensor,3857.get_apu_thermal_limit = smu_get_apu_thermal_limit,3858.set_apu_thermal_limit = smu_set_apu_thermal_limit,3859.get_performance_level = smu_get_performance_level,3860.get_current_power_state = smu_get_current_power_state,3861.get_fan_speed_rpm = smu_get_fan_speed_rpm,3862.set_fan_speed_rpm = smu_set_fan_speed_rpm,3863.get_pp_num_states = smu_get_power_num_states,3864.get_pp_table = smu_sys_get_pp_table,3865.set_pp_table = smu_sys_set_pp_table,3866.switch_power_profile = smu_switch_power_profile,3867.pause_power_profile = smu_pause_power_profile,3868/* export to amdgpu */3869.dispatch_tasks = smu_handle_dpm_task,3870.load_firmware = smu_load_microcode,3871.set_powergating_by_smu = smu_dpm_set_power_gate,3872.set_power_limit = smu_set_power_limit,3873.get_power_limit = smu_get_power_limit,3874.get_power_profile_mode = smu_get_power_profile_mode,3875.set_power_profile_mode = smu_set_power_profile_mode,3876.odn_edit_dpm_table = smu_od_edit_dpm_table,3877.set_mp1_state = smu_set_mp1_state,3878.gfx_state_change_set = smu_gfx_state_change_set,3879/* export to DC */3880.get_sclk = smu_get_sclk,3881.get_mclk = smu_get_mclk,3882.display_configuration_change = smu_display_configuration_change,3883.get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,3884.display_clock_voltage_request = smu_display_clock_voltage_request,3885.enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,3886.set_active_display_count = smu_set_display_count,3887.set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,3888.get_asic_baco_capability = smu_get_baco_capability,3889.set_asic_baco_state = smu_baco_set_state,3890.get_ppfeature_status = smu_sys_get_pp_feature_mask,3891.set_ppfeature_status = smu_sys_set_pp_feature_mask,3892.asic_reset_mode_2 = smu_mode2_reset,3893.asic_reset_enable_gfx_features = smu_enable_gfx_features,3894.set_df_cstate = smu_set_df_cstate,3895.set_xgmi_pstate = smu_set_xgmi_pstate,3896.get_gpu_metrics = smu_sys_get_gpu_metrics,3897.get_pm_metrics = smu_sys_get_pm_metrics,3898.set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,3899.display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,3900.get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,3901.get_uclk_dpm_states = smu_get_uclk_dpm_states,3902.get_dpm_clock_table = smu_get_dpm_clock_table,3903.get_smu_prv_buf_details = smu_get_prv_buffer_details,3904.get_xcp_metrics = smu_sys_get_xcp_metrics,3905};39063907int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,3908uint64_t event_arg)3909{3910int ret = -EINVAL;39113912if (smu->ppt_funcs->wait_for_event)3913ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);39143915return ret;3916}39173918int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)3919{39203921if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)3922return -EOPNOTSUPP;39233924/* Confirm the buffer allocated is of correct size */3925if (size != smu->stb_context.stb_buf_size)3926return -EINVAL;39273928/*3929* No need to lock smu mutex as we access STB directly through MMIO3930* and not going through SMU messaging route (for now at least).3931* For registers access rely on implementation internal locking.3932*/3933return smu->ppt_funcs->stb_collect_info(smu, buf, size);3934}39353936#if defined(CONFIG_DEBUG_FS)39373938static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)3939{3940struct amdgpu_device *adev = filp->f_inode->i_private;3941struct smu_context *smu = adev->powerplay.pp_handle;3942unsigned char *buf;3943int r;39443945buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);3946if (!buf)3947return -ENOMEM;39483949r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);3950if (r)3951goto out;39523953filp->private_data = buf;39543955return 0;39563957out:3958kvfree(buf);3959return r;3960}39613962static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,3963loff_t *pos)3964{3965struct amdgpu_device *adev = filp->f_inode->i_private;3966struct smu_context *smu = adev->powerplay.pp_handle;396739683969if (!filp->private_data)3970return -EINVAL;39713972return simple_read_from_buffer(buf,3973size,3974pos, filp->private_data,3975smu->stb_context.stb_buf_size);3976}39773978static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)3979{3980kvfree(filp->private_data);3981filp->private_data = NULL;39823983return 0;3984}39853986/*3987* We have to define not only read method but also3988* open and release because .read takes up to PAGE_SIZE3989* data each time so and so is invoked multiple times.3990* We allocate the STB buffer in .open and release it3991* in .release3992*/3993static const struct file_operations smu_stb_debugfs_fops = {3994.owner = THIS_MODULE,3995.open = smu_stb_debugfs_open,3996.read = smu_stb_debugfs_read,3997.release = smu_stb_debugfs_release,3998.llseek = default_llseek,3999};40004001#endif40024003void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)4004{4005#if defined(CONFIG_DEBUG_FS)40064007struct smu_context *smu = adev->powerplay.pp_handle;40084009if (!smu || (!smu->stb_context.stb_buf_size))4010return;40114012debugfs_create_file_size("amdgpu_smu_stb_dump",4013S_IRUSR,4014adev_to_drm(adev)->primary->debugfs_root,4015adev,4016&smu_stb_debugfs_fops,4017smu->stb_context.stb_buf_size);4018#endif4019}40204021int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)4022{4023int ret = 0;40244025if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)4026ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);40274028return ret;4029}40304031int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)4032{4033int ret = 0;40344035if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)4036ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);40374038return ret;4039}40404041int smu_send_rma_reason(struct smu_context *smu)4042{4043int ret = 0;40444045if (smu->ppt_funcs && smu->ppt_funcs->send_rma_reason)4046ret = smu->ppt_funcs->send_rma_reason(smu);40474048return ret;4049}40504051/**4052* smu_reset_sdma_is_supported - Check if SDMA reset is supported by SMU4053* @smu: smu_context pointer4054*4055* This function checks if the SMU supports resetting the SDMA engine.4056* It returns true if supported, false otherwise.4057*/4058bool smu_reset_sdma_is_supported(struct smu_context *smu)4059{4060bool ret = false;40614062if (smu->ppt_funcs && smu->ppt_funcs->reset_sdma_is_supported)4063ret = smu->ppt_funcs->reset_sdma_is_supported(smu);40644065return ret;4066}40674068int smu_reset_sdma(struct smu_context *smu, uint32_t inst_mask)4069{4070int ret = 0;40714072if (smu->ppt_funcs && smu->ppt_funcs->reset_sdma)4073ret = smu->ppt_funcs->reset_sdma(smu, inst_mask);40744075return ret;4076}40774078int smu_reset_vcn(struct smu_context *smu, uint32_t inst_mask)4079{4080if (smu->ppt_funcs && smu->ppt_funcs->dpm_reset_vcn)4081smu->ppt_funcs->dpm_reset_vcn(smu, inst_mask);40824083return 0;4084}408540864087