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"
42 * DO NOT use these for err/warn/info/debug messages.
43 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
44 * They are more MGPU friendly.
51 static const struct amd_pm_funcs swsmu_pm_funcs;
52 static int smu_force_smuclk_levels(struct smu_context *smu,
53 enum smu_clk_type clk_type,
55 static int smu_handle_task(struct smu_context *smu,
56 enum amd_dpm_forced_level level,
57 enum amd_pp_task task_id,
59 static int smu_reset(struct smu_context *smu);
60 static int smu_set_fan_speed_percent(void *handle, u32 speed);
61 static int smu_set_fan_control_mode(struct smu_context *smu, int value);
62 static int smu_set_power_limit(void *handle, uint32_t limit);
63 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);
64 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);
66 static int smu_sys_get_pp_feature_mask(void *handle,
69 struct smu_context *smu = handle;
72 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
75 mutex_lock(&smu->mutex);
77 size = smu_get_pp_feature_mask(smu, buf);
79 mutex_unlock(&smu->mutex);
84 static int smu_sys_set_pp_feature_mask(void *handle,
87 struct smu_context *smu = handle;
90 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
93 mutex_lock(&smu->mutex);
95 ret = smu_set_pp_feature_mask(smu, new_mask);
97 mutex_unlock(&smu->mutex);
102 int smu_get_status_gfxoff(struct amdgpu_device *adev, uint32_t *value)
105 struct smu_context *smu = &adev->smu;
107 if (is_support_sw_smu(adev) && smu->ppt_funcs->get_gfx_off_status)
108 *value = smu_get_gfx_off_status(smu);
115 int smu_set_soft_freq_range(struct smu_context *smu,
116 enum smu_clk_type clk_type,
122 mutex_lock(&smu->mutex);
124 if (smu->ppt_funcs->set_soft_freq_limited_range)
125 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
130 mutex_unlock(&smu->mutex);
135 int smu_get_dpm_freq_range(struct smu_context *smu,
136 enum smu_clk_type clk_type,
145 mutex_lock(&smu->mutex);
147 if (smu->ppt_funcs->get_dpm_ultimate_freq)
148 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
153 mutex_unlock(&smu->mutex);
158 static u32 smu_get_mclk(void *handle, bool low)
160 struct smu_context *smu = handle;
164 ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
165 low ? &clk_freq : NULL,
166 !low ? &clk_freq : NULL);
169 return clk_freq * 100;
172 static u32 smu_get_sclk(void *handle, bool low)
174 struct smu_context *smu = handle;
178 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
179 low ? &clk_freq : NULL,
180 !low ? &clk_freq : NULL);
183 return clk_freq * 100;
186 static int smu_dpm_set_vcn_enable_locked(struct smu_context *smu,
189 struct smu_power_context *smu_power = &smu->smu_power;
190 struct smu_power_gate *power_gate = &smu_power->power_gate;
193 if (!smu->ppt_funcs->dpm_set_vcn_enable)
196 if (atomic_read(&power_gate->vcn_gated) ^ enable)
199 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
201 atomic_set(&power_gate->vcn_gated, !enable);
206 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
209 struct smu_power_context *smu_power = &smu->smu_power;
210 struct smu_power_gate *power_gate = &smu_power->power_gate;
213 mutex_lock(&power_gate->vcn_gate_lock);
215 ret = smu_dpm_set_vcn_enable_locked(smu, enable);
217 mutex_unlock(&power_gate->vcn_gate_lock);
222 static int smu_dpm_set_jpeg_enable_locked(struct smu_context *smu,
225 struct smu_power_context *smu_power = &smu->smu_power;
226 struct smu_power_gate *power_gate = &smu_power->power_gate;
229 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
232 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
235 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
237 atomic_set(&power_gate->jpeg_gated, !enable);
242 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
245 struct smu_power_context *smu_power = &smu->smu_power;
246 struct smu_power_gate *power_gate = &smu_power->power_gate;
249 mutex_lock(&power_gate->jpeg_gate_lock);
251 ret = smu_dpm_set_jpeg_enable_locked(smu, enable);
253 mutex_unlock(&power_gate->jpeg_gate_lock);
259 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
261 * @handle: smu_context pointer
262 * @block_type: the IP block to power gate/ungate
263 * @gate: to power gate if true, ungate otherwise
265 * This API uses no smu->mutex lock protection due to:
266 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
267 * This is guarded to be race condition free by the caller.
268 * 2. Or get called on user setting request of power_dpm_force_performance_level.
269 * Under this case, the smu->mutex lock protection is already enforced on
270 * the parent API smu_force_performance_level of the call path.
272 static int smu_dpm_set_power_gate(void *handle,
276 struct smu_context *smu = handle;
279 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
282 switch (block_type) {
284 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
285 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
287 case AMD_IP_BLOCK_TYPE_UVD:
288 case AMD_IP_BLOCK_TYPE_VCN:
289 ret = smu_dpm_set_vcn_enable(smu, !gate);
291 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
292 gate ? "gate" : "ungate");
294 case AMD_IP_BLOCK_TYPE_GFX:
295 ret = smu_gfx_off_control(smu, gate);
297 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
298 gate ? "enable" : "disable");
300 case AMD_IP_BLOCK_TYPE_SDMA:
301 ret = smu_powergate_sdma(smu, gate);
303 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
304 gate ? "gate" : "ungate");
306 case AMD_IP_BLOCK_TYPE_JPEG:
307 ret = smu_dpm_set_jpeg_enable(smu, !gate);
309 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
310 gate ? "gate" : "ungate");
313 dev_err(smu->adev->dev, "Unsupported block type!\n");
321 * smu_set_user_clk_dependencies - set user profile clock dependencies
323 * @smu: smu_context pointer
324 * @clk: enum smu_clk_type type
326 * Enable/Disable the clock dependency for the @clk type.
328 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
330 if (smu->adev->in_suspend)
333 if (clk == SMU_MCLK) {
334 smu->user_dpm_profile.clk_dependency = 0;
335 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
336 } else if (clk == SMU_FCLK) {
337 /* MCLK takes precedence over FCLK */
338 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
341 smu->user_dpm_profile.clk_dependency = 0;
342 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
343 } else if (clk == SMU_SOCCLK) {
344 /* MCLK takes precedence over SOCCLK */
345 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
348 smu->user_dpm_profile.clk_dependency = 0;
349 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
351 /* Add clk dependencies here, if any */
356 * smu_restore_dpm_user_profile - reinstate user dpm profile
358 * @smu: smu_context pointer
360 * Restore the saved user power configurations include power limit,
361 * clock frequencies, fan control mode and fan speed.
363 static void smu_restore_dpm_user_profile(struct smu_context *smu)
365 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
368 if (!smu->adev->in_suspend)
371 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
374 /* Enable restore flag */
375 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
377 /* set the user dpm power limit */
378 if (smu->user_dpm_profile.power_limit) {
379 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
381 dev_err(smu->adev->dev, "Failed to set power limit value\n");
384 /* set the user dpm clock configurations */
385 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
386 enum smu_clk_type clk_type;
388 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
390 * Iterate over smu clk type and force the saved user clk
391 * configs, skip if clock dependency is enabled
393 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
394 smu->user_dpm_profile.clk_mask[clk_type]) {
395 ret = smu_force_smuclk_levels(smu, clk_type,
396 smu->user_dpm_profile.clk_mask[clk_type]);
398 dev_err(smu->adev->dev,
399 "Failed to set clock type = %d\n", clk_type);
404 /* set the user dpm fan configurations */
405 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL) {
406 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
408 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
412 if (!ret && smu->user_dpm_profile.fan_speed_percent) {
413 ret = smu_set_fan_speed_percent(smu, smu->user_dpm_profile.fan_speed_percent);
415 dev_err(smu->adev->dev, "Failed to set manual fan speed\n");
419 /* Disable restore flag */
420 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
423 static int smu_get_power_num_states(void *handle,
424 struct pp_states_info *state_info)
429 /* not support power state */
430 memset(state_info, 0, sizeof(struct pp_states_info));
431 state_info->nums = 1;
432 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
437 bool is_support_sw_smu(struct amdgpu_device *adev)
439 if (adev->asic_type >= CHIP_ARCTURUS)
445 bool is_support_cclk_dpm(struct amdgpu_device *adev)
447 struct smu_context *smu = &adev->smu;
449 if (!is_support_sw_smu(adev))
452 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
459 static int smu_sys_get_pp_table(void *handle,
462 struct smu_context *smu = handle;
463 struct smu_table_context *smu_table = &smu->smu_table;
464 uint32_t powerplay_table_size;
466 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
469 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
472 mutex_lock(&smu->mutex);
474 if (smu_table->hardcode_pptable)
475 *table = smu_table->hardcode_pptable;
477 *table = smu_table->power_play_table;
479 powerplay_table_size = smu_table->power_play_table_size;
481 mutex_unlock(&smu->mutex);
483 return powerplay_table_size;
486 static int smu_sys_set_pp_table(void *handle,
490 struct smu_context *smu = handle;
491 struct smu_table_context *smu_table = &smu->smu_table;
492 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
495 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
498 if (header->usStructureSize != size) {
499 dev_err(smu->adev->dev, "pp table size not matched !\n");
503 mutex_lock(&smu->mutex);
504 if (!smu_table->hardcode_pptable)
505 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
506 if (!smu_table->hardcode_pptable) {
511 memcpy(smu_table->hardcode_pptable, buf, size);
512 smu_table->power_play_table = smu_table->hardcode_pptable;
513 smu_table->power_play_table_size = size;
516 * Special hw_fini action(for Navi1x, the DPMs disablement will be
517 * skipped) may be needed for custom pptable uploading.
519 smu->uploading_custom_pp_table = true;
521 ret = smu_reset(smu);
523 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
525 smu->uploading_custom_pp_table = false;
528 mutex_unlock(&smu->mutex);
532 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
534 struct smu_feature *feature = &smu->smu_feature;
536 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
538 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
540 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
545 bitmap_or(feature->allowed, feature->allowed,
546 (unsigned long *)allowed_feature_mask,
547 feature->feature_num);
552 static int smu_set_funcs(struct amdgpu_device *adev)
554 struct smu_context *smu = &adev->smu;
556 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
557 smu->od_enabled = true;
559 switch (adev->asic_type) {
563 navi10_set_ppt_funcs(smu);
566 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
567 arcturus_set_ppt_funcs(smu);
568 /* OD is not supported on Arcturus */
569 smu->od_enabled =false;
571 case CHIP_SIENNA_CICHLID:
572 case CHIP_NAVY_FLOUNDER:
573 case CHIP_DIMGREY_CAVEFISH:
574 case CHIP_BEIGE_GOBY:
575 sienna_cichlid_set_ppt_funcs(smu);
578 aldebaran_set_ppt_funcs(smu);
579 /* Enable pp_od_clk_voltage node */
580 smu->od_enabled = true;
583 renoir_set_ppt_funcs(smu);
586 vangogh_set_ppt_funcs(smu);
588 case CHIP_YELLOW_CARP:
589 yellow_carp_set_ppt_funcs(smu);
598 static int smu_early_init(void *handle)
600 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
601 struct smu_context *smu = &adev->smu;
604 smu->pm_enabled = !!amdgpu_dpm;
606 mutex_init(&smu->mutex);
607 mutex_init(&smu->smu_baco.mutex);
608 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
609 smu->smu_baco.platform_support = false;
611 adev->powerplay.pp_handle = smu;
612 adev->powerplay.pp_funcs = &swsmu_pm_funcs;
614 return smu_set_funcs(adev);
617 static int smu_set_default_dpm_table(struct smu_context *smu)
619 struct smu_power_context *smu_power = &smu->smu_power;
620 struct smu_power_gate *power_gate = &smu_power->power_gate;
621 int vcn_gate, jpeg_gate;
624 if (!smu->ppt_funcs->set_default_dpm_table)
627 mutex_lock(&power_gate->vcn_gate_lock);
628 mutex_lock(&power_gate->jpeg_gate_lock);
630 vcn_gate = atomic_read(&power_gate->vcn_gated);
631 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
633 ret = smu_dpm_set_vcn_enable_locked(smu, true);
637 ret = smu_dpm_set_jpeg_enable_locked(smu, true);
641 ret = smu->ppt_funcs->set_default_dpm_table(smu);
643 dev_err(smu->adev->dev,
644 "Failed to setup default dpm clock tables!\n");
646 smu_dpm_set_jpeg_enable_locked(smu, !jpeg_gate);
648 smu_dpm_set_vcn_enable_locked(smu, !vcn_gate);
650 mutex_unlock(&power_gate->jpeg_gate_lock);
651 mutex_unlock(&power_gate->vcn_gate_lock);
657 static int smu_late_init(void *handle)
659 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
660 struct smu_context *smu = &adev->smu;
663 smu_set_fine_grain_gfx_freq_parameters(smu);
665 if (!smu->pm_enabled)
668 ret = smu_post_init(smu);
670 dev_err(adev->dev, "Failed to post smu init!\n");
674 if (adev->asic_type == CHIP_YELLOW_CARP)
677 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
678 ret = smu_set_default_od_settings(smu);
680 dev_err(adev->dev, "Failed to setup default OD settings!\n");
685 ret = smu_populate_umd_state_clk(smu);
687 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
691 ret = smu_get_asic_power_limits(smu);
693 dev_err(adev->dev, "Failed to get asic power limits!\n");
697 if (!amdgpu_sriov_vf(adev))
698 smu_get_unique_id(smu);
700 smu_get_fan_parameters(smu);
702 smu_handle_task(&adev->smu,
703 smu->smu_dpm.dpm_level,
704 AMD_PP_TASK_COMPLETE_INIT,
707 smu_restore_dpm_user_profile(smu);
712 static int smu_init_fb_allocations(struct smu_context *smu)
714 struct amdgpu_device *adev = smu->adev;
715 struct smu_table_context *smu_table = &smu->smu_table;
716 struct smu_table *tables = smu_table->tables;
717 struct smu_table *driver_table = &(smu_table->driver_table);
718 uint32_t max_table_size = 0;
721 /* VRAM allocation for tool table */
722 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
723 ret = amdgpu_bo_create_kernel(adev,
724 tables[SMU_TABLE_PMSTATUSLOG].size,
725 tables[SMU_TABLE_PMSTATUSLOG].align,
726 tables[SMU_TABLE_PMSTATUSLOG].domain,
727 &tables[SMU_TABLE_PMSTATUSLOG].bo,
728 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
729 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
731 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
736 /* VRAM allocation for driver table */
737 for (i = 0; i < SMU_TABLE_COUNT; i++) {
738 if (tables[i].size == 0)
741 if (i == SMU_TABLE_PMSTATUSLOG)
744 if (max_table_size < tables[i].size)
745 max_table_size = tables[i].size;
748 driver_table->size = max_table_size;
749 driver_table->align = PAGE_SIZE;
750 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
752 ret = amdgpu_bo_create_kernel(adev,
755 driver_table->domain,
757 &driver_table->mc_address,
758 &driver_table->cpu_addr);
760 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
761 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
762 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
763 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
764 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
770 static int smu_fini_fb_allocations(struct smu_context *smu)
772 struct smu_table_context *smu_table = &smu->smu_table;
773 struct smu_table *tables = smu_table->tables;
774 struct smu_table *driver_table = &(smu_table->driver_table);
776 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
777 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
778 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
779 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
781 amdgpu_bo_free_kernel(&driver_table->bo,
782 &driver_table->mc_address,
783 &driver_table->cpu_addr);
789 * smu_alloc_memory_pool - allocate memory pool in the system memory
791 * @smu: amdgpu_device pointer
793 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
794 * and DramLogSetDramAddr can notify it changed.
796 * Returns 0 on success, error on failure.
798 static int smu_alloc_memory_pool(struct smu_context *smu)
800 struct amdgpu_device *adev = smu->adev;
801 struct smu_table_context *smu_table = &smu->smu_table;
802 struct smu_table *memory_pool = &smu_table->memory_pool;
803 uint64_t pool_size = smu->pool_size;
806 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
809 memory_pool->size = pool_size;
810 memory_pool->align = PAGE_SIZE;
811 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
814 case SMU_MEMORY_POOL_SIZE_256_MB:
815 case SMU_MEMORY_POOL_SIZE_512_MB:
816 case SMU_MEMORY_POOL_SIZE_1_GB:
817 case SMU_MEMORY_POOL_SIZE_2_GB:
818 ret = amdgpu_bo_create_kernel(adev,
823 &memory_pool->mc_address,
824 &memory_pool->cpu_addr);
826 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
835 static int smu_free_memory_pool(struct smu_context *smu)
837 struct smu_table_context *smu_table = &smu->smu_table;
838 struct smu_table *memory_pool = &smu_table->memory_pool;
840 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
843 amdgpu_bo_free_kernel(&memory_pool->bo,
844 &memory_pool->mc_address,
845 &memory_pool->cpu_addr);
847 memset(memory_pool, 0, sizeof(struct smu_table));
852 static int smu_alloc_dummy_read_table(struct smu_context *smu)
854 struct smu_table_context *smu_table = &smu->smu_table;
855 struct smu_table *dummy_read_1_table =
856 &smu_table->dummy_read_1_table;
857 struct amdgpu_device *adev = smu->adev;
860 dummy_read_1_table->size = 0x40000;
861 dummy_read_1_table->align = PAGE_SIZE;
862 dummy_read_1_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
864 ret = amdgpu_bo_create_kernel(adev,
865 dummy_read_1_table->size,
866 dummy_read_1_table->align,
867 dummy_read_1_table->domain,
868 &dummy_read_1_table->bo,
869 &dummy_read_1_table->mc_address,
870 &dummy_read_1_table->cpu_addr);
872 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
877 static void smu_free_dummy_read_table(struct smu_context *smu)
879 struct smu_table_context *smu_table = &smu->smu_table;
880 struct smu_table *dummy_read_1_table =
881 &smu_table->dummy_read_1_table;
884 amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
885 &dummy_read_1_table->mc_address,
886 &dummy_read_1_table->cpu_addr);
888 memset(dummy_read_1_table, 0, sizeof(struct smu_table));
891 static int smu_smc_table_sw_init(struct smu_context *smu)
896 * Create smu_table structure, and init smc tables such as
897 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
899 ret = smu_init_smc_tables(smu);
901 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
906 * Create smu_power_context structure, and allocate smu_dpm_context and
907 * context size to fill the smu_power_context data.
909 ret = smu_init_power(smu);
911 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
916 * allocate vram bos to store smc table contents.
918 ret = smu_init_fb_allocations(smu);
922 ret = smu_alloc_memory_pool(smu);
926 ret = smu_alloc_dummy_read_table(smu);
930 ret = smu_i2c_init(smu, &smu->adev->pm.smu_i2c);
937 static int smu_smc_table_sw_fini(struct smu_context *smu)
941 smu_i2c_fini(smu, &smu->adev->pm.smu_i2c);
943 smu_free_dummy_read_table(smu);
945 ret = smu_free_memory_pool(smu);
949 ret = smu_fini_fb_allocations(smu);
953 ret = smu_fini_power(smu);
955 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
959 ret = smu_fini_smc_tables(smu);
961 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
968 static void smu_throttling_logging_work_fn(struct work_struct *work)
970 struct smu_context *smu = container_of(work, struct smu_context,
971 throttling_logging_work);
973 smu_log_thermal_throttling(smu);
976 static void smu_interrupt_work_fn(struct work_struct *work)
978 struct smu_context *smu = container_of(work, struct smu_context,
981 mutex_lock(&smu->mutex);
983 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
984 smu->ppt_funcs->interrupt_work(smu);
986 mutex_unlock(&smu->mutex);
989 static int smu_sw_init(void *handle)
991 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
992 struct smu_context *smu = &adev->smu;
995 smu->pool_size = adev->pm.smu_prv_buffer_size;
996 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
997 mutex_init(&smu->smu_feature.mutex);
998 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
999 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
1000 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1002 mutex_init(&smu->sensor_lock);
1003 mutex_init(&smu->metrics_lock);
1004 mutex_init(&smu->message_lock);
1006 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
1007 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
1008 atomic64_set(&smu->throttle_int_counter, 0);
1009 smu->watermarks_bitmap = 0;
1010 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1011 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1013 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
1014 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
1015 mutex_init(&smu->smu_power.power_gate.vcn_gate_lock);
1016 mutex_init(&smu->smu_power.power_gate.jpeg_gate_lock);
1018 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
1019 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
1020 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
1021 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
1022 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
1023 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
1024 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
1025 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
1027 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1028 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1029 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
1030 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
1031 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
1032 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
1033 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
1034 smu->display_config = &adev->pm.pm_display_cfg;
1036 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1037 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1039 ret = smu_init_microcode(smu);
1041 dev_err(adev->dev, "Failed to load smu firmware!\n");
1045 ret = smu_smc_table_sw_init(smu);
1047 dev_err(adev->dev, "Failed to sw init smc table!\n");
1051 ret = smu_register_irq_handler(smu);
1053 dev_err(adev->dev, "Failed to register smc irq handler!\n");
1057 /* If there is no way to query fan control mode, fan control is not supported */
1058 if (!smu->ppt_funcs->get_fan_control_mode)
1059 smu->adev->pm.no_fan = true;
1064 static int smu_sw_fini(void *handle)
1066 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1067 struct smu_context *smu = &adev->smu;
1070 ret = smu_smc_table_sw_fini(smu);
1072 dev_err(adev->dev, "Failed to sw fini smc table!\n");
1076 smu_fini_microcode(smu);
1081 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1083 struct amdgpu_device *adev = smu->adev;
1084 struct smu_temperature_range *range =
1085 &smu->thermal_range;
1088 if (!smu->ppt_funcs->get_thermal_temperature_range)
1091 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1095 adev->pm.dpm.thermal.min_temp = range->min;
1096 adev->pm.dpm.thermal.max_temp = range->max;
1097 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1098 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1099 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1100 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1101 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1102 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1103 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1108 static int smu_smc_hw_setup(struct smu_context *smu)
1110 struct amdgpu_device *adev = smu->adev;
1111 uint32_t pcie_gen = 0, pcie_width = 0;
1114 if (adev->in_suspend && smu_is_dpm_running(smu)) {
1115 dev_info(adev->dev, "dpm has been enabled\n");
1116 /* this is needed specifically */
1117 if ((adev->asic_type >= CHIP_SIENNA_CICHLID) &&
1118 (adev->asic_type <= CHIP_DIMGREY_CAVEFISH))
1119 ret = smu_system_features_control(smu, true);
1123 ret = smu_init_display_count(smu, 0);
1125 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1129 ret = smu_set_driver_table_location(smu);
1131 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1136 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1138 ret = smu_set_tool_table_location(smu);
1140 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1145 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1148 ret = smu_notify_memory_pool_location(smu);
1150 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1154 /* smu_dump_pptable(smu); */
1156 * Copy pptable bo in the vram to smc with SMU MSGs such as
1157 * SetDriverDramAddr and TransferTableDram2Smu.
1159 ret = smu_write_pptable(smu);
1161 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1165 /* issue Run*Btc msg */
1166 ret = smu_run_btc(smu);
1170 ret = smu_feature_set_allowed_mask(smu);
1172 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1176 ret = smu_system_features_control(smu, true);
1178 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1182 if (!smu_is_dpm_running(smu))
1183 dev_info(adev->dev, "dpm has been disabled\n");
1185 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1187 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1189 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1191 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1194 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1195 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1196 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
1198 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1200 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1202 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1204 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1206 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1208 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1210 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1212 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1216 ret = smu_get_thermal_temperature_range(smu);
1218 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1222 ret = smu_enable_thermal_alert(smu);
1224 dev_err(adev->dev, "Failed to enable thermal alert!\n");
1229 * Set initialized values (get from vbios) to dpm tables context such as
1230 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1233 ret = smu_set_default_dpm_table(smu);
1235 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1239 ret = smu_notify_display_change(smu);
1244 * Set min deep sleep dce fclk with bootup value from vbios via
1245 * SetMinDeepSleepDcefclk MSG.
1247 ret = smu_set_min_dcef_deep_sleep(smu,
1248 smu->smu_table.boot_values.dcefclk / 100);
1255 static int smu_start_smc_engine(struct smu_context *smu)
1257 struct amdgpu_device *adev = smu->adev;
1260 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1261 if (adev->asic_type < CHIP_NAVI10) {
1262 if (smu->ppt_funcs->load_microcode) {
1263 ret = smu->ppt_funcs->load_microcode(smu);
1270 if (smu->ppt_funcs->check_fw_status) {
1271 ret = smu->ppt_funcs->check_fw_status(smu);
1273 dev_err(adev->dev, "SMC is not ready\n");
1279 * Send msg GetDriverIfVersion to check if the return value is equal
1280 * with DRIVER_IF_VERSION of smc header.
1282 ret = smu_check_fw_version(smu);
1289 static int smu_hw_init(void *handle)
1292 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1293 struct smu_context *smu = &adev->smu;
1295 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1296 smu->pm_enabled = false;
1300 ret = smu_start_smc_engine(smu);
1302 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1307 smu_powergate_sdma(&adev->smu, false);
1308 smu_dpm_set_vcn_enable(smu, true);
1309 smu_dpm_set_jpeg_enable(smu, true);
1310 smu_set_gfx_cgpg(&adev->smu, true);
1313 if (!smu->pm_enabled)
1316 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1317 ret = smu_get_vbios_bootup_values(smu);
1319 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1323 ret = smu_setup_pptable(smu);
1325 dev_err(adev->dev, "Failed to setup pptable!\n");
1329 ret = smu_get_driver_allowed_feature_mask(smu);
1333 ret = smu_smc_hw_setup(smu);
1335 dev_err(adev->dev, "Failed to setup smc hw!\n");
1340 * Move maximum sustainable clock retrieving here considering
1341 * 1. It is not needed on resume(from S3).
1342 * 2. DAL settings come between .hw_init and .late_init of SMU.
1343 * And DAL needs to know the maximum sustainable clocks. Thus
1344 * it cannot be put in .late_init().
1346 ret = smu_init_max_sustainable_clocks(smu);
1348 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1352 adev->pm.dpm_enabled = true;
1354 dev_info(adev->dev, "SMU is initialized successfully!\n");
1359 static int smu_disable_dpms(struct smu_context *smu)
1361 struct amdgpu_device *adev = smu->adev;
1363 bool use_baco = !smu->is_apu &&
1364 ((amdgpu_in_reset(adev) &&
1365 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1366 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1369 * For custom pptable uploading, skip the DPM features
1370 * disable process on Navi1x ASICs.
1371 * - As the gfx related features are under control of
1372 * RLC on those ASICs. RLC reinitialization will be
1373 * needed to reenable them. That will cost much more
1376 * - SMU firmware can handle the DPM reenablement
1379 if (smu->uploading_custom_pp_table &&
1380 (adev->asic_type >= CHIP_NAVI10) &&
1381 (adev->asic_type <= CHIP_DIMGREY_CAVEFISH))
1385 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1386 * on BACO in. Driver involvement is unnecessary.
1388 if ((adev->asic_type == CHIP_SIENNA_CICHLID) &&
1393 * For gpu reset, runpm and hibernation through BACO,
1394 * BACO feature has to be kept enabled.
1396 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1397 ret = smu_disable_all_features_with_exception(smu,
1398 SMU_FEATURE_BACO_BIT);
1400 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1402 ret = smu_system_features_control(smu, false);
1404 dev_err(adev->dev, "Failed to disable smu features.\n");
1407 if (adev->asic_type >= CHIP_NAVI10 &&
1408 adev->gfx.rlc.funcs->stop)
1409 adev->gfx.rlc.funcs->stop(adev);
1414 static int smu_smc_hw_cleanup(struct smu_context *smu)
1416 struct amdgpu_device *adev = smu->adev;
1419 cancel_work_sync(&smu->throttling_logging_work);
1420 cancel_work_sync(&smu->interrupt_work);
1422 ret = smu_disable_thermal_alert(smu);
1424 dev_err(adev->dev, "Fail to disable thermal alert!\n");
1428 ret = smu_disable_dpms(smu);
1430 dev_err(adev->dev, "Fail to disable dpm features!\n");
1437 static int smu_hw_fini(void *handle)
1439 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1440 struct smu_context *smu = &adev->smu;
1442 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1446 smu_powergate_sdma(&adev->smu, true);
1447 smu_dpm_set_vcn_enable(smu, false);
1448 smu_dpm_set_jpeg_enable(smu, false);
1451 if (!smu->pm_enabled)
1454 adev->pm.dpm_enabled = false;
1456 return smu_smc_hw_cleanup(smu);
1459 static int smu_reset(struct smu_context *smu)
1461 struct amdgpu_device *adev = smu->adev;
1464 amdgpu_gfx_off_ctrl(smu->adev, false);
1466 ret = smu_hw_fini(adev);
1470 ret = smu_hw_init(adev);
1474 ret = smu_late_init(adev);
1478 amdgpu_gfx_off_ctrl(smu->adev, true);
1483 static int smu_suspend(void *handle)
1485 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1486 struct smu_context *smu = &adev->smu;
1489 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1492 if (!smu->pm_enabled)
1495 adev->pm.dpm_enabled = false;
1497 ret = smu_smc_hw_cleanup(smu);
1501 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1503 /* skip CGPG when in S0ix */
1504 if (smu->is_apu && !adev->in_s0ix)
1505 smu_set_gfx_cgpg(&adev->smu, false);
1510 static int smu_resume(void *handle)
1513 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1514 struct smu_context *smu = &adev->smu;
1516 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1519 if (!smu->pm_enabled)
1522 dev_info(adev->dev, "SMU is resuming...\n");
1524 ret = smu_start_smc_engine(smu);
1526 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1530 ret = smu_smc_hw_setup(smu);
1532 dev_err(adev->dev, "Failed to setup smc hw!\n");
1537 smu_set_gfx_cgpg(&adev->smu, true);
1539 smu->disable_uclk_switch = 0;
1541 adev->pm.dpm_enabled = true;
1543 dev_info(adev->dev, "SMU is resumed successfully!\n");
1548 static int smu_display_configuration_change(void *handle,
1549 const struct amd_pp_display_configuration *display_config)
1551 struct smu_context *smu = handle;
1553 int num_of_active_display = 0;
1555 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1558 if (!display_config)
1561 mutex_lock(&smu->mutex);
1563 smu_set_min_dcef_deep_sleep(smu,
1564 display_config->min_dcef_deep_sleep_set_clk / 100);
1566 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1567 if (display_config->displays[index].controller_id != 0)
1568 num_of_active_display++;
1571 mutex_unlock(&smu->mutex);
1576 static int smu_set_clockgating_state(void *handle,
1577 enum amd_clockgating_state state)
1582 static int smu_set_powergating_state(void *handle,
1583 enum amd_powergating_state state)
1588 static int smu_enable_umd_pstate(void *handle,
1589 enum amd_dpm_forced_level *level)
1591 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1592 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1593 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1594 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1596 struct smu_context *smu = (struct smu_context*)(handle);
1597 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1599 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1602 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1603 /* enter umd pstate, save current level, disable gfx cg*/
1604 if (*level & profile_mode_mask) {
1605 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1606 smu_dpm_ctx->enable_umd_pstate = true;
1607 smu_gpo_control(smu, false);
1608 amdgpu_device_ip_set_powergating_state(smu->adev,
1609 AMD_IP_BLOCK_TYPE_GFX,
1610 AMD_PG_STATE_UNGATE);
1611 amdgpu_device_ip_set_clockgating_state(smu->adev,
1612 AMD_IP_BLOCK_TYPE_GFX,
1613 AMD_CG_STATE_UNGATE);
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 smu_dpm_ctx->enable_umd_pstate = false;
1624 amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
1625 smu_deep_sleep_control(smu, true);
1626 smu_gfx_ulv_control(smu, true);
1627 amdgpu_device_ip_set_clockgating_state(smu->adev,
1628 AMD_IP_BLOCK_TYPE_GFX,
1630 amdgpu_device_ip_set_powergating_state(smu->adev,
1631 AMD_IP_BLOCK_TYPE_GFX,
1633 smu_gpo_control(smu, true);
1640 static int smu_bump_power_profile_mode(struct smu_context *smu,
1642 uint32_t param_size)
1646 if (smu->ppt_funcs->set_power_profile_mode)
1647 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
1652 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
1653 enum amd_dpm_forced_level level,
1654 bool skip_display_settings)
1659 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1661 if (!skip_display_settings) {
1662 ret = smu_display_config_changed(smu);
1664 dev_err(smu->adev->dev, "Failed to change display config!");
1669 ret = smu_apply_clocks_adjust_rules(smu);
1671 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
1675 if (!skip_display_settings) {
1676 ret = smu_notify_smc_display_config(smu);
1678 dev_err(smu->adev->dev, "Failed to notify smc display config!");
1683 if (smu_dpm_ctx->dpm_level != level) {
1684 ret = smu_asic_set_performance_level(smu, level);
1686 dev_err(smu->adev->dev, "Failed to set performance level!");
1690 /* update the saved copy */
1691 smu_dpm_ctx->dpm_level = level;
1694 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1695 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
1696 index = fls(smu->workload_mask);
1697 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1698 workload = smu->workload_setting[index];
1700 if (smu->power_profile_mode != workload)
1701 smu_bump_power_profile_mode(smu, &workload, 0);
1707 static int smu_handle_task(struct smu_context *smu,
1708 enum amd_dpm_forced_level level,
1709 enum amd_pp_task task_id,
1714 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1718 mutex_lock(&smu->mutex);
1721 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1722 ret = smu_pre_display_config_changed(smu);
1725 ret = smu_adjust_power_state_dynamic(smu, level, false);
1727 case AMD_PP_TASK_COMPLETE_INIT:
1728 case AMD_PP_TASK_READJUST_POWER_STATE:
1729 ret = smu_adjust_power_state_dynamic(smu, level, true);
1737 mutex_unlock(&smu->mutex);
1742 static int smu_handle_dpm_task(void *handle,
1743 enum amd_pp_task task_id,
1744 enum amd_pm_state_type *user_state)
1746 struct smu_context *smu = handle;
1747 struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
1749 return smu_handle_task(smu, smu_dpm->dpm_level, task_id, true);
1753 static int smu_switch_power_profile(void *handle,
1754 enum PP_SMC_POWER_PROFILE type,
1757 struct smu_context *smu = handle;
1758 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1762 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1765 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1768 mutex_lock(&smu->mutex);
1771 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1772 index = fls(smu->workload_mask);
1773 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1774 workload = smu->workload_setting[index];
1776 smu->workload_mask |= (1 << smu->workload_prority[type]);
1777 index = fls(smu->workload_mask);
1778 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1779 workload = smu->workload_setting[index];
1782 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1783 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
1784 smu_bump_power_profile_mode(smu, &workload, 0);
1786 mutex_unlock(&smu->mutex);
1791 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
1793 struct smu_context *smu = handle;
1794 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1795 enum amd_dpm_forced_level level;
1797 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1800 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1803 mutex_lock(&(smu->mutex));
1804 level = smu_dpm_ctx->dpm_level;
1805 mutex_unlock(&(smu->mutex));
1810 static int smu_force_performance_level(void *handle,
1811 enum amd_dpm_forced_level level)
1813 struct smu_context *smu = handle;
1814 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1817 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1820 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1823 mutex_lock(&smu->mutex);
1825 ret = smu_enable_umd_pstate(smu, &level);
1827 mutex_unlock(&smu->mutex);
1831 ret = smu_handle_task(smu, level,
1832 AMD_PP_TASK_READJUST_POWER_STATE,
1835 mutex_unlock(&smu->mutex);
1837 /* reset user dpm clock state */
1838 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1839 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
1840 smu->user_dpm_profile.clk_dependency = 0;
1846 static int smu_set_display_count(void *handle, uint32_t count)
1848 struct smu_context *smu = handle;
1851 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1854 mutex_lock(&smu->mutex);
1855 ret = smu_init_display_count(smu, count);
1856 mutex_unlock(&smu->mutex);
1861 static int smu_force_smuclk_levels(struct smu_context *smu,
1862 enum smu_clk_type clk_type,
1865 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1868 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1871 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1872 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
1876 mutex_lock(&smu->mutex);
1878 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
1879 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1880 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
1881 smu->user_dpm_profile.clk_mask[clk_type] = mask;
1882 smu_set_user_clk_dependencies(smu, clk_type);
1886 mutex_unlock(&smu->mutex);
1891 static int smu_force_ppclk_levels(void *handle,
1892 enum pp_clock_type type,
1895 struct smu_context *smu = handle;
1896 enum smu_clk_type clk_type;
1900 clk_type = SMU_SCLK; break;
1902 clk_type = SMU_MCLK; break;
1904 clk_type = SMU_PCIE; break;
1906 clk_type = SMU_SOCCLK; break;
1908 clk_type = SMU_FCLK; break;
1910 clk_type = SMU_DCEFCLK; break;
1912 clk_type = SMU_VCLK; break;
1914 clk_type = SMU_DCLK; break;
1916 clk_type = SMU_OD_SCLK; break;
1918 clk_type = SMU_OD_MCLK; break;
1920 clk_type = SMU_OD_VDDC_CURVE; break;
1922 clk_type = SMU_OD_RANGE; break;
1927 return smu_force_smuclk_levels(smu, clk_type, mask);
1931 * On system suspending or resetting, the dpm_enabled
1932 * flag will be cleared. So that those SMU services which
1933 * are not supported will be gated.
1934 * However, the mp1 state setting should still be granted
1935 * even if the dpm_enabled cleared.
1937 static int smu_set_mp1_state(void *handle,
1938 enum pp_mp1_state mp1_state)
1940 struct smu_context *smu = handle;
1943 if (!smu->pm_enabled)
1946 mutex_lock(&smu->mutex);
1948 if (smu->ppt_funcs &&
1949 smu->ppt_funcs->set_mp1_state)
1950 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
1952 mutex_unlock(&smu->mutex);
1957 static int smu_set_df_cstate(void *handle,
1958 enum pp_df_cstate state)
1960 struct smu_context *smu = handle;
1963 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1966 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
1969 mutex_lock(&smu->mutex);
1971 ret = smu->ppt_funcs->set_df_cstate(smu, state);
1973 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
1975 mutex_unlock(&smu->mutex);
1980 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en)
1984 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1987 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down)
1990 mutex_lock(&smu->mutex);
1992 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en);
1994 dev_err(smu->adev->dev, "[AllowXgmiPowerDown] failed!\n");
1996 mutex_unlock(&smu->mutex);
2001 int smu_write_watermarks_table(struct smu_context *smu)
2005 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2008 mutex_lock(&smu->mutex);
2010 ret = smu_set_watermarks_table(smu, NULL);
2012 mutex_unlock(&smu->mutex);
2017 static int smu_set_watermarks_for_clock_ranges(void *handle,
2018 struct pp_smu_wm_range_sets *clock_ranges)
2020 struct smu_context *smu = handle;
2023 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2026 if (smu->disable_watermark)
2029 mutex_lock(&smu->mutex);
2031 ret = smu_set_watermarks_table(smu, clock_ranges);
2033 mutex_unlock(&smu->mutex);
2038 int smu_set_ac_dc(struct smu_context *smu)
2042 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2045 /* controlled by firmware */
2046 if (smu->dc_controlled_by_gpio)
2049 mutex_lock(&smu->mutex);
2050 ret = smu_set_power_source(smu,
2051 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
2052 SMU_POWER_SOURCE_DC);
2054 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
2055 smu->adev->pm.ac_power ? "AC" : "DC");
2056 mutex_unlock(&smu->mutex);
2061 const struct amd_ip_funcs smu_ip_funcs = {
2063 .early_init = smu_early_init,
2064 .late_init = smu_late_init,
2065 .sw_init = smu_sw_init,
2066 .sw_fini = smu_sw_fini,
2067 .hw_init = smu_hw_init,
2068 .hw_fini = smu_hw_fini,
2069 .suspend = smu_suspend,
2070 .resume = smu_resume,
2072 .check_soft_reset = NULL,
2073 .wait_for_idle = NULL,
2075 .set_clockgating_state = smu_set_clockgating_state,
2076 .set_powergating_state = smu_set_powergating_state,
2077 .enable_umd_pstate = smu_enable_umd_pstate,
2080 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
2082 .type = AMD_IP_BLOCK_TYPE_SMC,
2086 .funcs = &smu_ip_funcs,
2089 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
2091 .type = AMD_IP_BLOCK_TYPE_SMC,
2095 .funcs = &smu_ip_funcs,
2098 const struct amdgpu_ip_block_version smu_v13_0_ip_block =
2100 .type = AMD_IP_BLOCK_TYPE_SMC,
2104 .funcs = &smu_ip_funcs,
2107 static int smu_load_microcode(void *handle)
2109 struct smu_context *smu = handle;
2110 struct amdgpu_device *adev = smu->adev;
2113 if (!smu->pm_enabled)
2116 /* This should be used for non PSP loading */
2117 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2120 if (smu->ppt_funcs->load_microcode) {
2121 ret = smu->ppt_funcs->load_microcode(smu);
2123 dev_err(adev->dev, "Load microcode failed\n");
2128 if (smu->ppt_funcs->check_fw_status) {
2129 ret = smu->ppt_funcs->check_fw_status(smu);
2131 dev_err(adev->dev, "SMC is not ready\n");
2139 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2143 mutex_lock(&smu->mutex);
2145 if (smu->ppt_funcs->set_gfx_cgpg)
2146 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2148 mutex_unlock(&smu->mutex);
2153 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2155 struct smu_context *smu = handle;
2159 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2162 mutex_lock(&smu->mutex);
2164 if (smu->ppt_funcs->set_fan_speed_percent) {
2165 percent = speed * 100 / smu->fan_max_rpm;
2166 ret = smu->ppt_funcs->set_fan_speed_percent(smu, percent);
2167 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2168 smu->user_dpm_profile.fan_speed_percent = percent;
2171 mutex_unlock(&smu->mutex);
2176 int smu_get_power_limit(void *handle,
2178 enum pp_power_limit_level pp_limit_level,
2179 enum pp_power_type pp_power_type)
2181 struct smu_context *smu = handle;
2182 enum smu_ppt_limit_level limit_level;
2183 uint32_t limit_type;
2186 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2189 switch(pp_power_type) {
2190 case PP_PWR_TYPE_SUSTAINED:
2191 limit_type = SMU_DEFAULT_PPT_LIMIT;
2193 case PP_PWR_TYPE_FAST:
2194 limit_type = SMU_FAST_PPT_LIMIT;
2201 switch(pp_limit_level){
2202 case PP_PWR_LIMIT_CURRENT:
2203 limit_level = SMU_PPT_LIMIT_CURRENT;
2205 case PP_PWR_LIMIT_DEFAULT:
2206 limit_level = SMU_PPT_LIMIT_DEFAULT;
2208 case PP_PWR_LIMIT_MAX:
2209 limit_level = SMU_PPT_LIMIT_MAX;
2211 case PP_PWR_LIMIT_MIN:
2217 mutex_lock(&smu->mutex);
2219 if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2220 if (smu->ppt_funcs->get_ppt_limit)
2221 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2223 switch (limit_level) {
2224 case SMU_PPT_LIMIT_CURRENT:
2225 *limit = smu->current_power_limit;
2227 case SMU_PPT_LIMIT_DEFAULT:
2228 *limit = smu->default_power_limit;
2230 case SMU_PPT_LIMIT_MAX:
2231 *limit = smu->max_power_limit;
2238 mutex_unlock(&smu->mutex);
2243 static int smu_set_power_limit(void *handle, uint32_t limit)
2245 struct smu_context *smu = handle;
2246 uint32_t limit_type = limit >> 24;
2249 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2252 mutex_lock(&smu->mutex);
2254 if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2255 if (smu->ppt_funcs->set_power_limit) {
2256 ret = smu->ppt_funcs->set_power_limit(smu, limit);
2260 if (limit > smu->max_power_limit) {
2261 dev_err(smu->adev->dev,
2262 "New power limit (%d) is over the max allowed %d\n",
2263 limit, smu->max_power_limit);
2269 limit = smu->current_power_limit;
2271 if (smu->ppt_funcs->set_power_limit) {
2272 ret = smu->ppt_funcs->set_power_limit(smu, limit);
2273 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2274 smu->user_dpm_profile.power_limit = limit;
2278 mutex_unlock(&smu->mutex);
2283 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2287 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2290 mutex_lock(&smu->mutex);
2292 if (smu->ppt_funcs->print_clk_levels)
2293 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2295 mutex_unlock(&smu->mutex);
2300 static int smu_print_ppclk_levels(void *handle,
2301 enum pp_clock_type type,
2304 struct smu_context *smu = handle;
2305 enum smu_clk_type clk_type;
2309 clk_type = SMU_SCLK; break;
2311 clk_type = SMU_MCLK; break;
2313 clk_type = SMU_PCIE; break;
2315 clk_type = SMU_SOCCLK; break;
2317 clk_type = SMU_FCLK; break;
2319 clk_type = SMU_DCEFCLK; break;
2321 clk_type = SMU_VCLK; break;
2323 clk_type = SMU_DCLK; break;
2325 clk_type = SMU_OD_SCLK; break;
2327 clk_type = SMU_OD_MCLK; break;
2329 clk_type = SMU_OD_VDDC_CURVE; break;
2331 clk_type = SMU_OD_RANGE; break;
2332 case OD_VDDGFX_OFFSET:
2333 clk_type = SMU_OD_VDDGFX_OFFSET; break;
2335 clk_type = SMU_OD_CCLK; break;
2340 return smu_print_smuclk_levels(smu, clk_type, buf);
2343 static int smu_od_edit_dpm_table(void *handle,
2344 enum PP_OD_DPM_TABLE_COMMAND type,
2345 long *input, uint32_t size)
2347 struct smu_context *smu = handle;
2350 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2353 mutex_lock(&smu->mutex);
2355 if (smu->ppt_funcs->od_edit_dpm_table) {
2356 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2359 mutex_unlock(&smu->mutex);
2364 static int smu_read_sensor(void *handle,
2369 struct smu_context *smu = handle;
2370 struct smu_umd_pstate_table *pstate_table =
2373 uint32_t *size, size_val;
2375 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2378 if (!data || !size_arg)
2381 size_val = *size_arg;
2384 mutex_lock(&smu->mutex);
2386 if (smu->ppt_funcs->read_sensor)
2387 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2391 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2392 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2395 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2396 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2399 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2400 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
2403 case AMDGPU_PP_SENSOR_UVD_POWER:
2404 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
2407 case AMDGPU_PP_SENSOR_VCE_POWER:
2408 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
2411 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
2412 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0: 1;
2415 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
2416 *(uint32_t *)data = 0;
2426 mutex_unlock(&smu->mutex);
2428 // assign uint32_t to int
2429 *size_arg = size_val;
2434 static int smu_get_power_profile_mode(void *handle, char *buf)
2436 struct smu_context *smu = handle;
2439 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2442 mutex_lock(&smu->mutex);
2444 if (smu->ppt_funcs->get_power_profile_mode)
2445 ret = smu->ppt_funcs->get_power_profile_mode(smu, buf);
2447 mutex_unlock(&smu->mutex);
2452 static int smu_set_power_profile_mode(void *handle,
2454 uint32_t param_size)
2456 struct smu_context *smu = handle;
2459 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2462 mutex_lock(&smu->mutex);
2464 smu_bump_power_profile_mode(smu, param, param_size);
2466 mutex_unlock(&smu->mutex);
2472 static u32 smu_get_fan_control_mode(void *handle)
2474 struct smu_context *smu = handle;
2477 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2478 return AMD_FAN_CTRL_NONE;
2480 mutex_lock(&smu->mutex);
2482 if (smu->ppt_funcs->get_fan_control_mode)
2483 ret = smu->ppt_funcs->get_fan_control_mode(smu);
2485 mutex_unlock(&smu->mutex);
2490 static int smu_set_fan_control_mode(struct smu_context *smu, int value)
2494 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2497 mutex_lock(&smu->mutex);
2499 if (smu->ppt_funcs->set_fan_control_mode) {
2500 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2501 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2502 smu->user_dpm_profile.fan_mode = value;
2505 mutex_unlock(&smu->mutex);
2507 /* reset user dpm fan speed */
2508 if (!ret && value != AMD_FAN_CTRL_MANUAL &&
2509 !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2510 smu->user_dpm_profile.fan_speed_percent = 0;
2515 static void smu_pp_set_fan_control_mode(void *handle, u32 value)
2517 struct smu_context *smu = handle;
2519 smu_set_fan_control_mode(smu, value);
2523 static int smu_get_fan_speed_percent(void *handle, u32 *speed)
2525 struct smu_context *smu = handle;
2529 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2532 mutex_lock(&smu->mutex);
2534 if (smu->ppt_funcs->get_fan_speed_percent) {
2535 ret = smu->ppt_funcs->get_fan_speed_percent(smu, &percent);
2537 *speed = percent > 100 ? 100 : percent;
2541 mutex_unlock(&smu->mutex);
2547 static int smu_set_fan_speed_percent(void *handle, u32 speed)
2549 struct smu_context *smu = handle;
2552 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2555 mutex_lock(&smu->mutex);
2557 if (smu->ppt_funcs->set_fan_speed_percent) {
2560 ret = smu->ppt_funcs->set_fan_speed_percent(smu, speed);
2561 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2562 smu->user_dpm_profile.fan_speed_percent = speed;
2565 mutex_unlock(&smu->mutex);
2570 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
2572 struct smu_context *smu = handle;
2576 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2579 mutex_lock(&smu->mutex);
2581 if (smu->ppt_funcs->get_fan_speed_percent) {
2582 ret = smu->ppt_funcs->get_fan_speed_percent(smu, &percent);
2583 *speed = percent * smu->fan_max_rpm / 100;
2586 mutex_unlock(&smu->mutex);
2591 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
2593 struct smu_context *smu = handle;
2596 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2599 mutex_lock(&smu->mutex);
2601 ret = smu_set_min_dcef_deep_sleep(smu, clk);
2603 mutex_unlock(&smu->mutex);
2608 static int smu_get_clock_by_type_with_latency(void *handle,
2609 enum amd_pp_clock_type type,
2610 struct pp_clock_levels_with_latency *clocks)
2612 struct smu_context *smu = handle;
2613 enum smu_clk_type clk_type;
2616 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2619 mutex_lock(&smu->mutex);
2621 if (smu->ppt_funcs->get_clock_by_type_with_latency) {
2623 case amd_pp_sys_clock:
2624 clk_type = SMU_GFXCLK;
2626 case amd_pp_mem_clock:
2627 clk_type = SMU_MCLK;
2629 case amd_pp_dcef_clock:
2630 clk_type = SMU_DCEFCLK;
2632 case amd_pp_disp_clock:
2633 clk_type = SMU_DISPCLK;
2636 dev_err(smu->adev->dev, "Invalid clock type!\n");
2637 mutex_unlock(&smu->mutex);
2641 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2644 mutex_unlock(&smu->mutex);
2649 static int smu_display_clock_voltage_request(void *handle,
2650 struct pp_display_clock_request *clock_req)
2652 struct smu_context *smu = handle;
2655 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2658 mutex_lock(&smu->mutex);
2660 if (smu->ppt_funcs->display_clock_voltage_request)
2661 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2663 mutex_unlock(&smu->mutex);
2669 static int smu_display_disable_memory_clock_switch(void *handle,
2670 bool disable_memory_clock_switch)
2672 struct smu_context *smu = handle;
2675 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2678 mutex_lock(&smu->mutex);
2680 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2681 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2683 mutex_unlock(&smu->mutex);
2688 static int smu_set_xgmi_pstate(void *handle,
2691 struct smu_context *smu = handle;
2694 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2697 mutex_lock(&smu->mutex);
2699 if (smu->ppt_funcs->set_xgmi_pstate)
2700 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2702 mutex_unlock(&smu->mutex);
2705 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
2710 static int smu_get_baco_capability(void *handle, bool *cap)
2712 struct smu_context *smu = handle;
2717 if (!smu->pm_enabled)
2720 mutex_lock(&smu->mutex);
2722 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2723 *cap = smu->ppt_funcs->baco_is_support(smu);
2725 mutex_unlock(&smu->mutex);
2730 static int smu_baco_set_state(void *handle, int state)
2732 struct smu_context *smu = handle;
2735 if (!smu->pm_enabled)
2739 mutex_lock(&smu->mutex);
2741 if (smu->ppt_funcs->baco_exit)
2742 ret = smu->ppt_funcs->baco_exit(smu);
2744 mutex_unlock(&smu->mutex);
2745 } else if (state == 1) {
2746 mutex_lock(&smu->mutex);
2748 if (smu->ppt_funcs->baco_enter)
2749 ret = smu->ppt_funcs->baco_enter(smu);
2751 mutex_unlock(&smu->mutex);
2758 dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
2759 (state)?"enter":"exit");
2764 bool smu_mode1_reset_is_support(struct smu_context *smu)
2768 if (!smu->pm_enabled)
2771 mutex_lock(&smu->mutex);
2773 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
2774 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
2776 mutex_unlock(&smu->mutex);
2781 bool smu_mode2_reset_is_support(struct smu_context *smu)
2785 if (!smu->pm_enabled)
2788 mutex_lock(&smu->mutex);
2790 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
2791 ret = smu->ppt_funcs->mode2_reset_is_support(smu);
2793 mutex_unlock(&smu->mutex);
2798 int smu_mode1_reset(struct smu_context *smu)
2802 if (!smu->pm_enabled)
2805 mutex_lock(&smu->mutex);
2807 if (smu->ppt_funcs->mode1_reset)
2808 ret = smu->ppt_funcs->mode1_reset(smu);
2810 mutex_unlock(&smu->mutex);
2815 static int smu_mode2_reset(void *handle)
2817 struct smu_context *smu = handle;
2820 if (!smu->pm_enabled)
2823 mutex_lock(&smu->mutex);
2825 if (smu->ppt_funcs->mode2_reset)
2826 ret = smu->ppt_funcs->mode2_reset(smu);
2828 mutex_unlock(&smu->mutex);
2831 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
2836 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
2837 struct pp_smu_nv_clock_table *max_clocks)
2839 struct smu_context *smu = handle;
2842 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2845 mutex_lock(&smu->mutex);
2847 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2848 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2850 mutex_unlock(&smu->mutex);
2855 static int smu_get_uclk_dpm_states(void *handle,
2856 unsigned int *clock_values_in_khz,
2857 unsigned int *num_states)
2859 struct smu_context *smu = handle;
2862 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2865 mutex_lock(&smu->mutex);
2867 if (smu->ppt_funcs->get_uclk_dpm_states)
2868 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2870 mutex_unlock(&smu->mutex);
2875 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
2877 struct smu_context *smu = handle;
2878 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2880 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2883 mutex_lock(&smu->mutex);
2885 if (smu->ppt_funcs->get_current_power_state)
2886 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2888 mutex_unlock(&smu->mutex);
2893 static int smu_get_dpm_clock_table(void *handle,
2894 struct dpm_clocks *clock_table)
2896 struct smu_context *smu = handle;
2899 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2902 mutex_lock(&smu->mutex);
2904 if (smu->ppt_funcs->get_dpm_clock_table)
2905 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2907 mutex_unlock(&smu->mutex);
2912 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
2914 struct smu_context *smu = handle;
2917 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2920 if (!smu->ppt_funcs->get_gpu_metrics)
2923 mutex_lock(&smu->mutex);
2925 size = smu->ppt_funcs->get_gpu_metrics(smu, table);
2927 mutex_unlock(&smu->mutex);
2932 static int smu_enable_mgpu_fan_boost(void *handle)
2934 struct smu_context *smu = handle;
2937 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2940 mutex_lock(&smu->mutex);
2942 if (smu->ppt_funcs->enable_mgpu_fan_boost)
2943 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
2945 mutex_unlock(&smu->mutex);
2950 static int smu_gfx_state_change_set(void *handle,
2953 struct smu_context *smu = handle;
2956 mutex_lock(&smu->mutex);
2957 if (smu->ppt_funcs->gfx_state_change_set)
2958 ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
2959 mutex_unlock(&smu->mutex);
2964 int smu_set_light_sbr(struct smu_context *smu, bool enable)
2968 mutex_lock(&smu->mutex);
2969 if (smu->ppt_funcs->set_light_sbr)
2970 ret = smu->ppt_funcs->set_light_sbr(smu, enable);
2971 mutex_unlock(&smu->mutex);
2976 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
2978 struct smu_context *smu = handle;
2979 struct smu_table_context *smu_table = &smu->smu_table;
2980 struct smu_table *memory_pool = &smu_table->memory_pool;
2987 mutex_lock(&smu->mutex);
2988 if (memory_pool->bo) {
2989 *addr = memory_pool->cpu_addr;
2990 *size = memory_pool->size;
2992 mutex_unlock(&smu->mutex);
2997 static const struct amd_pm_funcs swsmu_pm_funcs = {
2998 /* export for sysfs */
2999 .set_fan_control_mode = smu_pp_set_fan_control_mode,
3000 .get_fan_control_mode = smu_get_fan_control_mode,
3001 .set_fan_speed_percent = smu_set_fan_speed_percent,
3002 .get_fan_speed_percent = smu_get_fan_speed_percent,
3003 .force_clock_level = smu_force_ppclk_levels,
3004 .print_clock_levels = smu_print_ppclk_levels,
3005 .force_performance_level = smu_force_performance_level,
3006 .read_sensor = smu_read_sensor,
3007 .get_performance_level = smu_get_performance_level,
3008 .get_current_power_state = smu_get_current_power_state,
3009 .get_fan_speed_rpm = smu_get_fan_speed_rpm,
3010 .set_fan_speed_rpm = smu_set_fan_speed_rpm,
3011 .get_pp_num_states = smu_get_power_num_states,
3012 .get_pp_table = smu_sys_get_pp_table,
3013 .set_pp_table = smu_sys_set_pp_table,
3014 .switch_power_profile = smu_switch_power_profile,
3015 /* export to amdgpu */
3016 .dispatch_tasks = smu_handle_dpm_task,
3017 .load_firmware = smu_load_microcode,
3018 .set_powergating_by_smu = smu_dpm_set_power_gate,
3019 .set_power_limit = smu_set_power_limit,
3020 .get_power_limit = smu_get_power_limit,
3021 .get_power_profile_mode = smu_get_power_profile_mode,
3022 .set_power_profile_mode = smu_set_power_profile_mode,
3023 .odn_edit_dpm_table = smu_od_edit_dpm_table,
3024 .set_mp1_state = smu_set_mp1_state,
3025 .gfx_state_change_set = smu_gfx_state_change_set,
3027 .get_sclk = smu_get_sclk,
3028 .get_mclk = smu_get_mclk,
3029 .display_configuration_change = smu_display_configuration_change,
3030 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,
3031 .display_clock_voltage_request = smu_display_clock_voltage_request,
3032 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,
3033 .set_active_display_count = smu_set_display_count,
3034 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,
3035 .get_asic_baco_capability = smu_get_baco_capability,
3036 .set_asic_baco_state = smu_baco_set_state,
3037 .get_ppfeature_status = smu_sys_get_pp_feature_mask,
3038 .set_ppfeature_status = smu_sys_set_pp_feature_mask,
3039 .asic_reset_mode_2 = smu_mode2_reset,
3040 .set_df_cstate = smu_set_df_cstate,
3041 .set_xgmi_pstate = smu_set_xgmi_pstate,
3042 .get_gpu_metrics = smu_sys_get_gpu_metrics,
3043 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,
3044 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
3045 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,
3046 .get_uclk_dpm_states = smu_get_uclk_dpm_states,
3047 .get_dpm_clock_table = smu_get_dpm_clock_table,
3048 .get_smu_prv_buf_details = smu_get_prv_buffer_details,
3051 int smu_wait_for_event(struct amdgpu_device *adev, enum smu_event_type event,
3055 struct smu_context *smu = &adev->smu;
3057 if (smu->ppt_funcs->wait_for_event) {
3058 mutex_lock(&smu->mutex);
3059 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
3060 mutex_unlock(&smu->mutex);