2 * Copyright 2017 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <drm/drm_debugfs.h>
29 #include "amdgpu_drv.h"
30 #include "amdgpu_pm.h"
31 #include "amdgpu_dpm.h"
32 #include "amdgpu_display.h"
33 #include "amdgpu_smu.h"
35 #include <linux/power_supply.h>
36 #include <linux/pci.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-sysfs.h>
39 #include <linux/nospec.h>
40 #include <linux/pm_runtime.h>
44 static const struct cg_flag_name clocks[] = {
45 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
46 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
47 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
48 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
49 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
50 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
51 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
52 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
53 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
54 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
55 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
56 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
57 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
58 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
59 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
60 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
61 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
62 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
63 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
64 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
65 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
66 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
67 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
68 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
70 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
71 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
75 static const struct hwmon_temp_label {
76 enum PP_HWMON_TEMP channel;
79 {PP_TEMP_EDGE, "edge"},
80 {PP_TEMP_JUNCTION, "junction"},
84 void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
86 if (adev->pm.dpm_enabled) {
87 mutex_lock(&adev->pm.mutex);
88 if (power_supply_is_system_supplied() > 0)
89 adev->pm.ac_power = true;
91 adev->pm.ac_power = false;
92 if (adev->powerplay.pp_funcs &&
93 adev->powerplay.pp_funcs->enable_bapm)
94 amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power);
95 mutex_unlock(&adev->pm.mutex);
97 if (is_support_sw_smu(adev))
98 smu_set_ac_dc(&adev->smu);
102 int amdgpu_dpm_read_sensor(struct amdgpu_device *adev, enum amd_pp_sensors sensor,
103 void *data, uint32_t *size)
110 if (is_support_sw_smu(adev))
111 ret = smu_read_sensor(&adev->smu, sensor, data, size);
113 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
114 ret = adev->powerplay.pp_funcs->read_sensor((adev)->powerplay.pp_handle,
124 * DOC: power_dpm_state
126 * The power_dpm_state file is a legacy interface and is only provided for
127 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
128 * certain power related parameters. The file power_dpm_state is used for this.
129 * It accepts the following arguments:
139 * On older GPUs, the vbios provided a special power state for battery
140 * operation. Selecting battery switched to this state. This is no
141 * longer provided on newer GPUs so the option does nothing in that case.
145 * On older GPUs, the vbios provided a special power state for balanced
146 * operation. Selecting balanced switched to this state. This is no
147 * longer provided on newer GPUs so the option does nothing in that case.
151 * On older GPUs, the vbios provided a special power state for performance
152 * operation. Selecting performance switched to this state. This is no
153 * longer provided on newer GPUs so the option does nothing in that case.
157 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
158 struct device_attribute *attr,
161 struct drm_device *ddev = dev_get_drvdata(dev);
162 struct amdgpu_device *adev = ddev->dev_private;
163 enum amd_pm_state_type pm;
166 if (adev->in_gpu_reset)
169 ret = pm_runtime_get_sync(ddev->dev);
171 pm_runtime_put_autosuspend(ddev->dev);
175 if (is_support_sw_smu(adev)) {
176 if (adev->smu.ppt_funcs->get_current_power_state)
177 pm = smu_get_current_power_state(&adev->smu);
179 pm = adev->pm.dpm.user_state;
180 } else if (adev->powerplay.pp_funcs->get_current_power_state) {
181 pm = amdgpu_dpm_get_current_power_state(adev);
183 pm = adev->pm.dpm.user_state;
186 pm_runtime_mark_last_busy(ddev->dev);
187 pm_runtime_put_autosuspend(ddev->dev);
189 return snprintf(buf, PAGE_SIZE, "%s\n",
190 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
191 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
194 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
195 struct device_attribute *attr,
199 struct drm_device *ddev = dev_get_drvdata(dev);
200 struct amdgpu_device *adev = ddev->dev_private;
201 enum amd_pm_state_type state;
204 if (adev->in_gpu_reset)
207 if (strncmp("battery", buf, strlen("battery")) == 0)
208 state = POWER_STATE_TYPE_BATTERY;
209 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
210 state = POWER_STATE_TYPE_BALANCED;
211 else if (strncmp("performance", buf, strlen("performance")) == 0)
212 state = POWER_STATE_TYPE_PERFORMANCE;
216 ret = pm_runtime_get_sync(ddev->dev);
218 pm_runtime_put_autosuspend(ddev->dev);
222 if (is_support_sw_smu(adev)) {
223 mutex_lock(&adev->pm.mutex);
224 adev->pm.dpm.user_state = state;
225 mutex_unlock(&adev->pm.mutex);
226 } else if (adev->powerplay.pp_funcs->dispatch_tasks) {
227 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
229 mutex_lock(&adev->pm.mutex);
230 adev->pm.dpm.user_state = state;
231 mutex_unlock(&adev->pm.mutex);
233 amdgpu_pm_compute_clocks(adev);
235 pm_runtime_mark_last_busy(ddev->dev);
236 pm_runtime_put_autosuspend(ddev->dev);
243 * DOC: power_dpm_force_performance_level
245 * The amdgpu driver provides a sysfs API for adjusting certain power
246 * related parameters. The file power_dpm_force_performance_level is
247 * used for this. It accepts the following arguments:
267 * When auto is selected, the driver will attempt to dynamically select
268 * the optimal power profile for current conditions in the driver.
272 * When low is selected, the clocks are forced to the lowest power state.
276 * When high is selected, the clocks are forced to the highest power state.
280 * When manual is selected, the user can manually adjust which power states
281 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
282 * and pp_dpm_pcie files and adjust the power state transition heuristics
283 * via the pp_power_profile_mode sysfs file.
290 * When the profiling modes are selected, clock and power gating are
291 * disabled and the clocks are set for different profiling cases. This
292 * mode is recommended for profiling specific work loads where you do
293 * not want clock or power gating for clock fluctuation to interfere
294 * with your results. profile_standard sets the clocks to a fixed clock
295 * level which varies from asic to asic. profile_min_sclk forces the sclk
296 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
297 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
301 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
302 struct device_attribute *attr,
305 struct drm_device *ddev = dev_get_drvdata(dev);
306 struct amdgpu_device *adev = ddev->dev_private;
307 enum amd_dpm_forced_level level = 0xff;
310 if (adev->in_gpu_reset)
313 ret = pm_runtime_get_sync(ddev->dev);
315 pm_runtime_put_autosuspend(ddev->dev);
319 if (is_support_sw_smu(adev))
320 level = smu_get_performance_level(&adev->smu);
321 else if (adev->powerplay.pp_funcs->get_performance_level)
322 level = amdgpu_dpm_get_performance_level(adev);
324 level = adev->pm.dpm.forced_level;
326 pm_runtime_mark_last_busy(ddev->dev);
327 pm_runtime_put_autosuspend(ddev->dev);
329 return snprintf(buf, PAGE_SIZE, "%s\n",
330 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
331 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
332 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
333 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
334 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
335 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
336 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
337 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
341 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
342 struct device_attribute *attr,
346 struct drm_device *ddev = dev_get_drvdata(dev);
347 struct amdgpu_device *adev = ddev->dev_private;
348 enum amd_dpm_forced_level level;
349 enum amd_dpm_forced_level current_level = 0xff;
352 if (adev->in_gpu_reset)
355 if (strncmp("low", buf, strlen("low")) == 0) {
356 level = AMD_DPM_FORCED_LEVEL_LOW;
357 } else if (strncmp("high", buf, strlen("high")) == 0) {
358 level = AMD_DPM_FORCED_LEVEL_HIGH;
359 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
360 level = AMD_DPM_FORCED_LEVEL_AUTO;
361 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
362 level = AMD_DPM_FORCED_LEVEL_MANUAL;
363 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
364 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
365 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
366 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
367 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
368 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
369 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
370 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
371 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
372 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
377 ret = pm_runtime_get_sync(ddev->dev);
379 pm_runtime_put_autosuspend(ddev->dev);
383 if (is_support_sw_smu(adev))
384 current_level = smu_get_performance_level(&adev->smu);
385 else if (adev->powerplay.pp_funcs->get_performance_level)
386 current_level = amdgpu_dpm_get_performance_level(adev);
388 if (current_level == level) {
389 pm_runtime_mark_last_busy(ddev->dev);
390 pm_runtime_put_autosuspend(ddev->dev);
394 if (adev->asic_type == CHIP_RAVEN) {
395 if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) {
396 if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL)
397 amdgpu_gfx_off_ctrl(adev, false);
398 else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL)
399 amdgpu_gfx_off_ctrl(adev, true);
403 /* profile_exit setting is valid only when current mode is in profile mode */
404 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
405 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
406 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
407 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
408 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
409 pr_err("Currently not in any profile mode!\n");
410 pm_runtime_mark_last_busy(ddev->dev);
411 pm_runtime_put_autosuspend(ddev->dev);
415 if (is_support_sw_smu(adev)) {
416 ret = smu_force_performance_level(&adev->smu, level);
418 pm_runtime_mark_last_busy(ddev->dev);
419 pm_runtime_put_autosuspend(ddev->dev);
422 } else if (adev->powerplay.pp_funcs->force_performance_level) {
423 mutex_lock(&adev->pm.mutex);
424 if (adev->pm.dpm.thermal_active) {
425 mutex_unlock(&adev->pm.mutex);
426 pm_runtime_mark_last_busy(ddev->dev);
427 pm_runtime_put_autosuspend(ddev->dev);
430 ret = amdgpu_dpm_force_performance_level(adev, level);
432 mutex_unlock(&adev->pm.mutex);
433 pm_runtime_mark_last_busy(ddev->dev);
434 pm_runtime_put_autosuspend(ddev->dev);
437 adev->pm.dpm.forced_level = level;
439 mutex_unlock(&adev->pm.mutex);
441 pm_runtime_mark_last_busy(ddev->dev);
442 pm_runtime_put_autosuspend(ddev->dev);
447 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
448 struct device_attribute *attr,
451 struct drm_device *ddev = dev_get_drvdata(dev);
452 struct amdgpu_device *adev = ddev->dev_private;
453 struct pp_states_info data;
456 if (adev->in_gpu_reset)
459 ret = pm_runtime_get_sync(ddev->dev);
461 pm_runtime_put_autosuspend(ddev->dev);
465 if (is_support_sw_smu(adev)) {
466 ret = smu_get_power_num_states(&adev->smu, &data);
469 } else if (adev->powerplay.pp_funcs->get_pp_num_states) {
470 amdgpu_dpm_get_pp_num_states(adev, &data);
472 memset(&data, 0, sizeof(data));
475 pm_runtime_mark_last_busy(ddev->dev);
476 pm_runtime_put_autosuspend(ddev->dev);
478 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
479 for (i = 0; i < data.nums; i++)
480 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
481 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
482 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
483 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
484 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
489 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
490 struct device_attribute *attr,
493 struct drm_device *ddev = dev_get_drvdata(dev);
494 struct amdgpu_device *adev = ddev->dev_private;
495 struct pp_states_info data;
496 struct smu_context *smu = &adev->smu;
497 enum amd_pm_state_type pm = 0;
500 if (adev->in_gpu_reset)
503 ret = pm_runtime_get_sync(ddev->dev);
505 pm_runtime_put_autosuspend(ddev->dev);
509 if (is_support_sw_smu(adev)) {
510 pm = smu_get_current_power_state(smu);
511 ret = smu_get_power_num_states(smu, &data);
514 } else if (adev->powerplay.pp_funcs->get_current_power_state
515 && adev->powerplay.pp_funcs->get_pp_num_states) {
516 pm = amdgpu_dpm_get_current_power_state(adev);
517 amdgpu_dpm_get_pp_num_states(adev, &data);
520 pm_runtime_mark_last_busy(ddev->dev);
521 pm_runtime_put_autosuspend(ddev->dev);
523 for (i = 0; i < data.nums; i++) {
524 if (pm == data.states[i])
531 return snprintf(buf, PAGE_SIZE, "%d\n", i);
534 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
535 struct device_attribute *attr,
538 struct drm_device *ddev = dev_get_drvdata(dev);
539 struct amdgpu_device *adev = ddev->dev_private;
541 if (adev->in_gpu_reset)
544 if (adev->pp_force_state_enabled)
545 return amdgpu_get_pp_cur_state(dev, attr, buf);
547 return snprintf(buf, PAGE_SIZE, "\n");
550 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
551 struct device_attribute *attr,
555 struct drm_device *ddev = dev_get_drvdata(dev);
556 struct amdgpu_device *adev = ddev->dev_private;
557 enum amd_pm_state_type state = 0;
561 if (adev->in_gpu_reset)
564 if (strlen(buf) == 1)
565 adev->pp_force_state_enabled = false;
566 else if (is_support_sw_smu(adev))
567 adev->pp_force_state_enabled = false;
568 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
569 adev->powerplay.pp_funcs->get_pp_num_states) {
570 struct pp_states_info data;
572 ret = kstrtoul(buf, 0, &idx);
573 if (ret || idx >= ARRAY_SIZE(data.states))
576 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
578 amdgpu_dpm_get_pp_num_states(adev, &data);
579 state = data.states[idx];
581 ret = pm_runtime_get_sync(ddev->dev);
583 pm_runtime_put_autosuspend(ddev->dev);
587 /* only set user selected power states */
588 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
589 state != POWER_STATE_TYPE_DEFAULT) {
590 amdgpu_dpm_dispatch_task(adev,
591 AMD_PP_TASK_ENABLE_USER_STATE, &state);
592 adev->pp_force_state_enabled = true;
594 pm_runtime_mark_last_busy(ddev->dev);
595 pm_runtime_put_autosuspend(ddev->dev);
604 * The amdgpu driver provides a sysfs API for uploading new powerplay
605 * tables. The file pp_table is used for this. Reading the file
606 * will dump the current power play table. Writing to the file
607 * will attempt to upload a new powerplay table and re-initialize
608 * powerplay using that new table.
612 static ssize_t amdgpu_get_pp_table(struct device *dev,
613 struct device_attribute *attr,
616 struct drm_device *ddev = dev_get_drvdata(dev);
617 struct amdgpu_device *adev = ddev->dev_private;
621 if (adev->in_gpu_reset)
624 ret = pm_runtime_get_sync(ddev->dev);
626 pm_runtime_put_autosuspend(ddev->dev);
630 if (is_support_sw_smu(adev)) {
631 size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
632 pm_runtime_mark_last_busy(ddev->dev);
633 pm_runtime_put_autosuspend(ddev->dev);
636 } else if (adev->powerplay.pp_funcs->get_pp_table) {
637 size = amdgpu_dpm_get_pp_table(adev, &table);
638 pm_runtime_mark_last_busy(ddev->dev);
639 pm_runtime_put_autosuspend(ddev->dev);
643 pm_runtime_mark_last_busy(ddev->dev);
644 pm_runtime_put_autosuspend(ddev->dev);
648 if (size >= PAGE_SIZE)
649 size = PAGE_SIZE - 1;
651 memcpy(buf, table, size);
656 static ssize_t amdgpu_set_pp_table(struct device *dev,
657 struct device_attribute *attr,
661 struct drm_device *ddev = dev_get_drvdata(dev);
662 struct amdgpu_device *adev = ddev->dev_private;
665 if (adev->in_gpu_reset)
668 ret = pm_runtime_get_sync(ddev->dev);
670 pm_runtime_put_autosuspend(ddev->dev);
674 if (is_support_sw_smu(adev)) {
675 ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
677 pm_runtime_mark_last_busy(ddev->dev);
678 pm_runtime_put_autosuspend(ddev->dev);
681 } else if (adev->powerplay.pp_funcs->set_pp_table)
682 amdgpu_dpm_set_pp_table(adev, buf, count);
684 pm_runtime_mark_last_busy(ddev->dev);
685 pm_runtime_put_autosuspend(ddev->dev);
691 * DOC: pp_od_clk_voltage
693 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
694 * in each power level within a power state. The pp_od_clk_voltage is used for
697 * < For Vega10 and previous ASICs >
699 * Reading the file will display:
701 * - a list of engine clock levels and voltages labeled OD_SCLK
703 * - a list of memory clock levels and voltages labeled OD_MCLK
705 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
707 * To manually adjust these settings, first select manual using
708 * power_dpm_force_performance_level. Enter a new value for each
709 * level by writing a string that contains "s/m level clock voltage" to
710 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
711 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
712 * 810 mV. When you have edited all of the states as needed, write
713 * "c" (commit) to the file to commit your changes. If you want to reset to the
714 * default power levels, write "r" (reset) to the file to reset them.
717 * < For Vega20 and newer ASICs >
719 * Reading the file will display:
721 * - minimum and maximum engine clock labeled OD_SCLK
723 * - maximum memory clock labeled OD_MCLK
725 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
726 * They can be used to calibrate the sclk voltage curve.
728 * - a list of valid ranges for sclk, mclk, and voltage curve points
731 * To manually adjust these settings:
733 * - First select manual using power_dpm_force_performance_level
735 * - For clock frequency setting, enter a new value by writing a
736 * string that contains "s/m index clock" to the file. The index
737 * should be 0 if to set minimum clock. And 1 if to set maximum
738 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
739 * "m 1 800" will update maximum mclk to be 800Mhz.
741 * For sclk voltage curve, enter the new values by writing a
742 * string that contains "vc point clock voltage" to the file. The
743 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
744 * update point1 with clock set as 300Mhz and voltage as
745 * 600mV. "vc 2 1000 1000" will update point3 with clock set
746 * as 1000Mhz and voltage 1000mV.
748 * - When you have edited all of the states as needed, write "c" (commit)
749 * to the file to commit your changes
751 * - If you want to reset to the default power levels, write "r" (reset)
752 * to the file to reset them
756 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
757 struct device_attribute *attr,
761 struct drm_device *ddev = dev_get_drvdata(dev);
762 struct amdgpu_device *adev = ddev->dev_private;
764 uint32_t parameter_size = 0;
769 const char delimiter[3] = {' ', '\n', '\0'};
772 if (adev->in_gpu_reset)
779 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
780 else if (*buf == 'm')
781 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
783 type = PP_OD_RESTORE_DEFAULT_TABLE;
784 else if (*buf == 'c')
785 type = PP_OD_COMMIT_DPM_TABLE;
786 else if (!strncmp(buf, "vc", 2))
787 type = PP_OD_EDIT_VDDC_CURVE;
791 memcpy(buf_cpy, buf, count+1);
795 if (type == PP_OD_EDIT_VDDC_CURVE)
797 while (isspace(*++tmp_str));
800 sub_str = strsep(&tmp_str, delimiter);
801 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
806 while (isspace(*tmp_str))
810 ret = pm_runtime_get_sync(ddev->dev);
812 pm_runtime_put_autosuspend(ddev->dev);
816 if (is_support_sw_smu(adev)) {
817 ret = smu_od_edit_dpm_table(&adev->smu, type,
818 parameter, parameter_size);
821 pm_runtime_mark_last_busy(ddev->dev);
822 pm_runtime_put_autosuspend(ddev->dev);
826 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
827 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
828 parameter, parameter_size);
830 pm_runtime_mark_last_busy(ddev->dev);
831 pm_runtime_put_autosuspend(ddev->dev);
836 if (type == PP_OD_COMMIT_DPM_TABLE) {
837 if (adev->powerplay.pp_funcs->dispatch_tasks) {
838 amdgpu_dpm_dispatch_task(adev,
839 AMD_PP_TASK_READJUST_POWER_STATE,
841 pm_runtime_mark_last_busy(ddev->dev);
842 pm_runtime_put_autosuspend(ddev->dev);
845 pm_runtime_mark_last_busy(ddev->dev);
846 pm_runtime_put_autosuspend(ddev->dev);
851 pm_runtime_mark_last_busy(ddev->dev);
852 pm_runtime_put_autosuspend(ddev->dev);
857 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
858 struct device_attribute *attr,
861 struct drm_device *ddev = dev_get_drvdata(dev);
862 struct amdgpu_device *adev = ddev->dev_private;
866 if (adev->in_gpu_reset)
869 ret = pm_runtime_get_sync(ddev->dev);
871 pm_runtime_put_autosuspend(ddev->dev);
875 if (is_support_sw_smu(adev)) {
876 size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf);
877 size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size);
878 size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size);
879 size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size);
880 } else if (adev->powerplay.pp_funcs->print_clock_levels) {
881 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
882 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
883 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
884 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
886 size = snprintf(buf, PAGE_SIZE, "\n");
888 pm_runtime_mark_last_busy(ddev->dev);
889 pm_runtime_put_autosuspend(ddev->dev);
897 * The amdgpu driver provides a sysfs API for adjusting what powerplay
898 * features to be enabled. The file pp_features is used for this. And
899 * this is only available for Vega10 and later dGPUs.
901 * Reading back the file will show you the followings:
902 * - Current ppfeature masks
903 * - List of the all supported powerplay features with their naming,
904 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
906 * To manually enable or disable a specific feature, just set or clear
907 * the corresponding bit from original ppfeature masks and input the
908 * new ppfeature masks.
910 static ssize_t amdgpu_set_pp_features(struct device *dev,
911 struct device_attribute *attr,
915 struct drm_device *ddev = dev_get_drvdata(dev);
916 struct amdgpu_device *adev = ddev->dev_private;
917 uint64_t featuremask;
920 if (adev->in_gpu_reset)
923 ret = kstrtou64(buf, 0, &featuremask);
927 pr_debug("featuremask = 0x%llx\n", featuremask);
929 ret = pm_runtime_get_sync(ddev->dev);
931 pm_runtime_put_autosuspend(ddev->dev);
935 if (is_support_sw_smu(adev)) {
936 ret = smu_sys_set_pp_feature_mask(&adev->smu, featuremask);
938 pm_runtime_mark_last_busy(ddev->dev);
939 pm_runtime_put_autosuspend(ddev->dev);
942 } else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
943 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
945 pm_runtime_mark_last_busy(ddev->dev);
946 pm_runtime_put_autosuspend(ddev->dev);
950 pm_runtime_mark_last_busy(ddev->dev);
951 pm_runtime_put_autosuspend(ddev->dev);
956 static ssize_t amdgpu_get_pp_features(struct device *dev,
957 struct device_attribute *attr,
960 struct drm_device *ddev = dev_get_drvdata(dev);
961 struct amdgpu_device *adev = ddev->dev_private;
965 if (adev->in_gpu_reset)
968 ret = pm_runtime_get_sync(ddev->dev);
970 pm_runtime_put_autosuspend(ddev->dev);
974 if (is_support_sw_smu(adev))
975 size = smu_sys_get_pp_feature_mask(&adev->smu, buf);
976 else if (adev->powerplay.pp_funcs->get_ppfeature_status)
977 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
979 size = snprintf(buf, PAGE_SIZE, "\n");
981 pm_runtime_mark_last_busy(ddev->dev);
982 pm_runtime_put_autosuspend(ddev->dev);
988 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
990 * The amdgpu driver provides a sysfs API for adjusting what power levels
991 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
992 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
995 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
996 * Vega10 and later ASICs.
997 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
999 * Reading back the files will show you the available power levels within
1000 * the power state and the clock information for those levels.
1002 * To manually adjust these states, first select manual using
1003 * power_dpm_force_performance_level.
1004 * Secondly, enter a new value for each level by inputing a string that
1005 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1008 * .. code-block:: bash
1010 * echo "4 5 6" > pp_dpm_sclk
1012 * will enable sclk levels 4, 5, and 6.
1014 * NOTE: change to the dcefclk max dpm level is not supported now
1017 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1018 struct device_attribute *attr,
1021 struct drm_device *ddev = dev_get_drvdata(dev);
1022 struct amdgpu_device *adev = ddev->dev_private;
1026 if (adev->in_gpu_reset)
1029 ret = pm_runtime_get_sync(ddev->dev);
1031 pm_runtime_put_autosuspend(ddev->dev);
1035 if (is_support_sw_smu(adev))
1036 size = smu_print_clk_levels(&adev->smu, SMU_SCLK, buf);
1037 else if (adev->powerplay.pp_funcs->print_clock_levels)
1038 size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
1040 size = snprintf(buf, PAGE_SIZE, "\n");
1042 pm_runtime_mark_last_busy(ddev->dev);
1043 pm_runtime_put_autosuspend(ddev->dev);
1049 * Worst case: 32 bits individually specified, in octal at 12 characters
1050 * per line (+1 for \n).
1052 #define AMDGPU_MASK_BUF_MAX (32 * 13)
1054 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1058 char *sub_str = NULL;
1060 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1061 const char delimiter[3] = {' ', '\n', '\0'};
1066 bytes = min(count, sizeof(buf_cpy) - 1);
1067 memcpy(buf_cpy, buf, bytes);
1068 buf_cpy[bytes] = '\0';
1071 sub_str = strsep(&tmp, delimiter);
1072 if (strlen(sub_str)) {
1073 ret = kstrtol(sub_str, 0, &level);
1076 *mask |= 1 << level;
1084 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1085 struct device_attribute *attr,
1089 struct drm_device *ddev = dev_get_drvdata(dev);
1090 struct amdgpu_device *adev = ddev->dev_private;
1094 if (adev->in_gpu_reset)
1097 ret = amdgpu_read_mask(buf, count, &mask);
1101 ret = pm_runtime_get_sync(ddev->dev);
1103 pm_runtime_put_autosuspend(ddev->dev);
1107 if (is_support_sw_smu(adev))
1108 ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask, true);
1109 else if (adev->powerplay.pp_funcs->force_clock_level)
1110 ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
1112 pm_runtime_mark_last_busy(ddev->dev);
1113 pm_runtime_put_autosuspend(ddev->dev);
1121 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1122 struct device_attribute *attr,
1125 struct drm_device *ddev = dev_get_drvdata(dev);
1126 struct amdgpu_device *adev = ddev->dev_private;
1130 if (adev->in_gpu_reset)
1133 ret = pm_runtime_get_sync(ddev->dev);
1135 pm_runtime_put_autosuspend(ddev->dev);
1139 if (is_support_sw_smu(adev))
1140 size = smu_print_clk_levels(&adev->smu, SMU_MCLK, buf);
1141 else if (adev->powerplay.pp_funcs->print_clock_levels)
1142 size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
1144 size = snprintf(buf, PAGE_SIZE, "\n");
1146 pm_runtime_mark_last_busy(ddev->dev);
1147 pm_runtime_put_autosuspend(ddev->dev);
1152 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1153 struct device_attribute *attr,
1157 struct drm_device *ddev = dev_get_drvdata(dev);
1158 struct amdgpu_device *adev = ddev->dev_private;
1162 if (adev->in_gpu_reset)
1165 ret = amdgpu_read_mask(buf, count, &mask);
1169 ret = pm_runtime_get_sync(ddev->dev);
1171 pm_runtime_put_autosuspend(ddev->dev);
1175 if (is_support_sw_smu(adev))
1176 ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask, true);
1177 else if (adev->powerplay.pp_funcs->force_clock_level)
1178 ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
1180 pm_runtime_mark_last_busy(ddev->dev);
1181 pm_runtime_put_autosuspend(ddev->dev);
1189 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1190 struct device_attribute *attr,
1193 struct drm_device *ddev = dev_get_drvdata(dev);
1194 struct amdgpu_device *adev = ddev->dev_private;
1198 if (adev->in_gpu_reset)
1201 ret = pm_runtime_get_sync(ddev->dev);
1203 pm_runtime_put_autosuspend(ddev->dev);
1207 if (is_support_sw_smu(adev))
1208 size = smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf);
1209 else if (adev->powerplay.pp_funcs->print_clock_levels)
1210 size = amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
1212 size = snprintf(buf, PAGE_SIZE, "\n");
1214 pm_runtime_mark_last_busy(ddev->dev);
1215 pm_runtime_put_autosuspend(ddev->dev);
1220 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1221 struct device_attribute *attr,
1225 struct drm_device *ddev = dev_get_drvdata(dev);
1226 struct amdgpu_device *adev = ddev->dev_private;
1230 if (adev->in_gpu_reset)
1233 ret = amdgpu_read_mask(buf, count, &mask);
1237 ret = pm_runtime_get_sync(ddev->dev);
1239 pm_runtime_put_autosuspend(ddev->dev);
1243 if (is_support_sw_smu(adev))
1244 ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask, true);
1245 else if (adev->powerplay.pp_funcs->force_clock_level)
1246 ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
1250 pm_runtime_mark_last_busy(ddev->dev);
1251 pm_runtime_put_autosuspend(ddev->dev);
1259 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1260 struct device_attribute *attr,
1263 struct drm_device *ddev = dev_get_drvdata(dev);
1264 struct amdgpu_device *adev = ddev->dev_private;
1268 if (adev->in_gpu_reset)
1271 ret = pm_runtime_get_sync(ddev->dev);
1273 pm_runtime_put_autosuspend(ddev->dev);
1277 if (is_support_sw_smu(adev))
1278 size = smu_print_clk_levels(&adev->smu, SMU_FCLK, buf);
1279 else if (adev->powerplay.pp_funcs->print_clock_levels)
1280 size = amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
1282 size = snprintf(buf, PAGE_SIZE, "\n");
1284 pm_runtime_mark_last_busy(ddev->dev);
1285 pm_runtime_put_autosuspend(ddev->dev);
1290 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1291 struct device_attribute *attr,
1295 struct drm_device *ddev = dev_get_drvdata(dev);
1296 struct amdgpu_device *adev = ddev->dev_private;
1300 if (adev->in_gpu_reset)
1303 ret = amdgpu_read_mask(buf, count, &mask);
1307 ret = pm_runtime_get_sync(ddev->dev);
1309 pm_runtime_put_autosuspend(ddev->dev);
1313 if (is_support_sw_smu(adev))
1314 ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask, true);
1315 else if (adev->powerplay.pp_funcs->force_clock_level)
1316 ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1320 pm_runtime_mark_last_busy(ddev->dev);
1321 pm_runtime_put_autosuspend(ddev->dev);
1329 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1330 struct device_attribute *attr,
1333 struct drm_device *ddev = dev_get_drvdata(dev);
1334 struct amdgpu_device *adev = ddev->dev_private;
1338 if (adev->in_gpu_reset)
1341 ret = pm_runtime_get_sync(ddev->dev);
1343 pm_runtime_put_autosuspend(ddev->dev);
1347 if (is_support_sw_smu(adev))
1348 size = smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf);
1349 else if (adev->powerplay.pp_funcs->print_clock_levels)
1350 size = amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1352 size = snprintf(buf, PAGE_SIZE, "\n");
1354 pm_runtime_mark_last_busy(ddev->dev);
1355 pm_runtime_put_autosuspend(ddev->dev);
1360 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1361 struct device_attribute *attr,
1365 struct drm_device *ddev = dev_get_drvdata(dev);
1366 struct amdgpu_device *adev = ddev->dev_private;
1370 if (adev->in_gpu_reset)
1373 ret = amdgpu_read_mask(buf, count, &mask);
1377 ret = pm_runtime_get_sync(ddev->dev);
1379 pm_runtime_put_autosuspend(ddev->dev);
1383 if (is_support_sw_smu(adev))
1384 ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask, true);
1385 else if (adev->powerplay.pp_funcs->force_clock_level)
1386 ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1390 pm_runtime_mark_last_busy(ddev->dev);
1391 pm_runtime_put_autosuspend(ddev->dev);
1399 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1400 struct device_attribute *attr,
1403 struct drm_device *ddev = dev_get_drvdata(dev);
1404 struct amdgpu_device *adev = ddev->dev_private;
1408 if (adev->in_gpu_reset)
1411 ret = pm_runtime_get_sync(ddev->dev);
1413 pm_runtime_put_autosuspend(ddev->dev);
1417 if (is_support_sw_smu(adev))
1418 size = smu_print_clk_levels(&adev->smu, SMU_PCIE, buf);
1419 else if (adev->powerplay.pp_funcs->print_clock_levels)
1420 size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1422 size = snprintf(buf, PAGE_SIZE, "\n");
1424 pm_runtime_mark_last_busy(ddev->dev);
1425 pm_runtime_put_autosuspend(ddev->dev);
1430 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1431 struct device_attribute *attr,
1435 struct drm_device *ddev = dev_get_drvdata(dev);
1436 struct amdgpu_device *adev = ddev->dev_private;
1440 if (adev->in_gpu_reset)
1443 ret = amdgpu_read_mask(buf, count, &mask);
1447 ret = pm_runtime_get_sync(ddev->dev);
1449 pm_runtime_put_autosuspend(ddev->dev);
1453 if (is_support_sw_smu(adev))
1454 ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask, true);
1455 else if (adev->powerplay.pp_funcs->force_clock_level)
1456 ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1460 pm_runtime_mark_last_busy(ddev->dev);
1461 pm_runtime_put_autosuspend(ddev->dev);
1469 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1470 struct device_attribute *attr,
1473 struct drm_device *ddev = dev_get_drvdata(dev);
1474 struct amdgpu_device *adev = ddev->dev_private;
1478 if (adev->in_gpu_reset)
1481 ret = pm_runtime_get_sync(ddev->dev);
1483 pm_runtime_put_autosuspend(ddev->dev);
1487 if (is_support_sw_smu(adev))
1488 value = smu_get_od_percentage(&(adev->smu), SMU_OD_SCLK);
1489 else if (adev->powerplay.pp_funcs->get_sclk_od)
1490 value = amdgpu_dpm_get_sclk_od(adev);
1492 pm_runtime_mark_last_busy(ddev->dev);
1493 pm_runtime_put_autosuspend(ddev->dev);
1495 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1498 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1499 struct device_attribute *attr,
1503 struct drm_device *ddev = dev_get_drvdata(dev);
1504 struct amdgpu_device *adev = ddev->dev_private;
1508 if (adev->in_gpu_reset)
1511 ret = kstrtol(buf, 0, &value);
1516 ret = pm_runtime_get_sync(ddev->dev);
1518 pm_runtime_put_autosuspend(ddev->dev);
1522 if (is_support_sw_smu(adev)) {
1523 value = smu_set_od_percentage(&(adev->smu), SMU_OD_SCLK, (uint32_t)value);
1525 if (adev->powerplay.pp_funcs->set_sclk_od)
1526 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1528 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1529 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1531 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1532 amdgpu_pm_compute_clocks(adev);
1536 pm_runtime_mark_last_busy(ddev->dev);
1537 pm_runtime_put_autosuspend(ddev->dev);
1542 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1543 struct device_attribute *attr,
1546 struct drm_device *ddev = dev_get_drvdata(dev);
1547 struct amdgpu_device *adev = ddev->dev_private;
1551 if (adev->in_gpu_reset)
1554 ret = pm_runtime_get_sync(ddev->dev);
1556 pm_runtime_put_autosuspend(ddev->dev);
1560 if (is_support_sw_smu(adev))
1561 value = smu_get_od_percentage(&(adev->smu), SMU_OD_MCLK);
1562 else if (adev->powerplay.pp_funcs->get_mclk_od)
1563 value = amdgpu_dpm_get_mclk_od(adev);
1565 pm_runtime_mark_last_busy(ddev->dev);
1566 pm_runtime_put_autosuspend(ddev->dev);
1568 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1571 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1572 struct device_attribute *attr,
1576 struct drm_device *ddev = dev_get_drvdata(dev);
1577 struct amdgpu_device *adev = ddev->dev_private;
1581 if (adev->in_gpu_reset)
1584 ret = kstrtol(buf, 0, &value);
1589 ret = pm_runtime_get_sync(ddev->dev);
1591 pm_runtime_put_autosuspend(ddev->dev);
1595 if (is_support_sw_smu(adev)) {
1596 value = smu_set_od_percentage(&(adev->smu), SMU_OD_MCLK, (uint32_t)value);
1598 if (adev->powerplay.pp_funcs->set_mclk_od)
1599 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1601 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1602 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1604 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1605 amdgpu_pm_compute_clocks(adev);
1609 pm_runtime_mark_last_busy(ddev->dev);
1610 pm_runtime_put_autosuspend(ddev->dev);
1616 * DOC: pp_power_profile_mode
1618 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1619 * related to switching between power levels in a power state. The file
1620 * pp_power_profile_mode is used for this.
1622 * Reading this file outputs a list of all of the predefined power profiles
1623 * and the relevant heuristics settings for that profile.
1625 * To select a profile or create a custom profile, first select manual using
1626 * power_dpm_force_performance_level. Writing the number of a predefined
1627 * profile to pp_power_profile_mode will enable those heuristics. To
1628 * create a custom set of heuristics, write a string of numbers to the file
1629 * starting with the number of the custom profile along with a setting
1630 * for each heuristic parameter. Due to differences across asic families
1631 * the heuristic parameters vary from family to family.
1635 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1636 struct device_attribute *attr,
1639 struct drm_device *ddev = dev_get_drvdata(dev);
1640 struct amdgpu_device *adev = ddev->dev_private;
1644 if (adev->in_gpu_reset)
1647 ret = pm_runtime_get_sync(ddev->dev);
1649 pm_runtime_put_autosuspend(ddev->dev);
1653 if (is_support_sw_smu(adev))
1654 size = smu_get_power_profile_mode(&adev->smu, buf);
1655 else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1656 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1658 size = snprintf(buf, PAGE_SIZE, "\n");
1660 pm_runtime_mark_last_busy(ddev->dev);
1661 pm_runtime_put_autosuspend(ddev->dev);
1667 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1668 struct device_attribute *attr,
1673 struct drm_device *ddev = dev_get_drvdata(dev);
1674 struct amdgpu_device *adev = ddev->dev_private;
1675 uint32_t parameter_size = 0;
1677 char *sub_str, buf_cpy[128];
1681 long int profile_mode = 0;
1682 const char delimiter[3] = {' ', '\n', '\0'};
1684 if (adev->in_gpu_reset)
1689 ret = kstrtol(tmp, 0, &profile_mode);
1693 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1694 if (count < 2 || count > 127)
1696 while (isspace(*++buf))
1698 memcpy(buf_cpy, buf, count-i);
1700 while (tmp_str[0]) {
1701 sub_str = strsep(&tmp_str, delimiter);
1702 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1706 while (isspace(*tmp_str))
1710 parameter[parameter_size] = profile_mode;
1712 ret = pm_runtime_get_sync(ddev->dev);
1714 pm_runtime_put_autosuspend(ddev->dev);
1718 if (is_support_sw_smu(adev))
1719 ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size, true);
1720 else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1721 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1723 pm_runtime_mark_last_busy(ddev->dev);
1724 pm_runtime_put_autosuspend(ddev->dev);
1733 * DOC: gpu_busy_percent
1735 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1736 * is as a percentage. The file gpu_busy_percent is used for this.
1737 * The SMU firmware computes a percentage of load based on the
1738 * aggregate activity level in the IP cores.
1740 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1741 struct device_attribute *attr,
1744 struct drm_device *ddev = dev_get_drvdata(dev);
1745 struct amdgpu_device *adev = ddev->dev_private;
1746 int r, value, size = sizeof(value);
1748 if (adev->in_gpu_reset)
1751 r = pm_runtime_get_sync(ddev->dev);
1753 pm_runtime_put_autosuspend(ddev->dev);
1757 /* read the IP busy sensor */
1758 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1759 (void *)&value, &size);
1761 pm_runtime_mark_last_busy(ddev->dev);
1762 pm_runtime_put_autosuspend(ddev->dev);
1767 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1771 * DOC: mem_busy_percent
1773 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1774 * is as a percentage. The file mem_busy_percent is used for this.
1775 * The SMU firmware computes a percentage of load based on the
1776 * aggregate activity level in the IP cores.
1778 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1779 struct device_attribute *attr,
1782 struct drm_device *ddev = dev_get_drvdata(dev);
1783 struct amdgpu_device *adev = ddev->dev_private;
1784 int r, value, size = sizeof(value);
1786 if (adev->in_gpu_reset)
1789 r = pm_runtime_get_sync(ddev->dev);
1791 pm_runtime_put_autosuspend(ddev->dev);
1795 /* read the IP busy sensor */
1796 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1797 (void *)&value, &size);
1799 pm_runtime_mark_last_busy(ddev->dev);
1800 pm_runtime_put_autosuspend(ddev->dev);
1805 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1811 * The amdgpu driver provides a sysfs API for estimating how much data
1812 * has been received and sent by the GPU in the last second through PCIe.
1813 * The file pcie_bw is used for this.
1814 * The Perf counters count the number of received and sent messages and return
1815 * those values, as well as the maximum payload size of a PCIe packet (mps).
1816 * Note that it is not possible to easily and quickly obtain the size of each
1817 * packet transmitted, so we output the max payload size (mps) to allow for
1818 * quick estimation of the PCIe bandwidth usage
1820 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1821 struct device_attribute *attr,
1824 struct drm_device *ddev = dev_get_drvdata(dev);
1825 struct amdgpu_device *adev = ddev->dev_private;
1826 uint64_t count0 = 0, count1 = 0;
1829 if (adev->in_gpu_reset)
1832 if (adev->flags & AMD_IS_APU)
1835 if (!adev->asic_funcs->get_pcie_usage)
1838 ret = pm_runtime_get_sync(ddev->dev);
1840 pm_runtime_put_autosuspend(ddev->dev);
1844 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1846 pm_runtime_mark_last_busy(ddev->dev);
1847 pm_runtime_put_autosuspend(ddev->dev);
1849 return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n",
1850 count0, count1, pcie_get_mps(adev->pdev));
1856 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1857 * The file unique_id is used for this.
1858 * This will provide a Unique ID that will persist from machine to machine
1860 * NOTE: This will only work for GFX9 and newer. This file will be absent
1861 * on unsupported ASICs (GFX8 and older)
1863 static ssize_t amdgpu_get_unique_id(struct device *dev,
1864 struct device_attribute *attr,
1867 struct drm_device *ddev = dev_get_drvdata(dev);
1868 struct amdgpu_device *adev = ddev->dev_private;
1870 if (adev->in_gpu_reset)
1873 if (adev->unique_id)
1874 return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1880 * DOC: thermal_throttling_logging
1882 * Thermal throttling pulls down the clock frequency and thus the performance.
1883 * It's an useful mechanism to protect the chip from overheating. Since it
1884 * impacts performance, the user controls whether it is enabled and if so,
1885 * the log frequency.
1887 * Reading back the file shows you the status(enabled or disabled) and
1888 * the interval(in seconds) between each thermal logging.
1890 * Writing an integer to the file, sets a new logging interval, in seconds.
1891 * The value should be between 1 and 3600. If the value is less than 1,
1892 * thermal logging is disabled. Values greater than 3600 are ignored.
1894 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1895 struct device_attribute *attr,
1898 struct drm_device *ddev = dev_get_drvdata(dev);
1899 struct amdgpu_device *adev = ddev->dev_private;
1901 return snprintf(buf, PAGE_SIZE, "%s: thermal throttling logging %s, with interval %d seconds\n",
1903 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1904 adev->throttling_logging_rs.interval / HZ + 1);
1907 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1908 struct device_attribute *attr,
1912 struct drm_device *ddev = dev_get_drvdata(dev);
1913 struct amdgpu_device *adev = ddev->dev_private;
1914 long throttling_logging_interval;
1915 unsigned long flags;
1918 ret = kstrtol(buf, 0, &throttling_logging_interval);
1922 if (throttling_logging_interval > 3600)
1925 if (throttling_logging_interval > 0) {
1926 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1928 * Reset the ratelimit timer internals.
1929 * This can effectively restart the timer.
1931 adev->throttling_logging_rs.interval =
1932 (throttling_logging_interval - 1) * HZ;
1933 adev->throttling_logging_rs.begin = 0;
1934 adev->throttling_logging_rs.printed = 0;
1935 adev->throttling_logging_rs.missed = 0;
1936 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1938 atomic_set(&adev->throttling_logging_enabled, 1);
1940 atomic_set(&adev->throttling_logging_enabled, 0);
1946 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1947 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1948 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC),
1949 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC),
1950 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC),
1951 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC),
1952 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC),
1953 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1954 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1955 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1956 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1957 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC),
1958 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC),
1959 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
1960 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
1961 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC),
1962 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC),
1963 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC),
1964 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC),
1965 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
1966 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC),
1967 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC),
1968 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC),
1971 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1972 uint32_t mask, enum amdgpu_device_attr_states *states)
1974 struct device_attribute *dev_attr = &attr->dev_attr;
1975 const char *attr_name = dev_attr->attr.name;
1976 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
1977 enum amd_asic_type asic_type = adev->asic_type;
1979 if (!(attr->flags & mask)) {
1980 *states = ATTR_STATE_UNSUPPORTED;
1984 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name))
1986 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
1987 if (asic_type < CHIP_VEGA10)
1988 *states = ATTR_STATE_UNSUPPORTED;
1989 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
1990 if (asic_type < CHIP_VEGA10 || asic_type == CHIP_ARCTURUS)
1991 *states = ATTR_STATE_UNSUPPORTED;
1992 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
1993 if (asic_type < CHIP_VEGA20)
1994 *states = ATTR_STATE_UNSUPPORTED;
1995 } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
1996 if (asic_type == CHIP_ARCTURUS)
1997 *states = ATTR_STATE_UNSUPPORTED;
1998 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
1999 *states = ATTR_STATE_UNSUPPORTED;
2000 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2001 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2002 *states = ATTR_STATE_SUPPORTED;
2003 } else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2004 if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2005 *states = ATTR_STATE_UNSUPPORTED;
2006 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2007 /* PCIe Perf counters won't work on APU nodes */
2008 if (adev->flags & AMD_IS_APU)
2009 *states = ATTR_STATE_UNSUPPORTED;
2010 } else if (DEVICE_ATTR_IS(unique_id)) {
2011 if (asic_type != CHIP_VEGA10 &&
2012 asic_type != CHIP_VEGA20 &&
2013 asic_type != CHIP_ARCTURUS)
2014 *states = ATTR_STATE_UNSUPPORTED;
2015 } else if (DEVICE_ATTR_IS(pp_features)) {
2016 if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2017 *states = ATTR_STATE_UNSUPPORTED;
2020 if (asic_type == CHIP_ARCTURUS) {
2021 /* Arcturus does not support standalone mclk/socclk/fclk level setting */
2022 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2023 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2024 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2025 dev_attr->attr.mode &= ~S_IWUGO;
2026 dev_attr->store = NULL;
2030 #undef DEVICE_ATTR_IS
2036 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2037 struct amdgpu_device_attr *attr,
2038 uint32_t mask, struct list_head *attr_list)
2041 struct device_attribute *dev_attr = &attr->dev_attr;
2042 const char *name = dev_attr->attr.name;
2043 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2044 struct amdgpu_device_attr_entry *attr_entry;
2046 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2047 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2051 attr_update = attr->attr_update ? attr_update : default_attr_update;
2053 ret = attr_update(adev, attr, mask, &attr_states);
2055 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2060 if (attr_states == ATTR_STATE_UNSUPPORTED)
2063 ret = device_create_file(adev->dev, dev_attr);
2065 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2069 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2073 attr_entry->attr = attr;
2074 INIT_LIST_HEAD(&attr_entry->entry);
2076 list_add_tail(&attr_entry->entry, attr_list);
2081 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2083 struct device_attribute *dev_attr = &attr->dev_attr;
2085 device_remove_file(adev->dev, dev_attr);
2088 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2089 struct list_head *attr_list);
2091 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2092 struct amdgpu_device_attr *attrs,
2095 struct list_head *attr_list)
2100 for (i = 0; i < counts; i++) {
2101 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2109 amdgpu_device_attr_remove_groups(adev, attr_list);
2114 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2115 struct list_head *attr_list)
2117 struct amdgpu_device_attr_entry *entry, *entry_tmp;
2119 if (list_empty(attr_list))
2122 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2123 amdgpu_device_attr_remove(adev, entry->attr);
2124 list_del(&entry->entry);
2129 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2130 struct device_attribute *attr,
2133 struct amdgpu_device *adev = dev_get_drvdata(dev);
2134 int channel = to_sensor_dev_attr(attr)->index;
2135 int r, temp = 0, size = sizeof(temp);
2137 if (adev->in_gpu_reset)
2140 if (channel >= PP_TEMP_MAX)
2143 r = pm_runtime_get_sync(adev->ddev->dev);
2145 pm_runtime_put_autosuspend(adev->ddev->dev);
2150 case PP_TEMP_JUNCTION:
2151 /* get current junction temperature */
2152 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2153 (void *)&temp, &size);
2156 /* get current edge temperature */
2157 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2158 (void *)&temp, &size);
2161 /* get current memory temperature */
2162 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2163 (void *)&temp, &size);
2170 pm_runtime_mark_last_busy(adev->ddev->dev);
2171 pm_runtime_put_autosuspend(adev->ddev->dev);
2176 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2179 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2180 struct device_attribute *attr,
2183 struct amdgpu_device *adev = dev_get_drvdata(dev);
2184 int hyst = to_sensor_dev_attr(attr)->index;
2188 temp = adev->pm.dpm.thermal.min_temp;
2190 temp = adev->pm.dpm.thermal.max_temp;
2192 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2195 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2196 struct device_attribute *attr,
2199 struct amdgpu_device *adev = dev_get_drvdata(dev);
2200 int hyst = to_sensor_dev_attr(attr)->index;
2204 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2206 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2208 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2211 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2212 struct device_attribute *attr,
2215 struct amdgpu_device *adev = dev_get_drvdata(dev);
2216 int hyst = to_sensor_dev_attr(attr)->index;
2220 temp = adev->pm.dpm.thermal.min_mem_temp;
2222 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2224 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2227 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2228 struct device_attribute *attr,
2231 int channel = to_sensor_dev_attr(attr)->index;
2233 if (channel >= PP_TEMP_MAX)
2236 return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
2239 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2240 struct device_attribute *attr,
2243 struct amdgpu_device *adev = dev_get_drvdata(dev);
2244 int channel = to_sensor_dev_attr(attr)->index;
2247 if (channel >= PP_TEMP_MAX)
2251 case PP_TEMP_JUNCTION:
2252 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2255 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2258 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2262 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2265 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2266 struct device_attribute *attr,
2269 struct amdgpu_device *adev = dev_get_drvdata(dev);
2273 if (adev->in_gpu_reset)
2276 ret = pm_runtime_get_sync(adev->ddev->dev);
2278 pm_runtime_put_autosuspend(adev->ddev->dev);
2282 if (is_support_sw_smu(adev)) {
2283 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2285 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2286 pm_runtime_mark_last_busy(adev->ddev->dev);
2287 pm_runtime_put_autosuspend(adev->ddev->dev);
2291 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2294 pm_runtime_mark_last_busy(adev->ddev->dev);
2295 pm_runtime_put_autosuspend(adev->ddev->dev);
2297 return sprintf(buf, "%i\n", pwm_mode);
2300 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2301 struct device_attribute *attr,
2305 struct amdgpu_device *adev = dev_get_drvdata(dev);
2309 if (adev->in_gpu_reset)
2312 err = kstrtoint(buf, 10, &value);
2316 ret = pm_runtime_get_sync(adev->ddev->dev);
2318 pm_runtime_put_autosuspend(adev->ddev->dev);
2322 if (is_support_sw_smu(adev)) {
2323 smu_set_fan_control_mode(&adev->smu, value);
2325 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2326 pm_runtime_mark_last_busy(adev->ddev->dev);
2327 pm_runtime_put_autosuspend(adev->ddev->dev);
2331 amdgpu_dpm_set_fan_control_mode(adev, value);
2334 pm_runtime_mark_last_busy(adev->ddev->dev);
2335 pm_runtime_put_autosuspend(adev->ddev->dev);
2340 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2341 struct device_attribute *attr,
2344 return sprintf(buf, "%i\n", 0);
2347 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2348 struct device_attribute *attr,
2351 return sprintf(buf, "%i\n", 255);
2354 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2355 struct device_attribute *attr,
2356 const char *buf, size_t count)
2358 struct amdgpu_device *adev = dev_get_drvdata(dev);
2363 if (adev->in_gpu_reset)
2366 err = pm_runtime_get_sync(adev->ddev->dev);
2368 pm_runtime_put_autosuspend(adev->ddev->dev);
2372 if (is_support_sw_smu(adev))
2373 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2375 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2377 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2378 pr_info("manual fan speed control should be enabled first\n");
2379 pm_runtime_mark_last_busy(adev->ddev->dev);
2380 pm_runtime_put_autosuspend(adev->ddev->dev);
2384 err = kstrtou32(buf, 10, &value);
2386 pm_runtime_mark_last_busy(adev->ddev->dev);
2387 pm_runtime_put_autosuspend(adev->ddev->dev);
2391 value = (value * 100) / 255;
2393 if (is_support_sw_smu(adev))
2394 err = smu_set_fan_speed_percent(&adev->smu, value);
2395 else if (adev->powerplay.pp_funcs->set_fan_speed_percent)
2396 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
2400 pm_runtime_mark_last_busy(adev->ddev->dev);
2401 pm_runtime_put_autosuspend(adev->ddev->dev);
2409 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2410 struct device_attribute *attr,
2413 struct amdgpu_device *adev = dev_get_drvdata(dev);
2417 if (adev->in_gpu_reset)
2420 err = pm_runtime_get_sync(adev->ddev->dev);
2422 pm_runtime_put_autosuspend(adev->ddev->dev);
2426 if (is_support_sw_smu(adev))
2427 err = smu_get_fan_speed_percent(&adev->smu, &speed);
2428 else if (adev->powerplay.pp_funcs->get_fan_speed_percent)
2429 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
2433 pm_runtime_mark_last_busy(adev->ddev->dev);
2434 pm_runtime_put_autosuspend(adev->ddev->dev);
2439 speed = (speed * 255) / 100;
2441 return sprintf(buf, "%i\n", speed);
2444 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2445 struct device_attribute *attr,
2448 struct amdgpu_device *adev = dev_get_drvdata(dev);
2452 if (adev->in_gpu_reset)
2455 err = pm_runtime_get_sync(adev->ddev->dev);
2457 pm_runtime_put_autosuspend(adev->ddev->dev);
2461 if (is_support_sw_smu(adev))
2462 err = smu_get_fan_speed_rpm(&adev->smu, &speed);
2463 else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2464 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2468 pm_runtime_mark_last_busy(adev->ddev->dev);
2469 pm_runtime_put_autosuspend(adev->ddev->dev);
2474 return sprintf(buf, "%i\n", speed);
2477 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2478 struct device_attribute *attr,
2481 struct amdgpu_device *adev = dev_get_drvdata(dev);
2483 u32 size = sizeof(min_rpm);
2486 if (adev->in_gpu_reset)
2489 r = pm_runtime_get_sync(adev->ddev->dev);
2491 pm_runtime_put_autosuspend(adev->ddev->dev);
2495 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2496 (void *)&min_rpm, &size);
2498 pm_runtime_mark_last_busy(adev->ddev->dev);
2499 pm_runtime_put_autosuspend(adev->ddev->dev);
2504 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
2507 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2508 struct device_attribute *attr,
2511 struct amdgpu_device *adev = dev_get_drvdata(dev);
2513 u32 size = sizeof(max_rpm);
2516 if (adev->in_gpu_reset)
2519 r = pm_runtime_get_sync(adev->ddev->dev);
2521 pm_runtime_put_autosuspend(adev->ddev->dev);
2525 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2526 (void *)&max_rpm, &size);
2528 pm_runtime_mark_last_busy(adev->ddev->dev);
2529 pm_runtime_put_autosuspend(adev->ddev->dev);
2534 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
2537 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2538 struct device_attribute *attr,
2541 struct amdgpu_device *adev = dev_get_drvdata(dev);
2545 if (adev->in_gpu_reset)
2548 err = pm_runtime_get_sync(adev->ddev->dev);
2550 pm_runtime_put_autosuspend(adev->ddev->dev);
2554 if (is_support_sw_smu(adev))
2555 err = smu_get_fan_speed_rpm(&adev->smu, &rpm);
2556 else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2557 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2561 pm_runtime_mark_last_busy(adev->ddev->dev);
2562 pm_runtime_put_autosuspend(adev->ddev->dev);
2567 return sprintf(buf, "%i\n", rpm);
2570 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2571 struct device_attribute *attr,
2572 const char *buf, size_t count)
2574 struct amdgpu_device *adev = dev_get_drvdata(dev);
2579 if (adev->in_gpu_reset)
2582 err = pm_runtime_get_sync(adev->ddev->dev);
2584 pm_runtime_put_autosuspend(adev->ddev->dev);
2588 if (is_support_sw_smu(adev))
2589 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2591 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2593 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2594 pm_runtime_mark_last_busy(adev->ddev->dev);
2595 pm_runtime_put_autosuspend(adev->ddev->dev);
2599 err = kstrtou32(buf, 10, &value);
2601 pm_runtime_mark_last_busy(adev->ddev->dev);
2602 pm_runtime_put_autosuspend(adev->ddev->dev);
2606 if (is_support_sw_smu(adev))
2607 err = smu_set_fan_speed_rpm(&adev->smu, value);
2608 else if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2609 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2613 pm_runtime_mark_last_busy(adev->ddev->dev);
2614 pm_runtime_put_autosuspend(adev->ddev->dev);
2622 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2623 struct device_attribute *attr,
2626 struct amdgpu_device *adev = dev_get_drvdata(dev);
2630 if (adev->in_gpu_reset)
2633 ret = pm_runtime_get_sync(adev->ddev->dev);
2635 pm_runtime_put_autosuspend(adev->ddev->dev);
2639 if (is_support_sw_smu(adev)) {
2640 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2642 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2643 pm_runtime_mark_last_busy(adev->ddev->dev);
2644 pm_runtime_put_autosuspend(adev->ddev->dev);
2648 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2651 pm_runtime_mark_last_busy(adev->ddev->dev);
2652 pm_runtime_put_autosuspend(adev->ddev->dev);
2654 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2657 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2658 struct device_attribute *attr,
2662 struct amdgpu_device *adev = dev_get_drvdata(dev);
2667 if (adev->in_gpu_reset)
2670 err = kstrtoint(buf, 10, &value);
2675 pwm_mode = AMD_FAN_CTRL_AUTO;
2676 else if (value == 1)
2677 pwm_mode = AMD_FAN_CTRL_MANUAL;
2681 err = pm_runtime_get_sync(adev->ddev->dev);
2683 pm_runtime_put_autosuspend(adev->ddev->dev);
2687 if (is_support_sw_smu(adev)) {
2688 smu_set_fan_control_mode(&adev->smu, pwm_mode);
2690 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2691 pm_runtime_mark_last_busy(adev->ddev->dev);
2692 pm_runtime_put_autosuspend(adev->ddev->dev);
2695 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2698 pm_runtime_mark_last_busy(adev->ddev->dev);
2699 pm_runtime_put_autosuspend(adev->ddev->dev);
2704 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2705 struct device_attribute *attr,
2708 struct amdgpu_device *adev = dev_get_drvdata(dev);
2710 int r, size = sizeof(vddgfx);
2712 if (adev->in_gpu_reset)
2715 r = pm_runtime_get_sync(adev->ddev->dev);
2717 pm_runtime_put_autosuspend(adev->ddev->dev);
2721 /* get the voltage */
2722 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2723 (void *)&vddgfx, &size);
2725 pm_runtime_mark_last_busy(adev->ddev->dev);
2726 pm_runtime_put_autosuspend(adev->ddev->dev);
2731 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
2734 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2735 struct device_attribute *attr,
2738 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
2741 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2742 struct device_attribute *attr,
2745 struct amdgpu_device *adev = dev_get_drvdata(dev);
2747 int r, size = sizeof(vddnb);
2749 if (adev->in_gpu_reset)
2752 /* only APUs have vddnb */
2753 if (!(adev->flags & AMD_IS_APU))
2756 r = pm_runtime_get_sync(adev->ddev->dev);
2758 pm_runtime_put_autosuspend(adev->ddev->dev);
2762 /* get the voltage */
2763 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2764 (void *)&vddnb, &size);
2766 pm_runtime_mark_last_busy(adev->ddev->dev);
2767 pm_runtime_put_autosuspend(adev->ddev->dev);
2772 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
2775 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2776 struct device_attribute *attr,
2779 return snprintf(buf, PAGE_SIZE, "vddnb\n");
2782 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2783 struct device_attribute *attr,
2786 struct amdgpu_device *adev = dev_get_drvdata(dev);
2788 int r, size = sizeof(u32);
2791 if (adev->in_gpu_reset)
2794 r = pm_runtime_get_sync(adev->ddev->dev);
2796 pm_runtime_put_autosuspend(adev->ddev->dev);
2800 /* get the voltage */
2801 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2802 (void *)&query, &size);
2804 pm_runtime_mark_last_busy(adev->ddev->dev);
2805 pm_runtime_put_autosuspend(adev->ddev->dev);
2810 /* convert to microwatts */
2811 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2813 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
2816 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2817 struct device_attribute *attr,
2820 return sprintf(buf, "%i\n", 0);
2823 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2824 struct device_attribute *attr,
2827 struct amdgpu_device *adev = dev_get_drvdata(dev);
2832 if (adev->in_gpu_reset)
2835 r = pm_runtime_get_sync(adev->ddev->dev);
2837 pm_runtime_put_autosuspend(adev->ddev->dev);
2841 if (is_support_sw_smu(adev)) {
2842 smu_get_power_limit(&adev->smu, &limit, true);
2843 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2844 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2845 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2846 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2848 size = snprintf(buf, PAGE_SIZE, "\n");
2851 pm_runtime_mark_last_busy(adev->ddev->dev);
2852 pm_runtime_put_autosuspend(adev->ddev->dev);
2857 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2858 struct device_attribute *attr,
2861 struct amdgpu_device *adev = dev_get_drvdata(dev);
2866 if (adev->in_gpu_reset)
2869 r = pm_runtime_get_sync(adev->ddev->dev);
2871 pm_runtime_put_autosuspend(adev->ddev->dev);
2875 if (is_support_sw_smu(adev)) {
2876 smu_get_power_limit(&adev->smu, &limit, false);
2877 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2878 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2879 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2880 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2882 size = snprintf(buf, PAGE_SIZE, "\n");
2885 pm_runtime_mark_last_busy(adev->ddev->dev);
2886 pm_runtime_put_autosuspend(adev->ddev->dev);
2892 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2893 struct device_attribute *attr,
2897 struct amdgpu_device *adev = dev_get_drvdata(dev);
2901 if (adev->in_gpu_reset)
2904 if (amdgpu_sriov_vf(adev))
2907 err = kstrtou32(buf, 10, &value);
2911 value = value / 1000000; /* convert to Watt */
2914 err = pm_runtime_get_sync(adev->ddev->dev);
2916 pm_runtime_put_autosuspend(adev->ddev->dev);
2920 if (is_support_sw_smu(adev))
2921 err = smu_set_power_limit(&adev->smu, value);
2922 else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit)
2923 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2927 pm_runtime_mark_last_busy(adev->ddev->dev);
2928 pm_runtime_put_autosuspend(adev->ddev->dev);
2936 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2937 struct device_attribute *attr,
2940 struct amdgpu_device *adev = dev_get_drvdata(dev);
2942 int r, size = sizeof(sclk);
2944 if (adev->in_gpu_reset)
2947 r = pm_runtime_get_sync(adev->ddev->dev);
2949 pm_runtime_put_autosuspend(adev->ddev->dev);
2954 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2955 (void *)&sclk, &size);
2957 pm_runtime_mark_last_busy(adev->ddev->dev);
2958 pm_runtime_put_autosuspend(adev->ddev->dev);
2963 return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000);
2966 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2967 struct device_attribute *attr,
2970 return snprintf(buf, PAGE_SIZE, "sclk\n");
2973 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2974 struct device_attribute *attr,
2977 struct amdgpu_device *adev = dev_get_drvdata(dev);
2979 int r, size = sizeof(mclk);
2981 if (adev->in_gpu_reset)
2984 r = pm_runtime_get_sync(adev->ddev->dev);
2986 pm_runtime_put_autosuspend(adev->ddev->dev);
2991 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2992 (void *)&mclk, &size);
2994 pm_runtime_mark_last_busy(adev->ddev->dev);
2995 pm_runtime_put_autosuspend(adev->ddev->dev);
3000 return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000);
3003 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3004 struct device_attribute *attr,
3007 return snprintf(buf, PAGE_SIZE, "mclk\n");
3013 * The amdgpu driver exposes the following sensor interfaces:
3015 * - GPU temperature (via the on-die sensor)
3019 * - Northbridge voltage (APUs only)
3025 * - GPU gfx/compute engine clock
3027 * - GPU memory clock (dGPU only)
3029 * hwmon interfaces for GPU temperature:
3031 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3032 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
3034 * - temp[1-3]_label: temperature channel label
3035 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3037 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3038 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3040 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3041 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3043 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3044 * - these are supported on SOC15 dGPUs only
3046 * hwmon interfaces for GPU voltage:
3048 * - in0_input: the voltage on the GPU in millivolts
3050 * - in1_input: the voltage on the Northbridge in millivolts
3052 * hwmon interfaces for GPU power:
3054 * - power1_average: average power used by the GPU in microWatts
3056 * - power1_cap_min: minimum cap supported in microWatts
3058 * - power1_cap_max: maximum cap supported in microWatts
3060 * - power1_cap: selected power cap in microWatts
3062 * hwmon interfaces for GPU fan:
3064 * - pwm1: pulse width modulation fan level (0-255)
3066 * - 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)
3068 * - pwm1_min: pulse width modulation fan control minimum level (0)
3070 * - pwm1_max: pulse width modulation fan control maximum level (255)
3072 * - fan1_min: an minimum value Unit: revolution/min (RPM)
3074 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
3076 * - fan1_input: fan speed in RPM
3078 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3080 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3082 * hwmon interfaces for GPU clocks:
3084 * - freq1_input: the gfx/compute clock in hertz
3086 * - freq2_input: the memory clock in hertz
3088 * You can use hwmon tools like sensors to view this information on your system.
3092 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3093 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3094 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3095 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3096 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3097 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3098 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3099 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3100 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3101 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3102 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3103 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3104 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3105 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3106 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3107 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3108 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3109 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3110 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3111 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3112 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3113 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3114 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3115 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3116 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3117 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3118 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3119 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3120 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3121 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3122 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3123 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3124 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3125 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3126 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3127 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3129 static struct attribute *hwmon_attributes[] = {
3130 &sensor_dev_attr_temp1_input.dev_attr.attr,
3131 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3132 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3133 &sensor_dev_attr_temp2_input.dev_attr.attr,
3134 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3135 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3136 &sensor_dev_attr_temp3_input.dev_attr.attr,
3137 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3138 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3139 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3140 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3141 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
3142 &sensor_dev_attr_temp1_label.dev_attr.attr,
3143 &sensor_dev_attr_temp2_label.dev_attr.attr,
3144 &sensor_dev_attr_temp3_label.dev_attr.attr,
3145 &sensor_dev_attr_pwm1.dev_attr.attr,
3146 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3147 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3148 &sensor_dev_attr_pwm1_max.dev_attr.attr,
3149 &sensor_dev_attr_fan1_input.dev_attr.attr,
3150 &sensor_dev_attr_fan1_min.dev_attr.attr,
3151 &sensor_dev_attr_fan1_max.dev_attr.attr,
3152 &sensor_dev_attr_fan1_target.dev_attr.attr,
3153 &sensor_dev_attr_fan1_enable.dev_attr.attr,
3154 &sensor_dev_attr_in0_input.dev_attr.attr,
3155 &sensor_dev_attr_in0_label.dev_attr.attr,
3156 &sensor_dev_attr_in1_input.dev_attr.attr,
3157 &sensor_dev_attr_in1_label.dev_attr.attr,
3158 &sensor_dev_attr_power1_average.dev_attr.attr,
3159 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3160 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3161 &sensor_dev_attr_power1_cap.dev_attr.attr,
3162 &sensor_dev_attr_freq1_input.dev_attr.attr,
3163 &sensor_dev_attr_freq1_label.dev_attr.attr,
3164 &sensor_dev_attr_freq2_input.dev_attr.attr,
3165 &sensor_dev_attr_freq2_label.dev_attr.attr,
3169 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3170 struct attribute *attr, int index)
3172 struct device *dev = kobj_to_dev(kobj);
3173 struct amdgpu_device *adev = dev_get_drvdata(dev);
3174 umode_t effective_mode = attr->mode;
3176 /* under multi-vf mode, the hwmon attributes are all not supported */
3177 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3180 /* there is no fan under pp one vf mode */
3181 if (amdgpu_sriov_is_pp_one_vf(adev) &&
3182 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3183 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3184 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3185 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3186 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3187 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3188 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3189 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3190 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3193 /* Skip fan attributes if fan is not present */
3194 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3195 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3196 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3197 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3198 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3199 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3200 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3201 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3202 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3205 /* Skip fan attributes on APU */
3206 if ((adev->flags & AMD_IS_APU) &&
3207 (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))
3218 /* Skip limit attributes if DPM is not enabled */
3219 if (!adev->pm.dpm_enabled &&
3220 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3221 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3222 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3223 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3224 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3225 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3226 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3227 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3228 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3229 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3230 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3233 if (!is_support_sw_smu(adev)) {
3234 /* mask fan attributes if we have no bindings for this asic to expose */
3235 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
3236 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3237 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3238 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3239 effective_mode &= ~S_IRUGO;
3241 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3242 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3243 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3244 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3245 effective_mode &= ~S_IWUSR;
3248 if (((adev->flags & AMD_IS_APU) ||
3249 adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3250 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */
3251 (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
3252 attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3253 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
3254 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
3257 if (!is_support_sw_smu(adev)) {
3258 /* hide max/min values if we can't both query and manage the fan */
3259 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3260 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
3261 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3262 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3263 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3264 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3267 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3268 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3269 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3270 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3274 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3275 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */
3276 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3277 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3280 /* only APUs have vddnb */
3281 if (!(adev->flags & AMD_IS_APU) &&
3282 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3283 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3286 /* no mclk on APUs */
3287 if ((adev->flags & AMD_IS_APU) &&
3288 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3289 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3292 /* only SOC15 dGPUs support hotspot and mem temperatures */
3293 if (((adev->flags & AMD_IS_APU) ||
3294 adev->asic_type < CHIP_VEGA10) &&
3295 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3296 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3297 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3298 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3299 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3300 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3301 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3302 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3303 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3304 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3305 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3308 return effective_mode;
3311 static const struct attribute_group hwmon_attrgroup = {
3312 .attrs = hwmon_attributes,
3313 .is_visible = hwmon_attributes_visible,
3316 static const struct attribute_group *hwmon_groups[] = {
3321 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
3323 struct amdgpu_device *adev =
3324 container_of(work, struct amdgpu_device,
3325 pm.dpm.thermal.work);
3326 /* switch to the thermal state */
3327 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
3328 int temp, size = sizeof(temp);
3330 if (!adev->pm.dpm_enabled)
3333 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
3334 (void *)&temp, &size)) {
3335 if (temp < adev->pm.dpm.thermal.min_temp)
3336 /* switch back the user state */
3337 dpm_state = adev->pm.dpm.user_state;
3339 if (adev->pm.dpm.thermal.high_to_low)
3340 /* switch back the user state */
3341 dpm_state = adev->pm.dpm.user_state;
3343 mutex_lock(&adev->pm.mutex);
3344 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
3345 adev->pm.dpm.thermal_active = true;
3347 adev->pm.dpm.thermal_active = false;
3348 adev->pm.dpm.state = dpm_state;
3349 mutex_unlock(&adev->pm.mutex);
3351 amdgpu_pm_compute_clocks(adev);
3354 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
3355 enum amd_pm_state_type dpm_state)
3358 struct amdgpu_ps *ps;
3360 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
3363 /* check if the vblank period is too short to adjust the mclk */
3364 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
3365 if (amdgpu_dpm_vblank_too_short(adev))
3366 single_display = false;
3369 /* certain older asics have a separare 3D performance state,
3370 * so try that first if the user selected performance
3372 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
3373 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
3374 /* balanced states don't exist at the moment */
3375 if (dpm_state == POWER_STATE_TYPE_BALANCED)
3376 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3379 /* Pick the best power state based on current conditions */
3380 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
3381 ps = &adev->pm.dpm.ps[i];
3382 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
3383 switch (dpm_state) {
3385 case POWER_STATE_TYPE_BATTERY:
3386 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
3387 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3394 case POWER_STATE_TYPE_BALANCED:
3395 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
3396 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3403 case POWER_STATE_TYPE_PERFORMANCE:
3404 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
3405 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3412 /* internal states */
3413 case POWER_STATE_TYPE_INTERNAL_UVD:
3414 if (adev->pm.dpm.uvd_ps)
3415 return adev->pm.dpm.uvd_ps;
3418 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
3419 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
3422 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
3423 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
3426 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
3427 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
3430 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
3431 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
3434 case POWER_STATE_TYPE_INTERNAL_BOOT:
3435 return adev->pm.dpm.boot_ps;
3436 case POWER_STATE_TYPE_INTERNAL_THERMAL:
3437 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
3440 case POWER_STATE_TYPE_INTERNAL_ACPI:
3441 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
3444 case POWER_STATE_TYPE_INTERNAL_ULV:
3445 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
3448 case POWER_STATE_TYPE_INTERNAL_3DPERF:
3449 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
3456 /* use a fallback state if we didn't match */
3457 switch (dpm_state) {
3458 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
3459 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
3460 goto restart_search;
3461 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
3462 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
3463 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
3464 if (adev->pm.dpm.uvd_ps) {
3465 return adev->pm.dpm.uvd_ps;
3467 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3468 goto restart_search;
3470 case POWER_STATE_TYPE_INTERNAL_THERMAL:
3471 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
3472 goto restart_search;
3473 case POWER_STATE_TYPE_INTERNAL_ACPI:
3474 dpm_state = POWER_STATE_TYPE_BATTERY;
3475 goto restart_search;
3476 case POWER_STATE_TYPE_BATTERY:
3477 case POWER_STATE_TYPE_BALANCED:
3478 case POWER_STATE_TYPE_INTERNAL_3DPERF:
3479 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3480 goto restart_search;
3488 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
3490 struct amdgpu_ps *ps;
3491 enum amd_pm_state_type dpm_state;
3495 /* if dpm init failed */
3496 if (!adev->pm.dpm_enabled)
3499 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
3500 /* add other state override checks here */
3501 if ((!adev->pm.dpm.thermal_active) &&
3502 (!adev->pm.dpm.uvd_active))
3503 adev->pm.dpm.state = adev->pm.dpm.user_state;
3505 dpm_state = adev->pm.dpm.state;
3507 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
3509 adev->pm.dpm.requested_ps = ps;
3513 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
3514 printk("switching from power state:\n");
3515 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
3516 printk("switching to power state:\n");
3517 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
3520 /* update whether vce is active */
3521 ps->vce_active = adev->pm.dpm.vce_active;
3522 if (adev->powerplay.pp_funcs->display_configuration_changed)
3523 amdgpu_dpm_display_configuration_changed(adev);
3525 ret = amdgpu_dpm_pre_set_power_state(adev);
3529 if (adev->powerplay.pp_funcs->check_state_equal) {
3530 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
3537 amdgpu_dpm_set_power_state(adev);
3538 amdgpu_dpm_post_set_power_state(adev);
3540 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
3541 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
3543 if (adev->powerplay.pp_funcs->force_performance_level) {
3544 if (adev->pm.dpm.thermal_active) {
3545 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
3546 /* force low perf level for thermal */
3547 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
3548 /* save the user's level */
3549 adev->pm.dpm.forced_level = level;
3551 /* otherwise, user selected level */
3552 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
3557 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
3561 if (adev->family == AMDGPU_FAMILY_SI) {
3563 mutex_lock(&adev->pm.mutex);
3564 adev->pm.dpm.uvd_active = true;
3565 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
3566 mutex_unlock(&adev->pm.mutex);
3568 mutex_lock(&adev->pm.mutex);
3569 adev->pm.dpm.uvd_active = false;
3570 mutex_unlock(&adev->pm.mutex);
3573 amdgpu_pm_compute_clocks(adev);
3575 ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
3577 DRM_ERROR("Dpm %s uvd failed, ret = %d. \n",
3578 enable ? "enable" : "disable", ret);
3580 /* enable/disable Low Memory PState for UVD (4k videos) */
3581 if (adev->asic_type == CHIP_STONEY &&
3582 adev->uvd.decode_image_width >= WIDTH_4K) {
3583 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
3585 if (hwmgr && hwmgr->hwmgr_func &&
3586 hwmgr->hwmgr_func->update_nbdpm_pstate)
3587 hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
3594 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
3598 if (adev->family == AMDGPU_FAMILY_SI) {
3600 mutex_lock(&adev->pm.mutex);
3601 adev->pm.dpm.vce_active = true;
3602 /* XXX select vce level based on ring/task */
3603 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
3604 mutex_unlock(&adev->pm.mutex);
3606 mutex_lock(&adev->pm.mutex);
3607 adev->pm.dpm.vce_active = false;
3608 mutex_unlock(&adev->pm.mutex);
3611 amdgpu_pm_compute_clocks(adev);
3613 ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
3615 DRM_ERROR("Dpm %s vce failed, ret = %d. \n",
3616 enable ? "enable" : "disable", ret);
3620 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
3624 if (adev->powerplay.pp_funcs->print_power_state == NULL)
3627 for (i = 0; i < adev->pm.dpm.num_ps; i++)
3628 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
3632 void amdgpu_dpm_enable_jpeg(struct amdgpu_device *adev, bool enable)
3636 ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_JPEG, !enable);
3638 DRM_ERROR("Dpm %s jpeg failed, ret = %d. \n",
3639 enable ? "enable" : "disable", ret);
3642 int amdgpu_pm_load_smu_firmware(struct amdgpu_device *adev, uint32_t *smu_version)
3646 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) {
3647 r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle);
3649 pr_err("smu firmware loading failed\n");
3652 *smu_version = adev->pm.fw_version;
3657 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3662 if (adev->pm.sysfs_initialized)
3665 if (adev->pm.dpm_enabled == 0)
3668 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3670 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3673 if (IS_ERR(adev->pm.int_hwmon_dev)) {
3674 ret = PTR_ERR(adev->pm.int_hwmon_dev);
3676 "Unable to register hwmon device: %d\n", ret);
3680 switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3681 case SRIOV_VF_MODE_ONE_VF:
3682 mask = ATTR_FLAG_ONEVF;
3684 case SRIOV_VF_MODE_MULTI_VF:
3687 case SRIOV_VF_MODE_BARE_METAL:
3689 mask = ATTR_FLAG_MASK_ALL;
3693 ret = amdgpu_device_attr_create_groups(adev,
3694 amdgpu_device_attrs,
3695 ARRAY_SIZE(amdgpu_device_attrs),
3697 &adev->pm.pm_attr_list);
3701 adev->pm.sysfs_initialized = true;
3706 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3708 if (adev->pm.dpm_enabled == 0)
3711 if (adev->pm.int_hwmon_dev)
3712 hwmon_device_unregister(adev->pm.int_hwmon_dev);
3714 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3717 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
3721 if (!adev->pm.dpm_enabled)
3724 if (adev->mode_info.num_crtc)
3725 amdgpu_display_bandwidth_update(adev);
3727 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3728 struct amdgpu_ring *ring = adev->rings[i];
3729 if (ring && ring->sched.ready)
3730 amdgpu_fence_wait_empty(ring);
3733 if (is_support_sw_smu(adev)) {
3734 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
3735 smu_handle_task(&adev->smu,
3737 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE,
3740 if (adev->powerplay.pp_funcs->dispatch_tasks) {
3741 if (!amdgpu_device_has_dc_support(adev)) {
3742 mutex_lock(&adev->pm.mutex);
3743 amdgpu_dpm_get_active_displays(adev);
3744 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
3745 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
3746 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
3747 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
3748 if (adev->pm.pm_display_cfg.vrefresh > 120)
3749 adev->pm.pm_display_cfg.min_vblank_time = 0;
3750 if (adev->powerplay.pp_funcs->display_configuration_change)
3751 adev->powerplay.pp_funcs->display_configuration_change(
3752 adev->powerplay.pp_handle,
3753 &adev->pm.pm_display_cfg);
3754 mutex_unlock(&adev->pm.mutex);
3756 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
3758 mutex_lock(&adev->pm.mutex);
3759 amdgpu_dpm_get_active_displays(adev);
3760 amdgpu_dpm_change_power_state_locked(adev);
3761 mutex_unlock(&adev->pm.mutex);
3769 #if defined(CONFIG_DEBUG_FS)
3771 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3779 size = sizeof(value);
3780 seq_printf(m, "GFX Clocks and Power:\n");
3781 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3782 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3783 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3784 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3785 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3786 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3787 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3788 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3789 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3790 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3791 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3792 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3793 size = sizeof(uint32_t);
3794 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3795 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3796 size = sizeof(value);
3797 seq_printf(m, "\n");
3800 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3801 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3804 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3805 seq_printf(m, "GPU Load: %u %%\n", value);
3807 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3808 seq_printf(m, "MEM Load: %u %%\n", value);
3810 seq_printf(m, "\n");
3812 /* SMC feature mask */
3813 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3814 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3816 if (adev->asic_type > CHIP_VEGA20) {
3818 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3820 seq_printf(m, "VCN: Disabled\n");
3822 seq_printf(m, "VCN: Enabled\n");
3823 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3824 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3825 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3826 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3829 seq_printf(m, "\n");
3832 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3834 seq_printf(m, "UVD: Disabled\n");
3836 seq_printf(m, "UVD: Enabled\n");
3837 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3838 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3839 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3840 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3843 seq_printf(m, "\n");
3846 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3848 seq_printf(m, "VCE: Disabled\n");
3850 seq_printf(m, "VCE: Enabled\n");
3851 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3852 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3860 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3864 for (i = 0; clocks[i].flag; i++)
3865 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3866 (flags & clocks[i].flag) ? "On" : "Off");
3869 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3871 struct drm_info_node *node = (struct drm_info_node *) m->private;
3872 struct drm_device *dev = node->minor->dev;
3873 struct amdgpu_device *adev = dev->dev_private;
3877 if (adev->in_gpu_reset)
3880 r = pm_runtime_get_sync(dev->dev);
3882 pm_runtime_put_autosuspend(dev->dev);
3886 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3887 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3888 amdgpu_parse_cg_state(m, flags);
3889 seq_printf(m, "\n");
3891 if (!adev->pm.dpm_enabled) {
3892 seq_printf(m, "dpm not enabled\n");
3893 pm_runtime_mark_last_busy(dev->dev);
3894 pm_runtime_put_autosuspend(dev->dev);
3898 if (!is_support_sw_smu(adev) &&
3899 adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3900 mutex_lock(&adev->pm.mutex);
3901 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3902 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3904 seq_printf(m, "Debugfs support not implemented for this asic\n");
3905 mutex_unlock(&adev->pm.mutex);
3908 r = amdgpu_debugfs_pm_info_pp(m, adev);
3911 pm_runtime_mark_last_busy(dev->dev);
3912 pm_runtime_put_autosuspend(dev->dev);
3917 static const struct drm_info_list amdgpu_pm_info_list[] = {
3918 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3922 int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3924 #if defined(CONFIG_DEBUG_FS)
3925 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));