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