xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision feae1bd80ec69a3a0011ba1fb88994785f705e3e)
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 
38e098bc96SEvan Quan static const struct cg_flag_name clocks[] = {
39adf16996SJinzhou.Su 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
40e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
41e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
42e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
43e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
44e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
45e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
46e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
47e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
48e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
49e098bc96SEvan Quan 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
50e098bc96SEvan Quan 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
51e098bc96SEvan Quan 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
52e098bc96SEvan Quan 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
53e098bc96SEvan Quan 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
54e098bc96SEvan Quan 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
55e098bc96SEvan Quan 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
56e098bc96SEvan Quan 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
57e098bc96SEvan Quan 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
58e098bc96SEvan Quan 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
59e098bc96SEvan Quan 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
60e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
61e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
62e098bc96SEvan Quan 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
63e098bc96SEvan Quan 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
6471037bfcSKevin Wang 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
6571037bfcSKevin Wang 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
6671037bfcSKevin Wang 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
6771037bfcSKevin Wang 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
6871037bfcSKevin Wang 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
69d6b9a91fSEvan Quan 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
70915b5ce7SEvan Quan 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
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 
853867e370SDarren Powell const char * const amdgpu_pp_profile_name[] = {
863867e370SDarren Powell 	"BOOTUP_DEFAULT",
873867e370SDarren Powell 	"3D_FULL_SCREEN",
883867e370SDarren Powell 	"POWER_SAVING",
893867e370SDarren Powell 	"VIDEO",
903867e370SDarren Powell 	"VR",
913867e370SDarren Powell 	"COMPUTE",
92334682aeSKenneth Feng 	"CUSTOM",
93334682aeSKenneth Feng 	"WINDOW_3D",
9431865e96SPerry Yuan 	"CAPPED",
9531865e96SPerry Yuan 	"UNCAPPED",
963867e370SDarren Powell };
973867e370SDarren Powell 
98e098bc96SEvan Quan /**
99e098bc96SEvan Quan  * DOC: power_dpm_state
100e098bc96SEvan Quan  *
101e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
102e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
103e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
104e098bc96SEvan Quan  * It accepts the following arguments:
105e098bc96SEvan Quan  *
106e098bc96SEvan Quan  * - battery
107e098bc96SEvan Quan  *
108e098bc96SEvan Quan  * - balanced
109e098bc96SEvan Quan  *
110e098bc96SEvan Quan  * - performance
111e098bc96SEvan Quan  *
112e098bc96SEvan Quan  * battery
113e098bc96SEvan Quan  *
114e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
115e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
116e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
117e098bc96SEvan Quan  *
118e098bc96SEvan Quan  * balanced
119e098bc96SEvan Quan  *
120e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
121e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
122e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
123e098bc96SEvan Quan  *
124e098bc96SEvan Quan  * performance
125e098bc96SEvan Quan  *
126e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
127e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
128e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
129e098bc96SEvan Quan  *
130e098bc96SEvan Quan  */
131e098bc96SEvan Quan 
132e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
133e098bc96SEvan Quan 					  struct device_attribute *attr,
134e098bc96SEvan Quan 					  char *buf)
135e098bc96SEvan Quan {
136e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1371348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
138e098bc96SEvan Quan 	enum amd_pm_state_type pm;
139e098bc96SEvan Quan 	int ret;
140e098bc96SEvan Quan 
14153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
142e098bc96SEvan Quan 		return -EPERM;
143d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
144d2ae842dSAlex Deucher 		return -EPERM;
145e098bc96SEvan Quan 
146e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
147e098bc96SEvan Quan 	if (ret < 0) {
148e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
149e098bc96SEvan Quan 		return ret;
150e098bc96SEvan Quan 	}
151e098bc96SEvan Quan 
15279c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
153e098bc96SEvan Quan 
154e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
155e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
156e098bc96SEvan Quan 
157a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
158e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
159e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
160e098bc96SEvan Quan }
161e098bc96SEvan Quan 
162e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
163e098bc96SEvan Quan 					  struct device_attribute *attr,
164e098bc96SEvan Quan 					  const char *buf,
165e098bc96SEvan Quan 					  size_t count)
166e098bc96SEvan Quan {
167e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1681348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
169e098bc96SEvan Quan 	enum amd_pm_state_type  state;
170e098bc96SEvan Quan 	int ret;
171e098bc96SEvan Quan 
17253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
173e098bc96SEvan Quan 		return -EPERM;
174d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
175d2ae842dSAlex Deucher 		return -EPERM;
176e098bc96SEvan Quan 
177e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
178e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
179e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
180e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
181e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
182e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
183e098bc96SEvan Quan 	else
184e098bc96SEvan Quan 		return -EINVAL;
185e098bc96SEvan Quan 
186e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
187e098bc96SEvan Quan 	if (ret < 0) {
188e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
189e098bc96SEvan Quan 		return ret;
190e098bc96SEvan Quan 	}
191e098bc96SEvan Quan 
19279c65f3fSEvan Quan 	amdgpu_dpm_set_power_state(adev, state);
193e098bc96SEvan Quan 
194e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
195e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
196e098bc96SEvan Quan 
197e098bc96SEvan Quan 	return count;
198e098bc96SEvan Quan }
199e098bc96SEvan Quan 
200e098bc96SEvan Quan 
201e098bc96SEvan Quan /**
202e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
203e098bc96SEvan Quan  *
204e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
205e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
206e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
207e098bc96SEvan Quan  *
208e098bc96SEvan Quan  * - auto
209e098bc96SEvan Quan  *
210e098bc96SEvan Quan  * - low
211e098bc96SEvan Quan  *
212e098bc96SEvan Quan  * - high
213e098bc96SEvan Quan  *
214e098bc96SEvan Quan  * - manual
215e098bc96SEvan Quan  *
216e098bc96SEvan Quan  * - profile_standard
217e098bc96SEvan Quan  *
218e098bc96SEvan Quan  * - profile_min_sclk
219e098bc96SEvan Quan  *
220e098bc96SEvan Quan  * - profile_min_mclk
221e098bc96SEvan Quan  *
222e098bc96SEvan Quan  * - profile_peak
223e098bc96SEvan Quan  *
224e098bc96SEvan Quan  * auto
225e098bc96SEvan Quan  *
226e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
227e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
228e098bc96SEvan Quan  *
229e098bc96SEvan Quan  * low
230e098bc96SEvan Quan  *
231e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
232e098bc96SEvan Quan  *
233e098bc96SEvan Quan  * high
234e098bc96SEvan Quan  *
235e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
236e098bc96SEvan Quan  *
237e098bc96SEvan Quan  * manual
238e098bc96SEvan Quan  *
239e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
240e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
241e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
242e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
243e098bc96SEvan Quan  *
244e098bc96SEvan Quan  * profile_standard
245e098bc96SEvan Quan  * profile_min_sclk
246e098bc96SEvan Quan  * profile_min_mclk
247e098bc96SEvan Quan  * profile_peak
248e098bc96SEvan Quan  *
249e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
250e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
251e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
252e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
253e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
254e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
255e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
256e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
257e098bc96SEvan Quan  *
258e098bc96SEvan Quan  */
259e098bc96SEvan Quan 
260e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
261e098bc96SEvan Quan 							    struct device_attribute *attr,
262e098bc96SEvan Quan 							    char *buf)
263e098bc96SEvan Quan {
264e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
266e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
267e098bc96SEvan Quan 	int ret;
268e098bc96SEvan Quan 
26953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
270e098bc96SEvan Quan 		return -EPERM;
271d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
272d2ae842dSAlex Deucher 		return -EPERM;
273e098bc96SEvan Quan 
274e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
275e098bc96SEvan Quan 	if (ret < 0) {
276e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
277e098bc96SEvan Quan 		return ret;
278e098bc96SEvan Quan 	}
279e098bc96SEvan Quan 
280e098bc96SEvan Quan 	level = amdgpu_dpm_get_performance_level(adev);
281e098bc96SEvan Quan 
282e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
283e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
284e098bc96SEvan Quan 
285a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
286e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
287e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
288e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
289e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
290e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
291e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
292e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
293e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
2946be64246SLijo Lazar 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
295e098bc96SEvan Quan 			  "unknown");
296e098bc96SEvan Quan }
297e098bc96SEvan Quan 
298e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
299e098bc96SEvan Quan 							    struct device_attribute *attr,
300e098bc96SEvan Quan 							    const char *buf,
301e098bc96SEvan Quan 							    size_t count)
302e098bc96SEvan Quan {
303e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3041348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
305e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
306e098bc96SEvan Quan 	int ret = 0;
307e098bc96SEvan Quan 
30853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
309e098bc96SEvan Quan 		return -EPERM;
310d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
311d2ae842dSAlex Deucher 		return -EPERM;
312e098bc96SEvan Quan 
313e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
314e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
315e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
316e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
317e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
318e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
319e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
320e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
321e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
322e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
323e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
324e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
325e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
326e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
327e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
328e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
329e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
330e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
3316be64246SLijo Lazar 	} else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
3326be64246SLijo Lazar 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
333e098bc96SEvan Quan 	}  else {
334e098bc96SEvan Quan 		return -EINVAL;
335e098bc96SEvan Quan 	}
336e098bc96SEvan Quan 
337e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
338e098bc96SEvan Quan 	if (ret < 0) {
339e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
340e098bc96SEvan Quan 		return ret;
341e098bc96SEvan Quan 	}
342e098bc96SEvan Quan 
3438cda7a4fSAlex Deucher 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
34479c65f3fSEvan Quan 	if (amdgpu_dpm_force_performance_level(adev, level)) {
345e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
346e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
3478cda7a4fSAlex Deucher 		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
348e098bc96SEvan Quan 		return -EINVAL;
349e098bc96SEvan Quan 	}
3508cda7a4fSAlex Deucher 	/* override whatever a user ctx may have set */
3518cda7a4fSAlex Deucher 	adev->pm.stable_pstate_ctx = NULL;
3528cda7a4fSAlex Deucher 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
35379c65f3fSEvan Quan 
354e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
355e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
356e098bc96SEvan Quan 
357e098bc96SEvan Quan 	return count;
358e098bc96SEvan Quan }
359e098bc96SEvan Quan 
360e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
361e098bc96SEvan Quan 		struct device_attribute *attr,
362e098bc96SEvan Quan 		char *buf)
363e098bc96SEvan Quan {
364e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
366e098bc96SEvan Quan 	struct pp_states_info data;
36709b6744cSDarren Powell 	uint32_t i;
36809b6744cSDarren Powell 	int buf_len, ret;
369e098bc96SEvan Quan 
37053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
371e098bc96SEvan Quan 		return -EPERM;
372d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
373d2ae842dSAlex Deucher 		return -EPERM;
374e098bc96SEvan Quan 
375e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
376e098bc96SEvan Quan 	if (ret < 0) {
377e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
378e098bc96SEvan Quan 		return ret;
379e098bc96SEvan Quan 	}
380e098bc96SEvan Quan 
38179c65f3fSEvan Quan 	if (amdgpu_dpm_get_pp_num_states(adev, &data))
382e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
383e098bc96SEvan Quan 
384e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
385e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
386e098bc96SEvan Quan 
38709b6744cSDarren Powell 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
388e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
38909b6744cSDarren Powell 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
390e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
391e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
392e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
393e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
394e098bc96SEvan Quan 
395e098bc96SEvan Quan 	return buf_len;
396e098bc96SEvan Quan }
397e098bc96SEvan Quan 
398e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
399e098bc96SEvan Quan 		struct device_attribute *attr,
400e098bc96SEvan Quan 		char *buf)
401e098bc96SEvan Quan {
402e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4031348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
4042b24c199STom Rix 	struct pp_states_info data = {0};
405e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
406e098bc96SEvan Quan 	int i = 0, ret = 0;
407e098bc96SEvan Quan 
40853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
409e098bc96SEvan Quan 		return -EPERM;
410d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
411d2ae842dSAlex Deucher 		return -EPERM;
412e098bc96SEvan Quan 
413e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
414e098bc96SEvan Quan 	if (ret < 0) {
415e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
416e098bc96SEvan Quan 		return ret;
417e098bc96SEvan Quan 	}
418e098bc96SEvan Quan 
41979c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
42079c65f3fSEvan Quan 
42179c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
422e098bc96SEvan Quan 
423e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
424e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
425e098bc96SEvan Quan 
42679c65f3fSEvan Quan 	if (ret)
42779c65f3fSEvan Quan 		return ret;
42879c65f3fSEvan Quan 
429e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
430e098bc96SEvan Quan 		if (pm == data.states[i])
431e098bc96SEvan Quan 			break;
432e098bc96SEvan Quan 	}
433e098bc96SEvan Quan 
434e098bc96SEvan Quan 	if (i == data.nums)
435e098bc96SEvan Quan 		i = -EINVAL;
436e098bc96SEvan Quan 
437a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", i);
438e098bc96SEvan Quan }
439e098bc96SEvan Quan 
440e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
441e098bc96SEvan Quan 		struct device_attribute *attr,
442e098bc96SEvan Quan 		char *buf)
443e098bc96SEvan Quan {
444e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4451348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
446e098bc96SEvan Quan 
44753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
448e098bc96SEvan Quan 		return -EPERM;
449d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
450d2ae842dSAlex Deucher 		return -EPERM;
451e098bc96SEvan Quan 
452d698a2c4SEvan Quan 	if (adev->pm.pp_force_state_enabled)
453e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
454e098bc96SEvan Quan 	else
455a9ca9bb3STian Tao 		return sysfs_emit(buf, "\n");
456e098bc96SEvan Quan }
457e098bc96SEvan Quan 
458e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
459e098bc96SEvan Quan 		struct device_attribute *attr,
460e098bc96SEvan Quan 		const char *buf,
461e098bc96SEvan Quan 		size_t count)
462e098bc96SEvan Quan {
463e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4641348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
465e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
46679c65f3fSEvan Quan 	struct pp_states_info data;
467e098bc96SEvan Quan 	unsigned long idx;
468e098bc96SEvan Quan 	int ret;
469e098bc96SEvan Quan 
47053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
471e098bc96SEvan Quan 		return -EPERM;
472d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
473d2ae842dSAlex Deucher 		return -EPERM;
474e098bc96SEvan Quan 
475d698a2c4SEvan Quan 	adev->pm.pp_force_state_enabled = false;
47679c65f3fSEvan Quan 
477e098bc96SEvan Quan 	if (strlen(buf) == 1)
47879c65f3fSEvan Quan 		return count;
479e098bc96SEvan Quan 
480e098bc96SEvan Quan 	ret = kstrtoul(buf, 0, &idx);
481e098bc96SEvan Quan 	if (ret || idx >= ARRAY_SIZE(data.states))
482e098bc96SEvan Quan 		return -EINVAL;
483e098bc96SEvan Quan 
484e098bc96SEvan Quan 	idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
485e098bc96SEvan Quan 
486e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
487e098bc96SEvan Quan 	if (ret < 0) {
488e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
489e098bc96SEvan Quan 		return ret;
490e098bc96SEvan Quan 	}
491e098bc96SEvan Quan 
49279c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
49379c65f3fSEvan Quan 	if (ret)
49479c65f3fSEvan Quan 		goto err_out;
49579c65f3fSEvan Quan 
49679c65f3fSEvan Quan 	state = data.states[idx];
49779c65f3fSEvan Quan 
498e098bc96SEvan Quan 	/* only set user selected power states */
499e098bc96SEvan Quan 	if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
500e098bc96SEvan Quan 	    state != POWER_STATE_TYPE_DEFAULT) {
50179c65f3fSEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
502e098bc96SEvan Quan 				AMD_PP_TASK_ENABLE_USER_STATE, &state);
50379c65f3fSEvan Quan 		if (ret)
50479c65f3fSEvan Quan 			goto err_out;
50579c65f3fSEvan Quan 
506d698a2c4SEvan Quan 		adev->pm.pp_force_state_enabled = true;
507e098bc96SEvan Quan 	}
50879c65f3fSEvan Quan 
509e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
510e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
511e098bc96SEvan Quan 
512e098bc96SEvan Quan 	return count;
51379c65f3fSEvan Quan 
51479c65f3fSEvan Quan err_out:
51579c65f3fSEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
51679c65f3fSEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
51779c65f3fSEvan Quan 	return ret;
518e098bc96SEvan Quan }
519e098bc96SEvan Quan 
520e098bc96SEvan Quan /**
521e098bc96SEvan Quan  * DOC: pp_table
522e098bc96SEvan Quan  *
523e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
524e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
525e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
526e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
527e098bc96SEvan Quan  * powerplay using that new table.
528e098bc96SEvan Quan  *
529e098bc96SEvan Quan  */
530e098bc96SEvan Quan 
531e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
532e098bc96SEvan Quan 		struct device_attribute *attr,
533e098bc96SEvan Quan 		char *buf)
534e098bc96SEvan Quan {
535e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5361348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
537e098bc96SEvan Quan 	char *table = NULL;
538e098bc96SEvan Quan 	int size, ret;
539e098bc96SEvan Quan 
54053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
541e098bc96SEvan Quan 		return -EPERM;
542d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
543d2ae842dSAlex Deucher 		return -EPERM;
544e098bc96SEvan Quan 
545e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
546e098bc96SEvan Quan 	if (ret < 0) {
547e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
548e098bc96SEvan Quan 		return ret;
549e098bc96SEvan Quan 	}
550e098bc96SEvan Quan 
551e098bc96SEvan Quan 	size = amdgpu_dpm_get_pp_table(adev, &table);
55279c65f3fSEvan Quan 
553e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
554e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
55579c65f3fSEvan Quan 
55679c65f3fSEvan Quan 	if (size <= 0)
557e098bc96SEvan Quan 		return size;
558e098bc96SEvan Quan 
559e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
560e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
561e098bc96SEvan Quan 
562e098bc96SEvan Quan 	memcpy(buf, table, size);
563e098bc96SEvan Quan 
564e098bc96SEvan Quan 	return size;
565e098bc96SEvan Quan }
566e098bc96SEvan Quan 
567e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
568e098bc96SEvan Quan 		struct device_attribute *attr,
569e098bc96SEvan Quan 		const char *buf,
570e098bc96SEvan Quan 		size_t count)
571e098bc96SEvan Quan {
572e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5731348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
574e098bc96SEvan Quan 	int ret = 0;
575e098bc96SEvan Quan 
57653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
577e098bc96SEvan Quan 		return -EPERM;
578d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
579d2ae842dSAlex Deucher 		return -EPERM;
580e098bc96SEvan Quan 
581e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
582e098bc96SEvan Quan 	if (ret < 0) {
583e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
584e098bc96SEvan Quan 		return ret;
585e098bc96SEvan Quan 	}
586e098bc96SEvan Quan 
5878f4828d0SDarren Powell 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
588e098bc96SEvan Quan 
589e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
590e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
591e098bc96SEvan Quan 
59279c65f3fSEvan Quan 	if (ret)
59379c65f3fSEvan Quan 		return ret;
59479c65f3fSEvan Quan 
595e098bc96SEvan Quan 	return count;
596e098bc96SEvan Quan }
597e098bc96SEvan Quan 
598e098bc96SEvan Quan /**
599e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
600e098bc96SEvan Quan  *
601e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
602e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
603e098bc96SEvan Quan  * this.
604e098bc96SEvan Quan  *
605e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
606e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
607e098bc96SEvan Quan  * following formula:
608e098bc96SEvan Quan  *
609e098bc96SEvan Quan  * Clock conversion (Mhz):
610e098bc96SEvan Quan  *
611e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
612e098bc96SEvan Quan  *
613e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
614e098bc96SEvan Quan  *
615e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
616e098bc96SEvan Quan  *
617e098bc96SEvan Quan  * DRAM data rate (MT/s):
618e098bc96SEvan Quan  *
619e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
620e098bc96SEvan Quan  *
621e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
622e098bc96SEvan Quan  *
623e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
624e098bc96SEvan Quan  *
625e098bc96SEvan Quan  * Bandwidth (MB/s):
626e098bc96SEvan Quan  *
627e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
628e098bc96SEvan Quan  *
629e098bc96SEvan Quan  * Some examples:
630e098bc96SEvan Quan  *
631e098bc96SEvan Quan  * G5 on RX460:
632e098bc96SEvan Quan  *
633e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
634e098bc96SEvan Quan  *
635e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
636e098bc96SEvan Quan  *
637e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
638e098bc96SEvan Quan  *
639e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
640e098bc96SEvan Quan  *
641e098bc96SEvan Quan  * G6 on RX5700:
642e098bc96SEvan Quan  *
643e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
644e098bc96SEvan Quan  *
645e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
646e098bc96SEvan Quan  *
647e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
648e098bc96SEvan Quan  *
649e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
650e098bc96SEvan Quan  *
651e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
652e098bc96SEvan Quan  *
653e098bc96SEvan Quan  * Reading the file will display:
654e098bc96SEvan Quan  *
655e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
656e098bc96SEvan Quan  *
657e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
658e098bc96SEvan Quan  *
659e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
660e098bc96SEvan Quan  *
661e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
662e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
663e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
664e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
665e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
666e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
667e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
668e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
669e098bc96SEvan Quan  *
670e098bc96SEvan Quan  *
671e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
672e098bc96SEvan Quan  *
673e098bc96SEvan Quan  * Reading the file will display:
674e098bc96SEvan Quan  *
675e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
676e098bc96SEvan Quan  *
67737a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
67837a58f69SEvan Quan  *   clock labeled OD_MCLK
679e098bc96SEvan Quan  *
680e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
681e098bc96SEvan Quan  *   They can be used to calibrate the sclk voltage curve.
682e098bc96SEvan Quan  *
683a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
684a2b6df4fSEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder and Dimgrey
685a2b6df4fSEvan Quan  *   Cavefish. For these ASICs, the target voltage calculation can be
686a2b6df4fSEvan Quan  *   illustrated by "voltage = voltage calculated from v/f curve +
687a2b6df4fSEvan Quan  *   overdrive vddgfx offset"
688a2b6df4fSEvan Quan  *
689e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage curve points
690e098bc96SEvan Quan  *   labeled OD_RANGE
691e098bc96SEvan Quan  *
6920487bbb4SAlex Deucher  * < For APUs >
6930487bbb4SAlex Deucher  *
6940487bbb4SAlex Deucher  * Reading the file will display:
6950487bbb4SAlex Deucher  *
6960487bbb4SAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
6970487bbb4SAlex Deucher  *
6980487bbb4SAlex Deucher  * - a list of valid ranges for sclk labeled OD_RANGE
6990487bbb4SAlex Deucher  *
7003dc8077fSAlex Deucher  * < For VanGogh >
7013dc8077fSAlex Deucher  *
7023dc8077fSAlex Deucher  * Reading the file will display:
7033dc8077fSAlex Deucher  *
7043dc8077fSAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7053dc8077fSAlex Deucher  * - minimum and maximum core clocks labeled OD_CCLK
7063dc8077fSAlex Deucher  *
7073dc8077fSAlex Deucher  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
7083dc8077fSAlex Deucher  *
709e098bc96SEvan Quan  * To manually adjust these settings:
710e098bc96SEvan Quan  *
711e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
712e098bc96SEvan Quan  *
713e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
714e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
715e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
716e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
7173dc8077fSAlex Deucher  *   "m 1 800" will update maximum mclk to be 800Mhz. For core
7183dc8077fSAlex Deucher  *   clocks on VanGogh, the string contains "p core index clock".
7193dc8077fSAlex Deucher  *   E.g., "p 2 0 800" would set the minimum core clock on core
7203dc8077fSAlex Deucher  *   2 to 800Mhz.
721e098bc96SEvan Quan  *
722e098bc96SEvan Quan  *   For sclk voltage curve, enter the new values by writing a
723e098bc96SEvan Quan  *   string that contains "vc point clock voltage" to the file. The
724e098bc96SEvan Quan  *   points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
725e098bc96SEvan Quan  *   update point1 with clock set as 300Mhz and voltage as
726e098bc96SEvan Quan  *   600mV. "vc 2 1000 1000" will update point3 with clock set
727e098bc96SEvan Quan  *   as 1000Mhz and voltage 1000mV.
728e098bc96SEvan Quan  *
729a2b6df4fSEvan Quan  *   To update the voltage offset applied for gfxclk/voltage calculation,
730a2b6df4fSEvan Quan  *   enter the new value by writing a string that contains "vo offset".
731a2b6df4fSEvan Quan  *   This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
732a2b6df4fSEvan Quan  *   And the offset can be a positive or negative value.
733a2b6df4fSEvan Quan  *
734e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
735e098bc96SEvan Quan  *   to the file to commit your changes
736e098bc96SEvan Quan  *
737e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
738e098bc96SEvan Quan  *   to the file to reset them
739e098bc96SEvan Quan  *
740e098bc96SEvan Quan  */
741e098bc96SEvan Quan 
742e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
743e098bc96SEvan Quan 		struct device_attribute *attr,
744e098bc96SEvan Quan 		const char *buf,
745e098bc96SEvan Quan 		size_t count)
746e098bc96SEvan Quan {
747e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7481348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
749e098bc96SEvan Quan 	int ret;
750e098bc96SEvan Quan 	uint32_t parameter_size = 0;
751e098bc96SEvan Quan 	long parameter[64];
752e098bc96SEvan Quan 	char buf_cpy[128];
753e098bc96SEvan Quan 	char *tmp_str;
754e098bc96SEvan Quan 	char *sub_str;
755e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
756e098bc96SEvan Quan 	uint32_t type;
757e098bc96SEvan Quan 
75853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
759e098bc96SEvan Quan 		return -EPERM;
760d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
761d2ae842dSAlex Deucher 		return -EPERM;
762e098bc96SEvan Quan 
763e098bc96SEvan Quan 	if (count > 127)
764e098bc96SEvan Quan 		return -EINVAL;
765e098bc96SEvan Quan 
766e098bc96SEvan Quan 	if (*buf == 's')
767e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
7680d90d0ddSHuang Rui 	else if (*buf == 'p')
7690d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
770e098bc96SEvan Quan 	else if (*buf == 'm')
771e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
772e098bc96SEvan Quan 	else if(*buf == 'r')
773e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
774e098bc96SEvan Quan 	else if (*buf == 'c')
775e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
776e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
777e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
778a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
779a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
780e098bc96SEvan Quan 	else
781e098bc96SEvan Quan 		return -EINVAL;
782e098bc96SEvan Quan 
783e098bc96SEvan Quan 	memcpy(buf_cpy, buf, count+1);
784e098bc96SEvan Quan 
785e098bc96SEvan Quan 	tmp_str = buf_cpy;
786e098bc96SEvan Quan 
787a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
788a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
789e098bc96SEvan Quan 		tmp_str++;
790e098bc96SEvan Quan 	while (isspace(*++tmp_str));
791e098bc96SEvan Quan 
792ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
793aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
794aec1d870SMatt Coffin 			continue;
795e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
796e098bc96SEvan Quan 		if (ret)
797e098bc96SEvan Quan 			return -EINVAL;
798e098bc96SEvan Quan 		parameter_size++;
799e098bc96SEvan Quan 
800e098bc96SEvan Quan 		while (isspace(*tmp_str))
801e098bc96SEvan Quan 			tmp_str++;
802e098bc96SEvan Quan 	}
803e098bc96SEvan Quan 
804e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
805e098bc96SEvan Quan 	if (ret < 0) {
806e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
807e098bc96SEvan Quan 		return ret;
808e098bc96SEvan Quan 	}
809e098bc96SEvan Quan 
81079c65f3fSEvan Quan 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
81179c65f3fSEvan Quan 					      type,
81212a6727dSXiaojian Du 					      parameter,
81379c65f3fSEvan Quan 					      parameter_size))
81479c65f3fSEvan Quan 		goto err_out;
81512a6727dSXiaojian Du 
81679c65f3fSEvan Quan 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
81779c65f3fSEvan Quan 					  parameter, parameter_size))
81879c65f3fSEvan Quan 		goto err_out;
819e098bc96SEvan Quan 
820e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
82179c65f3fSEvan Quan 		if (amdgpu_dpm_dispatch_task(adev,
822e098bc96SEvan Quan 					     AMD_PP_TASK_READJUST_POWER_STATE,
82379c65f3fSEvan Quan 					     NULL))
82479c65f3fSEvan Quan 			goto err_out;
82579c65f3fSEvan Quan 	}
82679c65f3fSEvan Quan 
827e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
828e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
82979c65f3fSEvan Quan 
830e098bc96SEvan Quan 	return count;
83179c65f3fSEvan Quan 
83279c65f3fSEvan Quan err_out:
833e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
834e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
835e098bc96SEvan Quan 	return -EINVAL;
836e098bc96SEvan Quan }
837e098bc96SEvan Quan 
838e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
839e098bc96SEvan Quan 		struct device_attribute *attr,
840e098bc96SEvan Quan 		char *buf)
841e098bc96SEvan Quan {
842e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8431348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
844c8cb19c7SDarren Powell 	int size = 0;
845e098bc96SEvan Quan 	int ret;
846c8cb19c7SDarren Powell 	enum pp_clock_type od_clocks[6] = {
847c8cb19c7SDarren Powell 		OD_SCLK,
848c8cb19c7SDarren Powell 		OD_MCLK,
849c8cb19c7SDarren Powell 		OD_VDDC_CURVE,
850c8cb19c7SDarren Powell 		OD_RANGE,
851c8cb19c7SDarren Powell 		OD_VDDGFX_OFFSET,
852c8cb19c7SDarren Powell 		OD_CCLK,
853c8cb19c7SDarren Powell 	};
854c8cb19c7SDarren Powell 	uint clk_index;
855e098bc96SEvan Quan 
85653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
857e098bc96SEvan Quan 		return -EPERM;
858d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
859d2ae842dSAlex Deucher 		return -EPERM;
860e098bc96SEvan Quan 
861e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
862e098bc96SEvan Quan 	if (ret < 0) {
863e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
864e098bc96SEvan Quan 		return ret;
865e098bc96SEvan Quan 	}
866e098bc96SEvan Quan 
867c8cb19c7SDarren Powell 	for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
868c8cb19c7SDarren Powell 		ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
869c8cb19c7SDarren Powell 		if (ret)
870c8cb19c7SDarren Powell 			break;
871c8cb19c7SDarren Powell 	}
872c8cb19c7SDarren Powell 	if (ret == -ENOENT) {
873e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
87479c65f3fSEvan Quan 		if (size > 0) {
875e098bc96SEvan Quan 			size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
876e098bc96SEvan Quan 			size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
8778f4828d0SDarren Powell 			size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
878e098bc96SEvan Quan 			size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
8798f4828d0SDarren Powell 			size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
880e098bc96SEvan Quan 		}
881c8cb19c7SDarren Powell 	}
882c8cb19c7SDarren Powell 
883c8cb19c7SDarren Powell 	if (size == 0)
884c8cb19c7SDarren Powell 		size = sysfs_emit(buf, "\n");
885c8cb19c7SDarren Powell 
886e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
887e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
888e098bc96SEvan Quan 
889e098bc96SEvan Quan 	return size;
890e098bc96SEvan Quan }
891e098bc96SEvan Quan 
892e098bc96SEvan Quan /**
893e098bc96SEvan Quan  * DOC: pp_features
894e098bc96SEvan Quan  *
895e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
896e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
897e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
898e098bc96SEvan Quan  *
899e098bc96SEvan Quan  * Reading back the file will show you the followings:
900e098bc96SEvan Quan  * - Current ppfeature masks
901e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
902e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
903e098bc96SEvan Quan  *
904e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
905e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
906e098bc96SEvan Quan  * new ppfeature masks.
907e098bc96SEvan Quan  */
908e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
909e098bc96SEvan Quan 				      struct device_attribute *attr,
910e098bc96SEvan Quan 				      const char *buf,
911e098bc96SEvan Quan 				      size_t count)
912e098bc96SEvan Quan {
913e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9141348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
915e098bc96SEvan Quan 	uint64_t featuremask;
916e098bc96SEvan Quan 	int ret;
917e098bc96SEvan Quan 
91853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
919e098bc96SEvan Quan 		return -EPERM;
920d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
921d2ae842dSAlex Deucher 		return -EPERM;
922e098bc96SEvan Quan 
923e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
924e098bc96SEvan Quan 	if (ret)
925e098bc96SEvan Quan 		return -EINVAL;
926e098bc96SEvan Quan 
927e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
928e098bc96SEvan Quan 	if (ret < 0) {
929e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
930e098bc96SEvan Quan 		return ret;
931e098bc96SEvan Quan 	}
932e098bc96SEvan Quan 
933e098bc96SEvan Quan 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
93479c65f3fSEvan Quan 
935e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
936e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
93779c65f3fSEvan Quan 
93879c65f3fSEvan Quan 	if (ret)
939e098bc96SEvan Quan 		return -EINVAL;
940e098bc96SEvan Quan 
941e098bc96SEvan Quan 	return count;
942e098bc96SEvan Quan }
943e098bc96SEvan Quan 
944e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
945e098bc96SEvan Quan 				      struct device_attribute *attr,
946e098bc96SEvan Quan 				      char *buf)
947e098bc96SEvan Quan {
948e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9491348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
950e098bc96SEvan Quan 	ssize_t size;
951e098bc96SEvan Quan 	int ret;
952e098bc96SEvan Quan 
95353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
954e098bc96SEvan Quan 		return -EPERM;
955d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
956d2ae842dSAlex Deucher 		return -EPERM;
957e098bc96SEvan Quan 
958e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
959e098bc96SEvan Quan 	if (ret < 0) {
960e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
961e098bc96SEvan Quan 		return ret;
962e098bc96SEvan Quan 	}
963e098bc96SEvan Quan 
964e098bc96SEvan Quan 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
96579c65f3fSEvan Quan 	if (size <= 0)
96609b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
967e098bc96SEvan Quan 
968e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
969e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
970e098bc96SEvan Quan 
971e098bc96SEvan Quan 	return size;
972e098bc96SEvan Quan }
973e098bc96SEvan Quan 
974e098bc96SEvan Quan /**
975e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
976e098bc96SEvan Quan  *
977e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
978e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
979e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
980e098bc96SEvan Quan  * this.
981e098bc96SEvan Quan  *
982e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
983e098bc96SEvan Quan  * Vega10 and later ASICs.
984e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
985e098bc96SEvan Quan  *
986e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
987e098bc96SEvan Quan  * the power state and the clock information for those levels.
988e098bc96SEvan Quan  *
989e098bc96SEvan Quan  * To manually adjust these states, first select manual using
990e098bc96SEvan Quan  * power_dpm_force_performance_level.
991e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
992e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
993e098bc96SEvan Quan  * E.g.,
994e098bc96SEvan Quan  *
995e098bc96SEvan Quan  * .. code-block:: bash
996e098bc96SEvan Quan  *
997e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
998e098bc96SEvan Quan  *
999e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
1000e098bc96SEvan Quan  *
1001e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1002e098bc96SEvan Quan  */
1003e098bc96SEvan Quan 
10042ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
10052ea092e5SDarren Powell 		enum pp_clock_type type,
1006e098bc96SEvan Quan 		char *buf)
1007e098bc96SEvan Quan {
1008e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10091348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1010c8cb19c7SDarren Powell 	int size = 0;
1011c8cb19c7SDarren Powell 	int ret = 0;
1012e098bc96SEvan Quan 
101353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1014e098bc96SEvan Quan 		return -EPERM;
1015d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1016d2ae842dSAlex Deucher 		return -EPERM;
1017e098bc96SEvan Quan 
1018e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1019e098bc96SEvan Quan 	if (ret < 0) {
1020e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1021e098bc96SEvan Quan 		return ret;
1022e098bc96SEvan Quan 	}
1023e098bc96SEvan Quan 
1024c8cb19c7SDarren Powell 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1025c8cb19c7SDarren Powell 	if (ret == -ENOENT)
10262ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1027c8cb19c7SDarren Powell 
1028c8cb19c7SDarren Powell 	if (size == 0)
102909b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1030e098bc96SEvan Quan 
1031e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1032e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1033e098bc96SEvan Quan 
1034e098bc96SEvan Quan 	return size;
1035e098bc96SEvan Quan }
1036e098bc96SEvan Quan 
1037e098bc96SEvan Quan /*
1038e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1039e098bc96SEvan Quan  * per line (+1 for \n).
1040e098bc96SEvan Quan  */
1041e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1042e098bc96SEvan Quan 
1043e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1044e098bc96SEvan Quan {
1045e098bc96SEvan Quan 	int ret;
1046c915ef89SDan Carpenter 	unsigned long level;
1047e098bc96SEvan Quan 	char *sub_str = NULL;
1048e098bc96SEvan Quan 	char *tmp;
1049e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1050e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1051e098bc96SEvan Quan 	size_t bytes;
1052e098bc96SEvan Quan 
1053e098bc96SEvan Quan 	*mask = 0;
1054e098bc96SEvan Quan 
1055e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1056e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1057e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1058e098bc96SEvan Quan 	tmp = buf_cpy;
1059ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1060e098bc96SEvan Quan 		if (strlen(sub_str)) {
1061c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1062c915ef89SDan Carpenter 			if (ret || level > 31)
1063e098bc96SEvan Quan 				return -EINVAL;
1064e098bc96SEvan Quan 			*mask |= 1 << level;
1065e098bc96SEvan Quan 		} else
1066e098bc96SEvan Quan 			break;
1067e098bc96SEvan Quan 	}
1068e098bc96SEvan Quan 
1069e098bc96SEvan Quan 	return 0;
1070e098bc96SEvan Quan }
1071e098bc96SEvan Quan 
10722ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
10732ea092e5SDarren Powell 		enum pp_clock_type type,
1074e098bc96SEvan Quan 		const char *buf,
1075e098bc96SEvan Quan 		size_t count)
1076e098bc96SEvan Quan {
1077e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10781348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1079e098bc96SEvan Quan 	int ret;
1080e098bc96SEvan Quan 	uint32_t mask = 0;
1081e098bc96SEvan Quan 
108253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1083e098bc96SEvan Quan 		return -EPERM;
1084d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1085d2ae842dSAlex Deucher 		return -EPERM;
1086e098bc96SEvan Quan 
1087e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1088e098bc96SEvan Quan 	if (ret)
1089e098bc96SEvan Quan 		return ret;
1090e098bc96SEvan Quan 
1091e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1092e098bc96SEvan Quan 	if (ret < 0) {
1093e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1094e098bc96SEvan Quan 		return ret;
1095e098bc96SEvan Quan 	}
1096e098bc96SEvan Quan 
10972ea092e5SDarren Powell 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1098e098bc96SEvan Quan 
1099e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1100e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1101e098bc96SEvan Quan 
1102e098bc96SEvan Quan 	if (ret)
1103e098bc96SEvan Quan 		return -EINVAL;
1104e098bc96SEvan Quan 
1105e098bc96SEvan Quan 	return count;
1106e098bc96SEvan Quan }
1107e098bc96SEvan Quan 
11082ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
11092ea092e5SDarren Powell 		struct device_attribute *attr,
11102ea092e5SDarren Powell 		char *buf)
11112ea092e5SDarren Powell {
11122ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
11132ea092e5SDarren Powell }
11142ea092e5SDarren Powell 
11152ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
11162ea092e5SDarren Powell 		struct device_attribute *attr,
11172ea092e5SDarren Powell 		const char *buf,
11182ea092e5SDarren Powell 		size_t count)
11192ea092e5SDarren Powell {
11202ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
11212ea092e5SDarren Powell }
11222ea092e5SDarren Powell 
1123e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1124e098bc96SEvan Quan 		struct device_attribute *attr,
1125e098bc96SEvan Quan 		char *buf)
1126e098bc96SEvan Quan {
11272ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1128e098bc96SEvan Quan }
1129e098bc96SEvan Quan 
1130e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1131e098bc96SEvan Quan 		struct device_attribute *attr,
1132e098bc96SEvan Quan 		const char *buf,
1133e098bc96SEvan Quan 		size_t count)
1134e098bc96SEvan Quan {
11352ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1136e098bc96SEvan Quan }
1137e098bc96SEvan Quan 
1138e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1139e098bc96SEvan Quan 		struct device_attribute *attr,
1140e098bc96SEvan Quan 		char *buf)
1141e098bc96SEvan Quan {
11422ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1143e098bc96SEvan Quan }
1144e098bc96SEvan Quan 
1145e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1146e098bc96SEvan Quan 		struct device_attribute *attr,
1147e098bc96SEvan Quan 		const char *buf,
1148e098bc96SEvan Quan 		size_t count)
1149e098bc96SEvan Quan {
11502ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1151e098bc96SEvan Quan }
1152e098bc96SEvan Quan 
1153e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1154e098bc96SEvan Quan 		struct device_attribute *attr,
1155e098bc96SEvan Quan 		char *buf)
1156e098bc96SEvan Quan {
11572ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1158e098bc96SEvan Quan }
1159e098bc96SEvan Quan 
1160e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1161e098bc96SEvan Quan 		struct device_attribute *attr,
1162e098bc96SEvan Quan 		const char *buf,
1163e098bc96SEvan Quan 		size_t count)
1164e098bc96SEvan Quan {
11652ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1166e098bc96SEvan Quan }
1167e098bc96SEvan Quan 
11689577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
11699577b0ecSXiaojian Du 		struct device_attribute *attr,
11709577b0ecSXiaojian Du 		char *buf)
11719577b0ecSXiaojian Du {
11722ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
11739577b0ecSXiaojian Du }
11749577b0ecSXiaojian Du 
11759577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
11769577b0ecSXiaojian Du 		struct device_attribute *attr,
11779577b0ecSXiaojian Du 		const char *buf,
11789577b0ecSXiaojian Du 		size_t count)
11799577b0ecSXiaojian Du {
11802ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
11819577b0ecSXiaojian Du }
11829577b0ecSXiaojian Du 
1183d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1184d7001e72STong Liu01 		struct device_attribute *attr,
1185d7001e72STong Liu01 		char *buf)
1186d7001e72STong Liu01 {
1187d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1188d7001e72STong Liu01 }
1189d7001e72STong Liu01 
1190d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1191d7001e72STong Liu01 		struct device_attribute *attr,
1192d7001e72STong Liu01 		const char *buf,
1193d7001e72STong Liu01 		size_t count)
1194d7001e72STong Liu01 {
1195d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1196d7001e72STong Liu01 }
1197d7001e72STong Liu01 
11989577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
11999577b0ecSXiaojian Du 		struct device_attribute *attr,
12009577b0ecSXiaojian Du 		char *buf)
12019577b0ecSXiaojian Du {
12022ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
12039577b0ecSXiaojian Du }
12049577b0ecSXiaojian Du 
12059577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
12069577b0ecSXiaojian Du 		struct device_attribute *attr,
12079577b0ecSXiaojian Du 		const char *buf,
12089577b0ecSXiaojian Du 		size_t count)
12099577b0ecSXiaojian Du {
12102ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
12119577b0ecSXiaojian Du }
12129577b0ecSXiaojian Du 
1213d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1214d7001e72STong Liu01 		struct device_attribute *attr,
1215d7001e72STong Liu01 		char *buf)
1216d7001e72STong Liu01 {
1217d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1218d7001e72STong Liu01 }
1219d7001e72STong Liu01 
1220d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1221d7001e72STong Liu01 		struct device_attribute *attr,
1222d7001e72STong Liu01 		const char *buf,
1223d7001e72STong Liu01 		size_t count)
1224d7001e72STong Liu01 {
1225d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1226d7001e72STong Liu01 }
1227d7001e72STong Liu01 
1228e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1229e098bc96SEvan Quan 		struct device_attribute *attr,
1230e098bc96SEvan Quan 		char *buf)
1231e098bc96SEvan Quan {
12322ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1233e098bc96SEvan Quan }
1234e098bc96SEvan Quan 
1235e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1236e098bc96SEvan Quan 		struct device_attribute *attr,
1237e098bc96SEvan Quan 		const char *buf,
1238e098bc96SEvan Quan 		size_t count)
1239e098bc96SEvan Quan {
12402ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1241e098bc96SEvan Quan }
1242e098bc96SEvan Quan 
1243e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1244e098bc96SEvan Quan 		struct device_attribute *attr,
1245e098bc96SEvan Quan 		char *buf)
1246e098bc96SEvan Quan {
12472ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1248e098bc96SEvan Quan }
1249e098bc96SEvan Quan 
1250e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1251e098bc96SEvan Quan 		struct device_attribute *attr,
1252e098bc96SEvan Quan 		const char *buf,
1253e098bc96SEvan Quan 		size_t count)
1254e098bc96SEvan Quan {
12552ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1256e098bc96SEvan Quan }
1257e098bc96SEvan Quan 
1258e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1259e098bc96SEvan Quan 		struct device_attribute *attr,
1260e098bc96SEvan Quan 		char *buf)
1261e098bc96SEvan Quan {
1262e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12631348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1264e098bc96SEvan Quan 	uint32_t value = 0;
1265e098bc96SEvan Quan 	int ret;
1266e098bc96SEvan Quan 
126753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1268e098bc96SEvan Quan 		return -EPERM;
1269d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1270d2ae842dSAlex Deucher 		return -EPERM;
1271e098bc96SEvan Quan 
1272e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1273e098bc96SEvan Quan 	if (ret < 0) {
1274e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1275e098bc96SEvan Quan 		return ret;
1276e098bc96SEvan Quan 	}
1277e098bc96SEvan Quan 
1278e098bc96SEvan Quan 	value = amdgpu_dpm_get_sclk_od(adev);
1279e098bc96SEvan Quan 
1280e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1281e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1282e098bc96SEvan Quan 
1283a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1284e098bc96SEvan Quan }
1285e098bc96SEvan Quan 
1286e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1287e098bc96SEvan Quan 		struct device_attribute *attr,
1288e098bc96SEvan Quan 		const char *buf,
1289e098bc96SEvan Quan 		size_t count)
1290e098bc96SEvan Quan {
1291e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12921348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1293e098bc96SEvan Quan 	int ret;
1294e098bc96SEvan Quan 	long int value;
1295e098bc96SEvan Quan 
129653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1297e098bc96SEvan Quan 		return -EPERM;
1298d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1299d2ae842dSAlex Deucher 		return -EPERM;
1300e098bc96SEvan Quan 
1301e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1302e098bc96SEvan Quan 
1303e098bc96SEvan Quan 	if (ret)
1304e098bc96SEvan Quan 		return -EINVAL;
1305e098bc96SEvan Quan 
1306e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1307e098bc96SEvan Quan 	if (ret < 0) {
1308e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1309e098bc96SEvan Quan 		return ret;
1310e098bc96SEvan Quan 	}
1311e098bc96SEvan Quan 
1312e098bc96SEvan Quan 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1313e098bc96SEvan Quan 
1314e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1315e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1316e098bc96SEvan Quan 
1317e098bc96SEvan Quan 	return count;
1318e098bc96SEvan Quan }
1319e098bc96SEvan Quan 
1320e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1321e098bc96SEvan Quan 		struct device_attribute *attr,
1322e098bc96SEvan Quan 		char *buf)
1323e098bc96SEvan Quan {
1324e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13251348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1326e098bc96SEvan Quan 	uint32_t value = 0;
1327e098bc96SEvan Quan 	int ret;
1328e098bc96SEvan Quan 
132953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1330e098bc96SEvan Quan 		return -EPERM;
1331d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1332d2ae842dSAlex Deucher 		return -EPERM;
1333e098bc96SEvan Quan 
1334e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1335e098bc96SEvan Quan 	if (ret < 0) {
1336e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1337e098bc96SEvan Quan 		return ret;
1338e098bc96SEvan Quan 	}
1339e098bc96SEvan Quan 
1340e098bc96SEvan Quan 	value = amdgpu_dpm_get_mclk_od(adev);
1341e098bc96SEvan Quan 
1342e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1343e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1344e098bc96SEvan Quan 
1345a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1346e098bc96SEvan Quan }
1347e098bc96SEvan Quan 
1348e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1349e098bc96SEvan Quan 		struct device_attribute *attr,
1350e098bc96SEvan Quan 		const char *buf,
1351e098bc96SEvan Quan 		size_t count)
1352e098bc96SEvan Quan {
1353e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13541348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1355e098bc96SEvan Quan 	int ret;
1356e098bc96SEvan Quan 	long int value;
1357e098bc96SEvan Quan 
135853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1359e098bc96SEvan Quan 		return -EPERM;
1360d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1361d2ae842dSAlex Deucher 		return -EPERM;
1362e098bc96SEvan Quan 
1363e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1364e098bc96SEvan Quan 
1365e098bc96SEvan Quan 	if (ret)
1366e098bc96SEvan Quan 		return -EINVAL;
1367e098bc96SEvan Quan 
1368e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1369e098bc96SEvan Quan 	if (ret < 0) {
1370e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1371e098bc96SEvan Quan 		return ret;
1372e098bc96SEvan Quan 	}
1373e098bc96SEvan Quan 
1374e098bc96SEvan Quan 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1375e098bc96SEvan Quan 
1376e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1377e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1378e098bc96SEvan Quan 
1379e098bc96SEvan Quan 	return count;
1380e098bc96SEvan Quan }
1381e098bc96SEvan Quan 
1382e098bc96SEvan Quan /**
1383e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1384e098bc96SEvan Quan  *
1385e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1386e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1387e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1388e098bc96SEvan Quan  *
1389e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1390e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1391e098bc96SEvan Quan  *
1392e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1393e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1394e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1395e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1396e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1397e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1398e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1399e098bc96SEvan Quan  *
1400e098bc96SEvan Quan  */
1401e098bc96SEvan Quan 
1402e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1403e098bc96SEvan Quan 		struct device_attribute *attr,
1404e098bc96SEvan Quan 		char *buf)
1405e098bc96SEvan Quan {
1406e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14071348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1408e098bc96SEvan Quan 	ssize_t size;
1409e098bc96SEvan Quan 	int ret;
1410e098bc96SEvan Quan 
141153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1412e098bc96SEvan Quan 		return -EPERM;
1413d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1414d2ae842dSAlex Deucher 		return -EPERM;
1415e098bc96SEvan Quan 
1416e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1417e098bc96SEvan Quan 	if (ret < 0) {
1418e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1419e098bc96SEvan Quan 		return ret;
1420e098bc96SEvan Quan 	}
1421e098bc96SEvan Quan 
1422e098bc96SEvan Quan 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
142379c65f3fSEvan Quan 	if (size <= 0)
142409b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1425e098bc96SEvan Quan 
1426e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1427e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1428e098bc96SEvan Quan 
1429e098bc96SEvan Quan 	return size;
1430e098bc96SEvan Quan }
1431e098bc96SEvan Quan 
1432e098bc96SEvan Quan 
1433e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1434e098bc96SEvan Quan 		struct device_attribute *attr,
1435e098bc96SEvan Quan 		const char *buf,
1436e098bc96SEvan Quan 		size_t count)
1437e098bc96SEvan Quan {
1438e098bc96SEvan Quan 	int ret;
1439e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14401348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1441e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1442e098bc96SEvan Quan 	long parameter[64];
1443e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1444e098bc96SEvan Quan 	char *tmp_str;
1445e098bc96SEvan Quan 	uint32_t i = 0;
1446e098bc96SEvan Quan 	char tmp[2];
1447e098bc96SEvan Quan 	long int profile_mode = 0;
1448e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1449e098bc96SEvan Quan 
145053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1451e098bc96SEvan Quan 		return -EPERM;
1452d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1453d2ae842dSAlex Deucher 		return -EPERM;
1454e098bc96SEvan Quan 
1455e098bc96SEvan Quan 	tmp[0] = *(buf);
1456e098bc96SEvan Quan 	tmp[1] = '\0';
1457e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1458e098bc96SEvan Quan 	if (ret)
1459e098bc96SEvan Quan 		return -EINVAL;
1460e098bc96SEvan Quan 
1461e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1462e098bc96SEvan Quan 		if (count < 2 || count > 127)
1463e098bc96SEvan Quan 			return -EINVAL;
1464e098bc96SEvan Quan 		while (isspace(*++buf))
1465e098bc96SEvan Quan 			i++;
1466e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1467e098bc96SEvan Quan 		tmp_str = buf_cpy;
1468ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1469c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1470c2efbc3fSEvan Quan 				continue;
1471e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1472e098bc96SEvan Quan 			if (ret)
1473e098bc96SEvan Quan 				return -EINVAL;
1474e098bc96SEvan Quan 			parameter_size++;
1475e098bc96SEvan Quan 			while (isspace(*tmp_str))
1476e098bc96SEvan Quan 				tmp_str++;
1477e098bc96SEvan Quan 		}
1478e098bc96SEvan Quan 	}
1479e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1480e098bc96SEvan Quan 
1481e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1482e098bc96SEvan Quan 	if (ret < 0) {
1483e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1484e098bc96SEvan Quan 		return ret;
1485e098bc96SEvan Quan 	}
1486e098bc96SEvan Quan 
1487e098bc96SEvan Quan 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1488e098bc96SEvan Quan 
1489e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1490e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1491e098bc96SEvan Quan 
1492e098bc96SEvan Quan 	if (!ret)
1493e098bc96SEvan Quan 		return count;
1494e098bc96SEvan Quan 
1495e098bc96SEvan Quan 	return -EINVAL;
1496e098bc96SEvan Quan }
1497e098bc96SEvan Quan 
1498e098bc96SEvan Quan /**
1499e098bc96SEvan Quan  * DOC: gpu_busy_percent
1500e098bc96SEvan Quan  *
1501e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1502e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1503e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1504e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1505e098bc96SEvan Quan  */
1506e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1507e098bc96SEvan Quan 					   struct device_attribute *attr,
1508e098bc96SEvan Quan 					   char *buf)
1509e098bc96SEvan Quan {
1510e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15111348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1512e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1513e098bc96SEvan Quan 
151453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1515e098bc96SEvan Quan 		return -EPERM;
1516d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1517d2ae842dSAlex Deucher 		return -EPERM;
1518e098bc96SEvan Quan 
1519e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1520e098bc96SEvan Quan 	if (r < 0) {
1521e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1522e098bc96SEvan Quan 		return r;
1523e098bc96SEvan Quan 	}
1524e098bc96SEvan Quan 
1525e098bc96SEvan Quan 	/* read the IP busy sensor */
1526e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1527e098bc96SEvan Quan 				   (void *)&value, &size);
1528e098bc96SEvan Quan 
1529e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1530e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1531e098bc96SEvan Quan 
1532e098bc96SEvan Quan 	if (r)
1533e098bc96SEvan Quan 		return r;
1534e098bc96SEvan Quan 
1535a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1536e098bc96SEvan Quan }
1537e098bc96SEvan Quan 
1538e098bc96SEvan Quan /**
1539e098bc96SEvan Quan  * DOC: mem_busy_percent
1540e098bc96SEvan Quan  *
1541e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1542e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1543e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1544e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1545e098bc96SEvan Quan  */
1546e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1547e098bc96SEvan Quan 					   struct device_attribute *attr,
1548e098bc96SEvan Quan 					   char *buf)
1549e098bc96SEvan Quan {
1550e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15511348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1552e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1553e098bc96SEvan Quan 
155453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1555e098bc96SEvan Quan 		return -EPERM;
1556d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1557d2ae842dSAlex Deucher 		return -EPERM;
1558e098bc96SEvan Quan 
1559e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1560e098bc96SEvan Quan 	if (r < 0) {
1561e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1562e098bc96SEvan Quan 		return r;
1563e098bc96SEvan Quan 	}
1564e098bc96SEvan Quan 
1565e098bc96SEvan Quan 	/* read the IP busy sensor */
1566e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1567e098bc96SEvan Quan 				   (void *)&value, &size);
1568e098bc96SEvan Quan 
1569e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1570e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1571e098bc96SEvan Quan 
1572e098bc96SEvan Quan 	if (r)
1573e098bc96SEvan Quan 		return r;
1574e098bc96SEvan Quan 
1575a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1576e098bc96SEvan Quan }
1577e098bc96SEvan Quan 
1578e098bc96SEvan Quan /**
1579e098bc96SEvan Quan  * DOC: pcie_bw
1580e098bc96SEvan Quan  *
1581e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1582e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1583e098bc96SEvan Quan  * The file pcie_bw is used for this.
1584e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1585e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1586e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1587e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1588e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1589e098bc96SEvan Quan  */
1590e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1591e098bc96SEvan Quan 		struct device_attribute *attr,
1592e098bc96SEvan Quan 		char *buf)
1593e098bc96SEvan Quan {
1594e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15951348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1596e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1597e098bc96SEvan Quan 	int ret;
1598e098bc96SEvan Quan 
159953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1600e098bc96SEvan Quan 		return -EPERM;
1601d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1602d2ae842dSAlex Deucher 		return -EPERM;
1603e098bc96SEvan Quan 
1604e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1605e098bc96SEvan Quan 		return -ENODATA;
1606e098bc96SEvan Quan 
1607e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1608e098bc96SEvan Quan 		return -ENODATA;
1609e098bc96SEvan Quan 
1610e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1611e098bc96SEvan Quan 	if (ret < 0) {
1612e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1613e098bc96SEvan Quan 		return ret;
1614e098bc96SEvan Quan 	}
1615e098bc96SEvan Quan 
1616e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1617e098bc96SEvan Quan 
1618e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1619e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1620e098bc96SEvan Quan 
1621a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1622e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1623e098bc96SEvan Quan }
1624e098bc96SEvan Quan 
1625e098bc96SEvan Quan /**
1626e098bc96SEvan Quan  * DOC: unique_id
1627e098bc96SEvan Quan  *
1628e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1629e098bc96SEvan Quan  * The file unique_id is used for this.
1630e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1631e098bc96SEvan Quan  *
1632e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1633e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1634e098bc96SEvan Quan  */
1635e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1636e098bc96SEvan Quan 		struct device_attribute *attr,
1637e098bc96SEvan Quan 		char *buf)
1638e098bc96SEvan Quan {
1639e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16401348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1641e098bc96SEvan Quan 
164253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1643e098bc96SEvan Quan 		return -EPERM;
1644d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1645d2ae842dSAlex Deucher 		return -EPERM;
1646e098bc96SEvan Quan 
1647e098bc96SEvan Quan 	if (adev->unique_id)
1648a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1649e098bc96SEvan Quan 
1650e098bc96SEvan Quan 	return 0;
1651e098bc96SEvan Quan }
1652e098bc96SEvan Quan 
1653e098bc96SEvan Quan /**
1654e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1655e098bc96SEvan Quan  *
1656e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1657e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1658e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1659e098bc96SEvan Quan  * the log frequency.
1660e098bc96SEvan Quan  *
1661e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1662e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1663e098bc96SEvan Quan  *
1664e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1665e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1666e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1667e098bc96SEvan Quan  */
1668e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1669e098bc96SEvan Quan 						     struct device_attribute *attr,
1670e098bc96SEvan Quan 						     char *buf)
1671e098bc96SEvan Quan {
1672e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16731348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1674e098bc96SEvan Quan 
1675a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
16764a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1677e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1678e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1679e098bc96SEvan Quan }
1680e098bc96SEvan Quan 
1681e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1682e098bc96SEvan Quan 						     struct device_attribute *attr,
1683e098bc96SEvan Quan 						     const char *buf,
1684e098bc96SEvan Quan 						     size_t count)
1685e098bc96SEvan Quan {
1686e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16871348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1688e098bc96SEvan Quan 	long throttling_logging_interval;
1689e098bc96SEvan Quan 	unsigned long flags;
1690e098bc96SEvan Quan 	int ret = 0;
1691e098bc96SEvan Quan 
1692e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1693e098bc96SEvan Quan 	if (ret)
1694e098bc96SEvan Quan 		return ret;
1695e098bc96SEvan Quan 
1696e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1697e098bc96SEvan Quan 		return -EINVAL;
1698e098bc96SEvan Quan 
1699e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1700e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1701e098bc96SEvan Quan 		/*
1702e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1703e098bc96SEvan Quan 		 * This can effectively restart the timer.
1704e098bc96SEvan Quan 		 */
1705e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1706e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1707e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1708e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1709e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1710e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1711e098bc96SEvan Quan 
1712e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1713e098bc96SEvan Quan 	} else {
1714e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1715e098bc96SEvan Quan 	}
1716e098bc96SEvan Quan 
1717e098bc96SEvan Quan 	return count;
1718e098bc96SEvan Quan }
1719e098bc96SEvan Quan 
1720e098bc96SEvan Quan /**
1721c3ed0e72SKun Liu  * DOC: apu_thermal_cap
1722c3ed0e72SKun Liu  *
1723c3ed0e72SKun Liu  * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1724c3ed0e72SKun Liu  * limit temperature in millidegrees Celsius
1725c3ed0e72SKun Liu  *
1726c3ed0e72SKun Liu  * Reading back the file shows you core limit value
1727c3ed0e72SKun Liu  *
1728c3ed0e72SKun Liu  * Writing an integer to the file, sets a new thermal limit. The value
1729c3ed0e72SKun Liu  * should be between 0 and 100. If the value is less than 0 or greater
1730c3ed0e72SKun Liu  * than 100, then the write request will be ignored.
1731c3ed0e72SKun Liu  */
1732c3ed0e72SKun Liu static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1733c3ed0e72SKun Liu 					 struct device_attribute *attr,
1734c3ed0e72SKun Liu 					 char *buf)
1735c3ed0e72SKun Liu {
1736c3ed0e72SKun Liu 	int ret, size;
1737c3ed0e72SKun Liu 	u32 limit;
1738c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1739c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1740c3ed0e72SKun Liu 
1741c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1742c3ed0e72SKun Liu 	if (ret < 0) {
1743c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1744c3ed0e72SKun Liu 		return ret;
1745c3ed0e72SKun Liu 	}
1746c3ed0e72SKun Liu 
1747c3ed0e72SKun Liu 	ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1748c3ed0e72SKun Liu 	if (!ret)
1749c3ed0e72SKun Liu 		size = sysfs_emit(buf, "%u\n", limit);
1750c3ed0e72SKun Liu 	else
1751c3ed0e72SKun Liu 		size = sysfs_emit(buf, "failed to get thermal limit\n");
1752c3ed0e72SKun Liu 
1753c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1754c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1755c3ed0e72SKun Liu 
1756c3ed0e72SKun Liu 	return size;
1757c3ed0e72SKun Liu }
1758c3ed0e72SKun Liu 
1759c3ed0e72SKun Liu static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1760c3ed0e72SKun Liu 					 struct device_attribute *attr,
1761c3ed0e72SKun Liu 					 const char *buf,
1762c3ed0e72SKun Liu 					 size_t count)
1763c3ed0e72SKun Liu {
1764c3ed0e72SKun Liu 	int ret;
1765c3ed0e72SKun Liu 	u32 value;
1766c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1767c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1768c3ed0e72SKun Liu 
1769c3ed0e72SKun Liu 	ret = kstrtou32(buf, 10, &value);
1770c3ed0e72SKun Liu 	if (ret)
1771c3ed0e72SKun Liu 		return ret;
1772c3ed0e72SKun Liu 
17734d2c09d6SMuhammad Usama Anjum 	if (value > 100) {
1774c3ed0e72SKun Liu 		dev_err(dev, "Invalid argument !\n");
1775c3ed0e72SKun Liu 		return -EINVAL;
1776c3ed0e72SKun Liu 	}
1777c3ed0e72SKun Liu 
1778c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1779c3ed0e72SKun Liu 	if (ret < 0) {
1780c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1781c3ed0e72SKun Liu 		return ret;
1782c3ed0e72SKun Liu 	}
1783c3ed0e72SKun Liu 
1784c3ed0e72SKun Liu 	ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1785c3ed0e72SKun Liu 	if (ret) {
1786c3ed0e72SKun Liu 		dev_err(dev, "failed to update thermal limit\n");
1787c3ed0e72SKun Liu 		return ret;
1788c3ed0e72SKun Liu 	}
1789c3ed0e72SKun Liu 
1790c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1791c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1792c3ed0e72SKun Liu 
1793c3ed0e72SKun Liu 	return count;
1794c3ed0e72SKun Liu }
1795c3ed0e72SKun Liu 
1796c3ed0e72SKun Liu /**
1797e098bc96SEvan Quan  * DOC: gpu_metrics
1798e098bc96SEvan Quan  *
1799e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1800e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1801e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1802e098bc96SEvan Quan  *
1803e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1804e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1805e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1806e098bc96SEvan Quan  * at the same time.
1807e098bc96SEvan Quan  */
1808e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1809e098bc96SEvan Quan 				      struct device_attribute *attr,
1810e098bc96SEvan Quan 				      char *buf)
1811e098bc96SEvan Quan {
1812e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
18131348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1814e098bc96SEvan Quan 	void *gpu_metrics;
1815e098bc96SEvan Quan 	ssize_t size = 0;
1816e098bc96SEvan Quan 	int ret;
1817e098bc96SEvan Quan 
181853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1819e098bc96SEvan Quan 		return -EPERM;
1820d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1821d2ae842dSAlex Deucher 		return -EPERM;
1822e098bc96SEvan Quan 
1823e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1824e098bc96SEvan Quan 	if (ret < 0) {
1825e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1826e098bc96SEvan Quan 		return ret;
1827e098bc96SEvan Quan 	}
1828e098bc96SEvan Quan 
1829e098bc96SEvan Quan 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1830e098bc96SEvan Quan 	if (size <= 0)
1831e098bc96SEvan Quan 		goto out;
1832e098bc96SEvan Quan 
1833e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1834e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1835e098bc96SEvan Quan 
1836e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1837e098bc96SEvan Quan 
1838e098bc96SEvan Quan out:
1839e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1840e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1841e098bc96SEvan Quan 
1842e098bc96SEvan Quan 	return size;
1843e098bc96SEvan Quan }
1844e098bc96SEvan Quan 
1845494c1432SSathishkumar S static int amdgpu_device_read_powershift(struct amdgpu_device *adev,
1846494c1432SSathishkumar S 						uint32_t *ss_power, bool dgpu_share)
1847494c1432SSathishkumar S {
1848494c1432SSathishkumar S 	struct drm_device *ddev = adev_to_drm(adev);
1849494c1432SSathishkumar S 	uint32_t size;
1850494c1432SSathishkumar S 	int r = 0;
1851494c1432SSathishkumar S 
1852494c1432SSathishkumar S 	if (amdgpu_in_reset(adev))
1853494c1432SSathishkumar S 		return -EPERM;
1854494c1432SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
1855494c1432SSathishkumar S 		return -EPERM;
1856494c1432SSathishkumar S 
1857494c1432SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
1858494c1432SSathishkumar S 	if (r < 0) {
1859494c1432SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
1860494c1432SSathishkumar S 		return r;
1861494c1432SSathishkumar S 	}
1862494c1432SSathishkumar S 
1863494c1432SSathishkumar S 	if (dgpu_share)
1864494c1432SSathishkumar S 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1865494c1432SSathishkumar S 				   (void *)ss_power, &size);
1866494c1432SSathishkumar S 	else
1867494c1432SSathishkumar S 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1868494c1432SSathishkumar S 				   (void *)ss_power, &size);
1869494c1432SSathishkumar S 
1870494c1432SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
1871494c1432SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
1872494c1432SSathishkumar S 	return r;
1873494c1432SSathishkumar S }
1874494c1432SSathishkumar S 
1875494c1432SSathishkumar S static int amdgpu_show_powershift_percent(struct device *dev,
1876494c1432SSathishkumar S 					char *buf, bool dgpu_share)
1877494c1432SSathishkumar S {
1878494c1432SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1879494c1432SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1880494c1432SSathishkumar S 	uint32_t ss_power;
1881494c1432SSathishkumar S 	int r = 0, i;
1882494c1432SSathishkumar S 
1883494c1432SSathishkumar S 	r = amdgpu_device_read_powershift(adev, &ss_power, dgpu_share);
1884494c1432SSathishkumar S 	if (r == -EOPNOTSUPP) {
1885494c1432SSathishkumar S 		/* sensor not available on dGPU, try to read from APU */
1886494c1432SSathishkumar S 		adev = NULL;
1887494c1432SSathishkumar S 		mutex_lock(&mgpu_info.mutex);
1888494c1432SSathishkumar S 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1889494c1432SSathishkumar S 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1890494c1432SSathishkumar S 				adev = mgpu_info.gpu_ins[i].adev;
1891494c1432SSathishkumar S 				break;
1892494c1432SSathishkumar S 			}
1893494c1432SSathishkumar S 		}
1894494c1432SSathishkumar S 		mutex_unlock(&mgpu_info.mutex);
1895494c1432SSathishkumar S 		if (adev)
1896494c1432SSathishkumar S 			r = amdgpu_device_read_powershift(adev, &ss_power, dgpu_share);
1897494c1432SSathishkumar S 	}
1898494c1432SSathishkumar S 
1899494c1432SSathishkumar S 	if (!r)
1900494c1432SSathishkumar S 		r = sysfs_emit(buf, "%u%%\n", ss_power);
1901494c1432SSathishkumar S 
1902494c1432SSathishkumar S 	return r;
1903494c1432SSathishkumar S }
1904a7673a1cSSathishkumar S /**
1905a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1906a7673a1cSSathishkumar S  *
1907a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1908494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1909494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power
1910494c1432SSathishkumar S  * is shifted to APU, the percentage of boost is with respect to APU power
1911494c1432SSathishkumar S  * limit on the platform.
1912a7673a1cSSathishkumar S  */
1913a7673a1cSSathishkumar S 
1914a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1915a7673a1cSSathishkumar S 					       char *buf)
1916a7673a1cSSathishkumar S {
1917494c1432SSathishkumar S 	return amdgpu_show_powershift_percent(dev, buf, false);
1918a7673a1cSSathishkumar S }
1919a7673a1cSSathishkumar S 
1920a7673a1cSSathishkumar S /**
1921a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1922a7673a1cSSathishkumar S  *
1923494c1432SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting dGPU power
1924494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1925494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power is
1926494c1432SSathishkumar S  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1927494c1432SSathishkumar S  * limit on the platform.
1928a7673a1cSSathishkumar S  */
1929a7673a1cSSathishkumar S 
1930a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1931a7673a1cSSathishkumar S 						char *buf)
1932a7673a1cSSathishkumar S {
1933494c1432SSathishkumar S 	return amdgpu_show_powershift_percent(dev, buf, true);
1934a7673a1cSSathishkumar S }
1935a7673a1cSSathishkumar S 
193630d95a37SSathishkumar S /**
193730d95a37SSathishkumar S  * DOC: smartshift_bias
193830d95a37SSathishkumar S  *
193930d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
194030d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
194130d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
194230d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
194330d95a37SSathishkumar S  */
194430d95a37SSathishkumar S 
194530d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
194630d95a37SSathishkumar S 					  struct device_attribute *attr,
194730d95a37SSathishkumar S 					  char *buf)
194830d95a37SSathishkumar S {
194930d95a37SSathishkumar S 	int r = 0;
195030d95a37SSathishkumar S 
195130d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
195230d95a37SSathishkumar S 
195330d95a37SSathishkumar S 	return r;
195430d95a37SSathishkumar S }
195530d95a37SSathishkumar S 
195630d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
195730d95a37SSathishkumar S 					  struct device_attribute *attr,
195830d95a37SSathishkumar S 					  const char *buf, size_t count)
195930d95a37SSathishkumar S {
196030d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
196130d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
196230d95a37SSathishkumar S 	int r = 0;
196330d95a37SSathishkumar S 	int bias = 0;
196430d95a37SSathishkumar S 
196530d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
196630d95a37SSathishkumar S 		return -EPERM;
196730d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
196830d95a37SSathishkumar S 		return -EPERM;
196930d95a37SSathishkumar S 
197030d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
197130d95a37SSathishkumar S 	if (r < 0) {
197230d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
197330d95a37SSathishkumar S 		return r;
197430d95a37SSathishkumar S 	}
197530d95a37SSathishkumar S 
197630d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
197730d95a37SSathishkumar S 	if (r)
197830d95a37SSathishkumar S 		goto out;
197930d95a37SSathishkumar S 
198030d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
198130d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
198230d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
198330d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
198430d95a37SSathishkumar S 
198530d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
198630d95a37SSathishkumar S 	r = count;
198730d95a37SSathishkumar S 
1988bd4b9bb7SJulia Lawall 	/* TODO: update bias level with SMU message */
198930d95a37SSathishkumar S 
199030d95a37SSathishkumar S out:
199130d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
199230d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
199330d95a37SSathishkumar S 	return r;
199430d95a37SSathishkumar S }
199530d95a37SSathishkumar S 
199630d95a37SSathishkumar S 
1997a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1998a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
1999a7673a1cSSathishkumar S {
2000494c1432SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
2001a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
2002a7673a1cSSathishkumar S 
2003a7673a1cSSathishkumar S 	return 0;
2004a7673a1cSSathishkumar S }
2005a7673a1cSSathishkumar S 
200630d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
200730d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
200830d95a37SSathishkumar S {
200930d95a37SSathishkumar S 	uint32_t ss_power, size;
201030d95a37SSathishkumar S 
201130d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
201230d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
201330d95a37SSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
201430d95a37SSathishkumar S 		 (void *)&ss_power, &size))
201530d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
201630d95a37SSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
201730d95a37SSathishkumar S 		 (void *)&ss_power, &size))
201830d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
201930d95a37SSathishkumar S 
202030d95a37SSathishkumar S 	return 0;
202130d95a37SSathishkumar S }
202230d95a37SSathishkumar S 
2023e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2024e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20254215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20267884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20277884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20287884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20297884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2030e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2031e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2032e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2033e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20349577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2035d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20369577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2037d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2038f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2039f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2040e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2041e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2042ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2043e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
2044ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2045ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2046e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2047ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2048ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2049ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2050c3ed0e72SKun Liu 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2051ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2052a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2053a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
2054a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2055a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
205630d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
205730d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
2058e098bc96SEvan Quan };
2059e098bc96SEvan Quan 
2060e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2061e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2062e098bc96SEvan Quan {
2063e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
20648ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
20658ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2066e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
2067e098bc96SEvan Quan 
2068e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2069e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2070e098bc96SEvan Quan 		return 0;
2071e098bc96SEvan Quan 	}
2072e098bc96SEvan Quan 
2073e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2074e098bc96SEvan Quan 
2075e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
20768ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0))
2077e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2078e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
20798ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0) ||
20808ecad8d6SLijo Lazar 		    gc_ver == IP_VERSION(9, 4, 1) ||
20818ecad8d6SLijo Lazar 		    gc_ver == IP_VERSION(9, 4, 2))
2082e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2083e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
20848ecad8d6SLijo Lazar 		if (mp1_ver < IP_VERSION(10, 0, 0))
2085e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2086e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2087e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
208879c65f3fSEvan Quan 		if (amdgpu_dpm_is_overdrive_supported(adev))
2089e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
2090e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
20918ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1))
2092e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2093e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2094e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
2095e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
2096e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2097e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
209860044748SKent Russell 		switch (gc_ver) {
209960044748SKent Russell 		case IP_VERSION(9, 0, 1):
210060044748SKent Russell 		case IP_VERSION(9, 4, 0):
210160044748SKent Russell 		case IP_VERSION(9, 4, 1):
210260044748SKent Russell 		case IP_VERSION(9, 4, 2):
2103ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
2104276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
210535e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
210635e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
210760044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
210860044748SKent Russell 			break;
210960044748SKent Russell 		default:
2110e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
211160044748SKent Russell 		}
2112e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
21138ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver < IP_VERSION(9, 0, 0))
2114e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2115e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
21168ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2117e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
21189577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
21198ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2120a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
212164440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
21223929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
21232f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
21242f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 3)))
21259577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
21260b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
21270b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2128*feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2129*feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2130*feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
21310b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
21329577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
21338ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2134a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
213564440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
21363929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
21372f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
21382f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 3)))
21399577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
21400b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
21410b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2142*feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2143*feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2144*feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
21450b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
2146a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
214779c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2148a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
21491b852572SDanijel Slivka 		else if (gc_ver == IP_VERSION(10, 3, 0) && amdgpu_sriov_vf(adev))
21501b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
2151e098bc96SEvan Quan 	}
2152e098bc96SEvan Quan 
21538ecad8d6SLijo Lazar 	switch (gc_ver) {
21548ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 1):
21558ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 2):
21561d0e622fSKevin Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2157e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2158e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2159e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2160e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2161e098bc96SEvan Quan 			dev_attr->store = NULL;
2162e098bc96SEvan Quan 		}
21631d0e622fSKevin Wang 		break;
21641b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
21651b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
21661b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
21671b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
21681b852572SDanijel Slivka 			dev_attr->store = NULL;
21691b852572SDanijel Slivka 		}
21701b852572SDanijel Slivka 		break;
21711d0e622fSKevin Wang 	default:
21721d0e622fSKevin Wang 		break;
2173e098bc96SEvan Quan 	}
2174e098bc96SEvan Quan 
2175ede14a1bSDarren Powell 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2176ede14a1bSDarren Powell 		/* SMU MP1 does not support dcefclk level setting */
21778ecad8d6SLijo Lazar 		if (gc_ver >= IP_VERSION(10, 0, 0)) {
2178ede14a1bSDarren Powell 			dev_attr->attr.mode &= ~S_IWUGO;
2179ede14a1bSDarren Powell 			dev_attr->store = NULL;
2180ede14a1bSDarren Powell 		}
2181ede14a1bSDarren Powell 	}
2182ede14a1bSDarren Powell 
2183e610941cSYiqing Yao 	/* setting should not be allowed from VF if not in one VF mode */
2184e610941cSYiqing Yao 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
218511c9cc95SMarina Nikolic 		dev_attr->attr.mode &= ~S_IWUGO;
218611c9cc95SMarina Nikolic 		dev_attr->store = NULL;
218711c9cc95SMarina Nikolic 	}
218811c9cc95SMarina Nikolic 
2189e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2190e098bc96SEvan Quan 
2191e098bc96SEvan Quan 	return 0;
2192e098bc96SEvan Quan }
2193e098bc96SEvan Quan 
2194e098bc96SEvan Quan 
2195e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2196e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2197e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2198e098bc96SEvan Quan {
2199e098bc96SEvan Quan 	int ret = 0;
2200e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2201e098bc96SEvan Quan 	const char *name = dev_attr->attr.name;
2202e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2203e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
2204e098bc96SEvan Quan 
2205e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2206e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2207e098bc96SEvan Quan 
2208e098bc96SEvan Quan 	BUG_ON(!attr);
2209e098bc96SEvan Quan 
22108a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2211e098bc96SEvan Quan 
2212e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2213e098bc96SEvan Quan 	if (ret) {
2214e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2215e098bc96SEvan Quan 			name, ret);
2216e098bc96SEvan Quan 		return ret;
2217e098bc96SEvan Quan 	}
2218e098bc96SEvan Quan 
2219e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2220e098bc96SEvan Quan 		return 0;
2221e098bc96SEvan Quan 
2222e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2223e098bc96SEvan Quan 	if (ret) {
2224e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2225e098bc96SEvan Quan 			name, ret);
2226e098bc96SEvan Quan 	}
2227e098bc96SEvan Quan 
2228e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2229e098bc96SEvan Quan 	if (!attr_entry)
2230e098bc96SEvan Quan 		return -ENOMEM;
2231e098bc96SEvan Quan 
2232e098bc96SEvan Quan 	attr_entry->attr = attr;
2233e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2234e098bc96SEvan Quan 
2235e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2236e098bc96SEvan Quan 
2237e098bc96SEvan Quan 	return ret;
2238e098bc96SEvan Quan }
2239e098bc96SEvan Quan 
2240e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2241e098bc96SEvan Quan {
2242e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2243e098bc96SEvan Quan 
2244e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2245e098bc96SEvan Quan }
2246e098bc96SEvan Quan 
2247e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2248e098bc96SEvan Quan 					     struct list_head *attr_list);
2249e098bc96SEvan Quan 
2250e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2251e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2252e098bc96SEvan Quan 					    uint32_t counts,
2253e098bc96SEvan Quan 					    uint32_t mask,
2254e098bc96SEvan Quan 					    struct list_head *attr_list)
2255e098bc96SEvan Quan {
2256e098bc96SEvan Quan 	int ret = 0;
2257e098bc96SEvan Quan 	uint32_t i = 0;
2258e098bc96SEvan Quan 
2259e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2260e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2261e098bc96SEvan Quan 		if (ret)
2262e098bc96SEvan Quan 			goto failed;
2263e098bc96SEvan Quan 	}
2264e098bc96SEvan Quan 
2265e098bc96SEvan Quan 	return 0;
2266e098bc96SEvan Quan 
2267e098bc96SEvan Quan failed:
2268e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2269e098bc96SEvan Quan 
2270e098bc96SEvan Quan 	return ret;
2271e098bc96SEvan Quan }
2272e098bc96SEvan Quan 
2273e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2274e098bc96SEvan Quan 					     struct list_head *attr_list)
2275e098bc96SEvan Quan {
2276e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2277e098bc96SEvan Quan 
2278e098bc96SEvan Quan 	if (list_empty(attr_list))
2279e098bc96SEvan Quan 		return ;
2280e098bc96SEvan Quan 
2281e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2282e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2283e098bc96SEvan Quan 		list_del(&entry->entry);
2284e098bc96SEvan Quan 		kfree(entry);
2285e098bc96SEvan Quan 	}
2286e098bc96SEvan Quan }
2287e098bc96SEvan Quan 
2288e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2289e098bc96SEvan Quan 				      struct device_attribute *attr,
2290e098bc96SEvan Quan 				      char *buf)
2291e098bc96SEvan Quan {
2292e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2293e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2294e098bc96SEvan Quan 	int r, temp = 0, size = sizeof(temp);
2295e098bc96SEvan Quan 
229653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2297e098bc96SEvan Quan 		return -EPERM;
2298d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2299d2ae842dSAlex Deucher 		return -EPERM;
2300e098bc96SEvan Quan 
2301e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2302e098bc96SEvan Quan 		return -EINVAL;
2303e098bc96SEvan Quan 
23044a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2305e098bc96SEvan Quan 	if (r < 0) {
23064a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2307e098bc96SEvan Quan 		return r;
2308e098bc96SEvan Quan 	}
2309e098bc96SEvan Quan 
2310e098bc96SEvan Quan 	switch (channel) {
2311e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2312e098bc96SEvan Quan 		/* get current junction temperature */
2313e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2314e098bc96SEvan Quan 					   (void *)&temp, &size);
2315e098bc96SEvan Quan 		break;
2316e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2317e098bc96SEvan Quan 		/* get current edge temperature */
2318e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2319e098bc96SEvan Quan 					   (void *)&temp, &size);
2320e098bc96SEvan Quan 		break;
2321e098bc96SEvan Quan 	case PP_TEMP_MEM:
2322e098bc96SEvan Quan 		/* get current memory temperature */
2323e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2324e098bc96SEvan Quan 					   (void *)&temp, &size);
2325e098bc96SEvan Quan 		break;
2326e098bc96SEvan Quan 	default:
2327e098bc96SEvan Quan 		r = -EINVAL;
2328e098bc96SEvan Quan 		break;
2329e098bc96SEvan Quan 	}
2330e098bc96SEvan Quan 
23314a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
23324a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2333e098bc96SEvan Quan 
2334e098bc96SEvan Quan 	if (r)
2335e098bc96SEvan Quan 		return r;
2336e098bc96SEvan Quan 
2337a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2338e098bc96SEvan Quan }
2339e098bc96SEvan Quan 
2340e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2341e098bc96SEvan Quan 					     struct device_attribute *attr,
2342e098bc96SEvan Quan 					     char *buf)
2343e098bc96SEvan Quan {
2344e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2345e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2346e098bc96SEvan Quan 	int temp;
2347e098bc96SEvan Quan 
2348e098bc96SEvan Quan 	if (hyst)
2349e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2350e098bc96SEvan Quan 	else
2351e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2352e098bc96SEvan Quan 
2353a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2354e098bc96SEvan Quan }
2355e098bc96SEvan Quan 
2356e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2357e098bc96SEvan Quan 					     struct device_attribute *attr,
2358e098bc96SEvan Quan 					     char *buf)
2359e098bc96SEvan Quan {
2360e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2361e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2362e098bc96SEvan Quan 	int temp;
2363e098bc96SEvan Quan 
2364e098bc96SEvan Quan 	if (hyst)
2365e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2366e098bc96SEvan Quan 	else
2367e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2368e098bc96SEvan Quan 
2369a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2370e098bc96SEvan Quan }
2371e098bc96SEvan Quan 
2372e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2373e098bc96SEvan Quan 					     struct device_attribute *attr,
2374e098bc96SEvan Quan 					     char *buf)
2375e098bc96SEvan Quan {
2376e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2377e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2378e098bc96SEvan Quan 	int temp;
2379e098bc96SEvan Quan 
2380e098bc96SEvan Quan 	if (hyst)
2381e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2382e098bc96SEvan Quan 	else
2383e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2384e098bc96SEvan Quan 
2385a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2386e098bc96SEvan Quan }
2387e098bc96SEvan Quan 
2388e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2389e098bc96SEvan Quan 					     struct device_attribute *attr,
2390e098bc96SEvan Quan 					     char *buf)
2391e098bc96SEvan Quan {
2392e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2393e098bc96SEvan Quan 
2394e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2395e098bc96SEvan Quan 		return -EINVAL;
2396e098bc96SEvan Quan 
2397a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2398e098bc96SEvan Quan }
2399e098bc96SEvan Quan 
2400e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2401e098bc96SEvan Quan 					     struct device_attribute *attr,
2402e098bc96SEvan Quan 					     char *buf)
2403e098bc96SEvan Quan {
2404e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2405e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2406e098bc96SEvan Quan 	int temp = 0;
2407e098bc96SEvan Quan 
2408e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2409e098bc96SEvan Quan 		return -EINVAL;
2410e098bc96SEvan Quan 
2411e098bc96SEvan Quan 	switch (channel) {
2412e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2413e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2414e098bc96SEvan Quan 		break;
2415e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2416e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2417e098bc96SEvan Quan 		break;
2418e098bc96SEvan Quan 	case PP_TEMP_MEM:
2419e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2420e098bc96SEvan Quan 		break;
2421e098bc96SEvan Quan 	}
2422e098bc96SEvan Quan 
2423a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2424e098bc96SEvan Quan }
2425e098bc96SEvan Quan 
2426e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2427e098bc96SEvan Quan 					    struct device_attribute *attr,
2428e098bc96SEvan Quan 					    char *buf)
2429e098bc96SEvan Quan {
2430e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2431e098bc96SEvan Quan 	u32 pwm_mode = 0;
2432e098bc96SEvan Quan 	int ret;
2433e098bc96SEvan Quan 
243453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2435e098bc96SEvan Quan 		return -EPERM;
2436d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2437d2ae842dSAlex Deucher 		return -EPERM;
2438e098bc96SEvan Quan 
24394a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2440e098bc96SEvan Quan 	if (ret < 0) {
24414a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2442e098bc96SEvan Quan 		return ret;
2443e098bc96SEvan Quan 	}
2444e098bc96SEvan Quan 
244579c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
244679c65f3fSEvan Quan 
24474a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24484a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
244979c65f3fSEvan Quan 
245079c65f3fSEvan Quan 	if (ret)
2451e098bc96SEvan Quan 		return -EINVAL;
2452e098bc96SEvan Quan 
2453fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2454e098bc96SEvan Quan }
2455e098bc96SEvan Quan 
2456e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2457e098bc96SEvan Quan 					    struct device_attribute *attr,
2458e098bc96SEvan Quan 					    const char *buf,
2459e098bc96SEvan Quan 					    size_t count)
2460e098bc96SEvan Quan {
2461e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2462e098bc96SEvan Quan 	int err, ret;
2463e098bc96SEvan Quan 	int value;
2464e098bc96SEvan Quan 
246553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2466e098bc96SEvan Quan 		return -EPERM;
2467d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2468d2ae842dSAlex Deucher 		return -EPERM;
2469e098bc96SEvan Quan 
2470e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2471e098bc96SEvan Quan 	if (err)
2472e098bc96SEvan Quan 		return err;
2473e098bc96SEvan Quan 
24744a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2475e098bc96SEvan Quan 	if (ret < 0) {
24764a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2477e098bc96SEvan Quan 		return ret;
2478e098bc96SEvan Quan 	}
2479e098bc96SEvan Quan 
248079c65f3fSEvan Quan 	ret = amdgpu_dpm_set_fan_control_mode(adev, value);
248179c65f3fSEvan Quan 
24824a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24834a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
248479c65f3fSEvan Quan 
248579c65f3fSEvan Quan 	if (ret)
2486e098bc96SEvan Quan 		return -EINVAL;
2487e098bc96SEvan Quan 
2488e098bc96SEvan Quan 	return count;
2489e098bc96SEvan Quan }
2490e098bc96SEvan Quan 
2491e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2492e098bc96SEvan Quan 					 struct device_attribute *attr,
2493e098bc96SEvan Quan 					 char *buf)
2494e098bc96SEvan Quan {
2495fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2496e098bc96SEvan Quan }
2497e098bc96SEvan Quan 
2498e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2499e098bc96SEvan Quan 					 struct device_attribute *attr,
2500e098bc96SEvan Quan 					 char *buf)
2501e098bc96SEvan Quan {
2502fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2503e098bc96SEvan Quan }
2504e098bc96SEvan Quan 
2505e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2506e098bc96SEvan Quan 				     struct device_attribute *attr,
2507e098bc96SEvan Quan 				     const char *buf, size_t count)
2508e098bc96SEvan Quan {
2509e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2510e098bc96SEvan Quan 	int err;
2511e098bc96SEvan Quan 	u32 value;
2512e098bc96SEvan Quan 	u32 pwm_mode;
2513e098bc96SEvan Quan 
251453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2515e098bc96SEvan Quan 		return -EPERM;
2516d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2517d2ae842dSAlex Deucher 		return -EPERM;
2518e098bc96SEvan Quan 
251979c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
252079c65f3fSEvan Quan 	if (err)
252179c65f3fSEvan Quan 		return err;
252279c65f3fSEvan Quan 
25234a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2524e098bc96SEvan Quan 	if (err < 0) {
25254a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2526e098bc96SEvan Quan 		return err;
2527e098bc96SEvan Quan 	}
2528e098bc96SEvan Quan 
252979c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
253079c65f3fSEvan Quan 	if (err)
253179c65f3fSEvan Quan 		goto out;
253279c65f3fSEvan Quan 
2533e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2534e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2535e098bc96SEvan Quan 		err = -EINVAL;
253679c65f3fSEvan Quan 		goto out;
253779c65f3fSEvan Quan 	}
2538e098bc96SEvan Quan 
253979c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
254079c65f3fSEvan Quan 
254179c65f3fSEvan Quan out:
25424a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25434a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2544e098bc96SEvan Quan 
2545e098bc96SEvan Quan 	if (err)
2546e098bc96SEvan Quan 		return err;
2547e098bc96SEvan Quan 
2548e098bc96SEvan Quan 	return count;
2549e098bc96SEvan Quan }
2550e098bc96SEvan Quan 
2551e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2552e098bc96SEvan Quan 				     struct device_attribute *attr,
2553e098bc96SEvan Quan 				     char *buf)
2554e098bc96SEvan Quan {
2555e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2556e098bc96SEvan Quan 	int err;
2557e098bc96SEvan Quan 	u32 speed = 0;
2558e098bc96SEvan Quan 
255953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2560e098bc96SEvan Quan 		return -EPERM;
2561d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2562d2ae842dSAlex Deucher 		return -EPERM;
2563e098bc96SEvan Quan 
25644a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2565e098bc96SEvan Quan 	if (err < 0) {
25664a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2567e098bc96SEvan Quan 		return err;
2568e098bc96SEvan Quan 	}
2569e098bc96SEvan Quan 
25700d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2571e098bc96SEvan Quan 
25724a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25734a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2574e098bc96SEvan Quan 
2575e098bc96SEvan Quan 	if (err)
2576e098bc96SEvan Quan 		return err;
2577e098bc96SEvan Quan 
2578fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2579e098bc96SEvan Quan }
2580e098bc96SEvan Quan 
2581e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2582e098bc96SEvan Quan 					   struct device_attribute *attr,
2583e098bc96SEvan Quan 					   char *buf)
2584e098bc96SEvan Quan {
2585e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2586e098bc96SEvan Quan 	int err;
2587e098bc96SEvan Quan 	u32 speed = 0;
2588e098bc96SEvan Quan 
258953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2590e098bc96SEvan Quan 		return -EPERM;
2591d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2592d2ae842dSAlex Deucher 		return -EPERM;
2593e098bc96SEvan Quan 
25944a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2595e098bc96SEvan Quan 	if (err < 0) {
25964a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2597e098bc96SEvan Quan 		return err;
2598e098bc96SEvan Quan 	}
2599e098bc96SEvan Quan 
2600e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2601e098bc96SEvan Quan 
26024a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26034a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2604e098bc96SEvan Quan 
2605e098bc96SEvan Quan 	if (err)
2606e098bc96SEvan Quan 		return err;
2607e098bc96SEvan Quan 
2608fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2609e098bc96SEvan Quan }
2610e098bc96SEvan Quan 
2611e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2612e098bc96SEvan Quan 					 struct device_attribute *attr,
2613e098bc96SEvan Quan 					 char *buf)
2614e098bc96SEvan Quan {
2615e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2616e098bc96SEvan Quan 	u32 min_rpm = 0;
2617e098bc96SEvan Quan 	u32 size = sizeof(min_rpm);
2618e098bc96SEvan Quan 	int r;
2619e098bc96SEvan Quan 
262053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2621e098bc96SEvan Quan 		return -EPERM;
2622d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2623d2ae842dSAlex Deucher 		return -EPERM;
2624e098bc96SEvan Quan 
26254a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2626e098bc96SEvan Quan 	if (r < 0) {
26274a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2628e098bc96SEvan Quan 		return r;
2629e098bc96SEvan Quan 	}
2630e098bc96SEvan Quan 
2631e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2632e098bc96SEvan Quan 				   (void *)&min_rpm, &size);
2633e098bc96SEvan Quan 
26344a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26354a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2636e098bc96SEvan Quan 
2637e098bc96SEvan Quan 	if (r)
2638e098bc96SEvan Quan 		return r;
2639e098bc96SEvan Quan 
2640a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2641e098bc96SEvan Quan }
2642e098bc96SEvan Quan 
2643e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2644e098bc96SEvan Quan 					 struct device_attribute *attr,
2645e098bc96SEvan Quan 					 char *buf)
2646e098bc96SEvan Quan {
2647e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2648e098bc96SEvan Quan 	u32 max_rpm = 0;
2649e098bc96SEvan Quan 	u32 size = sizeof(max_rpm);
2650e098bc96SEvan Quan 	int r;
2651e098bc96SEvan Quan 
265253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2653e098bc96SEvan Quan 		return -EPERM;
2654d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2655d2ae842dSAlex Deucher 		return -EPERM;
2656e098bc96SEvan Quan 
26574a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2658e098bc96SEvan Quan 	if (r < 0) {
26594a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2660e098bc96SEvan Quan 		return r;
2661e098bc96SEvan Quan 	}
2662e098bc96SEvan Quan 
2663e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2664e098bc96SEvan Quan 				   (void *)&max_rpm, &size);
2665e098bc96SEvan Quan 
26664a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26674a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2668e098bc96SEvan Quan 
2669e098bc96SEvan Quan 	if (r)
2670e098bc96SEvan Quan 		return r;
2671e098bc96SEvan Quan 
2672a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2673e098bc96SEvan Quan }
2674e098bc96SEvan Quan 
2675e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2676e098bc96SEvan Quan 					   struct device_attribute *attr,
2677e098bc96SEvan Quan 					   char *buf)
2678e098bc96SEvan Quan {
2679e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2680e098bc96SEvan Quan 	int err;
2681e098bc96SEvan Quan 	u32 rpm = 0;
2682e098bc96SEvan Quan 
268353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2684e098bc96SEvan Quan 		return -EPERM;
2685d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2686d2ae842dSAlex Deucher 		return -EPERM;
2687e098bc96SEvan Quan 
26884a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2689e098bc96SEvan Quan 	if (err < 0) {
26904a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2691e098bc96SEvan Quan 		return err;
2692e098bc96SEvan Quan 	}
2693e098bc96SEvan Quan 
2694e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2695e098bc96SEvan Quan 
26964a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26974a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2698e098bc96SEvan Quan 
2699e098bc96SEvan Quan 	if (err)
2700e098bc96SEvan Quan 		return err;
2701e098bc96SEvan Quan 
2702fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2703e098bc96SEvan Quan }
2704e098bc96SEvan Quan 
2705e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2706e098bc96SEvan Quan 				     struct device_attribute *attr,
2707e098bc96SEvan Quan 				     const char *buf, size_t count)
2708e098bc96SEvan Quan {
2709e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2710e098bc96SEvan Quan 	int err;
2711e098bc96SEvan Quan 	u32 value;
2712e098bc96SEvan Quan 	u32 pwm_mode;
2713e098bc96SEvan Quan 
271453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2715e098bc96SEvan Quan 		return -EPERM;
2716d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2717d2ae842dSAlex Deucher 		return -EPERM;
2718e098bc96SEvan Quan 
271979c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
272079c65f3fSEvan Quan 	if (err)
272179c65f3fSEvan Quan 		return err;
272279c65f3fSEvan Quan 
27234a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2724e098bc96SEvan Quan 	if (err < 0) {
27254a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2726e098bc96SEvan Quan 		return err;
2727e098bc96SEvan Quan 	}
2728e098bc96SEvan Quan 
272979c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
273079c65f3fSEvan Quan 	if (err)
273179c65f3fSEvan Quan 		goto out;
2732e098bc96SEvan Quan 
2733e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
273479c65f3fSEvan Quan 		err = -ENODATA;
273579c65f3fSEvan Quan 		goto out;
2736e098bc96SEvan Quan 	}
2737e098bc96SEvan Quan 
2738e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2739e098bc96SEvan Quan 
274079c65f3fSEvan Quan out:
27414a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27424a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2743e098bc96SEvan Quan 
2744e098bc96SEvan Quan 	if (err)
2745e098bc96SEvan Quan 		return err;
2746e098bc96SEvan Quan 
2747e098bc96SEvan Quan 	return count;
2748e098bc96SEvan Quan }
2749e098bc96SEvan Quan 
2750e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2751e098bc96SEvan Quan 					    struct device_attribute *attr,
2752e098bc96SEvan Quan 					    char *buf)
2753e098bc96SEvan Quan {
2754e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2755e098bc96SEvan Quan 	u32 pwm_mode = 0;
2756e098bc96SEvan Quan 	int ret;
2757e098bc96SEvan Quan 
275853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2759e098bc96SEvan Quan 		return -EPERM;
2760d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2761d2ae842dSAlex Deucher 		return -EPERM;
2762e098bc96SEvan Quan 
27634a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2764e098bc96SEvan Quan 	if (ret < 0) {
27654a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2766e098bc96SEvan Quan 		return ret;
2767e098bc96SEvan Quan 	}
2768e098bc96SEvan Quan 
276979c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
277079c65f3fSEvan Quan 
27714a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27724a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
277379c65f3fSEvan Quan 
277479c65f3fSEvan Quan 	if (ret)
2775e098bc96SEvan Quan 		return -EINVAL;
2776e098bc96SEvan Quan 
2777fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2778e098bc96SEvan Quan }
2779e098bc96SEvan Quan 
2780e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2781e098bc96SEvan Quan 					    struct device_attribute *attr,
2782e098bc96SEvan Quan 					    const char *buf,
2783e098bc96SEvan Quan 					    size_t count)
2784e098bc96SEvan Quan {
2785e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2786e098bc96SEvan Quan 	int err;
2787e098bc96SEvan Quan 	int value;
2788e098bc96SEvan Quan 	u32 pwm_mode;
2789e098bc96SEvan Quan 
279053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2791e098bc96SEvan Quan 		return -EPERM;
2792d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2793d2ae842dSAlex Deucher 		return -EPERM;
2794e098bc96SEvan Quan 
2795e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2796e098bc96SEvan Quan 	if (err)
2797e098bc96SEvan Quan 		return err;
2798e098bc96SEvan Quan 
2799e098bc96SEvan Quan 	if (value == 0)
2800e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2801e098bc96SEvan Quan 	else if (value == 1)
2802e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2803e098bc96SEvan Quan 	else
2804e098bc96SEvan Quan 		return -EINVAL;
2805e098bc96SEvan Quan 
28064a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2807e098bc96SEvan Quan 	if (err < 0) {
28084a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2809e098bc96SEvan Quan 		return err;
2810e098bc96SEvan Quan 	}
2811e098bc96SEvan Quan 
281279c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2813e098bc96SEvan Quan 
28144a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28154a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2816e098bc96SEvan Quan 
281779c65f3fSEvan Quan 	if (err)
281879c65f3fSEvan Quan 		return -EINVAL;
281979c65f3fSEvan Quan 
2820e098bc96SEvan Quan 	return count;
2821e098bc96SEvan Quan }
2822e098bc96SEvan Quan 
2823e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2824e098bc96SEvan Quan 					struct device_attribute *attr,
2825e098bc96SEvan Quan 					char *buf)
2826e098bc96SEvan Quan {
2827e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2828e098bc96SEvan Quan 	u32 vddgfx;
2829e098bc96SEvan Quan 	int r, size = sizeof(vddgfx);
2830e098bc96SEvan Quan 
283153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2832e098bc96SEvan Quan 		return -EPERM;
2833d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2834d2ae842dSAlex Deucher 		return -EPERM;
2835e098bc96SEvan Quan 
28364a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2837e098bc96SEvan Quan 	if (r < 0) {
28384a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2839e098bc96SEvan Quan 		return r;
2840e098bc96SEvan Quan 	}
2841e098bc96SEvan Quan 
2842e098bc96SEvan Quan 	/* get the voltage */
2843e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2844e098bc96SEvan Quan 				   (void *)&vddgfx, &size);
2845e098bc96SEvan Quan 
28464a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28474a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2848e098bc96SEvan Quan 
2849e098bc96SEvan Quan 	if (r)
2850e098bc96SEvan Quan 		return r;
2851e098bc96SEvan Quan 
2852a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
2853e098bc96SEvan Quan }
2854e098bc96SEvan Quan 
2855e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2856e098bc96SEvan Quan 					      struct device_attribute *attr,
2857e098bc96SEvan Quan 					      char *buf)
2858e098bc96SEvan Quan {
2859a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
2860e098bc96SEvan Quan }
2861e098bc96SEvan Quan 
2862e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2863e098bc96SEvan Quan 				       struct device_attribute *attr,
2864e098bc96SEvan Quan 				       char *buf)
2865e098bc96SEvan Quan {
2866e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2867e098bc96SEvan Quan 	u32 vddnb;
2868e098bc96SEvan Quan 	int r, size = sizeof(vddnb);
2869e098bc96SEvan Quan 
287053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2871e098bc96SEvan Quan 		return -EPERM;
2872d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2873d2ae842dSAlex Deucher 		return -EPERM;
2874e098bc96SEvan Quan 
2875e098bc96SEvan Quan 	/* only APUs have vddnb */
2876e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2877e098bc96SEvan Quan 		return -EINVAL;
2878e098bc96SEvan Quan 
28794a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2880e098bc96SEvan Quan 	if (r < 0) {
28814a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2882e098bc96SEvan Quan 		return r;
2883e098bc96SEvan Quan 	}
2884e098bc96SEvan Quan 
2885e098bc96SEvan Quan 	/* get the voltage */
2886e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2887e098bc96SEvan Quan 				   (void *)&vddnb, &size);
2888e098bc96SEvan Quan 
28894a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28904a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2891e098bc96SEvan Quan 
2892e098bc96SEvan Quan 	if (r)
2893e098bc96SEvan Quan 		return r;
2894e098bc96SEvan Quan 
2895a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
2896e098bc96SEvan Quan }
2897e098bc96SEvan Quan 
2898e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2899e098bc96SEvan Quan 					      struct device_attribute *attr,
2900e098bc96SEvan Quan 					      char *buf)
2901e098bc96SEvan Quan {
2902a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
2903e098bc96SEvan Quan }
2904e098bc96SEvan Quan 
2905e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2906e098bc96SEvan Quan 					   struct device_attribute *attr,
2907e098bc96SEvan Quan 					   char *buf)
2908e098bc96SEvan Quan {
2909e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2910e098bc96SEvan Quan 	u32 query = 0;
2911e098bc96SEvan Quan 	int r, size = sizeof(u32);
2912e098bc96SEvan Quan 	unsigned uw;
2913e098bc96SEvan Quan 
291453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2915e098bc96SEvan Quan 		return -EPERM;
2916d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2917d2ae842dSAlex Deucher 		return -EPERM;
2918e098bc96SEvan Quan 
29194a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2920e098bc96SEvan Quan 	if (r < 0) {
29214a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2922e098bc96SEvan Quan 		return r;
2923e098bc96SEvan Quan 	}
2924e098bc96SEvan Quan 
2925e098bc96SEvan Quan 	/* get the voltage */
2926e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2927e098bc96SEvan Quan 				   (void *)&query, &size);
2928e098bc96SEvan Quan 
29294a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29304a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2931e098bc96SEvan Quan 
2932e098bc96SEvan Quan 	if (r)
2933e098bc96SEvan Quan 		return r;
2934e098bc96SEvan Quan 
2935e098bc96SEvan Quan 	/* convert to microwatts */
2936e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2937e098bc96SEvan Quan 
2938a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", uw);
2939e098bc96SEvan Quan }
2940e098bc96SEvan Quan 
2941e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2942e098bc96SEvan Quan 					 struct device_attribute *attr,
2943e098bc96SEvan Quan 					 char *buf)
2944e098bc96SEvan Quan {
2945fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2946e098bc96SEvan Quan }
2947e098bc96SEvan Quan 
294891161b06SDarren Powell 
294991161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2950e098bc96SEvan Quan 					struct device_attribute *attr,
295191161b06SDarren Powell 					char *buf,
295291161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
2953e098bc96SEvan Quan {
2954e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2955a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2956a40a020dSDarren Powell 	uint32_t limit;
2957e098bc96SEvan Quan 	ssize_t size;
2958e098bc96SEvan Quan 	int r;
2959e098bc96SEvan Quan 
296053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2961e098bc96SEvan Quan 		return -EPERM;
2962d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2963d2ae842dSAlex Deucher 		return -EPERM;
2964e098bc96SEvan Quan 
29654a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2966e098bc96SEvan Quan 	if (r < 0) {
29674a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2968e098bc96SEvan Quan 		return r;
2969e098bc96SEvan Quan 	}
2970e098bc96SEvan Quan 
297179c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
297204bec521SDarren Powell 				      pp_limit_level, power_type);
2973dc2a8240SDarren Powell 
2974dc2a8240SDarren Powell 	if (!r)
297509b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2976dc2a8240SDarren Powell 	else
297709b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
2978e098bc96SEvan Quan 
29794a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29804a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2981e098bc96SEvan Quan 
2982e098bc96SEvan Quan 	return size;
2983e098bc96SEvan Quan }
2984e098bc96SEvan Quan 
298591161b06SDarren Powell 
298691161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
298791161b06SDarren Powell 					 struct device_attribute *attr,
298891161b06SDarren Powell 					 char *buf)
298991161b06SDarren Powell {
299091161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
299191161b06SDarren Powell 
299291161b06SDarren Powell }
299391161b06SDarren Powell 
2994e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2995e098bc96SEvan Quan 					 struct device_attribute *attr,
2996e098bc96SEvan Quan 					 char *buf)
2997e098bc96SEvan Quan {
299891161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2999e098bc96SEvan Quan 
3000e098bc96SEvan Quan }
3001e098bc96SEvan Quan 
30026e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
30036e58941cSEric Huang 					 struct device_attribute *attr,
30046e58941cSEric Huang 					 char *buf)
30056e58941cSEric Huang {
300691161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
30076e58941cSEric Huang 
30086e58941cSEric Huang }
30096e58941cSEric Huang 
3010ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3011ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
3012ae07970aSXiaomeng Hou 					 char *buf)
3013ae07970aSXiaomeng Hou {
30143b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
30158ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3016ae07970aSXiaomeng Hou 
30178ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
3018a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
30193b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
30203b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
30213b99e8e3SYang Wang 	else
30223b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
3023ae07970aSXiaomeng Hou }
3024e098bc96SEvan Quan 
3025e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3026e098bc96SEvan Quan 		struct device_attribute *attr,
3027e098bc96SEvan Quan 		const char *buf,
3028e098bc96SEvan Quan 		size_t count)
3029e098bc96SEvan Quan {
3030e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3031ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
3032e098bc96SEvan Quan 	int err;
3033e098bc96SEvan Quan 	u32 value;
3034e098bc96SEvan Quan 
303553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3036e098bc96SEvan Quan 		return -EPERM;
3037d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3038d2ae842dSAlex Deucher 		return -EPERM;
3039e098bc96SEvan Quan 
3040e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
3041e098bc96SEvan Quan 		return -EINVAL;
3042e098bc96SEvan Quan 
3043e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
3044e098bc96SEvan Quan 	if (err)
3045e098bc96SEvan Quan 		return err;
3046e098bc96SEvan Quan 
3047e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
3048ae07970aSXiaomeng Hou 	value |= limit_type << 24;
3049e098bc96SEvan Quan 
30504a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3051e098bc96SEvan Quan 	if (err < 0) {
30524a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3053e098bc96SEvan Quan 		return err;
3054e098bc96SEvan Quan 	}
3055e098bc96SEvan Quan 
305679c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
3057e098bc96SEvan Quan 
30584a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30594a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3060e098bc96SEvan Quan 
3061e098bc96SEvan Quan 	if (err)
3062e098bc96SEvan Quan 		return err;
3063e098bc96SEvan Quan 
3064e098bc96SEvan Quan 	return count;
3065e098bc96SEvan Quan }
3066e098bc96SEvan Quan 
3067e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(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 sclk;
3073e098bc96SEvan Quan 	int r, size = sizeof(sclk);
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_SCLK,
3088e098bc96SEvan Quan 				   (void *)&sclk, &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", sclk * 10 * 1000);
3097e098bc96SEvan Quan }
3098e098bc96SEvan Quan 
3099e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3100e098bc96SEvan Quan 					    struct device_attribute *attr,
3101e098bc96SEvan Quan 					    char *buf)
3102e098bc96SEvan Quan {
3103a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
3104e098bc96SEvan Quan }
3105e098bc96SEvan Quan 
3106e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3107e098bc96SEvan Quan 				      struct device_attribute *attr,
3108e098bc96SEvan Quan 				      char *buf)
3109e098bc96SEvan Quan {
3110e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3111e098bc96SEvan Quan 	uint32_t mclk;
3112e098bc96SEvan Quan 	int r, size = sizeof(mclk);
3113e098bc96SEvan Quan 
311453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3115e098bc96SEvan Quan 		return -EPERM;
3116d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3117d2ae842dSAlex Deucher 		return -EPERM;
3118e098bc96SEvan Quan 
31194a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3120e098bc96SEvan Quan 	if (r < 0) {
31214a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3122e098bc96SEvan Quan 		return r;
3123e098bc96SEvan Quan 	}
3124e098bc96SEvan Quan 
3125e098bc96SEvan Quan 	/* get the sclk */
3126e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3127e098bc96SEvan Quan 				   (void *)&mclk, &size);
3128e098bc96SEvan Quan 
31294a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31304a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3131e098bc96SEvan Quan 
3132e098bc96SEvan Quan 	if (r)
3133e098bc96SEvan Quan 		return r;
3134e098bc96SEvan Quan 
3135a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3136e098bc96SEvan Quan }
3137e098bc96SEvan Quan 
3138e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3139e098bc96SEvan Quan 					    struct device_attribute *attr,
3140e098bc96SEvan Quan 					    char *buf)
3141e098bc96SEvan Quan {
3142a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3143e098bc96SEvan Quan }
3144e098bc96SEvan Quan 
3145e098bc96SEvan Quan /**
3146e098bc96SEvan Quan  * DOC: hwmon
3147e098bc96SEvan Quan  *
3148e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3149e098bc96SEvan Quan  *
3150e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3151e098bc96SEvan Quan  *
3152e098bc96SEvan Quan  * - GPU voltage
3153e098bc96SEvan Quan  *
3154e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3155e098bc96SEvan Quan  *
3156e098bc96SEvan Quan  * - GPU power
3157e098bc96SEvan Quan  *
3158e098bc96SEvan Quan  * - GPU fan
3159e098bc96SEvan Quan  *
3160e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3161e098bc96SEvan Quan  *
3162e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3163e098bc96SEvan Quan  *
3164e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3165e098bc96SEvan Quan  *
3166e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3167e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3168e098bc96SEvan Quan  *
3169e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3170e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3171e098bc96SEvan Quan  *
3172e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3173e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3174e098bc96SEvan Quan  *
3175e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3176e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3177e098bc96SEvan Quan  *
3178e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3179e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3180e098bc96SEvan Quan  *
3181e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3182e098bc96SEvan Quan  *
3183e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3184e098bc96SEvan Quan  *
3185e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3186e098bc96SEvan Quan  *
3187e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3188e098bc96SEvan Quan  *
318929f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3190e098bc96SEvan Quan  *
3191e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3192e098bc96SEvan Quan  *
3193e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3194e098bc96SEvan Quan  *
3195e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3196e098bc96SEvan Quan  *
3197e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3198e098bc96SEvan Quan  *
3199e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3200e098bc96SEvan Quan  *
3201e098bc96SEvan 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)
3202e098bc96SEvan Quan  *
3203e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3204e098bc96SEvan Quan  *
3205e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3206e098bc96SEvan Quan  *
3207e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3208e098bc96SEvan Quan  *
3209e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3210e098bc96SEvan Quan  *
3211e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3212e098bc96SEvan Quan  *
3213e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3214e098bc96SEvan Quan  *
3215e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3216e098bc96SEvan Quan  *
321796401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
321896401f7cSEvan Quan  *       That will get the former one overridden.
321996401f7cSEvan Quan  *
3220e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3221e098bc96SEvan Quan  *
3222e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3223e098bc96SEvan Quan  *
3224e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3225e098bc96SEvan Quan  *
3226e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3227e098bc96SEvan Quan  *
3228e098bc96SEvan Quan  */
3229e098bc96SEvan Quan 
3230e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3231e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3232e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3233e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3234e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3235e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3236e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3237e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3238e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3239e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3240e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3241e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3242e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3243e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3244e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3245e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3246e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3247e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3248e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3249e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3250e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3251e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3252e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3253e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3254e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3255e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3256e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3257e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3258e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3259e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3260e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3261e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
32626e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3263ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3264ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3265ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3266ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3267ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
32686e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3269ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3270e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3271e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3272e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3273e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3274e098bc96SEvan Quan 
3275e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3276e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3277e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3278e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3279e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3280e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3281e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3282e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3283e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3284e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3285e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3286e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3287e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3288e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3289e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3290e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3291e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3292e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3293e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3294e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3295e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3296e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3297e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3298e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3299e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3300e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3301e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3302e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3303e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3304e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3305e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3306e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3307e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
33086e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3309ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3310ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3311ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3312ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3313ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
33146e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3315ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3316e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3317e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3318e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3319e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3320e098bc96SEvan Quan 	NULL
3321e098bc96SEvan Quan };
3322e098bc96SEvan Quan 
3323e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3324e098bc96SEvan Quan 					struct attribute *attr, int index)
3325e098bc96SEvan Quan {
3326e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3327e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3328e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
33298ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3330e098bc96SEvan Quan 
3331e098bc96SEvan Quan 	/* under multi-vf mode, the hwmon attributes are all not supported */
3332e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3333e098bc96SEvan Quan 		return 0;
3334e098bc96SEvan Quan 
33354f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
33364f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
33374f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
33384f0f1b58SDanijel Slivka 
3339e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3340e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3341e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3342e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3343e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3344e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3345e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3346e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3347e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3348e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3349e098bc96SEvan Quan 		return 0;
3350e098bc96SEvan Quan 
3351e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3352e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3353e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3354e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3355e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3356e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3357e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3358e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3359e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3360e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3361e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3362e098bc96SEvan Quan 		return 0;
3363e098bc96SEvan Quan 
3364e098bc96SEvan Quan 	/* Skip crit temp on APU */
3365e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3366e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3367e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3368e098bc96SEvan Quan 		return 0;
3369e098bc96SEvan Quan 
3370e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3371e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3372e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3373e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3374e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3375e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3376e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3377e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3378e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3379e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3380e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3381e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3382e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3383e098bc96SEvan Quan 		return 0;
3384e098bc96SEvan Quan 
3385e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3386685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3387e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3388685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3389e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3390e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3391e098bc96SEvan Quan 
3392685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3393e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3394685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3395e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3396e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3397e098bc96SEvan Quan 
33988ecad8d6SLijo Lazar 	/* not implemented yet for GC 10.3.1 APUs */
3399ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
34008ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)))) &&
3401367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3402e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
34036e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
34046e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3405e098bc96SEvan Quan 		return 0;
3406e098bc96SEvan Quan 
34078ecad8d6SLijo Lazar 	/* not implemented yet for APUs having <= GC 9.3.0 */
3408367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
34098ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3410367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3411367deb67SAlex Deucher 		return 0;
3412367deb67SAlex Deucher 
3413e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3414685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3415685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3416685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3417685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3418e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3419e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3420e098bc96SEvan Quan 		return 0;
3421e098bc96SEvan Quan 
3422685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3423685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3424e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3425e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3426e098bc96SEvan Quan 		return 0;
3427e098bc96SEvan Quan 
3428e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3429e098bc96SEvan Quan 	     adev->family == AMDGPU_FAMILY_KV) &&	/* not implemented yet */
3430e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3431e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3432e098bc96SEvan Quan 		return 0;
3433e098bc96SEvan Quan 
3434e098bc96SEvan Quan 	/* only APUs have vddnb */
3435e098bc96SEvan Quan 	if (!(adev->flags & AMD_IS_APU) &&
3436e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3437e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3438e098bc96SEvan Quan 		return 0;
3439e098bc96SEvan Quan 
3440e098bc96SEvan Quan 	/* no mclk on APUs */
3441e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3442e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3443e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3444e098bc96SEvan Quan 		return 0;
3445e098bc96SEvan Quan 
3446e098bc96SEvan Quan 	/* only SOC15 dGPUs support hotspot and mem temperatures */
34478ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3448e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3449e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3450e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3451e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3452e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3453e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3454e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3455e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3456e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3457e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3458e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3459e098bc96SEvan Quan 		return 0;
3460e098bc96SEvan Quan 
3461ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
34628ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3463ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3464ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3465ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3466ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
34676e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3468de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3469ae07970aSXiaomeng Hou 		return 0;
3470ae07970aSXiaomeng Hou 
3471e098bc96SEvan Quan 	return effective_mode;
3472e098bc96SEvan Quan }
3473e098bc96SEvan Quan 
3474e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3475e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3476e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3477e098bc96SEvan Quan };
3478e098bc96SEvan Quan 
3479e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3480e098bc96SEvan Quan 	&hwmon_attrgroup,
3481e098bc96SEvan Quan 	NULL
3482e098bc96SEvan Quan };
3483e098bc96SEvan Quan 
3484e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3485e098bc96SEvan Quan {
3486e098bc96SEvan Quan 	int ret;
3487e098bc96SEvan Quan 	uint32_t mask = 0;
3488e098bc96SEvan Quan 
3489e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
3490e098bc96SEvan Quan 		return 0;
3491e098bc96SEvan Quan 
34925fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
34935fa99373SZhenGuo Yin 
3494e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3495e098bc96SEvan Quan 		return 0;
3496e098bc96SEvan Quan 
3497e098bc96SEvan Quan 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3498e098bc96SEvan Quan 								   DRIVER_NAME, adev,
3499e098bc96SEvan Quan 								   hwmon_groups);
3500e098bc96SEvan Quan 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3501e098bc96SEvan Quan 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3502e098bc96SEvan Quan 		dev_err(adev->dev,
3503e098bc96SEvan Quan 			"Unable to register hwmon device: %d\n", ret);
3504e098bc96SEvan Quan 		return ret;
3505e098bc96SEvan Quan 	}
3506e098bc96SEvan Quan 
3507e098bc96SEvan Quan 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3508e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
3509e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
3510e098bc96SEvan Quan 		break;
3511e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
3512e098bc96SEvan Quan 		mask = 0;
3513e098bc96SEvan Quan 		break;
3514e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
3515e098bc96SEvan Quan 	default:
3516e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
3517e098bc96SEvan Quan 		break;
3518e098bc96SEvan Quan 	}
3519e098bc96SEvan Quan 
3520e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
3521e098bc96SEvan Quan 					       amdgpu_device_attrs,
3522e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
3523e098bc96SEvan Quan 					       mask,
3524e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
3525e098bc96SEvan Quan 	if (ret)
3526e098bc96SEvan Quan 		return ret;
3527e098bc96SEvan Quan 
3528e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
3529e098bc96SEvan Quan 
3530e098bc96SEvan Quan 	return 0;
3531e098bc96SEvan Quan }
3532e098bc96SEvan Quan 
3533e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3534e098bc96SEvan Quan {
3535e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
3536e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3537e098bc96SEvan Quan 
3538e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3539e098bc96SEvan Quan }
3540e098bc96SEvan Quan 
3541e098bc96SEvan Quan /*
3542e098bc96SEvan Quan  * Debugfs info
3543e098bc96SEvan Quan  */
3544e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3545e098bc96SEvan Quan 
3546517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3547517cb957SHuang Rui 					   struct amdgpu_device *adev) {
3548517cb957SHuang Rui 	uint16_t *p_val;
3549517cb957SHuang Rui 	uint32_t size;
3550517cb957SHuang Rui 	int i;
355179c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
3552517cb957SHuang Rui 
355379c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
355479c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
3555517cb957SHuang Rui 				GFP_KERNEL);
3556517cb957SHuang Rui 
3557517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3558517cb957SHuang Rui 					    (void *)p_val, &size)) {
355979c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
3560517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3561517cb957SHuang Rui 					   *(p_val + i), i);
3562517cb957SHuang Rui 		}
3563517cb957SHuang Rui 
3564517cb957SHuang Rui 		kfree(p_val);
3565517cb957SHuang Rui 	}
3566517cb957SHuang Rui }
3567517cb957SHuang Rui 
3568e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3569e098bc96SEvan Quan {
35708ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
35718ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3572e098bc96SEvan Quan 	uint32_t value;
3573800c53d6SXiaojian Du 	uint64_t value64 = 0;
3574e098bc96SEvan Quan 	uint32_t query = 0;
3575e098bc96SEvan Quan 	int size;
3576e098bc96SEvan Quan 
3577e098bc96SEvan Quan 	/* GPU Clocks */
3578e098bc96SEvan Quan 	size = sizeof(value);
3579e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
3580517cb957SHuang Rui 
3581517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
3582517cb957SHuang Rui 
3583e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3584e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3585e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3586e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3587e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3588e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3589e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3590e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3591e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3592e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3593e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3594e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3595e098bc96SEvan Quan 	size = sizeof(uint32_t);
3596e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3597e098bc96SEvan Quan 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3598e098bc96SEvan Quan 	size = sizeof(value);
3599e098bc96SEvan Quan 	seq_printf(m, "\n");
3600e098bc96SEvan Quan 
3601e098bc96SEvan Quan 	/* GPU Temp */
3602e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3603e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3604e098bc96SEvan Quan 
3605e098bc96SEvan Quan 	/* GPU Load */
3606e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3607e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
3608e098bc96SEvan Quan 	/* MEM Load */
3609e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3610e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
3611e098bc96SEvan Quan 
3612e098bc96SEvan Quan 	seq_printf(m, "\n");
3613e098bc96SEvan Quan 
3614e098bc96SEvan Quan 	/* SMC feature mask */
3615e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3616e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3617e098bc96SEvan Quan 
36188ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
36198ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
3620e098bc96SEvan Quan 		/* VCN clocks */
3621e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3622e098bc96SEvan Quan 			if (!value) {
3623e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
3624e098bc96SEvan Quan 			} else {
3625e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
3626e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3627e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3628e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3629e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3630e098bc96SEvan Quan 			}
3631e098bc96SEvan Quan 		}
3632e098bc96SEvan Quan 		seq_printf(m, "\n");
3633e098bc96SEvan Quan 	} else {
3634e098bc96SEvan Quan 		/* UVD clocks */
3635e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3636e098bc96SEvan Quan 			if (!value) {
3637e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
3638e098bc96SEvan Quan 			} else {
3639e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
3640e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3641e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3642e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3643e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3644e098bc96SEvan Quan 			}
3645e098bc96SEvan Quan 		}
3646e098bc96SEvan Quan 		seq_printf(m, "\n");
3647e098bc96SEvan Quan 
3648e098bc96SEvan Quan 		/* VCE clocks */
3649e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3650e098bc96SEvan Quan 			if (!value) {
3651e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
3652e098bc96SEvan Quan 			} else {
3653e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
3654e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3655e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3656e098bc96SEvan Quan 			}
3657e098bc96SEvan Quan 		}
3658e098bc96SEvan Quan 	}
3659e098bc96SEvan Quan 
3660e098bc96SEvan Quan 	return 0;
3661e098bc96SEvan Quan }
3662e098bc96SEvan Quan 
366325faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
3664e098bc96SEvan Quan {
3665e098bc96SEvan Quan 	int i;
3666e098bc96SEvan Quan 
3667e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
3668e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3669e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
3670e098bc96SEvan Quan }
3671e098bc96SEvan Quan 
3672373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3673e098bc96SEvan Quan {
3674373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3675373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
367625faeddcSEvan Quan 	u64 flags = 0;
3677e098bc96SEvan Quan 	int r;
3678e098bc96SEvan Quan 
367953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3680e098bc96SEvan Quan 		return -EPERM;
3681d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3682d2ae842dSAlex Deucher 		return -EPERM;
3683e098bc96SEvan Quan 
3684e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
3685e098bc96SEvan Quan 	if (r < 0) {
3686e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3687e098bc96SEvan Quan 		return r;
3688e098bc96SEvan Quan 	}
3689e098bc96SEvan Quan 
369079c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
3691e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3692e098bc96SEvan Quan 		if (r)
3693e098bc96SEvan Quan 			goto out;
369479c65f3fSEvan Quan 	}
3695e098bc96SEvan Quan 
3696e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3697e098bc96SEvan Quan 
369825faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
3699e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
3700e098bc96SEvan Quan 	seq_printf(m, "\n");
3701e098bc96SEvan Quan 
3702e098bc96SEvan Quan out:
3703e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
3704e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
3705e098bc96SEvan Quan 
3706e098bc96SEvan Quan 	return r;
3707e098bc96SEvan Quan }
3708e098bc96SEvan Quan 
3709373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3710373720f7SNirmoy Das 
371127ebf21fSLijo Lazar /*
371227ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
371327ebf21fSLijo Lazar  *
371427ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
371527ebf21fSLijo Lazar  */
371627ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
371727ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
371827ebf21fSLijo Lazar {
371927ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
372027ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
372127ebf21fSLijo Lazar 	void *smu_prv_buf;
372279c65f3fSEvan Quan 	int ret = 0;
372327ebf21fSLijo Lazar 
372427ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
372527ebf21fSLijo Lazar 		return -EPERM;
372627ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
372727ebf21fSLijo Lazar 		return -EPERM;
372827ebf21fSLijo Lazar 
372979c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
373079c65f3fSEvan Quan 	if (ret)
373179c65f3fSEvan Quan 		return ret;
373227ebf21fSLijo Lazar 
373327ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
373427ebf21fSLijo Lazar 		return -EINVAL;
373527ebf21fSLijo Lazar 
373627ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
373727ebf21fSLijo Lazar 				       smu_prv_buf_size);
373827ebf21fSLijo Lazar }
373927ebf21fSLijo Lazar 
374027ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
374127ebf21fSLijo Lazar 	.owner = THIS_MODULE,
374227ebf21fSLijo Lazar 	.open = simple_open,
374327ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
374427ebf21fSLijo Lazar 	.llseek = default_llseek,
374527ebf21fSLijo Lazar };
374627ebf21fSLijo Lazar 
3747e098bc96SEvan Quan #endif
3748e098bc96SEvan Quan 
3749373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3750e098bc96SEvan Quan {
3751e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3752373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
3753373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
3754373720f7SNirmoy Das 
37551613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
37561613f346SFlora Cui 		return;
37571613f346SFlora Cui 
3758373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3759373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
3760373720f7SNirmoy Das 
376127ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
376227ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
376327ebf21fSLijo Lazar 					 adev,
376427ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
376527ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
37661f5fc7a5SAndrey Grodzovsky 
376779c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
3768e098bc96SEvan Quan #endif
3769e098bc96SEvan Quan }
3770