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"
43 * DO NOT use these for err/warn/info/debug messages.
44 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
45 * They are more MGPU friendly.
52 static const struct amd_pm_funcs swsmu_pm_funcs;
53 static int smu_force_smuclk_levels(struct smu_context *smu,
54 enum smu_clk_type clk_type,
56 static int smu_handle_task(struct smu_context *smu,
57 enum amd_dpm_forced_level level,
58 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(struct smu_context *smu, int 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;
73 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
76 mutex_lock(&smu->mutex);
78 size = smu_get_pp_feature_mask(smu, buf);
80 mutex_unlock(&smu->mutex);
85 static int smu_sys_set_pp_feature_mask(void *handle,
88 struct smu_context *smu = handle;
91 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
94 mutex_lock(&smu->mutex);
96 ret = smu_set_pp_feature_mask(smu, new_mask);
98 mutex_unlock(&smu->mutex);
103 int smu_get_status_gfxoff(struct amdgpu_device *adev, uint32_t *value)
106 struct smu_context *smu = &adev->smu;
108 if (is_support_sw_smu(adev) && smu->ppt_funcs->get_gfx_off_status)
109 *value = smu_get_gfx_off_status(smu);
116 int smu_set_soft_freq_range(struct smu_context *smu,
117 enum smu_clk_type clk_type,
123 mutex_lock(&smu->mutex);
125 if (smu->ppt_funcs->set_soft_freq_limited_range)
126 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
131 mutex_unlock(&smu->mutex);
136 int smu_get_dpm_freq_range(struct smu_context *smu,
137 enum smu_clk_type clk_type,
146 mutex_lock(&smu->mutex);
148 if (smu->ppt_funcs->get_dpm_ultimate_freq)
149 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
154 mutex_unlock(&smu->mutex);
159 static u32 smu_get_mclk(void *handle, bool low)
161 struct smu_context *smu = handle;
165 ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
166 low ? &clk_freq : NULL,
167 !low ? &clk_freq : NULL);
170 return clk_freq * 100;
173 static u32 smu_get_sclk(void *handle, bool low)
175 struct smu_context *smu = handle;
179 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
180 low ? &clk_freq : NULL,
181 !low ? &clk_freq : NULL);
184 return clk_freq * 100;
187 static int smu_dpm_set_vcn_enable_locked(struct smu_context *smu,
190 struct smu_power_context *smu_power = &smu->smu_power;
191 struct smu_power_gate *power_gate = &smu_power->power_gate;
194 if (!smu->ppt_funcs->dpm_set_vcn_enable)
197 if (atomic_read(&power_gate->vcn_gated) ^ enable)
200 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
202 atomic_set(&power_gate->vcn_gated, !enable);
207 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
210 struct smu_power_context *smu_power = &smu->smu_power;
211 struct smu_power_gate *power_gate = &smu_power->power_gate;
214 mutex_lock(&power_gate->vcn_gate_lock);
216 ret = smu_dpm_set_vcn_enable_locked(smu, enable);
218 mutex_unlock(&power_gate->vcn_gate_lock);
223 static int smu_dpm_set_jpeg_enable_locked(struct smu_context *smu,
226 struct smu_power_context *smu_power = &smu->smu_power;
227 struct smu_power_gate *power_gate = &smu_power->power_gate;
230 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
233 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
236 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
238 atomic_set(&power_gate->jpeg_gated, !enable);
243 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
246 struct smu_power_context *smu_power = &smu->smu_power;
247 struct smu_power_gate *power_gate = &smu_power->power_gate;
250 mutex_lock(&power_gate->jpeg_gate_lock);
252 ret = smu_dpm_set_jpeg_enable_locked(smu, enable);
254 mutex_unlock(&power_gate->jpeg_gate_lock);
260 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
262 * @handle: smu_context pointer
263 * @block_type: the IP block to power gate/ungate
264 * @gate: to power gate if true, ungate otherwise
266 * This API uses no smu->mutex lock protection due to:
267 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
268 * This is guarded to be race condition free by the caller.
269 * 2. Or get called on user setting request of power_dpm_force_performance_level.
270 * Under this case, the smu->mutex lock protection is already enforced on
271 * the parent API smu_force_performance_level of the call path.
273 static int smu_dpm_set_power_gate(void *handle,
277 struct smu_context *smu = handle;
280 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
283 switch (block_type) {
285 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
286 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
288 case AMD_IP_BLOCK_TYPE_UVD:
289 case AMD_IP_BLOCK_TYPE_VCN:
290 ret = smu_dpm_set_vcn_enable(smu, !gate);
292 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
293 gate ? "gate" : "ungate");
295 case AMD_IP_BLOCK_TYPE_GFX:
296 ret = smu_gfx_off_control(smu, gate);
298 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
299 gate ? "enable" : "disable");
301 case AMD_IP_BLOCK_TYPE_SDMA:
302 ret = smu_powergate_sdma(smu, gate);
304 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
305 gate ? "gate" : "ungate");
307 case AMD_IP_BLOCK_TYPE_JPEG:
308 ret = smu_dpm_set_jpeg_enable(smu, !gate);
310 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
311 gate ? "gate" : "ungate");
314 dev_err(smu->adev->dev, "Unsupported block type!\n");
322 * smu_set_user_clk_dependencies - set user profile clock dependencies
324 * @smu: smu_context pointer
325 * @clk: enum smu_clk_type type
327 * Enable/Disable the clock dependency for the @clk type.
329 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
331 if (smu->adev->in_suspend)
334 if (clk == SMU_MCLK) {
335 smu->user_dpm_profile.clk_dependency = 0;
336 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
337 } else if (clk == SMU_FCLK) {
338 /* MCLK takes precedence over FCLK */
339 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
342 smu->user_dpm_profile.clk_dependency = 0;
343 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
344 } else if (clk == SMU_SOCCLK) {
345 /* MCLK takes precedence over SOCCLK */
346 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
349 smu->user_dpm_profile.clk_dependency = 0;
350 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
352 /* Add clk dependencies here, if any */
357 * smu_restore_dpm_user_profile - reinstate user dpm profile
359 * @smu: smu_context pointer
361 * Restore the saved user power configurations include power limit,
362 * clock frequencies, fan control mode and fan speed.
364 static void smu_restore_dpm_user_profile(struct smu_context *smu)
366 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
369 if (!smu->adev->in_suspend)
372 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
375 /* Enable restore flag */
376 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
378 /* set the user dpm power limit */
379 if (smu->user_dpm_profile.power_limit) {
380 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
382 dev_err(smu->adev->dev, "Failed to set power limit value\n");
385 /* set the user dpm clock configurations */
386 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
387 enum smu_clk_type clk_type;
389 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
391 * Iterate over smu clk type and force the saved user clk
392 * configs, skip if clock dependency is enabled
394 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
395 smu->user_dpm_profile.clk_mask[clk_type]) {
396 ret = smu_force_smuclk_levels(smu, clk_type,
397 smu->user_dpm_profile.clk_mask[clk_type]);
399 dev_err(smu->adev->dev,
400 "Failed to set clock type = %d\n", clk_type);
405 /* set the user dpm fan configurations */
406 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||
407 smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {
408 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
410 smu->user_dpm_profile.fan_speed_pwm = 0;
411 smu->user_dpm_profile.fan_speed_rpm = 0;
412 smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;
413 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
416 if (smu->user_dpm_profile.fan_speed_pwm) {
417 ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);
419 dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");
422 if (smu->user_dpm_profile.fan_speed_rpm) {
423 ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);
425 dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");
429 /* Restore user customized OD settings */
430 if (smu->user_dpm_profile.user_od) {
431 if (smu->ppt_funcs->restore_user_od_settings) {
432 ret = smu->ppt_funcs->restore_user_od_settings(smu);
434 dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");
438 /* Disable restore flag */
439 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
442 static int smu_get_power_num_states(void *handle,
443 struct pp_states_info *state_info)
448 /* not support power state */
449 memset(state_info, 0, sizeof(struct pp_states_info));
450 state_info->nums = 1;
451 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
456 bool is_support_sw_smu(struct amdgpu_device *adev)
458 /* vega20 is 11.0.2, but it's supported via the powerplay code */
459 if (adev->asic_type == CHIP_VEGA20)
462 if (adev->ip_versions[MP1_HWIP][0] >= IP_VERSION(11, 0, 0))
468 bool is_support_cclk_dpm(struct amdgpu_device *adev)
470 struct smu_context *smu = &adev->smu;
472 if (!is_support_sw_smu(adev))
475 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
482 static int smu_sys_get_pp_table(void *handle,
485 struct smu_context *smu = handle;
486 struct smu_table_context *smu_table = &smu->smu_table;
487 uint32_t powerplay_table_size;
489 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
492 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
495 mutex_lock(&smu->mutex);
497 if (smu_table->hardcode_pptable)
498 *table = smu_table->hardcode_pptable;
500 *table = smu_table->power_play_table;
502 powerplay_table_size = smu_table->power_play_table_size;
504 mutex_unlock(&smu->mutex);
506 return powerplay_table_size;
509 static int smu_sys_set_pp_table(void *handle,
513 struct smu_context *smu = handle;
514 struct smu_table_context *smu_table = &smu->smu_table;
515 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
518 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
521 if (header->usStructureSize != size) {
522 dev_err(smu->adev->dev, "pp table size not matched !\n");
526 mutex_lock(&smu->mutex);
527 if (!smu_table->hardcode_pptable)
528 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
529 if (!smu_table->hardcode_pptable) {
534 memcpy(smu_table->hardcode_pptable, buf, size);
535 smu_table->power_play_table = smu_table->hardcode_pptable;
536 smu_table->power_play_table_size = size;
539 * Special hw_fini action(for Navi1x, the DPMs disablement will be
540 * skipped) may be needed for custom pptable uploading.
542 smu->uploading_custom_pp_table = true;
544 ret = smu_reset(smu);
546 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
548 smu->uploading_custom_pp_table = false;
551 mutex_unlock(&smu->mutex);
555 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
557 struct smu_feature *feature = &smu->smu_feature;
559 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
561 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
563 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
568 bitmap_or(feature->allowed, feature->allowed,
569 (unsigned long *)allowed_feature_mask,
570 feature->feature_num);
575 static int smu_set_funcs(struct amdgpu_device *adev)
577 struct smu_context *smu = &adev->smu;
579 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
580 smu->od_enabled = true;
582 switch (adev->ip_versions[MP1_HWIP][0]) {
583 case IP_VERSION(11, 0, 0):
584 case IP_VERSION(11, 0, 5):
585 case IP_VERSION(11, 0, 9):
586 navi10_set_ppt_funcs(smu);
588 case IP_VERSION(11, 0, 7):
589 case IP_VERSION(11, 0, 11):
590 case IP_VERSION(11, 0, 12):
591 case IP_VERSION(11, 0, 13):
592 sienna_cichlid_set_ppt_funcs(smu);
594 case IP_VERSION(12, 0, 0):
595 case IP_VERSION(12, 0, 1):
596 renoir_set_ppt_funcs(smu);
598 case IP_VERSION(11, 5, 0):
599 vangogh_set_ppt_funcs(smu);
601 case IP_VERSION(13, 0, 1):
602 case IP_VERSION(13, 0, 3):
603 yellow_carp_set_ppt_funcs(smu);
605 case IP_VERSION(11, 0, 8):
606 cyan_skillfish_set_ppt_funcs(smu);
608 case IP_VERSION(11, 0, 2):
609 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
610 arcturus_set_ppt_funcs(smu);
611 /* OD is not supported on Arcturus */
612 smu->od_enabled =false;
614 case IP_VERSION(13, 0, 2):
615 aldebaran_set_ppt_funcs(smu);
616 /* Enable pp_od_clk_voltage node */
617 smu->od_enabled = true;
626 static int smu_early_init(void *handle)
628 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
629 struct smu_context *smu = &adev->smu;
632 smu->pm_enabled = !!amdgpu_dpm;
634 mutex_init(&smu->mutex);
635 mutex_init(&smu->smu_baco.mutex);
636 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
637 smu->smu_baco.platform_support = false;
638 smu->user_dpm_profile.fan_mode = -1;
640 adev->powerplay.pp_handle = smu;
641 adev->powerplay.pp_funcs = &swsmu_pm_funcs;
643 return smu_set_funcs(adev);
646 static int smu_set_default_dpm_table(struct smu_context *smu)
648 struct smu_power_context *smu_power = &smu->smu_power;
649 struct smu_power_gate *power_gate = &smu_power->power_gate;
650 int vcn_gate, jpeg_gate;
653 if (!smu->ppt_funcs->set_default_dpm_table)
656 mutex_lock(&power_gate->vcn_gate_lock);
657 mutex_lock(&power_gate->jpeg_gate_lock);
659 vcn_gate = atomic_read(&power_gate->vcn_gated);
660 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
662 ret = smu_dpm_set_vcn_enable_locked(smu, true);
666 ret = smu_dpm_set_jpeg_enable_locked(smu, true);
670 ret = smu->ppt_funcs->set_default_dpm_table(smu);
672 dev_err(smu->adev->dev,
673 "Failed to setup default dpm clock tables!\n");
675 smu_dpm_set_jpeg_enable_locked(smu, !jpeg_gate);
677 smu_dpm_set_vcn_enable_locked(smu, !vcn_gate);
679 mutex_unlock(&power_gate->jpeg_gate_lock);
680 mutex_unlock(&power_gate->vcn_gate_lock);
686 static int smu_late_init(void *handle)
688 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
689 struct smu_context *smu = &adev->smu;
692 smu_set_fine_grain_gfx_freq_parameters(smu);
694 if (!smu->pm_enabled)
697 ret = smu_post_init(smu);
699 dev_err(adev->dev, "Failed to post smu init!\n");
703 if ((adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 1)) ||
704 (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 3)))
707 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
708 ret = smu_set_default_od_settings(smu);
710 dev_err(adev->dev, "Failed to setup default OD settings!\n");
715 ret = smu_populate_umd_state_clk(smu);
717 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
721 ret = smu_get_asic_power_limits(smu,
722 &smu->current_power_limit,
723 &smu->default_power_limit,
724 &smu->max_power_limit);
726 dev_err(adev->dev, "Failed to get asic power limits!\n");
730 if (!amdgpu_sriov_vf(adev))
731 smu_get_unique_id(smu);
733 smu_get_fan_parameters(smu);
735 smu_handle_task(&adev->smu,
736 smu->smu_dpm.dpm_level,
737 AMD_PP_TASK_COMPLETE_INIT,
740 smu_restore_dpm_user_profile(smu);
745 static int smu_init_fb_allocations(struct smu_context *smu)
747 struct amdgpu_device *adev = smu->adev;
748 struct smu_table_context *smu_table = &smu->smu_table;
749 struct smu_table *tables = smu_table->tables;
750 struct smu_table *driver_table = &(smu_table->driver_table);
751 uint32_t max_table_size = 0;
754 /* VRAM allocation for tool table */
755 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
756 ret = amdgpu_bo_create_kernel(adev,
757 tables[SMU_TABLE_PMSTATUSLOG].size,
758 tables[SMU_TABLE_PMSTATUSLOG].align,
759 tables[SMU_TABLE_PMSTATUSLOG].domain,
760 &tables[SMU_TABLE_PMSTATUSLOG].bo,
761 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
762 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
764 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
769 /* VRAM allocation for driver table */
770 for (i = 0; i < SMU_TABLE_COUNT; i++) {
771 if (tables[i].size == 0)
774 if (i == SMU_TABLE_PMSTATUSLOG)
777 if (max_table_size < tables[i].size)
778 max_table_size = tables[i].size;
781 driver_table->size = max_table_size;
782 driver_table->align = PAGE_SIZE;
783 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
785 ret = amdgpu_bo_create_kernel(adev,
788 driver_table->domain,
790 &driver_table->mc_address,
791 &driver_table->cpu_addr);
793 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
794 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
795 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
796 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
797 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
803 static int smu_fini_fb_allocations(struct smu_context *smu)
805 struct smu_table_context *smu_table = &smu->smu_table;
806 struct smu_table *tables = smu_table->tables;
807 struct smu_table *driver_table = &(smu_table->driver_table);
809 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
810 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
811 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
812 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
814 amdgpu_bo_free_kernel(&driver_table->bo,
815 &driver_table->mc_address,
816 &driver_table->cpu_addr);
822 * smu_alloc_memory_pool - allocate memory pool in the system memory
824 * @smu: amdgpu_device pointer
826 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
827 * and DramLogSetDramAddr can notify it changed.
829 * Returns 0 on success, error on failure.
831 static int smu_alloc_memory_pool(struct smu_context *smu)
833 struct amdgpu_device *adev = smu->adev;
834 struct smu_table_context *smu_table = &smu->smu_table;
835 struct smu_table *memory_pool = &smu_table->memory_pool;
836 uint64_t pool_size = smu->pool_size;
839 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
842 memory_pool->size = pool_size;
843 memory_pool->align = PAGE_SIZE;
844 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
847 case SMU_MEMORY_POOL_SIZE_256_MB:
848 case SMU_MEMORY_POOL_SIZE_512_MB:
849 case SMU_MEMORY_POOL_SIZE_1_GB:
850 case SMU_MEMORY_POOL_SIZE_2_GB:
851 ret = amdgpu_bo_create_kernel(adev,
856 &memory_pool->mc_address,
857 &memory_pool->cpu_addr);
859 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
868 static int smu_free_memory_pool(struct smu_context *smu)
870 struct smu_table_context *smu_table = &smu->smu_table;
871 struct smu_table *memory_pool = &smu_table->memory_pool;
873 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
876 amdgpu_bo_free_kernel(&memory_pool->bo,
877 &memory_pool->mc_address,
878 &memory_pool->cpu_addr);
880 memset(memory_pool, 0, sizeof(struct smu_table));
885 static int smu_alloc_dummy_read_table(struct smu_context *smu)
887 struct smu_table_context *smu_table = &smu->smu_table;
888 struct smu_table *dummy_read_1_table =
889 &smu_table->dummy_read_1_table;
890 struct amdgpu_device *adev = smu->adev;
893 dummy_read_1_table->size = 0x40000;
894 dummy_read_1_table->align = PAGE_SIZE;
895 dummy_read_1_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
897 ret = amdgpu_bo_create_kernel(adev,
898 dummy_read_1_table->size,
899 dummy_read_1_table->align,
900 dummy_read_1_table->domain,
901 &dummy_read_1_table->bo,
902 &dummy_read_1_table->mc_address,
903 &dummy_read_1_table->cpu_addr);
905 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
910 static void smu_free_dummy_read_table(struct smu_context *smu)
912 struct smu_table_context *smu_table = &smu->smu_table;
913 struct smu_table *dummy_read_1_table =
914 &smu_table->dummy_read_1_table;
917 amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
918 &dummy_read_1_table->mc_address,
919 &dummy_read_1_table->cpu_addr);
921 memset(dummy_read_1_table, 0, sizeof(struct smu_table));
924 static int smu_smc_table_sw_init(struct smu_context *smu)
929 * Create smu_table structure, and init smc tables such as
930 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
932 ret = smu_init_smc_tables(smu);
934 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
939 * Create smu_power_context structure, and allocate smu_dpm_context and
940 * context size to fill the smu_power_context data.
942 ret = smu_init_power(smu);
944 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
949 * allocate vram bos to store smc table contents.
951 ret = smu_init_fb_allocations(smu);
955 ret = smu_alloc_memory_pool(smu);
959 ret = smu_alloc_dummy_read_table(smu);
963 ret = smu_i2c_init(smu, &smu->adev->pm.smu_i2c);
970 static int smu_smc_table_sw_fini(struct smu_context *smu)
974 smu_i2c_fini(smu, &smu->adev->pm.smu_i2c);
976 smu_free_dummy_read_table(smu);
978 ret = smu_free_memory_pool(smu);
982 ret = smu_fini_fb_allocations(smu);
986 ret = smu_fini_power(smu);
988 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
992 ret = smu_fini_smc_tables(smu);
994 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
1001 static void smu_throttling_logging_work_fn(struct work_struct *work)
1003 struct smu_context *smu = container_of(work, struct smu_context,
1004 throttling_logging_work);
1006 smu_log_thermal_throttling(smu);
1009 static void smu_interrupt_work_fn(struct work_struct *work)
1011 struct smu_context *smu = container_of(work, struct smu_context,
1014 mutex_lock(&smu->mutex);
1016 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
1017 smu->ppt_funcs->interrupt_work(smu);
1019 mutex_unlock(&smu->mutex);
1022 static int smu_sw_init(void *handle)
1024 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1025 struct smu_context *smu = &adev->smu;
1028 smu->pool_size = adev->pm.smu_prv_buffer_size;
1029 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
1030 mutex_init(&smu->smu_feature.mutex);
1031 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
1032 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
1033 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1035 mutex_init(&smu->sensor_lock);
1036 mutex_init(&smu->metrics_lock);
1037 mutex_init(&smu->message_lock);
1039 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
1040 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
1041 atomic64_set(&smu->throttle_int_counter, 0);
1042 smu->watermarks_bitmap = 0;
1043 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1044 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1046 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
1047 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
1048 mutex_init(&smu->smu_power.power_gate.vcn_gate_lock);
1049 mutex_init(&smu->smu_power.power_gate.jpeg_gate_lock);
1051 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
1052 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
1053 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
1054 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
1055 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
1056 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
1057 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
1058 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
1060 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1061 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1062 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
1063 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
1064 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
1065 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
1066 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
1067 smu->display_config = &adev->pm.pm_display_cfg;
1069 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1070 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1072 ret = smu_init_microcode(smu);
1074 dev_err(adev->dev, "Failed to load smu firmware!\n");
1078 ret = smu_smc_table_sw_init(smu);
1080 dev_err(adev->dev, "Failed to sw init smc table!\n");
1084 ret = smu_register_irq_handler(smu);
1086 dev_err(adev->dev, "Failed to register smc irq handler!\n");
1090 /* If there is no way to query fan control mode, fan control is not supported */
1091 if (!smu->ppt_funcs->get_fan_control_mode)
1092 smu->adev->pm.no_fan = true;
1097 static int smu_sw_fini(void *handle)
1099 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1100 struct smu_context *smu = &adev->smu;
1103 ret = smu_smc_table_sw_fini(smu);
1105 dev_err(adev->dev, "Failed to sw fini smc table!\n");
1109 smu_fini_microcode(smu);
1114 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1116 struct amdgpu_device *adev = smu->adev;
1117 struct smu_temperature_range *range =
1118 &smu->thermal_range;
1121 if (!smu->ppt_funcs->get_thermal_temperature_range)
1124 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1128 adev->pm.dpm.thermal.min_temp = range->min;
1129 adev->pm.dpm.thermal.max_temp = range->max;
1130 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1131 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1132 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1133 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1134 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1135 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1136 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1141 static int smu_smc_hw_setup(struct smu_context *smu)
1143 struct amdgpu_device *adev = smu->adev;
1144 uint32_t pcie_gen = 0, pcie_width = 0;
1147 if (adev->in_suspend && smu_is_dpm_running(smu)) {
1148 dev_info(adev->dev, "dpm has been enabled\n");
1149 /* this is needed specifically */
1150 switch (adev->ip_versions[MP1_HWIP][0]) {
1151 case IP_VERSION(11, 0, 7):
1152 case IP_VERSION(11, 0, 11):
1153 case IP_VERSION(11, 5, 0):
1154 case IP_VERSION(11, 0, 12):
1155 ret = smu_system_features_control(smu, true);
1163 ret = smu_init_display_count(smu, 0);
1165 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1169 ret = smu_set_driver_table_location(smu);
1171 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1176 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1178 ret = smu_set_tool_table_location(smu);
1180 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1185 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1188 ret = smu_notify_memory_pool_location(smu);
1190 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1194 /* smu_dump_pptable(smu); */
1196 * Copy pptable bo in the vram to smc with SMU MSGs such as
1197 * SetDriverDramAddr and TransferTableDram2Smu.
1199 ret = smu_write_pptable(smu);
1201 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1205 /* issue Run*Btc msg */
1206 ret = smu_run_btc(smu);
1210 ret = smu_feature_set_allowed_mask(smu);
1212 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1216 ret = smu_system_features_control(smu, true);
1218 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1222 if (!smu_is_dpm_running(smu))
1223 dev_info(adev->dev, "dpm has been disabled\n");
1225 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1227 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1229 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1231 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1234 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1235 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1236 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
1238 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1240 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1242 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1244 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1246 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1248 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1250 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1252 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1256 ret = smu_get_thermal_temperature_range(smu);
1258 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1262 ret = smu_enable_thermal_alert(smu);
1264 dev_err(adev->dev, "Failed to enable thermal alert!\n");
1269 * Set initialized values (get from vbios) to dpm tables context such as
1270 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1273 ret = smu_set_default_dpm_table(smu);
1275 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1279 ret = smu_notify_display_change(smu);
1284 * Set min deep sleep dce fclk with bootup value from vbios via
1285 * SetMinDeepSleepDcefclk MSG.
1287 ret = smu_set_min_dcef_deep_sleep(smu,
1288 smu->smu_table.boot_values.dcefclk / 100);
1295 static int smu_start_smc_engine(struct smu_context *smu)
1297 struct amdgpu_device *adev = smu->adev;
1300 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1301 if (adev->ip_versions[MP1_HWIP][0] < IP_VERSION(11, 0, 0)) {
1302 if (smu->ppt_funcs->load_microcode) {
1303 ret = smu->ppt_funcs->load_microcode(smu);
1310 if (smu->ppt_funcs->check_fw_status) {
1311 ret = smu->ppt_funcs->check_fw_status(smu);
1313 dev_err(adev->dev, "SMC is not ready\n");
1319 * Send msg GetDriverIfVersion to check if the return value is equal
1320 * with DRIVER_IF_VERSION of smc header.
1322 ret = smu_check_fw_version(smu);
1329 static int smu_hw_init(void *handle)
1332 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1333 struct smu_context *smu = &adev->smu;
1335 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1336 smu->pm_enabled = false;
1340 ret = smu_start_smc_engine(smu);
1342 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1347 smu_powergate_sdma(&adev->smu, false);
1348 smu_dpm_set_vcn_enable(smu, true);
1349 smu_dpm_set_jpeg_enable(smu, true);
1350 smu_set_gfx_cgpg(&adev->smu, true);
1353 if (!smu->pm_enabled)
1356 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1357 ret = smu_get_vbios_bootup_values(smu);
1359 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1363 ret = smu_setup_pptable(smu);
1365 dev_err(adev->dev, "Failed to setup pptable!\n");
1369 ret = smu_get_driver_allowed_feature_mask(smu);
1373 ret = smu_smc_hw_setup(smu);
1375 dev_err(adev->dev, "Failed to setup smc hw!\n");
1380 * Move maximum sustainable clock retrieving here considering
1381 * 1. It is not needed on resume(from S3).
1382 * 2. DAL settings come between .hw_init and .late_init of SMU.
1383 * And DAL needs to know the maximum sustainable clocks. Thus
1384 * it cannot be put in .late_init().
1386 ret = smu_init_max_sustainable_clocks(smu);
1388 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1392 adev->pm.dpm_enabled = true;
1394 dev_info(adev->dev, "SMU is initialized successfully!\n");
1399 static int smu_disable_dpms(struct smu_context *smu)
1401 struct amdgpu_device *adev = smu->adev;
1403 bool use_baco = !smu->is_apu &&
1404 ((amdgpu_in_reset(adev) &&
1405 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1406 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1409 * For custom pptable uploading, skip the DPM features
1410 * disable process on Navi1x ASICs.
1411 * - As the gfx related features are under control of
1412 * RLC on those ASICs. RLC reinitialization will be
1413 * needed to reenable them. That will cost much more
1416 * - SMU firmware can handle the DPM reenablement
1419 if (smu->uploading_custom_pp_table) {
1420 switch (adev->ip_versions[MP1_HWIP][0]) {
1421 case IP_VERSION(11, 0, 0):
1422 case IP_VERSION(11, 0, 5):
1423 case IP_VERSION(11, 0, 9):
1424 case IP_VERSION(11, 0, 7):
1425 case IP_VERSION(11, 0, 11):
1426 case IP_VERSION(11, 5, 0):
1427 case IP_VERSION(11, 0, 12):
1428 case IP_VERSION(11, 0, 13):
1429 return smu_disable_all_features_with_exception(smu,
1438 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1439 * on BACO in. Driver involvement is unnecessary.
1442 switch (adev->ip_versions[MP1_HWIP][0]) {
1443 case IP_VERSION(11, 0, 7):
1444 case IP_VERSION(11, 0, 0):
1445 case IP_VERSION(11, 0, 5):
1446 case IP_VERSION(11, 0, 9):
1447 return smu_disable_all_features_with_exception(smu,
1449 SMU_FEATURE_BACO_BIT);
1456 * For gpu reset, runpm and hibernation through BACO,
1457 * BACO feature has to be kept enabled.
1459 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1460 ret = smu_disable_all_features_with_exception(smu,
1462 SMU_FEATURE_BACO_BIT);
1464 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1466 ret = smu_system_features_control(smu, false);
1468 dev_err(adev->dev, "Failed to disable smu features.\n");
1471 if (adev->ip_versions[MP1_HWIP][0] >= IP_VERSION(11, 0, 0) &&
1472 adev->gfx.rlc.funcs->stop)
1473 adev->gfx.rlc.funcs->stop(adev);
1478 static int smu_smc_hw_cleanup(struct smu_context *smu)
1480 struct amdgpu_device *adev = smu->adev;
1483 cancel_work_sync(&smu->throttling_logging_work);
1484 cancel_work_sync(&smu->interrupt_work);
1486 ret = smu_disable_thermal_alert(smu);
1488 dev_err(adev->dev, "Fail to disable thermal alert!\n");
1492 ret = smu_disable_dpms(smu);
1494 dev_err(adev->dev, "Fail to disable dpm features!\n");
1501 static int smu_hw_fini(void *handle)
1503 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1504 struct smu_context *smu = &adev->smu;
1506 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1510 smu_powergate_sdma(&adev->smu, true);
1513 smu_dpm_set_vcn_enable(smu, false);
1514 smu_dpm_set_jpeg_enable(smu, false);
1516 adev->vcn.cur_state = AMD_PG_STATE_GATE;
1517 adev->jpeg.cur_state = AMD_PG_STATE_GATE;
1519 if (!smu->pm_enabled)
1522 adev->pm.dpm_enabled = false;
1524 return smu_smc_hw_cleanup(smu);
1527 static int smu_reset(struct smu_context *smu)
1529 struct amdgpu_device *adev = smu->adev;
1532 amdgpu_gfx_off_ctrl(smu->adev, false);
1534 ret = smu_hw_fini(adev);
1538 ret = smu_hw_init(adev);
1542 ret = smu_late_init(adev);
1546 amdgpu_gfx_off_ctrl(smu->adev, true);
1551 static int smu_suspend(void *handle)
1553 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1554 struct smu_context *smu = &adev->smu;
1557 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1560 if (!smu->pm_enabled)
1563 adev->pm.dpm_enabled = false;
1565 ret = smu_smc_hw_cleanup(smu);
1569 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1571 /* skip CGPG when in S0ix */
1572 if (smu->is_apu && !adev->in_s0ix)
1573 smu_set_gfx_cgpg(&adev->smu, false);
1578 static int smu_resume(void *handle)
1581 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1582 struct smu_context *smu = &adev->smu;
1584 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1587 if (!smu->pm_enabled)
1590 dev_info(adev->dev, "SMU is resuming...\n");
1592 ret = smu_start_smc_engine(smu);
1594 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1598 ret = smu_smc_hw_setup(smu);
1600 dev_err(adev->dev, "Failed to setup smc hw!\n");
1605 smu_set_gfx_cgpg(&adev->smu, true);
1607 smu->disable_uclk_switch = 0;
1609 adev->pm.dpm_enabled = true;
1611 dev_info(adev->dev, "SMU is resumed successfully!\n");
1616 static int smu_display_configuration_change(void *handle,
1617 const struct amd_pp_display_configuration *display_config)
1619 struct smu_context *smu = handle;
1621 int num_of_active_display = 0;
1623 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1626 if (!display_config)
1629 mutex_lock(&smu->mutex);
1631 smu_set_min_dcef_deep_sleep(smu,
1632 display_config->min_dcef_deep_sleep_set_clk / 100);
1634 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1635 if (display_config->displays[index].controller_id != 0)
1636 num_of_active_display++;
1639 mutex_unlock(&smu->mutex);
1644 static int smu_set_clockgating_state(void *handle,
1645 enum amd_clockgating_state state)
1650 static int smu_set_powergating_state(void *handle,
1651 enum amd_powergating_state state)
1656 static int smu_enable_umd_pstate(void *handle,
1657 enum amd_dpm_forced_level *level)
1659 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1660 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1661 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1662 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1664 struct smu_context *smu = (struct smu_context*)(handle);
1665 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1667 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1670 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1671 /* enter umd pstate, save current level, disable gfx cg*/
1672 if (*level & profile_mode_mask) {
1673 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1674 smu_dpm_ctx->enable_umd_pstate = true;
1675 smu_gpo_control(smu, false);
1676 amdgpu_device_ip_set_powergating_state(smu->adev,
1677 AMD_IP_BLOCK_TYPE_GFX,
1678 AMD_PG_STATE_UNGATE);
1679 amdgpu_device_ip_set_clockgating_state(smu->adev,
1680 AMD_IP_BLOCK_TYPE_GFX,
1681 AMD_CG_STATE_UNGATE);
1682 smu_gfx_ulv_control(smu, false);
1683 smu_deep_sleep_control(smu, false);
1684 amdgpu_asic_update_umd_stable_pstate(smu->adev, true);
1687 /* exit umd pstate, restore level, enable gfx cg*/
1688 if (!(*level & profile_mode_mask)) {
1689 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1690 *level = smu_dpm_ctx->saved_dpm_level;
1691 smu_dpm_ctx->enable_umd_pstate = false;
1692 amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
1693 smu_deep_sleep_control(smu, true);
1694 smu_gfx_ulv_control(smu, true);
1695 amdgpu_device_ip_set_clockgating_state(smu->adev,
1696 AMD_IP_BLOCK_TYPE_GFX,
1698 amdgpu_device_ip_set_powergating_state(smu->adev,
1699 AMD_IP_BLOCK_TYPE_GFX,
1701 smu_gpo_control(smu, true);
1708 static int smu_bump_power_profile_mode(struct smu_context *smu,
1710 uint32_t param_size)
1714 if (smu->ppt_funcs->set_power_profile_mode)
1715 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
1720 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
1721 enum amd_dpm_forced_level level,
1722 bool skip_display_settings)
1727 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1729 if (!skip_display_settings) {
1730 ret = smu_display_config_changed(smu);
1732 dev_err(smu->adev->dev, "Failed to change display config!");
1737 ret = smu_apply_clocks_adjust_rules(smu);
1739 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
1743 if (!skip_display_settings) {
1744 ret = smu_notify_smc_display_config(smu);
1746 dev_err(smu->adev->dev, "Failed to notify smc display config!");
1751 if (smu_dpm_ctx->dpm_level != level) {
1752 ret = smu_asic_set_performance_level(smu, level);
1754 dev_err(smu->adev->dev, "Failed to set performance level!");
1758 /* update the saved copy */
1759 smu_dpm_ctx->dpm_level = level;
1762 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1763 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
1764 index = fls(smu->workload_mask);
1765 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1766 workload = smu->workload_setting[index];
1768 if (smu->power_profile_mode != workload)
1769 smu_bump_power_profile_mode(smu, &workload, 0);
1775 static int smu_handle_task(struct smu_context *smu,
1776 enum amd_dpm_forced_level level,
1777 enum amd_pp_task task_id,
1782 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1786 mutex_lock(&smu->mutex);
1789 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1790 ret = smu_pre_display_config_changed(smu);
1793 ret = smu_adjust_power_state_dynamic(smu, level, false);
1795 case AMD_PP_TASK_COMPLETE_INIT:
1796 case AMD_PP_TASK_READJUST_POWER_STATE:
1797 ret = smu_adjust_power_state_dynamic(smu, level, true);
1805 mutex_unlock(&smu->mutex);
1810 static int smu_handle_dpm_task(void *handle,
1811 enum amd_pp_task task_id,
1812 enum amd_pm_state_type *user_state)
1814 struct smu_context *smu = handle;
1815 struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
1817 return smu_handle_task(smu, smu_dpm->dpm_level, task_id, true);
1821 static int smu_switch_power_profile(void *handle,
1822 enum PP_SMC_POWER_PROFILE type,
1825 struct smu_context *smu = handle;
1826 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1830 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1833 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1836 mutex_lock(&smu->mutex);
1839 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1840 index = fls(smu->workload_mask);
1841 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1842 workload = smu->workload_setting[index];
1844 smu->workload_mask |= (1 << smu->workload_prority[type]);
1845 index = fls(smu->workload_mask);
1846 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1847 workload = smu->workload_setting[index];
1850 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1851 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
1852 smu_bump_power_profile_mode(smu, &workload, 0);
1854 mutex_unlock(&smu->mutex);
1859 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
1861 struct smu_context *smu = handle;
1862 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1863 enum amd_dpm_forced_level level;
1865 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1868 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1871 mutex_lock(&(smu->mutex));
1872 level = smu_dpm_ctx->dpm_level;
1873 mutex_unlock(&(smu->mutex));
1878 static int smu_force_performance_level(void *handle,
1879 enum amd_dpm_forced_level level)
1881 struct smu_context *smu = handle;
1882 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1885 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1888 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1891 mutex_lock(&smu->mutex);
1893 ret = smu_enable_umd_pstate(smu, &level);
1895 mutex_unlock(&smu->mutex);
1899 ret = smu_handle_task(smu, level,
1900 AMD_PP_TASK_READJUST_POWER_STATE,
1903 mutex_unlock(&smu->mutex);
1905 /* reset user dpm clock state */
1906 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1907 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
1908 smu->user_dpm_profile.clk_dependency = 0;
1914 static int smu_set_display_count(void *handle, uint32_t count)
1916 struct smu_context *smu = handle;
1919 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1922 mutex_lock(&smu->mutex);
1923 ret = smu_init_display_count(smu, count);
1924 mutex_unlock(&smu->mutex);
1929 static int smu_force_smuclk_levels(struct smu_context *smu,
1930 enum smu_clk_type clk_type,
1933 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1936 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1939 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1940 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
1944 mutex_lock(&smu->mutex);
1946 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
1947 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1948 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
1949 smu->user_dpm_profile.clk_mask[clk_type] = mask;
1950 smu_set_user_clk_dependencies(smu, clk_type);
1954 mutex_unlock(&smu->mutex);
1959 static int smu_force_ppclk_levels(void *handle,
1960 enum pp_clock_type type,
1963 struct smu_context *smu = handle;
1964 enum smu_clk_type clk_type;
1968 clk_type = SMU_SCLK; break;
1970 clk_type = SMU_MCLK; break;
1972 clk_type = SMU_PCIE; break;
1974 clk_type = SMU_SOCCLK; break;
1976 clk_type = SMU_FCLK; break;
1978 clk_type = SMU_DCEFCLK; break;
1980 clk_type = SMU_VCLK; break;
1982 clk_type = SMU_DCLK; break;
1984 clk_type = SMU_OD_SCLK; break;
1986 clk_type = SMU_OD_MCLK; break;
1988 clk_type = SMU_OD_VDDC_CURVE; break;
1990 clk_type = SMU_OD_RANGE; break;
1995 return smu_force_smuclk_levels(smu, clk_type, mask);
1999 * On system suspending or resetting, the dpm_enabled
2000 * flag will be cleared. So that those SMU services which
2001 * are not supported will be gated.
2002 * However, the mp1 state setting should still be granted
2003 * even if the dpm_enabled cleared.
2005 static int smu_set_mp1_state(void *handle,
2006 enum pp_mp1_state mp1_state)
2008 struct smu_context *smu = handle;
2011 if (!smu->pm_enabled)
2014 mutex_lock(&smu->mutex);
2016 if (smu->ppt_funcs &&
2017 smu->ppt_funcs->set_mp1_state)
2018 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
2020 mutex_unlock(&smu->mutex);
2025 static int smu_set_df_cstate(void *handle,
2026 enum pp_df_cstate state)
2028 struct smu_context *smu = handle;
2031 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2034 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
2037 mutex_lock(&smu->mutex);
2039 ret = smu->ppt_funcs->set_df_cstate(smu, state);
2041 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
2043 mutex_unlock(&smu->mutex);
2048 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en)
2052 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2055 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down)
2058 mutex_lock(&smu->mutex);
2060 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en);
2062 dev_err(smu->adev->dev, "[AllowXgmiPowerDown] failed!\n");
2064 mutex_unlock(&smu->mutex);
2069 int smu_write_watermarks_table(struct smu_context *smu)
2073 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2076 mutex_lock(&smu->mutex);
2078 ret = smu_set_watermarks_table(smu, NULL);
2080 mutex_unlock(&smu->mutex);
2085 static int smu_set_watermarks_for_clock_ranges(void *handle,
2086 struct pp_smu_wm_range_sets *clock_ranges)
2088 struct smu_context *smu = handle;
2091 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2094 if (smu->disable_watermark)
2097 mutex_lock(&smu->mutex);
2099 ret = smu_set_watermarks_table(smu, clock_ranges);
2101 mutex_unlock(&smu->mutex);
2106 int smu_set_ac_dc(struct smu_context *smu)
2110 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2113 /* controlled by firmware */
2114 if (smu->dc_controlled_by_gpio)
2117 mutex_lock(&smu->mutex);
2118 ret = smu_set_power_source(smu,
2119 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
2120 SMU_POWER_SOURCE_DC);
2122 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
2123 smu->adev->pm.ac_power ? "AC" : "DC");
2124 mutex_unlock(&smu->mutex);
2129 const struct amd_ip_funcs smu_ip_funcs = {
2131 .early_init = smu_early_init,
2132 .late_init = smu_late_init,
2133 .sw_init = smu_sw_init,
2134 .sw_fini = smu_sw_fini,
2135 .hw_init = smu_hw_init,
2136 .hw_fini = smu_hw_fini,
2137 .suspend = smu_suspend,
2138 .resume = smu_resume,
2140 .check_soft_reset = NULL,
2141 .wait_for_idle = NULL,
2143 .set_clockgating_state = smu_set_clockgating_state,
2144 .set_powergating_state = smu_set_powergating_state,
2145 .enable_umd_pstate = smu_enable_umd_pstate,
2148 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
2150 .type = AMD_IP_BLOCK_TYPE_SMC,
2154 .funcs = &smu_ip_funcs,
2157 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
2159 .type = AMD_IP_BLOCK_TYPE_SMC,
2163 .funcs = &smu_ip_funcs,
2166 const struct amdgpu_ip_block_version smu_v13_0_ip_block =
2168 .type = AMD_IP_BLOCK_TYPE_SMC,
2172 .funcs = &smu_ip_funcs,
2175 static int smu_load_microcode(void *handle)
2177 struct smu_context *smu = handle;
2178 struct amdgpu_device *adev = smu->adev;
2181 if (!smu->pm_enabled)
2184 /* This should be used for non PSP loading */
2185 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2188 if (smu->ppt_funcs->load_microcode) {
2189 ret = smu->ppt_funcs->load_microcode(smu);
2191 dev_err(adev->dev, "Load microcode failed\n");
2196 if (smu->ppt_funcs->check_fw_status) {
2197 ret = smu->ppt_funcs->check_fw_status(smu);
2199 dev_err(adev->dev, "SMC is not ready\n");
2207 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2211 mutex_lock(&smu->mutex);
2213 if (smu->ppt_funcs->set_gfx_cgpg)
2214 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2216 mutex_unlock(&smu->mutex);
2221 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2223 struct smu_context *smu = handle;
2226 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2229 mutex_lock(&smu->mutex);
2231 if (smu->ppt_funcs->set_fan_speed_rpm) {
2232 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2233 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2234 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;
2235 smu->user_dpm_profile.fan_speed_rpm = speed;
2237 /* Override custom PWM setting as they cannot co-exist */
2238 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;
2239 smu->user_dpm_profile.fan_speed_pwm = 0;
2243 mutex_unlock(&smu->mutex);
2249 * smu_get_power_limit - Request one of the SMU Power Limits
2251 * @handle: pointer to smu context
2252 * @limit: requested limit is written back to this variable
2253 * @pp_limit_level: &pp_power_limit_level which limit of the power to return
2254 * @pp_power_type: &pp_power_type type of power
2255 * Return: 0 on success, <0 on error
2258 int smu_get_power_limit(void *handle,
2260 enum pp_power_limit_level pp_limit_level,
2261 enum pp_power_type pp_power_type)
2263 struct smu_context *smu = handle;
2264 struct amdgpu_device *adev = smu->adev;
2265 enum smu_ppt_limit_level limit_level;
2266 uint32_t limit_type;
2269 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2272 switch(pp_power_type) {
2273 case PP_PWR_TYPE_SUSTAINED:
2274 limit_type = SMU_DEFAULT_PPT_LIMIT;
2276 case PP_PWR_TYPE_FAST:
2277 limit_type = SMU_FAST_PPT_LIMIT;
2284 switch(pp_limit_level){
2285 case PP_PWR_LIMIT_CURRENT:
2286 limit_level = SMU_PPT_LIMIT_CURRENT;
2288 case PP_PWR_LIMIT_DEFAULT:
2289 limit_level = SMU_PPT_LIMIT_DEFAULT;
2291 case PP_PWR_LIMIT_MAX:
2292 limit_level = SMU_PPT_LIMIT_MAX;
2294 case PP_PWR_LIMIT_MIN:
2300 mutex_lock(&smu->mutex);
2302 if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2303 if (smu->ppt_funcs->get_ppt_limit)
2304 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2306 switch (limit_level) {
2307 case SMU_PPT_LIMIT_CURRENT:
2308 switch (adev->ip_versions[MP1_HWIP][0]) {
2309 case IP_VERSION(13, 0, 2):
2310 case IP_VERSION(11, 0, 7):
2311 case IP_VERSION(11, 0, 11):
2312 case IP_VERSION(11, 0, 12):
2313 case IP_VERSION(11, 0, 13):
2314 ret = smu_get_asic_power_limits(smu,
2315 &smu->current_power_limit,
2322 *limit = smu->current_power_limit;
2324 case SMU_PPT_LIMIT_DEFAULT:
2325 *limit = smu->default_power_limit;
2327 case SMU_PPT_LIMIT_MAX:
2328 *limit = smu->max_power_limit;
2335 mutex_unlock(&smu->mutex);
2340 static int smu_set_power_limit(void *handle, uint32_t limit)
2342 struct smu_context *smu = handle;
2343 uint32_t limit_type = limit >> 24;
2346 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2349 mutex_lock(&smu->mutex);
2352 if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2353 if (smu->ppt_funcs->set_power_limit) {
2354 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2358 if (limit > smu->max_power_limit) {
2359 dev_err(smu->adev->dev,
2360 "New power limit (%d) is over the max allowed %d\n",
2361 limit, smu->max_power_limit);
2367 limit = smu->current_power_limit;
2369 if (smu->ppt_funcs->set_power_limit) {
2370 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2371 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2372 smu->user_dpm_profile.power_limit = limit;
2376 mutex_unlock(&smu->mutex);
2381 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2385 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2388 mutex_lock(&smu->mutex);
2390 if (smu->ppt_funcs->print_clk_levels)
2391 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2393 mutex_unlock(&smu->mutex);
2398 static int smu_print_ppclk_levels(void *handle,
2399 enum pp_clock_type type,
2402 struct smu_context *smu = handle;
2403 enum smu_clk_type clk_type;
2407 clk_type = SMU_SCLK; break;
2409 clk_type = SMU_MCLK; break;
2411 clk_type = SMU_PCIE; break;
2413 clk_type = SMU_SOCCLK; break;
2415 clk_type = SMU_FCLK; break;
2417 clk_type = SMU_DCEFCLK; break;
2419 clk_type = SMU_VCLK; break;
2421 clk_type = SMU_DCLK; break;
2423 clk_type = SMU_OD_SCLK; break;
2425 clk_type = SMU_OD_MCLK; break;
2427 clk_type = SMU_OD_VDDC_CURVE; break;
2429 clk_type = SMU_OD_RANGE; break;
2430 case OD_VDDGFX_OFFSET:
2431 clk_type = SMU_OD_VDDGFX_OFFSET; break;
2433 clk_type = SMU_OD_CCLK; break;
2438 return smu_print_smuclk_levels(smu, clk_type, buf);
2441 static int smu_od_edit_dpm_table(void *handle,
2442 enum PP_OD_DPM_TABLE_COMMAND type,
2443 long *input, uint32_t size)
2445 struct smu_context *smu = handle;
2448 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2451 mutex_lock(&smu->mutex);
2453 if (smu->ppt_funcs->od_edit_dpm_table) {
2454 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2457 mutex_unlock(&smu->mutex);
2462 static int smu_read_sensor(void *handle,
2467 struct smu_context *smu = handle;
2468 struct smu_umd_pstate_table *pstate_table =
2471 uint32_t *size, size_val;
2473 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2476 if (!data || !size_arg)
2479 size_val = *size_arg;
2482 mutex_lock(&smu->mutex);
2484 if (smu->ppt_funcs->read_sensor)
2485 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2489 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2490 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2493 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2494 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2497 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2498 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
2501 case AMDGPU_PP_SENSOR_UVD_POWER:
2502 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
2505 case AMDGPU_PP_SENSOR_VCE_POWER:
2506 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
2509 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
2510 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0: 1;
2513 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
2514 *(uint32_t *)data = 0;
2524 mutex_unlock(&smu->mutex);
2526 // assign uint32_t to int
2527 *size_arg = size_val;
2532 static int smu_get_power_profile_mode(void *handle, char *buf)
2534 struct smu_context *smu = handle;
2537 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2540 mutex_lock(&smu->mutex);
2542 if (smu->ppt_funcs->get_power_profile_mode)
2543 ret = smu->ppt_funcs->get_power_profile_mode(smu, buf);
2545 mutex_unlock(&smu->mutex);
2550 static int smu_set_power_profile_mode(void *handle,
2552 uint32_t param_size)
2554 struct smu_context *smu = handle;
2557 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2560 mutex_lock(&smu->mutex);
2562 smu_bump_power_profile_mode(smu, param, param_size);
2564 mutex_unlock(&smu->mutex);
2570 static u32 smu_get_fan_control_mode(void *handle)
2572 struct smu_context *smu = handle;
2575 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2576 return AMD_FAN_CTRL_NONE;
2578 mutex_lock(&smu->mutex);
2580 if (smu->ppt_funcs->get_fan_control_mode)
2581 ret = smu->ppt_funcs->get_fan_control_mode(smu);
2583 mutex_unlock(&smu->mutex);
2588 static int smu_set_fan_control_mode(struct smu_context *smu, int value)
2592 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2595 mutex_lock(&smu->mutex);
2597 if (smu->ppt_funcs->set_fan_control_mode) {
2598 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2599 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2600 smu->user_dpm_profile.fan_mode = value;
2603 mutex_unlock(&smu->mutex);
2605 /* reset user dpm fan speed */
2606 if (!ret && value != AMD_FAN_CTRL_MANUAL &&
2607 !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2608 smu->user_dpm_profile.fan_speed_pwm = 0;
2609 smu->user_dpm_profile.fan_speed_rpm = 0;
2610 smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);
2616 static void smu_pp_set_fan_control_mode(void *handle, u32 value)
2618 struct smu_context *smu = handle;
2620 smu_set_fan_control_mode(smu, value);
2624 static int smu_get_fan_speed_pwm(void *handle, u32 *speed)
2626 struct smu_context *smu = handle;
2629 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2632 mutex_lock(&smu->mutex);
2634 if (smu->ppt_funcs->get_fan_speed_pwm)
2635 ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);
2637 mutex_unlock(&smu->mutex);
2642 static int smu_set_fan_speed_pwm(void *handle, u32 speed)
2644 struct smu_context *smu = handle;
2647 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2650 mutex_lock(&smu->mutex);
2652 if (smu->ppt_funcs->set_fan_speed_pwm) {
2653 ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);
2654 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2655 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;
2656 smu->user_dpm_profile.fan_speed_pwm = speed;
2658 /* Override custom RPM setting as they cannot co-exist */
2659 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;
2660 smu->user_dpm_profile.fan_speed_rpm = 0;
2664 mutex_unlock(&smu->mutex);
2669 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
2671 struct smu_context *smu = handle;
2674 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2677 mutex_lock(&smu->mutex);
2679 if (smu->ppt_funcs->get_fan_speed_rpm)
2680 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2682 mutex_unlock(&smu->mutex);
2687 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
2689 struct smu_context *smu = handle;
2692 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2695 mutex_lock(&smu->mutex);
2697 ret = smu_set_min_dcef_deep_sleep(smu, clk);
2699 mutex_unlock(&smu->mutex);
2704 static int smu_get_clock_by_type_with_latency(void *handle,
2705 enum amd_pp_clock_type type,
2706 struct pp_clock_levels_with_latency *clocks)
2708 struct smu_context *smu = handle;
2709 enum smu_clk_type clk_type;
2712 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2715 mutex_lock(&smu->mutex);
2717 if (smu->ppt_funcs->get_clock_by_type_with_latency) {
2719 case amd_pp_sys_clock:
2720 clk_type = SMU_GFXCLK;
2722 case amd_pp_mem_clock:
2723 clk_type = SMU_MCLK;
2725 case amd_pp_dcef_clock:
2726 clk_type = SMU_DCEFCLK;
2728 case amd_pp_disp_clock:
2729 clk_type = SMU_DISPCLK;
2732 dev_err(smu->adev->dev, "Invalid clock type!\n");
2733 mutex_unlock(&smu->mutex);
2737 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2740 mutex_unlock(&smu->mutex);
2745 static int smu_display_clock_voltage_request(void *handle,
2746 struct pp_display_clock_request *clock_req)
2748 struct smu_context *smu = handle;
2751 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2754 mutex_lock(&smu->mutex);
2756 if (smu->ppt_funcs->display_clock_voltage_request)
2757 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2759 mutex_unlock(&smu->mutex);
2765 static int smu_display_disable_memory_clock_switch(void *handle,
2766 bool disable_memory_clock_switch)
2768 struct smu_context *smu = handle;
2771 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2774 mutex_lock(&smu->mutex);
2776 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2777 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2779 mutex_unlock(&smu->mutex);
2784 static int smu_set_xgmi_pstate(void *handle,
2787 struct smu_context *smu = handle;
2790 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2793 mutex_lock(&smu->mutex);
2795 if (smu->ppt_funcs->set_xgmi_pstate)
2796 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2798 mutex_unlock(&smu->mutex);
2801 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
2806 static int smu_get_baco_capability(void *handle, bool *cap)
2808 struct smu_context *smu = handle;
2813 if (!smu->pm_enabled)
2816 mutex_lock(&smu->mutex);
2818 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2819 *cap = smu->ppt_funcs->baco_is_support(smu);
2821 mutex_unlock(&smu->mutex);
2826 static int smu_baco_set_state(void *handle, int state)
2828 struct smu_context *smu = handle;
2831 if (!smu->pm_enabled)
2835 mutex_lock(&smu->mutex);
2837 if (smu->ppt_funcs->baco_exit)
2838 ret = smu->ppt_funcs->baco_exit(smu);
2840 mutex_unlock(&smu->mutex);
2841 } else if (state == 1) {
2842 mutex_lock(&smu->mutex);
2844 if (smu->ppt_funcs->baco_enter)
2845 ret = smu->ppt_funcs->baco_enter(smu);
2847 mutex_unlock(&smu->mutex);
2854 dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
2855 (state)?"enter":"exit");
2860 bool smu_mode1_reset_is_support(struct smu_context *smu)
2864 if (!smu->pm_enabled)
2867 mutex_lock(&smu->mutex);
2869 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
2870 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
2872 mutex_unlock(&smu->mutex);
2877 bool smu_mode2_reset_is_support(struct smu_context *smu)
2881 if (!smu->pm_enabled)
2884 mutex_lock(&smu->mutex);
2886 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
2887 ret = smu->ppt_funcs->mode2_reset_is_support(smu);
2889 mutex_unlock(&smu->mutex);
2894 int smu_mode1_reset(struct smu_context *smu)
2898 if (!smu->pm_enabled)
2901 mutex_lock(&smu->mutex);
2903 if (smu->ppt_funcs->mode1_reset)
2904 ret = smu->ppt_funcs->mode1_reset(smu);
2906 mutex_unlock(&smu->mutex);
2911 static int smu_mode2_reset(void *handle)
2913 struct smu_context *smu = handle;
2916 if (!smu->pm_enabled)
2919 mutex_lock(&smu->mutex);
2921 if (smu->ppt_funcs->mode2_reset)
2922 ret = smu->ppt_funcs->mode2_reset(smu);
2924 mutex_unlock(&smu->mutex);
2927 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
2932 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
2933 struct pp_smu_nv_clock_table *max_clocks)
2935 struct smu_context *smu = handle;
2938 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2941 mutex_lock(&smu->mutex);
2943 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2944 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2946 mutex_unlock(&smu->mutex);
2951 static int smu_get_uclk_dpm_states(void *handle,
2952 unsigned int *clock_values_in_khz,
2953 unsigned int *num_states)
2955 struct smu_context *smu = handle;
2958 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2961 mutex_lock(&smu->mutex);
2963 if (smu->ppt_funcs->get_uclk_dpm_states)
2964 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2966 mutex_unlock(&smu->mutex);
2971 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
2973 struct smu_context *smu = handle;
2974 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2976 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2979 mutex_lock(&smu->mutex);
2981 if (smu->ppt_funcs->get_current_power_state)
2982 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2984 mutex_unlock(&smu->mutex);
2989 static int smu_get_dpm_clock_table(void *handle,
2990 struct dpm_clocks *clock_table)
2992 struct smu_context *smu = handle;
2995 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2998 mutex_lock(&smu->mutex);
3000 if (smu->ppt_funcs->get_dpm_clock_table)
3001 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
3003 mutex_unlock(&smu->mutex);
3008 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
3010 struct smu_context *smu = handle;
3013 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3016 if (!smu->ppt_funcs->get_gpu_metrics)
3019 mutex_lock(&smu->mutex);
3021 size = smu->ppt_funcs->get_gpu_metrics(smu, table);
3023 mutex_unlock(&smu->mutex);
3028 static int smu_enable_mgpu_fan_boost(void *handle)
3030 struct smu_context *smu = handle;
3033 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3036 mutex_lock(&smu->mutex);
3038 if (smu->ppt_funcs->enable_mgpu_fan_boost)
3039 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
3041 mutex_unlock(&smu->mutex);
3046 static int smu_gfx_state_change_set(void *handle,
3049 struct smu_context *smu = handle;
3052 mutex_lock(&smu->mutex);
3053 if (smu->ppt_funcs->gfx_state_change_set)
3054 ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
3055 mutex_unlock(&smu->mutex);
3060 int smu_set_light_sbr(struct smu_context *smu, bool enable)
3064 mutex_lock(&smu->mutex);
3065 if (smu->ppt_funcs->set_light_sbr)
3066 ret = smu->ppt_funcs->set_light_sbr(smu, enable);
3067 mutex_unlock(&smu->mutex);
3072 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
3074 struct smu_context *smu = handle;
3075 struct smu_table_context *smu_table = &smu->smu_table;
3076 struct smu_table *memory_pool = &smu_table->memory_pool;
3083 mutex_lock(&smu->mutex);
3084 if (memory_pool->bo) {
3085 *addr = memory_pool->cpu_addr;
3086 *size = memory_pool->size;
3088 mutex_unlock(&smu->mutex);
3093 static const struct amd_pm_funcs swsmu_pm_funcs = {
3094 /* export for sysfs */
3095 .set_fan_control_mode = smu_pp_set_fan_control_mode,
3096 .get_fan_control_mode = smu_get_fan_control_mode,
3097 .set_fan_speed_pwm = smu_set_fan_speed_pwm,
3098 .get_fan_speed_pwm = smu_get_fan_speed_pwm,
3099 .force_clock_level = smu_force_ppclk_levels,
3100 .print_clock_levels = smu_print_ppclk_levels,
3101 .force_performance_level = smu_force_performance_level,
3102 .read_sensor = smu_read_sensor,
3103 .get_performance_level = smu_get_performance_level,
3104 .get_current_power_state = smu_get_current_power_state,
3105 .get_fan_speed_rpm = smu_get_fan_speed_rpm,
3106 .set_fan_speed_rpm = smu_set_fan_speed_rpm,
3107 .get_pp_num_states = smu_get_power_num_states,
3108 .get_pp_table = smu_sys_get_pp_table,
3109 .set_pp_table = smu_sys_set_pp_table,
3110 .switch_power_profile = smu_switch_power_profile,
3111 /* export to amdgpu */
3112 .dispatch_tasks = smu_handle_dpm_task,
3113 .load_firmware = smu_load_microcode,
3114 .set_powergating_by_smu = smu_dpm_set_power_gate,
3115 .set_power_limit = smu_set_power_limit,
3116 .get_power_limit = smu_get_power_limit,
3117 .get_power_profile_mode = smu_get_power_profile_mode,
3118 .set_power_profile_mode = smu_set_power_profile_mode,
3119 .odn_edit_dpm_table = smu_od_edit_dpm_table,
3120 .set_mp1_state = smu_set_mp1_state,
3121 .gfx_state_change_set = smu_gfx_state_change_set,
3123 .get_sclk = smu_get_sclk,
3124 .get_mclk = smu_get_mclk,
3125 .display_configuration_change = smu_display_configuration_change,
3126 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,
3127 .display_clock_voltage_request = smu_display_clock_voltage_request,
3128 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,
3129 .set_active_display_count = smu_set_display_count,
3130 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,
3131 .get_asic_baco_capability = smu_get_baco_capability,
3132 .set_asic_baco_state = smu_baco_set_state,
3133 .get_ppfeature_status = smu_sys_get_pp_feature_mask,
3134 .set_ppfeature_status = smu_sys_set_pp_feature_mask,
3135 .asic_reset_mode_2 = smu_mode2_reset,
3136 .set_df_cstate = smu_set_df_cstate,
3137 .set_xgmi_pstate = smu_set_xgmi_pstate,
3138 .get_gpu_metrics = smu_sys_get_gpu_metrics,
3139 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,
3140 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
3141 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,
3142 .get_uclk_dpm_states = smu_get_uclk_dpm_states,
3143 .get_dpm_clock_table = smu_get_dpm_clock_table,
3144 .get_smu_prv_buf_details = smu_get_prv_buffer_details,
3147 int smu_wait_for_event(struct amdgpu_device *adev, enum smu_event_type event,
3151 struct smu_context *smu = &adev->smu;
3153 if (smu->ppt_funcs->wait_for_event) {
3154 mutex_lock(&smu->mutex);
3155 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
3156 mutex_unlock(&smu->mutex);