1 /* 2 * Copyright 2019 Advanced Micro Devices, Inc. 3 * 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: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 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. 21 */ 22 23 #define SWSMU_CODE_LAYER_L1 24 25 #include <linux/firmware.h> 26 #include <linux/pci.h> 27 28 #include "amdgpu.h" 29 #include "amdgpu_smu.h" 30 #include "smu_internal.h" 31 #include "atom.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" 45 #include "amd_pcie.h" 46 47 /* 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. 51 */ 52 #undef pr_err 53 #undef pr_warn 54 #undef pr_info 55 #undef pr_debug 56 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, 60 uint32_t mask); 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); 71 72 static int smu_sys_get_pp_feature_mask(void *handle, 73 char *buf) 74 { 75 struct smu_context *smu = handle; 76 77 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 78 return -EOPNOTSUPP; 79 80 return smu_get_pp_feature_mask(smu, buf); 81 } 82 83 static int smu_sys_set_pp_feature_mask(void *handle, 84 uint64_t new_mask) 85 { 86 struct smu_context *smu = handle; 87 88 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 89 return -EOPNOTSUPP; 90 91 return smu_set_pp_feature_mask(smu, new_mask); 92 } 93 94 int smu_set_residency_gfxoff(struct smu_context *smu, bool value) 95 { 96 if (!smu->ppt_funcs->set_gfx_off_residency) 97 return -EINVAL; 98 99 return smu_set_gfx_off_residency(smu, value); 100 } 101 102 int smu_get_residency_gfxoff(struct smu_context *smu, u32 *value) 103 { 104 if (!smu->ppt_funcs->get_gfx_off_residency) 105 return -EINVAL; 106 107 return smu_get_gfx_off_residency(smu, value); 108 } 109 110 int smu_get_entrycount_gfxoff(struct smu_context *smu, u64 *value) 111 { 112 if (!smu->ppt_funcs->get_gfx_off_entrycount) 113 return -EINVAL; 114 115 return smu_get_gfx_off_entrycount(smu, value); 116 } 117 118 int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value) 119 { 120 if (!smu->ppt_funcs->get_gfx_off_status) 121 return -EINVAL; 122 123 *value = smu_get_gfx_off_status(smu); 124 125 return 0; 126 } 127 128 int smu_set_soft_freq_range(struct smu_context *smu, 129 enum smu_clk_type clk_type, 130 uint32_t min, 131 uint32_t max) 132 { 133 int ret = 0; 134 135 if (smu->ppt_funcs->set_soft_freq_limited_range) 136 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu, 137 clk_type, 138 min, 139 max); 140 141 return ret; 142 } 143 144 int smu_get_dpm_freq_range(struct smu_context *smu, 145 enum smu_clk_type clk_type, 146 uint32_t *min, 147 uint32_t *max) 148 { 149 int ret = -ENOTSUPP; 150 151 if (!min && !max) 152 return -EINVAL; 153 154 if (smu->ppt_funcs->get_dpm_ultimate_freq) 155 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu, 156 clk_type, 157 min, 158 max); 159 160 return ret; 161 } 162 163 int smu_set_gfx_power_up_by_imu(struct smu_context *smu) 164 { 165 int ret = 0; 166 struct amdgpu_device *adev = smu->adev; 167 168 if (smu->ppt_funcs->set_gfx_power_up_by_imu) { 169 ret = smu->ppt_funcs->set_gfx_power_up_by_imu(smu); 170 if (ret) 171 dev_err(adev->dev, "Failed to enable gfx imu!\n"); 172 } 173 return ret; 174 } 175 176 static u32 smu_get_mclk(void *handle, bool low) 177 { 178 struct smu_context *smu = handle; 179 uint32_t clk_freq; 180 int ret = 0; 181 182 ret = smu_get_dpm_freq_range(smu, SMU_UCLK, 183 low ? &clk_freq : NULL, 184 !low ? &clk_freq : NULL); 185 if (ret) 186 return 0; 187 return clk_freq * 100; 188 } 189 190 static u32 smu_get_sclk(void *handle, bool low) 191 { 192 struct smu_context *smu = handle; 193 uint32_t clk_freq; 194 int ret = 0; 195 196 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK, 197 low ? &clk_freq : NULL, 198 !low ? &clk_freq : NULL); 199 if (ret) 200 return 0; 201 return clk_freq * 100; 202 } 203 204 static int smu_set_gfx_imu_enable(struct smu_context *smu) 205 { 206 struct amdgpu_device *adev = smu->adev; 207 208 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) 209 return 0; 210 211 if (amdgpu_in_reset(smu->adev) || adev->in_s0ix) 212 return 0; 213 214 return smu_set_gfx_power_up_by_imu(smu); 215 } 216 217 static int smu_dpm_set_vcn_enable(struct smu_context *smu, 218 bool enable) 219 { 220 struct smu_power_context *smu_power = &smu->smu_power; 221 struct smu_power_gate *power_gate = &smu_power->power_gate; 222 int ret = 0; 223 224 if (!smu->ppt_funcs->dpm_set_vcn_enable) 225 return 0; 226 227 if (atomic_read(&power_gate->vcn_gated) ^ enable) 228 return 0; 229 230 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable); 231 if (!ret) 232 atomic_set(&power_gate->vcn_gated, !enable); 233 234 return ret; 235 } 236 237 static int smu_dpm_set_jpeg_enable(struct smu_context *smu, 238 bool enable) 239 { 240 struct smu_power_context *smu_power = &smu->smu_power; 241 struct smu_power_gate *power_gate = &smu_power->power_gate; 242 int ret = 0; 243 244 if (!smu->ppt_funcs->dpm_set_jpeg_enable) 245 return 0; 246 247 if (atomic_read(&power_gate->jpeg_gated) ^ enable) 248 return 0; 249 250 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable); 251 if (!ret) 252 atomic_set(&power_gate->jpeg_gated, !enable); 253 254 return ret; 255 } 256 257 /** 258 * smu_dpm_set_power_gate - power gate/ungate the specific IP block 259 * 260 * @handle: smu_context pointer 261 * @block_type: the IP block to power gate/ungate 262 * @gate: to power gate if true, ungate otherwise 263 * 264 * This API uses no smu->mutex lock protection due to: 265 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce). 266 * This is guarded to be race condition free by the caller. 267 * 2. Or get called on user setting request of power_dpm_force_performance_level. 268 * Under this case, the smu->mutex lock protection is already enforced on 269 * the parent API smu_force_performance_level of the call path. 270 */ 271 static int smu_dpm_set_power_gate(void *handle, 272 uint32_t block_type, 273 bool gate) 274 { 275 struct smu_context *smu = handle; 276 int ret = 0; 277 278 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) { 279 dev_WARN(smu->adev->dev, 280 "SMU uninitialized but power %s requested for %u!\n", 281 gate ? "gate" : "ungate", block_type); 282 return -EOPNOTSUPP; 283 } 284 285 switch (block_type) { 286 /* 287 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses 288 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept. 289 */ 290 case AMD_IP_BLOCK_TYPE_UVD: 291 case AMD_IP_BLOCK_TYPE_VCN: 292 ret = smu_dpm_set_vcn_enable(smu, !gate); 293 if (ret) 294 dev_err(smu->adev->dev, "Failed to power %s VCN!\n", 295 gate ? "gate" : "ungate"); 296 break; 297 case AMD_IP_BLOCK_TYPE_GFX: 298 ret = smu_gfx_off_control(smu, gate); 299 if (ret) 300 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n", 301 gate ? "enable" : "disable"); 302 break; 303 case AMD_IP_BLOCK_TYPE_SDMA: 304 ret = smu_powergate_sdma(smu, gate); 305 if (ret) 306 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n", 307 gate ? "gate" : "ungate"); 308 break; 309 case AMD_IP_BLOCK_TYPE_JPEG: 310 ret = smu_dpm_set_jpeg_enable(smu, !gate); 311 if (ret) 312 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n", 313 gate ? "gate" : "ungate"); 314 break; 315 default: 316 dev_err(smu->adev->dev, "Unsupported block type!\n"); 317 return -EINVAL; 318 } 319 320 return ret; 321 } 322 323 /** 324 * smu_set_user_clk_dependencies - set user profile clock dependencies 325 * 326 * @smu: smu_context pointer 327 * @clk: enum smu_clk_type type 328 * 329 * Enable/Disable the clock dependency for the @clk type. 330 */ 331 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk) 332 { 333 if (smu->adev->in_suspend) 334 return; 335 336 if (clk == SMU_MCLK) { 337 smu->user_dpm_profile.clk_dependency = 0; 338 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK); 339 } else if (clk == SMU_FCLK) { 340 /* MCLK takes precedence over FCLK */ 341 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK))) 342 return; 343 344 smu->user_dpm_profile.clk_dependency = 0; 345 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK); 346 } else if (clk == SMU_SOCCLK) { 347 /* MCLK takes precedence over SOCCLK */ 348 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK))) 349 return; 350 351 smu->user_dpm_profile.clk_dependency = 0; 352 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK); 353 } else 354 /* Add clk dependencies here, if any */ 355 return; 356 } 357 358 /** 359 * smu_restore_dpm_user_profile - reinstate user dpm profile 360 * 361 * @smu: smu_context pointer 362 * 363 * Restore the saved user power configurations include power limit, 364 * clock frequencies, fan control mode and fan speed. 365 */ 366 static void smu_restore_dpm_user_profile(struct smu_context *smu) 367 { 368 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 369 int ret = 0; 370 371 if (!smu->adev->in_suspend) 372 return; 373 374 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 375 return; 376 377 /* Enable restore flag */ 378 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE; 379 380 /* set the user dpm power limit */ 381 if (smu->user_dpm_profile.power_limit) { 382 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit); 383 if (ret) 384 dev_err(smu->adev->dev, "Failed to set power limit value\n"); 385 } 386 387 /* set the user dpm clock configurations */ 388 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) { 389 enum smu_clk_type clk_type; 390 391 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) { 392 /* 393 * Iterate over smu clk type and force the saved user clk 394 * configs, skip if clock dependency is enabled 395 */ 396 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) && 397 smu->user_dpm_profile.clk_mask[clk_type]) { 398 ret = smu_force_smuclk_levels(smu, clk_type, 399 smu->user_dpm_profile.clk_mask[clk_type]); 400 if (ret) 401 dev_err(smu->adev->dev, 402 "Failed to set clock type = %d\n", clk_type); 403 } 404 } 405 } 406 407 /* set the user dpm fan configurations */ 408 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL || 409 smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) { 410 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode); 411 if (ret != -EOPNOTSUPP) { 412 smu->user_dpm_profile.fan_speed_pwm = 0; 413 smu->user_dpm_profile.fan_speed_rpm = 0; 414 smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO; 415 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n"); 416 } 417 418 if (smu->user_dpm_profile.fan_speed_pwm) { 419 ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm); 420 if (ret != -EOPNOTSUPP) 421 dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n"); 422 } 423 424 if (smu->user_dpm_profile.fan_speed_rpm) { 425 ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm); 426 if (ret != -EOPNOTSUPP) 427 dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n"); 428 } 429 } 430 431 /* Restore user customized OD settings */ 432 if (smu->user_dpm_profile.user_od) { 433 if (smu->ppt_funcs->restore_user_od_settings) { 434 ret = smu->ppt_funcs->restore_user_od_settings(smu); 435 if (ret) 436 dev_err(smu->adev->dev, "Failed to upload customized OD settings\n"); 437 } 438 } 439 440 /* Disable restore flag */ 441 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE; 442 } 443 444 static int smu_get_power_num_states(void *handle, 445 struct pp_states_info *state_info) 446 { 447 if (!state_info) 448 return -EINVAL; 449 450 /* not support power state */ 451 memset(state_info, 0, sizeof(struct pp_states_info)); 452 state_info->nums = 1; 453 state_info->states[0] = POWER_STATE_TYPE_DEFAULT; 454 455 return 0; 456 } 457 458 bool is_support_sw_smu(struct amdgpu_device *adev) 459 { 460 /* vega20 is 11.0.2, but it's supported via the powerplay code */ 461 if (adev->asic_type == CHIP_VEGA20) 462 return false; 463 464 if (adev->ip_versions[MP1_HWIP][0] >= IP_VERSION(11, 0, 0)) 465 return true; 466 467 return false; 468 } 469 470 bool is_support_cclk_dpm(struct amdgpu_device *adev) 471 { 472 struct smu_context *smu = adev->powerplay.pp_handle; 473 474 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT)) 475 return false; 476 477 return true; 478 } 479 480 481 static int smu_sys_get_pp_table(void *handle, 482 char **table) 483 { 484 struct smu_context *smu = handle; 485 struct smu_table_context *smu_table = &smu->smu_table; 486 487 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 488 return -EOPNOTSUPP; 489 490 if (!smu_table->power_play_table && !smu_table->hardcode_pptable) 491 return -EINVAL; 492 493 if (smu_table->hardcode_pptable) 494 *table = smu_table->hardcode_pptable; 495 else 496 *table = smu_table->power_play_table; 497 498 return smu_table->power_play_table_size; 499 } 500 501 static int smu_sys_set_pp_table(void *handle, 502 const char *buf, 503 size_t size) 504 { 505 struct smu_context *smu = handle; 506 struct smu_table_context *smu_table = &smu->smu_table; 507 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf; 508 int ret = 0; 509 510 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 511 return -EOPNOTSUPP; 512 513 if (header->usStructureSize != size) { 514 dev_err(smu->adev->dev, "pp table size not matched !\n"); 515 return -EIO; 516 } 517 518 if (!smu_table->hardcode_pptable) { 519 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL); 520 if (!smu_table->hardcode_pptable) 521 return -ENOMEM; 522 } 523 524 memcpy(smu_table->hardcode_pptable, buf, size); 525 smu_table->power_play_table = smu_table->hardcode_pptable; 526 smu_table->power_play_table_size = size; 527 528 /* 529 * Special hw_fini action(for Navi1x, the DPMs disablement will be 530 * skipped) may be needed for custom pptable uploading. 531 */ 532 smu->uploading_custom_pp_table = true; 533 534 ret = smu_reset(smu); 535 if (ret) 536 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret); 537 538 smu->uploading_custom_pp_table = false; 539 540 return ret; 541 } 542 543 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu) 544 { 545 struct smu_feature *feature = &smu->smu_feature; 546 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32]; 547 int ret = 0; 548 549 /* 550 * With SCPM enabled, the allowed featuremasks setting(via 551 * PPSMC_MSG_SetAllowedFeaturesMaskLow/High) is not permitted. 552 * That means there is no way to let PMFW knows the settings below. 553 * Thus, we just assume all the features are allowed under 554 * such scenario. 555 */ 556 if (smu->adev->scpm_enabled) { 557 bitmap_fill(feature->allowed, SMU_FEATURE_MAX); 558 return 0; 559 } 560 561 bitmap_zero(feature->allowed, SMU_FEATURE_MAX); 562 563 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask, 564 SMU_FEATURE_MAX/32); 565 if (ret) 566 return ret; 567 568 bitmap_or(feature->allowed, feature->allowed, 569 (unsigned long *)allowed_feature_mask, 570 feature->feature_num); 571 572 return ret; 573 } 574 575 static int smu_set_funcs(struct amdgpu_device *adev) 576 { 577 struct smu_context *smu = adev->powerplay.pp_handle; 578 579 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) 580 smu->od_enabled = true; 581 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); 587 break; 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); 593 break; 594 case IP_VERSION(12, 0, 0): 595 case IP_VERSION(12, 0, 1): 596 renoir_set_ppt_funcs(smu); 597 break; 598 case IP_VERSION(11, 5, 0): 599 vangogh_set_ppt_funcs(smu); 600 break; 601 case IP_VERSION(13, 0, 1): 602 case IP_VERSION(13, 0, 3): 603 case IP_VERSION(13, 0, 8): 604 yellow_carp_set_ppt_funcs(smu); 605 break; 606 case IP_VERSION(13, 0, 4): 607 case IP_VERSION(13, 0, 11): 608 smu_v13_0_4_set_ppt_funcs(smu); 609 break; 610 case IP_VERSION(13, 0, 5): 611 smu_v13_0_5_set_ppt_funcs(smu); 612 break; 613 case IP_VERSION(11, 0, 8): 614 cyan_skillfish_set_ppt_funcs(smu); 615 break; 616 case IP_VERSION(11, 0, 2): 617 adev->pm.pp_feature &= ~PP_GFXOFF_MASK; 618 arcturus_set_ppt_funcs(smu); 619 /* OD is not supported on Arcturus */ 620 smu->od_enabled =false; 621 break; 622 case IP_VERSION(13, 0, 2): 623 aldebaran_set_ppt_funcs(smu); 624 /* Enable pp_od_clk_voltage node */ 625 smu->od_enabled = true; 626 break; 627 case IP_VERSION(13, 0, 0): 628 case IP_VERSION(13, 0, 10): 629 smu_v13_0_0_set_ppt_funcs(smu); 630 break; 631 case IP_VERSION(13, 0, 6): 632 smu_v13_0_6_set_ppt_funcs(smu); 633 /* Enable pp_od_clk_voltage node */ 634 smu->od_enabled = true; 635 break; 636 case IP_VERSION(13, 0, 7): 637 smu_v13_0_7_set_ppt_funcs(smu); 638 break; 639 default: 640 return -EINVAL; 641 } 642 643 return 0; 644 } 645 646 static int smu_early_init(void *handle) 647 { 648 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 649 struct smu_context *smu; 650 int r; 651 652 smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL); 653 if (!smu) 654 return -ENOMEM; 655 656 smu->adev = adev; 657 smu->pm_enabled = !!amdgpu_dpm; 658 smu->is_apu = false; 659 smu->smu_baco.state = SMU_BACO_STATE_EXIT; 660 smu->smu_baco.platform_support = false; 661 smu->user_dpm_profile.fan_mode = -1; 662 663 mutex_init(&smu->message_lock); 664 665 adev->powerplay.pp_handle = smu; 666 adev->powerplay.pp_funcs = &swsmu_pm_funcs; 667 668 r = smu_set_funcs(adev); 669 if (r) 670 return r; 671 return smu_init_microcode(smu); 672 } 673 674 static int smu_set_default_dpm_table(struct smu_context *smu) 675 { 676 struct smu_power_context *smu_power = &smu->smu_power; 677 struct smu_power_gate *power_gate = &smu_power->power_gate; 678 int vcn_gate, jpeg_gate; 679 int ret = 0; 680 681 if (!smu->ppt_funcs->set_default_dpm_table) 682 return 0; 683 684 vcn_gate = atomic_read(&power_gate->vcn_gated); 685 jpeg_gate = atomic_read(&power_gate->jpeg_gated); 686 687 ret = smu_dpm_set_vcn_enable(smu, true); 688 if (ret) 689 return ret; 690 691 ret = smu_dpm_set_jpeg_enable(smu, true); 692 if (ret) 693 goto err_out; 694 695 ret = smu->ppt_funcs->set_default_dpm_table(smu); 696 if (ret) 697 dev_err(smu->adev->dev, 698 "Failed to setup default dpm clock tables!\n"); 699 700 smu_dpm_set_jpeg_enable(smu, !jpeg_gate); 701 err_out: 702 smu_dpm_set_vcn_enable(smu, !vcn_gate); 703 return ret; 704 } 705 706 static int smu_apply_default_config_table_settings(struct smu_context *smu) 707 { 708 struct amdgpu_device *adev = smu->adev; 709 int ret = 0; 710 711 ret = smu_get_default_config_table_settings(smu, 712 &adev->pm.config_table); 713 if (ret) 714 return ret; 715 716 return smu_set_config_table(smu, &adev->pm.config_table); 717 } 718 719 static int smu_late_init(void *handle) 720 { 721 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 722 struct smu_context *smu = adev->powerplay.pp_handle; 723 int ret = 0; 724 725 smu_set_fine_grain_gfx_freq_parameters(smu); 726 727 if (!smu->pm_enabled) 728 return 0; 729 730 ret = smu_post_init(smu); 731 if (ret) { 732 dev_err(adev->dev, "Failed to post smu init!\n"); 733 return ret; 734 } 735 736 if ((adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 1)) || 737 (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 3))) 738 return 0; 739 740 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) { 741 ret = smu_set_default_od_settings(smu); 742 if (ret) { 743 dev_err(adev->dev, "Failed to setup default OD settings!\n"); 744 return ret; 745 } 746 } 747 748 ret = smu_populate_umd_state_clk(smu); 749 if (ret) { 750 dev_err(adev->dev, "Failed to populate UMD state clocks!\n"); 751 return ret; 752 } 753 754 ret = smu_get_asic_power_limits(smu, 755 &smu->current_power_limit, 756 &smu->default_power_limit, 757 &smu->max_power_limit); 758 if (ret) { 759 dev_err(adev->dev, "Failed to get asic power limits!\n"); 760 return ret; 761 } 762 763 if (!amdgpu_sriov_vf(adev)) 764 smu_get_unique_id(smu); 765 766 smu_get_fan_parameters(smu); 767 768 smu_handle_task(smu, 769 smu->smu_dpm.dpm_level, 770 AMD_PP_TASK_COMPLETE_INIT); 771 772 ret = smu_apply_default_config_table_settings(smu); 773 if (ret && (ret != -EOPNOTSUPP)) { 774 dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n"); 775 return ret; 776 } 777 778 smu_restore_dpm_user_profile(smu); 779 780 return 0; 781 } 782 783 static int smu_init_fb_allocations(struct smu_context *smu) 784 { 785 struct amdgpu_device *adev = smu->adev; 786 struct smu_table_context *smu_table = &smu->smu_table; 787 struct smu_table *tables = smu_table->tables; 788 struct smu_table *driver_table = &(smu_table->driver_table); 789 uint32_t max_table_size = 0; 790 int ret, i; 791 792 /* VRAM allocation for tool table */ 793 if (tables[SMU_TABLE_PMSTATUSLOG].size) { 794 ret = amdgpu_bo_create_kernel(adev, 795 tables[SMU_TABLE_PMSTATUSLOG].size, 796 tables[SMU_TABLE_PMSTATUSLOG].align, 797 tables[SMU_TABLE_PMSTATUSLOG].domain, 798 &tables[SMU_TABLE_PMSTATUSLOG].bo, 799 &tables[SMU_TABLE_PMSTATUSLOG].mc_address, 800 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr); 801 if (ret) { 802 dev_err(adev->dev, "VRAM allocation for tool table failed!\n"); 803 return ret; 804 } 805 } 806 807 /* VRAM allocation for driver table */ 808 for (i = 0; i < SMU_TABLE_COUNT; i++) { 809 if (tables[i].size == 0) 810 continue; 811 812 if (i == SMU_TABLE_PMSTATUSLOG) 813 continue; 814 815 if (max_table_size < tables[i].size) 816 max_table_size = tables[i].size; 817 } 818 819 driver_table->size = max_table_size; 820 driver_table->align = PAGE_SIZE; 821 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM; 822 823 ret = amdgpu_bo_create_kernel(adev, 824 driver_table->size, 825 driver_table->align, 826 driver_table->domain, 827 &driver_table->bo, 828 &driver_table->mc_address, 829 &driver_table->cpu_addr); 830 if (ret) { 831 dev_err(adev->dev, "VRAM allocation for driver table failed!\n"); 832 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address) 833 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo, 834 &tables[SMU_TABLE_PMSTATUSLOG].mc_address, 835 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr); 836 } 837 838 return ret; 839 } 840 841 static int smu_fini_fb_allocations(struct smu_context *smu) 842 { 843 struct smu_table_context *smu_table = &smu->smu_table; 844 struct smu_table *tables = smu_table->tables; 845 struct smu_table *driver_table = &(smu_table->driver_table); 846 847 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address) 848 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo, 849 &tables[SMU_TABLE_PMSTATUSLOG].mc_address, 850 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr); 851 852 amdgpu_bo_free_kernel(&driver_table->bo, 853 &driver_table->mc_address, 854 &driver_table->cpu_addr); 855 856 return 0; 857 } 858 859 /** 860 * smu_alloc_memory_pool - allocate memory pool in the system memory 861 * 862 * @smu: amdgpu_device pointer 863 * 864 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr 865 * and DramLogSetDramAddr can notify it changed. 866 * 867 * Returns 0 on success, error on failure. 868 */ 869 static int smu_alloc_memory_pool(struct smu_context *smu) 870 { 871 struct amdgpu_device *adev = smu->adev; 872 struct smu_table_context *smu_table = &smu->smu_table; 873 struct smu_table *memory_pool = &smu_table->memory_pool; 874 uint64_t pool_size = smu->pool_size; 875 int ret = 0; 876 877 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO) 878 return ret; 879 880 memory_pool->size = pool_size; 881 memory_pool->align = PAGE_SIZE; 882 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT; 883 884 switch (pool_size) { 885 case SMU_MEMORY_POOL_SIZE_256_MB: 886 case SMU_MEMORY_POOL_SIZE_512_MB: 887 case SMU_MEMORY_POOL_SIZE_1_GB: 888 case SMU_MEMORY_POOL_SIZE_2_GB: 889 ret = amdgpu_bo_create_kernel(adev, 890 memory_pool->size, 891 memory_pool->align, 892 memory_pool->domain, 893 &memory_pool->bo, 894 &memory_pool->mc_address, 895 &memory_pool->cpu_addr); 896 if (ret) 897 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n"); 898 break; 899 default: 900 break; 901 } 902 903 return ret; 904 } 905 906 static int smu_free_memory_pool(struct smu_context *smu) 907 { 908 struct smu_table_context *smu_table = &smu->smu_table; 909 struct smu_table *memory_pool = &smu_table->memory_pool; 910 911 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO) 912 return 0; 913 914 amdgpu_bo_free_kernel(&memory_pool->bo, 915 &memory_pool->mc_address, 916 &memory_pool->cpu_addr); 917 918 memset(memory_pool, 0, sizeof(struct smu_table)); 919 920 return 0; 921 } 922 923 static int smu_alloc_dummy_read_table(struct smu_context *smu) 924 { 925 struct smu_table_context *smu_table = &smu->smu_table; 926 struct smu_table *dummy_read_1_table = 927 &smu_table->dummy_read_1_table; 928 struct amdgpu_device *adev = smu->adev; 929 int ret = 0; 930 931 if (!dummy_read_1_table->size) 932 return 0; 933 934 ret = amdgpu_bo_create_kernel(adev, 935 dummy_read_1_table->size, 936 dummy_read_1_table->align, 937 dummy_read_1_table->domain, 938 &dummy_read_1_table->bo, 939 &dummy_read_1_table->mc_address, 940 &dummy_read_1_table->cpu_addr); 941 if (ret) 942 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n"); 943 944 return ret; 945 } 946 947 static void smu_free_dummy_read_table(struct smu_context *smu) 948 { 949 struct smu_table_context *smu_table = &smu->smu_table; 950 struct smu_table *dummy_read_1_table = 951 &smu_table->dummy_read_1_table; 952 953 954 amdgpu_bo_free_kernel(&dummy_read_1_table->bo, 955 &dummy_read_1_table->mc_address, 956 &dummy_read_1_table->cpu_addr); 957 958 memset(dummy_read_1_table, 0, sizeof(struct smu_table)); 959 } 960 961 static int smu_smc_table_sw_init(struct smu_context *smu) 962 { 963 int ret; 964 965 /** 966 * Create smu_table structure, and init smc tables such as 967 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc. 968 */ 969 ret = smu_init_smc_tables(smu); 970 if (ret) { 971 dev_err(smu->adev->dev, "Failed to init smc tables!\n"); 972 return ret; 973 } 974 975 /** 976 * Create smu_power_context structure, and allocate smu_dpm_context and 977 * context size to fill the smu_power_context data. 978 */ 979 ret = smu_init_power(smu); 980 if (ret) { 981 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n"); 982 return ret; 983 } 984 985 /* 986 * allocate vram bos to store smc table contents. 987 */ 988 ret = smu_init_fb_allocations(smu); 989 if (ret) 990 return ret; 991 992 ret = smu_alloc_memory_pool(smu); 993 if (ret) 994 return ret; 995 996 ret = smu_alloc_dummy_read_table(smu); 997 if (ret) 998 return ret; 999 1000 ret = smu_i2c_init(smu); 1001 if (ret) 1002 return ret; 1003 1004 return 0; 1005 } 1006 1007 static int smu_smc_table_sw_fini(struct smu_context *smu) 1008 { 1009 int ret; 1010 1011 smu_i2c_fini(smu); 1012 1013 smu_free_dummy_read_table(smu); 1014 1015 ret = smu_free_memory_pool(smu); 1016 if (ret) 1017 return ret; 1018 1019 ret = smu_fini_fb_allocations(smu); 1020 if (ret) 1021 return ret; 1022 1023 ret = smu_fini_power(smu); 1024 if (ret) { 1025 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n"); 1026 return ret; 1027 } 1028 1029 ret = smu_fini_smc_tables(smu); 1030 if (ret) { 1031 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n"); 1032 return ret; 1033 } 1034 1035 return 0; 1036 } 1037 1038 static void smu_throttling_logging_work_fn(struct work_struct *work) 1039 { 1040 struct smu_context *smu = container_of(work, struct smu_context, 1041 throttling_logging_work); 1042 1043 smu_log_thermal_throttling(smu); 1044 } 1045 1046 static void smu_interrupt_work_fn(struct work_struct *work) 1047 { 1048 struct smu_context *smu = container_of(work, struct smu_context, 1049 interrupt_work); 1050 1051 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work) 1052 smu->ppt_funcs->interrupt_work(smu); 1053 } 1054 1055 static int smu_sw_init(void *handle) 1056 { 1057 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1058 struct smu_context *smu = adev->powerplay.pp_handle; 1059 int ret; 1060 1061 smu->pool_size = adev->pm.smu_prv_buffer_size; 1062 smu->smu_feature.feature_num = SMU_FEATURE_MAX; 1063 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX); 1064 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX); 1065 1066 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn); 1067 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn); 1068 atomic64_set(&smu->throttle_int_counter, 0); 1069 smu->watermarks_bitmap = 0; 1070 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT; 1071 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT; 1072 1073 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1); 1074 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1); 1075 1076 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT]; 1077 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0; 1078 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1; 1079 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2; 1080 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3; 1081 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4; 1082 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5; 1083 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6; 1084 1085 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT; 1086 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D; 1087 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING; 1088 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO; 1089 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR; 1090 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE; 1091 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM; 1092 smu->display_config = &adev->pm.pm_display_cfg; 1093 1094 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO; 1095 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO; 1096 1097 ret = smu_smc_table_sw_init(smu); 1098 if (ret) { 1099 dev_err(adev->dev, "Failed to sw init smc table!\n"); 1100 return ret; 1101 } 1102 1103 /* get boot_values from vbios to set revision, gfxclk, and etc. */ 1104 ret = smu_get_vbios_bootup_values(smu); 1105 if (ret) { 1106 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n"); 1107 return ret; 1108 } 1109 1110 ret = smu_init_pptable_microcode(smu); 1111 if (ret) { 1112 dev_err(adev->dev, "Failed to setup pptable firmware!\n"); 1113 return ret; 1114 } 1115 1116 ret = smu_register_irq_handler(smu); 1117 if (ret) { 1118 dev_err(adev->dev, "Failed to register smc irq handler!\n"); 1119 return ret; 1120 } 1121 1122 /* If there is no way to query fan control mode, fan control is not supported */ 1123 if (!smu->ppt_funcs->get_fan_control_mode) 1124 smu->adev->pm.no_fan = true; 1125 1126 return 0; 1127 } 1128 1129 static int smu_sw_fini(void *handle) 1130 { 1131 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1132 struct smu_context *smu = adev->powerplay.pp_handle; 1133 int ret; 1134 1135 ret = smu_smc_table_sw_fini(smu); 1136 if (ret) { 1137 dev_err(adev->dev, "Failed to sw fini smc table!\n"); 1138 return ret; 1139 } 1140 1141 smu_fini_microcode(smu); 1142 1143 return 0; 1144 } 1145 1146 static int smu_get_thermal_temperature_range(struct smu_context *smu) 1147 { 1148 struct amdgpu_device *adev = smu->adev; 1149 struct smu_temperature_range *range = 1150 &smu->thermal_range; 1151 int ret = 0; 1152 1153 if (!smu->ppt_funcs->get_thermal_temperature_range) 1154 return 0; 1155 1156 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range); 1157 if (ret) 1158 return ret; 1159 1160 adev->pm.dpm.thermal.min_temp = range->min; 1161 adev->pm.dpm.thermal.max_temp = range->max; 1162 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max; 1163 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min; 1164 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max; 1165 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max; 1166 adev->pm.dpm.thermal.min_mem_temp = range->mem_min; 1167 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max; 1168 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max; 1169 1170 return ret; 1171 } 1172 1173 static int smu_smc_hw_setup(struct smu_context *smu) 1174 { 1175 struct smu_feature *feature = &smu->smu_feature; 1176 struct amdgpu_device *adev = smu->adev; 1177 uint32_t pcie_gen = 0, pcie_width = 0; 1178 uint64_t features_supported; 1179 int ret = 0; 1180 1181 switch (adev->ip_versions[MP1_HWIP][0]) { 1182 case IP_VERSION(11, 0, 7): 1183 case IP_VERSION(11, 0, 11): 1184 case IP_VERSION(11, 5, 0): 1185 case IP_VERSION(11, 0, 12): 1186 if (adev->in_suspend && smu_is_dpm_running(smu)) { 1187 dev_info(adev->dev, "dpm has been enabled\n"); 1188 ret = smu_system_features_control(smu, true); 1189 if (ret) 1190 dev_err(adev->dev, "Failed system features control!\n"); 1191 return ret; 1192 } 1193 break; 1194 default: 1195 break; 1196 } 1197 1198 ret = smu_init_display_count(smu, 0); 1199 if (ret) { 1200 dev_info(adev->dev, "Failed to pre-set display count as 0!\n"); 1201 return ret; 1202 } 1203 1204 ret = smu_set_driver_table_location(smu); 1205 if (ret) { 1206 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n"); 1207 return ret; 1208 } 1209 1210 /* 1211 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools. 1212 */ 1213 ret = smu_set_tool_table_location(smu); 1214 if (ret) { 1215 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n"); 1216 return ret; 1217 } 1218 1219 /* 1220 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify 1221 * pool location. 1222 */ 1223 ret = smu_notify_memory_pool_location(smu); 1224 if (ret) { 1225 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n"); 1226 return ret; 1227 } 1228 1229 /* 1230 * It is assumed the pptable used before runpm is same as 1231 * the one used afterwards. Thus, we can reuse the stored 1232 * copy and do not need to resetup the pptable again. 1233 */ 1234 if (!adev->in_runpm) { 1235 ret = smu_setup_pptable(smu); 1236 if (ret) { 1237 dev_err(adev->dev, "Failed to setup pptable!\n"); 1238 return ret; 1239 } 1240 } 1241 1242 /* smu_dump_pptable(smu); */ 1243 1244 /* 1245 * With SCPM enabled, PSP is responsible for the PPTable transferring 1246 * (to SMU). Driver involvement is not needed and permitted. 1247 */ 1248 if (!adev->scpm_enabled) { 1249 /* 1250 * Copy pptable bo in the vram to smc with SMU MSGs such as 1251 * SetDriverDramAddr and TransferTableDram2Smu. 1252 */ 1253 ret = smu_write_pptable(smu); 1254 if (ret) { 1255 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n"); 1256 return ret; 1257 } 1258 } 1259 1260 /* issue Run*Btc msg */ 1261 ret = smu_run_btc(smu); 1262 if (ret) 1263 return ret; 1264 1265 /* 1266 * With SCPM enabled, these actions(and relevant messages) are 1267 * not needed and permitted. 1268 */ 1269 if (!adev->scpm_enabled) { 1270 ret = smu_feature_set_allowed_mask(smu); 1271 if (ret) { 1272 dev_err(adev->dev, "Failed to set driver allowed features mask!\n"); 1273 return ret; 1274 } 1275 } 1276 1277 ret = smu_system_features_control(smu, true); 1278 if (ret) { 1279 dev_err(adev->dev, "Failed to enable requested dpm features!\n"); 1280 return ret; 1281 } 1282 1283 ret = smu_feature_get_enabled_mask(smu, &features_supported); 1284 if (ret) { 1285 dev_err(adev->dev, "Failed to retrieve supported dpm features!\n"); 1286 return ret; 1287 } 1288 bitmap_copy(feature->supported, 1289 (unsigned long *)&features_supported, 1290 feature->feature_num); 1291 1292 if (!smu_is_dpm_running(smu)) 1293 dev_info(adev->dev, "dpm has been disabled\n"); 1294 1295 /* 1296 * Set initialized values (get from vbios) to dpm tables context such as 1297 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each 1298 * type of clks. 1299 */ 1300 ret = smu_set_default_dpm_table(smu); 1301 if (ret) { 1302 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n"); 1303 return ret; 1304 } 1305 1306 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4) 1307 pcie_gen = 3; 1308 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3) 1309 pcie_gen = 2; 1310 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2) 1311 pcie_gen = 1; 1312 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1) 1313 pcie_gen = 0; 1314 1315 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1 1316 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4 1317 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32 1318 */ 1319 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16) 1320 pcie_width = 6; 1321 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12) 1322 pcie_width = 5; 1323 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8) 1324 pcie_width = 4; 1325 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4) 1326 pcie_width = 3; 1327 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2) 1328 pcie_width = 2; 1329 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1) 1330 pcie_width = 1; 1331 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width); 1332 if (ret) { 1333 dev_err(adev->dev, "Attempt to override pcie params failed!\n"); 1334 return ret; 1335 } 1336 1337 ret = smu_get_thermal_temperature_range(smu); 1338 if (ret) { 1339 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n"); 1340 return ret; 1341 } 1342 1343 ret = smu_enable_thermal_alert(smu); 1344 if (ret) { 1345 dev_err(adev->dev, "Failed to enable thermal alert!\n"); 1346 return ret; 1347 } 1348 1349 ret = smu_notify_display_change(smu); 1350 if (ret) { 1351 dev_err(adev->dev, "Failed to notify display change!\n"); 1352 return ret; 1353 } 1354 1355 /* 1356 * Set min deep sleep dce fclk with bootup value from vbios via 1357 * SetMinDeepSleepDcefclk MSG. 1358 */ 1359 ret = smu_set_min_dcef_deep_sleep(smu, 1360 smu->smu_table.boot_values.dcefclk / 100); 1361 1362 return ret; 1363 } 1364 1365 static int smu_start_smc_engine(struct smu_context *smu) 1366 { 1367 struct amdgpu_device *adev = smu->adev; 1368 int ret = 0; 1369 1370 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 1371 if (adev->ip_versions[MP1_HWIP][0] < IP_VERSION(11, 0, 0)) { 1372 if (smu->ppt_funcs->load_microcode) { 1373 ret = smu->ppt_funcs->load_microcode(smu); 1374 if (ret) 1375 return ret; 1376 } 1377 } 1378 } 1379 1380 if (smu->ppt_funcs->check_fw_status) { 1381 ret = smu->ppt_funcs->check_fw_status(smu); 1382 if (ret) { 1383 dev_err(adev->dev, "SMC is not ready\n"); 1384 return ret; 1385 } 1386 } 1387 1388 /* 1389 * Send msg GetDriverIfVersion to check if the return value is equal 1390 * with DRIVER_IF_VERSION of smc header. 1391 */ 1392 ret = smu_check_fw_version(smu); 1393 if (ret) 1394 return ret; 1395 1396 return ret; 1397 } 1398 1399 static int smu_hw_init(void *handle) 1400 { 1401 int ret; 1402 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1403 struct smu_context *smu = adev->powerplay.pp_handle; 1404 1405 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) { 1406 smu->pm_enabled = false; 1407 return 0; 1408 } 1409 1410 ret = smu_start_smc_engine(smu); 1411 if (ret) { 1412 dev_err(adev->dev, "SMC engine is not correctly up!\n"); 1413 return ret; 1414 } 1415 1416 if (smu->is_apu) { 1417 ret = smu_set_gfx_imu_enable(smu); 1418 if (ret) 1419 return ret; 1420 smu_dpm_set_vcn_enable(smu, true); 1421 smu_dpm_set_jpeg_enable(smu, true); 1422 smu_set_gfx_cgpg(smu, true); 1423 } 1424 1425 if (!smu->pm_enabled) 1426 return 0; 1427 1428 ret = smu_get_driver_allowed_feature_mask(smu); 1429 if (ret) 1430 return ret; 1431 1432 ret = smu_smc_hw_setup(smu); 1433 if (ret) { 1434 dev_err(adev->dev, "Failed to setup smc hw!\n"); 1435 return ret; 1436 } 1437 1438 /* 1439 * Move maximum sustainable clock retrieving here considering 1440 * 1. It is not needed on resume(from S3). 1441 * 2. DAL settings come between .hw_init and .late_init of SMU. 1442 * And DAL needs to know the maximum sustainable clocks. Thus 1443 * it cannot be put in .late_init(). 1444 */ 1445 ret = smu_init_max_sustainable_clocks(smu); 1446 if (ret) { 1447 dev_err(adev->dev, "Failed to init max sustainable clocks!\n"); 1448 return ret; 1449 } 1450 1451 adev->pm.dpm_enabled = true; 1452 1453 dev_info(adev->dev, "SMU is initialized successfully!\n"); 1454 1455 return 0; 1456 } 1457 1458 static int smu_disable_dpms(struct smu_context *smu) 1459 { 1460 struct amdgpu_device *adev = smu->adev; 1461 int ret = 0; 1462 bool use_baco = !smu->is_apu && 1463 ((amdgpu_in_reset(adev) && 1464 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) || 1465 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev))); 1466 1467 /* 1468 * For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others) 1469 * properly on suspend/reset/unload. Driver involvement may cause some unexpected issues. 1470 */ 1471 switch (adev->ip_versions[MP1_HWIP][0]) { 1472 case IP_VERSION(13, 0, 0): 1473 case IP_VERSION(13, 0, 7): 1474 case IP_VERSION(13, 0, 10): 1475 return 0; 1476 default: 1477 break; 1478 } 1479 1480 /* 1481 * For custom pptable uploading, skip the DPM features 1482 * disable process on Navi1x ASICs. 1483 * - As the gfx related features are under control of 1484 * RLC on those ASICs. RLC reinitialization will be 1485 * needed to reenable them. That will cost much more 1486 * efforts. 1487 * 1488 * - SMU firmware can handle the DPM reenablement 1489 * properly. 1490 */ 1491 if (smu->uploading_custom_pp_table) { 1492 switch (adev->ip_versions[MP1_HWIP][0]) { 1493 case IP_VERSION(11, 0, 0): 1494 case IP_VERSION(11, 0, 5): 1495 case IP_VERSION(11, 0, 9): 1496 case IP_VERSION(11, 0, 7): 1497 case IP_VERSION(11, 0, 11): 1498 case IP_VERSION(11, 5, 0): 1499 case IP_VERSION(11, 0, 12): 1500 case IP_VERSION(11, 0, 13): 1501 return 0; 1502 default: 1503 break; 1504 } 1505 } 1506 1507 /* 1508 * For Sienna_Cichlid, PMFW will handle the features disablement properly 1509 * on BACO in. Driver involvement is unnecessary. 1510 */ 1511 if (use_baco) { 1512 switch (adev->ip_versions[MP1_HWIP][0]) { 1513 case IP_VERSION(11, 0, 7): 1514 case IP_VERSION(11, 0, 0): 1515 case IP_VERSION(11, 0, 5): 1516 case IP_VERSION(11, 0, 9): 1517 case IP_VERSION(13, 0, 7): 1518 return 0; 1519 default: 1520 break; 1521 } 1522 } 1523 1524 /* 1525 * For SMU 13.0.4/11, PMFW will handle the features disablement properly 1526 * for gpu reset case. Driver involvement is unnecessary. 1527 */ 1528 if (amdgpu_in_reset(adev)) { 1529 switch (adev->ip_versions[MP1_HWIP][0]) { 1530 case IP_VERSION(13, 0, 4): 1531 case IP_VERSION(13, 0, 11): 1532 return 0; 1533 default: 1534 break; 1535 } 1536 } 1537 1538 /* 1539 * For gpu reset, runpm and hibernation through BACO, 1540 * BACO feature has to be kept enabled. 1541 */ 1542 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) { 1543 ret = smu_disable_all_features_with_exception(smu, 1544 SMU_FEATURE_BACO_BIT); 1545 if (ret) 1546 dev_err(adev->dev, "Failed to disable smu features except BACO.\n"); 1547 } else { 1548 /* DisableAllSmuFeatures message is not permitted with SCPM enabled */ 1549 if (!adev->scpm_enabled) { 1550 ret = smu_system_features_control(smu, false); 1551 if (ret) 1552 dev_err(adev->dev, "Failed to disable smu features.\n"); 1553 } 1554 } 1555 1556 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(9, 4, 2) && 1557 !amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop) 1558 adev->gfx.rlc.funcs->stop(adev); 1559 1560 return ret; 1561 } 1562 1563 static int smu_smc_hw_cleanup(struct smu_context *smu) 1564 { 1565 struct amdgpu_device *adev = smu->adev; 1566 int ret = 0; 1567 1568 cancel_work_sync(&smu->throttling_logging_work); 1569 cancel_work_sync(&smu->interrupt_work); 1570 1571 ret = smu_disable_thermal_alert(smu); 1572 if (ret) { 1573 dev_err(adev->dev, "Fail to disable thermal alert!\n"); 1574 return ret; 1575 } 1576 1577 ret = smu_disable_dpms(smu); 1578 if (ret) { 1579 dev_err(adev->dev, "Fail to disable dpm features!\n"); 1580 return ret; 1581 } 1582 1583 return 0; 1584 } 1585 1586 static int smu_hw_fini(void *handle) 1587 { 1588 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1589 struct smu_context *smu = adev->powerplay.pp_handle; 1590 1591 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev)) 1592 return 0; 1593 1594 smu_dpm_set_vcn_enable(smu, false); 1595 smu_dpm_set_jpeg_enable(smu, false); 1596 1597 adev->vcn.cur_state = AMD_PG_STATE_GATE; 1598 adev->jpeg.cur_state = AMD_PG_STATE_GATE; 1599 1600 if (!smu->pm_enabled) 1601 return 0; 1602 1603 adev->pm.dpm_enabled = false; 1604 1605 return smu_smc_hw_cleanup(smu); 1606 } 1607 1608 static void smu_late_fini(void *handle) 1609 { 1610 struct amdgpu_device *adev = handle; 1611 struct smu_context *smu = adev->powerplay.pp_handle; 1612 1613 kfree(smu); 1614 } 1615 1616 static int smu_reset(struct smu_context *smu) 1617 { 1618 struct amdgpu_device *adev = smu->adev; 1619 int ret; 1620 1621 ret = smu_hw_fini(adev); 1622 if (ret) 1623 return ret; 1624 1625 ret = smu_hw_init(adev); 1626 if (ret) 1627 return ret; 1628 1629 ret = smu_late_init(adev); 1630 if (ret) 1631 return ret; 1632 1633 return 0; 1634 } 1635 1636 static int smu_suspend(void *handle) 1637 { 1638 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1639 struct smu_context *smu = adev->powerplay.pp_handle; 1640 int ret; 1641 uint64_t count; 1642 1643 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev)) 1644 return 0; 1645 1646 if (!smu->pm_enabled) 1647 return 0; 1648 1649 adev->pm.dpm_enabled = false; 1650 1651 ret = smu_smc_hw_cleanup(smu); 1652 if (ret) 1653 return ret; 1654 1655 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED); 1656 1657 smu_set_gfx_cgpg(smu, false); 1658 1659 /* 1660 * pwfw resets entrycount when device is suspended, so we save the 1661 * last value to be used when we resume to keep it consistent 1662 */ 1663 ret = smu_get_entrycount_gfxoff(smu, &count); 1664 if (!ret) 1665 adev->gfx.gfx_off_entrycount = count; 1666 1667 return 0; 1668 } 1669 1670 static int smu_resume(void *handle) 1671 { 1672 int ret; 1673 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1674 struct smu_context *smu = adev->powerplay.pp_handle; 1675 1676 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev)) 1677 return 0; 1678 1679 if (!smu->pm_enabled) 1680 return 0; 1681 1682 dev_info(adev->dev, "SMU is resuming...\n"); 1683 1684 ret = smu_start_smc_engine(smu); 1685 if (ret) { 1686 dev_err(adev->dev, "SMC engine is not correctly up!\n"); 1687 return ret; 1688 } 1689 1690 ret = smu_smc_hw_setup(smu); 1691 if (ret) { 1692 dev_err(adev->dev, "Failed to setup smc hw!\n"); 1693 return ret; 1694 } 1695 1696 ret = smu_set_gfx_imu_enable(smu); 1697 if (ret) 1698 return ret; 1699 1700 smu_set_gfx_cgpg(smu, true); 1701 1702 smu->disable_uclk_switch = 0; 1703 1704 adev->pm.dpm_enabled = true; 1705 1706 dev_info(adev->dev, "SMU is resumed successfully!\n"); 1707 1708 return 0; 1709 } 1710 1711 static int smu_display_configuration_change(void *handle, 1712 const struct amd_pp_display_configuration *display_config) 1713 { 1714 struct smu_context *smu = handle; 1715 1716 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1717 return -EOPNOTSUPP; 1718 1719 if (!display_config) 1720 return -EINVAL; 1721 1722 smu_set_min_dcef_deep_sleep(smu, 1723 display_config->min_dcef_deep_sleep_set_clk / 100); 1724 1725 return 0; 1726 } 1727 1728 static int smu_set_clockgating_state(void *handle, 1729 enum amd_clockgating_state state) 1730 { 1731 return 0; 1732 } 1733 1734 static int smu_set_powergating_state(void *handle, 1735 enum amd_powergating_state state) 1736 { 1737 return 0; 1738 } 1739 1740 static int smu_enable_umd_pstate(void *handle, 1741 enum amd_dpm_forced_level *level) 1742 { 1743 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD | 1744 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK | 1745 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK | 1746 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK; 1747 1748 struct smu_context *smu = (struct smu_context*)(handle); 1749 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 1750 1751 if (!smu->is_apu && !smu_dpm_ctx->dpm_context) 1752 return -EINVAL; 1753 1754 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) { 1755 /* enter umd pstate, save current level, disable gfx cg*/ 1756 if (*level & profile_mode_mask) { 1757 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level; 1758 smu_gpo_control(smu, false); 1759 smu_gfx_ulv_control(smu, false); 1760 smu_deep_sleep_control(smu, false); 1761 amdgpu_asic_update_umd_stable_pstate(smu->adev, true); 1762 } 1763 } else { 1764 /* exit umd pstate, restore level, enable gfx cg*/ 1765 if (!(*level & profile_mode_mask)) { 1766 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT) 1767 *level = smu_dpm_ctx->saved_dpm_level; 1768 amdgpu_asic_update_umd_stable_pstate(smu->adev, false); 1769 smu_deep_sleep_control(smu, true); 1770 smu_gfx_ulv_control(smu, true); 1771 smu_gpo_control(smu, true); 1772 } 1773 } 1774 1775 return 0; 1776 } 1777 1778 static int smu_bump_power_profile_mode(struct smu_context *smu, 1779 long *param, 1780 uint32_t param_size) 1781 { 1782 int ret = 0; 1783 1784 if (smu->ppt_funcs->set_power_profile_mode) 1785 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size); 1786 1787 return ret; 1788 } 1789 1790 static int smu_adjust_power_state_dynamic(struct smu_context *smu, 1791 enum amd_dpm_forced_level level, 1792 bool skip_display_settings) 1793 { 1794 int ret = 0; 1795 int index = 0; 1796 long workload; 1797 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 1798 1799 if (!skip_display_settings) { 1800 ret = smu_display_config_changed(smu); 1801 if (ret) { 1802 dev_err(smu->adev->dev, "Failed to change display config!"); 1803 return ret; 1804 } 1805 } 1806 1807 ret = smu_apply_clocks_adjust_rules(smu); 1808 if (ret) { 1809 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!"); 1810 return ret; 1811 } 1812 1813 if (!skip_display_settings) { 1814 ret = smu_notify_smc_display_config(smu); 1815 if (ret) { 1816 dev_err(smu->adev->dev, "Failed to notify smc display config!"); 1817 return ret; 1818 } 1819 } 1820 1821 if (smu_dpm_ctx->dpm_level != level) { 1822 ret = smu_asic_set_performance_level(smu, level); 1823 if (ret) { 1824 dev_err(smu->adev->dev, "Failed to set performance level!"); 1825 return ret; 1826 } 1827 1828 /* update the saved copy */ 1829 smu_dpm_ctx->dpm_level = level; 1830 } 1831 1832 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL && 1833 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) { 1834 index = fls(smu->workload_mask); 1835 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0; 1836 workload = smu->workload_setting[index]; 1837 1838 if (smu->power_profile_mode != workload) 1839 smu_bump_power_profile_mode(smu, &workload, 0); 1840 } 1841 1842 return ret; 1843 } 1844 1845 static int smu_handle_task(struct smu_context *smu, 1846 enum amd_dpm_forced_level level, 1847 enum amd_pp_task task_id) 1848 { 1849 int ret = 0; 1850 1851 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1852 return -EOPNOTSUPP; 1853 1854 switch (task_id) { 1855 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE: 1856 ret = smu_pre_display_config_changed(smu); 1857 if (ret) 1858 return ret; 1859 ret = smu_adjust_power_state_dynamic(smu, level, false); 1860 break; 1861 case AMD_PP_TASK_COMPLETE_INIT: 1862 case AMD_PP_TASK_READJUST_POWER_STATE: 1863 ret = smu_adjust_power_state_dynamic(smu, level, true); 1864 break; 1865 default: 1866 break; 1867 } 1868 1869 return ret; 1870 } 1871 1872 static int smu_handle_dpm_task(void *handle, 1873 enum amd_pp_task task_id, 1874 enum amd_pm_state_type *user_state) 1875 { 1876 struct smu_context *smu = handle; 1877 struct smu_dpm_context *smu_dpm = &smu->smu_dpm; 1878 1879 return smu_handle_task(smu, smu_dpm->dpm_level, task_id); 1880 1881 } 1882 1883 static int smu_switch_power_profile(void *handle, 1884 enum PP_SMC_POWER_PROFILE type, 1885 bool en) 1886 { 1887 struct smu_context *smu = handle; 1888 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 1889 long workload; 1890 uint32_t index; 1891 1892 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1893 return -EOPNOTSUPP; 1894 1895 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM)) 1896 return -EINVAL; 1897 1898 if (!en) { 1899 smu->workload_mask &= ~(1 << smu->workload_prority[type]); 1900 index = fls(smu->workload_mask); 1901 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0; 1902 workload = smu->workload_setting[index]; 1903 } else { 1904 smu->workload_mask |= (1 << smu->workload_prority[type]); 1905 index = fls(smu->workload_mask); 1906 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0; 1907 workload = smu->workload_setting[index]; 1908 } 1909 1910 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL && 1911 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) 1912 smu_bump_power_profile_mode(smu, &workload, 0); 1913 1914 return 0; 1915 } 1916 1917 static enum amd_dpm_forced_level smu_get_performance_level(void *handle) 1918 { 1919 struct smu_context *smu = handle; 1920 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 1921 1922 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1923 return -EOPNOTSUPP; 1924 1925 if (!smu->is_apu && !smu_dpm_ctx->dpm_context) 1926 return -EINVAL; 1927 1928 return smu_dpm_ctx->dpm_level; 1929 } 1930 1931 static int smu_force_performance_level(void *handle, 1932 enum amd_dpm_forced_level level) 1933 { 1934 struct smu_context *smu = handle; 1935 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 1936 int ret = 0; 1937 1938 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1939 return -EOPNOTSUPP; 1940 1941 if (!smu->is_apu && !smu_dpm_ctx->dpm_context) 1942 return -EINVAL; 1943 1944 ret = smu_enable_umd_pstate(smu, &level); 1945 if (ret) 1946 return ret; 1947 1948 ret = smu_handle_task(smu, level, 1949 AMD_PP_TASK_READJUST_POWER_STATE); 1950 1951 /* reset user dpm clock state */ 1952 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) { 1953 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask)); 1954 smu->user_dpm_profile.clk_dependency = 0; 1955 } 1956 1957 return ret; 1958 } 1959 1960 static int smu_set_display_count(void *handle, uint32_t count) 1961 { 1962 struct smu_context *smu = handle; 1963 1964 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1965 return -EOPNOTSUPP; 1966 1967 return smu_init_display_count(smu, count); 1968 } 1969 1970 static int smu_force_smuclk_levels(struct smu_context *smu, 1971 enum smu_clk_type clk_type, 1972 uint32_t mask) 1973 { 1974 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm); 1975 int ret = 0; 1976 1977 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 1978 return -EOPNOTSUPP; 1979 1980 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) { 1981 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n"); 1982 return -EINVAL; 1983 } 1984 1985 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) { 1986 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask); 1987 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) { 1988 smu->user_dpm_profile.clk_mask[clk_type] = mask; 1989 smu_set_user_clk_dependencies(smu, clk_type); 1990 } 1991 } 1992 1993 return ret; 1994 } 1995 1996 static int smu_force_ppclk_levels(void *handle, 1997 enum pp_clock_type type, 1998 uint32_t mask) 1999 { 2000 struct smu_context *smu = handle; 2001 enum smu_clk_type clk_type; 2002 2003 switch (type) { 2004 case PP_SCLK: 2005 clk_type = SMU_SCLK; break; 2006 case PP_MCLK: 2007 clk_type = SMU_MCLK; break; 2008 case PP_PCIE: 2009 clk_type = SMU_PCIE; break; 2010 case PP_SOCCLK: 2011 clk_type = SMU_SOCCLK; break; 2012 case PP_FCLK: 2013 clk_type = SMU_FCLK; break; 2014 case PP_DCEFCLK: 2015 clk_type = SMU_DCEFCLK; break; 2016 case PP_VCLK: 2017 clk_type = SMU_VCLK; break; 2018 case PP_VCLK1: 2019 clk_type = SMU_VCLK1; break; 2020 case PP_DCLK: 2021 clk_type = SMU_DCLK; break; 2022 case PP_DCLK1: 2023 clk_type = SMU_DCLK1; break; 2024 case OD_SCLK: 2025 clk_type = SMU_OD_SCLK; break; 2026 case OD_MCLK: 2027 clk_type = SMU_OD_MCLK; break; 2028 case OD_VDDC_CURVE: 2029 clk_type = SMU_OD_VDDC_CURVE; break; 2030 case OD_RANGE: 2031 clk_type = SMU_OD_RANGE; break; 2032 default: 2033 return -EINVAL; 2034 } 2035 2036 return smu_force_smuclk_levels(smu, clk_type, mask); 2037 } 2038 2039 /* 2040 * On system suspending or resetting, the dpm_enabled 2041 * flag will be cleared. So that those SMU services which 2042 * are not supported will be gated. 2043 * However, the mp1 state setting should still be granted 2044 * even if the dpm_enabled cleared. 2045 */ 2046 static int smu_set_mp1_state(void *handle, 2047 enum pp_mp1_state mp1_state) 2048 { 2049 struct smu_context *smu = handle; 2050 int ret = 0; 2051 2052 if (!smu->pm_enabled) 2053 return -EOPNOTSUPP; 2054 2055 if (smu->ppt_funcs && 2056 smu->ppt_funcs->set_mp1_state) 2057 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state); 2058 2059 return ret; 2060 } 2061 2062 static int smu_set_df_cstate(void *handle, 2063 enum pp_df_cstate state) 2064 { 2065 struct smu_context *smu = handle; 2066 int ret = 0; 2067 2068 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2069 return -EOPNOTSUPP; 2070 2071 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate) 2072 return 0; 2073 2074 ret = smu->ppt_funcs->set_df_cstate(smu, state); 2075 if (ret) 2076 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n"); 2077 2078 return ret; 2079 } 2080 2081 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en) 2082 { 2083 int ret = 0; 2084 2085 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2086 return -EOPNOTSUPP; 2087 2088 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down) 2089 return 0; 2090 2091 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en); 2092 if (ret) 2093 dev_err(smu->adev->dev, "[AllowXgmiPowerDown] failed!\n"); 2094 2095 return ret; 2096 } 2097 2098 int smu_write_watermarks_table(struct smu_context *smu) 2099 { 2100 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2101 return -EOPNOTSUPP; 2102 2103 return smu_set_watermarks_table(smu, NULL); 2104 } 2105 2106 static int smu_set_watermarks_for_clock_ranges(void *handle, 2107 struct pp_smu_wm_range_sets *clock_ranges) 2108 { 2109 struct smu_context *smu = handle; 2110 2111 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2112 return -EOPNOTSUPP; 2113 2114 if (smu->disable_watermark) 2115 return 0; 2116 2117 return smu_set_watermarks_table(smu, clock_ranges); 2118 } 2119 2120 int smu_set_ac_dc(struct smu_context *smu) 2121 { 2122 int ret = 0; 2123 2124 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2125 return -EOPNOTSUPP; 2126 2127 /* controlled by firmware */ 2128 if (smu->dc_controlled_by_gpio) 2129 return 0; 2130 2131 ret = smu_set_power_source(smu, 2132 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC : 2133 SMU_POWER_SOURCE_DC); 2134 if (ret) 2135 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n", 2136 smu->adev->pm.ac_power ? "AC" : "DC"); 2137 2138 return ret; 2139 } 2140 2141 const struct amd_ip_funcs smu_ip_funcs = { 2142 .name = "smu", 2143 .early_init = smu_early_init, 2144 .late_init = smu_late_init, 2145 .sw_init = smu_sw_init, 2146 .sw_fini = smu_sw_fini, 2147 .hw_init = smu_hw_init, 2148 .hw_fini = smu_hw_fini, 2149 .late_fini = smu_late_fini, 2150 .suspend = smu_suspend, 2151 .resume = smu_resume, 2152 .is_idle = NULL, 2153 .check_soft_reset = NULL, 2154 .wait_for_idle = NULL, 2155 .soft_reset = NULL, 2156 .set_clockgating_state = smu_set_clockgating_state, 2157 .set_powergating_state = smu_set_powergating_state, 2158 }; 2159 2160 const struct amdgpu_ip_block_version smu_v11_0_ip_block = 2161 { 2162 .type = AMD_IP_BLOCK_TYPE_SMC, 2163 .major = 11, 2164 .minor = 0, 2165 .rev = 0, 2166 .funcs = &smu_ip_funcs, 2167 }; 2168 2169 const struct amdgpu_ip_block_version smu_v12_0_ip_block = 2170 { 2171 .type = AMD_IP_BLOCK_TYPE_SMC, 2172 .major = 12, 2173 .minor = 0, 2174 .rev = 0, 2175 .funcs = &smu_ip_funcs, 2176 }; 2177 2178 const struct amdgpu_ip_block_version smu_v13_0_ip_block = 2179 { 2180 .type = AMD_IP_BLOCK_TYPE_SMC, 2181 .major = 13, 2182 .minor = 0, 2183 .rev = 0, 2184 .funcs = &smu_ip_funcs, 2185 }; 2186 2187 static int smu_load_microcode(void *handle) 2188 { 2189 struct smu_context *smu = handle; 2190 struct amdgpu_device *adev = smu->adev; 2191 int ret = 0; 2192 2193 if (!smu->pm_enabled) 2194 return -EOPNOTSUPP; 2195 2196 /* This should be used for non PSP loading */ 2197 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) 2198 return 0; 2199 2200 if (smu->ppt_funcs->load_microcode) { 2201 ret = smu->ppt_funcs->load_microcode(smu); 2202 if (ret) { 2203 dev_err(adev->dev, "Load microcode failed\n"); 2204 return ret; 2205 } 2206 } 2207 2208 if (smu->ppt_funcs->check_fw_status) { 2209 ret = smu->ppt_funcs->check_fw_status(smu); 2210 if (ret) { 2211 dev_err(adev->dev, "SMC is not ready\n"); 2212 return ret; 2213 } 2214 } 2215 2216 return ret; 2217 } 2218 2219 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled) 2220 { 2221 int ret = 0; 2222 2223 if (smu->ppt_funcs->set_gfx_cgpg) 2224 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled); 2225 2226 return ret; 2227 } 2228 2229 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed) 2230 { 2231 struct smu_context *smu = handle; 2232 int ret = 0; 2233 2234 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2235 return -EOPNOTSUPP; 2236 2237 if (!smu->ppt_funcs->set_fan_speed_rpm) 2238 return -EOPNOTSUPP; 2239 2240 if (speed == U32_MAX) 2241 return -EINVAL; 2242 2243 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed); 2244 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) { 2245 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM; 2246 smu->user_dpm_profile.fan_speed_rpm = speed; 2247 2248 /* Override custom PWM setting as they cannot co-exist */ 2249 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM; 2250 smu->user_dpm_profile.fan_speed_pwm = 0; 2251 } 2252 2253 return ret; 2254 } 2255 2256 /** 2257 * smu_get_power_limit - Request one of the SMU Power Limits 2258 * 2259 * @handle: pointer to smu context 2260 * @limit: requested limit is written back to this variable 2261 * @pp_limit_level: &pp_power_limit_level which limit of the power to return 2262 * @pp_power_type: &pp_power_type type of power 2263 * Return: 0 on success, <0 on error 2264 * 2265 */ 2266 int smu_get_power_limit(void *handle, 2267 uint32_t *limit, 2268 enum pp_power_limit_level pp_limit_level, 2269 enum pp_power_type pp_power_type) 2270 { 2271 struct smu_context *smu = handle; 2272 struct amdgpu_device *adev = smu->adev; 2273 enum smu_ppt_limit_level limit_level; 2274 uint32_t limit_type; 2275 int ret = 0; 2276 2277 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2278 return -EOPNOTSUPP; 2279 2280 switch(pp_power_type) { 2281 case PP_PWR_TYPE_SUSTAINED: 2282 limit_type = SMU_DEFAULT_PPT_LIMIT; 2283 break; 2284 case PP_PWR_TYPE_FAST: 2285 limit_type = SMU_FAST_PPT_LIMIT; 2286 break; 2287 default: 2288 return -EOPNOTSUPP; 2289 break; 2290 } 2291 2292 switch(pp_limit_level){ 2293 case PP_PWR_LIMIT_CURRENT: 2294 limit_level = SMU_PPT_LIMIT_CURRENT; 2295 break; 2296 case PP_PWR_LIMIT_DEFAULT: 2297 limit_level = SMU_PPT_LIMIT_DEFAULT; 2298 break; 2299 case PP_PWR_LIMIT_MAX: 2300 limit_level = SMU_PPT_LIMIT_MAX; 2301 break; 2302 case PP_PWR_LIMIT_MIN: 2303 default: 2304 return -EOPNOTSUPP; 2305 break; 2306 } 2307 2308 if (limit_type != SMU_DEFAULT_PPT_LIMIT) { 2309 if (smu->ppt_funcs->get_ppt_limit) 2310 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level); 2311 } else { 2312 switch (limit_level) { 2313 case SMU_PPT_LIMIT_CURRENT: 2314 switch (adev->ip_versions[MP1_HWIP][0]) { 2315 case IP_VERSION(13, 0, 2): 2316 case IP_VERSION(11, 0, 7): 2317 case IP_VERSION(11, 0, 11): 2318 case IP_VERSION(11, 0, 12): 2319 case IP_VERSION(11, 0, 13): 2320 ret = smu_get_asic_power_limits(smu, 2321 &smu->current_power_limit, 2322 NULL, 2323 NULL); 2324 break; 2325 default: 2326 break; 2327 } 2328 *limit = smu->current_power_limit; 2329 break; 2330 case SMU_PPT_LIMIT_DEFAULT: 2331 *limit = smu->default_power_limit; 2332 break; 2333 case SMU_PPT_LIMIT_MAX: 2334 *limit = smu->max_power_limit; 2335 break; 2336 default: 2337 break; 2338 } 2339 } 2340 2341 return ret; 2342 } 2343 2344 static int smu_set_power_limit(void *handle, uint32_t limit) 2345 { 2346 struct smu_context *smu = handle; 2347 uint32_t limit_type = limit >> 24; 2348 int ret = 0; 2349 2350 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2351 return -EOPNOTSUPP; 2352 2353 limit &= (1<<24)-1; 2354 if (limit_type != SMU_DEFAULT_PPT_LIMIT) 2355 if (smu->ppt_funcs->set_power_limit) 2356 return smu->ppt_funcs->set_power_limit(smu, limit_type, limit); 2357 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); 2362 return -EINVAL; 2363 } 2364 2365 if (!limit) 2366 limit = smu->current_power_limit; 2367 2368 if (smu->ppt_funcs->set_power_limit) { 2369 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit); 2370 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) 2371 smu->user_dpm_profile.power_limit = limit; 2372 } 2373 2374 return ret; 2375 } 2376 2377 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf) 2378 { 2379 int ret = 0; 2380 2381 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2382 return -EOPNOTSUPP; 2383 2384 if (smu->ppt_funcs->print_clk_levels) 2385 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf); 2386 2387 return ret; 2388 } 2389 2390 static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type) 2391 { 2392 enum smu_clk_type clk_type; 2393 2394 switch (type) { 2395 case PP_SCLK: 2396 clk_type = SMU_SCLK; break; 2397 case PP_MCLK: 2398 clk_type = SMU_MCLK; break; 2399 case PP_PCIE: 2400 clk_type = SMU_PCIE; break; 2401 case PP_SOCCLK: 2402 clk_type = SMU_SOCCLK; break; 2403 case PP_FCLK: 2404 clk_type = SMU_FCLK; break; 2405 case PP_DCEFCLK: 2406 clk_type = SMU_DCEFCLK; break; 2407 case PP_VCLK: 2408 clk_type = SMU_VCLK; break; 2409 case PP_VCLK1: 2410 clk_type = SMU_VCLK1; break; 2411 case PP_DCLK: 2412 clk_type = SMU_DCLK; break; 2413 case PP_DCLK1: 2414 clk_type = SMU_DCLK1; break; 2415 case OD_SCLK: 2416 clk_type = SMU_OD_SCLK; break; 2417 case OD_MCLK: 2418 clk_type = SMU_OD_MCLK; break; 2419 case OD_VDDC_CURVE: 2420 clk_type = SMU_OD_VDDC_CURVE; break; 2421 case OD_RANGE: 2422 clk_type = SMU_OD_RANGE; break; 2423 case OD_VDDGFX_OFFSET: 2424 clk_type = SMU_OD_VDDGFX_OFFSET; break; 2425 case OD_CCLK: 2426 clk_type = SMU_OD_CCLK; break; 2427 default: 2428 clk_type = SMU_CLK_COUNT; break; 2429 } 2430 2431 return clk_type; 2432 } 2433 2434 static int smu_print_ppclk_levels(void *handle, 2435 enum pp_clock_type type, 2436 char *buf) 2437 { 2438 struct smu_context *smu = handle; 2439 enum smu_clk_type clk_type; 2440 2441 clk_type = smu_convert_to_smuclk(type); 2442 if (clk_type == SMU_CLK_COUNT) 2443 return -EINVAL; 2444 2445 return smu_print_smuclk_levels(smu, clk_type, buf); 2446 } 2447 2448 static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset) 2449 { 2450 struct smu_context *smu = handle; 2451 enum smu_clk_type clk_type; 2452 2453 clk_type = smu_convert_to_smuclk(type); 2454 if (clk_type == SMU_CLK_COUNT) 2455 return -EINVAL; 2456 2457 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2458 return -EOPNOTSUPP; 2459 2460 if (!smu->ppt_funcs->emit_clk_levels) 2461 return -ENOENT; 2462 2463 return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset); 2464 2465 } 2466 2467 static int smu_od_edit_dpm_table(void *handle, 2468 enum PP_OD_DPM_TABLE_COMMAND type, 2469 long *input, uint32_t size) 2470 { 2471 struct smu_context *smu = handle; 2472 int ret = 0; 2473 2474 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2475 return -EOPNOTSUPP; 2476 2477 if (smu->ppt_funcs->od_edit_dpm_table) { 2478 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size); 2479 } 2480 2481 return ret; 2482 } 2483 2484 static int smu_read_sensor(void *handle, 2485 int sensor, 2486 void *data, 2487 int *size_arg) 2488 { 2489 struct smu_context *smu = handle; 2490 struct smu_umd_pstate_table *pstate_table = 2491 &smu->pstate_table; 2492 int ret = 0; 2493 uint32_t *size, size_val; 2494 2495 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2496 return -EOPNOTSUPP; 2497 2498 if (!data || !size_arg) 2499 return -EINVAL; 2500 2501 size_val = *size_arg; 2502 size = &size_val; 2503 2504 if (smu->ppt_funcs->read_sensor) 2505 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size)) 2506 goto unlock; 2507 2508 switch (sensor) { 2509 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK: 2510 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100; 2511 *size = 4; 2512 break; 2513 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK: 2514 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100; 2515 *size = 4; 2516 break; 2517 case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK: 2518 *((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100; 2519 *size = 4; 2520 break; 2521 case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK: 2522 *((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100; 2523 *size = 4; 2524 break; 2525 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK: 2526 ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data); 2527 *size = 8; 2528 break; 2529 case AMDGPU_PP_SENSOR_UVD_POWER: 2530 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0; 2531 *size = 4; 2532 break; 2533 case AMDGPU_PP_SENSOR_VCE_POWER: 2534 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0; 2535 *size = 4; 2536 break; 2537 case AMDGPU_PP_SENSOR_VCN_POWER_STATE: 2538 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0: 1; 2539 *size = 4; 2540 break; 2541 case AMDGPU_PP_SENSOR_MIN_FAN_RPM: 2542 *(uint32_t *)data = 0; 2543 *size = 4; 2544 break; 2545 default: 2546 *size = 0; 2547 ret = -EOPNOTSUPP; 2548 break; 2549 } 2550 2551 unlock: 2552 // assign uint32_t to int 2553 *size_arg = size_val; 2554 2555 return ret; 2556 } 2557 2558 static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit) 2559 { 2560 int ret = -EINVAL; 2561 struct smu_context *smu = handle; 2562 2563 if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit) 2564 ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit); 2565 2566 return ret; 2567 } 2568 2569 static int smu_set_apu_thermal_limit(void *handle, uint32_t limit) 2570 { 2571 int ret = -EINVAL; 2572 struct smu_context *smu = handle; 2573 2574 if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit) 2575 ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit); 2576 2577 return ret; 2578 } 2579 2580 static int smu_get_power_profile_mode(void *handle, char *buf) 2581 { 2582 struct smu_context *smu = handle; 2583 2584 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || 2585 !smu->ppt_funcs->get_power_profile_mode) 2586 return -EOPNOTSUPP; 2587 if (!buf) 2588 return -EINVAL; 2589 2590 return smu->ppt_funcs->get_power_profile_mode(smu, buf); 2591 } 2592 2593 static int smu_set_power_profile_mode(void *handle, 2594 long *param, 2595 uint32_t param_size) 2596 { 2597 struct smu_context *smu = handle; 2598 2599 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || 2600 !smu->ppt_funcs->set_power_profile_mode) 2601 return -EOPNOTSUPP; 2602 2603 return smu_bump_power_profile_mode(smu, param, param_size); 2604 } 2605 2606 static int smu_get_fan_control_mode(void *handle, u32 *fan_mode) 2607 { 2608 struct smu_context *smu = handle; 2609 2610 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2611 return -EOPNOTSUPP; 2612 2613 if (!smu->ppt_funcs->get_fan_control_mode) 2614 return -EOPNOTSUPP; 2615 2616 if (!fan_mode) 2617 return -EINVAL; 2618 2619 *fan_mode = smu->ppt_funcs->get_fan_control_mode(smu); 2620 2621 return 0; 2622 } 2623 2624 static int smu_set_fan_control_mode(void *handle, u32 value) 2625 { 2626 struct smu_context *smu = handle; 2627 int ret = 0; 2628 2629 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2630 return -EOPNOTSUPP; 2631 2632 if (!smu->ppt_funcs->set_fan_control_mode) 2633 return -EOPNOTSUPP; 2634 2635 if (value == U32_MAX) 2636 return -EINVAL; 2637 2638 ret = smu->ppt_funcs->set_fan_control_mode(smu, value); 2639 if (ret) 2640 goto out; 2641 2642 if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) { 2643 smu->user_dpm_profile.fan_mode = value; 2644 2645 /* reset user dpm fan speed */ 2646 if (value != AMD_FAN_CTRL_MANUAL) { 2647 smu->user_dpm_profile.fan_speed_pwm = 0; 2648 smu->user_dpm_profile.fan_speed_rpm = 0; 2649 smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM); 2650 } 2651 } 2652 2653 out: 2654 return ret; 2655 } 2656 2657 static int smu_get_fan_speed_pwm(void *handle, u32 *speed) 2658 { 2659 struct smu_context *smu = handle; 2660 int ret = 0; 2661 2662 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2663 return -EOPNOTSUPP; 2664 2665 if (!smu->ppt_funcs->get_fan_speed_pwm) 2666 return -EOPNOTSUPP; 2667 2668 if (!speed) 2669 return -EINVAL; 2670 2671 ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed); 2672 2673 return ret; 2674 } 2675 2676 static int smu_set_fan_speed_pwm(void *handle, u32 speed) 2677 { 2678 struct smu_context *smu = handle; 2679 int ret = 0; 2680 2681 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2682 return -EOPNOTSUPP; 2683 2684 if (!smu->ppt_funcs->set_fan_speed_pwm) 2685 return -EOPNOTSUPP; 2686 2687 if (speed == U32_MAX) 2688 return -EINVAL; 2689 2690 ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed); 2691 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) { 2692 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM; 2693 smu->user_dpm_profile.fan_speed_pwm = speed; 2694 2695 /* Override custom RPM setting as they cannot co-exist */ 2696 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM; 2697 smu->user_dpm_profile.fan_speed_rpm = 0; 2698 } 2699 2700 return ret; 2701 } 2702 2703 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed) 2704 { 2705 struct smu_context *smu = handle; 2706 int ret = 0; 2707 2708 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2709 return -EOPNOTSUPP; 2710 2711 if (!smu->ppt_funcs->get_fan_speed_rpm) 2712 return -EOPNOTSUPP; 2713 2714 if (!speed) 2715 return -EINVAL; 2716 2717 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed); 2718 2719 return ret; 2720 } 2721 2722 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk) 2723 { 2724 struct smu_context *smu = handle; 2725 2726 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2727 return -EOPNOTSUPP; 2728 2729 return smu_set_min_dcef_deep_sleep(smu, clk); 2730 } 2731 2732 static int smu_get_clock_by_type_with_latency(void *handle, 2733 enum amd_pp_clock_type type, 2734 struct pp_clock_levels_with_latency *clocks) 2735 { 2736 struct smu_context *smu = handle; 2737 enum smu_clk_type clk_type; 2738 int ret = 0; 2739 2740 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2741 return -EOPNOTSUPP; 2742 2743 if (smu->ppt_funcs->get_clock_by_type_with_latency) { 2744 switch (type) { 2745 case amd_pp_sys_clock: 2746 clk_type = SMU_GFXCLK; 2747 break; 2748 case amd_pp_mem_clock: 2749 clk_type = SMU_MCLK; 2750 break; 2751 case amd_pp_dcef_clock: 2752 clk_type = SMU_DCEFCLK; 2753 break; 2754 case amd_pp_disp_clock: 2755 clk_type = SMU_DISPCLK; 2756 break; 2757 default: 2758 dev_err(smu->adev->dev, "Invalid clock type!\n"); 2759 return -EINVAL; 2760 } 2761 2762 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks); 2763 } 2764 2765 return ret; 2766 } 2767 2768 static int smu_display_clock_voltage_request(void *handle, 2769 struct pp_display_clock_request *clock_req) 2770 { 2771 struct smu_context *smu = handle; 2772 int ret = 0; 2773 2774 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2775 return -EOPNOTSUPP; 2776 2777 if (smu->ppt_funcs->display_clock_voltage_request) 2778 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req); 2779 2780 return ret; 2781 } 2782 2783 2784 static int smu_display_disable_memory_clock_switch(void *handle, 2785 bool disable_memory_clock_switch) 2786 { 2787 struct smu_context *smu = handle; 2788 int ret = -EINVAL; 2789 2790 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2791 return -EOPNOTSUPP; 2792 2793 if (smu->ppt_funcs->display_disable_memory_clock_switch) 2794 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch); 2795 2796 return ret; 2797 } 2798 2799 static int smu_set_xgmi_pstate(void *handle, 2800 uint32_t pstate) 2801 { 2802 struct smu_context *smu = handle; 2803 int ret = 0; 2804 2805 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2806 return -EOPNOTSUPP; 2807 2808 if (smu->ppt_funcs->set_xgmi_pstate) 2809 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate); 2810 2811 if(ret) 2812 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n"); 2813 2814 return ret; 2815 } 2816 2817 static int smu_get_baco_capability(void *handle, bool *cap) 2818 { 2819 struct smu_context *smu = handle; 2820 2821 *cap = false; 2822 2823 if (!smu->pm_enabled) 2824 return 0; 2825 2826 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support) 2827 *cap = smu->ppt_funcs->baco_is_support(smu); 2828 2829 return 0; 2830 } 2831 2832 static int smu_baco_set_state(void *handle, int state) 2833 { 2834 struct smu_context *smu = handle; 2835 int ret = 0; 2836 2837 if (!smu->pm_enabled) 2838 return -EOPNOTSUPP; 2839 2840 if (state == 0) { 2841 if (smu->ppt_funcs->baco_exit) 2842 ret = smu->ppt_funcs->baco_exit(smu); 2843 } else if (state == 1) { 2844 if (smu->ppt_funcs->baco_enter) 2845 ret = smu->ppt_funcs->baco_enter(smu); 2846 } else { 2847 return -EINVAL; 2848 } 2849 2850 if (ret) 2851 dev_err(smu->adev->dev, "Failed to %s BACO state!\n", 2852 (state)?"enter":"exit"); 2853 2854 return ret; 2855 } 2856 2857 bool smu_mode1_reset_is_support(struct smu_context *smu) 2858 { 2859 bool ret = false; 2860 2861 if (!smu->pm_enabled) 2862 return false; 2863 2864 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support) 2865 ret = smu->ppt_funcs->mode1_reset_is_support(smu); 2866 2867 return ret; 2868 } 2869 2870 bool smu_mode2_reset_is_support(struct smu_context *smu) 2871 { 2872 bool ret = false; 2873 2874 if (!smu->pm_enabled) 2875 return false; 2876 2877 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support) 2878 ret = smu->ppt_funcs->mode2_reset_is_support(smu); 2879 2880 return ret; 2881 } 2882 2883 int smu_mode1_reset(struct smu_context *smu) 2884 { 2885 int ret = 0; 2886 2887 if (!smu->pm_enabled) 2888 return -EOPNOTSUPP; 2889 2890 if (smu->ppt_funcs->mode1_reset) 2891 ret = smu->ppt_funcs->mode1_reset(smu); 2892 2893 return ret; 2894 } 2895 2896 static int smu_mode2_reset(void *handle) 2897 { 2898 struct smu_context *smu = handle; 2899 int ret = 0; 2900 2901 if (!smu->pm_enabled) 2902 return -EOPNOTSUPP; 2903 2904 if (smu->ppt_funcs->mode2_reset) 2905 ret = smu->ppt_funcs->mode2_reset(smu); 2906 2907 if (ret) 2908 dev_err(smu->adev->dev, "Mode2 reset failed!\n"); 2909 2910 return ret; 2911 } 2912 2913 static int smu_enable_gfx_features(void *handle) 2914 { 2915 struct smu_context *smu = handle; 2916 int ret = 0; 2917 2918 if (!smu->pm_enabled) 2919 return -EOPNOTSUPP; 2920 2921 if (smu->ppt_funcs->enable_gfx_features) 2922 ret = smu->ppt_funcs->enable_gfx_features(smu); 2923 2924 if (ret) 2925 dev_err(smu->adev->dev, "enable gfx features failed!\n"); 2926 2927 return ret; 2928 } 2929 2930 static int smu_get_max_sustainable_clocks_by_dc(void *handle, 2931 struct pp_smu_nv_clock_table *max_clocks) 2932 { 2933 struct smu_context *smu = handle; 2934 int ret = 0; 2935 2936 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2937 return -EOPNOTSUPP; 2938 2939 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc) 2940 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks); 2941 2942 return ret; 2943 } 2944 2945 static int smu_get_uclk_dpm_states(void *handle, 2946 unsigned int *clock_values_in_khz, 2947 unsigned int *num_states) 2948 { 2949 struct smu_context *smu = handle; 2950 int ret = 0; 2951 2952 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2953 return -EOPNOTSUPP; 2954 2955 if (smu->ppt_funcs->get_uclk_dpm_states) 2956 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states); 2957 2958 return ret; 2959 } 2960 2961 static enum amd_pm_state_type smu_get_current_power_state(void *handle) 2962 { 2963 struct smu_context *smu = handle; 2964 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT; 2965 2966 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2967 return -EOPNOTSUPP; 2968 2969 if (smu->ppt_funcs->get_current_power_state) 2970 pm_state = smu->ppt_funcs->get_current_power_state(smu); 2971 2972 return pm_state; 2973 } 2974 2975 static int smu_get_dpm_clock_table(void *handle, 2976 struct dpm_clocks *clock_table) 2977 { 2978 struct smu_context *smu = handle; 2979 int ret = 0; 2980 2981 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2982 return -EOPNOTSUPP; 2983 2984 if (smu->ppt_funcs->get_dpm_clock_table) 2985 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table); 2986 2987 return ret; 2988 } 2989 2990 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table) 2991 { 2992 struct smu_context *smu = handle; 2993 2994 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 2995 return -EOPNOTSUPP; 2996 2997 if (!smu->ppt_funcs->get_gpu_metrics) 2998 return -EOPNOTSUPP; 2999 3000 return smu->ppt_funcs->get_gpu_metrics(smu, table); 3001 } 3002 3003 static int smu_enable_mgpu_fan_boost(void *handle) 3004 { 3005 struct smu_context *smu = handle; 3006 int ret = 0; 3007 3008 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) 3009 return -EOPNOTSUPP; 3010 3011 if (smu->ppt_funcs->enable_mgpu_fan_boost) 3012 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu); 3013 3014 return ret; 3015 } 3016 3017 static int smu_gfx_state_change_set(void *handle, 3018 uint32_t state) 3019 { 3020 struct smu_context *smu = handle; 3021 int ret = 0; 3022 3023 if (smu->ppt_funcs->gfx_state_change_set) 3024 ret = smu->ppt_funcs->gfx_state_change_set(smu, state); 3025 3026 return ret; 3027 } 3028 3029 int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable) 3030 { 3031 int ret = 0; 3032 3033 if (smu->ppt_funcs->smu_handle_passthrough_sbr) 3034 ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable); 3035 3036 return ret; 3037 } 3038 3039 int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc) 3040 { 3041 int ret = -EOPNOTSUPP; 3042 3043 if (smu->ppt_funcs && 3044 smu->ppt_funcs->get_ecc_info) 3045 ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc); 3046 3047 return ret; 3048 3049 } 3050 3051 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size) 3052 { 3053 struct smu_context *smu = handle; 3054 struct smu_table_context *smu_table = &smu->smu_table; 3055 struct smu_table *memory_pool = &smu_table->memory_pool; 3056 3057 if (!addr || !size) 3058 return -EINVAL; 3059 3060 *addr = NULL; 3061 *size = 0; 3062 if (memory_pool->bo) { 3063 *addr = memory_pool->cpu_addr; 3064 *size = memory_pool->size; 3065 } 3066 3067 return 0; 3068 } 3069 3070 static const struct amd_pm_funcs swsmu_pm_funcs = { 3071 /* export for sysfs */ 3072 .set_fan_control_mode = smu_set_fan_control_mode, 3073 .get_fan_control_mode = smu_get_fan_control_mode, 3074 .set_fan_speed_pwm = smu_set_fan_speed_pwm, 3075 .get_fan_speed_pwm = smu_get_fan_speed_pwm, 3076 .force_clock_level = smu_force_ppclk_levels, 3077 .print_clock_levels = smu_print_ppclk_levels, 3078 .emit_clock_levels = smu_emit_ppclk_levels, 3079 .force_performance_level = smu_force_performance_level, 3080 .read_sensor = smu_read_sensor, 3081 .get_apu_thermal_limit = smu_get_apu_thermal_limit, 3082 .set_apu_thermal_limit = smu_set_apu_thermal_limit, 3083 .get_performance_level = smu_get_performance_level, 3084 .get_current_power_state = smu_get_current_power_state, 3085 .get_fan_speed_rpm = smu_get_fan_speed_rpm, 3086 .set_fan_speed_rpm = smu_set_fan_speed_rpm, 3087 .get_pp_num_states = smu_get_power_num_states, 3088 .get_pp_table = smu_sys_get_pp_table, 3089 .set_pp_table = smu_sys_set_pp_table, 3090 .switch_power_profile = smu_switch_power_profile, 3091 /* export to amdgpu */ 3092 .dispatch_tasks = smu_handle_dpm_task, 3093 .load_firmware = smu_load_microcode, 3094 .set_powergating_by_smu = smu_dpm_set_power_gate, 3095 .set_power_limit = smu_set_power_limit, 3096 .get_power_limit = smu_get_power_limit, 3097 .get_power_profile_mode = smu_get_power_profile_mode, 3098 .set_power_profile_mode = smu_set_power_profile_mode, 3099 .odn_edit_dpm_table = smu_od_edit_dpm_table, 3100 .set_mp1_state = smu_set_mp1_state, 3101 .gfx_state_change_set = smu_gfx_state_change_set, 3102 /* export to DC */ 3103 .get_sclk = smu_get_sclk, 3104 .get_mclk = smu_get_mclk, 3105 .display_configuration_change = smu_display_configuration_change, 3106 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency, 3107 .display_clock_voltage_request = smu_display_clock_voltage_request, 3108 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost, 3109 .set_active_display_count = smu_set_display_count, 3110 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk, 3111 .get_asic_baco_capability = smu_get_baco_capability, 3112 .set_asic_baco_state = smu_baco_set_state, 3113 .get_ppfeature_status = smu_sys_get_pp_feature_mask, 3114 .set_ppfeature_status = smu_sys_set_pp_feature_mask, 3115 .asic_reset_mode_2 = smu_mode2_reset, 3116 .asic_reset_enable_gfx_features = smu_enable_gfx_features, 3117 .set_df_cstate = smu_set_df_cstate, 3118 .set_xgmi_pstate = smu_set_xgmi_pstate, 3119 .get_gpu_metrics = smu_sys_get_gpu_metrics, 3120 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges, 3121 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch, 3122 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc, 3123 .get_uclk_dpm_states = smu_get_uclk_dpm_states, 3124 .get_dpm_clock_table = smu_get_dpm_clock_table, 3125 .get_smu_prv_buf_details = smu_get_prv_buffer_details, 3126 }; 3127 3128 int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event, 3129 uint64_t event_arg) 3130 { 3131 int ret = -EINVAL; 3132 3133 if (smu->ppt_funcs->wait_for_event) 3134 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg); 3135 3136 return ret; 3137 } 3138 3139 int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size) 3140 { 3141 3142 if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled) 3143 return -EOPNOTSUPP; 3144 3145 /* Confirm the buffer allocated is of correct size */ 3146 if (size != smu->stb_context.stb_buf_size) 3147 return -EINVAL; 3148 3149 /* 3150 * No need to lock smu mutex as we access STB directly through MMIO 3151 * and not going through SMU messaging route (for now at least). 3152 * For registers access rely on implementation internal locking. 3153 */ 3154 return smu->ppt_funcs->stb_collect_info(smu, buf, size); 3155 } 3156 3157 #if defined(CONFIG_DEBUG_FS) 3158 3159 static int smu_stb_debugfs_open(struct inode *inode, struct file *filp) 3160 { 3161 struct amdgpu_device *adev = filp->f_inode->i_private; 3162 struct smu_context *smu = adev->powerplay.pp_handle; 3163 unsigned char *buf; 3164 int r; 3165 3166 buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL); 3167 if (!buf) 3168 return -ENOMEM; 3169 3170 r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size); 3171 if (r) 3172 goto out; 3173 3174 filp->private_data = buf; 3175 3176 return 0; 3177 3178 out: 3179 kvfree(buf); 3180 return r; 3181 } 3182 3183 static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size, 3184 loff_t *pos) 3185 { 3186 struct amdgpu_device *adev = filp->f_inode->i_private; 3187 struct smu_context *smu = adev->powerplay.pp_handle; 3188 3189 3190 if (!filp->private_data) 3191 return -EINVAL; 3192 3193 return simple_read_from_buffer(buf, 3194 size, 3195 pos, filp->private_data, 3196 smu->stb_context.stb_buf_size); 3197 } 3198 3199 static int smu_stb_debugfs_release(struct inode *inode, struct file *filp) 3200 { 3201 kvfree(filp->private_data); 3202 filp->private_data = NULL; 3203 3204 return 0; 3205 } 3206 3207 /* 3208 * We have to define not only read method but also 3209 * open and release because .read takes up to PAGE_SIZE 3210 * data each time so and so is invoked multiple times. 3211 * We allocate the STB buffer in .open and release it 3212 * in .release 3213 */ 3214 static const struct file_operations smu_stb_debugfs_fops = { 3215 .owner = THIS_MODULE, 3216 .open = smu_stb_debugfs_open, 3217 .read = smu_stb_debugfs_read, 3218 .release = smu_stb_debugfs_release, 3219 .llseek = default_llseek, 3220 }; 3221 3222 #endif 3223 3224 void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev) 3225 { 3226 #if defined(CONFIG_DEBUG_FS) 3227 3228 struct smu_context *smu = adev->powerplay.pp_handle; 3229 3230 if (!smu || (!smu->stb_context.stb_buf_size)) 3231 return; 3232 3233 debugfs_create_file_size("amdgpu_smu_stb_dump", 3234 S_IRUSR, 3235 adev_to_drm(adev)->primary->debugfs_root, 3236 adev, 3237 &smu_stb_debugfs_fops, 3238 smu->stb_context.stb_buf_size); 3239 #endif 3240 } 3241 3242 int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size) 3243 { 3244 int ret = 0; 3245 3246 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num) 3247 ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size); 3248 3249 return ret; 3250 } 3251 3252 int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size) 3253 { 3254 int ret = 0; 3255 3256 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag) 3257 ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size); 3258 3259 return ret; 3260 } 3261