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 <linux/units.h> 38 #include <asm/processor.h> 39 40 #define MAX_NUM_OF_FEATURES_PER_SUBSET 8 41 #define MAX_NUM_OF_SUBSETS 8 42 43 #define DEVICE_ATTR_IS(_name) (attr_id == device_attr_id__##_name) 44 45 struct od_attribute { 46 struct kobj_attribute attribute; 47 struct list_head entry; 48 }; 49 50 struct od_kobj { 51 struct kobject kobj; 52 struct list_head entry; 53 struct list_head attribute; 54 void *priv; 55 }; 56 57 struct od_feature_ops { 58 umode_t (*is_visible)(struct amdgpu_device *adev); 59 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, 60 char *buf); 61 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, 62 const char *buf, size_t count); 63 }; 64 65 struct od_feature_item { 66 const char *name; 67 struct od_feature_ops ops; 68 }; 69 70 struct od_feature_container { 71 char *name; 72 struct od_feature_ops ops; 73 struct od_feature_item sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET]; 74 }; 75 76 struct od_feature_set { 77 struct od_feature_container containers[MAX_NUM_OF_SUBSETS]; 78 }; 79 80 static const struct hwmon_temp_label { 81 enum PP_HWMON_TEMP channel; 82 const char *label; 83 } temp_label[] = { 84 {PP_TEMP_EDGE, "edge"}, 85 {PP_TEMP_JUNCTION, "junction"}, 86 {PP_TEMP_MEM, "mem"}, 87 }; 88 89 const char * const amdgpu_pp_profile_name[] = { 90 "BOOTUP_DEFAULT", 91 "3D_FULL_SCREEN", 92 "POWER_SAVING", 93 "VIDEO", 94 "VR", 95 "COMPUTE", 96 "CUSTOM", 97 "WINDOW_3D", 98 "CAPPED", 99 "UNCAPPED", 100 }; 101 102 static int amdgpu_pm_parse_long_params(char *str, long *params, 103 uint32_t max_params, 104 uint32_t *num_params) 105 { 106 const char delimiter[] = { ' ', '\n', '\0' }; 107 uint32_t count = 0; 108 char *sub_str; 109 int ret; 110 111 if (!params || !num_params) 112 return -EINVAL; 113 114 while ((sub_str = strsep(&str, delimiter)) != NULL) { 115 if (strlen(sub_str) == 0) 116 continue; 117 if (count >= max_params) 118 return -EINVAL; 119 ret = kstrtol(sub_str, 0, ¶ms[count]); 120 if (ret) 121 return -EINVAL; 122 count++; 123 if (!str) 124 break; 125 while (isspace(*str)) 126 str++; 127 } 128 *num_params = count; 129 130 return 0; 131 } 132 133 /** 134 * amdgpu_pm_dev_state_check - Check if device can be accessed. 135 * @adev: Target device. 136 * @runpm: Check runpm status for suspend state checks. 137 * 138 * Checks the state of the @adev for access. Return 0 if the device is 139 * accessible or a negative error code otherwise. 140 */ 141 static int amdgpu_pm_dev_state_check(struct amdgpu_device *adev, bool runpm) 142 { 143 bool runpm_check = runpm ? adev->in_runpm : false; 144 bool full_init = (adev->init_lvl->level == AMDGPU_INIT_LEVEL_DEFAULT); 145 146 if (amdgpu_in_reset(adev) || !full_init) 147 return -EBUSY; 148 149 if (adev->in_suspend && !runpm_check) 150 return -EBUSY; 151 152 return 0; 153 } 154 155 /** 156 * amdgpu_pm_get_access - Check if device can be accessed, resume if needed. 157 * @adev: Target device. 158 * 159 * Checks the state of the @adev for access. Use runtime pm API to resume if 160 * needed. Return 0 if the device is accessible or a negative error code 161 * otherwise. 162 */ 163 static int amdgpu_pm_get_access(struct amdgpu_device *adev) 164 { 165 int ret; 166 167 ret = amdgpu_pm_dev_state_check(adev, true); 168 if (ret) 169 return ret; 170 171 return pm_runtime_resume_and_get(adev->dev); 172 } 173 174 /** 175 * amdgpu_pm_get_access_if_active - Check if device is active for access. 176 * @adev: Target device. 177 * 178 * Checks the state of the @adev for access. Use runtime pm API to determine 179 * if device is active. Allow access only if device is active.Return 0 if the 180 * device is accessible or a negative error code otherwise. 181 */ 182 static int amdgpu_pm_get_access_if_active(struct amdgpu_device *adev) 183 { 184 int ret; 185 186 /* Ignore runpm status. If device is in suspended state, deny access */ 187 ret = amdgpu_pm_dev_state_check(adev, false); 188 if (ret) 189 return ret; 190 191 /* 192 * Allow only if device is active. If runpm is disabled also, as in 193 * kernels without CONFIG_PM, allow access. 194 */ 195 ret = pm_runtime_get_if_active(adev->dev); 196 if (!ret) 197 return -EPERM; 198 199 return 0; 200 } 201 202 /** 203 * amdgpu_pm_put_access - Put to auto suspend mode after a device access. 204 * @adev: Target device. 205 * 206 * Should be paired with amdgpu_pm_get_access* calls 207 */ 208 static inline void amdgpu_pm_put_access(struct amdgpu_device *adev) 209 { 210 pm_runtime_put_autosuspend(adev->dev); 211 } 212 213 /** 214 * DOC: power_dpm_state 215 * 216 * The power_dpm_state file is a legacy interface and is only provided for 217 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting 218 * certain power related parameters. The file power_dpm_state is used for this. 219 * It accepts the following arguments: 220 * 221 * - battery 222 * 223 * - balanced 224 * 225 * - performance 226 * 227 * battery 228 * 229 * On older GPUs, the vbios provided a special power state for battery 230 * operation. Selecting battery switched to this state. This is no 231 * longer provided on newer GPUs so the option does nothing in that case. 232 * 233 * balanced 234 * 235 * On older GPUs, the vbios provided a special power state for balanced 236 * operation. Selecting balanced switched to this state. This is no 237 * longer provided on newer GPUs so the option does nothing in that case. 238 * 239 * performance 240 * 241 * On older GPUs, the vbios provided a special power state for performance 242 * operation. Selecting performance switched to this state. This is no 243 * longer provided on newer GPUs so the option does nothing in that case. 244 * 245 */ 246 247 static ssize_t amdgpu_get_power_dpm_state(struct device *dev, 248 struct device_attribute *attr, 249 char *buf) 250 { 251 struct drm_device *ddev = dev_get_drvdata(dev); 252 struct amdgpu_device *adev = drm_to_adev(ddev); 253 enum amd_pm_state_type pm; 254 int ret; 255 256 ret = amdgpu_pm_get_access_if_active(adev); 257 if (ret) 258 return ret; 259 260 amdgpu_dpm_get_current_power_state(adev, &pm); 261 262 amdgpu_pm_put_access(adev); 263 264 return sysfs_emit(buf, "%s\n", 265 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" : 266 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance"); 267 } 268 269 static ssize_t amdgpu_set_power_dpm_state(struct device *dev, 270 struct device_attribute *attr, 271 const char *buf, 272 size_t count) 273 { 274 struct drm_device *ddev = dev_get_drvdata(dev); 275 struct amdgpu_device *adev = drm_to_adev(ddev); 276 enum amd_pm_state_type state; 277 int ret; 278 279 /* Reject empty/whitespace strings - fuzzing found this is not validated */ 280 if (count == 0 || sysfs_streq(buf, "")) 281 return -EINVAL; 282 283 if (sysfs_streq(buf, "battery")) 284 state = POWER_STATE_TYPE_BATTERY; 285 else if (sysfs_streq(buf, "balanced")) 286 state = POWER_STATE_TYPE_BALANCED; 287 else if (sysfs_streq(buf, "performance")) 288 state = POWER_STATE_TYPE_PERFORMANCE; 289 else 290 return -EINVAL; 291 292 ret = amdgpu_pm_get_access(adev); 293 if (ret < 0) 294 return ret; 295 296 amdgpu_dpm_set_power_state(adev, state); 297 298 amdgpu_pm_put_access(adev); 299 300 return count; 301 } 302 303 304 /** 305 * DOC: power_dpm_force_performance_level 306 * 307 * The amdgpu driver provides a sysfs API for adjusting certain power 308 * related parameters. The file power_dpm_force_performance_level is 309 * used for this. It accepts the following arguments: 310 * 311 * - auto 312 * 313 * - low 314 * 315 * - high 316 * 317 * - manual 318 * 319 * - profile_standard 320 * 321 * - profile_min_sclk 322 * 323 * - profile_min_mclk 324 * 325 * - profile_peak 326 * 327 * auto 328 * 329 * When auto is selected, the driver will attempt to dynamically select 330 * the optimal power profile for current conditions in the driver. 331 * 332 * low 333 * 334 * When low is selected, the clocks are forced to the lowest power state. 335 * 336 * high 337 * 338 * When high is selected, the clocks are forced to the highest power state. 339 * 340 * manual 341 * 342 * When manual is selected, the user can manually adjust which power states 343 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk, 344 * and pp_dpm_pcie files and adjust the power state transition heuristics 345 * via the pp_power_profile_mode sysfs file. 346 * 347 * profile_standard 348 * profile_min_sclk 349 * profile_min_mclk 350 * profile_peak 351 * 352 * When the profiling modes are selected, clock and power gating are 353 * disabled and the clocks are set for different profiling cases. This 354 * mode is recommended for profiling specific work loads where you do 355 * not want clock or power gating for clock fluctuation to interfere 356 * with your results. profile_standard sets the clocks to a fixed clock 357 * level which varies from asic to asic. profile_min_sclk forces the sclk 358 * to the lowest level. profile_min_mclk forces the mclk to the lowest level. 359 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels. 360 * 361 */ 362 363 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev, 364 struct device_attribute *attr, 365 char *buf) 366 { 367 struct drm_device *ddev = dev_get_drvdata(dev); 368 struct amdgpu_device *adev = drm_to_adev(ddev); 369 enum amd_dpm_forced_level level = 0xff; 370 int ret; 371 372 ret = amdgpu_pm_get_access_if_active(adev); 373 if (ret) 374 return ret; 375 376 level = amdgpu_dpm_get_performance_level(adev); 377 378 amdgpu_pm_put_access(adev); 379 380 return sysfs_emit(buf, "%s\n", 381 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" : 382 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" : 383 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" : 384 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" : 385 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" : 386 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" : 387 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" : 388 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" : 389 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" : 390 "unknown"); 391 } 392 393 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev, 394 struct device_attribute *attr, 395 const char *buf, 396 size_t count) 397 { 398 struct drm_device *ddev = dev_get_drvdata(dev); 399 struct amdgpu_device *adev = drm_to_adev(ddev); 400 enum amd_dpm_forced_level level; 401 int ret = 0; 402 403 /* Reject empty/whitespace strings - fuzzing found this is not validated */ 404 if (count == 0 || sysfs_streq(buf, "")) 405 return -EINVAL; 406 407 if (sysfs_streq(buf, "low")) 408 level = AMD_DPM_FORCED_LEVEL_LOW; 409 else if (sysfs_streq(buf, "high")) 410 level = AMD_DPM_FORCED_LEVEL_HIGH; 411 else if (sysfs_streq(buf, "auto")) 412 level = AMD_DPM_FORCED_LEVEL_AUTO; 413 else if (sysfs_streq(buf, "manual")) 414 level = AMD_DPM_FORCED_LEVEL_MANUAL; 415 else if (sysfs_streq(buf, "profile_exit")) 416 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT; 417 else if (sysfs_streq(buf, "profile_standard")) 418 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD; 419 else if (sysfs_streq(buf, "profile_min_sclk")) 420 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK; 421 else if (sysfs_streq(buf, "profile_min_mclk")) 422 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK; 423 else if (sysfs_streq(buf, "profile_peak")) 424 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK; 425 else if (sysfs_streq(buf, "perf_determinism")) 426 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM; 427 else 428 return -EINVAL; 429 430 ret = amdgpu_pm_get_access(adev); 431 if (ret < 0) 432 return ret; 433 434 mutex_lock(&adev->pm.stable_pstate_ctx_lock); 435 if (amdgpu_dpm_force_performance_level(adev, level)) { 436 amdgpu_pm_put_access(adev); 437 mutex_unlock(&adev->pm.stable_pstate_ctx_lock); 438 return -EINVAL; 439 } 440 /* override whatever a user ctx may have set */ 441 adev->pm.stable_pstate_ctx = NULL; 442 mutex_unlock(&adev->pm.stable_pstate_ctx_lock); 443 444 amdgpu_pm_put_access(adev); 445 446 return count; 447 } 448 449 static ssize_t amdgpu_get_pp_num_states(struct device *dev, 450 struct device_attribute *attr, 451 char *buf) 452 { 453 struct drm_device *ddev = dev_get_drvdata(dev); 454 struct amdgpu_device *adev = drm_to_adev(ddev); 455 struct pp_states_info data; 456 uint32_t i; 457 int buf_len, ret; 458 459 ret = amdgpu_pm_get_access_if_active(adev); 460 if (ret) 461 return ret; 462 463 if (amdgpu_dpm_get_pp_num_states(adev, &data)) 464 memset(&data, 0, sizeof(data)); 465 466 amdgpu_pm_put_access(adev); 467 468 buf_len = sysfs_emit(buf, "states: %d\n", data.nums); 469 for (i = 0; i < data.nums; i++) 470 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i, 471 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" : 472 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" : 473 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" : 474 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default"); 475 476 return buf_len; 477 } 478 479 static ssize_t amdgpu_get_pp_cur_state(struct device *dev, 480 struct device_attribute *attr, 481 char *buf) 482 { 483 struct drm_device *ddev = dev_get_drvdata(dev); 484 struct amdgpu_device *adev = drm_to_adev(ddev); 485 struct pp_states_info data = {0}; 486 enum amd_pm_state_type pm = 0; 487 int i = 0, ret = 0; 488 489 ret = amdgpu_pm_get_access_if_active(adev); 490 if (ret) 491 return ret; 492 493 amdgpu_dpm_get_current_power_state(adev, &pm); 494 495 ret = amdgpu_dpm_get_pp_num_states(adev, &data); 496 497 amdgpu_pm_put_access(adev); 498 499 if (ret) 500 return ret; 501 502 for (i = 0; i < data.nums; i++) { 503 if (pm == data.states[i]) 504 break; 505 } 506 507 if (i == data.nums) 508 i = -EINVAL; 509 510 return sysfs_emit(buf, "%d\n", i); 511 } 512 513 static ssize_t amdgpu_get_pp_force_state(struct device *dev, 514 struct device_attribute *attr, 515 char *buf) 516 { 517 struct drm_device *ddev = dev_get_drvdata(dev); 518 struct amdgpu_device *adev = drm_to_adev(ddev); 519 520 if (adev->pm.pp_force_state_enabled) 521 return amdgpu_get_pp_cur_state(dev, attr, buf); 522 else 523 return sysfs_emit(buf, "\n"); 524 } 525 526 static ssize_t amdgpu_set_pp_force_state(struct device *dev, 527 struct device_attribute *attr, 528 const char *buf, 529 size_t count) 530 { 531 struct drm_device *ddev = dev_get_drvdata(dev); 532 struct amdgpu_device *adev = drm_to_adev(ddev); 533 enum amd_pm_state_type state = 0; 534 struct pp_states_info data; 535 unsigned long idx; 536 int ret; 537 538 adev->pm.pp_force_state_enabled = false; 539 540 if (strlen(buf) == 1) 541 return count; 542 543 ret = kstrtoul(buf, 0, &idx); 544 if (ret || idx >= ARRAY_SIZE(data.states)) 545 return -EINVAL; 546 547 idx = array_index_nospec(idx, ARRAY_SIZE(data.states)); 548 549 ret = amdgpu_pm_get_access(adev); 550 if (ret < 0) 551 return ret; 552 553 ret = amdgpu_dpm_get_pp_num_states(adev, &data); 554 if (ret) 555 goto err_out; 556 557 state = data.states[idx]; 558 559 /* only set user selected power states */ 560 if (state != POWER_STATE_TYPE_INTERNAL_BOOT && 561 state != POWER_STATE_TYPE_DEFAULT) { 562 ret = amdgpu_dpm_dispatch_task(adev, 563 AMD_PP_TASK_ENABLE_USER_STATE, &state); 564 if (ret) 565 goto err_out; 566 567 adev->pm.pp_force_state_enabled = true; 568 } 569 570 amdgpu_pm_put_access(adev); 571 572 return count; 573 574 err_out: 575 amdgpu_pm_put_access(adev); 576 577 return ret; 578 } 579 580 /** 581 * DOC: pp_table 582 * 583 * The amdgpu driver provides a sysfs API for uploading new powerplay 584 * tables. The file pp_table is used for this. Reading the file 585 * will dump the current power play table. Writing to the file 586 * will attempt to upload a new powerplay table and re-initialize 587 * powerplay using that new table. 588 * 589 */ 590 591 static ssize_t amdgpu_get_pp_table(struct device *dev, 592 struct device_attribute *attr, 593 char *buf) 594 { 595 struct drm_device *ddev = dev_get_drvdata(dev); 596 struct amdgpu_device *adev = drm_to_adev(ddev); 597 int size, ret; 598 599 ret = amdgpu_pm_get_access_if_active(adev); 600 if (ret) 601 return ret; 602 603 size = amdgpu_dpm_get_pp_table(adev, buf, PAGE_SIZE - 1); 604 605 amdgpu_pm_put_access(adev); 606 607 if (size <= 0) 608 return size; 609 610 return size; 611 } 612 613 static ssize_t amdgpu_set_pp_table(struct device *dev, 614 struct device_attribute *attr, 615 const char *buf, 616 size_t count) 617 { 618 struct drm_device *ddev = dev_get_drvdata(dev); 619 struct amdgpu_device *adev = drm_to_adev(ddev); 620 int ret = 0; 621 622 ret = amdgpu_pm_get_access(adev); 623 if (ret < 0) 624 return ret; 625 626 ret = amdgpu_dpm_set_pp_table(adev, buf, count); 627 628 amdgpu_pm_put_access(adev); 629 630 if (ret) 631 return ret; 632 633 return count; 634 } 635 636 /** 637 * DOC: pp_od_clk_voltage 638 * 639 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages 640 * in each power level within a power state. The pp_od_clk_voltage is used for 641 * this. 642 * 643 * Note that the actual memory controller clock rate are exposed, not 644 * the effective memory clock of the DRAMs. To translate it, use the 645 * following formula: 646 * 647 * Clock conversion (Mhz): 648 * 649 * HBM: effective_memory_clock = memory_controller_clock * 1 650 * 651 * G5: effective_memory_clock = memory_controller_clock * 1 652 * 653 * G6: effective_memory_clock = memory_controller_clock * 2 654 * 655 * DRAM data rate (MT/s): 656 * 657 * HBM: effective_memory_clock * 2 = data_rate 658 * 659 * G5: effective_memory_clock * 4 = data_rate 660 * 661 * G6: effective_memory_clock * 8 = data_rate 662 * 663 * Bandwidth (MB/s): 664 * 665 * data_rate * vram_bit_width / 8 = memory_bandwidth 666 * 667 * Some examples: 668 * 669 * G5 on RX460: 670 * 671 * memory_controller_clock = 1750 Mhz 672 * 673 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz 674 * 675 * data rate = 1750 * 4 = 7000 MT/s 676 * 677 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s 678 * 679 * G6 on RX5700: 680 * 681 * memory_controller_clock = 875 Mhz 682 * 683 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz 684 * 685 * data rate = 1750 * 8 = 14000 MT/s 686 * 687 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s 688 * 689 * < For Vega10 and previous ASICs > 690 * 691 * Reading the file will display: 692 * 693 * - a list of engine clock levels and voltages labeled OD_SCLK 694 * 695 * - a list of memory clock levels and voltages labeled OD_MCLK 696 * 697 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE 698 * 699 * To manually adjust these settings, first select manual using 700 * power_dpm_force_performance_level. Enter a new value for each 701 * level by writing a string that contains "s/m level clock voltage" to 702 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz 703 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at 704 * 810 mV. When you have edited all of the states as needed, write 705 * "c" (commit) to the file to commit your changes. If you want to reset to the 706 * default power levels, write "r" (reset) to the file to reset them. 707 * 708 * 709 * < For Vega20 and newer ASICs > 710 * 711 * Reading the file will display: 712 * 713 * - minimum and maximum engine clock labeled OD_SCLK 714 * 715 * - minimum(not available for Vega20 and Navi1x) and maximum memory 716 * clock labeled OD_MCLK 717 * 718 * - minimum and maximum fabric clock labeled OD_FCLK (SMU13) 719 * 720 * - three <frequency, voltage> points labeled OD_VDDC_CURVE. 721 * They can be used to calibrate the sclk voltage curve. This is 722 * available for Vega20 and NV1X. 723 * 724 * - voltage offset(in mV) applied on target voltage calculation. 725 * This is available for Sienna Cichlid, Navy Flounder, Dimgrey 726 * Cavefish and some later SMU13 ASICs. For these ASICs, the target 727 * voltage calculation can be illustrated by "voltage = voltage 728 * calculated from v/f curve + overdrive vddgfx offset" 729 * 730 * - a list of valid ranges for sclk, mclk, voltage curve points 731 * or voltage offset labeled OD_RANGE 732 * 733 * < For APUs > 734 * 735 * Reading the file will display: 736 * 737 * - minimum and maximum engine clock labeled OD_SCLK 738 * 739 * - a list of valid ranges for sclk labeled OD_RANGE 740 * 741 * < For VanGogh > 742 * 743 * Reading the file will display: 744 * 745 * - minimum and maximum engine clock labeled OD_SCLK 746 * - minimum and maximum core clocks labeled OD_CCLK 747 * 748 * - a list of valid ranges for sclk and cclk labeled OD_RANGE 749 * 750 * To manually adjust these settings: 751 * 752 * - First select manual using power_dpm_force_performance_level 753 * 754 * - For clock frequency setting, enter a new value by writing a 755 * string that contains "s/m/f index clock" to the file. The index 756 * should be 0 if to set minimum clock. And 1 if to set maximum 757 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz. 758 * "m 1 800" will update maximum mclk to be 800Mhz. "f 1 1600" will 759 * update maximum fabric clock to be 1600Mhz. For core 760 * clocks on VanGogh, the string contains "p core index clock". 761 * E.g., "p 2 0 800" would set the minimum core clock on core 762 * 2 to 800Mhz. 763 * 764 * For sclk voltage curve supported by Vega20 and NV1X, enter the new 765 * values by writing a string that contains "vc point clock voltage" 766 * to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300 767 * 600" will update point1 with clock set as 300Mhz and voltage as 600mV. 768 * "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and 769 * voltage 1000mV. 770 * 771 * For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey 772 * Cavefish and some later SMU13 ASICs, enter the new value by writing a 773 * string that contains "vo offset". E.g., "vo -10" will update the extra 774 * voltage offset applied to the whole v/f curve line as -10mv. 775 * 776 * - When you have edited all of the states as needed, write "c" (commit) 777 * to the file to commit your changes 778 * 779 * - If you want to reset to the default power levels, write "r" (reset) 780 * to the file to reset them 781 * 782 */ 783 784 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev, 785 struct device_attribute *attr, 786 const char *buf, 787 size_t count) 788 { 789 struct drm_device *ddev = dev_get_drvdata(dev); 790 struct amdgpu_device *adev = drm_to_adev(ddev); 791 int ret; 792 uint32_t parameter_size = 0; 793 long parameter[64]; 794 char buf_cpy[128]; 795 char *tmp_str; 796 uint32_t type; 797 798 if (count > 127 || count == 0) 799 return -EINVAL; 800 801 if (*buf == 's') 802 type = PP_OD_EDIT_SCLK_VDDC_TABLE; 803 else if (*buf == 'p') 804 type = PP_OD_EDIT_CCLK_VDDC_TABLE; 805 else if (*buf == 'm') 806 type = PP_OD_EDIT_MCLK_VDDC_TABLE; 807 else if (*buf == 'f') 808 type = PP_OD_EDIT_FCLK_TABLE; 809 else if (*buf == 'r') 810 type = PP_OD_RESTORE_DEFAULT_TABLE; 811 else if (*buf == 'c') 812 type = PP_OD_COMMIT_DPM_TABLE; 813 else if (!strncmp(buf, "vc", 2)) 814 type = PP_OD_EDIT_VDDC_CURVE; 815 else if (!strncmp(buf, "vo", 2)) 816 type = PP_OD_EDIT_VDDGFX_OFFSET; 817 else 818 return -EINVAL; 819 820 memcpy(buf_cpy, buf, count); 821 buf_cpy[count] = 0; 822 823 tmp_str = buf_cpy; 824 825 if ((type == PP_OD_EDIT_VDDC_CURVE) || 826 (type == PP_OD_EDIT_VDDGFX_OFFSET)) 827 tmp_str++; 828 while (isspace(*++tmp_str)); 829 830 ret = amdgpu_pm_parse_long_params( 831 tmp_str, parameter, ARRAY_SIZE(parameter), ¶meter_size); 832 if (ret) 833 return ret; 834 835 ret = amdgpu_pm_get_access(adev); 836 if (ret < 0) 837 return ret; 838 839 if (amdgpu_dpm_set_fine_grain_clk_vol(adev, 840 type, 841 parameter, 842 parameter_size)) 843 goto err_out; 844 845 if (amdgpu_dpm_odn_edit_dpm_table(adev, type, 846 parameter, parameter_size)) 847 goto err_out; 848 849 if (type == PP_OD_COMMIT_DPM_TABLE) { 850 if (amdgpu_dpm_dispatch_task(adev, 851 AMD_PP_TASK_READJUST_POWER_STATE, 852 NULL)) 853 goto err_out; 854 } 855 856 amdgpu_pm_put_access(adev); 857 858 return count; 859 860 err_out: 861 amdgpu_pm_put_access(adev); 862 863 return -EINVAL; 864 } 865 866 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev, 867 struct device_attribute *attr, 868 char *buf) 869 { 870 struct drm_device *ddev = dev_get_drvdata(dev); 871 struct amdgpu_device *adev = drm_to_adev(ddev); 872 int size = 0; 873 int ret; 874 enum pp_clock_type od_clocks[] = { 875 OD_SCLK, 876 OD_MCLK, 877 OD_FCLK, 878 OD_VDDC_CURVE, 879 OD_RANGE, 880 OD_VDDGFX_OFFSET, 881 OD_CCLK, 882 }; 883 uint clk_index; 884 885 ret = amdgpu_pm_get_access_if_active(adev); 886 if (ret) 887 return ret; 888 889 for (clk_index = 0 ; clk_index < ARRAY_SIZE(od_clocks) ; clk_index++) { 890 amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size); 891 if (unlikely(size >= (PAGE_SIZE - 1))) 892 break; 893 } 894 895 if (size == 0) 896 size = sysfs_emit(buf, "\n"); 897 898 amdgpu_pm_put_access(adev); 899 900 return size; 901 } 902 903 /** 904 * DOC: pp_features 905 * 906 * The amdgpu driver provides a sysfs API for adjusting what powerplay 907 * features to be enabled. The file pp_features is used for this. And 908 * this is only available for Vega10 and later dGPUs. 909 * 910 * Reading back the file will show you the followings: 911 * - Current ppfeature masks 912 * - List of the all supported powerplay features with their naming, 913 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled"). 914 * 915 * To manually enable or disable a specific feature, just set or clear 916 * the corresponding bit from original ppfeature masks and input the 917 * new ppfeature masks. 918 */ 919 static ssize_t amdgpu_set_pp_features(struct device *dev, 920 struct device_attribute *attr, 921 const char *buf, 922 size_t count) 923 { 924 struct drm_device *ddev = dev_get_drvdata(dev); 925 struct amdgpu_device *adev = drm_to_adev(ddev); 926 uint64_t featuremask; 927 int ret; 928 929 /* Reject empty/whitespace strings - fuzzing found kstrtou64 accepts "" as 0 */ 930 if (count == 0 || sysfs_streq(buf, "")) 931 return -EINVAL; 932 933 ret = kstrtou64(buf, 0, &featuremask); 934 if (ret) 935 return -EINVAL; 936 937 ret = amdgpu_pm_get_access(adev); 938 if (ret < 0) 939 return ret; 940 941 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask); 942 943 amdgpu_pm_put_access(adev); 944 945 if (ret) 946 return -EINVAL; 947 948 return count; 949 } 950 951 static ssize_t amdgpu_get_pp_features(struct device *dev, 952 struct device_attribute *attr, 953 char *buf) 954 { 955 struct drm_device *ddev = dev_get_drvdata(dev); 956 struct amdgpu_device *adev = drm_to_adev(ddev); 957 ssize_t size; 958 int ret; 959 960 ret = amdgpu_pm_get_access_if_active(adev); 961 if (ret) 962 return ret; 963 964 size = amdgpu_dpm_get_ppfeature_status(adev, buf); 965 if (size <= 0) 966 size = sysfs_emit(buf, "\n"); 967 968 amdgpu_pm_put_access(adev); 969 970 return size; 971 } 972 973 /** 974 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie 975 * 976 * The amdgpu driver provides a sysfs API for adjusting what power levels 977 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk, 978 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for 979 * this. 980 * 981 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for 982 * Vega10 and later ASICs. 983 * pp_dpm_fclk interface is only available for Vega20 and later ASICs. 984 * 985 * Reading back the files will show you the available power levels within 986 * the power state and the clock information for those levels. If deep sleep is 987 * applied to a clock, the level will be denoted by a special level 'S:' 988 * E.g., :: 989 * 990 * S: 19Mhz * 991 * 0: 615Mhz 992 * 1: 800Mhz 993 * 2: 888Mhz 994 * 3: 1000Mhz 995 * 996 * 997 * To manually adjust these states, first select manual using 998 * power_dpm_force_performance_level. 999 * Secondly, enter a new value for each level by inputing a string that 1000 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie" 1001 * E.g., 1002 * 1003 * .. code-block:: bash 1004 * 1005 * echo "4 5 6" > pp_dpm_sclk 1006 * 1007 * will enable sclk levels 4, 5, and 6. 1008 * 1009 * NOTE: change to the dcefclk max dpm level is not supported now 1010 */ 1011 1012 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev, 1013 enum pp_clock_type type, 1014 char *buf) 1015 { 1016 struct drm_device *ddev = dev_get_drvdata(dev); 1017 struct amdgpu_device *adev = drm_to_adev(ddev); 1018 int size = 0; 1019 int ret = 0; 1020 1021 ret = amdgpu_pm_get_access_if_active(adev); 1022 if (ret) 1023 return ret; 1024 1025 ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size); 1026 if (ret) { 1027 size = ret; 1028 goto out_pm_put; 1029 } 1030 1031 if (size == 0) 1032 size = sysfs_emit(buf, "\n"); 1033 1034 out_pm_put: 1035 amdgpu_pm_put_access(adev); 1036 1037 return size; 1038 } 1039 1040 /* 1041 * Worst case: 32 bits individually specified, in octal at 12 characters 1042 * per line (+1 for \n). 1043 */ 1044 #define AMDGPU_MASK_BUF_MAX (32 * 13) 1045 1046 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask) 1047 { 1048 int ret; 1049 unsigned long level; 1050 char *sub_str = NULL; 1051 char *tmp; 1052 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1]; 1053 const char delimiter[3] = {' ', '\n', '\0'}; 1054 size_t bytes; 1055 1056 *mask = 0; 1057 1058 /* Reject empty/whitespace strings - fuzzing found this is not validated */ 1059 if (count == 0 || sysfs_streq(buf, "")) 1060 return -EINVAL; 1061 1062 bytes = min(count, sizeof(buf_cpy) - 1); 1063 memcpy(buf_cpy, buf, bytes); 1064 buf_cpy[bytes] = '\0'; 1065 tmp = buf_cpy; 1066 while ((sub_str = strsep(&tmp, delimiter)) != NULL) { 1067 if (strlen(sub_str)) { 1068 ret = kstrtoul(sub_str, 0, &level); 1069 if (ret || level > 31) 1070 return -EINVAL; 1071 *mask |= 1 << level; 1072 } else 1073 break; 1074 } 1075 1076 return 0; 1077 } 1078 1079 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev, 1080 enum pp_clock_type type, 1081 const char *buf, 1082 size_t count) 1083 { 1084 struct drm_device *ddev = dev_get_drvdata(dev); 1085 struct amdgpu_device *adev = drm_to_adev(ddev); 1086 int ret; 1087 uint32_t mask = 0; 1088 1089 ret = amdgpu_read_mask(buf, count, &mask); 1090 if (ret) 1091 return ret; 1092 1093 ret = amdgpu_pm_get_access(adev); 1094 if (ret < 0) 1095 return ret; 1096 1097 ret = amdgpu_dpm_force_clock_level(adev, type, mask); 1098 1099 amdgpu_pm_put_access(adev); 1100 1101 if (ret) 1102 return -EINVAL; 1103 1104 return count; 1105 } 1106 1107 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev, 1108 struct device_attribute *attr, 1109 char *buf) 1110 { 1111 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf); 1112 } 1113 1114 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev, 1115 struct device_attribute *attr, 1116 const char *buf, 1117 size_t count) 1118 { 1119 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count); 1120 } 1121 1122 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev, 1123 struct device_attribute *attr, 1124 char *buf) 1125 { 1126 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf); 1127 } 1128 1129 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev, 1130 struct device_attribute *attr, 1131 const char *buf, 1132 size_t count) 1133 { 1134 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count); 1135 } 1136 1137 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev, 1138 struct device_attribute *attr, 1139 char *buf) 1140 { 1141 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf); 1142 } 1143 1144 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev, 1145 struct device_attribute *attr, 1146 const char *buf, 1147 size_t count) 1148 { 1149 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count); 1150 } 1151 1152 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev, 1153 struct device_attribute *attr, 1154 char *buf) 1155 { 1156 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf); 1157 } 1158 1159 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev, 1160 struct device_attribute *attr, 1161 const char *buf, 1162 size_t count) 1163 { 1164 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count); 1165 } 1166 1167 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev, 1168 struct device_attribute *attr, 1169 char *buf) 1170 { 1171 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf); 1172 } 1173 1174 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev, 1175 struct device_attribute *attr, 1176 const char *buf, 1177 size_t count) 1178 { 1179 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count); 1180 } 1181 1182 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev, 1183 struct device_attribute *attr, 1184 char *buf) 1185 { 1186 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf); 1187 } 1188 1189 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev, 1190 struct device_attribute *attr, 1191 const char *buf, 1192 size_t count) 1193 { 1194 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count); 1195 } 1196 1197 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev, 1198 struct device_attribute *attr, 1199 char *buf) 1200 { 1201 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf); 1202 } 1203 1204 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev, 1205 struct device_attribute *attr, 1206 const char *buf, 1207 size_t count) 1208 { 1209 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count); 1210 } 1211 1212 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev, 1213 struct device_attribute *attr, 1214 char *buf) 1215 { 1216 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf); 1217 } 1218 1219 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev, 1220 struct device_attribute *attr, 1221 const char *buf, 1222 size_t count) 1223 { 1224 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count); 1225 } 1226 1227 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev, 1228 struct device_attribute *attr, 1229 char *buf) 1230 { 1231 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf); 1232 } 1233 1234 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev, 1235 struct device_attribute *attr, 1236 const char *buf, 1237 size_t count) 1238 { 1239 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count); 1240 } 1241 1242 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev, 1243 struct device_attribute *attr, 1244 char *buf) 1245 { 1246 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf); 1247 } 1248 1249 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev, 1250 struct device_attribute *attr, 1251 const char *buf, 1252 size_t count) 1253 { 1254 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count); 1255 } 1256 1257 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev, 1258 struct device_attribute *attr, 1259 char *buf) 1260 { 1261 struct drm_device *ddev = dev_get_drvdata(dev); 1262 struct amdgpu_device *adev = drm_to_adev(ddev); 1263 uint32_t value = 0; 1264 int ret; 1265 1266 ret = amdgpu_pm_get_access_if_active(adev); 1267 if (ret) 1268 return ret; 1269 1270 value = amdgpu_dpm_get_sclk_od(adev); 1271 1272 amdgpu_pm_put_access(adev); 1273 1274 return sysfs_emit(buf, "%d\n", value); 1275 } 1276 1277 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev, 1278 struct device_attribute *attr, 1279 const char *buf, 1280 size_t count) 1281 { 1282 struct drm_device *ddev = dev_get_drvdata(dev); 1283 struct amdgpu_device *adev = drm_to_adev(ddev); 1284 int ret; 1285 long int value; 1286 1287 ret = kstrtol(buf, 0, &value); 1288 1289 if (ret) 1290 return -EINVAL; 1291 1292 ret = amdgpu_pm_get_access(adev); 1293 if (ret < 0) 1294 return ret; 1295 1296 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value); 1297 1298 amdgpu_pm_put_access(adev); 1299 1300 return count; 1301 } 1302 1303 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev, 1304 struct device_attribute *attr, 1305 char *buf) 1306 { 1307 struct drm_device *ddev = dev_get_drvdata(dev); 1308 struct amdgpu_device *adev = drm_to_adev(ddev); 1309 uint32_t value = 0; 1310 int ret; 1311 1312 ret = amdgpu_pm_get_access_if_active(adev); 1313 if (ret) 1314 return ret; 1315 1316 value = amdgpu_dpm_get_mclk_od(adev); 1317 1318 amdgpu_pm_put_access(adev); 1319 1320 return sysfs_emit(buf, "%d\n", value); 1321 } 1322 1323 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev, 1324 struct device_attribute *attr, 1325 const char *buf, 1326 size_t count) 1327 { 1328 struct drm_device *ddev = dev_get_drvdata(dev); 1329 struct amdgpu_device *adev = drm_to_adev(ddev); 1330 int ret; 1331 long int value; 1332 1333 ret = kstrtol(buf, 0, &value); 1334 1335 if (ret) 1336 return -EINVAL; 1337 1338 ret = amdgpu_pm_get_access(adev); 1339 if (ret < 0) 1340 return ret; 1341 1342 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value); 1343 1344 amdgpu_pm_put_access(adev); 1345 1346 return count; 1347 } 1348 1349 /** 1350 * DOC: pp_power_profile_mode 1351 * 1352 * The amdgpu driver provides a sysfs API for adjusting the heuristics 1353 * related to switching between power levels in a power state. The file 1354 * pp_power_profile_mode is used for this. 1355 * 1356 * Reading this file outputs a list of all of the predefined power profiles 1357 * and the relevant heuristics settings for that profile. 1358 * 1359 * To select a profile or create a custom profile, first select manual using 1360 * power_dpm_force_performance_level. Writing the number of a predefined 1361 * profile to pp_power_profile_mode will enable those heuristics. To 1362 * create a custom set of heuristics, write a string of numbers to the file 1363 * starting with the number of the custom profile along with a setting 1364 * for each heuristic parameter. Due to differences across asic families 1365 * the heuristic parameters vary from family to family. Additionally, 1366 * you can apply the custom heuristics to different clock domains. Each 1367 * clock domain is considered a distinct operation so if you modify the 1368 * gfxclk heuristics and then the memclk heuristics, the all of the 1369 * custom heuristics will be retained until you switch to another profile. 1370 * 1371 */ 1372 1373 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev, 1374 struct device_attribute *attr, 1375 char *buf) 1376 { 1377 struct drm_device *ddev = dev_get_drvdata(dev); 1378 struct amdgpu_device *adev = drm_to_adev(ddev); 1379 ssize_t size; 1380 int ret; 1381 1382 ret = amdgpu_pm_get_access_if_active(adev); 1383 if (ret) 1384 return ret; 1385 1386 size = amdgpu_dpm_get_power_profile_mode(adev, buf); 1387 if (size <= 0) 1388 size = sysfs_emit(buf, "\n"); 1389 1390 amdgpu_pm_put_access(adev); 1391 1392 return size; 1393 } 1394 1395 1396 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev, 1397 struct device_attribute *attr, 1398 const char *buf, 1399 size_t count) 1400 { 1401 int ret; 1402 struct drm_device *ddev = dev_get_drvdata(dev); 1403 struct amdgpu_device *adev = drm_to_adev(ddev); 1404 uint32_t parameter_size = 0; 1405 long parameter[64]; 1406 char buf_cpy[128]; 1407 char tmp[2]; 1408 long int profile_mode = 0; 1409 1410 /* Reject empty/whitespace strings - fuzzing found this is not validated */ 1411 if (count == 0 || sysfs_streq(buf, "")) 1412 return -EINVAL; 1413 1414 tmp[0] = *(buf++); 1415 tmp[1] = '\0'; 1416 ret = kstrtol(tmp, 0, &profile_mode); 1417 if (ret) 1418 return -EINVAL; 1419 1420 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) { 1421 if (count < 2 || count > sizeof(buf_cpy)) 1422 return -EINVAL; 1423 while (isspace(*buf)) 1424 buf++; 1425 strscpy(buf_cpy, buf, sizeof(buf_cpy)); 1426 ret = amdgpu_pm_parse_long_params(buf_cpy, parameter, 1427 ARRAY_SIZE(parameter) - 1, 1428 ¶meter_size); 1429 if (ret) 1430 return ret; 1431 } 1432 parameter[parameter_size] = profile_mode; 1433 1434 ret = amdgpu_pm_get_access(adev); 1435 if (ret < 0) 1436 return ret; 1437 1438 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size); 1439 1440 amdgpu_pm_put_access(adev); 1441 1442 if (!ret) 1443 return count; 1444 1445 return -EINVAL; 1446 } 1447 1448 static int amdgpu_pm_get_sensor_generic(struct amdgpu_device *adev, 1449 enum amd_pp_sensors sensor, 1450 void *query) 1451 { 1452 int r, size = sizeof(uint32_t); 1453 1454 r = amdgpu_pm_get_access_if_active(adev); 1455 if (r) 1456 return r; 1457 1458 /* get the sensor value */ 1459 r = amdgpu_dpm_read_sensor(adev, sensor, query, &size); 1460 1461 amdgpu_pm_put_access(adev); 1462 1463 return r; 1464 } 1465 1466 /** 1467 * DOC: gpu_busy_percent 1468 * 1469 * The amdgpu driver provides a sysfs API for reading how busy the GPU 1470 * is as a percentage. The file gpu_busy_percent is used for this. 1471 * The SMU firmware computes a percentage of load based on the 1472 * aggregate activity level in the IP cores. 1473 */ 1474 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev, 1475 struct device_attribute *attr, 1476 char *buf) 1477 { 1478 struct drm_device *ddev = dev_get_drvdata(dev); 1479 struct amdgpu_device *adev = drm_to_adev(ddev); 1480 unsigned int value; 1481 int r; 1482 1483 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value); 1484 if (r) 1485 return r; 1486 1487 return sysfs_emit(buf, "%d\n", value); 1488 } 1489 1490 /** 1491 * DOC: mem_busy_percent 1492 * 1493 * The amdgpu driver provides a sysfs API for reading how busy the VRAM 1494 * is as a percentage. The file mem_busy_percent is used for this. 1495 * The SMU firmware computes a percentage of load based on the 1496 * aggregate activity level in the IP cores. 1497 */ 1498 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev, 1499 struct device_attribute *attr, 1500 char *buf) 1501 { 1502 struct drm_device *ddev = dev_get_drvdata(dev); 1503 struct amdgpu_device *adev = drm_to_adev(ddev); 1504 unsigned int value; 1505 int r; 1506 1507 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value); 1508 if (r) 1509 return r; 1510 1511 return sysfs_emit(buf, "%d\n", value); 1512 } 1513 1514 /** 1515 * DOC: vcn_busy_percent 1516 * 1517 * The amdgpu driver provides a sysfs API for reading how busy the VCN 1518 * is as a percentage. The file vcn_busy_percent is used for this. 1519 * The SMU firmware computes a percentage of load based on the 1520 * aggregate activity level in the IP cores. 1521 */ 1522 static ssize_t amdgpu_get_vcn_busy_percent(struct device *dev, 1523 struct device_attribute *attr, 1524 char *buf) 1525 { 1526 struct drm_device *ddev = dev_get_drvdata(dev); 1527 struct amdgpu_device *adev = drm_to_adev(ddev); 1528 unsigned int value; 1529 int r; 1530 1531 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VCN_LOAD, &value); 1532 if (r) 1533 return r; 1534 1535 return sysfs_emit(buf, "%d\n", value); 1536 } 1537 1538 /** 1539 * DOC: pcie_bw 1540 * 1541 * The amdgpu driver provides a sysfs API for estimating how much data 1542 * has been received and sent by the GPU in the last second through PCIe. 1543 * The file pcie_bw is used for this. 1544 * The Perf counters count the number of received and sent messages and return 1545 * those values, as well as the maximum payload size of a PCIe packet (mps). 1546 * Note that it is not possible to easily and quickly obtain the size of each 1547 * packet transmitted, so we output the max payload size (mps) to allow for 1548 * quick estimation of the PCIe bandwidth usage 1549 */ 1550 static ssize_t amdgpu_get_pcie_bw(struct device *dev, 1551 struct device_attribute *attr, 1552 char *buf) 1553 { 1554 struct drm_device *ddev = dev_get_drvdata(dev); 1555 struct amdgpu_device *adev = drm_to_adev(ddev); 1556 uint64_t count0 = 0, count1 = 0; 1557 int ret; 1558 1559 if (adev->flags & AMD_IS_APU) 1560 return -ENODATA; 1561 1562 if (!adev->asic_funcs->get_pcie_usage) 1563 return -ENODATA; 1564 1565 ret = amdgpu_pm_get_access_if_active(adev); 1566 if (ret) 1567 return ret; 1568 1569 amdgpu_asic_get_pcie_usage(adev, &count0, &count1); 1570 1571 amdgpu_pm_put_access(adev); 1572 1573 return sysfs_emit(buf, "%llu %llu %i\n", 1574 count0, count1, pcie_get_mps(adev->pdev)); 1575 } 1576 1577 /** 1578 * DOC: unique_id 1579 * 1580 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU 1581 * The file unique_id is used for this. 1582 * This will provide a Unique ID that will persist from machine to machine 1583 * 1584 * NOTE: This will only work for GFX9 and newer. This file will be absent 1585 * on unsupported ASICs (GFX8 and older) 1586 */ 1587 static ssize_t amdgpu_get_unique_id(struct device *dev, 1588 struct device_attribute *attr, 1589 char *buf) 1590 { 1591 struct drm_device *ddev = dev_get_drvdata(dev); 1592 struct amdgpu_device *adev = drm_to_adev(ddev); 1593 1594 if (adev->unique_id) 1595 return sysfs_emit(buf, "%016llx\n", adev->unique_id); 1596 1597 return 0; 1598 } 1599 1600 /** 1601 * DOC: thermal_throttling_logging 1602 * 1603 * Thermal throttling pulls down the clock frequency and thus the performance. 1604 * It's an useful mechanism to protect the chip from overheating. Since it 1605 * impacts performance, the user controls whether it is enabled and if so, 1606 * the log frequency. 1607 * 1608 * Reading back the file shows you the status(enabled or disabled) and 1609 * the interval(in seconds) between each thermal logging. 1610 * 1611 * Writing an integer to the file, sets a new logging interval, in seconds. 1612 * The value should be between 1 and 3600. If the value is less than 1, 1613 * thermal logging is disabled. Values greater than 3600 are ignored. 1614 */ 1615 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev, 1616 struct device_attribute *attr, 1617 char *buf) 1618 { 1619 struct drm_device *ddev = dev_get_drvdata(dev); 1620 struct amdgpu_device *adev = drm_to_adev(ddev); 1621 1622 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n", 1623 adev_to_drm(adev)->unique, 1624 str_enabled_disabled(atomic_read(&adev->throttling_logging_enabled)), 1625 adev->throttling_logging_rs.interval / HZ + 1); 1626 } 1627 1628 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev, 1629 struct device_attribute *attr, 1630 const char *buf, 1631 size_t count) 1632 { 1633 struct drm_device *ddev = dev_get_drvdata(dev); 1634 struct amdgpu_device *adev = drm_to_adev(ddev); 1635 long throttling_logging_interval; 1636 int ret = 0; 1637 1638 ret = kstrtol(buf, 0, &throttling_logging_interval); 1639 if (ret) 1640 return ret; 1641 1642 /* Reject negative values - only 0 (disable) or 1-3600 (seconds) are valid */ 1643 if (throttling_logging_interval < 0) 1644 return -EINVAL; 1645 1646 if (throttling_logging_interval > 3600) 1647 return -EINVAL; 1648 1649 if (throttling_logging_interval > 0) { 1650 /* 1651 * Reset the ratelimit timer internals. 1652 * This can effectively restart the timer. 1653 */ 1654 ratelimit_state_reset_interval(&adev->throttling_logging_rs, 1655 (throttling_logging_interval - 1) * HZ); 1656 atomic_set(&adev->throttling_logging_enabled, 1); 1657 } else { 1658 atomic_set(&adev->throttling_logging_enabled, 0); 1659 } 1660 1661 return count; 1662 } 1663 1664 /** 1665 * DOC: apu_thermal_cap 1666 * 1667 * The amdgpu driver provides a sysfs API for retrieving/updating thermal 1668 * limit temperature in millidegrees Celsius 1669 * 1670 * Reading back the file shows you core limit value 1671 * 1672 * Writing an integer to the file, sets a new thermal limit. The value 1673 * should be between 0 and 100. If the value is less than 0 or greater 1674 * than 100, then the write request will be ignored. 1675 */ 1676 static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev, 1677 struct device_attribute *attr, 1678 char *buf) 1679 { 1680 int ret, size; 1681 u32 limit; 1682 struct drm_device *ddev = dev_get_drvdata(dev); 1683 struct amdgpu_device *adev = drm_to_adev(ddev); 1684 1685 ret = amdgpu_pm_get_access_if_active(adev); 1686 if (ret) 1687 return ret; 1688 1689 ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit); 1690 if (!ret) 1691 size = sysfs_emit(buf, "%u\n", limit); 1692 else 1693 size = sysfs_emit(buf, "failed to get thermal limit\n"); 1694 1695 amdgpu_pm_put_access(adev); 1696 1697 return size; 1698 } 1699 1700 static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev, 1701 struct device_attribute *attr, 1702 const char *buf, 1703 size_t count) 1704 { 1705 int ret; 1706 u32 value; 1707 struct drm_device *ddev = dev_get_drvdata(dev); 1708 struct amdgpu_device *adev = drm_to_adev(ddev); 1709 1710 ret = kstrtou32(buf, 10, &value); 1711 if (ret) 1712 return ret; 1713 1714 if (value > 100) { 1715 dev_err(dev, "Invalid argument !\n"); 1716 return -EINVAL; 1717 } 1718 1719 ret = amdgpu_pm_get_access(adev); 1720 if (ret < 0) 1721 return ret; 1722 1723 ret = amdgpu_dpm_set_apu_thermal_limit(adev, value); 1724 if (ret) { 1725 amdgpu_pm_put_access(adev); 1726 dev_err(dev, "failed to update thermal limit\n"); 1727 return ret; 1728 } 1729 1730 amdgpu_pm_put_access(adev); 1731 1732 return count; 1733 } 1734 1735 static int amdgpu_pm_metrics_attr_update(struct amdgpu_device *adev, 1736 struct amdgpu_device_attr *attr, 1737 uint32_t mask, 1738 enum amdgpu_device_attr_states *states) 1739 { 1740 if (amdgpu_dpm_get_pm_metrics(adev, NULL, 0) == -EOPNOTSUPP) 1741 *states = ATTR_STATE_UNSUPPORTED; 1742 1743 return 0; 1744 } 1745 1746 static ssize_t amdgpu_get_pm_metrics(struct device *dev, 1747 struct device_attribute *attr, char *buf) 1748 { 1749 struct drm_device *ddev = dev_get_drvdata(dev); 1750 struct amdgpu_device *adev = drm_to_adev(ddev); 1751 ssize_t size = 0; 1752 int ret; 1753 1754 ret = amdgpu_pm_get_access_if_active(adev); 1755 if (ret) 1756 return ret; 1757 1758 size = amdgpu_dpm_get_pm_metrics(adev, buf, PAGE_SIZE); 1759 1760 amdgpu_pm_put_access(adev); 1761 1762 return size; 1763 } 1764 1765 /** 1766 * DOC: gpu_metrics 1767 * 1768 * The amdgpu driver provides a sysfs API for retrieving current gpu 1769 * metrics data. The file gpu_metrics is used for this. Reading the 1770 * file will dump all the current gpu metrics data. 1771 * 1772 * These data include temperature, frequency, engines utilization, 1773 * power consume, throttler status, fan speed and cpu core statistics( 1774 * available for APU only). That's it will give a snapshot of all sensors 1775 * at the same time. 1776 */ 1777 static ssize_t amdgpu_get_gpu_metrics(struct device *dev, 1778 struct device_attribute *attr, 1779 char *buf) 1780 { 1781 struct drm_device *ddev = dev_get_drvdata(dev); 1782 struct amdgpu_device *adev = drm_to_adev(ddev); 1783 ssize_t size = 0; 1784 int ret; 1785 1786 ret = amdgpu_pm_get_access_if_active(adev); 1787 if (ret) 1788 return ret; 1789 1790 size = amdgpu_dpm_get_gpu_metrics(adev, buf, PAGE_SIZE - 1); 1791 if (size <= 0) 1792 goto out; 1793 1794 out: 1795 amdgpu_pm_put_access(adev); 1796 1797 return size; 1798 } 1799 1800 static int amdgpu_show_powershift_percent(struct device *dev, 1801 char *buf, enum amd_pp_sensors sensor) 1802 { 1803 struct drm_device *ddev = dev_get_drvdata(dev); 1804 struct amdgpu_device *adev = drm_to_adev(ddev); 1805 uint32_t ss_power; 1806 int r = 0, i; 1807 1808 r = amdgpu_pm_get_sensor_generic(adev, sensor, (void *)&ss_power); 1809 if (r == -EOPNOTSUPP) { 1810 /* sensor not available on dGPU, try to read from APU */ 1811 adev = NULL; 1812 mutex_lock(&mgpu_info.mutex); 1813 for (i = 0; i < mgpu_info.num_gpu; i++) { 1814 if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) { 1815 adev = mgpu_info.gpu_ins[i].adev; 1816 break; 1817 } 1818 } 1819 mutex_unlock(&mgpu_info.mutex); 1820 if (adev) 1821 r = amdgpu_pm_get_sensor_generic(adev, sensor, (void *)&ss_power); 1822 } 1823 1824 if (r) 1825 return r; 1826 1827 return sysfs_emit(buf, "%u%%\n", ss_power); 1828 } 1829 1830 /** 1831 * DOC: smartshift_apu_power 1832 * 1833 * The amdgpu driver provides a sysfs API for reporting APU power 1834 * shift in percentage if platform supports smartshift. Value 0 means that 1835 * there is no powershift and values between [1-100] means that the power 1836 * is shifted to APU, the percentage of boost is with respect to APU power 1837 * limit on the platform. 1838 */ 1839 1840 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr, 1841 char *buf) 1842 { 1843 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE); 1844 } 1845 1846 /** 1847 * DOC: smartshift_dgpu_power 1848 * 1849 * The amdgpu driver provides a sysfs API for reporting dGPU power 1850 * shift in percentage if platform supports smartshift. Value 0 means that 1851 * there is no powershift and values between [1-100] means that the power is 1852 * shifted to dGPU, the percentage of boost is with respect to dGPU power 1853 * limit on the platform. 1854 */ 1855 1856 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr, 1857 char *buf) 1858 { 1859 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE); 1860 } 1861 1862 /** 1863 * DOC: smartshift_bias 1864 * 1865 * The amdgpu driver provides a sysfs API for reporting the 1866 * smartshift(SS2.0) bias level. The value ranges from -100 to 100 1867 * and the default is 0. -100 sets maximum preference to APU 1868 * and 100 sets max perference to dGPU. 1869 */ 1870 1871 static ssize_t amdgpu_get_smartshift_bias(struct device *dev, 1872 struct device_attribute *attr, 1873 char *buf) 1874 { 1875 int r = 0; 1876 1877 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias); 1878 1879 return r; 1880 } 1881 1882 static ssize_t amdgpu_set_smartshift_bias(struct device *dev, 1883 struct device_attribute *attr, 1884 const char *buf, size_t count) 1885 { 1886 struct drm_device *ddev = dev_get_drvdata(dev); 1887 struct amdgpu_device *adev = drm_to_adev(ddev); 1888 int r; 1889 int bias = 0; 1890 1891 r = kstrtoint(buf, 10, &bias); 1892 if (r) 1893 return r; 1894 1895 r = amdgpu_pm_get_access(adev); 1896 if (r < 0) 1897 return r; 1898 1899 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS) 1900 bias = AMDGPU_SMARTSHIFT_MAX_BIAS; 1901 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS) 1902 bias = AMDGPU_SMARTSHIFT_MIN_BIAS; 1903 1904 amdgpu_smartshift_bias = bias; 1905 1906 /* TODO: update bias level with SMU message */ 1907 1908 amdgpu_pm_put_access(adev); 1909 1910 return count; 1911 } 1912 1913 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1914 uint32_t mask, enum amdgpu_device_attr_states *states) 1915 { 1916 if (!amdgpu_device_supports_smart_shift(adev)) 1917 *states = ATTR_STATE_UNSUPPORTED; 1918 1919 return 0; 1920 } 1921 1922 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1923 uint32_t mask, enum amdgpu_device_attr_states *states) 1924 { 1925 uint32_t ss_power; 1926 1927 if (!amdgpu_device_supports_smart_shift(adev)) 1928 *states = ATTR_STATE_UNSUPPORTED; 1929 else if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 1930 (void *)&ss_power)) 1931 *states = ATTR_STATE_UNSUPPORTED; 1932 else if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 1933 (void *)&ss_power)) 1934 *states = ATTR_STATE_UNSUPPORTED; 1935 1936 return 0; 1937 } 1938 1939 static int pp_od_clk_voltage_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1940 uint32_t mask, enum amdgpu_device_attr_states *states) 1941 { 1942 *states = ATTR_STATE_SUPPORTED; 1943 1944 if (!amdgpu_dpm_is_overdrive_supported(adev)) { 1945 *states = ATTR_STATE_UNSUPPORTED; 1946 return 0; 1947 } 1948 1949 /* Enable pp_od_clk_voltage node for gc 9.4.3, 9.4.4, 9.5.0, 12.1.0 SRIOV/BM support */ 1950 if (amdgpu_is_multi_aid(adev)) { 1951 if (amdgpu_sriov_multi_vf_mode(adev)) 1952 *states = ATTR_STATE_UNSUPPORTED; 1953 return 0; 1954 } 1955 1956 if (!(attr->flags & mask)) 1957 *states = ATTR_STATE_UNSUPPORTED; 1958 1959 return 0; 1960 } 1961 1962 static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1963 uint32_t mask, enum amdgpu_device_attr_states *states) 1964 { 1965 struct device_attribute *dev_attr = &attr->dev_attr; 1966 uint32_t gc_ver; 1967 1968 *states = ATTR_STATE_SUPPORTED; 1969 1970 if (!(attr->flags & mask)) { 1971 *states = ATTR_STATE_UNSUPPORTED; 1972 return 0; 1973 } 1974 1975 gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 1976 /* dcefclk node is not available on gfx 11.0.3 sriov */ 1977 if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) || 1978 gc_ver < IP_VERSION(9, 0, 0) || 1979 !amdgpu_device_has_display_hardware(adev)) 1980 *states = ATTR_STATE_UNSUPPORTED; 1981 1982 /* SMU MP1 does not support dcefclk level setting, 1983 * setting should not be allowed from VF if not in one VF mode. 1984 */ 1985 if (gc_ver >= IP_VERSION(10, 0, 0) || 1986 (amdgpu_sriov_multi_vf_mode(adev))) { 1987 dev_attr->attr.mode &= ~S_IWUGO; 1988 dev_attr->store = NULL; 1989 } 1990 1991 return 0; 1992 } 1993 1994 static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1995 uint32_t mask, enum amdgpu_device_attr_states *states) 1996 { 1997 struct device_attribute *dev_attr = &attr->dev_attr; 1998 enum amdgpu_device_attr_id attr_id = attr->attr_id; 1999 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0); 2000 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 2001 2002 *states = ATTR_STATE_SUPPORTED; 2003 2004 if (!(attr->flags & mask)) { 2005 *states = ATTR_STATE_UNSUPPORTED; 2006 return 0; 2007 } 2008 2009 if (DEVICE_ATTR_IS(pp_dpm_socclk)) { 2010 if (gc_ver < IP_VERSION(9, 0, 0)) 2011 *states = ATTR_STATE_UNSUPPORTED; 2012 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) { 2013 if (mp1_ver < IP_VERSION(10, 0, 0)) 2014 *states = ATTR_STATE_UNSUPPORTED; 2015 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) { 2016 if (!(gc_ver == IP_VERSION(10, 3, 1) || 2017 gc_ver == IP_VERSION(10, 3, 3) || 2018 gc_ver == IP_VERSION(10, 3, 6) || 2019 gc_ver == IP_VERSION(10, 3, 7) || 2020 gc_ver == IP_VERSION(10, 3, 0) || 2021 gc_ver == IP_VERSION(10, 1, 2) || 2022 gc_ver == IP_VERSION(11, 0, 0) || 2023 gc_ver == IP_VERSION(11, 0, 1) || 2024 gc_ver == IP_VERSION(11, 0, 4) || 2025 gc_ver == IP_VERSION(11, 5, 0) || 2026 gc_ver == IP_VERSION(11, 5, 1) || 2027 gc_ver == IP_VERSION(11, 0, 2) || 2028 gc_ver == IP_VERSION(11, 0, 3) || 2029 amdgpu_is_multi_aid(adev))) 2030 *states = ATTR_STATE_UNSUPPORTED; 2031 } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) { 2032 if (!((gc_ver == IP_VERSION(10, 3, 1) || 2033 gc_ver == IP_VERSION(10, 3, 0) || 2034 gc_ver == IP_VERSION(11, 0, 2) || 2035 gc_ver == IP_VERSION(11, 0, 3) || 2036 gc_ver == IP_VERSION(11, 5, 1)) && adev->vcn.num_vcn_inst >= 2)) 2037 *states = ATTR_STATE_UNSUPPORTED; 2038 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) { 2039 if (!(gc_ver == IP_VERSION(10, 3, 1) || 2040 gc_ver == IP_VERSION(10, 3, 3) || 2041 gc_ver == IP_VERSION(10, 3, 6) || 2042 gc_ver == IP_VERSION(10, 3, 7) || 2043 gc_ver == IP_VERSION(10, 3, 0) || 2044 gc_ver == IP_VERSION(10, 1, 2) || 2045 gc_ver == IP_VERSION(11, 0, 0) || 2046 gc_ver == IP_VERSION(11, 0, 1) || 2047 gc_ver == IP_VERSION(11, 0, 4) || 2048 gc_ver == IP_VERSION(11, 5, 0) || 2049 gc_ver == IP_VERSION(11, 5, 1) || 2050 gc_ver == IP_VERSION(11, 0, 2) || 2051 gc_ver == IP_VERSION(11, 0, 3) || 2052 amdgpu_is_multi_aid(adev))) 2053 *states = ATTR_STATE_UNSUPPORTED; 2054 } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) { 2055 if (!((gc_ver == IP_VERSION(10, 3, 1) || 2056 gc_ver == IP_VERSION(10, 3, 0) || 2057 gc_ver == IP_VERSION(11, 0, 2) || 2058 gc_ver == IP_VERSION(11, 0, 3) || 2059 gc_ver == IP_VERSION(11, 5, 1)) && adev->vcn.num_vcn_inst >= 2)) 2060 *states = ATTR_STATE_UNSUPPORTED; 2061 } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) { 2062 if (amdgpu_is_multi_aid(adev)) 2063 *states = ATTR_STATE_UNSUPPORTED; 2064 } 2065 2066 switch (gc_ver) { 2067 case IP_VERSION(9, 4, 1): 2068 /* Arcturus does not support standalone mclk/socclk/fclk level setting */ 2069 if (DEVICE_ATTR_IS(pp_dpm_mclk) || 2070 DEVICE_ATTR_IS(pp_dpm_socclk) || 2071 DEVICE_ATTR_IS(pp_dpm_fclk)) { 2072 dev_attr->attr.mode &= ~S_IWUGO; 2073 dev_attr->store = NULL; 2074 } 2075 break; 2076 case IP_VERSION(9, 4, 2): 2077 if (DEVICE_ATTR_IS(pp_dpm_mclk) || 2078 DEVICE_ATTR_IS(pp_dpm_socclk)) { 2079 /* Aldebaran mclk/socclk DPM only supports voltage control, 2080 * not allow to set dpm level directly */ 2081 dev_attr->attr.mode &= ~S_IWUGO; 2082 dev_attr->store = NULL; 2083 } else if (DEVICE_ATTR_IS(pp_dpm_fclk) || 2084 DEVICE_ATTR_IS(pp_dpm_pcie)) { 2085 /* Aldebaran does not support fclk/pcie dpm */ 2086 *states = ATTR_STATE_UNSUPPORTED; 2087 } 2088 break; 2089 default: 2090 break; 2091 } 2092 2093 /* setting should not be allowed from VF if not in one VF mode */ 2094 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) { 2095 dev_attr->attr.mode &= ~S_IWUGO; 2096 dev_attr->store = NULL; 2097 } 2098 2099 return 0; 2100 } 2101 2102 /** 2103 * DOC: board 2104 * 2105 * Certain SOCs can support various board attributes reporting. This is useful 2106 * for user application to monitor various board reated attributes. 2107 * 2108 * The amdgpu driver provides a sysfs API for reporting board attributes. Presently, 2109 * nine types of attributes are reported. Baseboard temperature and 2110 * gpu board temperature are reported as binary files. Npm status, current node power limit, 2111 * max node power limit, node power, global ppt residency, baseboard_power, baseboard_power_limit 2112 * is reported as ASCII text file. 2113 * 2114 * * .. code-block:: console 2115 * 2116 * hexdump /sys/bus/pci/devices/.../board/baseboard_temp 2117 * 2118 * hexdump /sys/bus/pci/devices/.../board/gpuboard_temp 2119 * 2120 * hexdump /sys/bus/pci/devices/.../board/npm_status 2121 * 2122 * hexdump /sys/bus/pci/devices/.../board/cur_node_power_limit 2123 * 2124 * hexdump /sys/bus/pci/devices/.../board/max_node_power_limit 2125 * 2126 * hexdump /sys/bus/pci/devices/.../board/node_power 2127 * 2128 * hexdump /sys/bus/pci/devices/.../board/global_ppt_resid 2129 * 2130 * hexdump /sys/bus/pci/devices/.../board/baseboard_power 2131 * 2132 * hexdump /sys/bus/pci/devices/.../board/baseboard_power_limit 2133 */ 2134 2135 /** 2136 * DOC: baseboard_temp 2137 * 2138 * The amdgpu driver provides a sysfs API for retrieving current baseboard 2139 * temperature metrics data. The file baseboard_temp is used for this. 2140 * Reading the file will dump all the current baseboard temperature metrics data. 2141 */ 2142 static ssize_t amdgpu_get_baseboard_temp_metrics(struct device *dev, 2143 struct device_attribute *attr, char *buf) 2144 { 2145 struct drm_device *ddev = dev_get_drvdata(dev); 2146 struct amdgpu_device *adev = drm_to_adev(ddev); 2147 ssize_t size; 2148 int ret; 2149 2150 ret = amdgpu_pm_get_access_if_active(adev); 2151 if (ret) 2152 return ret; 2153 2154 size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, NULL); 2155 if (size <= 0) 2156 goto out; 2157 if (size >= PAGE_SIZE) { 2158 ret = -ENOSPC; 2159 goto out; 2160 } 2161 2162 amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, buf); 2163 2164 out: 2165 amdgpu_pm_put_access(adev); 2166 2167 if (ret) 2168 return ret; 2169 2170 return size; 2171 } 2172 2173 /** 2174 * DOC: gpuboard_temp 2175 * 2176 * The amdgpu driver provides a sysfs API for retrieving current gpuboard 2177 * temperature metrics data. The file gpuboard_temp is used for this. 2178 * Reading the file will dump all the current gpuboard temperature metrics data. 2179 */ 2180 static ssize_t amdgpu_get_gpuboard_temp_metrics(struct device *dev, 2181 struct device_attribute *attr, char *buf) 2182 { 2183 struct drm_device *ddev = dev_get_drvdata(dev); 2184 struct amdgpu_device *adev = drm_to_adev(ddev); 2185 ssize_t size; 2186 int ret; 2187 2188 ret = amdgpu_pm_get_access_if_active(adev); 2189 if (ret) 2190 return ret; 2191 2192 size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, NULL); 2193 if (size <= 0) 2194 goto out; 2195 if (size >= PAGE_SIZE) { 2196 ret = -ENOSPC; 2197 goto out; 2198 } 2199 2200 amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, buf); 2201 2202 out: 2203 amdgpu_pm_put_access(adev); 2204 2205 if (ret) 2206 return ret; 2207 2208 return size; 2209 } 2210 2211 /** 2212 * DOC: cur_node_power_limit 2213 * 2214 * The amdgpu driver provides a sysfs API for retrieving current node power limit. 2215 * The file cur_node_power_limit is used for this. 2216 */ 2217 static ssize_t amdgpu_show_cur_node_power_limit(struct device *dev, 2218 struct device_attribute *attr, char *buf) 2219 { 2220 struct drm_device *ddev = dev_get_drvdata(dev); 2221 struct amdgpu_device *adev = drm_to_adev(ddev); 2222 u32 nplimit; 2223 int r; 2224 2225 /* get the current node power limit */ 2226 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWERLIMIT, 2227 (void *)&nplimit); 2228 if (r) 2229 return r; 2230 2231 return sysfs_emit(buf, "%u\n", nplimit); 2232 } 2233 2234 /** 2235 * DOC: node_power 2236 * 2237 * The amdgpu driver provides a sysfs API for retrieving current node power. 2238 * The file node_power is used for this. 2239 */ 2240 static ssize_t amdgpu_show_node_power(struct device *dev, 2241 struct device_attribute *attr, char *buf) 2242 { 2243 struct drm_device *ddev = dev_get_drvdata(dev); 2244 struct amdgpu_device *adev = drm_to_adev(ddev); 2245 u32 npower; 2246 int r; 2247 2248 /* get the node power */ 2249 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWER, 2250 (void *)&npower); 2251 if (r) 2252 return r; 2253 2254 return sysfs_emit(buf, "%u\n", npower); 2255 } 2256 2257 /** 2258 * DOC: npm_status 2259 * 2260 * The amdgpu driver provides a sysfs API for retrieving current node power management status. 2261 * The file npm_status is used for this. It shows the status as enabled or disabled based on 2262 * current node power value. If node power is zero, status is disabled else enabled. 2263 */ 2264 static ssize_t amdgpu_show_npm_status(struct device *dev, 2265 struct device_attribute *attr, char *buf) 2266 { 2267 struct drm_device *ddev = dev_get_drvdata(dev); 2268 struct amdgpu_device *adev = drm_to_adev(ddev); 2269 u32 npm_status; 2270 int r; 2271 2272 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NPMSTATUS, 2273 (void *)&npm_status); 2274 if (r) 2275 return r; 2276 2277 return sysfs_emit(buf, "%s\n", str_enabled_disabled(npm_status)); 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 static void amdgpu_pm_npm_sysfs_init(struct amdgpu_device *adev) 4729 { 4730 static const struct { 4731 struct device_attribute *attr; 4732 unsigned int field; 4733 } files[] = { 4734 { &dev_attr_cur_node_power_limit, AMDGPU_NPM_CAP_CUR_NODE_POWER_LIMIT }, 4735 { &dev_attr_node_power, AMDGPU_NPM_CAP_NODE_POWER }, 4736 { &dev_attr_global_ppt_resid, AMDGPU_NPM_CAP_GLOBAL_PPT_RESID }, 4737 { &dev_attr_max_node_power_limit, AMDGPU_NPM_CAP_MAX_NODE_POWER_LIMIT }, 4738 { &dev_attr_npm_status, AMDGPU_NPM_CAP_NPM_STATUS }, 4739 }; 4740 u64 cap = amdgpu_dpm_get_npm_cap(adev); 4741 int i; 4742 4743 if (!cap) 4744 return; 4745 4746 for (i = 0; i < ARRAY_SIZE(files); i++) { 4747 unsigned int bits = (cap >> (files[i].field * 2)) & 0x3; 4748 struct device_attribute *attr = files[i].attr; 4749 4750 if (!bits) 4751 continue; 4752 4753 attr->attr.mode = ((bits & BIT(1)) && attr->store) ? 0644 : 0444; 4754 sysfs_add_file_to_group(&adev->dev->kobj, &attr->attr, 4755 amdgpu_board_attr_group.name); 4756 } 4757 } 4758 4759 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev) 4760 { 4761 enum amdgpu_sriov_vf_mode mode; 4762 uint32_t mask = 0; 4763 uint32_t tmp; 4764 int ret; 4765 4766 if (adev->pm.sysfs_initialized) 4767 return 0; 4768 4769 INIT_LIST_HEAD(&adev->pm.pm_attr_list); 4770 4771 if (adev->pm.dpm_enabled == 0) 4772 return 0; 4773 4774 mode = amdgpu_virt_get_sriov_vf_mode(adev); 4775 4776 /* under multi-vf mode, the hwmon attributes are all not supported */ 4777 if (mode != SRIOV_VF_MODE_MULTI_VF) { 4778 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev, 4779 DRIVER_NAME, adev, 4780 hwmon_groups); 4781 if (IS_ERR(adev->pm.int_hwmon_dev)) { 4782 ret = PTR_ERR(adev->pm.int_hwmon_dev); 4783 dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret); 4784 return ret; 4785 } 4786 } 4787 4788 switch (mode) { 4789 case SRIOV_VF_MODE_ONE_VF: 4790 mask = ATTR_FLAG_ONEVF; 4791 break; 4792 case SRIOV_VF_MODE_MULTI_VF: 4793 mask = 0; 4794 break; 4795 case SRIOV_VF_MODE_BARE_METAL: 4796 default: 4797 mask = ATTR_FLAG_MASK_ALL; 4798 break; 4799 } 4800 4801 ret = amdgpu_device_attr_create_groups(adev, 4802 amdgpu_device_attrs, 4803 ARRAY_SIZE(amdgpu_device_attrs), 4804 mask, 4805 &adev->pm.pm_attr_list); 4806 if (ret) 4807 goto err_out0; 4808 4809 if (amdgpu_dpm_is_overdrive_supported(adev)) { 4810 ret = amdgpu_od_set_init(adev); 4811 if (ret) 4812 goto err_out1; 4813 } else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) { 4814 dev_info(adev->dev, "overdrive feature is not supported\n"); 4815 } 4816 4817 if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) != 4818 -EOPNOTSUPP) { 4819 ret = devm_device_add_group(adev->dev, 4820 &amdgpu_pm_policy_attr_group); 4821 if (ret) 4822 goto err_out1; 4823 } 4824 4825 if (amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD)) { 4826 ret = devm_device_add_group(adev->dev, 4827 &amdgpu_board_attr_group); 4828 if (ret) 4829 goto err_out1; 4830 amdgpu_pm_npm_sysfs_init(adev); 4831 if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER_LIMIT, 4832 (void *)&tmp) != -EOPNOTSUPP) { 4833 sysfs_add_file_to_group(&adev->dev->kobj, 4834 &dev_attr_baseboard_power_limit.attr, 4835 amdgpu_board_attr_group.name); 4836 sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_baseboard_power.attr, 4837 amdgpu_board_attr_group.name); 4838 } 4839 } 4840 4841 adev->pm.sysfs_initialized = true; 4842 4843 return 0; 4844 4845 err_out1: 4846 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 4847 err_out0: 4848 if (adev->pm.int_hwmon_dev) 4849 hwmon_device_unregister(adev->pm.int_hwmon_dev); 4850 4851 return ret; 4852 } 4853 4854 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev) 4855 { 4856 amdgpu_od_set_fini(adev); 4857 4858 if (adev->pm.int_hwmon_dev) 4859 hwmon_device_unregister(adev->pm.int_hwmon_dev); 4860 4861 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 4862 } 4863 4864 /* 4865 * Debugfs info 4866 */ 4867 #if defined(CONFIG_DEBUG_FS) 4868 4869 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m, 4870 struct amdgpu_device *adev) 4871 { 4872 uint16_t *p_val; 4873 uint32_t size; 4874 int i; 4875 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev); 4876 4877 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) { 4878 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t), 4879 GFP_KERNEL); 4880 4881 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK, 4882 (void *)p_val, &size)) { 4883 for (i = 0; i < num_cpu_cores; i++) 4884 seq_printf(m, "\t%u MHz (CPU%d)\n", 4885 *(p_val + i), i); 4886 } 4887 4888 kfree(p_val); 4889 } 4890 } 4891 4892 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev) 4893 { 4894 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0); 4895 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 4896 uint64_t value64 = 0; 4897 uint32_t value; 4898 int size; 4899 4900 /* GPU Clocks */ 4901 size = sizeof(value); 4902 seq_printf(m, "GFX Clocks and Power:\n"); 4903 4904 amdgpu_debugfs_prints_cpu_info(m, adev); 4905 4906 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size)) 4907 seq_printf(m, "\t%u MHz (MCLK)\n", value/100); 4908 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size)) 4909 seq_printf(m, "\t%u MHz (SCLK)\n", value/100); 4910 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size)) 4911 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100); 4912 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size)) 4913 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100); 4914 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size)) 4915 seq_printf(m, "\t%u mV (VDDGFX)\n", value); 4916 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size)) 4917 seq_printf(m, "\t%u mV (VDDNB)\n", value); 4918 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&value, &size)) { 4919 if (adev->flags & AMD_IS_APU) 4920 seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", 4921 (u32)(value / MILLIWATT_PER_WATT), (u32)(value % MILLIWATT_PER_WATT) / 10); 4922 else 4923 seq_printf(m, "\t%u.%02u W (average SoC)\n", 4924 (u32)(value / MILLIWATT_PER_WATT), (u32)(value % MILLIWATT_PER_WATT) / 10); 4925 } 4926 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&value, &size)) { 4927 if (adev->flags & AMD_IS_APU) 4928 seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", 4929 (u32)(value / MILLIWATT_PER_WATT), (u32)(value % MILLIWATT_PER_WATT) / 10); 4930 else 4931 seq_printf(m, "\t%u.%02u W (current SoC)\n", 4932 (u32)(value / MILLIWATT_PER_WATT), (u32)(value % MILLIWATT_PER_WATT) / 10); 4933 } 4934 seq_printf(m, "\n"); 4935 4936 /* GPU Temp */ 4937 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size)) 4938 seq_printf(m, "GPU Temperature: %u C\n", value/1000); 4939 4940 /* GPU Load */ 4941 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size)) 4942 seq_printf(m, "GPU Load: %u %%\n", value); 4943 /* MEM Load */ 4944 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size)) 4945 seq_printf(m, "MEM Load: %u %%\n", value); 4946 /* VCN Load */ 4947 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size)) 4948 seq_printf(m, "VCN Load: %u %%\n", value); 4949 4950 seq_printf(m, "\n"); 4951 4952 /* SMC feature mask */ 4953 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size)) 4954 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64); 4955 4956 /* ASICs greater than CHIP_VEGA20 supports these sensors */ 4957 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) { 4958 /* VCN clocks */ 4959 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) { 4960 if (!value) { 4961 seq_printf(m, "VCN: Powered down\n"); 4962 } else { 4963 seq_printf(m, "VCN: Powered up\n"); 4964 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 4965 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 4966 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 4967 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 4968 } 4969 } 4970 seq_printf(m, "\n"); 4971 } else { 4972 /* UVD clocks */ 4973 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) { 4974 if (!value) { 4975 seq_printf(m, "UVD: Powered down\n"); 4976 } else { 4977 seq_printf(m, "UVD: Powered up\n"); 4978 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 4979 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 4980 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 4981 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 4982 } 4983 } 4984 seq_printf(m, "\n"); 4985 4986 /* VCE clocks */ 4987 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) { 4988 if (!value) { 4989 seq_printf(m, "VCE: Powered down\n"); 4990 } else { 4991 seq_printf(m, "VCE: Powered up\n"); 4992 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size)) 4993 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100); 4994 } 4995 } 4996 } 4997 4998 return 0; 4999 } 5000 5001 static const struct cg_flag_name clocks[] = { 5002 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"}, 5003 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"}, 5004 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"}, 5005 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"}, 5006 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"}, 5007 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"}, 5008 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"}, 5009 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"}, 5010 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"}, 5011 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"}, 5012 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"}, 5013 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"}, 5014 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"}, 5015 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"}, 5016 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"}, 5017 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"}, 5018 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"}, 5019 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"}, 5020 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"}, 5021 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"}, 5022 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"}, 5023 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"}, 5024 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"}, 5025 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"}, 5026 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"}, 5027 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"}, 5028 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"}, 5029 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"}, 5030 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"}, 5031 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"}, 5032 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"}, 5033 {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"}, 5034 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"}, 5035 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"}, 5036 {0, NULL}, 5037 }; 5038 5039 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags) 5040 { 5041 int i; 5042 5043 for (i = 0; clocks[i].flag; i++) 5044 seq_printf(m, "\t%s: %s\n", clocks[i].name, 5045 (flags & clocks[i].flag) ? "On" : "Off"); 5046 } 5047 5048 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused) 5049 { 5050 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 5051 u64 flags = 0; 5052 int r; 5053 5054 r = amdgpu_pm_get_access(adev); 5055 if (r < 0) 5056 return r; 5057 5058 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) { 5059 r = amdgpu_debugfs_pm_info_pp(m, adev); 5060 if (r) 5061 goto out; 5062 } 5063 5064 amdgpu_device_ip_get_clockgating_state(adev, &flags); 5065 5066 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags); 5067 amdgpu_parse_cg_state(m, flags); 5068 seq_printf(m, "\n"); 5069 5070 out: 5071 amdgpu_pm_put_access(adev); 5072 5073 return r; 5074 } 5075 5076 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info); 5077 5078 /* 5079 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW 5080 * 5081 * Reads debug memory region allocated to PMFW 5082 */ 5083 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf, 5084 size_t size, loff_t *pos) 5085 { 5086 struct amdgpu_device *adev = file_inode(f)->i_private; 5087 size_t smu_prv_buf_size; 5088 void *smu_prv_buf; 5089 int ret = 0; 5090 5091 ret = amdgpu_pm_dev_state_check(adev, true); 5092 if (ret) 5093 return ret; 5094 5095 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size); 5096 if (ret) 5097 return ret; 5098 5099 if (!smu_prv_buf || !smu_prv_buf_size) 5100 return -EINVAL; 5101 5102 return simple_read_from_buffer(buf, size, pos, smu_prv_buf, 5103 smu_prv_buf_size); 5104 } 5105 5106 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = { 5107 .owner = THIS_MODULE, 5108 .open = simple_open, 5109 .read = amdgpu_pm_prv_buffer_read, 5110 .llseek = default_llseek, 5111 }; 5112 5113 #endif 5114 5115 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev) 5116 { 5117 #if defined(CONFIG_DEBUG_FS) 5118 struct drm_minor *minor = adev_to_drm(adev)->primary; 5119 struct dentry *root = minor->debugfs_root; 5120 5121 if (!adev->pm.dpm_enabled) 5122 return; 5123 5124 debugfs_create_file("amdgpu_pm_info", 0444, root, adev, 5125 &amdgpu_debugfs_pm_info_fops); 5126 5127 if (adev->pm.smu_prv_buffer_size > 0) 5128 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root, 5129 adev, 5130 &amdgpu_debugfs_pm_prv_buffer_fops, 5131 adev->pm.smu_prv_buffer_size); 5132 5133 amdgpu_dpm_stb_debug_fs_init(adev); 5134 #endif 5135 } 5136