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 /* Following items will be read out to indicate current plpd policy: 1995 * - -1: none 1996 * - 0: disallow 1997 * - 1: default 1998 * - 2: optimized 1999 */ 2000 static ssize_t amdgpu_get_xgmi_plpd_policy(struct device *dev, 2001 struct device_attribute *attr, 2002 char *buf) 2003 { 2004 struct drm_device *ddev = dev_get_drvdata(dev); 2005 struct amdgpu_device *adev = drm_to_adev(ddev); 2006 char *mode_desc = "none"; 2007 int mode; 2008 2009 if (amdgpu_in_reset(adev)) 2010 return -EPERM; 2011 if (adev->in_suspend && !adev->in_runpm) 2012 return -EPERM; 2013 2014 mode = amdgpu_dpm_get_xgmi_plpd_mode(adev, &mode_desc); 2015 2016 return sysfs_emit(buf, "%d: %s\n", mode, mode_desc); 2017 } 2018 2019 /* Following argument value is expected from user to change plpd policy 2020 * - arg 0: disallow plpd 2021 * - arg 1: default policy 2022 * - arg 2: optimized policy 2023 */ 2024 static ssize_t amdgpu_set_xgmi_plpd_policy(struct device *dev, 2025 struct device_attribute *attr, 2026 const char *buf, size_t count) 2027 { 2028 struct drm_device *ddev = dev_get_drvdata(dev); 2029 struct amdgpu_device *adev = drm_to_adev(ddev); 2030 int mode, ret; 2031 2032 if (amdgpu_in_reset(adev)) 2033 return -EPERM; 2034 if (adev->in_suspend && !adev->in_runpm) 2035 return -EPERM; 2036 2037 ret = kstrtos32(buf, 0, &mode); 2038 if (ret) 2039 return -EINVAL; 2040 2041 ret = pm_runtime_get_sync(ddev->dev); 2042 if (ret < 0) { 2043 pm_runtime_put_autosuspend(ddev->dev); 2044 return ret; 2045 } 2046 2047 ret = amdgpu_dpm_set_xgmi_plpd_mode(adev, mode); 2048 2049 pm_runtime_mark_last_busy(ddev->dev); 2050 pm_runtime_put_autosuspend(ddev->dev); 2051 2052 if (ret) 2053 return ret; 2054 2055 return count; 2056 } 2057 2058 static struct amdgpu_device_attr amdgpu_device_attrs[] = { 2059 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2060 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2061 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2062 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2063 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2064 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2065 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2066 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2067 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2068 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2069 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2070 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2071 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2072 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2073 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2074 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2075 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC), 2076 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC), 2077 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2078 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC), 2079 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2080 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2081 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC), 2082 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2083 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2084 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2085 AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2086 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2087 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC, 2088 .attr_update = ss_power_attr_update), 2089 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC, 2090 .attr_update = ss_power_attr_update), 2091 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC, 2092 .attr_update = ss_bias_attr_update), 2093 AMDGPU_DEVICE_ATTR_RW(xgmi_plpd_policy, ATTR_FLAG_BASIC), 2094 }; 2095 2096 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2097 uint32_t mask, enum amdgpu_device_attr_states *states) 2098 { 2099 struct device_attribute *dev_attr = &attr->dev_attr; 2100 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0); 2101 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 2102 const char *attr_name = dev_attr->attr.name; 2103 2104 if (!(attr->flags & mask)) { 2105 *states = ATTR_STATE_UNSUPPORTED; 2106 return 0; 2107 } 2108 2109 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name)) 2110 2111 if (DEVICE_ATTR_IS(pp_dpm_socclk)) { 2112 if (gc_ver < IP_VERSION(9, 0, 0)) 2113 *states = ATTR_STATE_UNSUPPORTED; 2114 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) { 2115 if (gc_ver < IP_VERSION(9, 0, 0) || 2116 !amdgpu_device_has_display_hardware(adev)) 2117 *states = ATTR_STATE_UNSUPPORTED; 2118 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) { 2119 if (mp1_ver < IP_VERSION(10, 0, 0)) 2120 *states = ATTR_STATE_UNSUPPORTED; 2121 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) { 2122 *states = ATTR_STATE_UNSUPPORTED; 2123 if (amdgpu_dpm_is_overdrive_supported(adev)) 2124 *states = ATTR_STATE_SUPPORTED; 2125 } else if (DEVICE_ATTR_IS(mem_busy_percent)) { 2126 if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1)) 2127 *states = ATTR_STATE_UNSUPPORTED; 2128 } else if (DEVICE_ATTR_IS(pcie_bw)) { 2129 /* PCIe Perf counters won't work on APU nodes */ 2130 if (adev->flags & AMD_IS_APU) 2131 *states = ATTR_STATE_UNSUPPORTED; 2132 } else if (DEVICE_ATTR_IS(unique_id)) { 2133 switch (gc_ver) { 2134 case IP_VERSION(9, 0, 1): 2135 case IP_VERSION(9, 4, 0): 2136 case IP_VERSION(9, 4, 1): 2137 case IP_VERSION(9, 4, 2): 2138 case IP_VERSION(9, 4, 3): 2139 case IP_VERSION(10, 3, 0): 2140 case IP_VERSION(11, 0, 0): 2141 case IP_VERSION(11, 0, 1): 2142 case IP_VERSION(11, 0, 2): 2143 case IP_VERSION(11, 0, 3): 2144 *states = ATTR_STATE_SUPPORTED; 2145 break; 2146 default: 2147 *states = ATTR_STATE_UNSUPPORTED; 2148 } 2149 } else if (DEVICE_ATTR_IS(pp_features)) { 2150 if ((adev->flags & AMD_IS_APU && 2151 gc_ver != IP_VERSION(9, 4, 3)) || 2152 gc_ver < IP_VERSION(9, 0, 0)) 2153 *states = ATTR_STATE_UNSUPPORTED; 2154 } else if (DEVICE_ATTR_IS(gpu_metrics)) { 2155 if (gc_ver < IP_VERSION(9, 1, 0)) 2156 *states = ATTR_STATE_UNSUPPORTED; 2157 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) { 2158 if (!(gc_ver == IP_VERSION(10, 3, 1) || 2159 gc_ver == IP_VERSION(10, 3, 0) || 2160 gc_ver == IP_VERSION(10, 1, 2) || 2161 gc_ver == IP_VERSION(11, 0, 0) || 2162 gc_ver == IP_VERSION(11, 0, 2) || 2163 gc_ver == IP_VERSION(11, 0, 3) || 2164 gc_ver == IP_VERSION(9, 4, 3))) 2165 *states = ATTR_STATE_UNSUPPORTED; 2166 } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) { 2167 if (!((gc_ver == IP_VERSION(10, 3, 1) || 2168 gc_ver == IP_VERSION(10, 3, 0) || 2169 gc_ver == IP_VERSION(11, 0, 2) || 2170 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2)) 2171 *states = ATTR_STATE_UNSUPPORTED; 2172 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) { 2173 if (!(gc_ver == IP_VERSION(10, 3, 1) || 2174 gc_ver == IP_VERSION(10, 3, 0) || 2175 gc_ver == IP_VERSION(10, 1, 2) || 2176 gc_ver == IP_VERSION(11, 0, 0) || 2177 gc_ver == IP_VERSION(11, 0, 2) || 2178 gc_ver == IP_VERSION(11, 0, 3) || 2179 gc_ver == IP_VERSION(9, 4, 3))) 2180 *states = ATTR_STATE_UNSUPPORTED; 2181 } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) { 2182 if (!((gc_ver == IP_VERSION(10, 3, 1) || 2183 gc_ver == IP_VERSION(10, 3, 0) || 2184 gc_ver == IP_VERSION(11, 0, 2) || 2185 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2)) 2186 *states = ATTR_STATE_UNSUPPORTED; 2187 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) { 2188 if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP) 2189 *states = ATTR_STATE_UNSUPPORTED; 2190 else if ((gc_ver == IP_VERSION(10, 3, 0) || 2191 gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev)) 2192 *states = ATTR_STATE_UNSUPPORTED; 2193 } else if (DEVICE_ATTR_IS(xgmi_plpd_policy)) { 2194 if (amdgpu_dpm_get_xgmi_plpd_mode(adev, NULL) == XGMI_PLPD_NONE) 2195 *states = ATTR_STATE_UNSUPPORTED; 2196 } 2197 2198 switch (gc_ver) { 2199 case IP_VERSION(9, 4, 1): 2200 case IP_VERSION(9, 4, 2): 2201 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */ 2202 if (DEVICE_ATTR_IS(pp_dpm_mclk) || 2203 DEVICE_ATTR_IS(pp_dpm_socclk) || 2204 DEVICE_ATTR_IS(pp_dpm_fclk)) { 2205 dev_attr->attr.mode &= ~S_IWUGO; 2206 dev_attr->store = NULL; 2207 } 2208 break; 2209 case IP_VERSION(10, 3, 0): 2210 if (DEVICE_ATTR_IS(power_dpm_force_performance_level) && 2211 amdgpu_sriov_vf(adev)) { 2212 dev_attr->attr.mode &= ~0222; 2213 dev_attr->store = NULL; 2214 } 2215 break; 2216 default: 2217 break; 2218 } 2219 2220 if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) { 2221 /* SMU MP1 does not support dcefclk level setting */ 2222 if (gc_ver >= IP_VERSION(10, 0, 0)) { 2223 dev_attr->attr.mode &= ~S_IWUGO; 2224 dev_attr->store = NULL; 2225 } 2226 } 2227 2228 /* setting should not be allowed from VF if not in one VF mode */ 2229 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) { 2230 dev_attr->attr.mode &= ~S_IWUGO; 2231 dev_attr->store = NULL; 2232 } 2233 2234 #undef DEVICE_ATTR_IS 2235 2236 return 0; 2237 } 2238 2239 2240 static int amdgpu_device_attr_create(struct amdgpu_device *adev, 2241 struct amdgpu_device_attr *attr, 2242 uint32_t mask, struct list_head *attr_list) 2243 { 2244 int ret = 0; 2245 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED; 2246 struct amdgpu_device_attr_entry *attr_entry; 2247 struct device_attribute *dev_attr; 2248 const char *name; 2249 2250 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2251 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update; 2252 2253 if (!attr) 2254 return -EINVAL; 2255 2256 dev_attr = &attr->dev_attr; 2257 name = dev_attr->attr.name; 2258 2259 attr_update = attr->attr_update ? attr->attr_update : default_attr_update; 2260 2261 ret = attr_update(adev, attr, mask, &attr_states); 2262 if (ret) { 2263 dev_err(adev->dev, "failed to update device file %s, ret = %d\n", 2264 name, ret); 2265 return ret; 2266 } 2267 2268 if (attr_states == ATTR_STATE_UNSUPPORTED) 2269 return 0; 2270 2271 ret = device_create_file(adev->dev, dev_attr); 2272 if (ret) { 2273 dev_err(adev->dev, "failed to create device file %s, ret = %d\n", 2274 name, ret); 2275 } 2276 2277 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL); 2278 if (!attr_entry) 2279 return -ENOMEM; 2280 2281 attr_entry->attr = attr; 2282 INIT_LIST_HEAD(&attr_entry->entry); 2283 2284 list_add_tail(&attr_entry->entry, attr_list); 2285 2286 return ret; 2287 } 2288 2289 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr) 2290 { 2291 struct device_attribute *dev_attr = &attr->dev_attr; 2292 2293 device_remove_file(adev->dev, dev_attr); 2294 } 2295 2296 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2297 struct list_head *attr_list); 2298 2299 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev, 2300 struct amdgpu_device_attr *attrs, 2301 uint32_t counts, 2302 uint32_t mask, 2303 struct list_head *attr_list) 2304 { 2305 int ret = 0; 2306 uint32_t i = 0; 2307 2308 for (i = 0; i < counts; i++) { 2309 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list); 2310 if (ret) 2311 goto failed; 2312 } 2313 2314 return 0; 2315 2316 failed: 2317 amdgpu_device_attr_remove_groups(adev, attr_list); 2318 2319 return ret; 2320 } 2321 2322 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2323 struct list_head *attr_list) 2324 { 2325 struct amdgpu_device_attr_entry *entry, *entry_tmp; 2326 2327 if (list_empty(attr_list)) 2328 return ; 2329 2330 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) { 2331 amdgpu_device_attr_remove(adev, entry->attr); 2332 list_del(&entry->entry); 2333 kfree(entry); 2334 } 2335 } 2336 2337 static ssize_t amdgpu_hwmon_show_temp(struct device *dev, 2338 struct device_attribute *attr, 2339 char *buf) 2340 { 2341 struct amdgpu_device *adev = dev_get_drvdata(dev); 2342 int channel = to_sensor_dev_attr(attr)->index; 2343 int r, temp = 0; 2344 2345 if (channel >= PP_TEMP_MAX) 2346 return -EINVAL; 2347 2348 switch (channel) { 2349 case PP_TEMP_JUNCTION: 2350 /* get current junction temperature */ 2351 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP, 2352 (void *)&temp); 2353 break; 2354 case PP_TEMP_EDGE: 2355 /* get current edge temperature */ 2356 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP, 2357 (void *)&temp); 2358 break; 2359 case PP_TEMP_MEM: 2360 /* get current memory temperature */ 2361 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP, 2362 (void *)&temp); 2363 break; 2364 default: 2365 r = -EINVAL; 2366 break; 2367 } 2368 2369 if (r) 2370 return r; 2371 2372 return sysfs_emit(buf, "%d\n", temp); 2373 } 2374 2375 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev, 2376 struct device_attribute *attr, 2377 char *buf) 2378 { 2379 struct amdgpu_device *adev = dev_get_drvdata(dev); 2380 int hyst = to_sensor_dev_attr(attr)->index; 2381 int temp; 2382 2383 if (hyst) 2384 temp = adev->pm.dpm.thermal.min_temp; 2385 else 2386 temp = adev->pm.dpm.thermal.max_temp; 2387 2388 return sysfs_emit(buf, "%d\n", temp); 2389 } 2390 2391 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev, 2392 struct device_attribute *attr, 2393 char *buf) 2394 { 2395 struct amdgpu_device *adev = dev_get_drvdata(dev); 2396 int hyst = to_sensor_dev_attr(attr)->index; 2397 int temp; 2398 2399 if (hyst) 2400 temp = adev->pm.dpm.thermal.min_hotspot_temp; 2401 else 2402 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp; 2403 2404 return sysfs_emit(buf, "%d\n", temp); 2405 } 2406 2407 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev, 2408 struct device_attribute *attr, 2409 char *buf) 2410 { 2411 struct amdgpu_device *adev = dev_get_drvdata(dev); 2412 int hyst = to_sensor_dev_attr(attr)->index; 2413 int temp; 2414 2415 if (hyst) 2416 temp = adev->pm.dpm.thermal.min_mem_temp; 2417 else 2418 temp = adev->pm.dpm.thermal.max_mem_crit_temp; 2419 2420 return sysfs_emit(buf, "%d\n", temp); 2421 } 2422 2423 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev, 2424 struct device_attribute *attr, 2425 char *buf) 2426 { 2427 int channel = to_sensor_dev_attr(attr)->index; 2428 2429 if (channel >= PP_TEMP_MAX) 2430 return -EINVAL; 2431 2432 return sysfs_emit(buf, "%s\n", temp_label[channel].label); 2433 } 2434 2435 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev, 2436 struct device_attribute *attr, 2437 char *buf) 2438 { 2439 struct amdgpu_device *adev = dev_get_drvdata(dev); 2440 int channel = to_sensor_dev_attr(attr)->index; 2441 int temp = 0; 2442 2443 if (channel >= PP_TEMP_MAX) 2444 return -EINVAL; 2445 2446 switch (channel) { 2447 case PP_TEMP_JUNCTION: 2448 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp; 2449 break; 2450 case PP_TEMP_EDGE: 2451 temp = adev->pm.dpm.thermal.max_edge_emergency_temp; 2452 break; 2453 case PP_TEMP_MEM: 2454 temp = adev->pm.dpm.thermal.max_mem_emergency_temp; 2455 break; 2456 } 2457 2458 return sysfs_emit(buf, "%d\n", temp); 2459 } 2460 2461 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev, 2462 struct device_attribute *attr, 2463 char *buf) 2464 { 2465 struct amdgpu_device *adev = dev_get_drvdata(dev); 2466 u32 pwm_mode = 0; 2467 int ret; 2468 2469 if (amdgpu_in_reset(adev)) 2470 return -EPERM; 2471 if (adev->in_suspend && !adev->in_runpm) 2472 return -EPERM; 2473 2474 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2475 if (ret < 0) { 2476 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2477 return ret; 2478 } 2479 2480 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2481 2482 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2483 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2484 2485 if (ret) 2486 return -EINVAL; 2487 2488 return sysfs_emit(buf, "%u\n", pwm_mode); 2489 } 2490 2491 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev, 2492 struct device_attribute *attr, 2493 const char *buf, 2494 size_t count) 2495 { 2496 struct amdgpu_device *adev = dev_get_drvdata(dev); 2497 int err, ret; 2498 int value; 2499 2500 if (amdgpu_in_reset(adev)) 2501 return -EPERM; 2502 if (adev->in_suspend && !adev->in_runpm) 2503 return -EPERM; 2504 2505 err = kstrtoint(buf, 10, &value); 2506 if (err) 2507 return err; 2508 2509 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2510 if (ret < 0) { 2511 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2512 return ret; 2513 } 2514 2515 ret = amdgpu_dpm_set_fan_control_mode(adev, value); 2516 2517 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2518 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2519 2520 if (ret) 2521 return -EINVAL; 2522 2523 return count; 2524 } 2525 2526 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev, 2527 struct device_attribute *attr, 2528 char *buf) 2529 { 2530 return sysfs_emit(buf, "%i\n", 0); 2531 } 2532 2533 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev, 2534 struct device_attribute *attr, 2535 char *buf) 2536 { 2537 return sysfs_emit(buf, "%i\n", 255); 2538 } 2539 2540 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev, 2541 struct device_attribute *attr, 2542 const char *buf, size_t count) 2543 { 2544 struct amdgpu_device *adev = dev_get_drvdata(dev); 2545 int err; 2546 u32 value; 2547 u32 pwm_mode; 2548 2549 if (amdgpu_in_reset(adev)) 2550 return -EPERM; 2551 if (adev->in_suspend && !adev->in_runpm) 2552 return -EPERM; 2553 2554 err = kstrtou32(buf, 10, &value); 2555 if (err) 2556 return err; 2557 2558 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2559 if (err < 0) { 2560 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2561 return err; 2562 } 2563 2564 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2565 if (err) 2566 goto out; 2567 2568 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2569 pr_info("manual fan speed control should be enabled first\n"); 2570 err = -EINVAL; 2571 goto out; 2572 } 2573 2574 err = amdgpu_dpm_set_fan_speed_pwm(adev, value); 2575 2576 out: 2577 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2578 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2579 2580 if (err) 2581 return err; 2582 2583 return count; 2584 } 2585 2586 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev, 2587 struct device_attribute *attr, 2588 char *buf) 2589 { 2590 struct amdgpu_device *adev = dev_get_drvdata(dev); 2591 int err; 2592 u32 speed = 0; 2593 2594 if (amdgpu_in_reset(adev)) 2595 return -EPERM; 2596 if (adev->in_suspend && !adev->in_runpm) 2597 return -EPERM; 2598 2599 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2600 if (err < 0) { 2601 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2602 return err; 2603 } 2604 2605 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed); 2606 2607 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2608 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2609 2610 if (err) 2611 return err; 2612 2613 return sysfs_emit(buf, "%i\n", speed); 2614 } 2615 2616 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev, 2617 struct device_attribute *attr, 2618 char *buf) 2619 { 2620 struct amdgpu_device *adev = dev_get_drvdata(dev); 2621 int err; 2622 u32 speed = 0; 2623 2624 if (amdgpu_in_reset(adev)) 2625 return -EPERM; 2626 if (adev->in_suspend && !adev->in_runpm) 2627 return -EPERM; 2628 2629 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2630 if (err < 0) { 2631 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2632 return err; 2633 } 2634 2635 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed); 2636 2637 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2638 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2639 2640 if (err) 2641 return err; 2642 2643 return sysfs_emit(buf, "%i\n", speed); 2644 } 2645 2646 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev, 2647 struct device_attribute *attr, 2648 char *buf) 2649 { 2650 struct amdgpu_device *adev = dev_get_drvdata(dev); 2651 u32 min_rpm = 0; 2652 int r; 2653 2654 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM, 2655 (void *)&min_rpm); 2656 2657 if (r) 2658 return r; 2659 2660 return sysfs_emit(buf, "%d\n", min_rpm); 2661 } 2662 2663 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev, 2664 struct device_attribute *attr, 2665 char *buf) 2666 { 2667 struct amdgpu_device *adev = dev_get_drvdata(dev); 2668 u32 max_rpm = 0; 2669 int r; 2670 2671 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM, 2672 (void *)&max_rpm); 2673 2674 if (r) 2675 return r; 2676 2677 return sysfs_emit(buf, "%d\n", max_rpm); 2678 } 2679 2680 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev, 2681 struct device_attribute *attr, 2682 char *buf) 2683 { 2684 struct amdgpu_device *adev = dev_get_drvdata(dev); 2685 int err; 2686 u32 rpm = 0; 2687 2688 if (amdgpu_in_reset(adev)) 2689 return -EPERM; 2690 if (adev->in_suspend && !adev->in_runpm) 2691 return -EPERM; 2692 2693 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2694 if (err < 0) { 2695 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2696 return err; 2697 } 2698 2699 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm); 2700 2701 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2702 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2703 2704 if (err) 2705 return err; 2706 2707 return sysfs_emit(buf, "%i\n", rpm); 2708 } 2709 2710 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev, 2711 struct device_attribute *attr, 2712 const char *buf, size_t count) 2713 { 2714 struct amdgpu_device *adev = dev_get_drvdata(dev); 2715 int err; 2716 u32 value; 2717 u32 pwm_mode; 2718 2719 if (amdgpu_in_reset(adev)) 2720 return -EPERM; 2721 if (adev->in_suspend && !adev->in_runpm) 2722 return -EPERM; 2723 2724 err = kstrtou32(buf, 10, &value); 2725 if (err) 2726 return err; 2727 2728 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2729 if (err < 0) { 2730 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2731 return err; 2732 } 2733 2734 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2735 if (err) 2736 goto out; 2737 2738 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2739 err = -ENODATA; 2740 goto out; 2741 } 2742 2743 err = amdgpu_dpm_set_fan_speed_rpm(adev, value); 2744 2745 out: 2746 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2747 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2748 2749 if (err) 2750 return err; 2751 2752 return count; 2753 } 2754 2755 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev, 2756 struct device_attribute *attr, 2757 char *buf) 2758 { 2759 struct amdgpu_device *adev = dev_get_drvdata(dev); 2760 u32 pwm_mode = 0; 2761 int ret; 2762 2763 if (amdgpu_in_reset(adev)) 2764 return -EPERM; 2765 if (adev->in_suspend && !adev->in_runpm) 2766 return -EPERM; 2767 2768 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2769 if (ret < 0) { 2770 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2771 return ret; 2772 } 2773 2774 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2775 2776 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2777 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2778 2779 if (ret) 2780 return -EINVAL; 2781 2782 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1); 2783 } 2784 2785 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev, 2786 struct device_attribute *attr, 2787 const char *buf, 2788 size_t count) 2789 { 2790 struct amdgpu_device *adev = dev_get_drvdata(dev); 2791 int err; 2792 int value; 2793 u32 pwm_mode; 2794 2795 if (amdgpu_in_reset(adev)) 2796 return -EPERM; 2797 if (adev->in_suspend && !adev->in_runpm) 2798 return -EPERM; 2799 2800 err = kstrtoint(buf, 10, &value); 2801 if (err) 2802 return err; 2803 2804 if (value == 0) 2805 pwm_mode = AMD_FAN_CTRL_AUTO; 2806 else if (value == 1) 2807 pwm_mode = AMD_FAN_CTRL_MANUAL; 2808 else 2809 return -EINVAL; 2810 2811 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2812 if (err < 0) { 2813 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2814 return err; 2815 } 2816 2817 err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode); 2818 2819 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2820 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2821 2822 if (err) 2823 return -EINVAL; 2824 2825 return count; 2826 } 2827 2828 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev, 2829 struct device_attribute *attr, 2830 char *buf) 2831 { 2832 struct amdgpu_device *adev = dev_get_drvdata(dev); 2833 u32 vddgfx; 2834 int r; 2835 2836 /* get the voltage */ 2837 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX, 2838 (void *)&vddgfx); 2839 if (r) 2840 return r; 2841 2842 return sysfs_emit(buf, "%d\n", vddgfx); 2843 } 2844 2845 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev, 2846 struct device_attribute *attr, 2847 char *buf) 2848 { 2849 return sysfs_emit(buf, "vddgfx\n"); 2850 } 2851 2852 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev, 2853 struct device_attribute *attr, 2854 char *buf) 2855 { 2856 struct amdgpu_device *adev = dev_get_drvdata(dev); 2857 u32 vddnb; 2858 int r; 2859 2860 /* only APUs have vddnb */ 2861 if (!(adev->flags & AMD_IS_APU)) 2862 return -EINVAL; 2863 2864 /* get the voltage */ 2865 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB, 2866 (void *)&vddnb); 2867 if (r) 2868 return r; 2869 2870 return sysfs_emit(buf, "%d\n", vddnb); 2871 } 2872 2873 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev, 2874 struct device_attribute *attr, 2875 char *buf) 2876 { 2877 return sysfs_emit(buf, "vddnb\n"); 2878 } 2879 2880 static int amdgpu_hwmon_get_power(struct device *dev, 2881 enum amd_pp_sensors sensor) 2882 { 2883 struct amdgpu_device *adev = dev_get_drvdata(dev); 2884 unsigned int uw; 2885 u32 query = 0; 2886 int r; 2887 2888 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query); 2889 if (r) 2890 return r; 2891 2892 /* convert to microwatts */ 2893 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000; 2894 2895 return uw; 2896 } 2897 2898 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev, 2899 struct device_attribute *attr, 2900 char *buf) 2901 { 2902 ssize_t val; 2903 2904 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER); 2905 if (val < 0) 2906 return val; 2907 2908 return sysfs_emit(buf, "%zd\n", val); 2909 } 2910 2911 static ssize_t amdgpu_hwmon_show_power_input(struct device *dev, 2912 struct device_attribute *attr, 2913 char *buf) 2914 { 2915 ssize_t val; 2916 2917 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER); 2918 if (val < 0) 2919 return val; 2920 2921 return sysfs_emit(buf, "%zd\n", val); 2922 } 2923 2924 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev, 2925 struct device_attribute *attr, 2926 char *buf) 2927 { 2928 return sysfs_emit(buf, "%i\n", 0); 2929 } 2930 2931 2932 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev, 2933 struct device_attribute *attr, 2934 char *buf, 2935 enum pp_power_limit_level pp_limit_level) 2936 { 2937 struct amdgpu_device *adev = dev_get_drvdata(dev); 2938 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index; 2939 uint32_t limit; 2940 ssize_t size; 2941 int r; 2942 2943 if (amdgpu_in_reset(adev)) 2944 return -EPERM; 2945 if (adev->in_suspend && !adev->in_runpm) 2946 return -EPERM; 2947 2948 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2949 if (r < 0) { 2950 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2951 return r; 2952 } 2953 2954 r = amdgpu_dpm_get_power_limit(adev, &limit, 2955 pp_limit_level, power_type); 2956 2957 if (!r) 2958 size = sysfs_emit(buf, "%u\n", limit * 1000000); 2959 else 2960 size = sysfs_emit(buf, "\n"); 2961 2962 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2963 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2964 2965 return size; 2966 } 2967 2968 2969 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev, 2970 struct device_attribute *attr, 2971 char *buf) 2972 { 2973 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX); 2974 2975 } 2976 2977 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev, 2978 struct device_attribute *attr, 2979 char *buf) 2980 { 2981 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT); 2982 2983 } 2984 2985 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev, 2986 struct device_attribute *attr, 2987 char *buf) 2988 { 2989 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT); 2990 2991 } 2992 2993 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev, 2994 struct device_attribute *attr, 2995 char *buf) 2996 { 2997 struct amdgpu_device *adev = dev_get_drvdata(dev); 2998 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 2999 3000 if (gc_ver == IP_VERSION(10, 3, 1)) 3001 return sysfs_emit(buf, "%s\n", 3002 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ? 3003 "fastPPT" : "slowPPT"); 3004 else 3005 return sysfs_emit(buf, "PPT\n"); 3006 } 3007 3008 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev, 3009 struct device_attribute *attr, 3010 const char *buf, 3011 size_t count) 3012 { 3013 struct amdgpu_device *adev = dev_get_drvdata(dev); 3014 int limit_type = to_sensor_dev_attr(attr)->index; 3015 int err; 3016 u32 value; 3017 3018 if (amdgpu_in_reset(adev)) 3019 return -EPERM; 3020 if (adev->in_suspend && !adev->in_runpm) 3021 return -EPERM; 3022 3023 if (amdgpu_sriov_vf(adev)) 3024 return -EINVAL; 3025 3026 err = kstrtou32(buf, 10, &value); 3027 if (err) 3028 return err; 3029 3030 value = value / 1000000; /* convert to Watt */ 3031 value |= limit_type << 24; 3032 3033 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 3034 if (err < 0) { 3035 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3036 return err; 3037 } 3038 3039 err = amdgpu_dpm_set_power_limit(adev, value); 3040 3041 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 3042 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 3043 3044 if (err) 3045 return err; 3046 3047 return count; 3048 } 3049 3050 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev, 3051 struct device_attribute *attr, 3052 char *buf) 3053 { 3054 struct amdgpu_device *adev = dev_get_drvdata(dev); 3055 uint32_t sclk; 3056 int r; 3057 3058 /* get the sclk */ 3059 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK, 3060 (void *)&sclk); 3061 if (r) 3062 return r; 3063 3064 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000); 3065 } 3066 3067 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev, 3068 struct device_attribute *attr, 3069 char *buf) 3070 { 3071 return sysfs_emit(buf, "sclk\n"); 3072 } 3073 3074 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev, 3075 struct device_attribute *attr, 3076 char *buf) 3077 { 3078 struct amdgpu_device *adev = dev_get_drvdata(dev); 3079 uint32_t mclk; 3080 int r; 3081 3082 /* get the sclk */ 3083 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK, 3084 (void *)&mclk); 3085 if (r) 3086 return r; 3087 3088 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000); 3089 } 3090 3091 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev, 3092 struct device_attribute *attr, 3093 char *buf) 3094 { 3095 return sysfs_emit(buf, "mclk\n"); 3096 } 3097 3098 /** 3099 * DOC: hwmon 3100 * 3101 * The amdgpu driver exposes the following sensor interfaces: 3102 * 3103 * - GPU temperature (via the on-die sensor) 3104 * 3105 * - GPU voltage 3106 * 3107 * - Northbridge voltage (APUs only) 3108 * 3109 * - GPU power 3110 * 3111 * - GPU fan 3112 * 3113 * - GPU gfx/compute engine clock 3114 * 3115 * - GPU memory clock (dGPU only) 3116 * 3117 * hwmon interfaces for GPU temperature: 3118 * 3119 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius 3120 * - temp2_input and temp3_input are supported on SOC15 dGPUs only 3121 * 3122 * - temp[1-3]_label: temperature channel label 3123 * - temp2_label and temp3_label are supported on SOC15 dGPUs only 3124 * 3125 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius 3126 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only 3127 * 3128 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius 3129 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only 3130 * 3131 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius 3132 * - these are supported on SOC15 dGPUs only 3133 * 3134 * hwmon interfaces for GPU voltage: 3135 * 3136 * - in0_input: the voltage on the GPU in millivolts 3137 * 3138 * - in1_input: the voltage on the Northbridge in millivolts 3139 * 3140 * hwmon interfaces for GPU power: 3141 * 3142 * - power1_average: average power used by the SoC in microWatts. On APUs this includes the CPU. 3143 * 3144 * - power1_input: instantaneous power used by the SoC in microWatts. On APUs this includes the CPU. 3145 * 3146 * - power1_cap_min: minimum cap supported in microWatts 3147 * 3148 * - power1_cap_max: maximum cap supported in microWatts 3149 * 3150 * - power1_cap: selected power cap in microWatts 3151 * 3152 * hwmon interfaces for GPU fan: 3153 * 3154 * - pwm1: pulse width modulation fan level (0-255) 3155 * 3156 * - 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) 3157 * 3158 * - pwm1_min: pulse width modulation fan control minimum level (0) 3159 * 3160 * - pwm1_max: pulse width modulation fan control maximum level (255) 3161 * 3162 * - fan1_min: a minimum value Unit: revolution/min (RPM) 3163 * 3164 * - fan1_max: a maximum value Unit: revolution/max (RPM) 3165 * 3166 * - fan1_input: fan speed in RPM 3167 * 3168 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM) 3169 * 3170 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable 3171 * 3172 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time. 3173 * That will get the former one overridden. 3174 * 3175 * hwmon interfaces for GPU clocks: 3176 * 3177 * - freq1_input: the gfx/compute clock in hertz 3178 * 3179 * - freq2_input: the memory clock in hertz 3180 * 3181 * You can use hwmon tools like sensors to view this information on your system. 3182 * 3183 */ 3184 3185 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE); 3186 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0); 3187 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1); 3188 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE); 3189 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION); 3190 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0); 3191 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1); 3192 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION); 3193 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM); 3194 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0); 3195 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1); 3196 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM); 3197 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE); 3198 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION); 3199 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM); 3200 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0); 3201 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0); 3202 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0); 3203 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0); 3204 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0); 3205 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0); 3206 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0); 3207 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0); 3208 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0); 3209 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0); 3210 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0); 3211 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0); 3212 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0); 3213 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0); 3214 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0); 3215 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0); 3216 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0); 3217 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0); 3218 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0); 3219 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0); 3220 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1); 3221 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1); 3222 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1); 3223 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1); 3224 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1); 3225 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1); 3226 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0); 3227 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0); 3228 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0); 3229 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0); 3230 3231 static struct attribute *hwmon_attributes[] = { 3232 &sensor_dev_attr_temp1_input.dev_attr.attr, 3233 &sensor_dev_attr_temp1_crit.dev_attr.attr, 3234 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 3235 &sensor_dev_attr_temp2_input.dev_attr.attr, 3236 &sensor_dev_attr_temp2_crit.dev_attr.attr, 3237 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 3238 &sensor_dev_attr_temp3_input.dev_attr.attr, 3239 &sensor_dev_attr_temp3_crit.dev_attr.attr, 3240 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 3241 &sensor_dev_attr_temp1_emergency.dev_attr.attr, 3242 &sensor_dev_attr_temp2_emergency.dev_attr.attr, 3243 &sensor_dev_attr_temp3_emergency.dev_attr.attr, 3244 &sensor_dev_attr_temp1_label.dev_attr.attr, 3245 &sensor_dev_attr_temp2_label.dev_attr.attr, 3246 &sensor_dev_attr_temp3_label.dev_attr.attr, 3247 &sensor_dev_attr_pwm1.dev_attr.attr, 3248 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 3249 &sensor_dev_attr_pwm1_min.dev_attr.attr, 3250 &sensor_dev_attr_pwm1_max.dev_attr.attr, 3251 &sensor_dev_attr_fan1_input.dev_attr.attr, 3252 &sensor_dev_attr_fan1_min.dev_attr.attr, 3253 &sensor_dev_attr_fan1_max.dev_attr.attr, 3254 &sensor_dev_attr_fan1_target.dev_attr.attr, 3255 &sensor_dev_attr_fan1_enable.dev_attr.attr, 3256 &sensor_dev_attr_in0_input.dev_attr.attr, 3257 &sensor_dev_attr_in0_label.dev_attr.attr, 3258 &sensor_dev_attr_in1_input.dev_attr.attr, 3259 &sensor_dev_attr_in1_label.dev_attr.attr, 3260 &sensor_dev_attr_power1_average.dev_attr.attr, 3261 &sensor_dev_attr_power1_input.dev_attr.attr, 3262 &sensor_dev_attr_power1_cap_max.dev_attr.attr, 3263 &sensor_dev_attr_power1_cap_min.dev_attr.attr, 3264 &sensor_dev_attr_power1_cap.dev_attr.attr, 3265 &sensor_dev_attr_power1_cap_default.dev_attr.attr, 3266 &sensor_dev_attr_power1_label.dev_attr.attr, 3267 &sensor_dev_attr_power2_average.dev_attr.attr, 3268 &sensor_dev_attr_power2_cap_max.dev_attr.attr, 3269 &sensor_dev_attr_power2_cap_min.dev_attr.attr, 3270 &sensor_dev_attr_power2_cap.dev_attr.attr, 3271 &sensor_dev_attr_power2_cap_default.dev_attr.attr, 3272 &sensor_dev_attr_power2_label.dev_attr.attr, 3273 &sensor_dev_attr_freq1_input.dev_attr.attr, 3274 &sensor_dev_attr_freq1_label.dev_attr.attr, 3275 &sensor_dev_attr_freq2_input.dev_attr.attr, 3276 &sensor_dev_attr_freq2_label.dev_attr.attr, 3277 NULL 3278 }; 3279 3280 static umode_t hwmon_attributes_visible(struct kobject *kobj, 3281 struct attribute *attr, int index) 3282 { 3283 struct device *dev = kobj_to_dev(kobj); 3284 struct amdgpu_device *adev = dev_get_drvdata(dev); 3285 umode_t effective_mode = attr->mode; 3286 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 3287 uint32_t tmp; 3288 3289 /* under multi-vf mode, the hwmon attributes are all not supported */ 3290 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 3291 return 0; 3292 3293 /* under pp one vf mode manage of hwmon attributes is not supported */ 3294 if (amdgpu_sriov_is_pp_one_vf(adev)) 3295 effective_mode &= ~S_IWUSR; 3296 3297 /* Skip fan attributes if fan is not present */ 3298 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3299 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3300 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3301 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3302 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3303 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3304 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3305 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3306 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3307 return 0; 3308 3309 /* Skip fan attributes on APU */ 3310 if ((adev->flags & AMD_IS_APU) && 3311 (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3312 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3313 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3314 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3315 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3316 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3317 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3318 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3319 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3320 return 0; 3321 3322 /* Skip crit temp on APU */ 3323 if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) || 3324 (gc_ver == IP_VERSION(9, 4, 3))) && 3325 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3326 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr)) 3327 return 0; 3328 3329 /* Skip limit attributes if DPM is not enabled */ 3330 if (!adev->pm.dpm_enabled && 3331 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3332 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr || 3333 attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3334 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3335 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3336 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3337 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3338 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3339 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3340 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3341 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3342 return 0; 3343 3344 /* mask fan attributes if we have no bindings for this asic to expose */ 3345 if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) && 3346 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */ 3347 ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) && 3348 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */ 3349 effective_mode &= ~S_IRUGO; 3350 3351 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) && 3352 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */ 3353 ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) && 3354 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */ 3355 effective_mode &= ~S_IWUSR; 3356 3357 /* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */ 3358 if (((adev->family == AMDGPU_FAMILY_SI) || 3359 ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) && 3360 (gc_ver != IP_VERSION(9, 4, 3)))) && 3361 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr || 3362 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr || 3363 attr == &sensor_dev_attr_power1_cap.dev_attr.attr || 3364 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr)) 3365 return 0; 3366 3367 /* not implemented yet for APUs having < GC 9.3.0 (Renoir) */ 3368 if (((adev->family == AMDGPU_FAMILY_SI) || 3369 ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) && 3370 (attr == &sensor_dev_attr_power1_average.dev_attr.attr)) 3371 return 0; 3372 3373 /* not all products support both average and instantaneous */ 3374 if (attr == &sensor_dev_attr_power1_average.dev_attr.attr && 3375 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP) 3376 return 0; 3377 if (attr == &sensor_dev_attr_power1_input.dev_attr.attr && 3378 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP) 3379 return 0; 3380 3381 /* hide max/min values if we can't both query and manage the fan */ 3382 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) && 3383 (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) && 3384 (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) && 3385 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) && 3386 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3387 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr)) 3388 return 0; 3389 3390 if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) && 3391 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) && 3392 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3393 attr == &sensor_dev_attr_fan1_min.dev_attr.attr)) 3394 return 0; 3395 3396 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */ 3397 adev->family == AMDGPU_FAMILY_KV || /* not implemented yet */ 3398 (gc_ver == IP_VERSION(9, 4, 3))) && 3399 (attr == &sensor_dev_attr_in0_input.dev_attr.attr || 3400 attr == &sensor_dev_attr_in0_label.dev_attr.attr)) 3401 return 0; 3402 3403 /* only APUs other than gc 9,4,3 have vddnb */ 3404 if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) && 3405 (attr == &sensor_dev_attr_in1_input.dev_attr.attr || 3406 attr == &sensor_dev_attr_in1_label.dev_attr.attr)) 3407 return 0; 3408 3409 /* no mclk on APUs other than gc 9,4,3*/ 3410 if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) && 3411 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr || 3412 attr == &sensor_dev_attr_freq2_label.dev_attr.attr)) 3413 return 0; 3414 3415 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) && 3416 (gc_ver != IP_VERSION(9, 4, 3)) && 3417 (attr == &sensor_dev_attr_temp2_input.dev_attr.attr || 3418 attr == &sensor_dev_attr_temp2_label.dev_attr.attr || 3419 attr == &sensor_dev_attr_temp2_crit.dev_attr.attr || 3420 attr == &sensor_dev_attr_temp3_input.dev_attr.attr || 3421 attr == &sensor_dev_attr_temp3_label.dev_attr.attr || 3422 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr)) 3423 return 0; 3424 3425 /* hotspot temperature for gc 9,4,3*/ 3426 if ((gc_ver == IP_VERSION(9, 4, 3)) && 3427 (attr == &sensor_dev_attr_temp1_input.dev_attr.attr || 3428 attr == &sensor_dev_attr_temp1_label.dev_attr.attr)) 3429 return 0; 3430 3431 /* only SOC15 dGPUs support hotspot and mem temperatures */ 3432 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0) || 3433 (gc_ver == IP_VERSION(9, 4, 3))) && 3434 (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr || 3435 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr || 3436 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr || 3437 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr || 3438 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)) 3439 return 0; 3440 3441 /* only Vangogh has fast PPT limit and power labels */ 3442 if (!(gc_ver == IP_VERSION(10, 3, 1)) && 3443 (attr == &sensor_dev_attr_power2_average.dev_attr.attr || 3444 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr || 3445 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr || 3446 attr == &sensor_dev_attr_power2_cap.dev_attr.attr || 3447 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr || 3448 attr == &sensor_dev_attr_power2_label.dev_attr.attr)) 3449 return 0; 3450 3451 return effective_mode; 3452 } 3453 3454 static const struct attribute_group hwmon_attrgroup = { 3455 .attrs = hwmon_attributes, 3456 .is_visible = hwmon_attributes_visible, 3457 }; 3458 3459 static const struct attribute_group *hwmon_groups[] = { 3460 &hwmon_attrgroup, 3461 NULL 3462 }; 3463 3464 static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev, 3465 enum pp_clock_type od_type, 3466 char *buf) 3467 { 3468 int size = 0; 3469 int ret; 3470 3471 if (amdgpu_in_reset(adev)) 3472 return -EPERM; 3473 if (adev->in_suspend && !adev->in_runpm) 3474 return -EPERM; 3475 3476 ret = pm_runtime_get_sync(adev->dev); 3477 if (ret < 0) { 3478 pm_runtime_put_autosuspend(adev->dev); 3479 return ret; 3480 } 3481 3482 size = amdgpu_dpm_print_clock_levels(adev, od_type, buf); 3483 if (size == 0) 3484 size = sysfs_emit(buf, "\n"); 3485 3486 pm_runtime_mark_last_busy(adev->dev); 3487 pm_runtime_put_autosuspend(adev->dev); 3488 3489 return size; 3490 } 3491 3492 static int parse_input_od_command_lines(const char *buf, 3493 size_t count, 3494 u32 *type, 3495 long *params, 3496 uint32_t *num_of_params) 3497 { 3498 const char delimiter[3] = {' ', '\n', '\0'}; 3499 uint32_t parameter_size = 0; 3500 char buf_cpy[128] = {0}; 3501 char *tmp_str, *sub_str; 3502 int ret; 3503 3504 if (count > sizeof(buf_cpy) - 1) 3505 return -EINVAL; 3506 3507 memcpy(buf_cpy, buf, count); 3508 tmp_str = buf_cpy; 3509 3510 /* skip heading spaces */ 3511 while (isspace(*tmp_str)) 3512 tmp_str++; 3513 3514 switch (*tmp_str) { 3515 case 'c': 3516 *type = PP_OD_COMMIT_DPM_TABLE; 3517 return 0; 3518 case 'r': 3519 params[parameter_size] = *type; 3520 *num_of_params = 1; 3521 *type = PP_OD_RESTORE_DEFAULT_TABLE; 3522 return 0; 3523 default: 3524 break; 3525 } 3526 3527 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) { 3528 if (strlen(sub_str) == 0) 3529 continue; 3530 3531 ret = kstrtol(sub_str, 0, ¶ms[parameter_size]); 3532 if (ret) 3533 return -EINVAL; 3534 parameter_size++; 3535 3536 while (isspace(*tmp_str)) 3537 tmp_str++; 3538 } 3539 3540 *num_of_params = parameter_size; 3541 3542 return 0; 3543 } 3544 3545 static int 3546 amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev, 3547 enum PP_OD_DPM_TABLE_COMMAND cmd_type, 3548 const char *in_buf, 3549 size_t count) 3550 { 3551 uint32_t parameter_size = 0; 3552 long parameter[64]; 3553 int ret; 3554 3555 if (amdgpu_in_reset(adev)) 3556 return -EPERM; 3557 if (adev->in_suspend && !adev->in_runpm) 3558 return -EPERM; 3559 3560 ret = parse_input_od_command_lines(in_buf, 3561 count, 3562 &cmd_type, 3563 parameter, 3564 ¶meter_size); 3565 if (ret) 3566 return ret; 3567 3568 ret = pm_runtime_get_sync(adev->dev); 3569 if (ret < 0) 3570 goto err_out0; 3571 3572 ret = amdgpu_dpm_odn_edit_dpm_table(adev, 3573 cmd_type, 3574 parameter, 3575 parameter_size); 3576 if (ret) 3577 goto err_out1; 3578 3579 if (cmd_type == PP_OD_COMMIT_DPM_TABLE) { 3580 ret = amdgpu_dpm_dispatch_task(adev, 3581 AMD_PP_TASK_READJUST_POWER_STATE, 3582 NULL); 3583 if (ret) 3584 goto err_out1; 3585 } 3586 3587 pm_runtime_mark_last_busy(adev->dev); 3588 pm_runtime_put_autosuspend(adev->dev); 3589 3590 return count; 3591 3592 err_out1: 3593 pm_runtime_mark_last_busy(adev->dev); 3594 err_out0: 3595 pm_runtime_put_autosuspend(adev->dev); 3596 3597 return ret; 3598 } 3599 3600 /** 3601 * DOC: fan_curve 3602 * 3603 * The amdgpu driver provides a sysfs API for checking and adjusting the fan 3604 * control curve line. 3605 * 3606 * Reading back the file shows you the current settings(temperature in Celsius 3607 * degree and fan speed in pwm) applied to every anchor point of the curve line 3608 * and their permitted ranges if changable. 3609 * 3610 * Writing a desired string(with the format like "anchor_point_index temperature 3611 * fan_speed_in_pwm") to the file, change the settings for the specific anchor 3612 * point accordingly. 3613 * 3614 * When you have finished the editing, write "c" (commit) to the file to commit 3615 * your changes. 3616 * 3617 * If you want to reset to the default value, write "r" (reset) to the file to 3618 * reset them 3619 * 3620 * There are two fan control modes supported: auto and manual. With auto mode, 3621 * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature). 3622 * While with manual mode, users can set their own fan curve line as what 3623 * described here. Normally the ASIC is booted up with auto mode. Any 3624 * settings via this interface will switch the fan control to manual mode 3625 * implicitly. 3626 */ 3627 static ssize_t fan_curve_show(struct kobject *kobj, 3628 struct kobj_attribute *attr, 3629 char *buf) 3630 { 3631 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3632 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3633 3634 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf); 3635 } 3636 3637 static ssize_t fan_curve_store(struct kobject *kobj, 3638 struct kobj_attribute *attr, 3639 const char *buf, 3640 size_t count) 3641 { 3642 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3643 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3644 3645 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 3646 PP_OD_EDIT_FAN_CURVE, 3647 buf, 3648 count); 3649 } 3650 3651 static umode_t fan_curve_visible(struct amdgpu_device *adev) 3652 { 3653 umode_t umode = 0000; 3654 3655 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE) 3656 umode |= S_IRUSR | S_IRGRP | S_IROTH; 3657 3658 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET) 3659 umode |= S_IWUSR; 3660 3661 return umode; 3662 } 3663 3664 /** 3665 * DOC: acoustic_limit_rpm_threshold 3666 * 3667 * The amdgpu driver provides a sysfs API for checking and adjusting the 3668 * acoustic limit in RPM for fan control. 3669 * 3670 * Reading back the file shows you the current setting and the permitted 3671 * ranges if changable. 3672 * 3673 * Writing an integer to the file, change the setting accordingly. 3674 * 3675 * When you have finished the editing, write "c" (commit) to the file to commit 3676 * your changes. 3677 * 3678 * This setting works under auto fan control mode only. It adjusts the PMFW's 3679 * behavior about the maximum speed in RPM the fan can spin. Setting via this 3680 * interface will switch the fan control to auto mode implicitly. 3681 */ 3682 static ssize_t acoustic_limit_threshold_show(struct kobject *kobj, 3683 struct kobj_attribute *attr, 3684 char *buf) 3685 { 3686 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3687 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3688 3689 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf); 3690 } 3691 3692 static ssize_t acoustic_limit_threshold_store(struct kobject *kobj, 3693 struct kobj_attribute *attr, 3694 const char *buf, 3695 size_t count) 3696 { 3697 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3698 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3699 3700 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 3701 PP_OD_EDIT_ACOUSTIC_LIMIT, 3702 buf, 3703 count); 3704 } 3705 3706 static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev) 3707 { 3708 umode_t umode = 0000; 3709 3710 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE) 3711 umode |= S_IRUSR | S_IRGRP | S_IROTH; 3712 3713 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET) 3714 umode |= S_IWUSR; 3715 3716 return umode; 3717 } 3718 3719 /** 3720 * DOC: acoustic_target_rpm_threshold 3721 * 3722 * The amdgpu driver provides a sysfs API for checking and adjusting the 3723 * acoustic target in RPM for fan control. 3724 * 3725 * Reading back the file shows you the current setting and the permitted 3726 * ranges if changable. 3727 * 3728 * Writing an integer to the file, change the setting accordingly. 3729 * 3730 * When you have finished the editing, write "c" (commit) to the file to commit 3731 * your changes. 3732 * 3733 * This setting works under auto fan control mode only. It can co-exist with 3734 * other settings which can work also under auto mode. It adjusts the PMFW's 3735 * behavior about the maximum speed in RPM the fan can spin when ASIC 3736 * temperature is not greater than target temperature. Setting via this 3737 * interface will switch the fan control to auto mode implicitly. 3738 */ 3739 static ssize_t acoustic_target_threshold_show(struct kobject *kobj, 3740 struct kobj_attribute *attr, 3741 char *buf) 3742 { 3743 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3744 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3745 3746 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf); 3747 } 3748 3749 static ssize_t acoustic_target_threshold_store(struct kobject *kobj, 3750 struct kobj_attribute *attr, 3751 const char *buf, 3752 size_t count) 3753 { 3754 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3755 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3756 3757 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 3758 PP_OD_EDIT_ACOUSTIC_TARGET, 3759 buf, 3760 count); 3761 } 3762 3763 static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev) 3764 { 3765 umode_t umode = 0000; 3766 3767 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE) 3768 umode |= S_IRUSR | S_IRGRP | S_IROTH; 3769 3770 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET) 3771 umode |= S_IWUSR; 3772 3773 return umode; 3774 } 3775 3776 /** 3777 * DOC: fan_target_temperature 3778 * 3779 * The amdgpu driver provides a sysfs API for checking and adjusting the 3780 * target tempeature in Celsius degree for fan control. 3781 * 3782 * Reading back the file shows you the current setting and the permitted 3783 * ranges if changable. 3784 * 3785 * Writing an integer to the file, change the setting accordingly. 3786 * 3787 * When you have finished the editing, write "c" (commit) to the file to commit 3788 * your changes. 3789 * 3790 * This setting works under auto fan control mode only. It can co-exist with 3791 * other settings which can work also under auto mode. Paring with the 3792 * acoustic_target_rpm_threshold setting, they define the maximum speed in 3793 * RPM the fan can spin when ASIC temperature is not greater than target 3794 * temperature. Setting via this interface will switch the fan control to 3795 * auto mode implicitly. 3796 */ 3797 static ssize_t fan_target_temperature_show(struct kobject *kobj, 3798 struct kobj_attribute *attr, 3799 char *buf) 3800 { 3801 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3802 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3803 3804 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf); 3805 } 3806 3807 static ssize_t fan_target_temperature_store(struct kobject *kobj, 3808 struct kobj_attribute *attr, 3809 const char *buf, 3810 size_t count) 3811 { 3812 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3813 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3814 3815 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 3816 PP_OD_EDIT_FAN_TARGET_TEMPERATURE, 3817 buf, 3818 count); 3819 } 3820 3821 static umode_t fan_target_temperature_visible(struct amdgpu_device *adev) 3822 { 3823 umode_t umode = 0000; 3824 3825 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE) 3826 umode |= S_IRUSR | S_IRGRP | S_IROTH; 3827 3828 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET) 3829 umode |= S_IWUSR; 3830 3831 return umode; 3832 } 3833 3834 /** 3835 * DOC: fan_minimum_pwm 3836 * 3837 * The amdgpu driver provides a sysfs API for checking and adjusting the 3838 * minimum fan speed in PWM. 3839 * 3840 * Reading back the file shows you the current setting and the permitted 3841 * ranges if changable. 3842 * 3843 * Writing an integer to the file, change the setting accordingly. 3844 * 3845 * When you have finished the editing, write "c" (commit) to the file to commit 3846 * your changes. 3847 * 3848 * This setting works under auto fan control mode only. It can co-exist with 3849 * other settings which can work also under auto mode. It adjusts the PMFW's 3850 * behavior about the minimum fan speed in PWM the fan should spin. Setting 3851 * via this interface will switch the fan control to auto mode implicitly. 3852 */ 3853 static ssize_t fan_minimum_pwm_show(struct kobject *kobj, 3854 struct kobj_attribute *attr, 3855 char *buf) 3856 { 3857 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3858 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3859 3860 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf); 3861 } 3862 3863 static ssize_t fan_minimum_pwm_store(struct kobject *kobj, 3864 struct kobj_attribute *attr, 3865 const char *buf, 3866 size_t count) 3867 { 3868 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 3869 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 3870 3871 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 3872 PP_OD_EDIT_FAN_MINIMUM_PWM, 3873 buf, 3874 count); 3875 } 3876 3877 static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev) 3878 { 3879 umode_t umode = 0000; 3880 3881 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE) 3882 umode |= S_IRUSR | S_IRGRP | S_IROTH; 3883 3884 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET) 3885 umode |= S_IWUSR; 3886 3887 return umode; 3888 } 3889 3890 static struct od_feature_set amdgpu_od_set = { 3891 .containers = { 3892 [0] = { 3893 .name = "fan_ctrl", 3894 .sub_feature = { 3895 [0] = { 3896 .name = "fan_curve", 3897 .ops = { 3898 .is_visible = fan_curve_visible, 3899 .show = fan_curve_show, 3900 .store = fan_curve_store, 3901 }, 3902 }, 3903 [1] = { 3904 .name = "acoustic_limit_rpm_threshold", 3905 .ops = { 3906 .is_visible = acoustic_limit_threshold_visible, 3907 .show = acoustic_limit_threshold_show, 3908 .store = acoustic_limit_threshold_store, 3909 }, 3910 }, 3911 [2] = { 3912 .name = "acoustic_target_rpm_threshold", 3913 .ops = { 3914 .is_visible = acoustic_target_threshold_visible, 3915 .show = acoustic_target_threshold_show, 3916 .store = acoustic_target_threshold_store, 3917 }, 3918 }, 3919 [3] = { 3920 .name = "fan_target_temperature", 3921 .ops = { 3922 .is_visible = fan_target_temperature_visible, 3923 .show = fan_target_temperature_show, 3924 .store = fan_target_temperature_store, 3925 }, 3926 }, 3927 [4] = { 3928 .name = "fan_minimum_pwm", 3929 .ops = { 3930 .is_visible = fan_minimum_pwm_visible, 3931 .show = fan_minimum_pwm_show, 3932 .store = fan_minimum_pwm_store, 3933 }, 3934 }, 3935 }, 3936 }, 3937 }, 3938 }; 3939 3940 static void od_kobj_release(struct kobject *kobj) 3941 { 3942 struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj); 3943 3944 kfree(od_kobj); 3945 } 3946 3947 static const struct kobj_type od_ktype = { 3948 .release = od_kobj_release, 3949 .sysfs_ops = &kobj_sysfs_ops, 3950 }; 3951 3952 static void amdgpu_od_set_fini(struct amdgpu_device *adev) 3953 { 3954 struct od_kobj *container, *container_next; 3955 struct od_attribute *attribute, *attribute_next; 3956 3957 if (list_empty(&adev->pm.od_kobj_list)) 3958 return; 3959 3960 list_for_each_entry_safe(container, container_next, 3961 &adev->pm.od_kobj_list, entry) { 3962 list_del(&container->entry); 3963 3964 list_for_each_entry_safe(attribute, attribute_next, 3965 &container->attribute, entry) { 3966 list_del(&attribute->entry); 3967 sysfs_remove_file(&container->kobj, 3968 &attribute->attribute.attr); 3969 kfree(attribute); 3970 } 3971 3972 kobject_put(&container->kobj); 3973 } 3974 } 3975 3976 static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev, 3977 struct od_feature_ops *feature_ops) 3978 { 3979 umode_t mode; 3980 3981 if (!feature_ops->is_visible) 3982 return false; 3983 3984 /* 3985 * If the feature has no user read and write mode set, 3986 * we can assume the feature is actually not supported.(?) 3987 * And the revelant sysfs interface should not be exposed. 3988 */ 3989 mode = feature_ops->is_visible(adev); 3990 if (mode & (S_IRUSR | S_IWUSR)) 3991 return true; 3992 3993 return false; 3994 } 3995 3996 static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev, 3997 struct od_feature_container *container) 3998 { 3999 int i; 4000 4001 /* 4002 * If there is no valid entry within the container, the container 4003 * is recognized as a self contained container. And the valid entry 4004 * here means it has a valid naming and it is visible/supported by 4005 * the ASIC. 4006 */ 4007 for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) { 4008 if (container->sub_feature[i].name && 4009 amdgpu_is_od_feature_supported(adev, 4010 &container->sub_feature[i].ops)) 4011 return false; 4012 } 4013 4014 return true; 4015 } 4016 4017 static int amdgpu_od_set_init(struct amdgpu_device *adev) 4018 { 4019 struct od_kobj *top_set, *sub_set; 4020 struct od_attribute *attribute; 4021 struct od_feature_container *container; 4022 struct od_feature_item *feature; 4023 int i, j; 4024 int ret; 4025 4026 /* Setup the top `gpu_od` directory which holds all other OD interfaces */ 4027 top_set = kzalloc(sizeof(*top_set), GFP_KERNEL); 4028 if (!top_set) 4029 return -ENOMEM; 4030 list_add(&top_set->entry, &adev->pm.od_kobj_list); 4031 4032 ret = kobject_init_and_add(&top_set->kobj, 4033 &od_ktype, 4034 &adev->dev->kobj, 4035 "%s", 4036 "gpu_od"); 4037 if (ret) 4038 goto err_out; 4039 INIT_LIST_HEAD(&top_set->attribute); 4040 top_set->priv = adev; 4041 4042 for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) { 4043 container = &amdgpu_od_set.containers[i]; 4044 4045 if (!container->name) 4046 continue; 4047 4048 /* 4049 * If there is valid entries within the container, the container 4050 * will be presented as a sub directory and all its holding entries 4051 * will be presented as plain files under it. 4052 * While if there is no valid entry within the container, the container 4053 * itself will be presented as a plain file under top `gpu_od` directory. 4054 */ 4055 if (amdgpu_od_is_self_contained(adev, container)) { 4056 if (!amdgpu_is_od_feature_supported(adev, 4057 &container->ops)) 4058 continue; 4059 4060 /* 4061 * The container is presented as a plain file under top `gpu_od` 4062 * directory. 4063 */ 4064 attribute = kzalloc(sizeof(*attribute), GFP_KERNEL); 4065 if (!attribute) { 4066 ret = -ENOMEM; 4067 goto err_out; 4068 } 4069 list_add(&attribute->entry, &top_set->attribute); 4070 4071 attribute->attribute.attr.mode = 4072 container->ops.is_visible(adev); 4073 attribute->attribute.attr.name = container->name; 4074 attribute->attribute.show = 4075 container->ops.show; 4076 attribute->attribute.store = 4077 container->ops.store; 4078 ret = sysfs_create_file(&top_set->kobj, 4079 &attribute->attribute.attr); 4080 if (ret) 4081 goto err_out; 4082 } else { 4083 /* The container is presented as a sub directory. */ 4084 sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL); 4085 if (!sub_set) { 4086 ret = -ENOMEM; 4087 goto err_out; 4088 } 4089 list_add(&sub_set->entry, &adev->pm.od_kobj_list); 4090 4091 ret = kobject_init_and_add(&sub_set->kobj, 4092 &od_ktype, 4093 &top_set->kobj, 4094 "%s", 4095 container->name); 4096 if (ret) 4097 goto err_out; 4098 INIT_LIST_HEAD(&sub_set->attribute); 4099 sub_set->priv = adev; 4100 4101 for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) { 4102 feature = &container->sub_feature[j]; 4103 if (!feature->name) 4104 continue; 4105 4106 if (!amdgpu_is_od_feature_supported(adev, 4107 &feature->ops)) 4108 continue; 4109 4110 /* 4111 * With the container presented as a sub directory, the entry within 4112 * it is presented as a plain file under the sub directory. 4113 */ 4114 attribute = kzalloc(sizeof(*attribute), GFP_KERNEL); 4115 if (!attribute) { 4116 ret = -ENOMEM; 4117 goto err_out; 4118 } 4119 list_add(&attribute->entry, &sub_set->attribute); 4120 4121 attribute->attribute.attr.mode = 4122 feature->ops.is_visible(adev); 4123 attribute->attribute.attr.name = feature->name; 4124 attribute->attribute.show = 4125 feature->ops.show; 4126 attribute->attribute.store = 4127 feature->ops.store; 4128 ret = sysfs_create_file(&sub_set->kobj, 4129 &attribute->attribute.attr); 4130 if (ret) 4131 goto err_out; 4132 } 4133 } 4134 } 4135 4136 return 0; 4137 4138 err_out: 4139 amdgpu_od_set_fini(adev); 4140 4141 return ret; 4142 } 4143 4144 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev) 4145 { 4146 uint32_t mask = 0; 4147 int ret; 4148 4149 if (adev->pm.sysfs_initialized) 4150 return 0; 4151 4152 INIT_LIST_HEAD(&adev->pm.pm_attr_list); 4153 4154 if (adev->pm.dpm_enabled == 0) 4155 return 0; 4156 4157 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev, 4158 DRIVER_NAME, adev, 4159 hwmon_groups); 4160 if (IS_ERR(adev->pm.int_hwmon_dev)) { 4161 ret = PTR_ERR(adev->pm.int_hwmon_dev); 4162 dev_err(adev->dev, 4163 "Unable to register hwmon device: %d\n", ret); 4164 return ret; 4165 } 4166 4167 switch (amdgpu_virt_get_sriov_vf_mode(adev)) { 4168 case SRIOV_VF_MODE_ONE_VF: 4169 mask = ATTR_FLAG_ONEVF; 4170 break; 4171 case SRIOV_VF_MODE_MULTI_VF: 4172 mask = 0; 4173 break; 4174 case SRIOV_VF_MODE_BARE_METAL: 4175 default: 4176 mask = ATTR_FLAG_MASK_ALL; 4177 break; 4178 } 4179 4180 ret = amdgpu_device_attr_create_groups(adev, 4181 amdgpu_device_attrs, 4182 ARRAY_SIZE(amdgpu_device_attrs), 4183 mask, 4184 &adev->pm.pm_attr_list); 4185 if (ret) 4186 goto err_out0; 4187 4188 if (amdgpu_dpm_is_overdrive_supported(adev)) { 4189 ret = amdgpu_od_set_init(adev); 4190 if (ret) 4191 goto err_out1; 4192 } 4193 4194 adev->pm.sysfs_initialized = true; 4195 4196 return 0; 4197 4198 err_out1: 4199 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 4200 err_out0: 4201 if (adev->pm.int_hwmon_dev) 4202 hwmon_device_unregister(adev->pm.int_hwmon_dev); 4203 4204 return ret; 4205 } 4206 4207 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev) 4208 { 4209 amdgpu_od_set_fini(adev); 4210 4211 if (adev->pm.int_hwmon_dev) 4212 hwmon_device_unregister(adev->pm.int_hwmon_dev); 4213 4214 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 4215 } 4216 4217 /* 4218 * Debugfs info 4219 */ 4220 #if defined(CONFIG_DEBUG_FS) 4221 4222 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m, 4223 struct amdgpu_device *adev) 4224 { 4225 uint16_t *p_val; 4226 uint32_t size; 4227 int i; 4228 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev); 4229 4230 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) { 4231 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t), 4232 GFP_KERNEL); 4233 4234 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK, 4235 (void *)p_val, &size)) { 4236 for (i = 0; i < num_cpu_cores; i++) 4237 seq_printf(m, "\t%u MHz (CPU%d)\n", 4238 *(p_val + i), i); 4239 } 4240 4241 kfree(p_val); 4242 } 4243 } 4244 4245 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev) 4246 { 4247 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0); 4248 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 4249 uint32_t value; 4250 uint64_t value64 = 0; 4251 uint32_t query = 0; 4252 int size; 4253 4254 /* GPU Clocks */ 4255 size = sizeof(value); 4256 seq_printf(m, "GFX Clocks and Power:\n"); 4257 4258 amdgpu_debugfs_prints_cpu_info(m, adev); 4259 4260 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size)) 4261 seq_printf(m, "\t%u MHz (MCLK)\n", value/100); 4262 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size)) 4263 seq_printf(m, "\t%u MHz (SCLK)\n", value/100); 4264 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size)) 4265 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100); 4266 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size)) 4267 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100); 4268 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size)) 4269 seq_printf(m, "\t%u mV (VDDGFX)\n", value); 4270 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size)) 4271 seq_printf(m, "\t%u mV (VDDNB)\n", value); 4272 size = sizeof(uint32_t); 4273 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) 4274 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff); 4275 size = sizeof(uint32_t); 4276 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) 4277 seq_printf(m, "\t%u.%u W (current GPU)\n", query >> 8, query & 0xff); 4278 size = sizeof(value); 4279 seq_printf(m, "\n"); 4280 4281 /* GPU Temp */ 4282 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size)) 4283 seq_printf(m, "GPU Temperature: %u C\n", value/1000); 4284 4285 /* GPU Load */ 4286 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size)) 4287 seq_printf(m, "GPU Load: %u %%\n", value); 4288 /* MEM Load */ 4289 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size)) 4290 seq_printf(m, "MEM Load: %u %%\n", value); 4291 4292 seq_printf(m, "\n"); 4293 4294 /* SMC feature mask */ 4295 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size)) 4296 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64); 4297 4298 /* ASICs greater than CHIP_VEGA20 supports these sensors */ 4299 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) { 4300 /* VCN clocks */ 4301 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) { 4302 if (!value) { 4303 seq_printf(m, "VCN: Disabled\n"); 4304 } else { 4305 seq_printf(m, "VCN: Enabled\n"); 4306 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 4307 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 4308 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 4309 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 4310 } 4311 } 4312 seq_printf(m, "\n"); 4313 } else { 4314 /* UVD clocks */ 4315 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) { 4316 if (!value) { 4317 seq_printf(m, "UVD: Disabled\n"); 4318 } else { 4319 seq_printf(m, "UVD: Enabled\n"); 4320 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 4321 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 4322 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 4323 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 4324 } 4325 } 4326 seq_printf(m, "\n"); 4327 4328 /* VCE clocks */ 4329 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) { 4330 if (!value) { 4331 seq_printf(m, "VCE: Disabled\n"); 4332 } else { 4333 seq_printf(m, "VCE: Enabled\n"); 4334 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size)) 4335 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100); 4336 } 4337 } 4338 } 4339 4340 return 0; 4341 } 4342 4343 static const struct cg_flag_name clocks[] = { 4344 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"}, 4345 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"}, 4346 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"}, 4347 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"}, 4348 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"}, 4349 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"}, 4350 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"}, 4351 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"}, 4352 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"}, 4353 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"}, 4354 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"}, 4355 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"}, 4356 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"}, 4357 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"}, 4358 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"}, 4359 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"}, 4360 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"}, 4361 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"}, 4362 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"}, 4363 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"}, 4364 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"}, 4365 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"}, 4366 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"}, 4367 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"}, 4368 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"}, 4369 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"}, 4370 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"}, 4371 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"}, 4372 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"}, 4373 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"}, 4374 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"}, 4375 {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"}, 4376 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"}, 4377 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"}, 4378 {0, NULL}, 4379 }; 4380 4381 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags) 4382 { 4383 int i; 4384 4385 for (i = 0; clocks[i].flag; i++) 4386 seq_printf(m, "\t%s: %s\n", clocks[i].name, 4387 (flags & clocks[i].flag) ? "On" : "Off"); 4388 } 4389 4390 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused) 4391 { 4392 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 4393 struct drm_device *dev = adev_to_drm(adev); 4394 u64 flags = 0; 4395 int r; 4396 4397 if (amdgpu_in_reset(adev)) 4398 return -EPERM; 4399 if (adev->in_suspend && !adev->in_runpm) 4400 return -EPERM; 4401 4402 r = pm_runtime_get_sync(dev->dev); 4403 if (r < 0) { 4404 pm_runtime_put_autosuspend(dev->dev); 4405 return r; 4406 } 4407 4408 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) { 4409 r = amdgpu_debugfs_pm_info_pp(m, adev); 4410 if (r) 4411 goto out; 4412 } 4413 4414 amdgpu_device_ip_get_clockgating_state(adev, &flags); 4415 4416 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags); 4417 amdgpu_parse_cg_state(m, flags); 4418 seq_printf(m, "\n"); 4419 4420 out: 4421 pm_runtime_mark_last_busy(dev->dev); 4422 pm_runtime_put_autosuspend(dev->dev); 4423 4424 return r; 4425 } 4426 4427 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info); 4428 4429 /* 4430 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW 4431 * 4432 * Reads debug memory region allocated to PMFW 4433 */ 4434 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf, 4435 size_t size, loff_t *pos) 4436 { 4437 struct amdgpu_device *adev = file_inode(f)->i_private; 4438 size_t smu_prv_buf_size; 4439 void *smu_prv_buf; 4440 int ret = 0; 4441 4442 if (amdgpu_in_reset(adev)) 4443 return -EPERM; 4444 if (adev->in_suspend && !adev->in_runpm) 4445 return -EPERM; 4446 4447 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size); 4448 if (ret) 4449 return ret; 4450 4451 if (!smu_prv_buf || !smu_prv_buf_size) 4452 return -EINVAL; 4453 4454 return simple_read_from_buffer(buf, size, pos, smu_prv_buf, 4455 smu_prv_buf_size); 4456 } 4457 4458 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = { 4459 .owner = THIS_MODULE, 4460 .open = simple_open, 4461 .read = amdgpu_pm_prv_buffer_read, 4462 .llseek = default_llseek, 4463 }; 4464 4465 #endif 4466 4467 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev) 4468 { 4469 #if defined(CONFIG_DEBUG_FS) 4470 struct drm_minor *minor = adev_to_drm(adev)->primary; 4471 struct dentry *root = minor->debugfs_root; 4472 4473 if (!adev->pm.dpm_enabled) 4474 return; 4475 4476 debugfs_create_file("amdgpu_pm_info", 0444, root, adev, 4477 &amdgpu_debugfs_pm_info_fops); 4478 4479 if (adev->pm.smu_prv_buffer_size > 0) 4480 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root, 4481 adev, 4482 &amdgpu_debugfs_pm_prv_buffer_fops, 4483 adev->pm.smu_prv_buffer_size); 4484 4485 amdgpu_dpm_stb_debug_fs_init(adev); 4486 #endif 4487 } 4488