xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision e017fb66f7ac152009a41810023e23b7bb6debf7)
1e098bc96SEvan Quan /*
2e098bc96SEvan Quan  * Copyright 2017 Advanced Micro Devices, Inc.
3e098bc96SEvan Quan  *
4e098bc96SEvan Quan  * Permission is hereby granted, free of charge, to any person obtaining a
5e098bc96SEvan Quan  * copy of this software and associated documentation files (the "Software"),
6e098bc96SEvan Quan  * to deal in the Software without restriction, including without limitation
7e098bc96SEvan Quan  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e098bc96SEvan Quan  * and/or sell copies of the Software, and to permit persons to whom the
9e098bc96SEvan Quan  * Software is furnished to do so, subject to the following conditions:
10e098bc96SEvan Quan  *
11e098bc96SEvan Quan  * The above copyright notice and this permission notice shall be included in
12e098bc96SEvan Quan  * all copies or substantial portions of the Software.
13e098bc96SEvan Quan  *
14e098bc96SEvan Quan  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15e098bc96SEvan Quan  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16e098bc96SEvan Quan  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17e098bc96SEvan Quan  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18e098bc96SEvan Quan  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19e098bc96SEvan Quan  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20e098bc96SEvan Quan  * OTHER DEALINGS IN THE SOFTWARE.
21e098bc96SEvan Quan  *
22e098bc96SEvan Quan  * Authors: Rafał Miłecki <zajec5@gmail.com>
23e098bc96SEvan Quan  *          Alex Deucher <alexdeucher@gmail.com>
24e098bc96SEvan Quan  */
25e098bc96SEvan Quan 
26e098bc96SEvan Quan #include <drm/drm_debugfs.h>
27e098bc96SEvan Quan 
28e098bc96SEvan Quan #include "amdgpu.h"
29e098bc96SEvan Quan #include "amdgpu_drv.h"
30e098bc96SEvan Quan #include "amdgpu_pm.h"
31e098bc96SEvan Quan #include "amdgpu_dpm.h"
32e098bc96SEvan Quan #include "amdgpu_smu.h"
33e098bc96SEvan Quan #include "atom.h"
34e098bc96SEvan Quan #include <linux/pci.h>
35e098bc96SEvan Quan #include <linux/hwmon.h>
36e098bc96SEvan Quan #include <linux/hwmon-sysfs.h>
37e098bc96SEvan Quan #include <linux/nospec.h>
38e098bc96SEvan Quan #include <linux/pm_runtime.h>
39517cb957SHuang Rui #include <asm/processor.h>
40e098bc96SEvan Quan #include "hwmgr.h"
41e098bc96SEvan Quan 
42e098bc96SEvan Quan static const struct cg_flag_name clocks[] = {
43adf16996SJinzhou.Su 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
44e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
45e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
46e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
47e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
48e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
49e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
50e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
51e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
52e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
53e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
54e098bc96SEvan Quan 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
55e098bc96SEvan Quan 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
56e098bc96SEvan Quan 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
57e098bc96SEvan Quan 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
58e098bc96SEvan Quan 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
59e098bc96SEvan Quan 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
60e098bc96SEvan Quan 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
61e098bc96SEvan Quan 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
62e098bc96SEvan Quan 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
63e098bc96SEvan Quan 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
64e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
65e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
66e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
67e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
6871037bfcSKevin Wang 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
6971037bfcSKevin Wang 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
7071037bfcSKevin Wang 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
7171037bfcSKevin Wang 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
7271037bfcSKevin Wang 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
73e098bc96SEvan Quan 
74e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
75e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
76e098bc96SEvan Quan 	{0, NULL},
77e098bc96SEvan Quan };
78e098bc96SEvan Quan 
79e098bc96SEvan Quan static const struct hwmon_temp_label {
80e098bc96SEvan Quan 	enum PP_HWMON_TEMP channel;
81e098bc96SEvan Quan 	const char *label;
82e098bc96SEvan Quan } temp_label[] = {
83e098bc96SEvan Quan 	{PP_TEMP_EDGE, "edge"},
84e098bc96SEvan Quan 	{PP_TEMP_JUNCTION, "junction"},
85e098bc96SEvan Quan 	{PP_TEMP_MEM, "mem"},
86e098bc96SEvan Quan };
87e098bc96SEvan Quan 
88e098bc96SEvan Quan /**
89e098bc96SEvan Quan  * DOC: power_dpm_state
90e098bc96SEvan Quan  *
91e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
92e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
93e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
94e098bc96SEvan Quan  * It accepts the following arguments:
95e098bc96SEvan Quan  *
96e098bc96SEvan Quan  * - battery
97e098bc96SEvan Quan  *
98e098bc96SEvan Quan  * - balanced
99e098bc96SEvan Quan  *
100e098bc96SEvan Quan  * - performance
101e098bc96SEvan Quan  *
102e098bc96SEvan Quan  * battery
103e098bc96SEvan Quan  *
104e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
105e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
106e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
107e098bc96SEvan Quan  *
108e098bc96SEvan Quan  * balanced
109e098bc96SEvan Quan  *
110e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
111e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
112e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
113e098bc96SEvan Quan  *
114e098bc96SEvan Quan  * performance
115e098bc96SEvan Quan  *
116e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
117e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
118e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
119e098bc96SEvan Quan  *
120e098bc96SEvan Quan  */
121e098bc96SEvan Quan 
122e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
123e098bc96SEvan Quan 					  struct device_attribute *attr,
124e098bc96SEvan Quan 					  char *buf)
125e098bc96SEvan Quan {
126e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1271348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
128e098bc96SEvan Quan 	enum amd_pm_state_type pm;
129e098bc96SEvan Quan 	int ret;
130e098bc96SEvan Quan 
13153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
132e098bc96SEvan Quan 		return -EPERM;
133e098bc96SEvan Quan 
134e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
135e098bc96SEvan Quan 	if (ret < 0) {
136e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
137e098bc96SEvan Quan 		return ret;
138e098bc96SEvan Quan 	}
139e098bc96SEvan Quan 
140e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
141e098bc96SEvan Quan 		if (adev->smu.ppt_funcs->get_current_power_state)
142e098bc96SEvan Quan 			pm = smu_get_current_power_state(&adev->smu);
143e098bc96SEvan Quan 		else
144e098bc96SEvan Quan 			pm = adev->pm.dpm.user_state;
145e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->get_current_power_state) {
146e098bc96SEvan Quan 		pm = amdgpu_dpm_get_current_power_state(adev);
147e098bc96SEvan Quan 	} else {
148e098bc96SEvan Quan 		pm = adev->pm.dpm.user_state;
149e098bc96SEvan Quan 	}
150e098bc96SEvan Quan 
151e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
152e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
153e098bc96SEvan Quan 
154e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%s\n",
155e098bc96SEvan Quan 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
156e098bc96SEvan Quan 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
157e098bc96SEvan Quan }
158e098bc96SEvan Quan 
159e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
160e098bc96SEvan Quan 					  struct device_attribute *attr,
161e098bc96SEvan Quan 					  const char *buf,
162e098bc96SEvan Quan 					  size_t count)
163e098bc96SEvan Quan {
164e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
166e098bc96SEvan Quan 	enum amd_pm_state_type  state;
167e098bc96SEvan Quan 	int ret;
168e098bc96SEvan Quan 
16953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
170e098bc96SEvan Quan 		return -EPERM;
171e098bc96SEvan Quan 
172e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
173e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
174e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
175e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
176e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
177e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
178e098bc96SEvan Quan 	else
179e098bc96SEvan Quan 		return -EINVAL;
180e098bc96SEvan Quan 
181e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
182e098bc96SEvan Quan 	if (ret < 0) {
183e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
184e098bc96SEvan Quan 		return ret;
185e098bc96SEvan Quan 	}
186e098bc96SEvan Quan 
187e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
188e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
189e098bc96SEvan Quan 		adev->pm.dpm.user_state = state;
190e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
191e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->dispatch_tasks) {
192e098bc96SEvan Quan 		amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
193e098bc96SEvan Quan 	} else {
194e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
195e098bc96SEvan Quan 		adev->pm.dpm.user_state = state;
196e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
197e098bc96SEvan Quan 
198e098bc96SEvan Quan 		amdgpu_pm_compute_clocks(adev);
199e098bc96SEvan Quan 	}
200e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
201e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
202e098bc96SEvan Quan 
203e098bc96SEvan Quan 	return count;
204e098bc96SEvan Quan }
205e098bc96SEvan Quan 
206e098bc96SEvan Quan 
207e098bc96SEvan Quan /**
208e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
209e098bc96SEvan Quan  *
210e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
211e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
212e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
213e098bc96SEvan Quan  *
214e098bc96SEvan Quan  * - auto
215e098bc96SEvan Quan  *
216e098bc96SEvan Quan  * - low
217e098bc96SEvan Quan  *
218e098bc96SEvan Quan  * - high
219e098bc96SEvan Quan  *
220e098bc96SEvan Quan  * - manual
221e098bc96SEvan Quan  *
222e098bc96SEvan Quan  * - profile_standard
223e098bc96SEvan Quan  *
224e098bc96SEvan Quan  * - profile_min_sclk
225e098bc96SEvan Quan  *
226e098bc96SEvan Quan  * - profile_min_mclk
227e098bc96SEvan Quan  *
228e098bc96SEvan Quan  * - profile_peak
229e098bc96SEvan Quan  *
230e098bc96SEvan Quan  * auto
231e098bc96SEvan Quan  *
232e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
233e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
234e098bc96SEvan Quan  *
235e098bc96SEvan Quan  * low
236e098bc96SEvan Quan  *
237e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
238e098bc96SEvan Quan  *
239e098bc96SEvan Quan  * high
240e098bc96SEvan Quan  *
241e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
242e098bc96SEvan Quan  *
243e098bc96SEvan Quan  * manual
244e098bc96SEvan Quan  *
245e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
246e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
247e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
248e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
249e098bc96SEvan Quan  *
250e098bc96SEvan Quan  * profile_standard
251e098bc96SEvan Quan  * profile_min_sclk
252e098bc96SEvan Quan  * profile_min_mclk
253e098bc96SEvan Quan  * profile_peak
254e098bc96SEvan Quan  *
255e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
256e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
257e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
258e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
259e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
260e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
261e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
262e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
263e098bc96SEvan Quan  *
264e098bc96SEvan Quan  */
265e098bc96SEvan Quan 
266e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
267e098bc96SEvan Quan 							    struct device_attribute *attr,
268e098bc96SEvan Quan 							    char *buf)
269e098bc96SEvan Quan {
270e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2711348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
272e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
273e098bc96SEvan Quan 	int ret;
274e098bc96SEvan Quan 
27553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
276e098bc96SEvan Quan 		return -EPERM;
277e098bc96SEvan Quan 
278e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
279e098bc96SEvan Quan 	if (ret < 0) {
280e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
281e098bc96SEvan Quan 		return ret;
282e098bc96SEvan Quan 	}
283e098bc96SEvan Quan 
284e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
285e098bc96SEvan Quan 		level = smu_get_performance_level(&adev->smu);
286e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_performance_level)
287e098bc96SEvan Quan 		level = amdgpu_dpm_get_performance_level(adev);
288e098bc96SEvan Quan 	else
289e098bc96SEvan Quan 		level = adev->pm.dpm.forced_level;
290e098bc96SEvan Quan 
291e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
292e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
293e098bc96SEvan Quan 
294e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%s\n",
295e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
296e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
297e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
298e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
299e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
300e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
301e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
302e098bc96SEvan Quan 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
303e098bc96SEvan Quan 			"unknown");
304e098bc96SEvan Quan }
305e098bc96SEvan Quan 
306e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
307e098bc96SEvan Quan 							    struct device_attribute *attr,
308e098bc96SEvan Quan 							    const char *buf,
309e098bc96SEvan Quan 							    size_t count)
310e098bc96SEvan Quan {
311e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3121348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
313e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
314e098bc96SEvan Quan 	enum amd_dpm_forced_level current_level = 0xff;
315e098bc96SEvan Quan 	int ret = 0;
316e098bc96SEvan Quan 
31753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
318e098bc96SEvan Quan 		return -EPERM;
319e098bc96SEvan Quan 
320e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
321e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
322e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
323e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
324e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
325e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
326e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
327e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
328e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
329e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
330e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
331e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
332e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
333e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
334e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
335e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
336e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
337e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
338e098bc96SEvan Quan 	}  else {
339e098bc96SEvan Quan 		return -EINVAL;
340e098bc96SEvan Quan 	}
341e098bc96SEvan Quan 
342e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
343e098bc96SEvan Quan 	if (ret < 0) {
344e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
345e098bc96SEvan Quan 		return ret;
346e098bc96SEvan Quan 	}
347e098bc96SEvan Quan 
348e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
349e098bc96SEvan Quan 		current_level = smu_get_performance_level(&adev->smu);
350e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_performance_level)
351e098bc96SEvan Quan 		current_level = amdgpu_dpm_get_performance_level(adev);
352e098bc96SEvan Quan 
353e098bc96SEvan Quan 	if (current_level == level) {
354e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
355e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
356e098bc96SEvan Quan 		return count;
357e098bc96SEvan Quan 	}
358e098bc96SEvan Quan 
359e098bc96SEvan Quan 	if (adev->asic_type == CHIP_RAVEN) {
360e098bc96SEvan Quan 		if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) {
361e098bc96SEvan Quan 			if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL)
362e098bc96SEvan Quan 				amdgpu_gfx_off_ctrl(adev, false);
363e098bc96SEvan Quan 			else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL)
364e098bc96SEvan Quan 				amdgpu_gfx_off_ctrl(adev, true);
365e098bc96SEvan Quan 		}
366e098bc96SEvan Quan 	}
367e098bc96SEvan Quan 
368e098bc96SEvan Quan 	/* profile_exit setting is valid only when current mode is in profile mode */
369e098bc96SEvan Quan 	if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
370e098bc96SEvan Quan 	    AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
371e098bc96SEvan Quan 	    AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
372e098bc96SEvan Quan 	    AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
373e098bc96SEvan Quan 	    (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
374e098bc96SEvan Quan 		pr_err("Currently not in any profile mode!\n");
375e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
376e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
377e098bc96SEvan Quan 		return -EINVAL;
378e098bc96SEvan Quan 	}
379e098bc96SEvan Quan 
380e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
381e098bc96SEvan Quan 		ret = smu_force_performance_level(&adev->smu, level);
382e098bc96SEvan Quan 		if (ret) {
383e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
384e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
385e098bc96SEvan Quan 			return -EINVAL;
386e098bc96SEvan Quan 		}
387e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->force_performance_level) {
388e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
389e098bc96SEvan Quan 		if (adev->pm.dpm.thermal_active) {
390e098bc96SEvan Quan 			mutex_unlock(&adev->pm.mutex);
391e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
392e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
393e098bc96SEvan Quan 			return -EINVAL;
394e098bc96SEvan Quan 		}
395e098bc96SEvan Quan 		ret = amdgpu_dpm_force_performance_level(adev, level);
396e098bc96SEvan Quan 		if (ret) {
397e098bc96SEvan Quan 			mutex_unlock(&adev->pm.mutex);
398e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
399e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
400e098bc96SEvan Quan 			return -EINVAL;
401e098bc96SEvan Quan 		} else {
402e098bc96SEvan Quan 			adev->pm.dpm.forced_level = level;
403e098bc96SEvan Quan 		}
404e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
405e098bc96SEvan Quan 	}
406e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
407e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
408e098bc96SEvan Quan 
409e098bc96SEvan Quan 	return count;
410e098bc96SEvan Quan }
411e098bc96SEvan Quan 
412e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
413e098bc96SEvan Quan 		struct device_attribute *attr,
414e098bc96SEvan Quan 		char *buf)
415e098bc96SEvan Quan {
416e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4171348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
418e098bc96SEvan Quan 	struct pp_states_info data;
419e098bc96SEvan Quan 	int i, buf_len, ret;
420e098bc96SEvan Quan 
42153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
422e098bc96SEvan Quan 		return -EPERM;
423e098bc96SEvan Quan 
424e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
425e098bc96SEvan Quan 	if (ret < 0) {
426e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
427e098bc96SEvan Quan 		return ret;
428e098bc96SEvan Quan 	}
429e098bc96SEvan Quan 
430e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
431e098bc96SEvan Quan 		ret = smu_get_power_num_states(&adev->smu, &data);
432e098bc96SEvan Quan 		if (ret)
433e098bc96SEvan Quan 			return ret;
434e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->get_pp_num_states) {
435e098bc96SEvan Quan 		amdgpu_dpm_get_pp_num_states(adev, &data);
436e098bc96SEvan Quan 	} else {
437e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
438e098bc96SEvan Quan 	}
439e098bc96SEvan Quan 
440e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
441e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
442e098bc96SEvan Quan 
443e098bc96SEvan Quan 	buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
444e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
445e098bc96SEvan Quan 		buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
446e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
447e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
448e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
449e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
450e098bc96SEvan Quan 
451e098bc96SEvan Quan 	return buf_len;
452e098bc96SEvan Quan }
453e098bc96SEvan Quan 
454e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
455e098bc96SEvan Quan 		struct device_attribute *attr,
456e098bc96SEvan Quan 		char *buf)
457e098bc96SEvan Quan {
458e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4591348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
460e098bc96SEvan Quan 	struct pp_states_info data;
461e098bc96SEvan Quan 	struct smu_context *smu = &adev->smu;
462e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
463e098bc96SEvan Quan 	int i = 0, ret = 0;
464e098bc96SEvan Quan 
46553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
466e098bc96SEvan Quan 		return -EPERM;
467e098bc96SEvan Quan 
468e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
469e098bc96SEvan Quan 	if (ret < 0) {
470e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
471e098bc96SEvan Quan 		return ret;
472e098bc96SEvan Quan 	}
473e098bc96SEvan Quan 
474e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
475e098bc96SEvan Quan 		pm = smu_get_current_power_state(smu);
476e098bc96SEvan Quan 		ret = smu_get_power_num_states(smu, &data);
477e098bc96SEvan Quan 		if (ret)
478e098bc96SEvan Quan 			return ret;
479e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->get_current_power_state
480e098bc96SEvan Quan 		 && adev->powerplay.pp_funcs->get_pp_num_states) {
481e098bc96SEvan Quan 		pm = amdgpu_dpm_get_current_power_state(adev);
482e098bc96SEvan Quan 		amdgpu_dpm_get_pp_num_states(adev, &data);
483e098bc96SEvan Quan 	}
484e098bc96SEvan Quan 
485e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
486e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
487e098bc96SEvan Quan 
488e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
489e098bc96SEvan Quan 		if (pm == data.states[i])
490e098bc96SEvan Quan 			break;
491e098bc96SEvan Quan 	}
492e098bc96SEvan Quan 
493e098bc96SEvan Quan 	if (i == data.nums)
494e098bc96SEvan Quan 		i = -EINVAL;
495e098bc96SEvan Quan 
496e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", i);
497e098bc96SEvan Quan }
498e098bc96SEvan Quan 
499e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
500e098bc96SEvan Quan 		struct device_attribute *attr,
501e098bc96SEvan Quan 		char *buf)
502e098bc96SEvan Quan {
503e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5041348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
505e098bc96SEvan Quan 
50653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
507e098bc96SEvan Quan 		return -EPERM;
508e098bc96SEvan Quan 
509e098bc96SEvan Quan 	if (adev->pp_force_state_enabled)
510e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
511e098bc96SEvan Quan 	else
512e098bc96SEvan Quan 		return snprintf(buf, PAGE_SIZE, "\n");
513e098bc96SEvan Quan }
514e098bc96SEvan Quan 
515e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
516e098bc96SEvan Quan 		struct device_attribute *attr,
517e098bc96SEvan Quan 		const char *buf,
518e098bc96SEvan Quan 		size_t count)
519e098bc96SEvan Quan {
520e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5211348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
522e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
523e098bc96SEvan Quan 	unsigned long idx;
524e098bc96SEvan Quan 	int ret;
525e098bc96SEvan Quan 
52653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
527e098bc96SEvan Quan 		return -EPERM;
528e098bc96SEvan Quan 
529e098bc96SEvan Quan 	if (strlen(buf) == 1)
530e098bc96SEvan Quan 		adev->pp_force_state_enabled = false;
531e098bc96SEvan Quan 	else if (is_support_sw_smu(adev))
532e098bc96SEvan Quan 		adev->pp_force_state_enabled = false;
533e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->dispatch_tasks &&
534e098bc96SEvan Quan 			adev->powerplay.pp_funcs->get_pp_num_states) {
535e098bc96SEvan Quan 		struct pp_states_info data;
536e098bc96SEvan Quan 
537e098bc96SEvan Quan 		ret = kstrtoul(buf, 0, &idx);
538e098bc96SEvan Quan 		if (ret || idx >= ARRAY_SIZE(data.states))
539e098bc96SEvan Quan 			return -EINVAL;
540e098bc96SEvan Quan 
541e098bc96SEvan Quan 		idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
542e098bc96SEvan Quan 
543e098bc96SEvan Quan 		amdgpu_dpm_get_pp_num_states(adev, &data);
544e098bc96SEvan Quan 		state = data.states[idx];
545e098bc96SEvan Quan 
546e098bc96SEvan Quan 		ret = pm_runtime_get_sync(ddev->dev);
547e098bc96SEvan Quan 		if (ret < 0) {
548e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
549e098bc96SEvan Quan 			return ret;
550e098bc96SEvan Quan 		}
551e098bc96SEvan Quan 
552e098bc96SEvan Quan 		/* only set user selected power states */
553e098bc96SEvan Quan 		if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
554e098bc96SEvan Quan 		    state != POWER_STATE_TYPE_DEFAULT) {
555e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev,
556e098bc96SEvan Quan 					AMD_PP_TASK_ENABLE_USER_STATE, &state);
557e098bc96SEvan Quan 			adev->pp_force_state_enabled = true;
558e098bc96SEvan Quan 		}
559e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
560e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
561e098bc96SEvan Quan 	}
562e098bc96SEvan Quan 
563e098bc96SEvan Quan 	return count;
564e098bc96SEvan Quan }
565e098bc96SEvan Quan 
566e098bc96SEvan Quan /**
567e098bc96SEvan Quan  * DOC: pp_table
568e098bc96SEvan Quan  *
569e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
570e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
571e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
572e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
573e098bc96SEvan Quan  * powerplay using that new table.
574e098bc96SEvan Quan  *
575e098bc96SEvan Quan  */
576e098bc96SEvan Quan 
577e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
578e098bc96SEvan Quan 		struct device_attribute *attr,
579e098bc96SEvan Quan 		char *buf)
580e098bc96SEvan Quan {
581e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5821348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
583e098bc96SEvan Quan 	char *table = NULL;
584e098bc96SEvan Quan 	int size, ret;
585e098bc96SEvan Quan 
58653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
587e098bc96SEvan Quan 		return -EPERM;
588e098bc96SEvan Quan 
589e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
590e098bc96SEvan Quan 	if (ret < 0) {
591e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
592e098bc96SEvan Quan 		return ret;
593e098bc96SEvan Quan 	}
594e098bc96SEvan Quan 
595e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
596e098bc96SEvan Quan 		size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
597e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
598e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
599e098bc96SEvan Quan 		if (size < 0)
600e098bc96SEvan Quan 			return size;
601e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->get_pp_table) {
602e098bc96SEvan Quan 		size = amdgpu_dpm_get_pp_table(adev, &table);
603e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
604e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
605e098bc96SEvan Quan 		if (size < 0)
606e098bc96SEvan Quan 			return size;
607e098bc96SEvan Quan 	} else {
608e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
609e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
610e098bc96SEvan Quan 		return 0;
611e098bc96SEvan Quan 	}
612e098bc96SEvan Quan 
613e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
614e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
615e098bc96SEvan Quan 
616e098bc96SEvan Quan 	memcpy(buf, table, size);
617e098bc96SEvan Quan 
618e098bc96SEvan Quan 	return size;
619e098bc96SEvan Quan }
620e098bc96SEvan Quan 
621e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
622e098bc96SEvan Quan 		struct device_attribute *attr,
623e098bc96SEvan Quan 		const char *buf,
624e098bc96SEvan Quan 		size_t count)
625e098bc96SEvan Quan {
626e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
6271348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
628e098bc96SEvan Quan 	int ret = 0;
629e098bc96SEvan Quan 
63053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
631e098bc96SEvan Quan 		return -EPERM;
632e098bc96SEvan Quan 
633e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
634e098bc96SEvan Quan 	if (ret < 0) {
635e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
636e098bc96SEvan Quan 		return ret;
637e098bc96SEvan Quan 	}
638e098bc96SEvan Quan 
639e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
640e098bc96SEvan Quan 		ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
641e098bc96SEvan Quan 		if (ret) {
642e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
643e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
644e098bc96SEvan Quan 			return ret;
645e098bc96SEvan Quan 		}
646e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->set_pp_table)
647e098bc96SEvan Quan 		amdgpu_dpm_set_pp_table(adev, buf, count);
648e098bc96SEvan Quan 
649e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
650e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
651e098bc96SEvan Quan 
652e098bc96SEvan Quan 	return count;
653e098bc96SEvan Quan }
654e098bc96SEvan Quan 
655e098bc96SEvan Quan /**
656e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
657e098bc96SEvan Quan  *
658e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
659e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
660e098bc96SEvan Quan  * this.
661e098bc96SEvan Quan  *
662e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
663e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
664e098bc96SEvan Quan  * following formula:
665e098bc96SEvan Quan  *
666e098bc96SEvan Quan  * Clock conversion (Mhz):
667e098bc96SEvan Quan  *
668e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
669e098bc96SEvan Quan  *
670e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
671e098bc96SEvan Quan  *
672e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
673e098bc96SEvan Quan  *
674e098bc96SEvan Quan  * DRAM data rate (MT/s):
675e098bc96SEvan Quan  *
676e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
677e098bc96SEvan Quan  *
678e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
679e098bc96SEvan Quan  *
680e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
681e098bc96SEvan Quan  *
682e098bc96SEvan Quan  * Bandwidth (MB/s):
683e098bc96SEvan Quan  *
684e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
685e098bc96SEvan Quan  *
686e098bc96SEvan Quan  * Some examples:
687e098bc96SEvan Quan  *
688e098bc96SEvan Quan  * G5 on RX460:
689e098bc96SEvan Quan  *
690e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
691e098bc96SEvan Quan  *
692e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
693e098bc96SEvan Quan  *
694e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
695e098bc96SEvan Quan  *
696e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
697e098bc96SEvan Quan  *
698e098bc96SEvan Quan  * G6 on RX5700:
699e098bc96SEvan Quan  *
700e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
701e098bc96SEvan Quan  *
702e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
703e098bc96SEvan Quan  *
704e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
705e098bc96SEvan Quan  *
706e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
707e098bc96SEvan Quan  *
708e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
709e098bc96SEvan Quan  *
710e098bc96SEvan Quan  * Reading the file will display:
711e098bc96SEvan Quan  *
712e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
713e098bc96SEvan Quan  *
714e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
715e098bc96SEvan Quan  *
716e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
717e098bc96SEvan Quan  *
718e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
719e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
720e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
721e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
722e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
723e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
724e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
725e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
726e098bc96SEvan Quan  *
727e098bc96SEvan Quan  *
728e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
729e098bc96SEvan Quan  *
730e098bc96SEvan Quan  * Reading the file will display:
731e098bc96SEvan Quan  *
732e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
733e098bc96SEvan Quan  *
73437a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
73537a58f69SEvan Quan  *   clock labeled OD_MCLK
736e098bc96SEvan Quan  *
737e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
738e098bc96SEvan Quan  *   They can be used to calibrate the sclk voltage curve.
739e098bc96SEvan Quan  *
740a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
741a2b6df4fSEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder and Dimgrey
742a2b6df4fSEvan Quan  *   Cavefish. For these ASICs, the target voltage calculation can be
743a2b6df4fSEvan Quan  *   illustrated by "voltage = voltage calculated from v/f curve +
744a2b6df4fSEvan Quan  *   overdrive vddgfx offset"
745a2b6df4fSEvan Quan  *
746e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage curve points
747e098bc96SEvan Quan  *   labeled OD_RANGE
748e098bc96SEvan Quan  *
749e098bc96SEvan Quan  * To manually adjust these settings:
750e098bc96SEvan Quan  *
751e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
752e098bc96SEvan Quan  *
753e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
754e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
755e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
756e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
757e098bc96SEvan Quan  *   "m 1 800" will update maximum mclk to be 800Mhz.
758e098bc96SEvan Quan  *
759e098bc96SEvan Quan  *   For sclk voltage curve, enter the new values by writing a
760e098bc96SEvan Quan  *   string that contains "vc point clock voltage" to the file. The
761e098bc96SEvan Quan  *   points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
762e098bc96SEvan Quan  *   update point1 with clock set as 300Mhz and voltage as
763e098bc96SEvan Quan  *   600mV. "vc 2 1000 1000" will update point3 with clock set
764e098bc96SEvan Quan  *   as 1000Mhz and voltage 1000mV.
765e098bc96SEvan Quan  *
766a2b6df4fSEvan Quan  *   To update the voltage offset applied for gfxclk/voltage calculation,
767a2b6df4fSEvan Quan  *   enter the new value by writing a string that contains "vo offset".
768a2b6df4fSEvan Quan  *   This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
769a2b6df4fSEvan Quan  *   And the offset can be a positive or negative value.
770a2b6df4fSEvan Quan  *
771e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
772e098bc96SEvan Quan  *   to the file to commit your changes
773e098bc96SEvan Quan  *
774e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
775e098bc96SEvan Quan  *   to the file to reset them
776e098bc96SEvan Quan  *
777e098bc96SEvan Quan  */
778e098bc96SEvan Quan 
779e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
780e098bc96SEvan Quan 		struct device_attribute *attr,
781e098bc96SEvan Quan 		const char *buf,
782e098bc96SEvan Quan 		size_t count)
783e098bc96SEvan Quan {
784e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7851348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
786e098bc96SEvan Quan 	int ret;
787e098bc96SEvan Quan 	uint32_t parameter_size = 0;
788e098bc96SEvan Quan 	long parameter[64];
789e098bc96SEvan Quan 	char buf_cpy[128];
790e098bc96SEvan Quan 	char *tmp_str;
791e098bc96SEvan Quan 	char *sub_str;
792e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
793e098bc96SEvan Quan 	uint32_t type;
794e098bc96SEvan Quan 
79553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
796e098bc96SEvan Quan 		return -EPERM;
797e098bc96SEvan Quan 
798e098bc96SEvan Quan 	if (count > 127)
799e098bc96SEvan Quan 		return -EINVAL;
800e098bc96SEvan Quan 
801e098bc96SEvan Quan 	if (*buf == 's')
802e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
8030d90d0ddSHuang Rui 	else if (*buf == 'p')
8040d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
805e098bc96SEvan Quan 	else if (*buf == 'm')
806e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
807e098bc96SEvan Quan 	else if(*buf == 'r')
808e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
809e098bc96SEvan Quan 	else if (*buf == 'c')
810e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
811e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
812e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
813a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
814a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
815e098bc96SEvan Quan 	else
816e098bc96SEvan Quan 		return -EINVAL;
817e098bc96SEvan Quan 
818e098bc96SEvan Quan 	memcpy(buf_cpy, buf, count+1);
819e098bc96SEvan Quan 
820e098bc96SEvan Quan 	tmp_str = buf_cpy;
821e098bc96SEvan Quan 
822a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
823a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
824e098bc96SEvan Quan 		tmp_str++;
825e098bc96SEvan Quan 	while (isspace(*++tmp_str));
826e098bc96SEvan Quan 
827e098bc96SEvan Quan 	while (tmp_str[0]) {
828e098bc96SEvan Quan 		sub_str = strsep(&tmp_str, delimiter);
829e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
830e098bc96SEvan Quan 		if (ret)
831e098bc96SEvan Quan 			return -EINVAL;
832e098bc96SEvan Quan 		parameter_size++;
833e098bc96SEvan Quan 
834e098bc96SEvan Quan 		while (isspace(*tmp_str))
835e098bc96SEvan Quan 			tmp_str++;
836e098bc96SEvan Quan 	}
837e098bc96SEvan Quan 
838e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
839e098bc96SEvan Quan 	if (ret < 0) {
840e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
841e098bc96SEvan Quan 		return ret;
842e098bc96SEvan Quan 	}
843e098bc96SEvan Quan 
844e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
845e098bc96SEvan Quan 		ret = smu_od_edit_dpm_table(&adev->smu, type,
846e098bc96SEvan Quan 					    parameter, parameter_size);
847e098bc96SEvan Quan 
848e098bc96SEvan Quan 		if (ret) {
849e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
850e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
851e098bc96SEvan Quan 			return -EINVAL;
852e098bc96SEvan Quan 		}
853e098bc96SEvan Quan 	} else {
85412a6727dSXiaojian Du 
85512a6727dSXiaojian Du 		if (adev->powerplay.pp_funcs->set_fine_grain_clk_vol) {
85612a6727dSXiaojian Du 			ret = amdgpu_dpm_set_fine_grain_clk_vol(adev, type,
85712a6727dSXiaojian Du 								parameter,
85812a6727dSXiaojian Du 								parameter_size);
85912a6727dSXiaojian Du 			if (ret) {
86012a6727dSXiaojian Du 				pm_runtime_mark_last_busy(ddev->dev);
86112a6727dSXiaojian Du 				pm_runtime_put_autosuspend(ddev->dev);
86212a6727dSXiaojian Du 				return -EINVAL;
86312a6727dSXiaojian Du 			}
86412a6727dSXiaojian Du 		}
86512a6727dSXiaojian Du 
866e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
867e098bc96SEvan Quan 			ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
868e098bc96SEvan Quan 						parameter, parameter_size);
869e098bc96SEvan Quan 			if (ret) {
870e098bc96SEvan Quan 				pm_runtime_mark_last_busy(ddev->dev);
871e098bc96SEvan Quan 				pm_runtime_put_autosuspend(ddev->dev);
872e098bc96SEvan Quan 				return -EINVAL;
873e098bc96SEvan Quan 			}
874e098bc96SEvan Quan 		}
875e098bc96SEvan Quan 
876e098bc96SEvan Quan 		if (type == PP_OD_COMMIT_DPM_TABLE) {
877e098bc96SEvan Quan 			if (adev->powerplay.pp_funcs->dispatch_tasks) {
878e098bc96SEvan Quan 				amdgpu_dpm_dispatch_task(adev,
879e098bc96SEvan Quan 						AMD_PP_TASK_READJUST_POWER_STATE,
880e098bc96SEvan Quan 						NULL);
881e098bc96SEvan Quan 				pm_runtime_mark_last_busy(ddev->dev);
882e098bc96SEvan Quan 				pm_runtime_put_autosuspend(ddev->dev);
883e098bc96SEvan Quan 				return count;
884e098bc96SEvan Quan 			} else {
885e098bc96SEvan Quan 				pm_runtime_mark_last_busy(ddev->dev);
886e098bc96SEvan Quan 				pm_runtime_put_autosuspend(ddev->dev);
887e098bc96SEvan Quan 				return -EINVAL;
888e098bc96SEvan Quan 			}
889e098bc96SEvan Quan 		}
890e098bc96SEvan Quan 	}
891e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
892e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
893e098bc96SEvan Quan 
894e098bc96SEvan Quan 	return count;
895e098bc96SEvan Quan }
896e098bc96SEvan Quan 
897e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
898e098bc96SEvan Quan 		struct device_attribute *attr,
899e098bc96SEvan Quan 		char *buf)
900e098bc96SEvan Quan {
901e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9021348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
903e098bc96SEvan Quan 	ssize_t size;
904e098bc96SEvan Quan 	int ret;
905e098bc96SEvan Quan 
90653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
907e098bc96SEvan Quan 		return -EPERM;
908e098bc96SEvan Quan 
909e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
910e098bc96SEvan Quan 	if (ret < 0) {
911e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
912e098bc96SEvan Quan 		return ret;
913e098bc96SEvan Quan 	}
914e098bc96SEvan Quan 
915e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
916e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf);
917e098bc96SEvan Quan 		size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size);
918e098bc96SEvan Quan 		size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size);
919a2b6df4fSEvan Quan 		size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDGFX_OFFSET, buf+size);
920e098bc96SEvan Quan 		size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size);
9210d90d0ddSHuang Rui 		size += smu_print_clk_levels(&adev->smu, SMU_OD_CCLK, buf+size);
922e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->print_clock_levels) {
923e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
924e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
925e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
926e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
927e098bc96SEvan Quan 	} else {
928e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
929e098bc96SEvan Quan 	}
930e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
931e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
932e098bc96SEvan Quan 
933e098bc96SEvan Quan 	return size;
934e098bc96SEvan Quan }
935e098bc96SEvan Quan 
936e098bc96SEvan Quan /**
937e098bc96SEvan Quan  * DOC: pp_features
938e098bc96SEvan Quan  *
939e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
940e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
941e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
942e098bc96SEvan Quan  *
943e098bc96SEvan Quan  * Reading back the file will show you the followings:
944e098bc96SEvan Quan  * - Current ppfeature masks
945e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
946e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
947e098bc96SEvan Quan  *
948e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
949e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
950e098bc96SEvan Quan  * new ppfeature masks.
951e098bc96SEvan Quan  */
952e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
953e098bc96SEvan Quan 				      struct device_attribute *attr,
954e098bc96SEvan Quan 				      const char *buf,
955e098bc96SEvan Quan 				      size_t count)
956e098bc96SEvan Quan {
957e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9581348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
959e098bc96SEvan Quan 	uint64_t featuremask;
960e098bc96SEvan Quan 	int ret;
961e098bc96SEvan Quan 
96253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
963e098bc96SEvan Quan 		return -EPERM;
964e098bc96SEvan Quan 
965e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
966e098bc96SEvan Quan 	if (ret)
967e098bc96SEvan Quan 		return -EINVAL;
968e098bc96SEvan Quan 
969e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
970e098bc96SEvan Quan 	if (ret < 0) {
971e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
972e098bc96SEvan Quan 		return ret;
973e098bc96SEvan Quan 	}
974e098bc96SEvan Quan 
975e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
976e098bc96SEvan Quan 		ret = smu_sys_set_pp_feature_mask(&adev->smu, featuremask);
977e098bc96SEvan Quan 		if (ret) {
978e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
979e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
980e098bc96SEvan Quan 			return -EINVAL;
981e098bc96SEvan Quan 		}
982e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
983e098bc96SEvan Quan 		ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
984e098bc96SEvan Quan 		if (ret) {
985e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
986e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
987e098bc96SEvan Quan 			return -EINVAL;
988e098bc96SEvan Quan 		}
989e098bc96SEvan Quan 	}
990e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
991e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
992e098bc96SEvan Quan 
993e098bc96SEvan Quan 	return count;
994e098bc96SEvan Quan }
995e098bc96SEvan Quan 
996e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
997e098bc96SEvan Quan 				      struct device_attribute *attr,
998e098bc96SEvan Quan 				      char *buf)
999e098bc96SEvan Quan {
1000e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10011348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1002e098bc96SEvan Quan 	ssize_t size;
1003e098bc96SEvan Quan 	int ret;
1004e098bc96SEvan Quan 
100553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1006e098bc96SEvan Quan 		return -EPERM;
1007e098bc96SEvan Quan 
1008e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1009e098bc96SEvan Quan 	if (ret < 0) {
1010e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1011e098bc96SEvan Quan 		return ret;
1012e098bc96SEvan Quan 	}
1013e098bc96SEvan Quan 
1014e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1015e098bc96SEvan Quan 		size = smu_sys_get_pp_feature_mask(&adev->smu, buf);
1016e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_ppfeature_status)
1017e098bc96SEvan Quan 		size = amdgpu_dpm_get_ppfeature_status(adev, buf);
1018e098bc96SEvan Quan 	else
1019e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1020e098bc96SEvan Quan 
1021e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1022e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1023e098bc96SEvan Quan 
1024e098bc96SEvan Quan 	return size;
1025e098bc96SEvan Quan }
1026e098bc96SEvan Quan 
1027e098bc96SEvan Quan /**
1028e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
1029e098bc96SEvan Quan  *
1030e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
1031e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
1032e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
1033e098bc96SEvan Quan  * this.
1034e098bc96SEvan Quan  *
1035e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
1036e098bc96SEvan Quan  * Vega10 and later ASICs.
1037e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
1038e098bc96SEvan Quan  *
1039e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
1040e098bc96SEvan Quan  * the power state and the clock information for those levels.
1041e098bc96SEvan Quan  *
1042e098bc96SEvan Quan  * To manually adjust these states, first select manual using
1043e098bc96SEvan Quan  * power_dpm_force_performance_level.
1044e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
1045e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1046e098bc96SEvan Quan  * E.g.,
1047e098bc96SEvan Quan  *
1048e098bc96SEvan Quan  * .. code-block:: bash
1049e098bc96SEvan Quan  *
1050e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
1051e098bc96SEvan Quan  *
1052e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
1053e098bc96SEvan Quan  *
1054e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1055e098bc96SEvan Quan  */
1056e098bc96SEvan Quan 
1057e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1058e098bc96SEvan Quan 		struct device_attribute *attr,
1059e098bc96SEvan Quan 		char *buf)
1060e098bc96SEvan Quan {
1061e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10621348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1063e098bc96SEvan Quan 	ssize_t size;
1064e098bc96SEvan Quan 	int ret;
1065e098bc96SEvan Quan 
106653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1067e098bc96SEvan Quan 		return -EPERM;
1068e098bc96SEvan Quan 
1069e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1070e098bc96SEvan Quan 	if (ret < 0) {
1071e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1072e098bc96SEvan Quan 		return ret;
1073e098bc96SEvan Quan 	}
1074e098bc96SEvan Quan 
1075e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1076e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_SCLK, buf);
1077e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1078e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
1079e098bc96SEvan Quan 	else
1080e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1081e098bc96SEvan Quan 
1082e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1083e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1084e098bc96SEvan Quan 
1085e098bc96SEvan Quan 	return size;
1086e098bc96SEvan Quan }
1087e098bc96SEvan Quan 
1088e098bc96SEvan Quan /*
1089e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1090e098bc96SEvan Quan  * per line (+1 for \n).
1091e098bc96SEvan Quan  */
1092e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1093e098bc96SEvan Quan 
1094e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1095e098bc96SEvan Quan {
1096e098bc96SEvan Quan 	int ret;
1097e098bc96SEvan Quan 	long level;
1098e098bc96SEvan Quan 	char *sub_str = NULL;
1099e098bc96SEvan Quan 	char *tmp;
1100e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1101e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1102e098bc96SEvan Quan 	size_t bytes;
1103e098bc96SEvan Quan 
1104e098bc96SEvan Quan 	*mask = 0;
1105e098bc96SEvan Quan 
1106e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1107e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1108e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1109e098bc96SEvan Quan 	tmp = buf_cpy;
1110e098bc96SEvan Quan 	while (tmp[0]) {
1111e098bc96SEvan Quan 		sub_str = strsep(&tmp, delimiter);
1112e098bc96SEvan Quan 		if (strlen(sub_str)) {
1113e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &level);
1114e098bc96SEvan Quan 			if (ret)
1115e098bc96SEvan Quan 				return -EINVAL;
1116e098bc96SEvan Quan 			*mask |= 1 << level;
1117e098bc96SEvan Quan 		} else
1118e098bc96SEvan Quan 			break;
1119e098bc96SEvan Quan 	}
1120e098bc96SEvan Quan 
1121e098bc96SEvan Quan 	return 0;
1122e098bc96SEvan Quan }
1123e098bc96SEvan Quan 
1124e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1125e098bc96SEvan Quan 		struct device_attribute *attr,
1126e098bc96SEvan Quan 		const char *buf,
1127e098bc96SEvan Quan 		size_t count)
1128e098bc96SEvan Quan {
1129e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
11301348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1131e098bc96SEvan Quan 	int ret;
1132e098bc96SEvan Quan 	uint32_t mask = 0;
1133e098bc96SEvan Quan 
113453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1135e098bc96SEvan Quan 		return -EPERM;
1136e098bc96SEvan Quan 
1137e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1138e098bc96SEvan Quan 	if (ret)
1139e098bc96SEvan Quan 		return ret;
1140e098bc96SEvan Quan 
1141e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1142e098bc96SEvan Quan 	if (ret < 0) {
1143e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1144e098bc96SEvan Quan 		return ret;
1145e098bc96SEvan Quan 	}
1146e098bc96SEvan Quan 
1147e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1148e098bc96SEvan Quan 		ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask);
1149e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->force_clock_level)
1150e098bc96SEvan Quan 		ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
1151e098bc96SEvan Quan 
1152e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1153e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1154e098bc96SEvan Quan 
1155e098bc96SEvan Quan 	if (ret)
1156e098bc96SEvan Quan 		return -EINVAL;
1157e098bc96SEvan Quan 
1158e098bc96SEvan Quan 	return count;
1159e098bc96SEvan Quan }
1160e098bc96SEvan Quan 
1161e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1162e098bc96SEvan Quan 		struct device_attribute *attr,
1163e098bc96SEvan Quan 		char *buf)
1164e098bc96SEvan Quan {
1165e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
11661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1167e098bc96SEvan Quan 	ssize_t size;
1168e098bc96SEvan Quan 	int ret;
1169e098bc96SEvan Quan 
117053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1171e098bc96SEvan Quan 		return -EPERM;
1172e098bc96SEvan Quan 
1173e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1174e098bc96SEvan Quan 	if (ret < 0) {
1175e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1176e098bc96SEvan Quan 		return ret;
1177e098bc96SEvan Quan 	}
1178e098bc96SEvan Quan 
1179e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1180e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_MCLK, buf);
1181e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1182e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
1183e098bc96SEvan Quan 	else
1184e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1185e098bc96SEvan Quan 
1186e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1187e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1188e098bc96SEvan Quan 
1189e098bc96SEvan Quan 	return size;
1190e098bc96SEvan Quan }
1191e098bc96SEvan Quan 
1192e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1193e098bc96SEvan Quan 		struct device_attribute *attr,
1194e098bc96SEvan Quan 		const char *buf,
1195e098bc96SEvan Quan 		size_t count)
1196e098bc96SEvan Quan {
1197e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
11981348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1199e098bc96SEvan Quan 	uint32_t mask = 0;
1200e098bc96SEvan Quan 	int ret;
1201e098bc96SEvan Quan 
120253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1203e098bc96SEvan Quan 		return -EPERM;
1204e098bc96SEvan Quan 
1205e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1206e098bc96SEvan Quan 	if (ret)
1207e098bc96SEvan Quan 		return ret;
1208e098bc96SEvan Quan 
1209e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1210e098bc96SEvan Quan 	if (ret < 0) {
1211e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1212e098bc96SEvan Quan 		return ret;
1213e098bc96SEvan Quan 	}
1214e098bc96SEvan Quan 
1215e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1216e098bc96SEvan Quan 		ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask);
1217e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->force_clock_level)
1218e098bc96SEvan Quan 		ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
1219e098bc96SEvan Quan 
1220e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1221e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1222e098bc96SEvan Quan 
1223e098bc96SEvan Quan 	if (ret)
1224e098bc96SEvan Quan 		return -EINVAL;
1225e098bc96SEvan Quan 
1226e098bc96SEvan Quan 	return count;
1227e098bc96SEvan Quan }
1228e098bc96SEvan Quan 
1229e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1230e098bc96SEvan Quan 		struct device_attribute *attr,
1231e098bc96SEvan Quan 		char *buf)
1232e098bc96SEvan Quan {
1233e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12341348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1235e098bc96SEvan Quan 	ssize_t size;
1236e098bc96SEvan Quan 	int ret;
1237e098bc96SEvan Quan 
123853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1239e098bc96SEvan Quan 		return -EPERM;
1240e098bc96SEvan Quan 
1241e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1242e098bc96SEvan Quan 	if (ret < 0) {
1243e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1244e098bc96SEvan Quan 		return ret;
1245e098bc96SEvan Quan 	}
1246e098bc96SEvan Quan 
1247e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1248e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf);
1249e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1250e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
1251e098bc96SEvan Quan 	else
1252e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1253e098bc96SEvan Quan 
1254e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1255e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1256e098bc96SEvan Quan 
1257e098bc96SEvan Quan 	return size;
1258e098bc96SEvan Quan }
1259e098bc96SEvan Quan 
1260e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1261e098bc96SEvan Quan 		struct device_attribute *attr,
1262e098bc96SEvan Quan 		const char *buf,
1263e098bc96SEvan Quan 		size_t count)
1264e098bc96SEvan Quan {
1265e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1267e098bc96SEvan Quan 	int ret;
1268e098bc96SEvan Quan 	uint32_t mask = 0;
1269e098bc96SEvan Quan 
127053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1271e098bc96SEvan Quan 		return -EPERM;
1272e098bc96SEvan Quan 
1273e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1274e098bc96SEvan Quan 	if (ret)
1275e098bc96SEvan Quan 		return ret;
1276e098bc96SEvan Quan 
1277e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1278e098bc96SEvan Quan 	if (ret < 0) {
1279e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1280e098bc96SEvan Quan 		return ret;
1281e098bc96SEvan Quan 	}
1282e098bc96SEvan Quan 
1283e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1284e098bc96SEvan Quan 		ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask);
1285e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->force_clock_level)
1286e098bc96SEvan Quan 		ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
1287e098bc96SEvan Quan 	else
1288e098bc96SEvan Quan 		ret = 0;
1289e098bc96SEvan Quan 
1290e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1291e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1292e098bc96SEvan Quan 
1293e098bc96SEvan Quan 	if (ret)
1294e098bc96SEvan Quan 		return -EINVAL;
1295e098bc96SEvan Quan 
1296e098bc96SEvan Quan 	return count;
1297e098bc96SEvan Quan }
1298e098bc96SEvan Quan 
1299e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1300e098bc96SEvan Quan 		struct device_attribute *attr,
1301e098bc96SEvan Quan 		char *buf)
1302e098bc96SEvan Quan {
1303e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13041348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1305e098bc96SEvan Quan 	ssize_t size;
1306e098bc96SEvan Quan 	int ret;
1307e098bc96SEvan Quan 
130853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1309e098bc96SEvan Quan 		return -EPERM;
1310e098bc96SEvan Quan 
1311e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1312e098bc96SEvan Quan 	if (ret < 0) {
1313e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1314e098bc96SEvan Quan 		return ret;
1315e098bc96SEvan Quan 	}
1316e098bc96SEvan Quan 
1317e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1318e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_FCLK, buf);
1319e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1320e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
1321e098bc96SEvan Quan 	else
1322e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1323e098bc96SEvan Quan 
1324e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1325e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1326e098bc96SEvan Quan 
1327e098bc96SEvan Quan 	return size;
1328e098bc96SEvan Quan }
1329e098bc96SEvan Quan 
1330e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1331e098bc96SEvan Quan 		struct device_attribute *attr,
1332e098bc96SEvan Quan 		const char *buf,
1333e098bc96SEvan Quan 		size_t count)
1334e098bc96SEvan Quan {
1335e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13361348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1337e098bc96SEvan Quan 	int ret;
1338e098bc96SEvan Quan 	uint32_t mask = 0;
1339e098bc96SEvan Quan 
134053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1341e098bc96SEvan Quan 		return -EPERM;
1342e098bc96SEvan Quan 
1343e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1344e098bc96SEvan Quan 	if (ret)
1345e098bc96SEvan Quan 		return ret;
1346e098bc96SEvan Quan 
1347e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1348e098bc96SEvan Quan 	if (ret < 0) {
1349e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1350e098bc96SEvan Quan 		return ret;
1351e098bc96SEvan Quan 	}
1352e098bc96SEvan Quan 
1353e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1354e098bc96SEvan Quan 		ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask);
1355e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->force_clock_level)
1356e098bc96SEvan Quan 		ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1357e098bc96SEvan Quan 	else
1358e098bc96SEvan Quan 		ret = 0;
1359e098bc96SEvan Quan 
1360e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1361e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1362e098bc96SEvan Quan 
1363e098bc96SEvan Quan 	if (ret)
1364e098bc96SEvan Quan 		return -EINVAL;
1365e098bc96SEvan Quan 
1366e098bc96SEvan Quan 	return count;
1367e098bc96SEvan Quan }
1368e098bc96SEvan Quan 
13699577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
13709577b0ecSXiaojian Du 		struct device_attribute *attr,
13719577b0ecSXiaojian Du 		char *buf)
13729577b0ecSXiaojian Du {
13739577b0ecSXiaojian Du 	struct drm_device *ddev = dev_get_drvdata(dev);
13749577b0ecSXiaojian Du 	struct amdgpu_device *adev = drm_to_adev(ddev);
13759577b0ecSXiaojian Du 	ssize_t size;
13769577b0ecSXiaojian Du 	int ret;
13779577b0ecSXiaojian Du 
13789577b0ecSXiaojian Du 	if (amdgpu_in_reset(adev))
13799577b0ecSXiaojian Du 		return -EPERM;
13809577b0ecSXiaojian Du 
13819577b0ecSXiaojian Du 	ret = pm_runtime_get_sync(ddev->dev);
13829577b0ecSXiaojian Du 	if (ret < 0) {
13839577b0ecSXiaojian Du 		pm_runtime_put_autosuspend(ddev->dev);
13849577b0ecSXiaojian Du 		return ret;
13859577b0ecSXiaojian Du 	}
13869577b0ecSXiaojian Du 
13879577b0ecSXiaojian Du 	if (is_support_sw_smu(adev))
13889577b0ecSXiaojian Du 		size = smu_print_clk_levels(&adev->smu, SMU_VCLK, buf);
13899577b0ecSXiaojian Du 	else
13909577b0ecSXiaojian Du 		size = snprintf(buf, PAGE_SIZE, "\n");
13919577b0ecSXiaojian Du 
13929577b0ecSXiaojian Du 	pm_runtime_mark_last_busy(ddev->dev);
13939577b0ecSXiaojian Du 	pm_runtime_put_autosuspend(ddev->dev);
13949577b0ecSXiaojian Du 
13959577b0ecSXiaojian Du 	return size;
13969577b0ecSXiaojian Du }
13979577b0ecSXiaojian Du 
13989577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
13999577b0ecSXiaojian Du 		struct device_attribute *attr,
14009577b0ecSXiaojian Du 		const char *buf,
14019577b0ecSXiaojian Du 		size_t count)
14029577b0ecSXiaojian Du {
14039577b0ecSXiaojian Du 	struct drm_device *ddev = dev_get_drvdata(dev);
14049577b0ecSXiaojian Du 	struct amdgpu_device *adev = drm_to_adev(ddev);
14059577b0ecSXiaojian Du 	int ret;
14069577b0ecSXiaojian Du 	uint32_t mask = 0;
14079577b0ecSXiaojian Du 
14089577b0ecSXiaojian Du 	if (amdgpu_in_reset(adev))
14099577b0ecSXiaojian Du 		return -EPERM;
14109577b0ecSXiaojian Du 
14119577b0ecSXiaojian Du 	ret = amdgpu_read_mask(buf, count, &mask);
14129577b0ecSXiaojian Du 	if (ret)
14139577b0ecSXiaojian Du 		return ret;
14149577b0ecSXiaojian Du 
14159577b0ecSXiaojian Du 	ret = pm_runtime_get_sync(ddev->dev);
14169577b0ecSXiaojian Du 	if (ret < 0) {
14179577b0ecSXiaojian Du 		pm_runtime_put_autosuspend(ddev->dev);
14189577b0ecSXiaojian Du 		return ret;
14199577b0ecSXiaojian Du 	}
14209577b0ecSXiaojian Du 
14219577b0ecSXiaojian Du 	if (is_support_sw_smu(adev))
14229577b0ecSXiaojian Du 		ret = smu_force_clk_levels(&adev->smu, SMU_VCLK, mask);
14239577b0ecSXiaojian Du 	else
14249577b0ecSXiaojian Du 		ret = 0;
14259577b0ecSXiaojian Du 
14269577b0ecSXiaojian Du 	pm_runtime_mark_last_busy(ddev->dev);
14279577b0ecSXiaojian Du 	pm_runtime_put_autosuspend(ddev->dev);
14289577b0ecSXiaojian Du 
14299577b0ecSXiaojian Du 	if (ret)
14309577b0ecSXiaojian Du 		return -EINVAL;
14319577b0ecSXiaojian Du 
14329577b0ecSXiaojian Du 	return count;
14339577b0ecSXiaojian Du }
14349577b0ecSXiaojian Du 
14359577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
14369577b0ecSXiaojian Du 		struct device_attribute *attr,
14379577b0ecSXiaojian Du 		char *buf)
14389577b0ecSXiaojian Du {
14399577b0ecSXiaojian Du 	struct drm_device *ddev = dev_get_drvdata(dev);
14409577b0ecSXiaojian Du 	struct amdgpu_device *adev = drm_to_adev(ddev);
14419577b0ecSXiaojian Du 	ssize_t size;
14429577b0ecSXiaojian Du 	int ret;
14439577b0ecSXiaojian Du 
14449577b0ecSXiaojian Du 	if (amdgpu_in_reset(adev))
14459577b0ecSXiaojian Du 		return -EPERM;
14469577b0ecSXiaojian Du 
14479577b0ecSXiaojian Du 	ret = pm_runtime_get_sync(ddev->dev);
14489577b0ecSXiaojian Du 	if (ret < 0) {
14499577b0ecSXiaojian Du 		pm_runtime_put_autosuspend(ddev->dev);
14509577b0ecSXiaojian Du 		return ret;
14519577b0ecSXiaojian Du 	}
14529577b0ecSXiaojian Du 
14539577b0ecSXiaojian Du 	if (is_support_sw_smu(adev))
14549577b0ecSXiaojian Du 		size = smu_print_clk_levels(&adev->smu, SMU_DCLK, buf);
14559577b0ecSXiaojian Du 	else
14569577b0ecSXiaojian Du 		size = snprintf(buf, PAGE_SIZE, "\n");
14579577b0ecSXiaojian Du 
14589577b0ecSXiaojian Du 	pm_runtime_mark_last_busy(ddev->dev);
14599577b0ecSXiaojian Du 	pm_runtime_put_autosuspend(ddev->dev);
14609577b0ecSXiaojian Du 
14619577b0ecSXiaojian Du 	return size;
14629577b0ecSXiaojian Du }
14639577b0ecSXiaojian Du 
14649577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
14659577b0ecSXiaojian Du 		struct device_attribute *attr,
14669577b0ecSXiaojian Du 		const char *buf,
14679577b0ecSXiaojian Du 		size_t count)
14689577b0ecSXiaojian Du {
14699577b0ecSXiaojian Du 	struct drm_device *ddev = dev_get_drvdata(dev);
14709577b0ecSXiaojian Du 	struct amdgpu_device *adev = drm_to_adev(ddev);
14719577b0ecSXiaojian Du 	int ret;
14729577b0ecSXiaojian Du 	uint32_t mask = 0;
14739577b0ecSXiaojian Du 
14749577b0ecSXiaojian Du 	if (amdgpu_in_reset(adev))
14759577b0ecSXiaojian Du 		return -EPERM;
14769577b0ecSXiaojian Du 
14779577b0ecSXiaojian Du 	ret = amdgpu_read_mask(buf, count, &mask);
14789577b0ecSXiaojian Du 	if (ret)
14799577b0ecSXiaojian Du 		return ret;
14809577b0ecSXiaojian Du 
14819577b0ecSXiaojian Du 	ret = pm_runtime_get_sync(ddev->dev);
14829577b0ecSXiaojian Du 	if (ret < 0) {
14839577b0ecSXiaojian Du 		pm_runtime_put_autosuspend(ddev->dev);
14849577b0ecSXiaojian Du 		return ret;
14859577b0ecSXiaojian Du 	}
14869577b0ecSXiaojian Du 
14879577b0ecSXiaojian Du 	if (is_support_sw_smu(adev))
14889577b0ecSXiaojian Du 		ret = smu_force_clk_levels(&adev->smu, SMU_DCLK, mask);
14899577b0ecSXiaojian Du 	else
14909577b0ecSXiaojian Du 		ret = 0;
14919577b0ecSXiaojian Du 
14929577b0ecSXiaojian Du 	pm_runtime_mark_last_busy(ddev->dev);
14939577b0ecSXiaojian Du 	pm_runtime_put_autosuspend(ddev->dev);
14949577b0ecSXiaojian Du 
14959577b0ecSXiaojian Du 	if (ret)
14969577b0ecSXiaojian Du 		return -EINVAL;
14979577b0ecSXiaojian Du 
14989577b0ecSXiaojian Du 	return count;
14999577b0ecSXiaojian Du }
15009577b0ecSXiaojian Du 
1501e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1502e098bc96SEvan Quan 		struct device_attribute *attr,
1503e098bc96SEvan Quan 		char *buf)
1504e098bc96SEvan Quan {
1505e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15061348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1507e098bc96SEvan Quan 	ssize_t size;
1508e098bc96SEvan Quan 	int ret;
1509e098bc96SEvan Quan 
151053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1511e098bc96SEvan Quan 		return -EPERM;
1512e098bc96SEvan Quan 
1513e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1514e098bc96SEvan Quan 	if (ret < 0) {
1515e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1516e098bc96SEvan Quan 		return ret;
1517e098bc96SEvan Quan 	}
1518e098bc96SEvan Quan 
1519e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1520e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf);
1521e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1522e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1523e098bc96SEvan Quan 	else
1524e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1525e098bc96SEvan Quan 
1526e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1527e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1528e098bc96SEvan Quan 
1529e098bc96SEvan Quan 	return size;
1530e098bc96SEvan Quan }
1531e098bc96SEvan Quan 
1532e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1533e098bc96SEvan Quan 		struct device_attribute *attr,
1534e098bc96SEvan Quan 		const char *buf,
1535e098bc96SEvan Quan 		size_t count)
1536e098bc96SEvan Quan {
1537e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15381348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1539e098bc96SEvan Quan 	int ret;
1540e098bc96SEvan Quan 	uint32_t mask = 0;
1541e098bc96SEvan Quan 
154253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1543e098bc96SEvan Quan 		return -EPERM;
1544e098bc96SEvan Quan 
1545e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1546e098bc96SEvan Quan 	if (ret)
1547e098bc96SEvan Quan 		return ret;
1548e098bc96SEvan Quan 
1549e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1550e098bc96SEvan Quan 	if (ret < 0) {
1551e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1552e098bc96SEvan Quan 		return ret;
1553e098bc96SEvan Quan 	}
1554e098bc96SEvan Quan 
1555e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1556e098bc96SEvan Quan 		ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask);
1557e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->force_clock_level)
1558e098bc96SEvan Quan 		ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1559e098bc96SEvan Quan 	else
1560e098bc96SEvan Quan 		ret = 0;
1561e098bc96SEvan Quan 
1562e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1563e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1564e098bc96SEvan Quan 
1565e098bc96SEvan Quan 	if (ret)
1566e098bc96SEvan Quan 		return -EINVAL;
1567e098bc96SEvan Quan 
1568e098bc96SEvan Quan 	return count;
1569e098bc96SEvan Quan }
1570e098bc96SEvan Quan 
1571e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1572e098bc96SEvan Quan 		struct device_attribute *attr,
1573e098bc96SEvan Quan 		char *buf)
1574e098bc96SEvan Quan {
1575e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15761348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1577e098bc96SEvan Quan 	ssize_t size;
1578e098bc96SEvan Quan 	int ret;
1579e098bc96SEvan Quan 
158053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1581e098bc96SEvan Quan 		return -EPERM;
1582e098bc96SEvan Quan 
1583e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1584e098bc96SEvan Quan 	if (ret < 0) {
1585e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1586e098bc96SEvan Quan 		return ret;
1587e098bc96SEvan Quan 	}
1588e098bc96SEvan Quan 
1589e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1590e098bc96SEvan Quan 		size = smu_print_clk_levels(&adev->smu, SMU_PCIE, buf);
1591e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1592e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1593e098bc96SEvan Quan 	else
1594e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1595e098bc96SEvan Quan 
1596e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1597e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1598e098bc96SEvan Quan 
1599e098bc96SEvan Quan 	return size;
1600e098bc96SEvan Quan }
1601e098bc96SEvan Quan 
1602e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1603e098bc96SEvan Quan 		struct device_attribute *attr,
1604e098bc96SEvan Quan 		const char *buf,
1605e098bc96SEvan Quan 		size_t count)
1606e098bc96SEvan Quan {
1607e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16081348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1609e098bc96SEvan Quan 	int ret;
1610e098bc96SEvan Quan 	uint32_t mask = 0;
1611e098bc96SEvan Quan 
161253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1613e098bc96SEvan Quan 		return -EPERM;
1614e098bc96SEvan Quan 
1615e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1616e098bc96SEvan Quan 	if (ret)
1617e098bc96SEvan Quan 		return ret;
1618e098bc96SEvan Quan 
1619e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1620e098bc96SEvan Quan 	if (ret < 0) {
1621e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1622e098bc96SEvan Quan 		return ret;
1623e098bc96SEvan Quan 	}
1624e098bc96SEvan Quan 
1625e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1626e098bc96SEvan Quan 		ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask);
1627e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->force_clock_level)
1628e098bc96SEvan Quan 		ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1629e098bc96SEvan Quan 	else
1630e098bc96SEvan Quan 		ret = 0;
1631e098bc96SEvan Quan 
1632e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1633e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1634e098bc96SEvan Quan 
1635e098bc96SEvan Quan 	if (ret)
1636e098bc96SEvan Quan 		return -EINVAL;
1637e098bc96SEvan Quan 
1638e098bc96SEvan Quan 	return count;
1639e098bc96SEvan Quan }
1640e098bc96SEvan Quan 
1641e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1642e098bc96SEvan Quan 		struct device_attribute *attr,
1643e098bc96SEvan Quan 		char *buf)
1644e098bc96SEvan Quan {
1645e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16461348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1647e098bc96SEvan Quan 	uint32_t value = 0;
1648e098bc96SEvan Quan 	int ret;
1649e098bc96SEvan Quan 
165053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1651e098bc96SEvan Quan 		return -EPERM;
1652e098bc96SEvan Quan 
1653e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1654e098bc96SEvan Quan 	if (ret < 0) {
1655e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1656e098bc96SEvan Quan 		return ret;
1657e098bc96SEvan Quan 	}
1658e098bc96SEvan Quan 
1659e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
166075145aabSAlex Deucher 		value = 0;
1661e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_sclk_od)
1662e098bc96SEvan Quan 		value = amdgpu_dpm_get_sclk_od(adev);
1663e098bc96SEvan Quan 
1664e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1665e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1666e098bc96SEvan Quan 
1667e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1668e098bc96SEvan Quan }
1669e098bc96SEvan Quan 
1670e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1671e098bc96SEvan Quan 		struct device_attribute *attr,
1672e098bc96SEvan Quan 		const char *buf,
1673e098bc96SEvan Quan 		size_t count)
1674e098bc96SEvan Quan {
1675e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16761348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1677e098bc96SEvan Quan 	int ret;
1678e098bc96SEvan Quan 	long int value;
1679e098bc96SEvan Quan 
168053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1681e098bc96SEvan Quan 		return -EPERM;
1682e098bc96SEvan Quan 
1683e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1684e098bc96SEvan Quan 
1685e098bc96SEvan Quan 	if (ret)
1686e098bc96SEvan Quan 		return -EINVAL;
1687e098bc96SEvan Quan 
1688e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1689e098bc96SEvan Quan 	if (ret < 0) {
1690e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1691e098bc96SEvan Quan 		return ret;
1692e098bc96SEvan Quan 	}
1693e098bc96SEvan Quan 
1694e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
169575145aabSAlex Deucher 		value = 0;
1696e098bc96SEvan Quan 	} else {
1697e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->set_sclk_od)
1698e098bc96SEvan Quan 			amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1699e098bc96SEvan Quan 
1700e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
1701e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1702e098bc96SEvan Quan 		} else {
1703e098bc96SEvan Quan 			adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1704e098bc96SEvan Quan 			amdgpu_pm_compute_clocks(adev);
1705e098bc96SEvan Quan 		}
1706e098bc96SEvan Quan 	}
1707e098bc96SEvan Quan 
1708e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1709e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1710e098bc96SEvan Quan 
1711e098bc96SEvan Quan 	return count;
1712e098bc96SEvan Quan }
1713e098bc96SEvan Quan 
1714e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1715e098bc96SEvan Quan 		struct device_attribute *attr,
1716e098bc96SEvan Quan 		char *buf)
1717e098bc96SEvan Quan {
1718e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17191348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1720e098bc96SEvan Quan 	uint32_t value = 0;
1721e098bc96SEvan Quan 	int ret;
1722e098bc96SEvan Quan 
172353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1724e098bc96SEvan Quan 		return -EPERM;
1725e098bc96SEvan Quan 
1726e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1727e098bc96SEvan Quan 	if (ret < 0) {
1728e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1729e098bc96SEvan Quan 		return ret;
1730e098bc96SEvan Quan 	}
1731e098bc96SEvan Quan 
1732e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
173375145aabSAlex Deucher 		value = 0;
1734e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_mclk_od)
1735e098bc96SEvan Quan 		value = amdgpu_dpm_get_mclk_od(adev);
1736e098bc96SEvan Quan 
1737e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1738e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1739e098bc96SEvan Quan 
1740e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1741e098bc96SEvan Quan }
1742e098bc96SEvan Quan 
1743e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1744e098bc96SEvan Quan 		struct device_attribute *attr,
1745e098bc96SEvan Quan 		const char *buf,
1746e098bc96SEvan Quan 		size_t count)
1747e098bc96SEvan Quan {
1748e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17491348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1750e098bc96SEvan Quan 	int ret;
1751e098bc96SEvan Quan 	long int value;
1752e098bc96SEvan Quan 
175353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1754e098bc96SEvan Quan 		return -EPERM;
1755e098bc96SEvan Quan 
1756e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1757e098bc96SEvan Quan 
1758e098bc96SEvan Quan 	if (ret)
1759e098bc96SEvan Quan 		return -EINVAL;
1760e098bc96SEvan Quan 
1761e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1762e098bc96SEvan Quan 	if (ret < 0) {
1763e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1764e098bc96SEvan Quan 		return ret;
1765e098bc96SEvan Quan 	}
1766e098bc96SEvan Quan 
1767e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
176875145aabSAlex Deucher 		value = 0;
1769e098bc96SEvan Quan 	} else {
1770e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->set_mclk_od)
1771e098bc96SEvan Quan 			amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1772e098bc96SEvan Quan 
1773e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
1774e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1775e098bc96SEvan Quan 		} else {
1776e098bc96SEvan Quan 			adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1777e098bc96SEvan Quan 			amdgpu_pm_compute_clocks(adev);
1778e098bc96SEvan Quan 		}
1779e098bc96SEvan Quan 	}
1780e098bc96SEvan Quan 
1781e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1782e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1783e098bc96SEvan Quan 
1784e098bc96SEvan Quan 	return count;
1785e098bc96SEvan Quan }
1786e098bc96SEvan Quan 
1787e098bc96SEvan Quan /**
1788e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1789e098bc96SEvan Quan  *
1790e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1791e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1792e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1793e098bc96SEvan Quan  *
1794e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1795e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1796e098bc96SEvan Quan  *
1797e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1798e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1799e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1800e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1801e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1802e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1803e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1804e098bc96SEvan Quan  *
1805e098bc96SEvan Quan  */
1806e098bc96SEvan Quan 
1807e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1808e098bc96SEvan Quan 		struct device_attribute *attr,
1809e098bc96SEvan Quan 		char *buf)
1810e098bc96SEvan Quan {
1811e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
18121348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1813e098bc96SEvan Quan 	ssize_t size;
1814e098bc96SEvan Quan 	int ret;
1815e098bc96SEvan Quan 
181653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1817e098bc96SEvan Quan 		return -EPERM;
1818e098bc96SEvan Quan 
1819e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1820e098bc96SEvan Quan 	if (ret < 0) {
1821e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1822e098bc96SEvan Quan 		return ret;
1823e098bc96SEvan Quan 	}
1824e098bc96SEvan Quan 
1825e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1826e098bc96SEvan Quan 		size = smu_get_power_profile_mode(&adev->smu, buf);
1827e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1828e098bc96SEvan Quan 		size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1829e098bc96SEvan Quan 	else
1830e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
1831e098bc96SEvan Quan 
1832e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1833e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1834e098bc96SEvan Quan 
1835e098bc96SEvan Quan 	return size;
1836e098bc96SEvan Quan }
1837e098bc96SEvan Quan 
1838e098bc96SEvan Quan 
1839e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1840e098bc96SEvan Quan 		struct device_attribute *attr,
1841e098bc96SEvan Quan 		const char *buf,
1842e098bc96SEvan Quan 		size_t count)
1843e098bc96SEvan Quan {
1844e098bc96SEvan Quan 	int ret;
1845e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
18461348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1847e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1848e098bc96SEvan Quan 	long parameter[64];
1849e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1850e098bc96SEvan Quan 	char *tmp_str;
1851e098bc96SEvan Quan 	uint32_t i = 0;
1852e098bc96SEvan Quan 	char tmp[2];
1853e098bc96SEvan Quan 	long int profile_mode = 0;
1854e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1855e098bc96SEvan Quan 
185653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1857e098bc96SEvan Quan 		return -EPERM;
1858e098bc96SEvan Quan 
1859e098bc96SEvan Quan 	tmp[0] = *(buf);
1860e098bc96SEvan Quan 	tmp[1] = '\0';
1861e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1862e098bc96SEvan Quan 	if (ret)
1863e098bc96SEvan Quan 		return -EINVAL;
1864e098bc96SEvan Quan 
1865e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1866e098bc96SEvan Quan 		if (count < 2 || count > 127)
1867e098bc96SEvan Quan 			return -EINVAL;
1868e098bc96SEvan Quan 		while (isspace(*++buf))
1869e098bc96SEvan Quan 			i++;
1870e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1871e098bc96SEvan Quan 		tmp_str = buf_cpy;
1872e098bc96SEvan Quan 		while (tmp_str[0]) {
1873e098bc96SEvan Quan 			sub_str = strsep(&tmp_str, delimiter);
1874e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1875e098bc96SEvan Quan 			if (ret)
1876e098bc96SEvan Quan 				return -EINVAL;
1877e098bc96SEvan Quan 			parameter_size++;
1878e098bc96SEvan Quan 			while (isspace(*tmp_str))
1879e098bc96SEvan Quan 				tmp_str++;
1880e098bc96SEvan Quan 		}
1881e098bc96SEvan Quan 	}
1882e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1883e098bc96SEvan Quan 
1884e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1885e098bc96SEvan Quan 	if (ret < 0) {
1886e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1887e098bc96SEvan Quan 		return ret;
1888e098bc96SEvan Quan 	}
1889e098bc96SEvan Quan 
1890e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
1891e098bc96SEvan Quan 		ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size, true);
1892e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1893e098bc96SEvan Quan 		ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1894e098bc96SEvan Quan 
1895e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1896e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1897e098bc96SEvan Quan 
1898e098bc96SEvan Quan 	if (!ret)
1899e098bc96SEvan Quan 		return count;
1900e098bc96SEvan Quan 
1901e098bc96SEvan Quan 	return -EINVAL;
1902e098bc96SEvan Quan }
1903e098bc96SEvan Quan 
1904e098bc96SEvan Quan /**
1905e098bc96SEvan Quan  * DOC: gpu_busy_percent
1906e098bc96SEvan Quan  *
1907e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1908e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1909e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1910e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1911e098bc96SEvan Quan  */
1912e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1913e098bc96SEvan Quan 					   struct device_attribute *attr,
1914e098bc96SEvan Quan 					   char *buf)
1915e098bc96SEvan Quan {
1916e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
19171348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1918e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1919e098bc96SEvan Quan 
192053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1921e098bc96SEvan Quan 		return -EPERM;
1922e098bc96SEvan Quan 
1923e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1924e098bc96SEvan Quan 	if (r < 0) {
1925e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1926e098bc96SEvan Quan 		return r;
1927e098bc96SEvan Quan 	}
1928e098bc96SEvan Quan 
1929e098bc96SEvan Quan 	/* read the IP busy sensor */
1930e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1931e098bc96SEvan Quan 				   (void *)&value, &size);
1932e098bc96SEvan Quan 
1933e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1934e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1935e098bc96SEvan Quan 
1936e098bc96SEvan Quan 	if (r)
1937e098bc96SEvan Quan 		return r;
1938e098bc96SEvan Quan 
1939e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1940e098bc96SEvan Quan }
1941e098bc96SEvan Quan 
1942e098bc96SEvan Quan /**
1943e098bc96SEvan Quan  * DOC: mem_busy_percent
1944e098bc96SEvan Quan  *
1945e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1946e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1947e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1948e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1949e098bc96SEvan Quan  */
1950e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1951e098bc96SEvan Quan 					   struct device_attribute *attr,
1952e098bc96SEvan Quan 					   char *buf)
1953e098bc96SEvan Quan {
1954e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
19551348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1956e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1957e098bc96SEvan Quan 
195853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1959e098bc96SEvan Quan 		return -EPERM;
1960e098bc96SEvan Quan 
1961e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1962e098bc96SEvan Quan 	if (r < 0) {
1963e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1964e098bc96SEvan Quan 		return r;
1965e098bc96SEvan Quan 	}
1966e098bc96SEvan Quan 
1967e098bc96SEvan Quan 	/* read the IP busy sensor */
1968e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1969e098bc96SEvan Quan 				   (void *)&value, &size);
1970e098bc96SEvan Quan 
1971e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1972e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1973e098bc96SEvan Quan 
1974e098bc96SEvan Quan 	if (r)
1975e098bc96SEvan Quan 		return r;
1976e098bc96SEvan Quan 
1977e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1978e098bc96SEvan Quan }
1979e098bc96SEvan Quan 
1980e098bc96SEvan Quan /**
1981e098bc96SEvan Quan  * DOC: pcie_bw
1982e098bc96SEvan Quan  *
1983e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1984e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1985e098bc96SEvan Quan  * The file pcie_bw is used for this.
1986e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1987e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1988e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1989e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1990e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1991e098bc96SEvan Quan  */
1992e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1993e098bc96SEvan Quan 		struct device_attribute *attr,
1994e098bc96SEvan Quan 		char *buf)
1995e098bc96SEvan Quan {
1996e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
19971348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1998e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1999e098bc96SEvan Quan 	int ret;
2000e098bc96SEvan Quan 
200153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2002e098bc96SEvan Quan 		return -EPERM;
2003e098bc96SEvan Quan 
2004e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
2005e098bc96SEvan Quan 		return -ENODATA;
2006e098bc96SEvan Quan 
2007e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
2008e098bc96SEvan Quan 		return -ENODATA;
2009e098bc96SEvan Quan 
2010e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
2011e098bc96SEvan Quan 	if (ret < 0) {
2012e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
2013e098bc96SEvan Quan 		return ret;
2014e098bc96SEvan Quan 	}
2015e098bc96SEvan Quan 
2016e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
2017e098bc96SEvan Quan 
2018e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
2019e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
2020e098bc96SEvan Quan 
2021e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE,	"%llu %llu %i\n",
2022e098bc96SEvan Quan 			count0, count1, pcie_get_mps(adev->pdev));
2023e098bc96SEvan Quan }
2024e098bc96SEvan Quan 
2025e098bc96SEvan Quan /**
2026e098bc96SEvan Quan  * DOC: unique_id
2027e098bc96SEvan Quan  *
2028e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
2029e098bc96SEvan Quan  * The file unique_id is used for this.
2030e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
2031e098bc96SEvan Quan  *
2032e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
2033e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
2034e098bc96SEvan Quan  */
2035e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
2036e098bc96SEvan Quan 		struct device_attribute *attr,
2037e098bc96SEvan Quan 		char *buf)
2038e098bc96SEvan Quan {
2039e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
20401348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
2041e098bc96SEvan Quan 
204253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2043e098bc96SEvan Quan 		return -EPERM;
2044e098bc96SEvan Quan 
2045e098bc96SEvan Quan 	if (adev->unique_id)
2046e098bc96SEvan Quan 		return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
2047e098bc96SEvan Quan 
2048e098bc96SEvan Quan 	return 0;
2049e098bc96SEvan Quan }
2050e098bc96SEvan Quan 
2051e098bc96SEvan Quan /**
2052e098bc96SEvan Quan  * DOC: thermal_throttling_logging
2053e098bc96SEvan Quan  *
2054e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
2055e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
2056e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
2057e098bc96SEvan Quan  * the log frequency.
2058e098bc96SEvan Quan  *
2059e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
2060e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
2061e098bc96SEvan Quan  *
2062e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
2063e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
2064e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
2065e098bc96SEvan Quan  */
2066e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
2067e098bc96SEvan Quan 						     struct device_attribute *attr,
2068e098bc96SEvan Quan 						     char *buf)
2069e098bc96SEvan Quan {
2070e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
20711348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
2072e098bc96SEvan Quan 
2073e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%s: thermal throttling logging %s, with interval %d seconds\n",
20744a580877SLuben Tuikov 			adev_to_drm(adev)->unique,
2075e098bc96SEvan Quan 			atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
2076e098bc96SEvan Quan 			adev->throttling_logging_rs.interval / HZ + 1);
2077e098bc96SEvan Quan }
2078e098bc96SEvan Quan 
2079e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
2080e098bc96SEvan Quan 						     struct device_attribute *attr,
2081e098bc96SEvan Quan 						     const char *buf,
2082e098bc96SEvan Quan 						     size_t count)
2083e098bc96SEvan Quan {
2084e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
20851348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
2086e098bc96SEvan Quan 	long throttling_logging_interval;
2087e098bc96SEvan Quan 	unsigned long flags;
2088e098bc96SEvan Quan 	int ret = 0;
2089e098bc96SEvan Quan 
2090e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
2091e098bc96SEvan Quan 	if (ret)
2092e098bc96SEvan Quan 		return ret;
2093e098bc96SEvan Quan 
2094e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
2095e098bc96SEvan Quan 		return -EINVAL;
2096e098bc96SEvan Quan 
2097e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
2098e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
2099e098bc96SEvan Quan 		/*
2100e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
2101e098bc96SEvan Quan 		 * This can effectively restart the timer.
2102e098bc96SEvan Quan 		 */
2103e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
2104e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
2105e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
2106e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
2107e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
2108e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
2109e098bc96SEvan Quan 
2110e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
2111e098bc96SEvan Quan 	} else {
2112e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
2113e098bc96SEvan Quan 	}
2114e098bc96SEvan Quan 
2115e098bc96SEvan Quan 	return count;
2116e098bc96SEvan Quan }
2117e098bc96SEvan Quan 
2118e098bc96SEvan Quan /**
2119e098bc96SEvan Quan  * DOC: gpu_metrics
2120e098bc96SEvan Quan  *
2121e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
2122e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
2123e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
2124e098bc96SEvan Quan  *
2125e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
2126e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
2127e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
2128e098bc96SEvan Quan  * at the same time.
2129e098bc96SEvan Quan  */
2130e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
2131e098bc96SEvan Quan 				      struct device_attribute *attr,
2132e098bc96SEvan Quan 				      char *buf)
2133e098bc96SEvan Quan {
2134e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
21351348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
2136e098bc96SEvan Quan 	void *gpu_metrics;
2137e098bc96SEvan Quan 	ssize_t size = 0;
2138e098bc96SEvan Quan 	int ret;
2139e098bc96SEvan Quan 
214053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2141e098bc96SEvan Quan 		return -EPERM;
2142e098bc96SEvan Quan 
2143e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
2144e098bc96SEvan Quan 	if (ret < 0) {
2145e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
2146e098bc96SEvan Quan 		return ret;
2147e098bc96SEvan Quan 	}
2148e098bc96SEvan Quan 
2149e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2150e098bc96SEvan Quan 		size = smu_sys_get_gpu_metrics(&adev->smu, &gpu_metrics);
2151e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_gpu_metrics)
2152e098bc96SEvan Quan 		size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
2153e098bc96SEvan Quan 
2154e098bc96SEvan Quan 	if (size <= 0)
2155e098bc96SEvan Quan 		goto out;
2156e098bc96SEvan Quan 
2157e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
2158e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
2159e098bc96SEvan Quan 
2160e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
2161e098bc96SEvan Quan 
2162e098bc96SEvan Quan out:
2163e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
2164e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
2165e098bc96SEvan Quan 
2166e098bc96SEvan Quan 	return size;
2167e098bc96SEvan Quan }
2168e098bc96SEvan Quan 
2169e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2170e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2171e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC),
2172e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC),
2173e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC),
2174e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC),
2175e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC),
2176e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2177e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2178e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2179e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
21809577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
21819577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2182e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC),
2183e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC),
2184e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2185e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2186e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC),
2187e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
2188e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC),
2189e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC),
2190e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2191e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC),
2192e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC),
2193e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC),
2194e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC),
2195e098bc96SEvan Quan };
2196e098bc96SEvan Quan 
2197e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2198e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2199e098bc96SEvan Quan {
2200e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2201e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
2202e098bc96SEvan Quan 	struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2203e098bc96SEvan Quan 	enum amd_asic_type asic_type = adev->asic_type;
2204e098bc96SEvan Quan 
2205e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2206e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2207e098bc96SEvan Quan 		return 0;
2208e098bc96SEvan Quan 	}
2209e098bc96SEvan Quan 
2210e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2211e098bc96SEvan Quan 
2212e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2213e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA10)
2214e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2215e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2216e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA10 || asic_type == CHIP_ARCTURUS)
2217e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2218e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2219e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA20)
2220e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2221e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2222e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2223e098bc96SEvan Quan 		if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2224*e017fb66SXiaojian Du 		    (is_support_sw_smu(adev) && adev->smu.is_apu) ||
2225e098bc96SEvan Quan 			(!is_support_sw_smu(adev) && hwmgr->od_enabled))
2226e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
2227e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2228e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2229e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2230e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2231e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
2232e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
2233e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2234e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
2235e098bc96SEvan Quan 		if (asic_type != CHIP_VEGA10 &&
2236e098bc96SEvan Quan 		    asic_type != CHIP_VEGA20 &&
2237e098bc96SEvan Quan 		    asic_type != CHIP_ARCTURUS)
2238e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2239e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2240e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2241e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2242e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
2243e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA12)
2244e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
22459577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
22469577b0ecSXiaojian Du 		if (!(asic_type == CHIP_VANGOGH))
22479577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
22489577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
22499577b0ecSXiaojian Du 		if (!(asic_type == CHIP_VANGOGH))
22509577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
2251e098bc96SEvan Quan 	}
2252e098bc96SEvan Quan 
2253e098bc96SEvan Quan 	if (asic_type == CHIP_ARCTURUS) {
2254e098bc96SEvan Quan 		/* Arcturus does not support standalone mclk/socclk/fclk level setting */
2255e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2256e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2257e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2258e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2259e098bc96SEvan Quan 			dev_attr->store = NULL;
2260e098bc96SEvan Quan 		}
2261e098bc96SEvan Quan 	}
2262e098bc96SEvan Quan 
2263e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2264e098bc96SEvan Quan 
2265e098bc96SEvan Quan 	return 0;
2266e098bc96SEvan Quan }
2267e098bc96SEvan Quan 
2268e098bc96SEvan Quan 
2269e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2270e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2271e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2272e098bc96SEvan Quan {
2273e098bc96SEvan Quan 	int ret = 0;
2274e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2275e098bc96SEvan Quan 	const char *name = dev_attr->attr.name;
2276e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2277e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
2278e098bc96SEvan Quan 
2279e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2280e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2281e098bc96SEvan Quan 
2282e098bc96SEvan Quan 	BUG_ON(!attr);
2283e098bc96SEvan Quan 
2284e098bc96SEvan Quan 	attr_update = attr->attr_update ? attr_update : default_attr_update;
2285e098bc96SEvan Quan 
2286e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2287e098bc96SEvan Quan 	if (ret) {
2288e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2289e098bc96SEvan Quan 			name, ret);
2290e098bc96SEvan Quan 		return ret;
2291e098bc96SEvan Quan 	}
2292e098bc96SEvan Quan 
2293e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2294e098bc96SEvan Quan 		return 0;
2295e098bc96SEvan Quan 
2296e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2297e098bc96SEvan Quan 	if (ret) {
2298e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2299e098bc96SEvan Quan 			name, ret);
2300e098bc96SEvan Quan 	}
2301e098bc96SEvan Quan 
2302e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2303e098bc96SEvan Quan 	if (!attr_entry)
2304e098bc96SEvan Quan 		return -ENOMEM;
2305e098bc96SEvan Quan 
2306e098bc96SEvan Quan 	attr_entry->attr = attr;
2307e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2308e098bc96SEvan Quan 
2309e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2310e098bc96SEvan Quan 
2311e098bc96SEvan Quan 	return ret;
2312e098bc96SEvan Quan }
2313e098bc96SEvan Quan 
2314e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2315e098bc96SEvan Quan {
2316e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2317e098bc96SEvan Quan 
2318e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2319e098bc96SEvan Quan }
2320e098bc96SEvan Quan 
2321e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2322e098bc96SEvan Quan 					     struct list_head *attr_list);
2323e098bc96SEvan Quan 
2324e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2325e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2326e098bc96SEvan Quan 					    uint32_t counts,
2327e098bc96SEvan Quan 					    uint32_t mask,
2328e098bc96SEvan Quan 					    struct list_head *attr_list)
2329e098bc96SEvan Quan {
2330e098bc96SEvan Quan 	int ret = 0;
2331e098bc96SEvan Quan 	uint32_t i = 0;
2332e098bc96SEvan Quan 
2333e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2334e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2335e098bc96SEvan Quan 		if (ret)
2336e098bc96SEvan Quan 			goto failed;
2337e098bc96SEvan Quan 	}
2338e098bc96SEvan Quan 
2339e098bc96SEvan Quan 	return 0;
2340e098bc96SEvan Quan 
2341e098bc96SEvan Quan failed:
2342e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2343e098bc96SEvan Quan 
2344e098bc96SEvan Quan 	return ret;
2345e098bc96SEvan Quan }
2346e098bc96SEvan Quan 
2347e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2348e098bc96SEvan Quan 					     struct list_head *attr_list)
2349e098bc96SEvan Quan {
2350e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2351e098bc96SEvan Quan 
2352e098bc96SEvan Quan 	if (list_empty(attr_list))
2353e098bc96SEvan Quan 		return ;
2354e098bc96SEvan Quan 
2355e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2356e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2357e098bc96SEvan Quan 		list_del(&entry->entry);
2358e098bc96SEvan Quan 		kfree(entry);
2359e098bc96SEvan Quan 	}
2360e098bc96SEvan Quan }
2361e098bc96SEvan Quan 
2362e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2363e098bc96SEvan Quan 				      struct device_attribute *attr,
2364e098bc96SEvan Quan 				      char *buf)
2365e098bc96SEvan Quan {
2366e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2367e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2368e098bc96SEvan Quan 	int r, temp = 0, size = sizeof(temp);
2369e098bc96SEvan Quan 
237053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2371e098bc96SEvan Quan 		return -EPERM;
2372e098bc96SEvan Quan 
2373e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2374e098bc96SEvan Quan 		return -EINVAL;
2375e098bc96SEvan Quan 
23764a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2377e098bc96SEvan Quan 	if (r < 0) {
23784a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2379e098bc96SEvan Quan 		return r;
2380e098bc96SEvan Quan 	}
2381e098bc96SEvan Quan 
2382e098bc96SEvan Quan 	switch (channel) {
2383e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2384e098bc96SEvan Quan 		/* get current junction temperature */
2385e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2386e098bc96SEvan Quan 					   (void *)&temp, &size);
2387e098bc96SEvan Quan 		break;
2388e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2389e098bc96SEvan Quan 		/* get current edge temperature */
2390e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2391e098bc96SEvan Quan 					   (void *)&temp, &size);
2392e098bc96SEvan Quan 		break;
2393e098bc96SEvan Quan 	case PP_TEMP_MEM:
2394e098bc96SEvan Quan 		/* get current memory temperature */
2395e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2396e098bc96SEvan Quan 					   (void *)&temp, &size);
2397e098bc96SEvan Quan 		break;
2398e098bc96SEvan Quan 	default:
2399e098bc96SEvan Quan 		r = -EINVAL;
2400e098bc96SEvan Quan 		break;
2401e098bc96SEvan Quan 	}
2402e098bc96SEvan Quan 
24034a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24044a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2405e098bc96SEvan Quan 
2406e098bc96SEvan Quan 	if (r)
2407e098bc96SEvan Quan 		return r;
2408e098bc96SEvan Quan 
2409e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2410e098bc96SEvan Quan }
2411e098bc96SEvan Quan 
2412e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2413e098bc96SEvan Quan 					     struct device_attribute *attr,
2414e098bc96SEvan Quan 					     char *buf)
2415e098bc96SEvan Quan {
2416e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2417e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2418e098bc96SEvan Quan 	int temp;
2419e098bc96SEvan Quan 
2420e098bc96SEvan Quan 	if (hyst)
2421e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2422e098bc96SEvan Quan 	else
2423e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2424e098bc96SEvan Quan 
2425e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2426e098bc96SEvan Quan }
2427e098bc96SEvan Quan 
2428e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2429e098bc96SEvan Quan 					     struct device_attribute *attr,
2430e098bc96SEvan Quan 					     char *buf)
2431e098bc96SEvan Quan {
2432e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2433e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2434e098bc96SEvan Quan 	int temp;
2435e098bc96SEvan Quan 
2436e098bc96SEvan Quan 	if (hyst)
2437e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2438e098bc96SEvan Quan 	else
2439e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2440e098bc96SEvan Quan 
2441e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2442e098bc96SEvan Quan }
2443e098bc96SEvan Quan 
2444e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2445e098bc96SEvan Quan 					     struct device_attribute *attr,
2446e098bc96SEvan Quan 					     char *buf)
2447e098bc96SEvan Quan {
2448e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2449e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2450e098bc96SEvan Quan 	int temp;
2451e098bc96SEvan Quan 
2452e098bc96SEvan Quan 	if (hyst)
2453e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2454e098bc96SEvan Quan 	else
2455e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2456e098bc96SEvan Quan 
2457e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2458e098bc96SEvan Quan }
2459e098bc96SEvan Quan 
2460e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2461e098bc96SEvan Quan 					     struct device_attribute *attr,
2462e098bc96SEvan Quan 					     char *buf)
2463e098bc96SEvan Quan {
2464e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2465e098bc96SEvan Quan 
2466e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2467e098bc96SEvan Quan 		return -EINVAL;
2468e098bc96SEvan Quan 
2469e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
2470e098bc96SEvan Quan }
2471e098bc96SEvan Quan 
2472e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2473e098bc96SEvan Quan 					     struct device_attribute *attr,
2474e098bc96SEvan Quan 					     char *buf)
2475e098bc96SEvan Quan {
2476e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2477e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2478e098bc96SEvan Quan 	int temp = 0;
2479e098bc96SEvan Quan 
2480e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2481e098bc96SEvan Quan 		return -EINVAL;
2482e098bc96SEvan Quan 
2483e098bc96SEvan Quan 	switch (channel) {
2484e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2485e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2486e098bc96SEvan Quan 		break;
2487e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2488e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2489e098bc96SEvan Quan 		break;
2490e098bc96SEvan Quan 	case PP_TEMP_MEM:
2491e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2492e098bc96SEvan Quan 		break;
2493e098bc96SEvan Quan 	}
2494e098bc96SEvan Quan 
2495e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2496e098bc96SEvan Quan }
2497e098bc96SEvan Quan 
2498e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2499e098bc96SEvan Quan 					    struct device_attribute *attr,
2500e098bc96SEvan Quan 					    char *buf)
2501e098bc96SEvan Quan {
2502e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2503e098bc96SEvan Quan 	u32 pwm_mode = 0;
2504e098bc96SEvan Quan 	int ret;
2505e098bc96SEvan Quan 
250653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2507e098bc96SEvan Quan 		return -EPERM;
2508e098bc96SEvan Quan 
25094a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2510e098bc96SEvan Quan 	if (ret < 0) {
25114a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2512e098bc96SEvan Quan 		return ret;
2513e098bc96SEvan Quan 	}
2514e098bc96SEvan Quan 
2515e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
2516e098bc96SEvan Quan 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2517e098bc96SEvan Quan 	} else {
2518e098bc96SEvan Quan 		if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
25194a580877SLuben Tuikov 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25204a580877SLuben Tuikov 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2521e098bc96SEvan Quan 			return -EINVAL;
2522e098bc96SEvan Quan 		}
2523e098bc96SEvan Quan 
2524e098bc96SEvan Quan 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2525e098bc96SEvan Quan 	}
2526e098bc96SEvan Quan 
25274a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25284a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2529e098bc96SEvan Quan 
2530e098bc96SEvan Quan 	return sprintf(buf, "%i\n", pwm_mode);
2531e098bc96SEvan Quan }
2532e098bc96SEvan Quan 
2533e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2534e098bc96SEvan Quan 					    struct device_attribute *attr,
2535e098bc96SEvan Quan 					    const char *buf,
2536e098bc96SEvan Quan 					    size_t count)
2537e098bc96SEvan Quan {
2538e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2539e098bc96SEvan Quan 	int err, ret;
2540e098bc96SEvan Quan 	int value;
2541e098bc96SEvan Quan 
254253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2543e098bc96SEvan Quan 		return -EPERM;
2544e098bc96SEvan Quan 
2545e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2546e098bc96SEvan Quan 	if (err)
2547e098bc96SEvan Quan 		return err;
2548e098bc96SEvan Quan 
25494a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2550e098bc96SEvan Quan 	if (ret < 0) {
25514a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2552e098bc96SEvan Quan 		return ret;
2553e098bc96SEvan Quan 	}
2554e098bc96SEvan Quan 
2555e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
2556e098bc96SEvan Quan 		smu_set_fan_control_mode(&adev->smu, value);
2557e098bc96SEvan Quan 	} else {
2558e098bc96SEvan Quan 		if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
25594a580877SLuben Tuikov 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25604a580877SLuben Tuikov 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2561e098bc96SEvan Quan 			return -EINVAL;
2562e098bc96SEvan Quan 		}
2563e098bc96SEvan Quan 
2564e098bc96SEvan Quan 		amdgpu_dpm_set_fan_control_mode(adev, value);
2565e098bc96SEvan Quan 	}
2566e098bc96SEvan Quan 
25674a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25684a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2569e098bc96SEvan Quan 
2570e098bc96SEvan Quan 	return count;
2571e098bc96SEvan Quan }
2572e098bc96SEvan Quan 
2573e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2574e098bc96SEvan Quan 					 struct device_attribute *attr,
2575e098bc96SEvan Quan 					 char *buf)
2576e098bc96SEvan Quan {
2577e098bc96SEvan Quan 	return sprintf(buf, "%i\n", 0);
2578e098bc96SEvan Quan }
2579e098bc96SEvan Quan 
2580e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2581e098bc96SEvan Quan 					 struct device_attribute *attr,
2582e098bc96SEvan Quan 					 char *buf)
2583e098bc96SEvan Quan {
2584e098bc96SEvan Quan 	return sprintf(buf, "%i\n", 255);
2585e098bc96SEvan Quan }
2586e098bc96SEvan Quan 
2587e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2588e098bc96SEvan Quan 				     struct device_attribute *attr,
2589e098bc96SEvan Quan 				     const char *buf, size_t count)
2590e098bc96SEvan Quan {
2591e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2592e098bc96SEvan Quan 	int err;
2593e098bc96SEvan Quan 	u32 value;
2594e098bc96SEvan Quan 	u32 pwm_mode;
2595e098bc96SEvan Quan 
259653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2597e098bc96SEvan Quan 		return -EPERM;
2598e098bc96SEvan Quan 
25994a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2600e098bc96SEvan Quan 	if (err < 0) {
26014a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2602e098bc96SEvan Quan 		return err;
2603e098bc96SEvan Quan 	}
2604e098bc96SEvan Quan 
2605e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2606e098bc96SEvan Quan 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2607e098bc96SEvan Quan 	else
2608e098bc96SEvan Quan 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2609e098bc96SEvan Quan 
2610e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2611e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
26124a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26134a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2614e098bc96SEvan Quan 		return -EINVAL;
2615e098bc96SEvan Quan 	}
2616e098bc96SEvan Quan 
2617e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2618e098bc96SEvan Quan 	if (err) {
26194a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26204a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2621e098bc96SEvan Quan 		return err;
2622e098bc96SEvan Quan 	}
2623e098bc96SEvan Quan 
2624e098bc96SEvan Quan 	value = (value * 100) / 255;
2625e098bc96SEvan Quan 
2626e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2627e098bc96SEvan Quan 		err = smu_set_fan_speed_percent(&adev->smu, value);
2628e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->set_fan_speed_percent)
2629e098bc96SEvan Quan 		err = amdgpu_dpm_set_fan_speed_percent(adev, value);
2630e098bc96SEvan Quan 	else
2631e098bc96SEvan Quan 		err = -EINVAL;
2632e098bc96SEvan Quan 
26334a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26344a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2635e098bc96SEvan Quan 
2636e098bc96SEvan Quan 	if (err)
2637e098bc96SEvan Quan 		return err;
2638e098bc96SEvan Quan 
2639e098bc96SEvan Quan 	return count;
2640e098bc96SEvan Quan }
2641e098bc96SEvan Quan 
2642e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2643e098bc96SEvan Quan 				     struct device_attribute *attr,
2644e098bc96SEvan Quan 				     char *buf)
2645e098bc96SEvan Quan {
2646e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2647e098bc96SEvan Quan 	int err;
2648e098bc96SEvan Quan 	u32 speed = 0;
2649e098bc96SEvan Quan 
265053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2651e098bc96SEvan Quan 		return -EPERM;
2652e098bc96SEvan Quan 
26534a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2654e098bc96SEvan Quan 	if (err < 0) {
26554a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2656e098bc96SEvan Quan 		return err;
2657e098bc96SEvan Quan 	}
2658e098bc96SEvan Quan 
2659e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2660e098bc96SEvan Quan 		err = smu_get_fan_speed_percent(&adev->smu, &speed);
2661e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_fan_speed_percent)
2662e098bc96SEvan Quan 		err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
2663e098bc96SEvan Quan 	else
2664e098bc96SEvan Quan 		err = -EINVAL;
2665e098bc96SEvan Quan 
26664a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26674a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2668e098bc96SEvan Quan 
2669e098bc96SEvan Quan 	if (err)
2670e098bc96SEvan Quan 		return err;
2671e098bc96SEvan Quan 
2672e098bc96SEvan Quan 	speed = (speed * 255) / 100;
2673e098bc96SEvan Quan 
2674e098bc96SEvan Quan 	return sprintf(buf, "%i\n", speed);
2675e098bc96SEvan Quan }
2676e098bc96SEvan Quan 
2677e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2678e098bc96SEvan Quan 					   struct device_attribute *attr,
2679e098bc96SEvan Quan 					   char *buf)
2680e098bc96SEvan Quan {
2681e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2682e098bc96SEvan Quan 	int err;
2683e098bc96SEvan Quan 	u32 speed = 0;
2684e098bc96SEvan Quan 
268553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2686e098bc96SEvan Quan 		return -EPERM;
2687e098bc96SEvan Quan 
26884a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2689e098bc96SEvan Quan 	if (err < 0) {
26904a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2691e098bc96SEvan Quan 		return err;
2692e098bc96SEvan Quan 	}
2693e098bc96SEvan Quan 
2694e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2695e098bc96SEvan Quan 		err = smu_get_fan_speed_rpm(&adev->smu, &speed);
2696e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2697e098bc96SEvan Quan 		err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2698e098bc96SEvan Quan 	else
2699e098bc96SEvan Quan 		err = -EINVAL;
2700e098bc96SEvan Quan 
27014a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27024a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2703e098bc96SEvan Quan 
2704e098bc96SEvan Quan 	if (err)
2705e098bc96SEvan Quan 		return err;
2706e098bc96SEvan Quan 
2707e098bc96SEvan Quan 	return sprintf(buf, "%i\n", speed);
2708e098bc96SEvan Quan }
2709e098bc96SEvan Quan 
2710e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2711e098bc96SEvan Quan 					 struct device_attribute *attr,
2712e098bc96SEvan Quan 					 char *buf)
2713e098bc96SEvan Quan {
2714e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2715e098bc96SEvan Quan 	u32 min_rpm = 0;
2716e098bc96SEvan Quan 	u32 size = sizeof(min_rpm);
2717e098bc96SEvan Quan 	int r;
2718e098bc96SEvan Quan 
271953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2720e098bc96SEvan Quan 		return -EPERM;
2721e098bc96SEvan Quan 
27224a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2723e098bc96SEvan Quan 	if (r < 0) {
27244a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2725e098bc96SEvan Quan 		return r;
2726e098bc96SEvan Quan 	}
2727e098bc96SEvan Quan 
2728e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2729e098bc96SEvan Quan 				   (void *)&min_rpm, &size);
2730e098bc96SEvan Quan 
27314a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27324a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2733e098bc96SEvan Quan 
2734e098bc96SEvan Quan 	if (r)
2735e098bc96SEvan Quan 		return r;
2736e098bc96SEvan Quan 
2737e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
2738e098bc96SEvan Quan }
2739e098bc96SEvan Quan 
2740e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2741e098bc96SEvan Quan 					 struct device_attribute *attr,
2742e098bc96SEvan Quan 					 char *buf)
2743e098bc96SEvan Quan {
2744e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2745e098bc96SEvan Quan 	u32 max_rpm = 0;
2746e098bc96SEvan Quan 	u32 size = sizeof(max_rpm);
2747e098bc96SEvan Quan 	int r;
2748e098bc96SEvan Quan 
274953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2750e098bc96SEvan Quan 		return -EPERM;
2751e098bc96SEvan Quan 
27524a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2753e098bc96SEvan Quan 	if (r < 0) {
27544a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2755e098bc96SEvan Quan 		return r;
2756e098bc96SEvan Quan 	}
2757e098bc96SEvan Quan 
2758e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2759e098bc96SEvan Quan 				   (void *)&max_rpm, &size);
2760e098bc96SEvan Quan 
27614a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27624a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2763e098bc96SEvan Quan 
2764e098bc96SEvan Quan 	if (r)
2765e098bc96SEvan Quan 		return r;
2766e098bc96SEvan Quan 
2767e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
2768e098bc96SEvan Quan }
2769e098bc96SEvan Quan 
2770e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2771e098bc96SEvan Quan 					   struct device_attribute *attr,
2772e098bc96SEvan Quan 					   char *buf)
2773e098bc96SEvan Quan {
2774e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2775e098bc96SEvan Quan 	int err;
2776e098bc96SEvan Quan 	u32 rpm = 0;
2777e098bc96SEvan Quan 
277853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2779e098bc96SEvan Quan 		return -EPERM;
2780e098bc96SEvan Quan 
27814a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2782e098bc96SEvan Quan 	if (err < 0) {
27834a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2784e098bc96SEvan Quan 		return err;
2785e098bc96SEvan Quan 	}
2786e098bc96SEvan Quan 
2787e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2788e098bc96SEvan Quan 		err = smu_get_fan_speed_rpm(&adev->smu, &rpm);
2789e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2790e098bc96SEvan Quan 		err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2791e098bc96SEvan Quan 	else
2792e098bc96SEvan Quan 		err = -EINVAL;
2793e098bc96SEvan Quan 
27944a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27954a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2796e098bc96SEvan Quan 
2797e098bc96SEvan Quan 	if (err)
2798e098bc96SEvan Quan 		return err;
2799e098bc96SEvan Quan 
2800e098bc96SEvan Quan 	return sprintf(buf, "%i\n", rpm);
2801e098bc96SEvan Quan }
2802e098bc96SEvan Quan 
2803e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2804e098bc96SEvan Quan 				     struct device_attribute *attr,
2805e098bc96SEvan Quan 				     const char *buf, size_t count)
2806e098bc96SEvan Quan {
2807e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2808e098bc96SEvan Quan 	int err;
2809e098bc96SEvan Quan 	u32 value;
2810e098bc96SEvan Quan 	u32 pwm_mode;
2811e098bc96SEvan Quan 
281253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2813e098bc96SEvan Quan 		return -EPERM;
2814e098bc96SEvan Quan 
28154a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2816e098bc96SEvan Quan 	if (err < 0) {
28174a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2818e098bc96SEvan Quan 		return err;
2819e098bc96SEvan Quan 	}
2820e098bc96SEvan Quan 
2821e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2822e098bc96SEvan Quan 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2823e098bc96SEvan Quan 	else
2824e098bc96SEvan Quan 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2825e098bc96SEvan Quan 
2826e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
28274a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28284a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2829e098bc96SEvan Quan 		return -ENODATA;
2830e098bc96SEvan Quan 	}
2831e098bc96SEvan Quan 
2832e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2833e098bc96SEvan Quan 	if (err) {
28344a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28354a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2836e098bc96SEvan Quan 		return err;
2837e098bc96SEvan Quan 	}
2838e098bc96SEvan Quan 
2839e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
2840e098bc96SEvan Quan 		err = smu_set_fan_speed_rpm(&adev->smu, value);
2841e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2842e098bc96SEvan Quan 		err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2843e098bc96SEvan Quan 	else
2844e098bc96SEvan Quan 		err = -EINVAL;
2845e098bc96SEvan Quan 
28464a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28474a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2848e098bc96SEvan Quan 
2849e098bc96SEvan Quan 	if (err)
2850e098bc96SEvan Quan 		return err;
2851e098bc96SEvan Quan 
2852e098bc96SEvan Quan 	return count;
2853e098bc96SEvan Quan }
2854e098bc96SEvan Quan 
2855e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2856e098bc96SEvan Quan 					    struct device_attribute *attr,
2857e098bc96SEvan Quan 					    char *buf)
2858e098bc96SEvan Quan {
2859e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2860e098bc96SEvan Quan 	u32 pwm_mode = 0;
2861e098bc96SEvan Quan 	int ret;
2862e098bc96SEvan Quan 
286353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2864e098bc96SEvan Quan 		return -EPERM;
2865e098bc96SEvan Quan 
28664a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2867e098bc96SEvan Quan 	if (ret < 0) {
28684a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2869e098bc96SEvan Quan 		return ret;
2870e098bc96SEvan Quan 	}
2871e098bc96SEvan Quan 
2872e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
2873e098bc96SEvan Quan 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2874e098bc96SEvan Quan 	} else {
2875e098bc96SEvan Quan 		if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
28764a580877SLuben Tuikov 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28774a580877SLuben Tuikov 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2878e098bc96SEvan Quan 			return -EINVAL;
2879e098bc96SEvan Quan 		}
2880e098bc96SEvan Quan 
2881e098bc96SEvan Quan 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2882e098bc96SEvan Quan 	}
2883e098bc96SEvan Quan 
28844a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28854a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2886e098bc96SEvan Quan 
2887e098bc96SEvan Quan 	return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2888e098bc96SEvan Quan }
2889e098bc96SEvan Quan 
2890e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2891e098bc96SEvan Quan 					    struct device_attribute *attr,
2892e098bc96SEvan Quan 					    const char *buf,
2893e098bc96SEvan Quan 					    size_t count)
2894e098bc96SEvan Quan {
2895e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2896e098bc96SEvan Quan 	int err;
2897e098bc96SEvan Quan 	int value;
2898e098bc96SEvan Quan 	u32 pwm_mode;
2899e098bc96SEvan Quan 
290053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2901e098bc96SEvan Quan 		return -EPERM;
2902e098bc96SEvan Quan 
2903e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2904e098bc96SEvan Quan 	if (err)
2905e098bc96SEvan Quan 		return err;
2906e098bc96SEvan Quan 
2907e098bc96SEvan Quan 	if (value == 0)
2908e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2909e098bc96SEvan Quan 	else if (value == 1)
2910e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2911e098bc96SEvan Quan 	else
2912e098bc96SEvan Quan 		return -EINVAL;
2913e098bc96SEvan Quan 
29144a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2915e098bc96SEvan Quan 	if (err < 0) {
29164a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2917e098bc96SEvan Quan 		return err;
2918e098bc96SEvan Quan 	}
2919e098bc96SEvan Quan 
2920e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
2921e098bc96SEvan Quan 		smu_set_fan_control_mode(&adev->smu, pwm_mode);
2922e098bc96SEvan Quan 	} else {
2923e098bc96SEvan Quan 		if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
29244a580877SLuben Tuikov 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29254a580877SLuben Tuikov 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2926e098bc96SEvan Quan 			return -EINVAL;
2927e098bc96SEvan Quan 		}
2928e098bc96SEvan Quan 		amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2929e098bc96SEvan Quan 	}
2930e098bc96SEvan Quan 
29314a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29324a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2933e098bc96SEvan Quan 
2934e098bc96SEvan Quan 	return count;
2935e098bc96SEvan Quan }
2936e098bc96SEvan Quan 
2937e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2938e098bc96SEvan Quan 					struct device_attribute *attr,
2939e098bc96SEvan Quan 					char *buf)
2940e098bc96SEvan Quan {
2941e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2942e098bc96SEvan Quan 	u32 vddgfx;
2943e098bc96SEvan Quan 	int r, size = sizeof(vddgfx);
2944e098bc96SEvan Quan 
294553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2946e098bc96SEvan Quan 		return -EPERM;
2947e098bc96SEvan Quan 
29484a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2949e098bc96SEvan Quan 	if (r < 0) {
29504a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2951e098bc96SEvan Quan 		return r;
2952e098bc96SEvan Quan 	}
2953e098bc96SEvan Quan 
2954e098bc96SEvan Quan 	/* get the voltage */
2955e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2956e098bc96SEvan Quan 				   (void *)&vddgfx, &size);
2957e098bc96SEvan Quan 
29584a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29594a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2960e098bc96SEvan Quan 
2961e098bc96SEvan Quan 	if (r)
2962e098bc96SEvan Quan 		return r;
2963e098bc96SEvan Quan 
2964e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
2965e098bc96SEvan Quan }
2966e098bc96SEvan Quan 
2967e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2968e098bc96SEvan Quan 					      struct device_attribute *attr,
2969e098bc96SEvan Quan 					      char *buf)
2970e098bc96SEvan Quan {
2971e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "vddgfx\n");
2972e098bc96SEvan Quan }
2973e098bc96SEvan Quan 
2974e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2975e098bc96SEvan Quan 				       struct device_attribute *attr,
2976e098bc96SEvan Quan 				       char *buf)
2977e098bc96SEvan Quan {
2978e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2979e098bc96SEvan Quan 	u32 vddnb;
2980e098bc96SEvan Quan 	int r, size = sizeof(vddnb);
2981e098bc96SEvan Quan 
298253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2983e098bc96SEvan Quan 		return -EPERM;
2984e098bc96SEvan Quan 
2985e098bc96SEvan Quan 	/* only APUs have vddnb */
2986e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2987e098bc96SEvan Quan 		return -EINVAL;
2988e098bc96SEvan Quan 
29894a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2990e098bc96SEvan Quan 	if (r < 0) {
29914a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2992e098bc96SEvan Quan 		return r;
2993e098bc96SEvan Quan 	}
2994e098bc96SEvan Quan 
2995e098bc96SEvan Quan 	/* get the voltage */
2996e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2997e098bc96SEvan Quan 				   (void *)&vddnb, &size);
2998e098bc96SEvan Quan 
29994a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30004a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3001e098bc96SEvan Quan 
3002e098bc96SEvan Quan 	if (r)
3003e098bc96SEvan Quan 		return r;
3004e098bc96SEvan Quan 
3005e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
3006e098bc96SEvan Quan }
3007e098bc96SEvan Quan 
3008e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
3009e098bc96SEvan Quan 					      struct device_attribute *attr,
3010e098bc96SEvan Quan 					      char *buf)
3011e098bc96SEvan Quan {
3012e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "vddnb\n");
3013e098bc96SEvan Quan }
3014e098bc96SEvan Quan 
3015e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3016e098bc96SEvan Quan 					   struct device_attribute *attr,
3017e098bc96SEvan Quan 					   char *buf)
3018e098bc96SEvan Quan {
3019e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3020e098bc96SEvan Quan 	u32 query = 0;
3021e098bc96SEvan Quan 	int r, size = sizeof(u32);
3022e098bc96SEvan Quan 	unsigned uw;
3023e098bc96SEvan Quan 
302453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3025e098bc96SEvan Quan 		return -EPERM;
3026e098bc96SEvan Quan 
30274a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3028e098bc96SEvan Quan 	if (r < 0) {
30294a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3030e098bc96SEvan Quan 		return r;
3031e098bc96SEvan Quan 	}
3032e098bc96SEvan Quan 
3033e098bc96SEvan Quan 	/* get the voltage */
3034e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
3035e098bc96SEvan Quan 				   (void *)&query, &size);
3036e098bc96SEvan Quan 
30374a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30384a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3039e098bc96SEvan Quan 
3040e098bc96SEvan Quan 	if (r)
3041e098bc96SEvan Quan 		return r;
3042e098bc96SEvan Quan 
3043e098bc96SEvan Quan 	/* convert to microwatts */
3044e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
3045e098bc96SEvan Quan 
3046e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%u\n", uw);
3047e098bc96SEvan Quan }
3048e098bc96SEvan Quan 
3049e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
3050e098bc96SEvan Quan 					 struct device_attribute *attr,
3051e098bc96SEvan Quan 					 char *buf)
3052e098bc96SEvan Quan {
3053e098bc96SEvan Quan 	return sprintf(buf, "%i\n", 0);
3054e098bc96SEvan Quan }
3055e098bc96SEvan Quan 
3056e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
3057e098bc96SEvan Quan 					 struct device_attribute *attr,
3058e098bc96SEvan Quan 					 char *buf)
3059e098bc96SEvan Quan {
3060e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3061e098bc96SEvan Quan 	uint32_t limit = 0;
3062e098bc96SEvan Quan 	ssize_t size;
3063e098bc96SEvan Quan 	int r;
3064e098bc96SEvan Quan 
306553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3066e098bc96SEvan Quan 		return -EPERM;
3067e098bc96SEvan Quan 
30684a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3069e098bc96SEvan Quan 	if (r < 0) {
30704a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3071e098bc96SEvan Quan 		return r;
3072e098bc96SEvan Quan 	}
3073e098bc96SEvan Quan 
3074e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
3075e098bc96SEvan Quan 		smu_get_power_limit(&adev->smu, &limit, true);
3076e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
3077e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
3078e098bc96SEvan Quan 		adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
3079e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
3080e098bc96SEvan Quan 	} else {
3081e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
3082e098bc96SEvan Quan 	}
3083e098bc96SEvan Quan 
30844a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30854a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3086e098bc96SEvan Quan 
3087e098bc96SEvan Quan 	return size;
3088e098bc96SEvan Quan }
3089e098bc96SEvan Quan 
3090e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3091e098bc96SEvan Quan 					 struct device_attribute *attr,
3092e098bc96SEvan Quan 					 char *buf)
3093e098bc96SEvan Quan {
3094e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3095e098bc96SEvan Quan 	uint32_t limit = 0;
3096e098bc96SEvan Quan 	ssize_t size;
3097e098bc96SEvan Quan 	int r;
3098e098bc96SEvan Quan 
309953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3100e098bc96SEvan Quan 		return -EPERM;
3101e098bc96SEvan Quan 
31024a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3103e098bc96SEvan Quan 	if (r < 0) {
31044a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3105e098bc96SEvan Quan 		return r;
3106e098bc96SEvan Quan 	}
3107e098bc96SEvan Quan 
3108e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
3109e098bc96SEvan Quan 		smu_get_power_limit(&adev->smu, &limit, false);
3110e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
3111e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
3112e098bc96SEvan Quan 		adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
3113e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
3114e098bc96SEvan Quan 	} else {
3115e098bc96SEvan Quan 		size = snprintf(buf, PAGE_SIZE, "\n");
3116e098bc96SEvan Quan 	}
3117e098bc96SEvan Quan 
31184a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31194a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3120e098bc96SEvan Quan 
3121e098bc96SEvan Quan 	return size;
3122e098bc96SEvan Quan }
3123e098bc96SEvan Quan 
3124e098bc96SEvan Quan 
3125e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3126e098bc96SEvan Quan 		struct device_attribute *attr,
3127e098bc96SEvan Quan 		const char *buf,
3128e098bc96SEvan Quan 		size_t count)
3129e098bc96SEvan Quan {
3130e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3131e098bc96SEvan Quan 	int err;
3132e098bc96SEvan Quan 	u32 value;
3133e098bc96SEvan Quan 
313453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3135e098bc96SEvan Quan 		return -EPERM;
3136e098bc96SEvan Quan 
3137e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
3138e098bc96SEvan Quan 		return -EINVAL;
3139e098bc96SEvan Quan 
3140e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
3141e098bc96SEvan Quan 	if (err)
3142e098bc96SEvan Quan 		return err;
3143e098bc96SEvan Quan 
3144e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
3145e098bc96SEvan Quan 
3146e098bc96SEvan Quan 
31474a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3148e098bc96SEvan Quan 	if (err < 0) {
31494a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3150e098bc96SEvan Quan 		return err;
3151e098bc96SEvan Quan 	}
3152e098bc96SEvan Quan 
3153e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
3154e098bc96SEvan Quan 		err = smu_set_power_limit(&adev->smu, value);
3155e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit)
3156e098bc96SEvan Quan 		err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
3157e098bc96SEvan Quan 	else
3158e098bc96SEvan Quan 		err = -EINVAL;
3159e098bc96SEvan Quan 
31604a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31614a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3162e098bc96SEvan Quan 
3163e098bc96SEvan Quan 	if (err)
3164e098bc96SEvan Quan 		return err;
3165e098bc96SEvan Quan 
3166e098bc96SEvan Quan 	return count;
3167e098bc96SEvan Quan }
3168e098bc96SEvan Quan 
3169e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3170e098bc96SEvan Quan 				      struct device_attribute *attr,
3171e098bc96SEvan Quan 				      char *buf)
3172e098bc96SEvan Quan {
3173e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3174e098bc96SEvan Quan 	uint32_t sclk;
3175e098bc96SEvan Quan 	int r, size = sizeof(sclk);
3176e098bc96SEvan Quan 
317753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3178e098bc96SEvan Quan 		return -EPERM;
3179e098bc96SEvan Quan 
31804a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3181e098bc96SEvan Quan 	if (r < 0) {
31824a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3183e098bc96SEvan Quan 		return r;
3184e098bc96SEvan Quan 	}
3185e098bc96SEvan Quan 
3186e098bc96SEvan Quan 	/* get the sclk */
3187e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3188e098bc96SEvan Quan 				   (void *)&sclk, &size);
3189e098bc96SEvan Quan 
31904a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31914a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3192e098bc96SEvan Quan 
3193e098bc96SEvan Quan 	if (r)
3194e098bc96SEvan Quan 		return r;
3195e098bc96SEvan Quan 
3196e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%u\n", sclk * 10 * 1000);
3197e098bc96SEvan Quan }
3198e098bc96SEvan Quan 
3199e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3200e098bc96SEvan Quan 					    struct device_attribute *attr,
3201e098bc96SEvan Quan 					    char *buf)
3202e098bc96SEvan Quan {
3203e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "sclk\n");
3204e098bc96SEvan Quan }
3205e098bc96SEvan Quan 
3206e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3207e098bc96SEvan Quan 				      struct device_attribute *attr,
3208e098bc96SEvan Quan 				      char *buf)
3209e098bc96SEvan Quan {
3210e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3211e098bc96SEvan Quan 	uint32_t mclk;
3212e098bc96SEvan Quan 	int r, size = sizeof(mclk);
3213e098bc96SEvan Quan 
321453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3215e098bc96SEvan Quan 		return -EPERM;
3216e098bc96SEvan Quan 
32174a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3218e098bc96SEvan Quan 	if (r < 0) {
32194a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3220e098bc96SEvan Quan 		return r;
3221e098bc96SEvan Quan 	}
3222e098bc96SEvan Quan 
3223e098bc96SEvan Quan 	/* get the sclk */
3224e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3225e098bc96SEvan Quan 				   (void *)&mclk, &size);
3226e098bc96SEvan Quan 
32274a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
32284a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3229e098bc96SEvan Quan 
3230e098bc96SEvan Quan 	if (r)
3231e098bc96SEvan Quan 		return r;
3232e098bc96SEvan Quan 
3233e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "%u\n", mclk * 10 * 1000);
3234e098bc96SEvan Quan }
3235e098bc96SEvan Quan 
3236e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3237e098bc96SEvan Quan 					    struct device_attribute *attr,
3238e098bc96SEvan Quan 					    char *buf)
3239e098bc96SEvan Quan {
3240e098bc96SEvan Quan 	return snprintf(buf, PAGE_SIZE, "mclk\n");
3241e098bc96SEvan Quan }
3242e098bc96SEvan Quan 
3243e098bc96SEvan Quan /**
3244e098bc96SEvan Quan  * DOC: hwmon
3245e098bc96SEvan Quan  *
3246e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3247e098bc96SEvan Quan  *
3248e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3249e098bc96SEvan Quan  *
3250e098bc96SEvan Quan  * - GPU voltage
3251e098bc96SEvan Quan  *
3252e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3253e098bc96SEvan Quan  *
3254e098bc96SEvan Quan  * - GPU power
3255e098bc96SEvan Quan  *
3256e098bc96SEvan Quan  * - GPU fan
3257e098bc96SEvan Quan  *
3258e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3259e098bc96SEvan Quan  *
3260e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3261e098bc96SEvan Quan  *
3262e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3263e098bc96SEvan Quan  *
3264e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3265e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3266e098bc96SEvan Quan  *
3267e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3268e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3269e098bc96SEvan Quan  *
3270e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3271e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3272e098bc96SEvan Quan  *
3273e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3274e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3275e098bc96SEvan Quan  *
3276e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3277e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3278e098bc96SEvan Quan  *
3279e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3280e098bc96SEvan Quan  *
3281e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3282e098bc96SEvan Quan  *
3283e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3284e098bc96SEvan Quan  *
3285e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3286e098bc96SEvan Quan  *
3287e098bc96SEvan Quan  * - power1_average: average power used by the GPU in microWatts
3288e098bc96SEvan Quan  *
3289e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3290e098bc96SEvan Quan  *
3291e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3292e098bc96SEvan Quan  *
3293e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3294e098bc96SEvan Quan  *
3295e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3296e098bc96SEvan Quan  *
3297e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3298e098bc96SEvan Quan  *
3299e098bc96SEvan Quan  * - 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)
3300e098bc96SEvan Quan  *
3301e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3302e098bc96SEvan Quan  *
3303e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3304e098bc96SEvan Quan  *
3305e098bc96SEvan Quan  * - fan1_min: an minimum value Unit: revolution/min (RPM)
3306e098bc96SEvan Quan  *
3307e098bc96SEvan Quan  * - fan1_max: an maxmum value Unit: revolution/max (RPM)
3308e098bc96SEvan Quan  *
3309e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3310e098bc96SEvan Quan  *
3311e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3312e098bc96SEvan Quan  *
3313e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3314e098bc96SEvan Quan  *
3315e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3316e098bc96SEvan Quan  *
3317e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3318e098bc96SEvan Quan  *
3319e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3320e098bc96SEvan Quan  *
3321e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3322e098bc96SEvan Quan  *
3323e098bc96SEvan Quan  */
3324e098bc96SEvan Quan 
3325e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3326e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3327e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3328e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3329e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3330e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3331e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3332e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3333e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3334e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3335e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3336e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3337e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3338e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3339e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3340e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3341e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3342e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3343e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3344e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3345e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3346e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3347e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3348e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3349e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3350e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3351e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3352e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3353e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3354e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3355e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3356e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3357e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3358e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3359e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3360e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3361e098bc96SEvan Quan 
3362e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3363e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3364e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3365e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3366e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3367e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3368e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3369e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3370e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3371e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3372e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3373e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3374e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3375e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3376e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3377e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3378e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3379e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3380e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3381e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3382e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3383e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3384e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3385e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3386e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3387e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3388e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3389e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3390e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3391e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3392e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3393e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3394e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
3395e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3396e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3397e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3398e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3399e098bc96SEvan Quan 	NULL
3400e098bc96SEvan Quan };
3401e098bc96SEvan Quan 
3402e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3403e098bc96SEvan Quan 					struct attribute *attr, int index)
3404e098bc96SEvan Quan {
3405e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3406e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3407e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
3408e098bc96SEvan Quan 
3409e098bc96SEvan Quan 	/* under multi-vf mode, the hwmon attributes are all not supported */
3410e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3411e098bc96SEvan Quan 		return 0;
3412e098bc96SEvan Quan 
3413e098bc96SEvan Quan 	/* there is no fan under pp one vf mode */
3414e098bc96SEvan Quan 	if (amdgpu_sriov_is_pp_one_vf(adev) &&
3415e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3416e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3417e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3418e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3419e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3420e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3421e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3422e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3423e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3424e098bc96SEvan Quan 		return 0;
3425e098bc96SEvan Quan 
3426e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3427e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3428e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3429e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3430e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3431e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3432e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3433e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3434e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3435e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3436e098bc96SEvan Quan 		return 0;
3437e098bc96SEvan Quan 
3438e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3439e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3440e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3441e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3442e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3443e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3444e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3445e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3446e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3447e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3448e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3449e098bc96SEvan Quan 		return 0;
3450e098bc96SEvan Quan 
3451e098bc96SEvan Quan 	/* Skip crit temp on APU */
3452e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3453e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3454e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3455e098bc96SEvan Quan 		return 0;
3456e098bc96SEvan Quan 
3457e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3458e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3459e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3460e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3461e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3462e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3463e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3464e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3465e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3466e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3467e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3468e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3469e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3470e098bc96SEvan Quan 		return 0;
3471e098bc96SEvan Quan 
3472e098bc96SEvan Quan 	if (!is_support_sw_smu(adev)) {
3473e098bc96SEvan Quan 		/* mask fan attributes if we have no bindings for this asic to expose */
3474e098bc96SEvan Quan 		if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
3475e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3476e098bc96SEvan Quan 		    (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3477e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3478e098bc96SEvan Quan 			effective_mode &= ~S_IRUGO;
3479e098bc96SEvan Quan 
3480e098bc96SEvan Quan 		if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3481e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3482e098bc96SEvan Quan 		    (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3483e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3484e098bc96SEvan Quan 			effective_mode &= ~S_IWUSR;
3485e098bc96SEvan Quan 	}
3486e098bc96SEvan Quan 
3487e098bc96SEvan Quan 	if (((adev->flags & AMD_IS_APU) ||
3488d0eb1b5cSAlex Deucher 	     adev->family == AMDGPU_FAMILY_SI) &&	/* not implemented yet */
3489367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3490e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
3491e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
3492e098bc96SEvan Quan 		return 0;
3493e098bc96SEvan Quan 
3494367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3495367deb67SAlex Deucher 	     ((adev->flags & AMD_IS_APU) &&
3496367deb67SAlex Deucher 	      (adev->asic_type < CHIP_RENOIR))) &&	/* not implemented yet */
3497367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3498367deb67SAlex Deucher 		return 0;
3499367deb67SAlex Deucher 
3500e098bc96SEvan Quan 	if (!is_support_sw_smu(adev)) {
3501e098bc96SEvan Quan 		/* hide max/min values if we can't both query and manage the fan */
3502e098bc96SEvan Quan 		if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3503e098bc96SEvan Quan 		     !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
3504e098bc96SEvan Quan 		     (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3505e098bc96SEvan Quan 		     !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3506e098bc96SEvan Quan 		    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3507e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3508e098bc96SEvan Quan 			return 0;
3509e098bc96SEvan Quan 
3510e098bc96SEvan Quan 		if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3511e098bc96SEvan Quan 		     !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3512e098bc96SEvan Quan 		    (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3513e098bc96SEvan Quan 		     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3514e098bc96SEvan Quan 			return 0;
3515e098bc96SEvan Quan 	}
3516e098bc96SEvan Quan 
3517e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3518e098bc96SEvan Quan 	     adev->family == AMDGPU_FAMILY_KV) &&	/* not implemented yet */
3519e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3520e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3521e098bc96SEvan Quan 		return 0;
3522e098bc96SEvan Quan 
3523e098bc96SEvan Quan 	/* only APUs have vddnb */
3524e098bc96SEvan Quan 	if (!(adev->flags & AMD_IS_APU) &&
3525e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3526e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3527e098bc96SEvan Quan 		return 0;
3528e098bc96SEvan Quan 
3529e098bc96SEvan Quan 	/* no mclk on APUs */
3530e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3531e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3532e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3533e098bc96SEvan Quan 		return 0;
3534e098bc96SEvan Quan 
3535e098bc96SEvan Quan 	/* only SOC15 dGPUs support hotspot and mem temperatures */
3536e098bc96SEvan Quan 	if (((adev->flags & AMD_IS_APU) ||
3537e098bc96SEvan Quan 	     adev->asic_type < CHIP_VEGA10) &&
3538e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3539e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3540e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3541e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3542e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3543e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3544e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3545e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3546e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3547e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3548e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3549e098bc96SEvan Quan 		return 0;
3550e098bc96SEvan Quan 
3551e098bc96SEvan Quan 	return effective_mode;
3552e098bc96SEvan Quan }
3553e098bc96SEvan Quan 
3554e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3555e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3556e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3557e098bc96SEvan Quan };
3558e098bc96SEvan Quan 
3559e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3560e098bc96SEvan Quan 	&hwmon_attrgroup,
3561e098bc96SEvan Quan 	NULL
3562e098bc96SEvan Quan };
3563e098bc96SEvan Quan 
3564e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3565e098bc96SEvan Quan {
3566e098bc96SEvan Quan 	int ret;
3567e098bc96SEvan Quan 	uint32_t mask = 0;
3568e098bc96SEvan Quan 
3569e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
3570e098bc96SEvan Quan 		return 0;
3571e098bc96SEvan Quan 
3572e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3573e098bc96SEvan Quan 		return 0;
3574e098bc96SEvan Quan 
3575e098bc96SEvan Quan 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3576e098bc96SEvan Quan 
3577e098bc96SEvan Quan 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3578e098bc96SEvan Quan 								   DRIVER_NAME, adev,
3579e098bc96SEvan Quan 								   hwmon_groups);
3580e098bc96SEvan Quan 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3581e098bc96SEvan Quan 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3582e098bc96SEvan Quan 		dev_err(adev->dev,
3583e098bc96SEvan Quan 			"Unable to register hwmon device: %d\n", ret);
3584e098bc96SEvan Quan 		return ret;
3585e098bc96SEvan Quan 	}
3586e098bc96SEvan Quan 
3587e098bc96SEvan Quan 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3588e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
3589e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
3590e098bc96SEvan Quan 		break;
3591e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
3592e098bc96SEvan Quan 		mask = 0;
3593e098bc96SEvan Quan 		break;
3594e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
3595e098bc96SEvan Quan 	default:
3596e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
3597e098bc96SEvan Quan 		break;
3598e098bc96SEvan Quan 	}
3599e098bc96SEvan Quan 
3600e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
3601e098bc96SEvan Quan 					       amdgpu_device_attrs,
3602e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
3603e098bc96SEvan Quan 					       mask,
3604e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
3605e098bc96SEvan Quan 	if (ret)
3606e098bc96SEvan Quan 		return ret;
3607e098bc96SEvan Quan 
3608e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
3609e098bc96SEvan Quan 
3610e098bc96SEvan Quan 	return 0;
3611e098bc96SEvan Quan }
3612e098bc96SEvan Quan 
3613e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3614e098bc96SEvan Quan {
3615e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3616e098bc96SEvan Quan 		return;
3617e098bc96SEvan Quan 
3618e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
3619e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3620e098bc96SEvan Quan 
3621e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3622e098bc96SEvan Quan }
3623e098bc96SEvan Quan 
3624e098bc96SEvan Quan /*
3625e098bc96SEvan Quan  * Debugfs info
3626e098bc96SEvan Quan  */
3627e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3628e098bc96SEvan Quan 
3629517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3630517cb957SHuang Rui 					   struct amdgpu_device *adev) {
3631517cb957SHuang Rui 	uint16_t *p_val;
3632517cb957SHuang Rui 	uint32_t size;
3633517cb957SHuang Rui 	int i;
3634517cb957SHuang Rui 
3635517cb957SHuang Rui 	if (is_support_cclk_dpm(adev)) {
3636517cb957SHuang Rui 		p_val = kcalloc(boot_cpu_data.x86_max_cores, sizeof(uint16_t),
3637517cb957SHuang Rui 				GFP_KERNEL);
3638517cb957SHuang Rui 
3639517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3640517cb957SHuang Rui 					    (void *)p_val, &size)) {
3641517cb957SHuang Rui 			for (i = 0; i < boot_cpu_data.x86_max_cores; i++)
3642517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3643517cb957SHuang Rui 					   *(p_val + i), i);
3644517cb957SHuang Rui 		}
3645517cb957SHuang Rui 
3646517cb957SHuang Rui 		kfree(p_val);
3647517cb957SHuang Rui 	}
3648517cb957SHuang Rui }
3649517cb957SHuang Rui 
3650e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3651e098bc96SEvan Quan {
3652e098bc96SEvan Quan 	uint32_t value;
3653800c53d6SXiaojian Du 	uint64_t value64 = 0;
3654e098bc96SEvan Quan 	uint32_t query = 0;
3655e098bc96SEvan Quan 	int size;
3656e098bc96SEvan Quan 
3657e098bc96SEvan Quan 	/* GPU Clocks */
3658e098bc96SEvan Quan 	size = sizeof(value);
3659e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
3660517cb957SHuang Rui 
3661517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
3662517cb957SHuang Rui 
3663e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3664e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3665e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3666e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3667e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3668e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3669e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3670e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3671e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3672e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3673e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3674e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3675e098bc96SEvan Quan 	size = sizeof(uint32_t);
3676e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3677e098bc96SEvan Quan 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3678e098bc96SEvan Quan 	size = sizeof(value);
3679e098bc96SEvan Quan 	seq_printf(m, "\n");
3680e098bc96SEvan Quan 
3681e098bc96SEvan Quan 	/* GPU Temp */
3682e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3683e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3684e098bc96SEvan Quan 
3685e098bc96SEvan Quan 	/* GPU Load */
3686e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3687e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
3688e098bc96SEvan Quan 	/* MEM Load */
3689e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3690e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
3691e098bc96SEvan Quan 
3692e098bc96SEvan Quan 	seq_printf(m, "\n");
3693e098bc96SEvan Quan 
3694e098bc96SEvan Quan 	/* SMC feature mask */
3695e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3696e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3697e098bc96SEvan Quan 
3698e098bc96SEvan Quan 	if (adev->asic_type > CHIP_VEGA20) {
3699e098bc96SEvan Quan 		/* VCN clocks */
3700e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3701e098bc96SEvan Quan 			if (!value) {
3702e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
3703e098bc96SEvan Quan 			} else {
3704e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
3705e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3706e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3707e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3708e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3709e098bc96SEvan Quan 			}
3710e098bc96SEvan Quan 		}
3711e098bc96SEvan Quan 		seq_printf(m, "\n");
3712e098bc96SEvan Quan 	} else {
3713e098bc96SEvan Quan 		/* UVD clocks */
3714e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3715e098bc96SEvan Quan 			if (!value) {
3716e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
3717e098bc96SEvan Quan 			} else {
3718e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
3719e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3720e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3721e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3722e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3723e098bc96SEvan Quan 			}
3724e098bc96SEvan Quan 		}
3725e098bc96SEvan Quan 		seq_printf(m, "\n");
3726e098bc96SEvan Quan 
3727e098bc96SEvan Quan 		/* VCE clocks */
3728e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3729e098bc96SEvan Quan 			if (!value) {
3730e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
3731e098bc96SEvan Quan 			} else {
3732e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
3733e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3734e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3735e098bc96SEvan Quan 			}
3736e098bc96SEvan Quan 		}
3737e098bc96SEvan Quan 	}
3738e098bc96SEvan Quan 
3739e098bc96SEvan Quan 	return 0;
3740e098bc96SEvan Quan }
3741e098bc96SEvan Quan 
3742e098bc96SEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3743e098bc96SEvan Quan {
3744e098bc96SEvan Quan 	int i;
3745e098bc96SEvan Quan 
3746e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
3747e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3748e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
3749e098bc96SEvan Quan }
3750e098bc96SEvan Quan 
3751e098bc96SEvan Quan static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3752e098bc96SEvan Quan {
3753e098bc96SEvan Quan 	struct drm_info_node *node = (struct drm_info_node *) m->private;
3754e098bc96SEvan Quan 	struct drm_device *dev = node->minor->dev;
37551348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
3756e098bc96SEvan Quan 	u32 flags = 0;
3757e098bc96SEvan Quan 	int r;
3758e098bc96SEvan Quan 
375953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3760e098bc96SEvan Quan 		return -EPERM;
3761e098bc96SEvan Quan 
3762e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
3763e098bc96SEvan Quan 	if (r < 0) {
3764e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3765e098bc96SEvan Quan 		return r;
3766e098bc96SEvan Quan 	}
3767e098bc96SEvan Quan 
3768e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled) {
3769e098bc96SEvan Quan 		seq_printf(m, "dpm not enabled\n");
3770e098bc96SEvan Quan 		pm_runtime_mark_last_busy(dev->dev);
3771e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3772e098bc96SEvan Quan 		return 0;
3773e098bc96SEvan Quan 	}
3774e098bc96SEvan Quan 
3775e098bc96SEvan Quan 	if (!is_support_sw_smu(adev) &&
3776e098bc96SEvan Quan 	    adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3777e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
3778e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3779e098bc96SEvan Quan 			adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3780e098bc96SEvan Quan 		else
3781e098bc96SEvan Quan 			seq_printf(m, "Debugfs support not implemented for this asic\n");
3782e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
3783e098bc96SEvan Quan 		r = 0;
3784e098bc96SEvan Quan 	} else {
3785e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3786e098bc96SEvan Quan 	}
3787e098bc96SEvan Quan 	if (r)
3788e098bc96SEvan Quan 		goto out;
3789e098bc96SEvan Quan 
3790e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3791e098bc96SEvan Quan 
3792e098bc96SEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3793e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
3794e098bc96SEvan Quan 	seq_printf(m, "\n");
3795e098bc96SEvan Quan 
3796e098bc96SEvan Quan out:
3797e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
3798e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
3799e098bc96SEvan Quan 
3800e098bc96SEvan Quan 	return r;
3801e098bc96SEvan Quan }
3802e098bc96SEvan Quan 
3803e098bc96SEvan Quan static const struct drm_info_list amdgpu_pm_info_list[] = {
3804e098bc96SEvan Quan 	{"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3805e098bc96SEvan Quan };
3806e098bc96SEvan Quan #endif
3807e098bc96SEvan Quan 
3808e098bc96SEvan Quan int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3809e098bc96SEvan Quan {
3810e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3811e098bc96SEvan Quan 	return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
3812e098bc96SEvan Quan #else
3813e098bc96SEvan Quan 	return 0;
3814e098bc96SEvan Quan #endif
3815e098bc96SEvan Quan }
3816