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 *states = ATTR_STATE_SUPPORTED; 1914 1915 if (!amdgpu_dpm_is_overdrive_supported(adev)) { 1916 *states = ATTR_STATE_UNSUPPORTED; 1917 return 0; 1918 } 1919 1920 /* Enable pp_od_clk_voltage node for gc 9.4.3, 9.4.4, 9.5.0, 12.1.0 SRIOV/BM support */ 1921 if (amdgpu_is_multi_aid(adev)) { 1922 if (amdgpu_sriov_multi_vf_mode(adev)) 1923 *states = ATTR_STATE_UNSUPPORTED; 1924 return 0; 1925 } 1926 1927 if (!(attr->flags & mask)) 1928 *states = ATTR_STATE_UNSUPPORTED; 1929 1930 return 0; 1931 } 1932 1933 static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1934 uint32_t mask, enum amdgpu_device_attr_states *states) 1935 { 1936 struct device_attribute *dev_attr = &attr->dev_attr; 1937 uint32_t gc_ver; 1938 1939 *states = ATTR_STATE_SUPPORTED; 1940 1941 if (!(attr->flags & mask)) { 1942 *states = ATTR_STATE_UNSUPPORTED; 1943 return 0; 1944 } 1945 1946 gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 1947 /* dcefclk node is not available on gfx 11.0.3 sriov */ 1948 if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) || 1949 gc_ver < IP_VERSION(9, 0, 0) || 1950 !amdgpu_device_has_display_hardware(adev)) 1951 *states = ATTR_STATE_UNSUPPORTED; 1952 1953 /* SMU MP1 does not support dcefclk level setting, 1954 * setting should not be allowed from VF if not in one VF mode. 1955 */ 1956 if (gc_ver >= IP_VERSION(10, 0, 0) || 1957 (amdgpu_sriov_multi_vf_mode(adev))) { 1958 dev_attr->attr.mode &= ~S_IWUGO; 1959 dev_attr->store = NULL; 1960 } 1961 1962 return 0; 1963 } 1964 1965 static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1966 uint32_t mask, enum amdgpu_device_attr_states *states) 1967 { 1968 struct device_attribute *dev_attr = &attr->dev_attr; 1969 enum amdgpu_device_attr_id attr_id = attr->attr_id; 1970 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0); 1971 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 1972 1973 *states = ATTR_STATE_SUPPORTED; 1974 1975 if (!(attr->flags & mask)) { 1976 *states = ATTR_STATE_UNSUPPORTED; 1977 return 0; 1978 } 1979 1980 if (DEVICE_ATTR_IS(pp_dpm_socclk)) { 1981 if (gc_ver < IP_VERSION(9, 0, 0)) 1982 *states = ATTR_STATE_UNSUPPORTED; 1983 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) { 1984 if (mp1_ver < IP_VERSION(10, 0, 0)) 1985 *states = ATTR_STATE_UNSUPPORTED; 1986 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) { 1987 if (!(gc_ver == IP_VERSION(10, 3, 1) || 1988 gc_ver == IP_VERSION(10, 3, 3) || 1989 gc_ver == IP_VERSION(10, 3, 6) || 1990 gc_ver == IP_VERSION(10, 3, 7) || 1991 gc_ver == IP_VERSION(10, 3, 0) || 1992 gc_ver == IP_VERSION(10, 1, 2) || 1993 gc_ver == IP_VERSION(11, 0, 0) || 1994 gc_ver == IP_VERSION(11, 0, 1) || 1995 gc_ver == IP_VERSION(11, 0, 4) || 1996 gc_ver == IP_VERSION(11, 5, 0) || 1997 gc_ver == IP_VERSION(11, 0, 2) || 1998 gc_ver == IP_VERSION(11, 0, 3) || 1999 amdgpu_is_multi_aid(adev))) 2000 *states = ATTR_STATE_UNSUPPORTED; 2001 } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) { 2002 if (!((gc_ver == IP_VERSION(10, 3, 1) || 2003 gc_ver == IP_VERSION(10, 3, 0) || 2004 gc_ver == IP_VERSION(11, 0, 2) || 2005 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2)) 2006 *states = ATTR_STATE_UNSUPPORTED; 2007 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) { 2008 if (!(gc_ver == IP_VERSION(10, 3, 1) || 2009 gc_ver == IP_VERSION(10, 3, 3) || 2010 gc_ver == IP_VERSION(10, 3, 6) || 2011 gc_ver == IP_VERSION(10, 3, 7) || 2012 gc_ver == IP_VERSION(10, 3, 0) || 2013 gc_ver == IP_VERSION(10, 1, 2) || 2014 gc_ver == IP_VERSION(11, 0, 0) || 2015 gc_ver == IP_VERSION(11, 0, 1) || 2016 gc_ver == IP_VERSION(11, 0, 4) || 2017 gc_ver == IP_VERSION(11, 5, 0) || 2018 gc_ver == IP_VERSION(11, 0, 2) || 2019 gc_ver == IP_VERSION(11, 0, 3) || 2020 amdgpu_is_multi_aid(adev))) 2021 *states = ATTR_STATE_UNSUPPORTED; 2022 } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) { 2023 if (!((gc_ver == IP_VERSION(10, 3, 1) || 2024 gc_ver == IP_VERSION(10, 3, 0) || 2025 gc_ver == IP_VERSION(11, 0, 2) || 2026 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2)) 2027 *states = ATTR_STATE_UNSUPPORTED; 2028 } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) { 2029 if (gc_ver == IP_VERSION(9, 4, 2) || 2030 amdgpu_is_multi_aid(adev)) 2031 *states = ATTR_STATE_UNSUPPORTED; 2032 } 2033 2034 switch (gc_ver) { 2035 case IP_VERSION(9, 4, 1): 2036 case IP_VERSION(9, 4, 2): 2037 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */ 2038 if (DEVICE_ATTR_IS(pp_dpm_mclk) || 2039 DEVICE_ATTR_IS(pp_dpm_socclk) || 2040 DEVICE_ATTR_IS(pp_dpm_fclk)) { 2041 dev_attr->attr.mode &= ~S_IWUGO; 2042 dev_attr->store = NULL; 2043 } 2044 break; 2045 default: 2046 break; 2047 } 2048 2049 /* setting should not be allowed from VF if not in one VF mode */ 2050 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) { 2051 dev_attr->attr.mode &= ~S_IWUGO; 2052 dev_attr->store = NULL; 2053 } 2054 2055 return 0; 2056 } 2057 2058 /** 2059 * DOC: board 2060 * 2061 * Certain SOCs can support various board attributes reporting. This is useful 2062 * for user application to monitor various board reated attributes. 2063 * 2064 * The amdgpu driver provides a sysfs API for reporting board attributes. Presently, 2065 * nine types of attributes are reported. Baseboard temperature and 2066 * gpu board temperature are reported as binary files. Npm status, current node power limit, 2067 * max node power limit, node power, global ppt residency, baseboard_power, baseboard_power_limit 2068 * is reported as ASCII text file. 2069 * 2070 * * .. code-block:: console 2071 * 2072 * hexdump /sys/bus/pci/devices/.../board/baseboard_temp 2073 * 2074 * hexdump /sys/bus/pci/devices/.../board/gpuboard_temp 2075 * 2076 * hexdump /sys/bus/pci/devices/.../board/npm_status 2077 * 2078 * hexdump /sys/bus/pci/devices/.../board/cur_node_power_limit 2079 * 2080 * hexdump /sys/bus/pci/devices/.../board/max_node_power_limit 2081 * 2082 * hexdump /sys/bus/pci/devices/.../board/node_power 2083 * 2084 * hexdump /sys/bus/pci/devices/.../board/global_ppt_resid 2085 * 2086 * hexdump /sys/bus/pci/devices/.../board/baseboard_power 2087 * 2088 * hexdump /sys/bus/pci/devices/.../board/baseboard_power_limit 2089 */ 2090 2091 /** 2092 * DOC: baseboard_temp 2093 * 2094 * The amdgpu driver provides a sysfs API for retrieving current baseboard 2095 * temperature metrics data. The file baseboard_temp is used for this. 2096 * Reading the file will dump all the current baseboard temperature metrics data. 2097 */ 2098 static ssize_t amdgpu_get_baseboard_temp_metrics(struct device *dev, 2099 struct device_attribute *attr, char *buf) 2100 { 2101 struct drm_device *ddev = dev_get_drvdata(dev); 2102 struct amdgpu_device *adev = drm_to_adev(ddev); 2103 ssize_t size; 2104 int ret; 2105 2106 ret = amdgpu_pm_get_access_if_active(adev); 2107 if (ret) 2108 return ret; 2109 2110 size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, NULL); 2111 if (size <= 0) 2112 goto out; 2113 if (size >= PAGE_SIZE) { 2114 ret = -ENOSPC; 2115 goto out; 2116 } 2117 2118 amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, buf); 2119 2120 out: 2121 amdgpu_pm_put_access(adev); 2122 2123 if (ret) 2124 return ret; 2125 2126 return size; 2127 } 2128 2129 /** 2130 * DOC: gpuboard_temp 2131 * 2132 * The amdgpu driver provides a sysfs API for retrieving current gpuboard 2133 * temperature metrics data. The file gpuboard_temp is used for this. 2134 * Reading the file will dump all the current gpuboard temperature metrics data. 2135 */ 2136 static ssize_t amdgpu_get_gpuboard_temp_metrics(struct device *dev, 2137 struct device_attribute *attr, char *buf) 2138 { 2139 struct drm_device *ddev = dev_get_drvdata(dev); 2140 struct amdgpu_device *adev = drm_to_adev(ddev); 2141 ssize_t size; 2142 int ret; 2143 2144 ret = amdgpu_pm_get_access_if_active(adev); 2145 if (ret) 2146 return ret; 2147 2148 size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, NULL); 2149 if (size <= 0) 2150 goto out; 2151 if (size >= PAGE_SIZE) { 2152 ret = -ENOSPC; 2153 goto out; 2154 } 2155 2156 amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, buf); 2157 2158 out: 2159 amdgpu_pm_put_access(adev); 2160 2161 if (ret) 2162 return ret; 2163 2164 return size; 2165 } 2166 2167 /** 2168 * DOC: cur_node_power_limit 2169 * 2170 * The amdgpu driver provides a sysfs API for retrieving current node power limit. 2171 * The file cur_node_power_limit is used for this. 2172 */ 2173 static ssize_t amdgpu_show_cur_node_power_limit(struct device *dev, 2174 struct device_attribute *attr, char *buf) 2175 { 2176 struct drm_device *ddev = dev_get_drvdata(dev); 2177 struct amdgpu_device *adev = drm_to_adev(ddev); 2178 u32 nplimit; 2179 int r; 2180 2181 /* get the current node power limit */ 2182 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWERLIMIT, 2183 (void *)&nplimit); 2184 if (r) 2185 return r; 2186 2187 return sysfs_emit(buf, "%u\n", nplimit); 2188 } 2189 2190 /** 2191 * DOC: node_power 2192 * 2193 * The amdgpu driver provides a sysfs API for retrieving current node power. 2194 * The file node_power is used for this. 2195 */ 2196 static ssize_t amdgpu_show_node_power(struct device *dev, 2197 struct device_attribute *attr, char *buf) 2198 { 2199 struct drm_device *ddev = dev_get_drvdata(dev); 2200 struct amdgpu_device *adev = drm_to_adev(ddev); 2201 u32 npower; 2202 int r; 2203 2204 /* get the node power */ 2205 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWER, 2206 (void *)&npower); 2207 if (r) 2208 return r; 2209 2210 return sysfs_emit(buf, "%u\n", npower); 2211 } 2212 2213 /** 2214 * DOC: npm_status 2215 * 2216 * The amdgpu driver provides a sysfs API for retrieving current node power management status. 2217 * The file npm_status is used for this. It shows the status as enabled or disabled based on 2218 * current node power value. If node power is zero, status is disabled else enabled. 2219 */ 2220 static ssize_t amdgpu_show_npm_status(struct device *dev, 2221 struct device_attribute *attr, char *buf) 2222 { 2223 struct drm_device *ddev = dev_get_drvdata(dev); 2224 struct amdgpu_device *adev = drm_to_adev(ddev); 2225 u32 npower; 2226 int r; 2227 2228 /* get the node power */ 2229 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWER, 2230 (void *)&npower); 2231 if (r) 2232 return r; 2233 2234 return sysfs_emit(buf, "%s\n", npower ? "enabled" : "disabled"); 2235 } 2236 2237 /** 2238 * DOC: global_ppt_resid 2239 * 2240 * The amdgpu driver provides a sysfs API for retrieving global ppt residency. 2241 * The file global_ppt_resid is used for this. 2242 */ 2243 static ssize_t amdgpu_show_global_ppt_resid(struct device *dev, 2244 struct device_attribute *attr, char *buf) 2245 { 2246 struct drm_device *ddev = dev_get_drvdata(dev); 2247 struct amdgpu_device *adev = drm_to_adev(ddev); 2248 u32 gpptresid; 2249 int r; 2250 2251 /* get the global ppt residency */ 2252 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPPTRESIDENCY, 2253 (void *)&gpptresid); 2254 if (r) 2255 return r; 2256 2257 return sysfs_emit(buf, "%u\n", gpptresid); 2258 } 2259 2260 /** 2261 * DOC: max_node_power_limit 2262 * 2263 * The amdgpu driver provides a sysfs API for retrieving maximum node power limit. 2264 * The file max_node_power_limit is used for this. 2265 */ 2266 static ssize_t amdgpu_show_max_node_power_limit(struct device *dev, 2267 struct device_attribute *attr, char *buf) 2268 { 2269 struct drm_device *ddev = dev_get_drvdata(dev); 2270 struct amdgpu_device *adev = drm_to_adev(ddev); 2271 u32 max_nplimit; 2272 int r; 2273 2274 /* get the max node power limit */ 2275 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAXNODEPOWERLIMIT, 2276 (void *)&max_nplimit); 2277 if (r) 2278 return r; 2279 2280 return sysfs_emit(buf, "%u\n", max_nplimit); 2281 } 2282 2283 /** 2284 * DOC: baseboard_power 2285 * 2286 * The amdgpu driver provides a sysfs API for retrieving current ubb power in watts. 2287 * The file baseboard_power is used for this. 2288 */ 2289 static ssize_t amdgpu_show_baseboard_power(struct device *dev, 2290 struct device_attribute *attr, char *buf) 2291 { 2292 struct drm_device *ddev = dev_get_drvdata(dev); 2293 struct amdgpu_device *adev = drm_to_adev(ddev); 2294 u32 ubbpower; 2295 int r; 2296 2297 /* get the ubb power */ 2298 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER, 2299 (void *)&ubbpower); 2300 if (r) 2301 return r; 2302 2303 return sysfs_emit(buf, "%u\n", ubbpower); 2304 } 2305 2306 /** 2307 * DOC: baseboard_power_limit 2308 * 2309 * The amdgpu driver provides a sysfs API for retrieving threshold ubb power in watts. 2310 * The file baseboard_power_limit is used for this. 2311 */ 2312 static ssize_t amdgpu_show_baseboard_power_limit(struct device *dev, 2313 struct device_attribute *attr, char *buf) 2314 { 2315 struct drm_device *ddev = dev_get_drvdata(dev); 2316 struct amdgpu_device *adev = drm_to_adev(ddev); 2317 u32 ubbpowerlimit; 2318 int r; 2319 2320 /* get the ubb power limit */ 2321 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER_LIMIT, 2322 (void *)&ubbpowerlimit); 2323 if (r) 2324 return r; 2325 2326 return sysfs_emit(buf, "%u\n", ubbpowerlimit); 2327 } 2328 2329 static DEVICE_ATTR(baseboard_temp, 0444, amdgpu_get_baseboard_temp_metrics, NULL); 2330 static DEVICE_ATTR(gpuboard_temp, 0444, amdgpu_get_gpuboard_temp_metrics, NULL); 2331 static DEVICE_ATTR(cur_node_power_limit, 0444, amdgpu_show_cur_node_power_limit, NULL); 2332 static DEVICE_ATTR(node_power, 0444, amdgpu_show_node_power, NULL); 2333 static DEVICE_ATTR(global_ppt_resid, 0444, amdgpu_show_global_ppt_resid, NULL); 2334 static DEVICE_ATTR(max_node_power_limit, 0444, amdgpu_show_max_node_power_limit, NULL); 2335 static DEVICE_ATTR(npm_status, 0444, amdgpu_show_npm_status, NULL); 2336 static DEVICE_ATTR(baseboard_power, 0444, amdgpu_show_baseboard_power, NULL); 2337 static DEVICE_ATTR(baseboard_power_limit, 0444, amdgpu_show_baseboard_power_limit, NULL); 2338 2339 static struct attribute *board_attrs[] = { 2340 &dev_attr_baseboard_temp.attr, 2341 &dev_attr_gpuboard_temp.attr, 2342 NULL 2343 }; 2344 2345 static umode_t amdgpu_board_attr_visible(struct kobject *kobj, struct attribute *attr, int n) 2346 { 2347 struct device *dev = kobj_to_dev(kobj); 2348 struct drm_device *ddev = dev_get_drvdata(dev); 2349 struct amdgpu_device *adev = drm_to_adev(ddev); 2350 2351 if (attr == &dev_attr_baseboard_temp.attr) { 2352 if (!amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_BASEBOARD)) 2353 return 0; 2354 } 2355 2356 if (attr == &dev_attr_gpuboard_temp.attr) { 2357 if (!amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD)) 2358 return 0; 2359 } 2360 2361 return attr->mode; 2362 } 2363 2364 const struct attribute_group amdgpu_board_attr_group = { 2365 .name = "board", 2366 .attrs = board_attrs, 2367 .is_visible = amdgpu_board_attr_visible, 2368 }; 2369 2370 /* pm policy attributes */ 2371 struct amdgpu_pm_policy_attr { 2372 struct device_attribute dev_attr; 2373 enum pp_pm_policy id; 2374 }; 2375 2376 /** 2377 * DOC: pm_policy 2378 * 2379 * Certain SOCs can support different power policies to optimize application 2380 * performance. However, this policy is provided only at SOC level and not at a 2381 * per-process level. This is useful especially when entire SOC is utilized for 2382 * dedicated workload. 2383 * 2384 * The amdgpu driver provides a sysfs API for selecting the policy. Presently, 2385 * only two types of policies are supported through this interface. 2386 * 2387 * Pstate Policy Selection - This is to select different Pstate profiles which 2388 * decides clock/throttling preferences. 2389 * 2390 * XGMI PLPD Policy Selection - When multiple devices are connected over XGMI, 2391 * this helps to select policy to be applied for per link power down. 2392 * 2393 * The list of available policies and policy levels vary between SOCs. They can 2394 * be viewed under pm_policy node directory. If SOC doesn't support any policy, 2395 * this node won't be available. The different policies supported will be 2396 * available as separate nodes under pm_policy. 2397 * 2398 * cat /sys/bus/pci/devices/.../pm_policy/<policy_type> 2399 * 2400 * Reading the policy file shows the different levels supported. The level which 2401 * is applied presently is denoted by * (asterisk). E.g., 2402 * 2403 * .. code-block:: console 2404 * 2405 * cat /sys/bus/pci/devices/.../pm_policy/soc_pstate 2406 * 0 : soc_pstate_default 2407 * 1 : soc_pstate_0 2408 * 2 : soc_pstate_1* 2409 * 3 : soc_pstate_2 2410 * 2411 * cat /sys/bus/pci/devices/.../pm_policy/xgmi_plpd 2412 * 0 : plpd_disallow 2413 * 1 : plpd_default 2414 * 2 : plpd_optimized* 2415 * 2416 * To apply a specific policy 2417 * 2418 * "echo <level> > /sys/bus/pci/devices/.../pm_policy/<policy_type>" 2419 * 2420 * For the levels listed in the example above, to select "plpd_optimized" for 2421 * XGMI and "soc_pstate_2" for soc pstate policy - 2422 * 2423 * .. code-block:: console 2424 * 2425 * echo "2" > /sys/bus/pci/devices/.../pm_policy/xgmi_plpd 2426 * echo "3" > /sys/bus/pci/devices/.../pm_policy/soc_pstate 2427 * 2428 */ 2429 static ssize_t amdgpu_get_pm_policy_attr(struct device *dev, 2430 struct device_attribute *attr, 2431 char *buf) 2432 { 2433 struct drm_device *ddev = dev_get_drvdata(dev); 2434 struct amdgpu_device *adev = drm_to_adev(ddev); 2435 struct amdgpu_pm_policy_attr *policy_attr; 2436 2437 policy_attr = 2438 container_of(attr, struct amdgpu_pm_policy_attr, dev_attr); 2439 2440 return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf); 2441 } 2442 2443 static ssize_t amdgpu_set_pm_policy_attr(struct device *dev, 2444 struct device_attribute *attr, 2445 const char *buf, size_t count) 2446 { 2447 struct drm_device *ddev = dev_get_drvdata(dev); 2448 struct amdgpu_device *adev = drm_to_adev(ddev); 2449 struct amdgpu_pm_policy_attr *policy_attr; 2450 int ret, num_params = 0; 2451 char delimiter[] = " \n\t"; 2452 char tmp_buf[128]; 2453 char *tmp, *param; 2454 long val; 2455 2456 count = min(count, sizeof(tmp_buf)); 2457 memcpy(tmp_buf, buf, count); 2458 tmp_buf[count - 1] = '\0'; 2459 tmp = tmp_buf; 2460 2461 tmp = skip_spaces(tmp); 2462 while ((param = strsep(&tmp, delimiter))) { 2463 if (!strlen(param)) { 2464 tmp = skip_spaces(tmp); 2465 continue; 2466 } 2467 ret = kstrtol(param, 0, &val); 2468 if (ret) 2469 return -EINVAL; 2470 num_params++; 2471 if (num_params > 1) 2472 return -EINVAL; 2473 } 2474 2475 if (num_params != 1) 2476 return -EINVAL; 2477 2478 policy_attr = 2479 container_of(attr, struct amdgpu_pm_policy_attr, dev_attr); 2480 2481 ret = amdgpu_pm_get_access(adev); 2482 if (ret < 0) 2483 return ret; 2484 2485 ret = amdgpu_dpm_set_pm_policy(adev, policy_attr->id, val); 2486 2487 amdgpu_pm_put_access(adev); 2488 2489 if (ret) 2490 return ret; 2491 2492 return count; 2493 } 2494 2495 #define AMDGPU_PM_POLICY_ATTR(_name, _id) \ 2496 static struct amdgpu_pm_policy_attr pm_policy_attr_##_name = { \ 2497 .dev_attr = __ATTR(_name, 0644, amdgpu_get_pm_policy_attr, \ 2498 amdgpu_set_pm_policy_attr), \ 2499 .id = PP_PM_POLICY_##_id, \ 2500 }; 2501 2502 #define AMDGPU_PM_POLICY_ATTR_VAR(_name) pm_policy_attr_##_name.dev_attr.attr 2503 2504 AMDGPU_PM_POLICY_ATTR(soc_pstate, SOC_PSTATE) 2505 AMDGPU_PM_POLICY_ATTR(xgmi_plpd, XGMI_PLPD) 2506 2507 static struct attribute *pm_policy_attrs[] = { 2508 &AMDGPU_PM_POLICY_ATTR_VAR(soc_pstate), 2509 &AMDGPU_PM_POLICY_ATTR_VAR(xgmi_plpd), 2510 NULL 2511 }; 2512 2513 static umode_t amdgpu_pm_policy_attr_visible(struct kobject *kobj, 2514 struct attribute *attr, int n) 2515 { 2516 struct device *dev = kobj_to_dev(kobj); 2517 struct drm_device *ddev = dev_get_drvdata(dev); 2518 struct amdgpu_device *adev = drm_to_adev(ddev); 2519 struct amdgpu_pm_policy_attr *policy_attr; 2520 2521 policy_attr = 2522 container_of(attr, struct amdgpu_pm_policy_attr, dev_attr.attr); 2523 2524 if (amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, NULL) == 2525 -ENOENT) 2526 return 0; 2527 2528 return attr->mode; 2529 } 2530 2531 const struct attribute_group amdgpu_pm_policy_attr_group = { 2532 .name = "pm_policy", 2533 .attrs = pm_policy_attrs, 2534 .is_visible = amdgpu_pm_policy_attr_visible, 2535 }; 2536 2537 static struct amdgpu_device_attr amdgpu_device_attrs[] = { 2538 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2539 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2540 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2541 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2542 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2543 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC), 2544 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2545 .attr_update = pp_dpm_clk_default_attr_update), 2546 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2547 .attr_update = pp_dpm_clk_default_attr_update), 2548 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2549 .attr_update = pp_dpm_clk_default_attr_update), 2550 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2551 .attr_update = pp_dpm_clk_default_attr_update), 2552 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2553 .attr_update = pp_dpm_clk_default_attr_update), 2554 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2555 .attr_update = pp_dpm_clk_default_attr_update), 2556 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2557 .attr_update = pp_dpm_clk_default_attr_update), 2558 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2559 .attr_update = pp_dpm_clk_default_attr_update), 2560 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2561 .attr_update = pp_dpm_dcefclk_attr_update), 2562 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF, 2563 .attr_update = pp_dpm_clk_default_attr_update), 2564 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC), 2565 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC), 2566 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2567 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC, 2568 .attr_update = pp_od_clk_voltage_attr_update), 2569 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2570 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2571 AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2572 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC), 2573 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2574 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2575 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2576 AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2577 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 2578 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC, 2579 .attr_update = ss_power_attr_update), 2580 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC, 2581 .attr_update = ss_power_attr_update), 2582 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC, 2583 .attr_update = ss_bias_attr_update), 2584 AMDGPU_DEVICE_ATTR_RO(pm_metrics, ATTR_FLAG_BASIC, 2585 .attr_update = amdgpu_pm_metrics_attr_update), 2586 }; 2587 2588 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2589 uint32_t mask, enum amdgpu_device_attr_states *states) 2590 { 2591 struct device_attribute *dev_attr = &attr->dev_attr; 2592 enum amdgpu_device_attr_id attr_id = attr->attr_id; 2593 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 2594 2595 if (!(attr->flags & mask)) { 2596 *states = ATTR_STATE_UNSUPPORTED; 2597 return 0; 2598 } 2599 2600 if (DEVICE_ATTR_IS(mem_busy_percent)) { 2601 if ((adev->flags & AMD_IS_APU && 2602 gc_ver != IP_VERSION(9, 4, 3)) || 2603 gc_ver == IP_VERSION(9, 0, 1)) 2604 *states = ATTR_STATE_UNSUPPORTED; 2605 } else if (DEVICE_ATTR_IS(vcn_busy_percent)) { 2606 if (!(gc_ver == IP_VERSION(9, 3, 0) || 2607 gc_ver == IP_VERSION(10, 3, 1) || 2608 gc_ver == IP_VERSION(10, 3, 3) || 2609 gc_ver == IP_VERSION(10, 3, 6) || 2610 gc_ver == IP_VERSION(10, 3, 7) || 2611 gc_ver == IP_VERSION(11, 0, 0) || 2612 gc_ver == IP_VERSION(11, 0, 1) || 2613 gc_ver == IP_VERSION(11, 0, 2) || 2614 gc_ver == IP_VERSION(11, 0, 3) || 2615 gc_ver == IP_VERSION(11, 0, 4) || 2616 gc_ver == IP_VERSION(11, 5, 0) || 2617 gc_ver == IP_VERSION(11, 5, 1) || 2618 gc_ver == IP_VERSION(11, 5, 2) || 2619 gc_ver == IP_VERSION(11, 5, 3) || 2620 gc_ver == IP_VERSION(12, 0, 0) || 2621 gc_ver == IP_VERSION(12, 0, 1))) 2622 *states = ATTR_STATE_UNSUPPORTED; 2623 } else if (DEVICE_ATTR_IS(pcie_bw)) { 2624 /* PCIe Perf counters won't work on APU nodes */ 2625 if (adev->flags & AMD_IS_APU || 2626 !adev->asic_funcs->get_pcie_usage) 2627 *states = ATTR_STATE_UNSUPPORTED; 2628 } else if (DEVICE_ATTR_IS(unique_id)) { 2629 switch (gc_ver) { 2630 case IP_VERSION(9, 0, 1): 2631 case IP_VERSION(9, 4, 0): 2632 case IP_VERSION(9, 4, 1): 2633 case IP_VERSION(9, 4, 2): 2634 case IP_VERSION(9, 4, 3): 2635 case IP_VERSION(9, 4, 4): 2636 case IP_VERSION(9, 5, 0): 2637 case IP_VERSION(10, 3, 0): 2638 case IP_VERSION(11, 0, 0): 2639 case IP_VERSION(11, 0, 1): 2640 case IP_VERSION(11, 0, 2): 2641 case IP_VERSION(11, 0, 3): 2642 case IP_VERSION(12, 0, 0): 2643 case IP_VERSION(12, 0, 1): 2644 case IP_VERSION(12, 1, 0): 2645 *states = ATTR_STATE_SUPPORTED; 2646 break; 2647 default: 2648 *states = ATTR_STATE_UNSUPPORTED; 2649 } 2650 } else if (DEVICE_ATTR_IS(pp_features)) { 2651 if ((adev->flags & AMD_IS_APU && 2652 gc_ver != IP_VERSION(9, 4, 3)) || 2653 gc_ver < IP_VERSION(9, 0, 0)) 2654 *states = ATTR_STATE_UNSUPPORTED; 2655 } else if (DEVICE_ATTR_IS(gpu_metrics)) { 2656 if (gc_ver < IP_VERSION(9, 1, 0)) 2657 *states = ATTR_STATE_UNSUPPORTED; 2658 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) { 2659 if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP) 2660 *states = ATTR_STATE_UNSUPPORTED; 2661 else if ((gc_ver == IP_VERSION(10, 3, 0) || 2662 gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev)) 2663 *states = ATTR_STATE_UNSUPPORTED; 2664 } else if (DEVICE_ATTR_IS(pp_mclk_od)) { 2665 if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP) 2666 *states = ATTR_STATE_UNSUPPORTED; 2667 } else if (DEVICE_ATTR_IS(pp_sclk_od)) { 2668 if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP) 2669 *states = ATTR_STATE_UNSUPPORTED; 2670 } else if (DEVICE_ATTR_IS(apu_thermal_cap)) { 2671 u32 limit; 2672 2673 if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) == 2674 -EOPNOTSUPP) 2675 *states = ATTR_STATE_UNSUPPORTED; 2676 } else if (DEVICE_ATTR_IS(pp_table)) { 2677 int ret; 2678 char *tmp = NULL; 2679 2680 ret = amdgpu_dpm_get_pp_table(adev, &tmp); 2681 if (ret == -EOPNOTSUPP || !tmp) 2682 *states = ATTR_STATE_UNSUPPORTED; 2683 else 2684 *states = ATTR_STATE_SUPPORTED; 2685 } 2686 2687 switch (gc_ver) { 2688 case IP_VERSION(10, 3, 0): 2689 if (DEVICE_ATTR_IS(power_dpm_force_performance_level) && 2690 amdgpu_sriov_vf(adev)) { 2691 dev_attr->attr.mode &= ~0222; 2692 dev_attr->store = NULL; 2693 } 2694 break; 2695 default: 2696 break; 2697 } 2698 2699 return 0; 2700 } 2701 2702 2703 static int amdgpu_device_attr_create(struct amdgpu_device *adev, 2704 struct amdgpu_device_attr *attr, 2705 uint32_t mask, struct list_head *attr_list) 2706 { 2707 int ret = 0; 2708 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED; 2709 struct amdgpu_device_attr_entry *attr_entry; 2710 struct device_attribute *dev_attr; 2711 const char *name; 2712 2713 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2714 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update; 2715 2716 if (!attr) 2717 return -EINVAL; 2718 2719 dev_attr = &attr->dev_attr; 2720 name = dev_attr->attr.name; 2721 2722 attr_update = attr->attr_update ? attr->attr_update : default_attr_update; 2723 2724 ret = attr_update(adev, attr, mask, &attr_states); 2725 if (ret) { 2726 dev_err(adev->dev, "failed to update device file %s, ret = %d\n", 2727 name, ret); 2728 return ret; 2729 } 2730 2731 if (attr_states == ATTR_STATE_UNSUPPORTED) 2732 return 0; 2733 2734 ret = device_create_file(adev->dev, dev_attr); 2735 if (ret) { 2736 dev_err(adev->dev, "failed to create device file %s, ret = %d\n", 2737 name, ret); 2738 } 2739 2740 attr_entry = kmalloc_obj(*attr_entry); 2741 if (!attr_entry) 2742 return -ENOMEM; 2743 2744 attr_entry->attr = attr; 2745 INIT_LIST_HEAD(&attr_entry->entry); 2746 2747 list_add_tail(&attr_entry->entry, attr_list); 2748 2749 return ret; 2750 } 2751 2752 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr) 2753 { 2754 struct device_attribute *dev_attr = &attr->dev_attr; 2755 2756 device_remove_file(adev->dev, dev_attr); 2757 } 2758 2759 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2760 struct list_head *attr_list); 2761 2762 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev, 2763 struct amdgpu_device_attr *attrs, 2764 uint32_t counts, 2765 uint32_t mask, 2766 struct list_head *attr_list) 2767 { 2768 int ret = 0; 2769 uint32_t i = 0; 2770 2771 for (i = 0; i < counts; i++) { 2772 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list); 2773 if (ret) 2774 goto failed; 2775 } 2776 2777 return 0; 2778 2779 failed: 2780 amdgpu_device_attr_remove_groups(adev, attr_list); 2781 2782 return ret; 2783 } 2784 2785 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2786 struct list_head *attr_list) 2787 { 2788 struct amdgpu_device_attr_entry *entry, *entry_tmp; 2789 2790 if (list_empty(attr_list)) 2791 return ; 2792 2793 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) { 2794 amdgpu_device_attr_remove(adev, entry->attr); 2795 list_del(&entry->entry); 2796 kfree(entry); 2797 } 2798 } 2799 2800 static ssize_t amdgpu_hwmon_show_temp(struct device *dev, 2801 struct device_attribute *attr, 2802 char *buf) 2803 { 2804 struct amdgpu_device *adev = dev_get_drvdata(dev); 2805 int channel = to_sensor_dev_attr(attr)->index; 2806 int r, temp = 0; 2807 2808 if (channel >= PP_TEMP_MAX) 2809 return -EINVAL; 2810 2811 switch (channel) { 2812 case PP_TEMP_JUNCTION: 2813 /* get current junction temperature */ 2814 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP, 2815 (void *)&temp); 2816 break; 2817 case PP_TEMP_EDGE: 2818 /* get current edge temperature */ 2819 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP, 2820 (void *)&temp); 2821 break; 2822 case PP_TEMP_MEM: 2823 /* get current memory temperature */ 2824 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP, 2825 (void *)&temp); 2826 break; 2827 default: 2828 r = -EINVAL; 2829 break; 2830 } 2831 2832 if (r) 2833 return r; 2834 2835 return sysfs_emit(buf, "%d\n", temp); 2836 } 2837 2838 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev, 2839 struct device_attribute *attr, 2840 char *buf) 2841 { 2842 struct amdgpu_device *adev = dev_get_drvdata(dev); 2843 int hyst = to_sensor_dev_attr(attr)->index; 2844 int temp; 2845 2846 if (hyst) 2847 temp = adev->pm.dpm.thermal.min_temp; 2848 else 2849 temp = adev->pm.dpm.thermal.max_temp; 2850 2851 return sysfs_emit(buf, "%d\n", temp); 2852 } 2853 2854 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev, 2855 struct device_attribute *attr, 2856 char *buf) 2857 { 2858 struct amdgpu_device *adev = dev_get_drvdata(dev); 2859 int hyst = to_sensor_dev_attr(attr)->index; 2860 int temp; 2861 2862 if (hyst) 2863 temp = adev->pm.dpm.thermal.min_hotspot_temp; 2864 else 2865 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp; 2866 2867 return sysfs_emit(buf, "%d\n", temp); 2868 } 2869 2870 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev, 2871 struct device_attribute *attr, 2872 char *buf) 2873 { 2874 struct amdgpu_device *adev = dev_get_drvdata(dev); 2875 int hyst = to_sensor_dev_attr(attr)->index; 2876 int temp; 2877 2878 if (hyst) 2879 temp = adev->pm.dpm.thermal.min_mem_temp; 2880 else 2881 temp = adev->pm.dpm.thermal.max_mem_crit_temp; 2882 2883 return sysfs_emit(buf, "%d\n", temp); 2884 } 2885 2886 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev, 2887 struct device_attribute *attr, 2888 char *buf) 2889 { 2890 int channel = to_sensor_dev_attr(attr)->index; 2891 2892 if (channel >= PP_TEMP_MAX) 2893 return -EINVAL; 2894 2895 return sysfs_emit(buf, "%s\n", temp_label[channel].label); 2896 } 2897 2898 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev, 2899 struct device_attribute *attr, 2900 char *buf) 2901 { 2902 struct amdgpu_device *adev = dev_get_drvdata(dev); 2903 int channel = to_sensor_dev_attr(attr)->index; 2904 int temp = 0; 2905 2906 if (channel >= PP_TEMP_MAX) 2907 return -EINVAL; 2908 2909 switch (channel) { 2910 case PP_TEMP_JUNCTION: 2911 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp; 2912 break; 2913 case PP_TEMP_EDGE: 2914 temp = adev->pm.dpm.thermal.max_edge_emergency_temp; 2915 break; 2916 case PP_TEMP_MEM: 2917 temp = adev->pm.dpm.thermal.max_mem_emergency_temp; 2918 break; 2919 } 2920 2921 return sysfs_emit(buf, "%d\n", temp); 2922 } 2923 2924 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev, 2925 struct device_attribute *attr, 2926 char *buf) 2927 { 2928 struct amdgpu_device *adev = dev_get_drvdata(dev); 2929 u32 pwm_mode = 0; 2930 int ret; 2931 2932 ret = amdgpu_pm_get_access_if_active(adev); 2933 if (ret) 2934 return ret; 2935 2936 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2937 2938 amdgpu_pm_put_access(adev); 2939 2940 if (ret) 2941 return -EINVAL; 2942 2943 return sysfs_emit(buf, "%u\n", pwm_mode); 2944 } 2945 2946 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev, 2947 struct device_attribute *attr, 2948 const char *buf, 2949 size_t count) 2950 { 2951 struct amdgpu_device *adev = dev_get_drvdata(dev); 2952 int err, ret; 2953 u32 pwm_mode; 2954 int value; 2955 2956 err = kstrtoint(buf, 10, &value); 2957 if (err) 2958 return err; 2959 2960 if (value == 0) 2961 pwm_mode = AMD_FAN_CTRL_NONE; 2962 else if (value == 1) 2963 pwm_mode = AMD_FAN_CTRL_MANUAL; 2964 else if (value == 2) 2965 pwm_mode = AMD_FAN_CTRL_AUTO; 2966 else 2967 return -EINVAL; 2968 2969 ret = amdgpu_pm_get_access(adev); 2970 if (ret < 0) 2971 return ret; 2972 2973 ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode); 2974 2975 amdgpu_pm_put_access(adev); 2976 2977 if (ret) 2978 return -EINVAL; 2979 2980 return count; 2981 } 2982 2983 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev, 2984 struct device_attribute *attr, 2985 char *buf) 2986 { 2987 return sysfs_emit(buf, "%i\n", 0); 2988 } 2989 2990 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev, 2991 struct device_attribute *attr, 2992 char *buf) 2993 { 2994 return sysfs_emit(buf, "%i\n", 255); 2995 } 2996 2997 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev, 2998 struct device_attribute *attr, 2999 const char *buf, size_t count) 3000 { 3001 struct amdgpu_device *adev = dev_get_drvdata(dev); 3002 int err; 3003 u32 value; 3004 u32 pwm_mode; 3005 3006 err = kstrtou32(buf, 10, &value); 3007 if (err) 3008 return err; 3009 3010 err = amdgpu_pm_get_access(adev); 3011 if (err < 0) 3012 return err; 3013 3014 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 3015 if (err) 3016 goto out; 3017 3018 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 3019 pr_info("manual fan speed control should be enabled first\n"); 3020 err = -EINVAL; 3021 goto out; 3022 } 3023 3024 err = amdgpu_dpm_set_fan_speed_pwm(adev, value); 3025 3026 out: 3027 amdgpu_pm_put_access(adev); 3028 3029 if (err) 3030 return err; 3031 3032 return count; 3033 } 3034 3035 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev, 3036 struct device_attribute *attr, 3037 char *buf) 3038 { 3039 struct amdgpu_device *adev = dev_get_drvdata(dev); 3040 int err; 3041 u32 speed = 0; 3042 3043 err = amdgpu_pm_get_access_if_active(adev); 3044 if (err) 3045 return err; 3046 3047 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed); 3048 3049 amdgpu_pm_put_access(adev); 3050 3051 if (err) 3052 return err; 3053 3054 return sysfs_emit(buf, "%i\n", speed); 3055 } 3056 3057 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev, 3058 struct device_attribute *attr, 3059 char *buf) 3060 { 3061 struct amdgpu_device *adev = dev_get_drvdata(dev); 3062 int err; 3063 u32 speed = 0; 3064 3065 err = amdgpu_pm_get_access_if_active(adev); 3066 if (err) 3067 return err; 3068 3069 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed); 3070 3071 amdgpu_pm_put_access(adev); 3072 3073 if (err) 3074 return err; 3075 3076 return sysfs_emit(buf, "%i\n", speed); 3077 } 3078 3079 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev, 3080 struct device_attribute *attr, 3081 char *buf) 3082 { 3083 struct amdgpu_device *adev = dev_get_drvdata(dev); 3084 u32 min_rpm = 0; 3085 int r; 3086 3087 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM, 3088 (void *)&min_rpm); 3089 3090 if (r) 3091 return r; 3092 3093 return sysfs_emit(buf, "%d\n", min_rpm); 3094 } 3095 3096 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev, 3097 struct device_attribute *attr, 3098 char *buf) 3099 { 3100 struct amdgpu_device *adev = dev_get_drvdata(dev); 3101 u32 max_rpm = 0; 3102 int r; 3103 3104 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM, 3105 (void *)&max_rpm); 3106 3107 if (r) 3108 return r; 3109 3110 return sysfs_emit(buf, "%d\n", max_rpm); 3111 } 3112 3113 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev, 3114 struct device_attribute *attr, 3115 char *buf) 3116 { 3117 struct amdgpu_device *adev = dev_get_drvdata(dev); 3118 int err; 3119 u32 rpm = 0; 3120 3121 err = amdgpu_pm_get_access_if_active(adev); 3122 if (err) 3123 return err; 3124 3125 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm); 3126 3127 amdgpu_pm_put_access(adev); 3128 3129 if (err) 3130 return err; 3131 3132 return sysfs_emit(buf, "%i\n", rpm); 3133 } 3134 3135 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev, 3136 struct device_attribute *attr, 3137 const char *buf, size_t count) 3138 { 3139 struct amdgpu_device *adev = dev_get_drvdata(dev); 3140 int err; 3141 u32 value; 3142 u32 pwm_mode; 3143 3144 err = kstrtou32(buf, 10, &value); 3145 if (err) 3146 return err; 3147 3148 err = amdgpu_pm_get_access(adev); 3149 if (err < 0) 3150 return err; 3151 3152 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 3153 if (err) 3154 goto out; 3155 3156 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 3157 err = -ENODATA; 3158 goto out; 3159 } 3160 3161 err = amdgpu_dpm_set_fan_speed_rpm(adev, value); 3162 3163 out: 3164 amdgpu_pm_put_access(adev); 3165 3166 if (err) 3167 return err; 3168 3169 return count; 3170 } 3171 3172 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev, 3173 struct device_attribute *attr, 3174 char *buf) 3175 { 3176 struct amdgpu_device *adev = dev_get_drvdata(dev); 3177 u32 pwm_mode = 0; 3178 int ret; 3179 3180 ret = amdgpu_pm_get_access_if_active(adev); 3181 if (ret) 3182 return ret; 3183 3184 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 3185 3186 amdgpu_pm_put_access(adev); 3187 3188 if (ret) 3189 return -EINVAL; 3190 3191 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1); 3192 } 3193 3194 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev, 3195 struct device_attribute *attr, 3196 const char *buf, 3197 size_t count) 3198 { 3199 struct amdgpu_device *adev = dev_get_drvdata(dev); 3200 int err; 3201 int value; 3202 u32 pwm_mode; 3203 3204 err = kstrtoint(buf, 10, &value); 3205 if (err) 3206 return err; 3207 3208 if (value == 0) 3209 pwm_mode = AMD_FAN_CTRL_AUTO; 3210 else if (value == 1) 3211 pwm_mode = AMD_FAN_CTRL_MANUAL; 3212 else 3213 return -EINVAL; 3214 3215 err = amdgpu_pm_get_access(adev); 3216 if (err < 0) 3217 return err; 3218 3219 err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode); 3220 3221 amdgpu_pm_put_access(adev); 3222 3223 if (err) 3224 return -EINVAL; 3225 3226 return count; 3227 } 3228 3229 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev, 3230 struct device_attribute *attr, 3231 char *buf) 3232 { 3233 struct amdgpu_device *adev = dev_get_drvdata(dev); 3234 u32 vddgfx; 3235 int r; 3236 3237 /* get the voltage */ 3238 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX, 3239 (void *)&vddgfx); 3240 if (r) 3241 return r; 3242 3243 return sysfs_emit(buf, "%d\n", vddgfx); 3244 } 3245 3246 static ssize_t amdgpu_hwmon_show_vddboard(struct device *dev, 3247 struct device_attribute *attr, 3248 char *buf) 3249 { 3250 struct amdgpu_device *adev = dev_get_drvdata(dev); 3251 u32 vddboard; 3252 int r; 3253 3254 /* get the voltage */ 3255 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD, 3256 (void *)&vddboard); 3257 if (r) 3258 return r; 3259 3260 return sysfs_emit(buf, "%d\n", vddboard); 3261 } 3262 3263 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev, 3264 struct device_attribute *attr, 3265 char *buf) 3266 { 3267 return sysfs_emit(buf, "vddgfx\n"); 3268 } 3269 3270 static ssize_t amdgpu_hwmon_show_vddboard_label(struct device *dev, 3271 struct device_attribute *attr, 3272 char *buf) 3273 { 3274 return sysfs_emit(buf, "vddboard\n"); 3275 } 3276 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev, 3277 struct device_attribute *attr, 3278 char *buf) 3279 { 3280 struct amdgpu_device *adev = dev_get_drvdata(dev); 3281 u32 vddnb; 3282 int r; 3283 3284 /* only APUs have vddnb */ 3285 if (!(adev->flags & AMD_IS_APU)) 3286 return -EINVAL; 3287 3288 /* get the voltage */ 3289 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB, 3290 (void *)&vddnb); 3291 if (r) 3292 return r; 3293 3294 return sysfs_emit(buf, "%d\n", vddnb); 3295 } 3296 3297 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev, 3298 struct device_attribute *attr, 3299 char *buf) 3300 { 3301 return sysfs_emit(buf, "vddnb\n"); 3302 } 3303 3304 static int amdgpu_hwmon_get_power(struct device *dev, 3305 enum amd_pp_sensors sensor) 3306 { 3307 struct amdgpu_device *adev = dev_get_drvdata(dev); 3308 unsigned int uw; 3309 u32 query = 0; 3310 int r; 3311 3312 r = amdgpu_pm_get_sensor_generic(adev, sensor, (void *)&query); 3313 if (r) 3314 return r; 3315 3316 /* convert to microwatts */ 3317 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000; 3318 3319 return uw; 3320 } 3321 3322 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev, 3323 struct device_attribute *attr, 3324 char *buf) 3325 { 3326 ssize_t val; 3327 3328 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER); 3329 if (val < 0) 3330 return val; 3331 3332 return sysfs_emit(buf, "%zd\n", val); 3333 } 3334 3335 static ssize_t amdgpu_hwmon_show_power_input(struct device *dev, 3336 struct device_attribute *attr, 3337 char *buf) 3338 { 3339 ssize_t val; 3340 3341 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER); 3342 if (val < 0) 3343 return val; 3344 3345 return sysfs_emit(buf, "%zd\n", val); 3346 } 3347 3348 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev, 3349 struct device_attribute *attr, 3350 char *buf, 3351 enum pp_power_limit_level pp_limit_level) 3352 { 3353 struct amdgpu_device *adev = dev_get_drvdata(dev); 3354 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index; 3355 uint32_t limit; 3356 ssize_t size; 3357 int r; 3358 3359 r = amdgpu_pm_get_access_if_active(adev); 3360 if (r) 3361 return r; 3362 3363 r = amdgpu_dpm_get_power_limit(adev, &limit, 3364 pp_limit_level, power_type); 3365 3366 if (!r) 3367 size = sysfs_emit(buf, "%u\n", limit * 1000000); 3368 else 3369 size = sysfs_emit(buf, "\n"); 3370 3371 amdgpu_pm_put_access(adev); 3372 3373 return size; 3374 } 3375 3376 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev, 3377 struct device_attribute *attr, 3378 char *buf) 3379 { 3380 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN); 3381 } 3382 3383 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev, 3384 struct device_attribute *attr, 3385 char *buf) 3386 { 3387 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX); 3388 3389 } 3390 3391 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev, 3392 struct device_attribute *attr, 3393 char *buf) 3394 { 3395 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT); 3396 3397 } 3398 3399 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev, 3400 struct device_attribute *attr, 3401 char *buf) 3402 { 3403 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT); 3404 3405 } 3406 3407 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev, 3408 struct device_attribute *attr, 3409 char *buf) 3410 { 3411 struct amdgpu_device *adev = dev_get_drvdata(dev); 3412 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 3413 3414 if (gc_ver == IP_VERSION(10, 3, 1)) 3415 return sysfs_emit(buf, "%s\n", 3416 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ? 3417 "fastPPT" : "slowPPT"); 3418 else 3419 return sysfs_emit(buf, "%s\n", 3420 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ? 3421 "PPT1" : "PPT"); 3422 } 3423 3424 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev, 3425 struct device_attribute *attr, 3426 const char *buf, 3427 size_t count) 3428 { 3429 struct amdgpu_device *adev = dev_get_drvdata(dev); 3430 int limit_type = to_sensor_dev_attr(attr)->index; 3431 int err; 3432 u32 value; 3433 3434 err = kstrtou32(buf, 10, &value); 3435 if (err) 3436 return err; 3437 3438 value = value / 1000000; /* convert to Watt */ 3439 3440 err = amdgpu_pm_get_access(adev); 3441 if (err < 0) 3442 return err; 3443 3444 err = amdgpu_dpm_set_power_limit(adev, limit_type, value); 3445 3446 amdgpu_pm_put_access(adev); 3447 3448 if (err) 3449 return err; 3450 3451 return count; 3452 } 3453 3454 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev, 3455 struct device_attribute *attr, 3456 char *buf) 3457 { 3458 struct amdgpu_device *adev = dev_get_drvdata(dev); 3459 uint32_t sclk; 3460 int r; 3461 3462 /* get the sclk */ 3463 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK, 3464 (void *)&sclk); 3465 if (r) 3466 return r; 3467 3468 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000); 3469 } 3470 3471 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev, 3472 struct device_attribute *attr, 3473 char *buf) 3474 { 3475 return sysfs_emit(buf, "sclk\n"); 3476 } 3477 3478 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev, 3479 struct device_attribute *attr, 3480 char *buf) 3481 { 3482 struct amdgpu_device *adev = dev_get_drvdata(dev); 3483 uint32_t mclk; 3484 int r; 3485 3486 /* get the sclk */ 3487 r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK, 3488 (void *)&mclk); 3489 if (r) 3490 return r; 3491 3492 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000); 3493 } 3494 3495 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev, 3496 struct device_attribute *attr, 3497 char *buf) 3498 { 3499 return sysfs_emit(buf, "mclk\n"); 3500 } 3501 3502 /** 3503 * DOC: hwmon 3504 * 3505 * The amdgpu driver exposes the following sensor interfaces: 3506 * 3507 * - GPU temperature (via the on-die sensor) 3508 * 3509 * - GPU voltage 3510 * 3511 * - Northbridge voltage (APUs only) 3512 * 3513 * - GPU power 3514 * 3515 * - GPU fan 3516 * 3517 * - GPU gfx/compute engine clock 3518 * 3519 * - GPU memory clock (dGPU only) 3520 * 3521 * hwmon interfaces for GPU temperature: 3522 * 3523 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius 3524 * - temp2_input and temp3_input are supported on SOC15 dGPUs only 3525 * 3526 * - temp[1-3]_label: temperature channel label 3527 * - temp2_label and temp3_label are supported on SOC15 dGPUs only 3528 * 3529 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius 3530 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only 3531 * 3532 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius 3533 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only 3534 * 3535 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius 3536 * - these are supported on SOC15 dGPUs only 3537 * 3538 * hwmon interfaces for GPU voltage: 3539 * 3540 * - in0_input: the voltage on the GPU in millivolts 3541 * 3542 * - in1_input: the voltage on the Northbridge in millivolts 3543 * 3544 * hwmon interfaces for GPU power: 3545 * 3546 * - power1_average: average power used by the SoC in microWatts. On APUs this includes the CPU. 3547 * 3548 * - power1_input: instantaneous power used by the SoC in microWatts. On APUs this includes the CPU. 3549 * 3550 * - power1_cap_min: minimum cap supported in microWatts 3551 * 3552 * - power1_cap_max: maximum cap supported in microWatts 3553 * 3554 * - power1_cap: selected power cap in microWatts 3555 * 3556 * hwmon interfaces for GPU fan: 3557 * 3558 * - pwm1: pulse width modulation fan level (0-255) 3559 * 3560 * - 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) 3561 * 3562 * - pwm1_min: pulse width modulation fan control minimum level (0) 3563 * 3564 * - pwm1_max: pulse width modulation fan control maximum level (255) 3565 * 3566 * - fan1_min: a minimum value Unit: revolution/min (RPM) 3567 * 3568 * - fan1_max: a maximum value Unit: revolution/max (RPM) 3569 * 3570 * - fan1_input: fan speed in RPM 3571 * 3572 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM) 3573 * 3574 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable 3575 * 3576 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time. 3577 * That will get the former one overridden. 3578 * 3579 * hwmon interfaces for GPU clocks: 3580 * 3581 * - freq1_input: the gfx/compute clock in hertz 3582 * 3583 * - freq2_input: the memory clock in hertz 3584 * 3585 * You can use hwmon tools like sensors to view this information on your system. 3586 * 3587 */ 3588 3589 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE); 3590 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0); 3591 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1); 3592 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE); 3593 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION); 3594 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0); 3595 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1); 3596 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION); 3597 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM); 3598 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0); 3599 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1); 3600 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM); 3601 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE); 3602 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION); 3603 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM); 3604 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0); 3605 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0); 3606 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0); 3607 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0); 3608 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0); 3609 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0); 3610 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0); 3611 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0); 3612 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0); 3613 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0); 3614 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0); 3615 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0); 3616 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0); 3617 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, amdgpu_hwmon_show_vddboard, NULL, 0); 3618 static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, amdgpu_hwmon_show_vddboard_label, NULL, 0); 3619 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0); 3620 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0); 3621 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0); 3622 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0); 3623 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0); 3624 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0); 3625 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0); 3626 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1); 3627 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1); 3628 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1); 3629 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1); 3630 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1); 3631 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0); 3632 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0); 3633 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0); 3634 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0); 3635 3636 static struct attribute *hwmon_attributes[] = { 3637 &sensor_dev_attr_temp1_input.dev_attr.attr, 3638 &sensor_dev_attr_temp1_crit.dev_attr.attr, 3639 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 3640 &sensor_dev_attr_temp2_input.dev_attr.attr, 3641 &sensor_dev_attr_temp2_crit.dev_attr.attr, 3642 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 3643 &sensor_dev_attr_temp3_input.dev_attr.attr, 3644 &sensor_dev_attr_temp3_crit.dev_attr.attr, 3645 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 3646 &sensor_dev_attr_temp1_emergency.dev_attr.attr, 3647 &sensor_dev_attr_temp2_emergency.dev_attr.attr, 3648 &sensor_dev_attr_temp3_emergency.dev_attr.attr, 3649 &sensor_dev_attr_temp1_label.dev_attr.attr, 3650 &sensor_dev_attr_temp2_label.dev_attr.attr, 3651 &sensor_dev_attr_temp3_label.dev_attr.attr, 3652 &sensor_dev_attr_pwm1.dev_attr.attr, 3653 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 3654 &sensor_dev_attr_pwm1_min.dev_attr.attr, 3655 &sensor_dev_attr_pwm1_max.dev_attr.attr, 3656 &sensor_dev_attr_fan1_input.dev_attr.attr, 3657 &sensor_dev_attr_fan1_min.dev_attr.attr, 3658 &sensor_dev_attr_fan1_max.dev_attr.attr, 3659 &sensor_dev_attr_fan1_target.dev_attr.attr, 3660 &sensor_dev_attr_fan1_enable.dev_attr.attr, 3661 &sensor_dev_attr_in0_input.dev_attr.attr, 3662 &sensor_dev_attr_in0_label.dev_attr.attr, 3663 &sensor_dev_attr_in1_input.dev_attr.attr, 3664 &sensor_dev_attr_in1_label.dev_attr.attr, 3665 &sensor_dev_attr_in2_input.dev_attr.attr, 3666 &sensor_dev_attr_in2_label.dev_attr.attr, 3667 &sensor_dev_attr_power1_average.dev_attr.attr, 3668 &sensor_dev_attr_power1_input.dev_attr.attr, 3669 &sensor_dev_attr_power1_cap_max.dev_attr.attr, 3670 &sensor_dev_attr_power1_cap_min.dev_attr.attr, 3671 &sensor_dev_attr_power1_cap.dev_attr.attr, 3672 &sensor_dev_attr_power1_cap_default.dev_attr.attr, 3673 &sensor_dev_attr_power1_label.dev_attr.attr, 3674 &sensor_dev_attr_power2_cap_max.dev_attr.attr, 3675 &sensor_dev_attr_power2_cap_min.dev_attr.attr, 3676 &sensor_dev_attr_power2_cap.dev_attr.attr, 3677 &sensor_dev_attr_power2_cap_default.dev_attr.attr, 3678 &sensor_dev_attr_power2_label.dev_attr.attr, 3679 &sensor_dev_attr_freq1_input.dev_attr.attr, 3680 &sensor_dev_attr_freq1_label.dev_attr.attr, 3681 &sensor_dev_attr_freq2_input.dev_attr.attr, 3682 &sensor_dev_attr_freq2_label.dev_attr.attr, 3683 NULL 3684 }; 3685 3686 static umode_t hwmon_attributes_visible(struct kobject *kobj, 3687 struct attribute *attr, int index) 3688 { 3689 struct device *dev = kobj_to_dev(kobj); 3690 struct amdgpu_device *adev = dev_get_drvdata(dev); 3691 umode_t effective_mode = attr->mode; 3692 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 3693 uint32_t tmp; 3694 3695 /* under pp one vf mode manage of hwmon attributes is not supported */ 3696 if (amdgpu_sriov_is_pp_one_vf(adev)) 3697 effective_mode &= ~S_IWUSR; 3698 3699 /* Skip fan attributes if fan is not present */ 3700 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3701 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3702 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3703 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3704 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3705 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3706 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3707 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3708 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3709 return 0; 3710 3711 /* Skip fan attributes on APU */ 3712 if ((adev->flags & AMD_IS_APU) && 3713 (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3714 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3715 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3716 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3717 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3718 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3719 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3720 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3721 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3722 return 0; 3723 3724 /* Skip crit temp on APU */ 3725 if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) || 3726 amdgpu_is_multi_aid(adev)) && 3727 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3728 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr)) 3729 return 0; 3730 3731 /* Skip limit attributes if DPM is not enabled */ 3732 if (!adev->pm.dpm_enabled && 3733 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3734 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr || 3735 attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3736 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3737 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3738 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3739 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3740 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3741 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3742 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3743 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3744 return 0; 3745 3746 /* mask fan attributes if we have no bindings for this asic to expose */ 3747 if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) && 3748 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */ 3749 ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) && 3750 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */ 3751 effective_mode &= ~S_IRUGO; 3752 3753 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) && 3754 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */ 3755 ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) && 3756 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */ 3757 effective_mode &= ~S_IWUSR; 3758 3759 /* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */ 3760 if (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr || 3761 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr || 3762 attr == &sensor_dev_attr_power1_cap.dev_attr.attr || 3763 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr) { 3764 if (adev->family == AMDGPU_FAMILY_SI || 3765 ((adev->flags & AMD_IS_APU) && gc_ver != IP_VERSION(10, 3, 1) && 3766 (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4))) || 3767 (amdgpu_sriov_vf(adev) && gc_ver == IP_VERSION(11, 0, 3))) 3768 return 0; 3769 } 3770 3771 if (attr == &sensor_dev_attr_power1_cap.dev_attr.attr && 3772 amdgpu_virt_cap_is_rw(&adev->virt.virt_caps, AMDGPU_VIRT_CAP_POWER_LIMIT)) 3773 effective_mode |= S_IWUSR; 3774 3775 /* not implemented yet for APUs having < GC 9.3.0 (Renoir) */ 3776 if (((adev->family == AMDGPU_FAMILY_SI) || 3777 ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) && 3778 (attr == &sensor_dev_attr_power1_average.dev_attr.attr)) 3779 return 0; 3780 3781 /* not all products support both average and instantaneous */ 3782 if (attr == &sensor_dev_attr_power1_average.dev_attr.attr && 3783 amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, 3784 (void *)&tmp) == -EOPNOTSUPP) 3785 return 0; 3786 if (attr == &sensor_dev_attr_power1_input.dev_attr.attr && 3787 amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, 3788 (void *)&tmp) == -EOPNOTSUPP) 3789 return 0; 3790 3791 /* hide max/min values if we can't both query and manage the fan */ 3792 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) && 3793 (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) && 3794 (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) && 3795 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) && 3796 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3797 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr)) 3798 return 0; 3799 3800 if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) && 3801 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) && 3802 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3803 attr == &sensor_dev_attr_fan1_min.dev_attr.attr)) 3804 return 0; 3805 3806 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */ 3807 adev->family == AMDGPU_FAMILY_KV || /* not implemented yet */ 3808 amdgpu_is_multi_aid(adev)) && 3809 (attr == &sensor_dev_attr_in0_input.dev_attr.attr || 3810 attr == &sensor_dev_attr_in0_label.dev_attr.attr)) 3811 return 0; 3812 3813 /* only APUs other than gc 9,4,3 have vddnb */ 3814 if ((!(adev->flags & AMD_IS_APU) || 3815 amdgpu_is_multi_aid(adev)) && 3816 (attr == &sensor_dev_attr_in1_input.dev_attr.attr || 3817 attr == &sensor_dev_attr_in1_label.dev_attr.attr)) 3818 return 0; 3819 3820 /* only few boards support vddboard */ 3821 if ((attr == &sensor_dev_attr_in2_input.dev_attr.attr || 3822 attr == &sensor_dev_attr_in2_label.dev_attr.attr) && 3823 amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD, 3824 (void *)&tmp) == -EOPNOTSUPP) 3825 return 0; 3826 3827 /* no mclk on APUs other than gc 9,4,3*/ 3828 if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) && 3829 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr || 3830 attr == &sensor_dev_attr_freq2_label.dev_attr.attr)) 3831 return 0; 3832 3833 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) && 3834 (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)) && 3835 (attr == &sensor_dev_attr_temp2_input.dev_attr.attr || 3836 attr == &sensor_dev_attr_temp2_label.dev_attr.attr || 3837 attr == &sensor_dev_attr_temp2_crit.dev_attr.attr || 3838 attr == &sensor_dev_attr_temp3_input.dev_attr.attr || 3839 attr == &sensor_dev_attr_temp3_label.dev_attr.attr || 3840 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr)) 3841 return 0; 3842 3843 /* hotspot temperature for gc 9,4,3*/ 3844 if (amdgpu_is_multi_aid(adev)) { 3845 if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr || 3846 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr || 3847 attr == &sensor_dev_attr_temp1_label.dev_attr.attr) 3848 return 0; 3849 3850 if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr || 3851 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr) 3852 return attr->mode; 3853 } 3854 3855 /* only SOC15 dGPUs support hotspot and mem temperatures */ 3856 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) && 3857 (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr || 3858 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr || 3859 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr || 3860 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr || 3861 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)) 3862 return 0; 3863 3864 /* only a few GPUs have fast PPT limit and power labels */ 3865 if ((attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr || 3866 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr || 3867 attr == &sensor_dev_attr_power2_cap.dev_attr.attr || 3868 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr || 3869 attr == &sensor_dev_attr_power2_label.dev_attr.attr) && 3870 (amdgpu_dpm_get_power_limit(adev, &tmp, 3871 PP_PWR_LIMIT_MAX, 3872 PP_PWR_TYPE_FAST) == -EOPNOTSUPP)) 3873 return 0; 3874 3875 return effective_mode; 3876 } 3877 3878 static const struct attribute_group hwmon_attrgroup = { 3879 .attrs = hwmon_attributes, 3880 .is_visible = hwmon_attributes_visible, 3881 }; 3882 3883 static const struct attribute_group *hwmon_groups[] = { 3884 &hwmon_attrgroup, 3885 NULL 3886 }; 3887 3888 static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev, 3889 enum pp_clock_type od_type, 3890 char *buf) 3891 { 3892 int size = 0; 3893 int ret; 3894 3895 ret = amdgpu_pm_get_access_if_active(adev); 3896 if (ret) 3897 return ret; 3898 3899 ret = amdgpu_dpm_emit_clock_levels(adev, od_type, buf, &size); 3900 if (ret) 3901 return ret; 3902 if (size == 0) 3903 size = sysfs_emit(buf, "\n"); 3904 3905 amdgpu_pm_put_access(adev); 3906 3907 return size; 3908 } 3909 3910 static int parse_input_od_command_lines(const char *buf, 3911 size_t count, 3912 u32 *type, 3913 long *params, 3914 uint32_t *num_of_params) 3915 { 3916 const char delimiter[3] = {' ', '\n', '\0'}; 3917 uint32_t parameter_size = 0; 3918 char buf_cpy[128] = {0}; 3919 char *tmp_str, *sub_str; 3920 int ret; 3921 3922 if (count > sizeof(buf_cpy) - 1) 3923 return -EINVAL; 3924 3925 memcpy(buf_cpy, buf, count); 3926 tmp_str = buf_cpy; 3927 3928 /* skip heading spaces */ 3929 while (isspace(*tmp_str)) 3930 tmp_str++; 3931 3932 switch (*tmp_str) { 3933 case 'c': 3934 *type = PP_OD_COMMIT_DPM_TABLE; 3935 return 0; 3936 case 'r': 3937 params[parameter_size] = *type; 3938 *num_of_params = 1; 3939 *type = PP_OD_RESTORE_DEFAULT_TABLE; 3940 return 0; 3941 default: 3942 break; 3943 } 3944 3945 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) { 3946 if (strlen(sub_str) == 0) 3947 continue; 3948 3949 ret = kstrtol(sub_str, 0, ¶ms[parameter_size]); 3950 if (ret) 3951 return -EINVAL; 3952 parameter_size++; 3953 3954 if (!tmp_str) 3955 break; 3956 3957 while (isspace(*tmp_str)) 3958 tmp_str++; 3959 } 3960 3961 *num_of_params = parameter_size; 3962 3963 return 0; 3964 } 3965 3966 static int 3967 amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev, 3968 enum PP_OD_DPM_TABLE_COMMAND cmd_type, 3969 const char *in_buf, 3970 size_t count) 3971 { 3972 uint32_t parameter_size = 0; 3973 long parameter[64]; 3974 int ret; 3975 3976 ret = parse_input_od_command_lines(in_buf, 3977 count, 3978 &cmd_type, 3979 parameter, 3980 ¶meter_size); 3981 if (ret) 3982 return ret; 3983 3984 ret = amdgpu_pm_get_access(adev); 3985 if (ret < 0) 3986 return ret; 3987 3988 ret = amdgpu_dpm_odn_edit_dpm_table(adev, 3989 cmd_type, 3990 parameter, 3991 parameter_size); 3992 if (ret) 3993 goto err_out; 3994 3995 if (cmd_type == PP_OD_COMMIT_DPM_TABLE) { 3996 ret = amdgpu_dpm_dispatch_task(adev, 3997 AMD_PP_TASK_READJUST_POWER_STATE, 3998 NULL); 3999 if (ret) 4000 goto err_out; 4001 } 4002 4003 amdgpu_pm_put_access(adev); 4004 4005 return count; 4006 4007 err_out: 4008 amdgpu_pm_put_access(adev); 4009 4010 return ret; 4011 } 4012 4013 /** 4014 * DOC: fan_curve 4015 * 4016 * The amdgpu driver provides a sysfs API for checking and adjusting the fan 4017 * control curve line. 4018 * 4019 * Reading back the file shows you the current settings(temperature in Celsius 4020 * degree and fan speed in pwm) applied to every anchor point of the curve line 4021 * and their permitted ranges if changable. 4022 * 4023 * Writing a desired string(with the format like "anchor_point_index temperature 4024 * fan_speed_in_pwm") to the file, change the settings for the specific anchor 4025 * point accordingly. 4026 * 4027 * When you have finished the editing, write "c" (commit) to the file to commit 4028 * your changes. 4029 * 4030 * If you want to reset to the default value, write "r" (reset) to the file to 4031 * reset them 4032 * 4033 * There are two fan control modes supported: auto and manual. With auto mode, 4034 * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature). 4035 * While with manual mode, users can set their own fan curve line as what 4036 * described here. Normally the ASIC is booted up with auto mode. Any 4037 * settings via this interface will switch the fan control to manual mode 4038 * implicitly. 4039 */ 4040 static ssize_t fan_curve_show(struct kobject *kobj, 4041 struct kobj_attribute *attr, 4042 char *buf) 4043 { 4044 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4045 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4046 4047 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf); 4048 } 4049 4050 static ssize_t fan_curve_store(struct kobject *kobj, 4051 struct kobj_attribute *attr, 4052 const char *buf, 4053 size_t count) 4054 { 4055 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4056 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4057 4058 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4059 PP_OD_EDIT_FAN_CURVE, 4060 buf, 4061 count); 4062 } 4063 4064 static umode_t fan_curve_visible(struct amdgpu_device *adev) 4065 { 4066 umode_t umode = 0000; 4067 4068 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE) 4069 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4070 4071 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET) 4072 umode |= S_IWUSR; 4073 4074 return umode; 4075 } 4076 4077 /** 4078 * DOC: acoustic_limit_rpm_threshold 4079 * 4080 * The amdgpu driver provides a sysfs API for checking and adjusting the 4081 * acoustic limit in RPM for fan control. 4082 * 4083 * Reading back the file shows you the current setting and the permitted 4084 * ranges if changable. 4085 * 4086 * Writing an integer to the file, change the setting accordingly. 4087 * 4088 * When you have finished the editing, write "c" (commit) to the file to commit 4089 * your changes. 4090 * 4091 * If you want to reset to the default value, write "r" (reset) to the file to 4092 * reset them 4093 * 4094 * This setting works under auto fan control mode only. It adjusts the PMFW's 4095 * behavior about the maximum speed in RPM the fan can spin. Setting via this 4096 * interface will switch the fan control to auto mode implicitly. 4097 */ 4098 static ssize_t acoustic_limit_threshold_show(struct kobject *kobj, 4099 struct kobj_attribute *attr, 4100 char *buf) 4101 { 4102 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4103 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4104 4105 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf); 4106 } 4107 4108 static ssize_t acoustic_limit_threshold_store(struct kobject *kobj, 4109 struct kobj_attribute *attr, 4110 const char *buf, 4111 size_t count) 4112 { 4113 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4114 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4115 4116 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4117 PP_OD_EDIT_ACOUSTIC_LIMIT, 4118 buf, 4119 count); 4120 } 4121 4122 static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev) 4123 { 4124 umode_t umode = 0000; 4125 4126 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE) 4127 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4128 4129 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET) 4130 umode |= S_IWUSR; 4131 4132 return umode; 4133 } 4134 4135 /** 4136 * DOC: acoustic_target_rpm_threshold 4137 * 4138 * The amdgpu driver provides a sysfs API for checking and adjusting the 4139 * acoustic target in RPM for fan control. 4140 * 4141 * Reading back the file shows you the current setting and the permitted 4142 * ranges if changable. 4143 * 4144 * Writing an integer to the file, change the setting accordingly. 4145 * 4146 * When you have finished the editing, write "c" (commit) to the file to commit 4147 * your changes. 4148 * 4149 * If you want to reset to the default value, write "r" (reset) to the file to 4150 * reset them 4151 * 4152 * This setting works under auto fan control mode only. It can co-exist with 4153 * other settings which can work also under auto mode. It adjusts the PMFW's 4154 * behavior about the maximum speed in RPM the fan can spin when ASIC 4155 * temperature is not greater than target temperature. Setting via this 4156 * interface will switch the fan control to auto mode implicitly. 4157 */ 4158 static ssize_t acoustic_target_threshold_show(struct kobject *kobj, 4159 struct kobj_attribute *attr, 4160 char *buf) 4161 { 4162 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4163 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4164 4165 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf); 4166 } 4167 4168 static ssize_t acoustic_target_threshold_store(struct kobject *kobj, 4169 struct kobj_attribute *attr, 4170 const char *buf, 4171 size_t count) 4172 { 4173 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4174 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4175 4176 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4177 PP_OD_EDIT_ACOUSTIC_TARGET, 4178 buf, 4179 count); 4180 } 4181 4182 static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev) 4183 { 4184 umode_t umode = 0000; 4185 4186 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE) 4187 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4188 4189 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET) 4190 umode |= S_IWUSR; 4191 4192 return umode; 4193 } 4194 4195 /** 4196 * DOC: fan_target_temperature 4197 * 4198 * The amdgpu driver provides a sysfs API for checking and adjusting the 4199 * target tempeature in Celsius degree for fan control. 4200 * 4201 * Reading back the file shows you the current setting and the permitted 4202 * ranges if changable. 4203 * 4204 * Writing an integer to the file, change the setting accordingly. 4205 * 4206 * When you have finished the editing, write "c" (commit) to the file to commit 4207 * your changes. 4208 * 4209 * If you want to reset to the default value, write "r" (reset) to the file to 4210 * reset them 4211 * 4212 * This setting works under auto fan control mode only. It can co-exist with 4213 * other settings which can work also under auto mode. Paring with the 4214 * acoustic_target_rpm_threshold setting, they define the maximum speed in 4215 * RPM the fan can spin when ASIC temperature is not greater than target 4216 * temperature. Setting via this interface will switch the fan control to 4217 * auto mode implicitly. 4218 */ 4219 static ssize_t fan_target_temperature_show(struct kobject *kobj, 4220 struct kobj_attribute *attr, 4221 char *buf) 4222 { 4223 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4224 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4225 4226 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf); 4227 } 4228 4229 static ssize_t fan_target_temperature_store(struct kobject *kobj, 4230 struct kobj_attribute *attr, 4231 const char *buf, 4232 size_t count) 4233 { 4234 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4235 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4236 4237 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4238 PP_OD_EDIT_FAN_TARGET_TEMPERATURE, 4239 buf, 4240 count); 4241 } 4242 4243 static umode_t fan_target_temperature_visible(struct amdgpu_device *adev) 4244 { 4245 umode_t umode = 0000; 4246 4247 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE) 4248 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4249 4250 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET) 4251 umode |= S_IWUSR; 4252 4253 return umode; 4254 } 4255 4256 /** 4257 * DOC: fan_minimum_pwm 4258 * 4259 * The amdgpu driver provides a sysfs API for checking and adjusting the 4260 * minimum fan speed in PWM. 4261 * 4262 * Reading back the file shows you the current setting and the permitted 4263 * ranges if changable. 4264 * 4265 * Writing an integer to the file, change the setting accordingly. 4266 * 4267 * When you have finished the editing, write "c" (commit) to the file to commit 4268 * your changes. 4269 * 4270 * If you want to reset to the default value, write "r" (reset) to the file to 4271 * reset them 4272 * 4273 * This setting works under auto fan control mode only. It can co-exist with 4274 * other settings which can work also under auto mode. It adjusts the PMFW's 4275 * behavior about the minimum fan speed in PWM the fan should spin. Setting 4276 * via this interface will switch the fan control to auto mode implicitly. 4277 */ 4278 static ssize_t fan_minimum_pwm_show(struct kobject *kobj, 4279 struct kobj_attribute *attr, 4280 char *buf) 4281 { 4282 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4283 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4284 4285 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf); 4286 } 4287 4288 static ssize_t fan_minimum_pwm_store(struct kobject *kobj, 4289 struct kobj_attribute *attr, 4290 const char *buf, 4291 size_t count) 4292 { 4293 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4294 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4295 4296 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4297 PP_OD_EDIT_FAN_MINIMUM_PWM, 4298 buf, 4299 count); 4300 } 4301 4302 static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev) 4303 { 4304 umode_t umode = 0000; 4305 4306 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE) 4307 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4308 4309 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET) 4310 umode |= S_IWUSR; 4311 4312 return umode; 4313 } 4314 4315 /** 4316 * DOC: fan_zero_rpm_enable 4317 * 4318 * The amdgpu driver provides a sysfs API for checking and adjusting the 4319 * zero RPM feature. 4320 * 4321 * Reading back the file shows you the current setting and the permitted 4322 * ranges if changable. 4323 * 4324 * Writing an integer to the file, change the setting accordingly. 4325 * 4326 * When you have finished the editing, write "c" (commit) to the file to commit 4327 * your changes. 4328 * 4329 * If you want to reset to the default value, write "r" (reset) to the file to 4330 * reset them. 4331 */ 4332 static ssize_t fan_zero_rpm_enable_show(struct kobject *kobj, 4333 struct kobj_attribute *attr, 4334 char *buf) 4335 { 4336 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4337 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4338 4339 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_ENABLE, buf); 4340 } 4341 4342 static ssize_t fan_zero_rpm_enable_store(struct kobject *kobj, 4343 struct kobj_attribute *attr, 4344 const char *buf, 4345 size_t count) 4346 { 4347 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4348 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4349 4350 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4351 PP_OD_EDIT_FAN_ZERO_RPM_ENABLE, 4352 buf, 4353 count); 4354 } 4355 4356 static umode_t fan_zero_rpm_enable_visible(struct amdgpu_device *adev) 4357 { 4358 umode_t umode = 0000; 4359 4360 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE) 4361 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4362 4363 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET) 4364 umode |= S_IWUSR; 4365 4366 return umode; 4367 } 4368 4369 /** 4370 * DOC: fan_zero_rpm_stop_temperature 4371 * 4372 * The amdgpu driver provides a sysfs API for checking and adjusting the 4373 * zero RPM stop temperature feature. 4374 * 4375 * Reading back the file shows you the current setting and the permitted 4376 * ranges if changable. 4377 * 4378 * Writing an integer to the file, change the setting accordingly. 4379 * 4380 * When you have finished the editing, write "c" (commit) to the file to commit 4381 * your changes. 4382 * 4383 * If you want to reset to the default value, write "r" (reset) to the file to 4384 * reset them. 4385 * 4386 * This setting works only if the Zero RPM setting is enabled. It adjusts the 4387 * temperature below which the fan can stop. 4388 */ 4389 static ssize_t fan_zero_rpm_stop_temp_show(struct kobject *kobj, 4390 struct kobj_attribute *attr, 4391 char *buf) 4392 { 4393 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4394 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4395 4396 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_STOP_TEMP, buf); 4397 } 4398 4399 static ssize_t fan_zero_rpm_stop_temp_store(struct kobject *kobj, 4400 struct kobj_attribute *attr, 4401 const char *buf, 4402 size_t count) 4403 { 4404 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj); 4405 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv; 4406 4407 return (ssize_t)amdgpu_distribute_custom_od_settings(adev, 4408 PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP, 4409 buf, 4410 count); 4411 } 4412 4413 static umode_t fan_zero_rpm_stop_temp_visible(struct amdgpu_device *adev) 4414 { 4415 umode_t umode = 0000; 4416 4417 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE) 4418 umode |= S_IRUSR | S_IRGRP | S_IROTH; 4419 4420 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET) 4421 umode |= S_IWUSR; 4422 4423 return umode; 4424 } 4425 4426 static struct od_feature_set amdgpu_od_set = { 4427 .containers = { 4428 [0] = { 4429 .name = "fan_ctrl", 4430 .sub_feature = { 4431 [0] = { 4432 .name = "fan_curve", 4433 .ops = { 4434 .is_visible = fan_curve_visible, 4435 .show = fan_curve_show, 4436 .store = fan_curve_store, 4437 }, 4438 }, 4439 [1] = { 4440 .name = "acoustic_limit_rpm_threshold", 4441 .ops = { 4442 .is_visible = acoustic_limit_threshold_visible, 4443 .show = acoustic_limit_threshold_show, 4444 .store = acoustic_limit_threshold_store, 4445 }, 4446 }, 4447 [2] = { 4448 .name = "acoustic_target_rpm_threshold", 4449 .ops = { 4450 .is_visible = acoustic_target_threshold_visible, 4451 .show = acoustic_target_threshold_show, 4452 .store = acoustic_target_threshold_store, 4453 }, 4454 }, 4455 [3] = { 4456 .name = "fan_target_temperature", 4457 .ops = { 4458 .is_visible = fan_target_temperature_visible, 4459 .show = fan_target_temperature_show, 4460 .store = fan_target_temperature_store, 4461 }, 4462 }, 4463 [4] = { 4464 .name = "fan_minimum_pwm", 4465 .ops = { 4466 .is_visible = fan_minimum_pwm_visible, 4467 .show = fan_minimum_pwm_show, 4468 .store = fan_minimum_pwm_store, 4469 }, 4470 }, 4471 [5] = { 4472 .name = "fan_zero_rpm_enable", 4473 .ops = { 4474 .is_visible = fan_zero_rpm_enable_visible, 4475 .show = fan_zero_rpm_enable_show, 4476 .store = fan_zero_rpm_enable_store, 4477 }, 4478 }, 4479 [6] = { 4480 .name = "fan_zero_rpm_stop_temperature", 4481 .ops = { 4482 .is_visible = fan_zero_rpm_stop_temp_visible, 4483 .show = fan_zero_rpm_stop_temp_show, 4484 .store = fan_zero_rpm_stop_temp_store, 4485 }, 4486 }, 4487 }, 4488 }, 4489 }, 4490 }; 4491 4492 static void od_kobj_release(struct kobject *kobj) 4493 { 4494 struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj); 4495 4496 kfree(od_kobj); 4497 } 4498 4499 static const struct kobj_type od_ktype = { 4500 .release = od_kobj_release, 4501 .sysfs_ops = &kobj_sysfs_ops, 4502 }; 4503 4504 static void amdgpu_od_set_fini(struct amdgpu_device *adev) 4505 { 4506 struct od_kobj *container, *container_next; 4507 struct od_attribute *attribute, *attribute_next; 4508 4509 if (list_empty(&adev->pm.od_kobj_list)) 4510 return; 4511 4512 list_for_each_entry_safe(container, container_next, 4513 &adev->pm.od_kobj_list, entry) { 4514 list_del(&container->entry); 4515 4516 list_for_each_entry_safe(attribute, attribute_next, 4517 &container->attribute, entry) { 4518 list_del(&attribute->entry); 4519 sysfs_remove_file(&container->kobj, 4520 &attribute->attribute.attr); 4521 kfree(attribute); 4522 } 4523 4524 kobject_put(&container->kobj); 4525 } 4526 } 4527 4528 static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev, 4529 struct od_feature_ops *feature_ops) 4530 { 4531 umode_t mode; 4532 4533 if (!feature_ops->is_visible) 4534 return false; 4535 4536 /* 4537 * If the feature has no user read and write mode set, 4538 * we can assume the feature is actually not supported.(?) 4539 * And the revelant sysfs interface should not be exposed. 4540 */ 4541 mode = feature_ops->is_visible(adev); 4542 if (mode & (S_IRUSR | S_IWUSR)) 4543 return true; 4544 4545 return false; 4546 } 4547 4548 static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev, 4549 struct od_feature_container *container) 4550 { 4551 int i; 4552 4553 /* 4554 * If there is no valid entry within the container, the container 4555 * is recognized as a self contained container. And the valid entry 4556 * here means it has a valid naming and it is visible/supported by 4557 * the ASIC. 4558 */ 4559 for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) { 4560 if (container->sub_feature[i].name && 4561 amdgpu_is_od_feature_supported(adev, 4562 &container->sub_feature[i].ops)) 4563 return false; 4564 } 4565 4566 return true; 4567 } 4568 4569 static int amdgpu_od_set_init(struct amdgpu_device *adev) 4570 { 4571 struct od_kobj *top_set, *sub_set; 4572 struct od_attribute *attribute; 4573 struct od_feature_container *container; 4574 struct od_feature_item *feature; 4575 int i, j; 4576 int ret; 4577 4578 /* Setup the top `gpu_od` directory which holds all other OD interfaces */ 4579 top_set = kzalloc_obj(*top_set); 4580 if (!top_set) 4581 return -ENOMEM; 4582 list_add(&top_set->entry, &adev->pm.od_kobj_list); 4583 4584 ret = kobject_init_and_add(&top_set->kobj, 4585 &od_ktype, 4586 &adev->dev->kobj, 4587 "%s", 4588 "gpu_od"); 4589 if (ret) 4590 goto err_out; 4591 INIT_LIST_HEAD(&top_set->attribute); 4592 top_set->priv = adev; 4593 4594 for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) { 4595 container = &amdgpu_od_set.containers[i]; 4596 4597 if (!container->name) 4598 continue; 4599 4600 /* 4601 * If there is valid entries within the container, the container 4602 * will be presented as a sub directory and all its holding entries 4603 * will be presented as plain files under it. 4604 * While if there is no valid entry within the container, the container 4605 * itself will be presented as a plain file under top `gpu_od` directory. 4606 */ 4607 if (amdgpu_od_is_self_contained(adev, container)) { 4608 if (!amdgpu_is_od_feature_supported(adev, 4609 &container->ops)) 4610 continue; 4611 4612 /* 4613 * The container is presented as a plain file under top `gpu_od` 4614 * directory. 4615 */ 4616 attribute = kzalloc_obj(*attribute); 4617 if (!attribute) { 4618 ret = -ENOMEM; 4619 goto err_out; 4620 } 4621 list_add(&attribute->entry, &top_set->attribute); 4622 4623 attribute->attribute.attr.mode = 4624 container->ops.is_visible(adev); 4625 attribute->attribute.attr.name = container->name; 4626 attribute->attribute.show = 4627 container->ops.show; 4628 attribute->attribute.store = 4629 container->ops.store; 4630 ret = sysfs_create_file(&top_set->kobj, 4631 &attribute->attribute.attr); 4632 if (ret) 4633 goto err_out; 4634 } else { 4635 /* The container is presented as a sub directory. */ 4636 sub_set = kzalloc_obj(*sub_set); 4637 if (!sub_set) { 4638 ret = -ENOMEM; 4639 goto err_out; 4640 } 4641 list_add(&sub_set->entry, &adev->pm.od_kobj_list); 4642 4643 ret = kobject_init_and_add(&sub_set->kobj, 4644 &od_ktype, 4645 &top_set->kobj, 4646 "%s", 4647 container->name); 4648 if (ret) 4649 goto err_out; 4650 INIT_LIST_HEAD(&sub_set->attribute); 4651 sub_set->priv = adev; 4652 4653 for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) { 4654 feature = &container->sub_feature[j]; 4655 if (!feature->name) 4656 continue; 4657 4658 if (!amdgpu_is_od_feature_supported(adev, 4659 &feature->ops)) 4660 continue; 4661 4662 /* 4663 * With the container presented as a sub directory, the entry within 4664 * it is presented as a plain file under the sub directory. 4665 */ 4666 attribute = kzalloc_obj(*attribute); 4667 if (!attribute) { 4668 ret = -ENOMEM; 4669 goto err_out; 4670 } 4671 list_add(&attribute->entry, &sub_set->attribute); 4672 4673 attribute->attribute.attr.mode = 4674 feature->ops.is_visible(adev); 4675 attribute->attribute.attr.name = feature->name; 4676 attribute->attribute.show = 4677 feature->ops.show; 4678 attribute->attribute.store = 4679 feature->ops.store; 4680 ret = sysfs_create_file(&sub_set->kobj, 4681 &attribute->attribute.attr); 4682 if (ret) 4683 goto err_out; 4684 } 4685 } 4686 } 4687 4688 /* 4689 * If gpu_od is the only member in the list, that means gpu_od is an 4690 * empty directory, so remove it. 4691 */ 4692 if (list_is_singular(&adev->pm.od_kobj_list)) 4693 goto err_out; 4694 4695 return 0; 4696 4697 err_out: 4698 amdgpu_od_set_fini(adev); 4699 4700 return ret; 4701 } 4702 4703 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev) 4704 { 4705 enum amdgpu_sriov_vf_mode mode; 4706 uint32_t mask = 0; 4707 uint32_t tmp; 4708 int ret; 4709 4710 if (adev->pm.sysfs_initialized) 4711 return 0; 4712 4713 INIT_LIST_HEAD(&adev->pm.pm_attr_list); 4714 4715 if (adev->pm.dpm_enabled == 0) 4716 return 0; 4717 4718 mode = amdgpu_virt_get_sriov_vf_mode(adev); 4719 4720 /* under multi-vf mode, the hwmon attributes are all not supported */ 4721 if (mode != SRIOV_VF_MODE_MULTI_VF) { 4722 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev, 4723 DRIVER_NAME, adev, 4724 hwmon_groups); 4725 if (IS_ERR(adev->pm.int_hwmon_dev)) { 4726 ret = PTR_ERR(adev->pm.int_hwmon_dev); 4727 dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret); 4728 return ret; 4729 } 4730 } 4731 4732 switch (mode) { 4733 case SRIOV_VF_MODE_ONE_VF: 4734 mask = ATTR_FLAG_ONEVF; 4735 break; 4736 case SRIOV_VF_MODE_MULTI_VF: 4737 mask = 0; 4738 break; 4739 case SRIOV_VF_MODE_BARE_METAL: 4740 default: 4741 mask = ATTR_FLAG_MASK_ALL; 4742 break; 4743 } 4744 4745 ret = amdgpu_device_attr_create_groups(adev, 4746 amdgpu_device_attrs, 4747 ARRAY_SIZE(amdgpu_device_attrs), 4748 mask, 4749 &adev->pm.pm_attr_list); 4750 if (ret) 4751 goto err_out0; 4752 4753 if (amdgpu_dpm_is_overdrive_supported(adev)) { 4754 ret = amdgpu_od_set_init(adev); 4755 if (ret) 4756 goto err_out1; 4757 } else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) { 4758 dev_info(adev->dev, "overdrive feature is not supported\n"); 4759 } 4760 4761 if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) != 4762 -EOPNOTSUPP) { 4763 ret = devm_device_add_group(adev->dev, 4764 &amdgpu_pm_policy_attr_group); 4765 if (ret) 4766 goto err_out1; 4767 } 4768 4769 if (amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD)) { 4770 ret = devm_device_add_group(adev->dev, 4771 &amdgpu_board_attr_group); 4772 if (ret) 4773 goto err_out1; 4774 if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAXNODEPOWERLIMIT, 4775 (void *)&tmp) != -EOPNOTSUPP) { 4776 sysfs_add_file_to_group(&adev->dev->kobj, 4777 &dev_attr_cur_node_power_limit.attr, 4778 amdgpu_board_attr_group.name); 4779 sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_node_power.attr, 4780 amdgpu_board_attr_group.name); 4781 sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_global_ppt_resid.attr, 4782 amdgpu_board_attr_group.name); 4783 sysfs_add_file_to_group(&adev->dev->kobj, 4784 &dev_attr_max_node_power_limit.attr, 4785 amdgpu_board_attr_group.name); 4786 sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_npm_status.attr, 4787 amdgpu_board_attr_group.name); 4788 } 4789 if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER_LIMIT, 4790 (void *)&tmp) != -EOPNOTSUPP) { 4791 sysfs_add_file_to_group(&adev->dev->kobj, 4792 &dev_attr_baseboard_power_limit.attr, 4793 amdgpu_board_attr_group.name); 4794 sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_baseboard_power.attr, 4795 amdgpu_board_attr_group.name); 4796 } 4797 } 4798 4799 adev->pm.sysfs_initialized = true; 4800 4801 return 0; 4802 4803 err_out1: 4804 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 4805 err_out0: 4806 if (adev->pm.int_hwmon_dev) 4807 hwmon_device_unregister(adev->pm.int_hwmon_dev); 4808 4809 return ret; 4810 } 4811 4812 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev) 4813 { 4814 amdgpu_od_set_fini(adev); 4815 4816 if (adev->pm.int_hwmon_dev) 4817 hwmon_device_unregister(adev->pm.int_hwmon_dev); 4818 4819 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 4820 } 4821 4822 /* 4823 * Debugfs info 4824 */ 4825 #if defined(CONFIG_DEBUG_FS) 4826 4827 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m, 4828 struct amdgpu_device *adev) 4829 { 4830 uint16_t *p_val; 4831 uint32_t size; 4832 int i; 4833 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev); 4834 4835 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) { 4836 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t), 4837 GFP_KERNEL); 4838 4839 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK, 4840 (void *)p_val, &size)) { 4841 for (i = 0; i < num_cpu_cores; i++) 4842 seq_printf(m, "\t%u MHz (CPU%d)\n", 4843 *(p_val + i), i); 4844 } 4845 4846 kfree(p_val); 4847 } 4848 } 4849 4850 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev) 4851 { 4852 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0); 4853 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 4854 uint32_t value; 4855 uint64_t value64 = 0; 4856 uint32_t query = 0; 4857 int size; 4858 4859 /* GPU Clocks */ 4860 size = sizeof(value); 4861 seq_printf(m, "GFX Clocks and Power:\n"); 4862 4863 amdgpu_debugfs_prints_cpu_info(m, adev); 4864 4865 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size)) 4866 seq_printf(m, "\t%u MHz (MCLK)\n", value/100); 4867 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size)) 4868 seq_printf(m, "\t%u MHz (SCLK)\n", value/100); 4869 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size)) 4870 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100); 4871 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size)) 4872 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100); 4873 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size)) 4874 seq_printf(m, "\t%u mV (VDDGFX)\n", value); 4875 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size)) 4876 seq_printf(m, "\t%u mV (VDDNB)\n", value); 4877 size = sizeof(uint32_t); 4878 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) { 4879 if (adev->flags & AMD_IS_APU) 4880 seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff); 4881 else 4882 seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff); 4883 } 4884 size = sizeof(uint32_t); 4885 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) { 4886 if (adev->flags & AMD_IS_APU) 4887 seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff); 4888 else 4889 seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff); 4890 } 4891 size = sizeof(value); 4892 seq_printf(m, "\n"); 4893 4894 /* GPU Temp */ 4895 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size)) 4896 seq_printf(m, "GPU Temperature: %u C\n", value/1000); 4897 4898 /* GPU Load */ 4899 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size)) 4900 seq_printf(m, "GPU Load: %u %%\n", value); 4901 /* MEM Load */ 4902 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size)) 4903 seq_printf(m, "MEM Load: %u %%\n", value); 4904 /* VCN Load */ 4905 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size)) 4906 seq_printf(m, "VCN Load: %u %%\n", value); 4907 4908 seq_printf(m, "\n"); 4909 4910 /* SMC feature mask */ 4911 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size)) 4912 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64); 4913 4914 /* ASICs greater than CHIP_VEGA20 supports these sensors */ 4915 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) { 4916 /* VCN clocks */ 4917 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) { 4918 if (!value) { 4919 seq_printf(m, "VCN: Powered down\n"); 4920 } else { 4921 seq_printf(m, "VCN: Powered up\n"); 4922 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 4923 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 4924 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 4925 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 4926 } 4927 } 4928 seq_printf(m, "\n"); 4929 } else { 4930 /* UVD clocks */ 4931 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) { 4932 if (!value) { 4933 seq_printf(m, "UVD: Powered down\n"); 4934 } else { 4935 seq_printf(m, "UVD: Powered up\n"); 4936 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 4937 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 4938 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 4939 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 4940 } 4941 } 4942 seq_printf(m, "\n"); 4943 4944 /* VCE clocks */ 4945 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) { 4946 if (!value) { 4947 seq_printf(m, "VCE: Powered down\n"); 4948 } else { 4949 seq_printf(m, "VCE: Powered up\n"); 4950 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size)) 4951 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100); 4952 } 4953 } 4954 } 4955 4956 return 0; 4957 } 4958 4959 static const struct cg_flag_name clocks[] = { 4960 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"}, 4961 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"}, 4962 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"}, 4963 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"}, 4964 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"}, 4965 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"}, 4966 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"}, 4967 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"}, 4968 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"}, 4969 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"}, 4970 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"}, 4971 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"}, 4972 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"}, 4973 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"}, 4974 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"}, 4975 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"}, 4976 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"}, 4977 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"}, 4978 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"}, 4979 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"}, 4980 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"}, 4981 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"}, 4982 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"}, 4983 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"}, 4984 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"}, 4985 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"}, 4986 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"}, 4987 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"}, 4988 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"}, 4989 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"}, 4990 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"}, 4991 {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"}, 4992 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"}, 4993 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"}, 4994 {0, NULL}, 4995 }; 4996 4997 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags) 4998 { 4999 int i; 5000 5001 for (i = 0; clocks[i].flag; i++) 5002 seq_printf(m, "\t%s: %s\n", clocks[i].name, 5003 (flags & clocks[i].flag) ? "On" : "Off"); 5004 } 5005 5006 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused) 5007 { 5008 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 5009 u64 flags = 0; 5010 int r; 5011 5012 r = amdgpu_pm_get_access(adev); 5013 if (r < 0) 5014 return r; 5015 5016 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) { 5017 r = amdgpu_debugfs_pm_info_pp(m, adev); 5018 if (r) 5019 goto out; 5020 } 5021 5022 amdgpu_device_ip_get_clockgating_state(adev, &flags); 5023 5024 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags); 5025 amdgpu_parse_cg_state(m, flags); 5026 seq_printf(m, "\n"); 5027 5028 out: 5029 amdgpu_pm_put_access(adev); 5030 5031 return r; 5032 } 5033 5034 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info); 5035 5036 /* 5037 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW 5038 * 5039 * Reads debug memory region allocated to PMFW 5040 */ 5041 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf, 5042 size_t size, loff_t *pos) 5043 { 5044 struct amdgpu_device *adev = file_inode(f)->i_private; 5045 size_t smu_prv_buf_size; 5046 void *smu_prv_buf; 5047 int ret = 0; 5048 5049 ret = amdgpu_pm_dev_state_check(adev, true); 5050 if (ret) 5051 return ret; 5052 5053 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size); 5054 if (ret) 5055 return ret; 5056 5057 if (!smu_prv_buf || !smu_prv_buf_size) 5058 return -EINVAL; 5059 5060 return simple_read_from_buffer(buf, size, pos, smu_prv_buf, 5061 smu_prv_buf_size); 5062 } 5063 5064 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = { 5065 .owner = THIS_MODULE, 5066 .open = simple_open, 5067 .read = amdgpu_pm_prv_buffer_read, 5068 .llseek = default_llseek, 5069 }; 5070 5071 #endif 5072 5073 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev) 5074 { 5075 #if defined(CONFIG_DEBUG_FS) 5076 struct drm_minor *minor = adev_to_drm(adev)->primary; 5077 struct dentry *root = minor->debugfs_root; 5078 5079 if (!adev->pm.dpm_enabled) 5080 return; 5081 5082 debugfs_create_file("amdgpu_pm_info", 0444, root, adev, 5083 &amdgpu_debugfs_pm_info_fops); 5084 5085 if (adev->pm.smu_prv_buffer_size > 0) 5086 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root, 5087 adev, 5088 &amdgpu_debugfs_pm_prv_buffer_fops, 5089 adev->pm.smu_prv_buffer_size); 5090 5091 amdgpu_dpm_stb_debug_fs_init(adev); 5092 #endif 5093 } 5094