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