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