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