xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision 0d8318e11203c2d1ec54ae9a4aad71fb0ecf9c36)
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 "amdgpu.h"
27e098bc96SEvan Quan #include "amdgpu_drv.h"
28e098bc96SEvan Quan #include "amdgpu_pm.h"
29e098bc96SEvan Quan #include "amdgpu_dpm.h"
30e098bc96SEvan Quan #include "atom.h"
31e098bc96SEvan Quan #include <linux/pci.h>
32e098bc96SEvan Quan #include <linux/hwmon.h>
33e098bc96SEvan Quan #include <linux/hwmon-sysfs.h>
34e098bc96SEvan Quan #include <linux/nospec.h>
35e098bc96SEvan Quan #include <linux/pm_runtime.h>
36517cb957SHuang Rui #include <asm/processor.h>
37e098bc96SEvan Quan #include "hwmgr.h"
38e098bc96SEvan Quan 
39e098bc96SEvan Quan static const struct cg_flag_name clocks[] = {
40adf16996SJinzhou.Su 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
41e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
42e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
43e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
44e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
45e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
46e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
47e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
48e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
49e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
50e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
51e098bc96SEvan Quan 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
52e098bc96SEvan Quan 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
53e098bc96SEvan Quan 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
54e098bc96SEvan Quan 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
55e098bc96SEvan Quan 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
56e098bc96SEvan Quan 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
57e098bc96SEvan Quan 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
58e098bc96SEvan Quan 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
59e098bc96SEvan Quan 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
60e098bc96SEvan Quan 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
61e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
62e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
63e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
64e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
6571037bfcSKevin Wang 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
6671037bfcSKevin Wang 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
6771037bfcSKevin Wang 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
6871037bfcSKevin Wang 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
6971037bfcSKevin Wang 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
70e098bc96SEvan Quan 
71e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
72e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
73e098bc96SEvan Quan 	{0, NULL},
74e098bc96SEvan Quan };
75e098bc96SEvan Quan 
76e098bc96SEvan Quan static const struct hwmon_temp_label {
77e098bc96SEvan Quan 	enum PP_HWMON_TEMP channel;
78e098bc96SEvan Quan 	const char *label;
79e098bc96SEvan Quan } temp_label[] = {
80e098bc96SEvan Quan 	{PP_TEMP_EDGE, "edge"},
81e098bc96SEvan Quan 	{PP_TEMP_JUNCTION, "junction"},
82e098bc96SEvan Quan 	{PP_TEMP_MEM, "mem"},
83e098bc96SEvan Quan };
84e098bc96SEvan Quan 
85e098bc96SEvan Quan /**
86e098bc96SEvan Quan  * DOC: power_dpm_state
87e098bc96SEvan Quan  *
88e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
89e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
90e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
91e098bc96SEvan Quan  * It accepts the following arguments:
92e098bc96SEvan Quan  *
93e098bc96SEvan Quan  * - battery
94e098bc96SEvan Quan  *
95e098bc96SEvan Quan  * - balanced
96e098bc96SEvan Quan  *
97e098bc96SEvan Quan  * - performance
98e098bc96SEvan Quan  *
99e098bc96SEvan Quan  * battery
100e098bc96SEvan Quan  *
101e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
102e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
103e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
104e098bc96SEvan Quan  *
105e098bc96SEvan Quan  * balanced
106e098bc96SEvan Quan  *
107e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
108e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
109e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
110e098bc96SEvan Quan  *
111e098bc96SEvan Quan  * performance
112e098bc96SEvan Quan  *
113e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
114e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
115e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
116e098bc96SEvan Quan  *
117e098bc96SEvan Quan  */
118e098bc96SEvan Quan 
119e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
120e098bc96SEvan Quan 					  struct device_attribute *attr,
121e098bc96SEvan Quan 					  char *buf)
122e098bc96SEvan Quan {
123e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1241348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1258dfc8c53SDarren Powell 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
126e098bc96SEvan Quan 	enum amd_pm_state_type pm;
127e098bc96SEvan Quan 	int ret;
128e098bc96SEvan Quan 
12953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
130e098bc96SEvan Quan 		return -EPERM;
131d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
132d2ae842dSAlex Deucher 		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 
1408dfc8c53SDarren Powell 	if (pp_funcs->get_current_power_state) {
141e098bc96SEvan Quan 		pm = amdgpu_dpm_get_current_power_state(adev);
142e098bc96SEvan Quan 	} else {
143e098bc96SEvan Quan 		pm = adev->pm.dpm.user_state;
144e098bc96SEvan Quan 	}
145e098bc96SEvan Quan 
146e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
147e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
148e098bc96SEvan Quan 
149a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
150e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
151e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
152e098bc96SEvan Quan }
153e098bc96SEvan Quan 
154e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
155e098bc96SEvan Quan 					  struct device_attribute *attr,
156e098bc96SEvan Quan 					  const char *buf,
157e098bc96SEvan Quan 					  size_t count)
158e098bc96SEvan Quan {
159e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1601348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
161e098bc96SEvan Quan 	enum amd_pm_state_type  state;
162e098bc96SEvan Quan 	int ret;
163e098bc96SEvan Quan 
16453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
165e098bc96SEvan Quan 		return -EPERM;
166d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
167d2ae842dSAlex Deucher 		return -EPERM;
168e098bc96SEvan Quan 
169e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
170e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
171e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
172e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
173e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
174e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
175e098bc96SEvan Quan 	else
176e098bc96SEvan Quan 		return -EINVAL;
177e098bc96SEvan Quan 
178e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
179e098bc96SEvan Quan 	if (ret < 0) {
180e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
181e098bc96SEvan Quan 		return ret;
182e098bc96SEvan Quan 	}
183e098bc96SEvan Quan 
184e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
185e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
186e098bc96SEvan Quan 		adev->pm.dpm.user_state = state;
187e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
188e098bc96SEvan Quan 	} else if (adev->powerplay.pp_funcs->dispatch_tasks) {
189e098bc96SEvan Quan 		amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
190e098bc96SEvan Quan 	} else {
191e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
192e098bc96SEvan Quan 		adev->pm.dpm.user_state = state;
193e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
194e098bc96SEvan Quan 
195e098bc96SEvan Quan 		amdgpu_pm_compute_clocks(adev);
196e098bc96SEvan Quan 	}
197e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
198e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
199e098bc96SEvan Quan 
200e098bc96SEvan Quan 	return count;
201e098bc96SEvan Quan }
202e098bc96SEvan Quan 
203e098bc96SEvan Quan 
204e098bc96SEvan Quan /**
205e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
206e098bc96SEvan Quan  *
207e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
208e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
209e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
210e098bc96SEvan Quan  *
211e098bc96SEvan Quan  * - auto
212e098bc96SEvan Quan  *
213e098bc96SEvan Quan  * - low
214e098bc96SEvan Quan  *
215e098bc96SEvan Quan  * - high
216e098bc96SEvan Quan  *
217e098bc96SEvan Quan  * - manual
218e098bc96SEvan Quan  *
219e098bc96SEvan Quan  * - profile_standard
220e098bc96SEvan Quan  *
221e098bc96SEvan Quan  * - profile_min_sclk
222e098bc96SEvan Quan  *
223e098bc96SEvan Quan  * - profile_min_mclk
224e098bc96SEvan Quan  *
225e098bc96SEvan Quan  * - profile_peak
226e098bc96SEvan Quan  *
227e098bc96SEvan Quan  * auto
228e098bc96SEvan Quan  *
229e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
230e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
231e098bc96SEvan Quan  *
232e098bc96SEvan Quan  * low
233e098bc96SEvan Quan  *
234e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
235e098bc96SEvan Quan  *
236e098bc96SEvan Quan  * high
237e098bc96SEvan Quan  *
238e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
239e098bc96SEvan Quan  *
240e098bc96SEvan Quan  * manual
241e098bc96SEvan Quan  *
242e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
243e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
244e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
245e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
246e098bc96SEvan Quan  *
247e098bc96SEvan Quan  * profile_standard
248e098bc96SEvan Quan  * profile_min_sclk
249e098bc96SEvan Quan  * profile_min_mclk
250e098bc96SEvan Quan  * profile_peak
251e098bc96SEvan Quan  *
252e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
253e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
254e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
255e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
256e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
257e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
258e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
259e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
260e098bc96SEvan Quan  *
261e098bc96SEvan Quan  */
262e098bc96SEvan Quan 
263e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
264e098bc96SEvan Quan 							    struct device_attribute *attr,
265e098bc96SEvan Quan 							    char *buf)
266e098bc96SEvan Quan {
267e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2681348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
269e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
270e098bc96SEvan Quan 	int ret;
271e098bc96SEvan Quan 
27253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
273e098bc96SEvan Quan 		return -EPERM;
274d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
275d2ae842dSAlex Deucher 		return -EPERM;
276e098bc96SEvan Quan 
277e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
278e098bc96SEvan Quan 	if (ret < 0) {
279e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
280e098bc96SEvan Quan 		return ret;
281e098bc96SEvan Quan 	}
282e098bc96SEvan Quan 
2834df144f8SDarren Powell 	if (adev->powerplay.pp_funcs->get_performance_level)
284e098bc96SEvan Quan 		level = amdgpu_dpm_get_performance_level(adev);
285e098bc96SEvan Quan 	else
286e098bc96SEvan Quan 		level = adev->pm.dpm.forced_level;
287e098bc96SEvan Quan 
288e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
289e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
290e098bc96SEvan Quan 
291a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
292e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
293e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
294e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
295e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
296e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
297e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
298e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
299e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
3006be64246SLijo Lazar 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
301e098bc96SEvan Quan 			  "unknown");
302e098bc96SEvan Quan }
303e098bc96SEvan Quan 
304e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
305e098bc96SEvan Quan 							    struct device_attribute *attr,
306e098bc96SEvan Quan 							    const char *buf,
307e098bc96SEvan Quan 							    size_t count)
308e098bc96SEvan Quan {
309e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3101348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
3118dfc8c53SDarren Powell 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
312e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
313e098bc96SEvan Quan 	enum amd_dpm_forced_level current_level = 0xff;
314e098bc96SEvan Quan 	int ret = 0;
315e098bc96SEvan Quan 
31653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
317e098bc96SEvan Quan 		return -EPERM;
318d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
319d2ae842dSAlex Deucher 		return -EPERM;
320e098bc96SEvan Quan 
321e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
322e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
323e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
324e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
325e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
326e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
327e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
328e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
329e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
330e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
331e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
332e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
333e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
334e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
335e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
336e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
337e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
338e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
3396be64246SLijo Lazar 	} else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
3406be64246SLijo Lazar 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
341e098bc96SEvan Quan 	}  else {
342e098bc96SEvan Quan 		return -EINVAL;
343e098bc96SEvan Quan 	}
344e098bc96SEvan Quan 
345e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
346e098bc96SEvan Quan 	if (ret < 0) {
347e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
348e098bc96SEvan Quan 		return ret;
349e098bc96SEvan Quan 	}
350e098bc96SEvan Quan 
3518dfc8c53SDarren Powell 	if (pp_funcs->get_performance_level)
352e098bc96SEvan Quan 		current_level = amdgpu_dpm_get_performance_level(adev);
353e098bc96SEvan Quan 
354e098bc96SEvan Quan 	if (current_level == level) {
355e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
356e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
357e098bc96SEvan Quan 		return count;
358e098bc96SEvan Quan 	}
359e098bc96SEvan Quan 
360e098bc96SEvan Quan 	if (adev->asic_type == CHIP_RAVEN) {
361e098bc96SEvan Quan 		if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) {
362e098bc96SEvan Quan 			if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL)
363e098bc96SEvan Quan 				amdgpu_gfx_off_ctrl(adev, false);
364e098bc96SEvan Quan 			else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL)
365e098bc96SEvan Quan 				amdgpu_gfx_off_ctrl(adev, true);
366e098bc96SEvan Quan 		}
367e098bc96SEvan Quan 	}
368e098bc96SEvan Quan 
369e098bc96SEvan Quan 	/* profile_exit setting is valid only when current mode is in profile mode */
370e098bc96SEvan Quan 	if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
371e098bc96SEvan Quan 	    AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
372e098bc96SEvan Quan 	    AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
373e098bc96SEvan Quan 	    AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
374e098bc96SEvan Quan 	    (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
375e098bc96SEvan Quan 		pr_err("Currently not in any profile mode!\n");
376e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
377e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
378e098bc96SEvan Quan 		return -EINVAL;
379e098bc96SEvan Quan 	}
380e098bc96SEvan Quan 
3818f4828d0SDarren Powell 	if (pp_funcs->force_performance_level) {
382e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
383e098bc96SEvan Quan 		if (adev->pm.dpm.thermal_active) {
384e098bc96SEvan Quan 			mutex_unlock(&adev->pm.mutex);
385e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
386e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
387e098bc96SEvan Quan 			return -EINVAL;
388e098bc96SEvan Quan 		}
389e098bc96SEvan Quan 		ret = amdgpu_dpm_force_performance_level(adev, level);
390e098bc96SEvan Quan 		if (ret) {
391e098bc96SEvan Quan 			mutex_unlock(&adev->pm.mutex);
392e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
393e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
394e098bc96SEvan Quan 			return -EINVAL;
395e098bc96SEvan Quan 		} else {
396e098bc96SEvan Quan 			adev->pm.dpm.forced_level = level;
397e098bc96SEvan Quan 		}
398e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
399e098bc96SEvan Quan 	}
400e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
401e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
402e098bc96SEvan Quan 
403e098bc96SEvan Quan 	return count;
404e098bc96SEvan Quan }
405e098bc96SEvan Quan 
406e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
407e098bc96SEvan Quan 		struct device_attribute *attr,
408e098bc96SEvan Quan 		char *buf)
409e098bc96SEvan Quan {
410e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4111348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
4128dfc8c53SDarren Powell 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
413e098bc96SEvan Quan 	struct pp_states_info data;
41409b6744cSDarren Powell 	uint32_t i;
41509b6744cSDarren Powell 	int buf_len, ret;
416e098bc96SEvan Quan 
41753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
418e098bc96SEvan Quan 		return -EPERM;
419d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
420d2ae842dSAlex Deucher 		return -EPERM;
421e098bc96SEvan Quan 
422e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
423e098bc96SEvan Quan 	if (ret < 0) {
424e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
425e098bc96SEvan Quan 		return ret;
426e098bc96SEvan Quan 	}
427e098bc96SEvan Quan 
4288dfc8c53SDarren Powell 	if (pp_funcs->get_pp_num_states) {
429e098bc96SEvan Quan 		amdgpu_dpm_get_pp_num_states(adev, &data);
430e098bc96SEvan Quan 	} else {
431e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
432e098bc96SEvan Quan 	}
433e098bc96SEvan Quan 
434e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
435e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
436e098bc96SEvan Quan 
43709b6744cSDarren Powell 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
438e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
43909b6744cSDarren Powell 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
440e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
441e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
442e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
443e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
444e098bc96SEvan Quan 
445e098bc96SEvan Quan 	return buf_len;
446e098bc96SEvan Quan }
447e098bc96SEvan Quan 
448e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
449e098bc96SEvan Quan 		struct device_attribute *attr,
450e098bc96SEvan Quan 		char *buf)
451e098bc96SEvan Quan {
452e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4531348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
4548dfc8c53SDarren Powell 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
4552b24c199STom Rix 	struct pp_states_info data = {0};
456e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
457e098bc96SEvan Quan 	int i = 0, ret = 0;
458e098bc96SEvan Quan 
45953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
460e098bc96SEvan Quan 		return -EPERM;
461d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
462d2ae842dSAlex Deucher 		return -EPERM;
463e098bc96SEvan Quan 
464e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
465e098bc96SEvan Quan 	if (ret < 0) {
466e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
467e098bc96SEvan Quan 		return ret;
468e098bc96SEvan Quan 	}
469e098bc96SEvan Quan 
4708dfc8c53SDarren Powell 	if (pp_funcs->get_current_power_state
4718dfc8c53SDarren Powell 		 && pp_funcs->get_pp_num_states) {
472e098bc96SEvan Quan 		pm = amdgpu_dpm_get_current_power_state(adev);
473e098bc96SEvan Quan 		amdgpu_dpm_get_pp_num_states(adev, &data);
474e098bc96SEvan Quan 	}
475e098bc96SEvan Quan 
476e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
477e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
478e098bc96SEvan Quan 
479e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
480e098bc96SEvan Quan 		if (pm == data.states[i])
481e098bc96SEvan Quan 			break;
482e098bc96SEvan Quan 	}
483e098bc96SEvan Quan 
484e098bc96SEvan Quan 	if (i == data.nums)
485e098bc96SEvan Quan 		i = -EINVAL;
486e098bc96SEvan Quan 
487a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", i);
488e098bc96SEvan Quan }
489e098bc96SEvan Quan 
490e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
491e098bc96SEvan Quan 		struct device_attribute *attr,
492e098bc96SEvan Quan 		char *buf)
493e098bc96SEvan Quan {
494e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4951348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
496e098bc96SEvan Quan 
49753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
498e098bc96SEvan Quan 		return -EPERM;
499d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
500d2ae842dSAlex Deucher 		return -EPERM;
501e098bc96SEvan Quan 
502e098bc96SEvan Quan 	if (adev->pp_force_state_enabled)
503e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
504e098bc96SEvan Quan 	else
505a9ca9bb3STian Tao 		return sysfs_emit(buf, "\n");
506e098bc96SEvan Quan }
507e098bc96SEvan Quan 
508e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
509e098bc96SEvan Quan 		struct device_attribute *attr,
510e098bc96SEvan Quan 		const char *buf,
511e098bc96SEvan Quan 		size_t count)
512e098bc96SEvan Quan {
513e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5141348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
515e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
516e098bc96SEvan Quan 	unsigned long idx;
517e098bc96SEvan Quan 	int ret;
518e098bc96SEvan Quan 
51953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
520e098bc96SEvan Quan 		return -EPERM;
521d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
522d2ae842dSAlex Deucher 		return -EPERM;
523e098bc96SEvan Quan 
524e098bc96SEvan Quan 	if (strlen(buf) == 1)
525e098bc96SEvan Quan 		adev->pp_force_state_enabled = false;
526e098bc96SEvan Quan 	else if (is_support_sw_smu(adev))
527e098bc96SEvan Quan 		adev->pp_force_state_enabled = false;
528e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->dispatch_tasks &&
529e098bc96SEvan Quan 			adev->powerplay.pp_funcs->get_pp_num_states) {
530e098bc96SEvan Quan 		struct pp_states_info data;
531e098bc96SEvan Quan 
532e098bc96SEvan Quan 		ret = kstrtoul(buf, 0, &idx);
533e098bc96SEvan Quan 		if (ret || idx >= ARRAY_SIZE(data.states))
534e098bc96SEvan Quan 			return -EINVAL;
535e098bc96SEvan Quan 
536e098bc96SEvan Quan 		idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
537e098bc96SEvan Quan 
538e098bc96SEvan Quan 		amdgpu_dpm_get_pp_num_states(adev, &data);
539e098bc96SEvan Quan 		state = data.states[idx];
540e098bc96SEvan Quan 
541e098bc96SEvan Quan 		ret = pm_runtime_get_sync(ddev->dev);
542e098bc96SEvan Quan 		if (ret < 0) {
543e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
544e098bc96SEvan Quan 			return ret;
545e098bc96SEvan Quan 		}
546e098bc96SEvan Quan 
547e098bc96SEvan Quan 		/* only set user selected power states */
548e098bc96SEvan Quan 		if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
549e098bc96SEvan Quan 		    state != POWER_STATE_TYPE_DEFAULT) {
550e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev,
551e098bc96SEvan Quan 					AMD_PP_TASK_ENABLE_USER_STATE, &state);
552e098bc96SEvan Quan 			adev->pp_force_state_enabled = true;
553e098bc96SEvan Quan 		}
554e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
555e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
556e098bc96SEvan Quan 	}
557e098bc96SEvan Quan 
558e098bc96SEvan Quan 	return count;
559e098bc96SEvan Quan }
560e098bc96SEvan Quan 
561e098bc96SEvan Quan /**
562e098bc96SEvan Quan  * DOC: pp_table
563e098bc96SEvan Quan  *
564e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
565e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
566e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
567e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
568e098bc96SEvan Quan  * powerplay using that new table.
569e098bc96SEvan Quan  *
570e098bc96SEvan Quan  */
571e098bc96SEvan Quan 
572e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
573e098bc96SEvan Quan 		struct device_attribute *attr,
574e098bc96SEvan Quan 		char *buf)
575e098bc96SEvan Quan {
576e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5771348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
578e098bc96SEvan Quan 	char *table = NULL;
579e098bc96SEvan Quan 	int size, ret;
580e098bc96SEvan Quan 
58153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
582e098bc96SEvan Quan 		return -EPERM;
583d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
584d2ae842dSAlex Deucher 		return -EPERM;
585e098bc96SEvan Quan 
586e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
587e098bc96SEvan Quan 	if (ret < 0) {
588e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
589e098bc96SEvan Quan 		return ret;
590e098bc96SEvan Quan 	}
591e098bc96SEvan Quan 
5928dfc8c53SDarren Powell 	if (adev->powerplay.pp_funcs->get_pp_table) {
593e098bc96SEvan Quan 		size = amdgpu_dpm_get_pp_table(adev, &table);
594e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
595e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
596e098bc96SEvan Quan 		if (size < 0)
597e098bc96SEvan Quan 			return size;
598e098bc96SEvan Quan 	} else {
599e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
600e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
601e098bc96SEvan Quan 		return 0;
602e098bc96SEvan Quan 	}
603e098bc96SEvan Quan 
604e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
605e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
606e098bc96SEvan Quan 
607e098bc96SEvan Quan 	memcpy(buf, table, size);
608e098bc96SEvan Quan 
609e098bc96SEvan Quan 	return size;
610e098bc96SEvan Quan }
611e098bc96SEvan Quan 
612e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
613e098bc96SEvan Quan 		struct device_attribute *attr,
614e098bc96SEvan Quan 		const char *buf,
615e098bc96SEvan Quan 		size_t count)
616e098bc96SEvan Quan {
617e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
6181348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
619e098bc96SEvan Quan 	int ret = 0;
620e098bc96SEvan Quan 
62153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
622e098bc96SEvan Quan 		return -EPERM;
623d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
624d2ae842dSAlex Deucher 		return -EPERM;
625e098bc96SEvan Quan 
626e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
627e098bc96SEvan Quan 	if (ret < 0) {
628e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
629e098bc96SEvan Quan 		return ret;
630e098bc96SEvan Quan 	}
631e098bc96SEvan Quan 
6328f4828d0SDarren Powell 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
633e098bc96SEvan Quan 	if (ret) {
634e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
635e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
636e098bc96SEvan Quan 		return ret;
637e098bc96SEvan Quan 	}
638e098bc96SEvan Quan 
639e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
640e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
641e098bc96SEvan Quan 
642e098bc96SEvan Quan 	return count;
643e098bc96SEvan Quan }
644e098bc96SEvan Quan 
645e098bc96SEvan Quan /**
646e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
647e098bc96SEvan Quan  *
648e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
649e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
650e098bc96SEvan Quan  * this.
651e098bc96SEvan Quan  *
652e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
653e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
654e098bc96SEvan Quan  * following formula:
655e098bc96SEvan Quan  *
656e098bc96SEvan Quan  * Clock conversion (Mhz):
657e098bc96SEvan Quan  *
658e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
659e098bc96SEvan Quan  *
660e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
661e098bc96SEvan Quan  *
662e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
663e098bc96SEvan Quan  *
664e098bc96SEvan Quan  * DRAM data rate (MT/s):
665e098bc96SEvan Quan  *
666e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
667e098bc96SEvan Quan  *
668e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
669e098bc96SEvan Quan  *
670e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
671e098bc96SEvan Quan  *
672e098bc96SEvan Quan  * Bandwidth (MB/s):
673e098bc96SEvan Quan  *
674e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
675e098bc96SEvan Quan  *
676e098bc96SEvan Quan  * Some examples:
677e098bc96SEvan Quan  *
678e098bc96SEvan Quan  * G5 on RX460:
679e098bc96SEvan Quan  *
680e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
681e098bc96SEvan Quan  *
682e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
683e098bc96SEvan Quan  *
684e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
685e098bc96SEvan Quan  *
686e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
687e098bc96SEvan Quan  *
688e098bc96SEvan Quan  * G6 on RX5700:
689e098bc96SEvan Quan  *
690e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
691e098bc96SEvan Quan  *
692e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
693e098bc96SEvan Quan  *
694e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
695e098bc96SEvan Quan  *
696e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
697e098bc96SEvan Quan  *
698e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
699e098bc96SEvan Quan  *
700e098bc96SEvan Quan  * Reading the file will display:
701e098bc96SEvan Quan  *
702e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
703e098bc96SEvan Quan  *
704e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
705e098bc96SEvan Quan  *
706e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
707e098bc96SEvan Quan  *
708e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
709e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
710e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
711e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
712e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
713e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
714e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
715e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
716e098bc96SEvan Quan  *
717e098bc96SEvan Quan  *
718e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
719e098bc96SEvan Quan  *
720e098bc96SEvan Quan  * Reading the file will display:
721e098bc96SEvan Quan  *
722e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
723e098bc96SEvan Quan  *
72437a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
72537a58f69SEvan Quan  *   clock labeled OD_MCLK
726e098bc96SEvan Quan  *
727e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
728e098bc96SEvan Quan  *   They can be used to calibrate the sclk voltage curve.
729e098bc96SEvan Quan  *
730a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
731a2b6df4fSEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder and Dimgrey
732a2b6df4fSEvan Quan  *   Cavefish. For these ASICs, the target voltage calculation can be
733a2b6df4fSEvan Quan  *   illustrated by "voltage = voltage calculated from v/f curve +
734a2b6df4fSEvan Quan  *   overdrive vddgfx offset"
735a2b6df4fSEvan Quan  *
736e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage curve points
737e098bc96SEvan Quan  *   labeled OD_RANGE
738e098bc96SEvan Quan  *
7390487bbb4SAlex Deucher  * < For APUs >
7400487bbb4SAlex Deucher  *
7410487bbb4SAlex Deucher  * Reading the file will display:
7420487bbb4SAlex Deucher  *
7430487bbb4SAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7440487bbb4SAlex Deucher  *
7450487bbb4SAlex Deucher  * - a list of valid ranges for sclk labeled OD_RANGE
7460487bbb4SAlex Deucher  *
7473dc8077fSAlex Deucher  * < For VanGogh >
7483dc8077fSAlex Deucher  *
7493dc8077fSAlex Deucher  * Reading the file will display:
7503dc8077fSAlex Deucher  *
7513dc8077fSAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7523dc8077fSAlex Deucher  * - minimum and maximum core clocks labeled OD_CCLK
7533dc8077fSAlex Deucher  *
7543dc8077fSAlex Deucher  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
7553dc8077fSAlex Deucher  *
756e098bc96SEvan Quan  * To manually adjust these settings:
757e098bc96SEvan Quan  *
758e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
759e098bc96SEvan Quan  *
760e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
761e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
762e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
763e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
7643dc8077fSAlex Deucher  *   "m 1 800" will update maximum mclk to be 800Mhz. For core
7653dc8077fSAlex Deucher  *   clocks on VanGogh, the string contains "p core index clock".
7663dc8077fSAlex Deucher  *   E.g., "p 2 0 800" would set the minimum core clock on core
7673dc8077fSAlex Deucher  *   2 to 800Mhz.
768e098bc96SEvan Quan  *
769e098bc96SEvan Quan  *   For sclk voltage curve, enter the new values by writing a
770e098bc96SEvan Quan  *   string that contains "vc point clock voltage" to the file. The
771e098bc96SEvan Quan  *   points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
772e098bc96SEvan Quan  *   update point1 with clock set as 300Mhz and voltage as
773e098bc96SEvan Quan  *   600mV. "vc 2 1000 1000" will update point3 with clock set
774e098bc96SEvan Quan  *   as 1000Mhz and voltage 1000mV.
775e098bc96SEvan Quan  *
776a2b6df4fSEvan Quan  *   To update the voltage offset applied for gfxclk/voltage calculation,
777a2b6df4fSEvan Quan  *   enter the new value by writing a string that contains "vo offset".
778a2b6df4fSEvan Quan  *   This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
779a2b6df4fSEvan Quan  *   And the offset can be a positive or negative value.
780a2b6df4fSEvan Quan  *
781e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
782e098bc96SEvan Quan  *   to the file to commit your changes
783e098bc96SEvan Quan  *
784e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
785e098bc96SEvan Quan  *   to the file to reset them
786e098bc96SEvan Quan  *
787e098bc96SEvan Quan  */
788e098bc96SEvan Quan 
789e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
790e098bc96SEvan Quan 		struct device_attribute *attr,
791e098bc96SEvan Quan 		const char *buf,
792e098bc96SEvan Quan 		size_t count)
793e098bc96SEvan Quan {
794e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7951348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
796e098bc96SEvan Quan 	int ret;
797e098bc96SEvan Quan 	uint32_t parameter_size = 0;
798e098bc96SEvan Quan 	long parameter[64];
799e098bc96SEvan Quan 	char buf_cpy[128];
800e098bc96SEvan Quan 	char *tmp_str;
801e098bc96SEvan Quan 	char *sub_str;
802e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
803e098bc96SEvan Quan 	uint32_t type;
804e098bc96SEvan Quan 
80553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
806e098bc96SEvan Quan 		return -EPERM;
807d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
808d2ae842dSAlex Deucher 		return -EPERM;
809e098bc96SEvan Quan 
810e098bc96SEvan Quan 	if (count > 127)
811e098bc96SEvan Quan 		return -EINVAL;
812e098bc96SEvan Quan 
813e098bc96SEvan Quan 	if (*buf == 's')
814e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
8150d90d0ddSHuang Rui 	else if (*buf == 'p')
8160d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
817e098bc96SEvan Quan 	else if (*buf == 'm')
818e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
819e098bc96SEvan Quan 	else if(*buf == 'r')
820e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
821e098bc96SEvan Quan 	else if (*buf == 'c')
822e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
823e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
824e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
825a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
826a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
827e098bc96SEvan Quan 	else
828e098bc96SEvan Quan 		return -EINVAL;
829e098bc96SEvan Quan 
830e098bc96SEvan Quan 	memcpy(buf_cpy, buf, count+1);
831e098bc96SEvan Quan 
832e098bc96SEvan Quan 	tmp_str = buf_cpy;
833e098bc96SEvan Quan 
834a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
835a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
836e098bc96SEvan Quan 		tmp_str++;
837e098bc96SEvan Quan 	while (isspace(*++tmp_str));
838e098bc96SEvan Quan 
839ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
840aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
841aec1d870SMatt Coffin 			continue;
842e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
843e098bc96SEvan Quan 		if (ret)
844e098bc96SEvan Quan 			return -EINVAL;
845e098bc96SEvan Quan 		parameter_size++;
846e098bc96SEvan Quan 
847e098bc96SEvan Quan 		while (isspace(*tmp_str))
848e098bc96SEvan Quan 			tmp_str++;
849e098bc96SEvan Quan 	}
850e098bc96SEvan Quan 
851e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
852e098bc96SEvan Quan 	if (ret < 0) {
853e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
854e098bc96SEvan Quan 		return ret;
855e098bc96SEvan Quan 	}
856e098bc96SEvan Quan 
85712a6727dSXiaojian Du 	if (adev->powerplay.pp_funcs->set_fine_grain_clk_vol) {
85812a6727dSXiaojian Du 		ret = amdgpu_dpm_set_fine_grain_clk_vol(adev, type,
85912a6727dSXiaojian Du 							parameter,
86012a6727dSXiaojian Du 							parameter_size);
86112a6727dSXiaojian Du 		if (ret) {
86212a6727dSXiaojian Du 			pm_runtime_mark_last_busy(ddev->dev);
86312a6727dSXiaojian Du 			pm_runtime_put_autosuspend(ddev->dev);
86412a6727dSXiaojian Du 			return -EINVAL;
86512a6727dSXiaojian Du 		}
86612a6727dSXiaojian Du 	}
86712a6727dSXiaojian Du 
868e098bc96SEvan Quan 	if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
869e098bc96SEvan Quan 		ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
870e098bc96SEvan Quan 						    parameter, parameter_size);
871e098bc96SEvan Quan 		if (ret) {
872e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
873e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
874e098bc96SEvan Quan 			return -EINVAL;
875e098bc96SEvan Quan 		}
876e098bc96SEvan Quan 	}
877e098bc96SEvan Quan 
878e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
879e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
880e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev,
881e098bc96SEvan Quan 						 AMD_PP_TASK_READJUST_POWER_STATE,
882e098bc96SEvan Quan 						 NULL);
883e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
884e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
885e098bc96SEvan Quan 			return count;
886e098bc96SEvan Quan 		} else {
887e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
888e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
889e098bc96SEvan Quan 			return -EINVAL;
890e098bc96SEvan Quan 		}
891e098bc96SEvan Quan 	}
8928f4828d0SDarren Powell 
893e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
894e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
895e098bc96SEvan Quan 
896e098bc96SEvan Quan 	return count;
897e098bc96SEvan Quan }
898e098bc96SEvan Quan 
899e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
900e098bc96SEvan Quan 		struct device_attribute *attr,
901e098bc96SEvan Quan 		char *buf)
902e098bc96SEvan Quan {
903e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9041348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
905e098bc96SEvan Quan 	ssize_t size;
906e098bc96SEvan Quan 	int ret;
907e098bc96SEvan Quan 
90853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
909e098bc96SEvan Quan 		return -EPERM;
910d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
911d2ae842dSAlex Deucher 		return -EPERM;
912e098bc96SEvan Quan 
913e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
914e098bc96SEvan Quan 	if (ret < 0) {
915e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
916e098bc96SEvan Quan 		return ret;
917e098bc96SEvan Quan 	}
918e098bc96SEvan Quan 
9198f4828d0SDarren Powell 	if (adev->powerplay.pp_funcs->print_clock_levels) {
920e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
921e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
922e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
9238f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf+size);
924e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
9258f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf+size);
926e098bc96SEvan Quan 	} else {
92709b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
928e098bc96SEvan Quan 	}
929e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
930e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
931e098bc96SEvan Quan 
932e098bc96SEvan Quan 	return size;
933e098bc96SEvan Quan }
934e098bc96SEvan Quan 
935e098bc96SEvan Quan /**
936e098bc96SEvan Quan  * DOC: pp_features
937e098bc96SEvan Quan  *
938e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
939e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
940e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
941e098bc96SEvan Quan  *
942e098bc96SEvan Quan  * Reading back the file will show you the followings:
943e098bc96SEvan Quan  * - Current ppfeature masks
944e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
945e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
946e098bc96SEvan Quan  *
947e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
948e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
949e098bc96SEvan Quan  * new ppfeature masks.
950e098bc96SEvan Quan  */
951e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
952e098bc96SEvan Quan 				      struct device_attribute *attr,
953e098bc96SEvan Quan 				      const char *buf,
954e098bc96SEvan Quan 				      size_t count)
955e098bc96SEvan Quan {
956e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9571348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
958e098bc96SEvan Quan 	uint64_t featuremask;
959e098bc96SEvan Quan 	int ret;
960e098bc96SEvan Quan 
96153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
962e098bc96SEvan Quan 		return -EPERM;
963d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
964d2ae842dSAlex Deucher 		return -EPERM;
965e098bc96SEvan Quan 
966e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
967e098bc96SEvan Quan 	if (ret)
968e098bc96SEvan Quan 		return -EINVAL;
969e098bc96SEvan Quan 
970e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
971e098bc96SEvan Quan 	if (ret < 0) {
972e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
973e098bc96SEvan Quan 		return ret;
974e098bc96SEvan Quan 	}
975e098bc96SEvan Quan 
976c6ce68e6SEvan Quan 	if (adev->powerplay.pp_funcs->set_ppfeature_status) {
977e098bc96SEvan Quan 		ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
978e098bc96SEvan Quan 		if (ret) {
979e098bc96SEvan Quan 			pm_runtime_mark_last_busy(ddev->dev);
980e098bc96SEvan Quan 			pm_runtime_put_autosuspend(ddev->dev);
981e098bc96SEvan Quan 			return -EINVAL;
982e098bc96SEvan Quan 		}
983e098bc96SEvan Quan 	}
984e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
985e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
986e098bc96SEvan Quan 
987e098bc96SEvan Quan 	return count;
988e098bc96SEvan Quan }
989e098bc96SEvan Quan 
990e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
991e098bc96SEvan Quan 				      struct device_attribute *attr,
992e098bc96SEvan Quan 				      char *buf)
993e098bc96SEvan Quan {
994e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9951348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
996e098bc96SEvan Quan 	ssize_t size;
997e098bc96SEvan Quan 	int ret;
998e098bc96SEvan Quan 
99953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1000e098bc96SEvan Quan 		return -EPERM;
1001d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1002d2ae842dSAlex Deucher 		return -EPERM;
1003e098bc96SEvan Quan 
1004e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1005e098bc96SEvan Quan 	if (ret < 0) {
1006e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1007e098bc96SEvan Quan 		return ret;
1008e098bc96SEvan Quan 	}
1009e098bc96SEvan Quan 
10108dfc8c53SDarren Powell 	if (adev->powerplay.pp_funcs->get_ppfeature_status)
1011e098bc96SEvan Quan 		size = amdgpu_dpm_get_ppfeature_status(adev, buf);
1012e098bc96SEvan Quan 	else
101309b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1014e098bc96SEvan Quan 
1015e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1016e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1017e098bc96SEvan Quan 
1018e098bc96SEvan Quan 	return size;
1019e098bc96SEvan Quan }
1020e098bc96SEvan Quan 
1021e098bc96SEvan Quan /**
1022e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
1023e098bc96SEvan Quan  *
1024e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
1025e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
1026e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
1027e098bc96SEvan Quan  * this.
1028e098bc96SEvan Quan  *
1029e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
1030e098bc96SEvan Quan  * Vega10 and later ASICs.
1031e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
1032e098bc96SEvan Quan  *
1033e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
1034e098bc96SEvan Quan  * the power state and the clock information for those levels.
1035e098bc96SEvan Quan  *
1036e098bc96SEvan Quan  * To manually adjust these states, first select manual using
1037e098bc96SEvan Quan  * power_dpm_force_performance_level.
1038e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
1039e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1040e098bc96SEvan Quan  * E.g.,
1041e098bc96SEvan Quan  *
1042e098bc96SEvan Quan  * .. code-block:: bash
1043e098bc96SEvan Quan  *
1044e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
1045e098bc96SEvan Quan  *
1046e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
1047e098bc96SEvan Quan  *
1048e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1049e098bc96SEvan Quan  */
1050e098bc96SEvan Quan 
10512ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
10522ea092e5SDarren Powell 		enum pp_clock_type type,
1053e098bc96SEvan Quan 		char *buf)
1054e098bc96SEvan Quan {
1055e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10561348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1057e098bc96SEvan Quan 	ssize_t size;
1058e098bc96SEvan Quan 	int ret;
1059e098bc96SEvan Quan 
106053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1061e098bc96SEvan Quan 		return -EPERM;
1062d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1063d2ae842dSAlex Deucher 		return -EPERM;
1064e098bc96SEvan Quan 
1065e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1066e098bc96SEvan Quan 	if (ret < 0) {
1067e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1068e098bc96SEvan Quan 		return ret;
1069e098bc96SEvan Quan 	}
1070e098bc96SEvan Quan 
10712ea092e5SDarren Powell 	if (adev->powerplay.pp_funcs->print_clock_levels)
10722ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1073e098bc96SEvan Quan 	else
107409b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1075e098bc96SEvan Quan 
1076e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1077e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1078e098bc96SEvan Quan 
1079e098bc96SEvan Quan 	return size;
1080e098bc96SEvan Quan }
1081e098bc96SEvan Quan 
1082e098bc96SEvan Quan /*
1083e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1084e098bc96SEvan Quan  * per line (+1 for \n).
1085e098bc96SEvan Quan  */
1086e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1087e098bc96SEvan Quan 
1088e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1089e098bc96SEvan Quan {
1090e098bc96SEvan Quan 	int ret;
1091c915ef89SDan Carpenter 	unsigned long level;
1092e098bc96SEvan Quan 	char *sub_str = NULL;
1093e098bc96SEvan Quan 	char *tmp;
1094e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1095e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1096e098bc96SEvan Quan 	size_t bytes;
1097e098bc96SEvan Quan 
1098e098bc96SEvan Quan 	*mask = 0;
1099e098bc96SEvan Quan 
1100e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1101e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1102e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1103e098bc96SEvan Quan 	tmp = buf_cpy;
1104ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1105e098bc96SEvan Quan 		if (strlen(sub_str)) {
1106c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1107c915ef89SDan Carpenter 			if (ret || level > 31)
1108e098bc96SEvan Quan 				return -EINVAL;
1109e098bc96SEvan Quan 			*mask |= 1 << level;
1110e098bc96SEvan Quan 		} else
1111e098bc96SEvan Quan 			break;
1112e098bc96SEvan Quan 	}
1113e098bc96SEvan Quan 
1114e098bc96SEvan Quan 	return 0;
1115e098bc96SEvan Quan }
1116e098bc96SEvan Quan 
11172ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
11182ea092e5SDarren Powell 		enum pp_clock_type type,
1119e098bc96SEvan Quan 		const char *buf,
1120e098bc96SEvan Quan 		size_t count)
1121e098bc96SEvan Quan {
1122e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
11231348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1124e098bc96SEvan Quan 	int ret;
1125e098bc96SEvan Quan 	uint32_t mask = 0;
1126e098bc96SEvan Quan 
112753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1128e098bc96SEvan Quan 		return -EPERM;
1129d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1130d2ae842dSAlex Deucher 		return -EPERM;
1131e098bc96SEvan Quan 
1132e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1133e098bc96SEvan Quan 	if (ret)
1134e098bc96SEvan Quan 		return ret;
1135e098bc96SEvan Quan 
1136e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1137e098bc96SEvan Quan 	if (ret < 0) {
1138e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1139e098bc96SEvan Quan 		return ret;
1140e098bc96SEvan Quan 	}
1141e098bc96SEvan Quan 
11422ea092e5SDarren Powell 	if (adev->powerplay.pp_funcs->force_clock_level)
11432ea092e5SDarren Powell 		ret = amdgpu_dpm_force_clock_level(adev, type, mask);
11442ea092e5SDarren Powell 	else
11452ea092e5SDarren Powell 		ret = 0;
1146e098bc96SEvan Quan 
1147e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1148e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1149e098bc96SEvan Quan 
1150e098bc96SEvan Quan 	if (ret)
1151e098bc96SEvan Quan 		return -EINVAL;
1152e098bc96SEvan Quan 
1153e098bc96SEvan Quan 	return count;
1154e098bc96SEvan Quan }
1155e098bc96SEvan Quan 
11562ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
11572ea092e5SDarren Powell 		struct device_attribute *attr,
11582ea092e5SDarren Powell 		char *buf)
11592ea092e5SDarren Powell {
11602ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
11612ea092e5SDarren Powell }
11622ea092e5SDarren Powell 
11632ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
11642ea092e5SDarren Powell 		struct device_attribute *attr,
11652ea092e5SDarren Powell 		const char *buf,
11662ea092e5SDarren Powell 		size_t count)
11672ea092e5SDarren Powell {
11682ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
11692ea092e5SDarren Powell }
11702ea092e5SDarren Powell 
1171e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1172e098bc96SEvan Quan 		struct device_attribute *attr,
1173e098bc96SEvan Quan 		char *buf)
1174e098bc96SEvan Quan {
11752ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1176e098bc96SEvan Quan }
1177e098bc96SEvan Quan 
1178e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1179e098bc96SEvan Quan 		struct device_attribute *attr,
1180e098bc96SEvan Quan 		const char *buf,
1181e098bc96SEvan Quan 		size_t count)
1182e098bc96SEvan Quan {
11832ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1184e098bc96SEvan Quan }
1185e098bc96SEvan Quan 
1186e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1187e098bc96SEvan Quan 		struct device_attribute *attr,
1188e098bc96SEvan Quan 		char *buf)
1189e098bc96SEvan Quan {
11902ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1191e098bc96SEvan Quan }
1192e098bc96SEvan Quan 
1193e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1194e098bc96SEvan Quan 		struct device_attribute *attr,
1195e098bc96SEvan Quan 		const char *buf,
1196e098bc96SEvan Quan 		size_t count)
1197e098bc96SEvan Quan {
11982ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1199e098bc96SEvan Quan }
1200e098bc96SEvan Quan 
1201e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1202e098bc96SEvan Quan 		struct device_attribute *attr,
1203e098bc96SEvan Quan 		char *buf)
1204e098bc96SEvan Quan {
12052ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1206e098bc96SEvan Quan }
1207e098bc96SEvan Quan 
1208e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1209e098bc96SEvan Quan 		struct device_attribute *attr,
1210e098bc96SEvan Quan 		const char *buf,
1211e098bc96SEvan Quan 		size_t count)
1212e098bc96SEvan Quan {
12132ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1214e098bc96SEvan Quan }
1215e098bc96SEvan Quan 
12169577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
12179577b0ecSXiaojian Du 		struct device_attribute *attr,
12189577b0ecSXiaojian Du 		char *buf)
12199577b0ecSXiaojian Du {
12202ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
12219577b0ecSXiaojian Du }
12229577b0ecSXiaojian Du 
12239577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
12249577b0ecSXiaojian Du 		struct device_attribute *attr,
12259577b0ecSXiaojian Du 		const char *buf,
12269577b0ecSXiaojian Du 		size_t count)
12279577b0ecSXiaojian Du {
12282ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
12299577b0ecSXiaojian Du }
12309577b0ecSXiaojian Du 
12319577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
12329577b0ecSXiaojian Du 		struct device_attribute *attr,
12339577b0ecSXiaojian Du 		char *buf)
12349577b0ecSXiaojian Du {
12352ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
12369577b0ecSXiaojian Du }
12379577b0ecSXiaojian Du 
12389577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
12399577b0ecSXiaojian Du 		struct device_attribute *attr,
12409577b0ecSXiaojian Du 		const char *buf,
12419577b0ecSXiaojian Du 		size_t count)
12429577b0ecSXiaojian Du {
12432ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
12449577b0ecSXiaojian Du }
12459577b0ecSXiaojian Du 
1246e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1247e098bc96SEvan Quan 		struct device_attribute *attr,
1248e098bc96SEvan Quan 		char *buf)
1249e098bc96SEvan Quan {
12502ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1251e098bc96SEvan Quan }
1252e098bc96SEvan Quan 
1253e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1254e098bc96SEvan Quan 		struct device_attribute *attr,
1255e098bc96SEvan Quan 		const char *buf,
1256e098bc96SEvan Quan 		size_t count)
1257e098bc96SEvan Quan {
12582ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1259e098bc96SEvan Quan }
1260e098bc96SEvan Quan 
1261e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1262e098bc96SEvan Quan 		struct device_attribute *attr,
1263e098bc96SEvan Quan 		char *buf)
1264e098bc96SEvan Quan {
12652ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1266e098bc96SEvan Quan }
1267e098bc96SEvan Quan 
1268e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1269e098bc96SEvan Quan 		struct device_attribute *attr,
1270e098bc96SEvan Quan 		const char *buf,
1271e098bc96SEvan Quan 		size_t count)
1272e098bc96SEvan Quan {
12732ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1274e098bc96SEvan Quan }
1275e098bc96SEvan Quan 
1276e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1277e098bc96SEvan Quan 		struct device_attribute *attr,
1278e098bc96SEvan Quan 		char *buf)
1279e098bc96SEvan Quan {
1280e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12811348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1282e098bc96SEvan Quan 	uint32_t value = 0;
1283e098bc96SEvan Quan 	int ret;
1284e098bc96SEvan Quan 
128553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1286e098bc96SEvan Quan 		return -EPERM;
1287d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1288d2ae842dSAlex Deucher 		return -EPERM;
1289e098bc96SEvan Quan 
1290e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1291e098bc96SEvan Quan 	if (ret < 0) {
1292e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1293e098bc96SEvan Quan 		return ret;
1294e098bc96SEvan Quan 	}
1295e098bc96SEvan Quan 
1296e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
129775145aabSAlex Deucher 		value = 0;
1298e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_sclk_od)
1299e098bc96SEvan Quan 		value = amdgpu_dpm_get_sclk_od(adev);
1300e098bc96SEvan Quan 
1301e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1302e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1303e098bc96SEvan Quan 
1304a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1305e098bc96SEvan Quan }
1306e098bc96SEvan Quan 
1307e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1308e098bc96SEvan Quan 		struct device_attribute *attr,
1309e098bc96SEvan Quan 		const char *buf,
1310e098bc96SEvan Quan 		size_t count)
1311e098bc96SEvan Quan {
1312e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13131348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1314e098bc96SEvan Quan 	int ret;
1315e098bc96SEvan Quan 	long int value;
1316e098bc96SEvan Quan 
131753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1318e098bc96SEvan Quan 		return -EPERM;
1319d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1320d2ae842dSAlex Deucher 		return -EPERM;
1321e098bc96SEvan Quan 
1322e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1323e098bc96SEvan Quan 
1324e098bc96SEvan Quan 	if (ret)
1325e098bc96SEvan Quan 		return -EINVAL;
1326e098bc96SEvan Quan 
1327e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1328e098bc96SEvan Quan 	if (ret < 0) {
1329e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1330e098bc96SEvan Quan 		return ret;
1331e098bc96SEvan Quan 	}
1332e098bc96SEvan Quan 
1333e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
133475145aabSAlex Deucher 		value = 0;
1335e098bc96SEvan Quan 	} else {
1336e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->set_sclk_od)
1337e098bc96SEvan Quan 			amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1338e098bc96SEvan Quan 
1339e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
1340e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1341e098bc96SEvan Quan 		} else {
1342e098bc96SEvan Quan 			adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1343e098bc96SEvan Quan 			amdgpu_pm_compute_clocks(adev);
1344e098bc96SEvan Quan 		}
1345e098bc96SEvan Quan 	}
1346e098bc96SEvan Quan 
1347e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1348e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1349e098bc96SEvan Quan 
1350e098bc96SEvan Quan 	return count;
1351e098bc96SEvan Quan }
1352e098bc96SEvan Quan 
1353e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1354e098bc96SEvan Quan 		struct device_attribute *attr,
1355e098bc96SEvan Quan 		char *buf)
1356e098bc96SEvan Quan {
1357e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13581348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1359e098bc96SEvan Quan 	uint32_t value = 0;
1360e098bc96SEvan Quan 	int ret;
1361e098bc96SEvan Quan 
136253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1363e098bc96SEvan Quan 		return -EPERM;
1364d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1365d2ae842dSAlex Deucher 		return -EPERM;
1366e098bc96SEvan Quan 
1367e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1368e098bc96SEvan Quan 	if (ret < 0) {
1369e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1370e098bc96SEvan Quan 		return ret;
1371e098bc96SEvan Quan 	}
1372e098bc96SEvan Quan 
1373e098bc96SEvan Quan 	if (is_support_sw_smu(adev))
137475145aabSAlex Deucher 		value = 0;
1375e098bc96SEvan Quan 	else if (adev->powerplay.pp_funcs->get_mclk_od)
1376e098bc96SEvan Quan 		value = amdgpu_dpm_get_mclk_od(adev);
1377e098bc96SEvan Quan 
1378e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1379e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1380e098bc96SEvan Quan 
1381a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1382e098bc96SEvan Quan }
1383e098bc96SEvan Quan 
1384e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1385e098bc96SEvan Quan 		struct device_attribute *attr,
1386e098bc96SEvan Quan 		const char *buf,
1387e098bc96SEvan Quan 		size_t count)
1388e098bc96SEvan Quan {
1389e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13901348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1391e098bc96SEvan Quan 	int ret;
1392e098bc96SEvan Quan 	long int value;
1393e098bc96SEvan Quan 
139453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1395e098bc96SEvan Quan 		return -EPERM;
1396d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1397d2ae842dSAlex Deucher 		return -EPERM;
1398e098bc96SEvan Quan 
1399e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1400e098bc96SEvan Quan 
1401e098bc96SEvan Quan 	if (ret)
1402e098bc96SEvan Quan 		return -EINVAL;
1403e098bc96SEvan Quan 
1404e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1405e098bc96SEvan Quan 	if (ret < 0) {
1406e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1407e098bc96SEvan Quan 		return ret;
1408e098bc96SEvan Quan 	}
1409e098bc96SEvan Quan 
1410e098bc96SEvan Quan 	if (is_support_sw_smu(adev)) {
141175145aabSAlex Deucher 		value = 0;
1412e098bc96SEvan Quan 	} else {
1413e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->set_mclk_od)
1414e098bc96SEvan Quan 			amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1415e098bc96SEvan Quan 
1416e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
1417e098bc96SEvan Quan 			amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1418e098bc96SEvan Quan 		} else {
1419e098bc96SEvan Quan 			adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1420e098bc96SEvan Quan 			amdgpu_pm_compute_clocks(adev);
1421e098bc96SEvan Quan 		}
1422e098bc96SEvan Quan 	}
1423e098bc96SEvan Quan 
1424e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1425e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1426e098bc96SEvan Quan 
1427e098bc96SEvan Quan 	return count;
1428e098bc96SEvan Quan }
1429e098bc96SEvan Quan 
1430e098bc96SEvan Quan /**
1431e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1432e098bc96SEvan Quan  *
1433e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1434e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1435e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1436e098bc96SEvan Quan  *
1437e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1438e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1439e098bc96SEvan Quan  *
1440e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1441e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1442e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1443e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1444e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1445e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1446e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1447e098bc96SEvan Quan  *
1448e098bc96SEvan Quan  */
1449e098bc96SEvan Quan 
1450e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1451e098bc96SEvan Quan 		struct device_attribute *attr,
1452e098bc96SEvan Quan 		char *buf)
1453e098bc96SEvan Quan {
1454e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14551348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1456e098bc96SEvan Quan 	ssize_t size;
1457e098bc96SEvan Quan 	int ret;
1458e098bc96SEvan Quan 
145953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1460e098bc96SEvan Quan 		return -EPERM;
1461d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1462d2ae842dSAlex Deucher 		return -EPERM;
1463e098bc96SEvan Quan 
1464e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1465e098bc96SEvan Quan 	if (ret < 0) {
1466e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1467e098bc96SEvan Quan 		return ret;
1468e098bc96SEvan Quan 	}
1469e098bc96SEvan Quan 
14702ea092e5SDarren Powell 	if (adev->powerplay.pp_funcs->get_power_profile_mode)
1471e098bc96SEvan Quan 		size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1472e098bc96SEvan Quan 	else
147309b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1474e098bc96SEvan Quan 
1475e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1476e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1477e098bc96SEvan Quan 
1478e098bc96SEvan Quan 	return size;
1479e098bc96SEvan Quan }
1480e098bc96SEvan Quan 
1481e098bc96SEvan Quan 
1482e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1483e098bc96SEvan Quan 		struct device_attribute *attr,
1484e098bc96SEvan Quan 		const char *buf,
1485e098bc96SEvan Quan 		size_t count)
1486e098bc96SEvan Quan {
1487e098bc96SEvan Quan 	int ret;
1488e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14891348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1490e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1491e098bc96SEvan Quan 	long parameter[64];
1492e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1493e098bc96SEvan Quan 	char *tmp_str;
1494e098bc96SEvan Quan 	uint32_t i = 0;
1495e098bc96SEvan Quan 	char tmp[2];
1496e098bc96SEvan Quan 	long int profile_mode = 0;
1497e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1498e098bc96SEvan Quan 
149953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1500e098bc96SEvan Quan 		return -EPERM;
1501d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1502d2ae842dSAlex Deucher 		return -EPERM;
1503e098bc96SEvan Quan 
1504e098bc96SEvan Quan 	tmp[0] = *(buf);
1505e098bc96SEvan Quan 	tmp[1] = '\0';
1506e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1507e098bc96SEvan Quan 	if (ret)
1508e098bc96SEvan Quan 		return -EINVAL;
1509e098bc96SEvan Quan 
1510e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1511e098bc96SEvan Quan 		if (count < 2 || count > 127)
1512e098bc96SEvan Quan 			return -EINVAL;
1513e098bc96SEvan Quan 		while (isspace(*++buf))
1514e098bc96SEvan Quan 			i++;
1515e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1516e098bc96SEvan Quan 		tmp_str = buf_cpy;
1517ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1518c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1519c2efbc3fSEvan Quan 				continue;
1520e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1521e098bc96SEvan Quan 			if (ret)
1522e098bc96SEvan Quan 				return -EINVAL;
1523e098bc96SEvan Quan 			parameter_size++;
1524e098bc96SEvan Quan 			while (isspace(*tmp_str))
1525e098bc96SEvan Quan 				tmp_str++;
1526e098bc96SEvan Quan 		}
1527e098bc96SEvan Quan 	}
1528e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1529e098bc96SEvan Quan 
1530e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1531e098bc96SEvan Quan 	if (ret < 0) {
1532e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1533e098bc96SEvan Quan 		return ret;
1534e098bc96SEvan Quan 	}
1535e098bc96SEvan Quan 
15362ea092e5SDarren Powell 	if (adev->powerplay.pp_funcs->set_power_profile_mode)
1537e098bc96SEvan Quan 		ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1538e098bc96SEvan Quan 
1539e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1540e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1541e098bc96SEvan Quan 
1542e098bc96SEvan Quan 	if (!ret)
1543e098bc96SEvan Quan 		return count;
1544e098bc96SEvan Quan 
1545e098bc96SEvan Quan 	return -EINVAL;
1546e098bc96SEvan Quan }
1547e098bc96SEvan Quan 
1548e098bc96SEvan Quan /**
1549e098bc96SEvan Quan  * DOC: gpu_busy_percent
1550e098bc96SEvan Quan  *
1551e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1552e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1553e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1554e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1555e098bc96SEvan Quan  */
1556e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1557e098bc96SEvan Quan 					   struct device_attribute *attr,
1558e098bc96SEvan Quan 					   char *buf)
1559e098bc96SEvan Quan {
1560e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15611348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1562e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1563e098bc96SEvan Quan 
156453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1565e098bc96SEvan Quan 		return -EPERM;
1566d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1567d2ae842dSAlex Deucher 		return -EPERM;
1568e098bc96SEvan Quan 
1569e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1570e098bc96SEvan Quan 	if (r < 0) {
1571e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1572e098bc96SEvan Quan 		return r;
1573e098bc96SEvan Quan 	}
1574e098bc96SEvan Quan 
1575e098bc96SEvan Quan 	/* read the IP busy sensor */
1576e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1577e098bc96SEvan Quan 				   (void *)&value, &size);
1578e098bc96SEvan Quan 
1579e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1580e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1581e098bc96SEvan Quan 
1582e098bc96SEvan Quan 	if (r)
1583e098bc96SEvan Quan 		return r;
1584e098bc96SEvan Quan 
1585a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1586e098bc96SEvan Quan }
1587e098bc96SEvan Quan 
1588e098bc96SEvan Quan /**
1589e098bc96SEvan Quan  * DOC: mem_busy_percent
1590e098bc96SEvan Quan  *
1591e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1592e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1593e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1594e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1595e098bc96SEvan Quan  */
1596e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1597e098bc96SEvan Quan 					   struct device_attribute *attr,
1598e098bc96SEvan Quan 					   char *buf)
1599e098bc96SEvan Quan {
1600e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16011348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1602e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1603e098bc96SEvan Quan 
160453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1605e098bc96SEvan Quan 		return -EPERM;
1606d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1607d2ae842dSAlex Deucher 		return -EPERM;
1608e098bc96SEvan Quan 
1609e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1610e098bc96SEvan Quan 	if (r < 0) {
1611e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1612e098bc96SEvan Quan 		return r;
1613e098bc96SEvan Quan 	}
1614e098bc96SEvan Quan 
1615e098bc96SEvan Quan 	/* read the IP busy sensor */
1616e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1617e098bc96SEvan Quan 				   (void *)&value, &size);
1618e098bc96SEvan Quan 
1619e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1620e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1621e098bc96SEvan Quan 
1622e098bc96SEvan Quan 	if (r)
1623e098bc96SEvan Quan 		return r;
1624e098bc96SEvan Quan 
1625a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1626e098bc96SEvan Quan }
1627e098bc96SEvan Quan 
1628e098bc96SEvan Quan /**
1629e098bc96SEvan Quan  * DOC: pcie_bw
1630e098bc96SEvan Quan  *
1631e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1632e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1633e098bc96SEvan Quan  * The file pcie_bw is used for this.
1634e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1635e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1636e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1637e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1638e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1639e098bc96SEvan Quan  */
1640e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1641e098bc96SEvan Quan 		struct device_attribute *attr,
1642e098bc96SEvan Quan 		char *buf)
1643e098bc96SEvan Quan {
1644e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16451348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1646e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1647e098bc96SEvan Quan 	int ret;
1648e098bc96SEvan Quan 
164953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1650e098bc96SEvan Quan 		return -EPERM;
1651d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1652d2ae842dSAlex Deucher 		return -EPERM;
1653e098bc96SEvan Quan 
1654e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1655e098bc96SEvan Quan 		return -ENODATA;
1656e098bc96SEvan Quan 
1657e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1658e098bc96SEvan Quan 		return -ENODATA;
1659e098bc96SEvan Quan 
1660e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1661e098bc96SEvan Quan 	if (ret < 0) {
1662e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1663e098bc96SEvan Quan 		return ret;
1664e098bc96SEvan Quan 	}
1665e098bc96SEvan Quan 
1666e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1667e098bc96SEvan Quan 
1668e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1669e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1670e098bc96SEvan Quan 
1671a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1672e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1673e098bc96SEvan Quan }
1674e098bc96SEvan Quan 
1675e098bc96SEvan Quan /**
1676e098bc96SEvan Quan  * DOC: unique_id
1677e098bc96SEvan Quan  *
1678e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1679e098bc96SEvan Quan  * The file unique_id is used for this.
1680e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1681e098bc96SEvan Quan  *
1682e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1683e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1684e098bc96SEvan Quan  */
1685e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1686e098bc96SEvan Quan 		struct device_attribute *attr,
1687e098bc96SEvan Quan 		char *buf)
1688e098bc96SEvan Quan {
1689e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16901348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1691e098bc96SEvan Quan 
169253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1693e098bc96SEvan Quan 		return -EPERM;
1694d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1695d2ae842dSAlex Deucher 		return -EPERM;
1696e098bc96SEvan Quan 
1697e098bc96SEvan Quan 	if (adev->unique_id)
1698a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1699e098bc96SEvan Quan 
1700e098bc96SEvan Quan 	return 0;
1701e098bc96SEvan Quan }
1702e098bc96SEvan Quan 
1703e098bc96SEvan Quan /**
1704e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1705e098bc96SEvan Quan  *
1706e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1707e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1708e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1709e098bc96SEvan Quan  * the log frequency.
1710e098bc96SEvan Quan  *
1711e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1712e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1713e098bc96SEvan Quan  *
1714e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1715e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1716e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1717e098bc96SEvan Quan  */
1718e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1719e098bc96SEvan Quan 						     struct device_attribute *attr,
1720e098bc96SEvan Quan 						     char *buf)
1721e098bc96SEvan Quan {
1722e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17231348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1724e098bc96SEvan Quan 
1725a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
17264a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1727e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1728e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1729e098bc96SEvan Quan }
1730e098bc96SEvan Quan 
1731e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1732e098bc96SEvan Quan 						     struct device_attribute *attr,
1733e098bc96SEvan Quan 						     const char *buf,
1734e098bc96SEvan Quan 						     size_t count)
1735e098bc96SEvan Quan {
1736e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17371348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1738e098bc96SEvan Quan 	long throttling_logging_interval;
1739e098bc96SEvan Quan 	unsigned long flags;
1740e098bc96SEvan Quan 	int ret = 0;
1741e098bc96SEvan Quan 
1742e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1743e098bc96SEvan Quan 	if (ret)
1744e098bc96SEvan Quan 		return ret;
1745e098bc96SEvan Quan 
1746e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1747e098bc96SEvan Quan 		return -EINVAL;
1748e098bc96SEvan Quan 
1749e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1750e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1751e098bc96SEvan Quan 		/*
1752e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1753e098bc96SEvan Quan 		 * This can effectively restart the timer.
1754e098bc96SEvan Quan 		 */
1755e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1756e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1757e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1758e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1759e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1760e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1761e098bc96SEvan Quan 
1762e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1763e098bc96SEvan Quan 	} else {
1764e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1765e098bc96SEvan Quan 	}
1766e098bc96SEvan Quan 
1767e098bc96SEvan Quan 	return count;
1768e098bc96SEvan Quan }
1769e098bc96SEvan Quan 
1770e098bc96SEvan Quan /**
1771e098bc96SEvan Quan  * DOC: gpu_metrics
1772e098bc96SEvan Quan  *
1773e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1774e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1775e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1776e098bc96SEvan Quan  *
1777e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1778e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1779e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1780e098bc96SEvan Quan  * at the same time.
1781e098bc96SEvan Quan  */
1782e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1783e098bc96SEvan Quan 				      struct device_attribute *attr,
1784e098bc96SEvan Quan 				      char *buf)
1785e098bc96SEvan Quan {
1786e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17871348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1788e098bc96SEvan Quan 	void *gpu_metrics;
1789e098bc96SEvan Quan 	ssize_t size = 0;
1790e098bc96SEvan Quan 	int ret;
1791e098bc96SEvan Quan 
179253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1793e098bc96SEvan Quan 		return -EPERM;
1794d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1795d2ae842dSAlex Deucher 		return -EPERM;
1796e098bc96SEvan Quan 
1797e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1798e098bc96SEvan Quan 	if (ret < 0) {
1799e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1800e098bc96SEvan Quan 		return ret;
1801e098bc96SEvan Quan 	}
1802e098bc96SEvan Quan 
18032ea092e5SDarren Powell 	if (adev->powerplay.pp_funcs->get_gpu_metrics)
1804e098bc96SEvan Quan 		size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1805e098bc96SEvan Quan 
1806e098bc96SEvan Quan 	if (size <= 0)
1807e098bc96SEvan Quan 		goto out;
1808e098bc96SEvan Quan 
1809e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1810e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1811e098bc96SEvan Quan 
1812e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1813e098bc96SEvan Quan 
1814e098bc96SEvan Quan out:
1815e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1816e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1817e098bc96SEvan Quan 
1818e098bc96SEvan Quan 	return size;
1819e098bc96SEvan Quan }
1820e098bc96SEvan Quan 
1821a7673a1cSSathishkumar S /**
1822a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1823a7673a1cSSathishkumar S  *
1824a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1825a7673a1cSSathishkumar S  * share if it supports smartshift. The value is expressed as
1826a7673a1cSSathishkumar S  * the proportion of stapm limit where stapm limit is the total APU
1827a7673a1cSSathishkumar S  * power limit. The result is in percentage. If APU power is 130% of
1828a7673a1cSSathishkumar S  * STAPM, then APU is using 30% of the dGPU's headroom.
1829a7673a1cSSathishkumar S  */
1830a7673a1cSSathishkumar S 
1831a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1832a7673a1cSSathishkumar S 					       char *buf)
1833a7673a1cSSathishkumar S {
1834a7673a1cSSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1835a7673a1cSSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1836a7673a1cSSathishkumar S 	uint32_t ss_power, size;
1837a7673a1cSSathishkumar S 	int r = 0;
1838a7673a1cSSathishkumar S 
1839a7673a1cSSathishkumar S 	if (amdgpu_in_reset(adev))
1840a7673a1cSSathishkumar S 		return -EPERM;
1841a7673a1cSSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
1842a7673a1cSSathishkumar S 		return -EPERM;
1843a7673a1cSSathishkumar S 
1844a7673a1cSSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
1845a7673a1cSSathishkumar S 	if (r < 0) {
1846a7673a1cSSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
1847a7673a1cSSathishkumar S 		return r;
1848a7673a1cSSathishkumar S 	}
1849a7673a1cSSathishkumar S 
1850a7673a1cSSathishkumar S 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1851a7673a1cSSathishkumar S 				   (void *)&ss_power, &size);
1852a7673a1cSSathishkumar S 	if (r)
1853a7673a1cSSathishkumar S 		goto out;
1854a7673a1cSSathishkumar S 
1855a7673a1cSSathishkumar S 	r = sysfs_emit(buf, "%u%%\n", ss_power);
1856a7673a1cSSathishkumar S 
1857a7673a1cSSathishkumar S out:
1858a7673a1cSSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
1859a7673a1cSSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
1860a7673a1cSSathishkumar S 	return r;
1861a7673a1cSSathishkumar S }
1862a7673a1cSSathishkumar S 
1863a7673a1cSSathishkumar S /**
1864a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1865a7673a1cSSathishkumar S  *
1866a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the dGPU power
1867a7673a1cSSathishkumar S  * share if the device is in HG and supports smartshift. The value
1868a7673a1cSSathishkumar S  * is expressed as the proportion of stapm limit where stapm limit
1869a7673a1cSSathishkumar S  * is the total APU power limit. The value is in percentage. If dGPU
1870a7673a1cSSathishkumar S  * power is 20% higher than STAPM power(120%), it's using 20% of the
1871a7673a1cSSathishkumar S  * APU's power headroom.
1872a7673a1cSSathishkumar S  */
1873a7673a1cSSathishkumar S 
1874a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1875a7673a1cSSathishkumar S 						char *buf)
1876a7673a1cSSathishkumar S {
1877a7673a1cSSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1878a7673a1cSSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1879a7673a1cSSathishkumar S 	uint32_t ss_power, size;
1880a7673a1cSSathishkumar S 	int r = 0;
1881a7673a1cSSathishkumar S 
1882a7673a1cSSathishkumar S 	if (amdgpu_in_reset(adev))
1883a7673a1cSSathishkumar S 		return -EPERM;
1884a7673a1cSSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
1885a7673a1cSSathishkumar S 		return -EPERM;
1886a7673a1cSSathishkumar S 
1887a7673a1cSSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
1888a7673a1cSSathishkumar S 	if (r < 0) {
1889a7673a1cSSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
1890a7673a1cSSathishkumar S 		return r;
1891a7673a1cSSathishkumar S 	}
1892a7673a1cSSathishkumar S 
1893a7673a1cSSathishkumar S 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1894a7673a1cSSathishkumar S 				   (void *)&ss_power, &size);
1895a7673a1cSSathishkumar S 
1896a7673a1cSSathishkumar S 	if (r)
1897a7673a1cSSathishkumar S 		goto out;
1898a7673a1cSSathishkumar S 
1899a7673a1cSSathishkumar S 	r = sysfs_emit(buf, "%u%%\n", ss_power);
1900a7673a1cSSathishkumar S 
1901a7673a1cSSathishkumar S out:
1902a7673a1cSSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
1903a7673a1cSSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
1904a7673a1cSSathishkumar S 	return r;
1905a7673a1cSSathishkumar S }
1906a7673a1cSSathishkumar S 
190730d95a37SSathishkumar S /**
190830d95a37SSathishkumar S  * DOC: smartshift_bias
190930d95a37SSathishkumar S  *
191030d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
191130d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
191230d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
191330d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
191430d95a37SSathishkumar S  */
191530d95a37SSathishkumar S 
191630d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
191730d95a37SSathishkumar S 					  struct device_attribute *attr,
191830d95a37SSathishkumar S 					  char *buf)
191930d95a37SSathishkumar S {
192030d95a37SSathishkumar S 	int r = 0;
192130d95a37SSathishkumar S 
192230d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
192330d95a37SSathishkumar S 
192430d95a37SSathishkumar S 	return r;
192530d95a37SSathishkumar S }
192630d95a37SSathishkumar S 
192730d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
192830d95a37SSathishkumar S 					  struct device_attribute *attr,
192930d95a37SSathishkumar S 					  const char *buf, size_t count)
193030d95a37SSathishkumar S {
193130d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
193230d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
193330d95a37SSathishkumar S 	int r = 0;
193430d95a37SSathishkumar S 	int bias = 0;
193530d95a37SSathishkumar S 
193630d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
193730d95a37SSathishkumar S 		return -EPERM;
193830d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
193930d95a37SSathishkumar S 		return -EPERM;
194030d95a37SSathishkumar S 
194130d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
194230d95a37SSathishkumar S 	if (r < 0) {
194330d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
194430d95a37SSathishkumar S 		return r;
194530d95a37SSathishkumar S 	}
194630d95a37SSathishkumar S 
194730d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
194830d95a37SSathishkumar S 	if (r)
194930d95a37SSathishkumar S 		goto out;
195030d95a37SSathishkumar S 
195130d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
195230d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
195330d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
195430d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
195530d95a37SSathishkumar S 
195630d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
195730d95a37SSathishkumar S 	r = count;
195830d95a37SSathishkumar S 
195930d95a37SSathishkumar S 	/* TODO: upadte bias level with SMU message */
196030d95a37SSathishkumar S 
196130d95a37SSathishkumar S out:
196230d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
196330d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
196430d95a37SSathishkumar S 	return r;
196530d95a37SSathishkumar S }
196630d95a37SSathishkumar S 
196730d95a37SSathishkumar S 
1968a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1969a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
1970a7673a1cSSathishkumar S {
1971a7673a1cSSathishkumar S 	uint32_t ss_power, size;
1972a7673a1cSSathishkumar S 
1973a7673a1cSSathishkumar S 	if (!amdgpu_acpi_is_power_shift_control_supported())
1974a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1975a7673a1cSSathishkumar S 	else if ((adev->flags & AMD_IS_PX) &&
1976a7673a1cSSathishkumar S 		 !amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1977a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1978a7673a1cSSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1979a7673a1cSSathishkumar S 		 (void *)&ss_power, &size))
1980a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1981a7673a1cSSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1982a7673a1cSSathishkumar S 		 (void *)&ss_power, &size))
1983a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1984a7673a1cSSathishkumar S 
1985a7673a1cSSathishkumar S 	return 0;
1986a7673a1cSSathishkumar S }
1987a7673a1cSSathishkumar S 
198830d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
198930d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
199030d95a37SSathishkumar S {
199130d95a37SSathishkumar S 	uint32_t ss_power, size;
199230d95a37SSathishkumar S 
199330d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
199430d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
199530d95a37SSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
199630d95a37SSathishkumar S 		 (void *)&ss_power, &size))
199730d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
199830d95a37SSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
199930d95a37SSathishkumar S 		 (void *)&ss_power, &size))
200030d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
200130d95a37SSathishkumar S 
200230d95a37SSathishkumar S 	return 0;
200330d95a37SSathishkumar S }
200430d95a37SSathishkumar S 
2005e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2006e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20074215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2008e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC),
2009e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC),
2010e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC),
2011e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC),
2012e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2013e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2014e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2015e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20169577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20179577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2018e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC),
2019e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC),
2020e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2021e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2022e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC),
2023e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
2024e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC),
2025e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC),
2026e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2027e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC),
2028e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC),
2029e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC),
2030e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC),
2031a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2032a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
2033a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2034a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
203530d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
203630d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
2037e098bc96SEvan Quan };
2038e098bc96SEvan Quan 
2039e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2040e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2041e098bc96SEvan Quan {
2042e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2043e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
2044e098bc96SEvan Quan 	struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2045e098bc96SEvan Quan 	enum amd_asic_type asic_type = adev->asic_type;
2046e098bc96SEvan Quan 
2047e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2048e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2049e098bc96SEvan Quan 		return 0;
2050e098bc96SEvan Quan 	}
2051e098bc96SEvan Quan 
2052e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2053e098bc96SEvan Quan 
2054e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2055e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA10)
2056e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2057e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
20580133840fSKent Russell 		if (asic_type < CHIP_VEGA10 ||
20590133840fSKent Russell 		    asic_type == CHIP_ARCTURUS ||
20600133840fSKent Russell 		    asic_type == CHIP_ALDEBARAN)
2061e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2062e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2063e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA20)
2064e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2065e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2066e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2067e098bc96SEvan Quan 		if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2068e017fb66SXiaojian Du 		    (is_support_sw_smu(adev) && adev->smu.is_apu) ||
2069e098bc96SEvan Quan 			(!is_support_sw_smu(adev) && hwmgr->od_enabled))
2070e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
2071e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2072e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2073e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2074e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2075e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
2076e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
2077e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2078e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
2079e098bc96SEvan Quan 		if (asic_type != CHIP_VEGA10 &&
2080e098bc96SEvan Quan 		    asic_type != CHIP_VEGA20 &&
2081e098bc96SEvan Quan 		    asic_type != CHIP_ARCTURUS)
2082e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2083e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2084e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2085e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2086e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
2087e098bc96SEvan Quan 		if (asic_type < CHIP_VEGA12)
2088e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
20899577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
20909577b0ecSXiaojian Du 		if (!(asic_type == CHIP_VANGOGH))
20919577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
20929577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
20939577b0ecSXiaojian Du 		if (!(asic_type == CHIP_VANGOGH))
20949577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
2095e098bc96SEvan Quan 	}
2096e098bc96SEvan Quan 
20971d0e622fSKevin Wang 	switch (asic_type) {
20981d0e622fSKevin Wang 	case CHIP_ARCTURUS:
20991d0e622fSKevin Wang 	case CHIP_ALDEBARAN:
21001d0e622fSKevin Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2101e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2102e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2103e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2104e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2105e098bc96SEvan Quan 			dev_attr->store = NULL;
2106e098bc96SEvan Quan 		}
21071d0e622fSKevin Wang 		break;
21081d0e622fSKevin Wang 	default:
21091d0e622fSKevin Wang 		break;
2110e098bc96SEvan Quan 	}
2111e098bc96SEvan Quan 
2112ede14a1bSDarren Powell 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2113ede14a1bSDarren Powell 		/* SMU MP1 does not support dcefclk level setting */
2114ede14a1bSDarren Powell 		if (asic_type >= CHIP_NAVI10) {
2115ede14a1bSDarren Powell 			dev_attr->attr.mode &= ~S_IWUGO;
2116ede14a1bSDarren Powell 			dev_attr->store = NULL;
2117ede14a1bSDarren Powell 		}
2118ede14a1bSDarren Powell 	}
2119ede14a1bSDarren Powell 
2120e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2121e098bc96SEvan Quan 
2122e098bc96SEvan Quan 	return 0;
2123e098bc96SEvan Quan }
2124e098bc96SEvan Quan 
2125e098bc96SEvan Quan 
2126e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2127e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2128e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2129e098bc96SEvan Quan {
2130e098bc96SEvan Quan 	int ret = 0;
2131e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2132e098bc96SEvan Quan 	const char *name = dev_attr->attr.name;
2133e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2134e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
2135e098bc96SEvan Quan 
2136e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2137e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2138e098bc96SEvan Quan 
2139e098bc96SEvan Quan 	BUG_ON(!attr);
2140e098bc96SEvan Quan 
21418a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2142e098bc96SEvan Quan 
2143e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2144e098bc96SEvan Quan 	if (ret) {
2145e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2146e098bc96SEvan Quan 			name, ret);
2147e098bc96SEvan Quan 		return ret;
2148e098bc96SEvan Quan 	}
2149e098bc96SEvan Quan 
2150e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2151e098bc96SEvan Quan 		return 0;
2152e098bc96SEvan Quan 
2153e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2154e098bc96SEvan Quan 	if (ret) {
2155e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2156e098bc96SEvan Quan 			name, ret);
2157e098bc96SEvan Quan 	}
2158e098bc96SEvan Quan 
2159e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2160e098bc96SEvan Quan 	if (!attr_entry)
2161e098bc96SEvan Quan 		return -ENOMEM;
2162e098bc96SEvan Quan 
2163e098bc96SEvan Quan 	attr_entry->attr = attr;
2164e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2165e098bc96SEvan Quan 
2166e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2167e098bc96SEvan Quan 
2168e098bc96SEvan Quan 	return ret;
2169e098bc96SEvan Quan }
2170e098bc96SEvan Quan 
2171e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2172e098bc96SEvan Quan {
2173e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2174e098bc96SEvan Quan 
2175e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2176e098bc96SEvan Quan }
2177e098bc96SEvan Quan 
2178e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2179e098bc96SEvan Quan 					     struct list_head *attr_list);
2180e098bc96SEvan Quan 
2181e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2182e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2183e098bc96SEvan Quan 					    uint32_t counts,
2184e098bc96SEvan Quan 					    uint32_t mask,
2185e098bc96SEvan Quan 					    struct list_head *attr_list)
2186e098bc96SEvan Quan {
2187e098bc96SEvan Quan 	int ret = 0;
2188e098bc96SEvan Quan 	uint32_t i = 0;
2189e098bc96SEvan Quan 
2190e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2191e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2192e098bc96SEvan Quan 		if (ret)
2193e098bc96SEvan Quan 			goto failed;
2194e098bc96SEvan Quan 	}
2195e098bc96SEvan Quan 
2196e098bc96SEvan Quan 	return 0;
2197e098bc96SEvan Quan 
2198e098bc96SEvan Quan failed:
2199e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2200e098bc96SEvan Quan 
2201e098bc96SEvan Quan 	return ret;
2202e098bc96SEvan Quan }
2203e098bc96SEvan Quan 
2204e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2205e098bc96SEvan Quan 					     struct list_head *attr_list)
2206e098bc96SEvan Quan {
2207e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2208e098bc96SEvan Quan 
2209e098bc96SEvan Quan 	if (list_empty(attr_list))
2210e098bc96SEvan Quan 		return ;
2211e098bc96SEvan Quan 
2212e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2213e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2214e098bc96SEvan Quan 		list_del(&entry->entry);
2215e098bc96SEvan Quan 		kfree(entry);
2216e098bc96SEvan Quan 	}
2217e098bc96SEvan Quan }
2218e098bc96SEvan Quan 
2219e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2220e098bc96SEvan Quan 				      struct device_attribute *attr,
2221e098bc96SEvan Quan 				      char *buf)
2222e098bc96SEvan Quan {
2223e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2224e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2225e098bc96SEvan Quan 	int r, temp = 0, size = sizeof(temp);
2226e098bc96SEvan Quan 
222753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2228e098bc96SEvan Quan 		return -EPERM;
2229d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2230d2ae842dSAlex Deucher 		return -EPERM;
2231e098bc96SEvan Quan 
2232e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2233e098bc96SEvan Quan 		return -EINVAL;
2234e098bc96SEvan Quan 
22354a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2236e098bc96SEvan Quan 	if (r < 0) {
22374a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2238e098bc96SEvan Quan 		return r;
2239e098bc96SEvan Quan 	}
2240e098bc96SEvan Quan 
2241e098bc96SEvan Quan 	switch (channel) {
2242e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2243e098bc96SEvan Quan 		/* get current junction temperature */
2244e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2245e098bc96SEvan Quan 					   (void *)&temp, &size);
2246e098bc96SEvan Quan 		break;
2247e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2248e098bc96SEvan Quan 		/* get current edge temperature */
2249e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2250e098bc96SEvan Quan 					   (void *)&temp, &size);
2251e098bc96SEvan Quan 		break;
2252e098bc96SEvan Quan 	case PP_TEMP_MEM:
2253e098bc96SEvan Quan 		/* get current memory temperature */
2254e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2255e098bc96SEvan Quan 					   (void *)&temp, &size);
2256e098bc96SEvan Quan 		break;
2257e098bc96SEvan Quan 	default:
2258e098bc96SEvan Quan 		r = -EINVAL;
2259e098bc96SEvan Quan 		break;
2260e098bc96SEvan Quan 	}
2261e098bc96SEvan Quan 
22624a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
22634a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2264e098bc96SEvan Quan 
2265e098bc96SEvan Quan 	if (r)
2266e098bc96SEvan Quan 		return r;
2267e098bc96SEvan Quan 
2268a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2269e098bc96SEvan Quan }
2270e098bc96SEvan Quan 
2271e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2272e098bc96SEvan Quan 					     struct device_attribute *attr,
2273e098bc96SEvan Quan 					     char *buf)
2274e098bc96SEvan Quan {
2275e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2276e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2277e098bc96SEvan Quan 	int temp;
2278e098bc96SEvan Quan 
2279e098bc96SEvan Quan 	if (hyst)
2280e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2281e098bc96SEvan Quan 	else
2282e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2283e098bc96SEvan Quan 
2284a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2285e098bc96SEvan Quan }
2286e098bc96SEvan Quan 
2287e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2288e098bc96SEvan Quan 					     struct device_attribute *attr,
2289e098bc96SEvan Quan 					     char *buf)
2290e098bc96SEvan Quan {
2291e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2292e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2293e098bc96SEvan Quan 	int temp;
2294e098bc96SEvan Quan 
2295e098bc96SEvan Quan 	if (hyst)
2296e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2297e098bc96SEvan Quan 	else
2298e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2299e098bc96SEvan Quan 
2300a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2301e098bc96SEvan Quan }
2302e098bc96SEvan Quan 
2303e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2304e098bc96SEvan Quan 					     struct device_attribute *attr,
2305e098bc96SEvan Quan 					     char *buf)
2306e098bc96SEvan Quan {
2307e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2308e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2309e098bc96SEvan Quan 	int temp;
2310e098bc96SEvan Quan 
2311e098bc96SEvan Quan 	if (hyst)
2312e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2313e098bc96SEvan Quan 	else
2314e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2315e098bc96SEvan Quan 
2316a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2317e098bc96SEvan Quan }
2318e098bc96SEvan Quan 
2319e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2320e098bc96SEvan Quan 					     struct device_attribute *attr,
2321e098bc96SEvan Quan 					     char *buf)
2322e098bc96SEvan Quan {
2323e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2324e098bc96SEvan Quan 
2325e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2326e098bc96SEvan Quan 		return -EINVAL;
2327e098bc96SEvan Quan 
2328a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2329e098bc96SEvan Quan }
2330e098bc96SEvan Quan 
2331e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2332e098bc96SEvan Quan 					     struct device_attribute *attr,
2333e098bc96SEvan Quan 					     char *buf)
2334e098bc96SEvan Quan {
2335e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2336e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2337e098bc96SEvan Quan 	int temp = 0;
2338e098bc96SEvan Quan 
2339e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2340e098bc96SEvan Quan 		return -EINVAL;
2341e098bc96SEvan Quan 
2342e098bc96SEvan Quan 	switch (channel) {
2343e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2344e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2345e098bc96SEvan Quan 		break;
2346e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2347e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2348e098bc96SEvan Quan 		break;
2349e098bc96SEvan Quan 	case PP_TEMP_MEM:
2350e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2351e098bc96SEvan Quan 		break;
2352e098bc96SEvan Quan 	}
2353e098bc96SEvan Quan 
2354a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2355e098bc96SEvan Quan }
2356e098bc96SEvan Quan 
2357e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2358e098bc96SEvan Quan 					    struct device_attribute *attr,
2359e098bc96SEvan Quan 					    char *buf)
2360e098bc96SEvan Quan {
2361e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2362e098bc96SEvan Quan 	u32 pwm_mode = 0;
2363e098bc96SEvan Quan 	int ret;
2364e098bc96SEvan Quan 
236553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2366e098bc96SEvan Quan 		return -EPERM;
2367d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2368d2ae842dSAlex Deucher 		return -EPERM;
2369e098bc96SEvan Quan 
23704a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2371e098bc96SEvan Quan 	if (ret < 0) {
23724a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2373e098bc96SEvan Quan 		return ret;
2374e098bc96SEvan Quan 	}
2375e098bc96SEvan Quan 
2376e098bc96SEvan Quan 	if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
23774a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
23784a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2379e098bc96SEvan Quan 		return -EINVAL;
2380e098bc96SEvan Quan 	}
2381e098bc96SEvan Quan 
2382e098bc96SEvan Quan 	pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2383e098bc96SEvan Quan 
23844a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
23854a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2386e098bc96SEvan Quan 
2387fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2388e098bc96SEvan Quan }
2389e098bc96SEvan Quan 
2390e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2391e098bc96SEvan Quan 					    struct device_attribute *attr,
2392e098bc96SEvan Quan 					    const char *buf,
2393e098bc96SEvan Quan 					    size_t count)
2394e098bc96SEvan Quan {
2395e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2396e098bc96SEvan Quan 	int err, ret;
2397e098bc96SEvan Quan 	int value;
2398e098bc96SEvan Quan 
239953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2400e098bc96SEvan Quan 		return -EPERM;
2401d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2402d2ae842dSAlex Deucher 		return -EPERM;
2403e098bc96SEvan Quan 
2404e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2405e098bc96SEvan Quan 	if (err)
2406e098bc96SEvan Quan 		return err;
2407e098bc96SEvan Quan 
24084a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2409e098bc96SEvan Quan 	if (ret < 0) {
24104a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2411e098bc96SEvan Quan 		return ret;
2412e098bc96SEvan Quan 	}
2413e098bc96SEvan Quan 
2414e098bc96SEvan Quan 	if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
24154a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24164a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2417e098bc96SEvan Quan 		return -EINVAL;
2418e098bc96SEvan Quan 	}
2419e098bc96SEvan Quan 
2420e098bc96SEvan Quan 	amdgpu_dpm_set_fan_control_mode(adev, value);
2421e098bc96SEvan Quan 
24224a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24234a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2424e098bc96SEvan Quan 
2425e098bc96SEvan Quan 	return count;
2426e098bc96SEvan Quan }
2427e098bc96SEvan Quan 
2428e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2429e098bc96SEvan Quan 					 struct device_attribute *attr,
2430e098bc96SEvan Quan 					 char *buf)
2431e098bc96SEvan Quan {
2432fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2433e098bc96SEvan Quan }
2434e098bc96SEvan Quan 
2435e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2436e098bc96SEvan Quan 					 struct device_attribute *attr,
2437e098bc96SEvan Quan 					 char *buf)
2438e098bc96SEvan Quan {
2439fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2440e098bc96SEvan Quan }
2441e098bc96SEvan Quan 
2442e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2443e098bc96SEvan Quan 				     struct device_attribute *attr,
2444e098bc96SEvan Quan 				     const char *buf, size_t count)
2445e098bc96SEvan Quan {
2446e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2447e098bc96SEvan Quan 	int err;
2448e098bc96SEvan Quan 	u32 value;
2449e098bc96SEvan Quan 	u32 pwm_mode;
2450e098bc96SEvan Quan 
245153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2452e098bc96SEvan Quan 		return -EPERM;
2453d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2454d2ae842dSAlex Deucher 		return -EPERM;
2455e098bc96SEvan Quan 
24564a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2457e098bc96SEvan Quan 	if (err < 0) {
24584a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2459e098bc96SEvan Quan 		return err;
2460e098bc96SEvan Quan 	}
2461e098bc96SEvan Quan 
2462e098bc96SEvan Quan 	pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2463e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2464e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
24654a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24664a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2467e098bc96SEvan Quan 		return -EINVAL;
2468e098bc96SEvan Quan 	}
2469e098bc96SEvan Quan 
2470e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2471e098bc96SEvan Quan 	if (err) {
24724a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24734a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2474e098bc96SEvan Quan 		return err;
2475e098bc96SEvan Quan 	}
2476e098bc96SEvan Quan 
2477*0d8318e1SEvan Quan 	if (adev->powerplay.pp_funcs->set_fan_speed_pwm)
2478*0d8318e1SEvan Quan 		err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2479e098bc96SEvan Quan 	else
2480e098bc96SEvan Quan 		err = -EINVAL;
2481e098bc96SEvan Quan 
24824a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24834a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2484e098bc96SEvan Quan 
2485e098bc96SEvan Quan 	if (err)
2486e098bc96SEvan Quan 		return err;
2487e098bc96SEvan Quan 
2488e098bc96SEvan Quan 	return count;
2489e098bc96SEvan Quan }
2490e098bc96SEvan Quan 
2491e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2492e098bc96SEvan Quan 				     struct device_attribute *attr,
2493e098bc96SEvan Quan 				     char *buf)
2494e098bc96SEvan Quan {
2495e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2496e098bc96SEvan Quan 	int err;
2497e098bc96SEvan Quan 	u32 speed = 0;
2498e098bc96SEvan Quan 
249953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2500e098bc96SEvan Quan 		return -EPERM;
2501d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2502d2ae842dSAlex Deucher 		return -EPERM;
2503e098bc96SEvan Quan 
25044a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2505e098bc96SEvan Quan 	if (err < 0) {
25064a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2507e098bc96SEvan Quan 		return err;
2508e098bc96SEvan Quan 	}
2509e098bc96SEvan Quan 
2510*0d8318e1SEvan Quan 	if (adev->powerplay.pp_funcs->get_fan_speed_pwm)
2511*0d8318e1SEvan Quan 		err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2512e098bc96SEvan Quan 	else
2513e098bc96SEvan Quan 		err = -EINVAL;
2514e098bc96SEvan Quan 
25154a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25164a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2517e098bc96SEvan Quan 
2518e098bc96SEvan Quan 	if (err)
2519e098bc96SEvan Quan 		return err;
2520e098bc96SEvan Quan 
2521fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2522e098bc96SEvan Quan }
2523e098bc96SEvan Quan 
2524e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2525e098bc96SEvan Quan 					   struct device_attribute *attr,
2526e098bc96SEvan Quan 					   char *buf)
2527e098bc96SEvan Quan {
2528e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2529e098bc96SEvan Quan 	int err;
2530e098bc96SEvan Quan 	u32 speed = 0;
2531e098bc96SEvan Quan 
253253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2533e098bc96SEvan Quan 		return -EPERM;
2534d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2535d2ae842dSAlex Deucher 		return -EPERM;
2536e098bc96SEvan Quan 
25374a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2538e098bc96SEvan Quan 	if (err < 0) {
25394a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2540e098bc96SEvan Quan 		return err;
2541e098bc96SEvan Quan 	}
2542e098bc96SEvan Quan 
2543f46587bcSDarren Powell 	if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2544e098bc96SEvan Quan 		err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2545e098bc96SEvan Quan 	else
2546e098bc96SEvan Quan 		err = -EINVAL;
2547e098bc96SEvan Quan 
25484a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25494a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2550e098bc96SEvan Quan 
2551e098bc96SEvan Quan 	if (err)
2552e098bc96SEvan Quan 		return err;
2553e098bc96SEvan Quan 
2554fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2555e098bc96SEvan Quan }
2556e098bc96SEvan Quan 
2557e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2558e098bc96SEvan Quan 					 struct device_attribute *attr,
2559e098bc96SEvan Quan 					 char *buf)
2560e098bc96SEvan Quan {
2561e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2562e098bc96SEvan Quan 	u32 min_rpm = 0;
2563e098bc96SEvan Quan 	u32 size = sizeof(min_rpm);
2564e098bc96SEvan Quan 	int r;
2565e098bc96SEvan Quan 
256653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2567e098bc96SEvan Quan 		return -EPERM;
2568d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2569d2ae842dSAlex Deucher 		return -EPERM;
2570e098bc96SEvan Quan 
25714a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2572e098bc96SEvan Quan 	if (r < 0) {
25734a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2574e098bc96SEvan Quan 		return r;
2575e098bc96SEvan Quan 	}
2576e098bc96SEvan Quan 
2577e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2578e098bc96SEvan Quan 				   (void *)&min_rpm, &size);
2579e098bc96SEvan Quan 
25804a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25814a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2582e098bc96SEvan Quan 
2583e098bc96SEvan Quan 	if (r)
2584e098bc96SEvan Quan 		return r;
2585e098bc96SEvan Quan 
2586a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2587e098bc96SEvan Quan }
2588e098bc96SEvan Quan 
2589e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2590e098bc96SEvan Quan 					 struct device_attribute *attr,
2591e098bc96SEvan Quan 					 char *buf)
2592e098bc96SEvan Quan {
2593e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2594e098bc96SEvan Quan 	u32 max_rpm = 0;
2595e098bc96SEvan Quan 	u32 size = sizeof(max_rpm);
2596e098bc96SEvan Quan 	int r;
2597e098bc96SEvan Quan 
259853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2599e098bc96SEvan Quan 		return -EPERM;
2600d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2601d2ae842dSAlex Deucher 		return -EPERM;
2602e098bc96SEvan Quan 
26034a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2604e098bc96SEvan Quan 	if (r < 0) {
26054a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2606e098bc96SEvan Quan 		return r;
2607e098bc96SEvan Quan 	}
2608e098bc96SEvan Quan 
2609e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2610e098bc96SEvan Quan 				   (void *)&max_rpm, &size);
2611e098bc96SEvan Quan 
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 
2615e098bc96SEvan Quan 	if (r)
2616e098bc96SEvan Quan 		return r;
2617e098bc96SEvan Quan 
2618a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2619e098bc96SEvan Quan }
2620e098bc96SEvan Quan 
2621e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2622e098bc96SEvan Quan 					   struct device_attribute *attr,
2623e098bc96SEvan Quan 					   char *buf)
2624e098bc96SEvan Quan {
2625e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2626e098bc96SEvan Quan 	int err;
2627e098bc96SEvan Quan 	u32 rpm = 0;
2628e098bc96SEvan Quan 
262953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2630e098bc96SEvan Quan 		return -EPERM;
2631d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2632d2ae842dSAlex Deucher 		return -EPERM;
2633e098bc96SEvan Quan 
26344a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2635e098bc96SEvan Quan 	if (err < 0) {
26364a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2637e098bc96SEvan Quan 		return err;
2638e098bc96SEvan Quan 	}
2639e098bc96SEvan Quan 
2640f46587bcSDarren Powell 	if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2641e098bc96SEvan Quan 		err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2642e098bc96SEvan Quan 	else
2643e098bc96SEvan Quan 		err = -EINVAL;
2644e098bc96SEvan Quan 
26454a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26464a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2647e098bc96SEvan Quan 
2648e098bc96SEvan Quan 	if (err)
2649e098bc96SEvan Quan 		return err;
2650e098bc96SEvan Quan 
2651fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2652e098bc96SEvan Quan }
2653e098bc96SEvan Quan 
2654e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2655e098bc96SEvan Quan 				     struct device_attribute *attr,
2656e098bc96SEvan Quan 				     const char *buf, size_t count)
2657e098bc96SEvan Quan {
2658e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2659e098bc96SEvan Quan 	int err;
2660e098bc96SEvan Quan 	u32 value;
2661e098bc96SEvan Quan 	u32 pwm_mode;
2662e098bc96SEvan Quan 
266353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2664e098bc96SEvan Quan 		return -EPERM;
2665d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2666d2ae842dSAlex Deucher 		return -EPERM;
2667e098bc96SEvan Quan 
26684a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2669e098bc96SEvan Quan 	if (err < 0) {
26704a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2671e098bc96SEvan Quan 		return err;
2672e098bc96SEvan Quan 	}
2673e098bc96SEvan Quan 
2674e098bc96SEvan Quan 	pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2675e098bc96SEvan Quan 
2676e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
26774a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26784a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2679e098bc96SEvan Quan 		return -ENODATA;
2680e098bc96SEvan Quan 	}
2681e098bc96SEvan Quan 
2682e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2683e098bc96SEvan Quan 	if (err) {
26844a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26854a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2686e098bc96SEvan Quan 		return err;
2687e098bc96SEvan Quan 	}
2688e098bc96SEvan Quan 
2689f46587bcSDarren Powell 	if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2690e098bc96SEvan Quan 		err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2691e098bc96SEvan Quan 	else
2692e098bc96SEvan Quan 		err = -EINVAL;
2693e098bc96SEvan Quan 
26944a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26954a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2696e098bc96SEvan Quan 
2697e098bc96SEvan Quan 	if (err)
2698e098bc96SEvan Quan 		return err;
2699e098bc96SEvan Quan 
2700e098bc96SEvan Quan 	return count;
2701e098bc96SEvan Quan }
2702e098bc96SEvan Quan 
2703e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2704e098bc96SEvan Quan 					    struct device_attribute *attr,
2705e098bc96SEvan Quan 					    char *buf)
2706e098bc96SEvan Quan {
2707e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2708e098bc96SEvan Quan 	u32 pwm_mode = 0;
2709e098bc96SEvan Quan 	int ret;
2710e098bc96SEvan Quan 
271153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2712e098bc96SEvan Quan 		return -EPERM;
2713d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2714d2ae842dSAlex Deucher 		return -EPERM;
2715e098bc96SEvan Quan 
27164a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2717e098bc96SEvan Quan 	if (ret < 0) {
27184a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2719e098bc96SEvan Quan 		return ret;
2720e098bc96SEvan Quan 	}
2721e098bc96SEvan Quan 
2722e098bc96SEvan Quan 	if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
27234a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27244a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2725e098bc96SEvan Quan 		return -EINVAL;
2726e098bc96SEvan Quan 	}
2727e098bc96SEvan Quan 
2728e098bc96SEvan Quan 	pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2729e098bc96SEvan Quan 
27304a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27314a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2732e098bc96SEvan Quan 
2733fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2734e098bc96SEvan Quan }
2735e098bc96SEvan Quan 
2736e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2737e098bc96SEvan Quan 					    struct device_attribute *attr,
2738e098bc96SEvan Quan 					    const char *buf,
2739e098bc96SEvan Quan 					    size_t count)
2740e098bc96SEvan Quan {
2741e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2742e098bc96SEvan Quan 	int err;
2743e098bc96SEvan Quan 	int value;
2744e098bc96SEvan Quan 	u32 pwm_mode;
2745e098bc96SEvan Quan 
274653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2747e098bc96SEvan Quan 		return -EPERM;
2748d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2749d2ae842dSAlex Deucher 		return -EPERM;
2750e098bc96SEvan Quan 
2751e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2752e098bc96SEvan Quan 	if (err)
2753e098bc96SEvan Quan 		return err;
2754e098bc96SEvan Quan 
2755e098bc96SEvan Quan 	if (value == 0)
2756e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2757e098bc96SEvan Quan 	else if (value == 1)
2758e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2759e098bc96SEvan Quan 	else
2760e098bc96SEvan Quan 		return -EINVAL;
2761e098bc96SEvan Quan 
27624a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2763e098bc96SEvan Quan 	if (err < 0) {
27644a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2765e098bc96SEvan Quan 		return err;
2766e098bc96SEvan Quan 	}
2767e098bc96SEvan Quan 
2768e098bc96SEvan Quan 	if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
27694a580877SLuben Tuikov 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27704a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2771e098bc96SEvan Quan 		return -EINVAL;
2772e098bc96SEvan Quan 	}
2773e098bc96SEvan Quan 	amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2774e098bc96SEvan Quan 
27754a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27764a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2777e098bc96SEvan Quan 
2778e098bc96SEvan Quan 	return count;
2779e098bc96SEvan Quan }
2780e098bc96SEvan Quan 
2781e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2782e098bc96SEvan Quan 					struct device_attribute *attr,
2783e098bc96SEvan Quan 					char *buf)
2784e098bc96SEvan Quan {
2785e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2786e098bc96SEvan Quan 	u32 vddgfx;
2787e098bc96SEvan Quan 	int r, size = sizeof(vddgfx);
2788e098bc96SEvan Quan 
278953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2790e098bc96SEvan Quan 		return -EPERM;
2791d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2792d2ae842dSAlex Deucher 		return -EPERM;
2793e098bc96SEvan Quan 
27944a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2795e098bc96SEvan Quan 	if (r < 0) {
27964a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2797e098bc96SEvan Quan 		return r;
2798e098bc96SEvan Quan 	}
2799e098bc96SEvan Quan 
2800e098bc96SEvan Quan 	/* get the voltage */
2801e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2802e098bc96SEvan Quan 				   (void *)&vddgfx, &size);
2803e098bc96SEvan Quan 
28044a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28054a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2806e098bc96SEvan Quan 
2807e098bc96SEvan Quan 	if (r)
2808e098bc96SEvan Quan 		return r;
2809e098bc96SEvan Quan 
2810a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
2811e098bc96SEvan Quan }
2812e098bc96SEvan Quan 
2813e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2814e098bc96SEvan Quan 					      struct device_attribute *attr,
2815e098bc96SEvan Quan 					      char *buf)
2816e098bc96SEvan Quan {
2817a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
2818e098bc96SEvan Quan }
2819e098bc96SEvan Quan 
2820e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2821e098bc96SEvan Quan 				       struct device_attribute *attr,
2822e098bc96SEvan Quan 				       char *buf)
2823e098bc96SEvan Quan {
2824e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2825e098bc96SEvan Quan 	u32 vddnb;
2826e098bc96SEvan Quan 	int r, size = sizeof(vddnb);
2827e098bc96SEvan Quan 
282853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2829e098bc96SEvan Quan 		return -EPERM;
2830d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2831d2ae842dSAlex Deucher 		return -EPERM;
2832e098bc96SEvan Quan 
2833e098bc96SEvan Quan 	/* only APUs have vddnb */
2834e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2835e098bc96SEvan Quan 		return -EINVAL;
2836e098bc96SEvan Quan 
28374a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2838e098bc96SEvan Quan 	if (r < 0) {
28394a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2840e098bc96SEvan Quan 		return r;
2841e098bc96SEvan Quan 	}
2842e098bc96SEvan Quan 
2843e098bc96SEvan Quan 	/* get the voltage */
2844e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2845e098bc96SEvan Quan 				   (void *)&vddnb, &size);
2846e098bc96SEvan Quan 
28474a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28484a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2849e098bc96SEvan Quan 
2850e098bc96SEvan Quan 	if (r)
2851e098bc96SEvan Quan 		return r;
2852e098bc96SEvan Quan 
2853a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
2854e098bc96SEvan Quan }
2855e098bc96SEvan Quan 
2856e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2857e098bc96SEvan Quan 					      struct device_attribute *attr,
2858e098bc96SEvan Quan 					      char *buf)
2859e098bc96SEvan Quan {
2860a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
2861e098bc96SEvan Quan }
2862e098bc96SEvan Quan 
2863e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2864e098bc96SEvan Quan 					   struct device_attribute *attr,
2865e098bc96SEvan Quan 					   char *buf)
2866e098bc96SEvan Quan {
2867e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2868e098bc96SEvan Quan 	u32 query = 0;
2869e098bc96SEvan Quan 	int r, size = sizeof(u32);
2870e098bc96SEvan Quan 	unsigned uw;
2871e098bc96SEvan Quan 
287253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2873e098bc96SEvan Quan 		return -EPERM;
2874d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2875d2ae842dSAlex Deucher 		return -EPERM;
2876e098bc96SEvan Quan 
28774a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2878e098bc96SEvan Quan 	if (r < 0) {
28794a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2880e098bc96SEvan Quan 		return r;
2881e098bc96SEvan Quan 	}
2882e098bc96SEvan Quan 
2883e098bc96SEvan Quan 	/* get the voltage */
2884e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2885e098bc96SEvan Quan 				   (void *)&query, &size);
2886e098bc96SEvan Quan 
28874a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28884a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2889e098bc96SEvan Quan 
2890e098bc96SEvan Quan 	if (r)
2891e098bc96SEvan Quan 		return r;
2892e098bc96SEvan Quan 
2893e098bc96SEvan Quan 	/* convert to microwatts */
2894e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2895e098bc96SEvan Quan 
2896a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", uw);
2897e098bc96SEvan Quan }
2898e098bc96SEvan Quan 
2899e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2900e098bc96SEvan Quan 					 struct device_attribute *attr,
2901e098bc96SEvan Quan 					 char *buf)
2902e098bc96SEvan Quan {
2903fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2904e098bc96SEvan Quan }
2905e098bc96SEvan Quan 
290691161b06SDarren Powell 
290791161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2908e098bc96SEvan Quan 					struct device_attribute *attr,
290991161b06SDarren Powell 					char *buf,
291091161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
2911e098bc96SEvan Quan {
2912e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
29138dfc8c53SDarren Powell 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
2914a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2915a40a020dSDarren Powell 	uint32_t limit;
2916e098bc96SEvan Quan 	ssize_t size;
2917e098bc96SEvan Quan 	int r;
2918e098bc96SEvan Quan 
291953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2920e098bc96SEvan Quan 		return -EPERM;
2921d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2922d2ae842dSAlex Deucher 		return -EPERM;
2923e098bc96SEvan Quan 
292491161b06SDarren Powell 	if ( !(pp_funcs && pp_funcs->get_power_limit))
292591161b06SDarren Powell 		return -ENODATA;
292691161b06SDarren Powell 
29274a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2928e098bc96SEvan Quan 	if (r < 0) {
29294a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2930e098bc96SEvan Quan 		return r;
2931e098bc96SEvan Quan 	}
2932e098bc96SEvan Quan 
2933dc2a8240SDarren Powell 	r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,
293404bec521SDarren Powell 				      pp_limit_level, power_type);
2935dc2a8240SDarren Powell 
2936dc2a8240SDarren Powell 	if (!r)
293709b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2938dc2a8240SDarren Powell 	else
293909b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
2940e098bc96SEvan Quan 
29414a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29424a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2943e098bc96SEvan Quan 
2944e098bc96SEvan Quan 	return size;
2945e098bc96SEvan Quan }
2946e098bc96SEvan Quan 
294791161b06SDarren Powell 
294891161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
294991161b06SDarren Powell 					 struct device_attribute *attr,
295091161b06SDarren Powell 					 char *buf)
295191161b06SDarren Powell {
295291161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
295391161b06SDarren Powell 
295491161b06SDarren Powell }
295591161b06SDarren Powell 
2956e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2957e098bc96SEvan Quan 					 struct device_attribute *attr,
2958e098bc96SEvan Quan 					 char *buf)
2959e098bc96SEvan Quan {
296091161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2961e098bc96SEvan Quan 
2962e098bc96SEvan Quan }
2963e098bc96SEvan Quan 
29646e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
29656e58941cSEric Huang 					 struct device_attribute *attr,
29666e58941cSEric Huang 					 char *buf)
29676e58941cSEric Huang {
296891161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
29696e58941cSEric Huang 
29706e58941cSEric Huang }
29716e58941cSEric Huang 
2972ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2973ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
2974ae07970aSXiaomeng Hou 					 char *buf)
2975ae07970aSXiaomeng Hou {
2976ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
2977ae07970aSXiaomeng Hou 
2978a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
2979ae07970aSXiaomeng Hou 		limit_type == SMU_FAST_PPT_LIMIT ? "fastPPT" : "slowPPT");
2980ae07970aSXiaomeng Hou }
2981e098bc96SEvan Quan 
2982e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2983e098bc96SEvan Quan 		struct device_attribute *attr,
2984e098bc96SEvan Quan 		const char *buf,
2985e098bc96SEvan Quan 		size_t count)
2986e098bc96SEvan Quan {
2987e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
29888dfc8c53SDarren Powell 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
2989ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
2990e098bc96SEvan Quan 	int err;
2991e098bc96SEvan Quan 	u32 value;
2992e098bc96SEvan Quan 
299353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2994e098bc96SEvan Quan 		return -EPERM;
2995d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2996d2ae842dSAlex Deucher 		return -EPERM;
2997e098bc96SEvan Quan 
2998e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
2999e098bc96SEvan Quan 		return -EINVAL;
3000e098bc96SEvan Quan 
3001e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
3002e098bc96SEvan Quan 	if (err)
3003e098bc96SEvan Quan 		return err;
3004e098bc96SEvan Quan 
3005e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
3006ae07970aSXiaomeng Hou 	value |= limit_type << 24;
3007e098bc96SEvan Quan 
30084a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3009e098bc96SEvan Quan 	if (err < 0) {
30104a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3011e098bc96SEvan Quan 		return err;
3012e098bc96SEvan Quan 	}
3013e098bc96SEvan Quan 
30148dfc8c53SDarren Powell 	if (pp_funcs && pp_funcs->set_power_limit)
30158dfc8c53SDarren Powell 		err = pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
3016e098bc96SEvan Quan 	else
3017e098bc96SEvan Quan 		err = -EINVAL;
3018e098bc96SEvan Quan 
30194a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30204a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3021e098bc96SEvan Quan 
3022e098bc96SEvan Quan 	if (err)
3023e098bc96SEvan Quan 		return err;
3024e098bc96SEvan Quan 
3025e098bc96SEvan Quan 	return count;
3026e098bc96SEvan Quan }
3027e098bc96SEvan Quan 
3028e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3029e098bc96SEvan Quan 				      struct device_attribute *attr,
3030e098bc96SEvan Quan 				      char *buf)
3031e098bc96SEvan Quan {
3032e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3033e098bc96SEvan Quan 	uint32_t sclk;
3034e098bc96SEvan Quan 	int r, size = sizeof(sclk);
3035e098bc96SEvan Quan 
303653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3037e098bc96SEvan Quan 		return -EPERM;
3038d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3039d2ae842dSAlex Deucher 		return -EPERM;
3040e098bc96SEvan Quan 
30414a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3042e098bc96SEvan Quan 	if (r < 0) {
30434a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3044e098bc96SEvan Quan 		return r;
3045e098bc96SEvan Quan 	}
3046e098bc96SEvan Quan 
3047e098bc96SEvan Quan 	/* get the sclk */
3048e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3049e098bc96SEvan Quan 				   (void *)&sclk, &size);
3050e098bc96SEvan Quan 
30514a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30524a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3053e098bc96SEvan Quan 
3054e098bc96SEvan Quan 	if (r)
3055e098bc96SEvan Quan 		return r;
3056e098bc96SEvan Quan 
3057a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3058e098bc96SEvan Quan }
3059e098bc96SEvan Quan 
3060e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3061e098bc96SEvan Quan 					    struct device_attribute *attr,
3062e098bc96SEvan Quan 					    char *buf)
3063e098bc96SEvan Quan {
3064a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
3065e098bc96SEvan Quan }
3066e098bc96SEvan Quan 
3067e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3068e098bc96SEvan Quan 				      struct device_attribute *attr,
3069e098bc96SEvan Quan 				      char *buf)
3070e098bc96SEvan Quan {
3071e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3072e098bc96SEvan Quan 	uint32_t mclk;
3073e098bc96SEvan Quan 	int r, size = sizeof(mclk);
3074e098bc96SEvan Quan 
307553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3076e098bc96SEvan Quan 		return -EPERM;
3077d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3078d2ae842dSAlex Deucher 		return -EPERM;
3079e098bc96SEvan Quan 
30804a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3081e098bc96SEvan Quan 	if (r < 0) {
30824a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3083e098bc96SEvan Quan 		return r;
3084e098bc96SEvan Quan 	}
3085e098bc96SEvan Quan 
3086e098bc96SEvan Quan 	/* get the sclk */
3087e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3088e098bc96SEvan Quan 				   (void *)&mclk, &size);
3089e098bc96SEvan Quan 
30904a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30914a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3092e098bc96SEvan Quan 
3093e098bc96SEvan Quan 	if (r)
3094e098bc96SEvan Quan 		return r;
3095e098bc96SEvan Quan 
3096a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3097e098bc96SEvan Quan }
3098e098bc96SEvan Quan 
3099e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3100e098bc96SEvan Quan 					    struct device_attribute *attr,
3101e098bc96SEvan Quan 					    char *buf)
3102e098bc96SEvan Quan {
3103a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3104e098bc96SEvan Quan }
3105e098bc96SEvan Quan 
3106e098bc96SEvan Quan /**
3107e098bc96SEvan Quan  * DOC: hwmon
3108e098bc96SEvan Quan  *
3109e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3110e098bc96SEvan Quan  *
3111e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3112e098bc96SEvan Quan  *
3113e098bc96SEvan Quan  * - GPU voltage
3114e098bc96SEvan Quan  *
3115e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3116e098bc96SEvan Quan  *
3117e098bc96SEvan Quan  * - GPU power
3118e098bc96SEvan Quan  *
3119e098bc96SEvan Quan  * - GPU fan
3120e098bc96SEvan Quan  *
3121e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3122e098bc96SEvan Quan  *
3123e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3124e098bc96SEvan Quan  *
3125e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3126e098bc96SEvan Quan  *
3127e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3128e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3129e098bc96SEvan Quan  *
3130e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3131e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3132e098bc96SEvan Quan  *
3133e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3134e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3135e098bc96SEvan Quan  *
3136e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3137e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3138e098bc96SEvan Quan  *
3139e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3140e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3141e098bc96SEvan Quan  *
3142e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3143e098bc96SEvan Quan  *
3144e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3145e098bc96SEvan Quan  *
3146e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3147e098bc96SEvan Quan  *
3148e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3149e098bc96SEvan Quan  *
3150e098bc96SEvan Quan  * - power1_average: average power used by the GPU in microWatts
3151e098bc96SEvan Quan  *
3152e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3153e098bc96SEvan Quan  *
3154e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3155e098bc96SEvan Quan  *
3156e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3157e098bc96SEvan Quan  *
3158e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3159e098bc96SEvan Quan  *
3160e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3161e098bc96SEvan Quan  *
3162e098bc96SEvan 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)
3163e098bc96SEvan Quan  *
3164e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3165e098bc96SEvan Quan  *
3166e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3167e098bc96SEvan Quan  *
3168e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3169e098bc96SEvan Quan  *
3170e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3171e098bc96SEvan Quan  *
3172e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3173e098bc96SEvan Quan  *
3174e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3175e098bc96SEvan Quan  *
3176e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3177e098bc96SEvan Quan  *
317896401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
317996401f7cSEvan Quan  *       That will get the former one overridden.
318096401f7cSEvan Quan  *
3181e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3182e098bc96SEvan Quan  *
3183e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3184e098bc96SEvan Quan  *
3185e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3186e098bc96SEvan Quan  *
3187e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3188e098bc96SEvan Quan  *
3189e098bc96SEvan Quan  */
3190e098bc96SEvan Quan 
3191e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3192e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3193e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3194e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3195e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3196e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3197e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3198e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3199e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3200e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3201e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3202e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3203e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3204e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3205e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3206e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3207e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3208e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3209e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3210e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3211e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3212e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3213e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3214e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3215e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3216e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3217e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3218e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3219e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3220e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3221e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3222e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
32236e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3224ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3225ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3226ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3227ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3228ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
32296e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3230ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3231e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3232e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3233e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3234e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3235e098bc96SEvan Quan 
3236e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3237e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3238e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3239e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3240e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3241e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3242e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3243e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3244e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3245e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3246e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3247e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3248e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3249e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3250e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3251e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3252e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3253e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3254e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3255e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3256e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3257e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3258e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3259e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3260e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3261e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3262e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3263e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3264e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3265e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3266e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3267e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3268e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
32696e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3270ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3271ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3272ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3273ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3274ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
32756e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3276ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3277e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3278e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3279e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3280e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3281e098bc96SEvan Quan 	NULL
3282e098bc96SEvan Quan };
3283e098bc96SEvan Quan 
3284e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3285e098bc96SEvan Quan 					struct attribute *attr, int index)
3286e098bc96SEvan Quan {
3287e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3288e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3289e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
3290e098bc96SEvan Quan 
3291e098bc96SEvan Quan 	/* under multi-vf mode, the hwmon attributes are all not supported */
3292e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3293e098bc96SEvan Quan 		return 0;
3294e098bc96SEvan Quan 
3295e098bc96SEvan Quan 	/* there is no fan under pp one vf mode */
3296e098bc96SEvan Quan 	if (amdgpu_sriov_is_pp_one_vf(adev) &&
3297e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3298e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3299e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3300e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3301e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3302e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3303e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3304e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3305e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3306e098bc96SEvan Quan 		return 0;
3307e098bc96SEvan Quan 
3308e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3309e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3310e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3311e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3312e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3313e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3314e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3315e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3316e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3317e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3318e098bc96SEvan Quan 		return 0;
3319e098bc96SEvan Quan 
3320e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3321e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3322e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3323e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3324e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3325e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3326e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3327e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3328e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3329e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3330e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3331e098bc96SEvan Quan 		return 0;
3332e098bc96SEvan Quan 
3333e098bc96SEvan Quan 	/* Skip crit temp on APU */
3334e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3335e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3336e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3337e098bc96SEvan Quan 		return 0;
3338e098bc96SEvan Quan 
3339e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3340e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3341e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3342e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3343e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3344e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3345e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3346e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3347e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3348e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3349e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3350e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3351e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3352e098bc96SEvan Quan 		return 0;
3353e098bc96SEvan Quan 
3354e098bc96SEvan Quan 	if (!is_support_sw_smu(adev)) {
3355e098bc96SEvan Quan 		/* mask fan attributes if we have no bindings for this asic to expose */
3356*0d8318e1SEvan Quan 		if ((!adev->powerplay.pp_funcs->get_fan_speed_pwm &&
3357e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3358e098bc96SEvan Quan 		    (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3359e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3360e098bc96SEvan Quan 			effective_mode &= ~S_IRUGO;
3361e098bc96SEvan Quan 
3362*0d8318e1SEvan Quan 		if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm &&
3363e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3364e098bc96SEvan Quan 		    (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3365e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3366e098bc96SEvan Quan 			effective_mode &= ~S_IWUSR;
3367e098bc96SEvan Quan 	}
3368e098bc96SEvan Quan 
3369ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3370ae07970aSXiaomeng Hou 		 ((adev->flags & AMD_IS_APU) &&
3371ae07970aSXiaomeng Hou 	      (adev->asic_type != CHIP_VANGOGH))) &&	/* not implemented yet */
3372367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3373e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
33746e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
33756e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3376e098bc96SEvan Quan 		return 0;
3377e098bc96SEvan Quan 
3378367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3379367deb67SAlex Deucher 	     ((adev->flags & AMD_IS_APU) &&
3380367deb67SAlex Deucher 	      (adev->asic_type < CHIP_RENOIR))) &&	/* not implemented yet */
3381367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3382367deb67SAlex Deucher 		return 0;
3383367deb67SAlex Deucher 
3384e098bc96SEvan Quan 	if (!is_support_sw_smu(adev)) {
3385e098bc96SEvan Quan 		/* hide max/min values if we can't both query and manage the fan */
3386*0d8318e1SEvan Quan 		if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm &&
3387*0d8318e1SEvan Quan 		     !adev->powerplay.pp_funcs->get_fan_speed_pwm) &&
3388e098bc96SEvan Quan 		     (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3389e098bc96SEvan Quan 		     !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3390e098bc96SEvan Quan 		    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3391e098bc96SEvan Quan 		     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3392e098bc96SEvan Quan 			return 0;
3393e098bc96SEvan Quan 
3394e098bc96SEvan Quan 		if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3395e098bc96SEvan Quan 		     !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3396e098bc96SEvan Quan 		    (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3397e098bc96SEvan Quan 		     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3398e098bc96SEvan Quan 			return 0;
3399e098bc96SEvan Quan 	}
3400e098bc96SEvan Quan 
3401e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3402e098bc96SEvan Quan 	     adev->family == AMDGPU_FAMILY_KV) &&	/* not implemented yet */
3403e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3404e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3405e098bc96SEvan Quan 		return 0;
3406e098bc96SEvan Quan 
3407e098bc96SEvan Quan 	/* only APUs have vddnb */
3408e098bc96SEvan Quan 	if (!(adev->flags & AMD_IS_APU) &&
3409e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3410e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3411e098bc96SEvan Quan 		return 0;
3412e098bc96SEvan Quan 
3413e098bc96SEvan Quan 	/* no mclk on APUs */
3414e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3415e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3416e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3417e098bc96SEvan Quan 		return 0;
3418e098bc96SEvan Quan 
3419e098bc96SEvan Quan 	/* only SOC15 dGPUs support hotspot and mem temperatures */
3420e098bc96SEvan Quan 	if (((adev->flags & AMD_IS_APU) ||
3421e098bc96SEvan Quan 	     adev->asic_type < CHIP_VEGA10) &&
3422e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3423e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3424e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3425e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3426e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3427e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3428e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3429e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3430e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3431e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3432e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3433e098bc96SEvan Quan 		return 0;
3434e098bc96SEvan Quan 
3435ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
3436ae07970aSXiaomeng Hou 	if (!(adev->asic_type == CHIP_VANGOGH) &&
3437ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3438ae07970aSXiaomeng Hou 		 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3439ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3440ae07970aSXiaomeng Hou 		 attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
34416e58941cSEric Huang 		 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3442ae07970aSXiaomeng Hou 		 attr == &sensor_dev_attr_power2_label.dev_attr.attr ||
3443ae07970aSXiaomeng Hou 		 attr == &sensor_dev_attr_power1_label.dev_attr.attr))
3444ae07970aSXiaomeng Hou 		return 0;
3445ae07970aSXiaomeng Hou 
3446e098bc96SEvan Quan 	return effective_mode;
3447e098bc96SEvan Quan }
3448e098bc96SEvan Quan 
3449e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3450e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3451e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3452e098bc96SEvan Quan };
3453e098bc96SEvan Quan 
3454e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3455e098bc96SEvan Quan 	&hwmon_attrgroup,
3456e098bc96SEvan Quan 	NULL
3457e098bc96SEvan Quan };
3458e098bc96SEvan Quan 
3459e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3460e098bc96SEvan Quan {
3461e098bc96SEvan Quan 	int ret;
3462e098bc96SEvan Quan 	uint32_t mask = 0;
3463e098bc96SEvan Quan 
3464e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
3465e098bc96SEvan Quan 		return 0;
3466e098bc96SEvan Quan 
3467e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3468e098bc96SEvan Quan 		return 0;
3469e098bc96SEvan Quan 
3470e098bc96SEvan Quan 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3471e098bc96SEvan Quan 
3472e098bc96SEvan Quan 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3473e098bc96SEvan Quan 								   DRIVER_NAME, adev,
3474e098bc96SEvan Quan 								   hwmon_groups);
3475e098bc96SEvan Quan 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3476e098bc96SEvan Quan 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3477e098bc96SEvan Quan 		dev_err(adev->dev,
3478e098bc96SEvan Quan 			"Unable to register hwmon device: %d\n", ret);
3479e098bc96SEvan Quan 		return ret;
3480e098bc96SEvan Quan 	}
3481e098bc96SEvan Quan 
3482e098bc96SEvan Quan 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3483e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
3484e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
3485e098bc96SEvan Quan 		break;
3486e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
3487e098bc96SEvan Quan 		mask = 0;
3488e098bc96SEvan Quan 		break;
3489e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
3490e098bc96SEvan Quan 	default:
3491e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
3492e098bc96SEvan Quan 		break;
3493e098bc96SEvan Quan 	}
3494e098bc96SEvan Quan 
3495e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
3496e098bc96SEvan Quan 					       amdgpu_device_attrs,
3497e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
3498e098bc96SEvan Quan 					       mask,
3499e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
3500e098bc96SEvan Quan 	if (ret)
3501e098bc96SEvan Quan 		return ret;
3502e098bc96SEvan Quan 
3503e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
3504e098bc96SEvan Quan 
3505e098bc96SEvan Quan 	return 0;
3506e098bc96SEvan Quan }
3507e098bc96SEvan Quan 
3508e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3509e098bc96SEvan Quan {
3510e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3511e098bc96SEvan Quan 		return;
3512e098bc96SEvan Quan 
3513e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
3514e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3515e098bc96SEvan Quan 
3516e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3517e098bc96SEvan Quan }
3518e098bc96SEvan Quan 
3519e098bc96SEvan Quan /*
3520e098bc96SEvan Quan  * Debugfs info
3521e098bc96SEvan Quan  */
3522e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3523e098bc96SEvan Quan 
3524517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3525517cb957SHuang Rui 					   struct amdgpu_device *adev) {
3526517cb957SHuang Rui 	uint16_t *p_val;
3527517cb957SHuang Rui 	uint32_t size;
3528517cb957SHuang Rui 	int i;
3529517cb957SHuang Rui 
3530517cb957SHuang Rui 	if (is_support_cclk_dpm(adev)) {
35314aef0ebcSHuang Rui 		p_val = kcalloc(adev->smu.cpu_core_num, sizeof(uint16_t),
3532517cb957SHuang Rui 				GFP_KERNEL);
3533517cb957SHuang Rui 
3534517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3535517cb957SHuang Rui 					    (void *)p_val, &size)) {
35364aef0ebcSHuang Rui 			for (i = 0; i < adev->smu.cpu_core_num; i++)
3537517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3538517cb957SHuang Rui 					   *(p_val + i), i);
3539517cb957SHuang Rui 		}
3540517cb957SHuang Rui 
3541517cb957SHuang Rui 		kfree(p_val);
3542517cb957SHuang Rui 	}
3543517cb957SHuang Rui }
3544517cb957SHuang Rui 
3545e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3546e098bc96SEvan Quan {
3547e098bc96SEvan Quan 	uint32_t value;
3548800c53d6SXiaojian Du 	uint64_t value64 = 0;
3549e098bc96SEvan Quan 	uint32_t query = 0;
3550e098bc96SEvan Quan 	int size;
3551e098bc96SEvan Quan 
3552e098bc96SEvan Quan 	/* GPU Clocks */
3553e098bc96SEvan Quan 	size = sizeof(value);
3554e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
3555517cb957SHuang Rui 
3556517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
3557517cb957SHuang Rui 
3558e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3559e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3560e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3561e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3562e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3563e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3564e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3565e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3566e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3567e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3568e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3569e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3570e098bc96SEvan Quan 	size = sizeof(uint32_t);
3571e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3572e098bc96SEvan Quan 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3573e098bc96SEvan Quan 	size = sizeof(value);
3574e098bc96SEvan Quan 	seq_printf(m, "\n");
3575e098bc96SEvan Quan 
3576e098bc96SEvan Quan 	/* GPU Temp */
3577e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3578e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3579e098bc96SEvan Quan 
3580e098bc96SEvan Quan 	/* GPU Load */
3581e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3582e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
3583e098bc96SEvan Quan 	/* MEM Load */
3584e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3585e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
3586e098bc96SEvan Quan 
3587e098bc96SEvan Quan 	seq_printf(m, "\n");
3588e098bc96SEvan Quan 
3589e098bc96SEvan Quan 	/* SMC feature mask */
3590e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3591e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3592e098bc96SEvan Quan 
3593e098bc96SEvan Quan 	if (adev->asic_type > CHIP_VEGA20) {
3594e098bc96SEvan Quan 		/* VCN clocks */
3595e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3596e098bc96SEvan Quan 			if (!value) {
3597e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
3598e098bc96SEvan Quan 			} else {
3599e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
3600e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3601e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3602e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3603e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3604e098bc96SEvan Quan 			}
3605e098bc96SEvan Quan 		}
3606e098bc96SEvan Quan 		seq_printf(m, "\n");
3607e098bc96SEvan Quan 	} else {
3608e098bc96SEvan Quan 		/* UVD clocks */
3609e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3610e098bc96SEvan Quan 			if (!value) {
3611e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
3612e098bc96SEvan Quan 			} else {
3613e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
3614e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3615e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3616e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3617e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3618e098bc96SEvan Quan 			}
3619e098bc96SEvan Quan 		}
3620e098bc96SEvan Quan 		seq_printf(m, "\n");
3621e098bc96SEvan Quan 
3622e098bc96SEvan Quan 		/* VCE clocks */
3623e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3624e098bc96SEvan Quan 			if (!value) {
3625e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
3626e098bc96SEvan Quan 			} else {
3627e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
3628e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3629e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3630e098bc96SEvan Quan 			}
3631e098bc96SEvan Quan 		}
3632e098bc96SEvan Quan 	}
3633e098bc96SEvan Quan 
3634e098bc96SEvan Quan 	return 0;
3635e098bc96SEvan Quan }
3636e098bc96SEvan Quan 
3637e098bc96SEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3638e098bc96SEvan Quan {
3639e098bc96SEvan Quan 	int i;
3640e098bc96SEvan Quan 
3641e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
3642e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3643e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
3644e098bc96SEvan Quan }
3645e098bc96SEvan Quan 
3646373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3647e098bc96SEvan Quan {
3648373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3649373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
3650e098bc96SEvan Quan 	u32 flags = 0;
3651e098bc96SEvan Quan 	int r;
3652e098bc96SEvan Quan 
365353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3654e098bc96SEvan Quan 		return -EPERM;
3655d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3656d2ae842dSAlex Deucher 		return -EPERM;
3657e098bc96SEvan Quan 
3658e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
3659e098bc96SEvan Quan 	if (r < 0) {
3660e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3661e098bc96SEvan Quan 		return r;
3662e098bc96SEvan Quan 	}
3663e098bc96SEvan Quan 
3664e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled) {
3665e098bc96SEvan Quan 		seq_printf(m, "dpm not enabled\n");
3666e098bc96SEvan Quan 		pm_runtime_mark_last_busy(dev->dev);
3667e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3668e098bc96SEvan Quan 		return 0;
3669e098bc96SEvan Quan 	}
3670e098bc96SEvan Quan 
3671e098bc96SEvan Quan 	if (!is_support_sw_smu(adev) &&
3672e098bc96SEvan Quan 	    adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3673e098bc96SEvan Quan 		mutex_lock(&adev->pm.mutex);
3674e098bc96SEvan Quan 		if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3675e098bc96SEvan Quan 			adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3676e098bc96SEvan Quan 		else
3677e098bc96SEvan Quan 			seq_printf(m, "Debugfs support not implemented for this asic\n");
3678e098bc96SEvan Quan 		mutex_unlock(&adev->pm.mutex);
3679e098bc96SEvan Quan 		r = 0;
3680e098bc96SEvan Quan 	} else {
3681e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3682e098bc96SEvan Quan 	}
3683e098bc96SEvan Quan 	if (r)
3684e098bc96SEvan Quan 		goto out;
3685e098bc96SEvan Quan 
3686e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3687e098bc96SEvan Quan 
3688e098bc96SEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3689e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
3690e098bc96SEvan Quan 	seq_printf(m, "\n");
3691e098bc96SEvan Quan 
3692e098bc96SEvan Quan out:
3693e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
3694e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
3695e098bc96SEvan Quan 
3696e098bc96SEvan Quan 	return r;
3697e098bc96SEvan Quan }
3698e098bc96SEvan Quan 
3699373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3700373720f7SNirmoy Das 
370127ebf21fSLijo Lazar /*
370227ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
370327ebf21fSLijo Lazar  *
370427ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
370527ebf21fSLijo Lazar  */
370627ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
370727ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
370827ebf21fSLijo Lazar {
370927ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
371027ebf21fSLijo Lazar 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
371127ebf21fSLijo Lazar 	void *pp_handle = adev->powerplay.pp_handle;
371227ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
371327ebf21fSLijo Lazar 	void *smu_prv_buf;
371427ebf21fSLijo Lazar 
371527ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
371627ebf21fSLijo Lazar 		return -EPERM;
371727ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
371827ebf21fSLijo Lazar 		return -EPERM;
371927ebf21fSLijo Lazar 
372027ebf21fSLijo Lazar 	if (pp_funcs && pp_funcs->get_smu_prv_buf_details)
372127ebf21fSLijo Lazar 		pp_funcs->get_smu_prv_buf_details(pp_handle, &smu_prv_buf,
372227ebf21fSLijo Lazar 						  &smu_prv_buf_size);
372327ebf21fSLijo Lazar 	else
372427ebf21fSLijo Lazar 		return -ENOSYS;
372527ebf21fSLijo Lazar 
372627ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
372727ebf21fSLijo Lazar 		return -EINVAL;
372827ebf21fSLijo Lazar 
372927ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
373027ebf21fSLijo Lazar 				       smu_prv_buf_size);
373127ebf21fSLijo Lazar }
373227ebf21fSLijo Lazar 
373327ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
373427ebf21fSLijo Lazar 	.owner = THIS_MODULE,
373527ebf21fSLijo Lazar 	.open = simple_open,
373627ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
373727ebf21fSLijo Lazar 	.llseek = default_llseek,
373827ebf21fSLijo Lazar };
373927ebf21fSLijo Lazar 
3740e098bc96SEvan Quan #endif
3741e098bc96SEvan Quan 
3742373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3743e098bc96SEvan Quan {
3744e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3745373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
3746373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
3747373720f7SNirmoy Das 
3748373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3749373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
3750373720f7SNirmoy Das 
375127ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
375227ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
375327ebf21fSLijo Lazar 					 adev,
375427ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
375527ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
3756e098bc96SEvan Quan #endif
3757e098bc96SEvan Quan }
3758