xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision bb619539629cee523df886705d6ef866e099640a)
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 
76408e9ebc7SBas Nieuwenhuizen 	if (count > 127 || count == 0)
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 
78408e9ebc7SBas Nieuwenhuizen 	memcpy(buf_cpy, buf, count);
78508e9ebc7SBas Nieuwenhuizen 	buf_cpy[count] = 0;
786e098bc96SEvan Quan 
787e098bc96SEvan Quan 	tmp_str = buf_cpy;
788e098bc96SEvan Quan 
789a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
790a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
791e098bc96SEvan Quan 		tmp_str++;
792e098bc96SEvan Quan 	while (isspace(*++tmp_str));
793e098bc96SEvan Quan 
794ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
795aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
796aec1d870SMatt Coffin 			continue;
797e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
798e098bc96SEvan Quan 		if (ret)
799e098bc96SEvan Quan 			return -EINVAL;
800e098bc96SEvan Quan 		parameter_size++;
801e098bc96SEvan Quan 
80208e9ebc7SBas Nieuwenhuizen 		if (!tmp_str)
80308e9ebc7SBas Nieuwenhuizen 			break;
80408e9ebc7SBas Nieuwenhuizen 
805e098bc96SEvan Quan 		while (isspace(*tmp_str))
806e098bc96SEvan Quan 			tmp_str++;
807e098bc96SEvan Quan 	}
808e098bc96SEvan Quan 
809e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
810e098bc96SEvan Quan 	if (ret < 0) {
811e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
812e098bc96SEvan Quan 		return ret;
813e098bc96SEvan Quan 	}
814e098bc96SEvan Quan 
81579c65f3fSEvan Quan 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
81679c65f3fSEvan Quan 					      type,
81712a6727dSXiaojian Du 					      parameter,
81879c65f3fSEvan Quan 					      parameter_size))
81979c65f3fSEvan Quan 		goto err_out;
82012a6727dSXiaojian Du 
82179c65f3fSEvan Quan 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
82279c65f3fSEvan Quan 					  parameter, parameter_size))
82379c65f3fSEvan Quan 		goto err_out;
824e098bc96SEvan Quan 
825e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
82679c65f3fSEvan Quan 		if (amdgpu_dpm_dispatch_task(adev,
827e098bc96SEvan Quan 					     AMD_PP_TASK_READJUST_POWER_STATE,
82879c65f3fSEvan Quan 					     NULL))
82979c65f3fSEvan Quan 			goto err_out;
83079c65f3fSEvan Quan 	}
83179c65f3fSEvan Quan 
832e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
833e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
83479c65f3fSEvan Quan 
835e098bc96SEvan Quan 	return count;
83679c65f3fSEvan Quan 
83779c65f3fSEvan Quan err_out:
838e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
839e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
840e098bc96SEvan Quan 	return -EINVAL;
841e098bc96SEvan Quan }
842e098bc96SEvan Quan 
843e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
844e098bc96SEvan Quan 		struct device_attribute *attr,
845e098bc96SEvan Quan 		char *buf)
846e098bc96SEvan Quan {
847e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8481348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
849c8cb19c7SDarren Powell 	int size = 0;
850e098bc96SEvan Quan 	int ret;
851c8cb19c7SDarren Powell 	enum pp_clock_type od_clocks[6] = {
852c8cb19c7SDarren Powell 		OD_SCLK,
853c8cb19c7SDarren Powell 		OD_MCLK,
854c8cb19c7SDarren Powell 		OD_VDDC_CURVE,
855c8cb19c7SDarren Powell 		OD_RANGE,
856c8cb19c7SDarren Powell 		OD_VDDGFX_OFFSET,
857c8cb19c7SDarren Powell 		OD_CCLK,
858c8cb19c7SDarren Powell 	};
859c8cb19c7SDarren Powell 	uint clk_index;
860e098bc96SEvan Quan 
86153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
862e098bc96SEvan Quan 		return -EPERM;
863d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
864d2ae842dSAlex Deucher 		return -EPERM;
865e098bc96SEvan Quan 
866e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
867e098bc96SEvan Quan 	if (ret < 0) {
868e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
869e098bc96SEvan Quan 		return ret;
870e098bc96SEvan Quan 	}
871e098bc96SEvan Quan 
872c8cb19c7SDarren Powell 	for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
873c8cb19c7SDarren Powell 		ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
874c8cb19c7SDarren Powell 		if (ret)
875c8cb19c7SDarren Powell 			break;
876c8cb19c7SDarren Powell 	}
877c8cb19c7SDarren Powell 	if (ret == -ENOENT) {
878e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
879e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
880e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
8818f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
882e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
8838f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
884e098bc96SEvan Quan 	}
885c8cb19c7SDarren Powell 
886c8cb19c7SDarren Powell 	if (size == 0)
887c8cb19c7SDarren Powell 		size = sysfs_emit(buf, "\n");
888c8cb19c7SDarren Powell 
889e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
890e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
891e098bc96SEvan Quan 
892e098bc96SEvan Quan 	return size;
893e098bc96SEvan Quan }
894e098bc96SEvan Quan 
895e098bc96SEvan Quan /**
896e098bc96SEvan Quan  * DOC: pp_features
897e098bc96SEvan Quan  *
898e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
899e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
900e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
901e098bc96SEvan Quan  *
902e098bc96SEvan Quan  * Reading back the file will show you the followings:
903e098bc96SEvan Quan  * - Current ppfeature masks
904e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
905e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
906e098bc96SEvan Quan  *
907e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
908e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
909e098bc96SEvan Quan  * new ppfeature masks.
910e098bc96SEvan Quan  */
911e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
912e098bc96SEvan Quan 				      struct device_attribute *attr,
913e098bc96SEvan Quan 				      const char *buf,
914e098bc96SEvan Quan 				      size_t count)
915e098bc96SEvan Quan {
916e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9171348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
918e098bc96SEvan Quan 	uint64_t featuremask;
919e098bc96SEvan Quan 	int ret;
920e098bc96SEvan Quan 
92153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
922e098bc96SEvan Quan 		return -EPERM;
923d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
924d2ae842dSAlex Deucher 		return -EPERM;
925e098bc96SEvan Quan 
926e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
927e098bc96SEvan Quan 	if (ret)
928e098bc96SEvan Quan 		return -EINVAL;
929e098bc96SEvan Quan 
930e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
931e098bc96SEvan Quan 	if (ret < 0) {
932e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
933e098bc96SEvan Quan 		return ret;
934e098bc96SEvan Quan 	}
935e098bc96SEvan Quan 
936e098bc96SEvan Quan 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
93779c65f3fSEvan Quan 
938e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
939e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
94079c65f3fSEvan Quan 
94179c65f3fSEvan Quan 	if (ret)
942e098bc96SEvan Quan 		return -EINVAL;
943e098bc96SEvan Quan 
944e098bc96SEvan Quan 	return count;
945e098bc96SEvan Quan }
946e098bc96SEvan Quan 
947e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
948e098bc96SEvan Quan 				      struct device_attribute *attr,
949e098bc96SEvan Quan 				      char *buf)
950e098bc96SEvan Quan {
951e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9521348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
953e098bc96SEvan Quan 	ssize_t size;
954e098bc96SEvan Quan 	int ret;
955e098bc96SEvan Quan 
95653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
957e098bc96SEvan Quan 		return -EPERM;
958d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
959d2ae842dSAlex Deucher 		return -EPERM;
960e098bc96SEvan Quan 
961e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
962e098bc96SEvan Quan 	if (ret < 0) {
963e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
964e098bc96SEvan Quan 		return ret;
965e098bc96SEvan Quan 	}
966e098bc96SEvan Quan 
967e098bc96SEvan Quan 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
96879c65f3fSEvan Quan 	if (size <= 0)
96909b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
970e098bc96SEvan Quan 
971e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
972e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
973e098bc96SEvan Quan 
974e098bc96SEvan Quan 	return size;
975e098bc96SEvan Quan }
976e098bc96SEvan Quan 
977e098bc96SEvan Quan /**
978e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
979e098bc96SEvan Quan  *
980e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
981e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
982e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
983e098bc96SEvan Quan  * this.
984e098bc96SEvan Quan  *
985e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
986e098bc96SEvan Quan  * Vega10 and later ASICs.
987e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
988e098bc96SEvan Quan  *
989e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
990615585d0SLijo Lazar  * the power state and the clock information for those levels. If deep sleep is
991615585d0SLijo Lazar  * applied to a clock, the level will be denoted by a special level 'S:'
992*bb619539SHunter Chasens  * E.g., ::
993*bb619539SHunter Chasens  *
994615585d0SLijo Lazar  *  S: 19Mhz *
995615585d0SLijo Lazar  *  0: 615Mhz
996615585d0SLijo Lazar  *  1: 800Mhz
997615585d0SLijo Lazar  *  2: 888Mhz
998615585d0SLijo Lazar  *  3: 1000Mhz
999615585d0SLijo Lazar  *
1000e098bc96SEvan Quan  *
1001e098bc96SEvan Quan  * To manually adjust these states, first select manual using
1002e098bc96SEvan Quan  * power_dpm_force_performance_level.
1003e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
1004e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1005e098bc96SEvan Quan  * E.g.,
1006e098bc96SEvan Quan  *
1007e098bc96SEvan Quan  * .. code-block:: bash
1008e098bc96SEvan Quan  *
1009e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
1010e098bc96SEvan Quan  *
1011e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
1012e098bc96SEvan Quan  *
1013e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1014e098bc96SEvan Quan  */
1015e098bc96SEvan Quan 
10162ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
10172ea092e5SDarren Powell 		enum pp_clock_type type,
1018e098bc96SEvan Quan 		char *buf)
1019e098bc96SEvan Quan {
1020e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10211348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1022c8cb19c7SDarren Powell 	int size = 0;
1023c8cb19c7SDarren Powell 	int ret = 0;
1024e098bc96SEvan Quan 
102553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1026e098bc96SEvan Quan 		return -EPERM;
1027d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1028d2ae842dSAlex Deucher 		return -EPERM;
1029e098bc96SEvan Quan 
1030e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1031e098bc96SEvan Quan 	if (ret < 0) {
1032e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1033e098bc96SEvan Quan 		return ret;
1034e098bc96SEvan Quan 	}
1035e098bc96SEvan Quan 
1036c8cb19c7SDarren Powell 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1037c8cb19c7SDarren Powell 	if (ret == -ENOENT)
10382ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1039c8cb19c7SDarren Powell 
1040c8cb19c7SDarren Powell 	if (size == 0)
104109b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1042e098bc96SEvan Quan 
1043e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1044e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1045e098bc96SEvan Quan 
1046e098bc96SEvan Quan 	return size;
1047e098bc96SEvan Quan }
1048e098bc96SEvan Quan 
1049e098bc96SEvan Quan /*
1050e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1051e098bc96SEvan Quan  * per line (+1 for \n).
1052e098bc96SEvan Quan  */
1053e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1054e098bc96SEvan Quan 
1055e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1056e098bc96SEvan Quan {
1057e098bc96SEvan Quan 	int ret;
1058c915ef89SDan Carpenter 	unsigned long level;
1059e098bc96SEvan Quan 	char *sub_str = NULL;
1060e098bc96SEvan Quan 	char *tmp;
1061e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1062e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1063e098bc96SEvan Quan 	size_t bytes;
1064e098bc96SEvan Quan 
1065e098bc96SEvan Quan 	*mask = 0;
1066e098bc96SEvan Quan 
1067e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1068e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1069e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1070e098bc96SEvan Quan 	tmp = buf_cpy;
1071ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1072e098bc96SEvan Quan 		if (strlen(sub_str)) {
1073c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1074c915ef89SDan Carpenter 			if (ret || level > 31)
1075e098bc96SEvan Quan 				return -EINVAL;
1076e098bc96SEvan Quan 			*mask |= 1 << level;
1077e098bc96SEvan Quan 		} else
1078e098bc96SEvan Quan 			break;
1079e098bc96SEvan Quan 	}
1080e098bc96SEvan Quan 
1081e098bc96SEvan Quan 	return 0;
1082e098bc96SEvan Quan }
1083e098bc96SEvan Quan 
10842ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
10852ea092e5SDarren Powell 		enum pp_clock_type type,
1086e098bc96SEvan Quan 		const char *buf,
1087e098bc96SEvan Quan 		size_t count)
1088e098bc96SEvan Quan {
1089e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10901348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1091e098bc96SEvan Quan 	int ret;
1092e098bc96SEvan Quan 	uint32_t mask = 0;
1093e098bc96SEvan Quan 
109453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1095e098bc96SEvan Quan 		return -EPERM;
1096d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1097d2ae842dSAlex Deucher 		return -EPERM;
1098e098bc96SEvan Quan 
1099e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1100e098bc96SEvan Quan 	if (ret)
1101e098bc96SEvan Quan 		return ret;
1102e098bc96SEvan Quan 
1103e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1104e098bc96SEvan Quan 	if (ret < 0) {
1105e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1106e098bc96SEvan Quan 		return ret;
1107e098bc96SEvan Quan 	}
1108e098bc96SEvan Quan 
11092ea092e5SDarren Powell 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1110e098bc96SEvan Quan 
1111e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1112e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1113e098bc96SEvan Quan 
1114e098bc96SEvan Quan 	if (ret)
1115e098bc96SEvan Quan 		return -EINVAL;
1116e098bc96SEvan Quan 
1117e098bc96SEvan Quan 	return count;
1118e098bc96SEvan Quan }
1119e098bc96SEvan Quan 
11202ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
11212ea092e5SDarren Powell 		struct device_attribute *attr,
11222ea092e5SDarren Powell 		char *buf)
11232ea092e5SDarren Powell {
11242ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
11252ea092e5SDarren Powell }
11262ea092e5SDarren Powell 
11272ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
11282ea092e5SDarren Powell 		struct device_attribute *attr,
11292ea092e5SDarren Powell 		const char *buf,
11302ea092e5SDarren Powell 		size_t count)
11312ea092e5SDarren Powell {
11322ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
11332ea092e5SDarren Powell }
11342ea092e5SDarren Powell 
1135e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1136e098bc96SEvan Quan 		struct device_attribute *attr,
1137e098bc96SEvan Quan 		char *buf)
1138e098bc96SEvan Quan {
11392ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1140e098bc96SEvan Quan }
1141e098bc96SEvan Quan 
1142e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1143e098bc96SEvan Quan 		struct device_attribute *attr,
1144e098bc96SEvan Quan 		const char *buf,
1145e098bc96SEvan Quan 		size_t count)
1146e098bc96SEvan Quan {
11472ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1148e098bc96SEvan Quan }
1149e098bc96SEvan Quan 
1150e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1151e098bc96SEvan Quan 		struct device_attribute *attr,
1152e098bc96SEvan Quan 		char *buf)
1153e098bc96SEvan Quan {
11542ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1155e098bc96SEvan Quan }
1156e098bc96SEvan Quan 
1157e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1158e098bc96SEvan Quan 		struct device_attribute *attr,
1159e098bc96SEvan Quan 		const char *buf,
1160e098bc96SEvan Quan 		size_t count)
1161e098bc96SEvan Quan {
11622ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1163e098bc96SEvan Quan }
1164e098bc96SEvan Quan 
1165e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1166e098bc96SEvan Quan 		struct device_attribute *attr,
1167e098bc96SEvan Quan 		char *buf)
1168e098bc96SEvan Quan {
11692ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1170e098bc96SEvan Quan }
1171e098bc96SEvan Quan 
1172e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1173e098bc96SEvan Quan 		struct device_attribute *attr,
1174e098bc96SEvan Quan 		const char *buf,
1175e098bc96SEvan Quan 		size_t count)
1176e098bc96SEvan Quan {
11772ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1178e098bc96SEvan Quan }
1179e098bc96SEvan Quan 
11809577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
11819577b0ecSXiaojian Du 		struct device_attribute *attr,
11829577b0ecSXiaojian Du 		char *buf)
11839577b0ecSXiaojian Du {
11842ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
11859577b0ecSXiaojian Du }
11869577b0ecSXiaojian Du 
11879577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
11889577b0ecSXiaojian Du 		struct device_attribute *attr,
11899577b0ecSXiaojian Du 		const char *buf,
11909577b0ecSXiaojian Du 		size_t count)
11919577b0ecSXiaojian Du {
11922ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
11939577b0ecSXiaojian Du }
11949577b0ecSXiaojian Du 
1195d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1196d7001e72STong Liu01 		struct device_attribute *attr,
1197d7001e72STong Liu01 		char *buf)
1198d7001e72STong Liu01 {
1199d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1200d7001e72STong Liu01 }
1201d7001e72STong Liu01 
1202d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1203d7001e72STong Liu01 		struct device_attribute *attr,
1204d7001e72STong Liu01 		const char *buf,
1205d7001e72STong Liu01 		size_t count)
1206d7001e72STong Liu01 {
1207d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1208d7001e72STong Liu01 }
1209d7001e72STong Liu01 
12109577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
12119577b0ecSXiaojian Du 		struct device_attribute *attr,
12129577b0ecSXiaojian Du 		char *buf)
12139577b0ecSXiaojian Du {
12142ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
12159577b0ecSXiaojian Du }
12169577b0ecSXiaojian Du 
12179577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
12189577b0ecSXiaojian Du 		struct device_attribute *attr,
12199577b0ecSXiaojian Du 		const char *buf,
12209577b0ecSXiaojian Du 		size_t count)
12219577b0ecSXiaojian Du {
12222ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
12239577b0ecSXiaojian Du }
12249577b0ecSXiaojian Du 
1225d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1226d7001e72STong Liu01 		struct device_attribute *attr,
1227d7001e72STong Liu01 		char *buf)
1228d7001e72STong Liu01 {
1229d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1230d7001e72STong Liu01 }
1231d7001e72STong Liu01 
1232d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1233d7001e72STong Liu01 		struct device_attribute *attr,
1234d7001e72STong Liu01 		const char *buf,
1235d7001e72STong Liu01 		size_t count)
1236d7001e72STong Liu01 {
1237d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1238d7001e72STong Liu01 }
1239d7001e72STong Liu01 
1240e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1241e098bc96SEvan Quan 		struct device_attribute *attr,
1242e098bc96SEvan Quan 		char *buf)
1243e098bc96SEvan Quan {
12442ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1245e098bc96SEvan Quan }
1246e098bc96SEvan Quan 
1247e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1248e098bc96SEvan Quan 		struct device_attribute *attr,
1249e098bc96SEvan Quan 		const char *buf,
1250e098bc96SEvan Quan 		size_t count)
1251e098bc96SEvan Quan {
12522ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1253e098bc96SEvan Quan }
1254e098bc96SEvan Quan 
1255e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1256e098bc96SEvan Quan 		struct device_attribute *attr,
1257e098bc96SEvan Quan 		char *buf)
1258e098bc96SEvan Quan {
12592ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1260e098bc96SEvan Quan }
1261e098bc96SEvan Quan 
1262e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1263e098bc96SEvan Quan 		struct device_attribute *attr,
1264e098bc96SEvan Quan 		const char *buf,
1265e098bc96SEvan Quan 		size_t count)
1266e098bc96SEvan Quan {
12672ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1268e098bc96SEvan Quan }
1269e098bc96SEvan Quan 
1270e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1271e098bc96SEvan Quan 		struct device_attribute *attr,
1272e098bc96SEvan Quan 		char *buf)
1273e098bc96SEvan Quan {
1274e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12751348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1276e098bc96SEvan Quan 	uint32_t value = 0;
1277e098bc96SEvan Quan 	int ret;
1278e098bc96SEvan Quan 
127953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1280e098bc96SEvan Quan 		return -EPERM;
1281d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1282d2ae842dSAlex Deucher 		return -EPERM;
1283e098bc96SEvan Quan 
1284e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1285e098bc96SEvan Quan 	if (ret < 0) {
1286e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1287e098bc96SEvan Quan 		return ret;
1288e098bc96SEvan Quan 	}
1289e098bc96SEvan Quan 
1290e098bc96SEvan Quan 	value = amdgpu_dpm_get_sclk_od(adev);
1291e098bc96SEvan Quan 
1292e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1293e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1294e098bc96SEvan Quan 
1295a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1296e098bc96SEvan Quan }
1297e098bc96SEvan Quan 
1298e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1299e098bc96SEvan Quan 		struct device_attribute *attr,
1300e098bc96SEvan Quan 		const char *buf,
1301e098bc96SEvan Quan 		size_t count)
1302e098bc96SEvan Quan {
1303e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13041348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1305e098bc96SEvan Quan 	int ret;
1306e098bc96SEvan Quan 	long int value;
1307e098bc96SEvan Quan 
130853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1309e098bc96SEvan Quan 		return -EPERM;
1310d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1311d2ae842dSAlex Deucher 		return -EPERM;
1312e098bc96SEvan Quan 
1313e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1314e098bc96SEvan Quan 
1315e098bc96SEvan Quan 	if (ret)
1316e098bc96SEvan Quan 		return -EINVAL;
1317e098bc96SEvan Quan 
1318e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1319e098bc96SEvan Quan 	if (ret < 0) {
1320e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1321e098bc96SEvan Quan 		return ret;
1322e098bc96SEvan Quan 	}
1323e098bc96SEvan Quan 
1324e098bc96SEvan Quan 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1325e098bc96SEvan Quan 
1326e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1327e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1328e098bc96SEvan Quan 
1329e098bc96SEvan Quan 	return count;
1330e098bc96SEvan Quan }
1331e098bc96SEvan Quan 
1332e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1333e098bc96SEvan Quan 		struct device_attribute *attr,
1334e098bc96SEvan Quan 		char *buf)
1335e098bc96SEvan Quan {
1336e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13371348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1338e098bc96SEvan Quan 	uint32_t value = 0;
1339e098bc96SEvan Quan 	int ret;
1340e098bc96SEvan Quan 
134153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1342e098bc96SEvan Quan 		return -EPERM;
1343d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1344d2ae842dSAlex Deucher 		return -EPERM;
1345e098bc96SEvan Quan 
1346e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1347e098bc96SEvan Quan 	if (ret < 0) {
1348e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1349e098bc96SEvan Quan 		return ret;
1350e098bc96SEvan Quan 	}
1351e098bc96SEvan Quan 
1352e098bc96SEvan Quan 	value = amdgpu_dpm_get_mclk_od(adev);
1353e098bc96SEvan Quan 
1354e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1355e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1356e098bc96SEvan Quan 
1357a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1358e098bc96SEvan Quan }
1359e098bc96SEvan Quan 
1360e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1361e098bc96SEvan Quan 		struct device_attribute *attr,
1362e098bc96SEvan Quan 		const char *buf,
1363e098bc96SEvan Quan 		size_t count)
1364e098bc96SEvan Quan {
1365e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1367e098bc96SEvan Quan 	int ret;
1368e098bc96SEvan Quan 	long int value;
1369e098bc96SEvan Quan 
137053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1371e098bc96SEvan Quan 		return -EPERM;
1372d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1373d2ae842dSAlex Deucher 		return -EPERM;
1374e098bc96SEvan Quan 
1375e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1376e098bc96SEvan Quan 
1377e098bc96SEvan Quan 	if (ret)
1378e098bc96SEvan Quan 		return -EINVAL;
1379e098bc96SEvan Quan 
1380e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1381e098bc96SEvan Quan 	if (ret < 0) {
1382e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1383e098bc96SEvan Quan 		return ret;
1384e098bc96SEvan Quan 	}
1385e098bc96SEvan Quan 
1386e098bc96SEvan Quan 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1387e098bc96SEvan Quan 
1388e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1389e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1390e098bc96SEvan Quan 
1391e098bc96SEvan Quan 	return count;
1392e098bc96SEvan Quan }
1393e098bc96SEvan Quan 
1394e098bc96SEvan Quan /**
1395e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1396e098bc96SEvan Quan  *
1397e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1398e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1399e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1400e098bc96SEvan Quan  *
1401e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1402e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1403e098bc96SEvan Quan  *
1404e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1405e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1406e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1407e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1408e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1409e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1410e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1411e098bc96SEvan Quan  *
1412e098bc96SEvan Quan  */
1413e098bc96SEvan Quan 
1414e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1415e098bc96SEvan Quan 		struct device_attribute *attr,
1416e098bc96SEvan Quan 		char *buf)
1417e098bc96SEvan Quan {
1418e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14191348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1420e098bc96SEvan Quan 	ssize_t size;
1421e098bc96SEvan Quan 	int ret;
1422e098bc96SEvan Quan 
142353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1424e098bc96SEvan Quan 		return -EPERM;
1425d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1426d2ae842dSAlex Deucher 		return -EPERM;
1427e098bc96SEvan Quan 
1428e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1429e098bc96SEvan Quan 	if (ret < 0) {
1430e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1431e098bc96SEvan Quan 		return ret;
1432e098bc96SEvan Quan 	}
1433e098bc96SEvan Quan 
1434e098bc96SEvan Quan 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
143579c65f3fSEvan Quan 	if (size <= 0)
143609b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1437e098bc96SEvan Quan 
1438e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1439e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1440e098bc96SEvan Quan 
1441e098bc96SEvan Quan 	return size;
1442e098bc96SEvan Quan }
1443e098bc96SEvan Quan 
1444e098bc96SEvan Quan 
1445e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1446e098bc96SEvan Quan 		struct device_attribute *attr,
1447e098bc96SEvan Quan 		const char *buf,
1448e098bc96SEvan Quan 		size_t count)
1449e098bc96SEvan Quan {
1450e098bc96SEvan Quan 	int ret;
1451e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14521348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1453e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1454e098bc96SEvan Quan 	long parameter[64];
1455e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1456e098bc96SEvan Quan 	char *tmp_str;
1457e098bc96SEvan Quan 	uint32_t i = 0;
1458e098bc96SEvan Quan 	char tmp[2];
1459e098bc96SEvan Quan 	long int profile_mode = 0;
1460e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1461e098bc96SEvan Quan 
146253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1463e098bc96SEvan Quan 		return -EPERM;
1464d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1465d2ae842dSAlex Deucher 		return -EPERM;
1466e098bc96SEvan Quan 
1467e098bc96SEvan Quan 	tmp[0] = *(buf);
1468e098bc96SEvan Quan 	tmp[1] = '\0';
1469e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1470e098bc96SEvan Quan 	if (ret)
1471e098bc96SEvan Quan 		return -EINVAL;
1472e098bc96SEvan Quan 
1473e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1474e098bc96SEvan Quan 		if (count < 2 || count > 127)
1475e098bc96SEvan Quan 			return -EINVAL;
1476e098bc96SEvan Quan 		while (isspace(*++buf))
1477e098bc96SEvan Quan 			i++;
1478e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1479e098bc96SEvan Quan 		tmp_str = buf_cpy;
1480ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1481c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1482c2efbc3fSEvan Quan 				continue;
1483e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1484e098bc96SEvan Quan 			if (ret)
1485e098bc96SEvan Quan 				return -EINVAL;
1486e098bc96SEvan Quan 			parameter_size++;
1487e098bc96SEvan Quan 			while (isspace(*tmp_str))
1488e098bc96SEvan Quan 				tmp_str++;
1489e098bc96SEvan Quan 		}
1490e098bc96SEvan Quan 	}
1491e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1492e098bc96SEvan Quan 
1493e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1494e098bc96SEvan Quan 	if (ret < 0) {
1495e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1496e098bc96SEvan Quan 		return ret;
1497e098bc96SEvan Quan 	}
1498e098bc96SEvan Quan 
1499e098bc96SEvan Quan 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1500e098bc96SEvan Quan 
1501e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1502e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1503e098bc96SEvan Quan 
1504e098bc96SEvan Quan 	if (!ret)
1505e098bc96SEvan Quan 		return count;
1506e098bc96SEvan Quan 
1507e098bc96SEvan Quan 	return -EINVAL;
1508e098bc96SEvan Quan }
1509e098bc96SEvan Quan 
1510a5600853SAlex Deucher static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1511d78c227fSMario Limonciello 					   enum amd_pp_sensors sensor,
1512d78c227fSMario Limonciello 					   void *query)
1513d78c227fSMario Limonciello {
1514d78c227fSMario Limonciello 	int r, size = sizeof(uint32_t);
1515d78c227fSMario Limonciello 
1516d78c227fSMario Limonciello 	if (amdgpu_in_reset(adev))
1517d78c227fSMario Limonciello 		return -EPERM;
1518d78c227fSMario Limonciello 	if (adev->in_suspend && !adev->in_runpm)
1519d78c227fSMario Limonciello 		return -EPERM;
1520d78c227fSMario Limonciello 
1521d78c227fSMario Limonciello 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1522d78c227fSMario Limonciello 	if (r < 0) {
1523d78c227fSMario Limonciello 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1524d78c227fSMario Limonciello 		return r;
1525d78c227fSMario Limonciello 	}
1526d78c227fSMario Limonciello 
1527d78c227fSMario Limonciello 	/* get the sensor value */
1528d78c227fSMario Limonciello 	r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1529d78c227fSMario Limonciello 
1530d78c227fSMario Limonciello 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1531d78c227fSMario Limonciello 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1532d78c227fSMario Limonciello 
1533d78c227fSMario Limonciello 	return r;
1534d78c227fSMario Limonciello }
1535d78c227fSMario Limonciello 
1536e098bc96SEvan Quan /**
1537e098bc96SEvan Quan  * DOC: gpu_busy_percent
1538e098bc96SEvan Quan  *
1539e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1540e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1541e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1542e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1543e098bc96SEvan Quan  */
1544e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1545e098bc96SEvan Quan 					   struct device_attribute *attr,
1546e098bc96SEvan Quan 					   char *buf)
1547e098bc96SEvan Quan {
1548e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15491348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1550d78c227fSMario Limonciello 	unsigned int value;
1551d78c227fSMario Limonciello 	int r;
1552e098bc96SEvan Quan 
1553d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1554e098bc96SEvan Quan 	if (r)
1555e098bc96SEvan Quan 		return r;
1556e098bc96SEvan Quan 
1557a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1558e098bc96SEvan Quan }
1559e098bc96SEvan Quan 
1560e098bc96SEvan Quan /**
1561e098bc96SEvan Quan  * DOC: mem_busy_percent
1562e098bc96SEvan Quan  *
1563e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1564e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1565e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1566e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1567e098bc96SEvan Quan  */
1568e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1569e098bc96SEvan Quan 					   struct device_attribute *attr,
1570e098bc96SEvan Quan 					   char *buf)
1571e098bc96SEvan Quan {
1572e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15731348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1574d78c227fSMario Limonciello 	unsigned int value;
1575d78c227fSMario Limonciello 	int r;
1576e098bc96SEvan Quan 
1577d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1578e098bc96SEvan Quan 	if (r)
1579e098bc96SEvan Quan 		return r;
1580e098bc96SEvan Quan 
1581a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1582e098bc96SEvan Quan }
1583e098bc96SEvan Quan 
1584e098bc96SEvan Quan /**
1585e098bc96SEvan Quan  * DOC: pcie_bw
1586e098bc96SEvan Quan  *
1587e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1588e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1589e098bc96SEvan Quan  * The file pcie_bw is used for this.
1590e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1591e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1592e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1593e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1594e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1595e098bc96SEvan Quan  */
1596e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1597e098bc96SEvan Quan 		struct device_attribute *attr,
1598e098bc96SEvan Quan 		char *buf)
1599e098bc96SEvan Quan {
1600e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16011348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1602e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1603e098bc96SEvan Quan 	int ret;
1604e098bc96SEvan Quan 
160553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1606e098bc96SEvan Quan 		return -EPERM;
1607d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1608d2ae842dSAlex Deucher 		return -EPERM;
1609e098bc96SEvan Quan 
1610e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1611e098bc96SEvan Quan 		return -ENODATA;
1612e098bc96SEvan Quan 
1613e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1614e098bc96SEvan Quan 		return -ENODATA;
1615e098bc96SEvan Quan 
1616e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1617e098bc96SEvan Quan 	if (ret < 0) {
1618e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1619e098bc96SEvan Quan 		return ret;
1620e098bc96SEvan Quan 	}
1621e098bc96SEvan Quan 
1622e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1623e098bc96SEvan Quan 
1624e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1625e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1626e098bc96SEvan Quan 
1627a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1628e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1629e098bc96SEvan Quan }
1630e098bc96SEvan Quan 
1631e098bc96SEvan Quan /**
1632e098bc96SEvan Quan  * DOC: unique_id
1633e098bc96SEvan Quan  *
1634e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1635e098bc96SEvan Quan  * The file unique_id is used for this.
1636e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1637e098bc96SEvan Quan  *
1638e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1639e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1640e098bc96SEvan Quan  */
1641e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1642e098bc96SEvan Quan 		struct device_attribute *attr,
1643e098bc96SEvan Quan 		char *buf)
1644e098bc96SEvan Quan {
1645e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16461348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1647e098bc96SEvan Quan 
164853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1649e098bc96SEvan Quan 		return -EPERM;
1650d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1651d2ae842dSAlex Deucher 		return -EPERM;
1652e098bc96SEvan Quan 
1653e098bc96SEvan Quan 	if (adev->unique_id)
1654a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1655e098bc96SEvan Quan 
1656e098bc96SEvan Quan 	return 0;
1657e098bc96SEvan Quan }
1658e098bc96SEvan Quan 
1659e098bc96SEvan Quan /**
1660e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1661e098bc96SEvan Quan  *
1662e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1663e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1664e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1665e098bc96SEvan Quan  * the log frequency.
1666e098bc96SEvan Quan  *
1667e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1668e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1669e098bc96SEvan Quan  *
1670e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1671e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1672e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1673e098bc96SEvan Quan  */
1674e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1675e098bc96SEvan Quan 						     struct device_attribute *attr,
1676e098bc96SEvan Quan 						     char *buf)
1677e098bc96SEvan Quan {
1678e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16791348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1680e098bc96SEvan Quan 
1681a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
16824a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1683e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1684e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1685e098bc96SEvan Quan }
1686e098bc96SEvan Quan 
1687e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1688e098bc96SEvan Quan 						     struct device_attribute *attr,
1689e098bc96SEvan Quan 						     const char *buf,
1690e098bc96SEvan Quan 						     size_t count)
1691e098bc96SEvan Quan {
1692e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16931348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1694e098bc96SEvan Quan 	long throttling_logging_interval;
1695e098bc96SEvan Quan 	unsigned long flags;
1696e098bc96SEvan Quan 	int ret = 0;
1697e098bc96SEvan Quan 
1698e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1699e098bc96SEvan Quan 	if (ret)
1700e098bc96SEvan Quan 		return ret;
1701e098bc96SEvan Quan 
1702e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1703e098bc96SEvan Quan 		return -EINVAL;
1704e098bc96SEvan Quan 
1705e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1706e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1707e098bc96SEvan Quan 		/*
1708e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1709e098bc96SEvan Quan 		 * This can effectively restart the timer.
1710e098bc96SEvan Quan 		 */
1711e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1712e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1713e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1714e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1715e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1716e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1717e098bc96SEvan Quan 
1718e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1719e098bc96SEvan Quan 	} else {
1720e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1721e098bc96SEvan Quan 	}
1722e098bc96SEvan Quan 
1723e098bc96SEvan Quan 	return count;
1724e098bc96SEvan Quan }
1725e098bc96SEvan Quan 
1726e098bc96SEvan Quan /**
1727c3ed0e72SKun Liu  * DOC: apu_thermal_cap
1728c3ed0e72SKun Liu  *
1729c3ed0e72SKun Liu  * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1730c3ed0e72SKun Liu  * limit temperature in millidegrees Celsius
1731c3ed0e72SKun Liu  *
1732c3ed0e72SKun Liu  * Reading back the file shows you core limit value
1733c3ed0e72SKun Liu  *
1734c3ed0e72SKun Liu  * Writing an integer to the file, sets a new thermal limit. The value
1735c3ed0e72SKun Liu  * should be between 0 and 100. If the value is less than 0 or greater
1736c3ed0e72SKun Liu  * than 100, then the write request will be ignored.
1737c3ed0e72SKun Liu  */
1738c3ed0e72SKun Liu static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1739c3ed0e72SKun Liu 					 struct device_attribute *attr,
1740c3ed0e72SKun Liu 					 char *buf)
1741c3ed0e72SKun Liu {
1742c3ed0e72SKun Liu 	int ret, size;
1743c3ed0e72SKun Liu 	u32 limit;
1744c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1745c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1746c3ed0e72SKun Liu 
1747c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1748c3ed0e72SKun Liu 	if (ret < 0) {
1749c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1750c3ed0e72SKun Liu 		return ret;
1751c3ed0e72SKun Liu 	}
1752c3ed0e72SKun Liu 
1753c3ed0e72SKun Liu 	ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1754c3ed0e72SKun Liu 	if (!ret)
1755c3ed0e72SKun Liu 		size = sysfs_emit(buf, "%u\n", limit);
1756c3ed0e72SKun Liu 	else
1757c3ed0e72SKun Liu 		size = sysfs_emit(buf, "failed to get thermal limit\n");
1758c3ed0e72SKun Liu 
1759c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1760c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1761c3ed0e72SKun Liu 
1762c3ed0e72SKun Liu 	return size;
1763c3ed0e72SKun Liu }
1764c3ed0e72SKun Liu 
1765c3ed0e72SKun Liu static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1766c3ed0e72SKun Liu 					 struct device_attribute *attr,
1767c3ed0e72SKun Liu 					 const char *buf,
1768c3ed0e72SKun Liu 					 size_t count)
1769c3ed0e72SKun Liu {
1770c3ed0e72SKun Liu 	int ret;
1771c3ed0e72SKun Liu 	u32 value;
1772c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1773c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1774c3ed0e72SKun Liu 
1775c3ed0e72SKun Liu 	ret = kstrtou32(buf, 10, &value);
1776c3ed0e72SKun Liu 	if (ret)
1777c3ed0e72SKun Liu 		return ret;
1778c3ed0e72SKun Liu 
17794d2c09d6SMuhammad Usama Anjum 	if (value > 100) {
1780c3ed0e72SKun Liu 		dev_err(dev, "Invalid argument !\n");
1781c3ed0e72SKun Liu 		return -EINVAL;
1782c3ed0e72SKun Liu 	}
1783c3ed0e72SKun Liu 
1784c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1785c3ed0e72SKun Liu 	if (ret < 0) {
1786c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1787c3ed0e72SKun Liu 		return ret;
1788c3ed0e72SKun Liu 	}
1789c3ed0e72SKun Liu 
1790c3ed0e72SKun Liu 	ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1791c3ed0e72SKun Liu 	if (ret) {
1792c3ed0e72SKun Liu 		dev_err(dev, "failed to update thermal limit\n");
1793c3ed0e72SKun Liu 		return ret;
1794c3ed0e72SKun Liu 	}
1795c3ed0e72SKun Liu 
1796c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1797c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1798c3ed0e72SKun Liu 
1799c3ed0e72SKun Liu 	return count;
1800c3ed0e72SKun Liu }
1801c3ed0e72SKun Liu 
1802c3ed0e72SKun Liu /**
1803e098bc96SEvan Quan  * DOC: gpu_metrics
1804e098bc96SEvan Quan  *
1805e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1806e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1807e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1808e098bc96SEvan Quan  *
1809e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1810e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1811e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1812e098bc96SEvan Quan  * at the same time.
1813e098bc96SEvan Quan  */
1814e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1815e098bc96SEvan Quan 				      struct device_attribute *attr,
1816e098bc96SEvan Quan 				      char *buf)
1817e098bc96SEvan Quan {
1818e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
18191348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1820e098bc96SEvan Quan 	void *gpu_metrics;
1821e098bc96SEvan Quan 	ssize_t size = 0;
1822e098bc96SEvan Quan 	int ret;
1823e098bc96SEvan Quan 
182453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1825e098bc96SEvan Quan 		return -EPERM;
1826d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1827d2ae842dSAlex Deucher 		return -EPERM;
1828e098bc96SEvan Quan 
1829e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1830e098bc96SEvan Quan 	if (ret < 0) {
1831e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1832e098bc96SEvan Quan 		return ret;
1833e098bc96SEvan Quan 	}
1834e098bc96SEvan Quan 
1835e098bc96SEvan Quan 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1836e098bc96SEvan Quan 	if (size <= 0)
1837e098bc96SEvan Quan 		goto out;
1838e098bc96SEvan Quan 
1839e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1840e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1841e098bc96SEvan Quan 
1842e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1843e098bc96SEvan Quan 
1844e098bc96SEvan Quan out:
1845e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1846e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1847e098bc96SEvan Quan 
1848e098bc96SEvan Quan 	return size;
1849e098bc96SEvan Quan }
1850e098bc96SEvan Quan 
1851494c1432SSathishkumar S static int amdgpu_show_powershift_percent(struct device *dev,
1852d78c227fSMario Limonciello 					char *buf, enum amd_pp_sensors sensor)
1853494c1432SSathishkumar S {
1854494c1432SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1855494c1432SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1856494c1432SSathishkumar S 	uint32_t ss_power;
1857494c1432SSathishkumar S 	int r = 0, i;
1858494c1432SSathishkumar S 
1859d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1860494c1432SSathishkumar S 	if (r == -EOPNOTSUPP) {
1861494c1432SSathishkumar S 		/* sensor not available on dGPU, try to read from APU */
1862494c1432SSathishkumar S 		adev = NULL;
1863494c1432SSathishkumar S 		mutex_lock(&mgpu_info.mutex);
1864494c1432SSathishkumar S 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1865494c1432SSathishkumar S 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1866494c1432SSathishkumar S 				adev = mgpu_info.gpu_ins[i].adev;
1867494c1432SSathishkumar S 				break;
1868494c1432SSathishkumar S 			}
1869494c1432SSathishkumar S 		}
1870494c1432SSathishkumar S 		mutex_unlock(&mgpu_info.mutex);
1871494c1432SSathishkumar S 		if (adev)
1872d78c227fSMario Limonciello 			r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1873494c1432SSathishkumar S 	}
1874494c1432SSathishkumar S 
1875d78c227fSMario Limonciello 	if (r)
1876494c1432SSathishkumar S 		return r;
1877d78c227fSMario Limonciello 
1878d78c227fSMario Limonciello 	return sysfs_emit(buf, "%u%%\n", ss_power);
1879494c1432SSathishkumar S }
1880d78c227fSMario Limonciello 
1881a7673a1cSSathishkumar S /**
1882a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1883a7673a1cSSathishkumar S  *
1884a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1885494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1886494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power
1887494c1432SSathishkumar S  * is shifted to APU, the percentage of boost is with respect to APU power
1888494c1432SSathishkumar S  * limit on the platform.
1889a7673a1cSSathishkumar S  */
1890a7673a1cSSathishkumar S 
1891a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1892a7673a1cSSathishkumar S 					       char *buf)
1893a7673a1cSSathishkumar S {
1894d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1895a7673a1cSSathishkumar S }
1896a7673a1cSSathishkumar S 
1897a7673a1cSSathishkumar S /**
1898a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1899a7673a1cSSathishkumar S  *
1900494c1432SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting dGPU power
1901494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1902494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power is
1903494c1432SSathishkumar S  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1904494c1432SSathishkumar S  * limit on the platform.
1905a7673a1cSSathishkumar S  */
1906a7673a1cSSathishkumar S 
1907a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1908a7673a1cSSathishkumar S 						char *buf)
1909a7673a1cSSathishkumar S {
1910d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1911a7673a1cSSathishkumar S }
1912a7673a1cSSathishkumar S 
191330d95a37SSathishkumar S /**
191430d95a37SSathishkumar S  * DOC: smartshift_bias
191530d95a37SSathishkumar S  *
191630d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
191730d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
191830d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
191930d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
192030d95a37SSathishkumar S  */
192130d95a37SSathishkumar S 
192230d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
192330d95a37SSathishkumar S 					  struct device_attribute *attr,
192430d95a37SSathishkumar S 					  char *buf)
192530d95a37SSathishkumar S {
192630d95a37SSathishkumar S 	int r = 0;
192730d95a37SSathishkumar S 
192830d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
192930d95a37SSathishkumar S 
193030d95a37SSathishkumar S 	return r;
193130d95a37SSathishkumar S }
193230d95a37SSathishkumar S 
193330d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
193430d95a37SSathishkumar S 					  struct device_attribute *attr,
193530d95a37SSathishkumar S 					  const char *buf, size_t count)
193630d95a37SSathishkumar S {
193730d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
193830d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
193930d95a37SSathishkumar S 	int r = 0;
194030d95a37SSathishkumar S 	int bias = 0;
194130d95a37SSathishkumar S 
194230d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
194330d95a37SSathishkumar S 		return -EPERM;
194430d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
194530d95a37SSathishkumar S 		return -EPERM;
194630d95a37SSathishkumar S 
194730d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
194830d95a37SSathishkumar S 	if (r < 0) {
194930d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
195030d95a37SSathishkumar S 		return r;
195130d95a37SSathishkumar S 	}
195230d95a37SSathishkumar S 
195330d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
195430d95a37SSathishkumar S 	if (r)
195530d95a37SSathishkumar S 		goto out;
195630d95a37SSathishkumar S 
195730d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
195830d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
195930d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
196030d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
196130d95a37SSathishkumar S 
196230d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
196330d95a37SSathishkumar S 	r = count;
196430d95a37SSathishkumar S 
1965bd4b9bb7SJulia Lawall 	/* TODO: update bias level with SMU message */
196630d95a37SSathishkumar S 
196730d95a37SSathishkumar S out:
196830d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
196930d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
197030d95a37SSathishkumar S 	return r;
197130d95a37SSathishkumar S }
197230d95a37SSathishkumar S 
1973a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1974a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
1975a7673a1cSSathishkumar S {
1976494c1432SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1977a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1978a7673a1cSSathishkumar S 
1979a7673a1cSSathishkumar S 	return 0;
1980a7673a1cSSathishkumar S }
1981a7673a1cSSathishkumar S 
198230d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
198330d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
198430d95a37SSathishkumar S {
1985d78c227fSMario Limonciello 	uint32_t ss_power;
198630d95a37SSathishkumar S 
198730d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
198830d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1989d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1990d78c227fSMario Limonciello 		 (void *)&ss_power))
199130d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
1992d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1993d78c227fSMario Limonciello 		 (void *)&ss_power))
199430d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
199530d95a37SSathishkumar S 
199630d95a37SSathishkumar S 	return 0;
199730d95a37SSathishkumar S }
199830d95a37SSathishkumar S 
199921e43386SLe Ma /* Following items will be read out to indicate current plpd policy:
200021e43386SLe Ma  *  - -1: none
200121e43386SLe Ma  *  - 0: disallow
200221e43386SLe Ma  *  - 1: default
200321e43386SLe Ma  *  - 2: optimized
200421e43386SLe Ma  */
200521e43386SLe Ma static ssize_t amdgpu_get_xgmi_plpd_policy(struct device *dev,
200621e43386SLe Ma 					   struct device_attribute *attr,
200721e43386SLe Ma 					   char *buf)
200821e43386SLe Ma {
200921e43386SLe Ma 	struct drm_device *ddev = dev_get_drvdata(dev);
201021e43386SLe Ma 	struct amdgpu_device *adev = drm_to_adev(ddev);
201121e43386SLe Ma 	char *mode_desc = "none";
201221e43386SLe Ma 	int mode;
201321e43386SLe Ma 
201421e43386SLe Ma 	if (amdgpu_in_reset(adev))
201521e43386SLe Ma 		return -EPERM;
201621e43386SLe Ma 	if (adev->in_suspend && !adev->in_runpm)
201721e43386SLe Ma 		return -EPERM;
201821e43386SLe Ma 
201921e43386SLe Ma 	mode = amdgpu_dpm_get_xgmi_plpd_mode(adev, &mode_desc);
202021e43386SLe Ma 
202121e43386SLe Ma 	return sysfs_emit(buf, "%d: %s\n", mode, mode_desc);
202221e43386SLe Ma }
202321e43386SLe Ma 
202421e43386SLe Ma /* Following argument value is expected from user to change plpd policy
202521e43386SLe Ma  *  - arg 0: disallow plpd
202621e43386SLe Ma  *  - arg 1: default policy
202721e43386SLe Ma  *  - arg 2: optimized policy
202821e43386SLe Ma  */
202921e43386SLe Ma static ssize_t amdgpu_set_xgmi_plpd_policy(struct device *dev,
203021e43386SLe Ma 					   struct device_attribute *attr,
203121e43386SLe Ma 					   const char *buf, size_t count)
203221e43386SLe Ma {
203321e43386SLe Ma 	struct drm_device *ddev = dev_get_drvdata(dev);
203421e43386SLe Ma 	struct amdgpu_device *adev = drm_to_adev(ddev);
203521e43386SLe Ma 	int mode, ret;
203621e43386SLe Ma 
203721e43386SLe Ma 	if (amdgpu_in_reset(adev))
203821e43386SLe Ma 		return -EPERM;
203921e43386SLe Ma 	if (adev->in_suspend && !adev->in_runpm)
204021e43386SLe Ma 		return -EPERM;
204121e43386SLe Ma 
204221e43386SLe Ma 	ret = kstrtos32(buf, 0, &mode);
204321e43386SLe Ma 	if (ret)
204421e43386SLe Ma 		return -EINVAL;
204521e43386SLe Ma 
204621e43386SLe Ma 	ret = pm_runtime_get_sync(ddev->dev);
204721e43386SLe Ma 	if (ret < 0) {
204821e43386SLe Ma 		pm_runtime_put_autosuspend(ddev->dev);
204921e43386SLe Ma 		return ret;
205021e43386SLe Ma 	}
205121e43386SLe Ma 
205221e43386SLe Ma 	ret = amdgpu_dpm_set_xgmi_plpd_mode(adev, mode);
205321e43386SLe Ma 
205421e43386SLe Ma 	pm_runtime_mark_last_busy(ddev->dev);
205521e43386SLe Ma 	pm_runtime_put_autosuspend(ddev->dev);
205621e43386SLe Ma 
205721e43386SLe Ma 	if (ret)
205821e43386SLe Ma 		return ret;
205921e43386SLe Ma 
206021e43386SLe Ma 	return count;
206121e43386SLe Ma }
206221e43386SLe Ma 
2063e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2064e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20654215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20667884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20677884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20687884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20697884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2070e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2071e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2072e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2073e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20749577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2075d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
20769577b0ecSXiaojian Du 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2077d7001e72STong Liu01 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2078f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2079f3527a64SMarina Nikolic 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2080e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2081e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2082ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2083e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
2084ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2085ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2086e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2087ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2088ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2089ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2090c3ed0e72SKun Liu 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2091ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2092a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2093a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
2094a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2095a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
209630d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
209730d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
209821e43386SLe Ma 	AMDGPU_DEVICE_ATTR_RW(xgmi_plpd_policy,				ATTR_FLAG_BASIC),
2099e098bc96SEvan Quan };
2100e098bc96SEvan Quan 
2101e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2102e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2103e098bc96SEvan Quan {
2104e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
21054e8303cfSLijo Lazar 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
21064e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2107e098bc96SEvan Quan 	const char *attr_name = dev_attr->attr.name;
2108e098bc96SEvan Quan 
2109e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2110e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2111e098bc96SEvan Quan 		return 0;
2112e098bc96SEvan Quan 	}
2113e098bc96SEvan Quan 
2114e098bc96SEvan Quan #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2115e098bc96SEvan Quan 
2116e098bc96SEvan Quan 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
21178ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0))
2118e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2119e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
21208ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 0, 0) ||
21210127ab1bSYang Wang 		    !amdgpu_device_has_display_hardware(adev))
2122e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2123e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
21248ecad8d6SLijo Lazar 		if (mp1_ver < IP_VERSION(10, 0, 0))
2125e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2126e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2127e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
212879c65f3fSEvan Quan 		if (amdgpu_dpm_is_overdrive_supported(adev))
2129e098bc96SEvan Quan 			*states = ATTR_STATE_SUPPORTED;
2130e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
21318ecad8d6SLijo Lazar 		if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1))
2132e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2133e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2134e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
2135e098bc96SEvan Quan 		if (adev->flags & AMD_IS_APU)
2136e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2137e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
213860044748SKent Russell 		switch (gc_ver) {
213960044748SKent Russell 		case IP_VERSION(9, 0, 1):
214060044748SKent Russell 		case IP_VERSION(9, 4, 0):
214160044748SKent Russell 		case IP_VERSION(9, 4, 1):
214260044748SKent Russell 		case IP_VERSION(9, 4, 2):
2143baf65745SLijo Lazar 		case IP_VERSION(9, 4, 3):
2144ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
2145276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
214635e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
214735e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
2148d82758adSKenneth Feng 		case IP_VERSION(11, 0, 3):
214960044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
215060044748SKent Russell 			break;
215160044748SKent Russell 		default:
2152e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
215360044748SKent Russell 		}
2154e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2155fc8e84a2SLijo Lazar 		if ((adev->flags & AMD_IS_APU &&
2156fc8e84a2SLijo Lazar 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2157fc8e84a2SLijo Lazar 		    gc_ver < IP_VERSION(9, 0, 0))
2158e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2159e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
21608ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2161e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
21629577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
21638ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2164a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
216564440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
21663929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
21672f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
2168707b570fSAsad Kamal 		      gc_ver == IP_VERSION(11, 0, 3) ||
2169707b570fSAsad Kamal 		      gc_ver == IP_VERSION(9, 4, 3)))
21709577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
21710b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
21720b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2173feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2174feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2175feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
21760b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
21779577b0ecSXiaojian Du 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
21788ecad8d6SLijo Lazar 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2179a68bec2cSMarko Zekovic 		      gc_ver == IP_VERSION(10, 3, 0) ||
218064440743SEvan Quan 		      gc_ver == IP_VERSION(10, 1, 2) ||
21813929f338SKenneth Feng 		      gc_ver == IP_VERSION(11, 0, 0) ||
21822f68c414SYiqing Yao 		      gc_ver == IP_VERSION(11, 0, 2) ||
2183707b570fSAsad Kamal 		      gc_ver == IP_VERSION(11, 0, 3) ||
2184707b570fSAsad Kamal 		      gc_ver == IP_VERSION(9, 4, 3)))
21859577b0ecSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
21860b872f65STong Liu01 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
21870b872f65STong Liu01 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2188feae1bd8STong Liu01 			   gc_ver == IP_VERSION(10, 3, 0) ||
2189feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 2) ||
2190feae1bd8STong Liu01 			   gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
21910b872f65STong Liu01 			*states = ATTR_STATE_UNSUPPORTED;
2192a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
219379c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2194a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
2195b57c4f1cSVictor Zhao 		else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2196b57c4f1cSVictor Zhao 			  gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
21971b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
219821e43386SLe Ma 	} else if (DEVICE_ATTR_IS(xgmi_plpd_policy)) {
219921e43386SLe Ma 		if (amdgpu_dpm_get_xgmi_plpd_mode(adev, NULL) == XGMI_PLPD_NONE)
220021e43386SLe Ma 			*states = ATTR_STATE_UNSUPPORTED;
22018cfd6a05SLijo Lazar 	} else if (DEVICE_ATTR_IS(pp_dpm_mclk_od)) {
22028cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
22038cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
22048cfd6a05SLijo Lazar 	} else if (DEVICE_ATTR_IS(pp_dpm_sclk_od)) {
22058cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
22068cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
22078cfd6a05SLijo Lazar 	} else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
22088cfd6a05SLijo Lazar 		u32 limit;
22098cfd6a05SLijo Lazar 
22108cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
22118cfd6a05SLijo Lazar 		    -EOPNOTSUPP)
22128cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
22138abf799eSLijo Lazar 	} else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
22148abf799eSLijo Lazar 		if (gc_ver == IP_VERSION(9, 4, 2) ||
22158abf799eSLijo Lazar 		    gc_ver == IP_VERSION(9, 4, 3))
22168abf799eSLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
2217e098bc96SEvan Quan 	}
2218e098bc96SEvan Quan 
22198ecad8d6SLijo Lazar 	switch (gc_ver) {
22208ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 1):
22218ecad8d6SLijo Lazar 	case IP_VERSION(9, 4, 2):
22221d0e622fSKevin Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2223e098bc96SEvan Quan 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2224e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2225e098bc96SEvan Quan 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2226e098bc96SEvan Quan 			dev_attr->attr.mode &= ~S_IWUGO;
2227e098bc96SEvan Quan 			dev_attr->store = NULL;
2228e098bc96SEvan Quan 		}
22291d0e622fSKevin Wang 		break;
22301b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
22311b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
22321b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
22331b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
22341b852572SDanijel Slivka 			dev_attr->store = NULL;
22351b852572SDanijel Slivka 		}
22361b852572SDanijel Slivka 		break;
22371d0e622fSKevin Wang 	default:
22381d0e622fSKevin Wang 		break;
2239e098bc96SEvan Quan 	}
2240e098bc96SEvan Quan 
2241ede14a1bSDarren Powell 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2242ede14a1bSDarren Powell 		/* SMU MP1 does not support dcefclk level setting */
22438ecad8d6SLijo Lazar 		if (gc_ver >= IP_VERSION(10, 0, 0)) {
2244ede14a1bSDarren Powell 			dev_attr->attr.mode &= ~S_IWUGO;
2245ede14a1bSDarren Powell 			dev_attr->store = NULL;
2246ede14a1bSDarren Powell 		}
2247ede14a1bSDarren Powell 	}
2248ede14a1bSDarren Powell 
2249e610941cSYiqing Yao 	/* setting should not be allowed from VF if not in one VF mode */
2250e610941cSYiqing Yao 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
225111c9cc95SMarina Nikolic 		dev_attr->attr.mode &= ~S_IWUGO;
225211c9cc95SMarina Nikolic 		dev_attr->store = NULL;
225311c9cc95SMarina Nikolic 	}
225411c9cc95SMarina Nikolic 
2255e098bc96SEvan Quan #undef DEVICE_ATTR_IS
2256e098bc96SEvan Quan 
2257e098bc96SEvan Quan 	return 0;
2258e098bc96SEvan Quan }
2259e098bc96SEvan Quan 
2260e098bc96SEvan Quan 
2261e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2262e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2263e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2264e098bc96SEvan Quan {
2265e098bc96SEvan Quan 	int ret = 0;
2266e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2267e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
226825e6373aSYang Wang 	struct device_attribute *dev_attr;
226925e6373aSYang Wang 	const char *name;
2270e098bc96SEvan Quan 
2271e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2272e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2273e098bc96SEvan Quan 
227425e6373aSYang Wang 	if (!attr)
227525e6373aSYang Wang 		return -EINVAL;
227625e6373aSYang Wang 
227725e6373aSYang Wang 	dev_attr = &attr->dev_attr;
227825e6373aSYang Wang 	name = dev_attr->attr.name;
2279e098bc96SEvan Quan 
22808a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2281e098bc96SEvan Quan 
2282e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2283e098bc96SEvan Quan 	if (ret) {
2284e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2285e098bc96SEvan Quan 			name, ret);
2286e098bc96SEvan Quan 		return ret;
2287e098bc96SEvan Quan 	}
2288e098bc96SEvan Quan 
2289e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2290e098bc96SEvan Quan 		return 0;
2291e098bc96SEvan Quan 
2292e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2293e098bc96SEvan Quan 	if (ret) {
2294e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2295e098bc96SEvan Quan 			name, ret);
2296e098bc96SEvan Quan 	}
2297e098bc96SEvan Quan 
2298e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2299e098bc96SEvan Quan 	if (!attr_entry)
2300e098bc96SEvan Quan 		return -ENOMEM;
2301e098bc96SEvan Quan 
2302e098bc96SEvan Quan 	attr_entry->attr = attr;
2303e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2304e098bc96SEvan Quan 
2305e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2306e098bc96SEvan Quan 
2307e098bc96SEvan Quan 	return ret;
2308e098bc96SEvan Quan }
2309e098bc96SEvan Quan 
2310e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2311e098bc96SEvan Quan {
2312e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2313e098bc96SEvan Quan 
2314e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2315e098bc96SEvan Quan }
2316e098bc96SEvan Quan 
2317e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2318e098bc96SEvan Quan 					     struct list_head *attr_list);
2319e098bc96SEvan Quan 
2320e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2321e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2322e098bc96SEvan Quan 					    uint32_t counts,
2323e098bc96SEvan Quan 					    uint32_t mask,
2324e098bc96SEvan Quan 					    struct list_head *attr_list)
2325e098bc96SEvan Quan {
2326e098bc96SEvan Quan 	int ret = 0;
2327e098bc96SEvan Quan 	uint32_t i = 0;
2328e098bc96SEvan Quan 
2329e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2330e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2331e098bc96SEvan Quan 		if (ret)
2332e098bc96SEvan Quan 			goto failed;
2333e098bc96SEvan Quan 	}
2334e098bc96SEvan Quan 
2335e098bc96SEvan Quan 	return 0;
2336e098bc96SEvan Quan 
2337e098bc96SEvan Quan failed:
2338e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2339e098bc96SEvan Quan 
2340e098bc96SEvan Quan 	return ret;
2341e098bc96SEvan Quan }
2342e098bc96SEvan Quan 
2343e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2344e098bc96SEvan Quan 					     struct list_head *attr_list)
2345e098bc96SEvan Quan {
2346e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2347e098bc96SEvan Quan 
2348e098bc96SEvan Quan 	if (list_empty(attr_list))
2349e098bc96SEvan Quan 		return ;
2350e098bc96SEvan Quan 
2351e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2352e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2353e098bc96SEvan Quan 		list_del(&entry->entry);
2354e098bc96SEvan Quan 		kfree(entry);
2355e098bc96SEvan Quan 	}
2356e098bc96SEvan Quan }
2357e098bc96SEvan Quan 
2358e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2359e098bc96SEvan Quan 				      struct device_attribute *attr,
2360e098bc96SEvan Quan 				      char *buf)
2361e098bc96SEvan Quan {
2362e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2363e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2364d78c227fSMario Limonciello 	int r, temp = 0;
2365e098bc96SEvan Quan 
2366e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2367e098bc96SEvan Quan 		return -EINVAL;
2368e098bc96SEvan Quan 
2369e098bc96SEvan Quan 	switch (channel) {
2370e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2371e098bc96SEvan Quan 		/* get current junction temperature */
2372d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2373d78c227fSMario Limonciello 					   (void *)&temp);
2374e098bc96SEvan Quan 		break;
2375e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2376e098bc96SEvan Quan 		/* get current edge temperature */
2377d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2378d78c227fSMario Limonciello 					   (void *)&temp);
2379e098bc96SEvan Quan 		break;
2380e098bc96SEvan Quan 	case PP_TEMP_MEM:
2381e098bc96SEvan Quan 		/* get current memory temperature */
2382d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2383d78c227fSMario Limonciello 					   (void *)&temp);
2384e098bc96SEvan Quan 		break;
2385e098bc96SEvan Quan 	default:
2386e098bc96SEvan Quan 		r = -EINVAL;
2387e098bc96SEvan Quan 		break;
2388e098bc96SEvan Quan 	}
2389e098bc96SEvan Quan 
2390e098bc96SEvan Quan 	if (r)
2391e098bc96SEvan Quan 		return r;
2392e098bc96SEvan Quan 
2393a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2394e098bc96SEvan Quan }
2395e098bc96SEvan Quan 
2396e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2397e098bc96SEvan Quan 					     struct device_attribute *attr,
2398e098bc96SEvan Quan 					     char *buf)
2399e098bc96SEvan Quan {
2400e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2401e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2402e098bc96SEvan Quan 	int temp;
2403e098bc96SEvan Quan 
2404e098bc96SEvan Quan 	if (hyst)
2405e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2406e098bc96SEvan Quan 	else
2407e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2408e098bc96SEvan Quan 
2409a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2410e098bc96SEvan Quan }
2411e098bc96SEvan Quan 
2412e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2413e098bc96SEvan Quan 					     struct device_attribute *attr,
2414e098bc96SEvan Quan 					     char *buf)
2415e098bc96SEvan Quan {
2416e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2417e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2418e098bc96SEvan Quan 	int temp;
2419e098bc96SEvan Quan 
2420e098bc96SEvan Quan 	if (hyst)
2421e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2422e098bc96SEvan Quan 	else
2423e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2424e098bc96SEvan Quan 
2425a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2426e098bc96SEvan Quan }
2427e098bc96SEvan Quan 
2428e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2429e098bc96SEvan Quan 					     struct device_attribute *attr,
2430e098bc96SEvan Quan 					     char *buf)
2431e098bc96SEvan Quan {
2432e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2433e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2434e098bc96SEvan Quan 	int temp;
2435e098bc96SEvan Quan 
2436e098bc96SEvan Quan 	if (hyst)
2437e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2438e098bc96SEvan Quan 	else
2439e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2440e098bc96SEvan Quan 
2441a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2442e098bc96SEvan Quan }
2443e098bc96SEvan Quan 
2444e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2445e098bc96SEvan Quan 					     struct device_attribute *attr,
2446e098bc96SEvan Quan 					     char *buf)
2447e098bc96SEvan Quan {
2448e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2449e098bc96SEvan Quan 
2450e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2451e098bc96SEvan Quan 		return -EINVAL;
2452e098bc96SEvan Quan 
2453a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2454e098bc96SEvan Quan }
2455e098bc96SEvan Quan 
2456e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2457e098bc96SEvan Quan 					     struct device_attribute *attr,
2458e098bc96SEvan Quan 					     char *buf)
2459e098bc96SEvan Quan {
2460e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2461e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2462e098bc96SEvan Quan 	int temp = 0;
2463e098bc96SEvan Quan 
2464e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2465e098bc96SEvan Quan 		return -EINVAL;
2466e098bc96SEvan Quan 
2467e098bc96SEvan Quan 	switch (channel) {
2468e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2469e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2470e098bc96SEvan Quan 		break;
2471e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2472e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2473e098bc96SEvan Quan 		break;
2474e098bc96SEvan Quan 	case PP_TEMP_MEM:
2475e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2476e098bc96SEvan Quan 		break;
2477e098bc96SEvan Quan 	}
2478e098bc96SEvan Quan 
2479a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2480e098bc96SEvan Quan }
2481e098bc96SEvan Quan 
2482e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2483e098bc96SEvan Quan 					    struct device_attribute *attr,
2484e098bc96SEvan Quan 					    char *buf)
2485e098bc96SEvan Quan {
2486e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2487e098bc96SEvan Quan 	u32 pwm_mode = 0;
2488e098bc96SEvan Quan 	int ret;
2489e098bc96SEvan Quan 
249053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2491e098bc96SEvan Quan 		return -EPERM;
2492d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2493d2ae842dSAlex Deucher 		return -EPERM;
2494e098bc96SEvan Quan 
24954a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2496e098bc96SEvan Quan 	if (ret < 0) {
24974a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2498e098bc96SEvan Quan 		return ret;
2499e098bc96SEvan Quan 	}
2500e098bc96SEvan Quan 
250179c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
250279c65f3fSEvan Quan 
25034a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25044a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
250579c65f3fSEvan Quan 
250679c65f3fSEvan Quan 	if (ret)
2507e098bc96SEvan Quan 		return -EINVAL;
2508e098bc96SEvan Quan 
2509fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2510e098bc96SEvan Quan }
2511e098bc96SEvan Quan 
2512e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2513e098bc96SEvan Quan 					    struct device_attribute *attr,
2514e098bc96SEvan Quan 					    const char *buf,
2515e098bc96SEvan Quan 					    size_t count)
2516e098bc96SEvan Quan {
2517e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2518e098bc96SEvan Quan 	int err, ret;
2519e098bc96SEvan Quan 	int value;
2520e098bc96SEvan Quan 
252153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2522e098bc96SEvan Quan 		return -EPERM;
2523d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2524d2ae842dSAlex Deucher 		return -EPERM;
2525e098bc96SEvan Quan 
2526e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2527e098bc96SEvan Quan 	if (err)
2528e098bc96SEvan Quan 		return err;
2529e098bc96SEvan Quan 
25304a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2531e098bc96SEvan Quan 	if (ret < 0) {
25324a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2533e098bc96SEvan Quan 		return ret;
2534e098bc96SEvan Quan 	}
2535e098bc96SEvan Quan 
253679c65f3fSEvan Quan 	ret = amdgpu_dpm_set_fan_control_mode(adev, value);
253779c65f3fSEvan Quan 
25384a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25394a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
254079c65f3fSEvan Quan 
254179c65f3fSEvan Quan 	if (ret)
2542e098bc96SEvan Quan 		return -EINVAL;
2543e098bc96SEvan Quan 
2544e098bc96SEvan Quan 	return count;
2545e098bc96SEvan Quan }
2546e098bc96SEvan Quan 
2547e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2548e098bc96SEvan Quan 					 struct device_attribute *attr,
2549e098bc96SEvan Quan 					 char *buf)
2550e098bc96SEvan Quan {
2551fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2552e098bc96SEvan Quan }
2553e098bc96SEvan Quan 
2554e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2555e098bc96SEvan Quan 					 struct device_attribute *attr,
2556e098bc96SEvan Quan 					 char *buf)
2557e098bc96SEvan Quan {
2558fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2559e098bc96SEvan Quan }
2560e098bc96SEvan Quan 
2561e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2562e098bc96SEvan Quan 				     struct device_attribute *attr,
2563e098bc96SEvan Quan 				     const char *buf, size_t count)
2564e098bc96SEvan Quan {
2565e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2566e098bc96SEvan Quan 	int err;
2567e098bc96SEvan Quan 	u32 value;
2568e098bc96SEvan Quan 	u32 pwm_mode;
2569e098bc96SEvan Quan 
257053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2571e098bc96SEvan Quan 		return -EPERM;
2572d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2573d2ae842dSAlex Deucher 		return -EPERM;
2574e098bc96SEvan Quan 
257579c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
257679c65f3fSEvan Quan 	if (err)
257779c65f3fSEvan Quan 		return err;
257879c65f3fSEvan Quan 
25794a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2580e098bc96SEvan Quan 	if (err < 0) {
25814a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2582e098bc96SEvan Quan 		return err;
2583e098bc96SEvan Quan 	}
2584e098bc96SEvan Quan 
258579c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
258679c65f3fSEvan Quan 	if (err)
258779c65f3fSEvan Quan 		goto out;
258879c65f3fSEvan Quan 
2589e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2590e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2591e098bc96SEvan Quan 		err = -EINVAL;
259279c65f3fSEvan Quan 		goto out;
259379c65f3fSEvan Quan 	}
2594e098bc96SEvan Quan 
259579c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
259679c65f3fSEvan Quan 
259779c65f3fSEvan Quan out:
25984a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
25994a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2600e098bc96SEvan Quan 
2601e098bc96SEvan Quan 	if (err)
2602e098bc96SEvan Quan 		return err;
2603e098bc96SEvan Quan 
2604e098bc96SEvan Quan 	return count;
2605e098bc96SEvan Quan }
2606e098bc96SEvan Quan 
2607e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2608e098bc96SEvan Quan 				     struct device_attribute *attr,
2609e098bc96SEvan Quan 				     char *buf)
2610e098bc96SEvan Quan {
2611e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2612e098bc96SEvan Quan 	int err;
2613e098bc96SEvan Quan 	u32 speed = 0;
2614e098bc96SEvan Quan 
261553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2616e098bc96SEvan Quan 		return -EPERM;
2617d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2618d2ae842dSAlex Deucher 		return -EPERM;
2619e098bc96SEvan Quan 
26204a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2621e098bc96SEvan Quan 	if (err < 0) {
26224a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2623e098bc96SEvan Quan 		return err;
2624e098bc96SEvan Quan 	}
2625e098bc96SEvan Quan 
26260d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2627e098bc96SEvan Quan 
26284a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26294a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2630e098bc96SEvan Quan 
2631e098bc96SEvan Quan 	if (err)
2632e098bc96SEvan Quan 		return err;
2633e098bc96SEvan Quan 
2634fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2635e098bc96SEvan Quan }
2636e098bc96SEvan Quan 
2637e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2638e098bc96SEvan Quan 					   struct device_attribute *attr,
2639e098bc96SEvan Quan 					   char *buf)
2640e098bc96SEvan Quan {
2641e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2642e098bc96SEvan Quan 	int err;
2643e098bc96SEvan Quan 	u32 speed = 0;
2644e098bc96SEvan Quan 
264553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2646e098bc96SEvan Quan 		return -EPERM;
2647d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2648d2ae842dSAlex Deucher 		return -EPERM;
2649e098bc96SEvan 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 
2656e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2657e098bc96SEvan Quan 
26584a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26594a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2660e098bc96SEvan Quan 
2661e098bc96SEvan Quan 	if (err)
2662e098bc96SEvan Quan 		return err;
2663e098bc96SEvan Quan 
2664fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2665e098bc96SEvan Quan }
2666e098bc96SEvan Quan 
2667e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2668e098bc96SEvan Quan 					 struct device_attribute *attr,
2669e098bc96SEvan Quan 					 char *buf)
2670e098bc96SEvan Quan {
2671e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2672e098bc96SEvan Quan 	u32 min_rpm = 0;
2673e098bc96SEvan Quan 	int r;
2674e098bc96SEvan Quan 
2675d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2676d78c227fSMario Limonciello 				   (void *)&min_rpm);
2677e098bc96SEvan Quan 
2678e098bc96SEvan Quan 	if (r)
2679e098bc96SEvan Quan 		return r;
2680e098bc96SEvan Quan 
2681a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2682e098bc96SEvan Quan }
2683e098bc96SEvan Quan 
2684e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2685e098bc96SEvan Quan 					 struct device_attribute *attr,
2686e098bc96SEvan Quan 					 char *buf)
2687e098bc96SEvan Quan {
2688e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2689e098bc96SEvan Quan 	u32 max_rpm = 0;
2690e098bc96SEvan Quan 	int r;
2691e098bc96SEvan Quan 
2692d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2693d78c227fSMario Limonciello 				   (void *)&max_rpm);
2694e098bc96SEvan Quan 
2695e098bc96SEvan Quan 	if (r)
2696e098bc96SEvan Quan 		return r;
2697e098bc96SEvan Quan 
2698a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2699e098bc96SEvan Quan }
2700e098bc96SEvan Quan 
2701e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2702e098bc96SEvan Quan 					   struct device_attribute *attr,
2703e098bc96SEvan Quan 					   char *buf)
2704e098bc96SEvan Quan {
2705e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2706e098bc96SEvan Quan 	int err;
2707e098bc96SEvan Quan 	u32 rpm = 0;
2708e098bc96SEvan Quan 
270953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2710e098bc96SEvan Quan 		return -EPERM;
2711d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2712d2ae842dSAlex Deucher 		return -EPERM;
2713e098bc96SEvan Quan 
27144a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2715e098bc96SEvan Quan 	if (err < 0) {
27164a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2717e098bc96SEvan Quan 		return err;
2718e098bc96SEvan Quan 	}
2719e098bc96SEvan Quan 
2720e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2721e098bc96SEvan Quan 
27224a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27234a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2724e098bc96SEvan Quan 
2725e098bc96SEvan Quan 	if (err)
2726e098bc96SEvan Quan 		return err;
2727e098bc96SEvan Quan 
2728fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2729e098bc96SEvan Quan }
2730e098bc96SEvan Quan 
2731e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2732e098bc96SEvan Quan 				     struct device_attribute *attr,
2733e098bc96SEvan Quan 				     const char *buf, size_t count)
2734e098bc96SEvan Quan {
2735e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2736e098bc96SEvan Quan 	int err;
2737e098bc96SEvan Quan 	u32 value;
2738e098bc96SEvan Quan 	u32 pwm_mode;
2739e098bc96SEvan Quan 
274053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2741e098bc96SEvan Quan 		return -EPERM;
2742d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2743d2ae842dSAlex Deucher 		return -EPERM;
2744e098bc96SEvan Quan 
274579c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
274679c65f3fSEvan Quan 	if (err)
274779c65f3fSEvan Quan 		return err;
274879c65f3fSEvan Quan 
27494a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2750e098bc96SEvan Quan 	if (err < 0) {
27514a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2752e098bc96SEvan Quan 		return err;
2753e098bc96SEvan Quan 	}
2754e098bc96SEvan Quan 
275579c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
275679c65f3fSEvan Quan 	if (err)
275779c65f3fSEvan Quan 		goto out;
2758e098bc96SEvan Quan 
2759e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
276079c65f3fSEvan Quan 		err = -ENODATA;
276179c65f3fSEvan Quan 		goto out;
2762e098bc96SEvan Quan 	}
2763e098bc96SEvan Quan 
2764e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2765e098bc96SEvan Quan 
276679c65f3fSEvan Quan out:
27674a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27684a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2769e098bc96SEvan Quan 
2770e098bc96SEvan Quan 	if (err)
2771e098bc96SEvan Quan 		return err;
2772e098bc96SEvan Quan 
2773e098bc96SEvan Quan 	return count;
2774e098bc96SEvan Quan }
2775e098bc96SEvan Quan 
2776e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2777e098bc96SEvan Quan 					    struct device_attribute *attr,
2778e098bc96SEvan Quan 					    char *buf)
2779e098bc96SEvan Quan {
2780e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2781e098bc96SEvan Quan 	u32 pwm_mode = 0;
2782e098bc96SEvan Quan 	int ret;
2783e098bc96SEvan Quan 
278453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2785e098bc96SEvan Quan 		return -EPERM;
2786d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2787d2ae842dSAlex Deucher 		return -EPERM;
2788e098bc96SEvan Quan 
27894a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2790e098bc96SEvan Quan 	if (ret < 0) {
27914a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2792e098bc96SEvan Quan 		return ret;
2793e098bc96SEvan Quan 	}
2794e098bc96SEvan Quan 
279579c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
279679c65f3fSEvan Quan 
27974a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27984a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
279979c65f3fSEvan Quan 
280079c65f3fSEvan Quan 	if (ret)
2801e098bc96SEvan Quan 		return -EINVAL;
2802e098bc96SEvan Quan 
2803fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2804e098bc96SEvan Quan }
2805e098bc96SEvan Quan 
2806e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2807e098bc96SEvan Quan 					    struct device_attribute *attr,
2808e098bc96SEvan Quan 					    const char *buf,
2809e098bc96SEvan Quan 					    size_t count)
2810e098bc96SEvan Quan {
2811e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2812e098bc96SEvan Quan 	int err;
2813e098bc96SEvan Quan 	int value;
2814e098bc96SEvan Quan 	u32 pwm_mode;
2815e098bc96SEvan Quan 
281653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2817e098bc96SEvan Quan 		return -EPERM;
2818d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2819d2ae842dSAlex Deucher 		return -EPERM;
2820e098bc96SEvan Quan 
2821e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2822e098bc96SEvan Quan 	if (err)
2823e098bc96SEvan Quan 		return err;
2824e098bc96SEvan Quan 
2825e098bc96SEvan Quan 	if (value == 0)
2826e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
2827e098bc96SEvan Quan 	else if (value == 1)
2828e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2829e098bc96SEvan Quan 	else
2830e098bc96SEvan Quan 		return -EINVAL;
2831e098bc96SEvan Quan 
28324a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2833e098bc96SEvan Quan 	if (err < 0) {
28344a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2835e098bc96SEvan Quan 		return err;
2836e098bc96SEvan Quan 	}
2837e098bc96SEvan Quan 
283879c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2839e098bc96SEvan Quan 
28404a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28414a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2842e098bc96SEvan Quan 
284379c65f3fSEvan Quan 	if (err)
284479c65f3fSEvan Quan 		return -EINVAL;
284579c65f3fSEvan Quan 
2846e098bc96SEvan Quan 	return count;
2847e098bc96SEvan Quan }
2848e098bc96SEvan Quan 
2849e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2850e098bc96SEvan Quan 					struct device_attribute *attr,
2851e098bc96SEvan Quan 					char *buf)
2852e098bc96SEvan Quan {
2853e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2854e098bc96SEvan Quan 	u32 vddgfx;
2855d78c227fSMario Limonciello 	int r;
2856e098bc96SEvan Quan 
2857e098bc96SEvan Quan 	/* get the voltage */
2858d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
2859d78c227fSMario Limonciello 				   (void *)&vddgfx);
2860e098bc96SEvan Quan 	if (r)
2861e098bc96SEvan Quan 		return r;
2862e098bc96SEvan Quan 
2863a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
2864e098bc96SEvan Quan }
2865e098bc96SEvan Quan 
2866e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2867e098bc96SEvan Quan 					      struct device_attribute *attr,
2868e098bc96SEvan Quan 					      char *buf)
2869e098bc96SEvan Quan {
2870a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
2871e098bc96SEvan Quan }
2872e098bc96SEvan Quan 
2873e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2874e098bc96SEvan Quan 				       struct device_attribute *attr,
2875e098bc96SEvan Quan 				       char *buf)
2876e098bc96SEvan Quan {
2877e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2878e098bc96SEvan Quan 	u32 vddnb;
2879d78c227fSMario Limonciello 	int r;
2880e098bc96SEvan Quan 
2881e098bc96SEvan Quan 	/* only APUs have vddnb */
2882e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
2883e098bc96SEvan Quan 		return -EINVAL;
2884e098bc96SEvan Quan 
2885e098bc96SEvan Quan 	/* get the voltage */
2886d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
2887d78c227fSMario Limonciello 				   (void *)&vddnb);
2888e098bc96SEvan Quan 	if (r)
2889e098bc96SEvan Quan 		return r;
2890e098bc96SEvan Quan 
2891a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
2892e098bc96SEvan Quan }
2893e098bc96SEvan Quan 
2894e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2895e098bc96SEvan Quan 					      struct device_attribute *attr,
2896e098bc96SEvan Quan 					      char *buf)
2897e098bc96SEvan Quan {
2898a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
2899e098bc96SEvan Quan }
2900e098bc96SEvan Quan 
2901a5600853SAlex Deucher static int amdgpu_hwmon_get_power(struct device *dev,
2902d78c227fSMario Limonciello 				  enum amd_pp_sensors sensor)
2903e098bc96SEvan Quan {
2904e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2905d78c227fSMario Limonciello 	unsigned int uw;
2906e098bc96SEvan Quan 	u32 query = 0;
2907d78c227fSMario Limonciello 	int r;
2908e098bc96SEvan Quan 
2909d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
2910e098bc96SEvan Quan 	if (r)
2911e098bc96SEvan Quan 		return r;
2912e098bc96SEvan Quan 
2913e098bc96SEvan Quan 	/* convert to microwatts */
2914e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2915e098bc96SEvan Quan 
2916d78c227fSMario Limonciello 	return uw;
2917d78c227fSMario Limonciello }
2918d78c227fSMario Limonciello 
2919d78c227fSMario Limonciello static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2920d78c227fSMario Limonciello 					   struct device_attribute *attr,
2921d78c227fSMario Limonciello 					   char *buf)
2922d78c227fSMario Limonciello {
2923d1090194SSrinivasan Shanmugam 	ssize_t val;
2924d78c227fSMario Limonciello 
29259366c2e8SMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
2926d78c227fSMario Limonciello 	if (val < 0)
2927d78c227fSMario Limonciello 		return val;
2928d78c227fSMario Limonciello 
2929d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
2930e098bc96SEvan Quan }
2931e098bc96SEvan Quan 
2932bb9f7b68SMario Limonciello static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
2933bb9f7b68SMario Limonciello 					     struct device_attribute *attr,
2934bb9f7b68SMario Limonciello 					     char *buf)
2935bb9f7b68SMario Limonciello {
2936d1090194SSrinivasan Shanmugam 	ssize_t val;
2937bb9f7b68SMario Limonciello 
293847f1724dSMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
2939bb9f7b68SMario Limonciello 	if (val < 0)
2940bb9f7b68SMario Limonciello 		return val;
2941bb9f7b68SMario Limonciello 
2942d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
2943bb9f7b68SMario Limonciello }
2944bb9f7b68SMario Limonciello 
294591161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2946e098bc96SEvan Quan 					struct device_attribute *attr,
294791161b06SDarren Powell 					char *buf,
294891161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
2949e098bc96SEvan Quan {
2950e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2951a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2952a40a020dSDarren Powell 	uint32_t limit;
2953e098bc96SEvan Quan 	ssize_t size;
2954e098bc96SEvan Quan 	int r;
2955e098bc96SEvan Quan 
295653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2957e098bc96SEvan Quan 		return -EPERM;
2958d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2959d2ae842dSAlex Deucher 		return -EPERM;
2960e098bc96SEvan Quan 
29614a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2962e098bc96SEvan Quan 	if (r < 0) {
29634a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2964e098bc96SEvan Quan 		return r;
2965e098bc96SEvan Quan 	}
2966e098bc96SEvan Quan 
296779c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
296804bec521SDarren Powell 				      pp_limit_level, power_type);
2969dc2a8240SDarren Powell 
2970dc2a8240SDarren Powell 	if (!r)
297109b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2972dc2a8240SDarren Powell 	else
297309b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
2974e098bc96SEvan Quan 
29754a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29764a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2977e098bc96SEvan Quan 
2978e098bc96SEvan Quan 	return size;
2979e098bc96SEvan Quan }
2980e098bc96SEvan Quan 
298119589468SMa Jun static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
298219589468SMa Jun 					 struct device_attribute *attr,
298319589468SMa Jun 					 char *buf)
298419589468SMa Jun {
298519589468SMa Jun 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
298619589468SMa Jun }
298791161b06SDarren Powell 
298891161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
298991161b06SDarren Powell 					 struct device_attribute *attr,
299091161b06SDarren Powell 					 char *buf)
299191161b06SDarren Powell {
299291161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
299391161b06SDarren Powell 
299491161b06SDarren Powell }
299591161b06SDarren Powell 
2996e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2997e098bc96SEvan Quan 					 struct device_attribute *attr,
2998e098bc96SEvan Quan 					 char *buf)
2999e098bc96SEvan Quan {
300091161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
3001e098bc96SEvan Quan 
3002e098bc96SEvan Quan }
3003e098bc96SEvan Quan 
30046e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
30056e58941cSEric Huang 					 struct device_attribute *attr,
30066e58941cSEric Huang 					 char *buf)
30076e58941cSEric Huang {
300891161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
30096e58941cSEric Huang 
30106e58941cSEric Huang }
30116e58941cSEric Huang 
3012ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3013ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
3014ae07970aSXiaomeng Hou 					 char *buf)
3015ae07970aSXiaomeng Hou {
30163b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
30174e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3018ae07970aSXiaomeng Hou 
30198ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
3020a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
30213b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
30223b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
30233b99e8e3SYang Wang 	else
30243b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
3025ae07970aSXiaomeng Hou }
3026e098bc96SEvan Quan 
3027e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3028e098bc96SEvan Quan 		struct device_attribute *attr,
3029e098bc96SEvan Quan 		const char *buf,
3030e098bc96SEvan Quan 		size_t count)
3031e098bc96SEvan Quan {
3032e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3033ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
3034e098bc96SEvan Quan 	int err;
3035e098bc96SEvan Quan 	u32 value;
3036e098bc96SEvan Quan 
303753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3038e098bc96SEvan Quan 		return -EPERM;
3039d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3040d2ae842dSAlex Deucher 		return -EPERM;
3041e098bc96SEvan Quan 
3042e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
3043e098bc96SEvan Quan 		return -EINVAL;
3044e098bc96SEvan Quan 
3045e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
3046e098bc96SEvan Quan 	if (err)
3047e098bc96SEvan Quan 		return err;
3048e098bc96SEvan Quan 
3049e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
3050ae07970aSXiaomeng Hou 	value |= limit_type << 24;
3051e098bc96SEvan Quan 
30524a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3053e098bc96SEvan Quan 	if (err < 0) {
30544a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3055e098bc96SEvan Quan 		return err;
3056e098bc96SEvan Quan 	}
3057e098bc96SEvan Quan 
305879c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
3059e098bc96SEvan Quan 
30604a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30614a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3062e098bc96SEvan Quan 
3063e098bc96SEvan Quan 	if (err)
3064e098bc96SEvan Quan 		return err;
3065e098bc96SEvan Quan 
3066e098bc96SEvan Quan 	return count;
3067e098bc96SEvan Quan }
3068e098bc96SEvan Quan 
3069e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3070e098bc96SEvan Quan 				      struct device_attribute *attr,
3071e098bc96SEvan Quan 				      char *buf)
3072e098bc96SEvan Quan {
3073e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3074e098bc96SEvan Quan 	uint32_t sclk;
3075d78c227fSMario Limonciello 	int r;
3076e098bc96SEvan Quan 
3077e098bc96SEvan Quan 	/* get the sclk */
3078d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3079d78c227fSMario Limonciello 				   (void *)&sclk);
3080e098bc96SEvan Quan 	if (r)
3081e098bc96SEvan Quan 		return r;
3082e098bc96SEvan Quan 
3083a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3084e098bc96SEvan Quan }
3085e098bc96SEvan Quan 
3086e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3087e098bc96SEvan Quan 					    struct device_attribute *attr,
3088e098bc96SEvan Quan 					    char *buf)
3089e098bc96SEvan Quan {
3090a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
3091e098bc96SEvan Quan }
3092e098bc96SEvan Quan 
3093e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3094e098bc96SEvan Quan 				      struct device_attribute *attr,
3095e098bc96SEvan Quan 				      char *buf)
3096e098bc96SEvan Quan {
3097e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3098e098bc96SEvan Quan 	uint32_t mclk;
3099d78c227fSMario Limonciello 	int r;
3100e098bc96SEvan Quan 
3101e098bc96SEvan Quan 	/* get the sclk */
3102d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3103d78c227fSMario Limonciello 				   (void *)&mclk);
3104e098bc96SEvan Quan 	if (r)
3105e098bc96SEvan Quan 		return r;
3106e098bc96SEvan Quan 
3107a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3108e098bc96SEvan Quan }
3109e098bc96SEvan Quan 
3110e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3111e098bc96SEvan Quan 					    struct device_attribute *attr,
3112e098bc96SEvan Quan 					    char *buf)
3113e098bc96SEvan Quan {
3114a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3115e098bc96SEvan Quan }
3116e098bc96SEvan Quan 
3117e098bc96SEvan Quan /**
3118e098bc96SEvan Quan  * DOC: hwmon
3119e098bc96SEvan Quan  *
3120e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3121e098bc96SEvan Quan  *
3122e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3123e098bc96SEvan Quan  *
3124e098bc96SEvan Quan  * - GPU voltage
3125e098bc96SEvan Quan  *
3126e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3127e098bc96SEvan Quan  *
3128e098bc96SEvan Quan  * - GPU power
3129e098bc96SEvan Quan  *
3130e098bc96SEvan Quan  * - GPU fan
3131e098bc96SEvan Quan  *
3132e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3133e098bc96SEvan Quan  *
3134e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3135e098bc96SEvan Quan  *
3136e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3137e098bc96SEvan Quan  *
3138e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3139e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3140e098bc96SEvan Quan  *
3141e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3142e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3143e098bc96SEvan Quan  *
3144e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3145e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3146e098bc96SEvan Quan  *
3147e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3148e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3149e098bc96SEvan Quan  *
3150e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3151e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3152e098bc96SEvan Quan  *
3153e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3154e098bc96SEvan Quan  *
3155e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3156e098bc96SEvan Quan  *
3157e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3158e098bc96SEvan Quan  *
3159e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3160e098bc96SEvan Quan  *
316129f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3162e098bc96SEvan Quan  *
3163bb9f7b68SMario Limonciello  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3164bb9f7b68SMario Limonciello  *
3165e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3166e098bc96SEvan Quan  *
3167e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3168e098bc96SEvan Quan  *
3169e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3170e098bc96SEvan Quan  *
3171e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3172e098bc96SEvan Quan  *
3173e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3174e098bc96SEvan Quan  *
3175e098bc96SEvan 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)
3176e098bc96SEvan Quan  *
3177e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3178e098bc96SEvan Quan  *
3179e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3180e098bc96SEvan Quan  *
3181e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3182e098bc96SEvan Quan  *
3183e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3184e098bc96SEvan Quan  *
3185e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3186e098bc96SEvan Quan  *
3187e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3188e098bc96SEvan Quan  *
3189e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3190e098bc96SEvan Quan  *
319196401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
319296401f7cSEvan Quan  *       That will get the former one overridden.
319396401f7cSEvan Quan  *
3194e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3195e098bc96SEvan Quan  *
3196e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3197e098bc96SEvan Quan  *
3198e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3199e098bc96SEvan Quan  *
3200e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3201e098bc96SEvan Quan  *
3202e098bc96SEvan Quan  */
3203e098bc96SEvan Quan 
3204e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3205e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3206e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3207e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3208e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3209e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3210e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3211e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3212e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3213e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3214e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3215e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3216e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3217e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3218e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3219e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3220e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3221e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3222e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3223e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3224e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3225e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3226e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3227e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3228e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3229e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3230e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3231e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3232e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3233bb9f7b68SMario Limonciello static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3234e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3235e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3236e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
32376e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3238ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3239ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3240ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3241ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3242ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
32436e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3244ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3245e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3246e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3247e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3248e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3249e098bc96SEvan Quan 
3250e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3251e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3252e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3253e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3254e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3255e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3256e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3257e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3258e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3259e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3260e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3261e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3262e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3263e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3264e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3265e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3266e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3267e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3268e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3269e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3270e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3271e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3272e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3273e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3274e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3275e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3276e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3277e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3278e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3279e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3280bb9f7b68SMario Limonciello 	&sensor_dev_attr_power1_input.dev_attr.attr,
3281e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3282e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3283e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
32846e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3285ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3286ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3287ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3288ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3289ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
32906e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3291ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3292e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3293e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3294e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3295e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3296e098bc96SEvan Quan 	NULL
3297e098bc96SEvan Quan };
3298e098bc96SEvan Quan 
3299e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3300e098bc96SEvan Quan 					struct attribute *attr, int index)
3301e098bc96SEvan Quan {
3302e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3303e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3304e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
33054e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
330615419813SMario Limonciello 	uint32_t tmp;
3307e098bc96SEvan Quan 
33084f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
33094f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
33104f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
33114f0f1b58SDanijel Slivka 
3312e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3313e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3314e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3315e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3316e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3317e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3318e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3319e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3320e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3321e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3322e098bc96SEvan Quan 		return 0;
3323e098bc96SEvan Quan 
3324e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3325e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3326e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3327e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3328e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3329e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3330e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3331e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3332e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3333e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3334e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3335e098bc96SEvan Quan 		return 0;
3336e098bc96SEvan Quan 
3337e098bc96SEvan Quan 	/* Skip crit temp on APU */
33388572fa2aSAsad Kamal 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
33398572fa2aSAsad Kamal 	    (gc_ver == IP_VERSION(9, 4, 3))) &&
3340e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3341e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3342e098bc96SEvan Quan 		return 0;
3343e098bc96SEvan Quan 
3344e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3345e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3346e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3347e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3348e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3349e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3350e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3351e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3352e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3353e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3354e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3355e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3356e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3357e098bc96SEvan Quan 		return 0;
3358e098bc96SEvan Quan 
3359e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3360685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3361e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3362685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3363e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3364e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3365e098bc96SEvan Quan 
3366685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3367e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3368685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3369e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3370e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3371e098bc96SEvan Quan 
33728572fa2aSAsad Kamal 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3373ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
33748572fa2aSAsad Kamal 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
33758572fa2aSAsad Kamal 	      (gc_ver != IP_VERSION(9, 4, 3)))) &&
3376367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3377e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
33786e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
33796e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3380e098bc96SEvan Quan 		return 0;
3381e098bc96SEvan Quan 
338289317d42SGuilherme G. Piccoli 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3383367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
33848ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3385367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3386367deb67SAlex Deucher 		return 0;
3387367deb67SAlex Deucher 
338815419813SMario Limonciello 	/* not all products support both average and instantaneous */
338915419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
339015419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
339115419813SMario Limonciello 		return 0;
339215419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
339315419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
339415419813SMario Limonciello 		return 0;
339515419813SMario Limonciello 
3396e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3397685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3398685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3399685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3400685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3401e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3402e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3403e098bc96SEvan Quan 		return 0;
3404e098bc96SEvan Quan 
3405685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3406685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3407e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3408e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3409e098bc96SEvan Quan 		return 0;
3410e098bc96SEvan Quan 
3411e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
34128572fa2aSAsad Kamal 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
34138572fa2aSAsad Kamal 	     (gc_ver == IP_VERSION(9, 4, 3))) &&
3414e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3415e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3416e098bc96SEvan Quan 		return 0;
3417e098bc96SEvan Quan 
34188572fa2aSAsad Kamal 	/* only APUs other than gc 9,4,3 have vddnb */
34198572fa2aSAsad Kamal 	if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) &&
3420e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3421e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3422e098bc96SEvan Quan 		return 0;
3423e098bc96SEvan Quan 
34248572fa2aSAsad Kamal 	/* no mclk on APUs other than gc 9,4,3*/
34258572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3426e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3427e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3428e098bc96SEvan Quan 		return 0;
3429e098bc96SEvan Quan 
34308ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
34318572fa2aSAsad Kamal 	    (gc_ver != IP_VERSION(9, 4, 3)) &&
34328572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3433bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
343407864911SAsad Kamal 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3435bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
343607864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
343707864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
34388572fa2aSAsad Kamal 		return 0;
34398572fa2aSAsad Kamal 
3440bfb4fd20SAsad Kamal 	/* hotspot temperature for gc 9,4,3*/
34419cff0879SLijo Lazar 	if (gc_ver == IP_VERSION(9, 4, 3)) {
34429cff0879SLijo Lazar 		if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
34439cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
34449cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
34458572fa2aSAsad Kamal 			return 0;
34468572fa2aSAsad Kamal 
34479cff0879SLijo Lazar 		if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
34489cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
34499cff0879SLijo Lazar 			return attr->mode;
34509cff0879SLijo Lazar 	}
34519cff0879SLijo Lazar 
34528572fa2aSAsad Kamal 	/* only SOC15 dGPUs support hotspot and mem temperatures */
34539cff0879SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
345407864911SAsad Kamal 	    (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3455e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3456e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3457e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3458bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3459e098bc96SEvan Quan 		return 0;
3460e098bc96SEvan Quan 
3461ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
34628ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3463ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3464ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3465ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3466ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
34676e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3468de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3469ae07970aSXiaomeng Hou 		return 0;
3470ae07970aSXiaomeng Hou 
3471e098bc96SEvan Quan 	return effective_mode;
3472e098bc96SEvan Quan }
3473e098bc96SEvan Quan 
3474e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3475e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3476e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3477e098bc96SEvan Quan };
3478e098bc96SEvan Quan 
3479e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3480e098bc96SEvan Quan 	&hwmon_attrgroup,
3481e098bc96SEvan Quan 	NULL
3482e098bc96SEvan Quan };
3483e098bc96SEvan Quan 
3484d7bf1b55SEvan Quan static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3485d7bf1b55SEvan Quan 				       enum pp_clock_type od_type,
3486d7bf1b55SEvan Quan 				       char *buf)
3487d7bf1b55SEvan Quan {
3488d7bf1b55SEvan Quan 	int size = 0;
3489d7bf1b55SEvan Quan 	int ret;
3490d7bf1b55SEvan Quan 
3491d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3492d7bf1b55SEvan Quan 		return -EPERM;
3493d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3494d7bf1b55SEvan Quan 		return -EPERM;
3495d7bf1b55SEvan Quan 
3496d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3497d7bf1b55SEvan Quan 	if (ret < 0) {
3498d7bf1b55SEvan Quan 		pm_runtime_put_autosuspend(adev->dev);
3499d7bf1b55SEvan Quan 		return ret;
3500d7bf1b55SEvan Quan 	}
3501d7bf1b55SEvan Quan 
3502d7bf1b55SEvan Quan 	size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3503d7bf1b55SEvan Quan 	if (size == 0)
3504d7bf1b55SEvan Quan 		size = sysfs_emit(buf, "\n");
3505d7bf1b55SEvan Quan 
3506d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3507d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3508d7bf1b55SEvan Quan 
3509d7bf1b55SEvan Quan 	return size;
3510d7bf1b55SEvan Quan }
3511d7bf1b55SEvan Quan 
3512d7bf1b55SEvan Quan static int parse_input_od_command_lines(const char *buf,
3513d7bf1b55SEvan Quan 					size_t count,
3514d7bf1b55SEvan Quan 					u32 *type,
3515d7bf1b55SEvan Quan 					long *params,
3516d7bf1b55SEvan Quan 					uint32_t *num_of_params)
3517d7bf1b55SEvan Quan {
3518d7bf1b55SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
3519d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3520d7bf1b55SEvan Quan 	char buf_cpy[128] = {0};
3521d7bf1b55SEvan Quan 	char *tmp_str, *sub_str;
3522d7bf1b55SEvan Quan 	int ret;
3523d7bf1b55SEvan Quan 
3524d7bf1b55SEvan Quan 	if (count > sizeof(buf_cpy) - 1)
3525d7bf1b55SEvan Quan 		return -EINVAL;
3526d7bf1b55SEvan Quan 
3527d7bf1b55SEvan Quan 	memcpy(buf_cpy, buf, count);
3528d7bf1b55SEvan Quan 	tmp_str = buf_cpy;
3529d7bf1b55SEvan Quan 
3530d7bf1b55SEvan Quan 	/* skip heading spaces */
3531d7bf1b55SEvan Quan 	while (isspace(*tmp_str))
3532d7bf1b55SEvan Quan 		tmp_str++;
3533d7bf1b55SEvan Quan 
3534d7bf1b55SEvan Quan 	switch (*tmp_str) {
3535d7bf1b55SEvan Quan 	case 'c':
3536d7bf1b55SEvan Quan 		*type = PP_OD_COMMIT_DPM_TABLE;
3537d7bf1b55SEvan Quan 		return 0;
3538f7f9e48fSMa Jun 	case 'r':
3539f7f9e48fSMa Jun 		params[parameter_size] = *type;
3540f7f9e48fSMa Jun 		*num_of_params = 1;
3541f7f9e48fSMa Jun 		*type = PP_OD_RESTORE_DEFAULT_TABLE;
3542f7f9e48fSMa Jun 		return 0;
3543d7bf1b55SEvan Quan 	default:
3544d7bf1b55SEvan Quan 		break;
3545d7bf1b55SEvan Quan 	}
3546d7bf1b55SEvan Quan 
3547d7bf1b55SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3548d7bf1b55SEvan Quan 		if (strlen(sub_str) == 0)
3549d7bf1b55SEvan Quan 			continue;
3550d7bf1b55SEvan Quan 
3551d7bf1b55SEvan Quan 		ret = kstrtol(sub_str, 0, &params[parameter_size]);
3552d7bf1b55SEvan Quan 		if (ret)
3553d7bf1b55SEvan Quan 			return -EINVAL;
3554d7bf1b55SEvan Quan 		parameter_size++;
3555d7bf1b55SEvan Quan 
3556d7bf1b55SEvan Quan 		while (isspace(*tmp_str))
3557d7bf1b55SEvan Quan 			tmp_str++;
3558d7bf1b55SEvan Quan 	}
3559d7bf1b55SEvan Quan 
3560d7bf1b55SEvan Quan 	*num_of_params = parameter_size;
3561d7bf1b55SEvan Quan 
3562d7bf1b55SEvan Quan 	return 0;
3563d7bf1b55SEvan Quan }
3564d7bf1b55SEvan Quan 
3565d7bf1b55SEvan Quan static int
3566d7bf1b55SEvan Quan amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3567d7bf1b55SEvan Quan 				     enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3568d7bf1b55SEvan Quan 				     const char *in_buf,
3569d7bf1b55SEvan Quan 				     size_t count)
3570d7bf1b55SEvan Quan {
3571d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3572d7bf1b55SEvan Quan 	long parameter[64];
3573d7bf1b55SEvan Quan 	int ret;
3574d7bf1b55SEvan Quan 
3575d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3576d7bf1b55SEvan Quan 		return -EPERM;
3577d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3578d7bf1b55SEvan Quan 		return -EPERM;
3579d7bf1b55SEvan Quan 
3580d7bf1b55SEvan Quan 	ret = parse_input_od_command_lines(in_buf,
3581d7bf1b55SEvan Quan 					   count,
3582d7bf1b55SEvan Quan 					   &cmd_type,
3583d7bf1b55SEvan Quan 					   parameter,
3584d7bf1b55SEvan Quan 					   &parameter_size);
3585d7bf1b55SEvan Quan 	if (ret)
3586d7bf1b55SEvan Quan 		return ret;
3587d7bf1b55SEvan Quan 
3588d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3589d7bf1b55SEvan Quan 	if (ret < 0)
3590d7bf1b55SEvan Quan 		goto err_out0;
3591d7bf1b55SEvan Quan 
3592d7bf1b55SEvan Quan 	ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3593d7bf1b55SEvan Quan 					    cmd_type,
3594d7bf1b55SEvan Quan 					    parameter,
3595d7bf1b55SEvan Quan 					    parameter_size);
3596d7bf1b55SEvan Quan 	if (ret)
3597d7bf1b55SEvan Quan 		goto err_out1;
3598d7bf1b55SEvan Quan 
3599d7bf1b55SEvan Quan 	if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3600d7bf1b55SEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
3601d7bf1b55SEvan Quan 					       AMD_PP_TASK_READJUST_POWER_STATE,
3602d7bf1b55SEvan Quan 					       NULL);
3603d7bf1b55SEvan Quan 		if (ret)
3604d7bf1b55SEvan Quan 			goto err_out1;
3605d7bf1b55SEvan Quan 	}
3606d7bf1b55SEvan Quan 
3607d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3608d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3609d7bf1b55SEvan Quan 
3610d7bf1b55SEvan Quan 	return count;
3611d7bf1b55SEvan Quan 
3612d7bf1b55SEvan Quan err_out1:
3613d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3614d7bf1b55SEvan Quan err_out0:
3615d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3616d7bf1b55SEvan Quan 
3617d7bf1b55SEvan Quan 	return ret;
3618d7bf1b55SEvan Quan }
3619d7bf1b55SEvan Quan 
3620d7bf1b55SEvan Quan /**
3621d7bf1b55SEvan Quan  * DOC: fan_curve
3622d7bf1b55SEvan Quan  *
3623d7bf1b55SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3624d7bf1b55SEvan Quan  * control curve line.
3625d7bf1b55SEvan Quan  *
3626d7bf1b55SEvan Quan  * Reading back the file shows you the current settings(temperature in Celsius
3627d7bf1b55SEvan Quan  * degree and fan speed in pwm) applied to every anchor point of the curve line
3628d7bf1b55SEvan Quan  * and their permitted ranges if changable.
3629d7bf1b55SEvan Quan  *
3630d7bf1b55SEvan Quan  * Writing a desired string(with the format like "anchor_point_index temperature
3631d7bf1b55SEvan Quan  * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3632d7bf1b55SEvan Quan  * point accordingly.
3633d7bf1b55SEvan Quan  *
3634d7bf1b55SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3635d7bf1b55SEvan Quan  * your changes.
3636d7bf1b55SEvan Quan  *
3637f7f9e48fSMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
3638f7f9e48fSMa Jun  * reset them
3639f7f9e48fSMa Jun  *
3640d7bf1b55SEvan Quan  * There are two fan control modes supported: auto and manual. With auto mode,
3641d7bf1b55SEvan Quan  * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3642d7bf1b55SEvan Quan  * While with manual mode, users can set their own fan curve line as what
3643d7bf1b55SEvan Quan  * described here. Normally the ASIC is booted up with auto mode. Any
3644d7bf1b55SEvan Quan  * settings via this interface will switch the fan control to manual mode
3645d7bf1b55SEvan Quan  * implicitly.
3646d7bf1b55SEvan Quan  */
3647d7bf1b55SEvan Quan static ssize_t fan_curve_show(struct kobject *kobj,
3648d7bf1b55SEvan Quan 			      struct kobj_attribute *attr,
3649d7bf1b55SEvan Quan 			      char *buf)
3650d7bf1b55SEvan Quan {
3651d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3652d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3653d7bf1b55SEvan Quan 
3654d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3655d7bf1b55SEvan Quan }
3656d7bf1b55SEvan Quan 
3657d7bf1b55SEvan Quan static ssize_t fan_curve_store(struct kobject *kobj,
3658d7bf1b55SEvan Quan 			       struct kobj_attribute *attr,
3659d7bf1b55SEvan Quan 			       const char *buf,
3660d7bf1b55SEvan Quan 			       size_t count)
3661d7bf1b55SEvan Quan {
3662d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3663d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3664d7bf1b55SEvan Quan 
3665d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3666d7bf1b55SEvan Quan 							     PP_OD_EDIT_FAN_CURVE,
3667d7bf1b55SEvan Quan 							     buf,
3668d7bf1b55SEvan Quan 							     count);
3669d7bf1b55SEvan Quan }
3670d7bf1b55SEvan Quan 
3671d7bf1b55SEvan Quan static umode_t fan_curve_visible(struct amdgpu_device *adev)
3672d7bf1b55SEvan Quan {
3673d7bf1b55SEvan Quan 	umode_t umode = 0000;
3674d7bf1b55SEvan Quan 
3675d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3676d7bf1b55SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3677d7bf1b55SEvan Quan 
3678d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3679d7bf1b55SEvan Quan 		umode |= S_IWUSR;
3680d7bf1b55SEvan Quan 
3681d7bf1b55SEvan Quan 	return umode;
3682d7bf1b55SEvan Quan }
3683d7bf1b55SEvan Quan 
3684548009adSEvan Quan /**
3685548009adSEvan Quan  * DOC: acoustic_limit_rpm_threshold
3686548009adSEvan Quan  *
3687548009adSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
3688548009adSEvan Quan  * acoustic limit in RPM for fan control.
3689548009adSEvan Quan  *
3690548009adSEvan Quan  * Reading back the file shows you the current setting and the permitted
3691548009adSEvan Quan  * ranges if changable.
3692548009adSEvan Quan  *
3693548009adSEvan Quan  * Writing an integer to the file, change the setting accordingly.
3694548009adSEvan Quan  *
3695548009adSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3696548009adSEvan Quan  * your changes.
3697548009adSEvan Quan  *
36981007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
36991007bc36SMa Jun  * reset them
37001007bc36SMa Jun  *
3701548009adSEvan Quan  * This setting works under auto fan control mode only. It adjusts the PMFW's
3702548009adSEvan Quan  * behavior about the maximum speed in RPM the fan can spin. Setting via this
3703548009adSEvan Quan  * interface will switch the fan control to auto mode implicitly.
3704548009adSEvan Quan  */
3705548009adSEvan Quan static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
3706548009adSEvan Quan 					     struct kobj_attribute *attr,
3707548009adSEvan Quan 					     char *buf)
3708548009adSEvan Quan {
3709548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3710548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3711548009adSEvan Quan 
3712548009adSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
3713548009adSEvan Quan }
3714548009adSEvan Quan 
3715548009adSEvan Quan static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
3716548009adSEvan Quan 					      struct kobj_attribute *attr,
3717548009adSEvan Quan 					      const char *buf,
3718548009adSEvan Quan 					      size_t count)
3719548009adSEvan Quan {
3720548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3721548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3722548009adSEvan Quan 
3723548009adSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3724548009adSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_LIMIT,
3725548009adSEvan Quan 							     buf,
3726548009adSEvan Quan 							     count);
3727548009adSEvan Quan }
3728548009adSEvan Quan 
3729548009adSEvan Quan static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
3730548009adSEvan Quan {
3731548009adSEvan Quan 	umode_t umode = 0000;
3732548009adSEvan Quan 
3733548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
3734548009adSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3735548009adSEvan Quan 
3736548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
3737548009adSEvan Quan 		umode |= S_IWUSR;
3738548009adSEvan Quan 
3739548009adSEvan Quan 	return umode;
3740548009adSEvan Quan }
3741548009adSEvan Quan 
374247cf6fcbSEvan Quan /**
374347cf6fcbSEvan Quan  * DOC: acoustic_target_rpm_threshold
374447cf6fcbSEvan Quan  *
374547cf6fcbSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
374647cf6fcbSEvan Quan  * acoustic target in RPM for fan control.
374747cf6fcbSEvan Quan  *
374847cf6fcbSEvan Quan  * Reading back the file shows you the current setting and the permitted
374947cf6fcbSEvan Quan  * ranges if changable.
375047cf6fcbSEvan Quan  *
375147cf6fcbSEvan Quan  * Writing an integer to the file, change the setting accordingly.
375247cf6fcbSEvan Quan  *
375347cf6fcbSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
375447cf6fcbSEvan Quan  * your changes.
375547cf6fcbSEvan Quan  *
37561007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
37571007bc36SMa Jun  * reset them
37581007bc36SMa Jun  *
375947cf6fcbSEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
376047cf6fcbSEvan Quan  * other settings which can work also under auto mode. It adjusts the PMFW's
376147cf6fcbSEvan Quan  * behavior about the maximum speed in RPM the fan can spin when ASIC
376247cf6fcbSEvan Quan  * temperature is not greater than target temperature. Setting via this
376347cf6fcbSEvan Quan  * interface will switch the fan control to auto mode implicitly.
376447cf6fcbSEvan Quan  */
376547cf6fcbSEvan Quan static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
376647cf6fcbSEvan Quan 					      struct kobj_attribute *attr,
376747cf6fcbSEvan Quan 					      char *buf)
376847cf6fcbSEvan Quan {
376947cf6fcbSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
377047cf6fcbSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
377147cf6fcbSEvan Quan 
377247cf6fcbSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
377347cf6fcbSEvan Quan }
377447cf6fcbSEvan Quan 
377547cf6fcbSEvan Quan static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
377647cf6fcbSEvan Quan 					       struct kobj_attribute *attr,
377747cf6fcbSEvan Quan 					       const char *buf,
377847cf6fcbSEvan Quan 					       size_t count)
377947cf6fcbSEvan Quan {
378047cf6fcbSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
378147cf6fcbSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
378247cf6fcbSEvan Quan 
378347cf6fcbSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
378447cf6fcbSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_TARGET,
378547cf6fcbSEvan Quan 							     buf,
378647cf6fcbSEvan Quan 							     count);
378747cf6fcbSEvan Quan }
378847cf6fcbSEvan Quan 
378947cf6fcbSEvan Quan static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
379047cf6fcbSEvan Quan {
379147cf6fcbSEvan Quan 	umode_t umode = 0000;
379247cf6fcbSEvan Quan 
379347cf6fcbSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
379447cf6fcbSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
379547cf6fcbSEvan Quan 
379647cf6fcbSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
379747cf6fcbSEvan Quan 		umode |= S_IWUSR;
379847cf6fcbSEvan Quan 
379947cf6fcbSEvan Quan 	return umode;
380047cf6fcbSEvan Quan }
380147cf6fcbSEvan Quan 
3802eedd5a34SEvan Quan /**
3803eedd5a34SEvan Quan  * DOC: fan_target_temperature
3804eedd5a34SEvan Quan  *
3805eedd5a34SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
3806eedd5a34SEvan Quan  * target tempeature in Celsius degree for fan control.
3807eedd5a34SEvan Quan  *
3808eedd5a34SEvan Quan  * Reading back the file shows you the current setting and the permitted
3809eedd5a34SEvan Quan  * ranges if changable.
3810eedd5a34SEvan Quan  *
3811eedd5a34SEvan Quan  * Writing an integer to the file, change the setting accordingly.
3812eedd5a34SEvan Quan  *
3813eedd5a34SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3814eedd5a34SEvan Quan  * your changes.
3815eedd5a34SEvan Quan  *
38161007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
38171007bc36SMa Jun  * reset them
38181007bc36SMa Jun  *
3819eedd5a34SEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
3820eedd5a34SEvan Quan  * other settings which can work also under auto mode. Paring with the
3821eedd5a34SEvan Quan  * acoustic_target_rpm_threshold setting, they define the maximum speed in
3822eedd5a34SEvan Quan  * RPM the fan can spin when ASIC temperature is not greater than target
3823eedd5a34SEvan Quan  * temperature. Setting via this interface will switch the fan control to
3824eedd5a34SEvan Quan  * auto mode implicitly.
3825eedd5a34SEvan Quan  */
3826eedd5a34SEvan Quan static ssize_t fan_target_temperature_show(struct kobject *kobj,
3827eedd5a34SEvan Quan 					   struct kobj_attribute *attr,
3828eedd5a34SEvan Quan 					   char *buf)
3829eedd5a34SEvan Quan {
3830eedd5a34SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3831eedd5a34SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3832eedd5a34SEvan Quan 
3833eedd5a34SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
3834eedd5a34SEvan Quan }
3835eedd5a34SEvan Quan 
3836eedd5a34SEvan Quan static ssize_t fan_target_temperature_store(struct kobject *kobj,
3837eedd5a34SEvan Quan 					    struct kobj_attribute *attr,
3838eedd5a34SEvan Quan 					    const char *buf,
3839eedd5a34SEvan Quan 					    size_t count)
3840eedd5a34SEvan Quan {
3841eedd5a34SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3842eedd5a34SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3843eedd5a34SEvan Quan 
3844eedd5a34SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3845eedd5a34SEvan Quan 							     PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
3846eedd5a34SEvan Quan 							     buf,
3847eedd5a34SEvan Quan 							     count);
3848eedd5a34SEvan Quan }
3849eedd5a34SEvan Quan 
3850eedd5a34SEvan Quan static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
3851eedd5a34SEvan Quan {
3852eedd5a34SEvan Quan 	umode_t umode = 0000;
3853eedd5a34SEvan Quan 
3854eedd5a34SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
3855eedd5a34SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3856eedd5a34SEvan Quan 
3857eedd5a34SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
3858eedd5a34SEvan Quan 		umode |= S_IWUSR;
3859eedd5a34SEvan Quan 
3860eedd5a34SEvan Quan 	return umode;
3861eedd5a34SEvan Quan }
3862eedd5a34SEvan Quan 
38639df5d008SEvan Quan /**
38649df5d008SEvan Quan  * DOC: fan_minimum_pwm
38659df5d008SEvan Quan  *
38669df5d008SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
38679df5d008SEvan Quan  * minimum fan speed in PWM.
38689df5d008SEvan Quan  *
38699df5d008SEvan Quan  * Reading back the file shows you the current setting and the permitted
38709df5d008SEvan Quan  * ranges if changable.
38719df5d008SEvan Quan  *
38729df5d008SEvan Quan  * Writing an integer to the file, change the setting accordingly.
38739df5d008SEvan Quan  *
38749df5d008SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
38759df5d008SEvan Quan  * your changes.
38769df5d008SEvan Quan  *
38771007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
38781007bc36SMa Jun  * reset them
38791007bc36SMa Jun  *
38809df5d008SEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
38819df5d008SEvan Quan  * other settings which can work also under auto mode. It adjusts the PMFW's
38829df5d008SEvan Quan  * behavior about the minimum fan speed in PWM the fan should spin. Setting
38839df5d008SEvan Quan  * via this interface will switch the fan control to auto mode implicitly.
38849df5d008SEvan Quan  */
38859df5d008SEvan Quan static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
38869df5d008SEvan Quan 				    struct kobj_attribute *attr,
38879df5d008SEvan Quan 				    char *buf)
38889df5d008SEvan Quan {
38899df5d008SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
38909df5d008SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
38919df5d008SEvan Quan 
38929df5d008SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
38939df5d008SEvan Quan }
38949df5d008SEvan Quan 
38959df5d008SEvan Quan static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
38969df5d008SEvan Quan 				     struct kobj_attribute *attr,
38979df5d008SEvan Quan 				     const char *buf,
38989df5d008SEvan Quan 				     size_t count)
38999df5d008SEvan Quan {
39009df5d008SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
39019df5d008SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
39029df5d008SEvan Quan 
39039df5d008SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
39049df5d008SEvan Quan 							     PP_OD_EDIT_FAN_MINIMUM_PWM,
39059df5d008SEvan Quan 							     buf,
39069df5d008SEvan Quan 							     count);
39079df5d008SEvan Quan }
39089df5d008SEvan Quan 
39099df5d008SEvan Quan static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
39109df5d008SEvan Quan {
39119df5d008SEvan Quan 	umode_t umode = 0000;
39129df5d008SEvan Quan 
39139df5d008SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
39149df5d008SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
39159df5d008SEvan Quan 
39169df5d008SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
39179df5d008SEvan Quan 		umode |= S_IWUSR;
39189df5d008SEvan Quan 
39199df5d008SEvan Quan 	return umode;
39209df5d008SEvan Quan }
39219df5d008SEvan Quan 
3922d7bf1b55SEvan Quan static struct od_feature_set amdgpu_od_set = {
3923d7bf1b55SEvan Quan 	.containers = {
3924d7bf1b55SEvan Quan 		[0] = {
3925d7bf1b55SEvan Quan 			.name = "fan_ctrl",
3926d7bf1b55SEvan Quan 			.sub_feature = {
3927d7bf1b55SEvan Quan 				[0] = {
3928d7bf1b55SEvan Quan 					.name = "fan_curve",
3929d7bf1b55SEvan Quan 					.ops = {
3930d7bf1b55SEvan Quan 						.is_visible = fan_curve_visible,
3931d7bf1b55SEvan Quan 						.show = fan_curve_show,
3932d7bf1b55SEvan Quan 						.store = fan_curve_store,
3933d7bf1b55SEvan Quan 					},
3934d7bf1b55SEvan Quan 				},
3935548009adSEvan Quan 				[1] = {
3936548009adSEvan Quan 					.name = "acoustic_limit_rpm_threshold",
3937548009adSEvan Quan 					.ops = {
3938548009adSEvan Quan 						.is_visible = acoustic_limit_threshold_visible,
3939548009adSEvan Quan 						.show = acoustic_limit_threshold_show,
3940548009adSEvan Quan 						.store = acoustic_limit_threshold_store,
3941548009adSEvan Quan 					},
3942548009adSEvan Quan 				},
394347cf6fcbSEvan Quan 				[2] = {
394447cf6fcbSEvan Quan 					.name = "acoustic_target_rpm_threshold",
394547cf6fcbSEvan Quan 					.ops = {
394647cf6fcbSEvan Quan 						.is_visible = acoustic_target_threshold_visible,
394747cf6fcbSEvan Quan 						.show = acoustic_target_threshold_show,
394847cf6fcbSEvan Quan 						.store = acoustic_target_threshold_store,
394947cf6fcbSEvan Quan 					},
395047cf6fcbSEvan Quan 				},
3951eedd5a34SEvan Quan 				[3] = {
3952eedd5a34SEvan Quan 					.name = "fan_target_temperature",
3953eedd5a34SEvan Quan 					.ops = {
3954eedd5a34SEvan Quan 						.is_visible = fan_target_temperature_visible,
3955eedd5a34SEvan Quan 						.show = fan_target_temperature_show,
3956eedd5a34SEvan Quan 						.store = fan_target_temperature_store,
3957eedd5a34SEvan Quan 					},
3958eedd5a34SEvan Quan 				},
39599df5d008SEvan Quan 				[4] = {
39609df5d008SEvan Quan 					.name = "fan_minimum_pwm",
39619df5d008SEvan Quan 					.ops = {
39629df5d008SEvan Quan 						.is_visible = fan_minimum_pwm_visible,
39639df5d008SEvan Quan 						.show = fan_minimum_pwm_show,
39649df5d008SEvan Quan 						.store = fan_minimum_pwm_store,
39659df5d008SEvan Quan 					},
39669df5d008SEvan Quan 				},
3967d7bf1b55SEvan Quan 			},
3968d7bf1b55SEvan Quan 		},
3969d7bf1b55SEvan Quan 	},
3970d7bf1b55SEvan Quan };
39713e38b634SEvan Quan 
39723e38b634SEvan Quan static void od_kobj_release(struct kobject *kobj)
39733e38b634SEvan Quan {
39743e38b634SEvan Quan 	struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
39753e38b634SEvan Quan 
39763e38b634SEvan Quan 	kfree(od_kobj);
39773e38b634SEvan Quan }
39783e38b634SEvan Quan 
39793e38b634SEvan Quan static const struct kobj_type od_ktype = {
39803e38b634SEvan Quan 	.release	= od_kobj_release,
39813e38b634SEvan Quan 	.sysfs_ops	= &kobj_sysfs_ops,
39823e38b634SEvan Quan };
39833e38b634SEvan Quan 
39843e38b634SEvan Quan static void amdgpu_od_set_fini(struct amdgpu_device *adev)
39853e38b634SEvan Quan {
39863e38b634SEvan Quan 	struct od_kobj *container, *container_next;
39873e38b634SEvan Quan 	struct od_attribute *attribute, *attribute_next;
39883e38b634SEvan Quan 
39893e38b634SEvan Quan 	if (list_empty(&adev->pm.od_kobj_list))
39903e38b634SEvan Quan 		return;
39913e38b634SEvan Quan 
39923e38b634SEvan Quan 	list_for_each_entry_safe(container, container_next,
39933e38b634SEvan Quan 				 &adev->pm.od_kobj_list, entry) {
39943e38b634SEvan Quan 		list_del(&container->entry);
39953e38b634SEvan Quan 
39963e38b634SEvan Quan 		list_for_each_entry_safe(attribute, attribute_next,
39973e38b634SEvan Quan 					 &container->attribute, entry) {
39983e38b634SEvan Quan 			list_del(&attribute->entry);
39993e38b634SEvan Quan 			sysfs_remove_file(&container->kobj,
40003e38b634SEvan Quan 					  &attribute->attribute.attr);
40013e38b634SEvan Quan 			kfree(attribute);
40023e38b634SEvan Quan 		}
40033e38b634SEvan Quan 
40043e38b634SEvan Quan 		kobject_put(&container->kobj);
40053e38b634SEvan Quan 	}
40063e38b634SEvan Quan }
40073e38b634SEvan Quan 
40083e38b634SEvan Quan static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
40093e38b634SEvan Quan 					   struct od_feature_ops *feature_ops)
40103e38b634SEvan Quan {
40113e38b634SEvan Quan 	umode_t mode;
40123e38b634SEvan Quan 
40133e38b634SEvan Quan 	if (!feature_ops->is_visible)
40143e38b634SEvan Quan 		return false;
40153e38b634SEvan Quan 
40163e38b634SEvan Quan 	/*
40173e38b634SEvan Quan 	 * If the feature has no user read and write mode set,
40183e38b634SEvan Quan 	 * we can assume the feature is actually not supported.(?)
40193e38b634SEvan Quan 	 * And the revelant sysfs interface should not be exposed.
40203e38b634SEvan Quan 	 */
40213e38b634SEvan Quan 	mode = feature_ops->is_visible(adev);
40223e38b634SEvan Quan 	if (mode & (S_IRUSR | S_IWUSR))
40233e38b634SEvan Quan 		return true;
40243e38b634SEvan Quan 
40253e38b634SEvan Quan 	return false;
40263e38b634SEvan Quan }
40273e38b634SEvan Quan 
40283e38b634SEvan Quan static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
40293e38b634SEvan Quan 					struct od_feature_container *container)
40303e38b634SEvan Quan {
40313e38b634SEvan Quan 	int i;
40323e38b634SEvan Quan 
40333e38b634SEvan Quan 	/*
40343e38b634SEvan Quan 	 * If there is no valid entry within the container, the container
40353e38b634SEvan Quan 	 * is recognized as a self contained container. And the valid entry
40363e38b634SEvan Quan 	 * here means it has a valid naming and it is visible/supported by
40373e38b634SEvan Quan 	 * the ASIC.
40383e38b634SEvan Quan 	 */
40393e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
40403e38b634SEvan Quan 		if (container->sub_feature[i].name &&
40413e38b634SEvan Quan 		    amdgpu_is_od_feature_supported(adev,
40423e38b634SEvan Quan 			&container->sub_feature[i].ops))
40433e38b634SEvan Quan 			return false;
40443e38b634SEvan Quan 	}
40453e38b634SEvan Quan 
40463e38b634SEvan Quan 	return true;
40473e38b634SEvan Quan }
40483e38b634SEvan Quan 
40493e38b634SEvan Quan static int amdgpu_od_set_init(struct amdgpu_device *adev)
40503e38b634SEvan Quan {
40513e38b634SEvan Quan 	struct od_kobj *top_set, *sub_set;
40523e38b634SEvan Quan 	struct od_attribute *attribute;
40533e38b634SEvan Quan 	struct od_feature_container *container;
40543e38b634SEvan Quan 	struct od_feature_item *feature;
40553e38b634SEvan Quan 	int i, j;
40563e38b634SEvan Quan 	int ret;
40573e38b634SEvan Quan 
40583e38b634SEvan Quan 	/* Setup the top `gpu_od` directory which holds all other OD interfaces */
40593e38b634SEvan Quan 	top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
40603e38b634SEvan Quan 	if (!top_set)
40613e38b634SEvan Quan 		return -ENOMEM;
40623e38b634SEvan Quan 	list_add(&top_set->entry, &adev->pm.od_kobj_list);
40633e38b634SEvan Quan 
40643e38b634SEvan Quan 	ret = kobject_init_and_add(&top_set->kobj,
40653e38b634SEvan Quan 				   &od_ktype,
40663e38b634SEvan Quan 				   &adev->dev->kobj,
40673e38b634SEvan Quan 				   "%s",
40683e38b634SEvan Quan 				   "gpu_od");
40693e38b634SEvan Quan 	if (ret)
40703e38b634SEvan Quan 		goto err_out;
40713e38b634SEvan Quan 	INIT_LIST_HEAD(&top_set->attribute);
40723e38b634SEvan Quan 	top_set->priv = adev;
40733e38b634SEvan Quan 
40743e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
40753e38b634SEvan Quan 		container = &amdgpu_od_set.containers[i];
40763e38b634SEvan Quan 
40773e38b634SEvan Quan 		if (!container->name)
40783e38b634SEvan Quan 			continue;
40793e38b634SEvan Quan 
40803e38b634SEvan Quan 		/*
40813e38b634SEvan Quan 		 * If there is valid entries within the container, the container
40823e38b634SEvan Quan 		 * will be presented as a sub directory and all its holding entries
40833e38b634SEvan Quan 		 * will be presented as plain files under it.
40843e38b634SEvan Quan 		 * While if there is no valid entry within the container, the container
40853e38b634SEvan Quan 		 * itself will be presented as a plain file under top `gpu_od` directory.
40863e38b634SEvan Quan 		 */
40873e38b634SEvan Quan 		if (amdgpu_od_is_self_contained(adev, container)) {
40883e38b634SEvan Quan 			if (!amdgpu_is_od_feature_supported(adev,
40893e38b634SEvan Quan 			     &container->ops))
40903e38b634SEvan Quan 				continue;
40913e38b634SEvan Quan 
40923e38b634SEvan Quan 			/*
40933e38b634SEvan Quan 			 * The container is presented as a plain file under top `gpu_od`
40943e38b634SEvan Quan 			 * directory.
40953e38b634SEvan Quan 			 */
40963e38b634SEvan Quan 			attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
40973e38b634SEvan Quan 			if (!attribute) {
40983e38b634SEvan Quan 				ret = -ENOMEM;
40993e38b634SEvan Quan 				goto err_out;
41003e38b634SEvan Quan 			}
41013e38b634SEvan Quan 			list_add(&attribute->entry, &top_set->attribute);
41023e38b634SEvan Quan 
41033e38b634SEvan Quan 			attribute->attribute.attr.mode =
41043e38b634SEvan Quan 					container->ops.is_visible(adev);
41053e38b634SEvan Quan 			attribute->attribute.attr.name = container->name;
41063e38b634SEvan Quan 			attribute->attribute.show =
41073e38b634SEvan Quan 					container->ops.show;
41083e38b634SEvan Quan 			attribute->attribute.store =
41093e38b634SEvan Quan 					container->ops.store;
41103e38b634SEvan Quan 			ret = sysfs_create_file(&top_set->kobj,
41113e38b634SEvan Quan 						&attribute->attribute.attr);
41123e38b634SEvan Quan 			if (ret)
41133e38b634SEvan Quan 				goto err_out;
41143e38b634SEvan Quan 		} else {
41153e38b634SEvan Quan 			/* The container is presented as a sub directory. */
41163e38b634SEvan Quan 			sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
41173e38b634SEvan Quan 			if (!sub_set) {
41183e38b634SEvan Quan 				ret = -ENOMEM;
41193e38b634SEvan Quan 				goto err_out;
41203e38b634SEvan Quan 			}
41213e38b634SEvan Quan 			list_add(&sub_set->entry, &adev->pm.od_kobj_list);
41223e38b634SEvan Quan 
41233e38b634SEvan Quan 			ret = kobject_init_and_add(&sub_set->kobj,
41243e38b634SEvan Quan 						   &od_ktype,
41253e38b634SEvan Quan 						   &top_set->kobj,
41263e38b634SEvan Quan 						   "%s",
41273e38b634SEvan Quan 						   container->name);
41283e38b634SEvan Quan 			if (ret)
41293e38b634SEvan Quan 				goto err_out;
41303e38b634SEvan Quan 			INIT_LIST_HEAD(&sub_set->attribute);
41313e38b634SEvan Quan 			sub_set->priv = adev;
41323e38b634SEvan Quan 
41333e38b634SEvan Quan 			for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
41343e38b634SEvan Quan 				feature = &container->sub_feature[j];
41353e38b634SEvan Quan 				if (!feature->name)
41363e38b634SEvan Quan 					continue;
41373e38b634SEvan Quan 
41383e38b634SEvan Quan 				if (!amdgpu_is_od_feature_supported(adev,
41393e38b634SEvan Quan 				     &feature->ops))
41403e38b634SEvan Quan 					continue;
41413e38b634SEvan Quan 
41423e38b634SEvan Quan 				/*
41433e38b634SEvan Quan 				 * With the container presented as a sub directory, the entry within
41443e38b634SEvan Quan 				 * it is presented as a plain file under the sub directory.
41453e38b634SEvan Quan 				 */
41463e38b634SEvan Quan 				attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
41473e38b634SEvan Quan 				if (!attribute) {
41483e38b634SEvan Quan 					ret = -ENOMEM;
41493e38b634SEvan Quan 					goto err_out;
41503e38b634SEvan Quan 				}
41513e38b634SEvan Quan 				list_add(&attribute->entry, &sub_set->attribute);
41523e38b634SEvan Quan 
41533e38b634SEvan Quan 				attribute->attribute.attr.mode =
41543e38b634SEvan Quan 						feature->ops.is_visible(adev);
41553e38b634SEvan Quan 				attribute->attribute.attr.name = feature->name;
41563e38b634SEvan Quan 				attribute->attribute.show =
41573e38b634SEvan Quan 						feature->ops.show;
41583e38b634SEvan Quan 				attribute->attribute.store =
41593e38b634SEvan Quan 						feature->ops.store;
41603e38b634SEvan Quan 				ret = sysfs_create_file(&sub_set->kobj,
41613e38b634SEvan Quan 							&attribute->attribute.attr);
41623e38b634SEvan Quan 				if (ret)
41633e38b634SEvan Quan 					goto err_out;
41643e38b634SEvan Quan 			}
41653e38b634SEvan Quan 		}
41663e38b634SEvan Quan 	}
41673e38b634SEvan Quan 
41683e38b634SEvan Quan 	return 0;
41693e38b634SEvan Quan 
41703e38b634SEvan Quan err_out:
41713e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
41723e38b634SEvan Quan 
41733e38b634SEvan Quan 	return ret;
41743e38b634SEvan Quan }
41753e38b634SEvan Quan 
4176e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4177e098bc96SEvan Quan {
417888e5c8f8SMa Jun 	enum amdgpu_sriov_vf_mode mode;
4179e098bc96SEvan Quan 	uint32_t mask = 0;
41803e38b634SEvan Quan 	int ret;
4181e098bc96SEvan Quan 
4182e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
4183e098bc96SEvan Quan 		return 0;
4184e098bc96SEvan Quan 
41855fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
41865fa99373SZhenGuo Yin 
4187e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
4188e098bc96SEvan Quan 		return 0;
4189e098bc96SEvan Quan 
419088e5c8f8SMa Jun 	mode = amdgpu_virt_get_sriov_vf_mode(adev);
419188e5c8f8SMa Jun 
419288e5c8f8SMa Jun 	/* under multi-vf mode, the hwmon attributes are all not supported */
419388e5c8f8SMa Jun 	if (mode != SRIOV_VF_MODE_MULTI_VF) {
4194e098bc96SEvan Quan 		adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
4195e098bc96SEvan Quan 														DRIVER_NAME, adev,
4196e098bc96SEvan Quan 														hwmon_groups);
4197e098bc96SEvan Quan 		if (IS_ERR(adev->pm.int_hwmon_dev)) {
4198e098bc96SEvan Quan 			ret = PTR_ERR(adev->pm.int_hwmon_dev);
419988e5c8f8SMa Jun 			dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4200e098bc96SEvan Quan 			return ret;
4201e098bc96SEvan Quan 		}
420288e5c8f8SMa Jun 	}
4203e098bc96SEvan Quan 
420488e5c8f8SMa Jun 	switch (mode) {
4205e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
4206e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
4207e098bc96SEvan Quan 		break;
4208e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
4209e098bc96SEvan Quan 		mask = 0;
4210e098bc96SEvan Quan 		break;
4211e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
4212e098bc96SEvan Quan 	default:
4213e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
4214e098bc96SEvan Quan 		break;
4215e098bc96SEvan Quan 	}
4216e098bc96SEvan Quan 
4217e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
4218e098bc96SEvan Quan 					       amdgpu_device_attrs,
4219e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
4220e098bc96SEvan Quan 					       mask,
4221e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
4222e098bc96SEvan Quan 	if (ret)
42233e38b634SEvan Quan 		goto err_out0;
42243e38b634SEvan Quan 
42253e38b634SEvan Quan 	if (amdgpu_dpm_is_overdrive_supported(adev)) {
42263e38b634SEvan Quan 		ret = amdgpu_od_set_init(adev);
42273e38b634SEvan Quan 		if (ret)
42283e38b634SEvan Quan 			goto err_out1;
42293e38b634SEvan Quan 	}
4230e098bc96SEvan Quan 
4231e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
4232e098bc96SEvan Quan 
4233e098bc96SEvan Quan 	return 0;
42343e38b634SEvan Quan 
42353e38b634SEvan Quan err_out1:
42363e38b634SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
42373e38b634SEvan Quan err_out0:
42383e38b634SEvan Quan 	if (adev->pm.int_hwmon_dev)
42393e38b634SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
42403e38b634SEvan Quan 
42413e38b634SEvan Quan 	return ret;
4242e098bc96SEvan Quan }
4243e098bc96SEvan Quan 
4244e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4245e098bc96SEvan Quan {
42463e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
42473e38b634SEvan Quan 
4248e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
4249e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4250e098bc96SEvan Quan 
4251e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4252e098bc96SEvan Quan }
4253e098bc96SEvan Quan 
4254e098bc96SEvan Quan /*
4255e098bc96SEvan Quan  * Debugfs info
4256e098bc96SEvan Quan  */
4257e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4258e098bc96SEvan Quan 
4259517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
4260e1b3bcaaSRan Sun 					   struct amdgpu_device *adev)
4261e1b3bcaaSRan Sun {
4262517cb957SHuang Rui 	uint16_t *p_val;
4263517cb957SHuang Rui 	uint32_t size;
4264517cb957SHuang Rui 	int i;
426579c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
4266517cb957SHuang Rui 
426779c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
426879c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
4269517cb957SHuang Rui 				GFP_KERNEL);
4270517cb957SHuang Rui 
4271517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4272517cb957SHuang Rui 					    (void *)p_val, &size)) {
427379c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
4274517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
4275517cb957SHuang Rui 					   *(p_val + i), i);
4276517cb957SHuang Rui 		}
4277517cb957SHuang Rui 
4278517cb957SHuang Rui 		kfree(p_val);
4279517cb957SHuang Rui 	}
4280517cb957SHuang Rui }
4281517cb957SHuang Rui 
4282e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4283e098bc96SEvan Quan {
42844e8303cfSLijo Lazar 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
42854e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4286e098bc96SEvan Quan 	uint32_t value;
4287800c53d6SXiaojian Du 	uint64_t value64 = 0;
4288e098bc96SEvan Quan 	uint32_t query = 0;
4289e098bc96SEvan Quan 	int size;
4290e098bc96SEvan Quan 
4291e098bc96SEvan Quan 	/* GPU Clocks */
4292e098bc96SEvan Quan 	size = sizeof(value);
4293e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
4294517cb957SHuang Rui 
4295517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
4296517cb957SHuang Rui 
4297e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
4298e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
4299e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
4300e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
4301e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4302e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4303e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4304e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
4305e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
4306e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
4307e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
4308e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
4309e098bc96SEvan Quan 	size = sizeof(uint32_t);
43109366c2e8SMario Limonciello 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size))
4311f0b8f65bSLi Ma 		seq_printf(m, "\t%u.%02u W (average GPU)\n", query >> 8, query & 0xff);
4312e0e1764aSAlex Deucher 	size = sizeof(uint32_t);
4313e0e1764aSAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size))
4314f0b8f65bSLi Ma 		seq_printf(m, "\t%u.%02u W (current GPU)\n", query >> 8, query & 0xff);
4315e098bc96SEvan Quan 	size = sizeof(value);
4316e098bc96SEvan Quan 	seq_printf(m, "\n");
4317e098bc96SEvan Quan 
4318e098bc96SEvan Quan 	/* GPU Temp */
4319e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4320e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4321e098bc96SEvan Quan 
4322e098bc96SEvan Quan 	/* GPU Load */
4323e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4324e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
4325e098bc96SEvan Quan 	/* MEM Load */
4326e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4327e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
4328e098bc96SEvan Quan 
4329e098bc96SEvan Quan 	seq_printf(m, "\n");
4330e098bc96SEvan Quan 
4331e098bc96SEvan Quan 	/* SMC feature mask */
4332e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4333e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4334e098bc96SEvan Quan 
43358ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
43368ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4337e098bc96SEvan Quan 		/* VCN clocks */
4338e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4339e098bc96SEvan Quan 			if (!value) {
4340e098bc96SEvan Quan 				seq_printf(m, "VCN: Disabled\n");
4341e098bc96SEvan Quan 			} else {
4342e098bc96SEvan Quan 				seq_printf(m, "VCN: Enabled\n");
4343e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4344e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4345e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4346e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4347e098bc96SEvan Quan 			}
4348e098bc96SEvan Quan 		}
4349e098bc96SEvan Quan 		seq_printf(m, "\n");
4350e098bc96SEvan Quan 	} else {
4351e098bc96SEvan Quan 		/* UVD clocks */
4352e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4353e098bc96SEvan Quan 			if (!value) {
4354e098bc96SEvan Quan 				seq_printf(m, "UVD: Disabled\n");
4355e098bc96SEvan Quan 			} else {
4356e098bc96SEvan Quan 				seq_printf(m, "UVD: Enabled\n");
4357e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4358e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4359e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4360e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4361e098bc96SEvan Quan 			}
4362e098bc96SEvan Quan 		}
4363e098bc96SEvan Quan 		seq_printf(m, "\n");
4364e098bc96SEvan Quan 
4365e098bc96SEvan Quan 		/* VCE clocks */
4366e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4367e098bc96SEvan Quan 			if (!value) {
4368e098bc96SEvan Quan 				seq_printf(m, "VCE: Disabled\n");
4369e098bc96SEvan Quan 			} else {
4370e098bc96SEvan Quan 				seq_printf(m, "VCE: Enabled\n");
4371e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4372e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4373e098bc96SEvan Quan 			}
4374e098bc96SEvan Quan 		}
4375e098bc96SEvan Quan 	}
4376e098bc96SEvan Quan 
4377e098bc96SEvan Quan 	return 0;
4378e098bc96SEvan Quan }
4379e098bc96SEvan Quan 
438044762718SNathan Chancellor static const struct cg_flag_name clocks[] = {
438144762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
438244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
438344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
438444762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
438544762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
438644762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
438744762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
438844762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
438944762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
439044762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
439144762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
439244762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
439344762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
439444762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
439544762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
439644762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
439744762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
439844762718SNathan Chancellor 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
439944762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
440044762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
440144762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
440244762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
440344762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
440444762718SNathan Chancellor 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
440544762718SNathan Chancellor 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
440644762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
440744762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
440844762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
440944762718SNathan Chancellor 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
441044762718SNathan Chancellor 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
441144762718SNathan Chancellor 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
441244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
441344762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
441444762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
441544762718SNathan Chancellor 	{0, NULL},
441644762718SNathan Chancellor };
441744762718SNathan Chancellor 
441825faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
4419e098bc96SEvan Quan {
4420e098bc96SEvan Quan 	int i;
4421e098bc96SEvan Quan 
4422e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
4423e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
4424e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
4425e098bc96SEvan Quan }
4426e098bc96SEvan Quan 
4427373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
4428e098bc96SEvan Quan {
4429373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4430373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
443125faeddcSEvan Quan 	u64 flags = 0;
4432e098bc96SEvan Quan 	int r;
4433e098bc96SEvan Quan 
443453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
4435e098bc96SEvan Quan 		return -EPERM;
4436d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
4437d2ae842dSAlex Deucher 		return -EPERM;
4438e098bc96SEvan Quan 
4439e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
4440e098bc96SEvan Quan 	if (r < 0) {
4441e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
4442e098bc96SEvan Quan 		return r;
4443e098bc96SEvan Quan 	}
4444e098bc96SEvan Quan 
444579c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
4446e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
4447e098bc96SEvan Quan 		if (r)
4448e098bc96SEvan Quan 			goto out;
444979c65f3fSEvan Quan 	}
4450e098bc96SEvan Quan 
4451e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
4452e098bc96SEvan Quan 
445325faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
4454e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
4455e098bc96SEvan Quan 	seq_printf(m, "\n");
4456e098bc96SEvan Quan 
4457e098bc96SEvan Quan out:
4458e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
4459e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
4460e098bc96SEvan Quan 
4461e098bc96SEvan Quan 	return r;
4462e098bc96SEvan Quan }
4463e098bc96SEvan Quan 
4464373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4465373720f7SNirmoy Das 
446627ebf21fSLijo Lazar /*
446727ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
446827ebf21fSLijo Lazar  *
446927ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
447027ebf21fSLijo Lazar  */
447127ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
447227ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
447327ebf21fSLijo Lazar {
447427ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
447527ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
447627ebf21fSLijo Lazar 	void *smu_prv_buf;
447779c65f3fSEvan Quan 	int ret = 0;
447827ebf21fSLijo Lazar 
447927ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
448027ebf21fSLijo Lazar 		return -EPERM;
448127ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
448227ebf21fSLijo Lazar 		return -EPERM;
448327ebf21fSLijo Lazar 
448479c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
448579c65f3fSEvan Quan 	if (ret)
448679c65f3fSEvan Quan 		return ret;
448727ebf21fSLijo Lazar 
448827ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
448927ebf21fSLijo Lazar 		return -EINVAL;
449027ebf21fSLijo Lazar 
449127ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
449227ebf21fSLijo Lazar 				       smu_prv_buf_size);
449327ebf21fSLijo Lazar }
449427ebf21fSLijo Lazar 
449527ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
449627ebf21fSLijo Lazar 	.owner = THIS_MODULE,
449727ebf21fSLijo Lazar 	.open = simple_open,
449827ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
449927ebf21fSLijo Lazar 	.llseek = default_llseek,
450027ebf21fSLijo Lazar };
450127ebf21fSLijo Lazar 
4502e098bc96SEvan Quan #endif
4503e098bc96SEvan Quan 
4504373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
4505e098bc96SEvan Quan {
4506e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4507373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
4508373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
4509373720f7SNirmoy Das 
45101613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
45111613f346SFlora Cui 		return;
45121613f346SFlora Cui 
4513373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4514373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
4515373720f7SNirmoy Das 
451627ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
451727ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
451827ebf21fSLijo Lazar 					 adev,
451927ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
452027ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
45211f5fc7a5SAndrey Grodzovsky 
452279c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
4523e098bc96SEvan Quan #endif
4524e098bc96SEvan Quan }
4525