xref: /openbmc/linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
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 hwmon_temp_label {
39e098bc96SEvan Quan 	enum PP_HWMON_TEMP channel;
40e098bc96SEvan Quan 	const char *label;
41e098bc96SEvan Quan } temp_label[] = {
42e098bc96SEvan Quan 	{PP_TEMP_EDGE, "edge"},
43e098bc96SEvan Quan 	{PP_TEMP_JUNCTION, "junction"},
44e098bc96SEvan Quan 	{PP_TEMP_MEM, "mem"},
45e098bc96SEvan Quan };
46e098bc96SEvan Quan 
473867e370SDarren Powell const char * const amdgpu_pp_profile_name[] = {
483867e370SDarren Powell 	"BOOTUP_DEFAULT",
493867e370SDarren Powell 	"3D_FULL_SCREEN",
503867e370SDarren Powell 	"POWER_SAVING",
513867e370SDarren Powell 	"VIDEO",
523867e370SDarren Powell 	"VR",
533867e370SDarren Powell 	"COMPUTE",
54334682aeSKenneth Feng 	"CUSTOM",
55334682aeSKenneth Feng 	"WINDOW_3D",
5631865e96SPerry Yuan 	"CAPPED",
5731865e96SPerry Yuan 	"UNCAPPED",
583867e370SDarren Powell };
593867e370SDarren Powell 
60e098bc96SEvan Quan /**
61e098bc96SEvan Quan  * DOC: power_dpm_state
62e098bc96SEvan Quan  *
63e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
64e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
65e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
66e098bc96SEvan Quan  * It accepts the following arguments:
67e098bc96SEvan Quan  *
68e098bc96SEvan Quan  * - battery
69e098bc96SEvan Quan  *
70e098bc96SEvan Quan  * - balanced
71e098bc96SEvan Quan  *
72e098bc96SEvan Quan  * - performance
73e098bc96SEvan Quan  *
74e098bc96SEvan Quan  * battery
75e098bc96SEvan Quan  *
76e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
77e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
78e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
79e098bc96SEvan Quan  *
80e098bc96SEvan Quan  * balanced
81e098bc96SEvan Quan  *
82e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
83e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
84e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
85e098bc96SEvan Quan  *
86e098bc96SEvan Quan  * performance
87e098bc96SEvan Quan  *
88e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
89e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
90e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
91e098bc96SEvan Quan  *
92e098bc96SEvan Quan  */
93e098bc96SEvan Quan 
amdgpu_get_power_dpm_state(struct device * dev,struct device_attribute * attr,char * buf)94e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
95e098bc96SEvan Quan 					  struct device_attribute *attr,
96e098bc96SEvan Quan 					  char *buf)
97e098bc96SEvan Quan {
98e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
991348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
100e098bc96SEvan Quan 	enum amd_pm_state_type pm;
101e098bc96SEvan Quan 	int ret;
102e098bc96SEvan Quan 
10353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
104e098bc96SEvan Quan 		return -EPERM;
105d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
106d2ae842dSAlex Deucher 		return -EPERM;
107e098bc96SEvan Quan 
108e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
109e098bc96SEvan Quan 	if (ret < 0) {
110e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
111e098bc96SEvan Quan 		return ret;
112e098bc96SEvan Quan 	}
113e098bc96SEvan Quan 
11479c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
115e098bc96SEvan Quan 
116e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
117e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
118e098bc96SEvan Quan 
119a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
120e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
121e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
122e098bc96SEvan Quan }
123e098bc96SEvan Quan 
amdgpu_set_power_dpm_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)124e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
125e098bc96SEvan Quan 					  struct device_attribute *attr,
126e098bc96SEvan Quan 					  const char *buf,
127e098bc96SEvan Quan 					  size_t count)
128e098bc96SEvan Quan {
129e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1301348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
131e098bc96SEvan Quan 	enum amd_pm_state_type  state;
132e098bc96SEvan Quan 	int ret;
133e098bc96SEvan Quan 
13453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
135e098bc96SEvan Quan 		return -EPERM;
136d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
137d2ae842dSAlex Deucher 		return -EPERM;
138e098bc96SEvan Quan 
139e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
140e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
141e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
142e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
143e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
144e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
145e098bc96SEvan Quan 	else
146e098bc96SEvan Quan 		return -EINVAL;
147e098bc96SEvan Quan 
148e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
149e098bc96SEvan Quan 	if (ret < 0) {
150e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
151e098bc96SEvan Quan 		return ret;
152e098bc96SEvan Quan 	}
153e098bc96SEvan Quan 
15479c65f3fSEvan Quan 	amdgpu_dpm_set_power_state(adev, state);
155e098bc96SEvan Quan 
156e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
157e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
158e098bc96SEvan Quan 
159e098bc96SEvan Quan 	return count;
160e098bc96SEvan Quan }
161e098bc96SEvan Quan 
162e098bc96SEvan Quan 
163e098bc96SEvan Quan /**
164e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
165e098bc96SEvan Quan  *
166e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
167e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
168e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
169e098bc96SEvan Quan  *
170e098bc96SEvan Quan  * - auto
171e098bc96SEvan Quan  *
172e098bc96SEvan Quan  * - low
173e098bc96SEvan Quan  *
174e098bc96SEvan Quan  * - high
175e098bc96SEvan Quan  *
176e098bc96SEvan Quan  * - manual
177e098bc96SEvan Quan  *
178e098bc96SEvan Quan  * - profile_standard
179e098bc96SEvan Quan  *
180e098bc96SEvan Quan  * - profile_min_sclk
181e098bc96SEvan Quan  *
182e098bc96SEvan Quan  * - profile_min_mclk
183e098bc96SEvan Quan  *
184e098bc96SEvan Quan  * - profile_peak
185e098bc96SEvan Quan  *
186e098bc96SEvan Quan  * auto
187e098bc96SEvan Quan  *
188e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
189e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
190e098bc96SEvan Quan  *
191e098bc96SEvan Quan  * low
192e098bc96SEvan Quan  *
193e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
194e098bc96SEvan Quan  *
195e098bc96SEvan Quan  * high
196e098bc96SEvan Quan  *
197e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
198e098bc96SEvan Quan  *
199e098bc96SEvan Quan  * manual
200e098bc96SEvan Quan  *
201e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
202e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
203e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
204e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
205e098bc96SEvan Quan  *
206e098bc96SEvan Quan  * profile_standard
207e098bc96SEvan Quan  * profile_min_sclk
208e098bc96SEvan Quan  * profile_min_mclk
209e098bc96SEvan Quan  * profile_peak
210e098bc96SEvan Quan  *
211e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
212e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
213e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
214e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
215e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
216e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
217e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
218e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
219e098bc96SEvan Quan  *
220e098bc96SEvan Quan  */
221e098bc96SEvan Quan 
amdgpu_get_power_dpm_force_performance_level(struct device * dev,struct device_attribute * attr,char * buf)222e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
223e098bc96SEvan Quan 							    struct device_attribute *attr,
224e098bc96SEvan Quan 							    char *buf)
225e098bc96SEvan Quan {
226e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2271348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
228e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
229e098bc96SEvan Quan 	int ret;
230e098bc96SEvan Quan 
23153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
232e098bc96SEvan Quan 		return -EPERM;
233d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
234d2ae842dSAlex Deucher 		return -EPERM;
235e098bc96SEvan Quan 
236e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
237e098bc96SEvan Quan 	if (ret < 0) {
238e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
239e098bc96SEvan Quan 		return ret;
240e098bc96SEvan Quan 	}
241e098bc96SEvan Quan 
242e098bc96SEvan Quan 	level = amdgpu_dpm_get_performance_level(adev);
243e098bc96SEvan Quan 
244e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
245e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
246e098bc96SEvan Quan 
247a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
248e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
249e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
250e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
251e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
252e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
253e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
254e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
255e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
2566be64246SLijo Lazar 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
257e098bc96SEvan Quan 			  "unknown");
258e098bc96SEvan Quan }
259e098bc96SEvan Quan 
amdgpu_set_power_dpm_force_performance_level(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)260e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
261e098bc96SEvan Quan 							    struct device_attribute *attr,
262e098bc96SEvan Quan 							    const char *buf,
263e098bc96SEvan Quan 							    size_t count)
264e098bc96SEvan Quan {
265e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
267e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
268e098bc96SEvan Quan 	int ret = 0;
269e098bc96SEvan Quan 
27053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
271e098bc96SEvan Quan 		return -EPERM;
272d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
273d2ae842dSAlex Deucher 		return -EPERM;
274e098bc96SEvan Quan 
275e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
276e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
277e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
278e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
279e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
280e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
281e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
282e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
283e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
284e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
285e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
286e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
287e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
288e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
289e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
290e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
291e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
292e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
2936be64246SLijo Lazar 	} else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
2946be64246SLijo Lazar 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
295e098bc96SEvan Quan 	}  else {
296e098bc96SEvan Quan 		return -EINVAL;
297e098bc96SEvan Quan 	}
298e098bc96SEvan Quan 
299e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
300e098bc96SEvan Quan 	if (ret < 0) {
301e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
302e098bc96SEvan Quan 		return ret;
303e098bc96SEvan Quan 	}
304e098bc96SEvan Quan 
3058cda7a4fSAlex Deucher 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
30679c65f3fSEvan Quan 	if (amdgpu_dpm_force_performance_level(adev, level)) {
307e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
308e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
3098cda7a4fSAlex Deucher 		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
310e098bc96SEvan Quan 		return -EINVAL;
311e098bc96SEvan Quan 	}
3128cda7a4fSAlex Deucher 	/* override whatever a user ctx may have set */
3138cda7a4fSAlex Deucher 	adev->pm.stable_pstate_ctx = NULL;
3148cda7a4fSAlex Deucher 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
31579c65f3fSEvan Quan 
316e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
317e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
318e098bc96SEvan Quan 
319e098bc96SEvan Quan 	return count;
320e098bc96SEvan Quan }
321e098bc96SEvan Quan 
amdgpu_get_pp_num_states(struct device * dev,struct device_attribute * attr,char * buf)322e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
323e098bc96SEvan Quan 		struct device_attribute *attr,
324e098bc96SEvan Quan 		char *buf)
325e098bc96SEvan Quan {
326e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3271348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
328e098bc96SEvan Quan 	struct pp_states_info data;
32909b6744cSDarren Powell 	uint32_t i;
33009b6744cSDarren Powell 	int buf_len, ret;
331e098bc96SEvan Quan 
33253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
333e098bc96SEvan Quan 		return -EPERM;
334d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
335d2ae842dSAlex Deucher 		return -EPERM;
336e098bc96SEvan Quan 
337e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
338e098bc96SEvan Quan 	if (ret < 0) {
339e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
340e098bc96SEvan Quan 		return ret;
341e098bc96SEvan Quan 	}
342e098bc96SEvan Quan 
34379c65f3fSEvan Quan 	if (amdgpu_dpm_get_pp_num_states(adev, &data))
344e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
345e098bc96SEvan Quan 
346e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
347e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
348e098bc96SEvan Quan 
34909b6744cSDarren Powell 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
350e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
35109b6744cSDarren Powell 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
352e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
353e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
354e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
355e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
356e098bc96SEvan Quan 
357e098bc96SEvan Quan 	return buf_len;
358e098bc96SEvan Quan }
359e098bc96SEvan Quan 
amdgpu_get_pp_cur_state(struct device * dev,struct device_attribute * attr,char * buf)360e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
361e098bc96SEvan Quan 		struct device_attribute *attr,
362e098bc96SEvan Quan 		char *buf)
363e098bc96SEvan Quan {
364e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
3662b24c199STom Rix 	struct pp_states_info data = {0};
367e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
368e098bc96SEvan Quan 	int i = 0, ret = 0;
369e098bc96SEvan Quan 
37053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
371e098bc96SEvan Quan 		return -EPERM;
372d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
373d2ae842dSAlex Deucher 		return -EPERM;
374e098bc96SEvan Quan 
375e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
376e098bc96SEvan Quan 	if (ret < 0) {
377e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
378e098bc96SEvan Quan 		return ret;
379e098bc96SEvan Quan 	}
380e098bc96SEvan Quan 
38179c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
38279c65f3fSEvan Quan 
38379c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
384e098bc96SEvan Quan 
385e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
386e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
387e098bc96SEvan Quan 
38879c65f3fSEvan Quan 	if (ret)
38979c65f3fSEvan Quan 		return ret;
39079c65f3fSEvan Quan 
391e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
392e098bc96SEvan Quan 		if (pm == data.states[i])
393e098bc96SEvan Quan 			break;
394e098bc96SEvan Quan 	}
395e098bc96SEvan Quan 
396e098bc96SEvan Quan 	if (i == data.nums)
397e098bc96SEvan Quan 		i = -EINVAL;
398e098bc96SEvan Quan 
399a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", i);
400e098bc96SEvan Quan }
401e098bc96SEvan Quan 
amdgpu_get_pp_force_state(struct device * dev,struct device_attribute * attr,char * buf)402e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
403e098bc96SEvan Quan 		struct device_attribute *attr,
404e098bc96SEvan Quan 		char *buf)
405e098bc96SEvan Quan {
406e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4071348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
408e098bc96SEvan Quan 
40953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
410e098bc96SEvan Quan 		return -EPERM;
411d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
412d2ae842dSAlex Deucher 		return -EPERM;
413e098bc96SEvan Quan 
414d698a2c4SEvan Quan 	if (adev->pm.pp_force_state_enabled)
415e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
416e098bc96SEvan Quan 	else
417a9ca9bb3STian Tao 		return sysfs_emit(buf, "\n");
418e098bc96SEvan Quan }
419e098bc96SEvan Quan 
amdgpu_set_pp_force_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)420e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
421e098bc96SEvan Quan 		struct device_attribute *attr,
422e098bc96SEvan Quan 		const char *buf,
423e098bc96SEvan Quan 		size_t count)
424e098bc96SEvan Quan {
425e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4261348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
427e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
42879c65f3fSEvan Quan 	struct pp_states_info data;
429e098bc96SEvan Quan 	unsigned long idx;
430e098bc96SEvan Quan 	int ret;
431e098bc96SEvan Quan 
43253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
433e098bc96SEvan Quan 		return -EPERM;
434d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
435d2ae842dSAlex Deucher 		return -EPERM;
436e098bc96SEvan Quan 
437d698a2c4SEvan Quan 	adev->pm.pp_force_state_enabled = false;
43879c65f3fSEvan Quan 
439e098bc96SEvan Quan 	if (strlen(buf) == 1)
44079c65f3fSEvan Quan 		return count;
441e098bc96SEvan Quan 
442e098bc96SEvan Quan 	ret = kstrtoul(buf, 0, &idx);
443e098bc96SEvan Quan 	if (ret || idx >= ARRAY_SIZE(data.states))
444e098bc96SEvan Quan 		return -EINVAL;
445e098bc96SEvan Quan 
446e098bc96SEvan Quan 	idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
447e098bc96SEvan Quan 
448e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
449e098bc96SEvan Quan 	if (ret < 0) {
450e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
451e098bc96SEvan Quan 		return ret;
452e098bc96SEvan Quan 	}
453e098bc96SEvan Quan 
45479c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
45579c65f3fSEvan Quan 	if (ret)
45679c65f3fSEvan Quan 		goto err_out;
45779c65f3fSEvan Quan 
45879c65f3fSEvan Quan 	state = data.states[idx];
45979c65f3fSEvan Quan 
460e098bc96SEvan Quan 	/* only set user selected power states */
461e098bc96SEvan Quan 	if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
462e098bc96SEvan Quan 	    state != POWER_STATE_TYPE_DEFAULT) {
46379c65f3fSEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
464e098bc96SEvan Quan 				AMD_PP_TASK_ENABLE_USER_STATE, &state);
46579c65f3fSEvan Quan 		if (ret)
46679c65f3fSEvan Quan 			goto err_out;
46779c65f3fSEvan Quan 
468d698a2c4SEvan Quan 		adev->pm.pp_force_state_enabled = true;
469e098bc96SEvan Quan 	}
47079c65f3fSEvan Quan 
471e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
472e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
473e098bc96SEvan Quan 
474e098bc96SEvan Quan 	return count;
47579c65f3fSEvan Quan 
47679c65f3fSEvan Quan err_out:
47779c65f3fSEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
47879c65f3fSEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
47979c65f3fSEvan Quan 	return ret;
480e098bc96SEvan Quan }
481e098bc96SEvan Quan 
482e098bc96SEvan Quan /**
483e098bc96SEvan Quan  * DOC: pp_table
484e098bc96SEvan Quan  *
485e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
486e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
487e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
488e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
489e098bc96SEvan Quan  * powerplay using that new table.
490e098bc96SEvan Quan  *
491e098bc96SEvan Quan  */
492e098bc96SEvan Quan 
amdgpu_get_pp_table(struct device * dev,struct device_attribute * attr,char * buf)493e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
494e098bc96SEvan Quan 		struct device_attribute *attr,
495e098bc96SEvan Quan 		char *buf)
496e098bc96SEvan Quan {
497e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4981348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
499e098bc96SEvan Quan 	char *table = NULL;
500e098bc96SEvan Quan 	int size, ret;
501e098bc96SEvan Quan 
50253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
503e098bc96SEvan Quan 		return -EPERM;
504d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
505d2ae842dSAlex Deucher 		return -EPERM;
506e098bc96SEvan Quan 
507e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
508e098bc96SEvan Quan 	if (ret < 0) {
509e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
510e098bc96SEvan Quan 		return ret;
511e098bc96SEvan Quan 	}
512e098bc96SEvan Quan 
513e098bc96SEvan Quan 	size = amdgpu_dpm_get_pp_table(adev, &table);
51479c65f3fSEvan Quan 
515e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
516e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
51779c65f3fSEvan Quan 
51879c65f3fSEvan Quan 	if (size <= 0)
519e098bc96SEvan Quan 		return size;
520e098bc96SEvan Quan 
521e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
522e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
523e098bc96SEvan Quan 
524e098bc96SEvan Quan 	memcpy(buf, table, size);
525e098bc96SEvan Quan 
526e098bc96SEvan Quan 	return size;
527e098bc96SEvan Quan }
528e098bc96SEvan Quan 
amdgpu_set_pp_table(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)529e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
530e098bc96SEvan Quan 		struct device_attribute *attr,
531e098bc96SEvan Quan 		const char *buf,
532e098bc96SEvan Quan 		size_t count)
533e098bc96SEvan Quan {
534e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5351348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
536e098bc96SEvan Quan 	int ret = 0;
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 
5498f4828d0SDarren Powell 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
550e098bc96SEvan Quan 
551e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
552e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
553e098bc96SEvan Quan 
55479c65f3fSEvan Quan 	if (ret)
55579c65f3fSEvan Quan 		return ret;
55679c65f3fSEvan Quan 
557e098bc96SEvan Quan 	return count;
558e098bc96SEvan Quan }
559e098bc96SEvan Quan 
560e098bc96SEvan Quan /**
561e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
562e098bc96SEvan Quan  *
563e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
564e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
565e098bc96SEvan Quan  * this.
566e098bc96SEvan Quan  *
567e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
568e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
569e098bc96SEvan Quan  * following formula:
570e098bc96SEvan Quan  *
571e098bc96SEvan Quan  * Clock conversion (Mhz):
572e098bc96SEvan Quan  *
573e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
574e098bc96SEvan Quan  *
575e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
576e098bc96SEvan Quan  *
577e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
578e098bc96SEvan Quan  *
579e098bc96SEvan Quan  * DRAM data rate (MT/s):
580e098bc96SEvan Quan  *
581e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
582e098bc96SEvan Quan  *
583e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
584e098bc96SEvan Quan  *
585e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
586e098bc96SEvan Quan  *
587e098bc96SEvan Quan  * Bandwidth (MB/s):
588e098bc96SEvan Quan  *
589e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
590e098bc96SEvan Quan  *
591e098bc96SEvan Quan  * Some examples:
592e098bc96SEvan Quan  *
593e098bc96SEvan Quan  * G5 on RX460:
594e098bc96SEvan Quan  *
595e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
596e098bc96SEvan Quan  *
597e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
598e098bc96SEvan Quan  *
599e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
600e098bc96SEvan Quan  *
601e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
602e098bc96SEvan Quan  *
603e098bc96SEvan Quan  * G6 on RX5700:
604e098bc96SEvan Quan  *
605e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
606e098bc96SEvan Quan  *
607e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
608e098bc96SEvan Quan  *
609e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
610e098bc96SEvan Quan  *
611e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
612e098bc96SEvan Quan  *
613e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
614e098bc96SEvan Quan  *
615e098bc96SEvan Quan  * Reading the file will display:
616e098bc96SEvan Quan  *
617e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
618e098bc96SEvan Quan  *
619e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
620e098bc96SEvan Quan  *
621e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
622e098bc96SEvan Quan  *
623e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
624e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
625e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
626e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
627e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
628e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
629e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
630e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
631e098bc96SEvan Quan  *
632e098bc96SEvan Quan  *
633e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
634e098bc96SEvan Quan  *
635e098bc96SEvan Quan  * Reading the file will display:
636e098bc96SEvan Quan  *
637e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
638e098bc96SEvan Quan  *
63937a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
64037a58f69SEvan Quan  *   clock labeled OD_MCLK
641e098bc96SEvan Quan  *
642e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
6438f4f5f0bSEvan Quan  *   They can be used to calibrate the sclk voltage curve. This is
6448f4f5f0bSEvan Quan  *   available for Vega20 and NV1X.
6458f4f5f0bSEvan Quan  *
6468f4f5f0bSEvan Quan  * - voltage offset for the six anchor points of the v/f curve labeled
6478f4f5f0bSEvan Quan  *   OD_VDDC_CURVE. They can be used to calibrate the v/f curve. This
6488f4f5f0bSEvan Quan  *   is only availabe for some SMU13 ASICs.
649e098bc96SEvan Quan  *
650a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
651a2b6df4fSEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder and Dimgrey
652a2b6df4fSEvan Quan  *   Cavefish. For these ASICs, the target voltage calculation can be
653a2b6df4fSEvan Quan  *   illustrated by "voltage = voltage calculated from v/f curve +
654a2b6df4fSEvan Quan  *   overdrive vddgfx offset"
655a2b6df4fSEvan Quan  *
656e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage curve points
657e098bc96SEvan Quan  *   labeled OD_RANGE
658e098bc96SEvan Quan  *
6590487bbb4SAlex Deucher  * < For APUs >
6600487bbb4SAlex Deucher  *
6610487bbb4SAlex Deucher  * Reading the file will display:
6620487bbb4SAlex Deucher  *
6630487bbb4SAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
6640487bbb4SAlex Deucher  *
6650487bbb4SAlex Deucher  * - a list of valid ranges for sclk labeled OD_RANGE
6660487bbb4SAlex Deucher  *
6673dc8077fSAlex Deucher  * < For VanGogh >
6683dc8077fSAlex Deucher  *
6693dc8077fSAlex Deucher  * Reading the file will display:
6703dc8077fSAlex Deucher  *
6713dc8077fSAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
6723dc8077fSAlex Deucher  * - minimum and maximum core clocks labeled OD_CCLK
6733dc8077fSAlex Deucher  *
6743dc8077fSAlex Deucher  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
6753dc8077fSAlex Deucher  *
676e098bc96SEvan Quan  * To manually adjust these settings:
677e098bc96SEvan Quan  *
678e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
679e098bc96SEvan Quan  *
680e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
681e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
682e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
683e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
6843dc8077fSAlex Deucher  *   "m 1 800" will update maximum mclk to be 800Mhz. For core
6853dc8077fSAlex Deucher  *   clocks on VanGogh, the string contains "p core index clock".
6863dc8077fSAlex Deucher  *   E.g., "p 2 0 800" would set the minimum core clock on core
6873dc8077fSAlex Deucher  *   2 to 800Mhz.
688e098bc96SEvan Quan  *
6898f4f5f0bSEvan Quan  *   For sclk voltage curve,
6908f4f5f0bSEvan Quan  *     - For NV1X, enter the new values by writing a string that
6918f4f5f0bSEvan Quan  *       contains "vc point clock voltage" to the file. The points
6928f4f5f0bSEvan Quan  *       are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will update
6938f4f5f0bSEvan Quan  *       point1 with clock set as 300Mhz and voltage as 600mV. "vc 2
6948f4f5f0bSEvan Quan  *       1000 1000" will update point3 with clock set as 1000Mhz and
6958f4f5f0bSEvan Quan  *       voltage 1000mV.
6968f4f5f0bSEvan Quan  *     - For SMU13 ASICs, enter the new values by writing a string that
6978f4f5f0bSEvan Quan  *       contains "vc anchor_point_index voltage_offset" to the file.
6988f4f5f0bSEvan Quan  *       There are total six anchor points defined on the v/f curve with
6998f4f5f0bSEvan Quan  *       index as 0 - 5.
7008f4f5f0bSEvan Quan  *       - "vc 0 10" will update the voltage offset for point1 as 10mv.
7018f4f5f0bSEvan Quan  *       - "vc 5 -10" will update the voltage offset for point6 as -10mv.
702e098bc96SEvan Quan  *
703a2b6df4fSEvan Quan  *   To update the voltage offset applied for gfxclk/voltage calculation,
704a2b6df4fSEvan Quan  *   enter the new value by writing a string that contains "vo offset".
705a2b6df4fSEvan Quan  *   This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
706a2b6df4fSEvan Quan  *   And the offset can be a positive or negative value.
707a2b6df4fSEvan Quan  *
708e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
709e098bc96SEvan Quan  *   to the file to commit your changes
710e098bc96SEvan Quan  *
711e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
712e098bc96SEvan Quan  *   to the file to reset them
713e098bc96SEvan Quan  *
714e098bc96SEvan Quan  */
715e098bc96SEvan Quan 
amdgpu_set_pp_od_clk_voltage(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)716e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
717e098bc96SEvan Quan 		struct device_attribute *attr,
718e098bc96SEvan Quan 		const char *buf,
719e098bc96SEvan Quan 		size_t count)
720e098bc96SEvan Quan {
721e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7221348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
723e098bc96SEvan Quan 	int ret;
724e098bc96SEvan Quan 	uint32_t parameter_size = 0;
725e098bc96SEvan Quan 	long parameter[64];
726e098bc96SEvan Quan 	char buf_cpy[128];
727e098bc96SEvan Quan 	char *tmp_str;
728e098bc96SEvan Quan 	char *sub_str;
729e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
730e098bc96SEvan Quan 	uint32_t type;
731e098bc96SEvan Quan 
73253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
733e098bc96SEvan Quan 		return -EPERM;
734d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
735d2ae842dSAlex Deucher 		return -EPERM;
736e098bc96SEvan Quan 
737f3f1c94dSBas Nieuwenhuizen 	if (count > 127 || count == 0)
738e098bc96SEvan Quan 		return -EINVAL;
739e098bc96SEvan Quan 
740e098bc96SEvan Quan 	if (*buf == 's')
741e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
7420d90d0ddSHuang Rui 	else if (*buf == 'p')
7430d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
744e098bc96SEvan Quan 	else if (*buf == 'm')
745e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
746e098bc96SEvan Quan 	else if (*buf == 'r')
747e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
748e098bc96SEvan Quan 	else if (*buf == 'c')
749e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
750e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
751e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
752a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
753a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
754e098bc96SEvan Quan 	else
755e098bc96SEvan Quan 		return -EINVAL;
756e098bc96SEvan Quan 
757f3f1c94dSBas Nieuwenhuizen 	memcpy(buf_cpy, buf, count);
758f3f1c94dSBas Nieuwenhuizen 	buf_cpy[count] = 0;
759e098bc96SEvan Quan 
760e098bc96SEvan Quan 	tmp_str = buf_cpy;
761e098bc96SEvan Quan 
762a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
763a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
764e098bc96SEvan Quan 		tmp_str++;
765e098bc96SEvan Quan 	while (isspace(*++tmp_str));
766e098bc96SEvan Quan 
767ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
768aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
769aec1d870SMatt Coffin 			continue;
770e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
771e098bc96SEvan Quan 		if (ret)
772e098bc96SEvan Quan 			return -EINVAL;
773e098bc96SEvan Quan 		parameter_size++;
774e098bc96SEvan Quan 
775f3f1c94dSBas Nieuwenhuizen 		if (!tmp_str)
776f3f1c94dSBas Nieuwenhuizen 			break;
777f3f1c94dSBas Nieuwenhuizen 
778e098bc96SEvan Quan 		while (isspace(*tmp_str))
779e098bc96SEvan Quan 			tmp_str++;
780e098bc96SEvan Quan 	}
781e098bc96SEvan Quan 
782e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
783e098bc96SEvan Quan 	if (ret < 0) {
784e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
785e098bc96SEvan Quan 		return ret;
786e098bc96SEvan Quan 	}
787e098bc96SEvan Quan 
78879c65f3fSEvan Quan 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
78979c65f3fSEvan Quan 					      type,
79012a6727dSXiaojian Du 					      parameter,
79179c65f3fSEvan Quan 					      parameter_size))
79279c65f3fSEvan Quan 		goto err_out;
79312a6727dSXiaojian Du 
79479c65f3fSEvan Quan 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
79579c65f3fSEvan Quan 					  parameter, parameter_size))
79679c65f3fSEvan Quan 		goto err_out;
797e098bc96SEvan Quan 
798e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
79979c65f3fSEvan Quan 		if (amdgpu_dpm_dispatch_task(adev,
800e098bc96SEvan Quan 					     AMD_PP_TASK_READJUST_POWER_STATE,
80179c65f3fSEvan Quan 					     NULL))
80279c65f3fSEvan Quan 			goto err_out;
80379c65f3fSEvan Quan 	}
80479c65f3fSEvan Quan 
805e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
806e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
80779c65f3fSEvan Quan 
808e098bc96SEvan Quan 	return count;
80979c65f3fSEvan Quan 
81079c65f3fSEvan Quan err_out:
811e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
812e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
813e098bc96SEvan Quan 	return -EINVAL;
814e098bc96SEvan Quan }
815e098bc96SEvan Quan 
amdgpu_get_pp_od_clk_voltage(struct device * dev,struct device_attribute * attr,char * buf)816e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
817e098bc96SEvan Quan 		struct device_attribute *attr,
818e098bc96SEvan Quan 		char *buf)
819e098bc96SEvan Quan {
820e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8211348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
822c8cb19c7SDarren Powell 	int size = 0;
823e098bc96SEvan Quan 	int ret;
824c8cb19c7SDarren Powell 	enum pp_clock_type od_clocks[6] = {
825c8cb19c7SDarren Powell 		OD_SCLK,
826c8cb19c7SDarren Powell 		OD_MCLK,
827c8cb19c7SDarren Powell 		OD_VDDC_CURVE,
828c8cb19c7SDarren Powell 		OD_RANGE,
829c8cb19c7SDarren Powell 		OD_VDDGFX_OFFSET,
830c8cb19c7SDarren Powell 		OD_CCLK,
831c8cb19c7SDarren Powell 	};
832c8cb19c7SDarren Powell 	uint clk_index;
833e098bc96SEvan Quan 
83453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
835e098bc96SEvan Quan 		return -EPERM;
836d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
837d2ae842dSAlex Deucher 		return -EPERM;
838e098bc96SEvan Quan 
839e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
840e098bc96SEvan Quan 	if (ret < 0) {
841e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
842e098bc96SEvan Quan 		return ret;
843e098bc96SEvan Quan 	}
844e098bc96SEvan Quan 
845c8cb19c7SDarren Powell 	for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
846c8cb19c7SDarren Powell 		ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
847c8cb19c7SDarren Powell 		if (ret)
848c8cb19c7SDarren Powell 			break;
849c8cb19c7SDarren Powell 	}
850c8cb19c7SDarren Powell 	if (ret == -ENOENT) {
851e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
852e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
853e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
8548f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
855e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
8568f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
857e098bc96SEvan Quan 	}
858c8cb19c7SDarren Powell 
859c8cb19c7SDarren Powell 	if (size == 0)
860c8cb19c7SDarren Powell 		size = sysfs_emit(buf, "\n");
861c8cb19c7SDarren Powell 
862e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
863e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
864e098bc96SEvan Quan 
865e098bc96SEvan Quan 	return size;
866e098bc96SEvan Quan }
867e098bc96SEvan Quan 
868e098bc96SEvan Quan /**
869e098bc96SEvan Quan  * DOC: pp_features
870e098bc96SEvan Quan  *
871e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
872e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
873e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
874e098bc96SEvan Quan  *
875e098bc96SEvan Quan  * Reading back the file will show you the followings:
876e098bc96SEvan Quan  * - Current ppfeature masks
877e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
878e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
879e098bc96SEvan Quan  *
880e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
881e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
882e098bc96SEvan Quan  * new ppfeature masks.
883e098bc96SEvan Quan  */
amdgpu_set_pp_features(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)884e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
885e098bc96SEvan Quan 				      struct device_attribute *attr,
886e098bc96SEvan Quan 				      const char *buf,
887e098bc96SEvan Quan 				      size_t count)
888e098bc96SEvan Quan {
889e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8901348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
891e098bc96SEvan Quan 	uint64_t featuremask;
892e098bc96SEvan Quan 	int ret;
893e098bc96SEvan Quan 
89453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
895e098bc96SEvan Quan 		return -EPERM;
896d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
897d2ae842dSAlex Deucher 		return -EPERM;
898e098bc96SEvan Quan 
899e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
900e098bc96SEvan Quan 	if (ret)
901e098bc96SEvan Quan 		return -EINVAL;
902e098bc96SEvan Quan 
903e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
904e098bc96SEvan Quan 	if (ret < 0) {
905e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
906e098bc96SEvan Quan 		return ret;
907e098bc96SEvan Quan 	}
908e098bc96SEvan Quan 
909e098bc96SEvan Quan 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
91079c65f3fSEvan Quan 
911e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
912e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
91379c65f3fSEvan Quan 
91479c65f3fSEvan Quan 	if (ret)
915e098bc96SEvan Quan 		return -EINVAL;
916e098bc96SEvan Quan 
917e098bc96SEvan Quan 	return count;
918e098bc96SEvan Quan }
919e098bc96SEvan Quan 
amdgpu_get_pp_features(struct device * dev,struct device_attribute * attr,char * buf)920e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
921e098bc96SEvan Quan 				      struct device_attribute *attr,
922e098bc96SEvan Quan 				      char *buf)
923e098bc96SEvan Quan {
924e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9251348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
926e098bc96SEvan Quan 	ssize_t size;
927e098bc96SEvan Quan 	int ret;
928e098bc96SEvan Quan 
92953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
930e098bc96SEvan Quan 		return -EPERM;
931d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
932d2ae842dSAlex Deucher 		return -EPERM;
933e098bc96SEvan Quan 
934e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
935e098bc96SEvan Quan 	if (ret < 0) {
936e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
937e098bc96SEvan Quan 		return ret;
938e098bc96SEvan Quan 	}
939e098bc96SEvan Quan 
940e098bc96SEvan Quan 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
94179c65f3fSEvan Quan 	if (size <= 0)
94209b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
943e098bc96SEvan Quan 
944e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
945e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
946e098bc96SEvan Quan 
947e098bc96SEvan Quan 	return size;
948e098bc96SEvan Quan }
949e098bc96SEvan Quan 
950e098bc96SEvan Quan /**
951e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
952e098bc96SEvan Quan  *
953e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
954e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
955e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
956e098bc96SEvan Quan  * this.
957e098bc96SEvan Quan  *
958e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
959e098bc96SEvan Quan  * Vega10 and later ASICs.
960e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
961e098bc96SEvan Quan  *
962e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
963e098bc96SEvan Quan  * the power state and the clock information for those levels.
964e098bc96SEvan Quan  *
965e098bc96SEvan Quan  * To manually adjust these states, first select manual using
966e098bc96SEvan Quan  * power_dpm_force_performance_level.
967e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
968e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
969e098bc96SEvan Quan  * E.g.,
970e098bc96SEvan Quan  *
971e098bc96SEvan Quan  * .. code-block:: bash
972e098bc96SEvan Quan  *
973e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
974e098bc96SEvan Quan  *
975e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
976e098bc96SEvan Quan  *
977e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
978e098bc96SEvan Quan  */
979e098bc96SEvan Quan 
amdgpu_get_pp_dpm_clock(struct device * dev,enum pp_clock_type type,char * buf)9802ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
9812ea092e5SDarren Powell 		enum pp_clock_type type,
982e098bc96SEvan Quan 		char *buf)
983e098bc96SEvan Quan {
984e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9851348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
986c8cb19c7SDarren Powell 	int size = 0;
987c8cb19c7SDarren Powell 	int ret = 0;
988e098bc96SEvan Quan 
98953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
990e098bc96SEvan Quan 		return -EPERM;
991d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
992d2ae842dSAlex Deucher 		return -EPERM;
993e098bc96SEvan Quan 
994e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
995e098bc96SEvan Quan 	if (ret < 0) {
996e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
997e098bc96SEvan Quan 		return ret;
998e098bc96SEvan Quan 	}
999e098bc96SEvan Quan 
1000c8cb19c7SDarren Powell 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1001c8cb19c7SDarren Powell 	if (ret == -ENOENT)
10022ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1003c8cb19c7SDarren Powell 
1004c8cb19c7SDarren Powell 	if (size == 0)
100509b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1006e098bc96SEvan Quan 
1007e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1008e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1009e098bc96SEvan Quan 
1010e098bc96SEvan Quan 	return size;
1011e098bc96SEvan Quan }
1012e098bc96SEvan Quan 
1013e098bc96SEvan Quan /*
1014e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1015e098bc96SEvan Quan  * per line (+1 for \n).
1016e098bc96SEvan Quan  */
1017e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1018e098bc96SEvan Quan 
amdgpu_read_mask(const char * buf,size_t count,uint32_t * mask)1019e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1020e098bc96SEvan Quan {
1021e098bc96SEvan Quan 	int ret;
1022c915ef89SDan Carpenter 	unsigned long level;
1023e098bc96SEvan Quan 	char *sub_str = NULL;
1024e098bc96SEvan Quan 	char *tmp;
1025e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1026e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1027e098bc96SEvan Quan 	size_t bytes;
1028e098bc96SEvan Quan 
1029e098bc96SEvan Quan 	*mask = 0;
1030e098bc96SEvan Quan 
1031e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1032e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1033e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1034e098bc96SEvan Quan 	tmp = buf_cpy;
1035ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1036e098bc96SEvan Quan 		if (strlen(sub_str)) {
1037c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1038c915ef89SDan Carpenter 			if (ret || level > 31)
1039e098bc96SEvan Quan 				return -EINVAL;
1040e098bc96SEvan Quan 			*mask |= 1 << level;
1041e098bc96SEvan Quan 		} else
1042e098bc96SEvan Quan 			break;
1043e098bc96SEvan Quan 	}
1044e098bc96SEvan Quan 
1045e098bc96SEvan Quan 	return 0;
1046e098bc96SEvan Quan }
1047e098bc96SEvan Quan 
amdgpu_set_pp_dpm_clock(struct device * dev,enum pp_clock_type type,const char * buf,size_t count)10482ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
10492ea092e5SDarren Powell 		enum pp_clock_type type,
1050e098bc96SEvan Quan 		const char *buf,
1051e098bc96SEvan Quan 		size_t count)
1052e098bc96SEvan Quan {
1053e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10541348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1055e098bc96SEvan Quan 	int ret;
1056e098bc96SEvan Quan 	uint32_t mask = 0;
1057e098bc96SEvan Quan 
105853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1059e098bc96SEvan Quan 		return -EPERM;
1060d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1061d2ae842dSAlex Deucher 		return -EPERM;
1062e098bc96SEvan Quan 
1063e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1064e098bc96SEvan Quan 	if (ret)
1065e098bc96SEvan Quan 		return ret;
1066e098bc96SEvan Quan 
1067e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1068e098bc96SEvan Quan 	if (ret < 0) {
1069e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1070e098bc96SEvan Quan 		return ret;
1071e098bc96SEvan Quan 	}
1072e098bc96SEvan Quan 
10732ea092e5SDarren Powell 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1074e098bc96SEvan Quan 
1075e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1076e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1077e098bc96SEvan Quan 
1078e098bc96SEvan Quan 	if (ret)
1079e098bc96SEvan Quan 		return -EINVAL;
1080e098bc96SEvan Quan 
1081e098bc96SEvan Quan 	return count;
1082e098bc96SEvan Quan }
1083e098bc96SEvan Quan 
amdgpu_get_pp_dpm_sclk(struct device * dev,struct device_attribute * attr,char * buf)10842ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
10852ea092e5SDarren Powell 		struct device_attribute *attr,
10862ea092e5SDarren Powell 		char *buf)
10872ea092e5SDarren Powell {
10882ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
10892ea092e5SDarren Powell }
10902ea092e5SDarren Powell 
amdgpu_set_pp_dpm_sclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)10912ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
10922ea092e5SDarren Powell 		struct device_attribute *attr,
10932ea092e5SDarren Powell 		const char *buf,
10942ea092e5SDarren Powell 		size_t count)
10952ea092e5SDarren Powell {
10962ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
10972ea092e5SDarren Powell }
10982ea092e5SDarren Powell 
amdgpu_get_pp_dpm_mclk(struct device * dev,struct device_attribute * attr,char * buf)1099e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1100e098bc96SEvan Quan 		struct device_attribute *attr,
1101e098bc96SEvan Quan 		char *buf)
1102e098bc96SEvan Quan {
11032ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1104e098bc96SEvan Quan }
1105e098bc96SEvan Quan 
amdgpu_set_pp_dpm_mclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1106e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1107e098bc96SEvan Quan 		struct device_attribute *attr,
1108e098bc96SEvan Quan 		const char *buf,
1109e098bc96SEvan Quan 		size_t count)
1110e098bc96SEvan Quan {
11112ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1112e098bc96SEvan Quan }
1113e098bc96SEvan Quan 
amdgpu_get_pp_dpm_socclk(struct device * dev,struct device_attribute * attr,char * buf)1114e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1115e098bc96SEvan Quan 		struct device_attribute *attr,
1116e098bc96SEvan Quan 		char *buf)
1117e098bc96SEvan Quan {
11182ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1119e098bc96SEvan Quan }
1120e098bc96SEvan Quan 
amdgpu_set_pp_dpm_socclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1121e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1122e098bc96SEvan Quan 		struct device_attribute *attr,
1123e098bc96SEvan Quan 		const char *buf,
1124e098bc96SEvan Quan 		size_t count)
1125e098bc96SEvan Quan {
11262ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1127e098bc96SEvan Quan }
1128e098bc96SEvan Quan 
amdgpu_get_pp_dpm_fclk(struct device * dev,struct device_attribute * attr,char * buf)1129e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1130e098bc96SEvan Quan 		struct device_attribute *attr,
1131e098bc96SEvan Quan 		char *buf)
1132e098bc96SEvan Quan {
11332ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1134e098bc96SEvan Quan }
1135e098bc96SEvan Quan 
amdgpu_set_pp_dpm_fclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1136e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1137e098bc96SEvan Quan 		struct device_attribute *attr,
1138e098bc96SEvan Quan 		const char *buf,
1139e098bc96SEvan Quan 		size_t count)
1140e098bc96SEvan Quan {
11412ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1142e098bc96SEvan Quan }
1143e098bc96SEvan Quan 
amdgpu_get_pp_dpm_vclk(struct device * dev,struct device_attribute * attr,char * buf)11449577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
11459577b0ecSXiaojian Du 		struct device_attribute *attr,
11469577b0ecSXiaojian Du 		char *buf)
11479577b0ecSXiaojian Du {
11482ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
11499577b0ecSXiaojian Du }
11509577b0ecSXiaojian Du 
amdgpu_set_pp_dpm_vclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11519577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
11529577b0ecSXiaojian Du 		struct device_attribute *attr,
11539577b0ecSXiaojian Du 		const char *buf,
11549577b0ecSXiaojian Du 		size_t count)
11559577b0ecSXiaojian Du {
11562ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
11579577b0ecSXiaojian Du }
11589577b0ecSXiaojian Du 
amdgpu_get_pp_dpm_vclk1(struct device * dev,struct device_attribute * attr,char * buf)1159d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1160d7001e72STong Liu01 		struct device_attribute *attr,
1161d7001e72STong Liu01 		char *buf)
1162d7001e72STong Liu01 {
1163d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1164d7001e72STong Liu01 }
1165d7001e72STong Liu01 
amdgpu_set_pp_dpm_vclk1(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1166d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1167d7001e72STong Liu01 		struct device_attribute *attr,
1168d7001e72STong Liu01 		const char *buf,
1169d7001e72STong Liu01 		size_t count)
1170d7001e72STong Liu01 {
1171d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1172d7001e72STong Liu01 }
1173d7001e72STong Liu01 
amdgpu_get_pp_dpm_dclk(struct device * dev,struct device_attribute * attr,char * buf)11749577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
11759577b0ecSXiaojian Du 		struct device_attribute *attr,
11769577b0ecSXiaojian Du 		char *buf)
11779577b0ecSXiaojian Du {
11782ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
11799577b0ecSXiaojian Du }
11809577b0ecSXiaojian Du 
amdgpu_set_pp_dpm_dclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11819577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
11829577b0ecSXiaojian Du 		struct device_attribute *attr,
11839577b0ecSXiaojian Du 		const char *buf,
11849577b0ecSXiaojian Du 		size_t count)
11859577b0ecSXiaojian Du {
11862ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
11879577b0ecSXiaojian Du }
11889577b0ecSXiaojian Du 
amdgpu_get_pp_dpm_dclk1(struct device * dev,struct device_attribute * attr,char * buf)1189d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1190d7001e72STong Liu01 		struct device_attribute *attr,
1191d7001e72STong Liu01 		char *buf)
1192d7001e72STong Liu01 {
1193d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1194d7001e72STong Liu01 }
1195d7001e72STong Liu01 
amdgpu_set_pp_dpm_dclk1(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1196d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1197d7001e72STong Liu01 		struct device_attribute *attr,
1198d7001e72STong Liu01 		const char *buf,
1199d7001e72STong Liu01 		size_t count)
1200d7001e72STong Liu01 {
1201d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1202d7001e72STong Liu01 }
1203d7001e72STong Liu01 
amdgpu_get_pp_dpm_dcefclk(struct device * dev,struct device_attribute * attr,char * buf)1204e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1205e098bc96SEvan Quan 		struct device_attribute *attr,
1206e098bc96SEvan Quan 		char *buf)
1207e098bc96SEvan Quan {
12082ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1209e098bc96SEvan Quan }
1210e098bc96SEvan Quan 
amdgpu_set_pp_dpm_dcefclk(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1211e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1212e098bc96SEvan Quan 		struct device_attribute *attr,
1213e098bc96SEvan Quan 		const char *buf,
1214e098bc96SEvan Quan 		size_t count)
1215e098bc96SEvan Quan {
12162ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1217e098bc96SEvan Quan }
1218e098bc96SEvan Quan 
amdgpu_get_pp_dpm_pcie(struct device * dev,struct device_attribute * attr,char * buf)1219e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1220e098bc96SEvan Quan 		struct device_attribute *attr,
1221e098bc96SEvan Quan 		char *buf)
1222e098bc96SEvan Quan {
12232ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1224e098bc96SEvan Quan }
1225e098bc96SEvan Quan 
amdgpu_set_pp_dpm_pcie(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1226e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1227e098bc96SEvan Quan 		struct device_attribute *attr,
1228e098bc96SEvan Quan 		const char *buf,
1229e098bc96SEvan Quan 		size_t count)
1230e098bc96SEvan Quan {
12312ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1232e098bc96SEvan Quan }
1233e098bc96SEvan Quan 
amdgpu_get_pp_sclk_od(struct device * dev,struct device_attribute * attr,char * buf)1234e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1235e098bc96SEvan Quan 		struct device_attribute *attr,
1236e098bc96SEvan Quan 		char *buf)
1237e098bc96SEvan Quan {
1238e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12391348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1240e098bc96SEvan Quan 	uint32_t value = 0;
1241e098bc96SEvan Quan 	int ret;
1242e098bc96SEvan Quan 
124353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1244e098bc96SEvan Quan 		return -EPERM;
1245d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1246d2ae842dSAlex Deucher 		return -EPERM;
1247e098bc96SEvan Quan 
1248e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1249e098bc96SEvan Quan 	if (ret < 0) {
1250e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1251e098bc96SEvan Quan 		return ret;
1252e098bc96SEvan Quan 	}
1253e098bc96SEvan Quan 
1254e098bc96SEvan Quan 	value = amdgpu_dpm_get_sclk_od(adev);
1255e098bc96SEvan Quan 
1256e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1257e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1258e098bc96SEvan Quan 
1259a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1260e098bc96SEvan Quan }
1261e098bc96SEvan Quan 
amdgpu_set_pp_sclk_od(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1262e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1263e098bc96SEvan Quan 		struct device_attribute *attr,
1264e098bc96SEvan Quan 		const char *buf,
1265e098bc96SEvan Quan 		size_t count)
1266e098bc96SEvan Quan {
1267e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12681348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1269e098bc96SEvan Quan 	int ret;
1270e098bc96SEvan Quan 	long int value;
1271e098bc96SEvan Quan 
127253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1273e098bc96SEvan Quan 		return -EPERM;
1274d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1275d2ae842dSAlex Deucher 		return -EPERM;
1276e098bc96SEvan Quan 
1277e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1278e098bc96SEvan Quan 
1279e098bc96SEvan Quan 	if (ret)
1280e098bc96SEvan Quan 		return -EINVAL;
1281e098bc96SEvan Quan 
1282e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1283e098bc96SEvan Quan 	if (ret < 0) {
1284e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1285e098bc96SEvan Quan 		return ret;
1286e098bc96SEvan Quan 	}
1287e098bc96SEvan Quan 
1288e098bc96SEvan Quan 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1289e098bc96SEvan Quan 
1290e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1291e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1292e098bc96SEvan Quan 
1293e098bc96SEvan Quan 	return count;
1294e098bc96SEvan Quan }
1295e098bc96SEvan Quan 
amdgpu_get_pp_mclk_od(struct device * dev,struct device_attribute * attr,char * buf)1296e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1297e098bc96SEvan Quan 		struct device_attribute *attr,
1298e098bc96SEvan Quan 		char *buf)
1299e098bc96SEvan Quan {
1300e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13011348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1302e098bc96SEvan Quan 	uint32_t value = 0;
1303e098bc96SEvan Quan 	int ret;
1304e098bc96SEvan Quan 
130553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1306e098bc96SEvan Quan 		return -EPERM;
1307d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1308d2ae842dSAlex Deucher 		return -EPERM;
1309e098bc96SEvan Quan 
1310e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1311e098bc96SEvan Quan 	if (ret < 0) {
1312e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1313e098bc96SEvan Quan 		return ret;
1314e098bc96SEvan Quan 	}
1315e098bc96SEvan Quan 
1316e098bc96SEvan Quan 	value = amdgpu_dpm_get_mclk_od(adev);
1317e098bc96SEvan Quan 
1318e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1319e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1320e098bc96SEvan Quan 
1321a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1322e098bc96SEvan Quan }
1323e098bc96SEvan Quan 
amdgpu_set_pp_mclk_od(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1324e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1325e098bc96SEvan Quan 		struct device_attribute *attr,
1326e098bc96SEvan Quan 		const char *buf,
1327e098bc96SEvan Quan 		size_t count)
1328e098bc96SEvan Quan {
1329e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13301348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1331e098bc96SEvan Quan 	int ret;
1332e098bc96SEvan Quan 	long int value;
1333e098bc96SEvan Quan 
133453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1335e098bc96SEvan Quan 		return -EPERM;
1336d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1337d2ae842dSAlex Deucher 		return -EPERM;
1338e098bc96SEvan Quan 
1339e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1340e098bc96SEvan Quan 
1341e098bc96SEvan Quan 	if (ret)
1342e098bc96SEvan Quan 		return -EINVAL;
1343e098bc96SEvan Quan 
1344e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1345e098bc96SEvan Quan 	if (ret < 0) {
1346e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1347e098bc96SEvan Quan 		return ret;
1348e098bc96SEvan Quan 	}
1349e098bc96SEvan Quan 
1350e098bc96SEvan Quan 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1351e098bc96SEvan Quan 
1352e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1353e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1354e098bc96SEvan Quan 
1355e098bc96SEvan Quan 	return count;
1356e098bc96SEvan Quan }
1357e098bc96SEvan Quan 
1358e098bc96SEvan Quan /**
1359e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1360e098bc96SEvan Quan  *
1361e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1362e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1363e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1364e098bc96SEvan Quan  *
1365e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1366e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1367e098bc96SEvan Quan  *
1368e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1369e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1370e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1371e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1372e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1373e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1374e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1375e098bc96SEvan Quan  *
1376e098bc96SEvan Quan  */
1377e098bc96SEvan Quan 
amdgpu_get_pp_power_profile_mode(struct device * dev,struct device_attribute * attr,char * buf)1378e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1379e098bc96SEvan Quan 		struct device_attribute *attr,
1380e098bc96SEvan Quan 		char *buf)
1381e098bc96SEvan Quan {
1382e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13831348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1384e098bc96SEvan Quan 	ssize_t size;
1385e098bc96SEvan Quan 	int ret;
1386e098bc96SEvan Quan 
138753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1388e098bc96SEvan Quan 		return -EPERM;
1389d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1390d2ae842dSAlex Deucher 		return -EPERM;
1391e098bc96SEvan Quan 
1392e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1393e098bc96SEvan Quan 	if (ret < 0) {
1394e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1395e098bc96SEvan Quan 		return ret;
1396e098bc96SEvan Quan 	}
1397e098bc96SEvan Quan 
1398e098bc96SEvan Quan 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
139979c65f3fSEvan Quan 	if (size <= 0)
140009b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1401e098bc96SEvan Quan 
1402e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1403e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1404e098bc96SEvan Quan 
1405e098bc96SEvan Quan 	return size;
1406e098bc96SEvan Quan }
1407e098bc96SEvan Quan 
1408e098bc96SEvan Quan 
amdgpu_set_pp_power_profile_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1409e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1410e098bc96SEvan Quan 		struct device_attribute *attr,
1411e098bc96SEvan Quan 		const char *buf,
1412e098bc96SEvan Quan 		size_t count)
1413e098bc96SEvan Quan {
1414e098bc96SEvan Quan 	int ret;
1415e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14161348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1417e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1418e098bc96SEvan Quan 	long parameter[64];
1419e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1420e098bc96SEvan Quan 	char *tmp_str;
1421e098bc96SEvan Quan 	uint32_t i = 0;
1422e098bc96SEvan Quan 	char tmp[2];
1423e098bc96SEvan Quan 	long int profile_mode = 0;
1424e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1425e098bc96SEvan Quan 
142653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1427e098bc96SEvan Quan 		return -EPERM;
1428d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1429d2ae842dSAlex Deucher 		return -EPERM;
1430e098bc96SEvan Quan 
1431e098bc96SEvan Quan 	tmp[0] = *(buf);
1432e098bc96SEvan Quan 	tmp[1] = '\0';
1433e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1434e098bc96SEvan Quan 	if (ret)
1435e098bc96SEvan Quan 		return -EINVAL;
1436e098bc96SEvan Quan 
1437e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1438e098bc96SEvan Quan 		if (count < 2 || count > 127)
1439e098bc96SEvan Quan 			return -EINVAL;
1440e098bc96SEvan Quan 		while (isspace(*++buf))
1441e098bc96SEvan Quan 			i++;
1442e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1443e098bc96SEvan Quan 		tmp_str = buf_cpy;
1444ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1445c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1446c2efbc3fSEvan Quan 				continue;
1447e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1448e098bc96SEvan Quan 			if (ret)
1449e098bc96SEvan Quan 				return -EINVAL;
1450e098bc96SEvan Quan 			parameter_size++;
1451e098bc96SEvan Quan 			while (isspace(*tmp_str))
1452e098bc96SEvan Quan 				tmp_str++;
1453e098bc96SEvan Quan 		}
1454e098bc96SEvan Quan 	}
1455e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1456e098bc96SEvan Quan 
1457e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1458e098bc96SEvan Quan 	if (ret < 0) {
1459e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1460e098bc96SEvan Quan 		return ret;
1461e098bc96SEvan Quan 	}
1462e098bc96SEvan Quan 
1463e098bc96SEvan Quan 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1464e098bc96SEvan Quan 
1465e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1466e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1467e098bc96SEvan Quan 
1468e098bc96SEvan Quan 	if (!ret)
1469e098bc96SEvan Quan 		return count;
1470e098bc96SEvan Quan 
1471e098bc96SEvan Quan 	return -EINVAL;
1472e098bc96SEvan Quan }
1473e098bc96SEvan Quan 
amdgpu_hwmon_get_sensor_generic(struct amdgpu_device * adev,enum amd_pp_sensors sensor,void * query)1474*51e4630eSAlex Deucher static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1475d78c227fSMario Limonciello 					   enum amd_pp_sensors sensor,
1476d78c227fSMario Limonciello 					   void *query)
1477d78c227fSMario Limonciello {
1478d78c227fSMario Limonciello 	int r, size = sizeof(uint32_t);
1479d78c227fSMario Limonciello 
1480d78c227fSMario Limonciello 	if (amdgpu_in_reset(adev))
1481d78c227fSMario Limonciello 		return -EPERM;
1482d78c227fSMario Limonciello 	if (adev->in_suspend && !adev->in_runpm)
1483d78c227fSMario Limonciello 		return -EPERM;
1484d78c227fSMario Limonciello 
1485d78c227fSMario Limonciello 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1486d78c227fSMario Limonciello 	if (r < 0) {
1487d78c227fSMario Limonciello 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1488d78c227fSMario Limonciello 		return r;
1489d78c227fSMario Limonciello 	}
1490d78c227fSMario Limonciello 
1491d78c227fSMario Limonciello 	/* get the sensor value */
1492d78c227fSMario Limonciello 	r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1493d78c227fSMario Limonciello 
1494d78c227fSMario Limonciello 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1495d78c227fSMario Limonciello 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1496d78c227fSMario Limonciello 
1497d78c227fSMario Limonciello 	return r;
1498d78c227fSMario Limonciello }
1499d78c227fSMario Limonciello 
1500e098bc96SEvan Quan /**
1501e098bc96SEvan Quan  * DOC: gpu_busy_percent
1502e098bc96SEvan Quan  *
1503e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1504e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1505e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1506e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1507e098bc96SEvan Quan  */
amdgpu_get_gpu_busy_percent(struct device * dev,struct device_attribute * attr,char * buf)1508e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1509e098bc96SEvan Quan 					   struct device_attribute *attr,
1510e098bc96SEvan Quan 					   char *buf)
1511e098bc96SEvan Quan {
1512e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15131348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1514d78c227fSMario Limonciello 	unsigned int value;
1515d78c227fSMario Limonciello 	int r;
1516e098bc96SEvan Quan 
1517d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1518e098bc96SEvan Quan 	if (r)
1519e098bc96SEvan Quan 		return r;
1520e098bc96SEvan Quan 
1521a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1522e098bc96SEvan Quan }
1523e098bc96SEvan Quan 
1524e098bc96SEvan Quan /**
1525e098bc96SEvan Quan  * DOC: mem_busy_percent
1526e098bc96SEvan Quan  *
1527e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1528e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1529e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1530e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1531e098bc96SEvan Quan  */
amdgpu_get_mem_busy_percent(struct device * dev,struct device_attribute * attr,char * buf)1532e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1533e098bc96SEvan Quan 					   struct device_attribute *attr,
1534e098bc96SEvan Quan 					   char *buf)
1535e098bc96SEvan Quan {
1536e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15371348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1538d78c227fSMario Limonciello 	unsigned int value;
1539d78c227fSMario Limonciello 	int r;
1540e098bc96SEvan Quan 
1541d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1542e098bc96SEvan Quan 	if (r)
1543e098bc96SEvan Quan 		return r;
1544e098bc96SEvan Quan 
1545a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1546e098bc96SEvan Quan }
1547e098bc96SEvan Quan 
1548e098bc96SEvan Quan /**
1549e098bc96SEvan Quan  * DOC: pcie_bw
1550e098bc96SEvan Quan  *
1551e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1552e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1553e098bc96SEvan Quan  * The file pcie_bw is used for this.
1554e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1555e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1556e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1557e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1558e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1559e098bc96SEvan Quan  */
amdgpu_get_pcie_bw(struct device * dev,struct device_attribute * attr,char * buf)1560e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1561e098bc96SEvan Quan 		struct device_attribute *attr,
1562e098bc96SEvan Quan 		char *buf)
1563e098bc96SEvan Quan {
1564e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1566e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1567e098bc96SEvan Quan 	int ret;
1568e098bc96SEvan Quan 
156953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1570e098bc96SEvan Quan 		return -EPERM;
1571d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1572d2ae842dSAlex Deucher 		return -EPERM;
1573e098bc96SEvan Quan 
1574e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1575e098bc96SEvan Quan 		return -ENODATA;
1576e098bc96SEvan Quan 
1577e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1578e098bc96SEvan Quan 		return -ENODATA;
1579e098bc96SEvan Quan 
1580e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1581e098bc96SEvan Quan 	if (ret < 0) {
1582e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1583e098bc96SEvan Quan 		return ret;
1584e098bc96SEvan Quan 	}
1585e098bc96SEvan Quan 
1586e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1587e098bc96SEvan Quan 
1588e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1589e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1590e098bc96SEvan Quan 
1591a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1592e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1593e098bc96SEvan Quan }
1594e098bc96SEvan Quan 
1595e098bc96SEvan Quan /**
1596e098bc96SEvan Quan  * DOC: unique_id
1597e098bc96SEvan Quan  *
1598e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1599e098bc96SEvan Quan  * The file unique_id is used for this.
1600e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1601e098bc96SEvan Quan  *
1602e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1603e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1604e098bc96SEvan Quan  */
amdgpu_get_unique_id(struct device * dev,struct device_attribute * attr,char * buf)1605e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1606e098bc96SEvan Quan 		struct device_attribute *attr,
1607e098bc96SEvan Quan 		char *buf)
1608e098bc96SEvan Quan {
1609e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16101348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1611e098bc96SEvan Quan 
161253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1613e098bc96SEvan Quan 		return -EPERM;
1614d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1615d2ae842dSAlex Deucher 		return -EPERM;
1616e098bc96SEvan Quan 
1617e098bc96SEvan Quan 	if (adev->unique_id)
1618a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1619e098bc96SEvan Quan 
1620e098bc96SEvan Quan 	return 0;
1621e098bc96SEvan Quan }
1622e098bc96SEvan Quan 
1623e098bc96SEvan Quan /**
1624e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1625e098bc96SEvan Quan  *
1626e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1627e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1628e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1629e098bc96SEvan Quan  * the log frequency.
1630e098bc96SEvan Quan  *
1631e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1632e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1633e098bc96SEvan Quan  *
1634e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1635e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1636e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1637e098bc96SEvan Quan  */
amdgpu_get_thermal_throttling_logging(struct device * dev,struct device_attribute * attr,char * buf)1638e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1639e098bc96SEvan Quan 						     struct device_attribute *attr,
1640e098bc96SEvan Quan 						     char *buf)
1641e098bc96SEvan Quan {
1642e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16431348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1644e098bc96SEvan Quan 
1645a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
16464a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1647e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1648e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1649e098bc96SEvan Quan }
1650e098bc96SEvan Quan 
amdgpu_set_thermal_throttling_logging(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1651e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1652e098bc96SEvan Quan 						     struct device_attribute *attr,
1653e098bc96SEvan Quan 						     const char *buf,
1654e098bc96SEvan Quan 						     size_t count)
1655e098bc96SEvan Quan {
1656e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16571348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1658e098bc96SEvan Quan 	long throttling_logging_interval;
1659e098bc96SEvan Quan 	unsigned long flags;
1660e098bc96SEvan Quan 	int ret = 0;
1661e098bc96SEvan Quan 
1662e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1663e098bc96SEvan Quan 	if (ret)
1664e098bc96SEvan Quan 		return ret;
1665e098bc96SEvan Quan 
1666e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1667e098bc96SEvan Quan 		return -EINVAL;
1668e098bc96SEvan Quan 
1669e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1670e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1671e098bc96SEvan Quan 		/*
1672e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1673e098bc96SEvan Quan 		 * This can effectively restart the timer.
1674e098bc96SEvan Quan 		 */
1675e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1676e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1677e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1678e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1679e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1680e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1681e098bc96SEvan Quan 
1682e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1683e098bc96SEvan Quan 	} else {
1684e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1685e098bc96SEvan Quan 	}
1686e098bc96SEvan Quan 
1687e098bc96SEvan Quan 	return count;
1688e098bc96SEvan Quan }
1689e098bc96SEvan Quan 
1690e098bc96SEvan Quan /**
1691c3ed0e72SKun Liu  * DOC: apu_thermal_cap
1692c3ed0e72SKun Liu  *
1693c3ed0e72SKun Liu  * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1694c3ed0e72SKun Liu  * limit temperature in millidegrees Celsius
1695c3ed0e72SKun Liu  *
1696c3ed0e72SKun Liu  * Reading back the file shows you core limit value
1697c3ed0e72SKun Liu  *
1698c3ed0e72SKun Liu  * Writing an integer to the file, sets a new thermal limit. The value
1699c3ed0e72SKun Liu  * should be between 0 and 100. If the value is less than 0 or greater
1700c3ed0e72SKun Liu  * than 100, then the write request will be ignored.
1701c3ed0e72SKun Liu  */
amdgpu_get_apu_thermal_cap(struct device * dev,struct device_attribute * attr,char * buf)1702c3ed0e72SKun Liu static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1703c3ed0e72SKun Liu 					 struct device_attribute *attr,
1704c3ed0e72SKun Liu 					 char *buf)
1705c3ed0e72SKun Liu {
1706c3ed0e72SKun Liu 	int ret, size;
1707c3ed0e72SKun Liu 	u32 limit;
1708c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1709c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1710c3ed0e72SKun Liu 
1711c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1712c3ed0e72SKun Liu 	if (ret < 0) {
1713c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1714c3ed0e72SKun Liu 		return ret;
1715c3ed0e72SKun Liu 	}
1716c3ed0e72SKun Liu 
1717c3ed0e72SKun Liu 	ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1718c3ed0e72SKun Liu 	if (!ret)
1719c3ed0e72SKun Liu 		size = sysfs_emit(buf, "%u\n", limit);
1720c3ed0e72SKun Liu 	else
1721c3ed0e72SKun Liu 		size = sysfs_emit(buf, "failed to get thermal limit\n");
1722c3ed0e72SKun Liu 
1723c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1724c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1725c3ed0e72SKun Liu 
1726c3ed0e72SKun Liu 	return size;
1727c3ed0e72SKun Liu }
1728c3ed0e72SKun Liu 
amdgpu_set_apu_thermal_cap(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1729c3ed0e72SKun Liu static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1730c3ed0e72SKun Liu 					 struct device_attribute *attr,
1731c3ed0e72SKun Liu 					 const char *buf,
1732c3ed0e72SKun Liu 					 size_t count)
1733c3ed0e72SKun Liu {
1734c3ed0e72SKun Liu 	int ret;
1735c3ed0e72SKun Liu 	u32 value;
1736c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1737c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1738c3ed0e72SKun Liu 
1739c3ed0e72SKun Liu 	ret = kstrtou32(buf, 10, &value);
1740c3ed0e72SKun Liu 	if (ret)
1741c3ed0e72SKun Liu 		return ret;
1742c3ed0e72SKun Liu 
17434d2c09d6SMuhammad Usama Anjum 	if (value > 100) {
1744c3ed0e72SKun Liu 		dev_err(dev, "Invalid argument !\n");
1745c3ed0e72SKun Liu 		return -EINVAL;
1746c3ed0e72SKun Liu 	}
1747c3ed0e72SKun Liu 
1748c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1749c3ed0e72SKun Liu 	if (ret < 0) {
1750c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1751c3ed0e72SKun Liu 		return ret;
1752c3ed0e72SKun Liu 	}
1753c3ed0e72SKun Liu 
1754c3ed0e72SKun Liu 	ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1755c3ed0e72SKun Liu 	if (ret) {
1756c3ed0e72SKun Liu 		dev_err(dev, "failed to update thermal limit\n");
1757c3ed0e72SKun Liu 		return ret;
1758c3ed0e72SKun Liu 	}
1759c3ed0e72SKun Liu 
1760c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1761c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1762c3ed0e72SKun Liu 
1763c3ed0e72SKun Liu 	return count;
1764c3ed0e72SKun Liu }
1765c3ed0e72SKun Liu 
1766c3ed0e72SKun Liu /**
1767e098bc96SEvan Quan  * DOC: gpu_metrics
1768e098bc96SEvan Quan  *
1769e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1770e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1771e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1772e098bc96SEvan Quan  *
1773e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1774e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1775e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1776e098bc96SEvan Quan  * at the same time.
1777e098bc96SEvan Quan  */
amdgpu_get_gpu_metrics(struct device * dev,struct device_attribute * attr,char * buf)1778e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1779e098bc96SEvan Quan 				      struct device_attribute *attr,
1780e098bc96SEvan Quan 				      char *buf)
1781e098bc96SEvan Quan {
1782e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17831348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1784e098bc96SEvan Quan 	void *gpu_metrics;
1785e098bc96SEvan Quan 	ssize_t size = 0;
1786e098bc96SEvan Quan 	int ret;
1787e098bc96SEvan Quan 
178853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1789e098bc96SEvan Quan 		return -EPERM;
1790d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1791d2ae842dSAlex Deucher 		return -EPERM;
1792e098bc96SEvan Quan 
1793e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1794e098bc96SEvan Quan 	if (ret < 0) {
1795e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1796e098bc96SEvan Quan 		return ret;
1797e098bc96SEvan Quan 	}
1798e098bc96SEvan Quan 
1799e098bc96SEvan Quan 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1800e098bc96SEvan Quan 	if (size <= 0)
1801e098bc96SEvan Quan 		goto out;
1802e098bc96SEvan Quan 
1803e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1804e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1805e098bc96SEvan Quan 
1806e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1807e098bc96SEvan Quan 
1808e098bc96SEvan Quan out:
1809e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1810e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1811e098bc96SEvan Quan 
1812e098bc96SEvan Quan 	return size;
1813e098bc96SEvan Quan }
1814e098bc96SEvan Quan 
amdgpu_show_powershift_percent(struct device * dev,char * buf,enum amd_pp_sensors sensor)1815494c1432SSathishkumar S static int amdgpu_show_powershift_percent(struct device *dev,
1816d78c227fSMario Limonciello 					char *buf, enum amd_pp_sensors sensor)
1817494c1432SSathishkumar S {
1818494c1432SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1819494c1432SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1820494c1432SSathishkumar S 	uint32_t ss_power;
1821494c1432SSathishkumar S 	int r = 0, i;
1822494c1432SSathishkumar S 
1823d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1824494c1432SSathishkumar S 	if (r == -EOPNOTSUPP) {
1825494c1432SSathishkumar S 		/* sensor not available on dGPU, try to read from APU */
1826494c1432SSathishkumar S 		adev = NULL;
1827494c1432SSathishkumar S 		mutex_lock(&mgpu_info.mutex);
1828494c1432SSathishkumar S 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1829494c1432SSathishkumar S 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1830494c1432SSathishkumar S 				adev = mgpu_info.gpu_ins[i].adev;
1831494c1432SSathishkumar S 				break;
1832494c1432SSathishkumar S 			}
1833494c1432SSathishkumar S 		}
1834494c1432SSathishkumar S 		mutex_unlock(&mgpu_info.mutex);
1835494c1432SSathishkumar S 		if (adev)
1836d78c227fSMario Limonciello 			r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1837494c1432SSathishkumar S 	}
1838494c1432SSathishkumar S 
1839d78c227fSMario Limonciello 	if (r)
1840494c1432SSathishkumar S 		return r;
1841d78c227fSMario Limonciello 
1842d78c227fSMario Limonciello 	return sysfs_emit(buf, "%u%%\n", ss_power);
1843494c1432SSathishkumar S }
1844d78c227fSMario Limonciello 
1845a7673a1cSSathishkumar S /**
1846a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1847a7673a1cSSathishkumar S  *
1848a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1849494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1850494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power
1851494c1432SSathishkumar S  * is shifted to APU, the percentage of boost is with respect to APU power
1852494c1432SSathishkumar S  * limit on the platform.
1853a7673a1cSSathishkumar S  */
1854a7673a1cSSathishkumar S 
amdgpu_get_smartshift_apu_power(struct device * dev,struct device_attribute * attr,char * buf)1855a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1856a7673a1cSSathishkumar S 					       char *buf)
1857a7673a1cSSathishkumar S {
1858d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1859a7673a1cSSathishkumar S }
1860a7673a1cSSathishkumar S 
1861a7673a1cSSathishkumar S /**
1862a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1863a7673a1cSSathishkumar S  *
1864494c1432SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting dGPU power
1865494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1866494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power is
1867494c1432SSathishkumar S  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1868494c1432SSathishkumar S  * limit on the platform.
1869a7673a1cSSathishkumar S  */
1870a7673a1cSSathishkumar S 
amdgpu_get_smartshift_dgpu_power(struct device * dev,struct device_attribute * attr,char * buf)1871a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1872a7673a1cSSathishkumar S 						char *buf)
1873a7673a1cSSathishkumar S {
1874d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1875a7673a1cSSathishkumar S }
1876a7673a1cSSathishkumar S 
187730d95a37SSathishkumar S /**
187830d95a37SSathishkumar S  * DOC: smartshift_bias
187930d95a37SSathishkumar S  *
188030d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
188130d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
188230d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
188330d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
188430d95a37SSathishkumar S  */
188530d95a37SSathishkumar S 
amdgpu_get_smartshift_bias(struct device * dev,struct device_attribute * attr,char * buf)188630d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
188730d95a37SSathishkumar S 					  struct device_attribute *attr,
188830d95a37SSathishkumar S 					  char *buf)
188930d95a37SSathishkumar S {
189030d95a37SSathishkumar S 	int r = 0;
189130d95a37SSathishkumar S 
189230d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
189330d95a37SSathishkumar S 
189430d95a37SSathishkumar S 	return r;
189530d95a37SSathishkumar S }
189630d95a37SSathishkumar S 
amdgpu_set_smartshift_bias(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)189730d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
189830d95a37SSathishkumar S 					  struct device_attribute *attr,
189930d95a37SSathishkumar S 					  const char *buf, size_t count)
190030d95a37SSathishkumar S {
190130d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
190230d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
190330d95a37SSathishkumar S 	int r = 0;
190430d95a37SSathishkumar S 	int bias = 0;
190530d95a37SSathishkumar S 
190630d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
190730d95a37SSathishkumar S 		return -EPERM;
190830d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
190930d95a37SSathishkumar S 		return -EPERM;
191030d95a37SSathishkumar S 
191130d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
191230d95a37SSathishkumar S 	if (r < 0) {
191330d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
191430d95a37SSathishkumar S 		return r;
191530d95a37SSathishkumar S 	}
191630d95a37SSathishkumar S 
191730d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
191830d95a37SSathishkumar S 	if (r)
191930d95a37SSathishkumar S 		goto out;
192030d95a37SSathishkumar S 
192130d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
192230d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
192330d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
192430d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
192530d95a37SSathishkumar S 
192630d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
192730d95a37SSathishkumar S 	r = count;
192830d95a37SSathishkumar S 
1929bd4b9bb7SJulia Lawall 	/* TODO: update bias level with SMU message */
193030d95a37SSathishkumar S 
193130d95a37SSathishkumar S out:
193230d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
193330d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
193430d95a37SSathishkumar S 	return r;
193530d95a37SSathishkumar S }
193630d95a37SSathishkumar S 
ss_power_attr_update(struct amdgpu_device * adev,struct amdgpu_device_attr * attr,uint32_t mask,enum amdgpu_device_attr_states * states)1937a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1938a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
1939a7673a1cSSathishkumar S {
1940494c1432SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1941a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1942a7673a1cSSathishkumar S 
1943a7673a1cSSathishkumar S 	return 0;
1944a7673a1cSSathishkumar S }
1945a7673a1cSSathishkumar S 
ss_bias_attr_update(struct amdgpu_device * adev,struct amdgpu_device_attr * attr,uint32_t mask,enum amdgpu_device_attr_states * states)194630d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
194730d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
194830d95a37SSathishkumar S {
1949d78c227fSMario Limonciello 	uint32_t ss_power;
195030d95a37SSathishkumar S 
195130d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
195230d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1953d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1954d78c227fSMario Limonciello 		 (void *)&ss_power))
195530d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1956d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1957d78c227fSMario Limonciello 		 (void *)&ss_power))
195830d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
195930d95a37SSathishkumar S 
196030d95a37SSathishkumar S 	return 0;
196130d95a37SSathishkumar S }
196230d95a37SSathishkumar S 
1963e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1964e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19654215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19667884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19677884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19687884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19697884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1970e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1971e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1972e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1973e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19749577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1975d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19769577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1977d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1978f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1979f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1980e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
1981e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
1982ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1983e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
1984ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1985ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1986e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
1987ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1988ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1989ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1990c3ed0e72SKun Liu 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1991ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1992a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
1993a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
1994a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
1995a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
199630d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
199730d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
1998e098bc96SEvan Quan };
1999e098bc96SEvan Quan 
default_attr_update(struct amdgpu_device * adev,struct amdgpu_device_attr * attr,uint32_t mask,enum amdgpu_device_attr_states * states)2000e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2001e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2002e098bc96SEvan Quan {
2003e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
20048ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
20058ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2006e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
2007e098bc96SEvan Quan 
2008e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2009e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2010e098bc96SEvan Quan 		return 0;
2011e098bc96SEvan Quan 	}
2012e098bc96SEvan Quan 
2013e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2014e098bc96SEvan Quan 
2015e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
20168ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0))
2017e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2018e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
20198ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0) ||
20200127ab1bSYang Wang 		    !amdgpu_device_has_display_hardware(adev))
2021e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2022e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
20238ecad8d6SLijo Lazar 		if (mp1_ver < IP_VERSION(10, 0, 0))
2024e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2025e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2026e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
202779c65f3fSEvan Quan 		if (amdgpu_dpm_is_overdrive_supported(adev))
2028e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
2029e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
20308ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1))
2031e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2032e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2033e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
2034e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
2035e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2036e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
203760044748SKent Russell 		switch (gc_ver) {
203860044748SKent Russell 		case IP_VERSION(9, 0, 1):
203960044748SKent Russell 		case IP_VERSION(9, 4, 0):
204060044748SKent Russell 		case IP_VERSION(9, 4, 1):
204160044748SKent Russell 		case IP_VERSION(9, 4, 2):
2042baf65745SLijo Lazar 		case IP_VERSION(9, 4, 3):
2043ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
2044276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
204535e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
204635e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
20474953856fSKenneth Feng 		case IP_VERSION(11, 0, 3):
204860044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
204960044748SKent Russell 			break;
205060044748SKent Russell 		default:
2051e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
205260044748SKent Russell 		}
2053e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2054fc8e84a2SLijo Lazar 		if ((adev->flags & AMD_IS_APU &&
2055fc8e84a2SLijo Lazar 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2056fc8e84a2SLijo Lazar 		    gc_ver < IP_VERSION(9, 0, 0))
2057e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2058e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
20598ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2060e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
20619577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
20628ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2063a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
206464440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
20653929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
20662f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
2067707b570fSAsad Kamal 		      gc_ver == IP_VERSION(11, 0, 3) ||
2068707b570fSAsad Kamal 		      gc_ver == IP_VERSION(9, 4, 3)))
20699577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
20700b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
20710b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2072feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2073feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2074feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
20750b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
20769577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
20778ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2078a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
207964440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
20803929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
20812f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
2082707b570fSAsad Kamal 		      gc_ver == IP_VERSION(11, 0, 3) ||
2083707b570fSAsad Kamal 		      gc_ver == IP_VERSION(9, 4, 3)))
20849577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
20850b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
20860b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2087feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2088feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2089feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
20900b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
2091a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
209279c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2093a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
20941b852572SDanijel Slivka 		else if (gc_ver == IP_VERSION(10, 3, 0) && amdgpu_sriov_vf(adev))
20951b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
2096e098bc96SEvan Quan 	}
2097e098bc96SEvan Quan 
20988ecad8d6SLijo Lazar 	switch (gc_ver) {
20998ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 1):
21008ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 2):
21011d0e622fSKevin Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2102e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2103e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2104e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2105e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2106e098bc96SEvan Quan 			dev_attr->store = NULL;
2107e098bc96SEvan Quan 		}
21081d0e622fSKevin Wang 		break;
21091b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
21101b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
21111b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
21121b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
21131b852572SDanijel Slivka 			dev_attr->store = NULL;
21141b852572SDanijel Slivka 		}
21151b852572SDanijel Slivka 		break;
21161d0e622fSKevin Wang 	default:
21171d0e622fSKevin Wang 		break;
2118e098bc96SEvan Quan 	}
2119e098bc96SEvan Quan 
2120ede14a1bSDarren Powell 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2121ede14a1bSDarren Powell 		/* SMU MP1 does not support dcefclk level setting */
21228ecad8d6SLijo Lazar 		if (gc_ver >= IP_VERSION(10, 0, 0)) {
2123ede14a1bSDarren Powell 			dev_attr->attr.mode &= ~S_IWUGO;
2124ede14a1bSDarren Powell 			dev_attr->store = NULL;
2125ede14a1bSDarren Powell 		}
2126ede14a1bSDarren Powell 	}
2127ede14a1bSDarren Powell 
2128e610941cSYiqing Yao 	/* setting should not be allowed from VF if not in one VF mode */
2129e610941cSYiqing Yao 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
213011c9cc95SMarina Nikolic 		dev_attr->attr.mode &= ~S_IWUGO;
213111c9cc95SMarina Nikolic 		dev_attr->store = NULL;
213211c9cc95SMarina Nikolic 	}
213311c9cc95SMarina Nikolic 
2134e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2135e098bc96SEvan Quan 
2136e098bc96SEvan Quan 	return 0;
2137e098bc96SEvan Quan }
2138e098bc96SEvan Quan 
2139e098bc96SEvan Quan 
amdgpu_device_attr_create(struct amdgpu_device * adev,struct amdgpu_device_attr * attr,uint32_t mask,struct list_head * attr_list)2140e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2141e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2142e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2143e098bc96SEvan Quan {
2144e098bc96SEvan Quan 	int ret = 0;
2145e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2146e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
214725e6373aSYang Wang 	struct device_attribute *dev_attr;
214825e6373aSYang Wang 	const char *name;
2149e098bc96SEvan Quan 
2150e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2151e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2152e098bc96SEvan Quan 
215325e6373aSYang Wang 	if (!attr)
215425e6373aSYang Wang 		return -EINVAL;
215525e6373aSYang Wang 
215625e6373aSYang Wang 	dev_attr = &attr->dev_attr;
215725e6373aSYang Wang 	name = dev_attr->attr.name;
2158e098bc96SEvan Quan 
21598a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2160e098bc96SEvan Quan 
2161e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2162e098bc96SEvan Quan 	if (ret) {
2163e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2164e098bc96SEvan Quan 			name, ret);
2165e098bc96SEvan Quan 		return ret;
2166e098bc96SEvan Quan 	}
2167e098bc96SEvan Quan 
2168e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2169e098bc96SEvan Quan 		return 0;
2170e098bc96SEvan Quan 
2171e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2172e098bc96SEvan Quan 	if (ret) {
2173e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2174e098bc96SEvan Quan 			name, ret);
2175e098bc96SEvan Quan 	}
2176e098bc96SEvan Quan 
2177e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2178e098bc96SEvan Quan 	if (!attr_entry)
2179e098bc96SEvan Quan 		return -ENOMEM;
2180e098bc96SEvan Quan 
2181e098bc96SEvan Quan 	attr_entry->attr = attr;
2182e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2183e098bc96SEvan Quan 
2184e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2185e098bc96SEvan Quan 
2186e098bc96SEvan Quan 	return ret;
2187e098bc96SEvan Quan }
2188e098bc96SEvan Quan 
amdgpu_device_attr_remove(struct amdgpu_device * adev,struct amdgpu_device_attr * attr)2189e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2190e098bc96SEvan Quan {
2191e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2192e098bc96SEvan Quan 
2193e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2194e098bc96SEvan Quan }
2195e098bc96SEvan Quan 
2196e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2197e098bc96SEvan Quan 					     struct list_head *attr_list);
2198e098bc96SEvan Quan 
amdgpu_device_attr_create_groups(struct amdgpu_device * adev,struct amdgpu_device_attr * attrs,uint32_t counts,uint32_t mask,struct list_head * attr_list)2199e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2200e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2201e098bc96SEvan Quan 					    uint32_t counts,
2202e098bc96SEvan Quan 					    uint32_t mask,
2203e098bc96SEvan Quan 					    struct list_head *attr_list)
2204e098bc96SEvan Quan {
2205e098bc96SEvan Quan 	int ret = 0;
2206e098bc96SEvan Quan 	uint32_t i = 0;
2207e098bc96SEvan Quan 
2208e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2209e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2210e098bc96SEvan Quan 		if (ret)
2211e098bc96SEvan Quan 			goto failed;
2212e098bc96SEvan Quan 	}
2213e098bc96SEvan Quan 
2214e098bc96SEvan Quan 	return 0;
2215e098bc96SEvan Quan 
2216e098bc96SEvan Quan failed:
2217e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2218e098bc96SEvan Quan 
2219e098bc96SEvan Quan 	return ret;
2220e098bc96SEvan Quan }
2221e098bc96SEvan Quan 
amdgpu_device_attr_remove_groups(struct amdgpu_device * adev,struct list_head * attr_list)2222e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2223e098bc96SEvan Quan 					     struct list_head *attr_list)
2224e098bc96SEvan Quan {
2225e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2226e098bc96SEvan Quan 
2227e098bc96SEvan Quan 	if (list_empty(attr_list))
2228e098bc96SEvan Quan 		return ;
2229e098bc96SEvan Quan 
2230e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2231e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2232e098bc96SEvan Quan 		list_del(&entry->entry);
2233e098bc96SEvan Quan 		kfree(entry);
2234e098bc96SEvan Quan 	}
2235e098bc96SEvan Quan }
2236e098bc96SEvan Quan 
amdgpu_hwmon_show_temp(struct device * dev,struct device_attribute * attr,char * buf)2237e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2238e098bc96SEvan Quan 				      struct device_attribute *attr,
2239e098bc96SEvan Quan 				      char *buf)
2240e098bc96SEvan Quan {
2241e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2242e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2243d78c227fSMario Limonciello 	int r, temp = 0;
2244e098bc96SEvan Quan 
2245e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2246e098bc96SEvan Quan 		return -EINVAL;
2247e098bc96SEvan Quan 
2248e098bc96SEvan Quan 	switch (channel) {
2249e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2250e098bc96SEvan Quan 		/* get current junction temperature */
2251d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2252d78c227fSMario Limonciello 					   (void *)&temp);
2253e098bc96SEvan Quan 		break;
2254e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2255e098bc96SEvan Quan 		/* get current edge temperature */
2256d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2257d78c227fSMario Limonciello 					   (void *)&temp);
2258e098bc96SEvan Quan 		break;
2259e098bc96SEvan Quan 	case PP_TEMP_MEM:
2260e098bc96SEvan Quan 		/* get current memory temperature */
2261d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2262d78c227fSMario Limonciello 					   (void *)&temp);
2263e098bc96SEvan Quan 		break;
2264e098bc96SEvan Quan 	default:
2265e098bc96SEvan Quan 		r = -EINVAL;
2266e098bc96SEvan Quan 		break;
2267e098bc96SEvan Quan 	}
2268e098bc96SEvan Quan 
2269e098bc96SEvan Quan 	if (r)
2270e098bc96SEvan Quan 		return r;
2271e098bc96SEvan Quan 
2272a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2273e098bc96SEvan Quan }
2274e098bc96SEvan Quan 
amdgpu_hwmon_show_temp_thresh(struct device * dev,struct device_attribute * attr,char * buf)2275e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2276e098bc96SEvan Quan 					     struct device_attribute *attr,
2277e098bc96SEvan Quan 					     char *buf)
2278e098bc96SEvan Quan {
2279e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2280e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2281e098bc96SEvan Quan 	int temp;
2282e098bc96SEvan Quan 
2283e098bc96SEvan Quan 	if (hyst)
2284e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2285e098bc96SEvan Quan 	else
2286e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2287e098bc96SEvan Quan 
2288a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2289e098bc96SEvan Quan }
2290e098bc96SEvan Quan 
amdgpu_hwmon_show_hotspot_temp_thresh(struct device * dev,struct device_attribute * attr,char * buf)2291e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2292e098bc96SEvan Quan 					     struct device_attribute *attr,
2293e098bc96SEvan Quan 					     char *buf)
2294e098bc96SEvan Quan {
2295e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2296e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2297e098bc96SEvan Quan 	int temp;
2298e098bc96SEvan Quan 
2299e098bc96SEvan Quan 	if (hyst)
2300e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2301e098bc96SEvan Quan 	else
2302e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2303e098bc96SEvan Quan 
2304a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2305e098bc96SEvan Quan }
2306e098bc96SEvan Quan 
amdgpu_hwmon_show_mem_temp_thresh(struct device * dev,struct device_attribute * attr,char * buf)2307e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2308e098bc96SEvan Quan 					     struct device_attribute *attr,
2309e098bc96SEvan Quan 					     char *buf)
2310e098bc96SEvan Quan {
2311e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2312e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2313e098bc96SEvan Quan 	int temp;
2314e098bc96SEvan Quan 
2315e098bc96SEvan Quan 	if (hyst)
2316e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2317e098bc96SEvan Quan 	else
2318e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2319e098bc96SEvan Quan 
2320a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2321e098bc96SEvan Quan }
2322e098bc96SEvan Quan 
amdgpu_hwmon_show_temp_label(struct device * dev,struct device_attribute * attr,char * buf)2323e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2324e098bc96SEvan Quan 					     struct device_attribute *attr,
2325e098bc96SEvan Quan 					     char *buf)
2326e098bc96SEvan Quan {
2327e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2328e098bc96SEvan Quan 
2329e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2330e098bc96SEvan Quan 		return -EINVAL;
2331e098bc96SEvan Quan 
2332a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2333e098bc96SEvan Quan }
2334e098bc96SEvan Quan 
amdgpu_hwmon_show_temp_emergency(struct device * dev,struct device_attribute * attr,char * buf)2335e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2336e098bc96SEvan Quan 					     struct device_attribute *attr,
2337e098bc96SEvan Quan 					     char *buf)
2338e098bc96SEvan Quan {
2339e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2340e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2341e098bc96SEvan Quan 	int temp = 0;
2342e098bc96SEvan Quan 
2343e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2344e098bc96SEvan Quan 		return -EINVAL;
2345e098bc96SEvan Quan 
2346e098bc96SEvan Quan 	switch (channel) {
2347e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2348e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2349e098bc96SEvan Quan 		break;
2350e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2351e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2352e098bc96SEvan Quan 		break;
2353e098bc96SEvan Quan 	case PP_TEMP_MEM:
2354e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2355e098bc96SEvan Quan 		break;
2356e098bc96SEvan Quan 	}
2357e098bc96SEvan Quan 
2358a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2359e098bc96SEvan Quan }
2360e098bc96SEvan Quan 
amdgpu_hwmon_get_pwm1_enable(struct device * dev,struct device_attribute * attr,char * buf)2361e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2362e098bc96SEvan Quan 					    struct device_attribute *attr,
2363e098bc96SEvan Quan 					    char *buf)
2364e098bc96SEvan Quan {
2365e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2366e098bc96SEvan Quan 	u32 pwm_mode = 0;
2367e098bc96SEvan Quan 	int ret;
2368e098bc96SEvan Quan 
236953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2370e098bc96SEvan Quan 		return -EPERM;
2371d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2372d2ae842dSAlex Deucher 		return -EPERM;
2373e098bc96SEvan Quan 
23744a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2375e098bc96SEvan Quan 	if (ret < 0) {
23764a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2377e098bc96SEvan Quan 		return ret;
2378e098bc96SEvan Quan 	}
2379e098bc96SEvan Quan 
238079c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
238179c65f3fSEvan Quan 
23824a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
23834a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
238479c65f3fSEvan Quan 
238579c65f3fSEvan Quan 	if (ret)
2386e098bc96SEvan Quan 		return -EINVAL;
2387e098bc96SEvan Quan 
2388fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2389e098bc96SEvan Quan }
2390e098bc96SEvan Quan 
amdgpu_hwmon_set_pwm1_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2391e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2392e098bc96SEvan Quan 					    struct device_attribute *attr,
2393e098bc96SEvan Quan 					    const char *buf,
2394e098bc96SEvan Quan 					    size_t count)
2395e098bc96SEvan Quan {
2396e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2397e098bc96SEvan Quan 	int err, ret;
23986f3c1dabSMa Jun 	u32 pwm_mode;
2399e098bc96SEvan Quan 	int value;
2400e098bc96SEvan Quan 
240153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2402e098bc96SEvan Quan 		return -EPERM;
2403d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2404d2ae842dSAlex Deucher 		return -EPERM;
2405e098bc96SEvan Quan 
2406e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2407e098bc96SEvan Quan 	if (err)
2408e098bc96SEvan Quan 		return err;
2409e098bc96SEvan Quan 
24106f3c1dabSMa Jun 	if (value == 0)
24116f3c1dabSMa Jun 		pwm_mode = AMD_FAN_CTRL_NONE;
24126f3c1dabSMa Jun 	else if (value == 1)
24136f3c1dabSMa Jun 		pwm_mode = AMD_FAN_CTRL_MANUAL;
24146f3c1dabSMa Jun 	else if (value == 2)
24156f3c1dabSMa Jun 		pwm_mode = AMD_FAN_CTRL_AUTO;
24166f3c1dabSMa Jun 	else
24176f3c1dabSMa Jun 		return -EINVAL;
24186f3c1dabSMa Jun 
24194a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2420e098bc96SEvan Quan 	if (ret < 0) {
24214a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2422e098bc96SEvan Quan 		return ret;
2423e098bc96SEvan Quan 	}
2424e098bc96SEvan Quan 
24256f3c1dabSMa Jun 	ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
242679c65f3fSEvan Quan 
24274a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24284a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
242979c65f3fSEvan Quan 
243079c65f3fSEvan Quan 	if (ret)
2431e098bc96SEvan Quan 		return -EINVAL;
2432e098bc96SEvan Quan 
2433e098bc96SEvan Quan 	return count;
2434e098bc96SEvan Quan }
2435e098bc96SEvan Quan 
amdgpu_hwmon_get_pwm1_min(struct device * dev,struct device_attribute * attr,char * buf)2436e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2437e098bc96SEvan Quan 					 struct device_attribute *attr,
2438e098bc96SEvan Quan 					 char *buf)
2439e098bc96SEvan Quan {
2440fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2441e098bc96SEvan Quan }
2442e098bc96SEvan Quan 
amdgpu_hwmon_get_pwm1_max(struct device * dev,struct device_attribute * attr,char * buf)2443e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2444e098bc96SEvan Quan 					 struct device_attribute *attr,
2445e098bc96SEvan Quan 					 char *buf)
2446e098bc96SEvan Quan {
2447fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2448e098bc96SEvan Quan }
2449e098bc96SEvan Quan 
amdgpu_hwmon_set_pwm1(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2450e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2451e098bc96SEvan Quan 				     struct device_attribute *attr,
2452e098bc96SEvan Quan 				     const char *buf, size_t count)
2453e098bc96SEvan Quan {
2454e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2455e098bc96SEvan Quan 	int err;
2456e098bc96SEvan Quan 	u32 value;
2457e098bc96SEvan Quan 	u32 pwm_mode;
2458e098bc96SEvan Quan 
245953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2460e098bc96SEvan Quan 		return -EPERM;
2461d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2462d2ae842dSAlex Deucher 		return -EPERM;
2463e098bc96SEvan Quan 
246479c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
246579c65f3fSEvan Quan 	if (err)
246679c65f3fSEvan Quan 		return err;
246779c65f3fSEvan Quan 
24684a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2469e098bc96SEvan Quan 	if (err < 0) {
24704a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2471e098bc96SEvan Quan 		return err;
2472e098bc96SEvan Quan 	}
2473e098bc96SEvan Quan 
247479c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
247579c65f3fSEvan Quan 	if (err)
247679c65f3fSEvan Quan 		goto out;
247779c65f3fSEvan Quan 
2478e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2479e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2480e098bc96SEvan Quan 		err = -EINVAL;
248179c65f3fSEvan Quan 		goto out;
248279c65f3fSEvan Quan 	}
2483e098bc96SEvan Quan 
248479c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
248579c65f3fSEvan Quan 
248679c65f3fSEvan Quan out:
24874a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24884a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2489e098bc96SEvan Quan 
2490e098bc96SEvan Quan 	if (err)
2491e098bc96SEvan Quan 		return err;
2492e098bc96SEvan Quan 
2493e098bc96SEvan Quan 	return count;
2494e098bc96SEvan Quan }
2495e098bc96SEvan Quan 
amdgpu_hwmon_get_pwm1(struct device * dev,struct device_attribute * attr,char * buf)2496e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2497e098bc96SEvan Quan 				     struct device_attribute *attr,
2498e098bc96SEvan Quan 				     char *buf)
2499e098bc96SEvan Quan {
2500e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2501e098bc96SEvan Quan 	int err;
2502e098bc96SEvan Quan 	u32 speed = 0;
2503e098bc96SEvan Quan 
250453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2505e098bc96SEvan Quan 		return -EPERM;
2506d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2507d2ae842dSAlex Deucher 		return -EPERM;
2508e098bc96SEvan Quan 
25094a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2510e098bc96SEvan Quan 	if (err < 0) {
25114a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2512e098bc96SEvan Quan 		return err;
2513e098bc96SEvan Quan 	}
2514e098bc96SEvan Quan 
25150d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2516e098bc96SEvan Quan 
25174a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25184a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2519e098bc96SEvan Quan 
2520e098bc96SEvan Quan 	if (err)
2521e098bc96SEvan Quan 		return err;
2522e098bc96SEvan Quan 
2523fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2524e098bc96SEvan Quan }
2525e098bc96SEvan Quan 
amdgpu_hwmon_get_fan1_input(struct device * dev,struct device_attribute * attr,char * buf)2526e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2527e098bc96SEvan Quan 					   struct device_attribute *attr,
2528e098bc96SEvan Quan 					   char *buf)
2529e098bc96SEvan Quan {
2530e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2531e098bc96SEvan Quan 	int err;
2532e098bc96SEvan Quan 	u32 speed = 0;
2533e098bc96SEvan Quan 
253453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2535e098bc96SEvan Quan 		return -EPERM;
2536d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2537d2ae842dSAlex Deucher 		return -EPERM;
2538e098bc96SEvan Quan 
25394a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2540e098bc96SEvan Quan 	if (err < 0) {
25414a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2542e098bc96SEvan Quan 		return err;
2543e098bc96SEvan Quan 	}
2544e098bc96SEvan Quan 
2545e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2546e098bc96SEvan Quan 
25474a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25484a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2549e098bc96SEvan Quan 
2550e098bc96SEvan Quan 	if (err)
2551e098bc96SEvan Quan 		return err;
2552e098bc96SEvan Quan 
2553fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2554e098bc96SEvan Quan }
2555e098bc96SEvan Quan 
amdgpu_hwmon_get_fan1_min(struct device * dev,struct device_attribute * attr,char * buf)2556e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2557e098bc96SEvan Quan 					 struct device_attribute *attr,
2558e098bc96SEvan Quan 					 char *buf)
2559e098bc96SEvan Quan {
2560e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2561e098bc96SEvan Quan 	u32 min_rpm = 0;
2562e098bc96SEvan Quan 	int r;
2563e098bc96SEvan Quan 
2564d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2565d78c227fSMario Limonciello 				   (void *)&min_rpm);
2566e098bc96SEvan Quan 
2567e098bc96SEvan Quan 	if (r)
2568e098bc96SEvan Quan 		return r;
2569e098bc96SEvan Quan 
2570a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2571e098bc96SEvan Quan }
2572e098bc96SEvan Quan 
amdgpu_hwmon_get_fan1_max(struct device * dev,struct device_attribute * attr,char * buf)2573e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2574e098bc96SEvan Quan 					 struct device_attribute *attr,
2575e098bc96SEvan Quan 					 char *buf)
2576e098bc96SEvan Quan {
2577e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2578e098bc96SEvan Quan 	u32 max_rpm = 0;
2579e098bc96SEvan Quan 	int r;
2580e098bc96SEvan Quan 
2581d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2582d78c227fSMario Limonciello 				   (void *)&max_rpm);
2583e098bc96SEvan Quan 
2584e098bc96SEvan Quan 	if (r)
2585e098bc96SEvan Quan 		return r;
2586e098bc96SEvan Quan 
2587a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2588e098bc96SEvan Quan }
2589e098bc96SEvan Quan 
amdgpu_hwmon_get_fan1_target(struct device * dev,struct device_attribute * attr,char * buf)2590e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2591e098bc96SEvan Quan 					   struct device_attribute *attr,
2592e098bc96SEvan Quan 					   char *buf)
2593e098bc96SEvan Quan {
2594e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2595e098bc96SEvan Quan 	int err;
2596e098bc96SEvan Quan 	u32 rpm = 0;
2597e098bc96SEvan Quan 
259853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2599e098bc96SEvan Quan 		return -EPERM;
2600d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2601d2ae842dSAlex Deucher 		return -EPERM;
2602e098bc96SEvan Quan 
26034a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2604e098bc96SEvan Quan 	if (err < 0) {
26054a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2606e098bc96SEvan Quan 		return err;
2607e098bc96SEvan Quan 	}
2608e098bc96SEvan Quan 
2609e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2610e098bc96SEvan Quan 
26114a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26124a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2613e098bc96SEvan Quan 
2614e098bc96SEvan Quan 	if (err)
2615e098bc96SEvan Quan 		return err;
2616e098bc96SEvan Quan 
2617fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2618e098bc96SEvan Quan }
2619e098bc96SEvan Quan 
amdgpu_hwmon_set_fan1_target(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2620e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2621e098bc96SEvan Quan 				     struct device_attribute *attr,
2622e098bc96SEvan Quan 				     const char *buf, size_t count)
2623e098bc96SEvan Quan {
2624e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2625e098bc96SEvan Quan 	int err;
2626e098bc96SEvan Quan 	u32 value;
2627e098bc96SEvan Quan 	u32 pwm_mode;
2628e098bc96SEvan Quan 
262953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2630e098bc96SEvan Quan 		return -EPERM;
2631d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2632d2ae842dSAlex Deucher 		return -EPERM;
2633e098bc96SEvan Quan 
263479c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
263579c65f3fSEvan Quan 	if (err)
263679c65f3fSEvan Quan 		return err;
263779c65f3fSEvan Quan 
26384a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2639e098bc96SEvan Quan 	if (err < 0) {
26404a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2641e098bc96SEvan Quan 		return err;
2642e098bc96SEvan Quan 	}
2643e098bc96SEvan Quan 
264479c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
264579c65f3fSEvan Quan 	if (err)
264679c65f3fSEvan Quan 		goto out;
2647e098bc96SEvan Quan 
2648e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
264979c65f3fSEvan Quan 		err = -ENODATA;
265079c65f3fSEvan Quan 		goto out;
2651e098bc96SEvan Quan 	}
2652e098bc96SEvan Quan 
2653e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2654e098bc96SEvan Quan 
265579c65f3fSEvan Quan out:
26564a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26574a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2658e098bc96SEvan Quan 
2659e098bc96SEvan Quan 	if (err)
2660e098bc96SEvan Quan 		return err;
2661e098bc96SEvan Quan 
2662e098bc96SEvan Quan 	return count;
2663e098bc96SEvan Quan }
2664e098bc96SEvan Quan 
amdgpu_hwmon_get_fan1_enable(struct device * dev,struct device_attribute * attr,char * buf)2665e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2666e098bc96SEvan Quan 					    struct device_attribute *attr,
2667e098bc96SEvan Quan 					    char *buf)
2668e098bc96SEvan Quan {
2669e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2670e098bc96SEvan Quan 	u32 pwm_mode = 0;
2671e098bc96SEvan Quan 	int ret;
2672e098bc96SEvan Quan 
267353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2674e098bc96SEvan Quan 		return -EPERM;
2675d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2676d2ae842dSAlex Deucher 		return -EPERM;
2677e098bc96SEvan Quan 
26784a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2679e098bc96SEvan Quan 	if (ret < 0) {
26804a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2681e098bc96SEvan Quan 		return ret;
2682e098bc96SEvan Quan 	}
2683e098bc96SEvan Quan 
268479c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
268579c65f3fSEvan Quan 
26864a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26874a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
268879c65f3fSEvan Quan 
268979c65f3fSEvan Quan 	if (ret)
2690e098bc96SEvan Quan 		return -EINVAL;
2691e098bc96SEvan Quan 
2692fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2693e098bc96SEvan Quan }
2694e098bc96SEvan Quan 
amdgpu_hwmon_set_fan1_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2695e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2696e098bc96SEvan Quan 					    struct device_attribute *attr,
2697e098bc96SEvan Quan 					    const char *buf,
2698e098bc96SEvan Quan 					    size_t count)
2699e098bc96SEvan Quan {
2700e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2701e098bc96SEvan Quan 	int err;
2702e098bc96SEvan Quan 	int value;
2703e098bc96SEvan Quan 	u32 pwm_mode;
2704e098bc96SEvan Quan 
270553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2706e098bc96SEvan Quan 		return -EPERM;
2707d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2708d2ae842dSAlex Deucher 		return -EPERM;
2709e098bc96SEvan Quan 
2710e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2711e098bc96SEvan Quan 	if (err)
2712e098bc96SEvan Quan 		return err;
2713e098bc96SEvan Quan 
2714e098bc96SEvan Quan 	if (value == 0)
2715e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2716e098bc96SEvan Quan 	else if (value == 1)
2717e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2718e098bc96SEvan Quan 	else
2719e098bc96SEvan Quan 		return -EINVAL;
2720e098bc96SEvan Quan 
27214a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2722e098bc96SEvan Quan 	if (err < 0) {
27234a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2724e098bc96SEvan Quan 		return err;
2725e098bc96SEvan Quan 	}
2726e098bc96SEvan Quan 
272779c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2728e098bc96SEvan Quan 
27294a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27304a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2731e098bc96SEvan Quan 
273279c65f3fSEvan Quan 	if (err)
273379c65f3fSEvan Quan 		return -EINVAL;
273479c65f3fSEvan Quan 
2735e098bc96SEvan Quan 	return count;
2736e098bc96SEvan Quan }
2737e098bc96SEvan Quan 
amdgpu_hwmon_show_vddgfx(struct device * dev,struct device_attribute * attr,char * buf)2738e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2739e098bc96SEvan Quan 					struct device_attribute *attr,
2740e098bc96SEvan Quan 					char *buf)
2741e098bc96SEvan Quan {
2742e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2743e098bc96SEvan Quan 	u32 vddgfx;
2744d78c227fSMario Limonciello 	int r;
2745e098bc96SEvan Quan 
2746e098bc96SEvan Quan 	/* get the voltage */
2747d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
2748d78c227fSMario Limonciello 				   (void *)&vddgfx);
2749e098bc96SEvan Quan 	if (r)
2750e098bc96SEvan Quan 		return r;
2751e098bc96SEvan Quan 
2752a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
2753e098bc96SEvan Quan }
2754e098bc96SEvan Quan 
amdgpu_hwmon_show_vddgfx_label(struct device * dev,struct device_attribute * attr,char * buf)2755e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2756e098bc96SEvan Quan 					      struct device_attribute *attr,
2757e098bc96SEvan Quan 					      char *buf)
2758e098bc96SEvan Quan {
2759a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
2760e098bc96SEvan Quan }
2761e098bc96SEvan Quan 
amdgpu_hwmon_show_vddnb(struct device * dev,struct device_attribute * attr,char * buf)2762e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2763e098bc96SEvan Quan 				       struct device_attribute *attr,
2764e098bc96SEvan Quan 				       char *buf)
2765e098bc96SEvan Quan {
2766e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2767e098bc96SEvan Quan 	u32 vddnb;
2768d78c227fSMario Limonciello 	int r;
2769e098bc96SEvan Quan 
2770e098bc96SEvan Quan 	/* only APUs have vddnb */
2771e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2772e098bc96SEvan Quan 		return -EINVAL;
2773e098bc96SEvan Quan 
2774e098bc96SEvan Quan 	/* get the voltage */
2775d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
2776d78c227fSMario Limonciello 				   (void *)&vddnb);
2777e098bc96SEvan Quan 	if (r)
2778e098bc96SEvan Quan 		return r;
2779e098bc96SEvan Quan 
2780a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
2781e098bc96SEvan Quan }
2782e098bc96SEvan Quan 
amdgpu_hwmon_show_vddnb_label(struct device * dev,struct device_attribute * attr,char * buf)2783e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2784e098bc96SEvan Quan 					      struct device_attribute *attr,
2785e098bc96SEvan Quan 					      char *buf)
2786e098bc96SEvan Quan {
2787a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
2788e098bc96SEvan Quan }
2789e098bc96SEvan Quan 
amdgpu_hwmon_get_power(struct device * dev,enum amd_pp_sensors sensor)2790*51e4630eSAlex Deucher static int amdgpu_hwmon_get_power(struct device *dev,
2791d78c227fSMario Limonciello 				  enum amd_pp_sensors sensor)
2792e098bc96SEvan Quan {
2793e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2794d78c227fSMario Limonciello 	unsigned int uw;
2795e098bc96SEvan Quan 	u32 query = 0;
2796d78c227fSMario Limonciello 	int r;
2797e098bc96SEvan Quan 
2798d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
2799e098bc96SEvan Quan 	if (r)
2800e098bc96SEvan Quan 		return r;
2801e098bc96SEvan Quan 
2802e098bc96SEvan Quan 	/* convert to microwatts */
2803e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2804e098bc96SEvan Quan 
2805d78c227fSMario Limonciello 	return uw;
2806d78c227fSMario Limonciello }
2807d78c227fSMario Limonciello 
amdgpu_hwmon_show_power_avg(struct device * dev,struct device_attribute * attr,char * buf)2808d78c227fSMario Limonciello static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2809d78c227fSMario Limonciello 					   struct device_attribute *attr,
2810d78c227fSMario Limonciello 					   char *buf)
2811d78c227fSMario Limonciello {
2812*51e4630eSAlex Deucher 	int val;
2813d78c227fSMario Limonciello 
28149366c2e8SMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
2815d78c227fSMario Limonciello 	if (val < 0)
2816d78c227fSMario Limonciello 		return val;
2817d78c227fSMario Limonciello 
2818d78c227fSMario Limonciello 	return sysfs_emit(buf, "%u\n", val);
2819e098bc96SEvan Quan }
2820e098bc96SEvan Quan 
amdgpu_hwmon_show_power_input(struct device * dev,struct device_attribute * attr,char * buf)2821bb9f7b68SMario Limonciello static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
2822bb9f7b68SMario Limonciello 					     struct device_attribute *attr,
2823bb9f7b68SMario Limonciello 					     char *buf)
2824bb9f7b68SMario Limonciello {
2825*51e4630eSAlex Deucher 	int val;
2826bb9f7b68SMario Limonciello 
282747f1724dSMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
2828bb9f7b68SMario Limonciello 	if (val < 0)
2829bb9f7b68SMario Limonciello 		return val;
2830bb9f7b68SMario Limonciello 
2831bb9f7b68SMario Limonciello 	return sysfs_emit(buf, "%u\n", val);
2832bb9f7b68SMario Limonciello }
2833bb9f7b68SMario Limonciello 
amdgpu_hwmon_show_power_cap_min(struct device * dev,struct device_attribute * attr,char * buf)2834e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2835e098bc96SEvan Quan 					 struct device_attribute *attr,
2836e098bc96SEvan Quan 					 char *buf)
2837e098bc96SEvan Quan {
2838fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2839e098bc96SEvan Quan }
2840e098bc96SEvan Quan 
284191161b06SDarren Powell 
amdgpu_hwmon_show_power_cap_generic(struct device * dev,struct device_attribute * attr,char * buf,enum pp_power_limit_level pp_limit_level)284291161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2843e098bc96SEvan Quan 					struct device_attribute *attr,
284491161b06SDarren Powell 					char *buf,
284591161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
2846e098bc96SEvan Quan {
2847e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2848a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2849a40a020dSDarren Powell 	uint32_t limit;
2850e098bc96SEvan Quan 	ssize_t size;
2851e098bc96SEvan Quan 	int r;
2852e098bc96SEvan Quan 
285353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2854e098bc96SEvan Quan 		return -EPERM;
2855d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2856d2ae842dSAlex Deucher 		return -EPERM;
2857e098bc96SEvan Quan 
28584a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2859e098bc96SEvan Quan 	if (r < 0) {
28604a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2861e098bc96SEvan Quan 		return r;
2862e098bc96SEvan Quan 	}
2863e098bc96SEvan Quan 
286479c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
286504bec521SDarren Powell 				      pp_limit_level, power_type);
2866dc2a8240SDarren Powell 
2867dc2a8240SDarren Powell 	if (!r)
286809b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2869dc2a8240SDarren Powell 	else
287009b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
2871e098bc96SEvan Quan 
28724a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28734a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2874e098bc96SEvan Quan 
2875e098bc96SEvan Quan 	return size;
2876e098bc96SEvan Quan }
2877e098bc96SEvan Quan 
287891161b06SDarren Powell 
amdgpu_hwmon_show_power_cap_max(struct device * dev,struct device_attribute * attr,char * buf)287991161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
288091161b06SDarren Powell 					 struct device_attribute *attr,
288191161b06SDarren Powell 					 char *buf)
288291161b06SDarren Powell {
288391161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
288491161b06SDarren Powell 
288591161b06SDarren Powell }
288691161b06SDarren Powell 
amdgpu_hwmon_show_power_cap(struct device * dev,struct device_attribute * attr,char * buf)2887e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2888e098bc96SEvan Quan 					 struct device_attribute *attr,
2889e098bc96SEvan Quan 					 char *buf)
2890e098bc96SEvan Quan {
289191161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2892e098bc96SEvan Quan 
2893e098bc96SEvan Quan }
2894e098bc96SEvan Quan 
amdgpu_hwmon_show_power_cap_default(struct device * dev,struct device_attribute * attr,char * buf)28956e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
28966e58941cSEric Huang 					 struct device_attribute *attr,
28976e58941cSEric Huang 					 char *buf)
28986e58941cSEric Huang {
289991161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
29006e58941cSEric Huang 
29016e58941cSEric Huang }
29026e58941cSEric Huang 
amdgpu_hwmon_show_power_label(struct device * dev,struct device_attribute * attr,char * buf)2903ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2904ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
2905ae07970aSXiaomeng Hou 					 char *buf)
2906ae07970aSXiaomeng Hou {
29073b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
29088ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2909ae07970aSXiaomeng Hou 
29108ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
2911a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
29123b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
29133b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
29143b99e8e3SYang Wang 	else
29153b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
2916ae07970aSXiaomeng Hou }
2917e098bc96SEvan Quan 
amdgpu_hwmon_set_power_cap(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2918e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2919e098bc96SEvan Quan 		struct device_attribute *attr,
2920e098bc96SEvan Quan 		const char *buf,
2921e098bc96SEvan Quan 		size_t count)
2922e098bc96SEvan Quan {
2923e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2924ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
2925e098bc96SEvan Quan 	int err;
2926e098bc96SEvan Quan 	u32 value;
2927e098bc96SEvan Quan 
292853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2929e098bc96SEvan Quan 		return -EPERM;
2930d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2931d2ae842dSAlex Deucher 		return -EPERM;
2932e098bc96SEvan Quan 
2933e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
2934e098bc96SEvan Quan 		return -EINVAL;
2935e098bc96SEvan Quan 
2936e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2937e098bc96SEvan Quan 	if (err)
2938e098bc96SEvan Quan 		return err;
2939e098bc96SEvan Quan 
2940e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
2941ae07970aSXiaomeng Hou 	value |= limit_type << 24;
2942e098bc96SEvan Quan 
29434a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2944e098bc96SEvan Quan 	if (err < 0) {
29454a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2946e098bc96SEvan Quan 		return err;
2947e098bc96SEvan Quan 	}
2948e098bc96SEvan Quan 
294979c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
2950e098bc96SEvan Quan 
29514a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29524a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2953e098bc96SEvan Quan 
2954e098bc96SEvan Quan 	if (err)
2955e098bc96SEvan Quan 		return err;
2956e098bc96SEvan Quan 
2957e098bc96SEvan Quan 	return count;
2958e098bc96SEvan Quan }
2959e098bc96SEvan Quan 
amdgpu_hwmon_show_sclk(struct device * dev,struct device_attribute * attr,char * buf)2960e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2961e098bc96SEvan Quan 				      struct device_attribute *attr,
2962e098bc96SEvan Quan 				      char *buf)
2963e098bc96SEvan Quan {
2964e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2965e098bc96SEvan Quan 	uint32_t sclk;
2966d78c227fSMario Limonciello 	int r;
2967e098bc96SEvan Quan 
2968e098bc96SEvan Quan 	/* get the sclk */
2969d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2970d78c227fSMario Limonciello 				   (void *)&sclk);
2971e098bc96SEvan Quan 	if (r)
2972e098bc96SEvan Quan 		return r;
2973e098bc96SEvan Quan 
2974a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
2975e098bc96SEvan Quan }
2976e098bc96SEvan Quan 
amdgpu_hwmon_show_sclk_label(struct device * dev,struct device_attribute * attr,char * buf)2977e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2978e098bc96SEvan Quan 					    struct device_attribute *attr,
2979e098bc96SEvan Quan 					    char *buf)
2980e098bc96SEvan Quan {
2981a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
2982e098bc96SEvan Quan }
2983e098bc96SEvan Quan 
amdgpu_hwmon_show_mclk(struct device * dev,struct device_attribute * attr,char * buf)2984e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2985e098bc96SEvan Quan 				      struct device_attribute *attr,
2986e098bc96SEvan Quan 				      char *buf)
2987e098bc96SEvan Quan {
2988e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2989e098bc96SEvan Quan 	uint32_t mclk;
2990d78c227fSMario Limonciello 	int r;
2991e098bc96SEvan Quan 
2992e098bc96SEvan Quan 	/* get the sclk */
2993d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2994d78c227fSMario Limonciello 				   (void *)&mclk);
2995e098bc96SEvan Quan 	if (r)
2996e098bc96SEvan Quan 		return r;
2997e098bc96SEvan Quan 
2998a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
2999e098bc96SEvan Quan }
3000e098bc96SEvan Quan 
amdgpu_hwmon_show_mclk_label(struct device * dev,struct device_attribute * attr,char * buf)3001e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3002e098bc96SEvan Quan 					    struct device_attribute *attr,
3003e098bc96SEvan Quan 					    char *buf)
3004e098bc96SEvan Quan {
3005a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3006e098bc96SEvan Quan }
3007e098bc96SEvan Quan 
3008e098bc96SEvan Quan /**
3009e098bc96SEvan Quan  * DOC: hwmon
3010e098bc96SEvan Quan  *
3011e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3012e098bc96SEvan Quan  *
3013e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3014e098bc96SEvan Quan  *
3015e098bc96SEvan Quan  * - GPU voltage
3016e098bc96SEvan Quan  *
3017e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3018e098bc96SEvan Quan  *
3019e098bc96SEvan Quan  * - GPU power
3020e098bc96SEvan Quan  *
3021e098bc96SEvan Quan  * - GPU fan
3022e098bc96SEvan Quan  *
3023e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3024e098bc96SEvan Quan  *
3025e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3026e098bc96SEvan Quan  *
3027e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3028e098bc96SEvan Quan  *
3029e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3030e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3031e098bc96SEvan Quan  *
3032e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3033e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3034e098bc96SEvan Quan  *
3035e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3036e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3037e098bc96SEvan Quan  *
3038e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3039e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3040e098bc96SEvan Quan  *
3041e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3042e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3043e098bc96SEvan Quan  *
3044e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3045e098bc96SEvan Quan  *
3046e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3047e098bc96SEvan Quan  *
3048e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3049e098bc96SEvan Quan  *
3050e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3051e098bc96SEvan Quan  *
305229f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3053e098bc96SEvan Quan  *
3054bb9f7b68SMario Limonciello  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3055bb9f7b68SMario Limonciello  *
3056e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3057e098bc96SEvan Quan  *
3058e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3059e098bc96SEvan Quan  *
3060e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3061e098bc96SEvan Quan  *
3062e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3063e098bc96SEvan Quan  *
3064e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3065e098bc96SEvan Quan  *
3066e098bc96SEvan 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)
3067e098bc96SEvan Quan  *
3068e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3069e098bc96SEvan Quan  *
3070e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3071e098bc96SEvan Quan  *
3072e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3073e098bc96SEvan Quan  *
3074e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3075e098bc96SEvan Quan  *
3076e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3077e098bc96SEvan Quan  *
3078e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3079e098bc96SEvan Quan  *
3080e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3081e098bc96SEvan Quan  *
308296401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
308396401f7cSEvan Quan  *       That will get the former one overridden.
308496401f7cSEvan Quan  *
3085e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3086e098bc96SEvan Quan  *
3087e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3088e098bc96SEvan Quan  *
3089e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3090e098bc96SEvan Quan  *
3091e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3092e098bc96SEvan Quan  *
3093e098bc96SEvan Quan  */
3094e098bc96SEvan Quan 
3095e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3096e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3097e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3098e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3099e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3100e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3101e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3102e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3103e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3104e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3105e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3106e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3107e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3108e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3109e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3110e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3111e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3112e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3113e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3114e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3115e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3116e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3117e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3118e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3119e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3120e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3121e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3122e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3123e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3124bb9f7b68SMario Limonciello static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3125e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3126e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3127e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
31286e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3129ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3130ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3131ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3132ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3133ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
31346e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3135ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3136e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3137e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3138e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3139e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3140e098bc96SEvan Quan 
3141e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3142e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3143e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3144e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3145e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3146e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3147e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3148e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3149e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3150e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3151e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3152e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3153e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3154e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3155e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3156e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3157e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3158e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3159e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3160e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3161e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3162e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3163e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3164e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3165e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3166e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3167e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3168e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3169e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3170e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3171bb9f7b68SMario Limonciello 	&sensor_dev_attr_power1_input.dev_attr.attr,
3172e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3173e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3174e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
31756e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3176ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3177ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3178ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3179ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3180ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
31816e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3182ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3183e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3184e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3185e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3186e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3187e098bc96SEvan Quan 	NULL
3188e098bc96SEvan Quan };
3189e098bc96SEvan Quan 
hwmon_attributes_visible(struct kobject * kobj,struct attribute * attr,int index)3190e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3191e098bc96SEvan Quan 					struct attribute *attr, int index)
3192e098bc96SEvan Quan {
3193e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3194e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3195e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
31968ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
319715419813SMario Limonciello 	uint32_t tmp;
3198e098bc96SEvan Quan 
3199e098bc96SEvan Quan 	/* under multi-vf mode, the hwmon attributes are all not supported */
3200e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3201e098bc96SEvan Quan 		return 0;
3202e098bc96SEvan Quan 
32034f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
32044f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
32054f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
32064f0f1b58SDanijel Slivka 
3207e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3208e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3209e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3210e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3211e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3212e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3213e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3214e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3215e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3216e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3217e098bc96SEvan Quan 		return 0;
3218e098bc96SEvan Quan 
3219e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3220e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3221e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3222e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3223e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3224e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3225e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3226e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3227e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3228e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3229e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3230e098bc96SEvan Quan 		return 0;
3231e098bc96SEvan Quan 
3232e098bc96SEvan Quan 	/* Skip crit temp on APU */
32338572fa2aSAsad Kamal 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
32348572fa2aSAsad Kamal 	    (gc_ver == IP_VERSION(9, 4, 3))) &&
3235e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3236e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3237e098bc96SEvan Quan 		return 0;
3238e098bc96SEvan Quan 
3239e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3240e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3241e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3242e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3243e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3244e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3245e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3246e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3247e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3248e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3249e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3250e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3251e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3252e098bc96SEvan Quan 		return 0;
3253e098bc96SEvan Quan 
3254e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3255685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3256e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3257685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3258e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3259e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3260e098bc96SEvan Quan 
3261685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3262e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3263685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3264e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3265e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3266e098bc96SEvan Quan 
32678572fa2aSAsad Kamal 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3268ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
32698572fa2aSAsad Kamal 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
32708572fa2aSAsad Kamal 	      (gc_ver != IP_VERSION(9, 4, 3)))) &&
3271367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3272e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
32736e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
32746e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3275e098bc96SEvan Quan 		return 0;
3276e098bc96SEvan Quan 
327789317d42SGuilherme G. Piccoli 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3278367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
32798ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3280367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3281367deb67SAlex Deucher 		return 0;
3282367deb67SAlex Deucher 
328315419813SMario Limonciello 	/* not all products support both average and instantaneous */
328415419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
328515419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
328615419813SMario Limonciello 		return 0;
328715419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
328815419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
328915419813SMario Limonciello 		return 0;
329015419813SMario Limonciello 
3291e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3292685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3293685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3294685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3295685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3296e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3297e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3298e098bc96SEvan Quan 		return 0;
3299e098bc96SEvan Quan 
3300685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3301685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3302e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3303e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3304e098bc96SEvan Quan 		return 0;
3305e098bc96SEvan Quan 
3306e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
33078572fa2aSAsad Kamal 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
33088572fa2aSAsad Kamal 	     (gc_ver == IP_VERSION(9, 4, 3))) &&
3309e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3310e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3311e098bc96SEvan Quan 		return 0;
3312e098bc96SEvan Quan 
33138572fa2aSAsad Kamal 	/* only APUs other than gc 9,4,3 have vddnb */
33148572fa2aSAsad Kamal 	if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) &&
3315e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3316e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3317e098bc96SEvan Quan 		return 0;
3318e098bc96SEvan Quan 
33198572fa2aSAsad Kamal 	/* no mclk on APUs other than gc 9,4,3*/
33208572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3321e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3322e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3323e098bc96SEvan Quan 		return 0;
3324e098bc96SEvan Quan 
33258ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
33268572fa2aSAsad Kamal 	    (gc_ver != IP_VERSION(9, 4, 3)) &&
33278572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3328bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
33291836bb0aSAsad Kamal 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3330bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
33311836bb0aSAsad Kamal 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
33321836bb0aSAsad Kamal 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
33338572fa2aSAsad Kamal 		return 0;
33348572fa2aSAsad Kamal 
3335bfb4fd20SAsad Kamal 	/* hotspot temperature for gc 9,4,3*/
33368572fa2aSAsad Kamal 	if ((gc_ver == IP_VERSION(9, 4, 3)) &&
33378572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
33388572fa2aSAsad Kamal 	     attr == &sensor_dev_attr_temp1_label.dev_attr.attr))
33398572fa2aSAsad Kamal 		return 0;
33408572fa2aSAsad Kamal 
33418572fa2aSAsad Kamal 	/* only SOC15 dGPUs support hotspot and mem temperatures */
33428572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0) ||
33438572fa2aSAsad Kamal 	    (gc_ver == IP_VERSION(9, 4, 3))) &&
33441836bb0aSAsad Kamal 	     (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3345e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3346e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3347e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3348bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3349e098bc96SEvan Quan 		return 0;
3350e098bc96SEvan Quan 
3351ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
33528ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3353ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3354ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3355ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3356ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
33576e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3358de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3359ae07970aSXiaomeng Hou 		return 0;
3360ae07970aSXiaomeng Hou 
3361e098bc96SEvan Quan 	return effective_mode;
3362e098bc96SEvan Quan }
3363e098bc96SEvan Quan 
3364e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3365e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3366e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3367e098bc96SEvan Quan };
3368e098bc96SEvan Quan 
3369e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3370e098bc96SEvan Quan 	&hwmon_attrgroup,
3371e098bc96SEvan Quan 	NULL
3372e098bc96SEvan Quan };
3373e098bc96SEvan Quan 
amdgpu_pm_sysfs_init(struct amdgpu_device * adev)3374e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3375e098bc96SEvan Quan {
3376e098bc96SEvan Quan 	int ret;
3377e098bc96SEvan Quan 	uint32_t mask = 0;
3378e098bc96SEvan Quan 
3379e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
3380e098bc96SEvan Quan 		return 0;
3381e098bc96SEvan Quan 
33825fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
33835fa99373SZhenGuo Yin 
3384e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3385e098bc96SEvan Quan 		return 0;
3386e098bc96SEvan Quan 
3387e098bc96SEvan Quan 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3388e098bc96SEvan Quan 								   DRIVER_NAME, adev,
3389e098bc96SEvan Quan 								   hwmon_groups);
3390e098bc96SEvan Quan 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3391e098bc96SEvan Quan 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3392e098bc96SEvan Quan 		dev_err(adev->dev,
3393e098bc96SEvan Quan 			"Unable to register hwmon device: %d\n", ret);
3394e098bc96SEvan Quan 		return ret;
3395e098bc96SEvan Quan 	}
3396e098bc96SEvan Quan 
3397e098bc96SEvan Quan 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3398e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
3399e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
3400e098bc96SEvan Quan 		break;
3401e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
3402e098bc96SEvan Quan 		mask = 0;
3403e098bc96SEvan Quan 		break;
3404e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
3405e098bc96SEvan Quan 	default:
3406e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
3407e098bc96SEvan Quan 		break;
3408e098bc96SEvan Quan 	}
3409e098bc96SEvan Quan 
3410e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
3411e098bc96SEvan Quan 					       amdgpu_device_attrs,
3412e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
3413e098bc96SEvan Quan 					       mask,
3414e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
3415e098bc96SEvan Quan 	if (ret)
3416e098bc96SEvan Quan 		return ret;
3417e098bc96SEvan Quan 
3418e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
3419e098bc96SEvan Quan 
3420e098bc96SEvan Quan 	return 0;
3421e098bc96SEvan Quan }
3422e098bc96SEvan Quan 
amdgpu_pm_sysfs_fini(struct amdgpu_device * adev)3423e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3424e098bc96SEvan Quan {
3425e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
3426e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3427e098bc96SEvan Quan 
3428e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3429e098bc96SEvan Quan }
3430e098bc96SEvan Quan 
3431e098bc96SEvan Quan /*
3432e098bc96SEvan Quan  * Debugfs info
3433e098bc96SEvan Quan  */
3434e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3435e098bc96SEvan Quan 
amdgpu_debugfs_prints_cpu_info(struct seq_file * m,struct amdgpu_device * adev)3436517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3437e1b3bcaaSRan Sun 					   struct amdgpu_device *adev)
3438e1b3bcaaSRan Sun {
3439517cb957SHuang Rui 	uint16_t *p_val;
3440517cb957SHuang Rui 	uint32_t size;
3441517cb957SHuang Rui 	int i;
344279c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
3443517cb957SHuang Rui 
344479c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
344579c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
3446517cb957SHuang Rui 				GFP_KERNEL);
3447517cb957SHuang Rui 
3448517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3449517cb957SHuang Rui 					    (void *)p_val, &size)) {
345079c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
3451517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3452517cb957SHuang Rui 					   *(p_val + i), i);
3453517cb957SHuang Rui 		}
3454517cb957SHuang Rui 
3455517cb957SHuang Rui 		kfree(p_val);
3456517cb957SHuang Rui 	}
3457517cb957SHuang Rui }
3458517cb957SHuang Rui 
amdgpu_debugfs_pm_info_pp(struct seq_file * m,struct amdgpu_device * adev)3459e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3460e098bc96SEvan Quan {
34618ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
34628ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3463e098bc96SEvan Quan 	uint32_t value;
3464800c53d6SXiaojian Du 	uint64_t value64 = 0;
3465e098bc96SEvan Quan 	uint32_t query = 0;
3466e098bc96SEvan Quan 	int size;
3467e098bc96SEvan Quan 
3468e098bc96SEvan Quan 	/* GPU Clocks */
3469e098bc96SEvan Quan 	size = sizeof(value);
3470e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
3471517cb957SHuang Rui 
3472517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
3473517cb957SHuang Rui 
3474e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3475e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3476e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3477e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3478e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3479e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3480e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3481e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3482e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3483e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3484e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3485e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3486e098bc96SEvan Quan 	size = sizeof(uint32_t);
34879366c2e8SMario Limonciello 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size))
3488e098bc96SEvan Quan 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3489a7dd9b97SAlex Deucher 	size = sizeof(uint32_t);
3490a7dd9b97SAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size))
3491a7dd9b97SAlex Deucher 		seq_printf(m, "\t%u.%u W (current GPU)\n", query >> 8, query & 0xff);
3492e098bc96SEvan Quan 	size = sizeof(value);
3493e098bc96SEvan Quan 	seq_printf(m, "\n");
3494e098bc96SEvan Quan 
3495e098bc96SEvan Quan 	/* GPU Temp */
3496e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3497e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3498e098bc96SEvan Quan 
3499e098bc96SEvan Quan 	/* GPU Load */
3500e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3501e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
3502e098bc96SEvan Quan 	/* MEM Load */
3503e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3504e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
3505e098bc96SEvan Quan 
3506e098bc96SEvan Quan 	seq_printf(m, "\n");
3507e098bc96SEvan Quan 
3508e098bc96SEvan Quan 	/* SMC feature mask */
3509e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3510e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3511e098bc96SEvan Quan 
35128ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
35138ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
3514e098bc96SEvan Quan 		/* VCN clocks */
3515e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3516e098bc96SEvan Quan 			if (!value) {
3517e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
3518e098bc96SEvan Quan 			} else {
3519e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
3520e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3521e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3522e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3523e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3524e098bc96SEvan Quan 			}
3525e098bc96SEvan Quan 		}
3526e098bc96SEvan Quan 		seq_printf(m, "\n");
3527e098bc96SEvan Quan 	} else {
3528e098bc96SEvan Quan 		/* UVD clocks */
3529e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3530e098bc96SEvan Quan 			if (!value) {
3531e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
3532e098bc96SEvan Quan 			} else {
3533e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
3534e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3535e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3536e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3537e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3538e098bc96SEvan Quan 			}
3539e098bc96SEvan Quan 		}
3540e098bc96SEvan Quan 		seq_printf(m, "\n");
3541e098bc96SEvan Quan 
3542e098bc96SEvan Quan 		/* VCE clocks */
3543e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3544e098bc96SEvan Quan 			if (!value) {
3545e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
3546e098bc96SEvan Quan 			} else {
3547e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
3548e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3549e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3550e098bc96SEvan Quan 			}
3551e098bc96SEvan Quan 		}
3552e098bc96SEvan Quan 	}
3553e098bc96SEvan Quan 
3554e098bc96SEvan Quan 	return 0;
3555e098bc96SEvan Quan }
3556e098bc96SEvan Quan 
355744762718SNathan Chancellor static const struct cg_flag_name clocks[] = {
355844762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
355944762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
356044762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
356144762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
356244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
356344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
356444762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
356544762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
356644762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
356744762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
356844762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
356944762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
357044762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
357144762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
357244762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
357344762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
357444762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
357544762718SNathan Chancellor 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
357644762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
357744762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
357844762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
357944762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
358044762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
358144762718SNathan Chancellor 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
358244762718SNathan Chancellor 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
358344762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
358444762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
358544762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
358644762718SNathan Chancellor 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
358744762718SNathan Chancellor 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
358844762718SNathan Chancellor 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
358944762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
359044762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
359144762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
359244762718SNathan Chancellor 	{0, NULL},
359344762718SNathan Chancellor };
359444762718SNathan Chancellor 
amdgpu_parse_cg_state(struct seq_file * m,u64 flags)359525faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
3596e098bc96SEvan Quan {
3597e098bc96SEvan Quan 	int i;
3598e098bc96SEvan Quan 
3599e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
3600e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3601e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
3602e098bc96SEvan Quan }
3603e098bc96SEvan Quan 
amdgpu_debugfs_pm_info_show(struct seq_file * m,void * unused)3604373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3605e098bc96SEvan Quan {
3606373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3607373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
360825faeddcSEvan Quan 	u64 flags = 0;
3609e098bc96SEvan Quan 	int r;
3610e098bc96SEvan Quan 
361153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3612e098bc96SEvan Quan 		return -EPERM;
3613d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3614d2ae842dSAlex Deucher 		return -EPERM;
3615e098bc96SEvan Quan 
3616e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
3617e098bc96SEvan Quan 	if (r < 0) {
3618e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
3619e098bc96SEvan Quan 		return r;
3620e098bc96SEvan Quan 	}
3621e098bc96SEvan Quan 
362279c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
3623e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3624e098bc96SEvan Quan 		if (r)
3625e098bc96SEvan Quan 			goto out;
362679c65f3fSEvan Quan 	}
3627e098bc96SEvan Quan 
3628e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3629e098bc96SEvan Quan 
363025faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
3631e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
3632e098bc96SEvan Quan 	seq_printf(m, "\n");
3633e098bc96SEvan Quan 
3634e098bc96SEvan Quan out:
3635e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
3636e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
3637e098bc96SEvan Quan 
3638e098bc96SEvan Quan 	return r;
3639e098bc96SEvan Quan }
3640e098bc96SEvan Quan 
3641373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3642373720f7SNirmoy Das 
364327ebf21fSLijo Lazar /*
364427ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
364527ebf21fSLijo Lazar  *
364627ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
364727ebf21fSLijo Lazar  */
amdgpu_pm_prv_buffer_read(struct file * f,char __user * buf,size_t size,loff_t * pos)364827ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
364927ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
365027ebf21fSLijo Lazar {
365127ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
365227ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
365327ebf21fSLijo Lazar 	void *smu_prv_buf;
365479c65f3fSEvan Quan 	int ret = 0;
365527ebf21fSLijo Lazar 
365627ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
365727ebf21fSLijo Lazar 		return -EPERM;
365827ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
365927ebf21fSLijo Lazar 		return -EPERM;
366027ebf21fSLijo Lazar 
366179c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
366279c65f3fSEvan Quan 	if (ret)
366379c65f3fSEvan Quan 		return ret;
366427ebf21fSLijo Lazar 
366527ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
366627ebf21fSLijo Lazar 		return -EINVAL;
366727ebf21fSLijo Lazar 
366827ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
366927ebf21fSLijo Lazar 				       smu_prv_buf_size);
367027ebf21fSLijo Lazar }
367127ebf21fSLijo Lazar 
367227ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
367327ebf21fSLijo Lazar 	.owner = THIS_MODULE,
367427ebf21fSLijo Lazar 	.open = simple_open,
367527ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
367627ebf21fSLijo Lazar 	.llseek = default_llseek,
367727ebf21fSLijo Lazar };
367827ebf21fSLijo Lazar 
3679e098bc96SEvan Quan #endif
3680e098bc96SEvan Quan 
amdgpu_debugfs_pm_init(struct amdgpu_device * adev)3681373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3682e098bc96SEvan Quan {
3683e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3684373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
3685373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
3686373720f7SNirmoy Das 
36871613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
36881613f346SFlora Cui 		return;
36891613f346SFlora Cui 
3690373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3691373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
3692373720f7SNirmoy Das 
369327ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
369427ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
369527ebf21fSLijo Lazar 					 adev,
369627ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
369727ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
36981f5fc7a5SAndrey Grodzovsky 
369979c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
3700e098bc96SEvan Quan #endif
3701e098bc96SEvan Quan }
3702