1 /* 2 * Copyright 2017 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 * Authors: Rafał Miłecki <zajec5@gmail.com> 23 * Alex Deucher <alexdeucher@gmail.com> 24 */ 25 26 #include "amdgpu.h" 27 #include "amdgpu_drv.h" 28 #include "amdgpu_pm.h" 29 #include "amdgpu_dpm.h" 30 #include "atom.h" 31 #include <linux/pci.h> 32 #include <linux/hwmon.h> 33 #include <linux/hwmon-sysfs.h> 34 #include <linux/nospec.h> 35 #include <linux/pm_runtime.h> 36 #include <asm/processor.h> 37 #include "hwmgr.h" 38 39 static const struct cg_flag_name clocks[] = { 40 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"}, 41 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"}, 42 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"}, 43 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"}, 44 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"}, 45 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"}, 46 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"}, 47 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"}, 48 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"}, 49 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"}, 50 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"}, 51 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"}, 52 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"}, 53 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"}, 54 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"}, 55 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"}, 56 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"}, 57 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"}, 58 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"}, 59 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"}, 60 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"}, 61 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"}, 62 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"}, 63 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"}, 64 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"}, 65 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"}, 66 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"}, 67 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"}, 68 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"}, 69 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"}, 70 71 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"}, 72 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"}, 73 {0, NULL}, 74 }; 75 76 static const struct hwmon_temp_label { 77 enum PP_HWMON_TEMP channel; 78 const char *label; 79 } temp_label[] = { 80 {PP_TEMP_EDGE, "edge"}, 81 {PP_TEMP_JUNCTION, "junction"}, 82 {PP_TEMP_MEM, "mem"}, 83 }; 84 85 const char * const amdgpu_pp_profile_name[] = { 86 "BOOTUP_DEFAULT", 87 "3D_FULL_SCREEN", 88 "POWER_SAVING", 89 "VIDEO", 90 "VR", 91 "COMPUTE", 92 "CUSTOM" 93 }; 94 95 /** 96 * DOC: power_dpm_state 97 * 98 * The power_dpm_state file is a legacy interface and is only provided for 99 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting 100 * certain power related parameters. The file power_dpm_state is used for this. 101 * It accepts the following arguments: 102 * 103 * - battery 104 * 105 * - balanced 106 * 107 * - performance 108 * 109 * battery 110 * 111 * On older GPUs, the vbios provided a special power state for battery 112 * operation. Selecting battery switched to this state. This is no 113 * longer provided on newer GPUs so the option does nothing in that case. 114 * 115 * balanced 116 * 117 * On older GPUs, the vbios provided a special power state for balanced 118 * operation. Selecting balanced switched to this state. This is no 119 * longer provided on newer GPUs so the option does nothing in that case. 120 * 121 * performance 122 * 123 * On older GPUs, the vbios provided a special power state for performance 124 * operation. Selecting performance switched to this state. This is no 125 * longer provided on newer GPUs so the option does nothing in that case. 126 * 127 */ 128 129 static ssize_t amdgpu_get_power_dpm_state(struct device *dev, 130 struct device_attribute *attr, 131 char *buf) 132 { 133 struct drm_device *ddev = dev_get_drvdata(dev); 134 struct amdgpu_device *adev = drm_to_adev(ddev); 135 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 136 enum amd_pm_state_type pm; 137 int ret; 138 139 if (amdgpu_in_reset(adev)) 140 return -EPERM; 141 if (adev->in_suspend && !adev->in_runpm) 142 return -EPERM; 143 144 ret = pm_runtime_get_sync(ddev->dev); 145 if (ret < 0) { 146 pm_runtime_put_autosuspend(ddev->dev); 147 return ret; 148 } 149 150 if (pp_funcs->get_current_power_state) { 151 pm = amdgpu_dpm_get_current_power_state(adev); 152 } else { 153 pm = adev->pm.dpm.user_state; 154 } 155 156 pm_runtime_mark_last_busy(ddev->dev); 157 pm_runtime_put_autosuspend(ddev->dev); 158 159 return sysfs_emit(buf, "%s\n", 160 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" : 161 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance"); 162 } 163 164 static ssize_t amdgpu_set_power_dpm_state(struct device *dev, 165 struct device_attribute *attr, 166 const char *buf, 167 size_t count) 168 { 169 struct drm_device *ddev = dev_get_drvdata(dev); 170 struct amdgpu_device *adev = drm_to_adev(ddev); 171 enum amd_pm_state_type state; 172 int ret; 173 174 if (amdgpu_in_reset(adev)) 175 return -EPERM; 176 if (adev->in_suspend && !adev->in_runpm) 177 return -EPERM; 178 179 if (strncmp("battery", buf, strlen("battery")) == 0) 180 state = POWER_STATE_TYPE_BATTERY; 181 else if (strncmp("balanced", buf, strlen("balanced")) == 0) 182 state = POWER_STATE_TYPE_BALANCED; 183 else if (strncmp("performance", buf, strlen("performance")) == 0) 184 state = POWER_STATE_TYPE_PERFORMANCE; 185 else 186 return -EINVAL; 187 188 ret = pm_runtime_get_sync(ddev->dev); 189 if (ret < 0) { 190 pm_runtime_put_autosuspend(ddev->dev); 191 return ret; 192 } 193 194 if (is_support_sw_smu(adev)) { 195 mutex_lock(&adev->pm.mutex); 196 adev->pm.dpm.user_state = state; 197 mutex_unlock(&adev->pm.mutex); 198 } else if (adev->powerplay.pp_funcs->dispatch_tasks) { 199 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state); 200 } else { 201 mutex_lock(&adev->pm.mutex); 202 adev->pm.dpm.user_state = state; 203 mutex_unlock(&adev->pm.mutex); 204 205 amdgpu_pm_compute_clocks(adev); 206 } 207 pm_runtime_mark_last_busy(ddev->dev); 208 pm_runtime_put_autosuspend(ddev->dev); 209 210 return count; 211 } 212 213 214 /** 215 * DOC: power_dpm_force_performance_level 216 * 217 * The amdgpu driver provides a sysfs API for adjusting certain power 218 * related parameters. The file power_dpm_force_performance_level is 219 * used for this. It accepts the following arguments: 220 * 221 * - auto 222 * 223 * - low 224 * 225 * - high 226 * 227 * - manual 228 * 229 * - profile_standard 230 * 231 * - profile_min_sclk 232 * 233 * - profile_min_mclk 234 * 235 * - profile_peak 236 * 237 * auto 238 * 239 * When auto is selected, the driver will attempt to dynamically select 240 * the optimal power profile for current conditions in the driver. 241 * 242 * low 243 * 244 * When low is selected, the clocks are forced to the lowest power state. 245 * 246 * high 247 * 248 * When high is selected, the clocks are forced to the highest power state. 249 * 250 * manual 251 * 252 * When manual is selected, the user can manually adjust which power states 253 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk, 254 * and pp_dpm_pcie files and adjust the power state transition heuristics 255 * via the pp_power_profile_mode sysfs file. 256 * 257 * profile_standard 258 * profile_min_sclk 259 * profile_min_mclk 260 * profile_peak 261 * 262 * When the profiling modes are selected, clock and power gating are 263 * disabled and the clocks are set for different profiling cases. This 264 * mode is recommended for profiling specific work loads where you do 265 * not want clock or power gating for clock fluctuation to interfere 266 * with your results. profile_standard sets the clocks to a fixed clock 267 * level which varies from asic to asic. profile_min_sclk forces the sclk 268 * to the lowest level. profile_min_mclk forces the mclk to the lowest level. 269 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels. 270 * 271 */ 272 273 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev, 274 struct device_attribute *attr, 275 char *buf) 276 { 277 struct drm_device *ddev = dev_get_drvdata(dev); 278 struct amdgpu_device *adev = drm_to_adev(ddev); 279 enum amd_dpm_forced_level level = 0xff; 280 int ret; 281 282 if (amdgpu_in_reset(adev)) 283 return -EPERM; 284 if (adev->in_suspend && !adev->in_runpm) 285 return -EPERM; 286 287 ret = pm_runtime_get_sync(ddev->dev); 288 if (ret < 0) { 289 pm_runtime_put_autosuspend(ddev->dev); 290 return ret; 291 } 292 293 if (adev->powerplay.pp_funcs->get_performance_level) 294 level = amdgpu_dpm_get_performance_level(adev); 295 else 296 level = adev->pm.dpm.forced_level; 297 298 pm_runtime_mark_last_busy(ddev->dev); 299 pm_runtime_put_autosuspend(ddev->dev); 300 301 return sysfs_emit(buf, "%s\n", 302 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" : 303 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" : 304 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" : 305 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" : 306 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" : 307 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" : 308 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" : 309 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" : 310 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" : 311 "unknown"); 312 } 313 314 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev, 315 struct device_attribute *attr, 316 const char *buf, 317 size_t count) 318 { 319 struct drm_device *ddev = dev_get_drvdata(dev); 320 struct amdgpu_device *adev = drm_to_adev(ddev); 321 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 322 enum amd_dpm_forced_level level; 323 enum amd_dpm_forced_level current_level; 324 int ret = 0; 325 326 if (amdgpu_in_reset(adev)) 327 return -EPERM; 328 if (adev->in_suspend && !adev->in_runpm) 329 return -EPERM; 330 331 if (strncmp("low", buf, strlen("low")) == 0) { 332 level = AMD_DPM_FORCED_LEVEL_LOW; 333 } else if (strncmp("high", buf, strlen("high")) == 0) { 334 level = AMD_DPM_FORCED_LEVEL_HIGH; 335 } else if (strncmp("auto", buf, strlen("auto")) == 0) { 336 level = AMD_DPM_FORCED_LEVEL_AUTO; 337 } else if (strncmp("manual", buf, strlen("manual")) == 0) { 338 level = AMD_DPM_FORCED_LEVEL_MANUAL; 339 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) { 340 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT; 341 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) { 342 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD; 343 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) { 344 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK; 345 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) { 346 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK; 347 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) { 348 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK; 349 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) { 350 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM; 351 } else { 352 return -EINVAL; 353 } 354 355 ret = pm_runtime_get_sync(ddev->dev); 356 if (ret < 0) { 357 pm_runtime_put_autosuspend(ddev->dev); 358 return ret; 359 } 360 361 if (pp_funcs->get_performance_level) 362 current_level = amdgpu_dpm_get_performance_level(adev); 363 else 364 current_level = adev->pm.dpm.forced_level; 365 366 if (current_level == level) { 367 pm_runtime_mark_last_busy(ddev->dev); 368 pm_runtime_put_autosuspend(ddev->dev); 369 return count; 370 } 371 372 if (adev->asic_type == CHIP_RAVEN) { 373 if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) { 374 if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL) 375 amdgpu_gfx_off_ctrl(adev, false); 376 else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL) 377 amdgpu_gfx_off_ctrl(adev, true); 378 } 379 } 380 381 /* profile_exit setting is valid only when current mode is in profile mode */ 382 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD | 383 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK | 384 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK | 385 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) && 386 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) { 387 pr_err("Currently not in any profile mode!\n"); 388 pm_runtime_mark_last_busy(ddev->dev); 389 pm_runtime_put_autosuspend(ddev->dev); 390 return -EINVAL; 391 } 392 393 if (pp_funcs->force_performance_level) { 394 mutex_lock(&adev->pm.mutex); 395 if (adev->pm.dpm.thermal_active) { 396 mutex_unlock(&adev->pm.mutex); 397 pm_runtime_mark_last_busy(ddev->dev); 398 pm_runtime_put_autosuspend(ddev->dev); 399 return -EINVAL; 400 } 401 ret = amdgpu_dpm_force_performance_level(adev, level); 402 if (ret) { 403 mutex_unlock(&adev->pm.mutex); 404 pm_runtime_mark_last_busy(ddev->dev); 405 pm_runtime_put_autosuspend(ddev->dev); 406 return -EINVAL; 407 } else { 408 adev->pm.dpm.forced_level = level; 409 } 410 mutex_unlock(&adev->pm.mutex); 411 } 412 pm_runtime_mark_last_busy(ddev->dev); 413 pm_runtime_put_autosuspend(ddev->dev); 414 415 return count; 416 } 417 418 static ssize_t amdgpu_get_pp_num_states(struct device *dev, 419 struct device_attribute *attr, 420 char *buf) 421 { 422 struct drm_device *ddev = dev_get_drvdata(dev); 423 struct amdgpu_device *adev = drm_to_adev(ddev); 424 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 425 struct pp_states_info data; 426 uint32_t i; 427 int buf_len, ret; 428 429 if (amdgpu_in_reset(adev)) 430 return -EPERM; 431 if (adev->in_suspend && !adev->in_runpm) 432 return -EPERM; 433 434 ret = pm_runtime_get_sync(ddev->dev); 435 if (ret < 0) { 436 pm_runtime_put_autosuspend(ddev->dev); 437 return ret; 438 } 439 440 if (pp_funcs->get_pp_num_states) { 441 amdgpu_dpm_get_pp_num_states(adev, &data); 442 } else { 443 memset(&data, 0, sizeof(data)); 444 } 445 446 pm_runtime_mark_last_busy(ddev->dev); 447 pm_runtime_put_autosuspend(ddev->dev); 448 449 buf_len = sysfs_emit(buf, "states: %d\n", data.nums); 450 for (i = 0; i < data.nums; i++) 451 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i, 452 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" : 453 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" : 454 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" : 455 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default"); 456 457 return buf_len; 458 } 459 460 static ssize_t amdgpu_get_pp_cur_state(struct device *dev, 461 struct device_attribute *attr, 462 char *buf) 463 { 464 struct drm_device *ddev = dev_get_drvdata(dev); 465 struct amdgpu_device *adev = drm_to_adev(ddev); 466 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 467 struct pp_states_info data = {0}; 468 enum amd_pm_state_type pm = 0; 469 int i = 0, ret = 0; 470 471 if (amdgpu_in_reset(adev)) 472 return -EPERM; 473 if (adev->in_suspend && !adev->in_runpm) 474 return -EPERM; 475 476 ret = pm_runtime_get_sync(ddev->dev); 477 if (ret < 0) { 478 pm_runtime_put_autosuspend(ddev->dev); 479 return ret; 480 } 481 482 if (pp_funcs->get_current_power_state 483 && pp_funcs->get_pp_num_states) { 484 pm = amdgpu_dpm_get_current_power_state(adev); 485 amdgpu_dpm_get_pp_num_states(adev, &data); 486 } 487 488 pm_runtime_mark_last_busy(ddev->dev); 489 pm_runtime_put_autosuspend(ddev->dev); 490 491 for (i = 0; i < data.nums; i++) { 492 if (pm == data.states[i]) 493 break; 494 } 495 496 if (i == data.nums) 497 i = -EINVAL; 498 499 return sysfs_emit(buf, "%d\n", i); 500 } 501 502 static ssize_t amdgpu_get_pp_force_state(struct device *dev, 503 struct device_attribute *attr, 504 char *buf) 505 { 506 struct drm_device *ddev = dev_get_drvdata(dev); 507 struct amdgpu_device *adev = drm_to_adev(ddev); 508 509 if (amdgpu_in_reset(adev)) 510 return -EPERM; 511 if (adev->in_suspend && !adev->in_runpm) 512 return -EPERM; 513 514 if (adev->pp_force_state_enabled) 515 return amdgpu_get_pp_cur_state(dev, attr, buf); 516 else 517 return sysfs_emit(buf, "\n"); 518 } 519 520 static ssize_t amdgpu_set_pp_force_state(struct device *dev, 521 struct device_attribute *attr, 522 const char *buf, 523 size_t count) 524 { 525 struct drm_device *ddev = dev_get_drvdata(dev); 526 struct amdgpu_device *adev = drm_to_adev(ddev); 527 enum amd_pm_state_type state = 0; 528 unsigned long idx; 529 int ret; 530 531 if (amdgpu_in_reset(adev)) 532 return -EPERM; 533 if (adev->in_suspend && !adev->in_runpm) 534 return -EPERM; 535 536 if (strlen(buf) == 1) 537 adev->pp_force_state_enabled = false; 538 else if (is_support_sw_smu(adev)) 539 adev->pp_force_state_enabled = false; 540 else if (adev->powerplay.pp_funcs->dispatch_tasks && 541 adev->powerplay.pp_funcs->get_pp_num_states) { 542 struct pp_states_info data; 543 544 ret = kstrtoul(buf, 0, &idx); 545 if (ret || idx >= ARRAY_SIZE(data.states)) 546 return -EINVAL; 547 548 idx = array_index_nospec(idx, ARRAY_SIZE(data.states)); 549 550 amdgpu_dpm_get_pp_num_states(adev, &data); 551 state = data.states[idx]; 552 553 ret = pm_runtime_get_sync(ddev->dev); 554 if (ret < 0) { 555 pm_runtime_put_autosuspend(ddev->dev); 556 return ret; 557 } 558 559 /* only set user selected power states */ 560 if (state != POWER_STATE_TYPE_INTERNAL_BOOT && 561 state != POWER_STATE_TYPE_DEFAULT) { 562 amdgpu_dpm_dispatch_task(adev, 563 AMD_PP_TASK_ENABLE_USER_STATE, &state); 564 adev->pp_force_state_enabled = true; 565 } 566 pm_runtime_mark_last_busy(ddev->dev); 567 pm_runtime_put_autosuspend(ddev->dev); 568 } 569 570 return count; 571 } 572 573 /** 574 * DOC: pp_table 575 * 576 * The amdgpu driver provides a sysfs API for uploading new powerplay 577 * tables. The file pp_table is used for this. Reading the file 578 * will dump the current power play table. Writing to the file 579 * will attempt to upload a new powerplay table and re-initialize 580 * powerplay using that new table. 581 * 582 */ 583 584 static ssize_t amdgpu_get_pp_table(struct device *dev, 585 struct device_attribute *attr, 586 char *buf) 587 { 588 struct drm_device *ddev = dev_get_drvdata(dev); 589 struct amdgpu_device *adev = drm_to_adev(ddev); 590 char *table = NULL; 591 int size, ret; 592 593 if (amdgpu_in_reset(adev)) 594 return -EPERM; 595 if (adev->in_suspend && !adev->in_runpm) 596 return -EPERM; 597 598 ret = pm_runtime_get_sync(ddev->dev); 599 if (ret < 0) { 600 pm_runtime_put_autosuspend(ddev->dev); 601 return ret; 602 } 603 604 if (adev->powerplay.pp_funcs->get_pp_table) { 605 size = amdgpu_dpm_get_pp_table(adev, &table); 606 pm_runtime_mark_last_busy(ddev->dev); 607 pm_runtime_put_autosuspend(ddev->dev); 608 if (size < 0) 609 return size; 610 } else { 611 pm_runtime_mark_last_busy(ddev->dev); 612 pm_runtime_put_autosuspend(ddev->dev); 613 return 0; 614 } 615 616 if (size >= PAGE_SIZE) 617 size = PAGE_SIZE - 1; 618 619 memcpy(buf, table, size); 620 621 return size; 622 } 623 624 static ssize_t amdgpu_set_pp_table(struct device *dev, 625 struct device_attribute *attr, 626 const char *buf, 627 size_t count) 628 { 629 struct drm_device *ddev = dev_get_drvdata(dev); 630 struct amdgpu_device *adev = drm_to_adev(ddev); 631 int ret = 0; 632 633 if (amdgpu_in_reset(adev)) 634 return -EPERM; 635 if (adev->in_suspend && !adev->in_runpm) 636 return -EPERM; 637 638 ret = pm_runtime_get_sync(ddev->dev); 639 if (ret < 0) { 640 pm_runtime_put_autosuspend(ddev->dev); 641 return ret; 642 } 643 644 ret = amdgpu_dpm_set_pp_table(adev, buf, count); 645 if (ret) { 646 pm_runtime_mark_last_busy(ddev->dev); 647 pm_runtime_put_autosuspend(ddev->dev); 648 return ret; 649 } 650 651 pm_runtime_mark_last_busy(ddev->dev); 652 pm_runtime_put_autosuspend(ddev->dev); 653 654 return count; 655 } 656 657 /** 658 * DOC: pp_od_clk_voltage 659 * 660 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages 661 * in each power level within a power state. The pp_od_clk_voltage is used for 662 * this. 663 * 664 * Note that the actual memory controller clock rate are exposed, not 665 * the effective memory clock of the DRAMs. To translate it, use the 666 * following formula: 667 * 668 * Clock conversion (Mhz): 669 * 670 * HBM: effective_memory_clock = memory_controller_clock * 1 671 * 672 * G5: effective_memory_clock = memory_controller_clock * 1 673 * 674 * G6: effective_memory_clock = memory_controller_clock * 2 675 * 676 * DRAM data rate (MT/s): 677 * 678 * HBM: effective_memory_clock * 2 = data_rate 679 * 680 * G5: effective_memory_clock * 4 = data_rate 681 * 682 * G6: effective_memory_clock * 8 = data_rate 683 * 684 * Bandwidth (MB/s): 685 * 686 * data_rate * vram_bit_width / 8 = memory_bandwidth 687 * 688 * Some examples: 689 * 690 * G5 on RX460: 691 * 692 * memory_controller_clock = 1750 Mhz 693 * 694 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz 695 * 696 * data rate = 1750 * 4 = 7000 MT/s 697 * 698 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s 699 * 700 * G6 on RX5700: 701 * 702 * memory_controller_clock = 875 Mhz 703 * 704 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz 705 * 706 * data rate = 1750 * 8 = 14000 MT/s 707 * 708 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s 709 * 710 * < For Vega10 and previous ASICs > 711 * 712 * Reading the file will display: 713 * 714 * - a list of engine clock levels and voltages labeled OD_SCLK 715 * 716 * - a list of memory clock levels and voltages labeled OD_MCLK 717 * 718 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE 719 * 720 * To manually adjust these settings, first select manual using 721 * power_dpm_force_performance_level. Enter a new value for each 722 * level by writing a string that contains "s/m level clock voltage" to 723 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz 724 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at 725 * 810 mV. When you have edited all of the states as needed, write 726 * "c" (commit) to the file to commit your changes. If you want to reset to the 727 * default power levels, write "r" (reset) to the file to reset them. 728 * 729 * 730 * < For Vega20 and newer ASICs > 731 * 732 * Reading the file will display: 733 * 734 * - minimum and maximum engine clock labeled OD_SCLK 735 * 736 * - minimum(not available for Vega20 and Navi1x) and maximum memory 737 * clock labeled OD_MCLK 738 * 739 * - three <frequency, voltage> points labeled OD_VDDC_CURVE. 740 * They can be used to calibrate the sclk voltage curve. 741 * 742 * - voltage offset(in mV) applied on target voltage calculation. 743 * This is available for Sienna Cichlid, Navy Flounder and Dimgrey 744 * Cavefish. For these ASICs, the target voltage calculation can be 745 * illustrated by "voltage = voltage calculated from v/f curve + 746 * overdrive vddgfx offset" 747 * 748 * - a list of valid ranges for sclk, mclk, and voltage curve points 749 * labeled OD_RANGE 750 * 751 * < For APUs > 752 * 753 * Reading the file will display: 754 * 755 * - minimum and maximum engine clock labeled OD_SCLK 756 * 757 * - a list of valid ranges for sclk labeled OD_RANGE 758 * 759 * < For VanGogh > 760 * 761 * Reading the file will display: 762 * 763 * - minimum and maximum engine clock labeled OD_SCLK 764 * - minimum and maximum core clocks labeled OD_CCLK 765 * 766 * - a list of valid ranges for sclk and cclk labeled OD_RANGE 767 * 768 * To manually adjust these settings: 769 * 770 * - First select manual using power_dpm_force_performance_level 771 * 772 * - For clock frequency setting, enter a new value by writing a 773 * string that contains "s/m index clock" to the file. The index 774 * should be 0 if to set minimum clock. And 1 if to set maximum 775 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz. 776 * "m 1 800" will update maximum mclk to be 800Mhz. For core 777 * clocks on VanGogh, the string contains "p core index clock". 778 * E.g., "p 2 0 800" would set the minimum core clock on core 779 * 2 to 800Mhz. 780 * 781 * For sclk voltage curve, enter the new values by writing a 782 * string that contains "vc point clock voltage" to the file. The 783 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will 784 * update point1 with clock set as 300Mhz and voltage as 785 * 600mV. "vc 2 1000 1000" will update point3 with clock set 786 * as 1000Mhz and voltage 1000mV. 787 * 788 * To update the voltage offset applied for gfxclk/voltage calculation, 789 * enter the new value by writing a string that contains "vo offset". 790 * This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish. 791 * And the offset can be a positive or negative value. 792 * 793 * - When you have edited all of the states as needed, write "c" (commit) 794 * to the file to commit your changes 795 * 796 * - If you want to reset to the default power levels, write "r" (reset) 797 * to the file to reset them 798 * 799 */ 800 801 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev, 802 struct device_attribute *attr, 803 const char *buf, 804 size_t count) 805 { 806 struct drm_device *ddev = dev_get_drvdata(dev); 807 struct amdgpu_device *adev = drm_to_adev(ddev); 808 int ret; 809 uint32_t parameter_size = 0; 810 long parameter[64]; 811 char buf_cpy[128]; 812 char *tmp_str; 813 char *sub_str; 814 const char delimiter[3] = {' ', '\n', '\0'}; 815 uint32_t type; 816 817 if (amdgpu_in_reset(adev)) 818 return -EPERM; 819 if (adev->in_suspend && !adev->in_runpm) 820 return -EPERM; 821 822 if (count > 127) 823 return -EINVAL; 824 825 if (*buf == 's') 826 type = PP_OD_EDIT_SCLK_VDDC_TABLE; 827 else if (*buf == 'p') 828 type = PP_OD_EDIT_CCLK_VDDC_TABLE; 829 else if (*buf == 'm') 830 type = PP_OD_EDIT_MCLK_VDDC_TABLE; 831 else if(*buf == 'r') 832 type = PP_OD_RESTORE_DEFAULT_TABLE; 833 else if (*buf == 'c') 834 type = PP_OD_COMMIT_DPM_TABLE; 835 else if (!strncmp(buf, "vc", 2)) 836 type = PP_OD_EDIT_VDDC_CURVE; 837 else if (!strncmp(buf, "vo", 2)) 838 type = PP_OD_EDIT_VDDGFX_OFFSET; 839 else 840 return -EINVAL; 841 842 memcpy(buf_cpy, buf, count+1); 843 844 tmp_str = buf_cpy; 845 846 if ((type == PP_OD_EDIT_VDDC_CURVE) || 847 (type == PP_OD_EDIT_VDDGFX_OFFSET)) 848 tmp_str++; 849 while (isspace(*++tmp_str)); 850 851 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) { 852 if (strlen(sub_str) == 0) 853 continue; 854 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]); 855 if (ret) 856 return -EINVAL; 857 parameter_size++; 858 859 while (isspace(*tmp_str)) 860 tmp_str++; 861 } 862 863 ret = pm_runtime_get_sync(ddev->dev); 864 if (ret < 0) { 865 pm_runtime_put_autosuspend(ddev->dev); 866 return ret; 867 } 868 869 if (adev->powerplay.pp_funcs->set_fine_grain_clk_vol) { 870 ret = amdgpu_dpm_set_fine_grain_clk_vol(adev, type, 871 parameter, 872 parameter_size); 873 if (ret) { 874 pm_runtime_mark_last_busy(ddev->dev); 875 pm_runtime_put_autosuspend(ddev->dev); 876 return -EINVAL; 877 } 878 } 879 880 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) { 881 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type, 882 parameter, parameter_size); 883 if (ret) { 884 pm_runtime_mark_last_busy(ddev->dev); 885 pm_runtime_put_autosuspend(ddev->dev); 886 return -EINVAL; 887 } 888 } 889 890 if (type == PP_OD_COMMIT_DPM_TABLE) { 891 if (adev->powerplay.pp_funcs->dispatch_tasks) { 892 amdgpu_dpm_dispatch_task(adev, 893 AMD_PP_TASK_READJUST_POWER_STATE, 894 NULL); 895 pm_runtime_mark_last_busy(ddev->dev); 896 pm_runtime_put_autosuspend(ddev->dev); 897 return count; 898 } else { 899 pm_runtime_mark_last_busy(ddev->dev); 900 pm_runtime_put_autosuspend(ddev->dev); 901 return -EINVAL; 902 } 903 } 904 905 pm_runtime_mark_last_busy(ddev->dev); 906 pm_runtime_put_autosuspend(ddev->dev); 907 908 return count; 909 } 910 911 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev, 912 struct device_attribute *attr, 913 char *buf) 914 { 915 struct drm_device *ddev = dev_get_drvdata(dev); 916 struct amdgpu_device *adev = drm_to_adev(ddev); 917 ssize_t size; 918 int ret; 919 920 if (amdgpu_in_reset(adev)) 921 return -EPERM; 922 if (adev->in_suspend && !adev->in_runpm) 923 return -EPERM; 924 925 ret = pm_runtime_get_sync(ddev->dev); 926 if (ret < 0) { 927 pm_runtime_put_autosuspend(ddev->dev); 928 return ret; 929 } 930 931 if (adev->powerplay.pp_funcs->print_clock_levels) { 932 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf); 933 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size); 934 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size); 935 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf+size); 936 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size); 937 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf+size); 938 } else { 939 size = sysfs_emit(buf, "\n"); 940 } 941 pm_runtime_mark_last_busy(ddev->dev); 942 pm_runtime_put_autosuspend(ddev->dev); 943 944 return size; 945 } 946 947 /** 948 * DOC: pp_features 949 * 950 * The amdgpu driver provides a sysfs API for adjusting what powerplay 951 * features to be enabled. The file pp_features is used for this. And 952 * this is only available for Vega10 and later dGPUs. 953 * 954 * Reading back the file will show you the followings: 955 * - Current ppfeature masks 956 * - List of the all supported powerplay features with their naming, 957 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled"). 958 * 959 * To manually enable or disable a specific feature, just set or clear 960 * the corresponding bit from original ppfeature masks and input the 961 * new ppfeature masks. 962 */ 963 static ssize_t amdgpu_set_pp_features(struct device *dev, 964 struct device_attribute *attr, 965 const char *buf, 966 size_t count) 967 { 968 struct drm_device *ddev = dev_get_drvdata(dev); 969 struct amdgpu_device *adev = drm_to_adev(ddev); 970 uint64_t featuremask; 971 int ret; 972 973 if (amdgpu_in_reset(adev)) 974 return -EPERM; 975 if (adev->in_suspend && !adev->in_runpm) 976 return -EPERM; 977 978 ret = kstrtou64(buf, 0, &featuremask); 979 if (ret) 980 return -EINVAL; 981 982 ret = pm_runtime_get_sync(ddev->dev); 983 if (ret < 0) { 984 pm_runtime_put_autosuspend(ddev->dev); 985 return ret; 986 } 987 988 if (adev->powerplay.pp_funcs->set_ppfeature_status) { 989 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask); 990 if (ret) { 991 pm_runtime_mark_last_busy(ddev->dev); 992 pm_runtime_put_autosuspend(ddev->dev); 993 return -EINVAL; 994 } 995 } 996 pm_runtime_mark_last_busy(ddev->dev); 997 pm_runtime_put_autosuspend(ddev->dev); 998 999 return count; 1000 } 1001 1002 static ssize_t amdgpu_get_pp_features(struct device *dev, 1003 struct device_attribute *attr, 1004 char *buf) 1005 { 1006 struct drm_device *ddev = dev_get_drvdata(dev); 1007 struct amdgpu_device *adev = drm_to_adev(ddev); 1008 ssize_t size; 1009 int ret; 1010 1011 if (amdgpu_in_reset(adev)) 1012 return -EPERM; 1013 if (adev->in_suspend && !adev->in_runpm) 1014 return -EPERM; 1015 1016 ret = pm_runtime_get_sync(ddev->dev); 1017 if (ret < 0) { 1018 pm_runtime_put_autosuspend(ddev->dev); 1019 return ret; 1020 } 1021 1022 if (adev->powerplay.pp_funcs->get_ppfeature_status) 1023 size = amdgpu_dpm_get_ppfeature_status(adev, buf); 1024 else 1025 size = sysfs_emit(buf, "\n"); 1026 1027 pm_runtime_mark_last_busy(ddev->dev); 1028 pm_runtime_put_autosuspend(ddev->dev); 1029 1030 return size; 1031 } 1032 1033 /** 1034 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie 1035 * 1036 * The amdgpu driver provides a sysfs API for adjusting what power levels 1037 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk, 1038 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for 1039 * this. 1040 * 1041 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for 1042 * Vega10 and later ASICs. 1043 * pp_dpm_fclk interface is only available for Vega20 and later ASICs. 1044 * 1045 * Reading back the files will show you the available power levels within 1046 * the power state and the clock information for those levels. 1047 * 1048 * To manually adjust these states, first select manual using 1049 * power_dpm_force_performance_level. 1050 * Secondly, enter a new value for each level by inputing a string that 1051 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie" 1052 * E.g., 1053 * 1054 * .. code-block:: bash 1055 * 1056 * echo "4 5 6" > pp_dpm_sclk 1057 * 1058 * will enable sclk levels 4, 5, and 6. 1059 * 1060 * NOTE: change to the dcefclk max dpm level is not supported now 1061 */ 1062 1063 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev, 1064 enum pp_clock_type type, 1065 char *buf) 1066 { 1067 struct drm_device *ddev = dev_get_drvdata(dev); 1068 struct amdgpu_device *adev = drm_to_adev(ddev); 1069 ssize_t size; 1070 int ret; 1071 1072 if (amdgpu_in_reset(adev)) 1073 return -EPERM; 1074 if (adev->in_suspend && !adev->in_runpm) 1075 return -EPERM; 1076 1077 ret = pm_runtime_get_sync(ddev->dev); 1078 if (ret < 0) { 1079 pm_runtime_put_autosuspend(ddev->dev); 1080 return ret; 1081 } 1082 1083 if (adev->powerplay.pp_funcs->print_clock_levels) 1084 size = amdgpu_dpm_print_clock_levels(adev, type, buf); 1085 else 1086 size = sysfs_emit(buf, "\n"); 1087 1088 pm_runtime_mark_last_busy(ddev->dev); 1089 pm_runtime_put_autosuspend(ddev->dev); 1090 1091 return size; 1092 } 1093 1094 /* 1095 * Worst case: 32 bits individually specified, in octal at 12 characters 1096 * per line (+1 for \n). 1097 */ 1098 #define AMDGPU_MASK_BUF_MAX (32 * 13) 1099 1100 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask) 1101 { 1102 int ret; 1103 unsigned long level; 1104 char *sub_str = NULL; 1105 char *tmp; 1106 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1]; 1107 const char delimiter[3] = {' ', '\n', '\0'}; 1108 size_t bytes; 1109 1110 *mask = 0; 1111 1112 bytes = min(count, sizeof(buf_cpy) - 1); 1113 memcpy(buf_cpy, buf, bytes); 1114 buf_cpy[bytes] = '\0'; 1115 tmp = buf_cpy; 1116 while ((sub_str = strsep(&tmp, delimiter)) != NULL) { 1117 if (strlen(sub_str)) { 1118 ret = kstrtoul(sub_str, 0, &level); 1119 if (ret || level > 31) 1120 return -EINVAL; 1121 *mask |= 1 << level; 1122 } else 1123 break; 1124 } 1125 1126 return 0; 1127 } 1128 1129 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev, 1130 enum pp_clock_type type, 1131 const char *buf, 1132 size_t count) 1133 { 1134 struct drm_device *ddev = dev_get_drvdata(dev); 1135 struct amdgpu_device *adev = drm_to_adev(ddev); 1136 int ret; 1137 uint32_t mask = 0; 1138 1139 if (amdgpu_in_reset(adev)) 1140 return -EPERM; 1141 if (adev->in_suspend && !adev->in_runpm) 1142 return -EPERM; 1143 1144 ret = amdgpu_read_mask(buf, count, &mask); 1145 if (ret) 1146 return ret; 1147 1148 ret = pm_runtime_get_sync(ddev->dev); 1149 if (ret < 0) { 1150 pm_runtime_put_autosuspend(ddev->dev); 1151 return ret; 1152 } 1153 1154 if (adev->powerplay.pp_funcs->force_clock_level) 1155 ret = amdgpu_dpm_force_clock_level(adev, type, mask); 1156 else 1157 ret = 0; 1158 1159 pm_runtime_mark_last_busy(ddev->dev); 1160 pm_runtime_put_autosuspend(ddev->dev); 1161 1162 if (ret) 1163 return -EINVAL; 1164 1165 return count; 1166 } 1167 1168 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev, 1169 struct device_attribute *attr, 1170 char *buf) 1171 { 1172 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf); 1173 } 1174 1175 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev, 1176 struct device_attribute *attr, 1177 const char *buf, 1178 size_t count) 1179 { 1180 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count); 1181 } 1182 1183 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev, 1184 struct device_attribute *attr, 1185 char *buf) 1186 { 1187 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf); 1188 } 1189 1190 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev, 1191 struct device_attribute *attr, 1192 const char *buf, 1193 size_t count) 1194 { 1195 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count); 1196 } 1197 1198 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev, 1199 struct device_attribute *attr, 1200 char *buf) 1201 { 1202 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf); 1203 } 1204 1205 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev, 1206 struct device_attribute *attr, 1207 const char *buf, 1208 size_t count) 1209 { 1210 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count); 1211 } 1212 1213 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev, 1214 struct device_attribute *attr, 1215 char *buf) 1216 { 1217 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf); 1218 } 1219 1220 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev, 1221 struct device_attribute *attr, 1222 const char *buf, 1223 size_t count) 1224 { 1225 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count); 1226 } 1227 1228 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev, 1229 struct device_attribute *attr, 1230 char *buf) 1231 { 1232 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf); 1233 } 1234 1235 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev, 1236 struct device_attribute *attr, 1237 const char *buf, 1238 size_t count) 1239 { 1240 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count); 1241 } 1242 1243 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev, 1244 struct device_attribute *attr, 1245 char *buf) 1246 { 1247 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf); 1248 } 1249 1250 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev, 1251 struct device_attribute *attr, 1252 const char *buf, 1253 size_t count) 1254 { 1255 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count); 1256 } 1257 1258 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev, 1259 struct device_attribute *attr, 1260 char *buf) 1261 { 1262 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf); 1263 } 1264 1265 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev, 1266 struct device_attribute *attr, 1267 const char *buf, 1268 size_t count) 1269 { 1270 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count); 1271 } 1272 1273 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev, 1274 struct device_attribute *attr, 1275 char *buf) 1276 { 1277 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf); 1278 } 1279 1280 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev, 1281 struct device_attribute *attr, 1282 const char *buf, 1283 size_t count) 1284 { 1285 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count); 1286 } 1287 1288 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev, 1289 struct device_attribute *attr, 1290 char *buf) 1291 { 1292 struct drm_device *ddev = dev_get_drvdata(dev); 1293 struct amdgpu_device *adev = drm_to_adev(ddev); 1294 uint32_t value = 0; 1295 int ret; 1296 1297 if (amdgpu_in_reset(adev)) 1298 return -EPERM; 1299 if (adev->in_suspend && !adev->in_runpm) 1300 return -EPERM; 1301 1302 ret = pm_runtime_get_sync(ddev->dev); 1303 if (ret < 0) { 1304 pm_runtime_put_autosuspend(ddev->dev); 1305 return ret; 1306 } 1307 1308 if (is_support_sw_smu(adev)) 1309 value = 0; 1310 else if (adev->powerplay.pp_funcs->get_sclk_od) 1311 value = amdgpu_dpm_get_sclk_od(adev); 1312 1313 pm_runtime_mark_last_busy(ddev->dev); 1314 pm_runtime_put_autosuspend(ddev->dev); 1315 1316 return sysfs_emit(buf, "%d\n", value); 1317 } 1318 1319 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev, 1320 struct device_attribute *attr, 1321 const char *buf, 1322 size_t count) 1323 { 1324 struct drm_device *ddev = dev_get_drvdata(dev); 1325 struct amdgpu_device *adev = drm_to_adev(ddev); 1326 int ret; 1327 long int value; 1328 1329 if (amdgpu_in_reset(adev)) 1330 return -EPERM; 1331 if (adev->in_suspend && !adev->in_runpm) 1332 return -EPERM; 1333 1334 ret = kstrtol(buf, 0, &value); 1335 1336 if (ret) 1337 return -EINVAL; 1338 1339 ret = pm_runtime_get_sync(ddev->dev); 1340 if (ret < 0) { 1341 pm_runtime_put_autosuspend(ddev->dev); 1342 return ret; 1343 } 1344 1345 if (is_support_sw_smu(adev)) { 1346 value = 0; 1347 } else { 1348 if (adev->powerplay.pp_funcs->set_sclk_od) 1349 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value); 1350 1351 if (adev->powerplay.pp_funcs->dispatch_tasks) { 1352 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL); 1353 } else { 1354 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps; 1355 amdgpu_pm_compute_clocks(adev); 1356 } 1357 } 1358 1359 pm_runtime_mark_last_busy(ddev->dev); 1360 pm_runtime_put_autosuspend(ddev->dev); 1361 1362 return count; 1363 } 1364 1365 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev, 1366 struct device_attribute *attr, 1367 char *buf) 1368 { 1369 struct drm_device *ddev = dev_get_drvdata(dev); 1370 struct amdgpu_device *adev = drm_to_adev(ddev); 1371 uint32_t value = 0; 1372 int ret; 1373 1374 if (amdgpu_in_reset(adev)) 1375 return -EPERM; 1376 if (adev->in_suspend && !adev->in_runpm) 1377 return -EPERM; 1378 1379 ret = pm_runtime_get_sync(ddev->dev); 1380 if (ret < 0) { 1381 pm_runtime_put_autosuspend(ddev->dev); 1382 return ret; 1383 } 1384 1385 if (is_support_sw_smu(adev)) 1386 value = 0; 1387 else if (adev->powerplay.pp_funcs->get_mclk_od) 1388 value = amdgpu_dpm_get_mclk_od(adev); 1389 1390 pm_runtime_mark_last_busy(ddev->dev); 1391 pm_runtime_put_autosuspend(ddev->dev); 1392 1393 return sysfs_emit(buf, "%d\n", value); 1394 } 1395 1396 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev, 1397 struct device_attribute *attr, 1398 const char *buf, 1399 size_t count) 1400 { 1401 struct drm_device *ddev = dev_get_drvdata(dev); 1402 struct amdgpu_device *adev = drm_to_adev(ddev); 1403 int ret; 1404 long int value; 1405 1406 if (amdgpu_in_reset(adev)) 1407 return -EPERM; 1408 if (adev->in_suspend && !adev->in_runpm) 1409 return -EPERM; 1410 1411 ret = kstrtol(buf, 0, &value); 1412 1413 if (ret) 1414 return -EINVAL; 1415 1416 ret = pm_runtime_get_sync(ddev->dev); 1417 if (ret < 0) { 1418 pm_runtime_put_autosuspend(ddev->dev); 1419 return ret; 1420 } 1421 1422 if (is_support_sw_smu(adev)) { 1423 value = 0; 1424 } else { 1425 if (adev->powerplay.pp_funcs->set_mclk_od) 1426 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value); 1427 1428 if (adev->powerplay.pp_funcs->dispatch_tasks) { 1429 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL); 1430 } else { 1431 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps; 1432 amdgpu_pm_compute_clocks(adev); 1433 } 1434 } 1435 1436 pm_runtime_mark_last_busy(ddev->dev); 1437 pm_runtime_put_autosuspend(ddev->dev); 1438 1439 return count; 1440 } 1441 1442 /** 1443 * DOC: pp_power_profile_mode 1444 * 1445 * The amdgpu driver provides a sysfs API for adjusting the heuristics 1446 * related to switching between power levels in a power state. The file 1447 * pp_power_profile_mode is used for this. 1448 * 1449 * Reading this file outputs a list of all of the predefined power profiles 1450 * and the relevant heuristics settings for that profile. 1451 * 1452 * To select a profile or create a custom profile, first select manual using 1453 * power_dpm_force_performance_level. Writing the number of a predefined 1454 * profile to pp_power_profile_mode will enable those heuristics. To 1455 * create a custom set of heuristics, write a string of numbers to the file 1456 * starting with the number of the custom profile along with a setting 1457 * for each heuristic parameter. Due to differences across asic families 1458 * the heuristic parameters vary from family to family. 1459 * 1460 */ 1461 1462 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev, 1463 struct device_attribute *attr, 1464 char *buf) 1465 { 1466 struct drm_device *ddev = dev_get_drvdata(dev); 1467 struct amdgpu_device *adev = drm_to_adev(ddev); 1468 ssize_t size; 1469 int ret; 1470 1471 if (amdgpu_in_reset(adev)) 1472 return -EPERM; 1473 if (adev->in_suspend && !adev->in_runpm) 1474 return -EPERM; 1475 1476 ret = pm_runtime_get_sync(ddev->dev); 1477 if (ret < 0) { 1478 pm_runtime_put_autosuspend(ddev->dev); 1479 return ret; 1480 } 1481 1482 if (adev->powerplay.pp_funcs->get_power_profile_mode) 1483 size = amdgpu_dpm_get_power_profile_mode(adev, buf); 1484 else 1485 size = sysfs_emit(buf, "\n"); 1486 1487 pm_runtime_mark_last_busy(ddev->dev); 1488 pm_runtime_put_autosuspend(ddev->dev); 1489 1490 return size; 1491 } 1492 1493 1494 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev, 1495 struct device_attribute *attr, 1496 const char *buf, 1497 size_t count) 1498 { 1499 int ret; 1500 struct drm_device *ddev = dev_get_drvdata(dev); 1501 struct amdgpu_device *adev = drm_to_adev(ddev); 1502 uint32_t parameter_size = 0; 1503 long parameter[64]; 1504 char *sub_str, buf_cpy[128]; 1505 char *tmp_str; 1506 uint32_t i = 0; 1507 char tmp[2]; 1508 long int profile_mode = 0; 1509 const char delimiter[3] = {' ', '\n', '\0'}; 1510 1511 if (amdgpu_in_reset(adev)) 1512 return -EPERM; 1513 if (adev->in_suspend && !adev->in_runpm) 1514 return -EPERM; 1515 1516 tmp[0] = *(buf); 1517 tmp[1] = '\0'; 1518 ret = kstrtol(tmp, 0, &profile_mode); 1519 if (ret) 1520 return -EINVAL; 1521 1522 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) { 1523 if (count < 2 || count > 127) 1524 return -EINVAL; 1525 while (isspace(*++buf)) 1526 i++; 1527 memcpy(buf_cpy, buf, count-i); 1528 tmp_str = buf_cpy; 1529 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) { 1530 if (strlen(sub_str) == 0) 1531 continue; 1532 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]); 1533 if (ret) 1534 return -EINVAL; 1535 parameter_size++; 1536 while (isspace(*tmp_str)) 1537 tmp_str++; 1538 } 1539 } 1540 parameter[parameter_size] = profile_mode; 1541 1542 ret = pm_runtime_get_sync(ddev->dev); 1543 if (ret < 0) { 1544 pm_runtime_put_autosuspend(ddev->dev); 1545 return ret; 1546 } 1547 1548 if (adev->powerplay.pp_funcs->set_power_profile_mode) 1549 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size); 1550 1551 pm_runtime_mark_last_busy(ddev->dev); 1552 pm_runtime_put_autosuspend(ddev->dev); 1553 1554 if (!ret) 1555 return count; 1556 1557 return -EINVAL; 1558 } 1559 1560 /** 1561 * DOC: gpu_busy_percent 1562 * 1563 * The amdgpu driver provides a sysfs API for reading how busy the GPU 1564 * is as a percentage. The file gpu_busy_percent is used for this. 1565 * The SMU firmware computes a percentage of load based on the 1566 * aggregate activity level in the IP cores. 1567 */ 1568 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev, 1569 struct device_attribute *attr, 1570 char *buf) 1571 { 1572 struct drm_device *ddev = dev_get_drvdata(dev); 1573 struct amdgpu_device *adev = drm_to_adev(ddev); 1574 int r, value, size = sizeof(value); 1575 1576 if (amdgpu_in_reset(adev)) 1577 return -EPERM; 1578 if (adev->in_suspend && !adev->in_runpm) 1579 return -EPERM; 1580 1581 r = pm_runtime_get_sync(ddev->dev); 1582 if (r < 0) { 1583 pm_runtime_put_autosuspend(ddev->dev); 1584 return r; 1585 } 1586 1587 /* read the IP busy sensor */ 1588 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, 1589 (void *)&value, &size); 1590 1591 pm_runtime_mark_last_busy(ddev->dev); 1592 pm_runtime_put_autosuspend(ddev->dev); 1593 1594 if (r) 1595 return r; 1596 1597 return sysfs_emit(buf, "%d\n", value); 1598 } 1599 1600 /** 1601 * DOC: mem_busy_percent 1602 * 1603 * The amdgpu driver provides a sysfs API for reading how busy the VRAM 1604 * is as a percentage. The file mem_busy_percent is used for this. 1605 * The SMU firmware computes a percentage of load based on the 1606 * aggregate activity level in the IP cores. 1607 */ 1608 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev, 1609 struct device_attribute *attr, 1610 char *buf) 1611 { 1612 struct drm_device *ddev = dev_get_drvdata(dev); 1613 struct amdgpu_device *adev = drm_to_adev(ddev); 1614 int r, value, size = sizeof(value); 1615 1616 if (amdgpu_in_reset(adev)) 1617 return -EPERM; 1618 if (adev->in_suspend && !adev->in_runpm) 1619 return -EPERM; 1620 1621 r = pm_runtime_get_sync(ddev->dev); 1622 if (r < 0) { 1623 pm_runtime_put_autosuspend(ddev->dev); 1624 return r; 1625 } 1626 1627 /* read the IP busy sensor */ 1628 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, 1629 (void *)&value, &size); 1630 1631 pm_runtime_mark_last_busy(ddev->dev); 1632 pm_runtime_put_autosuspend(ddev->dev); 1633 1634 if (r) 1635 return r; 1636 1637 return sysfs_emit(buf, "%d\n", value); 1638 } 1639 1640 /** 1641 * DOC: pcie_bw 1642 * 1643 * The amdgpu driver provides a sysfs API for estimating how much data 1644 * has been received and sent by the GPU in the last second through PCIe. 1645 * The file pcie_bw is used for this. 1646 * The Perf counters count the number of received and sent messages and return 1647 * those values, as well as the maximum payload size of a PCIe packet (mps). 1648 * Note that it is not possible to easily and quickly obtain the size of each 1649 * packet transmitted, so we output the max payload size (mps) to allow for 1650 * quick estimation of the PCIe bandwidth usage 1651 */ 1652 static ssize_t amdgpu_get_pcie_bw(struct device *dev, 1653 struct device_attribute *attr, 1654 char *buf) 1655 { 1656 struct drm_device *ddev = dev_get_drvdata(dev); 1657 struct amdgpu_device *adev = drm_to_adev(ddev); 1658 uint64_t count0 = 0, count1 = 0; 1659 int ret; 1660 1661 if (amdgpu_in_reset(adev)) 1662 return -EPERM; 1663 if (adev->in_suspend && !adev->in_runpm) 1664 return -EPERM; 1665 1666 if (adev->flags & AMD_IS_APU) 1667 return -ENODATA; 1668 1669 if (!adev->asic_funcs->get_pcie_usage) 1670 return -ENODATA; 1671 1672 ret = pm_runtime_get_sync(ddev->dev); 1673 if (ret < 0) { 1674 pm_runtime_put_autosuspend(ddev->dev); 1675 return ret; 1676 } 1677 1678 amdgpu_asic_get_pcie_usage(adev, &count0, &count1); 1679 1680 pm_runtime_mark_last_busy(ddev->dev); 1681 pm_runtime_put_autosuspend(ddev->dev); 1682 1683 return sysfs_emit(buf, "%llu %llu %i\n", 1684 count0, count1, pcie_get_mps(adev->pdev)); 1685 } 1686 1687 /** 1688 * DOC: unique_id 1689 * 1690 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU 1691 * The file unique_id is used for this. 1692 * This will provide a Unique ID that will persist from machine to machine 1693 * 1694 * NOTE: This will only work for GFX9 and newer. This file will be absent 1695 * on unsupported ASICs (GFX8 and older) 1696 */ 1697 static ssize_t amdgpu_get_unique_id(struct device *dev, 1698 struct device_attribute *attr, 1699 char *buf) 1700 { 1701 struct drm_device *ddev = dev_get_drvdata(dev); 1702 struct amdgpu_device *adev = drm_to_adev(ddev); 1703 1704 if (amdgpu_in_reset(adev)) 1705 return -EPERM; 1706 if (adev->in_suspend && !adev->in_runpm) 1707 return -EPERM; 1708 1709 if (adev->unique_id) 1710 return sysfs_emit(buf, "%016llx\n", adev->unique_id); 1711 1712 return 0; 1713 } 1714 1715 /** 1716 * DOC: thermal_throttling_logging 1717 * 1718 * Thermal throttling pulls down the clock frequency and thus the performance. 1719 * It's an useful mechanism to protect the chip from overheating. Since it 1720 * impacts performance, the user controls whether it is enabled and if so, 1721 * the log frequency. 1722 * 1723 * Reading back the file shows you the status(enabled or disabled) and 1724 * the interval(in seconds) between each thermal logging. 1725 * 1726 * Writing an integer to the file, sets a new logging interval, in seconds. 1727 * The value should be between 1 and 3600. If the value is less than 1, 1728 * thermal logging is disabled. Values greater than 3600 are ignored. 1729 */ 1730 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev, 1731 struct device_attribute *attr, 1732 char *buf) 1733 { 1734 struct drm_device *ddev = dev_get_drvdata(dev); 1735 struct amdgpu_device *adev = drm_to_adev(ddev); 1736 1737 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n", 1738 adev_to_drm(adev)->unique, 1739 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled", 1740 adev->throttling_logging_rs.interval / HZ + 1); 1741 } 1742 1743 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev, 1744 struct device_attribute *attr, 1745 const char *buf, 1746 size_t count) 1747 { 1748 struct drm_device *ddev = dev_get_drvdata(dev); 1749 struct amdgpu_device *adev = drm_to_adev(ddev); 1750 long throttling_logging_interval; 1751 unsigned long flags; 1752 int ret = 0; 1753 1754 ret = kstrtol(buf, 0, &throttling_logging_interval); 1755 if (ret) 1756 return ret; 1757 1758 if (throttling_logging_interval > 3600) 1759 return -EINVAL; 1760 1761 if (throttling_logging_interval > 0) { 1762 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags); 1763 /* 1764 * Reset the ratelimit timer internals. 1765 * This can effectively restart the timer. 1766 */ 1767 adev->throttling_logging_rs.interval = 1768 (throttling_logging_interval - 1) * HZ; 1769 adev->throttling_logging_rs.begin = 0; 1770 adev->throttling_logging_rs.printed = 0; 1771 adev->throttling_logging_rs.missed = 0; 1772 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags); 1773 1774 atomic_set(&adev->throttling_logging_enabled, 1); 1775 } else { 1776 atomic_set(&adev->throttling_logging_enabled, 0); 1777 } 1778 1779 return count; 1780 } 1781 1782 /** 1783 * DOC: gpu_metrics 1784 * 1785 * The amdgpu driver provides a sysfs API for retrieving current gpu 1786 * metrics data. The file gpu_metrics is used for this. Reading the 1787 * file will dump all the current gpu metrics data. 1788 * 1789 * These data include temperature, frequency, engines utilization, 1790 * power consume, throttler status, fan speed and cpu core statistics( 1791 * available for APU only). That's it will give a snapshot of all sensors 1792 * at the same time. 1793 */ 1794 static ssize_t amdgpu_get_gpu_metrics(struct device *dev, 1795 struct device_attribute *attr, 1796 char *buf) 1797 { 1798 struct drm_device *ddev = dev_get_drvdata(dev); 1799 struct amdgpu_device *adev = drm_to_adev(ddev); 1800 void *gpu_metrics; 1801 ssize_t size = 0; 1802 int ret; 1803 1804 if (amdgpu_in_reset(adev)) 1805 return -EPERM; 1806 if (adev->in_suspend && !adev->in_runpm) 1807 return -EPERM; 1808 1809 ret = pm_runtime_get_sync(ddev->dev); 1810 if (ret < 0) { 1811 pm_runtime_put_autosuspend(ddev->dev); 1812 return ret; 1813 } 1814 1815 if (adev->powerplay.pp_funcs->get_gpu_metrics) 1816 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics); 1817 1818 if (size <= 0) 1819 goto out; 1820 1821 if (size >= PAGE_SIZE) 1822 size = PAGE_SIZE - 1; 1823 1824 memcpy(buf, gpu_metrics, size); 1825 1826 out: 1827 pm_runtime_mark_last_busy(ddev->dev); 1828 pm_runtime_put_autosuspend(ddev->dev); 1829 1830 return size; 1831 } 1832 1833 /** 1834 * DOC: smartshift_apu_power 1835 * 1836 * The amdgpu driver provides a sysfs API for reporting APU power 1837 * share if it supports smartshift. The value is expressed as 1838 * the proportion of stapm limit where stapm limit is the total APU 1839 * power limit. The result is in percentage. If APU power is 130% of 1840 * STAPM, then APU is using 30% of the dGPU's headroom. 1841 */ 1842 1843 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr, 1844 char *buf) 1845 { 1846 struct drm_device *ddev = dev_get_drvdata(dev); 1847 struct amdgpu_device *adev = drm_to_adev(ddev); 1848 uint32_t ss_power, size; 1849 int r = 0; 1850 1851 if (amdgpu_in_reset(adev)) 1852 return -EPERM; 1853 if (adev->in_suspend && !adev->in_runpm) 1854 return -EPERM; 1855 1856 r = pm_runtime_get_sync(ddev->dev); 1857 if (r < 0) { 1858 pm_runtime_put_autosuspend(ddev->dev); 1859 return r; 1860 } 1861 1862 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 1863 (void *)&ss_power, &size); 1864 if (r) 1865 goto out; 1866 1867 r = sysfs_emit(buf, "%u%%\n", ss_power); 1868 1869 out: 1870 pm_runtime_mark_last_busy(ddev->dev); 1871 pm_runtime_put_autosuspend(ddev->dev); 1872 return r; 1873 } 1874 1875 /** 1876 * DOC: smartshift_dgpu_power 1877 * 1878 * The amdgpu driver provides a sysfs API for reporting the dGPU power 1879 * share if the device is in HG and supports smartshift. The value 1880 * is expressed as the proportion of stapm limit where stapm limit 1881 * is the total APU power limit. The value is in percentage. If dGPU 1882 * power is 20% higher than STAPM power(120%), it's using 20% of the 1883 * APU's power headroom. 1884 */ 1885 1886 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr, 1887 char *buf) 1888 { 1889 struct drm_device *ddev = dev_get_drvdata(dev); 1890 struct amdgpu_device *adev = drm_to_adev(ddev); 1891 uint32_t ss_power, size; 1892 int r = 0; 1893 1894 if (amdgpu_in_reset(adev)) 1895 return -EPERM; 1896 if (adev->in_suspend && !adev->in_runpm) 1897 return -EPERM; 1898 1899 r = pm_runtime_get_sync(ddev->dev); 1900 if (r < 0) { 1901 pm_runtime_put_autosuspend(ddev->dev); 1902 return r; 1903 } 1904 1905 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 1906 (void *)&ss_power, &size); 1907 1908 if (r) 1909 goto out; 1910 1911 r = sysfs_emit(buf, "%u%%\n", ss_power); 1912 1913 out: 1914 pm_runtime_mark_last_busy(ddev->dev); 1915 pm_runtime_put_autosuspend(ddev->dev); 1916 return r; 1917 } 1918 1919 /** 1920 * DOC: smartshift_bias 1921 * 1922 * The amdgpu driver provides a sysfs API for reporting the 1923 * smartshift(SS2.0) bias level. The value ranges from -100 to 100 1924 * and the default is 0. -100 sets maximum preference to APU 1925 * and 100 sets max perference to dGPU. 1926 */ 1927 1928 static ssize_t amdgpu_get_smartshift_bias(struct device *dev, 1929 struct device_attribute *attr, 1930 char *buf) 1931 { 1932 int r = 0; 1933 1934 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias); 1935 1936 return r; 1937 } 1938 1939 static ssize_t amdgpu_set_smartshift_bias(struct device *dev, 1940 struct device_attribute *attr, 1941 const char *buf, size_t count) 1942 { 1943 struct drm_device *ddev = dev_get_drvdata(dev); 1944 struct amdgpu_device *adev = drm_to_adev(ddev); 1945 int r = 0; 1946 int bias = 0; 1947 1948 if (amdgpu_in_reset(adev)) 1949 return -EPERM; 1950 if (adev->in_suspend && !adev->in_runpm) 1951 return -EPERM; 1952 1953 r = pm_runtime_get_sync(ddev->dev); 1954 if (r < 0) { 1955 pm_runtime_put_autosuspend(ddev->dev); 1956 return r; 1957 } 1958 1959 r = kstrtoint(buf, 10, &bias); 1960 if (r) 1961 goto out; 1962 1963 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS) 1964 bias = AMDGPU_SMARTSHIFT_MAX_BIAS; 1965 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS) 1966 bias = AMDGPU_SMARTSHIFT_MIN_BIAS; 1967 1968 amdgpu_smartshift_bias = bias; 1969 r = count; 1970 1971 /* TODO: upadte bias level with SMU message */ 1972 1973 out: 1974 pm_runtime_mark_last_busy(ddev->dev); 1975 pm_runtime_put_autosuspend(ddev->dev); 1976 return r; 1977 } 1978 1979 1980 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1981 uint32_t mask, enum amdgpu_device_attr_states *states) 1982 { 1983 uint32_t ss_power, size; 1984 1985 if (!amdgpu_acpi_is_power_shift_control_supported()) 1986 *states = ATTR_STATE_UNSUPPORTED; 1987 else if ((adev->flags & AMD_IS_PX) && 1988 !amdgpu_device_supports_smart_shift(adev_to_drm(adev))) 1989 *states = ATTR_STATE_UNSUPPORTED; 1990 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 1991 (void *)&ss_power, &size)) 1992 *states = ATTR_STATE_UNSUPPORTED; 1993 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 1994 (void *)&ss_power, &size)) 1995 *states = ATTR_STATE_UNSUPPORTED; 1996 1997 return 0; 1998 } 1999 2000 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2001 uint32_t mask, enum amdgpu_device_attr_states *states) 2002 { 2003 uint32_t ss_power, size; 2004 2005 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev))) 2006 *states = ATTR_STATE_UNSUPPORTED; 2007 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 2008 (void *)&ss_power, &size)) 2009 *states = ATTR_STATE_UNSUPPORTED; 2010 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 2011 (void *)&ss_power, &size)) 2012 *states = ATTR_STATE_UNSUPPORTED; 2013 2014 return 0; 2015 } 2016 2017 static struct amdgpu_device_attr amdgpu_device_attrs[] = { 2018 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2019 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2020 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2021 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2022 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2023 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2024 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2025 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2026 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2027 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2028 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2029 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2030 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC), 2031 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC), 2032 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC), 2033 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC), 2034 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2035 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC), 2036 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2037 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2038 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC), 2039 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2040 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2041 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2042 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2043 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC, 2044 .attr_update = ss_power_attr_update), 2045 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC, 2046 .attr_update = ss_power_attr_update), 2047 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC, 2048 .attr_update = ss_bias_attr_update), 2049 }; 2050 2051 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2052 uint32_t mask, enum amdgpu_device_attr_states *states) 2053 { 2054 struct device_attribute *dev_attr = &attr->dev_attr; 2055 const char *attr_name = dev_attr->attr.name; 2056 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle; 2057 enum amd_asic_type asic_type = adev->asic_type; 2058 2059 if (!(attr->flags & mask)) { 2060 *states = ATTR_STATE_UNSUPPORTED; 2061 return 0; 2062 } 2063 2064 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name)) 2065 2066 if (DEVICE_ATTR_IS(pp_dpm_socclk)) { 2067 if (asic_type < CHIP_VEGA10) 2068 *states = ATTR_STATE_UNSUPPORTED; 2069 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) { 2070 if (asic_type < CHIP_VEGA10 || 2071 asic_type == CHIP_ARCTURUS || 2072 asic_type == CHIP_ALDEBARAN) 2073 *states = ATTR_STATE_UNSUPPORTED; 2074 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) { 2075 if (asic_type < CHIP_VEGA20) 2076 *states = ATTR_STATE_UNSUPPORTED; 2077 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) { 2078 *states = ATTR_STATE_UNSUPPORTED; 2079 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) || 2080 (is_support_sw_smu(adev) && adev->smu.is_apu) || 2081 (!is_support_sw_smu(adev) && hwmgr->od_enabled)) 2082 *states = ATTR_STATE_SUPPORTED; 2083 } else if (DEVICE_ATTR_IS(mem_busy_percent)) { 2084 if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10) 2085 *states = ATTR_STATE_UNSUPPORTED; 2086 } else if (DEVICE_ATTR_IS(pcie_bw)) { 2087 /* PCIe Perf counters won't work on APU nodes */ 2088 if (adev->flags & AMD_IS_APU) 2089 *states = ATTR_STATE_UNSUPPORTED; 2090 } else if (DEVICE_ATTR_IS(unique_id)) { 2091 if (asic_type != CHIP_VEGA10 && 2092 asic_type != CHIP_VEGA20 && 2093 asic_type != CHIP_ARCTURUS) 2094 *states = ATTR_STATE_UNSUPPORTED; 2095 } else if (DEVICE_ATTR_IS(pp_features)) { 2096 if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10) 2097 *states = ATTR_STATE_UNSUPPORTED; 2098 } else if (DEVICE_ATTR_IS(gpu_metrics)) { 2099 if (asic_type < CHIP_VEGA12) 2100 *states = ATTR_STATE_UNSUPPORTED; 2101 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) { 2102 if (!(asic_type == CHIP_VANGOGH || asic_type == CHIP_SIENNA_CICHLID)) 2103 *states = ATTR_STATE_UNSUPPORTED; 2104 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) { 2105 if (!(asic_type == CHIP_VANGOGH || asic_type == CHIP_SIENNA_CICHLID)) 2106 *states = ATTR_STATE_UNSUPPORTED; 2107 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) { 2108 if (!adev->powerplay.pp_funcs->get_power_profile_mode || 2109 amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP) 2110 *states = ATTR_STATE_UNSUPPORTED; 2111 } 2112 2113 switch (asic_type) { 2114 case CHIP_ARCTURUS: 2115 case CHIP_ALDEBARAN: 2116 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */ 2117 if (DEVICE_ATTR_IS(pp_dpm_mclk) || 2118 DEVICE_ATTR_IS(pp_dpm_socclk) || 2119 DEVICE_ATTR_IS(pp_dpm_fclk)) { 2120 dev_attr->attr.mode &= ~S_IWUGO; 2121 dev_attr->store = NULL; 2122 } 2123 break; 2124 default: 2125 break; 2126 } 2127 2128 if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) { 2129 /* SMU MP1 does not support dcefclk level setting */ 2130 if (asic_type >= CHIP_NAVI10) { 2131 dev_attr->attr.mode &= ~S_IWUGO; 2132 dev_attr->store = NULL; 2133 } 2134 } 2135 2136 #undef DEVICE_ATTR_IS 2137 2138 return 0; 2139 } 2140 2141 2142 static int amdgpu_device_attr_create(struct amdgpu_device *adev, 2143 struct amdgpu_device_attr *attr, 2144 uint32_t mask, struct list_head *attr_list) 2145 { 2146 int ret = 0; 2147 struct device_attribute *dev_attr = &attr->dev_attr; 2148 const char *name = dev_attr->attr.name; 2149 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED; 2150 struct amdgpu_device_attr_entry *attr_entry; 2151 2152 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2153 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update; 2154 2155 BUG_ON(!attr); 2156 2157 attr_update = attr->attr_update ? attr->attr_update : default_attr_update; 2158 2159 ret = attr_update(adev, attr, mask, &attr_states); 2160 if (ret) { 2161 dev_err(adev->dev, "failed to update device file %s, ret = %d\n", 2162 name, ret); 2163 return ret; 2164 } 2165 2166 if (attr_states == ATTR_STATE_UNSUPPORTED) 2167 return 0; 2168 2169 ret = device_create_file(adev->dev, dev_attr); 2170 if (ret) { 2171 dev_err(adev->dev, "failed to create device file %s, ret = %d\n", 2172 name, ret); 2173 } 2174 2175 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL); 2176 if (!attr_entry) 2177 return -ENOMEM; 2178 2179 attr_entry->attr = attr; 2180 INIT_LIST_HEAD(&attr_entry->entry); 2181 2182 list_add_tail(&attr_entry->entry, attr_list); 2183 2184 return ret; 2185 } 2186 2187 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr) 2188 { 2189 struct device_attribute *dev_attr = &attr->dev_attr; 2190 2191 device_remove_file(adev->dev, dev_attr); 2192 } 2193 2194 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2195 struct list_head *attr_list); 2196 2197 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev, 2198 struct amdgpu_device_attr *attrs, 2199 uint32_t counts, 2200 uint32_t mask, 2201 struct list_head *attr_list) 2202 { 2203 int ret = 0; 2204 uint32_t i = 0; 2205 2206 for (i = 0; i < counts; i++) { 2207 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list); 2208 if (ret) 2209 goto failed; 2210 } 2211 2212 return 0; 2213 2214 failed: 2215 amdgpu_device_attr_remove_groups(adev, attr_list); 2216 2217 return ret; 2218 } 2219 2220 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2221 struct list_head *attr_list) 2222 { 2223 struct amdgpu_device_attr_entry *entry, *entry_tmp; 2224 2225 if (list_empty(attr_list)) 2226 return ; 2227 2228 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) { 2229 amdgpu_device_attr_remove(adev, entry->attr); 2230 list_del(&entry->entry); 2231 kfree(entry); 2232 } 2233 } 2234 2235 static ssize_t amdgpu_hwmon_show_temp(struct device *dev, 2236 struct device_attribute *attr, 2237 char *buf) 2238 { 2239 struct amdgpu_device *adev = dev_get_drvdata(dev); 2240 int channel = to_sensor_dev_attr(attr)->index; 2241 int r, temp = 0, size = sizeof(temp); 2242 2243 if (amdgpu_in_reset(adev)) 2244 return -EPERM; 2245 if (adev->in_suspend && !adev->in_runpm) 2246 return -EPERM; 2247 2248 if (channel >= PP_TEMP_MAX) 2249 return -EINVAL; 2250 2251 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2252 if (r < 0) { 2253 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2254 return r; 2255 } 2256 2257 switch (channel) { 2258 case PP_TEMP_JUNCTION: 2259 /* get current junction temperature */ 2260 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP, 2261 (void *)&temp, &size); 2262 break; 2263 case PP_TEMP_EDGE: 2264 /* get current edge temperature */ 2265 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP, 2266 (void *)&temp, &size); 2267 break; 2268 case PP_TEMP_MEM: 2269 /* get current memory temperature */ 2270 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP, 2271 (void *)&temp, &size); 2272 break; 2273 default: 2274 r = -EINVAL; 2275 break; 2276 } 2277 2278 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2279 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2280 2281 if (r) 2282 return r; 2283 2284 return sysfs_emit(buf, "%d\n", temp); 2285 } 2286 2287 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev, 2288 struct device_attribute *attr, 2289 char *buf) 2290 { 2291 struct amdgpu_device *adev = dev_get_drvdata(dev); 2292 int hyst = to_sensor_dev_attr(attr)->index; 2293 int temp; 2294 2295 if (hyst) 2296 temp = adev->pm.dpm.thermal.min_temp; 2297 else 2298 temp = adev->pm.dpm.thermal.max_temp; 2299 2300 return sysfs_emit(buf, "%d\n", temp); 2301 } 2302 2303 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev, 2304 struct device_attribute *attr, 2305 char *buf) 2306 { 2307 struct amdgpu_device *adev = dev_get_drvdata(dev); 2308 int hyst = to_sensor_dev_attr(attr)->index; 2309 int temp; 2310 2311 if (hyst) 2312 temp = adev->pm.dpm.thermal.min_hotspot_temp; 2313 else 2314 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp; 2315 2316 return sysfs_emit(buf, "%d\n", temp); 2317 } 2318 2319 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev, 2320 struct device_attribute *attr, 2321 char *buf) 2322 { 2323 struct amdgpu_device *adev = dev_get_drvdata(dev); 2324 int hyst = to_sensor_dev_attr(attr)->index; 2325 int temp; 2326 2327 if (hyst) 2328 temp = adev->pm.dpm.thermal.min_mem_temp; 2329 else 2330 temp = adev->pm.dpm.thermal.max_mem_crit_temp; 2331 2332 return sysfs_emit(buf, "%d\n", temp); 2333 } 2334 2335 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev, 2336 struct device_attribute *attr, 2337 char *buf) 2338 { 2339 int channel = to_sensor_dev_attr(attr)->index; 2340 2341 if (channel >= PP_TEMP_MAX) 2342 return -EINVAL; 2343 2344 return sysfs_emit(buf, "%s\n", temp_label[channel].label); 2345 } 2346 2347 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev, 2348 struct device_attribute *attr, 2349 char *buf) 2350 { 2351 struct amdgpu_device *adev = dev_get_drvdata(dev); 2352 int channel = to_sensor_dev_attr(attr)->index; 2353 int temp = 0; 2354 2355 if (channel >= PP_TEMP_MAX) 2356 return -EINVAL; 2357 2358 switch (channel) { 2359 case PP_TEMP_JUNCTION: 2360 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp; 2361 break; 2362 case PP_TEMP_EDGE: 2363 temp = adev->pm.dpm.thermal.max_edge_emergency_temp; 2364 break; 2365 case PP_TEMP_MEM: 2366 temp = adev->pm.dpm.thermal.max_mem_emergency_temp; 2367 break; 2368 } 2369 2370 return sysfs_emit(buf, "%d\n", temp); 2371 } 2372 2373 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev, 2374 struct device_attribute *attr, 2375 char *buf) 2376 { 2377 struct amdgpu_device *adev = dev_get_drvdata(dev); 2378 u32 pwm_mode = 0; 2379 int ret; 2380 2381 if (amdgpu_in_reset(adev)) 2382 return -EPERM; 2383 if (adev->in_suspend && !adev->in_runpm) 2384 return -EPERM; 2385 2386 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2387 if (ret < 0) { 2388 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2389 return ret; 2390 } 2391 2392 if (!adev->powerplay.pp_funcs->get_fan_control_mode) { 2393 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2394 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2395 return -EINVAL; 2396 } 2397 2398 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2399 2400 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2401 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2402 2403 return sysfs_emit(buf, "%u\n", pwm_mode); 2404 } 2405 2406 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev, 2407 struct device_attribute *attr, 2408 const char *buf, 2409 size_t count) 2410 { 2411 struct amdgpu_device *adev = dev_get_drvdata(dev); 2412 int err, ret; 2413 int value; 2414 2415 if (amdgpu_in_reset(adev)) 2416 return -EPERM; 2417 if (adev->in_suspend && !adev->in_runpm) 2418 return -EPERM; 2419 2420 err = kstrtoint(buf, 10, &value); 2421 if (err) 2422 return err; 2423 2424 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2425 if (ret < 0) { 2426 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2427 return ret; 2428 } 2429 2430 if (!adev->powerplay.pp_funcs->set_fan_control_mode) { 2431 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2432 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2433 return -EINVAL; 2434 } 2435 2436 amdgpu_dpm_set_fan_control_mode(adev, value); 2437 2438 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2439 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2440 2441 return count; 2442 } 2443 2444 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev, 2445 struct device_attribute *attr, 2446 char *buf) 2447 { 2448 return sysfs_emit(buf, "%i\n", 0); 2449 } 2450 2451 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev, 2452 struct device_attribute *attr, 2453 char *buf) 2454 { 2455 return sysfs_emit(buf, "%i\n", 255); 2456 } 2457 2458 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev, 2459 struct device_attribute *attr, 2460 const char *buf, size_t count) 2461 { 2462 struct amdgpu_device *adev = dev_get_drvdata(dev); 2463 int err; 2464 u32 value; 2465 u32 pwm_mode; 2466 2467 if (amdgpu_in_reset(adev)) 2468 return -EPERM; 2469 if (adev->in_suspend && !adev->in_runpm) 2470 return -EPERM; 2471 2472 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2473 if (err < 0) { 2474 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2475 return err; 2476 } 2477 2478 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2479 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2480 pr_info("manual fan speed control should be enabled first\n"); 2481 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2482 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2483 return -EINVAL; 2484 } 2485 2486 err = kstrtou32(buf, 10, &value); 2487 if (err) { 2488 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2489 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2490 return err; 2491 } 2492 2493 if (adev->powerplay.pp_funcs->set_fan_speed_pwm) 2494 err = amdgpu_dpm_set_fan_speed_pwm(adev, value); 2495 else 2496 err = -EINVAL; 2497 2498 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2499 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2500 2501 if (err) 2502 return err; 2503 2504 return count; 2505 } 2506 2507 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev, 2508 struct device_attribute *attr, 2509 char *buf) 2510 { 2511 struct amdgpu_device *adev = dev_get_drvdata(dev); 2512 int err; 2513 u32 speed = 0; 2514 2515 if (amdgpu_in_reset(adev)) 2516 return -EPERM; 2517 if (adev->in_suspend && !adev->in_runpm) 2518 return -EPERM; 2519 2520 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2521 if (err < 0) { 2522 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2523 return err; 2524 } 2525 2526 if (adev->powerplay.pp_funcs->get_fan_speed_pwm) 2527 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed); 2528 else 2529 err = -EINVAL; 2530 2531 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2532 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2533 2534 if (err) 2535 return err; 2536 2537 return sysfs_emit(buf, "%i\n", speed); 2538 } 2539 2540 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev, 2541 struct device_attribute *attr, 2542 char *buf) 2543 { 2544 struct amdgpu_device *adev = dev_get_drvdata(dev); 2545 int err; 2546 u32 speed = 0; 2547 2548 if (amdgpu_in_reset(adev)) 2549 return -EPERM; 2550 if (adev->in_suspend && !adev->in_runpm) 2551 return -EPERM; 2552 2553 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2554 if (err < 0) { 2555 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2556 return err; 2557 } 2558 2559 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) 2560 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed); 2561 else 2562 err = -EINVAL; 2563 2564 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2565 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2566 2567 if (err) 2568 return err; 2569 2570 return sysfs_emit(buf, "%i\n", speed); 2571 } 2572 2573 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev, 2574 struct device_attribute *attr, 2575 char *buf) 2576 { 2577 struct amdgpu_device *adev = dev_get_drvdata(dev); 2578 u32 min_rpm = 0; 2579 u32 size = sizeof(min_rpm); 2580 int r; 2581 2582 if (amdgpu_in_reset(adev)) 2583 return -EPERM; 2584 if (adev->in_suspend && !adev->in_runpm) 2585 return -EPERM; 2586 2587 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2588 if (r < 0) { 2589 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2590 return r; 2591 } 2592 2593 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM, 2594 (void *)&min_rpm, &size); 2595 2596 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2597 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2598 2599 if (r) 2600 return r; 2601 2602 return sysfs_emit(buf, "%d\n", min_rpm); 2603 } 2604 2605 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev, 2606 struct device_attribute *attr, 2607 char *buf) 2608 { 2609 struct amdgpu_device *adev = dev_get_drvdata(dev); 2610 u32 max_rpm = 0; 2611 u32 size = sizeof(max_rpm); 2612 int r; 2613 2614 if (amdgpu_in_reset(adev)) 2615 return -EPERM; 2616 if (adev->in_suspend && !adev->in_runpm) 2617 return -EPERM; 2618 2619 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2620 if (r < 0) { 2621 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2622 return r; 2623 } 2624 2625 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM, 2626 (void *)&max_rpm, &size); 2627 2628 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2629 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2630 2631 if (r) 2632 return r; 2633 2634 return sysfs_emit(buf, "%d\n", max_rpm); 2635 } 2636 2637 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev, 2638 struct device_attribute *attr, 2639 char *buf) 2640 { 2641 struct amdgpu_device *adev = dev_get_drvdata(dev); 2642 int err; 2643 u32 rpm = 0; 2644 2645 if (amdgpu_in_reset(adev)) 2646 return -EPERM; 2647 if (adev->in_suspend && !adev->in_runpm) 2648 return -EPERM; 2649 2650 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2651 if (err < 0) { 2652 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2653 return err; 2654 } 2655 2656 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) 2657 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm); 2658 else 2659 err = -EINVAL; 2660 2661 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2662 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2663 2664 if (err) 2665 return err; 2666 2667 return sysfs_emit(buf, "%i\n", rpm); 2668 } 2669 2670 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev, 2671 struct device_attribute *attr, 2672 const char *buf, size_t count) 2673 { 2674 struct amdgpu_device *adev = dev_get_drvdata(dev); 2675 int err; 2676 u32 value; 2677 u32 pwm_mode; 2678 2679 if (amdgpu_in_reset(adev)) 2680 return -EPERM; 2681 if (adev->in_suspend && !adev->in_runpm) 2682 return -EPERM; 2683 2684 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2685 if (err < 0) { 2686 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2687 return err; 2688 } 2689 2690 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2691 2692 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2693 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2694 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2695 return -ENODATA; 2696 } 2697 2698 err = kstrtou32(buf, 10, &value); 2699 if (err) { 2700 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2701 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2702 return err; 2703 } 2704 2705 if (adev->powerplay.pp_funcs->set_fan_speed_rpm) 2706 err = amdgpu_dpm_set_fan_speed_rpm(adev, value); 2707 else 2708 err = -EINVAL; 2709 2710 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2711 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2712 2713 if (err) 2714 return err; 2715 2716 return count; 2717 } 2718 2719 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev, 2720 struct device_attribute *attr, 2721 char *buf) 2722 { 2723 struct amdgpu_device *adev = dev_get_drvdata(dev); 2724 u32 pwm_mode = 0; 2725 int ret; 2726 2727 if (amdgpu_in_reset(adev)) 2728 return -EPERM; 2729 if (adev->in_suspend && !adev->in_runpm) 2730 return -EPERM; 2731 2732 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2733 if (ret < 0) { 2734 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2735 return ret; 2736 } 2737 2738 if (!adev->powerplay.pp_funcs->get_fan_control_mode) { 2739 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2740 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2741 return -EINVAL; 2742 } 2743 2744 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2745 2746 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2747 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2748 2749 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1); 2750 } 2751 2752 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev, 2753 struct device_attribute *attr, 2754 const char *buf, 2755 size_t count) 2756 { 2757 struct amdgpu_device *adev = dev_get_drvdata(dev); 2758 int err; 2759 int value; 2760 u32 pwm_mode; 2761 2762 if (amdgpu_in_reset(adev)) 2763 return -EPERM; 2764 if (adev->in_suspend && !adev->in_runpm) 2765 return -EPERM; 2766 2767 err = kstrtoint(buf, 10, &value); 2768 if (err) 2769 return err; 2770 2771 if (value == 0) 2772 pwm_mode = AMD_FAN_CTRL_AUTO; 2773 else if (value == 1) 2774 pwm_mode = AMD_FAN_CTRL_MANUAL; 2775 else 2776 return -EINVAL; 2777 2778 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2779 if (err < 0) { 2780 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2781 return err; 2782 } 2783 2784 if (!adev->powerplay.pp_funcs->set_fan_control_mode) { 2785 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2786 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2787 return -EINVAL; 2788 } 2789 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode); 2790 2791 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2792 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2793 2794 return count; 2795 } 2796 2797 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev, 2798 struct device_attribute *attr, 2799 char *buf) 2800 { 2801 struct amdgpu_device *adev = dev_get_drvdata(dev); 2802 u32 vddgfx; 2803 int r, size = sizeof(vddgfx); 2804 2805 if (amdgpu_in_reset(adev)) 2806 return -EPERM; 2807 if (adev->in_suspend && !adev->in_runpm) 2808 return -EPERM; 2809 2810 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2811 if (r < 0) { 2812 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2813 return r; 2814 } 2815 2816 /* get the voltage */ 2817 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, 2818 (void *)&vddgfx, &size); 2819 2820 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2821 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2822 2823 if (r) 2824 return r; 2825 2826 return sysfs_emit(buf, "%d\n", vddgfx); 2827 } 2828 2829 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev, 2830 struct device_attribute *attr, 2831 char *buf) 2832 { 2833 return sysfs_emit(buf, "vddgfx\n"); 2834 } 2835 2836 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev, 2837 struct device_attribute *attr, 2838 char *buf) 2839 { 2840 struct amdgpu_device *adev = dev_get_drvdata(dev); 2841 u32 vddnb; 2842 int r, size = sizeof(vddnb); 2843 2844 if (amdgpu_in_reset(adev)) 2845 return -EPERM; 2846 if (adev->in_suspend && !adev->in_runpm) 2847 return -EPERM; 2848 2849 /* only APUs have vddnb */ 2850 if (!(adev->flags & AMD_IS_APU)) 2851 return -EINVAL; 2852 2853 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2854 if (r < 0) { 2855 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2856 return r; 2857 } 2858 2859 /* get the voltage */ 2860 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, 2861 (void *)&vddnb, &size); 2862 2863 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2864 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2865 2866 if (r) 2867 return r; 2868 2869 return sysfs_emit(buf, "%d\n", vddnb); 2870 } 2871 2872 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev, 2873 struct device_attribute *attr, 2874 char *buf) 2875 { 2876 return sysfs_emit(buf, "vddnb\n"); 2877 } 2878 2879 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev, 2880 struct device_attribute *attr, 2881 char *buf) 2882 { 2883 struct amdgpu_device *adev = dev_get_drvdata(dev); 2884 u32 query = 0; 2885 int r, size = sizeof(u32); 2886 unsigned uw; 2887 2888 if (amdgpu_in_reset(adev)) 2889 return -EPERM; 2890 if (adev->in_suspend && !adev->in_runpm) 2891 return -EPERM; 2892 2893 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2894 if (r < 0) { 2895 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2896 return r; 2897 } 2898 2899 /* get the voltage */ 2900 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, 2901 (void *)&query, &size); 2902 2903 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2904 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2905 2906 if (r) 2907 return r; 2908 2909 /* convert to microwatts */ 2910 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000; 2911 2912 return sysfs_emit(buf, "%u\n", uw); 2913 } 2914 2915 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev, 2916 struct device_attribute *attr, 2917 char *buf) 2918 { 2919 return sysfs_emit(buf, "%i\n", 0); 2920 } 2921 2922 2923 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev, 2924 struct device_attribute *attr, 2925 char *buf, 2926 enum pp_power_limit_level pp_limit_level) 2927 { 2928 struct amdgpu_device *adev = dev_get_drvdata(dev); 2929 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 2930 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index; 2931 uint32_t limit; 2932 ssize_t size; 2933 int r; 2934 2935 if (amdgpu_in_reset(adev)) 2936 return -EPERM; 2937 if (adev->in_suspend && !adev->in_runpm) 2938 return -EPERM; 2939 2940 if ( !(pp_funcs && pp_funcs->get_power_limit)) 2941 return -ENODATA; 2942 2943 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2944 if (r < 0) { 2945 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2946 return r; 2947 } 2948 2949 r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, 2950 pp_limit_level, power_type); 2951 2952 if (!r) 2953 size = sysfs_emit(buf, "%u\n", limit * 1000000); 2954 else 2955 size = sysfs_emit(buf, "\n"); 2956 2957 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2958 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2959 2960 return size; 2961 } 2962 2963 2964 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev, 2965 struct device_attribute *attr, 2966 char *buf) 2967 { 2968 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX); 2969 2970 } 2971 2972 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev, 2973 struct device_attribute *attr, 2974 char *buf) 2975 { 2976 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT); 2977 2978 } 2979 2980 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev, 2981 struct device_attribute *attr, 2982 char *buf) 2983 { 2984 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT); 2985 2986 } 2987 2988 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev, 2989 struct device_attribute *attr, 2990 char *buf) 2991 { 2992 int limit_type = to_sensor_dev_attr(attr)->index; 2993 2994 return sysfs_emit(buf, "%s\n", 2995 limit_type == SMU_FAST_PPT_LIMIT ? "fastPPT" : "slowPPT"); 2996 } 2997 2998 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev, 2999 struct device_attribute *attr, 3000 const char *buf, 3001 size_t count) 3002 { 3003 struct amdgpu_device *adev = dev_get_drvdata(dev); 3004 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 3005 int limit_type = to_sensor_dev_attr(attr)->index; 3006 int err; 3007 u32 value; 3008 3009 if (amdgpu_in_reset(adev)) 3010 return -EPERM; 3011 if (adev->in_suspend && !adev->in_runpm) 3012 return -EPERM; 3013 3014 if (amdgpu_sriov_vf(adev)) 3015 return -EINVAL; 3016 3017 err = kstrtou32(buf, 10, &value); 3018 if (err) 3019 return err; 3020 3021 value = value / 1000000; /* convert to Watt */ 3022 value |= limit_type << 24; 3023 3024 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 3025 if (err < 0) { 3026 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3027 return err; 3028 } 3029 3030 if (pp_funcs && pp_funcs->set_power_limit) 3031 err = pp_funcs->set_power_limit(adev->powerplay.pp_handle, value); 3032 else 3033 err = -EINVAL; 3034 3035 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 3036 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3037 3038 if (err) 3039 return err; 3040 3041 return count; 3042 } 3043 3044 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev, 3045 struct device_attribute *attr, 3046 char *buf) 3047 { 3048 struct amdgpu_device *adev = dev_get_drvdata(dev); 3049 uint32_t sclk; 3050 int r, size = sizeof(sclk); 3051 3052 if (amdgpu_in_reset(adev)) 3053 return -EPERM; 3054 if (adev->in_suspend && !adev->in_runpm) 3055 return -EPERM; 3056 3057 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 3058 if (r < 0) { 3059 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3060 return r; 3061 } 3062 3063 /* get the sclk */ 3064 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, 3065 (void *)&sclk, &size); 3066 3067 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 3068 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3069 3070 if (r) 3071 return r; 3072 3073 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000); 3074 } 3075 3076 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev, 3077 struct device_attribute *attr, 3078 char *buf) 3079 { 3080 return sysfs_emit(buf, "sclk\n"); 3081 } 3082 3083 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev, 3084 struct device_attribute *attr, 3085 char *buf) 3086 { 3087 struct amdgpu_device *adev = dev_get_drvdata(dev); 3088 uint32_t mclk; 3089 int r, size = sizeof(mclk); 3090 3091 if (amdgpu_in_reset(adev)) 3092 return -EPERM; 3093 if (adev->in_suspend && !adev->in_runpm) 3094 return -EPERM; 3095 3096 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 3097 if (r < 0) { 3098 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3099 return r; 3100 } 3101 3102 /* get the sclk */ 3103 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, 3104 (void *)&mclk, &size); 3105 3106 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 3107 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3108 3109 if (r) 3110 return r; 3111 3112 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000); 3113 } 3114 3115 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev, 3116 struct device_attribute *attr, 3117 char *buf) 3118 { 3119 return sysfs_emit(buf, "mclk\n"); 3120 } 3121 3122 /** 3123 * DOC: hwmon 3124 * 3125 * The amdgpu driver exposes the following sensor interfaces: 3126 * 3127 * - GPU temperature (via the on-die sensor) 3128 * 3129 * - GPU voltage 3130 * 3131 * - Northbridge voltage (APUs only) 3132 * 3133 * - GPU power 3134 * 3135 * - GPU fan 3136 * 3137 * - GPU gfx/compute engine clock 3138 * 3139 * - GPU memory clock (dGPU only) 3140 * 3141 * hwmon interfaces for GPU temperature: 3142 * 3143 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius 3144 * - temp2_input and temp3_input are supported on SOC15 dGPUs only 3145 * 3146 * - temp[1-3]_label: temperature channel label 3147 * - temp2_label and temp3_label are supported on SOC15 dGPUs only 3148 * 3149 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius 3150 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only 3151 * 3152 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius 3153 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only 3154 * 3155 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius 3156 * - these are supported on SOC15 dGPUs only 3157 * 3158 * hwmon interfaces for GPU voltage: 3159 * 3160 * - in0_input: the voltage on the GPU in millivolts 3161 * 3162 * - in1_input: the voltage on the Northbridge in millivolts 3163 * 3164 * hwmon interfaces for GPU power: 3165 * 3166 * - power1_average: average power used by the GPU in microWatts 3167 * 3168 * - power1_cap_min: minimum cap supported in microWatts 3169 * 3170 * - power1_cap_max: maximum cap supported in microWatts 3171 * 3172 * - power1_cap: selected power cap in microWatts 3173 * 3174 * hwmon interfaces for GPU fan: 3175 * 3176 * - pwm1: pulse width modulation fan level (0-255) 3177 * 3178 * - pwm1_enable: pulse width modulation fan control method (0: no fan speed control, 1: manual fan speed control using pwm interface, 2: automatic fan speed control) 3179 * 3180 * - pwm1_min: pulse width modulation fan control minimum level (0) 3181 * 3182 * - pwm1_max: pulse width modulation fan control maximum level (255) 3183 * 3184 * - fan1_min: a minimum value Unit: revolution/min (RPM) 3185 * 3186 * - fan1_max: a maximum value Unit: revolution/max (RPM) 3187 * 3188 * - fan1_input: fan speed in RPM 3189 * 3190 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM) 3191 * 3192 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable 3193 * 3194 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time. 3195 * That will get the former one overridden. 3196 * 3197 * hwmon interfaces for GPU clocks: 3198 * 3199 * - freq1_input: the gfx/compute clock in hertz 3200 * 3201 * - freq2_input: the memory clock in hertz 3202 * 3203 * You can use hwmon tools like sensors to view this information on your system. 3204 * 3205 */ 3206 3207 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE); 3208 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0); 3209 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1); 3210 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE); 3211 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION); 3212 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0); 3213 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1); 3214 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION); 3215 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM); 3216 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0); 3217 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1); 3218 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM); 3219 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE); 3220 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION); 3221 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM); 3222 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0); 3223 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0); 3224 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0); 3225 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0); 3226 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0); 3227 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0); 3228 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0); 3229 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0); 3230 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0); 3231 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0); 3232 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0); 3233 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0); 3234 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0); 3235 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0); 3236 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0); 3237 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0); 3238 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0); 3239 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0); 3240 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0); 3241 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1); 3242 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1); 3243 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1); 3244 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1); 3245 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1); 3246 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1); 3247 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0); 3248 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0); 3249 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0); 3250 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0); 3251 3252 static struct attribute *hwmon_attributes[] = { 3253 &sensor_dev_attr_temp1_input.dev_attr.attr, 3254 &sensor_dev_attr_temp1_crit.dev_attr.attr, 3255 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 3256 &sensor_dev_attr_temp2_input.dev_attr.attr, 3257 &sensor_dev_attr_temp2_crit.dev_attr.attr, 3258 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 3259 &sensor_dev_attr_temp3_input.dev_attr.attr, 3260 &sensor_dev_attr_temp3_crit.dev_attr.attr, 3261 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 3262 &sensor_dev_attr_temp1_emergency.dev_attr.attr, 3263 &sensor_dev_attr_temp2_emergency.dev_attr.attr, 3264 &sensor_dev_attr_temp3_emergency.dev_attr.attr, 3265 &sensor_dev_attr_temp1_label.dev_attr.attr, 3266 &sensor_dev_attr_temp2_label.dev_attr.attr, 3267 &sensor_dev_attr_temp3_label.dev_attr.attr, 3268 &sensor_dev_attr_pwm1.dev_attr.attr, 3269 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 3270 &sensor_dev_attr_pwm1_min.dev_attr.attr, 3271 &sensor_dev_attr_pwm1_max.dev_attr.attr, 3272 &sensor_dev_attr_fan1_input.dev_attr.attr, 3273 &sensor_dev_attr_fan1_min.dev_attr.attr, 3274 &sensor_dev_attr_fan1_max.dev_attr.attr, 3275 &sensor_dev_attr_fan1_target.dev_attr.attr, 3276 &sensor_dev_attr_fan1_enable.dev_attr.attr, 3277 &sensor_dev_attr_in0_input.dev_attr.attr, 3278 &sensor_dev_attr_in0_label.dev_attr.attr, 3279 &sensor_dev_attr_in1_input.dev_attr.attr, 3280 &sensor_dev_attr_in1_label.dev_attr.attr, 3281 &sensor_dev_attr_power1_average.dev_attr.attr, 3282 &sensor_dev_attr_power1_cap_max.dev_attr.attr, 3283 &sensor_dev_attr_power1_cap_min.dev_attr.attr, 3284 &sensor_dev_attr_power1_cap.dev_attr.attr, 3285 &sensor_dev_attr_power1_cap_default.dev_attr.attr, 3286 &sensor_dev_attr_power1_label.dev_attr.attr, 3287 &sensor_dev_attr_power2_average.dev_attr.attr, 3288 &sensor_dev_attr_power2_cap_max.dev_attr.attr, 3289 &sensor_dev_attr_power2_cap_min.dev_attr.attr, 3290 &sensor_dev_attr_power2_cap.dev_attr.attr, 3291 &sensor_dev_attr_power2_cap_default.dev_attr.attr, 3292 &sensor_dev_attr_power2_label.dev_attr.attr, 3293 &sensor_dev_attr_freq1_input.dev_attr.attr, 3294 &sensor_dev_attr_freq1_label.dev_attr.attr, 3295 &sensor_dev_attr_freq2_input.dev_attr.attr, 3296 &sensor_dev_attr_freq2_label.dev_attr.attr, 3297 NULL 3298 }; 3299 3300 static umode_t hwmon_attributes_visible(struct kobject *kobj, 3301 struct attribute *attr, int index) 3302 { 3303 struct device *dev = kobj_to_dev(kobj); 3304 struct amdgpu_device *adev = dev_get_drvdata(dev); 3305 umode_t effective_mode = attr->mode; 3306 3307 /* under multi-vf mode, the hwmon attributes are all not supported */ 3308 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 3309 return 0; 3310 3311 /* there is no fan under pp one vf mode */ 3312 if (amdgpu_sriov_is_pp_one_vf(adev) && 3313 (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3314 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3315 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3316 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3317 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3318 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3319 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3320 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3321 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3322 return 0; 3323 3324 /* Skip fan attributes if fan is not present */ 3325 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3326 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3327 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3328 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3329 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3330 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3331 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3332 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3333 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3334 return 0; 3335 3336 /* Skip fan attributes on APU */ 3337 if ((adev->flags & AMD_IS_APU) && 3338 (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3339 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3340 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3341 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3342 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3343 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3344 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3345 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3346 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3347 return 0; 3348 3349 /* Skip crit temp on APU */ 3350 if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) && 3351 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3352 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr)) 3353 return 0; 3354 3355 /* Skip limit attributes if DPM is not enabled */ 3356 if (!adev->pm.dpm_enabled && 3357 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3358 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr || 3359 attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3360 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3361 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3362 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3363 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3364 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3365 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3366 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3367 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3368 return 0; 3369 3370 if (!is_support_sw_smu(adev)) { 3371 /* mask fan attributes if we have no bindings for this asic to expose */ 3372 if ((!adev->powerplay.pp_funcs->get_fan_speed_pwm && 3373 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */ 3374 (!adev->powerplay.pp_funcs->get_fan_control_mode && 3375 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */ 3376 effective_mode &= ~S_IRUGO; 3377 3378 if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm && 3379 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */ 3380 (!adev->powerplay.pp_funcs->set_fan_control_mode && 3381 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */ 3382 effective_mode &= ~S_IWUSR; 3383 } 3384 3385 if (((adev->family == AMDGPU_FAMILY_SI) || 3386 ((adev->flags & AMD_IS_APU) && 3387 (adev->asic_type != CHIP_VANGOGH))) && /* not implemented yet */ 3388 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr || 3389 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr|| 3390 attr == &sensor_dev_attr_power1_cap.dev_attr.attr || 3391 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr)) 3392 return 0; 3393 3394 if (((adev->family == AMDGPU_FAMILY_SI) || 3395 ((adev->flags & AMD_IS_APU) && 3396 (adev->asic_type < CHIP_RENOIR))) && /* not implemented yet */ 3397 (attr == &sensor_dev_attr_power1_average.dev_attr.attr)) 3398 return 0; 3399 3400 if (!is_support_sw_smu(adev)) { 3401 /* hide max/min values if we can't both query and manage the fan */ 3402 if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm && 3403 !adev->powerplay.pp_funcs->get_fan_speed_pwm) && 3404 (!adev->powerplay.pp_funcs->set_fan_speed_rpm && 3405 !adev->powerplay.pp_funcs->get_fan_speed_rpm) && 3406 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3407 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr)) 3408 return 0; 3409 3410 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm && 3411 !adev->powerplay.pp_funcs->get_fan_speed_rpm) && 3412 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3413 attr == &sensor_dev_attr_fan1_min.dev_attr.attr)) 3414 return 0; 3415 } 3416 3417 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */ 3418 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */ 3419 (attr == &sensor_dev_attr_in0_input.dev_attr.attr || 3420 attr == &sensor_dev_attr_in0_label.dev_attr.attr)) 3421 return 0; 3422 3423 /* only APUs have vddnb */ 3424 if (!(adev->flags & AMD_IS_APU) && 3425 (attr == &sensor_dev_attr_in1_input.dev_attr.attr || 3426 attr == &sensor_dev_attr_in1_label.dev_attr.attr)) 3427 return 0; 3428 3429 /* no mclk on APUs */ 3430 if ((adev->flags & AMD_IS_APU) && 3431 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr || 3432 attr == &sensor_dev_attr_freq2_label.dev_attr.attr)) 3433 return 0; 3434 3435 /* only SOC15 dGPUs support hotspot and mem temperatures */ 3436 if (((adev->flags & AMD_IS_APU) || 3437 adev->asic_type < CHIP_VEGA10) && 3438 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr || 3439 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr || 3440 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr || 3441 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr || 3442 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr || 3443 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr || 3444 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr || 3445 attr == &sensor_dev_attr_temp2_input.dev_attr.attr || 3446 attr == &sensor_dev_attr_temp3_input.dev_attr.attr || 3447 attr == &sensor_dev_attr_temp2_label.dev_attr.attr || 3448 attr == &sensor_dev_attr_temp3_label.dev_attr.attr)) 3449 return 0; 3450 3451 /* only Vangogh has fast PPT limit and power labels */ 3452 if (!(adev->asic_type == CHIP_VANGOGH) && 3453 (attr == &sensor_dev_attr_power2_average.dev_attr.attr || 3454 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr || 3455 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr || 3456 attr == &sensor_dev_attr_power2_cap.dev_attr.attr || 3457 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr || 3458 attr == &sensor_dev_attr_power2_label.dev_attr.attr || 3459 attr == &sensor_dev_attr_power1_label.dev_attr.attr)) 3460 return 0; 3461 3462 return effective_mode; 3463 } 3464 3465 static const struct attribute_group hwmon_attrgroup = { 3466 .attrs = hwmon_attributes, 3467 .is_visible = hwmon_attributes_visible, 3468 }; 3469 3470 static const struct attribute_group *hwmon_groups[] = { 3471 &hwmon_attrgroup, 3472 NULL 3473 }; 3474 3475 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev) 3476 { 3477 int ret; 3478 uint32_t mask = 0; 3479 3480 if (adev->pm.sysfs_initialized) 3481 return 0; 3482 3483 if (adev->pm.dpm_enabled == 0) 3484 return 0; 3485 3486 INIT_LIST_HEAD(&adev->pm.pm_attr_list); 3487 3488 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev, 3489 DRIVER_NAME, adev, 3490 hwmon_groups); 3491 if (IS_ERR(adev->pm.int_hwmon_dev)) { 3492 ret = PTR_ERR(adev->pm.int_hwmon_dev); 3493 dev_err(adev->dev, 3494 "Unable to register hwmon device: %d\n", ret); 3495 return ret; 3496 } 3497 3498 switch (amdgpu_virt_get_sriov_vf_mode(adev)) { 3499 case SRIOV_VF_MODE_ONE_VF: 3500 mask = ATTR_FLAG_ONEVF; 3501 break; 3502 case SRIOV_VF_MODE_MULTI_VF: 3503 mask = 0; 3504 break; 3505 case SRIOV_VF_MODE_BARE_METAL: 3506 default: 3507 mask = ATTR_FLAG_MASK_ALL; 3508 break; 3509 } 3510 3511 ret = amdgpu_device_attr_create_groups(adev, 3512 amdgpu_device_attrs, 3513 ARRAY_SIZE(amdgpu_device_attrs), 3514 mask, 3515 &adev->pm.pm_attr_list); 3516 if (ret) 3517 return ret; 3518 3519 adev->pm.sysfs_initialized = true; 3520 3521 return 0; 3522 } 3523 3524 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev) 3525 { 3526 if (adev->pm.dpm_enabled == 0) 3527 return; 3528 3529 if (adev->pm.int_hwmon_dev) 3530 hwmon_device_unregister(adev->pm.int_hwmon_dev); 3531 3532 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 3533 } 3534 3535 /* 3536 * Debugfs info 3537 */ 3538 #if defined(CONFIG_DEBUG_FS) 3539 3540 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m, 3541 struct amdgpu_device *adev) { 3542 uint16_t *p_val; 3543 uint32_t size; 3544 int i; 3545 3546 if (is_support_cclk_dpm(adev)) { 3547 p_val = kcalloc(adev->smu.cpu_core_num, sizeof(uint16_t), 3548 GFP_KERNEL); 3549 3550 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK, 3551 (void *)p_val, &size)) { 3552 for (i = 0; i < adev->smu.cpu_core_num; i++) 3553 seq_printf(m, "\t%u MHz (CPU%d)\n", 3554 *(p_val + i), i); 3555 } 3556 3557 kfree(p_val); 3558 } 3559 } 3560 3561 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev) 3562 { 3563 uint32_t value; 3564 uint64_t value64 = 0; 3565 uint32_t query = 0; 3566 int size; 3567 3568 /* GPU Clocks */ 3569 size = sizeof(value); 3570 seq_printf(m, "GFX Clocks and Power:\n"); 3571 3572 amdgpu_debugfs_prints_cpu_info(m, adev); 3573 3574 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size)) 3575 seq_printf(m, "\t%u MHz (MCLK)\n", value/100); 3576 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size)) 3577 seq_printf(m, "\t%u MHz (SCLK)\n", value/100); 3578 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size)) 3579 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100); 3580 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size)) 3581 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100); 3582 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size)) 3583 seq_printf(m, "\t%u mV (VDDGFX)\n", value); 3584 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size)) 3585 seq_printf(m, "\t%u mV (VDDNB)\n", value); 3586 size = sizeof(uint32_t); 3587 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) 3588 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff); 3589 size = sizeof(value); 3590 seq_printf(m, "\n"); 3591 3592 /* GPU Temp */ 3593 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size)) 3594 seq_printf(m, "GPU Temperature: %u C\n", value/1000); 3595 3596 /* GPU Load */ 3597 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size)) 3598 seq_printf(m, "GPU Load: %u %%\n", value); 3599 /* MEM Load */ 3600 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size)) 3601 seq_printf(m, "MEM Load: %u %%\n", value); 3602 3603 seq_printf(m, "\n"); 3604 3605 /* SMC feature mask */ 3606 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size)) 3607 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64); 3608 3609 if (adev->asic_type > CHIP_VEGA20) { 3610 /* VCN clocks */ 3611 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) { 3612 if (!value) { 3613 seq_printf(m, "VCN: Disabled\n"); 3614 } else { 3615 seq_printf(m, "VCN: Enabled\n"); 3616 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 3617 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 3618 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 3619 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 3620 } 3621 } 3622 seq_printf(m, "\n"); 3623 } else { 3624 /* UVD clocks */ 3625 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) { 3626 if (!value) { 3627 seq_printf(m, "UVD: Disabled\n"); 3628 } else { 3629 seq_printf(m, "UVD: Enabled\n"); 3630 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 3631 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 3632 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 3633 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 3634 } 3635 } 3636 seq_printf(m, "\n"); 3637 3638 /* VCE clocks */ 3639 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) { 3640 if (!value) { 3641 seq_printf(m, "VCE: Disabled\n"); 3642 } else { 3643 seq_printf(m, "VCE: Enabled\n"); 3644 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size)) 3645 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100); 3646 } 3647 } 3648 } 3649 3650 return 0; 3651 } 3652 3653 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags) 3654 { 3655 int i; 3656 3657 for (i = 0; clocks[i].flag; i++) 3658 seq_printf(m, "\t%s: %s\n", clocks[i].name, 3659 (flags & clocks[i].flag) ? "On" : "Off"); 3660 } 3661 3662 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused) 3663 { 3664 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 3665 struct drm_device *dev = adev_to_drm(adev); 3666 u32 flags = 0; 3667 int r; 3668 3669 if (amdgpu_in_reset(adev)) 3670 return -EPERM; 3671 if (adev->in_suspend && !adev->in_runpm) 3672 return -EPERM; 3673 3674 r = pm_runtime_get_sync(dev->dev); 3675 if (r < 0) { 3676 pm_runtime_put_autosuspend(dev->dev); 3677 return r; 3678 } 3679 3680 if (!adev->pm.dpm_enabled) { 3681 seq_printf(m, "dpm not enabled\n"); 3682 pm_runtime_mark_last_busy(dev->dev); 3683 pm_runtime_put_autosuspend(dev->dev); 3684 return 0; 3685 } 3686 3687 if (!is_support_sw_smu(adev) && 3688 adev->powerplay.pp_funcs->debugfs_print_current_performance_level) { 3689 mutex_lock(&adev->pm.mutex); 3690 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) 3691 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m); 3692 else 3693 seq_printf(m, "Debugfs support not implemented for this asic\n"); 3694 mutex_unlock(&adev->pm.mutex); 3695 r = 0; 3696 } else { 3697 r = amdgpu_debugfs_pm_info_pp(m, adev); 3698 } 3699 if (r) 3700 goto out; 3701 3702 amdgpu_device_ip_get_clockgating_state(adev, &flags); 3703 3704 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags); 3705 amdgpu_parse_cg_state(m, flags); 3706 seq_printf(m, "\n"); 3707 3708 out: 3709 pm_runtime_mark_last_busy(dev->dev); 3710 pm_runtime_put_autosuspend(dev->dev); 3711 3712 return r; 3713 } 3714 3715 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info); 3716 3717 /* 3718 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW 3719 * 3720 * Reads debug memory region allocated to PMFW 3721 */ 3722 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf, 3723 size_t size, loff_t *pos) 3724 { 3725 struct amdgpu_device *adev = file_inode(f)->i_private; 3726 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs; 3727 void *pp_handle = adev->powerplay.pp_handle; 3728 size_t smu_prv_buf_size; 3729 void *smu_prv_buf; 3730 3731 if (amdgpu_in_reset(adev)) 3732 return -EPERM; 3733 if (adev->in_suspend && !adev->in_runpm) 3734 return -EPERM; 3735 3736 if (pp_funcs && pp_funcs->get_smu_prv_buf_details) 3737 pp_funcs->get_smu_prv_buf_details(pp_handle, &smu_prv_buf, 3738 &smu_prv_buf_size); 3739 else 3740 return -ENOSYS; 3741 3742 if (!smu_prv_buf || !smu_prv_buf_size) 3743 return -EINVAL; 3744 3745 return simple_read_from_buffer(buf, size, pos, smu_prv_buf, 3746 smu_prv_buf_size); 3747 } 3748 3749 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = { 3750 .owner = THIS_MODULE, 3751 .open = simple_open, 3752 .read = amdgpu_pm_prv_buffer_read, 3753 .llseek = default_llseek, 3754 }; 3755 3756 #endif 3757 3758 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev) 3759 { 3760 #if defined(CONFIG_DEBUG_FS) 3761 struct drm_minor *minor = adev_to_drm(adev)->primary; 3762 struct dentry *root = minor->debugfs_root; 3763 3764 debugfs_create_file("amdgpu_pm_info", 0444, root, adev, 3765 &amdgpu_debugfs_pm_info_fops); 3766 3767 if (adev->pm.smu_prv_buffer_size > 0) 3768 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root, 3769 adev, 3770 &amdgpu_debugfs_pm_prv_buffer_fops, 3771 adev->pm.smu_prv_buffer_size); 3772 3773 amdgpu_smu_stb_debug_fs_init(adev); 3774 #endif 3775 } 3776