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