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));
799 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
800 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
805 while (isspace(*tmp_str))
809 ret = pm_runtime_get_sync(ddev->dev);
811 pm_runtime_put_autosuspend(ddev->dev);
815 if (is_support_sw_smu(adev)) {
816 ret = smu_od_edit_dpm_table(&adev->smu, type,
817 parameter, parameter_size);
820 pm_runtime_mark_last_busy(ddev->dev);
821 pm_runtime_put_autosuspend(ddev->dev);
825 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
826 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
827 parameter, parameter_size);
829 pm_runtime_mark_last_busy(ddev->dev);
830 pm_runtime_put_autosuspend(ddev->dev);
835 if (type == PP_OD_COMMIT_DPM_TABLE) {
836 if (adev->powerplay.pp_funcs->dispatch_tasks) {
837 amdgpu_dpm_dispatch_task(adev,
838 AMD_PP_TASK_READJUST_POWER_STATE,
840 pm_runtime_mark_last_busy(ddev->dev);
841 pm_runtime_put_autosuspend(ddev->dev);
844 pm_runtime_mark_last_busy(ddev->dev);
845 pm_runtime_put_autosuspend(ddev->dev);
850 pm_runtime_mark_last_busy(ddev->dev);
851 pm_runtime_put_autosuspend(ddev->dev);
856 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
857 struct device_attribute *attr,
860 struct drm_device *ddev = dev_get_drvdata(dev);
861 struct amdgpu_device *adev = ddev->dev_private;
865 if (adev->in_gpu_reset)
868 ret = pm_runtime_get_sync(ddev->dev);
870 pm_runtime_put_autosuspend(ddev->dev);
874 if (is_support_sw_smu(adev)) {
875 size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf);
876 size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size);
877 size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size);
878 size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size);
879 } else if (adev->powerplay.pp_funcs->print_clock_levels) {
880 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
881 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
882 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
883 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
885 size = snprintf(buf, PAGE_SIZE, "\n");
887 pm_runtime_mark_last_busy(ddev->dev);
888 pm_runtime_put_autosuspend(ddev->dev);
896 * The amdgpu driver provides a sysfs API for adjusting what powerplay
897 * features to be enabled. The file pp_features is used for this. And
898 * this is only available for Vega10 and later dGPUs.
900 * Reading back the file will show you the followings:
901 * - Current ppfeature masks
902 * - List of the all supported powerplay features with their naming,
903 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
905 * To manually enable or disable a specific feature, just set or clear
906 * the corresponding bit from original ppfeature masks and input the
907 * new ppfeature masks.
909 static ssize_t amdgpu_set_pp_features(struct device *dev,
910 struct device_attribute *attr,
914 struct drm_device *ddev = dev_get_drvdata(dev);
915 struct amdgpu_device *adev = ddev->dev_private;
916 uint64_t featuremask;
919 if (adev->in_gpu_reset)
922 ret = kstrtou64(buf, 0, &featuremask);
926 pr_debug("featuremask = 0x%llx\n", featuremask);
928 ret = pm_runtime_get_sync(ddev->dev);
930 pm_runtime_put_autosuspend(ddev->dev);
934 if (is_support_sw_smu(adev)) {
935 ret = smu_sys_set_pp_feature_mask(&adev->smu, featuremask);
937 pm_runtime_mark_last_busy(ddev->dev);
938 pm_runtime_put_autosuspend(ddev->dev);
941 } else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
942 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
944 pm_runtime_mark_last_busy(ddev->dev);
945 pm_runtime_put_autosuspend(ddev->dev);
949 pm_runtime_mark_last_busy(ddev->dev);
950 pm_runtime_put_autosuspend(ddev->dev);
955 static ssize_t amdgpu_get_pp_features(struct device *dev,
956 struct device_attribute *attr,
959 struct drm_device *ddev = dev_get_drvdata(dev);
960 struct amdgpu_device *adev = ddev->dev_private;
964 if (adev->in_gpu_reset)
967 ret = pm_runtime_get_sync(ddev->dev);
969 pm_runtime_put_autosuspend(ddev->dev);
973 if (is_support_sw_smu(adev))
974 size = smu_sys_get_pp_feature_mask(&adev->smu, buf);
975 else if (adev->powerplay.pp_funcs->get_ppfeature_status)
976 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
978 size = snprintf(buf, PAGE_SIZE, "\n");
980 pm_runtime_mark_last_busy(ddev->dev);
981 pm_runtime_put_autosuspend(ddev->dev);
987 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
989 * The amdgpu driver provides a sysfs API for adjusting what power levels
990 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
991 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
994 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
995 * Vega10 and later ASICs.
996 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
998 * Reading back the files will show you the available power levels within
999 * the power state and the clock information for those levels.
1001 * To manually adjust these states, first select manual using
1002 * power_dpm_force_performance_level.
1003 * Secondly, enter a new value for each level by inputing a string that
1004 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1007 * .. code-block:: bash
1009 * echo "4 5 6" > pp_dpm_sclk
1011 * will enable sclk levels 4, 5, and 6.
1013 * NOTE: change to the dcefclk max dpm level is not supported now
1016 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1017 struct device_attribute *attr,
1020 struct drm_device *ddev = dev_get_drvdata(dev);
1021 struct amdgpu_device *adev = ddev->dev_private;
1025 if (adev->in_gpu_reset)
1028 ret = pm_runtime_get_sync(ddev->dev);
1030 pm_runtime_put_autosuspend(ddev->dev);
1034 if (is_support_sw_smu(adev))
1035 size = smu_print_clk_levels(&adev->smu, SMU_SCLK, buf);
1036 else if (adev->powerplay.pp_funcs->print_clock_levels)
1037 size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
1039 size = snprintf(buf, PAGE_SIZE, "\n");
1041 pm_runtime_mark_last_busy(ddev->dev);
1042 pm_runtime_put_autosuspend(ddev->dev);
1048 * Worst case: 32 bits individually specified, in octal at 12 characters
1049 * per line (+1 for \n).
1051 #define AMDGPU_MASK_BUF_MAX (32 * 13)
1053 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1057 char *sub_str = NULL;
1059 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1060 const char delimiter[3] = {' ', '\n', '\0'};
1065 bytes = min(count, sizeof(buf_cpy) - 1);
1066 memcpy(buf_cpy, buf, bytes);
1067 buf_cpy[bytes] = '\0';
1069 while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1070 if (strlen(sub_str)) {
1071 ret = kstrtol(sub_str, 0, &level);
1074 *mask |= 1 << level;
1082 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1083 struct device_attribute *attr,
1087 struct drm_device *ddev = dev_get_drvdata(dev);
1088 struct amdgpu_device *adev = ddev->dev_private;
1092 if (adev->in_gpu_reset)
1095 ret = amdgpu_read_mask(buf, count, &mask);
1099 ret = pm_runtime_get_sync(ddev->dev);
1101 pm_runtime_put_autosuspend(ddev->dev);
1105 if (is_support_sw_smu(adev))
1106 ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask);
1107 else if (adev->powerplay.pp_funcs->force_clock_level)
1108 ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
1110 pm_runtime_mark_last_busy(ddev->dev);
1111 pm_runtime_put_autosuspend(ddev->dev);
1119 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1120 struct device_attribute *attr,
1123 struct drm_device *ddev = dev_get_drvdata(dev);
1124 struct amdgpu_device *adev = ddev->dev_private;
1128 if (adev->in_gpu_reset)
1131 ret = pm_runtime_get_sync(ddev->dev);
1133 pm_runtime_put_autosuspend(ddev->dev);
1137 if (is_support_sw_smu(adev))
1138 size = smu_print_clk_levels(&adev->smu, SMU_MCLK, buf);
1139 else if (adev->powerplay.pp_funcs->print_clock_levels)
1140 size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
1142 size = snprintf(buf, PAGE_SIZE, "\n");
1144 pm_runtime_mark_last_busy(ddev->dev);
1145 pm_runtime_put_autosuspend(ddev->dev);
1150 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1151 struct device_attribute *attr,
1155 struct drm_device *ddev = dev_get_drvdata(dev);
1156 struct amdgpu_device *adev = ddev->dev_private;
1160 if (adev->in_gpu_reset)
1163 ret = amdgpu_read_mask(buf, count, &mask);
1167 ret = pm_runtime_get_sync(ddev->dev);
1169 pm_runtime_put_autosuspend(ddev->dev);
1173 if (is_support_sw_smu(adev))
1174 ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask);
1175 else if (adev->powerplay.pp_funcs->force_clock_level)
1176 ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
1178 pm_runtime_mark_last_busy(ddev->dev);
1179 pm_runtime_put_autosuspend(ddev->dev);
1187 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1188 struct device_attribute *attr,
1191 struct drm_device *ddev = dev_get_drvdata(dev);
1192 struct amdgpu_device *adev = ddev->dev_private;
1196 if (adev->in_gpu_reset)
1199 ret = pm_runtime_get_sync(ddev->dev);
1201 pm_runtime_put_autosuspend(ddev->dev);
1205 if (is_support_sw_smu(adev))
1206 size = smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf);
1207 else if (adev->powerplay.pp_funcs->print_clock_levels)
1208 size = amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
1210 size = snprintf(buf, PAGE_SIZE, "\n");
1212 pm_runtime_mark_last_busy(ddev->dev);
1213 pm_runtime_put_autosuspend(ddev->dev);
1218 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1219 struct device_attribute *attr,
1223 struct drm_device *ddev = dev_get_drvdata(dev);
1224 struct amdgpu_device *adev = ddev->dev_private;
1228 if (adev->in_gpu_reset)
1231 ret = amdgpu_read_mask(buf, count, &mask);
1235 ret = pm_runtime_get_sync(ddev->dev);
1237 pm_runtime_put_autosuspend(ddev->dev);
1241 if (is_support_sw_smu(adev))
1242 ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask);
1243 else if (adev->powerplay.pp_funcs->force_clock_level)
1244 ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
1248 pm_runtime_mark_last_busy(ddev->dev);
1249 pm_runtime_put_autosuspend(ddev->dev);
1257 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1258 struct device_attribute *attr,
1261 struct drm_device *ddev = dev_get_drvdata(dev);
1262 struct amdgpu_device *adev = ddev->dev_private;
1266 if (adev->in_gpu_reset)
1269 ret = pm_runtime_get_sync(ddev->dev);
1271 pm_runtime_put_autosuspend(ddev->dev);
1275 if (is_support_sw_smu(adev))
1276 size = smu_print_clk_levels(&adev->smu, SMU_FCLK, buf);
1277 else if (adev->powerplay.pp_funcs->print_clock_levels)
1278 size = amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
1280 size = snprintf(buf, PAGE_SIZE, "\n");
1282 pm_runtime_mark_last_busy(ddev->dev);
1283 pm_runtime_put_autosuspend(ddev->dev);
1288 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1289 struct device_attribute *attr,
1293 struct drm_device *ddev = dev_get_drvdata(dev);
1294 struct amdgpu_device *adev = ddev->dev_private;
1298 if (adev->in_gpu_reset)
1301 ret = amdgpu_read_mask(buf, count, &mask);
1305 ret = pm_runtime_get_sync(ddev->dev);
1307 pm_runtime_put_autosuspend(ddev->dev);
1311 if (is_support_sw_smu(adev))
1312 ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask);
1313 else if (adev->powerplay.pp_funcs->force_clock_level)
1314 ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1318 pm_runtime_mark_last_busy(ddev->dev);
1319 pm_runtime_put_autosuspend(ddev->dev);
1327 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1328 struct device_attribute *attr,
1331 struct drm_device *ddev = dev_get_drvdata(dev);
1332 struct amdgpu_device *adev = ddev->dev_private;
1336 if (adev->in_gpu_reset)
1339 ret = pm_runtime_get_sync(ddev->dev);
1341 pm_runtime_put_autosuspend(ddev->dev);
1345 if (is_support_sw_smu(adev))
1346 size = smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf);
1347 else if (adev->powerplay.pp_funcs->print_clock_levels)
1348 size = amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1350 size = snprintf(buf, PAGE_SIZE, "\n");
1352 pm_runtime_mark_last_busy(ddev->dev);
1353 pm_runtime_put_autosuspend(ddev->dev);
1358 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1359 struct device_attribute *attr,
1363 struct drm_device *ddev = dev_get_drvdata(dev);
1364 struct amdgpu_device *adev = ddev->dev_private;
1368 if (adev->in_gpu_reset)
1371 ret = amdgpu_read_mask(buf, count, &mask);
1375 ret = pm_runtime_get_sync(ddev->dev);
1377 pm_runtime_put_autosuspend(ddev->dev);
1381 if (is_support_sw_smu(adev))
1382 ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask);
1383 else if (adev->powerplay.pp_funcs->force_clock_level)
1384 ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1388 pm_runtime_mark_last_busy(ddev->dev);
1389 pm_runtime_put_autosuspend(ddev->dev);
1397 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1398 struct device_attribute *attr,
1401 struct drm_device *ddev = dev_get_drvdata(dev);
1402 struct amdgpu_device *adev = ddev->dev_private;
1406 if (adev->in_gpu_reset)
1409 ret = pm_runtime_get_sync(ddev->dev);
1411 pm_runtime_put_autosuspend(ddev->dev);
1415 if (is_support_sw_smu(adev))
1416 size = smu_print_clk_levels(&adev->smu, SMU_PCIE, buf);
1417 else if (adev->powerplay.pp_funcs->print_clock_levels)
1418 size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1420 size = snprintf(buf, PAGE_SIZE, "\n");
1422 pm_runtime_mark_last_busy(ddev->dev);
1423 pm_runtime_put_autosuspend(ddev->dev);
1428 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1429 struct device_attribute *attr,
1433 struct drm_device *ddev = dev_get_drvdata(dev);
1434 struct amdgpu_device *adev = ddev->dev_private;
1438 if (adev->in_gpu_reset)
1441 ret = amdgpu_read_mask(buf, count, &mask);
1445 ret = pm_runtime_get_sync(ddev->dev);
1447 pm_runtime_put_autosuspend(ddev->dev);
1451 if (is_support_sw_smu(adev))
1452 ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask);
1453 else if (adev->powerplay.pp_funcs->force_clock_level)
1454 ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1458 pm_runtime_mark_last_busy(ddev->dev);
1459 pm_runtime_put_autosuspend(ddev->dev);
1467 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1468 struct device_attribute *attr,
1471 struct drm_device *ddev = dev_get_drvdata(dev);
1472 struct amdgpu_device *adev = ddev->dev_private;
1476 if (adev->in_gpu_reset)
1479 ret = pm_runtime_get_sync(ddev->dev);
1481 pm_runtime_put_autosuspend(ddev->dev);
1485 if (is_support_sw_smu(adev))
1486 value = smu_get_od_percentage(&(adev->smu), SMU_OD_SCLK);
1487 else if (adev->powerplay.pp_funcs->get_sclk_od)
1488 value = amdgpu_dpm_get_sclk_od(adev);
1490 pm_runtime_mark_last_busy(ddev->dev);
1491 pm_runtime_put_autosuspend(ddev->dev);
1493 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1496 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1497 struct device_attribute *attr,
1501 struct drm_device *ddev = dev_get_drvdata(dev);
1502 struct amdgpu_device *adev = ddev->dev_private;
1506 if (adev->in_gpu_reset)
1509 ret = kstrtol(buf, 0, &value);
1514 ret = pm_runtime_get_sync(ddev->dev);
1516 pm_runtime_put_autosuspend(ddev->dev);
1520 if (is_support_sw_smu(adev)) {
1521 value = smu_set_od_percentage(&(adev->smu), SMU_OD_SCLK, (uint32_t)value);
1523 if (adev->powerplay.pp_funcs->set_sclk_od)
1524 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1526 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1527 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1529 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1530 amdgpu_pm_compute_clocks(adev);
1534 pm_runtime_mark_last_busy(ddev->dev);
1535 pm_runtime_put_autosuspend(ddev->dev);
1540 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1541 struct device_attribute *attr,
1544 struct drm_device *ddev = dev_get_drvdata(dev);
1545 struct amdgpu_device *adev = ddev->dev_private;
1549 if (adev->in_gpu_reset)
1552 ret = pm_runtime_get_sync(ddev->dev);
1554 pm_runtime_put_autosuspend(ddev->dev);
1558 if (is_support_sw_smu(adev))
1559 value = smu_get_od_percentage(&(adev->smu), SMU_OD_MCLK);
1560 else if (adev->powerplay.pp_funcs->get_mclk_od)
1561 value = amdgpu_dpm_get_mclk_od(adev);
1563 pm_runtime_mark_last_busy(ddev->dev);
1564 pm_runtime_put_autosuspend(ddev->dev);
1566 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1569 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1570 struct device_attribute *attr,
1574 struct drm_device *ddev = dev_get_drvdata(dev);
1575 struct amdgpu_device *adev = ddev->dev_private;
1579 if (adev->in_gpu_reset)
1582 ret = kstrtol(buf, 0, &value);
1587 ret = pm_runtime_get_sync(ddev->dev);
1589 pm_runtime_put_autosuspend(ddev->dev);
1593 if (is_support_sw_smu(adev)) {
1594 value = smu_set_od_percentage(&(adev->smu), SMU_OD_MCLK, (uint32_t)value);
1596 if (adev->powerplay.pp_funcs->set_mclk_od)
1597 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1599 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1600 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1602 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1603 amdgpu_pm_compute_clocks(adev);
1607 pm_runtime_mark_last_busy(ddev->dev);
1608 pm_runtime_put_autosuspend(ddev->dev);
1614 * DOC: pp_power_profile_mode
1616 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1617 * related to switching between power levels in a power state. The file
1618 * pp_power_profile_mode is used for this.
1620 * Reading this file outputs a list of all of the predefined power profiles
1621 * and the relevant heuristics settings for that profile.
1623 * To select a profile or create a custom profile, first select manual using
1624 * power_dpm_force_performance_level. Writing the number of a predefined
1625 * profile to pp_power_profile_mode will enable those heuristics. To
1626 * create a custom set of heuristics, write a string of numbers to the file
1627 * starting with the number of the custom profile along with a setting
1628 * for each heuristic parameter. Due to differences across asic families
1629 * the heuristic parameters vary from family to family.
1633 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1634 struct device_attribute *attr,
1637 struct drm_device *ddev = dev_get_drvdata(dev);
1638 struct amdgpu_device *adev = ddev->dev_private;
1642 if (adev->in_gpu_reset)
1645 ret = pm_runtime_get_sync(ddev->dev);
1647 pm_runtime_put_autosuspend(ddev->dev);
1651 if (is_support_sw_smu(adev))
1652 size = smu_get_power_profile_mode(&adev->smu, buf);
1653 else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1654 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1656 size = snprintf(buf, PAGE_SIZE, "\n");
1658 pm_runtime_mark_last_busy(ddev->dev);
1659 pm_runtime_put_autosuspend(ddev->dev);
1665 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1666 struct device_attribute *attr,
1671 struct drm_device *ddev = dev_get_drvdata(dev);
1672 struct amdgpu_device *adev = ddev->dev_private;
1673 uint32_t parameter_size = 0;
1675 char *sub_str, buf_cpy[128];
1679 long int profile_mode = 0;
1680 const char delimiter[3] = {' ', '\n', '\0'};
1682 if (adev->in_gpu_reset)
1687 ret = kstrtol(tmp, 0, &profile_mode);
1691 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1692 if (count < 2 || count > 127)
1694 while (isspace(*++buf))
1696 memcpy(buf_cpy, buf, count-i);
1698 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1699 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1703 while (isspace(*tmp_str))
1707 parameter[parameter_size] = profile_mode;
1709 ret = pm_runtime_get_sync(ddev->dev);
1711 pm_runtime_put_autosuspend(ddev->dev);
1715 if (is_support_sw_smu(adev))
1716 ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size, true);
1717 else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1718 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1720 pm_runtime_mark_last_busy(ddev->dev);
1721 pm_runtime_put_autosuspend(ddev->dev);
1730 * DOC: gpu_busy_percent
1732 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1733 * is as a percentage. The file gpu_busy_percent is used for this.
1734 * The SMU firmware computes a percentage of load based on the
1735 * aggregate activity level in the IP cores.
1737 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1738 struct device_attribute *attr,
1741 struct drm_device *ddev = dev_get_drvdata(dev);
1742 struct amdgpu_device *adev = ddev->dev_private;
1743 int r, value, size = sizeof(value);
1745 if (adev->in_gpu_reset)
1748 r = pm_runtime_get_sync(ddev->dev);
1750 pm_runtime_put_autosuspend(ddev->dev);
1754 /* read the IP busy sensor */
1755 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1756 (void *)&value, &size);
1758 pm_runtime_mark_last_busy(ddev->dev);
1759 pm_runtime_put_autosuspend(ddev->dev);
1764 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1768 * DOC: mem_busy_percent
1770 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1771 * is as a percentage. The file mem_busy_percent is used for this.
1772 * The SMU firmware computes a percentage of load based on the
1773 * aggregate activity level in the IP cores.
1775 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1776 struct device_attribute *attr,
1779 struct drm_device *ddev = dev_get_drvdata(dev);
1780 struct amdgpu_device *adev = ddev->dev_private;
1781 int r, value, size = sizeof(value);
1783 if (adev->in_gpu_reset)
1786 r = pm_runtime_get_sync(ddev->dev);
1788 pm_runtime_put_autosuspend(ddev->dev);
1792 /* read the IP busy sensor */
1793 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1794 (void *)&value, &size);
1796 pm_runtime_mark_last_busy(ddev->dev);
1797 pm_runtime_put_autosuspend(ddev->dev);
1802 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1808 * The amdgpu driver provides a sysfs API for estimating how much data
1809 * has been received and sent by the GPU in the last second through PCIe.
1810 * The file pcie_bw is used for this.
1811 * The Perf counters count the number of received and sent messages and return
1812 * those values, as well as the maximum payload size of a PCIe packet (mps).
1813 * Note that it is not possible to easily and quickly obtain the size of each
1814 * packet transmitted, so we output the max payload size (mps) to allow for
1815 * quick estimation of the PCIe bandwidth usage
1817 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1818 struct device_attribute *attr,
1821 struct drm_device *ddev = dev_get_drvdata(dev);
1822 struct amdgpu_device *adev = ddev->dev_private;
1823 uint64_t count0 = 0, count1 = 0;
1826 if (adev->in_gpu_reset)
1829 if (adev->flags & AMD_IS_APU)
1832 if (!adev->asic_funcs->get_pcie_usage)
1835 ret = pm_runtime_get_sync(ddev->dev);
1837 pm_runtime_put_autosuspend(ddev->dev);
1841 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1843 pm_runtime_mark_last_busy(ddev->dev);
1844 pm_runtime_put_autosuspend(ddev->dev);
1846 return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n",
1847 count0, count1, pcie_get_mps(adev->pdev));
1853 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1854 * The file unique_id is used for this.
1855 * This will provide a Unique ID that will persist from machine to machine
1857 * NOTE: This will only work for GFX9 and newer. This file will be absent
1858 * on unsupported ASICs (GFX8 and older)
1860 static ssize_t amdgpu_get_unique_id(struct device *dev,
1861 struct device_attribute *attr,
1864 struct drm_device *ddev = dev_get_drvdata(dev);
1865 struct amdgpu_device *adev = ddev->dev_private;
1867 if (adev->in_gpu_reset)
1870 if (adev->unique_id)
1871 return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1877 * DOC: thermal_throttling_logging
1879 * Thermal throttling pulls down the clock frequency and thus the performance.
1880 * It's an useful mechanism to protect the chip from overheating. Since it
1881 * impacts performance, the user controls whether it is enabled and if so,
1882 * the log frequency.
1884 * Reading back the file shows you the status(enabled or disabled) and
1885 * the interval(in seconds) between each thermal logging.
1887 * Writing an integer to the file, sets a new logging interval, in seconds.
1888 * The value should be between 1 and 3600. If the value is less than 1,
1889 * thermal logging is disabled. Values greater than 3600 are ignored.
1891 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1892 struct device_attribute *attr,
1895 struct drm_device *ddev = dev_get_drvdata(dev);
1896 struct amdgpu_device *adev = ddev->dev_private;
1898 return snprintf(buf, PAGE_SIZE, "%s: thermal throttling logging %s, with interval %d seconds\n",
1900 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1901 adev->throttling_logging_rs.interval / HZ + 1);
1904 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1905 struct device_attribute *attr,
1909 struct drm_device *ddev = dev_get_drvdata(dev);
1910 struct amdgpu_device *adev = ddev->dev_private;
1911 long throttling_logging_interval;
1912 unsigned long flags;
1915 ret = kstrtol(buf, 0, &throttling_logging_interval);
1919 if (throttling_logging_interval > 3600)
1922 if (throttling_logging_interval > 0) {
1923 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1925 * Reset the ratelimit timer internals.
1926 * This can effectively restart the timer.
1928 adev->throttling_logging_rs.interval =
1929 (throttling_logging_interval - 1) * HZ;
1930 adev->throttling_logging_rs.begin = 0;
1931 adev->throttling_logging_rs.printed = 0;
1932 adev->throttling_logging_rs.missed = 0;
1933 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1935 atomic_set(&adev->throttling_logging_enabled, 1);
1937 atomic_set(&adev->throttling_logging_enabled, 0);
1943 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1944 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1945 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC),
1946 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC),
1947 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC),
1948 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC),
1949 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC),
1950 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1951 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1952 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1953 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1954 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC),
1955 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC),
1956 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
1957 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
1958 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC),
1959 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC),
1960 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC),
1961 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC),
1962 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
1963 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC),
1964 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC),
1965 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC),
1968 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1969 uint32_t mask, enum amdgpu_device_attr_states *states)
1971 struct device_attribute *dev_attr = &attr->dev_attr;
1972 const char *attr_name = dev_attr->attr.name;
1973 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
1974 enum amd_asic_type asic_type = adev->asic_type;
1976 if (!(attr->flags & mask)) {
1977 *states = ATTR_STATE_UNSUPPORTED;
1981 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name))
1983 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
1984 if (asic_type < CHIP_VEGA10)
1985 *states = ATTR_STATE_UNSUPPORTED;
1986 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
1987 if (asic_type < CHIP_VEGA10 || asic_type == CHIP_ARCTURUS)
1988 *states = ATTR_STATE_UNSUPPORTED;
1989 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
1990 if (asic_type < CHIP_VEGA20)
1991 *states = ATTR_STATE_UNSUPPORTED;
1992 } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
1993 if (asic_type == CHIP_ARCTURUS)
1994 *states = ATTR_STATE_UNSUPPORTED;
1995 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
1996 *states = ATTR_STATE_UNSUPPORTED;
1997 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
1998 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
1999 *states = ATTR_STATE_SUPPORTED;
2000 } else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2001 if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2002 *states = ATTR_STATE_UNSUPPORTED;
2003 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2004 /* PCIe Perf counters won't work on APU nodes */
2005 if (adev->flags & AMD_IS_APU)
2006 *states = ATTR_STATE_UNSUPPORTED;
2007 } else if (DEVICE_ATTR_IS(unique_id)) {
2008 if (asic_type != CHIP_VEGA10 &&
2009 asic_type != CHIP_VEGA20 &&
2010 asic_type != CHIP_ARCTURUS)
2011 *states = ATTR_STATE_UNSUPPORTED;
2012 } else if (DEVICE_ATTR_IS(pp_features)) {
2013 if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2014 *states = ATTR_STATE_UNSUPPORTED;
2017 if (asic_type == CHIP_ARCTURUS) {
2018 /* Arcturus does not support standalone mclk/socclk/fclk level setting */
2019 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2020 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2021 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2022 dev_attr->attr.mode &= ~S_IWUGO;
2023 dev_attr->store = NULL;
2027 #undef DEVICE_ATTR_IS
2033 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2034 struct amdgpu_device_attr *attr,
2035 uint32_t mask, struct list_head *attr_list)
2038 struct device_attribute *dev_attr = &attr->dev_attr;
2039 const char *name = dev_attr->attr.name;
2040 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2041 struct amdgpu_device_attr_entry *attr_entry;
2043 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2044 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2048 attr_update = attr->attr_update ? attr_update : default_attr_update;
2050 ret = attr_update(adev, attr, mask, &attr_states);
2052 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2057 if (attr_states == ATTR_STATE_UNSUPPORTED)
2060 ret = device_create_file(adev->dev, dev_attr);
2062 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2066 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2070 attr_entry->attr = attr;
2071 INIT_LIST_HEAD(&attr_entry->entry);
2073 list_add_tail(&attr_entry->entry, attr_list);
2078 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2080 struct device_attribute *dev_attr = &attr->dev_attr;
2082 device_remove_file(adev->dev, dev_attr);
2085 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2086 struct list_head *attr_list);
2088 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2089 struct amdgpu_device_attr *attrs,
2092 struct list_head *attr_list)
2097 for (i = 0; i < counts; i++) {
2098 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2106 amdgpu_device_attr_remove_groups(adev, attr_list);
2111 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2112 struct list_head *attr_list)
2114 struct amdgpu_device_attr_entry *entry, *entry_tmp;
2116 if (list_empty(attr_list))
2119 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2120 amdgpu_device_attr_remove(adev, entry->attr);
2121 list_del(&entry->entry);
2126 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2127 struct device_attribute *attr,
2130 struct amdgpu_device *adev = dev_get_drvdata(dev);
2131 int channel = to_sensor_dev_attr(attr)->index;
2132 int r, temp = 0, size = sizeof(temp);
2134 if (adev->in_gpu_reset)
2137 if (channel >= PP_TEMP_MAX)
2140 r = pm_runtime_get_sync(adev->ddev->dev);
2142 pm_runtime_put_autosuspend(adev->ddev->dev);
2147 case PP_TEMP_JUNCTION:
2148 /* get current junction temperature */
2149 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2150 (void *)&temp, &size);
2153 /* get current edge temperature */
2154 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2155 (void *)&temp, &size);
2158 /* get current memory temperature */
2159 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2160 (void *)&temp, &size);
2167 pm_runtime_mark_last_busy(adev->ddev->dev);
2168 pm_runtime_put_autosuspend(adev->ddev->dev);
2173 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2176 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2177 struct device_attribute *attr,
2180 struct amdgpu_device *adev = dev_get_drvdata(dev);
2181 int hyst = to_sensor_dev_attr(attr)->index;
2185 temp = adev->pm.dpm.thermal.min_temp;
2187 temp = adev->pm.dpm.thermal.max_temp;
2189 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2192 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2193 struct device_attribute *attr,
2196 struct amdgpu_device *adev = dev_get_drvdata(dev);
2197 int hyst = to_sensor_dev_attr(attr)->index;
2201 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2203 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2205 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2208 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2209 struct device_attribute *attr,
2212 struct amdgpu_device *adev = dev_get_drvdata(dev);
2213 int hyst = to_sensor_dev_attr(attr)->index;
2217 temp = adev->pm.dpm.thermal.min_mem_temp;
2219 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2221 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2224 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2225 struct device_attribute *attr,
2228 int channel = to_sensor_dev_attr(attr)->index;
2230 if (channel >= PP_TEMP_MAX)
2233 return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
2236 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2237 struct device_attribute *attr,
2240 struct amdgpu_device *adev = dev_get_drvdata(dev);
2241 int channel = to_sensor_dev_attr(attr)->index;
2244 if (channel >= PP_TEMP_MAX)
2248 case PP_TEMP_JUNCTION:
2249 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2252 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2255 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2259 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2262 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2263 struct device_attribute *attr,
2266 struct amdgpu_device *adev = dev_get_drvdata(dev);
2270 if (adev->in_gpu_reset)
2273 ret = pm_runtime_get_sync(adev->ddev->dev);
2275 pm_runtime_put_autosuspend(adev->ddev->dev);
2279 if (is_support_sw_smu(adev)) {
2280 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2282 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2283 pm_runtime_mark_last_busy(adev->ddev->dev);
2284 pm_runtime_put_autosuspend(adev->ddev->dev);
2288 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2291 pm_runtime_mark_last_busy(adev->ddev->dev);
2292 pm_runtime_put_autosuspend(adev->ddev->dev);
2294 return sprintf(buf, "%i\n", pwm_mode);
2297 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2298 struct device_attribute *attr,
2302 struct amdgpu_device *adev = dev_get_drvdata(dev);
2306 if (adev->in_gpu_reset)
2309 err = kstrtoint(buf, 10, &value);
2313 ret = pm_runtime_get_sync(adev->ddev->dev);
2315 pm_runtime_put_autosuspend(adev->ddev->dev);
2319 if (is_support_sw_smu(adev)) {
2320 smu_set_fan_control_mode(&adev->smu, value);
2322 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2323 pm_runtime_mark_last_busy(adev->ddev->dev);
2324 pm_runtime_put_autosuspend(adev->ddev->dev);
2328 amdgpu_dpm_set_fan_control_mode(adev, value);
2331 pm_runtime_mark_last_busy(adev->ddev->dev);
2332 pm_runtime_put_autosuspend(adev->ddev->dev);
2337 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2338 struct device_attribute *attr,
2341 return sprintf(buf, "%i\n", 0);
2344 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2345 struct device_attribute *attr,
2348 return sprintf(buf, "%i\n", 255);
2351 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2352 struct device_attribute *attr,
2353 const char *buf, size_t count)
2355 struct amdgpu_device *adev = dev_get_drvdata(dev);
2360 if (adev->in_gpu_reset)
2363 err = pm_runtime_get_sync(adev->ddev->dev);
2365 pm_runtime_put_autosuspend(adev->ddev->dev);
2369 if (is_support_sw_smu(adev))
2370 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2372 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2374 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2375 pr_info("manual fan speed control should be enabled first\n");
2376 pm_runtime_mark_last_busy(adev->ddev->dev);
2377 pm_runtime_put_autosuspend(adev->ddev->dev);
2381 err = kstrtou32(buf, 10, &value);
2383 pm_runtime_mark_last_busy(adev->ddev->dev);
2384 pm_runtime_put_autosuspend(adev->ddev->dev);
2388 value = (value * 100) / 255;
2390 if (is_support_sw_smu(adev))
2391 err = smu_set_fan_speed_percent(&adev->smu, value);
2392 else if (adev->powerplay.pp_funcs->set_fan_speed_percent)
2393 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
2397 pm_runtime_mark_last_busy(adev->ddev->dev);
2398 pm_runtime_put_autosuspend(adev->ddev->dev);
2406 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2407 struct device_attribute *attr,
2410 struct amdgpu_device *adev = dev_get_drvdata(dev);
2414 if (adev->in_gpu_reset)
2417 err = pm_runtime_get_sync(adev->ddev->dev);
2419 pm_runtime_put_autosuspend(adev->ddev->dev);
2423 if (is_support_sw_smu(adev))
2424 err = smu_get_fan_speed_percent(&adev->smu, &speed);
2425 else if (adev->powerplay.pp_funcs->get_fan_speed_percent)
2426 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
2430 pm_runtime_mark_last_busy(adev->ddev->dev);
2431 pm_runtime_put_autosuspend(adev->ddev->dev);
2436 speed = (speed * 255) / 100;
2438 return sprintf(buf, "%i\n", speed);
2441 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2442 struct device_attribute *attr,
2445 struct amdgpu_device *adev = dev_get_drvdata(dev);
2449 if (adev->in_gpu_reset)
2452 err = pm_runtime_get_sync(adev->ddev->dev);
2454 pm_runtime_put_autosuspend(adev->ddev->dev);
2458 if (is_support_sw_smu(adev))
2459 err = smu_get_fan_speed_rpm(&adev->smu, &speed);
2460 else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2461 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2465 pm_runtime_mark_last_busy(adev->ddev->dev);
2466 pm_runtime_put_autosuspend(adev->ddev->dev);
2471 return sprintf(buf, "%i\n", speed);
2474 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2475 struct device_attribute *attr,
2478 struct amdgpu_device *adev = dev_get_drvdata(dev);
2480 u32 size = sizeof(min_rpm);
2483 if (adev->in_gpu_reset)
2486 r = pm_runtime_get_sync(adev->ddev->dev);
2488 pm_runtime_put_autosuspend(adev->ddev->dev);
2492 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2493 (void *)&min_rpm, &size);
2495 pm_runtime_mark_last_busy(adev->ddev->dev);
2496 pm_runtime_put_autosuspend(adev->ddev->dev);
2501 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
2504 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2505 struct device_attribute *attr,
2508 struct amdgpu_device *adev = dev_get_drvdata(dev);
2510 u32 size = sizeof(max_rpm);
2513 if (adev->in_gpu_reset)
2516 r = pm_runtime_get_sync(adev->ddev->dev);
2518 pm_runtime_put_autosuspend(adev->ddev->dev);
2522 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2523 (void *)&max_rpm, &size);
2525 pm_runtime_mark_last_busy(adev->ddev->dev);
2526 pm_runtime_put_autosuspend(adev->ddev->dev);
2531 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
2534 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2535 struct device_attribute *attr,
2538 struct amdgpu_device *adev = dev_get_drvdata(dev);
2542 if (adev->in_gpu_reset)
2545 err = pm_runtime_get_sync(adev->ddev->dev);
2547 pm_runtime_put_autosuspend(adev->ddev->dev);
2551 if (is_support_sw_smu(adev))
2552 err = smu_get_fan_speed_rpm(&adev->smu, &rpm);
2553 else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2554 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2558 pm_runtime_mark_last_busy(adev->ddev->dev);
2559 pm_runtime_put_autosuspend(adev->ddev->dev);
2564 return sprintf(buf, "%i\n", rpm);
2567 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2568 struct device_attribute *attr,
2569 const char *buf, size_t count)
2571 struct amdgpu_device *adev = dev_get_drvdata(dev);
2576 if (adev->in_gpu_reset)
2579 err = pm_runtime_get_sync(adev->ddev->dev);
2581 pm_runtime_put_autosuspend(adev->ddev->dev);
2585 if (is_support_sw_smu(adev))
2586 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2588 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2590 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2591 pm_runtime_mark_last_busy(adev->ddev->dev);
2592 pm_runtime_put_autosuspend(adev->ddev->dev);
2596 err = kstrtou32(buf, 10, &value);
2598 pm_runtime_mark_last_busy(adev->ddev->dev);
2599 pm_runtime_put_autosuspend(adev->ddev->dev);
2603 if (is_support_sw_smu(adev))
2604 err = smu_set_fan_speed_rpm(&adev->smu, value);
2605 else if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2606 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2610 pm_runtime_mark_last_busy(adev->ddev->dev);
2611 pm_runtime_put_autosuspend(adev->ddev->dev);
2619 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2620 struct device_attribute *attr,
2623 struct amdgpu_device *adev = dev_get_drvdata(dev);
2627 if (adev->in_gpu_reset)
2630 ret = pm_runtime_get_sync(adev->ddev->dev);
2632 pm_runtime_put_autosuspend(adev->ddev->dev);
2636 if (is_support_sw_smu(adev)) {
2637 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2639 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2640 pm_runtime_mark_last_busy(adev->ddev->dev);
2641 pm_runtime_put_autosuspend(adev->ddev->dev);
2645 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2648 pm_runtime_mark_last_busy(adev->ddev->dev);
2649 pm_runtime_put_autosuspend(adev->ddev->dev);
2651 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2654 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2655 struct device_attribute *attr,
2659 struct amdgpu_device *adev = dev_get_drvdata(dev);
2664 if (adev->in_gpu_reset)
2667 err = kstrtoint(buf, 10, &value);
2672 pwm_mode = AMD_FAN_CTRL_AUTO;
2673 else if (value == 1)
2674 pwm_mode = AMD_FAN_CTRL_MANUAL;
2678 err = pm_runtime_get_sync(adev->ddev->dev);
2680 pm_runtime_put_autosuspend(adev->ddev->dev);
2684 if (is_support_sw_smu(adev)) {
2685 smu_set_fan_control_mode(&adev->smu, pwm_mode);
2687 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2688 pm_runtime_mark_last_busy(adev->ddev->dev);
2689 pm_runtime_put_autosuspend(adev->ddev->dev);
2692 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2695 pm_runtime_mark_last_busy(adev->ddev->dev);
2696 pm_runtime_put_autosuspend(adev->ddev->dev);
2701 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2702 struct device_attribute *attr,
2705 struct amdgpu_device *adev = dev_get_drvdata(dev);
2707 int r, size = sizeof(vddgfx);
2709 if (adev->in_gpu_reset)
2712 r = pm_runtime_get_sync(adev->ddev->dev);
2714 pm_runtime_put_autosuspend(adev->ddev->dev);
2718 /* get the voltage */
2719 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2720 (void *)&vddgfx, &size);
2722 pm_runtime_mark_last_busy(adev->ddev->dev);
2723 pm_runtime_put_autosuspend(adev->ddev->dev);
2728 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
2731 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2732 struct device_attribute *attr,
2735 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
2738 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2739 struct device_attribute *attr,
2742 struct amdgpu_device *adev = dev_get_drvdata(dev);
2744 int r, size = sizeof(vddnb);
2746 if (adev->in_gpu_reset)
2749 /* only APUs have vddnb */
2750 if (!(adev->flags & AMD_IS_APU))
2753 r = pm_runtime_get_sync(adev->ddev->dev);
2755 pm_runtime_put_autosuspend(adev->ddev->dev);
2759 /* get the voltage */
2760 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2761 (void *)&vddnb, &size);
2763 pm_runtime_mark_last_busy(adev->ddev->dev);
2764 pm_runtime_put_autosuspend(adev->ddev->dev);
2769 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
2772 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2773 struct device_attribute *attr,
2776 return snprintf(buf, PAGE_SIZE, "vddnb\n");
2779 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2780 struct device_attribute *attr,
2783 struct amdgpu_device *adev = dev_get_drvdata(dev);
2785 int r, size = sizeof(u32);
2788 if (adev->in_gpu_reset)
2791 r = pm_runtime_get_sync(adev->ddev->dev);
2793 pm_runtime_put_autosuspend(adev->ddev->dev);
2797 /* get the voltage */
2798 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2799 (void *)&query, &size);
2801 pm_runtime_mark_last_busy(adev->ddev->dev);
2802 pm_runtime_put_autosuspend(adev->ddev->dev);
2807 /* convert to microwatts */
2808 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2810 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
2813 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2814 struct device_attribute *attr,
2817 return sprintf(buf, "%i\n", 0);
2820 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2821 struct device_attribute *attr,
2824 struct amdgpu_device *adev = dev_get_drvdata(dev);
2829 if (adev->in_gpu_reset)
2832 r = pm_runtime_get_sync(adev->ddev->dev);
2834 pm_runtime_put_autosuspend(adev->ddev->dev);
2838 if (is_support_sw_smu(adev)) {
2839 smu_get_power_limit(&adev->smu, &limit, true);
2840 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2841 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2842 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2843 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2845 size = snprintf(buf, PAGE_SIZE, "\n");
2848 pm_runtime_mark_last_busy(adev->ddev->dev);
2849 pm_runtime_put_autosuspend(adev->ddev->dev);
2854 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2855 struct device_attribute *attr,
2858 struct amdgpu_device *adev = dev_get_drvdata(dev);
2863 if (adev->in_gpu_reset)
2866 r = pm_runtime_get_sync(adev->ddev->dev);
2868 pm_runtime_put_autosuspend(adev->ddev->dev);
2872 if (is_support_sw_smu(adev)) {
2873 smu_get_power_limit(&adev->smu, &limit, false);
2874 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2875 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2876 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2877 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2879 size = snprintf(buf, PAGE_SIZE, "\n");
2882 pm_runtime_mark_last_busy(adev->ddev->dev);
2883 pm_runtime_put_autosuspend(adev->ddev->dev);
2889 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2890 struct device_attribute *attr,
2894 struct amdgpu_device *adev = dev_get_drvdata(dev);
2898 if (adev->in_gpu_reset)
2901 if (amdgpu_sriov_vf(adev))
2904 err = kstrtou32(buf, 10, &value);
2908 value = value / 1000000; /* convert to Watt */
2911 err = pm_runtime_get_sync(adev->ddev->dev);
2913 pm_runtime_put_autosuspend(adev->ddev->dev);
2917 if (is_support_sw_smu(adev))
2918 err = smu_set_power_limit(&adev->smu, value);
2919 else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit)
2920 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2924 pm_runtime_mark_last_busy(adev->ddev->dev);
2925 pm_runtime_put_autosuspend(adev->ddev->dev);
2933 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2934 struct device_attribute *attr,
2937 struct amdgpu_device *adev = dev_get_drvdata(dev);
2939 int r, size = sizeof(sclk);
2941 if (adev->in_gpu_reset)
2944 r = pm_runtime_get_sync(adev->ddev->dev);
2946 pm_runtime_put_autosuspend(adev->ddev->dev);
2951 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2952 (void *)&sclk, &size);
2954 pm_runtime_mark_last_busy(adev->ddev->dev);
2955 pm_runtime_put_autosuspend(adev->ddev->dev);
2960 return snprintf(buf, PAGE_SIZE, "%u\n", sclk * 10 * 1000);
2963 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2964 struct device_attribute *attr,
2967 return snprintf(buf, PAGE_SIZE, "sclk\n");
2970 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2971 struct device_attribute *attr,
2974 struct amdgpu_device *adev = dev_get_drvdata(dev);
2976 int r, size = sizeof(mclk);
2978 if (adev->in_gpu_reset)
2981 r = pm_runtime_get_sync(adev->ddev->dev);
2983 pm_runtime_put_autosuspend(adev->ddev->dev);
2988 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2989 (void *)&mclk, &size);
2991 pm_runtime_mark_last_busy(adev->ddev->dev);
2992 pm_runtime_put_autosuspend(adev->ddev->dev);
2997 return snprintf(buf, PAGE_SIZE, "%u\n", mclk * 10 * 1000);
3000 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3001 struct device_attribute *attr,
3004 return snprintf(buf, PAGE_SIZE, "mclk\n");
3010 * The amdgpu driver exposes the following sensor interfaces:
3012 * - GPU temperature (via the on-die sensor)
3016 * - Northbridge voltage (APUs only)
3022 * - GPU gfx/compute engine clock
3024 * - GPU memory clock (dGPU only)
3026 * hwmon interfaces for GPU temperature:
3028 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3029 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
3031 * - temp[1-3]_label: temperature channel label
3032 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3034 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3035 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3037 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3038 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3040 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3041 * - these are supported on SOC15 dGPUs only
3043 * hwmon interfaces for GPU voltage:
3045 * - in0_input: the voltage on the GPU in millivolts
3047 * - in1_input: the voltage on the Northbridge in millivolts
3049 * hwmon interfaces for GPU power:
3051 * - power1_average: average power used by the GPU in microWatts
3053 * - power1_cap_min: minimum cap supported in microWatts
3055 * - power1_cap_max: maximum cap supported in microWatts
3057 * - power1_cap: selected power cap in microWatts
3059 * hwmon interfaces for GPU fan:
3061 * - pwm1: pulse width modulation fan level (0-255)
3063 * - 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)
3065 * - pwm1_min: pulse width modulation fan control minimum level (0)
3067 * - pwm1_max: pulse width modulation fan control maximum level (255)
3069 * - fan1_min: an minimum value Unit: revolution/min (RPM)
3071 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
3073 * - fan1_input: fan speed in RPM
3075 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3077 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3079 * hwmon interfaces for GPU clocks:
3081 * - freq1_input: the gfx/compute clock in hertz
3083 * - freq2_input: the memory clock in hertz
3085 * You can use hwmon tools like sensors to view this information on your system.
3089 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3090 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3091 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3092 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3093 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3094 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3095 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3096 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3097 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3098 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3099 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3100 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3101 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3102 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3103 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3104 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3105 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3106 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3107 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3108 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3109 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3110 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3111 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3112 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3113 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3114 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3115 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3116 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3117 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3118 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3119 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3120 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3121 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3122 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3123 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3124 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3126 static struct attribute *hwmon_attributes[] = {
3127 &sensor_dev_attr_temp1_input.dev_attr.attr,
3128 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3129 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3130 &sensor_dev_attr_temp2_input.dev_attr.attr,
3131 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3132 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3133 &sensor_dev_attr_temp3_input.dev_attr.attr,
3134 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3135 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3136 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3137 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3138 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
3139 &sensor_dev_attr_temp1_label.dev_attr.attr,
3140 &sensor_dev_attr_temp2_label.dev_attr.attr,
3141 &sensor_dev_attr_temp3_label.dev_attr.attr,
3142 &sensor_dev_attr_pwm1.dev_attr.attr,
3143 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3144 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3145 &sensor_dev_attr_pwm1_max.dev_attr.attr,
3146 &sensor_dev_attr_fan1_input.dev_attr.attr,
3147 &sensor_dev_attr_fan1_min.dev_attr.attr,
3148 &sensor_dev_attr_fan1_max.dev_attr.attr,
3149 &sensor_dev_attr_fan1_target.dev_attr.attr,
3150 &sensor_dev_attr_fan1_enable.dev_attr.attr,
3151 &sensor_dev_attr_in0_input.dev_attr.attr,
3152 &sensor_dev_attr_in0_label.dev_attr.attr,
3153 &sensor_dev_attr_in1_input.dev_attr.attr,
3154 &sensor_dev_attr_in1_label.dev_attr.attr,
3155 &sensor_dev_attr_power1_average.dev_attr.attr,
3156 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3157 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3158 &sensor_dev_attr_power1_cap.dev_attr.attr,
3159 &sensor_dev_attr_freq1_input.dev_attr.attr,
3160 &sensor_dev_attr_freq1_label.dev_attr.attr,
3161 &sensor_dev_attr_freq2_input.dev_attr.attr,
3162 &sensor_dev_attr_freq2_label.dev_attr.attr,
3166 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3167 struct attribute *attr, int index)
3169 struct device *dev = kobj_to_dev(kobj);
3170 struct amdgpu_device *adev = dev_get_drvdata(dev);
3171 umode_t effective_mode = attr->mode;
3173 /* under multi-vf mode, the hwmon attributes are all not supported */
3174 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3177 /* there is no fan under pp one vf mode */
3178 if (amdgpu_sriov_is_pp_one_vf(adev) &&
3179 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3180 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3181 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3182 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3183 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3184 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3185 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3186 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3187 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3190 /* Skip fan attributes if fan is not present */
3191 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3192 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3193 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3194 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3195 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3196 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3197 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3198 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3199 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3202 /* Skip fan attributes on APU */
3203 if ((adev->flags & AMD_IS_APU) &&
3204 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3205 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3206 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3207 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3208 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3209 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3210 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3211 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3212 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3215 /* Skip crit temp on APU */
3216 if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3217 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3218 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3221 /* Skip limit attributes if DPM is not enabled */
3222 if (!adev->pm.dpm_enabled &&
3223 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3224 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3225 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3226 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3227 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3228 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3229 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3230 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3231 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3232 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3233 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3236 if (!is_support_sw_smu(adev)) {
3237 /* mask fan attributes if we have no bindings for this asic to expose */
3238 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
3239 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3240 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3241 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3242 effective_mode &= ~S_IRUGO;
3244 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3245 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3246 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3247 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3248 effective_mode &= ~S_IWUSR;
3251 if (((adev->flags & AMD_IS_APU) ||
3252 adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3253 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */
3254 (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
3255 attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3256 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
3257 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
3260 if (!is_support_sw_smu(adev)) {
3261 /* hide max/min values if we can't both query and manage the fan */
3262 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3263 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
3264 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3265 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3266 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3267 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3270 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3271 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3272 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3273 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3277 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3278 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */
3279 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3280 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3283 /* only APUs have vddnb */
3284 if (!(adev->flags & AMD_IS_APU) &&
3285 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3286 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3289 /* no mclk on APUs */
3290 if ((adev->flags & AMD_IS_APU) &&
3291 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3292 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3295 /* only SOC15 dGPUs support hotspot and mem temperatures */
3296 if (((adev->flags & AMD_IS_APU) ||
3297 adev->asic_type < CHIP_VEGA10) &&
3298 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3299 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3300 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3301 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3302 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3303 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3304 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3305 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3306 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3307 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3308 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3311 return effective_mode;
3314 static const struct attribute_group hwmon_attrgroup = {
3315 .attrs = hwmon_attributes,
3316 .is_visible = hwmon_attributes_visible,
3319 static const struct attribute_group *hwmon_groups[] = {
3324 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
3326 struct amdgpu_device *adev =
3327 container_of(work, struct amdgpu_device,
3328 pm.dpm.thermal.work);
3329 /* switch to the thermal state */
3330 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
3331 int temp, size = sizeof(temp);
3333 if (!adev->pm.dpm_enabled)
3336 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
3337 (void *)&temp, &size)) {
3338 if (temp < adev->pm.dpm.thermal.min_temp)
3339 /* switch back the user state */
3340 dpm_state = adev->pm.dpm.user_state;
3342 if (adev->pm.dpm.thermal.high_to_low)
3343 /* switch back the user state */
3344 dpm_state = adev->pm.dpm.user_state;
3346 mutex_lock(&adev->pm.mutex);
3347 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
3348 adev->pm.dpm.thermal_active = true;
3350 adev->pm.dpm.thermal_active = false;
3351 adev->pm.dpm.state = dpm_state;
3352 mutex_unlock(&adev->pm.mutex);
3354 amdgpu_pm_compute_clocks(adev);
3357 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
3358 enum amd_pm_state_type dpm_state)
3361 struct amdgpu_ps *ps;
3363 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
3366 /* check if the vblank period is too short to adjust the mclk */
3367 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
3368 if (amdgpu_dpm_vblank_too_short(adev))
3369 single_display = false;
3372 /* certain older asics have a separare 3D performance state,
3373 * so try that first if the user selected performance
3375 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
3376 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
3377 /* balanced states don't exist at the moment */
3378 if (dpm_state == POWER_STATE_TYPE_BALANCED)
3379 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3382 /* Pick the best power state based on current conditions */
3383 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
3384 ps = &adev->pm.dpm.ps[i];
3385 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
3386 switch (dpm_state) {
3388 case POWER_STATE_TYPE_BATTERY:
3389 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
3390 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3397 case POWER_STATE_TYPE_BALANCED:
3398 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
3399 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3406 case POWER_STATE_TYPE_PERFORMANCE:
3407 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
3408 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3415 /* internal states */
3416 case POWER_STATE_TYPE_INTERNAL_UVD:
3417 if (adev->pm.dpm.uvd_ps)
3418 return adev->pm.dpm.uvd_ps;
3421 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
3422 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
3425 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
3426 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
3429 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
3430 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
3433 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
3434 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
3437 case POWER_STATE_TYPE_INTERNAL_BOOT:
3438 return adev->pm.dpm.boot_ps;
3439 case POWER_STATE_TYPE_INTERNAL_THERMAL:
3440 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
3443 case POWER_STATE_TYPE_INTERNAL_ACPI:
3444 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
3447 case POWER_STATE_TYPE_INTERNAL_ULV:
3448 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
3451 case POWER_STATE_TYPE_INTERNAL_3DPERF:
3452 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
3459 /* use a fallback state if we didn't match */
3460 switch (dpm_state) {
3461 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
3462 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
3463 goto restart_search;
3464 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
3465 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
3466 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
3467 if (adev->pm.dpm.uvd_ps) {
3468 return adev->pm.dpm.uvd_ps;
3470 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3471 goto restart_search;
3473 case POWER_STATE_TYPE_INTERNAL_THERMAL:
3474 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
3475 goto restart_search;
3476 case POWER_STATE_TYPE_INTERNAL_ACPI:
3477 dpm_state = POWER_STATE_TYPE_BATTERY;
3478 goto restart_search;
3479 case POWER_STATE_TYPE_BATTERY:
3480 case POWER_STATE_TYPE_BALANCED:
3481 case POWER_STATE_TYPE_INTERNAL_3DPERF:
3482 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3483 goto restart_search;
3491 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
3493 struct amdgpu_ps *ps;
3494 enum amd_pm_state_type dpm_state;
3498 /* if dpm init failed */
3499 if (!adev->pm.dpm_enabled)
3502 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
3503 /* add other state override checks here */
3504 if ((!adev->pm.dpm.thermal_active) &&
3505 (!adev->pm.dpm.uvd_active))
3506 adev->pm.dpm.state = adev->pm.dpm.user_state;
3508 dpm_state = adev->pm.dpm.state;
3510 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
3512 adev->pm.dpm.requested_ps = ps;
3516 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
3517 printk("switching from power state:\n");
3518 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
3519 printk("switching to power state:\n");
3520 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
3523 /* update whether vce is active */
3524 ps->vce_active = adev->pm.dpm.vce_active;
3525 if (adev->powerplay.pp_funcs->display_configuration_changed)
3526 amdgpu_dpm_display_configuration_changed(adev);
3528 ret = amdgpu_dpm_pre_set_power_state(adev);
3532 if (adev->powerplay.pp_funcs->check_state_equal) {
3533 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
3540 amdgpu_dpm_set_power_state(adev);
3541 amdgpu_dpm_post_set_power_state(adev);
3543 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
3544 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
3546 if (adev->powerplay.pp_funcs->force_performance_level) {
3547 if (adev->pm.dpm.thermal_active) {
3548 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
3549 /* force low perf level for thermal */
3550 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
3551 /* save the user's level */
3552 adev->pm.dpm.forced_level = level;
3554 /* otherwise, user selected level */
3555 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
3560 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
3564 if (adev->family == AMDGPU_FAMILY_SI) {
3565 mutex_lock(&adev->pm.mutex);
3567 adev->pm.dpm.uvd_active = true;
3568 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
3570 adev->pm.dpm.uvd_active = false;
3572 mutex_unlock(&adev->pm.mutex);
3574 amdgpu_pm_compute_clocks(adev);
3576 ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
3578 DRM_ERROR("Dpm %s uvd failed, ret = %d. \n",
3579 enable ? "enable" : "disable", ret);
3581 /* enable/disable Low Memory PState for UVD (4k videos) */
3582 if (adev->asic_type == CHIP_STONEY &&
3583 adev->uvd.decode_image_width >= WIDTH_4K) {
3584 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
3586 if (hwmgr && hwmgr->hwmgr_func &&
3587 hwmgr->hwmgr_func->update_nbdpm_pstate)
3588 hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
3595 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
3599 if (adev->family == AMDGPU_FAMILY_SI) {
3600 mutex_lock(&adev->pm.mutex);
3602 adev->pm.dpm.vce_active = true;
3603 /* XXX select vce level based on ring/task */
3604 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
3606 adev->pm.dpm.vce_active = false;
3608 mutex_unlock(&adev->pm.mutex);
3610 amdgpu_pm_compute_clocks(adev);
3612 ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
3614 DRM_ERROR("Dpm %s vce failed, ret = %d. \n",
3615 enable ? "enable" : "disable", ret);
3619 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
3623 if (adev->powerplay.pp_funcs->print_power_state == NULL)
3626 for (i = 0; i < adev->pm.dpm.num_ps; i++)
3627 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
3631 void amdgpu_dpm_enable_jpeg(struct amdgpu_device *adev, bool enable)
3635 ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_JPEG, !enable);
3637 DRM_ERROR("Dpm %s jpeg failed, ret = %d. \n",
3638 enable ? "enable" : "disable", ret);
3641 int amdgpu_pm_load_smu_firmware(struct amdgpu_device *adev, uint32_t *smu_version)
3645 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) {
3646 r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle);
3648 pr_err("smu firmware loading failed\n");
3651 *smu_version = adev->pm.fw_version;
3656 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3661 if (adev->pm.sysfs_initialized)
3664 if (adev->pm.dpm_enabled == 0)
3667 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3669 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3672 if (IS_ERR(adev->pm.int_hwmon_dev)) {
3673 ret = PTR_ERR(adev->pm.int_hwmon_dev);
3675 "Unable to register hwmon device: %d\n", ret);
3679 switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3680 case SRIOV_VF_MODE_ONE_VF:
3681 mask = ATTR_FLAG_ONEVF;
3683 case SRIOV_VF_MODE_MULTI_VF:
3686 case SRIOV_VF_MODE_BARE_METAL:
3688 mask = ATTR_FLAG_MASK_ALL;
3692 ret = amdgpu_device_attr_create_groups(adev,
3693 amdgpu_device_attrs,
3694 ARRAY_SIZE(amdgpu_device_attrs),
3696 &adev->pm.pm_attr_list);
3700 adev->pm.sysfs_initialized = true;
3705 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3707 if (adev->pm.dpm_enabled == 0)
3710 if (adev->pm.int_hwmon_dev)
3711 hwmon_device_unregister(adev->pm.int_hwmon_dev);
3713 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3716 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
3720 if (!adev->pm.dpm_enabled)
3723 if (adev->mode_info.num_crtc)
3724 amdgpu_display_bandwidth_update(adev);
3726 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3727 struct amdgpu_ring *ring = adev->rings[i];
3728 if (ring && ring->sched.ready)
3729 amdgpu_fence_wait_empty(ring);
3732 if (is_support_sw_smu(adev)) {
3733 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
3734 smu_handle_task(&adev->smu,
3736 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE,
3739 if (adev->powerplay.pp_funcs->dispatch_tasks) {
3740 if (!amdgpu_device_has_dc_support(adev)) {
3741 mutex_lock(&adev->pm.mutex);
3742 amdgpu_dpm_get_active_displays(adev);
3743 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
3744 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
3745 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
3746 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
3747 if (adev->pm.pm_display_cfg.vrefresh > 120)
3748 adev->pm.pm_display_cfg.min_vblank_time = 0;
3749 if (adev->powerplay.pp_funcs->display_configuration_change)
3750 adev->powerplay.pp_funcs->display_configuration_change(
3751 adev->powerplay.pp_handle,
3752 &adev->pm.pm_display_cfg);
3753 mutex_unlock(&adev->pm.mutex);
3755 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
3757 mutex_lock(&adev->pm.mutex);
3758 amdgpu_dpm_get_active_displays(adev);
3759 amdgpu_dpm_change_power_state_locked(adev);
3760 mutex_unlock(&adev->pm.mutex);
3768 #if defined(CONFIG_DEBUG_FS)
3770 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3778 size = sizeof(value);
3779 seq_printf(m, "GFX Clocks and Power:\n");
3780 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3781 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3782 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3783 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3784 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3785 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3786 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3787 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3788 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3789 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3790 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3791 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3792 size = sizeof(uint32_t);
3793 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3794 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3795 size = sizeof(value);
3796 seq_printf(m, "\n");
3799 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3800 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3803 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3804 seq_printf(m, "GPU Load: %u %%\n", value);
3806 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3807 seq_printf(m, "MEM Load: %u %%\n", value);
3809 seq_printf(m, "\n");
3811 /* SMC feature mask */
3812 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3813 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3815 if (adev->asic_type > CHIP_VEGA20) {
3817 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3819 seq_printf(m, "VCN: Disabled\n");
3821 seq_printf(m, "VCN: Enabled\n");
3822 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3823 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3824 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3825 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3828 seq_printf(m, "\n");
3831 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3833 seq_printf(m, "UVD: Disabled\n");
3835 seq_printf(m, "UVD: Enabled\n");
3836 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3837 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3838 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3839 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3842 seq_printf(m, "\n");
3845 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3847 seq_printf(m, "VCE: Disabled\n");
3849 seq_printf(m, "VCE: Enabled\n");
3850 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3851 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3859 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3863 for (i = 0; clocks[i].flag; i++)
3864 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3865 (flags & clocks[i].flag) ? "On" : "Off");
3868 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3870 struct drm_info_node *node = (struct drm_info_node *) m->private;
3871 struct drm_device *dev = node->minor->dev;
3872 struct amdgpu_device *adev = dev->dev_private;
3876 if (adev->in_gpu_reset)
3879 r = pm_runtime_get_sync(dev->dev);
3881 pm_runtime_put_autosuspend(dev->dev);
3885 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3886 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3887 amdgpu_parse_cg_state(m, flags);
3888 seq_printf(m, "\n");
3890 if (!adev->pm.dpm_enabled) {
3891 seq_printf(m, "dpm not enabled\n");
3892 pm_runtime_mark_last_busy(dev->dev);
3893 pm_runtime_put_autosuspend(dev->dev);
3897 if (!is_support_sw_smu(adev) &&
3898 adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3899 mutex_lock(&adev->pm.mutex);
3900 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3901 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3903 seq_printf(m, "Debugfs support not implemented for this asic\n");
3904 mutex_unlock(&adev->pm.mutex);
3907 r = amdgpu_debugfs_pm_info_pp(m, adev);
3910 pm_runtime_mark_last_busy(dev->dev);
3911 pm_runtime_put_autosuspend(dev->dev);
3916 static const struct drm_info_list amdgpu_pm_info_list[] = {
3917 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3921 int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3923 #if defined(CONFIG_DEBUG_FS)
3924 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));