2 * Copyright 2019 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #define SWSMU_CODE_LAYER_L1
25 #include <linux/firmware.h>
26 #include <linux/pci.h>
29 #include "amdgpu_smu.h"
30 #include "smu_internal.h"
32 #include "arcturus_ppt.h"
33 #include "navi10_ppt.h"
34 #include "sienna_cichlid_ppt.h"
35 #include "renoir_ppt.h"
36 #include "vangogh_ppt.h"
37 #include "aldebaran_ppt.h"
38 #include "yellow_carp_ppt.h"
39 #include "cyan_skillfish_ppt.h"
40 #include "smu_v13_0_5_ppt.h"
44 * DO NOT use these for err/warn/info/debug messages.
45 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
46 * They are more MGPU friendly.
53 static const struct amd_pm_funcs swsmu_pm_funcs;
54 static int smu_force_smuclk_levels(struct smu_context *smu,
55 enum smu_clk_type clk_type,
57 static int smu_handle_task(struct smu_context *smu,
58 enum amd_dpm_forced_level level,
59 enum amd_pp_task task_id);
60 static int smu_reset(struct smu_context *smu);
61 static int smu_set_fan_speed_pwm(void *handle, u32 speed);
62 static int smu_set_fan_control_mode(void *handle, u32 value);
63 static int smu_set_power_limit(void *handle, uint32_t limit);
64 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);
65 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);
67 static int smu_sys_get_pp_feature_mask(void *handle,
70 struct smu_context *smu = handle;
72 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
75 return smu_get_pp_feature_mask(smu, buf);
78 static int smu_sys_set_pp_feature_mask(void *handle,
81 struct smu_context *smu = handle;
83 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
86 return smu_set_pp_feature_mask(smu, new_mask);
89 int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value)
91 if (!smu->ppt_funcs->get_gfx_off_status)
94 *value = smu_get_gfx_off_status(smu);
99 int smu_set_soft_freq_range(struct smu_context *smu,
100 enum smu_clk_type clk_type,
106 if (smu->ppt_funcs->set_soft_freq_limited_range)
107 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
115 int smu_get_dpm_freq_range(struct smu_context *smu,
116 enum smu_clk_type clk_type,
125 if (smu->ppt_funcs->get_dpm_ultimate_freq)
126 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
134 static u32 smu_get_mclk(void *handle, bool low)
136 struct smu_context *smu = handle;
140 ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
141 low ? &clk_freq : NULL,
142 !low ? &clk_freq : NULL);
145 return clk_freq * 100;
148 static u32 smu_get_sclk(void *handle, bool low)
150 struct smu_context *smu = handle;
154 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
155 low ? &clk_freq : NULL,
156 !low ? &clk_freq : NULL);
159 return clk_freq * 100;
162 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
165 struct smu_power_context *smu_power = &smu->smu_power;
166 struct smu_power_gate *power_gate = &smu_power->power_gate;
169 if (!smu->ppt_funcs->dpm_set_vcn_enable)
172 if (atomic_read(&power_gate->vcn_gated) ^ enable)
175 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
177 atomic_set(&power_gate->vcn_gated, !enable);
182 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
185 struct smu_power_context *smu_power = &smu->smu_power;
186 struct smu_power_gate *power_gate = &smu_power->power_gate;
189 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
192 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
195 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
197 atomic_set(&power_gate->jpeg_gated, !enable);
203 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
205 * @handle: smu_context pointer
206 * @block_type: the IP block to power gate/ungate
207 * @gate: to power gate if true, ungate otherwise
209 * This API uses no smu->mutex lock protection due to:
210 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
211 * This is guarded to be race condition free by the caller.
212 * 2. Or get called on user setting request of power_dpm_force_performance_level.
213 * Under this case, the smu->mutex lock protection is already enforced on
214 * the parent API smu_force_performance_level of the call path.
216 static int smu_dpm_set_power_gate(void *handle,
220 struct smu_context *smu = handle;
223 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) {
224 dev_WARN(smu->adev->dev,
225 "SMU uninitialized but power %s requested for %u!\n",
226 gate ? "gate" : "ungate", block_type);
230 switch (block_type) {
232 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
233 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
235 case AMD_IP_BLOCK_TYPE_UVD:
236 case AMD_IP_BLOCK_TYPE_VCN:
237 ret = smu_dpm_set_vcn_enable(smu, !gate);
239 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
240 gate ? "gate" : "ungate");
242 case AMD_IP_BLOCK_TYPE_GFX:
243 ret = smu_gfx_off_control(smu, gate);
245 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
246 gate ? "enable" : "disable");
248 case AMD_IP_BLOCK_TYPE_SDMA:
249 ret = smu_powergate_sdma(smu, gate);
251 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
252 gate ? "gate" : "ungate");
254 case AMD_IP_BLOCK_TYPE_JPEG:
255 ret = smu_dpm_set_jpeg_enable(smu, !gate);
257 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
258 gate ? "gate" : "ungate");
261 dev_err(smu->adev->dev, "Unsupported block type!\n");
269 * smu_set_user_clk_dependencies - set user profile clock dependencies
271 * @smu: smu_context pointer
272 * @clk: enum smu_clk_type type
274 * Enable/Disable the clock dependency for the @clk type.
276 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
278 if (smu->adev->in_suspend)
281 if (clk == SMU_MCLK) {
282 smu->user_dpm_profile.clk_dependency = 0;
283 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
284 } else if (clk == SMU_FCLK) {
285 /* MCLK takes precedence over FCLK */
286 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
289 smu->user_dpm_profile.clk_dependency = 0;
290 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
291 } else if (clk == SMU_SOCCLK) {
292 /* MCLK takes precedence over SOCCLK */
293 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
296 smu->user_dpm_profile.clk_dependency = 0;
297 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
299 /* Add clk dependencies here, if any */
304 * smu_restore_dpm_user_profile - reinstate user dpm profile
306 * @smu: smu_context pointer
308 * Restore the saved user power configurations include power limit,
309 * clock frequencies, fan control mode and fan speed.
311 static void smu_restore_dpm_user_profile(struct smu_context *smu)
313 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
316 if (!smu->adev->in_suspend)
319 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
322 /* Enable restore flag */
323 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
325 /* set the user dpm power limit */
326 if (smu->user_dpm_profile.power_limit) {
327 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
329 dev_err(smu->adev->dev, "Failed to set power limit value\n");
332 /* set the user dpm clock configurations */
333 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
334 enum smu_clk_type clk_type;
336 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
338 * Iterate over smu clk type and force the saved user clk
339 * configs, skip if clock dependency is enabled
341 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
342 smu->user_dpm_profile.clk_mask[clk_type]) {
343 ret = smu_force_smuclk_levels(smu, clk_type,
344 smu->user_dpm_profile.clk_mask[clk_type]);
346 dev_err(smu->adev->dev,
347 "Failed to set clock type = %d\n", clk_type);
352 /* set the user dpm fan configurations */
353 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||
354 smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {
355 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
356 if (ret != -EOPNOTSUPP) {
357 smu->user_dpm_profile.fan_speed_pwm = 0;
358 smu->user_dpm_profile.fan_speed_rpm = 0;
359 smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;
360 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
363 if (smu->user_dpm_profile.fan_speed_pwm) {
364 ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);
365 if (ret != -EOPNOTSUPP)
366 dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");
369 if (smu->user_dpm_profile.fan_speed_rpm) {
370 ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);
371 if (ret != -EOPNOTSUPP)
372 dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");
376 /* Restore user customized OD settings */
377 if (smu->user_dpm_profile.user_od) {
378 if (smu->ppt_funcs->restore_user_od_settings) {
379 ret = smu->ppt_funcs->restore_user_od_settings(smu);
381 dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");
385 /* Disable restore flag */
386 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
389 static int smu_get_power_num_states(void *handle,
390 struct pp_states_info *state_info)
395 /* not support power state */
396 memset(state_info, 0, sizeof(struct pp_states_info));
397 state_info->nums = 1;
398 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
403 bool is_support_sw_smu(struct amdgpu_device *adev)
405 /* vega20 is 11.0.2, but it's supported via the powerplay code */
406 if (adev->asic_type == CHIP_VEGA20)
409 if (adev->ip_versions[MP1_HWIP][0] >= IP_VERSION(11, 0, 0))
415 bool is_support_cclk_dpm(struct amdgpu_device *adev)
417 struct smu_context *smu = adev->powerplay.pp_handle;
419 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
426 static int smu_sys_get_pp_table(void *handle,
429 struct smu_context *smu = handle;
430 struct smu_table_context *smu_table = &smu->smu_table;
432 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
435 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
438 if (smu_table->hardcode_pptable)
439 *table = smu_table->hardcode_pptable;
441 *table = smu_table->power_play_table;
443 return smu_table->power_play_table_size;
446 static int smu_sys_set_pp_table(void *handle,
450 struct smu_context *smu = handle;
451 struct smu_table_context *smu_table = &smu->smu_table;
452 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
455 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
458 if (header->usStructureSize != size) {
459 dev_err(smu->adev->dev, "pp table size not matched !\n");
463 if (!smu_table->hardcode_pptable) {
464 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
465 if (!smu_table->hardcode_pptable)
469 memcpy(smu_table->hardcode_pptable, buf, size);
470 smu_table->power_play_table = smu_table->hardcode_pptable;
471 smu_table->power_play_table_size = size;
474 * Special hw_fini action(for Navi1x, the DPMs disablement will be
475 * skipped) may be needed for custom pptable uploading.
477 smu->uploading_custom_pp_table = true;
479 ret = smu_reset(smu);
481 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
483 smu->uploading_custom_pp_table = false;
488 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
490 struct smu_feature *feature = &smu->smu_feature;
492 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
494 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
496 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
501 bitmap_or(feature->allowed, feature->allowed,
502 (unsigned long *)allowed_feature_mask,
503 feature->feature_num);
508 static int smu_set_funcs(struct amdgpu_device *adev)
510 struct smu_context *smu = adev->powerplay.pp_handle;
512 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
513 smu->od_enabled = true;
515 switch (adev->ip_versions[MP1_HWIP][0]) {
516 case IP_VERSION(11, 0, 0):
517 case IP_VERSION(11, 0, 5):
518 case IP_VERSION(11, 0, 9):
519 navi10_set_ppt_funcs(smu);
521 case IP_VERSION(11, 0, 7):
522 case IP_VERSION(11, 0, 11):
523 case IP_VERSION(11, 0, 12):
524 case IP_VERSION(11, 0, 13):
525 sienna_cichlid_set_ppt_funcs(smu);
527 case IP_VERSION(12, 0, 0):
528 case IP_VERSION(12, 0, 1):
529 renoir_set_ppt_funcs(smu);
531 case IP_VERSION(11, 5, 0):
532 vangogh_set_ppt_funcs(smu);
534 case IP_VERSION(13, 0, 1):
535 case IP_VERSION(13, 0, 3):
536 case IP_VERSION(13, 0, 8):
537 yellow_carp_set_ppt_funcs(smu);
539 case IP_VERSION(13, 0, 5):
540 smu_v13_0_5_set_ppt_funcs(smu);
542 case IP_VERSION(11, 0, 8):
543 cyan_skillfish_set_ppt_funcs(smu);
545 case IP_VERSION(11, 0, 2):
546 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
547 arcturus_set_ppt_funcs(smu);
548 /* OD is not supported on Arcturus */
549 smu->od_enabled =false;
551 case IP_VERSION(13, 0, 2):
552 aldebaran_set_ppt_funcs(smu);
553 /* Enable pp_od_clk_voltage node */
554 smu->od_enabled = true;
563 static int smu_early_init(void *handle)
565 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
566 struct smu_context *smu;
568 smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL);
573 smu->pm_enabled = !!amdgpu_dpm;
575 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
576 smu->smu_baco.platform_support = false;
577 smu->user_dpm_profile.fan_mode = -1;
579 adev->powerplay.pp_handle = smu;
580 adev->powerplay.pp_funcs = &swsmu_pm_funcs;
582 return smu_set_funcs(adev);
585 static int smu_set_default_dpm_table(struct smu_context *smu)
587 struct smu_power_context *smu_power = &smu->smu_power;
588 struct smu_power_gate *power_gate = &smu_power->power_gate;
589 int vcn_gate, jpeg_gate;
592 if (!smu->ppt_funcs->set_default_dpm_table)
595 vcn_gate = atomic_read(&power_gate->vcn_gated);
596 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
598 ret = smu_dpm_set_vcn_enable(smu, true);
602 ret = smu_dpm_set_jpeg_enable(smu, true);
606 ret = smu->ppt_funcs->set_default_dpm_table(smu);
608 dev_err(smu->adev->dev,
609 "Failed to setup default dpm clock tables!\n");
611 smu_dpm_set_jpeg_enable(smu, !jpeg_gate);
613 smu_dpm_set_vcn_enable(smu, !vcn_gate);
617 static int smu_apply_default_config_table_settings(struct smu_context *smu)
619 struct amdgpu_device *adev = smu->adev;
622 ret = smu_get_default_config_table_settings(smu,
623 &adev->pm.config_table);
627 return smu_set_config_table(smu, &adev->pm.config_table);
630 static int smu_late_init(void *handle)
632 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
633 struct smu_context *smu = adev->powerplay.pp_handle;
636 smu_set_fine_grain_gfx_freq_parameters(smu);
638 if (!smu->pm_enabled)
641 ret = smu_post_init(smu);
643 dev_err(adev->dev, "Failed to post smu init!\n");
647 if ((adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 1)) ||
648 (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 3)))
651 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
652 ret = smu_set_default_od_settings(smu);
654 dev_err(adev->dev, "Failed to setup default OD settings!\n");
659 ret = smu_populate_umd_state_clk(smu);
661 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
665 ret = smu_get_asic_power_limits(smu,
666 &smu->current_power_limit,
667 &smu->default_power_limit,
668 &smu->max_power_limit);
670 dev_err(adev->dev, "Failed to get asic power limits!\n");
674 if (!amdgpu_sriov_vf(adev))
675 smu_get_unique_id(smu);
677 smu_get_fan_parameters(smu);
680 smu->smu_dpm.dpm_level,
681 AMD_PP_TASK_COMPLETE_INIT);
683 ret = smu_apply_default_config_table_settings(smu);
684 if (ret && (ret != -EOPNOTSUPP)) {
685 dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n");
689 smu_restore_dpm_user_profile(smu);
694 static int smu_init_fb_allocations(struct smu_context *smu)
696 struct amdgpu_device *adev = smu->adev;
697 struct smu_table_context *smu_table = &smu->smu_table;
698 struct smu_table *tables = smu_table->tables;
699 struct smu_table *driver_table = &(smu_table->driver_table);
700 uint32_t max_table_size = 0;
703 /* VRAM allocation for tool table */
704 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
705 ret = amdgpu_bo_create_kernel(adev,
706 tables[SMU_TABLE_PMSTATUSLOG].size,
707 tables[SMU_TABLE_PMSTATUSLOG].align,
708 tables[SMU_TABLE_PMSTATUSLOG].domain,
709 &tables[SMU_TABLE_PMSTATUSLOG].bo,
710 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
711 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
713 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
718 /* VRAM allocation for driver table */
719 for (i = 0; i < SMU_TABLE_COUNT; i++) {
720 if (tables[i].size == 0)
723 if (i == SMU_TABLE_PMSTATUSLOG)
726 if (max_table_size < tables[i].size)
727 max_table_size = tables[i].size;
730 driver_table->size = max_table_size;
731 driver_table->align = PAGE_SIZE;
732 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
734 ret = amdgpu_bo_create_kernel(adev,
737 driver_table->domain,
739 &driver_table->mc_address,
740 &driver_table->cpu_addr);
742 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
743 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
744 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
745 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
746 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
752 static int smu_fini_fb_allocations(struct smu_context *smu)
754 struct smu_table_context *smu_table = &smu->smu_table;
755 struct smu_table *tables = smu_table->tables;
756 struct smu_table *driver_table = &(smu_table->driver_table);
758 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
759 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
760 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
761 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
763 amdgpu_bo_free_kernel(&driver_table->bo,
764 &driver_table->mc_address,
765 &driver_table->cpu_addr);
771 * smu_alloc_memory_pool - allocate memory pool in the system memory
773 * @smu: amdgpu_device pointer
775 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
776 * and DramLogSetDramAddr can notify it changed.
778 * Returns 0 on success, error on failure.
780 static int smu_alloc_memory_pool(struct smu_context *smu)
782 struct amdgpu_device *adev = smu->adev;
783 struct smu_table_context *smu_table = &smu->smu_table;
784 struct smu_table *memory_pool = &smu_table->memory_pool;
785 uint64_t pool_size = smu->pool_size;
788 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
791 memory_pool->size = pool_size;
792 memory_pool->align = PAGE_SIZE;
793 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
796 case SMU_MEMORY_POOL_SIZE_256_MB:
797 case SMU_MEMORY_POOL_SIZE_512_MB:
798 case SMU_MEMORY_POOL_SIZE_1_GB:
799 case SMU_MEMORY_POOL_SIZE_2_GB:
800 ret = amdgpu_bo_create_kernel(adev,
805 &memory_pool->mc_address,
806 &memory_pool->cpu_addr);
808 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
817 static int smu_free_memory_pool(struct smu_context *smu)
819 struct smu_table_context *smu_table = &smu->smu_table;
820 struct smu_table *memory_pool = &smu_table->memory_pool;
822 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
825 amdgpu_bo_free_kernel(&memory_pool->bo,
826 &memory_pool->mc_address,
827 &memory_pool->cpu_addr);
829 memset(memory_pool, 0, sizeof(struct smu_table));
834 static int smu_alloc_dummy_read_table(struct smu_context *smu)
836 struct smu_table_context *smu_table = &smu->smu_table;
837 struct smu_table *dummy_read_1_table =
838 &smu_table->dummy_read_1_table;
839 struct amdgpu_device *adev = smu->adev;
842 dummy_read_1_table->size = 0x40000;
843 dummy_read_1_table->align = PAGE_SIZE;
844 dummy_read_1_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
846 ret = amdgpu_bo_create_kernel(adev,
847 dummy_read_1_table->size,
848 dummy_read_1_table->align,
849 dummy_read_1_table->domain,
850 &dummy_read_1_table->bo,
851 &dummy_read_1_table->mc_address,
852 &dummy_read_1_table->cpu_addr);
854 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
859 static void smu_free_dummy_read_table(struct smu_context *smu)
861 struct smu_table_context *smu_table = &smu->smu_table;
862 struct smu_table *dummy_read_1_table =
863 &smu_table->dummy_read_1_table;
866 amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
867 &dummy_read_1_table->mc_address,
868 &dummy_read_1_table->cpu_addr);
870 memset(dummy_read_1_table, 0, sizeof(struct smu_table));
873 static int smu_smc_table_sw_init(struct smu_context *smu)
878 * Create smu_table structure, and init smc tables such as
879 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
881 ret = smu_init_smc_tables(smu);
883 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
888 * Create smu_power_context structure, and allocate smu_dpm_context and
889 * context size to fill the smu_power_context data.
891 ret = smu_init_power(smu);
893 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
898 * allocate vram bos to store smc table contents.
900 ret = smu_init_fb_allocations(smu);
904 ret = smu_alloc_memory_pool(smu);
908 ret = smu_alloc_dummy_read_table(smu);
912 ret = smu_i2c_init(smu);
919 static int smu_smc_table_sw_fini(struct smu_context *smu)
925 smu_free_dummy_read_table(smu);
927 ret = smu_free_memory_pool(smu);
931 ret = smu_fini_fb_allocations(smu);
935 ret = smu_fini_power(smu);
937 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
941 ret = smu_fini_smc_tables(smu);
943 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
950 static void smu_throttling_logging_work_fn(struct work_struct *work)
952 struct smu_context *smu = container_of(work, struct smu_context,
953 throttling_logging_work);
955 smu_log_thermal_throttling(smu);
958 static void smu_interrupt_work_fn(struct work_struct *work)
960 struct smu_context *smu = container_of(work, struct smu_context,
963 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
964 smu->ppt_funcs->interrupt_work(smu);
967 static int smu_sw_init(void *handle)
969 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
970 struct smu_context *smu = adev->powerplay.pp_handle;
973 smu->pool_size = adev->pm.smu_prv_buffer_size;
974 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
975 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
976 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
978 mutex_init(&smu->message_lock);
980 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
981 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
982 atomic64_set(&smu->throttle_int_counter, 0);
983 smu->watermarks_bitmap = 0;
984 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
985 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
987 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
988 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
990 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
991 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
992 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
993 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
994 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
995 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
996 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
997 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
999 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1000 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1001 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
1002 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
1003 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
1004 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
1005 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
1006 smu->display_config = &adev->pm.pm_display_cfg;
1008 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1009 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1011 ret = smu_init_microcode(smu);
1013 dev_err(adev->dev, "Failed to load smu firmware!\n");
1017 ret = smu_smc_table_sw_init(smu);
1019 dev_err(adev->dev, "Failed to sw init smc table!\n");
1023 ret = smu_register_irq_handler(smu);
1025 dev_err(adev->dev, "Failed to register smc irq handler!\n");
1029 /* If there is no way to query fan control mode, fan control is not supported */
1030 if (!smu->ppt_funcs->get_fan_control_mode)
1031 smu->adev->pm.no_fan = true;
1036 static int smu_sw_fini(void *handle)
1038 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1039 struct smu_context *smu = adev->powerplay.pp_handle;
1042 ret = smu_smc_table_sw_fini(smu);
1044 dev_err(adev->dev, "Failed to sw fini smc table!\n");
1048 smu_fini_microcode(smu);
1053 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1055 struct amdgpu_device *adev = smu->adev;
1056 struct smu_temperature_range *range =
1057 &smu->thermal_range;
1060 if (!smu->ppt_funcs->get_thermal_temperature_range)
1063 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1067 adev->pm.dpm.thermal.min_temp = range->min;
1068 adev->pm.dpm.thermal.max_temp = range->max;
1069 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1070 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1071 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1072 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1073 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1074 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1075 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1080 static int smu_smc_hw_setup(struct smu_context *smu)
1082 struct smu_feature *feature = &smu->smu_feature;
1083 struct amdgpu_device *adev = smu->adev;
1084 uint32_t pcie_gen = 0, pcie_width = 0;
1085 uint64_t features_supported;
1088 if (adev->in_suspend && smu_is_dpm_running(smu)) {
1089 dev_info(adev->dev, "dpm has been enabled\n");
1090 /* this is needed specifically */
1091 switch (adev->ip_versions[MP1_HWIP][0]) {
1092 case IP_VERSION(11, 0, 7):
1093 case IP_VERSION(11, 0, 11):
1094 case IP_VERSION(11, 5, 0):
1095 case IP_VERSION(11, 0, 12):
1096 ret = smu_system_features_control(smu, true);
1098 dev_err(adev->dev, "Failed system features control!\n");
1106 ret = smu_init_display_count(smu, 0);
1108 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1112 ret = smu_set_driver_table_location(smu);
1114 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1119 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1121 ret = smu_set_tool_table_location(smu);
1123 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1128 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1131 ret = smu_notify_memory_pool_location(smu);
1133 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1137 /* smu_dump_pptable(smu); */
1139 * Copy pptable bo in the vram to smc with SMU MSGs such as
1140 * SetDriverDramAddr and TransferTableDram2Smu.
1142 ret = smu_write_pptable(smu);
1144 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1148 /* issue Run*Btc msg */
1149 ret = smu_run_btc(smu);
1153 ret = smu_feature_set_allowed_mask(smu);
1155 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1159 ret = smu_system_features_control(smu, true);
1161 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1165 ret = smu_feature_get_enabled_mask(smu, &features_supported);
1167 dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");
1170 bitmap_copy(feature->supported,
1171 (unsigned long *)&features_supported,
1172 feature->feature_num);
1174 if (!smu_is_dpm_running(smu))
1175 dev_info(adev->dev, "dpm has been disabled\n");
1177 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1179 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1181 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1183 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1186 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1187 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1188 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
1190 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1192 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1194 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1196 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1198 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1200 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1202 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1204 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1208 ret = smu_get_thermal_temperature_range(smu);
1210 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1214 ret = smu_enable_thermal_alert(smu);
1216 dev_err(adev->dev, "Failed to enable thermal alert!\n");
1221 * Set initialized values (get from vbios) to dpm tables context such as
1222 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1225 ret = smu_set_default_dpm_table(smu);
1227 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1231 ret = smu_notify_display_change(smu);
1233 dev_err(adev->dev, "Failed to notify display change!\n");
1238 * Set min deep sleep dce fclk with bootup value from vbios via
1239 * SetMinDeepSleepDcefclk MSG.
1241 ret = smu_set_min_dcef_deep_sleep(smu,
1242 smu->smu_table.boot_values.dcefclk / 100);
1247 static int smu_start_smc_engine(struct smu_context *smu)
1249 struct amdgpu_device *adev = smu->adev;
1252 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1253 if (adev->ip_versions[MP1_HWIP][0] < IP_VERSION(11, 0, 0)) {
1254 if (smu->ppt_funcs->load_microcode) {
1255 ret = smu->ppt_funcs->load_microcode(smu);
1262 if (smu->ppt_funcs->check_fw_status) {
1263 ret = smu->ppt_funcs->check_fw_status(smu);
1265 dev_err(adev->dev, "SMC is not ready\n");
1271 * Send msg GetDriverIfVersion to check if the return value is equal
1272 * with DRIVER_IF_VERSION of smc header.
1274 ret = smu_check_fw_version(smu);
1281 static int smu_hw_init(void *handle)
1284 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1285 struct smu_context *smu = adev->powerplay.pp_handle;
1287 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1288 smu->pm_enabled = false;
1292 ret = smu_start_smc_engine(smu);
1294 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1299 smu_dpm_set_vcn_enable(smu, true);
1300 smu_dpm_set_jpeg_enable(smu, true);
1301 smu_set_gfx_cgpg(smu, true);
1304 if (!smu->pm_enabled)
1307 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1308 ret = smu_get_vbios_bootup_values(smu);
1310 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1314 ret = smu_setup_pptable(smu);
1316 dev_err(adev->dev, "Failed to setup pptable!\n");
1320 ret = smu_get_driver_allowed_feature_mask(smu);
1324 ret = smu_smc_hw_setup(smu);
1326 dev_err(adev->dev, "Failed to setup smc hw!\n");
1331 * Move maximum sustainable clock retrieving here considering
1332 * 1. It is not needed on resume(from S3).
1333 * 2. DAL settings come between .hw_init and .late_init of SMU.
1334 * And DAL needs to know the maximum sustainable clocks. Thus
1335 * it cannot be put in .late_init().
1337 ret = smu_init_max_sustainable_clocks(smu);
1339 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1343 adev->pm.dpm_enabled = true;
1345 dev_info(adev->dev, "SMU is initialized successfully!\n");
1350 static int smu_disable_dpms(struct smu_context *smu)
1352 struct amdgpu_device *adev = smu->adev;
1354 bool use_baco = !smu->is_apu &&
1355 ((amdgpu_in_reset(adev) &&
1356 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1357 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1360 * For custom pptable uploading, skip the DPM features
1361 * disable process on Navi1x ASICs.
1362 * - As the gfx related features are under control of
1363 * RLC on those ASICs. RLC reinitialization will be
1364 * needed to reenable them. That will cost much more
1367 * - SMU firmware can handle the DPM reenablement
1370 if (smu->uploading_custom_pp_table) {
1371 switch (adev->ip_versions[MP1_HWIP][0]) {
1372 case IP_VERSION(11, 0, 0):
1373 case IP_VERSION(11, 0, 5):
1374 case IP_VERSION(11, 0, 9):
1375 case IP_VERSION(11, 0, 7):
1376 case IP_VERSION(11, 0, 11):
1377 case IP_VERSION(11, 5, 0):
1378 case IP_VERSION(11, 0, 12):
1379 case IP_VERSION(11, 0, 13):
1387 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1388 * on BACO in. Driver involvement is unnecessary.
1391 switch (adev->ip_versions[MP1_HWIP][0]) {
1392 case IP_VERSION(11, 0, 7):
1393 case IP_VERSION(11, 0, 0):
1394 case IP_VERSION(11, 0, 5):
1395 case IP_VERSION(11, 0, 9):
1403 * For gpu reset, runpm and hibernation through BACO,
1404 * BACO feature has to be kept enabled.
1406 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1407 ret = smu_disable_all_features_with_exception(smu,
1408 SMU_FEATURE_BACO_BIT);
1410 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1412 ret = smu_system_features_control(smu, false);
1414 dev_err(adev->dev, "Failed to disable smu features.\n");
1417 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(9, 4, 2) &&
1418 adev->gfx.rlc.funcs->stop)
1419 adev->gfx.rlc.funcs->stop(adev);
1424 static int smu_smc_hw_cleanup(struct smu_context *smu)
1426 struct amdgpu_device *adev = smu->adev;
1429 cancel_work_sync(&smu->throttling_logging_work);
1430 cancel_work_sync(&smu->interrupt_work);
1432 ret = smu_disable_thermal_alert(smu);
1434 dev_err(adev->dev, "Fail to disable thermal alert!\n");
1438 ret = smu_disable_dpms(smu);
1440 dev_err(adev->dev, "Fail to disable dpm features!\n");
1447 static int smu_hw_fini(void *handle)
1449 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1450 struct smu_context *smu = adev->powerplay.pp_handle;
1452 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1455 smu_dpm_set_vcn_enable(smu, false);
1456 smu_dpm_set_jpeg_enable(smu, false);
1458 adev->vcn.cur_state = AMD_PG_STATE_GATE;
1459 adev->jpeg.cur_state = AMD_PG_STATE_GATE;
1461 if (!smu->pm_enabled)
1464 adev->pm.dpm_enabled = false;
1466 return smu_smc_hw_cleanup(smu);
1469 static void smu_late_fini(void *handle)
1471 struct amdgpu_device *adev = handle;
1472 struct smu_context *smu = adev->powerplay.pp_handle;
1477 static int smu_reset(struct smu_context *smu)
1479 struct amdgpu_device *adev = smu->adev;
1482 ret = smu_hw_fini(adev);
1486 ret = smu_hw_init(adev);
1490 ret = smu_late_init(adev);
1497 static int smu_suspend(void *handle)
1499 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1500 struct smu_context *smu = adev->powerplay.pp_handle;
1503 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1506 if (!smu->pm_enabled)
1509 adev->pm.dpm_enabled = false;
1511 ret = smu_smc_hw_cleanup(smu);
1515 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1517 smu_set_gfx_cgpg(smu, false);
1522 static int smu_resume(void *handle)
1525 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1526 struct smu_context *smu = adev->powerplay.pp_handle;
1528 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1531 if (!smu->pm_enabled)
1534 dev_info(adev->dev, "SMU is resuming...\n");
1536 ret = smu_start_smc_engine(smu);
1538 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1542 ret = smu_smc_hw_setup(smu);
1544 dev_err(adev->dev, "Failed to setup smc hw!\n");
1548 smu_set_gfx_cgpg(smu, true);
1550 smu->disable_uclk_switch = 0;
1552 adev->pm.dpm_enabled = true;
1554 dev_info(adev->dev, "SMU is resumed successfully!\n");
1559 static int smu_display_configuration_change(void *handle,
1560 const struct amd_pp_display_configuration *display_config)
1562 struct smu_context *smu = handle;
1564 int num_of_active_display = 0;
1566 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1569 if (!display_config)
1572 smu_set_min_dcef_deep_sleep(smu,
1573 display_config->min_dcef_deep_sleep_set_clk / 100);
1575 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1576 if (display_config->displays[index].controller_id != 0)
1577 num_of_active_display++;
1583 static int smu_set_clockgating_state(void *handle,
1584 enum amd_clockgating_state state)
1589 static int smu_set_powergating_state(void *handle,
1590 enum amd_powergating_state state)
1595 static int smu_enable_umd_pstate(void *handle,
1596 enum amd_dpm_forced_level *level)
1598 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1599 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1600 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1601 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1603 struct smu_context *smu = (struct smu_context*)(handle);
1604 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1606 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1609 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1610 /* enter umd pstate, save current level, disable gfx cg*/
1611 if (*level & profile_mode_mask) {
1612 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1613 smu_gpo_control(smu, false);
1614 smu_gfx_ulv_control(smu, false);
1615 smu_deep_sleep_control(smu, false);
1616 amdgpu_asic_update_umd_stable_pstate(smu->adev, true);
1619 /* exit umd pstate, restore level, enable gfx cg*/
1620 if (!(*level & profile_mode_mask)) {
1621 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1622 *level = smu_dpm_ctx->saved_dpm_level;
1623 amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
1624 smu_deep_sleep_control(smu, true);
1625 smu_gfx_ulv_control(smu, true);
1626 smu_gpo_control(smu, true);
1633 static int smu_bump_power_profile_mode(struct smu_context *smu,
1635 uint32_t param_size)
1639 if (smu->ppt_funcs->set_power_profile_mode)
1640 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
1645 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
1646 enum amd_dpm_forced_level level,
1647 bool skip_display_settings)
1652 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1654 if (!skip_display_settings) {
1655 ret = smu_display_config_changed(smu);
1657 dev_err(smu->adev->dev, "Failed to change display config!");
1662 ret = smu_apply_clocks_adjust_rules(smu);
1664 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
1668 if (!skip_display_settings) {
1669 ret = smu_notify_smc_display_config(smu);
1671 dev_err(smu->adev->dev, "Failed to notify smc display config!");
1676 if (smu_dpm_ctx->dpm_level != level) {
1677 ret = smu_asic_set_performance_level(smu, level);
1679 dev_err(smu->adev->dev, "Failed to set performance level!");
1683 /* update the saved copy */
1684 smu_dpm_ctx->dpm_level = level;
1687 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1688 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
1689 index = fls(smu->workload_mask);
1690 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1691 workload = smu->workload_setting[index];
1693 if (smu->power_profile_mode != workload)
1694 smu_bump_power_profile_mode(smu, &workload, 0);
1700 static int smu_handle_task(struct smu_context *smu,
1701 enum amd_dpm_forced_level level,
1702 enum amd_pp_task task_id)
1706 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1710 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1711 ret = smu_pre_display_config_changed(smu);
1714 ret = smu_adjust_power_state_dynamic(smu, level, false);
1716 case AMD_PP_TASK_COMPLETE_INIT:
1717 case AMD_PP_TASK_READJUST_POWER_STATE:
1718 ret = smu_adjust_power_state_dynamic(smu, level, true);
1727 static int smu_handle_dpm_task(void *handle,
1728 enum amd_pp_task task_id,
1729 enum amd_pm_state_type *user_state)
1731 struct smu_context *smu = handle;
1732 struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
1734 return smu_handle_task(smu, smu_dpm->dpm_level, task_id);
1738 static int smu_switch_power_profile(void *handle,
1739 enum PP_SMC_POWER_PROFILE type,
1742 struct smu_context *smu = handle;
1743 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1747 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1750 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1754 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1755 index = fls(smu->workload_mask);
1756 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1757 workload = smu->workload_setting[index];
1759 smu->workload_mask |= (1 << smu->workload_prority[type]);
1760 index = fls(smu->workload_mask);
1761 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1762 workload = smu->workload_setting[index];
1765 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1766 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
1767 smu_bump_power_profile_mode(smu, &workload, 0);
1772 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
1774 struct smu_context *smu = handle;
1775 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1777 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1780 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1783 return smu_dpm_ctx->dpm_level;
1786 static int smu_force_performance_level(void *handle,
1787 enum amd_dpm_forced_level level)
1789 struct smu_context *smu = handle;
1790 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1793 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1796 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1799 ret = smu_enable_umd_pstate(smu, &level);
1803 ret = smu_handle_task(smu, level,
1804 AMD_PP_TASK_READJUST_POWER_STATE);
1806 /* reset user dpm clock state */
1807 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1808 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
1809 smu->user_dpm_profile.clk_dependency = 0;
1815 static int smu_set_display_count(void *handle, uint32_t count)
1817 struct smu_context *smu = handle;
1819 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1822 return smu_init_display_count(smu, count);
1825 static int smu_force_smuclk_levels(struct smu_context *smu,
1826 enum smu_clk_type clk_type,
1829 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1832 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1835 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1836 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
1840 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
1841 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1842 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
1843 smu->user_dpm_profile.clk_mask[clk_type] = mask;
1844 smu_set_user_clk_dependencies(smu, clk_type);
1851 static int smu_force_ppclk_levels(void *handle,
1852 enum pp_clock_type type,
1855 struct smu_context *smu = handle;
1856 enum smu_clk_type clk_type;
1860 clk_type = SMU_SCLK; break;
1862 clk_type = SMU_MCLK; break;
1864 clk_type = SMU_PCIE; break;
1866 clk_type = SMU_SOCCLK; break;
1868 clk_type = SMU_FCLK; break;
1870 clk_type = SMU_DCEFCLK; break;
1872 clk_type = SMU_VCLK; break;
1874 clk_type = SMU_DCLK; break;
1876 clk_type = SMU_OD_SCLK; break;
1878 clk_type = SMU_OD_MCLK; break;
1880 clk_type = SMU_OD_VDDC_CURVE; break;
1882 clk_type = SMU_OD_RANGE; break;
1887 return smu_force_smuclk_levels(smu, clk_type, mask);
1891 * On system suspending or resetting, the dpm_enabled
1892 * flag will be cleared. So that those SMU services which
1893 * are not supported will be gated.
1894 * However, the mp1 state setting should still be granted
1895 * even if the dpm_enabled cleared.
1897 static int smu_set_mp1_state(void *handle,
1898 enum pp_mp1_state mp1_state)
1900 struct smu_context *smu = handle;
1903 if (!smu->pm_enabled)
1906 if (smu->ppt_funcs &&
1907 smu->ppt_funcs->set_mp1_state)
1908 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
1913 static int smu_set_df_cstate(void *handle,
1914 enum pp_df_cstate state)
1916 struct smu_context *smu = handle;
1919 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1922 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
1925 ret = smu->ppt_funcs->set_df_cstate(smu, state);
1927 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
1932 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en)
1936 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1939 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down)
1942 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en);
1944 dev_err(smu->adev->dev, "[AllowXgmiPowerDown] failed!\n");
1949 int smu_write_watermarks_table(struct smu_context *smu)
1951 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1954 return smu_set_watermarks_table(smu, NULL);
1957 static int smu_set_watermarks_for_clock_ranges(void *handle,
1958 struct pp_smu_wm_range_sets *clock_ranges)
1960 struct smu_context *smu = handle;
1962 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1965 if (smu->disable_watermark)
1968 return smu_set_watermarks_table(smu, clock_ranges);
1971 int smu_set_ac_dc(struct smu_context *smu)
1975 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1978 /* controlled by firmware */
1979 if (smu->dc_controlled_by_gpio)
1982 ret = smu_set_power_source(smu,
1983 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
1984 SMU_POWER_SOURCE_DC);
1986 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
1987 smu->adev->pm.ac_power ? "AC" : "DC");
1992 const struct amd_ip_funcs smu_ip_funcs = {
1994 .early_init = smu_early_init,
1995 .late_init = smu_late_init,
1996 .sw_init = smu_sw_init,
1997 .sw_fini = smu_sw_fini,
1998 .hw_init = smu_hw_init,
1999 .hw_fini = smu_hw_fini,
2000 .late_fini = smu_late_fini,
2001 .suspend = smu_suspend,
2002 .resume = smu_resume,
2004 .check_soft_reset = NULL,
2005 .wait_for_idle = NULL,
2007 .set_clockgating_state = smu_set_clockgating_state,
2008 .set_powergating_state = smu_set_powergating_state,
2011 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
2013 .type = AMD_IP_BLOCK_TYPE_SMC,
2017 .funcs = &smu_ip_funcs,
2020 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
2022 .type = AMD_IP_BLOCK_TYPE_SMC,
2026 .funcs = &smu_ip_funcs,
2029 const struct amdgpu_ip_block_version smu_v13_0_ip_block =
2031 .type = AMD_IP_BLOCK_TYPE_SMC,
2035 .funcs = &smu_ip_funcs,
2038 static int smu_load_microcode(void *handle)
2040 struct smu_context *smu = handle;
2041 struct amdgpu_device *adev = smu->adev;
2044 if (!smu->pm_enabled)
2047 /* This should be used for non PSP loading */
2048 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2051 if (smu->ppt_funcs->load_microcode) {
2052 ret = smu->ppt_funcs->load_microcode(smu);
2054 dev_err(adev->dev, "Load microcode failed\n");
2059 if (smu->ppt_funcs->check_fw_status) {
2060 ret = smu->ppt_funcs->check_fw_status(smu);
2062 dev_err(adev->dev, "SMC is not ready\n");
2070 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2074 if (smu->ppt_funcs->set_gfx_cgpg)
2075 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2080 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2082 struct smu_context *smu = handle;
2085 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2088 if (!smu->ppt_funcs->set_fan_speed_rpm)
2091 if (speed == U32_MAX)
2094 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2095 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2096 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;
2097 smu->user_dpm_profile.fan_speed_rpm = speed;
2099 /* Override custom PWM setting as they cannot co-exist */
2100 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;
2101 smu->user_dpm_profile.fan_speed_pwm = 0;
2108 * smu_get_power_limit - Request one of the SMU Power Limits
2110 * @handle: pointer to smu context
2111 * @limit: requested limit is written back to this variable
2112 * @pp_limit_level: &pp_power_limit_level which limit of the power to return
2113 * @pp_power_type: &pp_power_type type of power
2114 * Return: 0 on success, <0 on error
2117 int smu_get_power_limit(void *handle,
2119 enum pp_power_limit_level pp_limit_level,
2120 enum pp_power_type pp_power_type)
2122 struct smu_context *smu = handle;
2123 struct amdgpu_device *adev = smu->adev;
2124 enum smu_ppt_limit_level limit_level;
2125 uint32_t limit_type;
2128 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2131 switch(pp_power_type) {
2132 case PP_PWR_TYPE_SUSTAINED:
2133 limit_type = SMU_DEFAULT_PPT_LIMIT;
2135 case PP_PWR_TYPE_FAST:
2136 limit_type = SMU_FAST_PPT_LIMIT;
2143 switch(pp_limit_level){
2144 case PP_PWR_LIMIT_CURRENT:
2145 limit_level = SMU_PPT_LIMIT_CURRENT;
2147 case PP_PWR_LIMIT_DEFAULT:
2148 limit_level = SMU_PPT_LIMIT_DEFAULT;
2150 case PP_PWR_LIMIT_MAX:
2151 limit_level = SMU_PPT_LIMIT_MAX;
2153 case PP_PWR_LIMIT_MIN:
2159 if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2160 if (smu->ppt_funcs->get_ppt_limit)
2161 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2163 switch (limit_level) {
2164 case SMU_PPT_LIMIT_CURRENT:
2165 switch (adev->ip_versions[MP1_HWIP][0]) {
2166 case IP_VERSION(13, 0, 2):
2167 case IP_VERSION(11, 0, 7):
2168 case IP_VERSION(11, 0, 11):
2169 case IP_VERSION(11, 0, 12):
2170 case IP_VERSION(11, 0, 13):
2171 ret = smu_get_asic_power_limits(smu,
2172 &smu->current_power_limit,
2179 *limit = smu->current_power_limit;
2181 case SMU_PPT_LIMIT_DEFAULT:
2182 *limit = smu->default_power_limit;
2184 case SMU_PPT_LIMIT_MAX:
2185 *limit = smu->max_power_limit;
2195 static int smu_set_power_limit(void *handle, uint32_t limit)
2197 struct smu_context *smu = handle;
2198 uint32_t limit_type = limit >> 24;
2201 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2205 if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2206 if (smu->ppt_funcs->set_power_limit)
2207 return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2209 if (limit > smu->max_power_limit) {
2210 dev_err(smu->adev->dev,
2211 "New power limit (%d) is over the max allowed %d\n",
2212 limit, smu->max_power_limit);
2217 limit = smu->current_power_limit;
2219 if (smu->ppt_funcs->set_power_limit) {
2220 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2221 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2222 smu->user_dpm_profile.power_limit = limit;
2228 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2232 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2235 if (smu->ppt_funcs->print_clk_levels)
2236 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2241 static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)
2243 enum smu_clk_type clk_type;
2247 clk_type = SMU_SCLK; break;
2249 clk_type = SMU_MCLK; break;
2251 clk_type = SMU_PCIE; break;
2253 clk_type = SMU_SOCCLK; break;
2255 clk_type = SMU_FCLK; break;
2257 clk_type = SMU_DCEFCLK; break;
2259 clk_type = SMU_VCLK; break;
2261 clk_type = SMU_DCLK; break;
2263 clk_type = SMU_OD_SCLK; break;
2265 clk_type = SMU_OD_MCLK; break;
2267 clk_type = SMU_OD_VDDC_CURVE; break;
2269 clk_type = SMU_OD_RANGE; break;
2270 case OD_VDDGFX_OFFSET:
2271 clk_type = SMU_OD_VDDGFX_OFFSET; break;
2273 clk_type = SMU_OD_CCLK; break;
2275 clk_type = SMU_CLK_COUNT; break;
2281 static int smu_print_ppclk_levels(void *handle,
2282 enum pp_clock_type type,
2285 struct smu_context *smu = handle;
2286 enum smu_clk_type clk_type;
2288 clk_type = smu_convert_to_smuclk(type);
2289 if (clk_type == SMU_CLK_COUNT)
2292 return smu_print_smuclk_levels(smu, clk_type, buf);
2295 static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)
2297 struct smu_context *smu = handle;
2298 enum smu_clk_type clk_type;
2300 clk_type = smu_convert_to_smuclk(type);
2301 if (clk_type == SMU_CLK_COUNT)
2304 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2307 if (!smu->ppt_funcs->emit_clk_levels)
2310 return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);
2314 static int smu_od_edit_dpm_table(void *handle,
2315 enum PP_OD_DPM_TABLE_COMMAND type,
2316 long *input, uint32_t size)
2318 struct smu_context *smu = handle;
2321 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2324 if (smu->ppt_funcs->od_edit_dpm_table) {
2325 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2331 static int smu_read_sensor(void *handle,
2336 struct smu_context *smu = handle;
2337 struct smu_umd_pstate_table *pstate_table =
2340 uint32_t *size, size_val;
2342 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2345 if (!data || !size_arg)
2348 size_val = *size_arg;
2351 if (smu->ppt_funcs->read_sensor)
2352 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2356 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2357 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2360 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2361 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2364 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2365 ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);
2368 case AMDGPU_PP_SENSOR_UVD_POWER:
2369 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
2372 case AMDGPU_PP_SENSOR_VCE_POWER:
2373 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
2376 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
2377 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0: 1;
2380 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
2381 *(uint32_t *)data = 0;
2391 // assign uint32_t to int
2392 *size_arg = size_val;
2397 static int smu_get_power_profile_mode(void *handle, char *buf)
2399 struct smu_context *smu = handle;
2401 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
2402 !smu->ppt_funcs->get_power_profile_mode)
2407 return smu->ppt_funcs->get_power_profile_mode(smu, buf);
2410 static int smu_set_power_profile_mode(void *handle,
2412 uint32_t param_size)
2414 struct smu_context *smu = handle;
2416 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
2417 !smu->ppt_funcs->set_power_profile_mode)
2420 return smu_bump_power_profile_mode(smu, param, param_size);
2424 static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)
2426 struct smu_context *smu = handle;
2428 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2431 if (!smu->ppt_funcs->get_fan_control_mode)
2437 *fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);
2442 static int smu_set_fan_control_mode(void *handle, u32 value)
2444 struct smu_context *smu = handle;
2447 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2450 if (!smu->ppt_funcs->set_fan_control_mode)
2453 if (value == U32_MAX)
2456 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2460 if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2461 smu->user_dpm_profile.fan_mode = value;
2463 /* reset user dpm fan speed */
2464 if (value != AMD_FAN_CTRL_MANUAL) {
2465 smu->user_dpm_profile.fan_speed_pwm = 0;
2466 smu->user_dpm_profile.fan_speed_rpm = 0;
2467 smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);
2475 static int smu_get_fan_speed_pwm(void *handle, u32 *speed)
2477 struct smu_context *smu = handle;
2480 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2483 if (!smu->ppt_funcs->get_fan_speed_pwm)
2489 ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);
2494 static int smu_set_fan_speed_pwm(void *handle, u32 speed)
2496 struct smu_context *smu = handle;
2499 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2502 if (!smu->ppt_funcs->set_fan_speed_pwm)
2505 if (speed == U32_MAX)
2508 ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);
2509 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2510 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;
2511 smu->user_dpm_profile.fan_speed_pwm = speed;
2513 /* Override custom RPM setting as they cannot co-exist */
2514 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;
2515 smu->user_dpm_profile.fan_speed_rpm = 0;
2521 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
2523 struct smu_context *smu = handle;
2526 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2529 if (!smu->ppt_funcs->get_fan_speed_rpm)
2535 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2540 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
2542 struct smu_context *smu = handle;
2544 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2547 return smu_set_min_dcef_deep_sleep(smu, clk);
2550 static int smu_get_clock_by_type_with_latency(void *handle,
2551 enum amd_pp_clock_type type,
2552 struct pp_clock_levels_with_latency *clocks)
2554 struct smu_context *smu = handle;
2555 enum smu_clk_type clk_type;
2558 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2561 if (smu->ppt_funcs->get_clock_by_type_with_latency) {
2563 case amd_pp_sys_clock:
2564 clk_type = SMU_GFXCLK;
2566 case amd_pp_mem_clock:
2567 clk_type = SMU_MCLK;
2569 case amd_pp_dcef_clock:
2570 clk_type = SMU_DCEFCLK;
2572 case amd_pp_disp_clock:
2573 clk_type = SMU_DISPCLK;
2576 dev_err(smu->adev->dev, "Invalid clock type!\n");
2580 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2586 static int smu_display_clock_voltage_request(void *handle,
2587 struct pp_display_clock_request *clock_req)
2589 struct smu_context *smu = handle;
2592 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2595 if (smu->ppt_funcs->display_clock_voltage_request)
2596 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2602 static int smu_display_disable_memory_clock_switch(void *handle,
2603 bool disable_memory_clock_switch)
2605 struct smu_context *smu = handle;
2608 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2611 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2612 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2617 static int smu_set_xgmi_pstate(void *handle,
2620 struct smu_context *smu = handle;
2623 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2626 if (smu->ppt_funcs->set_xgmi_pstate)
2627 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2630 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
2635 static int smu_get_baco_capability(void *handle, bool *cap)
2637 struct smu_context *smu = handle;
2641 if (!smu->pm_enabled)
2644 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2645 *cap = smu->ppt_funcs->baco_is_support(smu);
2650 static int smu_baco_set_state(void *handle, int state)
2652 struct smu_context *smu = handle;
2655 if (!smu->pm_enabled)
2659 if (smu->ppt_funcs->baco_exit)
2660 ret = smu->ppt_funcs->baco_exit(smu);
2661 } else if (state == 1) {
2662 if (smu->ppt_funcs->baco_enter)
2663 ret = smu->ppt_funcs->baco_enter(smu);
2669 dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
2670 (state)?"enter":"exit");
2675 bool smu_mode1_reset_is_support(struct smu_context *smu)
2679 if (!smu->pm_enabled)
2682 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
2683 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
2688 bool smu_mode2_reset_is_support(struct smu_context *smu)
2692 if (!smu->pm_enabled)
2695 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
2696 ret = smu->ppt_funcs->mode2_reset_is_support(smu);
2701 int smu_mode1_reset(struct smu_context *smu)
2705 if (!smu->pm_enabled)
2708 if (smu->ppt_funcs->mode1_reset)
2709 ret = smu->ppt_funcs->mode1_reset(smu);
2714 static int smu_mode2_reset(void *handle)
2716 struct smu_context *smu = handle;
2719 if (!smu->pm_enabled)
2722 if (smu->ppt_funcs->mode2_reset)
2723 ret = smu->ppt_funcs->mode2_reset(smu);
2726 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
2731 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
2732 struct pp_smu_nv_clock_table *max_clocks)
2734 struct smu_context *smu = handle;
2737 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2740 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2741 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2746 static int smu_get_uclk_dpm_states(void *handle,
2747 unsigned int *clock_values_in_khz,
2748 unsigned int *num_states)
2750 struct smu_context *smu = handle;
2753 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2756 if (smu->ppt_funcs->get_uclk_dpm_states)
2757 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2762 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
2764 struct smu_context *smu = handle;
2765 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2767 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2770 if (smu->ppt_funcs->get_current_power_state)
2771 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2776 static int smu_get_dpm_clock_table(void *handle,
2777 struct dpm_clocks *clock_table)
2779 struct smu_context *smu = handle;
2782 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2785 if (smu->ppt_funcs->get_dpm_clock_table)
2786 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2791 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
2793 struct smu_context *smu = handle;
2795 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2798 if (!smu->ppt_funcs->get_gpu_metrics)
2801 return smu->ppt_funcs->get_gpu_metrics(smu, table);
2804 static int smu_enable_mgpu_fan_boost(void *handle)
2806 struct smu_context *smu = handle;
2809 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2812 if (smu->ppt_funcs->enable_mgpu_fan_boost)
2813 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
2818 static int smu_gfx_state_change_set(void *handle,
2821 struct smu_context *smu = handle;
2824 if (smu->ppt_funcs->gfx_state_change_set)
2825 ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
2830 int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)
2834 if (smu->ppt_funcs->smu_handle_passthrough_sbr)
2835 ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);
2840 int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)
2842 int ret = -EOPNOTSUPP;
2844 if (smu->ppt_funcs &&
2845 smu->ppt_funcs->get_ecc_info)
2846 ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);
2852 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
2854 struct smu_context *smu = handle;
2855 struct smu_table_context *smu_table = &smu->smu_table;
2856 struct smu_table *memory_pool = &smu_table->memory_pool;
2863 if (memory_pool->bo) {
2864 *addr = memory_pool->cpu_addr;
2865 *size = memory_pool->size;
2871 static const struct amd_pm_funcs swsmu_pm_funcs = {
2872 /* export for sysfs */
2873 .set_fan_control_mode = smu_set_fan_control_mode,
2874 .get_fan_control_mode = smu_get_fan_control_mode,
2875 .set_fan_speed_pwm = smu_set_fan_speed_pwm,
2876 .get_fan_speed_pwm = smu_get_fan_speed_pwm,
2877 .force_clock_level = smu_force_ppclk_levels,
2878 .print_clock_levels = smu_print_ppclk_levels,
2879 .emit_clock_levels = smu_emit_ppclk_levels,
2880 .force_performance_level = smu_force_performance_level,
2881 .read_sensor = smu_read_sensor,
2882 .get_performance_level = smu_get_performance_level,
2883 .get_current_power_state = smu_get_current_power_state,
2884 .get_fan_speed_rpm = smu_get_fan_speed_rpm,
2885 .set_fan_speed_rpm = smu_set_fan_speed_rpm,
2886 .get_pp_num_states = smu_get_power_num_states,
2887 .get_pp_table = smu_sys_get_pp_table,
2888 .set_pp_table = smu_sys_set_pp_table,
2889 .switch_power_profile = smu_switch_power_profile,
2890 /* export to amdgpu */
2891 .dispatch_tasks = smu_handle_dpm_task,
2892 .load_firmware = smu_load_microcode,
2893 .set_powergating_by_smu = smu_dpm_set_power_gate,
2894 .set_power_limit = smu_set_power_limit,
2895 .get_power_limit = smu_get_power_limit,
2896 .get_power_profile_mode = smu_get_power_profile_mode,
2897 .set_power_profile_mode = smu_set_power_profile_mode,
2898 .odn_edit_dpm_table = smu_od_edit_dpm_table,
2899 .set_mp1_state = smu_set_mp1_state,
2900 .gfx_state_change_set = smu_gfx_state_change_set,
2902 .get_sclk = smu_get_sclk,
2903 .get_mclk = smu_get_mclk,
2904 .display_configuration_change = smu_display_configuration_change,
2905 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,
2906 .display_clock_voltage_request = smu_display_clock_voltage_request,
2907 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,
2908 .set_active_display_count = smu_set_display_count,
2909 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,
2910 .get_asic_baco_capability = smu_get_baco_capability,
2911 .set_asic_baco_state = smu_baco_set_state,
2912 .get_ppfeature_status = smu_sys_get_pp_feature_mask,
2913 .set_ppfeature_status = smu_sys_set_pp_feature_mask,
2914 .asic_reset_mode_2 = smu_mode2_reset,
2915 .set_df_cstate = smu_set_df_cstate,
2916 .set_xgmi_pstate = smu_set_xgmi_pstate,
2917 .get_gpu_metrics = smu_sys_get_gpu_metrics,
2918 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,
2919 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
2920 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,
2921 .get_uclk_dpm_states = smu_get_uclk_dpm_states,
2922 .get_dpm_clock_table = smu_get_dpm_clock_table,
2923 .get_smu_prv_buf_details = smu_get_prv_buffer_details,
2926 int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,
2931 if (smu->ppt_funcs->wait_for_event)
2932 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
2937 int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)
2940 if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)
2943 /* Confirm the buffer allocated is of correct size */
2944 if (size != smu->stb_context.stb_buf_size)
2948 * No need to lock smu mutex as we access STB directly through MMIO
2949 * and not going through SMU messaging route (for now at least).
2950 * For registers access rely on implementation internal locking.
2952 return smu->ppt_funcs->stb_collect_info(smu, buf, size);
2955 #if defined(CONFIG_DEBUG_FS)
2957 static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)
2959 struct amdgpu_device *adev = filp->f_inode->i_private;
2960 struct smu_context *smu = adev->powerplay.pp_handle;
2964 buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);
2968 r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);
2972 filp->private_data = buf;
2981 static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
2984 struct amdgpu_device *adev = filp->f_inode->i_private;
2985 struct smu_context *smu = adev->powerplay.pp_handle;
2988 if (!filp->private_data)
2991 return simple_read_from_buffer(buf,
2993 pos, filp->private_data,
2994 smu->stb_context.stb_buf_size);
2997 static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)
2999 kvfree(filp->private_data);
3000 filp->private_data = NULL;
3006 * We have to define not only read method but also
3007 * open and release because .read takes up to PAGE_SIZE
3008 * data each time so and so is invoked multiple times.
3009 * We allocate the STB buffer in .open and release it
3012 static const struct file_operations smu_stb_debugfs_fops = {
3013 .owner = THIS_MODULE,
3014 .open = smu_stb_debugfs_open,
3015 .read = smu_stb_debugfs_read,
3016 .release = smu_stb_debugfs_release,
3017 .llseek = default_llseek,
3022 void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)
3024 #if defined(CONFIG_DEBUG_FS)
3026 struct smu_context *smu = adev->powerplay.pp_handle;
3028 if (!smu->stb_context.stb_buf_size)
3031 debugfs_create_file_size("amdgpu_smu_stb_dump",
3033 adev_to_drm(adev)->primary->debugfs_root,
3035 &smu_stb_debugfs_fops,
3036 smu->stb_context.stb_buf_size);
3040 int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)
3044 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)
3045 ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);
3050 int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)
3054 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)
3055 ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);