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 static const struct cg_flag_name clocks[] = { 39 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"}, 40 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"}, 41 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"}, 42 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"}, 43 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"}, 44 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"}, 45 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"}, 46 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"}, 47 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"}, 48 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"}, 49 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"}, 50 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"}, 51 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"}, 52 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"}, 53 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"}, 54 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"}, 55 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"}, 56 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"}, 57 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"}, 58 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"}, 59 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"}, 60 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"}, 61 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"}, 62 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"}, 63 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"}, 64 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"}, 65 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"}, 66 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"}, 67 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"}, 68 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"}, 69 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"}, 70 71 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"}, 72 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"}, 73 {0, NULL}, 74 }; 75 76 static const struct hwmon_temp_label { 77 enum PP_HWMON_TEMP channel; 78 const char *label; 79 } temp_label[] = { 80 {PP_TEMP_EDGE, "edge"}, 81 {PP_TEMP_JUNCTION, "junction"}, 82 {PP_TEMP_MEM, "mem"}, 83 }; 84 85 const char * const amdgpu_pp_profile_name[] = { 86 "BOOTUP_DEFAULT", 87 "3D_FULL_SCREEN", 88 "POWER_SAVING", 89 "VIDEO", 90 "VR", 91 "COMPUTE", 92 "CUSTOM" 93 }; 94 95 /** 96 * DOC: power_dpm_state 97 * 98 * The power_dpm_state file is a legacy interface and is only provided for 99 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting 100 * certain power related parameters. The file power_dpm_state is used for this. 101 * It accepts the following arguments: 102 * 103 * - battery 104 * 105 * - balanced 106 * 107 * - performance 108 * 109 * battery 110 * 111 * On older GPUs, the vbios provided a special power state for battery 112 * operation. Selecting battery switched to this state. This is no 113 * longer provided on newer GPUs so the option does nothing in that case. 114 * 115 * balanced 116 * 117 * On older GPUs, the vbios provided a special power state for balanced 118 * operation. Selecting balanced switched to this state. This is no 119 * longer provided on newer GPUs so the option does nothing in that case. 120 * 121 * performance 122 * 123 * On older GPUs, the vbios provided a special power state for performance 124 * operation. Selecting performance switched to this state. This is no 125 * longer provided on newer GPUs so the option does nothing in that case. 126 * 127 */ 128 129 static ssize_t amdgpu_get_power_dpm_state(struct device *dev, 130 struct device_attribute *attr, 131 char *buf) 132 { 133 struct drm_device *ddev = dev_get_drvdata(dev); 134 struct amdgpu_device *adev = drm_to_adev(ddev); 135 enum amd_pm_state_type pm; 136 int ret; 137 138 if (amdgpu_in_reset(adev)) 139 return -EPERM; 140 if (adev->in_suspend && !adev->in_runpm) 141 return -EPERM; 142 143 ret = pm_runtime_get_sync(ddev->dev); 144 if (ret < 0) { 145 pm_runtime_put_autosuspend(ddev->dev); 146 return ret; 147 } 148 149 amdgpu_dpm_get_current_power_state(adev, &pm); 150 151 pm_runtime_mark_last_busy(ddev->dev); 152 pm_runtime_put_autosuspend(ddev->dev); 153 154 return sysfs_emit(buf, "%s\n", 155 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" : 156 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance"); 157 } 158 159 static ssize_t amdgpu_set_power_dpm_state(struct device *dev, 160 struct device_attribute *attr, 161 const char *buf, 162 size_t count) 163 { 164 struct drm_device *ddev = dev_get_drvdata(dev); 165 struct amdgpu_device *adev = drm_to_adev(ddev); 166 enum amd_pm_state_type state; 167 int ret; 168 169 if (amdgpu_in_reset(adev)) 170 return -EPERM; 171 if (adev->in_suspend && !adev->in_runpm) 172 return -EPERM; 173 174 if (strncmp("battery", buf, strlen("battery")) == 0) 175 state = POWER_STATE_TYPE_BATTERY; 176 else if (strncmp("balanced", buf, strlen("balanced")) == 0) 177 state = POWER_STATE_TYPE_BALANCED; 178 else if (strncmp("performance", buf, strlen("performance")) == 0) 179 state = POWER_STATE_TYPE_PERFORMANCE; 180 else 181 return -EINVAL; 182 183 ret = pm_runtime_get_sync(ddev->dev); 184 if (ret < 0) { 185 pm_runtime_put_autosuspend(ddev->dev); 186 return ret; 187 } 188 189 amdgpu_dpm_set_power_state(adev, state); 190 191 pm_runtime_mark_last_busy(ddev->dev); 192 pm_runtime_put_autosuspend(ddev->dev); 193 194 return count; 195 } 196 197 198 /** 199 * DOC: power_dpm_force_performance_level 200 * 201 * The amdgpu driver provides a sysfs API for adjusting certain power 202 * related parameters. The file power_dpm_force_performance_level is 203 * used for this. It accepts the following arguments: 204 * 205 * - auto 206 * 207 * - low 208 * 209 * - high 210 * 211 * - manual 212 * 213 * - profile_standard 214 * 215 * - profile_min_sclk 216 * 217 * - profile_min_mclk 218 * 219 * - profile_peak 220 * 221 * auto 222 * 223 * When auto is selected, the driver will attempt to dynamically select 224 * the optimal power profile for current conditions in the driver. 225 * 226 * low 227 * 228 * When low is selected, the clocks are forced to the lowest power state. 229 * 230 * high 231 * 232 * When high is selected, the clocks are forced to the highest power state. 233 * 234 * manual 235 * 236 * When manual is selected, the user can manually adjust which power states 237 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk, 238 * and pp_dpm_pcie files and adjust the power state transition heuristics 239 * via the pp_power_profile_mode sysfs file. 240 * 241 * profile_standard 242 * profile_min_sclk 243 * profile_min_mclk 244 * profile_peak 245 * 246 * When the profiling modes are selected, clock and power gating are 247 * disabled and the clocks are set for different profiling cases. This 248 * mode is recommended for profiling specific work loads where you do 249 * not want clock or power gating for clock fluctuation to interfere 250 * with your results. profile_standard sets the clocks to a fixed clock 251 * level which varies from asic to asic. profile_min_sclk forces the sclk 252 * to the lowest level. profile_min_mclk forces the mclk to the lowest level. 253 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels. 254 * 255 */ 256 257 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev, 258 struct device_attribute *attr, 259 char *buf) 260 { 261 struct drm_device *ddev = dev_get_drvdata(dev); 262 struct amdgpu_device *adev = drm_to_adev(ddev); 263 enum amd_dpm_forced_level level = 0xff; 264 int ret; 265 266 if (amdgpu_in_reset(adev)) 267 return -EPERM; 268 if (adev->in_suspend && !adev->in_runpm) 269 return -EPERM; 270 271 ret = pm_runtime_get_sync(ddev->dev); 272 if (ret < 0) { 273 pm_runtime_put_autosuspend(ddev->dev); 274 return ret; 275 } 276 277 level = amdgpu_dpm_get_performance_level(adev); 278 279 pm_runtime_mark_last_busy(ddev->dev); 280 pm_runtime_put_autosuspend(ddev->dev); 281 282 return sysfs_emit(buf, "%s\n", 283 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" : 284 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" : 285 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" : 286 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" : 287 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" : 288 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" : 289 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" : 290 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" : 291 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" : 292 "unknown"); 293 } 294 295 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev, 296 struct device_attribute *attr, 297 const char *buf, 298 size_t count) 299 { 300 struct drm_device *ddev = dev_get_drvdata(dev); 301 struct amdgpu_device *adev = drm_to_adev(ddev); 302 enum amd_dpm_forced_level level; 303 int ret = 0; 304 305 if (amdgpu_in_reset(adev)) 306 return -EPERM; 307 if (adev->in_suspend && !adev->in_runpm) 308 return -EPERM; 309 310 if (strncmp("low", buf, strlen("low")) == 0) { 311 level = AMD_DPM_FORCED_LEVEL_LOW; 312 } else if (strncmp("high", buf, strlen("high")) == 0) { 313 level = AMD_DPM_FORCED_LEVEL_HIGH; 314 } else if (strncmp("auto", buf, strlen("auto")) == 0) { 315 level = AMD_DPM_FORCED_LEVEL_AUTO; 316 } else if (strncmp("manual", buf, strlen("manual")) == 0) { 317 level = AMD_DPM_FORCED_LEVEL_MANUAL; 318 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) { 319 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT; 320 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) { 321 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD; 322 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) { 323 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK; 324 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) { 325 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK; 326 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) { 327 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK; 328 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) { 329 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM; 330 } else { 331 return -EINVAL; 332 } 333 334 ret = pm_runtime_get_sync(ddev->dev); 335 if (ret < 0) { 336 pm_runtime_put_autosuspend(ddev->dev); 337 return ret; 338 } 339 340 mutex_lock(&adev->pm.stable_pstate_ctx_lock); 341 if (amdgpu_dpm_force_performance_level(adev, level)) { 342 pm_runtime_mark_last_busy(ddev->dev); 343 pm_runtime_put_autosuspend(ddev->dev); 344 mutex_unlock(&adev->pm.stable_pstate_ctx_lock); 345 return -EINVAL; 346 } 347 /* override whatever a user ctx may have set */ 348 adev->pm.stable_pstate_ctx = NULL; 349 mutex_unlock(&adev->pm.stable_pstate_ctx_lock); 350 351 pm_runtime_mark_last_busy(ddev->dev); 352 pm_runtime_put_autosuspend(ddev->dev); 353 354 return count; 355 } 356 357 static ssize_t amdgpu_get_pp_num_states(struct device *dev, 358 struct device_attribute *attr, 359 char *buf) 360 { 361 struct drm_device *ddev = dev_get_drvdata(dev); 362 struct amdgpu_device *adev = drm_to_adev(ddev); 363 struct pp_states_info data; 364 uint32_t i; 365 int buf_len, ret; 366 367 if (amdgpu_in_reset(adev)) 368 return -EPERM; 369 if (adev->in_suspend && !adev->in_runpm) 370 return -EPERM; 371 372 ret = pm_runtime_get_sync(ddev->dev); 373 if (ret < 0) { 374 pm_runtime_put_autosuspend(ddev->dev); 375 return ret; 376 } 377 378 if (amdgpu_dpm_get_pp_num_states(adev, &data)) 379 memset(&data, 0, sizeof(data)); 380 381 pm_runtime_mark_last_busy(ddev->dev); 382 pm_runtime_put_autosuspend(ddev->dev); 383 384 buf_len = sysfs_emit(buf, "states: %d\n", data.nums); 385 for (i = 0; i < data.nums; i++) 386 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i, 387 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" : 388 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" : 389 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" : 390 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default"); 391 392 return buf_len; 393 } 394 395 static ssize_t amdgpu_get_pp_cur_state(struct device *dev, 396 struct device_attribute *attr, 397 char *buf) 398 { 399 struct drm_device *ddev = dev_get_drvdata(dev); 400 struct amdgpu_device *adev = drm_to_adev(ddev); 401 struct pp_states_info data = {0}; 402 enum amd_pm_state_type pm = 0; 403 int i = 0, ret = 0; 404 405 if (amdgpu_in_reset(adev)) 406 return -EPERM; 407 if (adev->in_suspend && !adev->in_runpm) 408 return -EPERM; 409 410 ret = pm_runtime_get_sync(ddev->dev); 411 if (ret < 0) { 412 pm_runtime_put_autosuspend(ddev->dev); 413 return ret; 414 } 415 416 amdgpu_dpm_get_current_power_state(adev, &pm); 417 418 ret = amdgpu_dpm_get_pp_num_states(adev, &data); 419 420 pm_runtime_mark_last_busy(ddev->dev); 421 pm_runtime_put_autosuspend(ddev->dev); 422 423 if (ret) 424 return ret; 425 426 for (i = 0; i < data.nums; i++) { 427 if (pm == data.states[i]) 428 break; 429 } 430 431 if (i == data.nums) 432 i = -EINVAL; 433 434 return sysfs_emit(buf, "%d\n", i); 435 } 436 437 static ssize_t amdgpu_get_pp_force_state(struct device *dev, 438 struct device_attribute *attr, 439 char *buf) 440 { 441 struct drm_device *ddev = dev_get_drvdata(dev); 442 struct amdgpu_device *adev = drm_to_adev(ddev); 443 444 if (amdgpu_in_reset(adev)) 445 return -EPERM; 446 if (adev->in_suspend && !adev->in_runpm) 447 return -EPERM; 448 449 if (adev->pm.pp_force_state_enabled) 450 return amdgpu_get_pp_cur_state(dev, attr, buf); 451 else 452 return sysfs_emit(buf, "\n"); 453 } 454 455 static ssize_t amdgpu_set_pp_force_state(struct device *dev, 456 struct device_attribute *attr, 457 const char *buf, 458 size_t count) 459 { 460 struct drm_device *ddev = dev_get_drvdata(dev); 461 struct amdgpu_device *adev = drm_to_adev(ddev); 462 enum amd_pm_state_type state = 0; 463 struct pp_states_info data; 464 unsigned long idx; 465 int ret; 466 467 if (amdgpu_in_reset(adev)) 468 return -EPERM; 469 if (adev->in_suspend && !adev->in_runpm) 470 return -EPERM; 471 472 adev->pm.pp_force_state_enabled = false; 473 474 if (strlen(buf) == 1) 475 return count; 476 477 ret = kstrtoul(buf, 0, &idx); 478 if (ret || idx >= ARRAY_SIZE(data.states)) 479 return -EINVAL; 480 481 idx = array_index_nospec(idx, ARRAY_SIZE(data.states)); 482 483 ret = pm_runtime_get_sync(ddev->dev); 484 if (ret < 0) { 485 pm_runtime_put_autosuspend(ddev->dev); 486 return ret; 487 } 488 489 ret = amdgpu_dpm_get_pp_num_states(adev, &data); 490 if (ret) 491 goto err_out; 492 493 state = data.states[idx]; 494 495 /* only set user selected power states */ 496 if (state != POWER_STATE_TYPE_INTERNAL_BOOT && 497 state != POWER_STATE_TYPE_DEFAULT) { 498 ret = amdgpu_dpm_dispatch_task(adev, 499 AMD_PP_TASK_ENABLE_USER_STATE, &state); 500 if (ret) 501 goto err_out; 502 503 adev->pm.pp_force_state_enabled = true; 504 } 505 506 pm_runtime_mark_last_busy(ddev->dev); 507 pm_runtime_put_autosuspend(ddev->dev); 508 509 return count; 510 511 err_out: 512 pm_runtime_mark_last_busy(ddev->dev); 513 pm_runtime_put_autosuspend(ddev->dev); 514 return ret; 515 } 516 517 /** 518 * DOC: pp_table 519 * 520 * The amdgpu driver provides a sysfs API for uploading new powerplay 521 * tables. The file pp_table is used for this. Reading the file 522 * will dump the current power play table. Writing to the file 523 * will attempt to upload a new powerplay table and re-initialize 524 * powerplay using that new table. 525 * 526 */ 527 528 static ssize_t amdgpu_get_pp_table(struct device *dev, 529 struct device_attribute *attr, 530 char *buf) 531 { 532 struct drm_device *ddev = dev_get_drvdata(dev); 533 struct amdgpu_device *adev = drm_to_adev(ddev); 534 char *table = NULL; 535 int size, ret; 536 537 if (amdgpu_in_reset(adev)) 538 return -EPERM; 539 if (adev->in_suspend && !adev->in_runpm) 540 return -EPERM; 541 542 ret = pm_runtime_get_sync(ddev->dev); 543 if (ret < 0) { 544 pm_runtime_put_autosuspend(ddev->dev); 545 return ret; 546 } 547 548 size = amdgpu_dpm_get_pp_table(adev, &table); 549 550 pm_runtime_mark_last_busy(ddev->dev); 551 pm_runtime_put_autosuspend(ddev->dev); 552 553 if (size <= 0) 554 return size; 555 556 if (size >= PAGE_SIZE) 557 size = PAGE_SIZE - 1; 558 559 memcpy(buf, table, size); 560 561 return size; 562 } 563 564 static ssize_t amdgpu_set_pp_table(struct device *dev, 565 struct device_attribute *attr, 566 const char *buf, 567 size_t count) 568 { 569 struct drm_device *ddev = dev_get_drvdata(dev); 570 struct amdgpu_device *adev = drm_to_adev(ddev); 571 int ret = 0; 572 573 if (amdgpu_in_reset(adev)) 574 return -EPERM; 575 if (adev->in_suspend && !adev->in_runpm) 576 return -EPERM; 577 578 ret = pm_runtime_get_sync(ddev->dev); 579 if (ret < 0) { 580 pm_runtime_put_autosuspend(ddev->dev); 581 return ret; 582 } 583 584 ret = amdgpu_dpm_set_pp_table(adev, buf, count); 585 586 pm_runtime_mark_last_busy(ddev->dev); 587 pm_runtime_put_autosuspend(ddev->dev); 588 589 if (ret) 590 return ret; 591 592 return count; 593 } 594 595 /** 596 * DOC: pp_od_clk_voltage 597 * 598 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages 599 * in each power level within a power state. The pp_od_clk_voltage is used for 600 * this. 601 * 602 * Note that the actual memory controller clock rate are exposed, not 603 * the effective memory clock of the DRAMs. To translate it, use the 604 * following formula: 605 * 606 * Clock conversion (Mhz): 607 * 608 * HBM: effective_memory_clock = memory_controller_clock * 1 609 * 610 * G5: effective_memory_clock = memory_controller_clock * 1 611 * 612 * G6: effective_memory_clock = memory_controller_clock * 2 613 * 614 * DRAM data rate (MT/s): 615 * 616 * HBM: effective_memory_clock * 2 = data_rate 617 * 618 * G5: effective_memory_clock * 4 = data_rate 619 * 620 * G6: effective_memory_clock * 8 = data_rate 621 * 622 * Bandwidth (MB/s): 623 * 624 * data_rate * vram_bit_width / 8 = memory_bandwidth 625 * 626 * Some examples: 627 * 628 * G5 on RX460: 629 * 630 * memory_controller_clock = 1750 Mhz 631 * 632 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz 633 * 634 * data rate = 1750 * 4 = 7000 MT/s 635 * 636 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s 637 * 638 * G6 on RX5700: 639 * 640 * memory_controller_clock = 875 Mhz 641 * 642 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz 643 * 644 * data rate = 1750 * 8 = 14000 MT/s 645 * 646 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s 647 * 648 * < For Vega10 and previous ASICs > 649 * 650 * Reading the file will display: 651 * 652 * - a list of engine clock levels and voltages labeled OD_SCLK 653 * 654 * - a list of memory clock levels and voltages labeled OD_MCLK 655 * 656 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE 657 * 658 * To manually adjust these settings, first select manual using 659 * power_dpm_force_performance_level. Enter a new value for each 660 * level by writing a string that contains "s/m level clock voltage" to 661 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz 662 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at 663 * 810 mV. When you have edited all of the states as needed, write 664 * "c" (commit) to the file to commit your changes. If you want to reset to the 665 * default power levels, write "r" (reset) to the file to reset them. 666 * 667 * 668 * < For Vega20 and newer ASICs > 669 * 670 * Reading the file will display: 671 * 672 * - minimum and maximum engine clock labeled OD_SCLK 673 * 674 * - minimum(not available for Vega20 and Navi1x) and maximum memory 675 * clock labeled OD_MCLK 676 * 677 * - three <frequency, voltage> points labeled OD_VDDC_CURVE. 678 * They can be used to calibrate the sclk voltage curve. 679 * 680 * - voltage offset(in mV) applied on target voltage calculation. 681 * This is available for Sienna Cichlid, Navy Flounder and Dimgrey 682 * Cavefish. For these ASICs, the target voltage calculation can be 683 * illustrated by "voltage = voltage calculated from v/f curve + 684 * overdrive vddgfx offset" 685 * 686 * - a list of valid ranges for sclk, mclk, and voltage curve points 687 * labeled OD_RANGE 688 * 689 * < For APUs > 690 * 691 * Reading the file will display: 692 * 693 * - minimum and maximum engine clock labeled OD_SCLK 694 * 695 * - a list of valid ranges for sclk labeled OD_RANGE 696 * 697 * < For VanGogh > 698 * 699 * Reading the file will display: 700 * 701 * - minimum and maximum engine clock labeled OD_SCLK 702 * - minimum and maximum core clocks labeled OD_CCLK 703 * 704 * - a list of valid ranges for sclk and cclk labeled OD_RANGE 705 * 706 * To manually adjust these settings: 707 * 708 * - First select manual using power_dpm_force_performance_level 709 * 710 * - For clock frequency setting, enter a new value by writing a 711 * string that contains "s/m index clock" to the file. The index 712 * should be 0 if to set minimum clock. And 1 if to set maximum 713 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz. 714 * "m 1 800" will update maximum mclk to be 800Mhz. For core 715 * clocks on VanGogh, the string contains "p core index clock". 716 * E.g., "p 2 0 800" would set the minimum core clock on core 717 * 2 to 800Mhz. 718 * 719 * For sclk voltage curve, enter the new values by writing a 720 * string that contains "vc point clock voltage" to the file. The 721 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will 722 * update point1 with clock set as 300Mhz and voltage as 723 * 600mV. "vc 2 1000 1000" will update point3 with clock set 724 * as 1000Mhz and voltage 1000mV. 725 * 726 * To update the voltage offset applied for gfxclk/voltage calculation, 727 * enter the new value by writing a string that contains "vo offset". 728 * This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish. 729 * And the offset can be a positive or negative value. 730 * 731 * - When you have edited all of the states as needed, write "c" (commit) 732 * to the file to commit your changes 733 * 734 * - If you want to reset to the default power levels, write "r" (reset) 735 * to the file to reset them 736 * 737 */ 738 739 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev, 740 struct device_attribute *attr, 741 const char *buf, 742 size_t count) 743 { 744 struct drm_device *ddev = dev_get_drvdata(dev); 745 struct amdgpu_device *adev = drm_to_adev(ddev); 746 int ret; 747 uint32_t parameter_size = 0; 748 long parameter[64]; 749 char buf_cpy[128]; 750 char *tmp_str; 751 char *sub_str; 752 const char delimiter[3] = {' ', '\n', '\0'}; 753 uint32_t type; 754 755 if (amdgpu_in_reset(adev)) 756 return -EPERM; 757 if (adev->in_suspend && !adev->in_runpm) 758 return -EPERM; 759 760 if (count > 127) 761 return -EINVAL; 762 763 if (*buf == 's') 764 type = PP_OD_EDIT_SCLK_VDDC_TABLE; 765 else if (*buf == 'p') 766 type = PP_OD_EDIT_CCLK_VDDC_TABLE; 767 else if (*buf == 'm') 768 type = PP_OD_EDIT_MCLK_VDDC_TABLE; 769 else if(*buf == 'r') 770 type = PP_OD_RESTORE_DEFAULT_TABLE; 771 else if (*buf == 'c') 772 type = PP_OD_COMMIT_DPM_TABLE; 773 else if (!strncmp(buf, "vc", 2)) 774 type = PP_OD_EDIT_VDDC_CURVE; 775 else if (!strncmp(buf, "vo", 2)) 776 type = PP_OD_EDIT_VDDGFX_OFFSET; 777 else 778 return -EINVAL; 779 780 memcpy(buf_cpy, buf, count+1); 781 782 tmp_str = buf_cpy; 783 784 if ((type == PP_OD_EDIT_VDDC_CURVE) || 785 (type == PP_OD_EDIT_VDDGFX_OFFSET)) 786 tmp_str++; 787 while (isspace(*++tmp_str)); 788 789 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) { 790 if (strlen(sub_str) == 0) 791 continue; 792 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]); 793 if (ret) 794 return -EINVAL; 795 parameter_size++; 796 797 while (isspace(*tmp_str)) 798 tmp_str++; 799 } 800 801 ret = pm_runtime_get_sync(ddev->dev); 802 if (ret < 0) { 803 pm_runtime_put_autosuspend(ddev->dev); 804 return ret; 805 } 806 807 if (amdgpu_dpm_set_fine_grain_clk_vol(adev, 808 type, 809 parameter, 810 parameter_size)) 811 goto err_out; 812 813 if (amdgpu_dpm_odn_edit_dpm_table(adev, type, 814 parameter, parameter_size)) 815 goto err_out; 816 817 if (type == PP_OD_COMMIT_DPM_TABLE) { 818 if (amdgpu_dpm_dispatch_task(adev, 819 AMD_PP_TASK_READJUST_POWER_STATE, 820 NULL)) 821 goto err_out; 822 } 823 824 pm_runtime_mark_last_busy(ddev->dev); 825 pm_runtime_put_autosuspend(ddev->dev); 826 827 return count; 828 829 err_out: 830 pm_runtime_mark_last_busy(ddev->dev); 831 pm_runtime_put_autosuspend(ddev->dev); 832 return -EINVAL; 833 } 834 835 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev, 836 struct device_attribute *attr, 837 char *buf) 838 { 839 struct drm_device *ddev = dev_get_drvdata(dev); 840 struct amdgpu_device *adev = drm_to_adev(ddev); 841 int size = 0; 842 int ret; 843 enum pp_clock_type od_clocks[6] = { 844 OD_SCLK, 845 OD_MCLK, 846 OD_VDDC_CURVE, 847 OD_RANGE, 848 OD_VDDGFX_OFFSET, 849 OD_CCLK, 850 }; 851 uint clk_index; 852 853 if (amdgpu_in_reset(adev)) 854 return -EPERM; 855 if (adev->in_suspend && !adev->in_runpm) 856 return -EPERM; 857 858 ret = pm_runtime_get_sync(ddev->dev); 859 if (ret < 0) { 860 pm_runtime_put_autosuspend(ddev->dev); 861 return ret; 862 } 863 864 for (clk_index = 0 ; clk_index < 6 ; clk_index++) { 865 ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size); 866 if (ret) 867 break; 868 } 869 if (ret == -ENOENT) { 870 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf); 871 if (size > 0) { 872 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size); 873 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size); 874 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size); 875 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size); 876 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size); 877 } 878 } 879 880 if (size == 0) 881 size = sysfs_emit(buf, "\n"); 882 883 pm_runtime_mark_last_busy(ddev->dev); 884 pm_runtime_put_autosuspend(ddev->dev); 885 886 return size; 887 } 888 889 /** 890 * DOC: pp_features 891 * 892 * The amdgpu driver provides a sysfs API for adjusting what powerplay 893 * features to be enabled. The file pp_features is used for this. And 894 * this is only available for Vega10 and later dGPUs. 895 * 896 * Reading back the file will show you the followings: 897 * - Current ppfeature masks 898 * - List of the all supported powerplay features with their naming, 899 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled"). 900 * 901 * To manually enable or disable a specific feature, just set or clear 902 * the corresponding bit from original ppfeature masks and input the 903 * new ppfeature masks. 904 */ 905 static ssize_t amdgpu_set_pp_features(struct device *dev, 906 struct device_attribute *attr, 907 const char *buf, 908 size_t count) 909 { 910 struct drm_device *ddev = dev_get_drvdata(dev); 911 struct amdgpu_device *adev = drm_to_adev(ddev); 912 uint64_t featuremask; 913 int ret; 914 915 if (amdgpu_in_reset(adev)) 916 return -EPERM; 917 if (adev->in_suspend && !adev->in_runpm) 918 return -EPERM; 919 920 ret = kstrtou64(buf, 0, &featuremask); 921 if (ret) 922 return -EINVAL; 923 924 ret = pm_runtime_get_sync(ddev->dev); 925 if (ret < 0) { 926 pm_runtime_put_autosuspend(ddev->dev); 927 return ret; 928 } 929 930 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask); 931 932 pm_runtime_mark_last_busy(ddev->dev); 933 pm_runtime_put_autosuspend(ddev->dev); 934 935 if (ret) 936 return -EINVAL; 937 938 return count; 939 } 940 941 static ssize_t amdgpu_get_pp_features(struct device *dev, 942 struct device_attribute *attr, 943 char *buf) 944 { 945 struct drm_device *ddev = dev_get_drvdata(dev); 946 struct amdgpu_device *adev = drm_to_adev(ddev); 947 ssize_t size; 948 int ret; 949 950 if (amdgpu_in_reset(adev)) 951 return -EPERM; 952 if (adev->in_suspend && !adev->in_runpm) 953 return -EPERM; 954 955 ret = pm_runtime_get_sync(ddev->dev); 956 if (ret < 0) { 957 pm_runtime_put_autosuspend(ddev->dev); 958 return ret; 959 } 960 961 size = amdgpu_dpm_get_ppfeature_status(adev, buf); 962 if (size <= 0) 963 size = sysfs_emit(buf, "\n"); 964 965 pm_runtime_mark_last_busy(ddev->dev); 966 pm_runtime_put_autosuspend(ddev->dev); 967 968 return size; 969 } 970 971 /** 972 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie 973 * 974 * The amdgpu driver provides a sysfs API for adjusting what power levels 975 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk, 976 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for 977 * this. 978 * 979 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for 980 * Vega10 and later ASICs. 981 * pp_dpm_fclk interface is only available for Vega20 and later ASICs. 982 * 983 * Reading back the files will show you the available power levels within 984 * the power state and the clock information for those levels. 985 * 986 * To manually adjust these states, first select manual using 987 * power_dpm_force_performance_level. 988 * Secondly, enter a new value for each level by inputing a string that 989 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie" 990 * E.g., 991 * 992 * .. code-block:: bash 993 * 994 * echo "4 5 6" > pp_dpm_sclk 995 * 996 * will enable sclk levels 4, 5, and 6. 997 * 998 * NOTE: change to the dcefclk max dpm level is not supported now 999 */ 1000 1001 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev, 1002 enum pp_clock_type type, 1003 char *buf) 1004 { 1005 struct drm_device *ddev = dev_get_drvdata(dev); 1006 struct amdgpu_device *adev = drm_to_adev(ddev); 1007 int size = 0; 1008 int ret = 0; 1009 1010 if (amdgpu_in_reset(adev)) 1011 return -EPERM; 1012 if (adev->in_suspend && !adev->in_runpm) 1013 return -EPERM; 1014 1015 ret = pm_runtime_get_sync(ddev->dev); 1016 if (ret < 0) { 1017 pm_runtime_put_autosuspend(ddev->dev); 1018 return ret; 1019 } 1020 1021 ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size); 1022 if (ret == -ENOENT) 1023 size = amdgpu_dpm_print_clock_levels(adev, type, buf); 1024 1025 if (size == 0) 1026 size = sysfs_emit(buf, "\n"); 1027 1028 pm_runtime_mark_last_busy(ddev->dev); 1029 pm_runtime_put_autosuspend(ddev->dev); 1030 1031 return size; 1032 } 1033 1034 /* 1035 * Worst case: 32 bits individually specified, in octal at 12 characters 1036 * per line (+1 for \n). 1037 */ 1038 #define AMDGPU_MASK_BUF_MAX (32 * 13) 1039 1040 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask) 1041 { 1042 int ret; 1043 unsigned long level; 1044 char *sub_str = NULL; 1045 char *tmp; 1046 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1]; 1047 const char delimiter[3] = {' ', '\n', '\0'}; 1048 size_t bytes; 1049 1050 *mask = 0; 1051 1052 bytes = min(count, sizeof(buf_cpy) - 1); 1053 memcpy(buf_cpy, buf, bytes); 1054 buf_cpy[bytes] = '\0'; 1055 tmp = buf_cpy; 1056 while ((sub_str = strsep(&tmp, delimiter)) != NULL) { 1057 if (strlen(sub_str)) { 1058 ret = kstrtoul(sub_str, 0, &level); 1059 if (ret || level > 31) 1060 return -EINVAL; 1061 *mask |= 1 << level; 1062 } else 1063 break; 1064 } 1065 1066 return 0; 1067 } 1068 1069 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev, 1070 enum pp_clock_type type, 1071 const char *buf, 1072 size_t count) 1073 { 1074 struct drm_device *ddev = dev_get_drvdata(dev); 1075 struct amdgpu_device *adev = drm_to_adev(ddev); 1076 int ret; 1077 uint32_t mask = 0; 1078 1079 if (amdgpu_in_reset(adev)) 1080 return -EPERM; 1081 if (adev->in_suspend && !adev->in_runpm) 1082 return -EPERM; 1083 1084 ret = amdgpu_read_mask(buf, count, &mask); 1085 if (ret) 1086 return ret; 1087 1088 ret = pm_runtime_get_sync(ddev->dev); 1089 if (ret < 0) { 1090 pm_runtime_put_autosuspend(ddev->dev); 1091 return ret; 1092 } 1093 1094 ret = amdgpu_dpm_force_clock_level(adev, type, mask); 1095 1096 pm_runtime_mark_last_busy(ddev->dev); 1097 pm_runtime_put_autosuspend(ddev->dev); 1098 1099 if (ret) 1100 return -EINVAL; 1101 1102 return count; 1103 } 1104 1105 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev, 1106 struct device_attribute *attr, 1107 char *buf) 1108 { 1109 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf); 1110 } 1111 1112 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev, 1113 struct device_attribute *attr, 1114 const char *buf, 1115 size_t count) 1116 { 1117 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count); 1118 } 1119 1120 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev, 1121 struct device_attribute *attr, 1122 char *buf) 1123 { 1124 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf); 1125 } 1126 1127 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev, 1128 struct device_attribute *attr, 1129 const char *buf, 1130 size_t count) 1131 { 1132 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count); 1133 } 1134 1135 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev, 1136 struct device_attribute *attr, 1137 char *buf) 1138 { 1139 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf); 1140 } 1141 1142 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev, 1143 struct device_attribute *attr, 1144 const char *buf, 1145 size_t count) 1146 { 1147 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count); 1148 } 1149 1150 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev, 1151 struct device_attribute *attr, 1152 char *buf) 1153 { 1154 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf); 1155 } 1156 1157 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev, 1158 struct device_attribute *attr, 1159 const char *buf, 1160 size_t count) 1161 { 1162 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count); 1163 } 1164 1165 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev, 1166 struct device_attribute *attr, 1167 char *buf) 1168 { 1169 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf); 1170 } 1171 1172 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev, 1173 struct device_attribute *attr, 1174 const char *buf, 1175 size_t count) 1176 { 1177 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count); 1178 } 1179 1180 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev, 1181 struct device_attribute *attr, 1182 char *buf) 1183 { 1184 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf); 1185 } 1186 1187 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev, 1188 struct device_attribute *attr, 1189 const char *buf, 1190 size_t count) 1191 { 1192 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count); 1193 } 1194 1195 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev, 1196 struct device_attribute *attr, 1197 char *buf) 1198 { 1199 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf); 1200 } 1201 1202 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev, 1203 struct device_attribute *attr, 1204 const char *buf, 1205 size_t count) 1206 { 1207 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count); 1208 } 1209 1210 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev, 1211 struct device_attribute *attr, 1212 char *buf) 1213 { 1214 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf); 1215 } 1216 1217 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev, 1218 struct device_attribute *attr, 1219 const char *buf, 1220 size_t count) 1221 { 1222 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count); 1223 } 1224 1225 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev, 1226 struct device_attribute *attr, 1227 char *buf) 1228 { 1229 struct drm_device *ddev = dev_get_drvdata(dev); 1230 struct amdgpu_device *adev = drm_to_adev(ddev); 1231 uint32_t value = 0; 1232 int ret; 1233 1234 if (amdgpu_in_reset(adev)) 1235 return -EPERM; 1236 if (adev->in_suspend && !adev->in_runpm) 1237 return -EPERM; 1238 1239 ret = pm_runtime_get_sync(ddev->dev); 1240 if (ret < 0) { 1241 pm_runtime_put_autosuspend(ddev->dev); 1242 return ret; 1243 } 1244 1245 value = amdgpu_dpm_get_sclk_od(adev); 1246 1247 pm_runtime_mark_last_busy(ddev->dev); 1248 pm_runtime_put_autosuspend(ddev->dev); 1249 1250 return sysfs_emit(buf, "%d\n", value); 1251 } 1252 1253 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev, 1254 struct device_attribute *attr, 1255 const char *buf, 1256 size_t count) 1257 { 1258 struct drm_device *ddev = dev_get_drvdata(dev); 1259 struct amdgpu_device *adev = drm_to_adev(ddev); 1260 int ret; 1261 long int value; 1262 1263 if (amdgpu_in_reset(adev)) 1264 return -EPERM; 1265 if (adev->in_suspend && !adev->in_runpm) 1266 return -EPERM; 1267 1268 ret = kstrtol(buf, 0, &value); 1269 1270 if (ret) 1271 return -EINVAL; 1272 1273 ret = pm_runtime_get_sync(ddev->dev); 1274 if (ret < 0) { 1275 pm_runtime_put_autosuspend(ddev->dev); 1276 return ret; 1277 } 1278 1279 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value); 1280 1281 pm_runtime_mark_last_busy(ddev->dev); 1282 pm_runtime_put_autosuspend(ddev->dev); 1283 1284 return count; 1285 } 1286 1287 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev, 1288 struct device_attribute *attr, 1289 char *buf) 1290 { 1291 struct drm_device *ddev = dev_get_drvdata(dev); 1292 struct amdgpu_device *adev = drm_to_adev(ddev); 1293 uint32_t value = 0; 1294 int ret; 1295 1296 if (amdgpu_in_reset(adev)) 1297 return -EPERM; 1298 if (adev->in_suspend && !adev->in_runpm) 1299 return -EPERM; 1300 1301 ret = pm_runtime_get_sync(ddev->dev); 1302 if (ret < 0) { 1303 pm_runtime_put_autosuspend(ddev->dev); 1304 return ret; 1305 } 1306 1307 value = amdgpu_dpm_get_mclk_od(adev); 1308 1309 pm_runtime_mark_last_busy(ddev->dev); 1310 pm_runtime_put_autosuspend(ddev->dev); 1311 1312 return sysfs_emit(buf, "%d\n", value); 1313 } 1314 1315 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev, 1316 struct device_attribute *attr, 1317 const char *buf, 1318 size_t count) 1319 { 1320 struct drm_device *ddev = dev_get_drvdata(dev); 1321 struct amdgpu_device *adev = drm_to_adev(ddev); 1322 int ret; 1323 long int value; 1324 1325 if (amdgpu_in_reset(adev)) 1326 return -EPERM; 1327 if (adev->in_suspend && !adev->in_runpm) 1328 return -EPERM; 1329 1330 ret = kstrtol(buf, 0, &value); 1331 1332 if (ret) 1333 return -EINVAL; 1334 1335 ret = pm_runtime_get_sync(ddev->dev); 1336 if (ret < 0) { 1337 pm_runtime_put_autosuspend(ddev->dev); 1338 return ret; 1339 } 1340 1341 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value); 1342 1343 pm_runtime_mark_last_busy(ddev->dev); 1344 pm_runtime_put_autosuspend(ddev->dev); 1345 1346 return count; 1347 } 1348 1349 /** 1350 * DOC: pp_power_profile_mode 1351 * 1352 * The amdgpu driver provides a sysfs API for adjusting the heuristics 1353 * related to switching between power levels in a power state. The file 1354 * pp_power_profile_mode is used for this. 1355 * 1356 * Reading this file outputs a list of all of the predefined power profiles 1357 * and the relevant heuristics settings for that profile. 1358 * 1359 * To select a profile or create a custom profile, first select manual using 1360 * power_dpm_force_performance_level. Writing the number of a predefined 1361 * profile to pp_power_profile_mode will enable those heuristics. To 1362 * create a custom set of heuristics, write a string of numbers to the file 1363 * starting with the number of the custom profile along with a setting 1364 * for each heuristic parameter. Due to differences across asic families 1365 * the heuristic parameters vary from family to family. 1366 * 1367 */ 1368 1369 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev, 1370 struct device_attribute *attr, 1371 char *buf) 1372 { 1373 struct drm_device *ddev = dev_get_drvdata(dev); 1374 struct amdgpu_device *adev = drm_to_adev(ddev); 1375 ssize_t size; 1376 int ret; 1377 1378 if (amdgpu_in_reset(adev)) 1379 return -EPERM; 1380 if (adev->in_suspend && !adev->in_runpm) 1381 return -EPERM; 1382 1383 ret = pm_runtime_get_sync(ddev->dev); 1384 if (ret < 0) { 1385 pm_runtime_put_autosuspend(ddev->dev); 1386 return ret; 1387 } 1388 1389 size = amdgpu_dpm_get_power_profile_mode(adev, buf); 1390 if (size <= 0) 1391 size = sysfs_emit(buf, "\n"); 1392 1393 pm_runtime_mark_last_busy(ddev->dev); 1394 pm_runtime_put_autosuspend(ddev->dev); 1395 1396 return size; 1397 } 1398 1399 1400 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev, 1401 struct device_attribute *attr, 1402 const char *buf, 1403 size_t count) 1404 { 1405 int ret; 1406 struct drm_device *ddev = dev_get_drvdata(dev); 1407 struct amdgpu_device *adev = drm_to_adev(ddev); 1408 uint32_t parameter_size = 0; 1409 long parameter[64]; 1410 char *sub_str, buf_cpy[128]; 1411 char *tmp_str; 1412 uint32_t i = 0; 1413 char tmp[2]; 1414 long int profile_mode = 0; 1415 const char delimiter[3] = {' ', '\n', '\0'}; 1416 1417 if (amdgpu_in_reset(adev)) 1418 return -EPERM; 1419 if (adev->in_suspend && !adev->in_runpm) 1420 return -EPERM; 1421 1422 tmp[0] = *(buf); 1423 tmp[1] = '\0'; 1424 ret = kstrtol(tmp, 0, &profile_mode); 1425 if (ret) 1426 return -EINVAL; 1427 1428 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) { 1429 if (count < 2 || count > 127) 1430 return -EINVAL; 1431 while (isspace(*++buf)) 1432 i++; 1433 memcpy(buf_cpy, buf, count-i); 1434 tmp_str = buf_cpy; 1435 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) { 1436 if (strlen(sub_str) == 0) 1437 continue; 1438 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]); 1439 if (ret) 1440 return -EINVAL; 1441 parameter_size++; 1442 while (isspace(*tmp_str)) 1443 tmp_str++; 1444 } 1445 } 1446 parameter[parameter_size] = profile_mode; 1447 1448 ret = pm_runtime_get_sync(ddev->dev); 1449 if (ret < 0) { 1450 pm_runtime_put_autosuspend(ddev->dev); 1451 return ret; 1452 } 1453 1454 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size); 1455 1456 pm_runtime_mark_last_busy(ddev->dev); 1457 pm_runtime_put_autosuspend(ddev->dev); 1458 1459 if (!ret) 1460 return count; 1461 1462 return -EINVAL; 1463 } 1464 1465 /** 1466 * DOC: gpu_busy_percent 1467 * 1468 * The amdgpu driver provides a sysfs API for reading how busy the GPU 1469 * is as a percentage. The file gpu_busy_percent is used for this. 1470 * The SMU firmware computes a percentage of load based on the 1471 * aggregate activity level in the IP cores. 1472 */ 1473 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev, 1474 struct device_attribute *attr, 1475 char *buf) 1476 { 1477 struct drm_device *ddev = dev_get_drvdata(dev); 1478 struct amdgpu_device *adev = drm_to_adev(ddev); 1479 int r, value, size = sizeof(value); 1480 1481 if (amdgpu_in_reset(adev)) 1482 return -EPERM; 1483 if (adev->in_suspend && !adev->in_runpm) 1484 return -EPERM; 1485 1486 r = pm_runtime_get_sync(ddev->dev); 1487 if (r < 0) { 1488 pm_runtime_put_autosuspend(ddev->dev); 1489 return r; 1490 } 1491 1492 /* read the IP busy sensor */ 1493 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, 1494 (void *)&value, &size); 1495 1496 pm_runtime_mark_last_busy(ddev->dev); 1497 pm_runtime_put_autosuspend(ddev->dev); 1498 1499 if (r) 1500 return r; 1501 1502 return sysfs_emit(buf, "%d\n", value); 1503 } 1504 1505 /** 1506 * DOC: mem_busy_percent 1507 * 1508 * The amdgpu driver provides a sysfs API for reading how busy the VRAM 1509 * is as a percentage. The file mem_busy_percent is used for this. 1510 * The SMU firmware computes a percentage of load based on the 1511 * aggregate activity level in the IP cores. 1512 */ 1513 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev, 1514 struct device_attribute *attr, 1515 char *buf) 1516 { 1517 struct drm_device *ddev = dev_get_drvdata(dev); 1518 struct amdgpu_device *adev = drm_to_adev(ddev); 1519 int r, value, size = sizeof(value); 1520 1521 if (amdgpu_in_reset(adev)) 1522 return -EPERM; 1523 if (adev->in_suspend && !adev->in_runpm) 1524 return -EPERM; 1525 1526 r = pm_runtime_get_sync(ddev->dev); 1527 if (r < 0) { 1528 pm_runtime_put_autosuspend(ddev->dev); 1529 return r; 1530 } 1531 1532 /* read the IP busy sensor */ 1533 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, 1534 (void *)&value, &size); 1535 1536 pm_runtime_mark_last_busy(ddev->dev); 1537 pm_runtime_put_autosuspend(ddev->dev); 1538 1539 if (r) 1540 return r; 1541 1542 return sysfs_emit(buf, "%d\n", value); 1543 } 1544 1545 /** 1546 * DOC: pcie_bw 1547 * 1548 * The amdgpu driver provides a sysfs API for estimating how much data 1549 * has been received and sent by the GPU in the last second through PCIe. 1550 * The file pcie_bw is used for this. 1551 * The Perf counters count the number of received and sent messages and return 1552 * those values, as well as the maximum payload size of a PCIe packet (mps). 1553 * Note that it is not possible to easily and quickly obtain the size of each 1554 * packet transmitted, so we output the max payload size (mps) to allow for 1555 * quick estimation of the PCIe bandwidth usage 1556 */ 1557 static ssize_t amdgpu_get_pcie_bw(struct device *dev, 1558 struct device_attribute *attr, 1559 char *buf) 1560 { 1561 struct drm_device *ddev = dev_get_drvdata(dev); 1562 struct amdgpu_device *adev = drm_to_adev(ddev); 1563 uint64_t count0 = 0, count1 = 0; 1564 int ret; 1565 1566 if (amdgpu_in_reset(adev)) 1567 return -EPERM; 1568 if (adev->in_suspend && !adev->in_runpm) 1569 return -EPERM; 1570 1571 if (adev->flags & AMD_IS_APU) 1572 return -ENODATA; 1573 1574 if (!adev->asic_funcs->get_pcie_usage) 1575 return -ENODATA; 1576 1577 ret = pm_runtime_get_sync(ddev->dev); 1578 if (ret < 0) { 1579 pm_runtime_put_autosuspend(ddev->dev); 1580 return ret; 1581 } 1582 1583 amdgpu_asic_get_pcie_usage(adev, &count0, &count1); 1584 1585 pm_runtime_mark_last_busy(ddev->dev); 1586 pm_runtime_put_autosuspend(ddev->dev); 1587 1588 return sysfs_emit(buf, "%llu %llu %i\n", 1589 count0, count1, pcie_get_mps(adev->pdev)); 1590 } 1591 1592 /** 1593 * DOC: unique_id 1594 * 1595 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU 1596 * The file unique_id is used for this. 1597 * This will provide a Unique ID that will persist from machine to machine 1598 * 1599 * NOTE: This will only work for GFX9 and newer. This file will be absent 1600 * on unsupported ASICs (GFX8 and older) 1601 */ 1602 static ssize_t amdgpu_get_unique_id(struct device *dev, 1603 struct device_attribute *attr, 1604 char *buf) 1605 { 1606 struct drm_device *ddev = dev_get_drvdata(dev); 1607 struct amdgpu_device *adev = drm_to_adev(ddev); 1608 1609 if (amdgpu_in_reset(adev)) 1610 return -EPERM; 1611 if (adev->in_suspend && !adev->in_runpm) 1612 return -EPERM; 1613 1614 if (adev->unique_id) 1615 return sysfs_emit(buf, "%016llx\n", adev->unique_id); 1616 1617 return 0; 1618 } 1619 1620 /** 1621 * DOC: thermal_throttling_logging 1622 * 1623 * Thermal throttling pulls down the clock frequency and thus the performance. 1624 * It's an useful mechanism to protect the chip from overheating. Since it 1625 * impacts performance, the user controls whether it is enabled and if so, 1626 * the log frequency. 1627 * 1628 * Reading back the file shows you the status(enabled or disabled) and 1629 * the interval(in seconds) between each thermal logging. 1630 * 1631 * Writing an integer to the file, sets a new logging interval, in seconds. 1632 * The value should be between 1 and 3600. If the value is less than 1, 1633 * thermal logging is disabled. Values greater than 3600 are ignored. 1634 */ 1635 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev, 1636 struct device_attribute *attr, 1637 char *buf) 1638 { 1639 struct drm_device *ddev = dev_get_drvdata(dev); 1640 struct amdgpu_device *adev = drm_to_adev(ddev); 1641 1642 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n", 1643 adev_to_drm(adev)->unique, 1644 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled", 1645 adev->throttling_logging_rs.interval / HZ + 1); 1646 } 1647 1648 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev, 1649 struct device_attribute *attr, 1650 const char *buf, 1651 size_t count) 1652 { 1653 struct drm_device *ddev = dev_get_drvdata(dev); 1654 struct amdgpu_device *adev = drm_to_adev(ddev); 1655 long throttling_logging_interval; 1656 unsigned long flags; 1657 int ret = 0; 1658 1659 ret = kstrtol(buf, 0, &throttling_logging_interval); 1660 if (ret) 1661 return ret; 1662 1663 if (throttling_logging_interval > 3600) 1664 return -EINVAL; 1665 1666 if (throttling_logging_interval > 0) { 1667 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags); 1668 /* 1669 * Reset the ratelimit timer internals. 1670 * This can effectively restart the timer. 1671 */ 1672 adev->throttling_logging_rs.interval = 1673 (throttling_logging_interval - 1) * HZ; 1674 adev->throttling_logging_rs.begin = 0; 1675 adev->throttling_logging_rs.printed = 0; 1676 adev->throttling_logging_rs.missed = 0; 1677 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags); 1678 1679 atomic_set(&adev->throttling_logging_enabled, 1); 1680 } else { 1681 atomic_set(&adev->throttling_logging_enabled, 0); 1682 } 1683 1684 return count; 1685 } 1686 1687 /** 1688 * DOC: gpu_metrics 1689 * 1690 * The amdgpu driver provides a sysfs API for retrieving current gpu 1691 * metrics data. The file gpu_metrics is used for this. Reading the 1692 * file will dump all the current gpu metrics data. 1693 * 1694 * These data include temperature, frequency, engines utilization, 1695 * power consume, throttler status, fan speed and cpu core statistics( 1696 * available for APU only). That's it will give a snapshot of all sensors 1697 * at the same time. 1698 */ 1699 static ssize_t amdgpu_get_gpu_metrics(struct device *dev, 1700 struct device_attribute *attr, 1701 char *buf) 1702 { 1703 struct drm_device *ddev = dev_get_drvdata(dev); 1704 struct amdgpu_device *adev = drm_to_adev(ddev); 1705 void *gpu_metrics; 1706 ssize_t size = 0; 1707 int ret; 1708 1709 if (amdgpu_in_reset(adev)) 1710 return -EPERM; 1711 if (adev->in_suspend && !adev->in_runpm) 1712 return -EPERM; 1713 1714 ret = pm_runtime_get_sync(ddev->dev); 1715 if (ret < 0) { 1716 pm_runtime_put_autosuspend(ddev->dev); 1717 return ret; 1718 } 1719 1720 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics); 1721 if (size <= 0) 1722 goto out; 1723 1724 if (size >= PAGE_SIZE) 1725 size = PAGE_SIZE - 1; 1726 1727 memcpy(buf, gpu_metrics, size); 1728 1729 out: 1730 pm_runtime_mark_last_busy(ddev->dev); 1731 pm_runtime_put_autosuspend(ddev->dev); 1732 1733 return size; 1734 } 1735 1736 /** 1737 * DOC: smartshift_apu_power 1738 * 1739 * The amdgpu driver provides a sysfs API for reporting APU power 1740 * share if it supports smartshift. The value is expressed as 1741 * the proportion of stapm limit where stapm limit is the total APU 1742 * power limit. The result is in percentage. If APU power is 130% of 1743 * STAPM, then APU is using 30% of the dGPU's headroom. 1744 */ 1745 1746 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr, 1747 char *buf) 1748 { 1749 struct drm_device *ddev = dev_get_drvdata(dev); 1750 struct amdgpu_device *adev = drm_to_adev(ddev); 1751 uint32_t ss_power, size; 1752 int r = 0; 1753 1754 if (amdgpu_in_reset(adev)) 1755 return -EPERM; 1756 if (adev->in_suspend && !adev->in_runpm) 1757 return -EPERM; 1758 1759 r = pm_runtime_get_sync(ddev->dev); 1760 if (r < 0) { 1761 pm_runtime_put_autosuspend(ddev->dev); 1762 return r; 1763 } 1764 1765 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 1766 (void *)&ss_power, &size); 1767 if (r) 1768 goto out; 1769 1770 r = sysfs_emit(buf, "%u%%\n", ss_power); 1771 1772 out: 1773 pm_runtime_mark_last_busy(ddev->dev); 1774 pm_runtime_put_autosuspend(ddev->dev); 1775 return r; 1776 } 1777 1778 /** 1779 * DOC: smartshift_dgpu_power 1780 * 1781 * The amdgpu driver provides a sysfs API for reporting the dGPU power 1782 * share if the device is in HG and supports smartshift. The value 1783 * is expressed as the proportion of stapm limit where stapm limit 1784 * is the total APU power limit. The value is in percentage. If dGPU 1785 * power is 20% higher than STAPM power(120%), it's using 20% of the 1786 * APU's power headroom. 1787 */ 1788 1789 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr, 1790 char *buf) 1791 { 1792 struct drm_device *ddev = dev_get_drvdata(dev); 1793 struct amdgpu_device *adev = drm_to_adev(ddev); 1794 uint32_t ss_power, size; 1795 int r = 0; 1796 1797 if (amdgpu_in_reset(adev)) 1798 return -EPERM; 1799 if (adev->in_suspend && !adev->in_runpm) 1800 return -EPERM; 1801 1802 r = pm_runtime_get_sync(ddev->dev); 1803 if (r < 0) { 1804 pm_runtime_put_autosuspend(ddev->dev); 1805 return r; 1806 } 1807 1808 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 1809 (void *)&ss_power, &size); 1810 1811 if (r) 1812 goto out; 1813 1814 r = sysfs_emit(buf, "%u%%\n", ss_power); 1815 1816 out: 1817 pm_runtime_mark_last_busy(ddev->dev); 1818 pm_runtime_put_autosuspend(ddev->dev); 1819 return r; 1820 } 1821 1822 /** 1823 * DOC: smartshift_bias 1824 * 1825 * The amdgpu driver provides a sysfs API for reporting the 1826 * smartshift(SS2.0) bias level. The value ranges from -100 to 100 1827 * and the default is 0. -100 sets maximum preference to APU 1828 * and 100 sets max perference to dGPU. 1829 */ 1830 1831 static ssize_t amdgpu_get_smartshift_bias(struct device *dev, 1832 struct device_attribute *attr, 1833 char *buf) 1834 { 1835 int r = 0; 1836 1837 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias); 1838 1839 return r; 1840 } 1841 1842 static ssize_t amdgpu_set_smartshift_bias(struct device *dev, 1843 struct device_attribute *attr, 1844 const char *buf, size_t count) 1845 { 1846 struct drm_device *ddev = dev_get_drvdata(dev); 1847 struct amdgpu_device *adev = drm_to_adev(ddev); 1848 int r = 0; 1849 int bias = 0; 1850 1851 if (amdgpu_in_reset(adev)) 1852 return -EPERM; 1853 if (adev->in_suspend && !adev->in_runpm) 1854 return -EPERM; 1855 1856 r = pm_runtime_get_sync(ddev->dev); 1857 if (r < 0) { 1858 pm_runtime_put_autosuspend(ddev->dev); 1859 return r; 1860 } 1861 1862 r = kstrtoint(buf, 10, &bias); 1863 if (r) 1864 goto out; 1865 1866 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS) 1867 bias = AMDGPU_SMARTSHIFT_MAX_BIAS; 1868 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS) 1869 bias = AMDGPU_SMARTSHIFT_MIN_BIAS; 1870 1871 amdgpu_smartshift_bias = bias; 1872 r = count; 1873 1874 /* TODO: update bias level with SMU message */ 1875 1876 out: 1877 pm_runtime_mark_last_busy(ddev->dev); 1878 pm_runtime_put_autosuspend(ddev->dev); 1879 return r; 1880 } 1881 1882 1883 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1884 uint32_t mask, enum amdgpu_device_attr_states *states) 1885 { 1886 uint32_t ss_power, size; 1887 1888 if (!amdgpu_acpi_is_power_shift_control_supported()) 1889 *states = ATTR_STATE_UNSUPPORTED; 1890 else if ((adev->flags & AMD_IS_PX) && 1891 !amdgpu_device_supports_smart_shift(adev_to_drm(adev))) 1892 *states = ATTR_STATE_UNSUPPORTED; 1893 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 1894 (void *)&ss_power, &size)) 1895 *states = ATTR_STATE_UNSUPPORTED; 1896 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 1897 (void *)&ss_power, &size)) 1898 *states = ATTR_STATE_UNSUPPORTED; 1899 1900 return 0; 1901 } 1902 1903 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1904 uint32_t mask, enum amdgpu_device_attr_states *states) 1905 { 1906 uint32_t ss_power, size; 1907 1908 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev))) 1909 *states = ATTR_STATE_UNSUPPORTED; 1910 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE, 1911 (void *)&ss_power, &size)) 1912 *states = ATTR_STATE_UNSUPPORTED; 1913 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE, 1914 (void *)&ss_power, &size)) 1915 *states = ATTR_STATE_UNSUPPORTED; 1916 1917 return 0; 1918 } 1919 1920 static struct amdgpu_device_attr amdgpu_device_attrs[] = { 1921 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1922 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1923 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1924 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1925 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1926 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1927 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1928 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1929 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1930 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1931 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1932 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1933 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1934 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1935 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC), 1936 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC), 1937 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1938 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC), 1939 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1940 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1941 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC), 1942 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1943 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1944 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1945 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF), 1946 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC, 1947 .attr_update = ss_power_attr_update), 1948 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC, 1949 .attr_update = ss_power_attr_update), 1950 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC, 1951 .attr_update = ss_bias_attr_update), 1952 }; 1953 1954 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 1955 uint32_t mask, enum amdgpu_device_attr_states *states) 1956 { 1957 struct device_attribute *dev_attr = &attr->dev_attr; 1958 uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0]; 1959 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0]; 1960 const char *attr_name = dev_attr->attr.name; 1961 1962 if (!(attr->flags & mask)) { 1963 *states = ATTR_STATE_UNSUPPORTED; 1964 return 0; 1965 } 1966 1967 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name)) 1968 1969 if (DEVICE_ATTR_IS(pp_dpm_socclk)) { 1970 if (gc_ver < IP_VERSION(9, 0, 0)) 1971 *states = ATTR_STATE_UNSUPPORTED; 1972 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) { 1973 if (gc_ver < IP_VERSION(9, 0, 0) || 1974 gc_ver == IP_VERSION(9, 4, 1) || 1975 gc_ver == IP_VERSION(9, 4, 2)) 1976 *states = ATTR_STATE_UNSUPPORTED; 1977 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) { 1978 if (mp1_ver < IP_VERSION(10, 0, 0)) 1979 *states = ATTR_STATE_UNSUPPORTED; 1980 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) { 1981 *states = ATTR_STATE_UNSUPPORTED; 1982 if (amdgpu_dpm_is_overdrive_supported(adev)) 1983 *states = ATTR_STATE_SUPPORTED; 1984 } else if (DEVICE_ATTR_IS(mem_busy_percent)) { 1985 if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1)) 1986 *states = ATTR_STATE_UNSUPPORTED; 1987 } else if (DEVICE_ATTR_IS(pcie_bw)) { 1988 /* PCIe Perf counters won't work on APU nodes */ 1989 if (adev->flags & AMD_IS_APU) 1990 *states = ATTR_STATE_UNSUPPORTED; 1991 } else if (DEVICE_ATTR_IS(unique_id)) { 1992 switch (gc_ver) { 1993 case IP_VERSION(9, 0, 1): 1994 case IP_VERSION(9, 4, 0): 1995 case IP_VERSION(9, 4, 1): 1996 case IP_VERSION(9, 4, 2): 1997 case IP_VERSION(10, 3, 0): 1998 case IP_VERSION(11, 0, 0): 1999 *states = ATTR_STATE_SUPPORTED; 2000 break; 2001 default: 2002 *states = ATTR_STATE_UNSUPPORTED; 2003 } 2004 } else if (DEVICE_ATTR_IS(pp_features)) { 2005 if (adev->flags & AMD_IS_APU || gc_ver < IP_VERSION(9, 0, 0)) 2006 *states = ATTR_STATE_UNSUPPORTED; 2007 } else if (DEVICE_ATTR_IS(gpu_metrics)) { 2008 if (gc_ver < IP_VERSION(9, 1, 0)) 2009 *states = ATTR_STATE_UNSUPPORTED; 2010 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) { 2011 if (!(gc_ver == IP_VERSION(10, 3, 1) || 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 *states = ATTR_STATE_UNSUPPORTED; 2016 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) { 2017 if (!(gc_ver == IP_VERSION(10, 3, 1) || 2018 gc_ver == IP_VERSION(10, 3, 0) || 2019 gc_ver == IP_VERSION(10, 1, 2) || 2020 gc_ver == IP_VERSION(11, 0, 0))) 2021 *states = ATTR_STATE_UNSUPPORTED; 2022 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) { 2023 if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP) 2024 *states = ATTR_STATE_UNSUPPORTED; 2025 } 2026 2027 switch (gc_ver) { 2028 case IP_VERSION(9, 4, 1): 2029 case IP_VERSION(9, 4, 2): 2030 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */ 2031 if (DEVICE_ATTR_IS(pp_dpm_mclk) || 2032 DEVICE_ATTR_IS(pp_dpm_socclk) || 2033 DEVICE_ATTR_IS(pp_dpm_fclk)) { 2034 dev_attr->attr.mode &= ~S_IWUGO; 2035 dev_attr->store = NULL; 2036 } 2037 break; 2038 default: 2039 break; 2040 } 2041 2042 if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) { 2043 /* SMU MP1 does not support dcefclk level setting */ 2044 if (gc_ver >= IP_VERSION(10, 0, 0)) { 2045 dev_attr->attr.mode &= ~S_IWUGO; 2046 dev_attr->store = NULL; 2047 } 2048 } 2049 2050 /* setting should not be allowed from VF if not in one VF mode */ 2051 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) { 2052 dev_attr->attr.mode &= ~S_IWUGO; 2053 dev_attr->store = NULL; 2054 } 2055 2056 #undef DEVICE_ATTR_IS 2057 2058 return 0; 2059 } 2060 2061 2062 static int amdgpu_device_attr_create(struct amdgpu_device *adev, 2063 struct amdgpu_device_attr *attr, 2064 uint32_t mask, struct list_head *attr_list) 2065 { 2066 int ret = 0; 2067 struct device_attribute *dev_attr = &attr->dev_attr; 2068 const char *name = dev_attr->attr.name; 2069 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED; 2070 struct amdgpu_device_attr_entry *attr_entry; 2071 2072 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr, 2073 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update; 2074 2075 BUG_ON(!attr); 2076 2077 attr_update = attr->attr_update ? attr->attr_update : default_attr_update; 2078 2079 ret = attr_update(adev, attr, mask, &attr_states); 2080 if (ret) { 2081 dev_err(adev->dev, "failed to update device file %s, ret = %d\n", 2082 name, ret); 2083 return ret; 2084 } 2085 2086 if (attr_states == ATTR_STATE_UNSUPPORTED) 2087 return 0; 2088 2089 ret = device_create_file(adev->dev, dev_attr); 2090 if (ret) { 2091 dev_err(adev->dev, "failed to create device file %s, ret = %d\n", 2092 name, ret); 2093 } 2094 2095 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL); 2096 if (!attr_entry) 2097 return -ENOMEM; 2098 2099 attr_entry->attr = attr; 2100 INIT_LIST_HEAD(&attr_entry->entry); 2101 2102 list_add_tail(&attr_entry->entry, attr_list); 2103 2104 return ret; 2105 } 2106 2107 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr) 2108 { 2109 struct device_attribute *dev_attr = &attr->dev_attr; 2110 2111 device_remove_file(adev->dev, dev_attr); 2112 } 2113 2114 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2115 struct list_head *attr_list); 2116 2117 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev, 2118 struct amdgpu_device_attr *attrs, 2119 uint32_t counts, 2120 uint32_t mask, 2121 struct list_head *attr_list) 2122 { 2123 int ret = 0; 2124 uint32_t i = 0; 2125 2126 for (i = 0; i < counts; i++) { 2127 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list); 2128 if (ret) 2129 goto failed; 2130 } 2131 2132 return 0; 2133 2134 failed: 2135 amdgpu_device_attr_remove_groups(adev, attr_list); 2136 2137 return ret; 2138 } 2139 2140 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev, 2141 struct list_head *attr_list) 2142 { 2143 struct amdgpu_device_attr_entry *entry, *entry_tmp; 2144 2145 if (list_empty(attr_list)) 2146 return ; 2147 2148 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) { 2149 amdgpu_device_attr_remove(adev, entry->attr); 2150 list_del(&entry->entry); 2151 kfree(entry); 2152 } 2153 } 2154 2155 static ssize_t amdgpu_hwmon_show_temp(struct device *dev, 2156 struct device_attribute *attr, 2157 char *buf) 2158 { 2159 struct amdgpu_device *adev = dev_get_drvdata(dev); 2160 int channel = to_sensor_dev_attr(attr)->index; 2161 int r, temp = 0, size = sizeof(temp); 2162 2163 if (amdgpu_in_reset(adev)) 2164 return -EPERM; 2165 if (adev->in_suspend && !adev->in_runpm) 2166 return -EPERM; 2167 2168 if (channel >= PP_TEMP_MAX) 2169 return -EINVAL; 2170 2171 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2172 if (r < 0) { 2173 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2174 return r; 2175 } 2176 2177 switch (channel) { 2178 case PP_TEMP_JUNCTION: 2179 /* get current junction temperature */ 2180 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP, 2181 (void *)&temp, &size); 2182 break; 2183 case PP_TEMP_EDGE: 2184 /* get current edge temperature */ 2185 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP, 2186 (void *)&temp, &size); 2187 break; 2188 case PP_TEMP_MEM: 2189 /* get current memory temperature */ 2190 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP, 2191 (void *)&temp, &size); 2192 break; 2193 default: 2194 r = -EINVAL; 2195 break; 2196 } 2197 2198 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2199 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2200 2201 if (r) 2202 return r; 2203 2204 return sysfs_emit(buf, "%d\n", temp); 2205 } 2206 2207 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev, 2208 struct device_attribute *attr, 2209 char *buf) 2210 { 2211 struct amdgpu_device *adev = dev_get_drvdata(dev); 2212 int hyst = to_sensor_dev_attr(attr)->index; 2213 int temp; 2214 2215 if (hyst) 2216 temp = adev->pm.dpm.thermal.min_temp; 2217 else 2218 temp = adev->pm.dpm.thermal.max_temp; 2219 2220 return sysfs_emit(buf, "%d\n", temp); 2221 } 2222 2223 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev, 2224 struct device_attribute *attr, 2225 char *buf) 2226 { 2227 struct amdgpu_device *adev = dev_get_drvdata(dev); 2228 int hyst = to_sensor_dev_attr(attr)->index; 2229 int temp; 2230 2231 if (hyst) 2232 temp = adev->pm.dpm.thermal.min_hotspot_temp; 2233 else 2234 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp; 2235 2236 return sysfs_emit(buf, "%d\n", temp); 2237 } 2238 2239 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev, 2240 struct device_attribute *attr, 2241 char *buf) 2242 { 2243 struct amdgpu_device *adev = dev_get_drvdata(dev); 2244 int hyst = to_sensor_dev_attr(attr)->index; 2245 int temp; 2246 2247 if (hyst) 2248 temp = adev->pm.dpm.thermal.min_mem_temp; 2249 else 2250 temp = adev->pm.dpm.thermal.max_mem_crit_temp; 2251 2252 return sysfs_emit(buf, "%d\n", temp); 2253 } 2254 2255 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev, 2256 struct device_attribute *attr, 2257 char *buf) 2258 { 2259 int channel = to_sensor_dev_attr(attr)->index; 2260 2261 if (channel >= PP_TEMP_MAX) 2262 return -EINVAL; 2263 2264 return sysfs_emit(buf, "%s\n", temp_label[channel].label); 2265 } 2266 2267 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev, 2268 struct device_attribute *attr, 2269 char *buf) 2270 { 2271 struct amdgpu_device *adev = dev_get_drvdata(dev); 2272 int channel = to_sensor_dev_attr(attr)->index; 2273 int temp = 0; 2274 2275 if (channel >= PP_TEMP_MAX) 2276 return -EINVAL; 2277 2278 switch (channel) { 2279 case PP_TEMP_JUNCTION: 2280 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp; 2281 break; 2282 case PP_TEMP_EDGE: 2283 temp = adev->pm.dpm.thermal.max_edge_emergency_temp; 2284 break; 2285 case PP_TEMP_MEM: 2286 temp = adev->pm.dpm.thermal.max_mem_emergency_temp; 2287 break; 2288 } 2289 2290 return sysfs_emit(buf, "%d\n", temp); 2291 } 2292 2293 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev, 2294 struct device_attribute *attr, 2295 char *buf) 2296 { 2297 struct amdgpu_device *adev = dev_get_drvdata(dev); 2298 u32 pwm_mode = 0; 2299 int ret; 2300 2301 if (amdgpu_in_reset(adev)) 2302 return -EPERM; 2303 if (adev->in_suspend && !adev->in_runpm) 2304 return -EPERM; 2305 2306 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2307 if (ret < 0) { 2308 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2309 return ret; 2310 } 2311 2312 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2313 2314 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2315 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2316 2317 if (ret) 2318 return -EINVAL; 2319 2320 return sysfs_emit(buf, "%u\n", pwm_mode); 2321 } 2322 2323 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev, 2324 struct device_attribute *attr, 2325 const char *buf, 2326 size_t count) 2327 { 2328 struct amdgpu_device *adev = dev_get_drvdata(dev); 2329 int err, ret; 2330 int value; 2331 2332 if (amdgpu_in_reset(adev)) 2333 return -EPERM; 2334 if (adev->in_suspend && !adev->in_runpm) 2335 return -EPERM; 2336 2337 err = kstrtoint(buf, 10, &value); 2338 if (err) 2339 return err; 2340 2341 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2342 if (ret < 0) { 2343 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2344 return ret; 2345 } 2346 2347 ret = amdgpu_dpm_set_fan_control_mode(adev, value); 2348 2349 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2350 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2351 2352 if (ret) 2353 return -EINVAL; 2354 2355 return count; 2356 } 2357 2358 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev, 2359 struct device_attribute *attr, 2360 char *buf) 2361 { 2362 return sysfs_emit(buf, "%i\n", 0); 2363 } 2364 2365 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev, 2366 struct device_attribute *attr, 2367 char *buf) 2368 { 2369 return sysfs_emit(buf, "%i\n", 255); 2370 } 2371 2372 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev, 2373 struct device_attribute *attr, 2374 const char *buf, size_t count) 2375 { 2376 struct amdgpu_device *adev = dev_get_drvdata(dev); 2377 int err; 2378 u32 value; 2379 u32 pwm_mode; 2380 2381 if (amdgpu_in_reset(adev)) 2382 return -EPERM; 2383 if (adev->in_suspend && !adev->in_runpm) 2384 return -EPERM; 2385 2386 err = kstrtou32(buf, 10, &value); 2387 if (err) 2388 return err; 2389 2390 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2391 if (err < 0) { 2392 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2393 return err; 2394 } 2395 2396 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2397 if (err) 2398 goto out; 2399 2400 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2401 pr_info("manual fan speed control should be enabled first\n"); 2402 err = -EINVAL; 2403 goto out; 2404 } 2405 2406 err = amdgpu_dpm_set_fan_speed_pwm(adev, value); 2407 2408 out: 2409 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2410 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2411 2412 if (err) 2413 return err; 2414 2415 return count; 2416 } 2417 2418 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev, 2419 struct device_attribute *attr, 2420 char *buf) 2421 { 2422 struct amdgpu_device *adev = dev_get_drvdata(dev); 2423 int err; 2424 u32 speed = 0; 2425 2426 if (amdgpu_in_reset(adev)) 2427 return -EPERM; 2428 if (adev->in_suspend && !adev->in_runpm) 2429 return -EPERM; 2430 2431 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2432 if (err < 0) { 2433 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2434 return err; 2435 } 2436 2437 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed); 2438 2439 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2440 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2441 2442 if (err) 2443 return err; 2444 2445 return sysfs_emit(buf, "%i\n", speed); 2446 } 2447 2448 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev, 2449 struct device_attribute *attr, 2450 char *buf) 2451 { 2452 struct amdgpu_device *adev = dev_get_drvdata(dev); 2453 int err; 2454 u32 speed = 0; 2455 2456 if (amdgpu_in_reset(adev)) 2457 return -EPERM; 2458 if (adev->in_suspend && !adev->in_runpm) 2459 return -EPERM; 2460 2461 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2462 if (err < 0) { 2463 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2464 return err; 2465 } 2466 2467 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed); 2468 2469 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2470 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2471 2472 if (err) 2473 return err; 2474 2475 return sysfs_emit(buf, "%i\n", speed); 2476 } 2477 2478 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev, 2479 struct device_attribute *attr, 2480 char *buf) 2481 { 2482 struct amdgpu_device *adev = dev_get_drvdata(dev); 2483 u32 min_rpm = 0; 2484 u32 size = sizeof(min_rpm); 2485 int r; 2486 2487 if (amdgpu_in_reset(adev)) 2488 return -EPERM; 2489 if (adev->in_suspend && !adev->in_runpm) 2490 return -EPERM; 2491 2492 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2493 if (r < 0) { 2494 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2495 return r; 2496 } 2497 2498 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM, 2499 (void *)&min_rpm, &size); 2500 2501 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2502 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2503 2504 if (r) 2505 return r; 2506 2507 return sysfs_emit(buf, "%d\n", min_rpm); 2508 } 2509 2510 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev, 2511 struct device_attribute *attr, 2512 char *buf) 2513 { 2514 struct amdgpu_device *adev = dev_get_drvdata(dev); 2515 u32 max_rpm = 0; 2516 u32 size = sizeof(max_rpm); 2517 int r; 2518 2519 if (amdgpu_in_reset(adev)) 2520 return -EPERM; 2521 if (adev->in_suspend && !adev->in_runpm) 2522 return -EPERM; 2523 2524 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2525 if (r < 0) { 2526 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2527 return r; 2528 } 2529 2530 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM, 2531 (void *)&max_rpm, &size); 2532 2533 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2534 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2535 2536 if (r) 2537 return r; 2538 2539 return sysfs_emit(buf, "%d\n", max_rpm); 2540 } 2541 2542 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev, 2543 struct device_attribute *attr, 2544 char *buf) 2545 { 2546 struct amdgpu_device *adev = dev_get_drvdata(dev); 2547 int err; 2548 u32 rpm = 0; 2549 2550 if (amdgpu_in_reset(adev)) 2551 return -EPERM; 2552 if (adev->in_suspend && !adev->in_runpm) 2553 return -EPERM; 2554 2555 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2556 if (err < 0) { 2557 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2558 return err; 2559 } 2560 2561 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm); 2562 2563 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2564 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2565 2566 if (err) 2567 return err; 2568 2569 return sysfs_emit(buf, "%i\n", rpm); 2570 } 2571 2572 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev, 2573 struct device_attribute *attr, 2574 const char *buf, size_t count) 2575 { 2576 struct amdgpu_device *adev = dev_get_drvdata(dev); 2577 int err; 2578 u32 value; 2579 u32 pwm_mode; 2580 2581 if (amdgpu_in_reset(adev)) 2582 return -EPERM; 2583 if (adev->in_suspend && !adev->in_runpm) 2584 return -EPERM; 2585 2586 err = kstrtou32(buf, 10, &value); 2587 if (err) 2588 return err; 2589 2590 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2591 if (err < 0) { 2592 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2593 return err; 2594 } 2595 2596 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2597 if (err) 2598 goto out; 2599 2600 if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2601 err = -ENODATA; 2602 goto out; 2603 } 2604 2605 err = amdgpu_dpm_set_fan_speed_rpm(adev, value); 2606 2607 out: 2608 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2609 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2610 2611 if (err) 2612 return err; 2613 2614 return count; 2615 } 2616 2617 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev, 2618 struct device_attribute *attr, 2619 char *buf) 2620 { 2621 struct amdgpu_device *adev = dev_get_drvdata(dev); 2622 u32 pwm_mode = 0; 2623 int ret; 2624 2625 if (amdgpu_in_reset(adev)) 2626 return -EPERM; 2627 if (adev->in_suspend && !adev->in_runpm) 2628 return -EPERM; 2629 2630 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2631 if (ret < 0) { 2632 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2633 return ret; 2634 } 2635 2636 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode); 2637 2638 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2639 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2640 2641 if (ret) 2642 return -EINVAL; 2643 2644 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1); 2645 } 2646 2647 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev, 2648 struct device_attribute *attr, 2649 const char *buf, 2650 size_t count) 2651 { 2652 struct amdgpu_device *adev = dev_get_drvdata(dev); 2653 int err; 2654 int value; 2655 u32 pwm_mode; 2656 2657 if (amdgpu_in_reset(adev)) 2658 return -EPERM; 2659 if (adev->in_suspend && !adev->in_runpm) 2660 return -EPERM; 2661 2662 err = kstrtoint(buf, 10, &value); 2663 if (err) 2664 return err; 2665 2666 if (value == 0) 2667 pwm_mode = AMD_FAN_CTRL_AUTO; 2668 else if (value == 1) 2669 pwm_mode = AMD_FAN_CTRL_MANUAL; 2670 else 2671 return -EINVAL; 2672 2673 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2674 if (err < 0) { 2675 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2676 return err; 2677 } 2678 2679 err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode); 2680 2681 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2682 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2683 2684 if (err) 2685 return -EINVAL; 2686 2687 return count; 2688 } 2689 2690 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev, 2691 struct device_attribute *attr, 2692 char *buf) 2693 { 2694 struct amdgpu_device *adev = dev_get_drvdata(dev); 2695 u32 vddgfx; 2696 int r, size = sizeof(vddgfx); 2697 2698 if (amdgpu_in_reset(adev)) 2699 return -EPERM; 2700 if (adev->in_suspend && !adev->in_runpm) 2701 return -EPERM; 2702 2703 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2704 if (r < 0) { 2705 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2706 return r; 2707 } 2708 2709 /* get the voltage */ 2710 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, 2711 (void *)&vddgfx, &size); 2712 2713 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2714 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2715 2716 if (r) 2717 return r; 2718 2719 return sysfs_emit(buf, "%d\n", vddgfx); 2720 } 2721 2722 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev, 2723 struct device_attribute *attr, 2724 char *buf) 2725 { 2726 return sysfs_emit(buf, "vddgfx\n"); 2727 } 2728 2729 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev, 2730 struct device_attribute *attr, 2731 char *buf) 2732 { 2733 struct amdgpu_device *adev = dev_get_drvdata(dev); 2734 u32 vddnb; 2735 int r, size = sizeof(vddnb); 2736 2737 if (amdgpu_in_reset(adev)) 2738 return -EPERM; 2739 if (adev->in_suspend && !adev->in_runpm) 2740 return -EPERM; 2741 2742 /* only APUs have vddnb */ 2743 if (!(adev->flags & AMD_IS_APU)) 2744 return -EINVAL; 2745 2746 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2747 if (r < 0) { 2748 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2749 return r; 2750 } 2751 2752 /* get the voltage */ 2753 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, 2754 (void *)&vddnb, &size); 2755 2756 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2757 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2758 2759 if (r) 2760 return r; 2761 2762 return sysfs_emit(buf, "%d\n", vddnb); 2763 } 2764 2765 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev, 2766 struct device_attribute *attr, 2767 char *buf) 2768 { 2769 return sysfs_emit(buf, "vddnb\n"); 2770 } 2771 2772 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev, 2773 struct device_attribute *attr, 2774 char *buf) 2775 { 2776 struct amdgpu_device *adev = dev_get_drvdata(dev); 2777 u32 query = 0; 2778 int r, size = sizeof(u32); 2779 unsigned uw; 2780 2781 if (amdgpu_in_reset(adev)) 2782 return -EPERM; 2783 if (adev->in_suspend && !adev->in_runpm) 2784 return -EPERM; 2785 2786 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2787 if (r < 0) { 2788 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2789 return r; 2790 } 2791 2792 /* get the voltage */ 2793 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, 2794 (void *)&query, &size); 2795 2796 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2797 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2798 2799 if (r) 2800 return r; 2801 2802 /* convert to microwatts */ 2803 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000; 2804 2805 return sysfs_emit(buf, "%u\n", uw); 2806 } 2807 2808 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev, 2809 struct device_attribute *attr, 2810 char *buf) 2811 { 2812 return sysfs_emit(buf, "%i\n", 0); 2813 } 2814 2815 2816 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev, 2817 struct device_attribute *attr, 2818 char *buf, 2819 enum pp_power_limit_level pp_limit_level) 2820 { 2821 struct amdgpu_device *adev = dev_get_drvdata(dev); 2822 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index; 2823 uint32_t limit; 2824 ssize_t size; 2825 int r; 2826 2827 if (amdgpu_in_reset(adev)) 2828 return -EPERM; 2829 if (adev->in_suspend && !adev->in_runpm) 2830 return -EPERM; 2831 2832 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2833 if (r < 0) { 2834 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2835 return r; 2836 } 2837 2838 r = amdgpu_dpm_get_power_limit(adev, &limit, 2839 pp_limit_level, power_type); 2840 2841 if (!r) 2842 size = sysfs_emit(buf, "%u\n", limit * 1000000); 2843 else 2844 size = sysfs_emit(buf, "\n"); 2845 2846 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2847 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2848 2849 return size; 2850 } 2851 2852 2853 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev, 2854 struct device_attribute *attr, 2855 char *buf) 2856 { 2857 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX); 2858 2859 } 2860 2861 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev, 2862 struct device_attribute *attr, 2863 char *buf) 2864 { 2865 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT); 2866 2867 } 2868 2869 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev, 2870 struct device_attribute *attr, 2871 char *buf) 2872 { 2873 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT); 2874 2875 } 2876 2877 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev, 2878 struct device_attribute *attr, 2879 char *buf) 2880 { 2881 struct amdgpu_device *adev = dev_get_drvdata(dev); 2882 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0]; 2883 2884 if (gc_ver == IP_VERSION(10, 3, 1)) 2885 return sysfs_emit(buf, "%s\n", 2886 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ? 2887 "fastPPT" : "slowPPT"); 2888 else 2889 return sysfs_emit(buf, "PPT\n"); 2890 } 2891 2892 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev, 2893 struct device_attribute *attr, 2894 const char *buf, 2895 size_t count) 2896 { 2897 struct amdgpu_device *adev = dev_get_drvdata(dev); 2898 int limit_type = to_sensor_dev_attr(attr)->index; 2899 int err; 2900 u32 value; 2901 2902 if (amdgpu_in_reset(adev)) 2903 return -EPERM; 2904 if (adev->in_suspend && !adev->in_runpm) 2905 return -EPERM; 2906 2907 if (amdgpu_sriov_vf(adev)) 2908 return -EINVAL; 2909 2910 err = kstrtou32(buf, 10, &value); 2911 if (err) 2912 return err; 2913 2914 value = value / 1000000; /* convert to Watt */ 2915 value |= limit_type << 24; 2916 2917 err = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2918 if (err < 0) { 2919 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2920 return err; 2921 } 2922 2923 err = amdgpu_dpm_set_power_limit(adev, value); 2924 2925 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2926 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2927 2928 if (err) 2929 return err; 2930 2931 return count; 2932 } 2933 2934 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev, 2935 struct device_attribute *attr, 2936 char *buf) 2937 { 2938 struct amdgpu_device *adev = dev_get_drvdata(dev); 2939 uint32_t sclk; 2940 int r, size = sizeof(sclk); 2941 2942 if (amdgpu_in_reset(adev)) 2943 return -EPERM; 2944 if (adev->in_suspend && !adev->in_runpm) 2945 return -EPERM; 2946 2947 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2948 if (r < 0) { 2949 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2950 return r; 2951 } 2952 2953 /* get the sclk */ 2954 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, 2955 (void *)&sclk, &size); 2956 2957 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2958 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2959 2960 if (r) 2961 return r; 2962 2963 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000); 2964 } 2965 2966 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev, 2967 struct device_attribute *attr, 2968 char *buf) 2969 { 2970 return sysfs_emit(buf, "sclk\n"); 2971 } 2972 2973 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev, 2974 struct device_attribute *attr, 2975 char *buf) 2976 { 2977 struct amdgpu_device *adev = dev_get_drvdata(dev); 2978 uint32_t mclk; 2979 int r, size = sizeof(mclk); 2980 2981 if (amdgpu_in_reset(adev)) 2982 return -EPERM; 2983 if (adev->in_suspend && !adev->in_runpm) 2984 return -EPERM; 2985 2986 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 2987 if (r < 0) { 2988 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2989 return r; 2990 } 2991 2992 /* get the sclk */ 2993 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, 2994 (void *)&mclk, &size); 2995 2996 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 2997 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 2998 2999 if (r) 3000 return r; 3001 3002 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000); 3003 } 3004 3005 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev, 3006 struct device_attribute *attr, 3007 char *buf) 3008 { 3009 return sysfs_emit(buf, "mclk\n"); 3010 } 3011 3012 /** 3013 * DOC: hwmon 3014 * 3015 * The amdgpu driver exposes the following sensor interfaces: 3016 * 3017 * - GPU temperature (via the on-die sensor) 3018 * 3019 * - GPU voltage 3020 * 3021 * - Northbridge voltage (APUs only) 3022 * 3023 * - GPU power 3024 * 3025 * - GPU fan 3026 * 3027 * - GPU gfx/compute engine clock 3028 * 3029 * - GPU memory clock (dGPU only) 3030 * 3031 * hwmon interfaces for GPU temperature: 3032 * 3033 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius 3034 * - temp2_input and temp3_input are supported on SOC15 dGPUs only 3035 * 3036 * - temp[1-3]_label: temperature channel label 3037 * - temp2_label and temp3_label are supported on SOC15 dGPUs only 3038 * 3039 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius 3040 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only 3041 * 3042 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius 3043 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only 3044 * 3045 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius 3046 * - these are supported on SOC15 dGPUs only 3047 * 3048 * hwmon interfaces for GPU voltage: 3049 * 3050 * - in0_input: the voltage on the GPU in millivolts 3051 * 3052 * - in1_input: the voltage on the Northbridge in millivolts 3053 * 3054 * hwmon interfaces for GPU power: 3055 * 3056 * - power1_average: average power used by the GPU in microWatts 3057 * 3058 * - power1_cap_min: minimum cap supported in microWatts 3059 * 3060 * - power1_cap_max: maximum cap supported in microWatts 3061 * 3062 * - power1_cap: selected power cap in microWatts 3063 * 3064 * hwmon interfaces for GPU fan: 3065 * 3066 * - pwm1: pulse width modulation fan level (0-255) 3067 * 3068 * - 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) 3069 * 3070 * - pwm1_min: pulse width modulation fan control minimum level (0) 3071 * 3072 * - pwm1_max: pulse width modulation fan control maximum level (255) 3073 * 3074 * - fan1_min: a minimum value Unit: revolution/min (RPM) 3075 * 3076 * - fan1_max: a maximum value Unit: revolution/max (RPM) 3077 * 3078 * - fan1_input: fan speed in RPM 3079 * 3080 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM) 3081 * 3082 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable 3083 * 3084 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time. 3085 * That will get the former one overridden. 3086 * 3087 * hwmon interfaces for GPU clocks: 3088 * 3089 * - freq1_input: the gfx/compute clock in hertz 3090 * 3091 * - freq2_input: the memory clock in hertz 3092 * 3093 * You can use hwmon tools like sensors to view this information on your system. 3094 * 3095 */ 3096 3097 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE); 3098 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0); 3099 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1); 3100 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE); 3101 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION); 3102 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0); 3103 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1); 3104 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION); 3105 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM); 3106 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0); 3107 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1); 3108 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM); 3109 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE); 3110 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION); 3111 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM); 3112 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0); 3113 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0); 3114 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0); 3115 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0); 3116 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0); 3117 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0); 3118 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0); 3119 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0); 3120 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0); 3121 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0); 3122 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0); 3123 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0); 3124 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0); 3125 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0); 3126 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0); 3127 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0); 3128 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0); 3129 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0); 3130 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0); 3131 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1); 3132 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1); 3133 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1); 3134 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1); 3135 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1); 3136 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1); 3137 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0); 3138 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0); 3139 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0); 3140 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0); 3141 3142 static struct attribute *hwmon_attributes[] = { 3143 &sensor_dev_attr_temp1_input.dev_attr.attr, 3144 &sensor_dev_attr_temp1_crit.dev_attr.attr, 3145 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 3146 &sensor_dev_attr_temp2_input.dev_attr.attr, 3147 &sensor_dev_attr_temp2_crit.dev_attr.attr, 3148 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 3149 &sensor_dev_attr_temp3_input.dev_attr.attr, 3150 &sensor_dev_attr_temp3_crit.dev_attr.attr, 3151 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 3152 &sensor_dev_attr_temp1_emergency.dev_attr.attr, 3153 &sensor_dev_attr_temp2_emergency.dev_attr.attr, 3154 &sensor_dev_attr_temp3_emergency.dev_attr.attr, 3155 &sensor_dev_attr_temp1_label.dev_attr.attr, 3156 &sensor_dev_attr_temp2_label.dev_attr.attr, 3157 &sensor_dev_attr_temp3_label.dev_attr.attr, 3158 &sensor_dev_attr_pwm1.dev_attr.attr, 3159 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 3160 &sensor_dev_attr_pwm1_min.dev_attr.attr, 3161 &sensor_dev_attr_pwm1_max.dev_attr.attr, 3162 &sensor_dev_attr_fan1_input.dev_attr.attr, 3163 &sensor_dev_attr_fan1_min.dev_attr.attr, 3164 &sensor_dev_attr_fan1_max.dev_attr.attr, 3165 &sensor_dev_attr_fan1_target.dev_attr.attr, 3166 &sensor_dev_attr_fan1_enable.dev_attr.attr, 3167 &sensor_dev_attr_in0_input.dev_attr.attr, 3168 &sensor_dev_attr_in0_label.dev_attr.attr, 3169 &sensor_dev_attr_in1_input.dev_attr.attr, 3170 &sensor_dev_attr_in1_label.dev_attr.attr, 3171 &sensor_dev_attr_power1_average.dev_attr.attr, 3172 &sensor_dev_attr_power1_cap_max.dev_attr.attr, 3173 &sensor_dev_attr_power1_cap_min.dev_attr.attr, 3174 &sensor_dev_attr_power1_cap.dev_attr.attr, 3175 &sensor_dev_attr_power1_cap_default.dev_attr.attr, 3176 &sensor_dev_attr_power1_label.dev_attr.attr, 3177 &sensor_dev_attr_power2_average.dev_attr.attr, 3178 &sensor_dev_attr_power2_cap_max.dev_attr.attr, 3179 &sensor_dev_attr_power2_cap_min.dev_attr.attr, 3180 &sensor_dev_attr_power2_cap.dev_attr.attr, 3181 &sensor_dev_attr_power2_cap_default.dev_attr.attr, 3182 &sensor_dev_attr_power2_label.dev_attr.attr, 3183 &sensor_dev_attr_freq1_input.dev_attr.attr, 3184 &sensor_dev_attr_freq1_label.dev_attr.attr, 3185 &sensor_dev_attr_freq2_input.dev_attr.attr, 3186 &sensor_dev_attr_freq2_label.dev_attr.attr, 3187 NULL 3188 }; 3189 3190 static umode_t hwmon_attributes_visible(struct kobject *kobj, 3191 struct attribute *attr, int index) 3192 { 3193 struct device *dev = kobj_to_dev(kobj); 3194 struct amdgpu_device *adev = dev_get_drvdata(dev); 3195 umode_t effective_mode = attr->mode; 3196 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0]; 3197 3198 /* under multi-vf mode, the hwmon attributes are all not supported */ 3199 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 3200 return 0; 3201 3202 /* under pp one vf mode manage of hwmon attributes is not supported */ 3203 if (amdgpu_sriov_is_pp_one_vf(adev)) 3204 effective_mode &= ~S_IWUSR; 3205 3206 /* Skip fan attributes if fan is not present */ 3207 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3208 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3209 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3210 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3211 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3212 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3213 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3214 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3215 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3216 return 0; 3217 3218 /* Skip fan attributes on APU */ 3219 if ((adev->flags & AMD_IS_APU) && 3220 (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3221 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3222 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3223 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3224 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3225 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3226 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3227 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3228 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3229 return 0; 3230 3231 /* Skip crit temp on APU */ 3232 if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) && 3233 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3234 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr)) 3235 return 0; 3236 3237 /* Skip limit attributes if DPM is not enabled */ 3238 if (!adev->pm.dpm_enabled && 3239 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 3240 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr || 3241 attr == &sensor_dev_attr_pwm1.dev_attr.attr || 3242 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 3243 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3244 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 3245 attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 3246 attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 3247 attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3248 attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 3249 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 3250 return 0; 3251 3252 /* mask fan attributes if we have no bindings for this asic to expose */ 3253 if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) && 3254 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */ 3255 ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) && 3256 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */ 3257 effective_mode &= ~S_IRUGO; 3258 3259 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) && 3260 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */ 3261 ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) && 3262 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */ 3263 effective_mode &= ~S_IWUSR; 3264 3265 /* not implemented yet for GC 10.3.1 APUs */ 3266 if (((adev->family == AMDGPU_FAMILY_SI) || 3267 ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)))) && 3268 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr || 3269 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr || 3270 attr == &sensor_dev_attr_power1_cap.dev_attr.attr || 3271 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr)) 3272 return 0; 3273 3274 /* not implemented yet for APUs having <= GC 9.3.0 */ 3275 if (((adev->family == AMDGPU_FAMILY_SI) || 3276 ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) && 3277 (attr == &sensor_dev_attr_power1_average.dev_attr.attr)) 3278 return 0; 3279 3280 /* hide max/min values if we can't both query and manage the fan */ 3281 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) && 3282 (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) && 3283 (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) && 3284 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) && 3285 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 3286 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr)) 3287 return 0; 3288 3289 if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) && 3290 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) && 3291 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 3292 attr == &sensor_dev_attr_fan1_min.dev_attr.attr)) 3293 return 0; 3294 3295 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */ 3296 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */ 3297 (attr == &sensor_dev_attr_in0_input.dev_attr.attr || 3298 attr == &sensor_dev_attr_in0_label.dev_attr.attr)) 3299 return 0; 3300 3301 /* only APUs have vddnb */ 3302 if (!(adev->flags & AMD_IS_APU) && 3303 (attr == &sensor_dev_attr_in1_input.dev_attr.attr || 3304 attr == &sensor_dev_attr_in1_label.dev_attr.attr)) 3305 return 0; 3306 3307 /* no mclk on APUs */ 3308 if ((adev->flags & AMD_IS_APU) && 3309 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr || 3310 attr == &sensor_dev_attr_freq2_label.dev_attr.attr)) 3311 return 0; 3312 3313 /* only SOC15 dGPUs support hotspot and mem temperatures */ 3314 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) && 3315 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr || 3316 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr || 3317 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr || 3318 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr || 3319 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr || 3320 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr || 3321 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr || 3322 attr == &sensor_dev_attr_temp2_input.dev_attr.attr || 3323 attr == &sensor_dev_attr_temp3_input.dev_attr.attr || 3324 attr == &sensor_dev_attr_temp2_label.dev_attr.attr || 3325 attr == &sensor_dev_attr_temp3_label.dev_attr.attr)) 3326 return 0; 3327 3328 /* only Vangogh has fast PPT limit and power labels */ 3329 if (!(gc_ver == IP_VERSION(10, 3, 1)) && 3330 (attr == &sensor_dev_attr_power2_average.dev_attr.attr || 3331 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr || 3332 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr || 3333 attr == &sensor_dev_attr_power2_cap.dev_attr.attr || 3334 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr || 3335 attr == &sensor_dev_attr_power2_label.dev_attr.attr)) 3336 return 0; 3337 3338 return effective_mode; 3339 } 3340 3341 static const struct attribute_group hwmon_attrgroup = { 3342 .attrs = hwmon_attributes, 3343 .is_visible = hwmon_attributes_visible, 3344 }; 3345 3346 static const struct attribute_group *hwmon_groups[] = { 3347 &hwmon_attrgroup, 3348 NULL 3349 }; 3350 3351 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev) 3352 { 3353 int ret; 3354 uint32_t mask = 0; 3355 3356 if (adev->pm.sysfs_initialized) 3357 return 0; 3358 3359 if (adev->pm.dpm_enabled == 0) 3360 return 0; 3361 3362 INIT_LIST_HEAD(&adev->pm.pm_attr_list); 3363 3364 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev, 3365 DRIVER_NAME, adev, 3366 hwmon_groups); 3367 if (IS_ERR(adev->pm.int_hwmon_dev)) { 3368 ret = PTR_ERR(adev->pm.int_hwmon_dev); 3369 dev_err(adev->dev, 3370 "Unable to register hwmon device: %d\n", ret); 3371 return ret; 3372 } 3373 3374 switch (amdgpu_virt_get_sriov_vf_mode(adev)) { 3375 case SRIOV_VF_MODE_ONE_VF: 3376 mask = ATTR_FLAG_ONEVF; 3377 break; 3378 case SRIOV_VF_MODE_MULTI_VF: 3379 mask = 0; 3380 break; 3381 case SRIOV_VF_MODE_BARE_METAL: 3382 default: 3383 mask = ATTR_FLAG_MASK_ALL; 3384 break; 3385 } 3386 3387 ret = amdgpu_device_attr_create_groups(adev, 3388 amdgpu_device_attrs, 3389 ARRAY_SIZE(amdgpu_device_attrs), 3390 mask, 3391 &adev->pm.pm_attr_list); 3392 if (ret) 3393 return ret; 3394 3395 adev->pm.sysfs_initialized = true; 3396 3397 return 0; 3398 } 3399 3400 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev) 3401 { 3402 if (adev->pm.dpm_enabled == 0) 3403 return; 3404 3405 if (adev->pm.int_hwmon_dev) 3406 hwmon_device_unregister(adev->pm.int_hwmon_dev); 3407 3408 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list); 3409 } 3410 3411 /* 3412 * Debugfs info 3413 */ 3414 #if defined(CONFIG_DEBUG_FS) 3415 3416 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m, 3417 struct amdgpu_device *adev) { 3418 uint16_t *p_val; 3419 uint32_t size; 3420 int i; 3421 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev); 3422 3423 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) { 3424 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t), 3425 GFP_KERNEL); 3426 3427 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK, 3428 (void *)p_val, &size)) { 3429 for (i = 0; i < num_cpu_cores; i++) 3430 seq_printf(m, "\t%u MHz (CPU%d)\n", 3431 *(p_val + i), i); 3432 } 3433 3434 kfree(p_val); 3435 } 3436 } 3437 3438 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev) 3439 { 3440 uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0]; 3441 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0]; 3442 uint32_t value; 3443 uint64_t value64 = 0; 3444 uint32_t query = 0; 3445 int size; 3446 3447 /* GPU Clocks */ 3448 size = sizeof(value); 3449 seq_printf(m, "GFX Clocks and Power:\n"); 3450 3451 amdgpu_debugfs_prints_cpu_info(m, adev); 3452 3453 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size)) 3454 seq_printf(m, "\t%u MHz (MCLK)\n", value/100); 3455 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size)) 3456 seq_printf(m, "\t%u MHz (SCLK)\n", value/100); 3457 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size)) 3458 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100); 3459 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size)) 3460 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100); 3461 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size)) 3462 seq_printf(m, "\t%u mV (VDDGFX)\n", value); 3463 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size)) 3464 seq_printf(m, "\t%u mV (VDDNB)\n", value); 3465 size = sizeof(uint32_t); 3466 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) 3467 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff); 3468 size = sizeof(value); 3469 seq_printf(m, "\n"); 3470 3471 /* GPU Temp */ 3472 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size)) 3473 seq_printf(m, "GPU Temperature: %u C\n", value/1000); 3474 3475 /* GPU Load */ 3476 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size)) 3477 seq_printf(m, "GPU Load: %u %%\n", value); 3478 /* MEM Load */ 3479 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size)) 3480 seq_printf(m, "MEM Load: %u %%\n", value); 3481 3482 seq_printf(m, "\n"); 3483 3484 /* SMC feature mask */ 3485 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size)) 3486 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64); 3487 3488 /* ASICs greater than CHIP_VEGA20 supports these sensors */ 3489 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) { 3490 /* VCN clocks */ 3491 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) { 3492 if (!value) { 3493 seq_printf(m, "VCN: Disabled\n"); 3494 } else { 3495 seq_printf(m, "VCN: Enabled\n"); 3496 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 3497 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 3498 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 3499 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 3500 } 3501 } 3502 seq_printf(m, "\n"); 3503 } else { 3504 /* UVD clocks */ 3505 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) { 3506 if (!value) { 3507 seq_printf(m, "UVD: Disabled\n"); 3508 } else { 3509 seq_printf(m, "UVD: Enabled\n"); 3510 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 3511 seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 3512 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 3513 seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 3514 } 3515 } 3516 seq_printf(m, "\n"); 3517 3518 /* VCE clocks */ 3519 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) { 3520 if (!value) { 3521 seq_printf(m, "VCE: Disabled\n"); 3522 } else { 3523 seq_printf(m, "VCE: Enabled\n"); 3524 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size)) 3525 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100); 3526 } 3527 } 3528 } 3529 3530 return 0; 3531 } 3532 3533 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags) 3534 { 3535 int i; 3536 3537 for (i = 0; clocks[i].flag; i++) 3538 seq_printf(m, "\t%s: %s\n", clocks[i].name, 3539 (flags & clocks[i].flag) ? "On" : "Off"); 3540 } 3541 3542 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused) 3543 { 3544 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 3545 struct drm_device *dev = adev_to_drm(adev); 3546 u64 flags = 0; 3547 int r; 3548 3549 if (amdgpu_in_reset(adev)) 3550 return -EPERM; 3551 if (adev->in_suspend && !adev->in_runpm) 3552 return -EPERM; 3553 3554 r = pm_runtime_get_sync(dev->dev); 3555 if (r < 0) { 3556 pm_runtime_put_autosuspend(dev->dev); 3557 return r; 3558 } 3559 3560 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) { 3561 r = amdgpu_debugfs_pm_info_pp(m, adev); 3562 if (r) 3563 goto out; 3564 } 3565 3566 amdgpu_device_ip_get_clockgating_state(adev, &flags); 3567 3568 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags); 3569 amdgpu_parse_cg_state(m, flags); 3570 seq_printf(m, "\n"); 3571 3572 out: 3573 pm_runtime_mark_last_busy(dev->dev); 3574 pm_runtime_put_autosuspend(dev->dev); 3575 3576 return r; 3577 } 3578 3579 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info); 3580 3581 /* 3582 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW 3583 * 3584 * Reads debug memory region allocated to PMFW 3585 */ 3586 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf, 3587 size_t size, loff_t *pos) 3588 { 3589 struct amdgpu_device *adev = file_inode(f)->i_private; 3590 size_t smu_prv_buf_size; 3591 void *smu_prv_buf; 3592 int ret = 0; 3593 3594 if (amdgpu_in_reset(adev)) 3595 return -EPERM; 3596 if (adev->in_suspend && !adev->in_runpm) 3597 return -EPERM; 3598 3599 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size); 3600 if (ret) 3601 return ret; 3602 3603 if (!smu_prv_buf || !smu_prv_buf_size) 3604 return -EINVAL; 3605 3606 return simple_read_from_buffer(buf, size, pos, smu_prv_buf, 3607 smu_prv_buf_size); 3608 } 3609 3610 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = { 3611 .owner = THIS_MODULE, 3612 .open = simple_open, 3613 .read = amdgpu_pm_prv_buffer_read, 3614 .llseek = default_llseek, 3615 }; 3616 3617 #endif 3618 3619 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev) 3620 { 3621 #if defined(CONFIG_DEBUG_FS) 3622 struct drm_minor *minor = adev_to_drm(adev)->primary; 3623 struct dentry *root = minor->debugfs_root; 3624 3625 if (!adev->pm.dpm_enabled) 3626 return; 3627 3628 debugfs_create_file("amdgpu_pm_info", 0444, root, adev, 3629 &amdgpu_debugfs_pm_info_fops); 3630 3631 if (adev->pm.smu_prv_buffer_size > 0) 3632 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root, 3633 adev, 3634 &amdgpu_debugfs_pm_prv_buffer_fops, 3635 adev->pm.smu_prv_buffer_size); 3636 3637 amdgpu_dpm_stb_debug_fs_init(adev); 3638 #endif 3639 } 3640