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