2 * Copyright 2019 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #define SWSMU_CODE_LAYER_L1
25 #include <linux/firmware.h>
26 #include <linux/pci.h>
29 #include "amdgpu_smu.h"
30 #include "smu_internal.h"
32 #include "arcturus_ppt.h"
33 #include "navi10_ppt.h"
34 #include "sienna_cichlid_ppt.h"
35 #include "renoir_ppt.h"
36 #include "vangogh_ppt.h"
37 #include "aldebaran_ppt.h"
38 #include "yellow_carp_ppt.h"
39 #include "cyan_skillfish_ppt.h"
40 #include "smu_v13_0_0_ppt.h"
41 #include "smu_v13_0_4_ppt.h"
42 #include "smu_v13_0_5_ppt.h"
43 #include "smu_v13_0_6_ppt.h"
44 #include "smu_v13_0_7_ppt.h"
48 * DO NOT use these for err/warn/info/debug messages.
49 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
50 * They are more MGPU friendly.
57 static const struct amd_pm_funcs swsmu_pm_funcs;
58 static int smu_force_smuclk_levels(struct smu_context *smu,
59 enum smu_clk_type clk_type,
61 static int smu_handle_task(struct smu_context *smu,
62 enum amd_dpm_forced_level level,
63 enum amd_pp_task task_id);
64 static int smu_reset(struct smu_context *smu);
65 static int smu_set_fan_speed_pwm(void *handle, u32 speed);
66 static int smu_set_fan_control_mode(void *handle, u32 value);
67 static int smu_set_power_limit(void *handle, uint32_t limit);
68 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);
69 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);
70 static int smu_set_mp1_state(void *handle, enum pp_mp1_state mp1_state);
72 static int smu_sys_get_pp_feature_mask(void *handle,
75 struct smu_context *smu = handle;
77 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
80 return smu_get_pp_feature_mask(smu, buf);
83 static int smu_sys_set_pp_feature_mask(void *handle,
86 struct smu_context *smu = handle;
88 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
91 return smu_set_pp_feature_mask(smu, new_mask);
94 int smu_set_residency_gfxoff(struct smu_context *smu, bool value)
96 if (!smu->ppt_funcs->set_gfx_off_residency)
99 return smu_set_gfx_off_residency(smu, value);
102 int smu_get_residency_gfxoff(struct smu_context *smu, u32 *value)
104 if (!smu->ppt_funcs->get_gfx_off_residency)
107 return smu_get_gfx_off_residency(smu, value);
110 int smu_get_entrycount_gfxoff(struct smu_context *smu, u64 *value)
112 if (!smu->ppt_funcs->get_gfx_off_entrycount)
115 return smu_get_gfx_off_entrycount(smu, value);
118 int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value)
120 if (!smu->ppt_funcs->get_gfx_off_status)
123 *value = smu_get_gfx_off_status(smu);
128 int smu_set_soft_freq_range(struct smu_context *smu,
129 enum smu_clk_type clk_type,
135 if (smu->ppt_funcs->set_soft_freq_limited_range)
136 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
144 int smu_get_dpm_freq_range(struct smu_context *smu,
145 enum smu_clk_type clk_type,
154 if (smu->ppt_funcs->get_dpm_ultimate_freq)
155 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
163 int smu_set_gfx_power_up_by_imu(struct smu_context *smu)
165 if (!smu->ppt_funcs || !smu->ppt_funcs->set_gfx_power_up_by_imu)
168 return smu->ppt_funcs->set_gfx_power_up_by_imu(smu);
171 static u32 smu_get_mclk(void *handle, bool low)
173 struct smu_context *smu = handle;
177 ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
178 low ? &clk_freq : NULL,
179 !low ? &clk_freq : NULL);
182 return clk_freq * 100;
185 static u32 smu_get_sclk(void *handle, bool low)
187 struct smu_context *smu = handle;
191 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
192 low ? &clk_freq : NULL,
193 !low ? &clk_freq : NULL);
196 return clk_freq * 100;
199 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
202 struct smu_power_context *smu_power = &smu->smu_power;
203 struct smu_power_gate *power_gate = &smu_power->power_gate;
206 if (!smu->ppt_funcs->dpm_set_vcn_enable)
209 if (atomic_read(&power_gate->vcn_gated) ^ enable)
212 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
214 atomic_set(&power_gate->vcn_gated, !enable);
219 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
222 struct smu_power_context *smu_power = &smu->smu_power;
223 struct smu_power_gate *power_gate = &smu_power->power_gate;
226 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
229 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
232 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
234 atomic_set(&power_gate->jpeg_gated, !enable);
240 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
242 * @handle: smu_context pointer
243 * @block_type: the IP block to power gate/ungate
244 * @gate: to power gate if true, ungate otherwise
246 * This API uses no smu->mutex lock protection due to:
247 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
248 * This is guarded to be race condition free by the caller.
249 * 2. Or get called on user setting request of power_dpm_force_performance_level.
250 * Under this case, the smu->mutex lock protection is already enforced on
251 * the parent API smu_force_performance_level of the call path.
253 static int smu_dpm_set_power_gate(void *handle,
257 struct smu_context *smu = handle;
260 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) {
261 dev_WARN(smu->adev->dev,
262 "SMU uninitialized but power %s requested for %u!\n",
263 gate ? "gate" : "ungate", block_type);
267 switch (block_type) {
269 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
270 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
272 case AMD_IP_BLOCK_TYPE_UVD:
273 case AMD_IP_BLOCK_TYPE_VCN:
274 ret = smu_dpm_set_vcn_enable(smu, !gate);
276 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
277 gate ? "gate" : "ungate");
279 case AMD_IP_BLOCK_TYPE_GFX:
280 ret = smu_gfx_off_control(smu, gate);
282 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
283 gate ? "enable" : "disable");
285 case AMD_IP_BLOCK_TYPE_SDMA:
286 ret = smu_powergate_sdma(smu, gate);
288 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
289 gate ? "gate" : "ungate");
291 case AMD_IP_BLOCK_TYPE_JPEG:
292 ret = smu_dpm_set_jpeg_enable(smu, !gate);
294 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
295 gate ? "gate" : "ungate");
298 dev_err(smu->adev->dev, "Unsupported block type!\n");
306 * smu_set_user_clk_dependencies - set user profile clock dependencies
308 * @smu: smu_context pointer
309 * @clk: enum smu_clk_type type
311 * Enable/Disable the clock dependency for the @clk type.
313 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
315 if (smu->adev->in_suspend)
318 if (clk == SMU_MCLK) {
319 smu->user_dpm_profile.clk_dependency = 0;
320 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
321 } else if (clk == SMU_FCLK) {
322 /* MCLK takes precedence over FCLK */
323 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
326 smu->user_dpm_profile.clk_dependency = 0;
327 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
328 } else if (clk == SMU_SOCCLK) {
329 /* MCLK takes precedence over SOCCLK */
330 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
333 smu->user_dpm_profile.clk_dependency = 0;
334 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
336 /* Add clk dependencies here, if any */
341 * smu_restore_dpm_user_profile - reinstate user dpm profile
343 * @smu: smu_context pointer
345 * Restore the saved user power configurations include power limit,
346 * clock frequencies, fan control mode and fan speed.
348 static void smu_restore_dpm_user_profile(struct smu_context *smu)
350 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
353 if (!smu->adev->in_suspend)
356 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
359 /* Enable restore flag */
360 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
362 /* set the user dpm power limit */
363 if (smu->user_dpm_profile.power_limit) {
364 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
366 dev_err(smu->adev->dev, "Failed to set power limit value\n");
369 /* set the user dpm clock configurations */
370 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
371 enum smu_clk_type clk_type;
373 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
375 * Iterate over smu clk type and force the saved user clk
376 * configs, skip if clock dependency is enabled
378 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
379 smu->user_dpm_profile.clk_mask[clk_type]) {
380 ret = smu_force_smuclk_levels(smu, clk_type,
381 smu->user_dpm_profile.clk_mask[clk_type]);
383 dev_err(smu->adev->dev,
384 "Failed to set clock type = %d\n", clk_type);
389 /* set the user dpm fan configurations */
390 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||
391 smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {
392 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
393 if (ret != -EOPNOTSUPP) {
394 smu->user_dpm_profile.fan_speed_pwm = 0;
395 smu->user_dpm_profile.fan_speed_rpm = 0;
396 smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;
397 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
400 if (smu->user_dpm_profile.fan_speed_pwm) {
401 ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);
402 if (ret != -EOPNOTSUPP)
403 dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");
406 if (smu->user_dpm_profile.fan_speed_rpm) {
407 ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);
408 if (ret != -EOPNOTSUPP)
409 dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");
413 /* Restore user customized OD settings */
414 if (smu->user_dpm_profile.user_od) {
415 if (smu->ppt_funcs->restore_user_od_settings) {
416 ret = smu->ppt_funcs->restore_user_od_settings(smu);
418 dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");
422 /* Disable restore flag */
423 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
426 static int smu_get_power_num_states(void *handle,
427 struct pp_states_info *state_info)
432 /* not support power state */
433 memset(state_info, 0, sizeof(struct pp_states_info));
434 state_info->nums = 1;
435 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
440 bool is_support_sw_smu(struct amdgpu_device *adev)
442 /* vega20 is 11.0.2, but it's supported via the powerplay code */
443 if (adev->asic_type == CHIP_VEGA20)
446 if (adev->ip_versions[MP1_HWIP][0] >= IP_VERSION(11, 0, 0))
452 bool is_support_cclk_dpm(struct amdgpu_device *adev)
454 struct smu_context *smu = adev->powerplay.pp_handle;
456 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
463 static int smu_sys_get_pp_table(void *handle,
466 struct smu_context *smu = handle;
467 struct smu_table_context *smu_table = &smu->smu_table;
469 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
472 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
475 if (smu_table->hardcode_pptable)
476 *table = smu_table->hardcode_pptable;
478 *table = smu_table->power_play_table;
480 return smu_table->power_play_table_size;
483 static int smu_sys_set_pp_table(void *handle,
487 struct smu_context *smu = handle;
488 struct smu_table_context *smu_table = &smu->smu_table;
489 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
492 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
495 if (header->usStructureSize != size) {
496 dev_err(smu->adev->dev, "pp table size not matched !\n");
500 if (!smu_table->hardcode_pptable) {
501 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
502 if (!smu_table->hardcode_pptable)
506 memcpy(smu_table->hardcode_pptable, buf, size);
507 smu_table->power_play_table = smu_table->hardcode_pptable;
508 smu_table->power_play_table_size = size;
511 * Special hw_fini action(for Navi1x, the DPMs disablement will be
512 * skipped) may be needed for custom pptable uploading.
514 smu->uploading_custom_pp_table = true;
516 ret = smu_reset(smu);
518 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
520 smu->uploading_custom_pp_table = false;
525 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
527 struct smu_feature *feature = &smu->smu_feature;
528 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
532 * With SCPM enabled, the allowed featuremasks setting(via
533 * PPSMC_MSG_SetAllowedFeaturesMaskLow/High) is not permitted.
534 * That means there is no way to let PMFW knows the settings below.
535 * Thus, we just assume all the features are allowed under
538 if (smu->adev->scpm_enabled) {
539 bitmap_fill(feature->allowed, SMU_FEATURE_MAX);
543 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
545 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
550 bitmap_or(feature->allowed, feature->allowed,
551 (unsigned long *)allowed_feature_mask,
552 feature->feature_num);
557 static int smu_set_funcs(struct amdgpu_device *adev)
559 struct smu_context *smu = adev->powerplay.pp_handle;
561 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
562 smu->od_enabled = true;
564 switch (adev->ip_versions[MP1_HWIP][0]) {
565 case IP_VERSION(11, 0, 0):
566 case IP_VERSION(11, 0, 5):
567 case IP_VERSION(11, 0, 9):
568 navi10_set_ppt_funcs(smu);
570 case IP_VERSION(11, 0, 7):
571 case IP_VERSION(11, 0, 11):
572 case IP_VERSION(11, 0, 12):
573 case IP_VERSION(11, 0, 13):
574 sienna_cichlid_set_ppt_funcs(smu);
576 case IP_VERSION(12, 0, 0):
577 case IP_VERSION(12, 0, 1):
578 renoir_set_ppt_funcs(smu);
580 case IP_VERSION(11, 5, 0):
581 vangogh_set_ppt_funcs(smu);
583 case IP_VERSION(13, 0, 1):
584 case IP_VERSION(13, 0, 3):
585 case IP_VERSION(13, 0, 8):
586 yellow_carp_set_ppt_funcs(smu);
588 case IP_VERSION(13, 0, 4):
589 case IP_VERSION(13, 0, 11):
590 smu_v13_0_4_set_ppt_funcs(smu);
592 case IP_VERSION(13, 0, 5):
593 smu_v13_0_5_set_ppt_funcs(smu);
595 case IP_VERSION(11, 0, 8):
596 cyan_skillfish_set_ppt_funcs(smu);
598 case IP_VERSION(11, 0, 2):
599 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
600 arcturus_set_ppt_funcs(smu);
601 /* OD is not supported on Arcturus */
602 smu->od_enabled =false;
604 case IP_VERSION(13, 0, 2):
605 aldebaran_set_ppt_funcs(smu);
606 /* Enable pp_od_clk_voltage node */
607 smu->od_enabled = true;
609 case IP_VERSION(13, 0, 0):
610 case IP_VERSION(13, 0, 10):
611 smu_v13_0_0_set_ppt_funcs(smu);
613 case IP_VERSION(13, 0, 6):
614 smu_v13_0_6_set_ppt_funcs(smu);
615 /* Enable pp_od_clk_voltage node */
616 smu->od_enabled = true;
618 case IP_VERSION(13, 0, 7):
619 smu_v13_0_7_set_ppt_funcs(smu);
628 static int smu_early_init(void *handle)
630 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
631 struct smu_context *smu;
634 smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL);
639 smu->pm_enabled = !!amdgpu_dpm;
641 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
642 smu->smu_baco.platform_support = false;
643 smu->user_dpm_profile.fan_mode = -1;
645 mutex_init(&smu->message_lock);
647 adev->powerplay.pp_handle = smu;
648 adev->powerplay.pp_funcs = &swsmu_pm_funcs;
650 r = smu_set_funcs(adev);
653 return smu_init_microcode(smu);
656 static int smu_set_default_dpm_table(struct smu_context *smu)
658 struct smu_power_context *smu_power = &smu->smu_power;
659 struct smu_power_gate *power_gate = &smu_power->power_gate;
660 int vcn_gate, jpeg_gate;
663 if (!smu->ppt_funcs->set_default_dpm_table)
666 vcn_gate = atomic_read(&power_gate->vcn_gated);
667 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
669 ret = smu_dpm_set_vcn_enable(smu, true);
673 ret = smu_dpm_set_jpeg_enable(smu, true);
677 ret = smu->ppt_funcs->set_default_dpm_table(smu);
679 dev_err(smu->adev->dev,
680 "Failed to setup default dpm clock tables!\n");
682 smu_dpm_set_jpeg_enable(smu, !jpeg_gate);
684 smu_dpm_set_vcn_enable(smu, !vcn_gate);
688 static int smu_apply_default_config_table_settings(struct smu_context *smu)
690 struct amdgpu_device *adev = smu->adev;
693 ret = smu_get_default_config_table_settings(smu,
694 &adev->pm.config_table);
698 return smu_set_config_table(smu, &adev->pm.config_table);
701 static int smu_late_init(void *handle)
703 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
704 struct smu_context *smu = adev->powerplay.pp_handle;
707 smu_set_fine_grain_gfx_freq_parameters(smu);
709 if (!smu->pm_enabled)
712 ret = smu_post_init(smu);
714 dev_err(adev->dev, "Failed to post smu init!\n");
718 if ((adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 1)) ||
719 (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 3)))
722 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
723 ret = smu_set_default_od_settings(smu);
725 dev_err(adev->dev, "Failed to setup default OD settings!\n");
730 ret = smu_populate_umd_state_clk(smu);
732 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
736 ret = smu_get_asic_power_limits(smu,
737 &smu->current_power_limit,
738 &smu->default_power_limit,
739 &smu->max_power_limit);
741 dev_err(adev->dev, "Failed to get asic power limits!\n");
745 if (!amdgpu_sriov_vf(adev))
746 smu_get_unique_id(smu);
748 smu_get_fan_parameters(smu);
751 smu->smu_dpm.dpm_level,
752 AMD_PP_TASK_COMPLETE_INIT);
754 ret = smu_apply_default_config_table_settings(smu);
755 if (ret && (ret != -EOPNOTSUPP)) {
756 dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n");
760 smu_restore_dpm_user_profile(smu);
765 static int smu_init_fb_allocations(struct smu_context *smu)
767 struct amdgpu_device *adev = smu->adev;
768 struct smu_table_context *smu_table = &smu->smu_table;
769 struct smu_table *tables = smu_table->tables;
770 struct smu_table *driver_table = &(smu_table->driver_table);
771 uint32_t max_table_size = 0;
774 /* VRAM allocation for tool table */
775 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
776 ret = amdgpu_bo_create_kernel(adev,
777 tables[SMU_TABLE_PMSTATUSLOG].size,
778 tables[SMU_TABLE_PMSTATUSLOG].align,
779 tables[SMU_TABLE_PMSTATUSLOG].domain,
780 &tables[SMU_TABLE_PMSTATUSLOG].bo,
781 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
782 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
784 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
789 /* VRAM allocation for driver table */
790 for (i = 0; i < SMU_TABLE_COUNT; i++) {
791 if (tables[i].size == 0)
794 if (i == SMU_TABLE_PMSTATUSLOG)
797 if (max_table_size < tables[i].size)
798 max_table_size = tables[i].size;
801 driver_table->size = max_table_size;
802 driver_table->align = PAGE_SIZE;
803 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
805 ret = amdgpu_bo_create_kernel(adev,
808 driver_table->domain,
810 &driver_table->mc_address,
811 &driver_table->cpu_addr);
813 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
814 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
815 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
816 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
817 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
823 static int smu_fini_fb_allocations(struct smu_context *smu)
825 struct smu_table_context *smu_table = &smu->smu_table;
826 struct smu_table *tables = smu_table->tables;
827 struct smu_table *driver_table = &(smu_table->driver_table);
829 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
830 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
831 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
832 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
834 amdgpu_bo_free_kernel(&driver_table->bo,
835 &driver_table->mc_address,
836 &driver_table->cpu_addr);
842 * smu_alloc_memory_pool - allocate memory pool in the system memory
844 * @smu: amdgpu_device pointer
846 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
847 * and DramLogSetDramAddr can notify it changed.
849 * Returns 0 on success, error on failure.
851 static int smu_alloc_memory_pool(struct smu_context *smu)
853 struct amdgpu_device *adev = smu->adev;
854 struct smu_table_context *smu_table = &smu->smu_table;
855 struct smu_table *memory_pool = &smu_table->memory_pool;
856 uint64_t pool_size = smu->pool_size;
859 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
862 memory_pool->size = pool_size;
863 memory_pool->align = PAGE_SIZE;
864 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
867 case SMU_MEMORY_POOL_SIZE_256_MB:
868 case SMU_MEMORY_POOL_SIZE_512_MB:
869 case SMU_MEMORY_POOL_SIZE_1_GB:
870 case SMU_MEMORY_POOL_SIZE_2_GB:
871 ret = amdgpu_bo_create_kernel(adev,
876 &memory_pool->mc_address,
877 &memory_pool->cpu_addr);
879 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
888 static int smu_free_memory_pool(struct smu_context *smu)
890 struct smu_table_context *smu_table = &smu->smu_table;
891 struct smu_table *memory_pool = &smu_table->memory_pool;
893 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
896 amdgpu_bo_free_kernel(&memory_pool->bo,
897 &memory_pool->mc_address,
898 &memory_pool->cpu_addr);
900 memset(memory_pool, 0, sizeof(struct smu_table));
905 static int smu_alloc_dummy_read_table(struct smu_context *smu)
907 struct smu_table_context *smu_table = &smu->smu_table;
908 struct smu_table *dummy_read_1_table =
909 &smu_table->dummy_read_1_table;
910 struct amdgpu_device *adev = smu->adev;
913 if (!dummy_read_1_table->size)
916 ret = amdgpu_bo_create_kernel(adev,
917 dummy_read_1_table->size,
918 dummy_read_1_table->align,
919 dummy_read_1_table->domain,
920 &dummy_read_1_table->bo,
921 &dummy_read_1_table->mc_address,
922 &dummy_read_1_table->cpu_addr);
924 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
929 static void smu_free_dummy_read_table(struct smu_context *smu)
931 struct smu_table_context *smu_table = &smu->smu_table;
932 struct smu_table *dummy_read_1_table =
933 &smu_table->dummy_read_1_table;
936 amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
937 &dummy_read_1_table->mc_address,
938 &dummy_read_1_table->cpu_addr);
940 memset(dummy_read_1_table, 0, sizeof(struct smu_table));
943 static int smu_smc_table_sw_init(struct smu_context *smu)
948 * Create smu_table structure, and init smc tables such as
949 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
951 ret = smu_init_smc_tables(smu);
953 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
958 * Create smu_power_context structure, and allocate smu_dpm_context and
959 * context size to fill the smu_power_context data.
961 ret = smu_init_power(smu);
963 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
968 * allocate vram bos to store smc table contents.
970 ret = smu_init_fb_allocations(smu);
974 ret = smu_alloc_memory_pool(smu);
978 ret = smu_alloc_dummy_read_table(smu);
982 ret = smu_i2c_init(smu);
989 static int smu_smc_table_sw_fini(struct smu_context *smu)
995 smu_free_dummy_read_table(smu);
997 ret = smu_free_memory_pool(smu);
1001 ret = smu_fini_fb_allocations(smu);
1005 ret = smu_fini_power(smu);
1007 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
1011 ret = smu_fini_smc_tables(smu);
1013 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
1020 static void smu_throttling_logging_work_fn(struct work_struct *work)
1022 struct smu_context *smu = container_of(work, struct smu_context,
1023 throttling_logging_work);
1025 smu_log_thermal_throttling(smu);
1028 static void smu_interrupt_work_fn(struct work_struct *work)
1030 struct smu_context *smu = container_of(work, struct smu_context,
1033 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
1034 smu->ppt_funcs->interrupt_work(smu);
1037 static int smu_sw_init(void *handle)
1039 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1040 struct smu_context *smu = adev->powerplay.pp_handle;
1043 smu->pool_size = adev->pm.smu_prv_buffer_size;
1044 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
1045 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
1046 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1048 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
1049 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
1050 atomic64_set(&smu->throttle_int_counter, 0);
1051 smu->watermarks_bitmap = 0;
1052 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1053 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1055 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
1056 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
1058 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
1059 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
1060 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
1061 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
1062 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
1063 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
1064 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
1065 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
1067 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1068 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1069 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
1070 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
1071 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
1072 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
1073 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
1074 smu->display_config = &adev->pm.pm_display_cfg;
1076 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1077 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1079 ret = smu_smc_table_sw_init(smu);
1081 dev_err(adev->dev, "Failed to sw init smc table!\n");
1085 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1086 ret = smu_get_vbios_bootup_values(smu);
1088 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1092 ret = smu_init_pptable_microcode(smu);
1094 dev_err(adev->dev, "Failed to setup pptable firmware!\n");
1098 ret = smu_register_irq_handler(smu);
1100 dev_err(adev->dev, "Failed to register smc irq handler!\n");
1104 /* If there is no way to query fan control mode, fan control is not supported */
1105 if (!smu->ppt_funcs->get_fan_control_mode)
1106 smu->adev->pm.no_fan = true;
1111 static int smu_sw_fini(void *handle)
1113 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1114 struct smu_context *smu = adev->powerplay.pp_handle;
1117 ret = smu_smc_table_sw_fini(smu);
1119 dev_err(adev->dev, "Failed to sw fini smc table!\n");
1123 smu_fini_microcode(smu);
1128 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1130 struct amdgpu_device *adev = smu->adev;
1131 struct smu_temperature_range *range =
1132 &smu->thermal_range;
1135 if (!smu->ppt_funcs->get_thermal_temperature_range)
1138 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1142 adev->pm.dpm.thermal.min_temp = range->min;
1143 adev->pm.dpm.thermal.max_temp = range->max;
1144 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1145 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1146 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1147 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1148 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1149 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1150 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1155 static int smu_smc_hw_setup(struct smu_context *smu)
1157 struct smu_feature *feature = &smu->smu_feature;
1158 struct amdgpu_device *adev = smu->adev;
1159 uint32_t pcie_gen = 0, pcie_width = 0;
1160 uint64_t features_supported;
1163 switch (adev->ip_versions[MP1_HWIP][0]) {
1164 case IP_VERSION(11, 0, 7):
1165 case IP_VERSION(11, 0, 11):
1166 case IP_VERSION(11, 5, 0):
1167 case IP_VERSION(11, 0, 12):
1168 if (adev->in_suspend && smu_is_dpm_running(smu)) {
1169 dev_info(adev->dev, "dpm has been enabled\n");
1170 ret = smu_system_features_control(smu, true);
1172 dev_err(adev->dev, "Failed system features control!\n");
1180 ret = smu_init_display_count(smu, 0);
1182 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1186 ret = smu_set_driver_table_location(smu);
1188 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1193 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1195 ret = smu_set_tool_table_location(smu);
1197 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1202 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1205 ret = smu_notify_memory_pool_location(smu);
1207 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1212 * It is assumed the pptable used before runpm is same as
1213 * the one used afterwards. Thus, we can reuse the stored
1214 * copy and do not need to resetup the pptable again.
1216 if (!adev->in_runpm) {
1217 ret = smu_setup_pptable(smu);
1219 dev_err(adev->dev, "Failed to setup pptable!\n");
1224 /* smu_dump_pptable(smu); */
1227 * With SCPM enabled, PSP is responsible for the PPTable transferring
1228 * (to SMU). Driver involvement is not needed and permitted.
1230 if (!adev->scpm_enabled) {
1232 * Copy pptable bo in the vram to smc with SMU MSGs such as
1233 * SetDriverDramAddr and TransferTableDram2Smu.
1235 ret = smu_write_pptable(smu);
1237 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1242 /* issue Run*Btc msg */
1243 ret = smu_run_btc(smu);
1248 * With SCPM enabled, these actions(and relevant messages) are
1249 * not needed and permitted.
1251 if (!adev->scpm_enabled) {
1252 ret = smu_feature_set_allowed_mask(smu);
1254 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1259 ret = smu_system_features_control(smu, true);
1261 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1265 ret = smu_feature_get_enabled_mask(smu, &features_supported);
1267 dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");
1270 bitmap_copy(feature->supported,
1271 (unsigned long *)&features_supported,
1272 feature->feature_num);
1274 if (!smu_is_dpm_running(smu))
1275 dev_info(adev->dev, "dpm has been disabled\n");
1278 * Set initialized values (get from vbios) to dpm tables context such as
1279 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1282 ret = smu_set_default_dpm_table(smu);
1284 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1288 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1290 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1292 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1294 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1297 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1298 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1299 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
1301 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1303 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1305 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1307 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1309 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1311 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1313 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1315 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1319 ret = smu_get_thermal_temperature_range(smu);
1321 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1325 ret = smu_enable_thermal_alert(smu);
1327 dev_err(adev->dev, "Failed to enable thermal alert!\n");
1331 ret = smu_notify_display_change(smu);
1333 dev_err(adev->dev, "Failed to notify display change!\n");
1338 * Set min deep sleep dce fclk with bootup value from vbios via
1339 * SetMinDeepSleepDcefclk MSG.
1341 ret = smu_set_min_dcef_deep_sleep(smu,
1342 smu->smu_table.boot_values.dcefclk / 100);
1347 static int smu_start_smc_engine(struct smu_context *smu)
1349 struct amdgpu_device *adev = smu->adev;
1352 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1353 if (adev->ip_versions[MP1_HWIP][0] < IP_VERSION(11, 0, 0)) {
1354 if (smu->ppt_funcs->load_microcode) {
1355 ret = smu->ppt_funcs->load_microcode(smu);
1362 if (smu->ppt_funcs->check_fw_status) {
1363 ret = smu->ppt_funcs->check_fw_status(smu);
1365 dev_err(adev->dev, "SMC is not ready\n");
1371 * Send msg GetDriverIfVersion to check if the return value is equal
1372 * with DRIVER_IF_VERSION of smc header.
1374 ret = smu_check_fw_version(smu);
1381 static int smu_hw_init(void *handle)
1384 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1385 struct smu_context *smu = adev->powerplay.pp_handle;
1387 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1388 smu->pm_enabled = false;
1392 ret = smu_start_smc_engine(smu);
1394 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1399 if ((smu->ppt_funcs->set_gfx_power_up_by_imu) &&
1400 likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
1401 ret = smu->ppt_funcs->set_gfx_power_up_by_imu(smu);
1403 dev_err(adev->dev, "Failed to Enable gfx imu!\n");
1408 smu_dpm_set_vcn_enable(smu, true);
1409 smu_dpm_set_jpeg_enable(smu, true);
1410 smu_set_gfx_cgpg(smu, true);
1413 if (!smu->pm_enabled)
1416 ret = smu_get_driver_allowed_feature_mask(smu);
1420 ret = smu_smc_hw_setup(smu);
1422 dev_err(adev->dev, "Failed to setup smc hw!\n");
1427 * Move maximum sustainable clock retrieving here considering
1428 * 1. It is not needed on resume(from S3).
1429 * 2. DAL settings come between .hw_init and .late_init of SMU.
1430 * And DAL needs to know the maximum sustainable clocks. Thus
1431 * it cannot be put in .late_init().
1433 ret = smu_init_max_sustainable_clocks(smu);
1435 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1439 adev->pm.dpm_enabled = true;
1441 dev_info(adev->dev, "SMU is initialized successfully!\n");
1446 static int smu_disable_dpms(struct smu_context *smu)
1448 struct amdgpu_device *adev = smu->adev;
1450 bool use_baco = !smu->is_apu &&
1451 ((amdgpu_in_reset(adev) &&
1452 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1453 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1456 * For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others)
1457 * properly on suspend/reset/unload. Driver involvement may cause some unexpected issues.
1459 switch (adev->ip_versions[MP1_HWIP][0]) {
1460 case IP_VERSION(13, 0, 0):
1461 case IP_VERSION(13, 0, 7):
1462 case IP_VERSION(13, 0, 10):
1469 * For custom pptable uploading, skip the DPM features
1470 * disable process on Navi1x ASICs.
1471 * - As the gfx related features are under control of
1472 * RLC on those ASICs. RLC reinitialization will be
1473 * needed to reenable them. That will cost much more
1476 * - SMU firmware can handle the DPM reenablement
1479 if (smu->uploading_custom_pp_table) {
1480 switch (adev->ip_versions[MP1_HWIP][0]) {
1481 case IP_VERSION(11, 0, 0):
1482 case IP_VERSION(11, 0, 5):
1483 case IP_VERSION(11, 0, 9):
1484 case IP_VERSION(11, 0, 7):
1485 case IP_VERSION(11, 0, 11):
1486 case IP_VERSION(11, 5, 0):
1487 case IP_VERSION(11, 0, 12):
1488 case IP_VERSION(11, 0, 13):
1496 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1497 * on BACO in. Driver involvement is unnecessary.
1500 switch (adev->ip_versions[MP1_HWIP][0]) {
1501 case IP_VERSION(11, 0, 7):
1502 case IP_VERSION(11, 0, 0):
1503 case IP_VERSION(11, 0, 5):
1504 case IP_VERSION(11, 0, 9):
1505 case IP_VERSION(13, 0, 7):
1513 * For SMU 13.0.4/11, PMFW will handle the features disablement properly
1514 * for gpu reset case. Driver involvement is unnecessary.
1516 if (amdgpu_in_reset(adev)) {
1517 switch (adev->ip_versions[MP1_HWIP][0]) {
1518 case IP_VERSION(13, 0, 4):
1519 case IP_VERSION(13, 0, 11):
1527 * For gpu reset, runpm and hibernation through BACO,
1528 * BACO feature has to be kept enabled.
1530 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1531 ret = smu_disable_all_features_with_exception(smu,
1532 SMU_FEATURE_BACO_BIT);
1534 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1536 /* DisableAllSmuFeatures message is not permitted with SCPM enabled */
1537 if (!adev->scpm_enabled) {
1538 ret = smu_system_features_control(smu, false);
1540 dev_err(adev->dev, "Failed to disable smu features.\n");
1544 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(9, 4, 2) &&
1545 !amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop)
1546 adev->gfx.rlc.funcs->stop(adev);
1551 static int smu_smc_hw_cleanup(struct smu_context *smu)
1553 struct amdgpu_device *adev = smu->adev;
1556 cancel_work_sync(&smu->throttling_logging_work);
1557 cancel_work_sync(&smu->interrupt_work);
1559 ret = smu_disable_thermal_alert(smu);
1561 dev_err(adev->dev, "Fail to disable thermal alert!\n");
1565 ret = smu_disable_dpms(smu);
1567 dev_err(adev->dev, "Fail to disable dpm features!\n");
1574 static int smu_hw_fini(void *handle)
1576 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1577 struct smu_context *smu = adev->powerplay.pp_handle;
1579 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1582 smu_dpm_set_vcn_enable(smu, false);
1583 smu_dpm_set_jpeg_enable(smu, false);
1585 adev->vcn.cur_state = AMD_PG_STATE_GATE;
1586 adev->jpeg.cur_state = AMD_PG_STATE_GATE;
1588 if (!smu->pm_enabled)
1591 adev->pm.dpm_enabled = false;
1593 return smu_smc_hw_cleanup(smu);
1596 static void smu_late_fini(void *handle)
1598 struct amdgpu_device *adev = handle;
1599 struct smu_context *smu = adev->powerplay.pp_handle;
1604 static int smu_reset(struct smu_context *smu)
1606 struct amdgpu_device *adev = smu->adev;
1609 ret = smu_hw_fini(adev);
1613 ret = smu_hw_init(adev);
1617 ret = smu_late_init(adev);
1624 static int smu_suspend(void *handle)
1626 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1627 struct smu_context *smu = adev->powerplay.pp_handle;
1631 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1634 if (!smu->pm_enabled)
1637 adev->pm.dpm_enabled = false;
1639 ret = smu_smc_hw_cleanup(smu);
1643 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1645 smu_set_gfx_cgpg(smu, false);
1648 * pwfw resets entrycount when device is suspended, so we save the
1649 * last value to be used when we resume to keep it consistent
1651 ret = smu_get_entrycount_gfxoff(smu, &count);
1653 adev->gfx.gfx_off_entrycount = count;
1658 static int smu_resume(void *handle)
1661 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1662 struct smu_context *smu = adev->powerplay.pp_handle;
1664 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1667 if (!smu->pm_enabled)
1670 dev_info(adev->dev, "SMU is resuming...\n");
1672 ret = smu_start_smc_engine(smu);
1674 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1678 ret = smu_smc_hw_setup(smu);
1680 dev_err(adev->dev, "Failed to setup smc hw!\n");
1684 smu_set_gfx_cgpg(smu, true);
1686 smu->disable_uclk_switch = 0;
1688 adev->pm.dpm_enabled = true;
1690 dev_info(adev->dev, "SMU is resumed successfully!\n");
1695 static int smu_display_configuration_change(void *handle,
1696 const struct amd_pp_display_configuration *display_config)
1698 struct smu_context *smu = handle;
1700 int num_of_active_display = 0;
1702 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1705 if (!display_config)
1708 smu_set_min_dcef_deep_sleep(smu,
1709 display_config->min_dcef_deep_sleep_set_clk / 100);
1711 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1712 if (display_config->displays[index].controller_id != 0)
1713 num_of_active_display++;
1719 static int smu_set_clockgating_state(void *handle,
1720 enum amd_clockgating_state state)
1725 static int smu_set_powergating_state(void *handle,
1726 enum amd_powergating_state state)
1731 static int smu_enable_umd_pstate(void *handle,
1732 enum amd_dpm_forced_level *level)
1734 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1735 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1736 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1737 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1739 struct smu_context *smu = (struct smu_context*)(handle);
1740 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1742 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1745 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1746 /* enter umd pstate, save current level, disable gfx cg*/
1747 if (*level & profile_mode_mask) {
1748 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1749 smu_gpo_control(smu, false);
1750 smu_gfx_ulv_control(smu, false);
1751 smu_deep_sleep_control(smu, false);
1752 amdgpu_asic_update_umd_stable_pstate(smu->adev, true);
1755 /* exit umd pstate, restore level, enable gfx cg*/
1756 if (!(*level & profile_mode_mask)) {
1757 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1758 *level = smu_dpm_ctx->saved_dpm_level;
1759 amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
1760 smu_deep_sleep_control(smu, true);
1761 smu_gfx_ulv_control(smu, true);
1762 smu_gpo_control(smu, true);
1769 static int smu_bump_power_profile_mode(struct smu_context *smu,
1771 uint32_t param_size)
1775 if (smu->ppt_funcs->set_power_profile_mode)
1776 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
1781 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
1782 enum amd_dpm_forced_level level,
1783 bool skip_display_settings)
1788 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1790 if (!skip_display_settings) {
1791 ret = smu_display_config_changed(smu);
1793 dev_err(smu->adev->dev, "Failed to change display config!");
1798 ret = smu_apply_clocks_adjust_rules(smu);
1800 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
1804 if (!skip_display_settings) {
1805 ret = smu_notify_smc_display_config(smu);
1807 dev_err(smu->adev->dev, "Failed to notify smc display config!");
1812 if (smu_dpm_ctx->dpm_level != level) {
1813 ret = smu_asic_set_performance_level(smu, level);
1815 dev_err(smu->adev->dev, "Failed to set performance level!");
1819 /* update the saved copy */
1820 smu_dpm_ctx->dpm_level = level;
1823 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1824 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
1825 index = fls(smu->workload_mask);
1826 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1827 workload = smu->workload_setting[index];
1829 if (smu->power_profile_mode != workload)
1830 smu_bump_power_profile_mode(smu, &workload, 0);
1836 static int smu_handle_task(struct smu_context *smu,
1837 enum amd_dpm_forced_level level,
1838 enum amd_pp_task task_id)
1842 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1846 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1847 ret = smu_pre_display_config_changed(smu);
1850 ret = smu_adjust_power_state_dynamic(smu, level, false);
1852 case AMD_PP_TASK_COMPLETE_INIT:
1853 case AMD_PP_TASK_READJUST_POWER_STATE:
1854 ret = smu_adjust_power_state_dynamic(smu, level, true);
1863 static int smu_handle_dpm_task(void *handle,
1864 enum amd_pp_task task_id,
1865 enum amd_pm_state_type *user_state)
1867 struct smu_context *smu = handle;
1868 struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
1870 return smu_handle_task(smu, smu_dpm->dpm_level, task_id);
1874 static int smu_switch_power_profile(void *handle,
1875 enum PP_SMC_POWER_PROFILE type,
1878 struct smu_context *smu = handle;
1879 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1883 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1886 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1890 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1891 index = fls(smu->workload_mask);
1892 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1893 workload = smu->workload_setting[index];
1895 smu->workload_mask |= (1 << smu->workload_prority[type]);
1896 index = fls(smu->workload_mask);
1897 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1898 workload = smu->workload_setting[index];
1901 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
1902 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
1903 smu_bump_power_profile_mode(smu, &workload, 0);
1908 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
1910 struct smu_context *smu = handle;
1911 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1913 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1916 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1919 return smu_dpm_ctx->dpm_level;
1922 static int smu_force_performance_level(void *handle,
1923 enum amd_dpm_forced_level level)
1925 struct smu_context *smu = handle;
1926 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1929 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1932 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1935 ret = smu_enable_umd_pstate(smu, &level);
1939 ret = smu_handle_task(smu, level,
1940 AMD_PP_TASK_READJUST_POWER_STATE);
1942 /* reset user dpm clock state */
1943 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1944 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
1945 smu->user_dpm_profile.clk_dependency = 0;
1951 static int smu_set_display_count(void *handle, uint32_t count)
1953 struct smu_context *smu = handle;
1955 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1958 return smu_init_display_count(smu, count);
1961 static int smu_force_smuclk_levels(struct smu_context *smu,
1962 enum smu_clk_type clk_type,
1965 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1968 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1971 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1972 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
1976 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
1977 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1978 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
1979 smu->user_dpm_profile.clk_mask[clk_type] = mask;
1980 smu_set_user_clk_dependencies(smu, clk_type);
1987 static int smu_force_ppclk_levels(void *handle,
1988 enum pp_clock_type type,
1991 struct smu_context *smu = handle;
1992 enum smu_clk_type clk_type;
1996 clk_type = SMU_SCLK; break;
1998 clk_type = SMU_MCLK; break;
2000 clk_type = SMU_PCIE; break;
2002 clk_type = SMU_SOCCLK; break;
2004 clk_type = SMU_FCLK; break;
2006 clk_type = SMU_DCEFCLK; break;
2008 clk_type = SMU_VCLK; break;
2010 clk_type = SMU_DCLK; break;
2012 clk_type = SMU_OD_SCLK; break;
2014 clk_type = SMU_OD_MCLK; break;
2016 clk_type = SMU_OD_VDDC_CURVE; break;
2018 clk_type = SMU_OD_RANGE; break;
2023 return smu_force_smuclk_levels(smu, clk_type, mask);
2027 * On system suspending or resetting, the dpm_enabled
2028 * flag will be cleared. So that those SMU services which
2029 * are not supported will be gated.
2030 * However, the mp1 state setting should still be granted
2031 * even if the dpm_enabled cleared.
2033 static int smu_set_mp1_state(void *handle,
2034 enum pp_mp1_state mp1_state)
2036 struct smu_context *smu = handle;
2039 if (!smu->pm_enabled)
2042 if (smu->ppt_funcs &&
2043 smu->ppt_funcs->set_mp1_state)
2044 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
2049 static int smu_set_df_cstate(void *handle,
2050 enum pp_df_cstate state)
2052 struct smu_context *smu = handle;
2055 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2058 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
2061 ret = smu->ppt_funcs->set_df_cstate(smu, state);
2063 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
2068 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en)
2072 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2075 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down)
2078 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en);
2080 dev_err(smu->adev->dev, "[AllowXgmiPowerDown] failed!\n");
2085 int smu_write_watermarks_table(struct smu_context *smu)
2087 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2090 return smu_set_watermarks_table(smu, NULL);
2093 static int smu_set_watermarks_for_clock_ranges(void *handle,
2094 struct pp_smu_wm_range_sets *clock_ranges)
2096 struct smu_context *smu = handle;
2098 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2101 if (smu->disable_watermark)
2104 return smu_set_watermarks_table(smu, clock_ranges);
2107 int smu_set_ac_dc(struct smu_context *smu)
2111 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2114 /* controlled by firmware */
2115 if (smu->dc_controlled_by_gpio)
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");
2128 const struct amd_ip_funcs smu_ip_funcs = {
2130 .early_init = smu_early_init,
2131 .late_init = smu_late_init,
2132 .sw_init = smu_sw_init,
2133 .sw_fini = smu_sw_fini,
2134 .hw_init = smu_hw_init,
2135 .hw_fini = smu_hw_fini,
2136 .late_fini = smu_late_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,
2147 const struct amdgpu_ip_block_version smu_v11_0_ip_block =
2149 .type = AMD_IP_BLOCK_TYPE_SMC,
2153 .funcs = &smu_ip_funcs,
2156 const struct amdgpu_ip_block_version smu_v12_0_ip_block =
2158 .type = AMD_IP_BLOCK_TYPE_SMC,
2162 .funcs = &smu_ip_funcs,
2165 const struct amdgpu_ip_block_version smu_v13_0_ip_block =
2167 .type = AMD_IP_BLOCK_TYPE_SMC,
2171 .funcs = &smu_ip_funcs,
2174 static int smu_load_microcode(void *handle)
2176 struct smu_context *smu = handle;
2177 struct amdgpu_device *adev = smu->adev;
2180 if (!smu->pm_enabled)
2183 /* This should be used for non PSP loading */
2184 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2187 if (smu->ppt_funcs->load_microcode) {
2188 ret = smu->ppt_funcs->load_microcode(smu);
2190 dev_err(adev->dev, "Load microcode failed\n");
2195 if (smu->ppt_funcs->check_fw_status) {
2196 ret = smu->ppt_funcs->check_fw_status(smu);
2198 dev_err(adev->dev, "SMC is not ready\n");
2206 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2210 if (smu->ppt_funcs->set_gfx_cgpg)
2211 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2216 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2218 struct smu_context *smu = handle;
2221 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2224 if (!smu->ppt_funcs->set_fan_speed_rpm)
2227 if (speed == U32_MAX)
2230 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2231 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2232 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;
2233 smu->user_dpm_profile.fan_speed_rpm = speed;
2235 /* Override custom PWM setting as they cannot co-exist */
2236 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;
2237 smu->user_dpm_profile.fan_speed_pwm = 0;
2244 * smu_get_power_limit - Request one of the SMU Power Limits
2246 * @handle: pointer to smu context
2247 * @limit: requested limit is written back to this variable
2248 * @pp_limit_level: &pp_power_limit_level which limit of the power to return
2249 * @pp_power_type: &pp_power_type type of power
2250 * Return: 0 on success, <0 on error
2253 int smu_get_power_limit(void *handle,
2255 enum pp_power_limit_level pp_limit_level,
2256 enum pp_power_type pp_power_type)
2258 struct smu_context *smu = handle;
2259 struct amdgpu_device *adev = smu->adev;
2260 enum smu_ppt_limit_level limit_level;
2261 uint32_t limit_type;
2264 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2267 switch(pp_power_type) {
2268 case PP_PWR_TYPE_SUSTAINED:
2269 limit_type = SMU_DEFAULT_PPT_LIMIT;
2271 case PP_PWR_TYPE_FAST:
2272 limit_type = SMU_FAST_PPT_LIMIT;
2279 switch(pp_limit_level){
2280 case PP_PWR_LIMIT_CURRENT:
2281 limit_level = SMU_PPT_LIMIT_CURRENT;
2283 case PP_PWR_LIMIT_DEFAULT:
2284 limit_level = SMU_PPT_LIMIT_DEFAULT;
2286 case PP_PWR_LIMIT_MAX:
2287 limit_level = SMU_PPT_LIMIT_MAX;
2289 case PP_PWR_LIMIT_MIN:
2295 if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2296 if (smu->ppt_funcs->get_ppt_limit)
2297 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2299 switch (limit_level) {
2300 case SMU_PPT_LIMIT_CURRENT:
2301 switch (adev->ip_versions[MP1_HWIP][0]) {
2302 case IP_VERSION(13, 0, 2):
2303 case IP_VERSION(11, 0, 7):
2304 case IP_VERSION(11, 0, 11):
2305 case IP_VERSION(11, 0, 12):
2306 case IP_VERSION(11, 0, 13):
2307 ret = smu_get_asic_power_limits(smu,
2308 &smu->current_power_limit,
2315 *limit = smu->current_power_limit;
2317 case SMU_PPT_LIMIT_DEFAULT:
2318 *limit = smu->default_power_limit;
2320 case SMU_PPT_LIMIT_MAX:
2321 *limit = smu->max_power_limit;
2331 static int smu_set_power_limit(void *handle, uint32_t limit)
2333 struct smu_context *smu = handle;
2334 uint32_t limit_type = limit >> 24;
2337 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2341 if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2342 if (smu->ppt_funcs->set_power_limit)
2343 return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2345 if (limit > smu->max_power_limit) {
2346 dev_err(smu->adev->dev,
2347 "New power limit (%d) is over the max allowed %d\n",
2348 limit, smu->max_power_limit);
2353 limit = smu->current_power_limit;
2355 if (smu->ppt_funcs->set_power_limit) {
2356 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2357 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2358 smu->user_dpm_profile.power_limit = limit;
2364 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2368 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2371 if (smu->ppt_funcs->print_clk_levels)
2372 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2377 static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)
2379 enum smu_clk_type clk_type;
2383 clk_type = SMU_SCLK; break;
2385 clk_type = SMU_MCLK; break;
2387 clk_type = SMU_PCIE; break;
2389 clk_type = SMU_SOCCLK; break;
2391 clk_type = SMU_FCLK; break;
2393 clk_type = SMU_DCEFCLK; break;
2395 clk_type = SMU_VCLK; break;
2397 clk_type = SMU_DCLK; break;
2399 clk_type = SMU_OD_SCLK; break;
2401 clk_type = SMU_OD_MCLK; break;
2403 clk_type = SMU_OD_VDDC_CURVE; break;
2405 clk_type = SMU_OD_RANGE; break;
2406 case OD_VDDGFX_OFFSET:
2407 clk_type = SMU_OD_VDDGFX_OFFSET; break;
2409 clk_type = SMU_OD_CCLK; break;
2411 clk_type = SMU_CLK_COUNT; break;
2417 static int smu_print_ppclk_levels(void *handle,
2418 enum pp_clock_type type,
2421 struct smu_context *smu = handle;
2422 enum smu_clk_type clk_type;
2424 clk_type = smu_convert_to_smuclk(type);
2425 if (clk_type == SMU_CLK_COUNT)
2428 return smu_print_smuclk_levels(smu, clk_type, buf);
2431 static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)
2433 struct smu_context *smu = handle;
2434 enum smu_clk_type clk_type;
2436 clk_type = smu_convert_to_smuclk(type);
2437 if (clk_type == SMU_CLK_COUNT)
2440 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2443 if (!smu->ppt_funcs->emit_clk_levels)
2446 return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);
2450 static int smu_od_edit_dpm_table(void *handle,
2451 enum PP_OD_DPM_TABLE_COMMAND type,
2452 long *input, uint32_t size)
2454 struct smu_context *smu = handle;
2457 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2460 if (smu->ppt_funcs->od_edit_dpm_table) {
2461 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2467 static int smu_read_sensor(void *handle,
2472 struct smu_context *smu = handle;
2473 struct smu_umd_pstate_table *pstate_table =
2476 uint32_t *size, size_val;
2478 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2481 if (!data || !size_arg)
2484 size_val = *size_arg;
2487 if (smu->ppt_funcs->read_sensor)
2488 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2492 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2493 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2496 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2497 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2500 case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
2501 *((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100;
2504 case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
2505 *((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100;
2508 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2509 ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);
2512 case AMDGPU_PP_SENSOR_UVD_POWER:
2513 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
2516 case AMDGPU_PP_SENSOR_VCE_POWER:
2517 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
2520 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
2521 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0: 1;
2524 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
2525 *(uint32_t *)data = 0;
2535 // assign uint32_t to int
2536 *size_arg = size_val;
2541 static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit)
2544 struct smu_context *smu = handle;
2546 if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit)
2547 ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit);
2552 static int smu_set_apu_thermal_limit(void *handle, uint32_t limit)
2555 struct smu_context *smu = handle;
2557 if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit)
2558 ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit);
2563 static int smu_get_power_profile_mode(void *handle, char *buf)
2565 struct smu_context *smu = handle;
2567 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
2568 !smu->ppt_funcs->get_power_profile_mode)
2573 return smu->ppt_funcs->get_power_profile_mode(smu, buf);
2576 static int smu_set_power_profile_mode(void *handle,
2578 uint32_t param_size)
2580 struct smu_context *smu = handle;
2582 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
2583 !smu->ppt_funcs->set_power_profile_mode)
2586 return smu_bump_power_profile_mode(smu, param, param_size);
2589 static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)
2591 struct smu_context *smu = handle;
2593 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2596 if (!smu->ppt_funcs->get_fan_control_mode)
2602 *fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);
2607 static int smu_set_fan_control_mode(void *handle, u32 value)
2609 struct smu_context *smu = handle;
2612 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2615 if (!smu->ppt_funcs->set_fan_control_mode)
2618 if (value == U32_MAX)
2621 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2625 if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2626 smu->user_dpm_profile.fan_mode = value;
2628 /* reset user dpm fan speed */
2629 if (value != AMD_FAN_CTRL_MANUAL) {
2630 smu->user_dpm_profile.fan_speed_pwm = 0;
2631 smu->user_dpm_profile.fan_speed_rpm = 0;
2632 smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);
2640 static int smu_get_fan_speed_pwm(void *handle, u32 *speed)
2642 struct smu_context *smu = handle;
2645 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2648 if (!smu->ppt_funcs->get_fan_speed_pwm)
2654 ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);
2659 static int smu_set_fan_speed_pwm(void *handle, u32 speed)
2661 struct smu_context *smu = handle;
2664 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2667 if (!smu->ppt_funcs->set_fan_speed_pwm)
2670 if (speed == U32_MAX)
2673 ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);
2674 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2675 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;
2676 smu->user_dpm_profile.fan_speed_pwm = speed;
2678 /* Override custom RPM setting as they cannot co-exist */
2679 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;
2680 smu->user_dpm_profile.fan_speed_rpm = 0;
2686 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
2688 struct smu_context *smu = handle;
2691 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2694 if (!smu->ppt_funcs->get_fan_speed_rpm)
2700 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2705 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
2707 struct smu_context *smu = handle;
2709 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2712 return smu_set_min_dcef_deep_sleep(smu, clk);
2715 static int smu_get_clock_by_type_with_latency(void *handle,
2716 enum amd_pp_clock_type type,
2717 struct pp_clock_levels_with_latency *clocks)
2719 struct smu_context *smu = handle;
2720 enum smu_clk_type clk_type;
2723 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2726 if (smu->ppt_funcs->get_clock_by_type_with_latency) {
2728 case amd_pp_sys_clock:
2729 clk_type = SMU_GFXCLK;
2731 case amd_pp_mem_clock:
2732 clk_type = SMU_MCLK;
2734 case amd_pp_dcef_clock:
2735 clk_type = SMU_DCEFCLK;
2737 case amd_pp_disp_clock:
2738 clk_type = SMU_DISPCLK;
2741 dev_err(smu->adev->dev, "Invalid clock type!\n");
2745 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2751 static int smu_display_clock_voltage_request(void *handle,
2752 struct pp_display_clock_request *clock_req)
2754 struct smu_context *smu = handle;
2757 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2760 if (smu->ppt_funcs->display_clock_voltage_request)
2761 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2767 static int smu_display_disable_memory_clock_switch(void *handle,
2768 bool disable_memory_clock_switch)
2770 struct smu_context *smu = handle;
2773 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
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);
2782 static int smu_set_xgmi_pstate(void *handle,
2785 struct smu_context *smu = handle;
2788 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2791 if (smu->ppt_funcs->set_xgmi_pstate)
2792 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2795 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
2800 static int smu_get_baco_capability(void *handle, bool *cap)
2802 struct smu_context *smu = handle;
2806 if (!smu->pm_enabled)
2809 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2810 *cap = smu->ppt_funcs->baco_is_support(smu);
2815 static int smu_baco_set_state(void *handle, int state)
2817 struct smu_context *smu = handle;
2820 if (!smu->pm_enabled)
2824 if (smu->ppt_funcs->baco_exit)
2825 ret = smu->ppt_funcs->baco_exit(smu);
2826 } else if (state == 1) {
2827 if (smu->ppt_funcs->baco_enter)
2828 ret = smu->ppt_funcs->baco_enter(smu);
2834 dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
2835 (state)?"enter":"exit");
2840 bool smu_mode1_reset_is_support(struct smu_context *smu)
2844 if (!smu->pm_enabled)
2847 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
2848 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
2853 bool smu_mode2_reset_is_support(struct smu_context *smu)
2857 if (!smu->pm_enabled)
2860 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
2861 ret = smu->ppt_funcs->mode2_reset_is_support(smu);
2866 int smu_mode1_reset(struct smu_context *smu)
2870 if (!smu->pm_enabled)
2873 if (smu->ppt_funcs->mode1_reset)
2874 ret = smu->ppt_funcs->mode1_reset(smu);
2879 static int smu_mode2_reset(void *handle)
2881 struct smu_context *smu = handle;
2884 if (!smu->pm_enabled)
2887 if (smu->ppt_funcs->mode2_reset)
2888 ret = smu->ppt_funcs->mode2_reset(smu);
2891 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
2896 static int smu_enable_gfx_features(void *handle)
2898 struct smu_context *smu = handle;
2901 if (!smu->pm_enabled)
2904 if (smu->ppt_funcs->enable_gfx_features)
2905 ret = smu->ppt_funcs->enable_gfx_features(smu);
2908 dev_err(smu->adev->dev, "enable gfx features failed!\n");
2913 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
2914 struct pp_smu_nv_clock_table *max_clocks)
2916 struct smu_context *smu = handle;
2919 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2922 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2923 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2928 static int smu_get_uclk_dpm_states(void *handle,
2929 unsigned int *clock_values_in_khz,
2930 unsigned int *num_states)
2932 struct smu_context *smu = handle;
2935 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2938 if (smu->ppt_funcs->get_uclk_dpm_states)
2939 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2944 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
2946 struct smu_context *smu = handle;
2947 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2949 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2952 if (smu->ppt_funcs->get_current_power_state)
2953 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2958 static int smu_get_dpm_clock_table(void *handle,
2959 struct dpm_clocks *clock_table)
2961 struct smu_context *smu = handle;
2964 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2967 if (smu->ppt_funcs->get_dpm_clock_table)
2968 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2973 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
2975 struct smu_context *smu = handle;
2977 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2980 if (!smu->ppt_funcs->get_gpu_metrics)
2983 return smu->ppt_funcs->get_gpu_metrics(smu, table);
2986 static int smu_enable_mgpu_fan_boost(void *handle)
2988 struct smu_context *smu = handle;
2991 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2994 if (smu->ppt_funcs->enable_mgpu_fan_boost)
2995 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
3000 static int smu_gfx_state_change_set(void *handle,
3003 struct smu_context *smu = handle;
3006 if (smu->ppt_funcs->gfx_state_change_set)
3007 ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
3012 int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)
3016 if (smu->ppt_funcs->smu_handle_passthrough_sbr)
3017 ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);
3022 int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)
3024 int ret = -EOPNOTSUPP;
3026 if (smu->ppt_funcs &&
3027 smu->ppt_funcs->get_ecc_info)
3028 ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);
3034 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
3036 struct smu_context *smu = handle;
3037 struct smu_table_context *smu_table = &smu->smu_table;
3038 struct smu_table *memory_pool = &smu_table->memory_pool;
3045 if (memory_pool->bo) {
3046 *addr = memory_pool->cpu_addr;
3047 *size = memory_pool->size;
3053 static const struct amd_pm_funcs swsmu_pm_funcs = {
3054 /* export for sysfs */
3055 .set_fan_control_mode = smu_set_fan_control_mode,
3056 .get_fan_control_mode = smu_get_fan_control_mode,
3057 .set_fan_speed_pwm = smu_set_fan_speed_pwm,
3058 .get_fan_speed_pwm = smu_get_fan_speed_pwm,
3059 .force_clock_level = smu_force_ppclk_levels,
3060 .print_clock_levels = smu_print_ppclk_levels,
3061 .emit_clock_levels = smu_emit_ppclk_levels,
3062 .force_performance_level = smu_force_performance_level,
3063 .read_sensor = smu_read_sensor,
3064 .get_apu_thermal_limit = smu_get_apu_thermal_limit,
3065 .set_apu_thermal_limit = smu_set_apu_thermal_limit,
3066 .get_performance_level = smu_get_performance_level,
3067 .get_current_power_state = smu_get_current_power_state,
3068 .get_fan_speed_rpm = smu_get_fan_speed_rpm,
3069 .set_fan_speed_rpm = smu_set_fan_speed_rpm,
3070 .get_pp_num_states = smu_get_power_num_states,
3071 .get_pp_table = smu_sys_get_pp_table,
3072 .set_pp_table = smu_sys_set_pp_table,
3073 .switch_power_profile = smu_switch_power_profile,
3074 /* export to amdgpu */
3075 .dispatch_tasks = smu_handle_dpm_task,
3076 .load_firmware = smu_load_microcode,
3077 .set_powergating_by_smu = smu_dpm_set_power_gate,
3078 .set_power_limit = smu_set_power_limit,
3079 .get_power_limit = smu_get_power_limit,
3080 .get_power_profile_mode = smu_get_power_profile_mode,
3081 .set_power_profile_mode = smu_set_power_profile_mode,
3082 .odn_edit_dpm_table = smu_od_edit_dpm_table,
3083 .set_mp1_state = smu_set_mp1_state,
3084 .gfx_state_change_set = smu_gfx_state_change_set,
3086 .get_sclk = smu_get_sclk,
3087 .get_mclk = smu_get_mclk,
3088 .display_configuration_change = smu_display_configuration_change,
3089 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,
3090 .display_clock_voltage_request = smu_display_clock_voltage_request,
3091 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,
3092 .set_active_display_count = smu_set_display_count,
3093 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,
3094 .get_asic_baco_capability = smu_get_baco_capability,
3095 .set_asic_baco_state = smu_baco_set_state,
3096 .get_ppfeature_status = smu_sys_get_pp_feature_mask,
3097 .set_ppfeature_status = smu_sys_set_pp_feature_mask,
3098 .asic_reset_mode_2 = smu_mode2_reset,
3099 .asic_reset_enable_gfx_features = smu_enable_gfx_features,
3100 .set_df_cstate = smu_set_df_cstate,
3101 .set_xgmi_pstate = smu_set_xgmi_pstate,
3102 .get_gpu_metrics = smu_sys_get_gpu_metrics,
3103 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,
3104 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
3105 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,
3106 .get_uclk_dpm_states = smu_get_uclk_dpm_states,
3107 .get_dpm_clock_table = smu_get_dpm_clock_table,
3108 .get_smu_prv_buf_details = smu_get_prv_buffer_details,
3111 int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,
3116 if (smu->ppt_funcs->wait_for_event)
3117 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
3122 int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)
3125 if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)
3128 /* Confirm the buffer allocated is of correct size */
3129 if (size != smu->stb_context.stb_buf_size)
3133 * No need to lock smu mutex as we access STB directly through MMIO
3134 * and not going through SMU messaging route (for now at least).
3135 * For registers access rely on implementation internal locking.
3137 return smu->ppt_funcs->stb_collect_info(smu, buf, size);
3140 #if defined(CONFIG_DEBUG_FS)
3142 static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)
3144 struct amdgpu_device *adev = filp->f_inode->i_private;
3145 struct smu_context *smu = adev->powerplay.pp_handle;
3149 buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);
3153 r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);
3157 filp->private_data = buf;
3166 static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
3169 struct amdgpu_device *adev = filp->f_inode->i_private;
3170 struct smu_context *smu = adev->powerplay.pp_handle;
3173 if (!filp->private_data)
3176 return simple_read_from_buffer(buf,
3178 pos, filp->private_data,
3179 smu->stb_context.stb_buf_size);
3182 static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)
3184 kvfree(filp->private_data);
3185 filp->private_data = NULL;
3191 * We have to define not only read method but also
3192 * open and release because .read takes up to PAGE_SIZE
3193 * data each time so and so is invoked multiple times.
3194 * We allocate the STB buffer in .open and release it
3197 static const struct file_operations smu_stb_debugfs_fops = {
3198 .owner = THIS_MODULE,
3199 .open = smu_stb_debugfs_open,
3200 .read = smu_stb_debugfs_read,
3201 .release = smu_stb_debugfs_release,
3202 .llseek = default_llseek,
3207 void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)
3209 #if defined(CONFIG_DEBUG_FS)
3211 struct smu_context *smu = adev->powerplay.pp_handle;
3213 if (!smu || (!smu->stb_context.stb_buf_size))
3216 debugfs_create_file_size("amdgpu_smu_stb_dump",
3218 adev_to_drm(adev)->primary->debugfs_root,
3220 &smu_stb_debugfs_fops,
3221 smu->stb_context.stb_buf_size);
3225 int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)
3229 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)
3230 ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);
3235 int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)
3239 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)
3240 ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);