xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision 548009ad1c9a8e7dedf3c50730214c2e33f03865)
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 
383e38b634SEvan Quan #define MAX_NUM_OF_FEATURES_PER_SUBSET		8
393e38b634SEvan Quan #define MAX_NUM_OF_SUBSETS			8
403e38b634SEvan Quan 
413e38b634SEvan Quan struct od_attribute {
423e38b634SEvan Quan 	struct kobj_attribute	attribute;
433e38b634SEvan Quan 	struct list_head	entry;
443e38b634SEvan Quan };
453e38b634SEvan Quan 
463e38b634SEvan Quan struct od_kobj {
473e38b634SEvan Quan 	struct kobject		kobj;
483e38b634SEvan Quan 	struct list_head	entry;
493e38b634SEvan Quan 	struct list_head	attribute;
503e38b634SEvan Quan 	void			*priv;
513e38b634SEvan Quan };
523e38b634SEvan Quan 
533e38b634SEvan Quan struct od_feature_ops {
543e38b634SEvan Quan 	umode_t (*is_visible)(struct amdgpu_device *adev);
553e38b634SEvan Quan 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
563e38b634SEvan Quan 			char *buf);
573e38b634SEvan Quan 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
583e38b634SEvan Quan 			 const char *buf, size_t count);
593e38b634SEvan Quan };
603e38b634SEvan Quan 
613e38b634SEvan Quan struct od_feature_item {
623e38b634SEvan Quan 	const char		*name;
633e38b634SEvan Quan 	struct od_feature_ops	ops;
643e38b634SEvan Quan };
653e38b634SEvan Quan 
663e38b634SEvan Quan struct od_feature_container {
673e38b634SEvan Quan 	char				*name;
683e38b634SEvan Quan 	struct od_feature_ops		ops;
693e38b634SEvan Quan 	struct od_feature_item		sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET];
703e38b634SEvan Quan };
713e38b634SEvan Quan 
723e38b634SEvan Quan struct od_feature_set {
733e38b634SEvan Quan 	struct od_feature_container	containers[MAX_NUM_OF_SUBSETS];
743e38b634SEvan Quan };
753e38b634SEvan Quan 
76e098bc96SEvan Quan static const struct hwmon_temp_label {
77e098bc96SEvan Quan 	enum PP_HWMON_TEMP channel;
78e098bc96SEvan Quan 	const char *label;
79e098bc96SEvan Quan } temp_label[] = {
80e098bc96SEvan Quan 	{PP_TEMP_EDGE, "edge"},
81e098bc96SEvan Quan 	{PP_TEMP_JUNCTION, "junction"},
82e098bc96SEvan Quan 	{PP_TEMP_MEM, "mem"},
83e098bc96SEvan Quan };
84e098bc96SEvan Quan 
853867e370SDarren Powell const char * const amdgpu_pp_profile_name[] = {
863867e370SDarren Powell 	"BOOTUP_DEFAULT",
873867e370SDarren Powell 	"3D_FULL_SCREEN",
883867e370SDarren Powell 	"POWER_SAVING",
893867e370SDarren Powell 	"VIDEO",
903867e370SDarren Powell 	"VR",
913867e370SDarren Powell 	"COMPUTE",
92334682aeSKenneth Feng 	"CUSTOM",
93334682aeSKenneth Feng 	"WINDOW_3D",
9431865e96SPerry Yuan 	"CAPPED",
9531865e96SPerry Yuan 	"UNCAPPED",
963867e370SDarren Powell };
973867e370SDarren Powell 
98e098bc96SEvan Quan /**
99e098bc96SEvan Quan  * DOC: power_dpm_state
100e098bc96SEvan Quan  *
101e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
102e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
103e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
104e098bc96SEvan Quan  * It accepts the following arguments:
105e098bc96SEvan Quan  *
106e098bc96SEvan Quan  * - battery
107e098bc96SEvan Quan  *
108e098bc96SEvan Quan  * - balanced
109e098bc96SEvan Quan  *
110e098bc96SEvan Quan  * - performance
111e098bc96SEvan Quan  *
112e098bc96SEvan Quan  * battery
113e098bc96SEvan Quan  *
114e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
115e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
116e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
117e098bc96SEvan Quan  *
118e098bc96SEvan Quan  * balanced
119e098bc96SEvan Quan  *
120e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
121e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
122e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
123e098bc96SEvan Quan  *
124e098bc96SEvan Quan  * performance
125e098bc96SEvan Quan  *
126e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
127e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
128e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
129e098bc96SEvan Quan  *
130e098bc96SEvan Quan  */
131e098bc96SEvan Quan 
132e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
133e098bc96SEvan Quan 					  struct device_attribute *attr,
134e098bc96SEvan Quan 					  char *buf)
135e098bc96SEvan Quan {
136e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1371348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
138e098bc96SEvan Quan 	enum amd_pm_state_type pm;
139e098bc96SEvan Quan 	int ret;
140e098bc96SEvan Quan 
14153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
142e098bc96SEvan Quan 		return -EPERM;
143d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
144d2ae842dSAlex Deucher 		return -EPERM;
145e098bc96SEvan Quan 
146e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
147e098bc96SEvan Quan 	if (ret < 0) {
148e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
149e098bc96SEvan Quan 		return ret;
150e098bc96SEvan Quan 	}
151e098bc96SEvan Quan 
15279c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
153e098bc96SEvan Quan 
154e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
155e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
156e098bc96SEvan Quan 
157a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
158e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
159e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
160e098bc96SEvan Quan }
161e098bc96SEvan Quan 
162e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
163e098bc96SEvan Quan 					  struct device_attribute *attr,
164e098bc96SEvan Quan 					  const char *buf,
165e098bc96SEvan Quan 					  size_t count)
166e098bc96SEvan Quan {
167e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1681348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
169e098bc96SEvan Quan 	enum amd_pm_state_type  state;
170e098bc96SEvan Quan 	int ret;
171e098bc96SEvan Quan 
17253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
173e098bc96SEvan Quan 		return -EPERM;
174d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
175d2ae842dSAlex Deucher 		return -EPERM;
176e098bc96SEvan Quan 
177e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
178e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
179e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
180e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
181e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
182e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
183e098bc96SEvan Quan 	else
184e098bc96SEvan Quan 		return -EINVAL;
185e098bc96SEvan Quan 
186e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
187e098bc96SEvan Quan 	if (ret < 0) {
188e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
189e098bc96SEvan Quan 		return ret;
190e098bc96SEvan Quan 	}
191e098bc96SEvan Quan 
19279c65f3fSEvan Quan 	amdgpu_dpm_set_power_state(adev, state);
193e098bc96SEvan Quan 
194e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
195e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
196e098bc96SEvan Quan 
197e098bc96SEvan Quan 	return count;
198e098bc96SEvan Quan }
199e098bc96SEvan Quan 
200e098bc96SEvan Quan 
201e098bc96SEvan Quan /**
202e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
203e098bc96SEvan Quan  *
204e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
205e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
206e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
207e098bc96SEvan Quan  *
208e098bc96SEvan Quan  * - auto
209e098bc96SEvan Quan  *
210e098bc96SEvan Quan  * - low
211e098bc96SEvan Quan  *
212e098bc96SEvan Quan  * - high
213e098bc96SEvan Quan  *
214e098bc96SEvan Quan  * - manual
215e098bc96SEvan Quan  *
216e098bc96SEvan Quan  * - profile_standard
217e098bc96SEvan Quan  *
218e098bc96SEvan Quan  * - profile_min_sclk
219e098bc96SEvan Quan  *
220e098bc96SEvan Quan  * - profile_min_mclk
221e098bc96SEvan Quan  *
222e098bc96SEvan Quan  * - profile_peak
223e098bc96SEvan Quan  *
224e098bc96SEvan Quan  * auto
225e098bc96SEvan Quan  *
226e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
227e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
228e098bc96SEvan Quan  *
229e098bc96SEvan Quan  * low
230e098bc96SEvan Quan  *
231e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
232e098bc96SEvan Quan  *
233e098bc96SEvan Quan  * high
234e098bc96SEvan Quan  *
235e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
236e098bc96SEvan Quan  *
237e098bc96SEvan Quan  * manual
238e098bc96SEvan Quan  *
239e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
240e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
241e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
242e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
243e098bc96SEvan Quan  *
244e098bc96SEvan Quan  * profile_standard
245e098bc96SEvan Quan  * profile_min_sclk
246e098bc96SEvan Quan  * profile_min_mclk
247e098bc96SEvan Quan  * profile_peak
248e098bc96SEvan Quan  *
249e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
250e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
251e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
252e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
253e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
254e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
255e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
256e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
257e098bc96SEvan Quan  *
258e098bc96SEvan Quan  */
259e098bc96SEvan Quan 
260e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
261e098bc96SEvan Quan 							    struct device_attribute *attr,
262e098bc96SEvan Quan 							    char *buf)
263e098bc96SEvan Quan {
264e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
266e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
267e098bc96SEvan Quan 	int ret;
268e098bc96SEvan Quan 
26953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
270e098bc96SEvan Quan 		return -EPERM;
271d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
272d2ae842dSAlex Deucher 		return -EPERM;
273e098bc96SEvan Quan 
274e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
275e098bc96SEvan Quan 	if (ret < 0) {
276e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
277e098bc96SEvan Quan 		return ret;
278e098bc96SEvan Quan 	}
279e098bc96SEvan Quan 
280e098bc96SEvan Quan 	level = amdgpu_dpm_get_performance_level(adev);
281e098bc96SEvan Quan 
282e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
283e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
284e098bc96SEvan Quan 
285a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
286e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
287e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
288e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
289e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
290e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
291e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
292e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
293e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
2946be64246SLijo Lazar 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
295e098bc96SEvan Quan 			  "unknown");
296e098bc96SEvan Quan }
297e098bc96SEvan Quan 
298e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
299e098bc96SEvan Quan 							    struct device_attribute *attr,
300e098bc96SEvan Quan 							    const char *buf,
301e098bc96SEvan Quan 							    size_t count)
302e098bc96SEvan Quan {
303e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3041348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
305e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
306e098bc96SEvan Quan 	int ret = 0;
307e098bc96SEvan Quan 
30853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
309e098bc96SEvan Quan 		return -EPERM;
310d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
311d2ae842dSAlex Deucher 		return -EPERM;
312e098bc96SEvan Quan 
313e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
314e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
315e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
316e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
317e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
318e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
319e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
320e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
321e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
322e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
323e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
324e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
325e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
326e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
327e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
328e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
329e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
330e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
3316be64246SLijo Lazar 	} else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
3326be64246SLijo Lazar 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
333e098bc96SEvan Quan 	}  else {
334e098bc96SEvan Quan 		return -EINVAL;
335e098bc96SEvan Quan 	}
336e098bc96SEvan Quan 
337e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
338e098bc96SEvan Quan 	if (ret < 0) {
339e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
340e098bc96SEvan Quan 		return ret;
341e098bc96SEvan Quan 	}
342e098bc96SEvan Quan 
3438cda7a4fSAlex Deucher 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
34479c65f3fSEvan Quan 	if (amdgpu_dpm_force_performance_level(adev, level)) {
345e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
346e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
3478cda7a4fSAlex Deucher 		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
348e098bc96SEvan Quan 		return -EINVAL;
349e098bc96SEvan Quan 	}
3508cda7a4fSAlex Deucher 	/* override whatever a user ctx may have set */
3518cda7a4fSAlex Deucher 	adev->pm.stable_pstate_ctx = NULL;
3528cda7a4fSAlex Deucher 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
35379c65f3fSEvan Quan 
354e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
355e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
356e098bc96SEvan Quan 
357e098bc96SEvan Quan 	return count;
358e098bc96SEvan Quan }
359e098bc96SEvan Quan 
360e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
361e098bc96SEvan Quan 		struct device_attribute *attr,
362e098bc96SEvan Quan 		char *buf)
363e098bc96SEvan Quan {
364e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
366e098bc96SEvan Quan 	struct pp_states_info data;
36709b6744cSDarren Powell 	uint32_t i;
36809b6744cSDarren Powell 	int buf_len, ret;
369e098bc96SEvan Quan 
37053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
371e098bc96SEvan Quan 		return -EPERM;
372d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
373d2ae842dSAlex Deucher 		return -EPERM;
374e098bc96SEvan Quan 
375e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
376e098bc96SEvan Quan 	if (ret < 0) {
377e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
378e098bc96SEvan Quan 		return ret;
379e098bc96SEvan Quan 	}
380e098bc96SEvan Quan 
38179c65f3fSEvan Quan 	if (amdgpu_dpm_get_pp_num_states(adev, &data))
382e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
383e098bc96SEvan Quan 
384e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
385e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
386e098bc96SEvan Quan 
38709b6744cSDarren Powell 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
388e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
38909b6744cSDarren Powell 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
390e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
391e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
392e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
393e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
394e098bc96SEvan Quan 
395e098bc96SEvan Quan 	return buf_len;
396e098bc96SEvan Quan }
397e098bc96SEvan Quan 
398e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
399e098bc96SEvan Quan 		struct device_attribute *attr,
400e098bc96SEvan Quan 		char *buf)
401e098bc96SEvan Quan {
402e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4031348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
4042b24c199STom Rix 	struct pp_states_info data = {0};
405e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
406e098bc96SEvan Quan 	int i = 0, ret = 0;
407e098bc96SEvan Quan 
40853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
409e098bc96SEvan Quan 		return -EPERM;
410d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
411d2ae842dSAlex Deucher 		return -EPERM;
412e098bc96SEvan Quan 
413e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
414e098bc96SEvan Quan 	if (ret < 0) {
415e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
416e098bc96SEvan Quan 		return ret;
417e098bc96SEvan Quan 	}
418e098bc96SEvan Quan 
41979c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
42079c65f3fSEvan Quan 
42179c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
422e098bc96SEvan Quan 
423e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
424e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
425e098bc96SEvan Quan 
42679c65f3fSEvan Quan 	if (ret)
42779c65f3fSEvan Quan 		return ret;
42879c65f3fSEvan Quan 
429e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
430e098bc96SEvan Quan 		if (pm == data.states[i])
431e098bc96SEvan Quan 			break;
432e098bc96SEvan Quan 	}
433e098bc96SEvan Quan 
434e098bc96SEvan Quan 	if (i == data.nums)
435e098bc96SEvan Quan 		i = -EINVAL;
436e098bc96SEvan Quan 
437a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", i);
438e098bc96SEvan Quan }
439e098bc96SEvan Quan 
440e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
441e098bc96SEvan Quan 		struct device_attribute *attr,
442e098bc96SEvan Quan 		char *buf)
443e098bc96SEvan Quan {
444e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4451348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
446e098bc96SEvan Quan 
44753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
448e098bc96SEvan Quan 		return -EPERM;
449d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
450d2ae842dSAlex Deucher 		return -EPERM;
451e098bc96SEvan Quan 
452d698a2c4SEvan Quan 	if (adev->pm.pp_force_state_enabled)
453e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
454e098bc96SEvan Quan 	else
455a9ca9bb3STian Tao 		return sysfs_emit(buf, "\n");
456e098bc96SEvan Quan }
457e098bc96SEvan Quan 
458e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
459e098bc96SEvan Quan 		struct device_attribute *attr,
460e098bc96SEvan Quan 		const char *buf,
461e098bc96SEvan Quan 		size_t count)
462e098bc96SEvan Quan {
463e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4641348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
465e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
46679c65f3fSEvan Quan 	struct pp_states_info data;
467e098bc96SEvan Quan 	unsigned long idx;
468e098bc96SEvan Quan 	int ret;
469e098bc96SEvan Quan 
47053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
471e098bc96SEvan Quan 		return -EPERM;
472d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
473d2ae842dSAlex Deucher 		return -EPERM;
474e098bc96SEvan Quan 
475d698a2c4SEvan Quan 	adev->pm.pp_force_state_enabled = false;
47679c65f3fSEvan Quan 
477e098bc96SEvan Quan 	if (strlen(buf) == 1)
47879c65f3fSEvan Quan 		return count;
479e098bc96SEvan Quan 
480e098bc96SEvan Quan 	ret = kstrtoul(buf, 0, &idx);
481e098bc96SEvan Quan 	if (ret || idx >= ARRAY_SIZE(data.states))
482e098bc96SEvan Quan 		return -EINVAL;
483e098bc96SEvan Quan 
484e098bc96SEvan Quan 	idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
485e098bc96SEvan Quan 
486e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
487e098bc96SEvan Quan 	if (ret < 0) {
488e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
489e098bc96SEvan Quan 		return ret;
490e098bc96SEvan Quan 	}
491e098bc96SEvan Quan 
49279c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
49379c65f3fSEvan Quan 	if (ret)
49479c65f3fSEvan Quan 		goto err_out;
49579c65f3fSEvan Quan 
49679c65f3fSEvan Quan 	state = data.states[idx];
49779c65f3fSEvan Quan 
498e098bc96SEvan Quan 	/* only set user selected power states */
499e098bc96SEvan Quan 	if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
500e098bc96SEvan Quan 	    state != POWER_STATE_TYPE_DEFAULT) {
50179c65f3fSEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
502e098bc96SEvan Quan 				AMD_PP_TASK_ENABLE_USER_STATE, &state);
50379c65f3fSEvan Quan 		if (ret)
50479c65f3fSEvan Quan 			goto err_out;
50579c65f3fSEvan Quan 
506d698a2c4SEvan Quan 		adev->pm.pp_force_state_enabled = true;
507e098bc96SEvan Quan 	}
50879c65f3fSEvan Quan 
509e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
510e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
511e098bc96SEvan Quan 
512e098bc96SEvan Quan 	return count;
51379c65f3fSEvan Quan 
51479c65f3fSEvan Quan err_out:
51579c65f3fSEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
51679c65f3fSEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
51779c65f3fSEvan Quan 	return ret;
518e098bc96SEvan Quan }
519e098bc96SEvan Quan 
520e098bc96SEvan Quan /**
521e098bc96SEvan Quan  * DOC: pp_table
522e098bc96SEvan Quan  *
523e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
524e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
525e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
526e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
527e098bc96SEvan Quan  * powerplay using that new table.
528e098bc96SEvan Quan  *
529e098bc96SEvan Quan  */
530e098bc96SEvan Quan 
531e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
532e098bc96SEvan Quan 		struct device_attribute *attr,
533e098bc96SEvan Quan 		char *buf)
534e098bc96SEvan Quan {
535e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5361348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
537e098bc96SEvan Quan 	char *table = NULL;
538e098bc96SEvan Quan 	int size, ret;
539e098bc96SEvan Quan 
54053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
541e098bc96SEvan Quan 		return -EPERM;
542d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
543d2ae842dSAlex Deucher 		return -EPERM;
544e098bc96SEvan Quan 
545e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
546e098bc96SEvan Quan 	if (ret < 0) {
547e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
548e098bc96SEvan Quan 		return ret;
549e098bc96SEvan Quan 	}
550e098bc96SEvan Quan 
551e098bc96SEvan Quan 	size = amdgpu_dpm_get_pp_table(adev, &table);
55279c65f3fSEvan Quan 
553e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
554e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
55579c65f3fSEvan Quan 
55679c65f3fSEvan Quan 	if (size <= 0)
557e098bc96SEvan Quan 		return size;
558e098bc96SEvan Quan 
559e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
560e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
561e098bc96SEvan Quan 
562e098bc96SEvan Quan 	memcpy(buf, table, size);
563e098bc96SEvan Quan 
564e098bc96SEvan Quan 	return size;
565e098bc96SEvan Quan }
566e098bc96SEvan Quan 
567e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
568e098bc96SEvan Quan 		struct device_attribute *attr,
569e098bc96SEvan Quan 		const char *buf,
570e098bc96SEvan Quan 		size_t count)
571e098bc96SEvan Quan {
572e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5731348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
574e098bc96SEvan Quan 	int ret = 0;
575e098bc96SEvan Quan 
57653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
577e098bc96SEvan Quan 		return -EPERM;
578d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
579d2ae842dSAlex Deucher 		return -EPERM;
580e098bc96SEvan Quan 
581e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
582e098bc96SEvan Quan 	if (ret < 0) {
583e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
584e098bc96SEvan Quan 		return ret;
585e098bc96SEvan Quan 	}
586e098bc96SEvan Quan 
5878f4828d0SDarren Powell 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
588e098bc96SEvan Quan 
589e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
590e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
591e098bc96SEvan Quan 
59279c65f3fSEvan Quan 	if (ret)
59379c65f3fSEvan Quan 		return ret;
59479c65f3fSEvan Quan 
595e098bc96SEvan Quan 	return count;
596e098bc96SEvan Quan }
597e098bc96SEvan Quan 
598e098bc96SEvan Quan /**
599e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
600e098bc96SEvan Quan  *
601e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
602e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
603e098bc96SEvan Quan  * this.
604e098bc96SEvan Quan  *
605e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
606e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
607e098bc96SEvan Quan  * following formula:
608e098bc96SEvan Quan  *
609e098bc96SEvan Quan  * Clock conversion (Mhz):
610e098bc96SEvan Quan  *
611e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
612e098bc96SEvan Quan  *
613e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
614e098bc96SEvan Quan  *
615e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
616e098bc96SEvan Quan  *
617e098bc96SEvan Quan  * DRAM data rate (MT/s):
618e098bc96SEvan Quan  *
619e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
620e098bc96SEvan Quan  *
621e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
622e098bc96SEvan Quan  *
623e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
624e098bc96SEvan Quan  *
625e098bc96SEvan Quan  * Bandwidth (MB/s):
626e098bc96SEvan Quan  *
627e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
628e098bc96SEvan Quan  *
629e098bc96SEvan Quan  * Some examples:
630e098bc96SEvan Quan  *
631e098bc96SEvan Quan  * G5 on RX460:
632e098bc96SEvan Quan  *
633e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
634e098bc96SEvan Quan  *
635e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
636e098bc96SEvan Quan  *
637e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
638e098bc96SEvan Quan  *
639e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
640e098bc96SEvan Quan  *
641e098bc96SEvan Quan  * G6 on RX5700:
642e098bc96SEvan Quan  *
643e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
644e098bc96SEvan Quan  *
645e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
646e098bc96SEvan Quan  *
647e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
648e098bc96SEvan Quan  *
649e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
650e098bc96SEvan Quan  *
651e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
652e098bc96SEvan Quan  *
653e098bc96SEvan Quan  * Reading the file will display:
654e098bc96SEvan Quan  *
655e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
656e098bc96SEvan Quan  *
657e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
658e098bc96SEvan Quan  *
659e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
660e098bc96SEvan Quan  *
661e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
662e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
663e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
664e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
665e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
666e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
667e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
668e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
669e098bc96SEvan Quan  *
670e098bc96SEvan Quan  *
671e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
672e098bc96SEvan Quan  *
673e098bc96SEvan Quan  * Reading the file will display:
674e098bc96SEvan Quan  *
675e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
676e098bc96SEvan Quan  *
67737a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
67837a58f69SEvan Quan  *   clock labeled OD_MCLK
679e098bc96SEvan Quan  *
680e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
6818f4f5f0bSEvan Quan  *   They can be used to calibrate the sclk voltage curve. This is
6828f4f5f0bSEvan Quan  *   available for Vega20 and NV1X.
6838f4f5f0bSEvan Quan  *
684a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
685e835bc26SEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder, Dimgrey
686e835bc26SEvan Quan  *   Cavefish and some later SMU13 ASICs. For these ASICs, the target
687e835bc26SEvan Quan  *   voltage calculation can be illustrated by "voltage = voltage
688e835bc26SEvan Quan  *   calculated from v/f curve + overdrive vddgfx offset"
689a2b6df4fSEvan Quan  *
690e835bc26SEvan Quan  * - a list of valid ranges for sclk, mclk, voltage curve points
691e835bc26SEvan Quan  *   or voltage offset labeled OD_RANGE
692e098bc96SEvan Quan  *
6930487bbb4SAlex Deucher  * < For APUs >
6940487bbb4SAlex Deucher  *
6950487bbb4SAlex Deucher  * Reading the file will display:
6960487bbb4SAlex Deucher  *
6970487bbb4SAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
6980487bbb4SAlex Deucher  *
6990487bbb4SAlex Deucher  * - a list of valid ranges for sclk labeled OD_RANGE
7000487bbb4SAlex Deucher  *
7013dc8077fSAlex Deucher  * < For VanGogh >
7023dc8077fSAlex Deucher  *
7033dc8077fSAlex Deucher  * Reading the file will display:
7043dc8077fSAlex Deucher  *
7053dc8077fSAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7063dc8077fSAlex Deucher  * - minimum and maximum core clocks labeled OD_CCLK
7073dc8077fSAlex Deucher  *
7083dc8077fSAlex Deucher  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
7093dc8077fSAlex Deucher  *
710e098bc96SEvan Quan  * To manually adjust these settings:
711e098bc96SEvan Quan  *
712e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
713e098bc96SEvan Quan  *
714e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
715e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
716e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
717e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
7183dc8077fSAlex Deucher  *   "m 1 800" will update maximum mclk to be 800Mhz. For core
7193dc8077fSAlex Deucher  *   clocks on VanGogh, the string contains "p core index clock".
7203dc8077fSAlex Deucher  *   E.g., "p 2 0 800" would set the minimum core clock on core
7213dc8077fSAlex Deucher  *   2 to 800Mhz.
722e098bc96SEvan Quan  *
723e835bc26SEvan Quan  *   For sclk voltage curve supported by Vega20 and NV1X, enter the new
724e835bc26SEvan Quan  *   values by writing a string that contains "vc point clock voltage"
725e835bc26SEvan Quan  *   to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300
726e835bc26SEvan Quan  *   600" will update point1 with clock set as 300Mhz and voltage as 600mV.
727e835bc26SEvan Quan  *   "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and
7288f4f5f0bSEvan Quan  *   voltage 1000mV.
729e098bc96SEvan Quan  *
730e835bc26SEvan Quan  *   For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey
731e835bc26SEvan Quan  *   Cavefish and some later SMU13 ASICs, enter the new value by writing a
732e835bc26SEvan Quan  *   string that contains "vo offset". E.g., "vo -10" will update the extra
733e835bc26SEvan Quan  *   voltage offset applied to the whole v/f curve line as -10mv.
734a2b6df4fSEvan Quan  *
735e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
736e098bc96SEvan Quan  *   to the file to commit your changes
737e098bc96SEvan Quan  *
738e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
739e098bc96SEvan Quan  *   to the file to reset them
740e098bc96SEvan Quan  *
741e098bc96SEvan Quan  */
742e098bc96SEvan Quan 
743e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
744e098bc96SEvan Quan 		struct device_attribute *attr,
745e098bc96SEvan Quan 		const char *buf,
746e098bc96SEvan Quan 		size_t count)
747e098bc96SEvan Quan {
748e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7491348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
750e098bc96SEvan Quan 	int ret;
751e098bc96SEvan Quan 	uint32_t parameter_size = 0;
752e098bc96SEvan Quan 	long parameter[64];
753e098bc96SEvan Quan 	char buf_cpy[128];
754e098bc96SEvan Quan 	char *tmp_str;
755e098bc96SEvan Quan 	char *sub_str;
756e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
757e098bc96SEvan Quan 	uint32_t type;
758e098bc96SEvan Quan 
75953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
760e098bc96SEvan Quan 		return -EPERM;
761d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
762d2ae842dSAlex Deucher 		return -EPERM;
763e098bc96SEvan Quan 
764e098bc96SEvan Quan 	if (count > 127)
765e098bc96SEvan Quan 		return -EINVAL;
766e098bc96SEvan Quan 
767e098bc96SEvan Quan 	if (*buf == 's')
768e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
7690d90d0ddSHuang Rui 	else if (*buf == 'p')
7700d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
771e098bc96SEvan Quan 	else if (*buf == 'm')
772e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
773e098bc96SEvan Quan 	else if (*buf == 'r')
774e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
775e098bc96SEvan Quan 	else if (*buf == 'c')
776e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
777e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
778e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
779a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
780a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
781e098bc96SEvan Quan 	else
782e098bc96SEvan Quan 		return -EINVAL;
783e098bc96SEvan Quan 
784e098bc96SEvan Quan 	memcpy(buf_cpy, buf, count+1);
785e098bc96SEvan Quan 
786e098bc96SEvan Quan 	tmp_str = buf_cpy;
787e098bc96SEvan Quan 
788a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
789a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
790e098bc96SEvan Quan 		tmp_str++;
791e098bc96SEvan Quan 	while (isspace(*++tmp_str));
792e098bc96SEvan Quan 
793ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
794aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
795aec1d870SMatt Coffin 			continue;
796e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
797e098bc96SEvan Quan 		if (ret)
798e098bc96SEvan Quan 			return -EINVAL;
799e098bc96SEvan Quan 		parameter_size++;
800e098bc96SEvan Quan 
801e098bc96SEvan Quan 		while (isspace(*tmp_str))
802e098bc96SEvan Quan 			tmp_str++;
803e098bc96SEvan Quan 	}
804e098bc96SEvan Quan 
805e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
806e098bc96SEvan Quan 	if (ret < 0) {
807e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
808e098bc96SEvan Quan 		return ret;
809e098bc96SEvan Quan 	}
810e098bc96SEvan Quan 
81179c65f3fSEvan Quan 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
81279c65f3fSEvan Quan 					      type,
81312a6727dSXiaojian Du 					      parameter,
81479c65f3fSEvan Quan 					      parameter_size))
81579c65f3fSEvan Quan 		goto err_out;
81612a6727dSXiaojian Du 
81779c65f3fSEvan Quan 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
81879c65f3fSEvan Quan 					  parameter, parameter_size))
81979c65f3fSEvan Quan 		goto err_out;
820e098bc96SEvan Quan 
821e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
82279c65f3fSEvan Quan 		if (amdgpu_dpm_dispatch_task(adev,
823e098bc96SEvan Quan 					     AMD_PP_TASK_READJUST_POWER_STATE,
82479c65f3fSEvan Quan 					     NULL))
82579c65f3fSEvan Quan 			goto err_out;
82679c65f3fSEvan Quan 	}
82779c65f3fSEvan Quan 
828e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
829e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
83079c65f3fSEvan Quan 
831e098bc96SEvan Quan 	return count;
83279c65f3fSEvan Quan 
83379c65f3fSEvan Quan err_out:
834e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
835e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
836e098bc96SEvan Quan 	return -EINVAL;
837e098bc96SEvan Quan }
838e098bc96SEvan Quan 
839e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
840e098bc96SEvan Quan 		struct device_attribute *attr,
841e098bc96SEvan Quan 		char *buf)
842e098bc96SEvan Quan {
843e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8441348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
845c8cb19c7SDarren Powell 	int size = 0;
846e098bc96SEvan Quan 	int ret;
847c8cb19c7SDarren Powell 	enum pp_clock_type od_clocks[6] = {
848c8cb19c7SDarren Powell 		OD_SCLK,
849c8cb19c7SDarren Powell 		OD_MCLK,
850c8cb19c7SDarren Powell 		OD_VDDC_CURVE,
851c8cb19c7SDarren Powell 		OD_RANGE,
852c8cb19c7SDarren Powell 		OD_VDDGFX_OFFSET,
853c8cb19c7SDarren Powell 		OD_CCLK,
854c8cb19c7SDarren Powell 	};
855c8cb19c7SDarren Powell 	uint clk_index;
856e098bc96SEvan Quan 
85753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
858e098bc96SEvan Quan 		return -EPERM;
859d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
860d2ae842dSAlex Deucher 		return -EPERM;
861e098bc96SEvan Quan 
862e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
863e098bc96SEvan Quan 	if (ret < 0) {
864e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
865e098bc96SEvan Quan 		return ret;
866e098bc96SEvan Quan 	}
867e098bc96SEvan Quan 
868c8cb19c7SDarren Powell 	for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
869c8cb19c7SDarren Powell 		ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
870c8cb19c7SDarren Powell 		if (ret)
871c8cb19c7SDarren Powell 			break;
872c8cb19c7SDarren Powell 	}
873c8cb19c7SDarren Powell 	if (ret == -ENOENT) {
874e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
875e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
876e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
8778f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
878e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
8798f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
880e098bc96SEvan Quan 	}
881c8cb19c7SDarren Powell 
882c8cb19c7SDarren Powell 	if (size == 0)
883c8cb19c7SDarren Powell 		size = sysfs_emit(buf, "\n");
884c8cb19c7SDarren Powell 
885e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
886e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
887e098bc96SEvan Quan 
888e098bc96SEvan Quan 	return size;
889e098bc96SEvan Quan }
890e098bc96SEvan Quan 
891e098bc96SEvan Quan /**
892e098bc96SEvan Quan  * DOC: pp_features
893e098bc96SEvan Quan  *
894e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
895e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
896e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
897e098bc96SEvan Quan  *
898e098bc96SEvan Quan  * Reading back the file will show you the followings:
899e098bc96SEvan Quan  * - Current ppfeature masks
900e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
901e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
902e098bc96SEvan Quan  *
903e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
904e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
905e098bc96SEvan Quan  * new ppfeature masks.
906e098bc96SEvan Quan  */
907e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
908e098bc96SEvan Quan 				      struct device_attribute *attr,
909e098bc96SEvan Quan 				      const char *buf,
910e098bc96SEvan Quan 				      size_t count)
911e098bc96SEvan Quan {
912e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9131348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
914e098bc96SEvan Quan 	uint64_t featuremask;
915e098bc96SEvan Quan 	int ret;
916e098bc96SEvan Quan 
91753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
918e098bc96SEvan Quan 		return -EPERM;
919d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
920d2ae842dSAlex Deucher 		return -EPERM;
921e098bc96SEvan Quan 
922e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
923e098bc96SEvan Quan 	if (ret)
924e098bc96SEvan Quan 		return -EINVAL;
925e098bc96SEvan Quan 
926e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
927e098bc96SEvan Quan 	if (ret < 0) {
928e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
929e098bc96SEvan Quan 		return ret;
930e098bc96SEvan Quan 	}
931e098bc96SEvan Quan 
932e098bc96SEvan Quan 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
93379c65f3fSEvan Quan 
934e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
935e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
93679c65f3fSEvan Quan 
93779c65f3fSEvan Quan 	if (ret)
938e098bc96SEvan Quan 		return -EINVAL;
939e098bc96SEvan Quan 
940e098bc96SEvan Quan 	return count;
941e098bc96SEvan Quan }
942e098bc96SEvan Quan 
943e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
944e098bc96SEvan Quan 				      struct device_attribute *attr,
945e098bc96SEvan Quan 				      char *buf)
946e098bc96SEvan Quan {
947e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9481348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
949e098bc96SEvan Quan 	ssize_t size;
950e098bc96SEvan Quan 	int ret;
951e098bc96SEvan Quan 
95253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
953e098bc96SEvan Quan 		return -EPERM;
954d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
955d2ae842dSAlex Deucher 		return -EPERM;
956e098bc96SEvan Quan 
957e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
958e098bc96SEvan Quan 	if (ret < 0) {
959e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
960e098bc96SEvan Quan 		return ret;
961e098bc96SEvan Quan 	}
962e098bc96SEvan Quan 
963e098bc96SEvan Quan 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
96479c65f3fSEvan Quan 	if (size <= 0)
96509b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
966e098bc96SEvan Quan 
967e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
968e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
969e098bc96SEvan Quan 
970e098bc96SEvan Quan 	return size;
971e098bc96SEvan Quan }
972e098bc96SEvan Quan 
973e098bc96SEvan Quan /**
974e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
975e098bc96SEvan Quan  *
976e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
977e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
978e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
979e098bc96SEvan Quan  * this.
980e098bc96SEvan Quan  *
981e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
982e098bc96SEvan Quan  * Vega10 and later ASICs.
983e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
984e098bc96SEvan Quan  *
985e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
986e098bc96SEvan Quan  * the power state and the clock information for those levels.
987e098bc96SEvan Quan  *
988e098bc96SEvan Quan  * To manually adjust these states, first select manual using
989e098bc96SEvan Quan  * power_dpm_force_performance_level.
990e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
991e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
992e098bc96SEvan Quan  * E.g.,
993e098bc96SEvan Quan  *
994e098bc96SEvan Quan  * .. code-block:: bash
995e098bc96SEvan Quan  *
996e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
997e098bc96SEvan Quan  *
998e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
999e098bc96SEvan Quan  *
1000e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1001e098bc96SEvan Quan  */
1002e098bc96SEvan Quan 
10032ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
10042ea092e5SDarren Powell 		enum pp_clock_type type,
1005e098bc96SEvan Quan 		char *buf)
1006e098bc96SEvan Quan {
1007e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10081348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1009c8cb19c7SDarren Powell 	int size = 0;
1010c8cb19c7SDarren Powell 	int ret = 0;
1011e098bc96SEvan Quan 
101253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1013e098bc96SEvan Quan 		return -EPERM;
1014d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1015d2ae842dSAlex Deucher 		return -EPERM;
1016e098bc96SEvan Quan 
1017e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1018e098bc96SEvan Quan 	if (ret < 0) {
1019e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1020e098bc96SEvan Quan 		return ret;
1021e098bc96SEvan Quan 	}
1022e098bc96SEvan Quan 
1023c8cb19c7SDarren Powell 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1024c8cb19c7SDarren Powell 	if (ret == -ENOENT)
10252ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1026c8cb19c7SDarren Powell 
1027c8cb19c7SDarren Powell 	if (size == 0)
102809b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1029e098bc96SEvan Quan 
1030e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1031e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1032e098bc96SEvan Quan 
1033e098bc96SEvan Quan 	return size;
1034e098bc96SEvan Quan }
1035e098bc96SEvan Quan 
1036e098bc96SEvan Quan /*
1037e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1038e098bc96SEvan Quan  * per line (+1 for \n).
1039e098bc96SEvan Quan  */
1040e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1041e098bc96SEvan Quan 
1042e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1043e098bc96SEvan Quan {
1044e098bc96SEvan Quan 	int ret;
1045c915ef89SDan Carpenter 	unsigned long level;
1046e098bc96SEvan Quan 	char *sub_str = NULL;
1047e098bc96SEvan Quan 	char *tmp;
1048e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1049e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1050e098bc96SEvan Quan 	size_t bytes;
1051e098bc96SEvan Quan 
1052e098bc96SEvan Quan 	*mask = 0;
1053e098bc96SEvan Quan 
1054e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1055e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1056e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1057e098bc96SEvan Quan 	tmp = buf_cpy;
1058ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1059e098bc96SEvan Quan 		if (strlen(sub_str)) {
1060c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1061c915ef89SDan Carpenter 			if (ret || level > 31)
1062e098bc96SEvan Quan 				return -EINVAL;
1063e098bc96SEvan Quan 			*mask |= 1 << level;
1064e098bc96SEvan Quan 		} else
1065e098bc96SEvan Quan 			break;
1066e098bc96SEvan Quan 	}
1067e098bc96SEvan Quan 
1068e098bc96SEvan Quan 	return 0;
1069e098bc96SEvan Quan }
1070e098bc96SEvan Quan 
10712ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
10722ea092e5SDarren Powell 		enum pp_clock_type type,
1073e098bc96SEvan Quan 		const char *buf,
1074e098bc96SEvan Quan 		size_t count)
1075e098bc96SEvan Quan {
1076e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10771348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1078e098bc96SEvan Quan 	int ret;
1079e098bc96SEvan Quan 	uint32_t mask = 0;
1080e098bc96SEvan Quan 
108153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1082e098bc96SEvan Quan 		return -EPERM;
1083d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1084d2ae842dSAlex Deucher 		return -EPERM;
1085e098bc96SEvan Quan 
1086e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1087e098bc96SEvan Quan 	if (ret)
1088e098bc96SEvan Quan 		return ret;
1089e098bc96SEvan Quan 
1090e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1091e098bc96SEvan Quan 	if (ret < 0) {
1092e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1093e098bc96SEvan Quan 		return ret;
1094e098bc96SEvan Quan 	}
1095e098bc96SEvan Quan 
10962ea092e5SDarren Powell 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1097e098bc96SEvan Quan 
1098e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1099e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1100e098bc96SEvan Quan 
1101e098bc96SEvan Quan 	if (ret)
1102e098bc96SEvan Quan 		return -EINVAL;
1103e098bc96SEvan Quan 
1104e098bc96SEvan Quan 	return count;
1105e098bc96SEvan Quan }
1106e098bc96SEvan Quan 
11072ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
11082ea092e5SDarren Powell 		struct device_attribute *attr,
11092ea092e5SDarren Powell 		char *buf)
11102ea092e5SDarren Powell {
11112ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
11122ea092e5SDarren Powell }
11132ea092e5SDarren Powell 
11142ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
11152ea092e5SDarren Powell 		struct device_attribute *attr,
11162ea092e5SDarren Powell 		const char *buf,
11172ea092e5SDarren Powell 		size_t count)
11182ea092e5SDarren Powell {
11192ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
11202ea092e5SDarren Powell }
11212ea092e5SDarren Powell 
1122e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1123e098bc96SEvan Quan 		struct device_attribute *attr,
1124e098bc96SEvan Quan 		char *buf)
1125e098bc96SEvan Quan {
11262ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1127e098bc96SEvan Quan }
1128e098bc96SEvan Quan 
1129e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1130e098bc96SEvan Quan 		struct device_attribute *attr,
1131e098bc96SEvan Quan 		const char *buf,
1132e098bc96SEvan Quan 		size_t count)
1133e098bc96SEvan Quan {
11342ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1135e098bc96SEvan Quan }
1136e098bc96SEvan Quan 
1137e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1138e098bc96SEvan Quan 		struct device_attribute *attr,
1139e098bc96SEvan Quan 		char *buf)
1140e098bc96SEvan Quan {
11412ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1142e098bc96SEvan Quan }
1143e098bc96SEvan Quan 
1144e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1145e098bc96SEvan Quan 		struct device_attribute *attr,
1146e098bc96SEvan Quan 		const char *buf,
1147e098bc96SEvan Quan 		size_t count)
1148e098bc96SEvan Quan {
11492ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1150e098bc96SEvan Quan }
1151e098bc96SEvan Quan 
1152e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1153e098bc96SEvan Quan 		struct device_attribute *attr,
1154e098bc96SEvan Quan 		char *buf)
1155e098bc96SEvan Quan {
11562ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1157e098bc96SEvan Quan }
1158e098bc96SEvan Quan 
1159e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1160e098bc96SEvan Quan 		struct device_attribute *attr,
1161e098bc96SEvan Quan 		const char *buf,
1162e098bc96SEvan Quan 		size_t count)
1163e098bc96SEvan Quan {
11642ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1165e098bc96SEvan Quan }
1166e098bc96SEvan Quan 
11679577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
11689577b0ecSXiaojian Du 		struct device_attribute *attr,
11699577b0ecSXiaojian Du 		char *buf)
11709577b0ecSXiaojian Du {
11712ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
11729577b0ecSXiaojian Du }
11739577b0ecSXiaojian Du 
11749577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
11759577b0ecSXiaojian Du 		struct device_attribute *attr,
11769577b0ecSXiaojian Du 		const char *buf,
11779577b0ecSXiaojian Du 		size_t count)
11789577b0ecSXiaojian Du {
11792ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
11809577b0ecSXiaojian Du }
11819577b0ecSXiaojian Du 
1182d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1183d7001e72STong Liu01 		struct device_attribute *attr,
1184d7001e72STong Liu01 		char *buf)
1185d7001e72STong Liu01 {
1186d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1187d7001e72STong Liu01 }
1188d7001e72STong Liu01 
1189d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1190d7001e72STong Liu01 		struct device_attribute *attr,
1191d7001e72STong Liu01 		const char *buf,
1192d7001e72STong Liu01 		size_t count)
1193d7001e72STong Liu01 {
1194d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1195d7001e72STong Liu01 }
1196d7001e72STong Liu01 
11979577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
11989577b0ecSXiaojian Du 		struct device_attribute *attr,
11999577b0ecSXiaojian Du 		char *buf)
12009577b0ecSXiaojian Du {
12012ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
12029577b0ecSXiaojian Du }
12039577b0ecSXiaojian Du 
12049577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
12059577b0ecSXiaojian Du 		struct device_attribute *attr,
12069577b0ecSXiaojian Du 		const char *buf,
12079577b0ecSXiaojian Du 		size_t count)
12089577b0ecSXiaojian Du {
12092ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
12109577b0ecSXiaojian Du }
12119577b0ecSXiaojian Du 
1212d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1213d7001e72STong Liu01 		struct device_attribute *attr,
1214d7001e72STong Liu01 		char *buf)
1215d7001e72STong Liu01 {
1216d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1217d7001e72STong Liu01 }
1218d7001e72STong Liu01 
1219d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1220d7001e72STong Liu01 		struct device_attribute *attr,
1221d7001e72STong Liu01 		const char *buf,
1222d7001e72STong Liu01 		size_t count)
1223d7001e72STong Liu01 {
1224d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1225d7001e72STong Liu01 }
1226d7001e72STong Liu01 
1227e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1228e098bc96SEvan Quan 		struct device_attribute *attr,
1229e098bc96SEvan Quan 		char *buf)
1230e098bc96SEvan Quan {
12312ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1232e098bc96SEvan Quan }
1233e098bc96SEvan Quan 
1234e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1235e098bc96SEvan Quan 		struct device_attribute *attr,
1236e098bc96SEvan Quan 		const char *buf,
1237e098bc96SEvan Quan 		size_t count)
1238e098bc96SEvan Quan {
12392ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1240e098bc96SEvan Quan }
1241e098bc96SEvan Quan 
1242e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1243e098bc96SEvan Quan 		struct device_attribute *attr,
1244e098bc96SEvan Quan 		char *buf)
1245e098bc96SEvan Quan {
12462ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1247e098bc96SEvan Quan }
1248e098bc96SEvan Quan 
1249e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1250e098bc96SEvan Quan 		struct device_attribute *attr,
1251e098bc96SEvan Quan 		const char *buf,
1252e098bc96SEvan Quan 		size_t count)
1253e098bc96SEvan Quan {
12542ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1255e098bc96SEvan Quan }
1256e098bc96SEvan Quan 
1257e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1258e098bc96SEvan Quan 		struct device_attribute *attr,
1259e098bc96SEvan Quan 		char *buf)
1260e098bc96SEvan Quan {
1261e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12621348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1263e098bc96SEvan Quan 	uint32_t value = 0;
1264e098bc96SEvan Quan 	int ret;
1265e098bc96SEvan Quan 
126653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1267e098bc96SEvan Quan 		return -EPERM;
1268d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1269d2ae842dSAlex Deucher 		return -EPERM;
1270e098bc96SEvan Quan 
1271e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1272e098bc96SEvan Quan 	if (ret < 0) {
1273e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1274e098bc96SEvan Quan 		return ret;
1275e098bc96SEvan Quan 	}
1276e098bc96SEvan Quan 
1277e098bc96SEvan Quan 	value = amdgpu_dpm_get_sclk_od(adev);
1278e098bc96SEvan Quan 
1279e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1280e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1281e098bc96SEvan Quan 
1282a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1283e098bc96SEvan Quan }
1284e098bc96SEvan Quan 
1285e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1286e098bc96SEvan Quan 		struct device_attribute *attr,
1287e098bc96SEvan Quan 		const char *buf,
1288e098bc96SEvan Quan 		size_t count)
1289e098bc96SEvan Quan {
1290e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12911348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1292e098bc96SEvan Quan 	int ret;
1293e098bc96SEvan Quan 	long int value;
1294e098bc96SEvan Quan 
129553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1296e098bc96SEvan Quan 		return -EPERM;
1297d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1298d2ae842dSAlex Deucher 		return -EPERM;
1299e098bc96SEvan Quan 
1300e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1301e098bc96SEvan Quan 
1302e098bc96SEvan Quan 	if (ret)
1303e098bc96SEvan Quan 		return -EINVAL;
1304e098bc96SEvan Quan 
1305e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1306e098bc96SEvan Quan 	if (ret < 0) {
1307e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1308e098bc96SEvan Quan 		return ret;
1309e098bc96SEvan Quan 	}
1310e098bc96SEvan Quan 
1311e098bc96SEvan Quan 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1312e098bc96SEvan Quan 
1313e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1314e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1315e098bc96SEvan Quan 
1316e098bc96SEvan Quan 	return count;
1317e098bc96SEvan Quan }
1318e098bc96SEvan Quan 
1319e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1320e098bc96SEvan Quan 		struct device_attribute *attr,
1321e098bc96SEvan Quan 		char *buf)
1322e098bc96SEvan Quan {
1323e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13241348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1325e098bc96SEvan Quan 	uint32_t value = 0;
1326e098bc96SEvan Quan 	int ret;
1327e098bc96SEvan Quan 
132853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1329e098bc96SEvan Quan 		return -EPERM;
1330d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1331d2ae842dSAlex Deucher 		return -EPERM;
1332e098bc96SEvan Quan 
1333e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1334e098bc96SEvan Quan 	if (ret < 0) {
1335e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1336e098bc96SEvan Quan 		return ret;
1337e098bc96SEvan Quan 	}
1338e098bc96SEvan Quan 
1339e098bc96SEvan Quan 	value = amdgpu_dpm_get_mclk_od(adev);
1340e098bc96SEvan Quan 
1341e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1342e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1343e098bc96SEvan Quan 
1344a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1345e098bc96SEvan Quan }
1346e098bc96SEvan Quan 
1347e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1348e098bc96SEvan Quan 		struct device_attribute *attr,
1349e098bc96SEvan Quan 		const char *buf,
1350e098bc96SEvan Quan 		size_t count)
1351e098bc96SEvan Quan {
1352e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13531348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1354e098bc96SEvan Quan 	int ret;
1355e098bc96SEvan Quan 	long int value;
1356e098bc96SEvan Quan 
135753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1358e098bc96SEvan Quan 		return -EPERM;
1359d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1360d2ae842dSAlex Deucher 		return -EPERM;
1361e098bc96SEvan Quan 
1362e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1363e098bc96SEvan Quan 
1364e098bc96SEvan Quan 	if (ret)
1365e098bc96SEvan Quan 		return -EINVAL;
1366e098bc96SEvan Quan 
1367e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1368e098bc96SEvan Quan 	if (ret < 0) {
1369e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1370e098bc96SEvan Quan 		return ret;
1371e098bc96SEvan Quan 	}
1372e098bc96SEvan Quan 
1373e098bc96SEvan Quan 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1374e098bc96SEvan Quan 
1375e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1376e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1377e098bc96SEvan Quan 
1378e098bc96SEvan Quan 	return count;
1379e098bc96SEvan Quan }
1380e098bc96SEvan Quan 
1381e098bc96SEvan Quan /**
1382e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1383e098bc96SEvan Quan  *
1384e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1385e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1386e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1387e098bc96SEvan Quan  *
1388e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1389e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1390e098bc96SEvan Quan  *
1391e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1392e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1393e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1394e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1395e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1396e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1397e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1398e098bc96SEvan Quan  *
1399e098bc96SEvan Quan  */
1400e098bc96SEvan Quan 
1401e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1402e098bc96SEvan Quan 		struct device_attribute *attr,
1403e098bc96SEvan Quan 		char *buf)
1404e098bc96SEvan Quan {
1405e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14061348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1407e098bc96SEvan Quan 	ssize_t size;
1408e098bc96SEvan Quan 	int ret;
1409e098bc96SEvan Quan 
141053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1411e098bc96SEvan Quan 		return -EPERM;
1412d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1413d2ae842dSAlex Deucher 		return -EPERM;
1414e098bc96SEvan Quan 
1415e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1416e098bc96SEvan Quan 	if (ret < 0) {
1417e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1418e098bc96SEvan Quan 		return ret;
1419e098bc96SEvan Quan 	}
1420e098bc96SEvan Quan 
1421e098bc96SEvan Quan 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
142279c65f3fSEvan Quan 	if (size <= 0)
142309b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1424e098bc96SEvan Quan 
1425e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1426e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1427e098bc96SEvan Quan 
1428e098bc96SEvan Quan 	return size;
1429e098bc96SEvan Quan }
1430e098bc96SEvan Quan 
1431e098bc96SEvan Quan 
1432e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1433e098bc96SEvan Quan 		struct device_attribute *attr,
1434e098bc96SEvan Quan 		const char *buf,
1435e098bc96SEvan Quan 		size_t count)
1436e098bc96SEvan Quan {
1437e098bc96SEvan Quan 	int ret;
1438e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14391348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1440e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1441e098bc96SEvan Quan 	long parameter[64];
1442e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1443e098bc96SEvan Quan 	char *tmp_str;
1444e098bc96SEvan Quan 	uint32_t i = 0;
1445e098bc96SEvan Quan 	char tmp[2];
1446e098bc96SEvan Quan 	long int profile_mode = 0;
1447e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1448e098bc96SEvan Quan 
144953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1450e098bc96SEvan Quan 		return -EPERM;
1451d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1452d2ae842dSAlex Deucher 		return -EPERM;
1453e098bc96SEvan Quan 
1454e098bc96SEvan Quan 	tmp[0] = *(buf);
1455e098bc96SEvan Quan 	tmp[1] = '\0';
1456e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1457e098bc96SEvan Quan 	if (ret)
1458e098bc96SEvan Quan 		return -EINVAL;
1459e098bc96SEvan Quan 
1460e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1461e098bc96SEvan Quan 		if (count < 2 || count > 127)
1462e098bc96SEvan Quan 			return -EINVAL;
1463e098bc96SEvan Quan 		while (isspace(*++buf))
1464e098bc96SEvan Quan 			i++;
1465e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1466e098bc96SEvan Quan 		tmp_str = buf_cpy;
1467ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1468c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1469c2efbc3fSEvan Quan 				continue;
1470e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1471e098bc96SEvan Quan 			if (ret)
1472e098bc96SEvan Quan 				return -EINVAL;
1473e098bc96SEvan Quan 			parameter_size++;
1474e098bc96SEvan Quan 			while (isspace(*tmp_str))
1475e098bc96SEvan Quan 				tmp_str++;
1476e098bc96SEvan Quan 		}
1477e098bc96SEvan Quan 	}
1478e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1479e098bc96SEvan Quan 
1480e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1481e098bc96SEvan Quan 	if (ret < 0) {
1482e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1483e098bc96SEvan Quan 		return ret;
1484e098bc96SEvan Quan 	}
1485e098bc96SEvan Quan 
1486e098bc96SEvan Quan 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1487e098bc96SEvan Quan 
1488e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1489e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1490e098bc96SEvan Quan 
1491e098bc96SEvan Quan 	if (!ret)
1492e098bc96SEvan Quan 		return count;
1493e098bc96SEvan Quan 
1494e098bc96SEvan Quan 	return -EINVAL;
1495e098bc96SEvan Quan }
1496e098bc96SEvan Quan 
1497a5600853SAlex Deucher static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1498d78c227fSMario Limonciello 					   enum amd_pp_sensors sensor,
1499d78c227fSMario Limonciello 					   void *query)
1500d78c227fSMario Limonciello {
1501d78c227fSMario Limonciello 	int r, size = sizeof(uint32_t);
1502d78c227fSMario Limonciello 
1503d78c227fSMario Limonciello 	if (amdgpu_in_reset(adev))
1504d78c227fSMario Limonciello 		return -EPERM;
1505d78c227fSMario Limonciello 	if (adev->in_suspend && !adev->in_runpm)
1506d78c227fSMario Limonciello 		return -EPERM;
1507d78c227fSMario Limonciello 
1508d78c227fSMario Limonciello 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1509d78c227fSMario Limonciello 	if (r < 0) {
1510d78c227fSMario Limonciello 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1511d78c227fSMario Limonciello 		return r;
1512d78c227fSMario Limonciello 	}
1513d78c227fSMario Limonciello 
1514d78c227fSMario Limonciello 	/* get the sensor value */
1515d78c227fSMario Limonciello 	r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1516d78c227fSMario Limonciello 
1517d78c227fSMario Limonciello 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1518d78c227fSMario Limonciello 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1519d78c227fSMario Limonciello 
1520d78c227fSMario Limonciello 	return r;
1521d78c227fSMario Limonciello }
1522d78c227fSMario Limonciello 
1523e098bc96SEvan Quan /**
1524e098bc96SEvan Quan  * DOC: gpu_busy_percent
1525e098bc96SEvan Quan  *
1526e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1527e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1528e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1529e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1530e098bc96SEvan Quan  */
1531e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1532e098bc96SEvan Quan 					   struct device_attribute *attr,
1533e098bc96SEvan Quan 					   char *buf)
1534e098bc96SEvan Quan {
1535e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15361348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1537d78c227fSMario Limonciello 	unsigned int value;
1538d78c227fSMario Limonciello 	int r;
1539e098bc96SEvan Quan 
1540d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1541e098bc96SEvan Quan 	if (r)
1542e098bc96SEvan Quan 		return r;
1543e098bc96SEvan Quan 
1544a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1545e098bc96SEvan Quan }
1546e098bc96SEvan Quan 
1547e098bc96SEvan Quan /**
1548e098bc96SEvan Quan  * DOC: mem_busy_percent
1549e098bc96SEvan Quan  *
1550e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1551e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1552e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1553e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1554e098bc96SEvan Quan  */
1555e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1556e098bc96SEvan Quan 					   struct device_attribute *attr,
1557e098bc96SEvan Quan 					   char *buf)
1558e098bc96SEvan Quan {
1559e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15601348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1561d78c227fSMario Limonciello 	unsigned int value;
1562d78c227fSMario Limonciello 	int r;
1563e098bc96SEvan Quan 
1564d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1565e098bc96SEvan Quan 	if (r)
1566e098bc96SEvan Quan 		return r;
1567e098bc96SEvan Quan 
1568a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1569e098bc96SEvan Quan }
1570e098bc96SEvan Quan 
1571e098bc96SEvan Quan /**
1572e098bc96SEvan Quan  * DOC: pcie_bw
1573e098bc96SEvan Quan  *
1574e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1575e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1576e098bc96SEvan Quan  * The file pcie_bw is used for this.
1577e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1578e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1579e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1580e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1581e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1582e098bc96SEvan Quan  */
1583e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1584e098bc96SEvan Quan 		struct device_attribute *attr,
1585e098bc96SEvan Quan 		char *buf)
1586e098bc96SEvan Quan {
1587e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15881348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1589e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1590e098bc96SEvan Quan 	int ret;
1591e098bc96SEvan Quan 
159253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1593e098bc96SEvan Quan 		return -EPERM;
1594d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1595d2ae842dSAlex Deucher 		return -EPERM;
1596e098bc96SEvan Quan 
1597e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1598e098bc96SEvan Quan 		return -ENODATA;
1599e098bc96SEvan Quan 
1600e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1601e098bc96SEvan Quan 		return -ENODATA;
1602e098bc96SEvan Quan 
1603e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1604e098bc96SEvan Quan 	if (ret < 0) {
1605e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1606e098bc96SEvan Quan 		return ret;
1607e098bc96SEvan Quan 	}
1608e098bc96SEvan Quan 
1609e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1610e098bc96SEvan Quan 
1611e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1612e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1613e098bc96SEvan Quan 
1614a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1615e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1616e098bc96SEvan Quan }
1617e098bc96SEvan Quan 
1618e098bc96SEvan Quan /**
1619e098bc96SEvan Quan  * DOC: unique_id
1620e098bc96SEvan Quan  *
1621e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1622e098bc96SEvan Quan  * The file unique_id is used for this.
1623e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1624e098bc96SEvan Quan  *
1625e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1626e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1627e098bc96SEvan Quan  */
1628e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1629e098bc96SEvan Quan 		struct device_attribute *attr,
1630e098bc96SEvan Quan 		char *buf)
1631e098bc96SEvan Quan {
1632e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16331348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1634e098bc96SEvan Quan 
163553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1636e098bc96SEvan Quan 		return -EPERM;
1637d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1638d2ae842dSAlex Deucher 		return -EPERM;
1639e098bc96SEvan Quan 
1640e098bc96SEvan Quan 	if (adev->unique_id)
1641a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1642e098bc96SEvan Quan 
1643e098bc96SEvan Quan 	return 0;
1644e098bc96SEvan Quan }
1645e098bc96SEvan Quan 
1646e098bc96SEvan Quan /**
1647e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1648e098bc96SEvan Quan  *
1649e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1650e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1651e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1652e098bc96SEvan Quan  * the log frequency.
1653e098bc96SEvan Quan  *
1654e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1655e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1656e098bc96SEvan Quan  *
1657e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1658e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1659e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1660e098bc96SEvan Quan  */
1661e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1662e098bc96SEvan Quan 						     struct device_attribute *attr,
1663e098bc96SEvan Quan 						     char *buf)
1664e098bc96SEvan Quan {
1665e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1667e098bc96SEvan Quan 
1668a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
16694a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1670e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1671e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1672e098bc96SEvan Quan }
1673e098bc96SEvan Quan 
1674e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1675e098bc96SEvan Quan 						     struct device_attribute *attr,
1676e098bc96SEvan Quan 						     const char *buf,
1677e098bc96SEvan Quan 						     size_t count)
1678e098bc96SEvan Quan {
1679e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16801348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1681e098bc96SEvan Quan 	long throttling_logging_interval;
1682e098bc96SEvan Quan 	unsigned long flags;
1683e098bc96SEvan Quan 	int ret = 0;
1684e098bc96SEvan Quan 
1685e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1686e098bc96SEvan Quan 	if (ret)
1687e098bc96SEvan Quan 		return ret;
1688e098bc96SEvan Quan 
1689e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1690e098bc96SEvan Quan 		return -EINVAL;
1691e098bc96SEvan Quan 
1692e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1693e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1694e098bc96SEvan Quan 		/*
1695e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1696e098bc96SEvan Quan 		 * This can effectively restart the timer.
1697e098bc96SEvan Quan 		 */
1698e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1699e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1700e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1701e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1702e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1703e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1704e098bc96SEvan Quan 
1705e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1706e098bc96SEvan Quan 	} else {
1707e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1708e098bc96SEvan Quan 	}
1709e098bc96SEvan Quan 
1710e098bc96SEvan Quan 	return count;
1711e098bc96SEvan Quan }
1712e098bc96SEvan Quan 
1713e098bc96SEvan Quan /**
1714c3ed0e72SKun Liu  * DOC: apu_thermal_cap
1715c3ed0e72SKun Liu  *
1716c3ed0e72SKun Liu  * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1717c3ed0e72SKun Liu  * limit temperature in millidegrees Celsius
1718c3ed0e72SKun Liu  *
1719c3ed0e72SKun Liu  * Reading back the file shows you core limit value
1720c3ed0e72SKun Liu  *
1721c3ed0e72SKun Liu  * Writing an integer to the file, sets a new thermal limit. The value
1722c3ed0e72SKun Liu  * should be between 0 and 100. If the value is less than 0 or greater
1723c3ed0e72SKun Liu  * than 100, then the write request will be ignored.
1724c3ed0e72SKun Liu  */
1725c3ed0e72SKun Liu static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1726c3ed0e72SKun Liu 					 struct device_attribute *attr,
1727c3ed0e72SKun Liu 					 char *buf)
1728c3ed0e72SKun Liu {
1729c3ed0e72SKun Liu 	int ret, size;
1730c3ed0e72SKun Liu 	u32 limit;
1731c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1732c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1733c3ed0e72SKun Liu 
1734c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1735c3ed0e72SKun Liu 	if (ret < 0) {
1736c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1737c3ed0e72SKun Liu 		return ret;
1738c3ed0e72SKun Liu 	}
1739c3ed0e72SKun Liu 
1740c3ed0e72SKun Liu 	ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1741c3ed0e72SKun Liu 	if (!ret)
1742c3ed0e72SKun Liu 		size = sysfs_emit(buf, "%u\n", limit);
1743c3ed0e72SKun Liu 	else
1744c3ed0e72SKun Liu 		size = sysfs_emit(buf, "failed to get thermal limit\n");
1745c3ed0e72SKun Liu 
1746c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1747c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1748c3ed0e72SKun Liu 
1749c3ed0e72SKun Liu 	return size;
1750c3ed0e72SKun Liu }
1751c3ed0e72SKun Liu 
1752c3ed0e72SKun Liu static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1753c3ed0e72SKun Liu 					 struct device_attribute *attr,
1754c3ed0e72SKun Liu 					 const char *buf,
1755c3ed0e72SKun Liu 					 size_t count)
1756c3ed0e72SKun Liu {
1757c3ed0e72SKun Liu 	int ret;
1758c3ed0e72SKun Liu 	u32 value;
1759c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1760c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1761c3ed0e72SKun Liu 
1762c3ed0e72SKun Liu 	ret = kstrtou32(buf, 10, &value);
1763c3ed0e72SKun Liu 	if (ret)
1764c3ed0e72SKun Liu 		return ret;
1765c3ed0e72SKun Liu 
17664d2c09d6SMuhammad Usama Anjum 	if (value > 100) {
1767c3ed0e72SKun Liu 		dev_err(dev, "Invalid argument !\n");
1768c3ed0e72SKun Liu 		return -EINVAL;
1769c3ed0e72SKun Liu 	}
1770c3ed0e72SKun Liu 
1771c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1772c3ed0e72SKun Liu 	if (ret < 0) {
1773c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1774c3ed0e72SKun Liu 		return ret;
1775c3ed0e72SKun Liu 	}
1776c3ed0e72SKun Liu 
1777c3ed0e72SKun Liu 	ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1778c3ed0e72SKun Liu 	if (ret) {
1779c3ed0e72SKun Liu 		dev_err(dev, "failed to update thermal limit\n");
1780c3ed0e72SKun Liu 		return ret;
1781c3ed0e72SKun Liu 	}
1782c3ed0e72SKun Liu 
1783c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1784c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1785c3ed0e72SKun Liu 
1786c3ed0e72SKun Liu 	return count;
1787c3ed0e72SKun Liu }
1788c3ed0e72SKun Liu 
1789c3ed0e72SKun Liu /**
1790e098bc96SEvan Quan  * DOC: gpu_metrics
1791e098bc96SEvan Quan  *
1792e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1793e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1794e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1795e098bc96SEvan Quan  *
1796e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1797e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1798e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1799e098bc96SEvan Quan  * at the same time.
1800e098bc96SEvan Quan  */
1801e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1802e098bc96SEvan Quan 				      struct device_attribute *attr,
1803e098bc96SEvan Quan 				      char *buf)
1804e098bc96SEvan Quan {
1805e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
18061348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1807e098bc96SEvan Quan 	void *gpu_metrics;
1808e098bc96SEvan Quan 	ssize_t size = 0;
1809e098bc96SEvan Quan 	int ret;
1810e098bc96SEvan Quan 
181153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1812e098bc96SEvan Quan 		return -EPERM;
1813d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1814d2ae842dSAlex Deucher 		return -EPERM;
1815e098bc96SEvan Quan 
1816e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1817e098bc96SEvan Quan 	if (ret < 0) {
1818e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1819e098bc96SEvan Quan 		return ret;
1820e098bc96SEvan Quan 	}
1821e098bc96SEvan Quan 
1822e098bc96SEvan Quan 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1823e098bc96SEvan Quan 	if (size <= 0)
1824e098bc96SEvan Quan 		goto out;
1825e098bc96SEvan Quan 
1826e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1827e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1828e098bc96SEvan Quan 
1829e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1830e098bc96SEvan Quan 
1831e098bc96SEvan Quan out:
1832e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1833e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1834e098bc96SEvan Quan 
1835e098bc96SEvan Quan 	return size;
1836e098bc96SEvan Quan }
1837e098bc96SEvan Quan 
1838494c1432SSathishkumar S static int amdgpu_show_powershift_percent(struct device *dev,
1839d78c227fSMario Limonciello 					char *buf, enum amd_pp_sensors sensor)
1840494c1432SSathishkumar S {
1841494c1432SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1842494c1432SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1843494c1432SSathishkumar S 	uint32_t ss_power;
1844494c1432SSathishkumar S 	int r = 0, i;
1845494c1432SSathishkumar S 
1846d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1847494c1432SSathishkumar S 	if (r == -EOPNOTSUPP) {
1848494c1432SSathishkumar S 		/* sensor not available on dGPU, try to read from APU */
1849494c1432SSathishkumar S 		adev = NULL;
1850494c1432SSathishkumar S 		mutex_lock(&mgpu_info.mutex);
1851494c1432SSathishkumar S 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1852494c1432SSathishkumar S 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1853494c1432SSathishkumar S 				adev = mgpu_info.gpu_ins[i].adev;
1854494c1432SSathishkumar S 				break;
1855494c1432SSathishkumar S 			}
1856494c1432SSathishkumar S 		}
1857494c1432SSathishkumar S 		mutex_unlock(&mgpu_info.mutex);
1858494c1432SSathishkumar S 		if (adev)
1859d78c227fSMario Limonciello 			r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1860494c1432SSathishkumar S 	}
1861494c1432SSathishkumar S 
1862d78c227fSMario Limonciello 	if (r)
1863494c1432SSathishkumar S 		return r;
1864d78c227fSMario Limonciello 
1865d78c227fSMario Limonciello 	return sysfs_emit(buf, "%u%%\n", ss_power);
1866494c1432SSathishkumar S }
1867d78c227fSMario Limonciello 
1868a7673a1cSSathishkumar S /**
1869a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1870a7673a1cSSathishkumar S  *
1871a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1872494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1873494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power
1874494c1432SSathishkumar S  * is shifted to APU, the percentage of boost is with respect to APU power
1875494c1432SSathishkumar S  * limit on the platform.
1876a7673a1cSSathishkumar S  */
1877a7673a1cSSathishkumar S 
1878a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1879a7673a1cSSathishkumar S 					       char *buf)
1880a7673a1cSSathishkumar S {
1881d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1882a7673a1cSSathishkumar S }
1883a7673a1cSSathishkumar S 
1884a7673a1cSSathishkumar S /**
1885a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1886a7673a1cSSathishkumar S  *
1887494c1432SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting dGPU power
1888494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1889494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power is
1890494c1432SSathishkumar S  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1891494c1432SSathishkumar S  * limit on the platform.
1892a7673a1cSSathishkumar S  */
1893a7673a1cSSathishkumar S 
1894a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1895a7673a1cSSathishkumar S 						char *buf)
1896a7673a1cSSathishkumar S {
1897d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1898a7673a1cSSathishkumar S }
1899a7673a1cSSathishkumar S 
190030d95a37SSathishkumar S /**
190130d95a37SSathishkumar S  * DOC: smartshift_bias
190230d95a37SSathishkumar S  *
190330d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
190430d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
190530d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
190630d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
190730d95a37SSathishkumar S  */
190830d95a37SSathishkumar S 
190930d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
191030d95a37SSathishkumar S 					  struct device_attribute *attr,
191130d95a37SSathishkumar S 					  char *buf)
191230d95a37SSathishkumar S {
191330d95a37SSathishkumar S 	int r = 0;
191430d95a37SSathishkumar S 
191530d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
191630d95a37SSathishkumar S 
191730d95a37SSathishkumar S 	return r;
191830d95a37SSathishkumar S }
191930d95a37SSathishkumar S 
192030d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
192130d95a37SSathishkumar S 					  struct device_attribute *attr,
192230d95a37SSathishkumar S 					  const char *buf, size_t count)
192330d95a37SSathishkumar S {
192430d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
192530d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
192630d95a37SSathishkumar S 	int r = 0;
192730d95a37SSathishkumar S 	int bias = 0;
192830d95a37SSathishkumar S 
192930d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
193030d95a37SSathishkumar S 		return -EPERM;
193130d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
193230d95a37SSathishkumar S 		return -EPERM;
193330d95a37SSathishkumar S 
193430d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
193530d95a37SSathishkumar S 	if (r < 0) {
193630d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
193730d95a37SSathishkumar S 		return r;
193830d95a37SSathishkumar S 	}
193930d95a37SSathishkumar S 
194030d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
194130d95a37SSathishkumar S 	if (r)
194230d95a37SSathishkumar S 		goto out;
194330d95a37SSathishkumar S 
194430d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
194530d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
194630d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
194730d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
194830d95a37SSathishkumar S 
194930d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
195030d95a37SSathishkumar S 	r = count;
195130d95a37SSathishkumar S 
1952bd4b9bb7SJulia Lawall 	/* TODO: update bias level with SMU message */
195330d95a37SSathishkumar S 
195430d95a37SSathishkumar S out:
195530d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
195630d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
195730d95a37SSathishkumar S 	return r;
195830d95a37SSathishkumar S }
195930d95a37SSathishkumar S 
1960a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1961a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
1962a7673a1cSSathishkumar S {
1963494c1432SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1964a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1965a7673a1cSSathishkumar S 
1966a7673a1cSSathishkumar S 	return 0;
1967a7673a1cSSathishkumar S }
1968a7673a1cSSathishkumar S 
196930d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
197030d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
197130d95a37SSathishkumar S {
1972d78c227fSMario Limonciello 	uint32_t ss_power;
197330d95a37SSathishkumar S 
197430d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
197530d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1976d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1977d78c227fSMario Limonciello 		 (void *)&ss_power))
197830d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1979d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1980d78c227fSMario Limonciello 		 (void *)&ss_power))
198130d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
198230d95a37SSathishkumar S 
198330d95a37SSathishkumar S 	return 0;
198430d95a37SSathishkumar S }
198530d95a37SSathishkumar S 
1986e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1987e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19884215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19897884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19907884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19917884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19927884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1993e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1994e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1995e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1996e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19979577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1998d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
19999577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2000d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2001f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2002f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2003e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2004e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2005ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2006e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
2007ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2008ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2009e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2010ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2011ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2012ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2013c3ed0e72SKun Liu 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2014ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2015a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2016a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
2017a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2018a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
201930d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
202030d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
2021e098bc96SEvan Quan };
2022e098bc96SEvan Quan 
2023e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2024e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2025e098bc96SEvan Quan {
2026e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
20278ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
20288ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2029e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
2030e098bc96SEvan Quan 
2031e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2032e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2033e098bc96SEvan Quan 		return 0;
2034e098bc96SEvan Quan 	}
2035e098bc96SEvan Quan 
2036e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2037e098bc96SEvan Quan 
2038e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
20398ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0))
2040e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2041e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
20428ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0) ||
20430127ab1bSYang Wang 		    !amdgpu_device_has_display_hardware(adev))
2044e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2045e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
20468ecad8d6SLijo Lazar 		if (mp1_ver < IP_VERSION(10, 0, 0))
2047e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2048e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2049e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
205079c65f3fSEvan Quan 		if (amdgpu_dpm_is_overdrive_supported(adev))
2051e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
2052e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
20538ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1))
2054e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2055e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2056e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
2057e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
2058e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2059e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
206060044748SKent Russell 		switch (gc_ver) {
206160044748SKent Russell 		case IP_VERSION(9, 0, 1):
206260044748SKent Russell 		case IP_VERSION(9, 4, 0):
206360044748SKent Russell 		case IP_VERSION(9, 4, 1):
206460044748SKent Russell 		case IP_VERSION(9, 4, 2):
2065baf65745SLijo Lazar 		case IP_VERSION(9, 4, 3):
2066ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
2067276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
206835e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
206935e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
207060044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
207160044748SKent Russell 			break;
207260044748SKent Russell 		default:
2073e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
207460044748SKent Russell 		}
2075e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2076fc8e84a2SLijo Lazar 		if ((adev->flags & AMD_IS_APU &&
2077fc8e84a2SLijo Lazar 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2078fc8e84a2SLijo Lazar 		    gc_ver < IP_VERSION(9, 0, 0))
2079e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2080e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
20818ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2082e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
20839577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
20848ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2085a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
208664440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
20873929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
20882f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
2089707b570fSAsad Kamal 		      gc_ver == IP_VERSION(11, 0, 3) ||
2090707b570fSAsad Kamal 		      gc_ver == IP_VERSION(9, 4, 3)))
20919577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
20920b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
20930b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2094feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2095feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2096feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
20970b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
20989577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
20998ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2100a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
210164440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
21023929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
21032f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
2104707b570fSAsad Kamal 		      gc_ver == IP_VERSION(11, 0, 3) ||
2105707b570fSAsad Kamal 		      gc_ver == IP_VERSION(9, 4, 3)))
21069577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
21070b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
21080b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2109feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2110feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2111feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
21120b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
2113a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
211479c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2115a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
21161b852572SDanijel Slivka 		else if (gc_ver == IP_VERSION(10, 3, 0) && amdgpu_sriov_vf(adev))
21171b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
2118e098bc96SEvan Quan 	}
2119e098bc96SEvan Quan 
21208ecad8d6SLijo Lazar 	switch (gc_ver) {
21218ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 1):
21228ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 2):
21231d0e622fSKevin Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2124e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2125e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2126e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2127e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2128e098bc96SEvan Quan 			dev_attr->store = NULL;
2129e098bc96SEvan Quan 		}
21301d0e622fSKevin Wang 		break;
21311b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
21321b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
21331b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
21341b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
21351b852572SDanijel Slivka 			dev_attr->store = NULL;
21361b852572SDanijel Slivka 		}
21371b852572SDanijel Slivka 		break;
21381d0e622fSKevin Wang 	default:
21391d0e622fSKevin Wang 		break;
2140e098bc96SEvan Quan 	}
2141e098bc96SEvan Quan 
2142ede14a1bSDarren Powell 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2143ede14a1bSDarren Powell 		/* SMU MP1 does not support dcefclk level setting */
21448ecad8d6SLijo Lazar 		if (gc_ver >= IP_VERSION(10, 0, 0)) {
2145ede14a1bSDarren Powell 			dev_attr->attr.mode &= ~S_IWUGO;
2146ede14a1bSDarren Powell 			dev_attr->store = NULL;
2147ede14a1bSDarren Powell 		}
2148ede14a1bSDarren Powell 	}
2149ede14a1bSDarren Powell 
2150e610941cSYiqing Yao 	/* setting should not be allowed from VF if not in one VF mode */
2151e610941cSYiqing Yao 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
215211c9cc95SMarina Nikolic 		dev_attr->attr.mode &= ~S_IWUGO;
215311c9cc95SMarina Nikolic 		dev_attr->store = NULL;
215411c9cc95SMarina Nikolic 	}
215511c9cc95SMarina Nikolic 
2156e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2157e098bc96SEvan Quan 
2158e098bc96SEvan Quan 	return 0;
2159e098bc96SEvan Quan }
2160e098bc96SEvan Quan 
2161e098bc96SEvan Quan 
2162e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2163e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2164e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2165e098bc96SEvan Quan {
2166e098bc96SEvan Quan 	int ret = 0;
2167e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2168e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
216925e6373aSYang Wang 	struct device_attribute *dev_attr;
217025e6373aSYang Wang 	const char *name;
2171e098bc96SEvan Quan 
2172e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2173e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2174e098bc96SEvan Quan 
217525e6373aSYang Wang 	if (!attr)
217625e6373aSYang Wang 		return -EINVAL;
217725e6373aSYang Wang 
217825e6373aSYang Wang 	dev_attr = &attr->dev_attr;
217925e6373aSYang Wang 	name = dev_attr->attr.name;
2180e098bc96SEvan Quan 
21818a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2182e098bc96SEvan Quan 
2183e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2184e098bc96SEvan Quan 	if (ret) {
2185e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2186e098bc96SEvan Quan 			name, ret);
2187e098bc96SEvan Quan 		return ret;
2188e098bc96SEvan Quan 	}
2189e098bc96SEvan Quan 
2190e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2191e098bc96SEvan Quan 		return 0;
2192e098bc96SEvan Quan 
2193e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2194e098bc96SEvan Quan 	if (ret) {
2195e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2196e098bc96SEvan Quan 			name, ret);
2197e098bc96SEvan Quan 	}
2198e098bc96SEvan Quan 
2199e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2200e098bc96SEvan Quan 	if (!attr_entry)
2201e098bc96SEvan Quan 		return -ENOMEM;
2202e098bc96SEvan Quan 
2203e098bc96SEvan Quan 	attr_entry->attr = attr;
2204e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2205e098bc96SEvan Quan 
2206e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2207e098bc96SEvan Quan 
2208e098bc96SEvan Quan 	return ret;
2209e098bc96SEvan Quan }
2210e098bc96SEvan Quan 
2211e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2212e098bc96SEvan Quan {
2213e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2214e098bc96SEvan Quan 
2215e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2216e098bc96SEvan Quan }
2217e098bc96SEvan Quan 
2218e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2219e098bc96SEvan Quan 					     struct list_head *attr_list);
2220e098bc96SEvan Quan 
2221e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2222e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2223e098bc96SEvan Quan 					    uint32_t counts,
2224e098bc96SEvan Quan 					    uint32_t mask,
2225e098bc96SEvan Quan 					    struct list_head *attr_list)
2226e098bc96SEvan Quan {
2227e098bc96SEvan Quan 	int ret = 0;
2228e098bc96SEvan Quan 	uint32_t i = 0;
2229e098bc96SEvan Quan 
2230e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2231e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2232e098bc96SEvan Quan 		if (ret)
2233e098bc96SEvan Quan 			goto failed;
2234e098bc96SEvan Quan 	}
2235e098bc96SEvan Quan 
2236e098bc96SEvan Quan 	return 0;
2237e098bc96SEvan Quan 
2238e098bc96SEvan Quan failed:
2239e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2240e098bc96SEvan Quan 
2241e098bc96SEvan Quan 	return ret;
2242e098bc96SEvan Quan }
2243e098bc96SEvan Quan 
2244e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2245e098bc96SEvan Quan 					     struct list_head *attr_list)
2246e098bc96SEvan Quan {
2247e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2248e098bc96SEvan Quan 
2249e098bc96SEvan Quan 	if (list_empty(attr_list))
2250e098bc96SEvan Quan 		return ;
2251e098bc96SEvan Quan 
2252e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2253e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2254e098bc96SEvan Quan 		list_del(&entry->entry);
2255e098bc96SEvan Quan 		kfree(entry);
2256e098bc96SEvan Quan 	}
2257e098bc96SEvan Quan }
2258e098bc96SEvan Quan 
2259e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2260e098bc96SEvan Quan 				      struct device_attribute *attr,
2261e098bc96SEvan Quan 				      char *buf)
2262e098bc96SEvan Quan {
2263e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2264e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2265d78c227fSMario Limonciello 	int r, temp = 0;
2266e098bc96SEvan Quan 
2267e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2268e098bc96SEvan Quan 		return -EINVAL;
2269e098bc96SEvan Quan 
2270e098bc96SEvan Quan 	switch (channel) {
2271e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2272e098bc96SEvan Quan 		/* get current junction temperature */
2273d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2274d78c227fSMario Limonciello 					   (void *)&temp);
2275e098bc96SEvan Quan 		break;
2276e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2277e098bc96SEvan Quan 		/* get current edge temperature */
2278d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2279d78c227fSMario Limonciello 					   (void *)&temp);
2280e098bc96SEvan Quan 		break;
2281e098bc96SEvan Quan 	case PP_TEMP_MEM:
2282e098bc96SEvan Quan 		/* get current memory temperature */
2283d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2284d78c227fSMario Limonciello 					   (void *)&temp);
2285e098bc96SEvan Quan 		break;
2286e098bc96SEvan Quan 	default:
2287e098bc96SEvan Quan 		r = -EINVAL;
2288e098bc96SEvan Quan 		break;
2289e098bc96SEvan Quan 	}
2290e098bc96SEvan Quan 
2291e098bc96SEvan Quan 	if (r)
2292e098bc96SEvan Quan 		return r;
2293e098bc96SEvan Quan 
2294a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2295e098bc96SEvan Quan }
2296e098bc96SEvan Quan 
2297e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2298e098bc96SEvan Quan 					     struct device_attribute *attr,
2299e098bc96SEvan Quan 					     char *buf)
2300e098bc96SEvan Quan {
2301e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2302e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2303e098bc96SEvan Quan 	int temp;
2304e098bc96SEvan Quan 
2305e098bc96SEvan Quan 	if (hyst)
2306e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2307e098bc96SEvan Quan 	else
2308e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2309e098bc96SEvan Quan 
2310a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2311e098bc96SEvan Quan }
2312e098bc96SEvan Quan 
2313e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2314e098bc96SEvan Quan 					     struct device_attribute *attr,
2315e098bc96SEvan Quan 					     char *buf)
2316e098bc96SEvan Quan {
2317e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2318e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2319e098bc96SEvan Quan 	int temp;
2320e098bc96SEvan Quan 
2321e098bc96SEvan Quan 	if (hyst)
2322e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2323e098bc96SEvan Quan 	else
2324e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2325e098bc96SEvan Quan 
2326a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2327e098bc96SEvan Quan }
2328e098bc96SEvan Quan 
2329e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2330e098bc96SEvan Quan 					     struct device_attribute *attr,
2331e098bc96SEvan Quan 					     char *buf)
2332e098bc96SEvan Quan {
2333e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2334e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2335e098bc96SEvan Quan 	int temp;
2336e098bc96SEvan Quan 
2337e098bc96SEvan Quan 	if (hyst)
2338e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2339e098bc96SEvan Quan 	else
2340e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2341e098bc96SEvan Quan 
2342a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2343e098bc96SEvan Quan }
2344e098bc96SEvan Quan 
2345e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2346e098bc96SEvan Quan 					     struct device_attribute *attr,
2347e098bc96SEvan Quan 					     char *buf)
2348e098bc96SEvan Quan {
2349e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2350e098bc96SEvan Quan 
2351e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2352e098bc96SEvan Quan 		return -EINVAL;
2353e098bc96SEvan Quan 
2354a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2355e098bc96SEvan Quan }
2356e098bc96SEvan Quan 
2357e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2358e098bc96SEvan Quan 					     struct device_attribute *attr,
2359e098bc96SEvan Quan 					     char *buf)
2360e098bc96SEvan Quan {
2361e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2362e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2363e098bc96SEvan Quan 	int temp = 0;
2364e098bc96SEvan Quan 
2365e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2366e098bc96SEvan Quan 		return -EINVAL;
2367e098bc96SEvan Quan 
2368e098bc96SEvan Quan 	switch (channel) {
2369e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2370e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2371e098bc96SEvan Quan 		break;
2372e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2373e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2374e098bc96SEvan Quan 		break;
2375e098bc96SEvan Quan 	case PP_TEMP_MEM:
2376e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2377e098bc96SEvan Quan 		break;
2378e098bc96SEvan Quan 	}
2379e098bc96SEvan Quan 
2380a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2381e098bc96SEvan Quan }
2382e098bc96SEvan Quan 
2383e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2384e098bc96SEvan Quan 					    struct device_attribute *attr,
2385e098bc96SEvan Quan 					    char *buf)
2386e098bc96SEvan Quan {
2387e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2388e098bc96SEvan Quan 	u32 pwm_mode = 0;
2389e098bc96SEvan Quan 	int ret;
2390e098bc96SEvan Quan 
239153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2392e098bc96SEvan Quan 		return -EPERM;
2393d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2394d2ae842dSAlex Deucher 		return -EPERM;
2395e098bc96SEvan Quan 
23964a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2397e098bc96SEvan Quan 	if (ret < 0) {
23984a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2399e098bc96SEvan Quan 		return ret;
2400e098bc96SEvan Quan 	}
2401e098bc96SEvan Quan 
240279c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
240379c65f3fSEvan Quan 
24044a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24054a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
240679c65f3fSEvan Quan 
240779c65f3fSEvan Quan 	if (ret)
2408e098bc96SEvan Quan 		return -EINVAL;
2409e098bc96SEvan Quan 
2410fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2411e098bc96SEvan Quan }
2412e098bc96SEvan Quan 
2413e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2414e098bc96SEvan Quan 					    struct device_attribute *attr,
2415e098bc96SEvan Quan 					    const char *buf,
2416e098bc96SEvan Quan 					    size_t count)
2417e098bc96SEvan Quan {
2418e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2419e098bc96SEvan Quan 	int err, ret;
2420e098bc96SEvan Quan 	int value;
2421e098bc96SEvan Quan 
242253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2423e098bc96SEvan Quan 		return -EPERM;
2424d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2425d2ae842dSAlex Deucher 		return -EPERM;
2426e098bc96SEvan Quan 
2427e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2428e098bc96SEvan Quan 	if (err)
2429e098bc96SEvan Quan 		return err;
2430e098bc96SEvan Quan 
24314a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2432e098bc96SEvan Quan 	if (ret < 0) {
24334a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2434e098bc96SEvan Quan 		return ret;
2435e098bc96SEvan Quan 	}
2436e098bc96SEvan Quan 
243779c65f3fSEvan Quan 	ret = amdgpu_dpm_set_fan_control_mode(adev, value);
243879c65f3fSEvan Quan 
24394a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
24404a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
244179c65f3fSEvan Quan 
244279c65f3fSEvan Quan 	if (ret)
2443e098bc96SEvan Quan 		return -EINVAL;
2444e098bc96SEvan Quan 
2445e098bc96SEvan Quan 	return count;
2446e098bc96SEvan Quan }
2447e098bc96SEvan Quan 
2448e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2449e098bc96SEvan Quan 					 struct device_attribute *attr,
2450e098bc96SEvan Quan 					 char *buf)
2451e098bc96SEvan Quan {
2452fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2453e098bc96SEvan Quan }
2454e098bc96SEvan Quan 
2455e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2456e098bc96SEvan Quan 					 struct device_attribute *attr,
2457e098bc96SEvan Quan 					 char *buf)
2458e098bc96SEvan Quan {
2459fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2460e098bc96SEvan Quan }
2461e098bc96SEvan Quan 
2462e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2463e098bc96SEvan Quan 				     struct device_attribute *attr,
2464e098bc96SEvan Quan 				     const char *buf, size_t count)
2465e098bc96SEvan Quan {
2466e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2467e098bc96SEvan Quan 	int err;
2468e098bc96SEvan Quan 	u32 value;
2469e098bc96SEvan Quan 	u32 pwm_mode;
2470e098bc96SEvan Quan 
247153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2472e098bc96SEvan Quan 		return -EPERM;
2473d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2474d2ae842dSAlex Deucher 		return -EPERM;
2475e098bc96SEvan Quan 
247679c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
247779c65f3fSEvan Quan 	if (err)
247879c65f3fSEvan Quan 		return err;
247979c65f3fSEvan Quan 
24804a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2481e098bc96SEvan Quan 	if (err < 0) {
24824a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2483e098bc96SEvan Quan 		return err;
2484e098bc96SEvan Quan 	}
2485e098bc96SEvan Quan 
248679c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
248779c65f3fSEvan Quan 	if (err)
248879c65f3fSEvan Quan 		goto out;
248979c65f3fSEvan Quan 
2490e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2491e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2492e098bc96SEvan Quan 		err = -EINVAL;
249379c65f3fSEvan Quan 		goto out;
249479c65f3fSEvan Quan 	}
2495e098bc96SEvan Quan 
249679c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
249779c65f3fSEvan Quan 
249879c65f3fSEvan Quan out:
24994a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25004a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2501e098bc96SEvan Quan 
2502e098bc96SEvan Quan 	if (err)
2503e098bc96SEvan Quan 		return err;
2504e098bc96SEvan Quan 
2505e098bc96SEvan Quan 	return count;
2506e098bc96SEvan Quan }
2507e098bc96SEvan Quan 
2508e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2509e098bc96SEvan Quan 				     struct device_attribute *attr,
2510e098bc96SEvan Quan 				     char *buf)
2511e098bc96SEvan Quan {
2512e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2513e098bc96SEvan Quan 	int err;
2514e098bc96SEvan Quan 	u32 speed = 0;
2515e098bc96SEvan Quan 
251653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2517e098bc96SEvan Quan 		return -EPERM;
2518d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2519d2ae842dSAlex Deucher 		return -EPERM;
2520e098bc96SEvan Quan 
25214a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2522e098bc96SEvan Quan 	if (err < 0) {
25234a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2524e098bc96SEvan Quan 		return err;
2525e098bc96SEvan Quan 	}
2526e098bc96SEvan Quan 
25270d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2528e098bc96SEvan Quan 
25294a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25304a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2531e098bc96SEvan Quan 
2532e098bc96SEvan Quan 	if (err)
2533e098bc96SEvan Quan 		return err;
2534e098bc96SEvan Quan 
2535fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2536e098bc96SEvan Quan }
2537e098bc96SEvan Quan 
2538e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2539e098bc96SEvan Quan 					   struct device_attribute *attr,
2540e098bc96SEvan Quan 					   char *buf)
2541e098bc96SEvan Quan {
2542e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2543e098bc96SEvan Quan 	int err;
2544e098bc96SEvan Quan 	u32 speed = 0;
2545e098bc96SEvan Quan 
254653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2547e098bc96SEvan Quan 		return -EPERM;
2548d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2549d2ae842dSAlex Deucher 		return -EPERM;
2550e098bc96SEvan Quan 
25514a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2552e098bc96SEvan Quan 	if (err < 0) {
25534a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2554e098bc96SEvan Quan 		return err;
2555e098bc96SEvan Quan 	}
2556e098bc96SEvan Quan 
2557e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2558e098bc96SEvan Quan 
25594a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25604a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2561e098bc96SEvan Quan 
2562e098bc96SEvan Quan 	if (err)
2563e098bc96SEvan Quan 		return err;
2564e098bc96SEvan Quan 
2565fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2566e098bc96SEvan Quan }
2567e098bc96SEvan Quan 
2568e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2569e098bc96SEvan Quan 					 struct device_attribute *attr,
2570e098bc96SEvan Quan 					 char *buf)
2571e098bc96SEvan Quan {
2572e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2573e098bc96SEvan Quan 	u32 min_rpm = 0;
2574e098bc96SEvan Quan 	int r;
2575e098bc96SEvan Quan 
2576d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2577d78c227fSMario Limonciello 				   (void *)&min_rpm);
2578e098bc96SEvan Quan 
2579e098bc96SEvan Quan 	if (r)
2580e098bc96SEvan Quan 		return r;
2581e098bc96SEvan Quan 
2582a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2583e098bc96SEvan Quan }
2584e098bc96SEvan Quan 
2585e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2586e098bc96SEvan Quan 					 struct device_attribute *attr,
2587e098bc96SEvan Quan 					 char *buf)
2588e098bc96SEvan Quan {
2589e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2590e098bc96SEvan Quan 	u32 max_rpm = 0;
2591e098bc96SEvan Quan 	int r;
2592e098bc96SEvan Quan 
2593d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2594d78c227fSMario Limonciello 				   (void *)&max_rpm);
2595e098bc96SEvan Quan 
2596e098bc96SEvan Quan 	if (r)
2597e098bc96SEvan Quan 		return r;
2598e098bc96SEvan Quan 
2599a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2600e098bc96SEvan Quan }
2601e098bc96SEvan Quan 
2602e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2603e098bc96SEvan Quan 					   struct device_attribute *attr,
2604e098bc96SEvan Quan 					   char *buf)
2605e098bc96SEvan Quan {
2606e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2607e098bc96SEvan Quan 	int err;
2608e098bc96SEvan Quan 	u32 rpm = 0;
2609e098bc96SEvan Quan 
261053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2611e098bc96SEvan Quan 		return -EPERM;
2612d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2613d2ae842dSAlex Deucher 		return -EPERM;
2614e098bc96SEvan Quan 
26154a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2616e098bc96SEvan Quan 	if (err < 0) {
26174a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2618e098bc96SEvan Quan 		return err;
2619e098bc96SEvan Quan 	}
2620e098bc96SEvan Quan 
2621e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2622e098bc96SEvan Quan 
26234a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26244a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2625e098bc96SEvan Quan 
2626e098bc96SEvan Quan 	if (err)
2627e098bc96SEvan Quan 		return err;
2628e098bc96SEvan Quan 
2629fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2630e098bc96SEvan Quan }
2631e098bc96SEvan Quan 
2632e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2633e098bc96SEvan Quan 				     struct device_attribute *attr,
2634e098bc96SEvan Quan 				     const char *buf, size_t count)
2635e098bc96SEvan Quan {
2636e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2637e098bc96SEvan Quan 	int err;
2638e098bc96SEvan Quan 	u32 value;
2639e098bc96SEvan Quan 	u32 pwm_mode;
2640e098bc96SEvan Quan 
264153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2642e098bc96SEvan Quan 		return -EPERM;
2643d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2644d2ae842dSAlex Deucher 		return -EPERM;
2645e098bc96SEvan Quan 
264679c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
264779c65f3fSEvan Quan 	if (err)
264879c65f3fSEvan Quan 		return err;
264979c65f3fSEvan Quan 
26504a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2651e098bc96SEvan Quan 	if (err < 0) {
26524a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2653e098bc96SEvan Quan 		return err;
2654e098bc96SEvan Quan 	}
2655e098bc96SEvan Quan 
265679c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
265779c65f3fSEvan Quan 	if (err)
265879c65f3fSEvan Quan 		goto out;
2659e098bc96SEvan Quan 
2660e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
266179c65f3fSEvan Quan 		err = -ENODATA;
266279c65f3fSEvan Quan 		goto out;
2663e098bc96SEvan Quan 	}
2664e098bc96SEvan Quan 
2665e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2666e098bc96SEvan Quan 
266779c65f3fSEvan Quan out:
26684a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26694a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2670e098bc96SEvan Quan 
2671e098bc96SEvan Quan 	if (err)
2672e098bc96SEvan Quan 		return err;
2673e098bc96SEvan Quan 
2674e098bc96SEvan Quan 	return count;
2675e098bc96SEvan Quan }
2676e098bc96SEvan Quan 
2677e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2678e098bc96SEvan Quan 					    struct device_attribute *attr,
2679e098bc96SEvan Quan 					    char *buf)
2680e098bc96SEvan Quan {
2681e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2682e098bc96SEvan Quan 	u32 pwm_mode = 0;
2683e098bc96SEvan Quan 	int ret;
2684e098bc96SEvan Quan 
268553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2686e098bc96SEvan Quan 		return -EPERM;
2687d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2688d2ae842dSAlex Deucher 		return -EPERM;
2689e098bc96SEvan Quan 
26904a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2691e098bc96SEvan Quan 	if (ret < 0) {
26924a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2693e098bc96SEvan Quan 		return ret;
2694e098bc96SEvan Quan 	}
2695e098bc96SEvan Quan 
269679c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
269779c65f3fSEvan Quan 
26984a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26994a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
270079c65f3fSEvan Quan 
270179c65f3fSEvan Quan 	if (ret)
2702e098bc96SEvan Quan 		return -EINVAL;
2703e098bc96SEvan Quan 
2704fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2705e098bc96SEvan Quan }
2706e098bc96SEvan Quan 
2707e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2708e098bc96SEvan Quan 					    struct device_attribute *attr,
2709e098bc96SEvan Quan 					    const char *buf,
2710e098bc96SEvan Quan 					    size_t count)
2711e098bc96SEvan Quan {
2712e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2713e098bc96SEvan Quan 	int err;
2714e098bc96SEvan Quan 	int value;
2715e098bc96SEvan Quan 	u32 pwm_mode;
2716e098bc96SEvan Quan 
271753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2718e098bc96SEvan Quan 		return -EPERM;
2719d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2720d2ae842dSAlex Deucher 		return -EPERM;
2721e098bc96SEvan Quan 
2722e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2723e098bc96SEvan Quan 	if (err)
2724e098bc96SEvan Quan 		return err;
2725e098bc96SEvan Quan 
2726e098bc96SEvan Quan 	if (value == 0)
2727e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2728e098bc96SEvan Quan 	else if (value == 1)
2729e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2730e098bc96SEvan Quan 	else
2731e098bc96SEvan Quan 		return -EINVAL;
2732e098bc96SEvan Quan 
27334a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2734e098bc96SEvan Quan 	if (err < 0) {
27354a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2736e098bc96SEvan Quan 		return err;
2737e098bc96SEvan Quan 	}
2738e098bc96SEvan Quan 
273979c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2740e098bc96SEvan Quan 
27414a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27424a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2743e098bc96SEvan Quan 
274479c65f3fSEvan Quan 	if (err)
274579c65f3fSEvan Quan 		return -EINVAL;
274679c65f3fSEvan Quan 
2747e098bc96SEvan Quan 	return count;
2748e098bc96SEvan Quan }
2749e098bc96SEvan Quan 
2750e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2751e098bc96SEvan Quan 					struct device_attribute *attr,
2752e098bc96SEvan Quan 					char *buf)
2753e098bc96SEvan Quan {
2754e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2755e098bc96SEvan Quan 	u32 vddgfx;
2756d78c227fSMario Limonciello 	int r;
2757e098bc96SEvan Quan 
2758e098bc96SEvan Quan 	/* get the voltage */
2759d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
2760d78c227fSMario Limonciello 				   (void *)&vddgfx);
2761e098bc96SEvan Quan 	if (r)
2762e098bc96SEvan Quan 		return r;
2763e098bc96SEvan Quan 
2764a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
2765e098bc96SEvan Quan }
2766e098bc96SEvan Quan 
2767e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2768e098bc96SEvan Quan 					      struct device_attribute *attr,
2769e098bc96SEvan Quan 					      char *buf)
2770e098bc96SEvan Quan {
2771a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
2772e098bc96SEvan Quan }
2773e098bc96SEvan Quan 
2774e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2775e098bc96SEvan Quan 				       struct device_attribute *attr,
2776e098bc96SEvan Quan 				       char *buf)
2777e098bc96SEvan Quan {
2778e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2779e098bc96SEvan Quan 	u32 vddnb;
2780d78c227fSMario Limonciello 	int r;
2781e098bc96SEvan Quan 
2782e098bc96SEvan Quan 	/* only APUs have vddnb */
2783e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2784e098bc96SEvan Quan 		return -EINVAL;
2785e098bc96SEvan Quan 
2786e098bc96SEvan Quan 	/* get the voltage */
2787d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
2788d78c227fSMario Limonciello 				   (void *)&vddnb);
2789e098bc96SEvan Quan 	if (r)
2790e098bc96SEvan Quan 		return r;
2791e098bc96SEvan Quan 
2792a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
2793e098bc96SEvan Quan }
2794e098bc96SEvan Quan 
2795e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2796e098bc96SEvan Quan 					      struct device_attribute *attr,
2797e098bc96SEvan Quan 					      char *buf)
2798e098bc96SEvan Quan {
2799a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
2800e098bc96SEvan Quan }
2801e098bc96SEvan Quan 
2802a5600853SAlex Deucher static int amdgpu_hwmon_get_power(struct device *dev,
2803d78c227fSMario Limonciello 				  enum amd_pp_sensors sensor)
2804e098bc96SEvan Quan {
2805e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2806d78c227fSMario Limonciello 	unsigned int uw;
2807e098bc96SEvan Quan 	u32 query = 0;
2808d78c227fSMario Limonciello 	int r;
2809e098bc96SEvan Quan 
2810d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
2811e098bc96SEvan Quan 	if (r)
2812e098bc96SEvan Quan 		return r;
2813e098bc96SEvan Quan 
2814e098bc96SEvan Quan 	/* convert to microwatts */
2815e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2816e098bc96SEvan Quan 
2817d78c227fSMario Limonciello 	return uw;
2818d78c227fSMario Limonciello }
2819d78c227fSMario Limonciello 
2820d78c227fSMario Limonciello static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2821d78c227fSMario Limonciello 					   struct device_attribute *attr,
2822d78c227fSMario Limonciello 					   char *buf)
2823d78c227fSMario Limonciello {
2824d1090194SSrinivasan Shanmugam 	ssize_t val;
2825d78c227fSMario Limonciello 
28269366c2e8SMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
2827d78c227fSMario Limonciello 	if (val < 0)
2828d78c227fSMario Limonciello 		return val;
2829d78c227fSMario Limonciello 
2830d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
2831e098bc96SEvan Quan }
2832e098bc96SEvan Quan 
2833bb9f7b68SMario Limonciello static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
2834bb9f7b68SMario Limonciello 					     struct device_attribute *attr,
2835bb9f7b68SMario Limonciello 					     char *buf)
2836bb9f7b68SMario Limonciello {
2837d1090194SSrinivasan Shanmugam 	ssize_t val;
2838bb9f7b68SMario Limonciello 
283947f1724dSMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
2840bb9f7b68SMario Limonciello 	if (val < 0)
2841bb9f7b68SMario Limonciello 		return val;
2842bb9f7b68SMario Limonciello 
2843d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
2844bb9f7b68SMario Limonciello }
2845bb9f7b68SMario Limonciello 
2846e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2847e098bc96SEvan Quan 					 struct device_attribute *attr,
2848e098bc96SEvan Quan 					 char *buf)
2849e098bc96SEvan Quan {
2850fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2851e098bc96SEvan Quan }
2852e098bc96SEvan Quan 
285391161b06SDarren Powell 
285491161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2855e098bc96SEvan Quan 					struct device_attribute *attr,
285691161b06SDarren Powell 					char *buf,
285791161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
2858e098bc96SEvan Quan {
2859e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2860a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2861a40a020dSDarren Powell 	uint32_t limit;
2862e098bc96SEvan Quan 	ssize_t size;
2863e098bc96SEvan Quan 	int r;
2864e098bc96SEvan Quan 
286553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2866e098bc96SEvan Quan 		return -EPERM;
2867d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2868d2ae842dSAlex Deucher 		return -EPERM;
2869e098bc96SEvan Quan 
28704a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2871e098bc96SEvan Quan 	if (r < 0) {
28724a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2873e098bc96SEvan Quan 		return r;
2874e098bc96SEvan Quan 	}
2875e098bc96SEvan Quan 
287679c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
287704bec521SDarren Powell 				      pp_limit_level, power_type);
2878dc2a8240SDarren Powell 
2879dc2a8240SDarren Powell 	if (!r)
288009b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2881dc2a8240SDarren Powell 	else
288209b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
2883e098bc96SEvan Quan 
28844a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28854a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2886e098bc96SEvan Quan 
2887e098bc96SEvan Quan 	return size;
2888e098bc96SEvan Quan }
2889e098bc96SEvan Quan 
289091161b06SDarren Powell 
289191161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
289291161b06SDarren Powell 					 struct device_attribute *attr,
289391161b06SDarren Powell 					 char *buf)
289491161b06SDarren Powell {
289591161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
289691161b06SDarren Powell 
289791161b06SDarren Powell }
289891161b06SDarren Powell 
2899e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2900e098bc96SEvan Quan 					 struct device_attribute *attr,
2901e098bc96SEvan Quan 					 char *buf)
2902e098bc96SEvan Quan {
290391161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2904e098bc96SEvan Quan 
2905e098bc96SEvan Quan }
2906e098bc96SEvan Quan 
29076e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
29086e58941cSEric Huang 					 struct device_attribute *attr,
29096e58941cSEric Huang 					 char *buf)
29106e58941cSEric Huang {
291191161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
29126e58941cSEric Huang 
29136e58941cSEric Huang }
29146e58941cSEric Huang 
2915ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2916ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
2917ae07970aSXiaomeng Hou 					 char *buf)
2918ae07970aSXiaomeng Hou {
29193b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
29208ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2921ae07970aSXiaomeng Hou 
29228ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
2923a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
29243b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
29253b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
29263b99e8e3SYang Wang 	else
29273b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
2928ae07970aSXiaomeng Hou }
2929e098bc96SEvan Quan 
2930e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2931e098bc96SEvan Quan 		struct device_attribute *attr,
2932e098bc96SEvan Quan 		const char *buf,
2933e098bc96SEvan Quan 		size_t count)
2934e098bc96SEvan Quan {
2935e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2936ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
2937e098bc96SEvan Quan 	int err;
2938e098bc96SEvan Quan 	u32 value;
2939e098bc96SEvan Quan 
294053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2941e098bc96SEvan Quan 		return -EPERM;
2942d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2943d2ae842dSAlex Deucher 		return -EPERM;
2944e098bc96SEvan Quan 
2945e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
2946e098bc96SEvan Quan 		return -EINVAL;
2947e098bc96SEvan Quan 
2948e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
2949e098bc96SEvan Quan 	if (err)
2950e098bc96SEvan Quan 		return err;
2951e098bc96SEvan Quan 
2952e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
2953ae07970aSXiaomeng Hou 	value |= limit_type << 24;
2954e098bc96SEvan Quan 
29554a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2956e098bc96SEvan Quan 	if (err < 0) {
29574a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2958e098bc96SEvan Quan 		return err;
2959e098bc96SEvan Quan 	}
2960e098bc96SEvan Quan 
296179c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
2962e098bc96SEvan Quan 
29634a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29644a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2965e098bc96SEvan Quan 
2966e098bc96SEvan Quan 	if (err)
2967e098bc96SEvan Quan 		return err;
2968e098bc96SEvan Quan 
2969e098bc96SEvan Quan 	return count;
2970e098bc96SEvan Quan }
2971e098bc96SEvan Quan 
2972e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2973e098bc96SEvan Quan 				      struct device_attribute *attr,
2974e098bc96SEvan Quan 				      char *buf)
2975e098bc96SEvan Quan {
2976e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2977e098bc96SEvan Quan 	uint32_t sclk;
2978d78c227fSMario Limonciello 	int r;
2979e098bc96SEvan Quan 
2980e098bc96SEvan Quan 	/* get the sclk */
2981d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2982d78c227fSMario Limonciello 				   (void *)&sclk);
2983e098bc96SEvan Quan 	if (r)
2984e098bc96SEvan Quan 		return r;
2985e098bc96SEvan Quan 
2986a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
2987e098bc96SEvan Quan }
2988e098bc96SEvan Quan 
2989e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2990e098bc96SEvan Quan 					    struct device_attribute *attr,
2991e098bc96SEvan Quan 					    char *buf)
2992e098bc96SEvan Quan {
2993a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
2994e098bc96SEvan Quan }
2995e098bc96SEvan Quan 
2996e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2997e098bc96SEvan Quan 				      struct device_attribute *attr,
2998e098bc96SEvan Quan 				      char *buf)
2999e098bc96SEvan Quan {
3000e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3001e098bc96SEvan Quan 	uint32_t mclk;
3002d78c227fSMario Limonciello 	int r;
3003e098bc96SEvan Quan 
3004e098bc96SEvan Quan 	/* get the sclk */
3005d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3006d78c227fSMario Limonciello 				   (void *)&mclk);
3007e098bc96SEvan Quan 	if (r)
3008e098bc96SEvan Quan 		return r;
3009e098bc96SEvan Quan 
3010a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3011e098bc96SEvan Quan }
3012e098bc96SEvan Quan 
3013e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3014e098bc96SEvan Quan 					    struct device_attribute *attr,
3015e098bc96SEvan Quan 					    char *buf)
3016e098bc96SEvan Quan {
3017a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3018e098bc96SEvan Quan }
3019e098bc96SEvan Quan 
3020e098bc96SEvan Quan /**
3021e098bc96SEvan Quan  * DOC: hwmon
3022e098bc96SEvan Quan  *
3023e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3024e098bc96SEvan Quan  *
3025e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3026e098bc96SEvan Quan  *
3027e098bc96SEvan Quan  * - GPU voltage
3028e098bc96SEvan Quan  *
3029e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3030e098bc96SEvan Quan  *
3031e098bc96SEvan Quan  * - GPU power
3032e098bc96SEvan Quan  *
3033e098bc96SEvan Quan  * - GPU fan
3034e098bc96SEvan Quan  *
3035e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3036e098bc96SEvan Quan  *
3037e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3038e098bc96SEvan Quan  *
3039e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3040e098bc96SEvan Quan  *
3041e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3042e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3043e098bc96SEvan Quan  *
3044e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3045e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3046e098bc96SEvan Quan  *
3047e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3048e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3049e098bc96SEvan Quan  *
3050e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3051e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3052e098bc96SEvan Quan  *
3053e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3054e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3055e098bc96SEvan Quan  *
3056e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3057e098bc96SEvan Quan  *
3058e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3059e098bc96SEvan Quan  *
3060e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3061e098bc96SEvan Quan  *
3062e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3063e098bc96SEvan Quan  *
306429f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3065e098bc96SEvan Quan  *
3066bb9f7b68SMario Limonciello  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3067bb9f7b68SMario Limonciello  *
3068e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3069e098bc96SEvan Quan  *
3070e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3071e098bc96SEvan Quan  *
3072e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3073e098bc96SEvan Quan  *
3074e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3075e098bc96SEvan Quan  *
3076e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3077e098bc96SEvan Quan  *
3078e098bc96SEvan Quan  * - pwm1_enable: pulse width modulation fan control method (0: no fan speed control, 1: manual fan speed control using pwm interface, 2: automatic fan speed control)
3079e098bc96SEvan Quan  *
3080e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3081e098bc96SEvan Quan  *
3082e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3083e098bc96SEvan Quan  *
3084e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3085e098bc96SEvan Quan  *
3086e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3087e098bc96SEvan Quan  *
3088e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3089e098bc96SEvan Quan  *
3090e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3091e098bc96SEvan Quan  *
3092e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3093e098bc96SEvan Quan  *
309496401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
309596401f7cSEvan Quan  *       That will get the former one overridden.
309696401f7cSEvan Quan  *
3097e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3098e098bc96SEvan Quan  *
3099e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3100e098bc96SEvan Quan  *
3101e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3102e098bc96SEvan Quan  *
3103e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3104e098bc96SEvan Quan  *
3105e098bc96SEvan Quan  */
3106e098bc96SEvan Quan 
3107e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3108e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3109e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3110e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3111e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3112e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3113e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3114e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3115e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3116e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3117e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3118e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3119e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3120e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3121e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3122e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3123e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3124e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3125e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3126e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3127e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3128e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3129e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3130e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3131e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3132e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3133e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3134e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3135e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3136bb9f7b68SMario Limonciello static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3137e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3138e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3139e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
31406e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3141ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3142ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3143ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3144ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3145ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
31466e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3147ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3148e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3149e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3150e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3151e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3152e098bc96SEvan Quan 
3153e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3154e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3155e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3156e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3157e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3158e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3159e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3160e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3161e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3162e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3163e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3164e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3165e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3166e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3167e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3168e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3169e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3170e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3171e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3172e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3173e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3174e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3175e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3176e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3177e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3178e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3179e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3180e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3181e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3182e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3183bb9f7b68SMario Limonciello 	&sensor_dev_attr_power1_input.dev_attr.attr,
3184e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3185e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3186e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
31876e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3188ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3189ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3190ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3191ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3192ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
31936e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3194ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3195e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3196e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3197e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3198e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3199e098bc96SEvan Quan 	NULL
3200e098bc96SEvan Quan };
3201e098bc96SEvan Quan 
3202e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3203e098bc96SEvan Quan 					struct attribute *attr, int index)
3204e098bc96SEvan Quan {
3205e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3206e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3207e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
32088ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
320915419813SMario Limonciello 	uint32_t tmp;
3210e098bc96SEvan Quan 
3211e098bc96SEvan Quan 	/* under multi-vf mode, the hwmon attributes are all not supported */
3212e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3213e098bc96SEvan Quan 		return 0;
3214e098bc96SEvan Quan 
32154f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
32164f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
32174f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
32184f0f1b58SDanijel Slivka 
3219e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3220e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3221e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3222e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3223e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3224e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3225e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3226e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3227e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3228e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3229e098bc96SEvan Quan 		return 0;
3230e098bc96SEvan Quan 
3231e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3232e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3233e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3234e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3235e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3236e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3237e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3238e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3239e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3240e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3241e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3242e098bc96SEvan Quan 		return 0;
3243e098bc96SEvan Quan 
3244e098bc96SEvan Quan 	/* Skip crit temp on APU */
32458572fa2aSAsad Kamal 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
32468572fa2aSAsad Kamal 	    (gc_ver == IP_VERSION(9, 4, 3))) &&
3247e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3248e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3249e098bc96SEvan Quan 		return 0;
3250e098bc96SEvan Quan 
3251e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3252e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3253e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3254e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3255e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3256e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3257e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3258e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3259e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3260e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3261e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3262e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3263e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3264e098bc96SEvan Quan 		return 0;
3265e098bc96SEvan Quan 
3266e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3267685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3268e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3269685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3270e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3271e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3272e098bc96SEvan Quan 
3273685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3274e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3275685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3276e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3277e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3278e098bc96SEvan Quan 
32798572fa2aSAsad Kamal 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3280ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
32818572fa2aSAsad Kamal 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
32828572fa2aSAsad Kamal 	      (gc_ver != IP_VERSION(9, 4, 3)))) &&
3283367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3284e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
32856e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
32866e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3287e098bc96SEvan Quan 		return 0;
3288e098bc96SEvan Quan 
328989317d42SGuilherme G. Piccoli 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3290367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
32918ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3292367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3293367deb67SAlex Deucher 		return 0;
3294367deb67SAlex Deucher 
329515419813SMario Limonciello 	/* not all products support both average and instantaneous */
329615419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
329715419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
329815419813SMario Limonciello 		return 0;
329915419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
330015419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
330115419813SMario Limonciello 		return 0;
330215419813SMario Limonciello 
3303e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3304685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3305685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3306685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3307685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3308e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3309e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3310e098bc96SEvan Quan 		return 0;
3311e098bc96SEvan Quan 
3312685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3313685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3314e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3315e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3316e098bc96SEvan Quan 		return 0;
3317e098bc96SEvan Quan 
3318e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
33198572fa2aSAsad Kamal 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
33208572fa2aSAsad Kamal 	     (gc_ver == IP_VERSION(9, 4, 3))) &&
3321e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3322e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3323e098bc96SEvan Quan 		return 0;
3324e098bc96SEvan Quan 
33258572fa2aSAsad Kamal 	/* only APUs other than gc 9,4,3 have vddnb */
33268572fa2aSAsad Kamal 	if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) &&
3327e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3328e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3329e098bc96SEvan Quan 		return 0;
3330e098bc96SEvan Quan 
33318572fa2aSAsad Kamal 	/* no mclk on APUs other than gc 9,4,3*/
33328572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3333e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3334e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3335e098bc96SEvan Quan 		return 0;
3336e098bc96SEvan Quan 
33378ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
33388572fa2aSAsad Kamal 	    (gc_ver != IP_VERSION(9, 4, 3)) &&
33398572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3340bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
334107864911SAsad Kamal 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3342bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
334307864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
334407864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
33458572fa2aSAsad Kamal 		return 0;
33468572fa2aSAsad Kamal 
3347bfb4fd20SAsad Kamal 	/* hotspot temperature for gc 9,4,3*/
33488572fa2aSAsad Kamal 	if ((gc_ver == IP_VERSION(9, 4, 3)) &&
33498572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
33508572fa2aSAsad Kamal 	     attr == &sensor_dev_attr_temp1_label.dev_attr.attr))
33518572fa2aSAsad Kamal 		return 0;
33528572fa2aSAsad Kamal 
33538572fa2aSAsad Kamal 	/* only SOC15 dGPUs support hotspot and mem temperatures */
33548572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0) ||
33558572fa2aSAsad Kamal 	    (gc_ver == IP_VERSION(9, 4, 3))) &&
335607864911SAsad Kamal 	     (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3357e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3358e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3359e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3360bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3361e098bc96SEvan Quan 		return 0;
3362e098bc96SEvan Quan 
3363ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
33648ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3365ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3366ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3367ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3368ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
33696e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3370de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3371ae07970aSXiaomeng Hou 		return 0;
3372ae07970aSXiaomeng Hou 
3373e098bc96SEvan Quan 	return effective_mode;
3374e098bc96SEvan Quan }
3375e098bc96SEvan Quan 
3376e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3377e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3378e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3379e098bc96SEvan Quan };
3380e098bc96SEvan Quan 
3381e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3382e098bc96SEvan Quan 	&hwmon_attrgroup,
3383e098bc96SEvan Quan 	NULL
3384e098bc96SEvan Quan };
3385e098bc96SEvan Quan 
3386d7bf1b55SEvan Quan static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3387d7bf1b55SEvan Quan 				       enum pp_clock_type od_type,
3388d7bf1b55SEvan Quan 				       char *buf)
3389d7bf1b55SEvan Quan {
3390d7bf1b55SEvan Quan 	int size = 0;
3391d7bf1b55SEvan Quan 	int ret;
3392d7bf1b55SEvan Quan 
3393d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3394d7bf1b55SEvan Quan 		return -EPERM;
3395d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3396d7bf1b55SEvan Quan 		return -EPERM;
3397d7bf1b55SEvan Quan 
3398d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3399d7bf1b55SEvan Quan 	if (ret < 0) {
3400d7bf1b55SEvan Quan 		pm_runtime_put_autosuspend(adev->dev);
3401d7bf1b55SEvan Quan 		return ret;
3402d7bf1b55SEvan Quan 	}
3403d7bf1b55SEvan Quan 
3404d7bf1b55SEvan Quan 	size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3405d7bf1b55SEvan Quan 	if (size == 0)
3406d7bf1b55SEvan Quan 		size = sysfs_emit(buf, "\n");
3407d7bf1b55SEvan Quan 
3408d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3409d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3410d7bf1b55SEvan Quan 
3411d7bf1b55SEvan Quan 	return size;
3412d7bf1b55SEvan Quan }
3413d7bf1b55SEvan Quan 
3414d7bf1b55SEvan Quan static int parse_input_od_command_lines(const char *buf,
3415d7bf1b55SEvan Quan 					size_t count,
3416d7bf1b55SEvan Quan 					u32 *type,
3417d7bf1b55SEvan Quan 					long *params,
3418d7bf1b55SEvan Quan 					uint32_t *num_of_params)
3419d7bf1b55SEvan Quan {
3420d7bf1b55SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
3421d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3422d7bf1b55SEvan Quan 	char buf_cpy[128] = {0};
3423d7bf1b55SEvan Quan 	char *tmp_str, *sub_str;
3424d7bf1b55SEvan Quan 	int ret;
3425d7bf1b55SEvan Quan 
3426d7bf1b55SEvan Quan 	if (count > sizeof(buf_cpy) - 1)
3427d7bf1b55SEvan Quan 		return -EINVAL;
3428d7bf1b55SEvan Quan 
3429d7bf1b55SEvan Quan 	memcpy(buf_cpy, buf, count);
3430d7bf1b55SEvan Quan 	tmp_str = buf_cpy;
3431d7bf1b55SEvan Quan 
3432d7bf1b55SEvan Quan 	/* skip heading spaces */
3433d7bf1b55SEvan Quan 	while (isspace(*tmp_str))
3434d7bf1b55SEvan Quan 		tmp_str++;
3435d7bf1b55SEvan Quan 
3436d7bf1b55SEvan Quan 	switch (*tmp_str) {
3437d7bf1b55SEvan Quan 	case 'c':
3438d7bf1b55SEvan Quan 		*type = PP_OD_COMMIT_DPM_TABLE;
3439d7bf1b55SEvan Quan 		return 0;
3440d7bf1b55SEvan Quan 	default:
3441d7bf1b55SEvan Quan 		break;
3442d7bf1b55SEvan Quan 	}
3443d7bf1b55SEvan Quan 
3444d7bf1b55SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3445d7bf1b55SEvan Quan 		if (strlen(sub_str) == 0)
3446d7bf1b55SEvan Quan 			continue;
3447d7bf1b55SEvan Quan 
3448d7bf1b55SEvan Quan 		ret = kstrtol(sub_str, 0, &params[parameter_size]);
3449d7bf1b55SEvan Quan 		if (ret)
3450d7bf1b55SEvan Quan 			return -EINVAL;
3451d7bf1b55SEvan Quan 		parameter_size++;
3452d7bf1b55SEvan Quan 
3453d7bf1b55SEvan Quan 		while (isspace(*tmp_str))
3454d7bf1b55SEvan Quan 			tmp_str++;
3455d7bf1b55SEvan Quan 	}
3456d7bf1b55SEvan Quan 
3457d7bf1b55SEvan Quan 	*num_of_params = parameter_size;
3458d7bf1b55SEvan Quan 
3459d7bf1b55SEvan Quan 	return 0;
3460d7bf1b55SEvan Quan }
3461d7bf1b55SEvan Quan 
3462d7bf1b55SEvan Quan static int
3463d7bf1b55SEvan Quan amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3464d7bf1b55SEvan Quan 				     enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3465d7bf1b55SEvan Quan 				     const char *in_buf,
3466d7bf1b55SEvan Quan 				     size_t count)
3467d7bf1b55SEvan Quan {
3468d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3469d7bf1b55SEvan Quan 	long parameter[64];
3470d7bf1b55SEvan Quan 	int ret;
3471d7bf1b55SEvan Quan 
3472d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3473d7bf1b55SEvan Quan 		return -EPERM;
3474d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3475d7bf1b55SEvan Quan 		return -EPERM;
3476d7bf1b55SEvan Quan 
3477d7bf1b55SEvan Quan 	ret = parse_input_od_command_lines(in_buf,
3478d7bf1b55SEvan Quan 					   count,
3479d7bf1b55SEvan Quan 					   &cmd_type,
3480d7bf1b55SEvan Quan 					   parameter,
3481d7bf1b55SEvan Quan 					   &parameter_size);
3482d7bf1b55SEvan Quan 	if (ret)
3483d7bf1b55SEvan Quan 		return ret;
3484d7bf1b55SEvan Quan 
3485d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3486d7bf1b55SEvan Quan 	if (ret < 0)
3487d7bf1b55SEvan Quan 		goto err_out0;
3488d7bf1b55SEvan Quan 
3489d7bf1b55SEvan Quan 	ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3490d7bf1b55SEvan Quan 					    cmd_type,
3491d7bf1b55SEvan Quan 					    parameter,
3492d7bf1b55SEvan Quan 					    parameter_size);
3493d7bf1b55SEvan Quan 	if (ret)
3494d7bf1b55SEvan Quan 		goto err_out1;
3495d7bf1b55SEvan Quan 
3496d7bf1b55SEvan Quan 	if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3497d7bf1b55SEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
3498d7bf1b55SEvan Quan 					       AMD_PP_TASK_READJUST_POWER_STATE,
3499d7bf1b55SEvan Quan 					       NULL);
3500d7bf1b55SEvan Quan 		if (ret)
3501d7bf1b55SEvan Quan 			goto err_out1;
3502d7bf1b55SEvan Quan 	}
3503d7bf1b55SEvan Quan 
3504d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3505d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3506d7bf1b55SEvan Quan 
3507d7bf1b55SEvan Quan 	return count;
3508d7bf1b55SEvan Quan 
3509d7bf1b55SEvan Quan err_out1:
3510d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3511d7bf1b55SEvan Quan err_out0:
3512d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3513d7bf1b55SEvan Quan 
3514d7bf1b55SEvan Quan 	return ret;
3515d7bf1b55SEvan Quan }
3516d7bf1b55SEvan Quan 
3517d7bf1b55SEvan Quan /**
3518d7bf1b55SEvan Quan  * DOC: fan_curve
3519d7bf1b55SEvan Quan  *
3520d7bf1b55SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3521d7bf1b55SEvan Quan  * control curve line.
3522d7bf1b55SEvan Quan  *
3523d7bf1b55SEvan Quan  * Reading back the file shows you the current settings(temperature in Celsius
3524d7bf1b55SEvan Quan  * degree and fan speed in pwm) applied to every anchor point of the curve line
3525d7bf1b55SEvan Quan  * and their permitted ranges if changable.
3526d7bf1b55SEvan Quan  *
3527d7bf1b55SEvan Quan  * Writing a desired string(with the format like "anchor_point_index temperature
3528d7bf1b55SEvan Quan  * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3529d7bf1b55SEvan Quan  * point accordingly.
3530d7bf1b55SEvan Quan  *
3531d7bf1b55SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3532d7bf1b55SEvan Quan  * your changes.
3533d7bf1b55SEvan Quan  *
3534d7bf1b55SEvan Quan  * There are two fan control modes supported: auto and manual. With auto mode,
3535d7bf1b55SEvan Quan  * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3536d7bf1b55SEvan Quan  * While with manual mode, users can set their own fan curve line as what
3537d7bf1b55SEvan Quan  * described here. Normally the ASIC is booted up with auto mode. Any
3538d7bf1b55SEvan Quan  * settings via this interface will switch the fan control to manual mode
3539d7bf1b55SEvan Quan  * implicitly.
3540d7bf1b55SEvan Quan  */
3541d7bf1b55SEvan Quan static ssize_t fan_curve_show(struct kobject *kobj,
3542d7bf1b55SEvan Quan 			      struct kobj_attribute *attr,
3543d7bf1b55SEvan Quan 			      char *buf)
3544d7bf1b55SEvan Quan {
3545d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3546d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3547d7bf1b55SEvan Quan 
3548d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3549d7bf1b55SEvan Quan }
3550d7bf1b55SEvan Quan 
3551d7bf1b55SEvan Quan static ssize_t fan_curve_store(struct kobject *kobj,
3552d7bf1b55SEvan Quan 			       struct kobj_attribute *attr,
3553d7bf1b55SEvan Quan 			       const char *buf,
3554d7bf1b55SEvan Quan 			       size_t count)
3555d7bf1b55SEvan Quan {
3556d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3557d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3558d7bf1b55SEvan Quan 
3559d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3560d7bf1b55SEvan Quan 							     PP_OD_EDIT_FAN_CURVE,
3561d7bf1b55SEvan Quan 							     buf,
3562d7bf1b55SEvan Quan 							     count);
3563d7bf1b55SEvan Quan }
3564d7bf1b55SEvan Quan 
3565d7bf1b55SEvan Quan static umode_t fan_curve_visible(struct amdgpu_device *adev)
3566d7bf1b55SEvan Quan {
3567d7bf1b55SEvan Quan 	umode_t umode = 0000;
3568d7bf1b55SEvan Quan 
3569d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3570d7bf1b55SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3571d7bf1b55SEvan Quan 
3572d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3573d7bf1b55SEvan Quan 		umode |= S_IWUSR;
3574d7bf1b55SEvan Quan 
3575d7bf1b55SEvan Quan 	return umode;
3576d7bf1b55SEvan Quan }
3577d7bf1b55SEvan Quan 
3578*548009adSEvan Quan /**
3579*548009adSEvan Quan  * DOC: acoustic_limit_rpm_threshold
3580*548009adSEvan Quan  *
3581*548009adSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
3582*548009adSEvan Quan  * acoustic limit in RPM for fan control.
3583*548009adSEvan Quan  *
3584*548009adSEvan Quan  * Reading back the file shows you the current setting and the permitted
3585*548009adSEvan Quan  * ranges if changable.
3586*548009adSEvan Quan  *
3587*548009adSEvan Quan  * Writing an integer to the file, change the setting accordingly.
3588*548009adSEvan Quan  *
3589*548009adSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3590*548009adSEvan Quan  * your changes.
3591*548009adSEvan Quan  *
3592*548009adSEvan Quan  * This setting works under auto fan control mode only. It adjusts the PMFW's
3593*548009adSEvan Quan  * behavior about the maximum speed in RPM the fan can spin. Setting via this
3594*548009adSEvan Quan  * interface will switch the fan control to auto mode implicitly.
3595*548009adSEvan Quan  */
3596*548009adSEvan Quan static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
3597*548009adSEvan Quan 					     struct kobj_attribute *attr,
3598*548009adSEvan Quan 					     char *buf)
3599*548009adSEvan Quan {
3600*548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3601*548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3602*548009adSEvan Quan 
3603*548009adSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
3604*548009adSEvan Quan }
3605*548009adSEvan Quan 
3606*548009adSEvan Quan static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
3607*548009adSEvan Quan 					      struct kobj_attribute *attr,
3608*548009adSEvan Quan 					      const char *buf,
3609*548009adSEvan Quan 					      size_t count)
3610*548009adSEvan Quan {
3611*548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3612*548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3613*548009adSEvan Quan 
3614*548009adSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3615*548009adSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_LIMIT,
3616*548009adSEvan Quan 							     buf,
3617*548009adSEvan Quan 							     count);
3618*548009adSEvan Quan }
3619*548009adSEvan Quan 
3620*548009adSEvan Quan static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
3621*548009adSEvan Quan {
3622*548009adSEvan Quan 	umode_t umode = 0000;
3623*548009adSEvan Quan 
3624*548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
3625*548009adSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3626*548009adSEvan Quan 
3627*548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
3628*548009adSEvan Quan 		umode |= S_IWUSR;
3629*548009adSEvan Quan 
3630*548009adSEvan Quan 	return umode;
3631*548009adSEvan Quan }
3632*548009adSEvan Quan 
3633d7bf1b55SEvan Quan static struct od_feature_set amdgpu_od_set = {
3634d7bf1b55SEvan Quan 	.containers = {
3635d7bf1b55SEvan Quan 		[0] = {
3636d7bf1b55SEvan Quan 			.name = "fan_ctrl",
3637d7bf1b55SEvan Quan 			.sub_feature = {
3638d7bf1b55SEvan Quan 				[0] = {
3639d7bf1b55SEvan Quan 					.name = "fan_curve",
3640d7bf1b55SEvan Quan 					.ops = {
3641d7bf1b55SEvan Quan 						.is_visible = fan_curve_visible,
3642d7bf1b55SEvan Quan 						.show = fan_curve_show,
3643d7bf1b55SEvan Quan 						.store = fan_curve_store,
3644d7bf1b55SEvan Quan 					},
3645d7bf1b55SEvan Quan 				},
3646*548009adSEvan Quan 				[1] = {
3647*548009adSEvan Quan 					.name = "acoustic_limit_rpm_threshold",
3648*548009adSEvan Quan 					.ops = {
3649*548009adSEvan Quan 						.is_visible = acoustic_limit_threshold_visible,
3650*548009adSEvan Quan 						.show = acoustic_limit_threshold_show,
3651*548009adSEvan Quan 						.store = acoustic_limit_threshold_store,
3652*548009adSEvan Quan 					},
3653*548009adSEvan Quan 				},
3654d7bf1b55SEvan Quan 			},
3655d7bf1b55SEvan Quan 		},
3656d7bf1b55SEvan Quan 	},
3657d7bf1b55SEvan Quan };
36583e38b634SEvan Quan 
36593e38b634SEvan Quan static void od_kobj_release(struct kobject *kobj)
36603e38b634SEvan Quan {
36613e38b634SEvan Quan 	struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
36623e38b634SEvan Quan 
36633e38b634SEvan Quan 	kfree(od_kobj);
36643e38b634SEvan Quan }
36653e38b634SEvan Quan 
36663e38b634SEvan Quan static const struct kobj_type od_ktype = {
36673e38b634SEvan Quan 	.release	= od_kobj_release,
36683e38b634SEvan Quan 	.sysfs_ops	= &kobj_sysfs_ops,
36693e38b634SEvan Quan };
36703e38b634SEvan Quan 
36713e38b634SEvan Quan static void amdgpu_od_set_fini(struct amdgpu_device *adev)
36723e38b634SEvan Quan {
36733e38b634SEvan Quan 	struct od_kobj *container, *container_next;
36743e38b634SEvan Quan 	struct od_attribute *attribute, *attribute_next;
36753e38b634SEvan Quan 
36763e38b634SEvan Quan 	if (list_empty(&adev->pm.od_kobj_list))
36773e38b634SEvan Quan 		return;
36783e38b634SEvan Quan 
36793e38b634SEvan Quan 	list_for_each_entry_safe(container, container_next,
36803e38b634SEvan Quan 				 &adev->pm.od_kobj_list, entry) {
36813e38b634SEvan Quan 		list_del(&container->entry);
36823e38b634SEvan Quan 
36833e38b634SEvan Quan 		list_for_each_entry_safe(attribute, attribute_next,
36843e38b634SEvan Quan 					 &container->attribute, entry) {
36853e38b634SEvan Quan 			list_del(&attribute->entry);
36863e38b634SEvan Quan 			sysfs_remove_file(&container->kobj,
36873e38b634SEvan Quan 					  &attribute->attribute.attr);
36883e38b634SEvan Quan 			kfree(attribute);
36893e38b634SEvan Quan 		}
36903e38b634SEvan Quan 
36913e38b634SEvan Quan 		kobject_put(&container->kobj);
36923e38b634SEvan Quan 	}
36933e38b634SEvan Quan }
36943e38b634SEvan Quan 
36953e38b634SEvan Quan static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
36963e38b634SEvan Quan 					   struct od_feature_ops *feature_ops)
36973e38b634SEvan Quan {
36983e38b634SEvan Quan 	umode_t mode;
36993e38b634SEvan Quan 
37003e38b634SEvan Quan 	if (!feature_ops->is_visible)
37013e38b634SEvan Quan 		return false;
37023e38b634SEvan Quan 
37033e38b634SEvan Quan 	/*
37043e38b634SEvan Quan 	 * If the feature has no user read and write mode set,
37053e38b634SEvan Quan 	 * we can assume the feature is actually not supported.(?)
37063e38b634SEvan Quan 	 * And the revelant sysfs interface should not be exposed.
37073e38b634SEvan Quan 	 */
37083e38b634SEvan Quan 	mode = feature_ops->is_visible(adev);
37093e38b634SEvan Quan 	if (mode & (S_IRUSR | S_IWUSR))
37103e38b634SEvan Quan 		return true;
37113e38b634SEvan Quan 
37123e38b634SEvan Quan 	return false;
37133e38b634SEvan Quan }
37143e38b634SEvan Quan 
37153e38b634SEvan Quan static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
37163e38b634SEvan Quan 					struct od_feature_container *container)
37173e38b634SEvan Quan {
37183e38b634SEvan Quan 	int i;
37193e38b634SEvan Quan 
37203e38b634SEvan Quan 	/*
37213e38b634SEvan Quan 	 * If there is no valid entry within the container, the container
37223e38b634SEvan Quan 	 * is recognized as a self contained container. And the valid entry
37233e38b634SEvan Quan 	 * here means it has a valid naming and it is visible/supported by
37243e38b634SEvan Quan 	 * the ASIC.
37253e38b634SEvan Quan 	 */
37263e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
37273e38b634SEvan Quan 		if (container->sub_feature[i].name &&
37283e38b634SEvan Quan 		    amdgpu_is_od_feature_supported(adev,
37293e38b634SEvan Quan 			&container->sub_feature[i].ops))
37303e38b634SEvan Quan 			return false;
37313e38b634SEvan Quan 	}
37323e38b634SEvan Quan 
37333e38b634SEvan Quan 	return true;
37343e38b634SEvan Quan }
37353e38b634SEvan Quan 
37363e38b634SEvan Quan static int amdgpu_od_set_init(struct amdgpu_device *adev)
37373e38b634SEvan Quan {
37383e38b634SEvan Quan 	struct od_kobj *top_set, *sub_set;
37393e38b634SEvan Quan 	struct od_attribute *attribute;
37403e38b634SEvan Quan 	struct od_feature_container *container;
37413e38b634SEvan Quan 	struct od_feature_item *feature;
37423e38b634SEvan Quan 	int i, j;
37433e38b634SEvan Quan 	int ret;
37443e38b634SEvan Quan 
37453e38b634SEvan Quan 	/* Setup the top `gpu_od` directory which holds all other OD interfaces */
37463e38b634SEvan Quan 	top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
37473e38b634SEvan Quan 	if (!top_set)
37483e38b634SEvan Quan 		return -ENOMEM;
37493e38b634SEvan Quan 	list_add(&top_set->entry, &adev->pm.od_kobj_list);
37503e38b634SEvan Quan 
37513e38b634SEvan Quan 	ret = kobject_init_and_add(&top_set->kobj,
37523e38b634SEvan Quan 				   &od_ktype,
37533e38b634SEvan Quan 				   &adev->dev->kobj,
37543e38b634SEvan Quan 				   "%s",
37553e38b634SEvan Quan 				   "gpu_od");
37563e38b634SEvan Quan 	if (ret)
37573e38b634SEvan Quan 		goto err_out;
37583e38b634SEvan Quan 	INIT_LIST_HEAD(&top_set->attribute);
37593e38b634SEvan Quan 	top_set->priv = adev;
37603e38b634SEvan Quan 
37613e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
37623e38b634SEvan Quan 		container = &amdgpu_od_set.containers[i];
37633e38b634SEvan Quan 
37643e38b634SEvan Quan 		if (!container->name)
37653e38b634SEvan Quan 			continue;
37663e38b634SEvan Quan 
37673e38b634SEvan Quan 		/*
37683e38b634SEvan Quan 		 * If there is valid entries within the container, the container
37693e38b634SEvan Quan 		 * will be presented as a sub directory and all its holding entries
37703e38b634SEvan Quan 		 * will be presented as plain files under it.
37713e38b634SEvan Quan 		 * While if there is no valid entry within the container, the container
37723e38b634SEvan Quan 		 * itself will be presented as a plain file under top `gpu_od` directory.
37733e38b634SEvan Quan 		 */
37743e38b634SEvan Quan 		if (amdgpu_od_is_self_contained(adev, container)) {
37753e38b634SEvan Quan 			if (!amdgpu_is_od_feature_supported(adev,
37763e38b634SEvan Quan 			     &container->ops))
37773e38b634SEvan Quan 				continue;
37783e38b634SEvan Quan 
37793e38b634SEvan Quan 			/*
37803e38b634SEvan Quan 			 * The container is presented as a plain file under top `gpu_od`
37813e38b634SEvan Quan 			 * directory.
37823e38b634SEvan Quan 			 */
37833e38b634SEvan Quan 			attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
37843e38b634SEvan Quan 			if (!attribute) {
37853e38b634SEvan Quan 				ret = -ENOMEM;
37863e38b634SEvan Quan 				goto err_out;
37873e38b634SEvan Quan 			}
37883e38b634SEvan Quan 			list_add(&attribute->entry, &top_set->attribute);
37893e38b634SEvan Quan 
37903e38b634SEvan Quan 			attribute->attribute.attr.mode =
37913e38b634SEvan Quan 					container->ops.is_visible(adev);
37923e38b634SEvan Quan 			attribute->attribute.attr.name = container->name;
37933e38b634SEvan Quan 			attribute->attribute.show =
37943e38b634SEvan Quan 					container->ops.show;
37953e38b634SEvan Quan 			attribute->attribute.store =
37963e38b634SEvan Quan 					container->ops.store;
37973e38b634SEvan Quan 			ret = sysfs_create_file(&top_set->kobj,
37983e38b634SEvan Quan 						&attribute->attribute.attr);
37993e38b634SEvan Quan 			if (ret)
38003e38b634SEvan Quan 				goto err_out;
38013e38b634SEvan Quan 		} else {
38023e38b634SEvan Quan 			/* The container is presented as a sub directory. */
38033e38b634SEvan Quan 			sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
38043e38b634SEvan Quan 			if (!sub_set) {
38053e38b634SEvan Quan 				ret = -ENOMEM;
38063e38b634SEvan Quan 				goto err_out;
38073e38b634SEvan Quan 			}
38083e38b634SEvan Quan 			list_add(&sub_set->entry, &adev->pm.od_kobj_list);
38093e38b634SEvan Quan 
38103e38b634SEvan Quan 			ret = kobject_init_and_add(&sub_set->kobj,
38113e38b634SEvan Quan 						   &od_ktype,
38123e38b634SEvan Quan 						   &top_set->kobj,
38133e38b634SEvan Quan 						   "%s",
38143e38b634SEvan Quan 						   container->name);
38153e38b634SEvan Quan 			if (ret)
38163e38b634SEvan Quan 				goto err_out;
38173e38b634SEvan Quan 			INIT_LIST_HEAD(&sub_set->attribute);
38183e38b634SEvan Quan 			sub_set->priv = adev;
38193e38b634SEvan Quan 
38203e38b634SEvan Quan 			for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
38213e38b634SEvan Quan 				feature = &container->sub_feature[j];
38223e38b634SEvan Quan 				if (!feature->name)
38233e38b634SEvan Quan 					continue;
38243e38b634SEvan Quan 
38253e38b634SEvan Quan 				if (!amdgpu_is_od_feature_supported(adev,
38263e38b634SEvan Quan 				     &feature->ops))
38273e38b634SEvan Quan 					continue;
38283e38b634SEvan Quan 
38293e38b634SEvan Quan 				/*
38303e38b634SEvan Quan 				 * With the container presented as a sub directory, the entry within
38313e38b634SEvan Quan 				 * it is presented as a plain file under the sub directory.
38323e38b634SEvan Quan 				 */
38333e38b634SEvan Quan 				attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
38343e38b634SEvan Quan 				if (!attribute) {
38353e38b634SEvan Quan 					ret = -ENOMEM;
38363e38b634SEvan Quan 					goto err_out;
38373e38b634SEvan Quan 				}
38383e38b634SEvan Quan 				list_add(&attribute->entry, &sub_set->attribute);
38393e38b634SEvan Quan 
38403e38b634SEvan Quan 				attribute->attribute.attr.mode =
38413e38b634SEvan Quan 						feature->ops.is_visible(adev);
38423e38b634SEvan Quan 				attribute->attribute.attr.name = feature->name;
38433e38b634SEvan Quan 				attribute->attribute.show =
38443e38b634SEvan Quan 						feature->ops.show;
38453e38b634SEvan Quan 				attribute->attribute.store =
38463e38b634SEvan Quan 						feature->ops.store;
38473e38b634SEvan Quan 				ret = sysfs_create_file(&sub_set->kobj,
38483e38b634SEvan Quan 							&attribute->attribute.attr);
38493e38b634SEvan Quan 				if (ret)
38503e38b634SEvan Quan 					goto err_out;
38513e38b634SEvan Quan 			}
38523e38b634SEvan Quan 		}
38533e38b634SEvan Quan 	}
38543e38b634SEvan Quan 
38553e38b634SEvan Quan 	return 0;
38563e38b634SEvan Quan 
38573e38b634SEvan Quan err_out:
38583e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
38593e38b634SEvan Quan 
38603e38b634SEvan Quan 	return ret;
38613e38b634SEvan Quan }
38623e38b634SEvan Quan 
3863e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3864e098bc96SEvan Quan {
3865e098bc96SEvan Quan 	uint32_t mask = 0;
38663e38b634SEvan Quan 	int ret;
3867e098bc96SEvan Quan 
3868e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
3869e098bc96SEvan Quan 		return 0;
3870e098bc96SEvan Quan 
38715fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
38725fa99373SZhenGuo Yin 
3873e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
3874e098bc96SEvan Quan 		return 0;
3875e098bc96SEvan Quan 
3876e098bc96SEvan Quan 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3877e098bc96SEvan Quan 								   DRIVER_NAME, adev,
3878e098bc96SEvan Quan 								   hwmon_groups);
3879e098bc96SEvan Quan 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3880e098bc96SEvan Quan 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3881e098bc96SEvan Quan 		dev_err(adev->dev,
3882e098bc96SEvan Quan 			"Unable to register hwmon device: %d\n", ret);
3883e098bc96SEvan Quan 		return ret;
3884e098bc96SEvan Quan 	}
3885e098bc96SEvan Quan 
3886e098bc96SEvan Quan 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3887e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
3888e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
3889e098bc96SEvan Quan 		break;
3890e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
3891e098bc96SEvan Quan 		mask = 0;
3892e098bc96SEvan Quan 		break;
3893e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
3894e098bc96SEvan Quan 	default:
3895e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
3896e098bc96SEvan Quan 		break;
3897e098bc96SEvan Quan 	}
3898e098bc96SEvan Quan 
3899e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
3900e098bc96SEvan Quan 					       amdgpu_device_attrs,
3901e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
3902e098bc96SEvan Quan 					       mask,
3903e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
3904e098bc96SEvan Quan 	if (ret)
39053e38b634SEvan Quan 		goto err_out0;
39063e38b634SEvan Quan 
39073e38b634SEvan Quan 	if (amdgpu_dpm_is_overdrive_supported(adev)) {
39083e38b634SEvan Quan 		ret = amdgpu_od_set_init(adev);
39093e38b634SEvan Quan 		if (ret)
39103e38b634SEvan Quan 			goto err_out1;
39113e38b634SEvan Quan 	}
3912e098bc96SEvan Quan 
3913e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
3914e098bc96SEvan Quan 
3915e098bc96SEvan Quan 	return 0;
39163e38b634SEvan Quan 
39173e38b634SEvan Quan err_out1:
39183e38b634SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
39193e38b634SEvan Quan err_out0:
39203e38b634SEvan Quan 	if (adev->pm.int_hwmon_dev)
39213e38b634SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
39223e38b634SEvan Quan 
39233e38b634SEvan Quan 	return ret;
3924e098bc96SEvan Quan }
3925e098bc96SEvan Quan 
3926e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3927e098bc96SEvan Quan {
39283e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
39293e38b634SEvan Quan 
3930e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
3931e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3932e098bc96SEvan Quan 
3933e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3934e098bc96SEvan Quan }
3935e098bc96SEvan Quan 
3936e098bc96SEvan Quan /*
3937e098bc96SEvan Quan  * Debugfs info
3938e098bc96SEvan Quan  */
3939e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
3940e098bc96SEvan Quan 
3941517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3942e1b3bcaaSRan Sun 					   struct amdgpu_device *adev)
3943e1b3bcaaSRan Sun {
3944517cb957SHuang Rui 	uint16_t *p_val;
3945517cb957SHuang Rui 	uint32_t size;
3946517cb957SHuang Rui 	int i;
394779c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
3948517cb957SHuang Rui 
394979c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
395079c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
3951517cb957SHuang Rui 				GFP_KERNEL);
3952517cb957SHuang Rui 
3953517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3954517cb957SHuang Rui 					    (void *)p_val, &size)) {
395579c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
3956517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3957517cb957SHuang Rui 					   *(p_val + i), i);
3958517cb957SHuang Rui 		}
3959517cb957SHuang Rui 
3960517cb957SHuang Rui 		kfree(p_val);
3961517cb957SHuang Rui 	}
3962517cb957SHuang Rui }
3963517cb957SHuang Rui 
3964e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3965e098bc96SEvan Quan {
39668ecad8d6SLijo Lazar 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
39678ecad8d6SLijo Lazar 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3968e098bc96SEvan Quan 	uint32_t value;
3969800c53d6SXiaojian Du 	uint64_t value64 = 0;
3970e098bc96SEvan Quan 	uint32_t query = 0;
3971e098bc96SEvan Quan 	int size;
3972e098bc96SEvan Quan 
3973e098bc96SEvan Quan 	/* GPU Clocks */
3974e098bc96SEvan Quan 	size = sizeof(value);
3975e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
3976517cb957SHuang Rui 
3977517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
3978517cb957SHuang Rui 
3979e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3980e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3981e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3982e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3983e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3984e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3985e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3986e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3987e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3988e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3989e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3990e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3991e098bc96SEvan Quan 	size = sizeof(uint32_t);
39929366c2e8SMario Limonciello 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size))
3993e098bc96SEvan Quan 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3994e0e1764aSAlex Deucher 	size = sizeof(uint32_t);
3995e0e1764aSAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size))
3996e0e1764aSAlex Deucher 		seq_printf(m, "\t%u.%u W (current GPU)\n", query >> 8, query & 0xff);
3997e098bc96SEvan Quan 	size = sizeof(value);
3998e098bc96SEvan Quan 	seq_printf(m, "\n");
3999e098bc96SEvan Quan 
4000e098bc96SEvan Quan 	/* GPU Temp */
4001e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4002e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4003e098bc96SEvan Quan 
4004e098bc96SEvan Quan 	/* GPU Load */
4005e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4006e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
4007e098bc96SEvan Quan 	/* MEM Load */
4008e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4009e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
4010e098bc96SEvan Quan 
4011e098bc96SEvan Quan 	seq_printf(m, "\n");
4012e098bc96SEvan Quan 
4013e098bc96SEvan Quan 	/* SMC feature mask */
4014e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4015e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4016e098bc96SEvan Quan 
40178ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
40188ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4019e098bc96SEvan Quan 		/* VCN clocks */
4020e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4021e098bc96SEvan Quan 			if (!value) {
4022e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
4023e098bc96SEvan Quan 			} else {
4024e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
4025e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4026e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4027e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4028e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4029e098bc96SEvan Quan 			}
4030e098bc96SEvan Quan 		}
4031e098bc96SEvan Quan 		seq_printf(m, "\n");
4032e098bc96SEvan Quan 	} else {
4033e098bc96SEvan Quan 		/* UVD clocks */
4034e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4035e098bc96SEvan Quan 			if (!value) {
4036e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
4037e098bc96SEvan Quan 			} else {
4038e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
4039e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4040e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4041e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4042e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4043e098bc96SEvan Quan 			}
4044e098bc96SEvan Quan 		}
4045e098bc96SEvan Quan 		seq_printf(m, "\n");
4046e098bc96SEvan Quan 
4047e098bc96SEvan Quan 		/* VCE clocks */
4048e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4049e098bc96SEvan Quan 			if (!value) {
4050e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
4051e098bc96SEvan Quan 			} else {
4052e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
4053e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4054e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4055e098bc96SEvan Quan 			}
4056e098bc96SEvan Quan 		}
4057e098bc96SEvan Quan 	}
4058e098bc96SEvan Quan 
4059e098bc96SEvan Quan 	return 0;
4060e098bc96SEvan Quan }
4061e098bc96SEvan Quan 
406244762718SNathan Chancellor static const struct cg_flag_name clocks[] = {
406344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
406444762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
406544762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
406644762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
406744762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
406844762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
406944762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
407044762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
407144762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
407244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
407344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
407444762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
407544762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
407644762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
407744762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
407844762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
407944762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
408044762718SNathan Chancellor 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
408144762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
408244762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
408344762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
408444762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
408544762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
408644762718SNathan Chancellor 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
408744762718SNathan Chancellor 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
408844762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
408944762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
409044762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
409144762718SNathan Chancellor 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
409244762718SNathan Chancellor 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
409344762718SNathan Chancellor 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
409444762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
409544762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
409644762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
409744762718SNathan Chancellor 	{0, NULL},
409844762718SNathan Chancellor };
409944762718SNathan Chancellor 
410025faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
4101e098bc96SEvan Quan {
4102e098bc96SEvan Quan 	int i;
4103e098bc96SEvan Quan 
4104e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
4105e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
4106e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
4107e098bc96SEvan Quan }
4108e098bc96SEvan Quan 
4109373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
4110e098bc96SEvan Quan {
4111373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4112373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
411325faeddcSEvan Quan 	u64 flags = 0;
4114e098bc96SEvan Quan 	int r;
4115e098bc96SEvan Quan 
411653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
4117e098bc96SEvan Quan 		return -EPERM;
4118d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
4119d2ae842dSAlex Deucher 		return -EPERM;
4120e098bc96SEvan Quan 
4121e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
4122e098bc96SEvan Quan 	if (r < 0) {
4123e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
4124e098bc96SEvan Quan 		return r;
4125e098bc96SEvan Quan 	}
4126e098bc96SEvan Quan 
412779c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
4128e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
4129e098bc96SEvan Quan 		if (r)
4130e098bc96SEvan Quan 			goto out;
413179c65f3fSEvan Quan 	}
4132e098bc96SEvan Quan 
4133e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
4134e098bc96SEvan Quan 
413525faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
4136e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
4137e098bc96SEvan Quan 	seq_printf(m, "\n");
4138e098bc96SEvan Quan 
4139e098bc96SEvan Quan out:
4140e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
4141e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
4142e098bc96SEvan Quan 
4143e098bc96SEvan Quan 	return r;
4144e098bc96SEvan Quan }
4145e098bc96SEvan Quan 
4146373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4147373720f7SNirmoy Das 
414827ebf21fSLijo Lazar /*
414927ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
415027ebf21fSLijo Lazar  *
415127ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
415227ebf21fSLijo Lazar  */
415327ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
415427ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
415527ebf21fSLijo Lazar {
415627ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
415727ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
415827ebf21fSLijo Lazar 	void *smu_prv_buf;
415979c65f3fSEvan Quan 	int ret = 0;
416027ebf21fSLijo Lazar 
416127ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
416227ebf21fSLijo Lazar 		return -EPERM;
416327ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
416427ebf21fSLijo Lazar 		return -EPERM;
416527ebf21fSLijo Lazar 
416679c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
416779c65f3fSEvan Quan 	if (ret)
416879c65f3fSEvan Quan 		return ret;
416927ebf21fSLijo Lazar 
417027ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
417127ebf21fSLijo Lazar 		return -EINVAL;
417227ebf21fSLijo Lazar 
417327ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
417427ebf21fSLijo Lazar 				       smu_prv_buf_size);
417527ebf21fSLijo Lazar }
417627ebf21fSLijo Lazar 
417727ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
417827ebf21fSLijo Lazar 	.owner = THIS_MODULE,
417927ebf21fSLijo Lazar 	.open = simple_open,
418027ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
418127ebf21fSLijo Lazar 	.llseek = default_llseek,
418227ebf21fSLijo Lazar };
418327ebf21fSLijo Lazar 
4184e098bc96SEvan Quan #endif
4185e098bc96SEvan Quan 
4186373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
4187e098bc96SEvan Quan {
4188e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4189373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
4190373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
4191373720f7SNirmoy Das 
41921613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
41931613f346SFlora Cui 		return;
41941613f346SFlora Cui 
4195373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4196373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
4197373720f7SNirmoy Das 
419827ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
419927ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
420027ebf21fSLijo Lazar 					 adev,
420127ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
420227ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
42031f5fc7a5SAndrey Grodzovsky 
420479c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
4205e098bc96SEvan Quan #endif
4206e098bc96SEvan Quan }
4207