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