xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision 35e67ca67635d48ce373ae51f93e4672216adc9d)
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",
943867e370SDarren Powell };
953867e370SDarren Powell 
96e098bc96SEvan Quan /**
97e098bc96SEvan Quan  * DOC: power_dpm_state
98e098bc96SEvan Quan  *
99e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
100e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
101e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
102e098bc96SEvan Quan  * It accepts the following arguments:
103e098bc96SEvan Quan  *
104e098bc96SEvan Quan  * - battery
105e098bc96SEvan Quan  *
106e098bc96SEvan Quan  * - balanced
107e098bc96SEvan Quan  *
108e098bc96SEvan Quan  * - performance
109e098bc96SEvan Quan  *
110e098bc96SEvan Quan  * battery
111e098bc96SEvan Quan  *
112e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
113e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
114e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
115e098bc96SEvan Quan  *
116e098bc96SEvan Quan  * balanced
117e098bc96SEvan Quan  *
118e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
119e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
120e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
121e098bc96SEvan Quan  *
122e098bc96SEvan Quan  * performance
123e098bc96SEvan Quan  *
124e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
125e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
126e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
127e098bc96SEvan Quan  *
128e098bc96SEvan Quan  */
129e098bc96SEvan Quan 
130e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
131e098bc96SEvan Quan 					  struct device_attribute *attr,
132e098bc96SEvan Quan 					  char *buf)
133e098bc96SEvan Quan {
134e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1351348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
136e098bc96SEvan Quan 	enum amd_pm_state_type pm;
137e098bc96SEvan Quan 	int ret;
138e098bc96SEvan Quan 
13953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
140e098bc96SEvan Quan 		return -EPERM;
141d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
142d2ae842dSAlex Deucher 		return -EPERM;
143e098bc96SEvan Quan 
144e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
145e098bc96SEvan Quan 	if (ret < 0) {
146e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
147e098bc96SEvan Quan 		return ret;
148e098bc96SEvan Quan 	}
149e098bc96SEvan Quan 
15079c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
151e098bc96SEvan Quan 
152e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
153e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
154e098bc96SEvan Quan 
155a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
156e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
157e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
158e098bc96SEvan Quan }
159e098bc96SEvan Quan 
160e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
161e098bc96SEvan Quan 					  struct device_attribute *attr,
162e098bc96SEvan Quan 					  const char *buf,
163e098bc96SEvan Quan 					  size_t count)
164e098bc96SEvan Quan {
165e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
167e098bc96SEvan Quan 	enum amd_pm_state_type  state;
168e098bc96SEvan Quan 	int ret;
169e098bc96SEvan Quan 
17053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
171e098bc96SEvan Quan 		return -EPERM;
172d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
173d2ae842dSAlex Deucher 		return -EPERM;
174e098bc96SEvan Quan 
175e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
176e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
177e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
178e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
179e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
180e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
181e098bc96SEvan Quan 	else
182e098bc96SEvan Quan 		return -EINVAL;
183e098bc96SEvan Quan 
184e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
185e098bc96SEvan Quan 	if (ret < 0) {
186e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
187e098bc96SEvan Quan 		return ret;
188e098bc96SEvan Quan 	}
189e098bc96SEvan Quan 
19079c65f3fSEvan Quan 	amdgpu_dpm_set_power_state(adev, state);
191e098bc96SEvan Quan 
192e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
193e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
194e098bc96SEvan Quan 
195e098bc96SEvan Quan 	return count;
196e098bc96SEvan Quan }
197e098bc96SEvan Quan 
198e098bc96SEvan Quan 
199e098bc96SEvan Quan /**
200e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
201e098bc96SEvan Quan  *
202e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
203e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
204e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
205e098bc96SEvan Quan  *
206e098bc96SEvan Quan  * - auto
207e098bc96SEvan Quan  *
208e098bc96SEvan Quan  * - low
209e098bc96SEvan Quan  *
210e098bc96SEvan Quan  * - high
211e098bc96SEvan Quan  *
212e098bc96SEvan Quan  * - manual
213e098bc96SEvan Quan  *
214e098bc96SEvan Quan  * - profile_standard
215e098bc96SEvan Quan  *
216e098bc96SEvan Quan  * - profile_min_sclk
217e098bc96SEvan Quan  *
218e098bc96SEvan Quan  * - profile_min_mclk
219e098bc96SEvan Quan  *
220e098bc96SEvan Quan  * - profile_peak
221e098bc96SEvan Quan  *
222e098bc96SEvan Quan  * auto
223e098bc96SEvan Quan  *
224e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
225e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
226e098bc96SEvan Quan  *
227e098bc96SEvan Quan  * low
228e098bc96SEvan Quan  *
229e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
230e098bc96SEvan Quan  *
231e098bc96SEvan Quan  * high
232e098bc96SEvan Quan  *
233e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
234e098bc96SEvan Quan  *
235e098bc96SEvan Quan  * manual
236e098bc96SEvan Quan  *
237e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
238e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
239e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
240e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
241e098bc96SEvan Quan  *
242e098bc96SEvan Quan  * profile_standard
243e098bc96SEvan Quan  * profile_min_sclk
244e098bc96SEvan Quan  * profile_min_mclk
245e098bc96SEvan Quan  * profile_peak
246e098bc96SEvan Quan  *
247e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
248e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
249e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
250e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
251e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
252e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
253e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
254e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
255e098bc96SEvan Quan  *
256e098bc96SEvan Quan  */
257e098bc96SEvan Quan 
258e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
259e098bc96SEvan Quan 							    struct device_attribute *attr,
260e098bc96SEvan Quan 							    char *buf)
261e098bc96SEvan Quan {
262e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2631348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
264e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
265e098bc96SEvan Quan 	int ret;
266e098bc96SEvan Quan 
26753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
268e098bc96SEvan Quan 		return -EPERM;
269d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
270d2ae842dSAlex Deucher 		return -EPERM;
271e098bc96SEvan Quan 
272e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
273e098bc96SEvan Quan 	if (ret < 0) {
274e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
275e098bc96SEvan Quan 		return ret;
276e098bc96SEvan Quan 	}
277e098bc96SEvan Quan 
278e098bc96SEvan Quan 	level = amdgpu_dpm_get_performance_level(adev);
279e098bc96SEvan Quan 
280e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
281e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
282e098bc96SEvan Quan 
283a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
284e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
285e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
286e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
287e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
288e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
289e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
290e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
291e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
2926be64246SLijo Lazar 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
293e098bc96SEvan Quan 			  "unknown");
294e098bc96SEvan Quan }
295e098bc96SEvan Quan 
296e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
297e098bc96SEvan Quan 							    struct device_attribute *attr,
298e098bc96SEvan Quan 							    const char *buf,
299e098bc96SEvan Quan 							    size_t count)
300e098bc96SEvan Quan {
301e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3021348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
303e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
304e098bc96SEvan Quan 	int ret = 0;
305e098bc96SEvan Quan 
30653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
307e098bc96SEvan Quan 		return -EPERM;
308d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
309d2ae842dSAlex Deucher 		return -EPERM;
310e098bc96SEvan Quan 
311e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
312e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
313e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
314e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
315e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
316e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
317e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
318e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
319e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
320e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
321e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
322e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
323e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
324e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
325e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
326e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
327e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
328e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
3296be64246SLijo Lazar 	} else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
3306be64246SLijo Lazar 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
331e098bc96SEvan Quan 	}  else {
332e098bc96SEvan Quan 		return -EINVAL;
333e098bc96SEvan Quan 	}
334e098bc96SEvan Quan 
335e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
336e098bc96SEvan Quan 	if (ret < 0) {
337e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
338e098bc96SEvan Quan 		return ret;
339e098bc96SEvan Quan 	}
340e098bc96SEvan Quan 
3418cda7a4fSAlex Deucher 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
34279c65f3fSEvan Quan 	if (amdgpu_dpm_force_performance_level(adev, level)) {
343e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
344e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
3458cda7a4fSAlex Deucher 		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
346e098bc96SEvan Quan 		return -EINVAL;
347e098bc96SEvan Quan 	}
3488cda7a4fSAlex Deucher 	/* override whatever a user ctx may have set */
3498cda7a4fSAlex Deucher 	adev->pm.stable_pstate_ctx = NULL;
3508cda7a4fSAlex Deucher 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
35179c65f3fSEvan Quan 
352e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
353e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
354e098bc96SEvan Quan 
355e098bc96SEvan Quan 	return count;
356e098bc96SEvan Quan }
357e098bc96SEvan Quan 
358e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
359e098bc96SEvan Quan 		struct device_attribute *attr,
360e098bc96SEvan Quan 		char *buf)
361e098bc96SEvan Quan {
362e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3631348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
364e098bc96SEvan Quan 	struct pp_states_info data;
36509b6744cSDarren Powell 	uint32_t i;
36609b6744cSDarren Powell 	int buf_len, ret;
367e098bc96SEvan Quan 
36853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
369e098bc96SEvan Quan 		return -EPERM;
370d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
371d2ae842dSAlex Deucher 		return -EPERM;
372e098bc96SEvan Quan 
373e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
374e098bc96SEvan Quan 	if (ret < 0) {
375e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
376e098bc96SEvan Quan 		return ret;
377e098bc96SEvan Quan 	}
378e098bc96SEvan Quan 
37979c65f3fSEvan Quan 	if (amdgpu_dpm_get_pp_num_states(adev, &data))
380e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
381e098bc96SEvan Quan 
382e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
383e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
384e098bc96SEvan Quan 
38509b6744cSDarren Powell 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
386e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
38709b6744cSDarren Powell 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
388e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
389e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
390e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
391e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
392e098bc96SEvan Quan 
393e098bc96SEvan Quan 	return buf_len;
394e098bc96SEvan Quan }
395e098bc96SEvan Quan 
396e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
397e098bc96SEvan Quan 		struct device_attribute *attr,
398e098bc96SEvan Quan 		char *buf)
399e098bc96SEvan Quan {
400e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4011348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
4022b24c199STom Rix 	struct pp_states_info data = {0};
403e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
404e098bc96SEvan Quan 	int i = 0, ret = 0;
405e098bc96SEvan Quan 
40653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
407e098bc96SEvan Quan 		return -EPERM;
408d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
409d2ae842dSAlex Deucher 		return -EPERM;
410e098bc96SEvan Quan 
411e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
412e098bc96SEvan Quan 	if (ret < 0) {
413e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
414e098bc96SEvan Quan 		return ret;
415e098bc96SEvan Quan 	}
416e098bc96SEvan Quan 
41779c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
41879c65f3fSEvan Quan 
41979c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
420e098bc96SEvan Quan 
421e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
422e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
423e098bc96SEvan Quan 
42479c65f3fSEvan Quan 	if (ret)
42579c65f3fSEvan Quan 		return ret;
42679c65f3fSEvan Quan 
427e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
428e098bc96SEvan Quan 		if (pm == data.states[i])
429e098bc96SEvan Quan 			break;
430e098bc96SEvan Quan 	}
431e098bc96SEvan Quan 
432e098bc96SEvan Quan 	if (i == data.nums)
433e098bc96SEvan Quan 		i = -EINVAL;
434e098bc96SEvan Quan 
435a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", i);
436e098bc96SEvan Quan }
437e098bc96SEvan Quan 
438e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
439e098bc96SEvan Quan 		struct device_attribute *attr,
440e098bc96SEvan Quan 		char *buf)
441e098bc96SEvan Quan {
442e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4431348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
444e098bc96SEvan Quan 
44553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
446e098bc96SEvan Quan 		return -EPERM;
447d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
448d2ae842dSAlex Deucher 		return -EPERM;
449e098bc96SEvan Quan 
450d698a2c4SEvan Quan 	if (adev->pm.pp_force_state_enabled)
451e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
452e098bc96SEvan Quan 	else
453a9ca9bb3STian Tao 		return sysfs_emit(buf, "\n");
454e098bc96SEvan Quan }
455e098bc96SEvan Quan 
456e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
457e098bc96SEvan Quan 		struct device_attribute *attr,
458e098bc96SEvan Quan 		const char *buf,
459e098bc96SEvan Quan 		size_t count)
460e098bc96SEvan Quan {
461e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4621348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
463e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
46479c65f3fSEvan Quan 	struct pp_states_info data;
465e098bc96SEvan Quan 	unsigned long idx;
466e098bc96SEvan Quan 	int ret;
467e098bc96SEvan Quan 
46853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
469e098bc96SEvan Quan 		return -EPERM;
470d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
471d2ae842dSAlex Deucher 		return -EPERM;
472e098bc96SEvan Quan 
473d698a2c4SEvan Quan 	adev->pm.pp_force_state_enabled = false;
47479c65f3fSEvan Quan 
475e098bc96SEvan Quan 	if (strlen(buf) == 1)
47679c65f3fSEvan Quan 		return count;
477e098bc96SEvan Quan 
478e098bc96SEvan Quan 	ret = kstrtoul(buf, 0, &idx);
479e098bc96SEvan Quan 	if (ret || idx >= ARRAY_SIZE(data.states))
480e098bc96SEvan Quan 		return -EINVAL;
481e098bc96SEvan Quan 
482e098bc96SEvan Quan 	idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
483e098bc96SEvan Quan 
484e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
485e098bc96SEvan Quan 	if (ret < 0) {
486e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
487e098bc96SEvan Quan 		return ret;
488e098bc96SEvan Quan 	}
489e098bc96SEvan Quan 
49079c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
49179c65f3fSEvan Quan 	if (ret)
49279c65f3fSEvan Quan 		goto err_out;
49379c65f3fSEvan Quan 
49479c65f3fSEvan Quan 	state = data.states[idx];
49579c65f3fSEvan Quan 
496e098bc96SEvan Quan 	/* only set user selected power states */
497e098bc96SEvan Quan 	if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
498e098bc96SEvan Quan 	    state != POWER_STATE_TYPE_DEFAULT) {
49979c65f3fSEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
500e098bc96SEvan Quan 				AMD_PP_TASK_ENABLE_USER_STATE, &state);
50179c65f3fSEvan Quan 		if (ret)
50279c65f3fSEvan Quan 			goto err_out;
50379c65f3fSEvan Quan 
504d698a2c4SEvan Quan 		adev->pm.pp_force_state_enabled = true;
505e098bc96SEvan Quan 	}
50679c65f3fSEvan Quan 
507e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
508e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
509e098bc96SEvan Quan 
510e098bc96SEvan Quan 	return count;
51179c65f3fSEvan Quan 
51279c65f3fSEvan Quan err_out:
51379c65f3fSEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
51479c65f3fSEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
51579c65f3fSEvan Quan 	return ret;
516e098bc96SEvan Quan }
517e098bc96SEvan Quan 
518e098bc96SEvan Quan /**
519e098bc96SEvan Quan  * DOC: pp_table
520e098bc96SEvan Quan  *
521e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
522e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
523e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
524e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
525e098bc96SEvan Quan  * powerplay using that new table.
526e098bc96SEvan Quan  *
527e098bc96SEvan Quan  */
528e098bc96SEvan Quan 
529e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
530e098bc96SEvan Quan 		struct device_attribute *attr,
531e098bc96SEvan Quan 		char *buf)
532e098bc96SEvan Quan {
533e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5341348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
535e098bc96SEvan Quan 	char *table = NULL;
536e098bc96SEvan Quan 	int size, ret;
537e098bc96SEvan Quan 
53853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
539e098bc96SEvan Quan 		return -EPERM;
540d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
541d2ae842dSAlex Deucher 		return -EPERM;
542e098bc96SEvan Quan 
543e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
544e098bc96SEvan Quan 	if (ret < 0) {
545e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
546e098bc96SEvan Quan 		return ret;
547e098bc96SEvan Quan 	}
548e098bc96SEvan Quan 
549e098bc96SEvan Quan 	size = amdgpu_dpm_get_pp_table(adev, &table);
55079c65f3fSEvan Quan 
551e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
552e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
55379c65f3fSEvan Quan 
55479c65f3fSEvan Quan 	if (size <= 0)
555e098bc96SEvan Quan 		return size;
556e098bc96SEvan Quan 
557e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
558e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
559e098bc96SEvan Quan 
560e098bc96SEvan Quan 	memcpy(buf, table, size);
561e098bc96SEvan Quan 
562e098bc96SEvan Quan 	return size;
563e098bc96SEvan Quan }
564e098bc96SEvan Quan 
565e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
566e098bc96SEvan Quan 		struct device_attribute *attr,
567e098bc96SEvan Quan 		const char *buf,
568e098bc96SEvan Quan 		size_t count)
569e098bc96SEvan Quan {
570e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5711348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
572e098bc96SEvan Quan 	int ret = 0;
573e098bc96SEvan Quan 
57453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
575e098bc96SEvan Quan 		return -EPERM;
576d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
577d2ae842dSAlex Deucher 		return -EPERM;
578e098bc96SEvan Quan 
579e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
580e098bc96SEvan Quan 	if (ret < 0) {
581e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
582e098bc96SEvan Quan 		return ret;
583e098bc96SEvan Quan 	}
584e098bc96SEvan Quan 
5858f4828d0SDarren Powell 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
586e098bc96SEvan Quan 
587e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
588e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
589e098bc96SEvan Quan 
59079c65f3fSEvan Quan 	if (ret)
59179c65f3fSEvan Quan 		return ret;
59279c65f3fSEvan Quan 
593e098bc96SEvan Quan 	return count;
594e098bc96SEvan Quan }
595e098bc96SEvan Quan 
596e098bc96SEvan Quan /**
597e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
598e098bc96SEvan Quan  *
599e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
600e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
601e098bc96SEvan Quan  * this.
602e098bc96SEvan Quan  *
603e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
604e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
605e098bc96SEvan Quan  * following formula:
606e098bc96SEvan Quan  *
607e098bc96SEvan Quan  * Clock conversion (Mhz):
608e098bc96SEvan Quan  *
609e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
610e098bc96SEvan Quan  *
611e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
612e098bc96SEvan Quan  *
613e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
614e098bc96SEvan Quan  *
615e098bc96SEvan Quan  * DRAM data rate (MT/s):
616e098bc96SEvan Quan  *
617e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
618e098bc96SEvan Quan  *
619e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
620e098bc96SEvan Quan  *
621e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
622e098bc96SEvan Quan  *
623e098bc96SEvan Quan  * Bandwidth (MB/s):
624e098bc96SEvan Quan  *
625e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
626e098bc96SEvan Quan  *
627e098bc96SEvan Quan  * Some examples:
628e098bc96SEvan Quan  *
629e098bc96SEvan Quan  * G5 on RX460:
630e098bc96SEvan Quan  *
631e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
632e098bc96SEvan Quan  *
633e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
634e098bc96SEvan Quan  *
635e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
636e098bc96SEvan Quan  *
637e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
638e098bc96SEvan Quan  *
639e098bc96SEvan Quan  * G6 on RX5700:
640e098bc96SEvan Quan  *
641e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
642e098bc96SEvan Quan  *
643e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
644e098bc96SEvan Quan  *
645e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
646e098bc96SEvan Quan  *
647e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
648e098bc96SEvan Quan  *
649e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
650e098bc96SEvan Quan  *
651e098bc96SEvan Quan  * Reading the file will display:
652e098bc96SEvan Quan  *
653e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
654e098bc96SEvan Quan  *
655e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
656e098bc96SEvan Quan  *
657e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
658e098bc96SEvan Quan  *
659e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
660e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
661e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
662e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
663e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
664e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
665e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
666e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
667e098bc96SEvan Quan  *
668e098bc96SEvan Quan  *
669e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
670e098bc96SEvan Quan  *
671e098bc96SEvan Quan  * Reading the file will display:
672e098bc96SEvan Quan  *
673e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
674e098bc96SEvan Quan  *
67537a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
67637a58f69SEvan Quan  *   clock labeled OD_MCLK
677e098bc96SEvan Quan  *
678e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
679e098bc96SEvan Quan  *   They can be used to calibrate the sclk voltage curve.
680e098bc96SEvan Quan  *
681a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
682a2b6df4fSEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder and Dimgrey
683a2b6df4fSEvan Quan  *   Cavefish. For these ASICs, the target voltage calculation can be
684a2b6df4fSEvan Quan  *   illustrated by "voltage = voltage calculated from v/f curve +
685a2b6df4fSEvan Quan  *   overdrive vddgfx offset"
686a2b6df4fSEvan Quan  *
687e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage curve points
688e098bc96SEvan Quan  *   labeled OD_RANGE
689e098bc96SEvan Quan  *
6900487bbb4SAlex Deucher  * < For APUs >
6910487bbb4SAlex Deucher  *
6920487bbb4SAlex Deucher  * Reading the file will display:
6930487bbb4SAlex Deucher  *
6940487bbb4SAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
6950487bbb4SAlex Deucher  *
6960487bbb4SAlex Deucher  * - a list of valid ranges for sclk labeled OD_RANGE
6970487bbb4SAlex Deucher  *
6983dc8077fSAlex Deucher  * < For VanGogh >
6993dc8077fSAlex Deucher  *
7003dc8077fSAlex Deucher  * Reading the file will display:
7013dc8077fSAlex Deucher  *
7023dc8077fSAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7033dc8077fSAlex Deucher  * - minimum and maximum core clocks labeled OD_CCLK
7043dc8077fSAlex Deucher  *
7053dc8077fSAlex Deucher  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
7063dc8077fSAlex Deucher  *
707e098bc96SEvan Quan  * To manually adjust these settings:
708e098bc96SEvan Quan  *
709e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
710e098bc96SEvan Quan  *
711e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
712e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
713e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
714e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
7153dc8077fSAlex Deucher  *   "m 1 800" will update maximum mclk to be 800Mhz. For core
7163dc8077fSAlex Deucher  *   clocks on VanGogh, the string contains "p core index clock".
7173dc8077fSAlex Deucher  *   E.g., "p 2 0 800" would set the minimum core clock on core
7183dc8077fSAlex Deucher  *   2 to 800Mhz.
719e098bc96SEvan Quan  *
720e098bc96SEvan Quan  *   For sclk voltage curve, enter the new values by writing a
721e098bc96SEvan Quan  *   string that contains "vc point clock voltage" to the file. The
722e098bc96SEvan Quan  *   points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
723e098bc96SEvan Quan  *   update point1 with clock set as 300Mhz and voltage as
724e098bc96SEvan Quan  *   600mV. "vc 2 1000 1000" will update point3 with clock set
725e098bc96SEvan Quan  *   as 1000Mhz and voltage 1000mV.
726e098bc96SEvan Quan  *
727a2b6df4fSEvan Quan  *   To update the voltage offset applied for gfxclk/voltage calculation,
728a2b6df4fSEvan Quan  *   enter the new value by writing a string that contains "vo offset".
729a2b6df4fSEvan Quan  *   This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
730a2b6df4fSEvan Quan  *   And the offset can be a positive or negative value.
731a2b6df4fSEvan Quan  *
732e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
733e098bc96SEvan Quan  *   to the file to commit your changes
734e098bc96SEvan Quan  *
735e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
736e098bc96SEvan Quan  *   to the file to reset them
737e098bc96SEvan Quan  *
738e098bc96SEvan Quan  */
739e098bc96SEvan Quan 
740e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
741e098bc96SEvan Quan 		struct device_attribute *attr,
742e098bc96SEvan Quan 		const char *buf,
743e098bc96SEvan Quan 		size_t count)
744e098bc96SEvan Quan {
745e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7461348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
747e098bc96SEvan Quan 	int ret;
748e098bc96SEvan Quan 	uint32_t parameter_size = 0;
749e098bc96SEvan Quan 	long parameter[64];
750e098bc96SEvan Quan 	char buf_cpy[128];
751e098bc96SEvan Quan 	char *tmp_str;
752e098bc96SEvan Quan 	char *sub_str;
753e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
754e098bc96SEvan Quan 	uint32_t type;
755e098bc96SEvan Quan 
75653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
757e098bc96SEvan Quan 		return -EPERM;
758d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
759d2ae842dSAlex Deucher 		return -EPERM;
760e098bc96SEvan Quan 
761e098bc96SEvan Quan 	if (count > 127)
762e098bc96SEvan Quan 		return -EINVAL;
763e098bc96SEvan Quan 
764e098bc96SEvan Quan 	if (*buf == 's')
765e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
7660d90d0ddSHuang Rui 	else if (*buf == 'p')
7670d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
768e098bc96SEvan Quan 	else if (*buf == 'm')
769e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
770e098bc96SEvan Quan 	else if(*buf == 'r')
771e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
772e098bc96SEvan Quan 	else if (*buf == 'c')
773e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
774e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
775e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
776a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
777a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
778e098bc96SEvan Quan 	else
779e098bc96SEvan Quan 		return -EINVAL;
780e098bc96SEvan Quan 
781e098bc96SEvan Quan 	memcpy(buf_cpy, buf, count+1);
782e098bc96SEvan Quan 
783e098bc96SEvan Quan 	tmp_str = buf_cpy;
784e098bc96SEvan Quan 
785a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
786a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
787e098bc96SEvan Quan 		tmp_str++;
788e098bc96SEvan Quan 	while (isspace(*++tmp_str));
789e098bc96SEvan Quan 
790ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
791aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
792aec1d870SMatt Coffin 			continue;
793e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
794e098bc96SEvan Quan 		if (ret)
795e098bc96SEvan Quan 			return -EINVAL;
796e098bc96SEvan Quan 		parameter_size++;
797e098bc96SEvan Quan 
798e098bc96SEvan Quan 		while (isspace(*tmp_str))
799e098bc96SEvan Quan 			tmp_str++;
800e098bc96SEvan Quan 	}
801e098bc96SEvan Quan 
802e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
803e098bc96SEvan Quan 	if (ret < 0) {
804e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
805e098bc96SEvan Quan 		return ret;
806e098bc96SEvan Quan 	}
807e098bc96SEvan Quan 
80879c65f3fSEvan Quan 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
80979c65f3fSEvan Quan 					      type,
81012a6727dSXiaojian Du 					      parameter,
81179c65f3fSEvan Quan 					      parameter_size))
81279c65f3fSEvan Quan 		goto err_out;
81312a6727dSXiaojian Du 
81479c65f3fSEvan Quan 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
81579c65f3fSEvan Quan 					  parameter, parameter_size))
81679c65f3fSEvan Quan 		goto err_out;
817e098bc96SEvan Quan 
818e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
81979c65f3fSEvan Quan 		if (amdgpu_dpm_dispatch_task(adev,
820e098bc96SEvan Quan 					     AMD_PP_TASK_READJUST_POWER_STATE,
82179c65f3fSEvan Quan 					     NULL))
82279c65f3fSEvan Quan 			goto err_out;
82379c65f3fSEvan Quan 	}
82479c65f3fSEvan Quan 
825e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
826e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
82779c65f3fSEvan Quan 
828e098bc96SEvan Quan 	return count;
82979c65f3fSEvan Quan 
83079c65f3fSEvan Quan err_out:
831e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
832e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
833e098bc96SEvan Quan 	return -EINVAL;
834e098bc96SEvan Quan }
835e098bc96SEvan Quan 
836e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
837e098bc96SEvan Quan 		struct device_attribute *attr,
838e098bc96SEvan Quan 		char *buf)
839e098bc96SEvan Quan {
840e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8411348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
842c8cb19c7SDarren Powell 	int size = 0;
843e098bc96SEvan Quan 	int ret;
844c8cb19c7SDarren Powell 	enum pp_clock_type od_clocks[6] = {
845c8cb19c7SDarren Powell 		OD_SCLK,
846c8cb19c7SDarren Powell 		OD_MCLK,
847c8cb19c7SDarren Powell 		OD_VDDC_CURVE,
848c8cb19c7SDarren Powell 		OD_RANGE,
849c8cb19c7SDarren Powell 		OD_VDDGFX_OFFSET,
850c8cb19c7SDarren Powell 		OD_CCLK,
851c8cb19c7SDarren Powell 	};
852c8cb19c7SDarren Powell 	uint clk_index;
853e098bc96SEvan Quan 
85453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
855e098bc96SEvan Quan 		return -EPERM;
856d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
857d2ae842dSAlex Deucher 		return -EPERM;
858e098bc96SEvan Quan 
859e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
860e098bc96SEvan Quan 	if (ret < 0) {
861e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
862e098bc96SEvan Quan 		return ret;
863e098bc96SEvan Quan 	}
864e098bc96SEvan Quan 
865c8cb19c7SDarren Powell 	for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
866c8cb19c7SDarren Powell 		ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
867c8cb19c7SDarren Powell 		if (ret)
868c8cb19c7SDarren Powell 			break;
869c8cb19c7SDarren Powell 	}
870c8cb19c7SDarren Powell 	if (ret == -ENOENT) {
871e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
87279c65f3fSEvan Quan 		if (size > 0) {
873e098bc96SEvan Quan 			size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
874e098bc96SEvan Quan 			size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
8758f4828d0SDarren Powell 			size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
876e098bc96SEvan Quan 			size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
8778f4828d0SDarren Powell 			size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
878e098bc96SEvan Quan 		}
879c8cb19c7SDarren Powell 	}
880c8cb19c7SDarren Powell 
881c8cb19c7SDarren Powell 	if (size == 0)
882c8cb19c7SDarren Powell 		size = sysfs_emit(buf, "\n");
883c8cb19c7SDarren Powell 
884e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
885e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
886e098bc96SEvan Quan 
887e098bc96SEvan Quan 	return size;
888e098bc96SEvan Quan }
889e098bc96SEvan Quan 
890e098bc96SEvan Quan /**
891e098bc96SEvan Quan  * DOC: pp_features
892e098bc96SEvan Quan  *
893e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
894e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
895e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
896e098bc96SEvan Quan  *
897e098bc96SEvan Quan  * Reading back the file will show you the followings:
898e098bc96SEvan Quan  * - Current ppfeature masks
899e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
900e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
901e098bc96SEvan Quan  *
902e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
903e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
904e098bc96SEvan Quan  * new ppfeature masks.
905e098bc96SEvan Quan  */
906e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
907e098bc96SEvan Quan 				      struct device_attribute *attr,
908e098bc96SEvan Quan 				      const char *buf,
909e098bc96SEvan Quan 				      size_t count)
910e098bc96SEvan Quan {
911e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9121348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
913e098bc96SEvan Quan 	uint64_t featuremask;
914e098bc96SEvan Quan 	int ret;
915e098bc96SEvan Quan 
91653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
917e098bc96SEvan Quan 		return -EPERM;
918d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
919d2ae842dSAlex Deucher 		return -EPERM;
920e098bc96SEvan Quan 
921e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
922e098bc96SEvan Quan 	if (ret)
923e098bc96SEvan Quan 		return -EINVAL;
924e098bc96SEvan Quan 
925e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
926e098bc96SEvan Quan 	if (ret < 0) {
927e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
928e098bc96SEvan Quan 		return ret;
929e098bc96SEvan Quan 	}
930e098bc96SEvan Quan 
931e098bc96SEvan Quan 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
93279c65f3fSEvan Quan 
933e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
934e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
93579c65f3fSEvan Quan 
93679c65f3fSEvan Quan 	if (ret)
937e098bc96SEvan Quan 		return -EINVAL;
938e098bc96SEvan Quan 
939e098bc96SEvan Quan 	return count;
940e098bc96SEvan Quan }
941e098bc96SEvan Quan 
942e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
943e098bc96SEvan Quan 				      struct device_attribute *attr,
944e098bc96SEvan Quan 				      char *buf)
945e098bc96SEvan Quan {
946e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9471348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
948e098bc96SEvan Quan 	ssize_t size;
949e098bc96SEvan Quan 	int ret;
950e098bc96SEvan Quan 
95153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
952e098bc96SEvan Quan 		return -EPERM;
953d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
954d2ae842dSAlex Deucher 		return -EPERM;
955e098bc96SEvan Quan 
956e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
957e098bc96SEvan Quan 	if (ret < 0) {
958e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
959e098bc96SEvan Quan 		return ret;
960e098bc96SEvan Quan 	}
961e098bc96SEvan Quan 
962e098bc96SEvan Quan 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
96379c65f3fSEvan Quan 	if (size <= 0)
96409b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
965e098bc96SEvan Quan 
966e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
967e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
968e098bc96SEvan Quan 
969e098bc96SEvan Quan 	return size;
970e098bc96SEvan Quan }
971e098bc96SEvan Quan 
972e098bc96SEvan Quan /**
973e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
974e098bc96SEvan Quan  *
975e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
976e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
977e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
978e098bc96SEvan Quan  * this.
979e098bc96SEvan Quan  *
980e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
981e098bc96SEvan Quan  * Vega10 and later ASICs.
982e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
983e098bc96SEvan Quan  *
984e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
985e098bc96SEvan Quan  * the power state and the clock information for those levels.
986e098bc96SEvan Quan  *
987e098bc96SEvan Quan  * To manually adjust these states, first select manual using
988e098bc96SEvan Quan  * power_dpm_force_performance_level.
989e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
990e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
991e098bc96SEvan Quan  * E.g.,
992e098bc96SEvan Quan  *
993e098bc96SEvan Quan  * .. code-block:: bash
994e098bc96SEvan Quan  *
995e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
996e098bc96SEvan Quan  *
997e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
998e098bc96SEvan Quan  *
999e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1000e098bc96SEvan Quan  */
1001e098bc96SEvan Quan 
10022ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
10032ea092e5SDarren Powell 		enum pp_clock_type type,
1004e098bc96SEvan Quan 		char *buf)
1005e098bc96SEvan Quan {
1006e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10071348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1008c8cb19c7SDarren Powell 	int size = 0;
1009c8cb19c7SDarren Powell 	int ret = 0;
1010e098bc96SEvan Quan 
101153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1012e098bc96SEvan Quan 		return -EPERM;
1013d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1014d2ae842dSAlex Deucher 		return -EPERM;
1015e098bc96SEvan Quan 
1016e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1017e098bc96SEvan Quan 	if (ret < 0) {
1018e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1019e098bc96SEvan Quan 		return ret;
1020e098bc96SEvan Quan 	}
1021e098bc96SEvan Quan 
1022c8cb19c7SDarren Powell 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1023c8cb19c7SDarren Powell 	if (ret == -ENOENT)
10242ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1025c8cb19c7SDarren Powell 
1026c8cb19c7SDarren Powell 	if (size == 0)
102709b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1028e098bc96SEvan Quan 
1029e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1030e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1031e098bc96SEvan Quan 
1032e098bc96SEvan Quan 	return size;
1033e098bc96SEvan Quan }
1034e098bc96SEvan Quan 
1035e098bc96SEvan Quan /*
1036e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1037e098bc96SEvan Quan  * per line (+1 for \n).
1038e098bc96SEvan Quan  */
1039e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1040e098bc96SEvan Quan 
1041e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1042e098bc96SEvan Quan {
1043e098bc96SEvan Quan 	int ret;
1044c915ef89SDan Carpenter 	unsigned long level;
1045e098bc96SEvan Quan 	char *sub_str = NULL;
1046e098bc96SEvan Quan 	char *tmp;
1047e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1048e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1049e098bc96SEvan Quan 	size_t bytes;
1050e098bc96SEvan Quan 
1051e098bc96SEvan Quan 	*mask = 0;
1052e098bc96SEvan Quan 
1053e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1054e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1055e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1056e098bc96SEvan Quan 	tmp = buf_cpy;
1057ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1058e098bc96SEvan Quan 		if (strlen(sub_str)) {
1059c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1060c915ef89SDan Carpenter 			if (ret || level > 31)
1061e098bc96SEvan Quan 				return -EINVAL;
1062e098bc96SEvan Quan 			*mask |= 1 << level;
1063e098bc96SEvan Quan 		} else
1064e098bc96SEvan Quan 			break;
1065e098bc96SEvan Quan 	}
1066e098bc96SEvan Quan 
1067e098bc96SEvan Quan 	return 0;
1068e098bc96SEvan Quan }
1069e098bc96SEvan Quan 
10702ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
10712ea092e5SDarren Powell 		enum pp_clock_type type,
1072e098bc96SEvan Quan 		const char *buf,
1073e098bc96SEvan Quan 		size_t count)
1074e098bc96SEvan Quan {
1075e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10761348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1077e098bc96SEvan Quan 	int ret;
1078e098bc96SEvan Quan 	uint32_t mask = 0;
1079e098bc96SEvan Quan 
108053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1081e098bc96SEvan Quan 		return -EPERM;
1082d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1083d2ae842dSAlex Deucher 		return -EPERM;
1084e098bc96SEvan Quan 
1085e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1086e098bc96SEvan Quan 	if (ret)
1087e098bc96SEvan Quan 		return ret;
1088e098bc96SEvan Quan 
1089e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1090e098bc96SEvan Quan 	if (ret < 0) {
1091e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1092e098bc96SEvan Quan 		return ret;
1093e098bc96SEvan Quan 	}
1094e098bc96SEvan Quan 
10952ea092e5SDarren Powell 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1096e098bc96SEvan Quan 
1097e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1098e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1099e098bc96SEvan Quan 
1100e098bc96SEvan Quan 	if (ret)
1101e098bc96SEvan Quan 		return -EINVAL;
1102e098bc96SEvan Quan 
1103e098bc96SEvan Quan 	return count;
1104e098bc96SEvan Quan }
1105e098bc96SEvan Quan 
11062ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
11072ea092e5SDarren Powell 		struct device_attribute *attr,
11082ea092e5SDarren Powell 		char *buf)
11092ea092e5SDarren Powell {
11102ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
11112ea092e5SDarren Powell }
11122ea092e5SDarren Powell 
11132ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
11142ea092e5SDarren Powell 		struct device_attribute *attr,
11152ea092e5SDarren Powell 		const char *buf,
11162ea092e5SDarren Powell 		size_t count)
11172ea092e5SDarren Powell {
11182ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
11192ea092e5SDarren Powell }
11202ea092e5SDarren Powell 
1121e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1122e098bc96SEvan Quan 		struct device_attribute *attr,
1123e098bc96SEvan Quan 		char *buf)
1124e098bc96SEvan Quan {
11252ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1126e098bc96SEvan Quan }
1127e098bc96SEvan Quan 
1128e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1129e098bc96SEvan Quan 		struct device_attribute *attr,
1130e098bc96SEvan Quan 		const char *buf,
1131e098bc96SEvan Quan 		size_t count)
1132e098bc96SEvan Quan {
11332ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1134e098bc96SEvan Quan }
1135e098bc96SEvan Quan 
1136e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1137e098bc96SEvan Quan 		struct device_attribute *attr,
1138e098bc96SEvan Quan 		char *buf)
1139e098bc96SEvan Quan {
11402ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1141e098bc96SEvan Quan }
1142e098bc96SEvan Quan 
1143e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1144e098bc96SEvan Quan 		struct device_attribute *attr,
1145e098bc96SEvan Quan 		const char *buf,
1146e098bc96SEvan Quan 		size_t count)
1147e098bc96SEvan Quan {
11482ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1149e098bc96SEvan Quan }
1150e098bc96SEvan Quan 
1151e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1152e098bc96SEvan Quan 		struct device_attribute *attr,
1153e098bc96SEvan Quan 		char *buf)
1154e098bc96SEvan Quan {
11552ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1156e098bc96SEvan Quan }
1157e098bc96SEvan Quan 
1158e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1159e098bc96SEvan Quan 		struct device_attribute *attr,
1160e098bc96SEvan Quan 		const char *buf,
1161e098bc96SEvan Quan 		size_t count)
1162e098bc96SEvan Quan {
11632ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1164e098bc96SEvan Quan }
1165e098bc96SEvan Quan 
11669577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
11679577b0ecSXiaojian Du 		struct device_attribute *attr,
11689577b0ecSXiaojian Du 		char *buf)
11699577b0ecSXiaojian Du {
11702ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
11719577b0ecSXiaojian Du }
11729577b0ecSXiaojian Du 
11739577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
11749577b0ecSXiaojian Du 		struct device_attribute *attr,
11759577b0ecSXiaojian Du 		const char *buf,
11769577b0ecSXiaojian Du 		size_t count)
11779577b0ecSXiaojian Du {
11782ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
11799577b0ecSXiaojian Du }
11809577b0ecSXiaojian Du 
11819577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
11829577b0ecSXiaojian Du 		struct device_attribute *attr,
11839577b0ecSXiaojian Du 		char *buf)
11849577b0ecSXiaojian Du {
11852ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
11869577b0ecSXiaojian Du }
11879577b0ecSXiaojian Du 
11889577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
11899577b0ecSXiaojian Du 		struct device_attribute *attr,
11909577b0ecSXiaojian Du 		const char *buf,
11919577b0ecSXiaojian Du 		size_t count)
11929577b0ecSXiaojian Du {
11932ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
11949577b0ecSXiaojian Du }
11959577b0ecSXiaojian Du 
1196e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1197e098bc96SEvan Quan 		struct device_attribute *attr,
1198e098bc96SEvan Quan 		char *buf)
1199e098bc96SEvan Quan {
12002ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1201e098bc96SEvan Quan }
1202e098bc96SEvan Quan 
1203e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1204e098bc96SEvan Quan 		struct device_attribute *attr,
1205e098bc96SEvan Quan 		const char *buf,
1206e098bc96SEvan Quan 		size_t count)
1207e098bc96SEvan Quan {
12082ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1209e098bc96SEvan Quan }
1210e098bc96SEvan Quan 
1211e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1212e098bc96SEvan Quan 		struct device_attribute *attr,
1213e098bc96SEvan Quan 		char *buf)
1214e098bc96SEvan Quan {
12152ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1216e098bc96SEvan Quan }
1217e098bc96SEvan Quan 
1218e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1219e098bc96SEvan Quan 		struct device_attribute *attr,
1220e098bc96SEvan Quan 		const char *buf,
1221e098bc96SEvan Quan 		size_t count)
1222e098bc96SEvan Quan {
12232ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1224e098bc96SEvan Quan }
1225e098bc96SEvan Quan 
1226e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1227e098bc96SEvan Quan 		struct device_attribute *attr,
1228e098bc96SEvan Quan 		char *buf)
1229e098bc96SEvan Quan {
1230e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12311348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1232e098bc96SEvan Quan 	uint32_t value = 0;
1233e098bc96SEvan Quan 	int ret;
1234e098bc96SEvan Quan 
123553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1236e098bc96SEvan Quan 		return -EPERM;
1237d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1238d2ae842dSAlex Deucher 		return -EPERM;
1239e098bc96SEvan Quan 
1240e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1241e098bc96SEvan Quan 	if (ret < 0) {
1242e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1243e098bc96SEvan Quan 		return ret;
1244e098bc96SEvan Quan 	}
1245e098bc96SEvan Quan 
1246e098bc96SEvan Quan 	value = amdgpu_dpm_get_sclk_od(adev);
1247e098bc96SEvan Quan 
1248e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1249e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1250e098bc96SEvan Quan 
1251a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1252e098bc96SEvan Quan }
1253e098bc96SEvan Quan 
1254e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1255e098bc96SEvan Quan 		struct device_attribute *attr,
1256e098bc96SEvan Quan 		const char *buf,
1257e098bc96SEvan Quan 		size_t count)
1258e098bc96SEvan Quan {
1259e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12601348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1261e098bc96SEvan Quan 	int ret;
1262e098bc96SEvan Quan 	long int value;
1263e098bc96SEvan Quan 
126453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1265e098bc96SEvan Quan 		return -EPERM;
1266d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1267d2ae842dSAlex Deucher 		return -EPERM;
1268e098bc96SEvan Quan 
1269e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1270e098bc96SEvan Quan 
1271e098bc96SEvan Quan 	if (ret)
1272e098bc96SEvan Quan 		return -EINVAL;
1273e098bc96SEvan Quan 
1274e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1275e098bc96SEvan Quan 	if (ret < 0) {
1276e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1277e098bc96SEvan Quan 		return ret;
1278e098bc96SEvan Quan 	}
1279e098bc96SEvan Quan 
1280e098bc96SEvan Quan 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1281e098bc96SEvan Quan 
1282e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1283e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1284e098bc96SEvan Quan 
1285e098bc96SEvan Quan 	return count;
1286e098bc96SEvan Quan }
1287e098bc96SEvan Quan 
1288e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1289e098bc96SEvan Quan 		struct device_attribute *attr,
1290e098bc96SEvan Quan 		char *buf)
1291e098bc96SEvan Quan {
1292e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12931348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1294e098bc96SEvan Quan 	uint32_t value = 0;
1295e098bc96SEvan Quan 	int ret;
1296e098bc96SEvan Quan 
129753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1298e098bc96SEvan Quan 		return -EPERM;
1299d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1300d2ae842dSAlex Deucher 		return -EPERM;
1301e098bc96SEvan Quan 
1302e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1303e098bc96SEvan Quan 	if (ret < 0) {
1304e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1305e098bc96SEvan Quan 		return ret;
1306e098bc96SEvan Quan 	}
1307e098bc96SEvan Quan 
1308e098bc96SEvan Quan 	value = amdgpu_dpm_get_mclk_od(adev);
1309e098bc96SEvan Quan 
1310e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1311e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1312e098bc96SEvan Quan 
1313a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1314e098bc96SEvan Quan }
1315e098bc96SEvan Quan 
1316e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1317e098bc96SEvan Quan 		struct device_attribute *attr,
1318e098bc96SEvan Quan 		const char *buf,
1319e098bc96SEvan Quan 		size_t count)
1320e098bc96SEvan Quan {
1321e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13221348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1323e098bc96SEvan Quan 	int ret;
1324e098bc96SEvan Quan 	long int value;
1325e098bc96SEvan Quan 
132653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1327e098bc96SEvan Quan 		return -EPERM;
1328d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1329d2ae842dSAlex Deucher 		return -EPERM;
1330e098bc96SEvan Quan 
1331e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1332e098bc96SEvan Quan 
1333e098bc96SEvan Quan 	if (ret)
1334e098bc96SEvan Quan 		return -EINVAL;
1335e098bc96SEvan Quan 
1336e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1337e098bc96SEvan Quan 	if (ret < 0) {
1338e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1339e098bc96SEvan Quan 		return ret;
1340e098bc96SEvan Quan 	}
1341e098bc96SEvan Quan 
1342e098bc96SEvan Quan 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1343e098bc96SEvan Quan 
1344e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1345e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1346e098bc96SEvan Quan 
1347e098bc96SEvan Quan 	return count;
1348e098bc96SEvan Quan }
1349e098bc96SEvan Quan 
1350e098bc96SEvan Quan /**
1351e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1352e098bc96SEvan Quan  *
1353e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1354e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1355e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1356e098bc96SEvan Quan  *
1357e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1358e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1359e098bc96SEvan Quan  *
1360e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1361e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1362e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1363e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1364e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1365e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1366e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1367e098bc96SEvan Quan  *
1368e098bc96SEvan Quan  */
1369e098bc96SEvan Quan 
1370e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1371e098bc96SEvan Quan 		struct device_attribute *attr,
1372e098bc96SEvan Quan 		char *buf)
1373e098bc96SEvan Quan {
1374e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13751348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1376e098bc96SEvan Quan 	ssize_t size;
1377e098bc96SEvan Quan 	int ret;
1378e098bc96SEvan Quan 
137953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1380e098bc96SEvan Quan 		return -EPERM;
1381d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1382d2ae842dSAlex Deucher 		return -EPERM;
1383e098bc96SEvan Quan 
1384e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1385e098bc96SEvan Quan 	if (ret < 0) {
1386e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1387e098bc96SEvan Quan 		return ret;
1388e098bc96SEvan Quan 	}
1389e098bc96SEvan Quan 
1390e098bc96SEvan Quan 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
139179c65f3fSEvan Quan 	if (size <= 0)
139209b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1393e098bc96SEvan Quan 
1394e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1395e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1396e098bc96SEvan Quan 
1397e098bc96SEvan Quan 	return size;
1398e098bc96SEvan Quan }
1399e098bc96SEvan Quan 
1400e098bc96SEvan Quan 
1401e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1402e098bc96SEvan Quan 		struct device_attribute *attr,
1403e098bc96SEvan Quan 		const char *buf,
1404e098bc96SEvan Quan 		size_t count)
1405e098bc96SEvan Quan {
1406e098bc96SEvan Quan 	int ret;
1407e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14081348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1409e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1410e098bc96SEvan Quan 	long parameter[64];
1411e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1412e098bc96SEvan Quan 	char *tmp_str;
1413e098bc96SEvan Quan 	uint32_t i = 0;
1414e098bc96SEvan Quan 	char tmp[2];
1415e098bc96SEvan Quan 	long int profile_mode = 0;
1416e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1417e098bc96SEvan Quan 
141853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1419e098bc96SEvan Quan 		return -EPERM;
1420d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1421d2ae842dSAlex Deucher 		return -EPERM;
1422e098bc96SEvan Quan 
1423e098bc96SEvan Quan 	tmp[0] = *(buf);
1424e098bc96SEvan Quan 	tmp[1] = '\0';
1425e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1426e098bc96SEvan Quan 	if (ret)
1427e098bc96SEvan Quan 		return -EINVAL;
1428e098bc96SEvan Quan 
1429e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1430e098bc96SEvan Quan 		if (count < 2 || count > 127)
1431e098bc96SEvan Quan 			return -EINVAL;
1432e098bc96SEvan Quan 		while (isspace(*++buf))
1433e098bc96SEvan Quan 			i++;
1434e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1435e098bc96SEvan Quan 		tmp_str = buf_cpy;
1436ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1437c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1438c2efbc3fSEvan Quan 				continue;
1439e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1440e098bc96SEvan Quan 			if (ret)
1441e098bc96SEvan Quan 				return -EINVAL;
1442e098bc96SEvan Quan 			parameter_size++;
1443e098bc96SEvan Quan 			while (isspace(*tmp_str))
1444e098bc96SEvan Quan 				tmp_str++;
1445e098bc96SEvan Quan 		}
1446e098bc96SEvan Quan 	}
1447e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1448e098bc96SEvan Quan 
1449e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1450e098bc96SEvan Quan 	if (ret < 0) {
1451e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1452e098bc96SEvan Quan 		return ret;
1453e098bc96SEvan Quan 	}
1454e098bc96SEvan Quan 
1455e098bc96SEvan Quan 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1456e098bc96SEvan Quan 
1457e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1458e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1459e098bc96SEvan Quan 
1460e098bc96SEvan Quan 	if (!ret)
1461e098bc96SEvan Quan 		return count;
1462e098bc96SEvan Quan 
1463e098bc96SEvan Quan 	return -EINVAL;
1464e098bc96SEvan Quan }
1465e098bc96SEvan Quan 
1466e098bc96SEvan Quan /**
1467e098bc96SEvan Quan  * DOC: gpu_busy_percent
1468e098bc96SEvan Quan  *
1469e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1470e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1471e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1472e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1473e098bc96SEvan Quan  */
1474e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1475e098bc96SEvan Quan 					   struct device_attribute *attr,
1476e098bc96SEvan Quan 					   char *buf)
1477e098bc96SEvan Quan {
1478e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14791348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1480e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1481e098bc96SEvan Quan 
148253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1483e098bc96SEvan Quan 		return -EPERM;
1484d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1485d2ae842dSAlex Deucher 		return -EPERM;
1486e098bc96SEvan Quan 
1487e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1488e098bc96SEvan Quan 	if (r < 0) {
1489e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1490e098bc96SEvan Quan 		return r;
1491e098bc96SEvan Quan 	}
1492e098bc96SEvan Quan 
1493e098bc96SEvan Quan 	/* read the IP busy sensor */
1494e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1495e098bc96SEvan Quan 				   (void *)&value, &size);
1496e098bc96SEvan Quan 
1497e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1498e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1499e098bc96SEvan Quan 
1500e098bc96SEvan Quan 	if (r)
1501e098bc96SEvan Quan 		return r;
1502e098bc96SEvan Quan 
1503a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1504e098bc96SEvan Quan }
1505e098bc96SEvan Quan 
1506e098bc96SEvan Quan /**
1507e098bc96SEvan Quan  * DOC: mem_busy_percent
1508e098bc96SEvan Quan  *
1509e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1510e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1511e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1512e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1513e098bc96SEvan Quan  */
1514e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1515e098bc96SEvan Quan 					   struct device_attribute *attr,
1516e098bc96SEvan Quan 					   char *buf)
1517e098bc96SEvan Quan {
1518e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15191348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1520e098bc96SEvan Quan 	int r, value, size = sizeof(value);
1521e098bc96SEvan Quan 
152253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1523e098bc96SEvan Quan 		return -EPERM;
1524d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1525d2ae842dSAlex Deucher 		return -EPERM;
1526e098bc96SEvan Quan 
1527e098bc96SEvan Quan 	r = pm_runtime_get_sync(ddev->dev);
1528e098bc96SEvan Quan 	if (r < 0) {
1529e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1530e098bc96SEvan Quan 		return r;
1531e098bc96SEvan Quan 	}
1532e098bc96SEvan Quan 
1533e098bc96SEvan Quan 	/* read the IP busy sensor */
1534e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1535e098bc96SEvan Quan 				   (void *)&value, &size);
1536e098bc96SEvan Quan 
1537e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1538e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1539e098bc96SEvan Quan 
1540e098bc96SEvan Quan 	if (r)
1541e098bc96SEvan Quan 		return r;
1542e098bc96SEvan Quan 
1543a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1544e098bc96SEvan Quan }
1545e098bc96SEvan Quan 
1546e098bc96SEvan Quan /**
1547e098bc96SEvan Quan  * DOC: pcie_bw
1548e098bc96SEvan Quan  *
1549e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1550e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1551e098bc96SEvan Quan  * The file pcie_bw is used for this.
1552e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1553e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1554e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1555e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1556e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1557e098bc96SEvan Quan  */
1558e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1559e098bc96SEvan Quan 		struct device_attribute *attr,
1560e098bc96SEvan Quan 		char *buf)
1561e098bc96SEvan Quan {
1562e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15631348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1564e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1565e098bc96SEvan Quan 	int ret;
1566e098bc96SEvan Quan 
156753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1568e098bc96SEvan Quan 		return -EPERM;
1569d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1570d2ae842dSAlex Deucher 		return -EPERM;
1571e098bc96SEvan Quan 
1572e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1573e098bc96SEvan Quan 		return -ENODATA;
1574e098bc96SEvan Quan 
1575e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1576e098bc96SEvan Quan 		return -ENODATA;
1577e098bc96SEvan Quan 
1578e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1579e098bc96SEvan Quan 	if (ret < 0) {
1580e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1581e098bc96SEvan Quan 		return ret;
1582e098bc96SEvan Quan 	}
1583e098bc96SEvan Quan 
1584e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1585e098bc96SEvan Quan 
1586e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1587e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1588e098bc96SEvan Quan 
1589a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1590e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1591e098bc96SEvan Quan }
1592e098bc96SEvan Quan 
1593e098bc96SEvan Quan /**
1594e098bc96SEvan Quan  * DOC: unique_id
1595e098bc96SEvan Quan  *
1596e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1597e098bc96SEvan Quan  * The file unique_id is used for this.
1598e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1599e098bc96SEvan Quan  *
1600e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1601e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1602e098bc96SEvan Quan  */
1603e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1604e098bc96SEvan Quan 		struct device_attribute *attr,
1605e098bc96SEvan Quan 		char *buf)
1606e098bc96SEvan Quan {
1607e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16081348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1609e098bc96SEvan Quan 
161053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1611e098bc96SEvan Quan 		return -EPERM;
1612d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1613d2ae842dSAlex Deucher 		return -EPERM;
1614e098bc96SEvan Quan 
1615e098bc96SEvan Quan 	if (adev->unique_id)
1616a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1617e098bc96SEvan Quan 
1618e098bc96SEvan Quan 	return 0;
1619e098bc96SEvan Quan }
1620e098bc96SEvan Quan 
1621e098bc96SEvan Quan /**
1622e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1623e098bc96SEvan Quan  *
1624e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1625e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1626e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1627e098bc96SEvan Quan  * the log frequency.
1628e098bc96SEvan Quan  *
1629e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1630e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1631e098bc96SEvan Quan  *
1632e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1633e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1634e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1635e098bc96SEvan Quan  */
1636e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1637e098bc96SEvan Quan 						     struct device_attribute *attr,
1638e098bc96SEvan Quan 						     char *buf)
1639e098bc96SEvan Quan {
1640e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16411348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1642e098bc96SEvan Quan 
1643a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
16444a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1645e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1646e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1647e098bc96SEvan Quan }
1648e098bc96SEvan Quan 
1649e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1650e098bc96SEvan Quan 						     struct device_attribute *attr,
1651e098bc96SEvan Quan 						     const char *buf,
1652e098bc96SEvan Quan 						     size_t count)
1653e098bc96SEvan Quan {
1654e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16551348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1656e098bc96SEvan Quan 	long throttling_logging_interval;
1657e098bc96SEvan Quan 	unsigned long flags;
1658e098bc96SEvan Quan 	int ret = 0;
1659e098bc96SEvan Quan 
1660e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1661e098bc96SEvan Quan 	if (ret)
1662e098bc96SEvan Quan 		return ret;
1663e098bc96SEvan Quan 
1664e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1665e098bc96SEvan Quan 		return -EINVAL;
1666e098bc96SEvan Quan 
1667e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1668e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1669e098bc96SEvan Quan 		/*
1670e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1671e098bc96SEvan Quan 		 * This can effectively restart the timer.
1672e098bc96SEvan Quan 		 */
1673e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1674e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1675e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1676e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1677e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1678e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1679e098bc96SEvan Quan 
1680e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1681e098bc96SEvan Quan 	} else {
1682e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1683e098bc96SEvan Quan 	}
1684e098bc96SEvan Quan 
1685e098bc96SEvan Quan 	return count;
1686e098bc96SEvan Quan }
1687e098bc96SEvan Quan 
1688e098bc96SEvan Quan /**
1689e098bc96SEvan Quan  * DOC: gpu_metrics
1690e098bc96SEvan Quan  *
1691e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1692e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1693e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1694e098bc96SEvan Quan  *
1695e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1696e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1697e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1698e098bc96SEvan Quan  * at the same time.
1699e098bc96SEvan Quan  */
1700e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1701e098bc96SEvan Quan 				      struct device_attribute *attr,
1702e098bc96SEvan Quan 				      char *buf)
1703e098bc96SEvan Quan {
1704e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17051348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1706e098bc96SEvan Quan 	void *gpu_metrics;
1707e098bc96SEvan Quan 	ssize_t size = 0;
1708e098bc96SEvan Quan 	int ret;
1709e098bc96SEvan Quan 
171053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1711e098bc96SEvan Quan 		return -EPERM;
1712d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1713d2ae842dSAlex Deucher 		return -EPERM;
1714e098bc96SEvan Quan 
1715e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1716e098bc96SEvan Quan 	if (ret < 0) {
1717e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1718e098bc96SEvan Quan 		return ret;
1719e098bc96SEvan Quan 	}
1720e098bc96SEvan Quan 
1721e098bc96SEvan Quan 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1722e098bc96SEvan Quan 	if (size <= 0)
1723e098bc96SEvan Quan 		goto out;
1724e098bc96SEvan Quan 
1725e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1726e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1727e098bc96SEvan Quan 
1728e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1729e098bc96SEvan Quan 
1730e098bc96SEvan Quan out:
1731e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1732e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1733e098bc96SEvan Quan 
1734e098bc96SEvan Quan 	return size;
1735e098bc96SEvan Quan }
1736e098bc96SEvan Quan 
1737494c1432SSathishkumar S static int amdgpu_device_read_powershift(struct amdgpu_device *adev,
1738494c1432SSathishkumar S 						uint32_t *ss_power, bool dgpu_share)
1739494c1432SSathishkumar S {
1740494c1432SSathishkumar S 	struct drm_device *ddev = adev_to_drm(adev);
1741494c1432SSathishkumar S 	uint32_t size;
1742494c1432SSathishkumar S 	int r = 0;
1743494c1432SSathishkumar S 
1744494c1432SSathishkumar S 	if (amdgpu_in_reset(adev))
1745494c1432SSathishkumar S 		return -EPERM;
1746494c1432SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
1747494c1432SSathishkumar S 		return -EPERM;
1748494c1432SSathishkumar S 
1749494c1432SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
1750494c1432SSathishkumar S 	if (r < 0) {
1751494c1432SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
1752494c1432SSathishkumar S 		return r;
1753494c1432SSathishkumar S 	}
1754494c1432SSathishkumar S 
1755494c1432SSathishkumar S 	if (dgpu_share)
1756494c1432SSathishkumar S 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1757494c1432SSathishkumar S 				   (void *)ss_power, &size);
1758494c1432SSathishkumar S 	else
1759494c1432SSathishkumar S 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1760494c1432SSathishkumar S 				   (void *)ss_power, &size);
1761494c1432SSathishkumar S 
1762494c1432SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
1763494c1432SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
1764494c1432SSathishkumar S 	return r;
1765494c1432SSathishkumar S }
1766494c1432SSathishkumar S 
1767494c1432SSathishkumar S static int amdgpu_show_powershift_percent(struct device *dev,
1768494c1432SSathishkumar S 					char *buf, bool dgpu_share)
1769494c1432SSathishkumar S {
1770494c1432SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1771494c1432SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1772494c1432SSathishkumar S 	uint32_t ss_power;
1773494c1432SSathishkumar S 	int r = 0, i;
1774494c1432SSathishkumar S 
1775494c1432SSathishkumar S 	r = amdgpu_device_read_powershift(adev, &ss_power, dgpu_share);
1776494c1432SSathishkumar S 	if (r == -EOPNOTSUPP) {
1777494c1432SSathishkumar S 		/* sensor not available on dGPU, try to read from APU */
1778494c1432SSathishkumar S 		adev = NULL;
1779494c1432SSathishkumar S 		mutex_lock(&mgpu_info.mutex);
1780494c1432SSathishkumar S 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1781494c1432SSathishkumar S 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1782494c1432SSathishkumar S 				adev = mgpu_info.gpu_ins[i].adev;
1783494c1432SSathishkumar S 				break;
1784494c1432SSathishkumar S 			}
1785494c1432SSathishkumar S 		}
1786494c1432SSathishkumar S 		mutex_unlock(&mgpu_info.mutex);
1787494c1432SSathishkumar S 		if (adev)
1788494c1432SSathishkumar S 			r = amdgpu_device_read_powershift(adev, &ss_power, dgpu_share);
1789494c1432SSathishkumar S 	}
1790494c1432SSathishkumar S 
1791494c1432SSathishkumar S 	if (!r)
1792494c1432SSathishkumar S 		r = sysfs_emit(buf, "%u%%\n", ss_power);
1793494c1432SSathishkumar S 
1794494c1432SSathishkumar S 	return r;
1795494c1432SSathishkumar S }
1796a7673a1cSSathishkumar S /**
1797a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1798a7673a1cSSathishkumar S  *
1799a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1800494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1801494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power
1802494c1432SSathishkumar S  * is shifted to APU, the percentage of boost is with respect to APU power
1803494c1432SSathishkumar S  * limit on the platform.
1804a7673a1cSSathishkumar S  */
1805a7673a1cSSathishkumar S 
1806a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1807a7673a1cSSathishkumar S 					       char *buf)
1808a7673a1cSSathishkumar S {
1809494c1432SSathishkumar S 	return amdgpu_show_powershift_percent(dev, buf, false);
1810a7673a1cSSathishkumar S }
1811a7673a1cSSathishkumar S 
1812a7673a1cSSathishkumar S /**
1813a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1814a7673a1cSSathishkumar S  *
1815494c1432SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting dGPU power
1816494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1817494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power is
1818494c1432SSathishkumar S  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1819494c1432SSathishkumar S  * limit on the platform.
1820a7673a1cSSathishkumar S  */
1821a7673a1cSSathishkumar S 
1822a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1823a7673a1cSSathishkumar S 						char *buf)
1824a7673a1cSSathishkumar S {
1825494c1432SSathishkumar S 	return amdgpu_show_powershift_percent(dev, buf, true);
1826a7673a1cSSathishkumar S }
1827a7673a1cSSathishkumar S 
182830d95a37SSathishkumar S /**
182930d95a37SSathishkumar S  * DOC: smartshift_bias
183030d95a37SSathishkumar S  *
183130d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
183230d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
183330d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
183430d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
183530d95a37SSathishkumar S  */
183630d95a37SSathishkumar S 
183730d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
183830d95a37SSathishkumar S 					  struct device_attribute *attr,
183930d95a37SSathishkumar S 					  char *buf)
184030d95a37SSathishkumar S {
184130d95a37SSathishkumar S 	int r = 0;
184230d95a37SSathishkumar S 
184330d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
184430d95a37SSathishkumar S 
184530d95a37SSathishkumar S 	return r;
184630d95a37SSathishkumar S }
184730d95a37SSathishkumar S 
184830d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
184930d95a37SSathishkumar S 					  struct device_attribute *attr,
185030d95a37SSathishkumar S 					  const char *buf, size_t count)
185130d95a37SSathishkumar S {
185230d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
185330d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
185430d95a37SSathishkumar S 	int r = 0;
185530d95a37SSathishkumar S 	int bias = 0;
185630d95a37SSathishkumar S 
185730d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
185830d95a37SSathishkumar S 		return -EPERM;
185930d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
186030d95a37SSathishkumar S 		return -EPERM;
186130d95a37SSathishkumar S 
186230d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
186330d95a37SSathishkumar S 	if (r < 0) {
186430d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
186530d95a37SSathishkumar S 		return r;
186630d95a37SSathishkumar S 	}
186730d95a37SSathishkumar S 
186830d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
186930d95a37SSathishkumar S 	if (r)
187030d95a37SSathishkumar S 		goto out;
187130d95a37SSathishkumar S 
187230d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
187330d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
187430d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
187530d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
187630d95a37SSathishkumar S 
187730d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
187830d95a37SSathishkumar S 	r = count;
187930d95a37SSathishkumar S 
1880bd4b9bb7SJulia Lawall 	/* TODO: update bias level with SMU message */
188130d95a37SSathishkumar S 
188230d95a37SSathishkumar S out:
188330d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
188430d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
188530d95a37SSathishkumar S 	return r;
188630d95a37SSathishkumar S }
188730d95a37SSathishkumar S 
188830d95a37SSathishkumar S 
1889a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1890a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
1891a7673a1cSSathishkumar S {
1892494c1432SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1893a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1894a7673a1cSSathishkumar S 
1895a7673a1cSSathishkumar S 	return 0;
1896a7673a1cSSathishkumar S }
1897a7673a1cSSathishkumar S 
189830d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
189930d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
190030d95a37SSathishkumar S {
190130d95a37SSathishkumar S 	uint32_t ss_power, size;
190230d95a37SSathishkumar S 
190330d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
190430d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
190530d95a37SSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
190630d95a37SSathishkumar S 		 (void *)&ss_power, &size))
190730d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
190830d95a37SSathishkumar S 	else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
190930d95a37SSathishkumar S 		 (void *)&ss_power, &size))
191030d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
191130d95a37SSathishkumar S 
191230d95a37SSathishkumar S 	return 0;
191330d95a37SSathishkumar S }
191430d95a37SSathishkumar S 
1915e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1916e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19174215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19187884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19197884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19207884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19217884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1922e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1923e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1924e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1925e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19269577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19279577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1928f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1929f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1930e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
1931e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
1932ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1933e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
1934ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1935ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1936e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
1937ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1938ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1939ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1940ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1941a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
1942a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
1943a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
1944a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
194530d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
194630d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
1947e098bc96SEvan Quan };
1948e098bc96SEvan Quan 
1949e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1950e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
1951e098bc96SEvan Quan {
1952e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
19538ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
19548ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
1955e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
1956e098bc96SEvan Quan 
1957e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
1958e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
1959e098bc96SEvan Quan 		return 0;
1960e098bc96SEvan Quan 	}
1961e098bc96SEvan Quan 
1962e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
1963e098bc96SEvan Quan 
1964e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
19658ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0))
1966e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
1967e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
19688ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0) ||
19698ecad8d6SLijo Lazar 		    gc_ver == IP_VERSION(9, 4, 1) ||
19708ecad8d6SLijo Lazar 		    gc_ver == IP_VERSION(9, 4, 2))
1971e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
1972e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
19738ecad8d6SLijo Lazar 		if (mp1_ver < IP_VERSION(10, 0, 0))
1974e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
1975e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
1976e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
197779c65f3fSEvan Quan 		if (amdgpu_dpm_is_overdrive_supported(adev))
1978e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
1979e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
19808ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1))
1981e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
1982e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
1983e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
1984e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
1985e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
1986e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
198760044748SKent Russell 		switch (gc_ver) {
198860044748SKent Russell 		case IP_VERSION(9, 0, 1):
198960044748SKent Russell 		case IP_VERSION(9, 4, 0):
199060044748SKent Russell 		case IP_VERSION(9, 4, 1):
199160044748SKent Russell 		case IP_VERSION(9, 4, 2):
1992ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
1993276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
1994*35e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
1995*35e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
199660044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
199760044748SKent Russell 			break;
199860044748SKent Russell 		default:
1999e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
200060044748SKent Russell 		}
2001e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
20028ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver < IP_VERSION(9, 0, 0))
2003e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2004e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
20058ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2006e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
20079577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
20088ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2009a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
201064440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
20113929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
20122f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
20132f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 3)))
20149577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
20159577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
20168ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2017a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
201864440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
20193929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
20202f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
20212f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 3)))
20229577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
2023a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
202479c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2025a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
20261b852572SDanijel Slivka 		else if (gc_ver == IP_VERSION(10, 3, 0) && amdgpu_sriov_vf(adev))
20271b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
2028e098bc96SEvan Quan 	}
2029e098bc96SEvan Quan 
20308ecad8d6SLijo Lazar 	switch (gc_ver) {
20318ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 1):
20328ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 2):
20331d0e622fSKevin Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2034e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2035e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2036e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2037e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2038e098bc96SEvan Quan 			dev_attr->store = NULL;
2039e098bc96SEvan Quan 		}
20401d0e622fSKevin Wang 		break;
20411b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
20421b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
20431b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
20441b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
20451b852572SDanijel Slivka 			dev_attr->store = NULL;
20461b852572SDanijel Slivka 		}
20471b852572SDanijel Slivka 		break;
20481d0e622fSKevin Wang 	default:
20491d0e622fSKevin Wang 		break;
2050e098bc96SEvan Quan 	}
2051e098bc96SEvan Quan 
2052ede14a1bSDarren Powell 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2053ede14a1bSDarren Powell 		/* SMU MP1 does not support dcefclk level setting */
20548ecad8d6SLijo Lazar 		if (gc_ver >= IP_VERSION(10, 0, 0)) {
2055ede14a1bSDarren Powell 			dev_attr->attr.mode &= ~S_IWUGO;
2056ede14a1bSDarren Powell 			dev_attr->store = NULL;
2057ede14a1bSDarren Powell 		}
2058ede14a1bSDarren Powell 	}
2059ede14a1bSDarren Powell 
2060e610941cSYiqing Yao 	/* setting should not be allowed from VF if not in one VF mode */
2061e610941cSYiqing Yao 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
206211c9cc95SMarina Nikolic 		dev_attr->attr.mode &= ~S_IWUGO;
206311c9cc95SMarina Nikolic 		dev_attr->store = NULL;
206411c9cc95SMarina Nikolic 	}
206511c9cc95SMarina Nikolic 
2066e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2067e098bc96SEvan Quan 
2068e098bc96SEvan Quan 	return 0;
2069e098bc96SEvan Quan }
2070e098bc96SEvan Quan 
2071e098bc96SEvan Quan 
2072e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2073e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2074e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2075e098bc96SEvan Quan {
2076e098bc96SEvan Quan 	int ret = 0;
2077e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2078e098bc96SEvan Quan 	const char *name = dev_attr->attr.name;
2079e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2080e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
2081e098bc96SEvan Quan 
2082e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2083e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2084e098bc96SEvan Quan 
2085e098bc96SEvan Quan 	BUG_ON(!attr);
2086e098bc96SEvan Quan 
20878a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2088e098bc96SEvan Quan 
2089e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2090e098bc96SEvan Quan 	if (ret) {
2091e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2092e098bc96SEvan Quan 			name, ret);
2093e098bc96SEvan Quan 		return ret;
2094e098bc96SEvan Quan 	}
2095e098bc96SEvan Quan 
2096e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2097e098bc96SEvan Quan 		return 0;
2098e098bc96SEvan Quan 
2099e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2100e098bc96SEvan Quan 	if (ret) {
2101e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2102e098bc96SEvan Quan 			name, ret);
2103e098bc96SEvan Quan 	}
2104e098bc96SEvan Quan 
2105e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2106e098bc96SEvan Quan 	if (!attr_entry)
2107e098bc96SEvan Quan 		return -ENOMEM;
2108e098bc96SEvan Quan 
2109e098bc96SEvan Quan 	attr_entry->attr = attr;
2110e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2111e098bc96SEvan Quan 
2112e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2113e098bc96SEvan Quan 
2114e098bc96SEvan Quan 	return ret;
2115e098bc96SEvan Quan }
2116e098bc96SEvan Quan 
2117e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2118e098bc96SEvan Quan {
2119e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2120e098bc96SEvan Quan 
2121e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2122e098bc96SEvan Quan }
2123e098bc96SEvan Quan 
2124e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2125e098bc96SEvan Quan 					     struct list_head *attr_list);
2126e098bc96SEvan Quan 
2127e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2128e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2129e098bc96SEvan Quan 					    uint32_t counts,
2130e098bc96SEvan Quan 					    uint32_t mask,
2131e098bc96SEvan Quan 					    struct list_head *attr_list)
2132e098bc96SEvan Quan {
2133e098bc96SEvan Quan 	int ret = 0;
2134e098bc96SEvan Quan 	uint32_t i = 0;
2135e098bc96SEvan Quan 
2136e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2137e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2138e098bc96SEvan Quan 		if (ret)
2139e098bc96SEvan Quan 			goto failed;
2140e098bc96SEvan Quan 	}
2141e098bc96SEvan Quan 
2142e098bc96SEvan Quan 	return 0;
2143e098bc96SEvan Quan 
2144e098bc96SEvan Quan failed:
2145e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2146e098bc96SEvan Quan 
2147e098bc96SEvan Quan 	return ret;
2148e098bc96SEvan Quan }
2149e098bc96SEvan Quan 
2150e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2151e098bc96SEvan Quan 					     struct list_head *attr_list)
2152e098bc96SEvan Quan {
2153e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2154e098bc96SEvan Quan 
2155e098bc96SEvan Quan 	if (list_empty(attr_list))
2156e098bc96SEvan Quan 		return ;
2157e098bc96SEvan Quan 
2158e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2159e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2160e098bc96SEvan Quan 		list_del(&entry->entry);
2161e098bc96SEvan Quan 		kfree(entry);
2162e098bc96SEvan Quan 	}
2163e098bc96SEvan Quan }
2164e098bc96SEvan Quan 
2165e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2166e098bc96SEvan Quan 				      struct device_attribute *attr,
2167e098bc96SEvan Quan 				      char *buf)
2168e098bc96SEvan Quan {
2169e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2170e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2171e098bc96SEvan Quan 	int r, temp = 0, size = sizeof(temp);
2172e098bc96SEvan Quan 
217353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2174e098bc96SEvan Quan 		return -EPERM;
2175d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2176d2ae842dSAlex Deucher 		return -EPERM;
2177e098bc96SEvan Quan 
2178e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2179e098bc96SEvan Quan 		return -EINVAL;
2180e098bc96SEvan Quan 
21814a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2182e098bc96SEvan Quan 	if (r < 0) {
21834a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2184e098bc96SEvan Quan 		return r;
2185e098bc96SEvan Quan 	}
2186e098bc96SEvan Quan 
2187e098bc96SEvan Quan 	switch (channel) {
2188e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2189e098bc96SEvan Quan 		/* get current junction temperature */
2190e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2191e098bc96SEvan Quan 					   (void *)&temp, &size);
2192e098bc96SEvan Quan 		break;
2193e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2194e098bc96SEvan Quan 		/* get current edge temperature */
2195e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2196e098bc96SEvan Quan 					   (void *)&temp, &size);
2197e098bc96SEvan Quan 		break;
2198e098bc96SEvan Quan 	case PP_TEMP_MEM:
2199e098bc96SEvan Quan 		/* get current memory temperature */
2200e098bc96SEvan Quan 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2201e098bc96SEvan Quan 					   (void *)&temp, &size);
2202e098bc96SEvan Quan 		break;
2203e098bc96SEvan Quan 	default:
2204e098bc96SEvan Quan 		r = -EINVAL;
2205e098bc96SEvan Quan 		break;
2206e098bc96SEvan Quan 	}
2207e098bc96SEvan Quan 
22084a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
22094a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2210e098bc96SEvan Quan 
2211e098bc96SEvan Quan 	if (r)
2212e098bc96SEvan Quan 		return r;
2213e098bc96SEvan Quan 
2214a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2215e098bc96SEvan Quan }
2216e098bc96SEvan Quan 
2217e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2218e098bc96SEvan Quan 					     struct device_attribute *attr,
2219e098bc96SEvan Quan 					     char *buf)
2220e098bc96SEvan Quan {
2221e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2222e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2223e098bc96SEvan Quan 	int temp;
2224e098bc96SEvan Quan 
2225e098bc96SEvan Quan 	if (hyst)
2226e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2227e098bc96SEvan Quan 	else
2228e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2229e098bc96SEvan Quan 
2230a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2231e098bc96SEvan Quan }
2232e098bc96SEvan Quan 
2233e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2234e098bc96SEvan Quan 					     struct device_attribute *attr,
2235e098bc96SEvan Quan 					     char *buf)
2236e098bc96SEvan Quan {
2237e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2238e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2239e098bc96SEvan Quan 	int temp;
2240e098bc96SEvan Quan 
2241e098bc96SEvan Quan 	if (hyst)
2242e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2243e098bc96SEvan Quan 	else
2244e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2245e098bc96SEvan Quan 
2246a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2247e098bc96SEvan Quan }
2248e098bc96SEvan Quan 
2249e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2250e098bc96SEvan Quan 					     struct device_attribute *attr,
2251e098bc96SEvan Quan 					     char *buf)
2252e098bc96SEvan Quan {
2253e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2254e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2255e098bc96SEvan Quan 	int temp;
2256e098bc96SEvan Quan 
2257e098bc96SEvan Quan 	if (hyst)
2258e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2259e098bc96SEvan Quan 	else
2260e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2261e098bc96SEvan Quan 
2262a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2263e098bc96SEvan Quan }
2264e098bc96SEvan Quan 
2265e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2266e098bc96SEvan Quan 					     struct device_attribute *attr,
2267e098bc96SEvan Quan 					     char *buf)
2268e098bc96SEvan Quan {
2269e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2270e098bc96SEvan Quan 
2271e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2272e098bc96SEvan Quan 		return -EINVAL;
2273e098bc96SEvan Quan 
2274a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2275e098bc96SEvan Quan }
2276e098bc96SEvan Quan 
2277e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2278e098bc96SEvan Quan 					     struct device_attribute *attr,
2279e098bc96SEvan Quan 					     char *buf)
2280e098bc96SEvan Quan {
2281e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2282e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2283e098bc96SEvan Quan 	int temp = 0;
2284e098bc96SEvan Quan 
2285e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2286e098bc96SEvan Quan 		return -EINVAL;
2287e098bc96SEvan Quan 
2288e098bc96SEvan Quan 	switch (channel) {
2289e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2290e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2291e098bc96SEvan Quan 		break;
2292e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2293e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2294e098bc96SEvan Quan 		break;
2295e098bc96SEvan Quan 	case PP_TEMP_MEM:
2296e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2297e098bc96SEvan Quan 		break;
2298e098bc96SEvan Quan 	}
2299e098bc96SEvan Quan 
2300a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2301e098bc96SEvan Quan }
2302e098bc96SEvan Quan 
2303e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2304e098bc96SEvan Quan 					    struct device_attribute *attr,
2305e098bc96SEvan Quan 					    char *buf)
2306e098bc96SEvan Quan {
2307e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2308e098bc96SEvan Quan 	u32 pwm_mode = 0;
2309e098bc96SEvan Quan 	int ret;
2310e098bc96SEvan Quan 
231153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2312e098bc96SEvan Quan 		return -EPERM;
2313d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2314d2ae842dSAlex Deucher 		return -EPERM;
2315e098bc96SEvan Quan 
23164a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2317e098bc96SEvan Quan 	if (ret < 0) {
23184a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2319e098bc96SEvan Quan 		return ret;
2320e098bc96SEvan Quan 	}
2321e098bc96SEvan Quan 
232279c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
232379c65f3fSEvan Quan 
23244a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
23254a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
232679c65f3fSEvan Quan 
232779c65f3fSEvan Quan 	if (ret)
2328e098bc96SEvan Quan 		return -EINVAL;
2329e098bc96SEvan Quan 
2330fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2331e098bc96SEvan Quan }
2332e098bc96SEvan Quan 
2333e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2334e098bc96SEvan Quan 					    struct device_attribute *attr,
2335e098bc96SEvan Quan 					    const char *buf,
2336e098bc96SEvan Quan 					    size_t count)
2337e098bc96SEvan Quan {
2338e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2339e098bc96SEvan Quan 	int err, ret;
2340e098bc96SEvan Quan 	int value;
2341e098bc96SEvan Quan 
234253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2343e098bc96SEvan Quan 		return -EPERM;
2344d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2345d2ae842dSAlex Deucher 		return -EPERM;
2346e098bc96SEvan Quan 
2347e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2348e098bc96SEvan Quan 	if (err)
2349e098bc96SEvan Quan 		return err;
2350e098bc96SEvan Quan 
23514a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2352e098bc96SEvan Quan 	if (ret < 0) {
23534a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2354e098bc96SEvan Quan 		return ret;
2355e098bc96SEvan Quan 	}
2356e098bc96SEvan Quan 
235779c65f3fSEvan Quan 	ret = amdgpu_dpm_set_fan_control_mode(adev, value);
235879c65f3fSEvan Quan 
23594a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
23604a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
236179c65f3fSEvan Quan 
236279c65f3fSEvan Quan 	if (ret)
2363e098bc96SEvan Quan 		return -EINVAL;
2364e098bc96SEvan Quan 
2365e098bc96SEvan Quan 	return count;
2366e098bc96SEvan Quan }
2367e098bc96SEvan Quan 
2368e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2369e098bc96SEvan Quan 					 struct device_attribute *attr,
2370e098bc96SEvan Quan 					 char *buf)
2371e098bc96SEvan Quan {
2372fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2373e098bc96SEvan Quan }
2374e098bc96SEvan Quan 
2375e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2376e098bc96SEvan Quan 					 struct device_attribute *attr,
2377e098bc96SEvan Quan 					 char *buf)
2378e098bc96SEvan Quan {
2379fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2380e098bc96SEvan Quan }
2381e098bc96SEvan Quan 
2382e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2383e098bc96SEvan Quan 				     struct device_attribute *attr,
2384e098bc96SEvan Quan 				     const char *buf, size_t count)
2385e098bc96SEvan Quan {
2386e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2387e098bc96SEvan Quan 	int err;
2388e098bc96SEvan Quan 	u32 value;
2389e098bc96SEvan Quan 	u32 pwm_mode;
2390e098bc96SEvan Quan 
239153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2392e098bc96SEvan Quan 		return -EPERM;
2393d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2394d2ae842dSAlex Deucher 		return -EPERM;
2395e098bc96SEvan Quan 
239679c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
239779c65f3fSEvan Quan 	if (err)
239879c65f3fSEvan Quan 		return err;
239979c65f3fSEvan Quan 
24004a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2401e098bc96SEvan Quan 	if (err < 0) {
24024a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2403e098bc96SEvan Quan 		return err;
2404e098bc96SEvan Quan 	}
2405e098bc96SEvan Quan 
240679c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
240779c65f3fSEvan Quan 	if (err)
240879c65f3fSEvan Quan 		goto out;
240979c65f3fSEvan Quan 
2410e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2411e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2412e098bc96SEvan Quan 		err = -EINVAL;
241379c65f3fSEvan Quan 		goto out;
241479c65f3fSEvan Quan 	}
2415e098bc96SEvan Quan 
241679c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
241779c65f3fSEvan Quan 
241879c65f3fSEvan Quan out:
24194a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24204a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2421e098bc96SEvan Quan 
2422e098bc96SEvan Quan 	if (err)
2423e098bc96SEvan Quan 		return err;
2424e098bc96SEvan Quan 
2425e098bc96SEvan Quan 	return count;
2426e098bc96SEvan Quan }
2427e098bc96SEvan Quan 
2428e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2429e098bc96SEvan Quan 				     struct device_attribute *attr,
2430e098bc96SEvan Quan 				     char *buf)
2431e098bc96SEvan Quan {
2432e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2433e098bc96SEvan Quan 	int err;
2434e098bc96SEvan Quan 	u32 speed = 0;
2435e098bc96SEvan Quan 
243653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2437e098bc96SEvan Quan 		return -EPERM;
2438d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2439d2ae842dSAlex Deucher 		return -EPERM;
2440e098bc96SEvan Quan 
24414a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2442e098bc96SEvan Quan 	if (err < 0) {
24434a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2444e098bc96SEvan Quan 		return err;
2445e098bc96SEvan Quan 	}
2446e098bc96SEvan Quan 
24470d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2448e098bc96SEvan Quan 
24494a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24504a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2451e098bc96SEvan Quan 
2452e098bc96SEvan Quan 	if (err)
2453e098bc96SEvan Quan 		return err;
2454e098bc96SEvan Quan 
2455fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2456e098bc96SEvan Quan }
2457e098bc96SEvan Quan 
2458e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2459e098bc96SEvan Quan 					   struct device_attribute *attr,
2460e098bc96SEvan Quan 					   char *buf)
2461e098bc96SEvan Quan {
2462e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2463e098bc96SEvan Quan 	int err;
2464e098bc96SEvan Quan 	u32 speed = 0;
2465e098bc96SEvan Quan 
246653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2467e098bc96SEvan Quan 		return -EPERM;
2468d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2469d2ae842dSAlex Deucher 		return -EPERM;
2470e098bc96SEvan Quan 
24714a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2472e098bc96SEvan Quan 	if (err < 0) {
24734a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2474e098bc96SEvan Quan 		return err;
2475e098bc96SEvan Quan 	}
2476e098bc96SEvan Quan 
2477e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2478e098bc96SEvan Quan 
24794a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24804a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2481e098bc96SEvan Quan 
2482e098bc96SEvan Quan 	if (err)
2483e098bc96SEvan Quan 		return err;
2484e098bc96SEvan Quan 
2485fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2486e098bc96SEvan Quan }
2487e098bc96SEvan Quan 
2488e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2489e098bc96SEvan Quan 					 struct device_attribute *attr,
2490e098bc96SEvan Quan 					 char *buf)
2491e098bc96SEvan Quan {
2492e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2493e098bc96SEvan Quan 	u32 min_rpm = 0;
2494e098bc96SEvan Quan 	u32 size = sizeof(min_rpm);
2495e098bc96SEvan Quan 	int r;
2496e098bc96SEvan Quan 
249753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2498e098bc96SEvan Quan 		return -EPERM;
2499d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2500d2ae842dSAlex Deucher 		return -EPERM;
2501e098bc96SEvan Quan 
25024a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2503e098bc96SEvan Quan 	if (r < 0) {
25044a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2505e098bc96SEvan Quan 		return r;
2506e098bc96SEvan Quan 	}
2507e098bc96SEvan Quan 
2508e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2509e098bc96SEvan Quan 				   (void *)&min_rpm, &size);
2510e098bc96SEvan Quan 
25114a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25124a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2513e098bc96SEvan Quan 
2514e098bc96SEvan Quan 	if (r)
2515e098bc96SEvan Quan 		return r;
2516e098bc96SEvan Quan 
2517a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2518e098bc96SEvan Quan }
2519e098bc96SEvan Quan 
2520e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2521e098bc96SEvan Quan 					 struct device_attribute *attr,
2522e098bc96SEvan Quan 					 char *buf)
2523e098bc96SEvan Quan {
2524e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2525e098bc96SEvan Quan 	u32 max_rpm = 0;
2526e098bc96SEvan Quan 	u32 size = sizeof(max_rpm);
2527e098bc96SEvan Quan 	int r;
2528e098bc96SEvan Quan 
252953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2530e098bc96SEvan Quan 		return -EPERM;
2531d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2532d2ae842dSAlex Deucher 		return -EPERM;
2533e098bc96SEvan Quan 
25344a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2535e098bc96SEvan Quan 	if (r < 0) {
25364a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2537e098bc96SEvan Quan 		return r;
2538e098bc96SEvan Quan 	}
2539e098bc96SEvan Quan 
2540e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2541e098bc96SEvan Quan 				   (void *)&max_rpm, &size);
2542e098bc96SEvan Quan 
25434a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25444a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2545e098bc96SEvan Quan 
2546e098bc96SEvan Quan 	if (r)
2547e098bc96SEvan Quan 		return r;
2548e098bc96SEvan Quan 
2549a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2550e098bc96SEvan Quan }
2551e098bc96SEvan Quan 
2552e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2553e098bc96SEvan Quan 					   struct device_attribute *attr,
2554e098bc96SEvan Quan 					   char *buf)
2555e098bc96SEvan Quan {
2556e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2557e098bc96SEvan Quan 	int err;
2558e098bc96SEvan Quan 	u32 rpm = 0;
2559e098bc96SEvan Quan 
256053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2561e098bc96SEvan Quan 		return -EPERM;
2562d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2563d2ae842dSAlex Deucher 		return -EPERM;
2564e098bc96SEvan Quan 
25654a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2566e098bc96SEvan Quan 	if (err < 0) {
25674a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2568e098bc96SEvan Quan 		return err;
2569e098bc96SEvan Quan 	}
2570e098bc96SEvan Quan 
2571e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2572e098bc96SEvan Quan 
25734a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25744a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2575e098bc96SEvan Quan 
2576e098bc96SEvan Quan 	if (err)
2577e098bc96SEvan Quan 		return err;
2578e098bc96SEvan Quan 
2579fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2580e098bc96SEvan Quan }
2581e098bc96SEvan Quan 
2582e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2583e098bc96SEvan Quan 				     struct device_attribute *attr,
2584e098bc96SEvan Quan 				     const char *buf, size_t count)
2585e098bc96SEvan Quan {
2586e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2587e098bc96SEvan Quan 	int err;
2588e098bc96SEvan Quan 	u32 value;
2589e098bc96SEvan Quan 	u32 pwm_mode;
2590e098bc96SEvan Quan 
259153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2592e098bc96SEvan Quan 		return -EPERM;
2593d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2594d2ae842dSAlex Deucher 		return -EPERM;
2595e098bc96SEvan Quan 
259679c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
259779c65f3fSEvan Quan 	if (err)
259879c65f3fSEvan Quan 		return err;
259979c65f3fSEvan Quan 
26004a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2601e098bc96SEvan Quan 	if (err < 0) {
26024a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2603e098bc96SEvan Quan 		return err;
2604e098bc96SEvan Quan 	}
2605e098bc96SEvan Quan 
260679c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
260779c65f3fSEvan Quan 	if (err)
260879c65f3fSEvan Quan 		goto out;
2609e098bc96SEvan Quan 
2610e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
261179c65f3fSEvan Quan 		err = -ENODATA;
261279c65f3fSEvan Quan 		goto out;
2613e098bc96SEvan Quan 	}
2614e098bc96SEvan Quan 
2615e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2616e098bc96SEvan Quan 
261779c65f3fSEvan Quan out:
26184a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26194a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2620e098bc96SEvan Quan 
2621e098bc96SEvan Quan 	if (err)
2622e098bc96SEvan Quan 		return err;
2623e098bc96SEvan Quan 
2624e098bc96SEvan Quan 	return count;
2625e098bc96SEvan Quan }
2626e098bc96SEvan Quan 
2627e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2628e098bc96SEvan Quan 					    struct device_attribute *attr,
2629e098bc96SEvan Quan 					    char *buf)
2630e098bc96SEvan Quan {
2631e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2632e098bc96SEvan Quan 	u32 pwm_mode = 0;
2633e098bc96SEvan Quan 	int ret;
2634e098bc96SEvan Quan 
263553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2636e098bc96SEvan Quan 		return -EPERM;
2637d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2638d2ae842dSAlex Deucher 		return -EPERM;
2639e098bc96SEvan Quan 
26404a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2641e098bc96SEvan Quan 	if (ret < 0) {
26424a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2643e098bc96SEvan Quan 		return ret;
2644e098bc96SEvan Quan 	}
2645e098bc96SEvan Quan 
264679c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
264779c65f3fSEvan Quan 
26484a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26494a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
265079c65f3fSEvan Quan 
265179c65f3fSEvan Quan 	if (ret)
2652e098bc96SEvan Quan 		return -EINVAL;
2653e098bc96SEvan Quan 
2654fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2655e098bc96SEvan Quan }
2656e098bc96SEvan Quan 
2657e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2658e098bc96SEvan Quan 					    struct device_attribute *attr,
2659e098bc96SEvan Quan 					    const char *buf,
2660e098bc96SEvan Quan 					    size_t count)
2661e098bc96SEvan Quan {
2662e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2663e098bc96SEvan Quan 	int err;
2664e098bc96SEvan Quan 	int value;
2665e098bc96SEvan Quan 	u32 pwm_mode;
2666e098bc96SEvan Quan 
266753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2668e098bc96SEvan Quan 		return -EPERM;
2669d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2670d2ae842dSAlex Deucher 		return -EPERM;
2671e098bc96SEvan Quan 
2672e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2673e098bc96SEvan Quan 	if (err)
2674e098bc96SEvan Quan 		return err;
2675e098bc96SEvan Quan 
2676e098bc96SEvan Quan 	if (value == 0)
2677e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2678e098bc96SEvan Quan 	else if (value == 1)
2679e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2680e098bc96SEvan Quan 	else
2681e098bc96SEvan Quan 		return -EINVAL;
2682e098bc96SEvan Quan 
26834a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2684e098bc96SEvan Quan 	if (err < 0) {
26854a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2686e098bc96SEvan Quan 		return err;
2687e098bc96SEvan Quan 	}
2688e098bc96SEvan Quan 
268979c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2690e098bc96SEvan Quan 
26914a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26924a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2693e098bc96SEvan Quan 
269479c65f3fSEvan Quan 	if (err)
269579c65f3fSEvan Quan 		return -EINVAL;
269679c65f3fSEvan Quan 
2697e098bc96SEvan Quan 	return count;
2698e098bc96SEvan Quan }
2699e098bc96SEvan Quan 
2700e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2701e098bc96SEvan Quan 					struct device_attribute *attr,
2702e098bc96SEvan Quan 					char *buf)
2703e098bc96SEvan Quan {
2704e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2705e098bc96SEvan Quan 	u32 vddgfx;
2706e098bc96SEvan Quan 	int r, size = sizeof(vddgfx);
2707e098bc96SEvan Quan 
270853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2709e098bc96SEvan Quan 		return -EPERM;
2710d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2711d2ae842dSAlex Deucher 		return -EPERM;
2712e098bc96SEvan Quan 
27134a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2714e098bc96SEvan Quan 	if (r < 0) {
27154a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2716e098bc96SEvan Quan 		return r;
2717e098bc96SEvan Quan 	}
2718e098bc96SEvan Quan 
2719e098bc96SEvan Quan 	/* get the voltage */
2720e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2721e098bc96SEvan Quan 				   (void *)&vddgfx, &size);
2722e098bc96SEvan Quan 
27234a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27244a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2725e098bc96SEvan Quan 
2726e098bc96SEvan Quan 	if (r)
2727e098bc96SEvan Quan 		return r;
2728e098bc96SEvan Quan 
2729a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
2730e098bc96SEvan Quan }
2731e098bc96SEvan Quan 
2732e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2733e098bc96SEvan Quan 					      struct device_attribute *attr,
2734e098bc96SEvan Quan 					      char *buf)
2735e098bc96SEvan Quan {
2736a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
2737e098bc96SEvan Quan }
2738e098bc96SEvan Quan 
2739e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2740e098bc96SEvan Quan 				       struct device_attribute *attr,
2741e098bc96SEvan Quan 				       char *buf)
2742e098bc96SEvan Quan {
2743e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2744e098bc96SEvan Quan 	u32 vddnb;
2745e098bc96SEvan Quan 	int r, size = sizeof(vddnb);
2746e098bc96SEvan Quan 
274753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2748e098bc96SEvan Quan 		return -EPERM;
2749d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2750d2ae842dSAlex Deucher 		return -EPERM;
2751e098bc96SEvan Quan 
2752e098bc96SEvan Quan 	/* only APUs have vddnb */
2753e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2754e098bc96SEvan Quan 		return -EINVAL;
2755e098bc96SEvan Quan 
27564a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2757e098bc96SEvan Quan 	if (r < 0) {
27584a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2759e098bc96SEvan Quan 		return r;
2760e098bc96SEvan Quan 	}
2761e098bc96SEvan Quan 
2762e098bc96SEvan Quan 	/* get the voltage */
2763e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2764e098bc96SEvan Quan 				   (void *)&vddnb, &size);
2765e098bc96SEvan Quan 
27664a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27674a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2768e098bc96SEvan Quan 
2769e098bc96SEvan Quan 	if (r)
2770e098bc96SEvan Quan 		return r;
2771e098bc96SEvan Quan 
2772a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
2773e098bc96SEvan Quan }
2774e098bc96SEvan Quan 
2775e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2776e098bc96SEvan Quan 					      struct device_attribute *attr,
2777e098bc96SEvan Quan 					      char *buf)
2778e098bc96SEvan Quan {
2779a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
2780e098bc96SEvan Quan }
2781e098bc96SEvan Quan 
2782e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2783e098bc96SEvan Quan 					   struct device_attribute *attr,
2784e098bc96SEvan Quan 					   char *buf)
2785e098bc96SEvan Quan {
2786e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2787e098bc96SEvan Quan 	u32 query = 0;
2788e098bc96SEvan Quan 	int r, size = sizeof(u32);
2789e098bc96SEvan Quan 	unsigned uw;
2790e098bc96SEvan Quan 
279153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2792e098bc96SEvan Quan 		return -EPERM;
2793d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2794d2ae842dSAlex Deucher 		return -EPERM;
2795e098bc96SEvan Quan 
27964a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2797e098bc96SEvan Quan 	if (r < 0) {
27984a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2799e098bc96SEvan Quan 		return r;
2800e098bc96SEvan Quan 	}
2801e098bc96SEvan Quan 
2802e098bc96SEvan Quan 	/* get the voltage */
2803e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2804e098bc96SEvan Quan 				   (void *)&query, &size);
2805e098bc96SEvan Quan 
28064a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28074a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2808e098bc96SEvan Quan 
2809e098bc96SEvan Quan 	if (r)
2810e098bc96SEvan Quan 		return r;
2811e098bc96SEvan Quan 
2812e098bc96SEvan Quan 	/* convert to microwatts */
2813e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2814e098bc96SEvan Quan 
2815a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", uw);
2816e098bc96SEvan Quan }
2817e098bc96SEvan Quan 
2818e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2819e098bc96SEvan Quan 					 struct device_attribute *attr,
2820e098bc96SEvan Quan 					 char *buf)
2821e098bc96SEvan Quan {
2822fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2823e098bc96SEvan Quan }
2824e098bc96SEvan Quan 
282591161b06SDarren Powell 
282691161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2827e098bc96SEvan Quan 					struct device_attribute *attr,
282891161b06SDarren Powell 					char *buf,
282991161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
2830e098bc96SEvan Quan {
2831e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2832a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2833a40a020dSDarren Powell 	uint32_t limit;
2834e098bc96SEvan Quan 	ssize_t size;
2835e098bc96SEvan Quan 	int r;
2836e098bc96SEvan Quan 
283753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2838e098bc96SEvan Quan 		return -EPERM;
2839d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2840d2ae842dSAlex Deucher 		return -EPERM;
2841e098bc96SEvan Quan 
28424a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2843e098bc96SEvan Quan 	if (r < 0) {
28444a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2845e098bc96SEvan Quan 		return r;
2846e098bc96SEvan Quan 	}
2847e098bc96SEvan Quan 
284879c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
284904bec521SDarren Powell 				      pp_limit_level, power_type);
2850dc2a8240SDarren Powell 
2851dc2a8240SDarren Powell 	if (!r)
285209b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2853dc2a8240SDarren Powell 	else
285409b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
2855e098bc96SEvan Quan 
28564a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28574a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2858e098bc96SEvan Quan 
2859e098bc96SEvan Quan 	return size;
2860e098bc96SEvan Quan }
2861e098bc96SEvan Quan 
286291161b06SDarren Powell 
286391161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
286491161b06SDarren Powell 					 struct device_attribute *attr,
286591161b06SDarren Powell 					 char *buf)
286691161b06SDarren Powell {
286791161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
286891161b06SDarren Powell 
286991161b06SDarren Powell }
287091161b06SDarren Powell 
2871e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2872e098bc96SEvan Quan 					 struct device_attribute *attr,
2873e098bc96SEvan Quan 					 char *buf)
2874e098bc96SEvan Quan {
287591161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2876e098bc96SEvan Quan 
2877e098bc96SEvan Quan }
2878e098bc96SEvan Quan 
28796e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
28806e58941cSEric Huang 					 struct device_attribute *attr,
28816e58941cSEric Huang 					 char *buf)
28826e58941cSEric Huang {
288391161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
28846e58941cSEric Huang 
28856e58941cSEric Huang }
28866e58941cSEric Huang 
2887ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2888ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
2889ae07970aSXiaomeng Hou 					 char *buf)
2890ae07970aSXiaomeng Hou {
28913b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
28928ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2893ae07970aSXiaomeng Hou 
28948ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
2895a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
28963b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
28973b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
28983b99e8e3SYang Wang 	else
28993b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
2900ae07970aSXiaomeng Hou }
2901e098bc96SEvan Quan 
2902e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2903e098bc96SEvan Quan 		struct device_attribute *attr,
2904e098bc96SEvan Quan 		const char *buf,
2905e098bc96SEvan Quan 		size_t count)
2906e098bc96SEvan Quan {
2907e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2908ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
2909e098bc96SEvan Quan 	int err;
2910e098bc96SEvan Quan 	u32 value;
2911e098bc96SEvan Quan 
291253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2913e098bc96SEvan Quan 		return -EPERM;
2914d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2915d2ae842dSAlex Deucher 		return -EPERM;
2916e098bc96SEvan Quan 
2917e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
2918e098bc96SEvan Quan 		return -EINVAL;
2919e098bc96SEvan Quan 
2920e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2921e098bc96SEvan Quan 	if (err)
2922e098bc96SEvan Quan 		return err;
2923e098bc96SEvan Quan 
2924e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
2925ae07970aSXiaomeng Hou 	value |= limit_type << 24;
2926e098bc96SEvan Quan 
29274a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2928e098bc96SEvan Quan 	if (err < 0) {
29294a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2930e098bc96SEvan Quan 		return err;
2931e098bc96SEvan Quan 	}
2932e098bc96SEvan Quan 
293379c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
2934e098bc96SEvan Quan 
29354a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29364a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2937e098bc96SEvan Quan 
2938e098bc96SEvan Quan 	if (err)
2939e098bc96SEvan Quan 		return err;
2940e098bc96SEvan Quan 
2941e098bc96SEvan Quan 	return count;
2942e098bc96SEvan Quan }
2943e098bc96SEvan Quan 
2944e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2945e098bc96SEvan Quan 				      struct device_attribute *attr,
2946e098bc96SEvan Quan 				      char *buf)
2947e098bc96SEvan Quan {
2948e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2949e098bc96SEvan Quan 	uint32_t sclk;
2950e098bc96SEvan Quan 	int r, size = sizeof(sclk);
2951e098bc96SEvan Quan 
295253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2953e098bc96SEvan Quan 		return -EPERM;
2954d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2955d2ae842dSAlex Deucher 		return -EPERM;
2956e098bc96SEvan Quan 
29574a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2958e098bc96SEvan Quan 	if (r < 0) {
29594a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2960e098bc96SEvan Quan 		return r;
2961e098bc96SEvan Quan 	}
2962e098bc96SEvan Quan 
2963e098bc96SEvan Quan 	/* get the sclk */
2964e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2965e098bc96SEvan Quan 				   (void *)&sclk, &size);
2966e098bc96SEvan Quan 
29674a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29684a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2969e098bc96SEvan Quan 
2970e098bc96SEvan Quan 	if (r)
2971e098bc96SEvan Quan 		return r;
2972e098bc96SEvan Quan 
2973a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
2974e098bc96SEvan Quan }
2975e098bc96SEvan Quan 
2976e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2977e098bc96SEvan Quan 					    struct device_attribute *attr,
2978e098bc96SEvan Quan 					    char *buf)
2979e098bc96SEvan Quan {
2980a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
2981e098bc96SEvan Quan }
2982e098bc96SEvan Quan 
2983e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2984e098bc96SEvan Quan 				      struct device_attribute *attr,
2985e098bc96SEvan Quan 				      char *buf)
2986e098bc96SEvan Quan {
2987e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2988e098bc96SEvan Quan 	uint32_t mclk;
2989e098bc96SEvan Quan 	int r, size = sizeof(mclk);
2990e098bc96SEvan Quan 
299153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2992e098bc96SEvan Quan 		return -EPERM;
2993d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2994d2ae842dSAlex Deucher 		return -EPERM;
2995e098bc96SEvan Quan 
29964a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2997e098bc96SEvan Quan 	if (r < 0) {
29984a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2999e098bc96SEvan Quan 		return r;
3000e098bc96SEvan Quan 	}
3001e098bc96SEvan Quan 
3002e098bc96SEvan Quan 	/* get the sclk */
3003e098bc96SEvan Quan 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3004e098bc96SEvan Quan 				   (void *)&mclk, &size);
3005e098bc96SEvan Quan 
30064a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30074a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3008e098bc96SEvan Quan 
3009e098bc96SEvan Quan 	if (r)
3010e098bc96SEvan Quan 		return r;
3011e098bc96SEvan Quan 
3012a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3013e098bc96SEvan Quan }
3014e098bc96SEvan Quan 
3015e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3016e098bc96SEvan Quan 					    struct device_attribute *attr,
3017e098bc96SEvan Quan 					    char *buf)
3018e098bc96SEvan Quan {
3019a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3020e098bc96SEvan Quan }
3021e098bc96SEvan Quan 
3022e098bc96SEvan Quan /**
3023e098bc96SEvan Quan  * DOC: hwmon
3024e098bc96SEvan Quan  *
3025e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3026e098bc96SEvan Quan  *
3027e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3028e098bc96SEvan Quan  *
3029e098bc96SEvan Quan  * - GPU voltage
3030e098bc96SEvan Quan  *
3031e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3032e098bc96SEvan Quan  *
3033e098bc96SEvan Quan  * - GPU power
3034e098bc96SEvan Quan  *
3035e098bc96SEvan Quan  * - GPU fan
3036e098bc96SEvan Quan  *
3037e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3038e098bc96SEvan Quan  *
3039e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3040e098bc96SEvan Quan  *
3041e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3042e098bc96SEvan Quan  *
3043e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3044e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3045e098bc96SEvan Quan  *
3046e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3047e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3048e098bc96SEvan Quan  *
3049e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3050e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3051e098bc96SEvan Quan  *
3052e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3053e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3054e098bc96SEvan Quan  *
3055e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3056e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3057e098bc96SEvan Quan  *
3058e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3059e098bc96SEvan Quan  *
3060e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3061e098bc96SEvan Quan  *
3062e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3063e098bc96SEvan Quan  *
3064e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3065e098bc96SEvan Quan  *
306629f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3067e098bc96SEvan Quan  *
3068e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3069e098bc96SEvan Quan  *
3070e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3071e098bc96SEvan Quan  *
3072e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3073e098bc96SEvan Quan  *
3074e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3075e098bc96SEvan Quan  *
3076e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3077e098bc96SEvan Quan  *
3078e098bc96SEvan 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)
3079e098bc96SEvan Quan  *
3080e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3081e098bc96SEvan Quan  *
3082e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3083e098bc96SEvan Quan  *
3084e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3085e098bc96SEvan Quan  *
3086e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3087e098bc96SEvan Quan  *
3088e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3089e098bc96SEvan Quan  *
3090e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3091e098bc96SEvan Quan  *
3092e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3093e098bc96SEvan Quan  *
309496401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
309596401f7cSEvan Quan  *       That will get the former one overridden.
309696401f7cSEvan Quan  *
3097e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3098e098bc96SEvan Quan  *
3099e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3100e098bc96SEvan Quan  *
3101e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3102e098bc96SEvan Quan  *
3103e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3104e098bc96SEvan Quan  *
3105e098bc96SEvan Quan  */
3106e098bc96SEvan Quan 
3107e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3108e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3109e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3110e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3111e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3112e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3113e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3114e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3115e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3116e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3117e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3118e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3119e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3120e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3121e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3122e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3123e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3124e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3125e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3126e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3127e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3128e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3129e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3130e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3131e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3132e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3133e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3134e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3135e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3136e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3137e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3138e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
31396e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3140ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3141ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3142ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3143ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3144ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
31456e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3146ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3147e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3148e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3149e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3150e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3151e098bc96SEvan Quan 
3152e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3153e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3154e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3155e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3156e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3157e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3158e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3159e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3160e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3161e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3162e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3163e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3164e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3165e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3166e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3167e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3168e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3169e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3170e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3171e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3172e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3173e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3174e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3175e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3176e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3177e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3178e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3179e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3180e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3181e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3182e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3183e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3184e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
31856e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3186ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3187ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3188ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3189ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3190ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
31916e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3192ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3193e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3194e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3195e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3196e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3197e098bc96SEvan Quan 	NULL
3198e098bc96SEvan Quan };
3199e098bc96SEvan Quan 
3200e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3201e098bc96SEvan Quan 					struct attribute *attr, int index)
3202e098bc96SEvan Quan {
3203e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3204e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3205e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
32068ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3207e098bc96SEvan Quan 
3208e098bc96SEvan Quan 	/* under multi-vf mode, the hwmon attributes are all not supported */
3209e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3210e098bc96SEvan Quan 		return 0;
3211e098bc96SEvan Quan 
32124f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
32134f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
32144f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
32154f0f1b58SDanijel Slivka 
3216e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3217e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3218e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3219e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3220e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3221e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3222e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3223e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3224e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3225e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3226e098bc96SEvan Quan 		return 0;
3227e098bc96SEvan Quan 
3228e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3229e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3230e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3231e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3232e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3233e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3234e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3235e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3236e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3237e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3238e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3239e098bc96SEvan Quan 		return 0;
3240e098bc96SEvan Quan 
3241e098bc96SEvan Quan 	/* Skip crit temp on APU */
3242e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3243e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3244e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3245e098bc96SEvan Quan 		return 0;
3246e098bc96SEvan Quan 
3247e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3248e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3249e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3250e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3251e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3252e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3253e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3254e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3255e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3256e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3257e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3258e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3259e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3260e098bc96SEvan Quan 		return 0;
3261e098bc96SEvan Quan 
3262e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3263685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3264e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3265685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3266e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3267e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3268e098bc96SEvan Quan 
3269685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3270e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3271685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3272e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3273e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3274e098bc96SEvan Quan 
32758ecad8d6SLijo Lazar 	/* not implemented yet for GC 10.3.1 APUs */
3276ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
32778ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)))) &&
3278367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3279e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
32806e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
32816e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3282e098bc96SEvan Quan 		return 0;
3283e098bc96SEvan Quan 
32848ecad8d6SLijo Lazar 	/* not implemented yet for APUs having <= GC 9.3.0 */
3285367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
32868ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3287367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3288367deb67SAlex Deucher 		return 0;
3289367deb67SAlex Deucher 
3290e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3291685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3292685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3293685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3294685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3295e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3296e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3297e098bc96SEvan Quan 		return 0;
3298e098bc96SEvan Quan 
3299685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3300685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3301e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3302e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3303e098bc96SEvan Quan 		return 0;
3304e098bc96SEvan Quan 
3305e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3306e098bc96SEvan Quan 	     adev->family == AMDGPU_FAMILY_KV) &&	/* not implemented yet */
3307e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3308e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3309e098bc96SEvan Quan 		return 0;
3310e098bc96SEvan Quan 
3311e098bc96SEvan Quan 	/* only APUs have vddnb */
3312e098bc96SEvan Quan 	if (!(adev->flags & AMD_IS_APU) &&
3313e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3314e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3315e098bc96SEvan Quan 		return 0;
3316e098bc96SEvan Quan 
3317e098bc96SEvan Quan 	/* no mclk on APUs */
3318e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3319e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3320e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3321e098bc96SEvan Quan 		return 0;
3322e098bc96SEvan Quan 
3323e098bc96SEvan Quan 	/* only SOC15 dGPUs support hotspot and mem temperatures */
33248ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3325e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3326e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3327e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3328e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3329e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3330e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3331e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3332e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3333e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3334e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3335e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3336e098bc96SEvan Quan 		return 0;
3337e098bc96SEvan Quan 
3338ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
33398ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3340ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3341ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3342ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3343ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
33446e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3345de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3346ae07970aSXiaomeng Hou 		return 0;
3347ae07970aSXiaomeng Hou 
3348e098bc96SEvan Quan 	return effective_mode;
3349e098bc96SEvan Quan }
3350e098bc96SEvan Quan 
3351e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3352e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3353e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3354e098bc96SEvan Quan };
3355e098bc96SEvan Quan 
3356e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3357e098bc96SEvan Quan 	&hwmon_attrgroup,
3358e098bc96SEvan Quan 	NULL
3359e098bc96SEvan Quan };
3360e098bc96SEvan Quan 
3361e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3362e098bc96SEvan Quan {
3363e098bc96SEvan Quan 	int ret;
3364e098bc96SEvan Quan 	uint32_t mask = 0;
3365e098bc96SEvan Quan 
3366e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
3367e098bc96SEvan Quan 		return 0;
3368e098bc96SEvan Quan 
33695fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
33705fa99373SZhenGuo Yin 
3371e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3372e098bc96SEvan Quan 		return 0;
3373e098bc96SEvan Quan 
3374e098bc96SEvan Quan 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3375e098bc96SEvan Quan 								   DRIVER_NAME, adev,
3376e098bc96SEvan Quan 								   hwmon_groups);
3377e098bc96SEvan Quan 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3378e098bc96SEvan Quan 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3379e098bc96SEvan Quan 		dev_err(adev->dev,
3380e098bc96SEvan Quan 			"Unable to register hwmon device: %d\n", ret);
3381e098bc96SEvan Quan 		return ret;
3382e098bc96SEvan Quan 	}
3383e098bc96SEvan Quan 
3384e098bc96SEvan Quan 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3385e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
3386e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
3387e098bc96SEvan Quan 		break;
3388e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
3389e098bc96SEvan Quan 		mask = 0;
3390e098bc96SEvan Quan 		break;
3391e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
3392e098bc96SEvan Quan 	default:
3393e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
3394e098bc96SEvan Quan 		break;
3395e098bc96SEvan Quan 	}
3396e098bc96SEvan Quan 
3397e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
3398e098bc96SEvan Quan 					       amdgpu_device_attrs,
3399e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
3400e098bc96SEvan Quan 					       mask,
3401e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
3402e098bc96SEvan Quan 	if (ret)
3403e098bc96SEvan Quan 		return ret;
3404e098bc96SEvan Quan 
3405e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
3406e098bc96SEvan Quan 
3407e098bc96SEvan Quan 	return 0;
3408e098bc96SEvan Quan }
3409e098bc96SEvan Quan 
3410e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3411e098bc96SEvan Quan {
3412e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
3413e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3414e098bc96SEvan Quan 
3415e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3416e098bc96SEvan Quan }
3417e098bc96SEvan Quan 
3418e098bc96SEvan Quan /*
3419e098bc96SEvan Quan  * Debugfs info
3420e098bc96SEvan Quan  */
3421e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3422e098bc96SEvan Quan 
3423517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3424517cb957SHuang Rui 					   struct amdgpu_device *adev) {
3425517cb957SHuang Rui 	uint16_t *p_val;
3426517cb957SHuang Rui 	uint32_t size;
3427517cb957SHuang Rui 	int i;
342879c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
3429517cb957SHuang Rui 
343079c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
343179c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
3432517cb957SHuang Rui 				GFP_KERNEL);
3433517cb957SHuang Rui 
3434517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3435517cb957SHuang Rui 					    (void *)p_val, &size)) {
343679c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
3437517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3438517cb957SHuang Rui 					   *(p_val + i), i);
3439517cb957SHuang Rui 		}
3440517cb957SHuang Rui 
3441517cb957SHuang Rui 		kfree(p_val);
3442517cb957SHuang Rui 	}
3443517cb957SHuang Rui }
3444517cb957SHuang Rui 
3445e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3446e098bc96SEvan Quan {
34478ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
34488ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3449e098bc96SEvan Quan 	uint32_t value;
3450800c53d6SXiaojian Du 	uint64_t value64 = 0;
3451e098bc96SEvan Quan 	uint32_t query = 0;
3452e098bc96SEvan Quan 	int size;
3453e098bc96SEvan Quan 
3454e098bc96SEvan Quan 	/* GPU Clocks */
3455e098bc96SEvan Quan 	size = sizeof(value);
3456e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
3457517cb957SHuang Rui 
3458517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
3459517cb957SHuang Rui 
3460e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3461e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3462e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3463e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3464e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3465e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3466e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3467e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3468e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3469e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3470e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3471e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3472e098bc96SEvan Quan 	size = sizeof(uint32_t);
3473e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3474e098bc96SEvan Quan 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3475e098bc96SEvan Quan 	size = sizeof(value);
3476e098bc96SEvan Quan 	seq_printf(m, "\n");
3477e098bc96SEvan Quan 
3478e098bc96SEvan Quan 	/* GPU Temp */
3479e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3480e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3481e098bc96SEvan Quan 
3482e098bc96SEvan Quan 	/* GPU Load */
3483e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3484e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
3485e098bc96SEvan Quan 	/* MEM Load */
3486e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3487e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
3488e098bc96SEvan Quan 
3489e098bc96SEvan Quan 	seq_printf(m, "\n");
3490e098bc96SEvan Quan 
3491e098bc96SEvan Quan 	/* SMC feature mask */
3492e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3493e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3494e098bc96SEvan Quan 
34958ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
34968ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
3497e098bc96SEvan Quan 		/* VCN clocks */
3498e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3499e098bc96SEvan Quan 			if (!value) {
3500e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
3501e098bc96SEvan Quan 			} else {
3502e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
3503e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3504e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3505e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3506e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3507e098bc96SEvan Quan 			}
3508e098bc96SEvan Quan 		}
3509e098bc96SEvan Quan 		seq_printf(m, "\n");
3510e098bc96SEvan Quan 	} else {
3511e098bc96SEvan Quan 		/* UVD clocks */
3512e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3513e098bc96SEvan Quan 			if (!value) {
3514e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
3515e098bc96SEvan Quan 			} else {
3516e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
3517e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3518e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3519e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3520e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3521e098bc96SEvan Quan 			}
3522e098bc96SEvan Quan 		}
3523e098bc96SEvan Quan 		seq_printf(m, "\n");
3524e098bc96SEvan Quan 
3525e098bc96SEvan Quan 		/* VCE clocks */
3526e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3527e098bc96SEvan Quan 			if (!value) {
3528e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
3529e098bc96SEvan Quan 			} else {
3530e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
3531e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3532e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3533e098bc96SEvan Quan 			}
3534e098bc96SEvan Quan 		}
3535e098bc96SEvan Quan 	}
3536e098bc96SEvan Quan 
3537e098bc96SEvan Quan 	return 0;
3538e098bc96SEvan Quan }
3539e098bc96SEvan Quan 
354025faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
3541e098bc96SEvan Quan {
3542e098bc96SEvan Quan 	int i;
3543e098bc96SEvan Quan 
3544e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
3545e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3546e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
3547e098bc96SEvan Quan }
3548e098bc96SEvan Quan 
3549373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3550e098bc96SEvan Quan {
3551373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3552373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
355325faeddcSEvan Quan 	u64 flags = 0;
3554e098bc96SEvan Quan 	int r;
3555e098bc96SEvan Quan 
355653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3557e098bc96SEvan Quan 		return -EPERM;
3558d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3559d2ae842dSAlex Deucher 		return -EPERM;
3560e098bc96SEvan Quan 
3561e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
3562e098bc96SEvan Quan 	if (r < 0) {
3563e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3564e098bc96SEvan Quan 		return r;
3565e098bc96SEvan Quan 	}
3566e098bc96SEvan Quan 
356779c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
3568e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3569e098bc96SEvan Quan 		if (r)
3570e098bc96SEvan Quan 			goto out;
357179c65f3fSEvan Quan 	}
3572e098bc96SEvan Quan 
3573e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3574e098bc96SEvan Quan 
357525faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
3576e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
3577e098bc96SEvan Quan 	seq_printf(m, "\n");
3578e098bc96SEvan Quan 
3579e098bc96SEvan Quan out:
3580e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
3581e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
3582e098bc96SEvan Quan 
3583e098bc96SEvan Quan 	return r;
3584e098bc96SEvan Quan }
3585e098bc96SEvan Quan 
3586373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3587373720f7SNirmoy Das 
358827ebf21fSLijo Lazar /*
358927ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
359027ebf21fSLijo Lazar  *
359127ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
359227ebf21fSLijo Lazar  */
359327ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
359427ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
359527ebf21fSLijo Lazar {
359627ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
359727ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
359827ebf21fSLijo Lazar 	void *smu_prv_buf;
359979c65f3fSEvan Quan 	int ret = 0;
360027ebf21fSLijo Lazar 
360127ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
360227ebf21fSLijo Lazar 		return -EPERM;
360327ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
360427ebf21fSLijo Lazar 		return -EPERM;
360527ebf21fSLijo Lazar 
360679c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
360779c65f3fSEvan Quan 	if (ret)
360879c65f3fSEvan Quan 		return ret;
360927ebf21fSLijo Lazar 
361027ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
361127ebf21fSLijo Lazar 		return -EINVAL;
361227ebf21fSLijo Lazar 
361327ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
361427ebf21fSLijo Lazar 				       smu_prv_buf_size);
361527ebf21fSLijo Lazar }
361627ebf21fSLijo Lazar 
361727ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
361827ebf21fSLijo Lazar 	.owner = THIS_MODULE,
361927ebf21fSLijo Lazar 	.open = simple_open,
362027ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
362127ebf21fSLijo Lazar 	.llseek = default_llseek,
362227ebf21fSLijo Lazar };
362327ebf21fSLijo Lazar 
3624e098bc96SEvan Quan #endif
3625e098bc96SEvan Quan 
3626373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3627e098bc96SEvan Quan {
3628e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3629373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
3630373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
3631373720f7SNirmoy Das 
36321613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
36331613f346SFlora Cui 		return;
36341613f346SFlora Cui 
3635373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3636373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
3637373720f7SNirmoy Das 
363827ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
363927ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
364027ebf21fSLijo Lazar 					 adev,
364127ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
364227ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
36431f5fc7a5SAndrey Grodzovsky 
364479c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
3645e098bc96SEvan Quan #endif
3646e098bc96SEvan Quan }
3647