xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision 69bc7a8a61aaa71e7cba1065484c421e7556edcc)
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 
41166a3c73SYang Wang #define DEVICE_ATTR_IS(_name)		(attr_id == device_attr_id__##_name)
42166a3c73SYang Wang 
433e38b634SEvan Quan struct od_attribute {
443e38b634SEvan Quan 	struct kobj_attribute	attribute;
453e38b634SEvan Quan 	struct list_head	entry;
463e38b634SEvan Quan };
473e38b634SEvan Quan 
483e38b634SEvan Quan struct od_kobj {
493e38b634SEvan Quan 	struct kobject		kobj;
503e38b634SEvan Quan 	struct list_head	entry;
513e38b634SEvan Quan 	struct list_head	attribute;
523e38b634SEvan Quan 	void			*priv;
533e38b634SEvan Quan };
543e38b634SEvan Quan 
553e38b634SEvan Quan struct od_feature_ops {
563e38b634SEvan Quan 	umode_t (*is_visible)(struct amdgpu_device *adev);
573e38b634SEvan Quan 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
583e38b634SEvan Quan 			char *buf);
593e38b634SEvan Quan 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
603e38b634SEvan Quan 			 const char *buf, size_t count);
613e38b634SEvan Quan };
623e38b634SEvan Quan 
633e38b634SEvan Quan struct od_feature_item {
643e38b634SEvan Quan 	const char		*name;
653e38b634SEvan Quan 	struct od_feature_ops	ops;
663e38b634SEvan Quan };
673e38b634SEvan Quan 
683e38b634SEvan Quan struct od_feature_container {
693e38b634SEvan Quan 	char				*name;
703e38b634SEvan Quan 	struct od_feature_ops		ops;
713e38b634SEvan Quan 	struct od_feature_item		sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET];
723e38b634SEvan Quan };
733e38b634SEvan Quan 
743e38b634SEvan Quan struct od_feature_set {
753e38b634SEvan Quan 	struct od_feature_container	containers[MAX_NUM_OF_SUBSETS];
763e38b634SEvan Quan };
773e38b634SEvan Quan 
78e098bc96SEvan Quan static const struct hwmon_temp_label {
79e098bc96SEvan Quan 	enum PP_HWMON_TEMP channel;
80e098bc96SEvan Quan 	const char *label;
81e098bc96SEvan Quan } temp_label[] = {
82e098bc96SEvan Quan 	{PP_TEMP_EDGE, "edge"},
83e098bc96SEvan Quan 	{PP_TEMP_JUNCTION, "junction"},
84e098bc96SEvan Quan 	{PP_TEMP_MEM, "mem"},
85e098bc96SEvan Quan };
86e098bc96SEvan Quan 
873867e370SDarren Powell const char * const amdgpu_pp_profile_name[] = {
883867e370SDarren Powell 	"BOOTUP_DEFAULT",
893867e370SDarren Powell 	"3D_FULL_SCREEN",
903867e370SDarren Powell 	"POWER_SAVING",
913867e370SDarren Powell 	"VIDEO",
923867e370SDarren Powell 	"VR",
933867e370SDarren Powell 	"COMPUTE",
94334682aeSKenneth Feng 	"CUSTOM",
95334682aeSKenneth Feng 	"WINDOW_3D",
9631865e96SPerry Yuan 	"CAPPED",
9731865e96SPerry Yuan 	"UNCAPPED",
983867e370SDarren Powell };
993867e370SDarren Powell 
100e098bc96SEvan Quan /**
101e098bc96SEvan Quan  * DOC: power_dpm_state
102e098bc96SEvan Quan  *
103e098bc96SEvan Quan  * The power_dpm_state file is a legacy interface and is only provided for
104e098bc96SEvan Quan  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
105e098bc96SEvan Quan  * certain power related parameters.  The file power_dpm_state is used for this.
106e098bc96SEvan Quan  * It accepts the following arguments:
107e098bc96SEvan Quan  *
108e098bc96SEvan Quan  * - battery
109e098bc96SEvan Quan  *
110e098bc96SEvan Quan  * - balanced
111e098bc96SEvan Quan  *
112e098bc96SEvan Quan  * - performance
113e098bc96SEvan Quan  *
114e098bc96SEvan Quan  * battery
115e098bc96SEvan Quan  *
116e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for battery
117e098bc96SEvan Quan  * operation.  Selecting battery switched to this state.  This is no
118e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
119e098bc96SEvan Quan  *
120e098bc96SEvan Quan  * balanced
121e098bc96SEvan Quan  *
122e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for balanced
123e098bc96SEvan Quan  * operation.  Selecting balanced switched to this state.  This is no
124e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
125e098bc96SEvan Quan  *
126e098bc96SEvan Quan  * performance
127e098bc96SEvan Quan  *
128e098bc96SEvan Quan  * On older GPUs, the vbios provided a special power state for performance
129e098bc96SEvan Quan  * operation.  Selecting performance switched to this state.  This is no
130e098bc96SEvan Quan  * longer provided on newer GPUs so the option does nothing in that case.
131e098bc96SEvan Quan  *
132e098bc96SEvan Quan  */
133e098bc96SEvan Quan 
134e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
135e098bc96SEvan Quan 					  struct device_attribute *attr,
136e098bc96SEvan Quan 					  char *buf)
137e098bc96SEvan Quan {
138e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1391348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
140e098bc96SEvan Quan 	enum amd_pm_state_type pm;
141e098bc96SEvan Quan 	int ret;
142e098bc96SEvan Quan 
14353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
144e098bc96SEvan Quan 		return -EPERM;
145d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
146d2ae842dSAlex Deucher 		return -EPERM;
147e098bc96SEvan Quan 
148e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
149e098bc96SEvan Quan 	if (ret < 0) {
150e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
151e098bc96SEvan Quan 		return ret;
152e098bc96SEvan Quan 	}
153e098bc96SEvan Quan 
15479c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
155e098bc96SEvan Quan 
156e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
157e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
158e098bc96SEvan Quan 
159a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
160e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
161e098bc96SEvan Quan 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
162e098bc96SEvan Quan }
163e098bc96SEvan Quan 
164e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
165e098bc96SEvan Quan 					  struct device_attribute *attr,
166e098bc96SEvan Quan 					  const char *buf,
167e098bc96SEvan Quan 					  size_t count)
168e098bc96SEvan Quan {
169e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
1701348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
171e098bc96SEvan Quan 	enum amd_pm_state_type  state;
172e098bc96SEvan Quan 	int ret;
173e098bc96SEvan Quan 
17453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
175e098bc96SEvan Quan 		return -EPERM;
176d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
177d2ae842dSAlex Deucher 		return -EPERM;
178e098bc96SEvan Quan 
179e098bc96SEvan Quan 	if (strncmp("battery", buf, strlen("battery")) == 0)
180e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BATTERY;
181e098bc96SEvan Quan 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
182e098bc96SEvan Quan 		state = POWER_STATE_TYPE_BALANCED;
183e098bc96SEvan Quan 	else if (strncmp("performance", buf, strlen("performance")) == 0)
184e098bc96SEvan Quan 		state = POWER_STATE_TYPE_PERFORMANCE;
185e098bc96SEvan Quan 	else
186e098bc96SEvan Quan 		return -EINVAL;
187e098bc96SEvan Quan 
188e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
189e098bc96SEvan Quan 	if (ret < 0) {
190e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
191e098bc96SEvan Quan 		return ret;
192e098bc96SEvan Quan 	}
193e098bc96SEvan Quan 
19479c65f3fSEvan Quan 	amdgpu_dpm_set_power_state(adev, state);
195e098bc96SEvan Quan 
196e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
197e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
198e098bc96SEvan Quan 
199e098bc96SEvan Quan 	return count;
200e098bc96SEvan Quan }
201e098bc96SEvan Quan 
202e098bc96SEvan Quan 
203e098bc96SEvan Quan /**
204e098bc96SEvan Quan  * DOC: power_dpm_force_performance_level
205e098bc96SEvan Quan  *
206e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting certain power
207e098bc96SEvan Quan  * related parameters.  The file power_dpm_force_performance_level is
208e098bc96SEvan Quan  * used for this.  It accepts the following arguments:
209e098bc96SEvan Quan  *
210e098bc96SEvan Quan  * - auto
211e098bc96SEvan Quan  *
212e098bc96SEvan Quan  * - low
213e098bc96SEvan Quan  *
214e098bc96SEvan Quan  * - high
215e098bc96SEvan Quan  *
216e098bc96SEvan Quan  * - manual
217e098bc96SEvan Quan  *
218e098bc96SEvan Quan  * - profile_standard
219e098bc96SEvan Quan  *
220e098bc96SEvan Quan  * - profile_min_sclk
221e098bc96SEvan Quan  *
222e098bc96SEvan Quan  * - profile_min_mclk
223e098bc96SEvan Quan  *
224e098bc96SEvan Quan  * - profile_peak
225e098bc96SEvan Quan  *
226e098bc96SEvan Quan  * auto
227e098bc96SEvan Quan  *
228e098bc96SEvan Quan  * When auto is selected, the driver will attempt to dynamically select
229e098bc96SEvan Quan  * the optimal power profile for current conditions in the driver.
230e098bc96SEvan Quan  *
231e098bc96SEvan Quan  * low
232e098bc96SEvan Quan  *
233e098bc96SEvan Quan  * When low is selected, the clocks are forced to the lowest power state.
234e098bc96SEvan Quan  *
235e098bc96SEvan Quan  * high
236e098bc96SEvan Quan  *
237e098bc96SEvan Quan  * When high is selected, the clocks are forced to the highest power state.
238e098bc96SEvan Quan  *
239e098bc96SEvan Quan  * manual
240e098bc96SEvan Quan  *
241e098bc96SEvan Quan  * When manual is selected, the user can manually adjust which power states
242e098bc96SEvan Quan  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
243e098bc96SEvan Quan  * and pp_dpm_pcie files and adjust the power state transition heuristics
244e098bc96SEvan Quan  * via the pp_power_profile_mode sysfs file.
245e098bc96SEvan Quan  *
246e098bc96SEvan Quan  * profile_standard
247e098bc96SEvan Quan  * profile_min_sclk
248e098bc96SEvan Quan  * profile_min_mclk
249e098bc96SEvan Quan  * profile_peak
250e098bc96SEvan Quan  *
251e098bc96SEvan Quan  * When the profiling modes are selected, clock and power gating are
252e098bc96SEvan Quan  * disabled and the clocks are set for different profiling cases. This
253e098bc96SEvan Quan  * mode is recommended for profiling specific work loads where you do
254e098bc96SEvan Quan  * not want clock or power gating for clock fluctuation to interfere
255e098bc96SEvan Quan  * with your results. profile_standard sets the clocks to a fixed clock
256e098bc96SEvan Quan  * level which varies from asic to asic.  profile_min_sclk forces the sclk
257e098bc96SEvan Quan  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
258e098bc96SEvan Quan  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
259e098bc96SEvan Quan  *
260e098bc96SEvan Quan  */
261e098bc96SEvan Quan 
262e098bc96SEvan Quan static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
263e098bc96SEvan Quan 							    struct device_attribute *attr,
264e098bc96SEvan Quan 							    char *buf)
265e098bc96SEvan Quan {
266e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
2671348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
268e098bc96SEvan Quan 	enum amd_dpm_forced_level level = 0xff;
269e098bc96SEvan Quan 	int ret;
270e098bc96SEvan Quan 
27153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
272e098bc96SEvan Quan 		return -EPERM;
273d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
274d2ae842dSAlex Deucher 		return -EPERM;
275e098bc96SEvan Quan 
276e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
277e098bc96SEvan Quan 	if (ret < 0) {
278e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
279e098bc96SEvan Quan 		return ret;
280e098bc96SEvan Quan 	}
281e098bc96SEvan Quan 
282e098bc96SEvan Quan 	level = amdgpu_dpm_get_performance_level(adev);
283e098bc96SEvan Quan 
284e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
285e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
286e098bc96SEvan Quan 
287a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n",
288e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
289e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
290e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
291e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
292e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
293e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
294e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
295e098bc96SEvan Quan 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
2966be64246SLijo Lazar 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
297e098bc96SEvan Quan 			  "unknown");
298e098bc96SEvan Quan }
299e098bc96SEvan Quan 
300e098bc96SEvan Quan static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
301e098bc96SEvan Quan 							    struct device_attribute *attr,
302e098bc96SEvan Quan 							    const char *buf,
303e098bc96SEvan Quan 							    size_t count)
304e098bc96SEvan Quan {
305e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3061348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
307e098bc96SEvan Quan 	enum amd_dpm_forced_level level;
308e098bc96SEvan Quan 	int ret = 0;
309e098bc96SEvan Quan 
31053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
311e098bc96SEvan Quan 		return -EPERM;
312d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
313d2ae842dSAlex Deucher 		return -EPERM;
314e098bc96SEvan Quan 
315e098bc96SEvan Quan 	if (strncmp("low", buf, strlen("low")) == 0) {
316e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_LOW;
317e098bc96SEvan Quan 	} else if (strncmp("high", buf, strlen("high")) == 0) {
318e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_HIGH;
319e098bc96SEvan Quan 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
320e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_AUTO;
321e098bc96SEvan Quan 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
322e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
323e098bc96SEvan Quan 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
324e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
325e098bc96SEvan Quan 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
326e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
327e098bc96SEvan Quan 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
328e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
329e098bc96SEvan Quan 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
330e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
331e098bc96SEvan Quan 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
332e098bc96SEvan Quan 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
3336be64246SLijo Lazar 	} else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
3346be64246SLijo Lazar 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
335e098bc96SEvan Quan 	}  else {
336e098bc96SEvan Quan 		return -EINVAL;
337e098bc96SEvan Quan 	}
338e098bc96SEvan Quan 
339e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
340e098bc96SEvan Quan 	if (ret < 0) {
341e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
342e098bc96SEvan Quan 		return ret;
343e098bc96SEvan Quan 	}
344e098bc96SEvan Quan 
3458cda7a4fSAlex Deucher 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
34679c65f3fSEvan Quan 	if (amdgpu_dpm_force_performance_level(adev, level)) {
347e098bc96SEvan Quan 		pm_runtime_mark_last_busy(ddev->dev);
348e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
3498cda7a4fSAlex Deucher 		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
350e098bc96SEvan Quan 		return -EINVAL;
351e098bc96SEvan Quan 	}
3528cda7a4fSAlex Deucher 	/* override whatever a user ctx may have set */
3538cda7a4fSAlex Deucher 	adev->pm.stable_pstate_ctx = NULL;
3548cda7a4fSAlex Deucher 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
35579c65f3fSEvan Quan 
356e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
357e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
358e098bc96SEvan Quan 
359e098bc96SEvan Quan 	return count;
360e098bc96SEvan Quan }
361e098bc96SEvan Quan 
362e098bc96SEvan Quan static ssize_t amdgpu_get_pp_num_states(struct device *dev,
363e098bc96SEvan Quan 		struct device_attribute *attr,
364e098bc96SEvan Quan 		char *buf)
365e098bc96SEvan Quan {
366e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
3671348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
368e098bc96SEvan Quan 	struct pp_states_info data;
36909b6744cSDarren Powell 	uint32_t i;
37009b6744cSDarren Powell 	int buf_len, ret;
371e098bc96SEvan Quan 
37253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
373e098bc96SEvan Quan 		return -EPERM;
374d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
375d2ae842dSAlex Deucher 		return -EPERM;
376e098bc96SEvan Quan 
377e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
378e098bc96SEvan Quan 	if (ret < 0) {
379e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
380e098bc96SEvan Quan 		return ret;
381e098bc96SEvan Quan 	}
382e098bc96SEvan Quan 
38379c65f3fSEvan Quan 	if (amdgpu_dpm_get_pp_num_states(adev, &data))
384e098bc96SEvan Quan 		memset(&data, 0, sizeof(data));
385e098bc96SEvan Quan 
386e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
387e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
388e098bc96SEvan Quan 
38909b6744cSDarren Powell 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
390e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++)
39109b6744cSDarren Powell 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
392e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
393e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
394e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
395e098bc96SEvan Quan 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
396e098bc96SEvan Quan 
397e098bc96SEvan Quan 	return buf_len;
398e098bc96SEvan Quan }
399e098bc96SEvan Quan 
400e098bc96SEvan Quan static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
401e098bc96SEvan Quan 		struct device_attribute *attr,
402e098bc96SEvan Quan 		char *buf)
403e098bc96SEvan Quan {
404e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4051348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
4062b24c199STom Rix 	struct pp_states_info data = {0};
407e098bc96SEvan Quan 	enum amd_pm_state_type pm = 0;
408e098bc96SEvan Quan 	int i = 0, ret = 0;
409e098bc96SEvan Quan 
41053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
411e098bc96SEvan Quan 		return -EPERM;
412d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
413d2ae842dSAlex Deucher 		return -EPERM;
414e098bc96SEvan Quan 
415e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
416e098bc96SEvan Quan 	if (ret < 0) {
417e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
418e098bc96SEvan Quan 		return ret;
419e098bc96SEvan Quan 	}
420e098bc96SEvan Quan 
42179c65f3fSEvan Quan 	amdgpu_dpm_get_current_power_state(adev, &pm);
42279c65f3fSEvan Quan 
42379c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
424e098bc96SEvan Quan 
425e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
426e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
427e098bc96SEvan Quan 
42879c65f3fSEvan Quan 	if (ret)
42979c65f3fSEvan Quan 		return ret;
43079c65f3fSEvan Quan 
431e098bc96SEvan Quan 	for (i = 0; i < data.nums; i++) {
432e098bc96SEvan Quan 		if (pm == data.states[i])
433e098bc96SEvan Quan 			break;
434e098bc96SEvan Quan 	}
435e098bc96SEvan Quan 
436e098bc96SEvan Quan 	if (i == data.nums)
437e098bc96SEvan Quan 		i = -EINVAL;
438e098bc96SEvan Quan 
439a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", i);
440e098bc96SEvan Quan }
441e098bc96SEvan Quan 
442e098bc96SEvan Quan static ssize_t amdgpu_get_pp_force_state(struct device *dev,
443e098bc96SEvan Quan 		struct device_attribute *attr,
444e098bc96SEvan Quan 		char *buf)
445e098bc96SEvan Quan {
446e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4471348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
448e098bc96SEvan Quan 
44953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
450e098bc96SEvan Quan 		return -EPERM;
451d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
452d2ae842dSAlex Deucher 		return -EPERM;
453e098bc96SEvan Quan 
454d698a2c4SEvan Quan 	if (adev->pm.pp_force_state_enabled)
455e098bc96SEvan Quan 		return amdgpu_get_pp_cur_state(dev, attr, buf);
456e098bc96SEvan Quan 	else
457a9ca9bb3STian Tao 		return sysfs_emit(buf, "\n");
458e098bc96SEvan Quan }
459e098bc96SEvan Quan 
460e098bc96SEvan Quan static ssize_t amdgpu_set_pp_force_state(struct device *dev,
461e098bc96SEvan Quan 		struct device_attribute *attr,
462e098bc96SEvan Quan 		const char *buf,
463e098bc96SEvan Quan 		size_t count)
464e098bc96SEvan Quan {
465e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
4661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
467e098bc96SEvan Quan 	enum amd_pm_state_type state = 0;
46879c65f3fSEvan Quan 	struct pp_states_info data;
469e098bc96SEvan Quan 	unsigned long idx;
470e098bc96SEvan Quan 	int ret;
471e098bc96SEvan Quan 
47253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
473e098bc96SEvan Quan 		return -EPERM;
474d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
475d2ae842dSAlex Deucher 		return -EPERM;
476e098bc96SEvan Quan 
477d698a2c4SEvan Quan 	adev->pm.pp_force_state_enabled = false;
47879c65f3fSEvan Quan 
479e098bc96SEvan Quan 	if (strlen(buf) == 1)
48079c65f3fSEvan Quan 		return count;
481e098bc96SEvan Quan 
482e098bc96SEvan Quan 	ret = kstrtoul(buf, 0, &idx);
483e098bc96SEvan Quan 	if (ret || idx >= ARRAY_SIZE(data.states))
484e098bc96SEvan Quan 		return -EINVAL;
485e098bc96SEvan Quan 
486e098bc96SEvan Quan 	idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
487e098bc96SEvan Quan 
488e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
489e098bc96SEvan Quan 	if (ret < 0) {
490e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
491e098bc96SEvan Quan 		return ret;
492e098bc96SEvan Quan 	}
493e098bc96SEvan Quan 
49479c65f3fSEvan Quan 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
49579c65f3fSEvan Quan 	if (ret)
49679c65f3fSEvan Quan 		goto err_out;
49779c65f3fSEvan Quan 
49879c65f3fSEvan Quan 	state = data.states[idx];
49979c65f3fSEvan Quan 
500e098bc96SEvan Quan 	/* only set user selected power states */
501e098bc96SEvan Quan 	if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
502e098bc96SEvan Quan 	    state != POWER_STATE_TYPE_DEFAULT) {
50379c65f3fSEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
504e098bc96SEvan Quan 				AMD_PP_TASK_ENABLE_USER_STATE, &state);
50579c65f3fSEvan Quan 		if (ret)
50679c65f3fSEvan Quan 			goto err_out;
50779c65f3fSEvan Quan 
508d698a2c4SEvan Quan 		adev->pm.pp_force_state_enabled = true;
509e098bc96SEvan Quan 	}
51079c65f3fSEvan Quan 
511e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
512e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
513e098bc96SEvan Quan 
514e098bc96SEvan Quan 	return count;
51579c65f3fSEvan Quan 
51679c65f3fSEvan Quan err_out:
51779c65f3fSEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
51879c65f3fSEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
51979c65f3fSEvan Quan 	return ret;
520e098bc96SEvan Quan }
521e098bc96SEvan Quan 
522e098bc96SEvan Quan /**
523e098bc96SEvan Quan  * DOC: pp_table
524e098bc96SEvan Quan  *
525e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for uploading new powerplay
526e098bc96SEvan Quan  * tables.  The file pp_table is used for this.  Reading the file
527e098bc96SEvan Quan  * will dump the current power play table.  Writing to the file
528e098bc96SEvan Quan  * will attempt to upload a new powerplay table and re-initialize
529e098bc96SEvan Quan  * powerplay using that new table.
530e098bc96SEvan Quan  *
531e098bc96SEvan Quan  */
532e098bc96SEvan Quan 
533e098bc96SEvan Quan static ssize_t amdgpu_get_pp_table(struct device *dev,
534e098bc96SEvan Quan 		struct device_attribute *attr,
535e098bc96SEvan Quan 		char *buf)
536e098bc96SEvan Quan {
537e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5381348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
539e098bc96SEvan Quan 	char *table = NULL;
540e098bc96SEvan Quan 	int size, ret;
541e098bc96SEvan Quan 
54253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
543e098bc96SEvan Quan 		return -EPERM;
544d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
545d2ae842dSAlex Deucher 		return -EPERM;
546e098bc96SEvan Quan 
547e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
548e098bc96SEvan Quan 	if (ret < 0) {
549e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
550e098bc96SEvan Quan 		return ret;
551e098bc96SEvan Quan 	}
552e098bc96SEvan Quan 
553e098bc96SEvan Quan 	size = amdgpu_dpm_get_pp_table(adev, &table);
55479c65f3fSEvan Quan 
555e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
556e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
55779c65f3fSEvan Quan 
55879c65f3fSEvan Quan 	if (size <= 0)
559e098bc96SEvan Quan 		return size;
560e098bc96SEvan Quan 
561e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
562e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
563e098bc96SEvan Quan 
564e098bc96SEvan Quan 	memcpy(buf, table, size);
565e098bc96SEvan Quan 
566e098bc96SEvan Quan 	return size;
567e098bc96SEvan Quan }
568e098bc96SEvan Quan 
569e098bc96SEvan Quan static ssize_t amdgpu_set_pp_table(struct device *dev,
570e098bc96SEvan Quan 		struct device_attribute *attr,
571e098bc96SEvan Quan 		const char *buf,
572e098bc96SEvan Quan 		size_t count)
573e098bc96SEvan Quan {
574e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
5751348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
576e098bc96SEvan Quan 	int ret = 0;
577e098bc96SEvan Quan 
57853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
579e098bc96SEvan Quan 		return -EPERM;
580d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
581d2ae842dSAlex Deucher 		return -EPERM;
582e098bc96SEvan Quan 
583e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
584e098bc96SEvan Quan 	if (ret < 0) {
585e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
586e098bc96SEvan Quan 		return ret;
587e098bc96SEvan Quan 	}
588e098bc96SEvan Quan 
5898f4828d0SDarren Powell 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
590e098bc96SEvan Quan 
591e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
592e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
593e098bc96SEvan Quan 
59479c65f3fSEvan Quan 	if (ret)
59579c65f3fSEvan Quan 		return ret;
59679c65f3fSEvan Quan 
597e098bc96SEvan Quan 	return count;
598e098bc96SEvan Quan }
599e098bc96SEvan Quan 
600e098bc96SEvan Quan /**
601e098bc96SEvan Quan  * DOC: pp_od_clk_voltage
602e098bc96SEvan Quan  *
603e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
604e098bc96SEvan Quan  * in each power level within a power state.  The pp_od_clk_voltage is used for
605e098bc96SEvan Quan  * this.
606e098bc96SEvan Quan  *
607e098bc96SEvan Quan  * Note that the actual memory controller clock rate are exposed, not
608e098bc96SEvan Quan  * the effective memory clock of the DRAMs. To translate it, use the
609e098bc96SEvan Quan  * following formula:
610e098bc96SEvan Quan  *
611e098bc96SEvan Quan  * Clock conversion (Mhz):
612e098bc96SEvan Quan  *
613e098bc96SEvan Quan  * HBM: effective_memory_clock = memory_controller_clock * 1
614e098bc96SEvan Quan  *
615e098bc96SEvan Quan  * G5: effective_memory_clock = memory_controller_clock * 1
616e098bc96SEvan Quan  *
617e098bc96SEvan Quan  * G6: effective_memory_clock = memory_controller_clock * 2
618e098bc96SEvan Quan  *
619e098bc96SEvan Quan  * DRAM data rate (MT/s):
620e098bc96SEvan Quan  *
621e098bc96SEvan Quan  * HBM: effective_memory_clock * 2 = data_rate
622e098bc96SEvan Quan  *
623e098bc96SEvan Quan  * G5: effective_memory_clock * 4 = data_rate
624e098bc96SEvan Quan  *
625e098bc96SEvan Quan  * G6: effective_memory_clock * 8 = data_rate
626e098bc96SEvan Quan  *
627e098bc96SEvan Quan  * Bandwidth (MB/s):
628e098bc96SEvan Quan  *
629e098bc96SEvan Quan  * data_rate * vram_bit_width / 8 = memory_bandwidth
630e098bc96SEvan Quan  *
631e098bc96SEvan Quan  * Some examples:
632e098bc96SEvan Quan  *
633e098bc96SEvan Quan  * G5 on RX460:
634e098bc96SEvan Quan  *
635e098bc96SEvan Quan  * memory_controller_clock = 1750 Mhz
636e098bc96SEvan Quan  *
637e098bc96SEvan Quan  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
638e098bc96SEvan Quan  *
639e098bc96SEvan Quan  * data rate = 1750 * 4 = 7000 MT/s
640e098bc96SEvan Quan  *
641e098bc96SEvan Quan  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
642e098bc96SEvan Quan  *
643e098bc96SEvan Quan  * G6 on RX5700:
644e098bc96SEvan Quan  *
645e098bc96SEvan Quan  * memory_controller_clock = 875 Mhz
646e098bc96SEvan Quan  *
647e098bc96SEvan Quan  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
648e098bc96SEvan Quan  *
649e098bc96SEvan Quan  * data rate = 1750 * 8 = 14000 MT/s
650e098bc96SEvan Quan  *
651e098bc96SEvan Quan  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
652e098bc96SEvan Quan  *
653e098bc96SEvan Quan  * < For Vega10 and previous ASICs >
654e098bc96SEvan Quan  *
655e098bc96SEvan Quan  * Reading the file will display:
656e098bc96SEvan Quan  *
657e098bc96SEvan Quan  * - a list of engine clock levels and voltages labeled OD_SCLK
658e098bc96SEvan Quan  *
659e098bc96SEvan Quan  * - a list of memory clock levels and voltages labeled OD_MCLK
660e098bc96SEvan Quan  *
661e098bc96SEvan Quan  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
662e098bc96SEvan Quan  *
663e098bc96SEvan Quan  * To manually adjust these settings, first select manual using
664e098bc96SEvan Quan  * power_dpm_force_performance_level. Enter a new value for each
665e098bc96SEvan Quan  * level by writing a string that contains "s/m level clock voltage" to
666e098bc96SEvan Quan  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
667e098bc96SEvan Quan  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
668e098bc96SEvan Quan  * 810 mV.  When you have edited all of the states as needed, write
669e098bc96SEvan Quan  * "c" (commit) to the file to commit your changes.  If you want to reset to the
670e098bc96SEvan Quan  * default power levels, write "r" (reset) to the file to reset them.
671e098bc96SEvan Quan  *
672e098bc96SEvan Quan  *
673e098bc96SEvan Quan  * < For Vega20 and newer ASICs >
674e098bc96SEvan Quan  *
675e098bc96SEvan Quan  * Reading the file will display:
676e098bc96SEvan Quan  *
677e098bc96SEvan Quan  * - minimum and maximum engine clock labeled OD_SCLK
678e098bc96SEvan Quan  *
67937a58f69SEvan Quan  * - minimum(not available for Vega20 and Navi1x) and maximum memory
68037a58f69SEvan Quan  *   clock labeled OD_MCLK
681e098bc96SEvan Quan  *
682e098bc96SEvan Quan  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
6838f4f5f0bSEvan Quan  *   They can be used to calibrate the sclk voltage curve. This is
6848f4f5f0bSEvan Quan  *   available for Vega20 and NV1X.
6858f4f5f0bSEvan Quan  *
686a2b6df4fSEvan Quan  * - voltage offset(in mV) applied on target voltage calculation.
687e835bc26SEvan Quan  *   This is available for Sienna Cichlid, Navy Flounder, Dimgrey
688e835bc26SEvan Quan  *   Cavefish and some later SMU13 ASICs. For these ASICs, the target
689e835bc26SEvan Quan  *   voltage calculation can be illustrated by "voltage = voltage
690e835bc26SEvan Quan  *   calculated from v/f curve + overdrive vddgfx offset"
691a2b6df4fSEvan Quan  *
692e835bc26SEvan Quan  * - a list of valid ranges for sclk, mclk, voltage curve points
693e835bc26SEvan Quan  *   or voltage offset labeled OD_RANGE
694e098bc96SEvan Quan  *
6950487bbb4SAlex Deucher  * < For APUs >
6960487bbb4SAlex Deucher  *
6970487bbb4SAlex Deucher  * Reading the file will display:
6980487bbb4SAlex Deucher  *
6990487bbb4SAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7000487bbb4SAlex Deucher  *
7010487bbb4SAlex Deucher  * - a list of valid ranges for sclk labeled OD_RANGE
7020487bbb4SAlex Deucher  *
7033dc8077fSAlex Deucher  * < For VanGogh >
7043dc8077fSAlex Deucher  *
7053dc8077fSAlex Deucher  * Reading the file will display:
7063dc8077fSAlex Deucher  *
7073dc8077fSAlex Deucher  * - minimum and maximum engine clock labeled OD_SCLK
7083dc8077fSAlex Deucher  * - minimum and maximum core clocks labeled OD_CCLK
7093dc8077fSAlex Deucher  *
7103dc8077fSAlex Deucher  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
7113dc8077fSAlex Deucher  *
712e098bc96SEvan Quan  * To manually adjust these settings:
713e098bc96SEvan Quan  *
714e098bc96SEvan Quan  * - First select manual using power_dpm_force_performance_level
715e098bc96SEvan Quan  *
716e098bc96SEvan Quan  * - For clock frequency setting, enter a new value by writing a
717e098bc96SEvan Quan  *   string that contains "s/m index clock" to the file. The index
718e098bc96SEvan Quan  *   should be 0 if to set minimum clock. And 1 if to set maximum
719e098bc96SEvan Quan  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
7203dc8077fSAlex Deucher  *   "m 1 800" will update maximum mclk to be 800Mhz. For core
7213dc8077fSAlex Deucher  *   clocks on VanGogh, the string contains "p core index clock".
7223dc8077fSAlex Deucher  *   E.g., "p 2 0 800" would set the minimum core clock on core
7233dc8077fSAlex Deucher  *   2 to 800Mhz.
724e098bc96SEvan Quan  *
725e835bc26SEvan Quan  *   For sclk voltage curve supported by Vega20 and NV1X, enter the new
726e835bc26SEvan Quan  *   values by writing a string that contains "vc point clock voltage"
727e835bc26SEvan Quan  *   to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300
728e835bc26SEvan Quan  *   600" will update point1 with clock set as 300Mhz and voltage as 600mV.
729e835bc26SEvan Quan  *   "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and
7308f4f5f0bSEvan Quan  *   voltage 1000mV.
731e098bc96SEvan Quan  *
732e835bc26SEvan Quan  *   For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey
733e835bc26SEvan Quan  *   Cavefish and some later SMU13 ASICs, enter the new value by writing a
734e835bc26SEvan Quan  *   string that contains "vo offset". E.g., "vo -10" will update the extra
735e835bc26SEvan Quan  *   voltage offset applied to the whole v/f curve line as -10mv.
736a2b6df4fSEvan Quan  *
737e098bc96SEvan Quan  * - When you have edited all of the states as needed, write "c" (commit)
738e098bc96SEvan Quan  *   to the file to commit your changes
739e098bc96SEvan Quan  *
740e098bc96SEvan Quan  * - If you want to reset to the default power levels, write "r" (reset)
741e098bc96SEvan Quan  *   to the file to reset them
742e098bc96SEvan Quan  *
743e098bc96SEvan Quan  */
744e098bc96SEvan Quan 
745e098bc96SEvan Quan static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
746e098bc96SEvan Quan 		struct device_attribute *attr,
747e098bc96SEvan Quan 		const char *buf,
748e098bc96SEvan Quan 		size_t count)
749e098bc96SEvan Quan {
750e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
7511348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
752e098bc96SEvan Quan 	int ret;
753e098bc96SEvan Quan 	uint32_t parameter_size = 0;
754e098bc96SEvan Quan 	long parameter[64];
755e098bc96SEvan Quan 	char buf_cpy[128];
756e098bc96SEvan Quan 	char *tmp_str;
757e098bc96SEvan Quan 	char *sub_str;
758e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
759e098bc96SEvan Quan 	uint32_t type;
760e098bc96SEvan Quan 
76153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
762e098bc96SEvan Quan 		return -EPERM;
763d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
764d2ae842dSAlex Deucher 		return -EPERM;
765e098bc96SEvan Quan 
76608e9ebc7SBas Nieuwenhuizen 	if (count > 127 || count == 0)
767e098bc96SEvan Quan 		return -EINVAL;
768e098bc96SEvan Quan 
769e098bc96SEvan Quan 	if (*buf == 's')
770e098bc96SEvan Quan 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
7710d90d0ddSHuang Rui 	else if (*buf == 'p')
7720d90d0ddSHuang Rui 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
773e098bc96SEvan Quan 	else if (*buf == 'm')
774e098bc96SEvan Quan 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
775e098bc96SEvan Quan 	else if (*buf == 'r')
776e098bc96SEvan Quan 		type = PP_OD_RESTORE_DEFAULT_TABLE;
777e098bc96SEvan Quan 	else if (*buf == 'c')
778e098bc96SEvan Quan 		type = PP_OD_COMMIT_DPM_TABLE;
779e098bc96SEvan Quan 	else if (!strncmp(buf, "vc", 2))
780e098bc96SEvan Quan 		type = PP_OD_EDIT_VDDC_CURVE;
781a2b6df4fSEvan Quan 	else if (!strncmp(buf, "vo", 2))
782a2b6df4fSEvan Quan 		type = PP_OD_EDIT_VDDGFX_OFFSET;
783e098bc96SEvan Quan 	else
784e098bc96SEvan Quan 		return -EINVAL;
785e098bc96SEvan Quan 
78608e9ebc7SBas Nieuwenhuizen 	memcpy(buf_cpy, buf, count);
78708e9ebc7SBas Nieuwenhuizen 	buf_cpy[count] = 0;
788e098bc96SEvan Quan 
789e098bc96SEvan Quan 	tmp_str = buf_cpy;
790e098bc96SEvan Quan 
791a2b6df4fSEvan Quan 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
792a2b6df4fSEvan Quan 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
793e098bc96SEvan Quan 		tmp_str++;
794e098bc96SEvan Quan 	while (isspace(*++tmp_str));
795e098bc96SEvan Quan 
796ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
797aec1d870SMatt Coffin 		if (strlen(sub_str) == 0)
798aec1d870SMatt Coffin 			continue;
799e098bc96SEvan Quan 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
800e098bc96SEvan Quan 		if (ret)
801e098bc96SEvan Quan 			return -EINVAL;
802e098bc96SEvan Quan 		parameter_size++;
803e098bc96SEvan Quan 
80408e9ebc7SBas Nieuwenhuizen 		if (!tmp_str)
80508e9ebc7SBas Nieuwenhuizen 			break;
80608e9ebc7SBas Nieuwenhuizen 
807e098bc96SEvan Quan 		while (isspace(*tmp_str))
808e098bc96SEvan Quan 			tmp_str++;
809e098bc96SEvan Quan 	}
810e098bc96SEvan Quan 
811e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
812e098bc96SEvan Quan 	if (ret < 0) {
813e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
814e098bc96SEvan Quan 		return ret;
815e098bc96SEvan Quan 	}
816e098bc96SEvan Quan 
81779c65f3fSEvan Quan 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
81879c65f3fSEvan Quan 					      type,
81912a6727dSXiaojian Du 					      parameter,
82079c65f3fSEvan Quan 					      parameter_size))
82179c65f3fSEvan Quan 		goto err_out;
82212a6727dSXiaojian Du 
82379c65f3fSEvan Quan 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
82479c65f3fSEvan Quan 					  parameter, parameter_size))
82579c65f3fSEvan Quan 		goto err_out;
826e098bc96SEvan Quan 
827e098bc96SEvan Quan 	if (type == PP_OD_COMMIT_DPM_TABLE) {
82879c65f3fSEvan Quan 		if (amdgpu_dpm_dispatch_task(adev,
829e098bc96SEvan Quan 					     AMD_PP_TASK_READJUST_POWER_STATE,
83079c65f3fSEvan Quan 					     NULL))
83179c65f3fSEvan Quan 			goto err_out;
83279c65f3fSEvan Quan 	}
83379c65f3fSEvan Quan 
834e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
835e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
83679c65f3fSEvan Quan 
837e098bc96SEvan Quan 	return count;
83879c65f3fSEvan Quan 
83979c65f3fSEvan Quan err_out:
840e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
841e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
842e098bc96SEvan Quan 	return -EINVAL;
843e098bc96SEvan Quan }
844e098bc96SEvan Quan 
845e098bc96SEvan Quan static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
846e098bc96SEvan Quan 		struct device_attribute *attr,
847e098bc96SEvan Quan 		char *buf)
848e098bc96SEvan Quan {
849e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
8501348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
851c8cb19c7SDarren Powell 	int size = 0;
852e098bc96SEvan Quan 	int ret;
853c8cb19c7SDarren Powell 	enum pp_clock_type od_clocks[6] = {
854c8cb19c7SDarren Powell 		OD_SCLK,
855c8cb19c7SDarren Powell 		OD_MCLK,
856c8cb19c7SDarren Powell 		OD_VDDC_CURVE,
857c8cb19c7SDarren Powell 		OD_RANGE,
858c8cb19c7SDarren Powell 		OD_VDDGFX_OFFSET,
859c8cb19c7SDarren Powell 		OD_CCLK,
860c8cb19c7SDarren Powell 	};
861c8cb19c7SDarren Powell 	uint clk_index;
862e098bc96SEvan Quan 
86353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
864e098bc96SEvan Quan 		return -EPERM;
865d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
866d2ae842dSAlex Deucher 		return -EPERM;
867e098bc96SEvan Quan 
868e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
869e098bc96SEvan Quan 	if (ret < 0) {
870e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
871e098bc96SEvan Quan 		return ret;
872e098bc96SEvan Quan 	}
873e098bc96SEvan Quan 
874c8cb19c7SDarren Powell 	for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
875c8cb19c7SDarren Powell 		ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
876c8cb19c7SDarren Powell 		if (ret)
877c8cb19c7SDarren Powell 			break;
878c8cb19c7SDarren Powell 	}
879c8cb19c7SDarren Powell 	if (ret == -ENOENT) {
880e098bc96SEvan Quan 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
881e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
882e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
8838f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
884e098bc96SEvan Quan 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
8858f4828d0SDarren Powell 		size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
886e098bc96SEvan Quan 	}
887c8cb19c7SDarren Powell 
888c8cb19c7SDarren Powell 	if (size == 0)
889c8cb19c7SDarren Powell 		size = sysfs_emit(buf, "\n");
890c8cb19c7SDarren Powell 
891e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
892e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
893e098bc96SEvan Quan 
894e098bc96SEvan Quan 	return size;
895e098bc96SEvan Quan }
896e098bc96SEvan Quan 
897e098bc96SEvan Quan /**
898e098bc96SEvan Quan  * DOC: pp_features
899e098bc96SEvan Quan  *
900e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what powerplay
901e098bc96SEvan Quan  * features to be enabled. The file pp_features is used for this. And
902e098bc96SEvan Quan  * this is only available for Vega10 and later dGPUs.
903e098bc96SEvan Quan  *
904e098bc96SEvan Quan  * Reading back the file will show you the followings:
905e098bc96SEvan Quan  * - Current ppfeature masks
906e098bc96SEvan Quan  * - List of the all supported powerplay features with their naming,
907e098bc96SEvan Quan  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
908e098bc96SEvan Quan  *
909e098bc96SEvan Quan  * To manually enable or disable a specific feature, just set or clear
910e098bc96SEvan Quan  * the corresponding bit from original ppfeature masks and input the
911e098bc96SEvan Quan  * new ppfeature masks.
912e098bc96SEvan Quan  */
913e098bc96SEvan Quan static ssize_t amdgpu_set_pp_features(struct device *dev,
914e098bc96SEvan Quan 				      struct device_attribute *attr,
915e098bc96SEvan Quan 				      const char *buf,
916e098bc96SEvan Quan 				      size_t count)
917e098bc96SEvan Quan {
918e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9191348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
920e098bc96SEvan Quan 	uint64_t featuremask;
921e098bc96SEvan Quan 	int ret;
922e098bc96SEvan Quan 
92353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
924e098bc96SEvan Quan 		return -EPERM;
925d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
926d2ae842dSAlex Deucher 		return -EPERM;
927e098bc96SEvan Quan 
928e098bc96SEvan Quan 	ret = kstrtou64(buf, 0, &featuremask);
929e098bc96SEvan Quan 	if (ret)
930e098bc96SEvan Quan 		return -EINVAL;
931e098bc96SEvan Quan 
932e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
933e098bc96SEvan Quan 	if (ret < 0) {
934e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
935e098bc96SEvan Quan 		return ret;
936e098bc96SEvan Quan 	}
937e098bc96SEvan Quan 
938e098bc96SEvan Quan 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
93979c65f3fSEvan Quan 
940e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
941e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
94279c65f3fSEvan Quan 
94379c65f3fSEvan Quan 	if (ret)
944e098bc96SEvan Quan 		return -EINVAL;
945e098bc96SEvan Quan 
946e098bc96SEvan Quan 	return count;
947e098bc96SEvan Quan }
948e098bc96SEvan Quan 
949e098bc96SEvan Quan static ssize_t amdgpu_get_pp_features(struct device *dev,
950e098bc96SEvan Quan 				      struct device_attribute *attr,
951e098bc96SEvan Quan 				      char *buf)
952e098bc96SEvan Quan {
953e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
9541348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
955e098bc96SEvan Quan 	ssize_t size;
956e098bc96SEvan Quan 	int ret;
957e098bc96SEvan Quan 
95853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
959e098bc96SEvan Quan 		return -EPERM;
960d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
961d2ae842dSAlex Deucher 		return -EPERM;
962e098bc96SEvan Quan 
963e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
964e098bc96SEvan Quan 	if (ret < 0) {
965e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
966e098bc96SEvan Quan 		return ret;
967e098bc96SEvan Quan 	}
968e098bc96SEvan Quan 
969e098bc96SEvan Quan 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
97079c65f3fSEvan Quan 	if (size <= 0)
97109b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
972e098bc96SEvan Quan 
973e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
974e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
975e098bc96SEvan Quan 
976e098bc96SEvan Quan 	return size;
977e098bc96SEvan Quan }
978e098bc96SEvan Quan 
979e098bc96SEvan Quan /**
980e098bc96SEvan Quan  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
981e098bc96SEvan Quan  *
982e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting what power levels
983e098bc96SEvan Quan  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
984e098bc96SEvan Quan  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
985e098bc96SEvan Quan  * this.
986e098bc96SEvan Quan  *
987e098bc96SEvan Quan  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
988e098bc96SEvan Quan  * Vega10 and later ASICs.
989e098bc96SEvan Quan  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
990e098bc96SEvan Quan  *
991e098bc96SEvan Quan  * Reading back the files will show you the available power levels within
992615585d0SLijo Lazar  * the power state and the clock information for those levels. If deep sleep is
993615585d0SLijo Lazar  * applied to a clock, the level will be denoted by a special level 'S:'
994bb619539SHunter Chasens  * E.g., ::
995bb619539SHunter Chasens  *
996615585d0SLijo Lazar  *  S: 19Mhz *
997615585d0SLijo Lazar  *  0: 615Mhz
998615585d0SLijo Lazar  *  1: 800Mhz
999615585d0SLijo Lazar  *  2: 888Mhz
1000615585d0SLijo Lazar  *  3: 1000Mhz
1001615585d0SLijo Lazar  *
1002e098bc96SEvan Quan  *
1003e098bc96SEvan Quan  * To manually adjust these states, first select manual using
1004e098bc96SEvan Quan  * power_dpm_force_performance_level.
1005e098bc96SEvan Quan  * Secondly, enter a new value for each level by inputing a string that
1006e098bc96SEvan Quan  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1007e098bc96SEvan Quan  * E.g.,
1008e098bc96SEvan Quan  *
1009e098bc96SEvan Quan  * .. code-block:: bash
1010e098bc96SEvan Quan  *
1011e098bc96SEvan Quan  *	echo "4 5 6" > pp_dpm_sclk
1012e098bc96SEvan Quan  *
1013e098bc96SEvan Quan  * will enable sclk levels 4, 5, and 6.
1014e098bc96SEvan Quan  *
1015e098bc96SEvan Quan  * NOTE: change to the dcefclk max dpm level is not supported now
1016e098bc96SEvan Quan  */
1017e098bc96SEvan Quan 
10182ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
10192ea092e5SDarren Powell 		enum pp_clock_type type,
1020e098bc96SEvan Quan 		char *buf)
1021e098bc96SEvan Quan {
1022e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10231348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1024c8cb19c7SDarren Powell 	int size = 0;
1025c8cb19c7SDarren Powell 	int ret = 0;
1026e098bc96SEvan Quan 
102753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1028e098bc96SEvan Quan 		return -EPERM;
1029d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1030d2ae842dSAlex Deucher 		return -EPERM;
1031e098bc96SEvan Quan 
1032e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1033e098bc96SEvan Quan 	if (ret < 0) {
1034e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1035e098bc96SEvan Quan 		return ret;
1036e098bc96SEvan Quan 	}
1037e098bc96SEvan Quan 
1038c8cb19c7SDarren Powell 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1039c8cb19c7SDarren Powell 	if (ret == -ENOENT)
10402ea092e5SDarren Powell 		size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1041c8cb19c7SDarren Powell 
1042c8cb19c7SDarren Powell 	if (size == 0)
104309b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1044e098bc96SEvan Quan 
1045e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1046e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1047e098bc96SEvan Quan 
1048e098bc96SEvan Quan 	return size;
1049e098bc96SEvan Quan }
1050e098bc96SEvan Quan 
1051e098bc96SEvan Quan /*
1052e098bc96SEvan Quan  * Worst case: 32 bits individually specified, in octal at 12 characters
1053e098bc96SEvan Quan  * per line (+1 for \n).
1054e098bc96SEvan Quan  */
1055e098bc96SEvan Quan #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1056e098bc96SEvan Quan 
1057e098bc96SEvan Quan static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1058e098bc96SEvan Quan {
1059e098bc96SEvan Quan 	int ret;
1060c915ef89SDan Carpenter 	unsigned long level;
1061e098bc96SEvan Quan 	char *sub_str = NULL;
1062e098bc96SEvan Quan 	char *tmp;
1063e098bc96SEvan Quan 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1064e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1065e098bc96SEvan Quan 	size_t bytes;
1066e098bc96SEvan Quan 
1067e098bc96SEvan Quan 	*mask = 0;
1068e098bc96SEvan Quan 
1069e098bc96SEvan Quan 	bytes = min(count, sizeof(buf_cpy) - 1);
1070e098bc96SEvan Quan 	memcpy(buf_cpy, buf, bytes);
1071e098bc96SEvan Quan 	buf_cpy[bytes] = '\0';
1072e098bc96SEvan Quan 	tmp = buf_cpy;
1073ce7c1d04SEvan Quan 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1074e098bc96SEvan Quan 		if (strlen(sub_str)) {
1075c915ef89SDan Carpenter 			ret = kstrtoul(sub_str, 0, &level);
1076c915ef89SDan Carpenter 			if (ret || level > 31)
1077e098bc96SEvan Quan 				return -EINVAL;
1078e098bc96SEvan Quan 			*mask |= 1 << level;
1079e098bc96SEvan Quan 		} else
1080e098bc96SEvan Quan 			break;
1081e098bc96SEvan Quan 	}
1082e098bc96SEvan Quan 
1083e098bc96SEvan Quan 	return 0;
1084e098bc96SEvan Quan }
1085e098bc96SEvan Quan 
10862ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
10872ea092e5SDarren Powell 		enum pp_clock_type type,
1088e098bc96SEvan Quan 		const char *buf,
1089e098bc96SEvan Quan 		size_t count)
1090e098bc96SEvan Quan {
1091e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
10921348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1093e098bc96SEvan Quan 	int ret;
1094e098bc96SEvan Quan 	uint32_t mask = 0;
1095e098bc96SEvan Quan 
109653b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1097e098bc96SEvan Quan 		return -EPERM;
1098d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1099d2ae842dSAlex Deucher 		return -EPERM;
1100e098bc96SEvan Quan 
1101e098bc96SEvan Quan 	ret = amdgpu_read_mask(buf, count, &mask);
1102e098bc96SEvan Quan 	if (ret)
1103e098bc96SEvan Quan 		return ret;
1104e098bc96SEvan Quan 
1105e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1106e098bc96SEvan Quan 	if (ret < 0) {
1107e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1108e098bc96SEvan Quan 		return ret;
1109e098bc96SEvan Quan 	}
1110e098bc96SEvan Quan 
11112ea092e5SDarren Powell 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1112e098bc96SEvan Quan 
1113e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1114e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1115e098bc96SEvan Quan 
1116e098bc96SEvan Quan 	if (ret)
1117e098bc96SEvan Quan 		return -EINVAL;
1118e098bc96SEvan Quan 
1119e098bc96SEvan Quan 	return count;
1120e098bc96SEvan Quan }
1121e098bc96SEvan Quan 
11222ea092e5SDarren Powell static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
11232ea092e5SDarren Powell 		struct device_attribute *attr,
11242ea092e5SDarren Powell 		char *buf)
11252ea092e5SDarren Powell {
11262ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
11272ea092e5SDarren Powell }
11282ea092e5SDarren Powell 
11292ea092e5SDarren Powell static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
11302ea092e5SDarren Powell 		struct device_attribute *attr,
11312ea092e5SDarren Powell 		const char *buf,
11322ea092e5SDarren Powell 		size_t count)
11332ea092e5SDarren Powell {
11342ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
11352ea092e5SDarren Powell }
11362ea092e5SDarren Powell 
1137e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1138e098bc96SEvan Quan 		struct device_attribute *attr,
1139e098bc96SEvan Quan 		char *buf)
1140e098bc96SEvan Quan {
11412ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1142e098bc96SEvan Quan }
1143e098bc96SEvan Quan 
1144e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1145e098bc96SEvan Quan 		struct device_attribute *attr,
1146e098bc96SEvan Quan 		const char *buf,
1147e098bc96SEvan Quan 		size_t count)
1148e098bc96SEvan Quan {
11492ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1150e098bc96SEvan Quan }
1151e098bc96SEvan Quan 
1152e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1153e098bc96SEvan Quan 		struct device_attribute *attr,
1154e098bc96SEvan Quan 		char *buf)
1155e098bc96SEvan Quan {
11562ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1157e098bc96SEvan Quan }
1158e098bc96SEvan Quan 
1159e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1160e098bc96SEvan Quan 		struct device_attribute *attr,
1161e098bc96SEvan Quan 		const char *buf,
1162e098bc96SEvan Quan 		size_t count)
1163e098bc96SEvan Quan {
11642ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1165e098bc96SEvan Quan }
1166e098bc96SEvan Quan 
1167e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1168e098bc96SEvan Quan 		struct device_attribute *attr,
1169e098bc96SEvan Quan 		char *buf)
1170e098bc96SEvan Quan {
11712ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1172e098bc96SEvan Quan }
1173e098bc96SEvan Quan 
1174e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1175e098bc96SEvan Quan 		struct device_attribute *attr,
1176e098bc96SEvan Quan 		const char *buf,
1177e098bc96SEvan Quan 		size_t count)
1178e098bc96SEvan Quan {
11792ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1180e098bc96SEvan Quan }
1181e098bc96SEvan Quan 
11829577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
11839577b0ecSXiaojian Du 		struct device_attribute *attr,
11849577b0ecSXiaojian Du 		char *buf)
11859577b0ecSXiaojian Du {
11862ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
11879577b0ecSXiaojian Du }
11889577b0ecSXiaojian Du 
11899577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
11909577b0ecSXiaojian Du 		struct device_attribute *attr,
11919577b0ecSXiaojian Du 		const char *buf,
11929577b0ecSXiaojian Du 		size_t count)
11939577b0ecSXiaojian Du {
11942ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
11959577b0ecSXiaojian Du }
11969577b0ecSXiaojian Du 
1197d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1198d7001e72STong Liu01 		struct device_attribute *attr,
1199d7001e72STong Liu01 		char *buf)
1200d7001e72STong Liu01 {
1201d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1202d7001e72STong Liu01 }
1203d7001e72STong Liu01 
1204d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1205d7001e72STong Liu01 		struct device_attribute *attr,
1206d7001e72STong Liu01 		const char *buf,
1207d7001e72STong Liu01 		size_t count)
1208d7001e72STong Liu01 {
1209d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1210d7001e72STong Liu01 }
1211d7001e72STong Liu01 
12129577b0ecSXiaojian Du static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
12139577b0ecSXiaojian Du 		struct device_attribute *attr,
12149577b0ecSXiaojian Du 		char *buf)
12159577b0ecSXiaojian Du {
12162ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
12179577b0ecSXiaojian Du }
12189577b0ecSXiaojian Du 
12199577b0ecSXiaojian Du static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
12209577b0ecSXiaojian Du 		struct device_attribute *attr,
12219577b0ecSXiaojian Du 		const char *buf,
12229577b0ecSXiaojian Du 		size_t count)
12239577b0ecSXiaojian Du {
12242ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
12259577b0ecSXiaojian Du }
12269577b0ecSXiaojian Du 
1227d7001e72STong Liu01 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1228d7001e72STong Liu01 		struct device_attribute *attr,
1229d7001e72STong Liu01 		char *buf)
1230d7001e72STong Liu01 {
1231d7001e72STong Liu01 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1232d7001e72STong Liu01 }
1233d7001e72STong Liu01 
1234d7001e72STong Liu01 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1235d7001e72STong Liu01 		struct device_attribute *attr,
1236d7001e72STong Liu01 		const char *buf,
1237d7001e72STong Liu01 		size_t count)
1238d7001e72STong Liu01 {
1239d7001e72STong Liu01 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1240d7001e72STong Liu01 }
1241d7001e72STong Liu01 
1242e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1243e098bc96SEvan Quan 		struct device_attribute *attr,
1244e098bc96SEvan Quan 		char *buf)
1245e098bc96SEvan Quan {
12462ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1247e098bc96SEvan Quan }
1248e098bc96SEvan Quan 
1249e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1250e098bc96SEvan Quan 		struct device_attribute *attr,
1251e098bc96SEvan Quan 		const char *buf,
1252e098bc96SEvan Quan 		size_t count)
1253e098bc96SEvan Quan {
12542ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1255e098bc96SEvan Quan }
1256e098bc96SEvan Quan 
1257e098bc96SEvan Quan static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1258e098bc96SEvan Quan 		struct device_attribute *attr,
1259e098bc96SEvan Quan 		char *buf)
1260e098bc96SEvan Quan {
12612ea092e5SDarren Powell 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1262e098bc96SEvan Quan }
1263e098bc96SEvan Quan 
1264e098bc96SEvan Quan static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1265e098bc96SEvan Quan 		struct device_attribute *attr,
1266e098bc96SEvan Quan 		const char *buf,
1267e098bc96SEvan Quan 		size_t count)
1268e098bc96SEvan Quan {
12692ea092e5SDarren Powell 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1270e098bc96SEvan Quan }
1271e098bc96SEvan Quan 
1272e098bc96SEvan Quan static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1273e098bc96SEvan Quan 		struct device_attribute *attr,
1274e098bc96SEvan Quan 		char *buf)
1275e098bc96SEvan Quan {
1276e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
12771348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1278e098bc96SEvan Quan 	uint32_t value = 0;
1279e098bc96SEvan Quan 	int ret;
1280e098bc96SEvan Quan 
128153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1282e098bc96SEvan Quan 		return -EPERM;
1283d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1284d2ae842dSAlex Deucher 		return -EPERM;
1285e098bc96SEvan Quan 
1286e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1287e098bc96SEvan Quan 	if (ret < 0) {
1288e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1289e098bc96SEvan Quan 		return ret;
1290e098bc96SEvan Quan 	}
1291e098bc96SEvan Quan 
1292e098bc96SEvan Quan 	value = amdgpu_dpm_get_sclk_od(adev);
1293e098bc96SEvan Quan 
1294e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1295e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1296e098bc96SEvan Quan 
1297a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1298e098bc96SEvan Quan }
1299e098bc96SEvan Quan 
1300e098bc96SEvan Quan static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1301e098bc96SEvan Quan 		struct device_attribute *attr,
1302e098bc96SEvan Quan 		const char *buf,
1303e098bc96SEvan Quan 		size_t count)
1304e098bc96SEvan Quan {
1305e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13061348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1307e098bc96SEvan Quan 	int ret;
1308e098bc96SEvan Quan 	long int value;
1309e098bc96SEvan Quan 
131053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1311e098bc96SEvan Quan 		return -EPERM;
1312d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1313d2ae842dSAlex Deucher 		return -EPERM;
1314e098bc96SEvan Quan 
1315e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1316e098bc96SEvan Quan 
1317e098bc96SEvan Quan 	if (ret)
1318e098bc96SEvan Quan 		return -EINVAL;
1319e098bc96SEvan Quan 
1320e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1321e098bc96SEvan Quan 	if (ret < 0) {
1322e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1323e098bc96SEvan Quan 		return ret;
1324e098bc96SEvan Quan 	}
1325e098bc96SEvan Quan 
1326e098bc96SEvan Quan 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1327e098bc96SEvan Quan 
1328e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1329e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1330e098bc96SEvan Quan 
1331e098bc96SEvan Quan 	return count;
1332e098bc96SEvan Quan }
1333e098bc96SEvan Quan 
1334e098bc96SEvan Quan static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1335e098bc96SEvan Quan 		struct device_attribute *attr,
1336e098bc96SEvan Quan 		char *buf)
1337e098bc96SEvan Quan {
1338e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13391348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1340e098bc96SEvan Quan 	uint32_t value = 0;
1341e098bc96SEvan Quan 	int ret;
1342e098bc96SEvan Quan 
134353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1344e098bc96SEvan Quan 		return -EPERM;
1345d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1346d2ae842dSAlex Deucher 		return -EPERM;
1347e098bc96SEvan Quan 
1348e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1349e098bc96SEvan Quan 	if (ret < 0) {
1350e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1351e098bc96SEvan Quan 		return ret;
1352e098bc96SEvan Quan 	}
1353e098bc96SEvan Quan 
1354e098bc96SEvan Quan 	value = amdgpu_dpm_get_mclk_od(adev);
1355e098bc96SEvan Quan 
1356e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1357e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1358e098bc96SEvan Quan 
1359a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1360e098bc96SEvan Quan }
1361e098bc96SEvan Quan 
1362e098bc96SEvan Quan static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1363e098bc96SEvan Quan 		struct device_attribute *attr,
1364e098bc96SEvan Quan 		const char *buf,
1365e098bc96SEvan Quan 		size_t count)
1366e098bc96SEvan Quan {
1367e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
13681348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1369e098bc96SEvan Quan 	int ret;
1370e098bc96SEvan Quan 	long int value;
1371e098bc96SEvan Quan 
137253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1373e098bc96SEvan Quan 		return -EPERM;
1374d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1375d2ae842dSAlex Deucher 		return -EPERM;
1376e098bc96SEvan Quan 
1377e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &value);
1378e098bc96SEvan Quan 
1379e098bc96SEvan Quan 	if (ret)
1380e098bc96SEvan Quan 		return -EINVAL;
1381e098bc96SEvan Quan 
1382e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1383e098bc96SEvan Quan 	if (ret < 0) {
1384e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1385e098bc96SEvan Quan 		return ret;
1386e098bc96SEvan Quan 	}
1387e098bc96SEvan Quan 
1388e098bc96SEvan Quan 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1389e098bc96SEvan Quan 
1390e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1391e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1392e098bc96SEvan Quan 
1393e098bc96SEvan Quan 	return count;
1394e098bc96SEvan Quan }
1395e098bc96SEvan Quan 
1396e098bc96SEvan Quan /**
1397e098bc96SEvan Quan  * DOC: pp_power_profile_mode
1398e098bc96SEvan Quan  *
1399e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1400e098bc96SEvan Quan  * related to switching between power levels in a power state.  The file
1401e098bc96SEvan Quan  * pp_power_profile_mode is used for this.
1402e098bc96SEvan Quan  *
1403e098bc96SEvan Quan  * Reading this file outputs a list of all of the predefined power profiles
1404e098bc96SEvan Quan  * and the relevant heuristics settings for that profile.
1405e098bc96SEvan Quan  *
1406e098bc96SEvan Quan  * To select a profile or create a custom profile, first select manual using
1407e098bc96SEvan Quan  * power_dpm_force_performance_level.  Writing the number of a predefined
1408e098bc96SEvan Quan  * profile to pp_power_profile_mode will enable those heuristics.  To
1409e098bc96SEvan Quan  * create a custom set of heuristics, write a string of numbers to the file
1410e098bc96SEvan Quan  * starting with the number of the custom profile along with a setting
1411e098bc96SEvan Quan  * for each heuristic parameter.  Due to differences across asic families
1412e098bc96SEvan Quan  * the heuristic parameters vary from family to family.
1413e098bc96SEvan Quan  *
1414e098bc96SEvan Quan  */
1415e098bc96SEvan Quan 
1416e098bc96SEvan Quan static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1417e098bc96SEvan Quan 		struct device_attribute *attr,
1418e098bc96SEvan Quan 		char *buf)
1419e098bc96SEvan Quan {
1420e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14211348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1422e098bc96SEvan Quan 	ssize_t size;
1423e098bc96SEvan Quan 	int ret;
1424e098bc96SEvan Quan 
142553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1426e098bc96SEvan Quan 		return -EPERM;
1427d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1428d2ae842dSAlex Deucher 		return -EPERM;
1429e098bc96SEvan Quan 
1430e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1431e098bc96SEvan Quan 	if (ret < 0) {
1432e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1433e098bc96SEvan Quan 		return ret;
1434e098bc96SEvan Quan 	}
1435e098bc96SEvan Quan 
1436e098bc96SEvan Quan 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
143779c65f3fSEvan Quan 	if (size <= 0)
143809b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
1439e098bc96SEvan Quan 
1440e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1441e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1442e098bc96SEvan Quan 
1443e098bc96SEvan Quan 	return size;
1444e098bc96SEvan Quan }
1445e098bc96SEvan Quan 
1446e098bc96SEvan Quan 
1447e098bc96SEvan Quan static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1448e098bc96SEvan Quan 		struct device_attribute *attr,
1449e098bc96SEvan Quan 		const char *buf,
1450e098bc96SEvan Quan 		size_t count)
1451e098bc96SEvan Quan {
1452e098bc96SEvan Quan 	int ret;
1453e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
14541348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1455e098bc96SEvan Quan 	uint32_t parameter_size = 0;
1456e098bc96SEvan Quan 	long parameter[64];
1457e098bc96SEvan Quan 	char *sub_str, buf_cpy[128];
1458e098bc96SEvan Quan 	char *tmp_str;
1459e098bc96SEvan Quan 	uint32_t i = 0;
1460e098bc96SEvan Quan 	char tmp[2];
1461e098bc96SEvan Quan 	long int profile_mode = 0;
1462e098bc96SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
1463e098bc96SEvan Quan 
146453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1465e098bc96SEvan Quan 		return -EPERM;
1466d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1467d2ae842dSAlex Deucher 		return -EPERM;
1468e098bc96SEvan Quan 
1469e098bc96SEvan Quan 	tmp[0] = *(buf);
1470e098bc96SEvan Quan 	tmp[1] = '\0';
1471e098bc96SEvan Quan 	ret = kstrtol(tmp, 0, &profile_mode);
1472e098bc96SEvan Quan 	if (ret)
1473e098bc96SEvan Quan 		return -EINVAL;
1474e098bc96SEvan Quan 
1475e098bc96SEvan Quan 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1476e098bc96SEvan Quan 		if (count < 2 || count > 127)
1477e098bc96SEvan Quan 			return -EINVAL;
1478e098bc96SEvan Quan 		while (isspace(*++buf))
1479e098bc96SEvan Quan 			i++;
1480e098bc96SEvan Quan 		memcpy(buf_cpy, buf, count-i);
1481e098bc96SEvan Quan 		tmp_str = buf_cpy;
1482ce7c1d04SEvan Quan 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1483c2efbc3fSEvan Quan 			if (strlen(sub_str) == 0)
1484c2efbc3fSEvan Quan 				continue;
1485e098bc96SEvan Quan 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1486e098bc96SEvan Quan 			if (ret)
1487e098bc96SEvan Quan 				return -EINVAL;
1488e098bc96SEvan Quan 			parameter_size++;
1489e098bc96SEvan Quan 			while (isspace(*tmp_str))
1490e098bc96SEvan Quan 				tmp_str++;
1491e098bc96SEvan Quan 		}
1492e098bc96SEvan Quan 	}
1493e098bc96SEvan Quan 	parameter[parameter_size] = profile_mode;
1494e098bc96SEvan Quan 
1495e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1496e098bc96SEvan Quan 	if (ret < 0) {
1497e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1498e098bc96SEvan Quan 		return ret;
1499e098bc96SEvan Quan 	}
1500e098bc96SEvan Quan 
1501e098bc96SEvan Quan 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1502e098bc96SEvan Quan 
1503e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1504e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1505e098bc96SEvan Quan 
1506e098bc96SEvan Quan 	if (!ret)
1507e098bc96SEvan Quan 		return count;
1508e098bc96SEvan Quan 
1509e098bc96SEvan Quan 	return -EINVAL;
1510e098bc96SEvan Quan }
1511e098bc96SEvan Quan 
1512a5600853SAlex Deucher static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1513d78c227fSMario Limonciello 					   enum amd_pp_sensors sensor,
1514d78c227fSMario Limonciello 					   void *query)
1515d78c227fSMario Limonciello {
1516d78c227fSMario Limonciello 	int r, size = sizeof(uint32_t);
1517d78c227fSMario Limonciello 
1518d78c227fSMario Limonciello 	if (amdgpu_in_reset(adev))
1519d78c227fSMario Limonciello 		return -EPERM;
1520d78c227fSMario Limonciello 	if (adev->in_suspend && !adev->in_runpm)
1521d78c227fSMario Limonciello 		return -EPERM;
1522d78c227fSMario Limonciello 
1523d78c227fSMario Limonciello 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1524d78c227fSMario Limonciello 	if (r < 0) {
1525d78c227fSMario Limonciello 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1526d78c227fSMario Limonciello 		return r;
1527d78c227fSMario Limonciello 	}
1528d78c227fSMario Limonciello 
1529d78c227fSMario Limonciello 	/* get the sensor value */
1530d78c227fSMario Limonciello 	r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1531d78c227fSMario Limonciello 
1532d78c227fSMario Limonciello 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1533d78c227fSMario Limonciello 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1534d78c227fSMario Limonciello 
1535d78c227fSMario Limonciello 	return r;
1536d78c227fSMario Limonciello }
1537d78c227fSMario Limonciello 
1538e098bc96SEvan Quan /**
1539e098bc96SEvan Quan  * DOC: gpu_busy_percent
1540e098bc96SEvan Quan  *
1541e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1542e098bc96SEvan Quan  * is as a percentage.  The file gpu_busy_percent is used for this.
1543e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1544e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1545e098bc96SEvan Quan  */
1546e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1547e098bc96SEvan Quan 					   struct device_attribute *attr,
1548e098bc96SEvan Quan 					   char *buf)
1549e098bc96SEvan Quan {
1550e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15511348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1552d78c227fSMario Limonciello 	unsigned int value;
1553d78c227fSMario Limonciello 	int r;
1554e098bc96SEvan Quan 
1555d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1556e098bc96SEvan Quan 	if (r)
1557e098bc96SEvan Quan 		return r;
1558e098bc96SEvan Quan 
1559a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1560e098bc96SEvan Quan }
1561e098bc96SEvan Quan 
1562e098bc96SEvan Quan /**
1563e098bc96SEvan Quan  * DOC: mem_busy_percent
1564e098bc96SEvan Quan  *
1565e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1566e098bc96SEvan Quan  * is as a percentage.  The file mem_busy_percent is used for this.
1567e098bc96SEvan Quan  * The SMU firmware computes a percentage of load based on the
1568e098bc96SEvan Quan  * aggregate activity level in the IP cores.
1569e098bc96SEvan Quan  */
1570e098bc96SEvan Quan static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1571e098bc96SEvan Quan 					   struct device_attribute *attr,
1572e098bc96SEvan Quan 					   char *buf)
1573e098bc96SEvan Quan {
1574e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
15751348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1576d78c227fSMario Limonciello 	unsigned int value;
1577d78c227fSMario Limonciello 	int r;
1578e098bc96SEvan Quan 
1579d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1580e098bc96SEvan Quan 	if (r)
1581e098bc96SEvan Quan 		return r;
1582e098bc96SEvan Quan 
1583a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", value);
1584e098bc96SEvan Quan }
1585e098bc96SEvan Quan 
1586e098bc96SEvan Quan /**
1587d1b2703cSXiaojian Du  * DOC: vcn_busy_percent
1588d1b2703cSXiaojian Du  *
1589d1b2703cSXiaojian Du  * The amdgpu driver provides a sysfs API for reading how busy the VCN
1590d1b2703cSXiaojian Du  * is as a percentage.  The file vcn_busy_percent is used for this.
1591d1b2703cSXiaojian Du  * The SMU firmware computes a percentage of load based on the
1592d1b2703cSXiaojian Du  * aggregate activity level in the IP cores.
1593d1b2703cSXiaojian Du  */
1594d1b2703cSXiaojian Du static ssize_t amdgpu_get_vcn_busy_percent(struct device *dev,
1595d1b2703cSXiaojian Du 						  struct device_attribute *attr,
1596d1b2703cSXiaojian Du 						  char *buf)
1597d1b2703cSXiaojian Du {
1598d1b2703cSXiaojian Du 	struct drm_device *ddev = dev_get_drvdata(dev);
1599d1b2703cSXiaojian Du 	struct amdgpu_device *adev = drm_to_adev(ddev);
1600d1b2703cSXiaojian Du 	unsigned int value;
1601d1b2703cSXiaojian Du 	int r;
1602d1b2703cSXiaojian Du 
1603d1b2703cSXiaojian Du 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VCN_LOAD, &value);
1604d1b2703cSXiaojian Du 	if (r)
1605d1b2703cSXiaojian Du 		return r;
1606d1b2703cSXiaojian Du 
1607d1b2703cSXiaojian Du 	return sysfs_emit(buf, "%d\n", value);
1608d1b2703cSXiaojian Du }
1609d1b2703cSXiaojian Du 
1610d1b2703cSXiaojian Du /**
1611e098bc96SEvan Quan  * DOC: pcie_bw
1612e098bc96SEvan Quan  *
1613e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for estimating how much data
1614e098bc96SEvan Quan  * has been received and sent by the GPU in the last second through PCIe.
1615e098bc96SEvan Quan  * The file pcie_bw is used for this.
1616e098bc96SEvan Quan  * The Perf counters count the number of received and sent messages and return
1617e098bc96SEvan Quan  * those values, as well as the maximum payload size of a PCIe packet (mps).
1618e098bc96SEvan Quan  * Note that it is not possible to easily and quickly obtain the size of each
1619e098bc96SEvan Quan  * packet transmitted, so we output the max payload size (mps) to allow for
1620e098bc96SEvan Quan  * quick estimation of the PCIe bandwidth usage
1621e098bc96SEvan Quan  */
1622e098bc96SEvan Quan static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1623e098bc96SEvan Quan 		struct device_attribute *attr,
1624e098bc96SEvan Quan 		char *buf)
1625e098bc96SEvan Quan {
1626e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16271348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1628e098bc96SEvan Quan 	uint64_t count0 = 0, count1 = 0;
1629e098bc96SEvan Quan 	int ret;
1630e098bc96SEvan Quan 
163153b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1632e098bc96SEvan Quan 		return -EPERM;
1633d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1634d2ae842dSAlex Deucher 		return -EPERM;
1635e098bc96SEvan Quan 
1636e098bc96SEvan Quan 	if (adev->flags & AMD_IS_APU)
1637e098bc96SEvan Quan 		return -ENODATA;
1638e098bc96SEvan Quan 
1639e098bc96SEvan Quan 	if (!adev->asic_funcs->get_pcie_usage)
1640e098bc96SEvan Quan 		return -ENODATA;
1641e098bc96SEvan Quan 
1642e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1643e098bc96SEvan Quan 	if (ret < 0) {
1644e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1645e098bc96SEvan Quan 		return ret;
1646e098bc96SEvan Quan 	}
1647e098bc96SEvan Quan 
1648e098bc96SEvan Quan 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1649e098bc96SEvan Quan 
1650e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1651e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1652e098bc96SEvan Quan 
1653a9ca9bb3STian Tao 	return sysfs_emit(buf, "%llu %llu %i\n",
1654e098bc96SEvan Quan 			  count0, count1, pcie_get_mps(adev->pdev));
1655e098bc96SEvan Quan }
1656e098bc96SEvan Quan 
1657e098bc96SEvan Quan /**
1658e098bc96SEvan Quan  * DOC: unique_id
1659e098bc96SEvan Quan  *
1660e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1661e098bc96SEvan Quan  * The file unique_id is used for this.
1662e098bc96SEvan Quan  * This will provide a Unique ID that will persist from machine to machine
1663e098bc96SEvan Quan  *
1664e098bc96SEvan Quan  * NOTE: This will only work for GFX9 and newer. This file will be absent
1665e098bc96SEvan Quan  * on unsupported ASICs (GFX8 and older)
1666e098bc96SEvan Quan  */
1667e098bc96SEvan Quan static ssize_t amdgpu_get_unique_id(struct device *dev,
1668e098bc96SEvan Quan 		struct device_attribute *attr,
1669e098bc96SEvan Quan 		char *buf)
1670e098bc96SEvan Quan {
1671e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
16721348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1673e098bc96SEvan Quan 
167453b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1675e098bc96SEvan Quan 		return -EPERM;
1676d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1677d2ae842dSAlex Deucher 		return -EPERM;
1678e098bc96SEvan Quan 
1679e098bc96SEvan Quan 	if (adev->unique_id)
1680a9ca9bb3STian Tao 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1681e098bc96SEvan Quan 
1682e098bc96SEvan Quan 	return 0;
1683e098bc96SEvan Quan }
1684e098bc96SEvan Quan 
1685e098bc96SEvan Quan /**
1686e098bc96SEvan Quan  * DOC: thermal_throttling_logging
1687e098bc96SEvan Quan  *
1688e098bc96SEvan Quan  * Thermal throttling pulls down the clock frequency and thus the performance.
1689e098bc96SEvan Quan  * It's an useful mechanism to protect the chip from overheating. Since it
1690e098bc96SEvan Quan  * impacts performance, the user controls whether it is enabled and if so,
1691e098bc96SEvan Quan  * the log frequency.
1692e098bc96SEvan Quan  *
1693e098bc96SEvan Quan  * Reading back the file shows you the status(enabled or disabled) and
1694e098bc96SEvan Quan  * the interval(in seconds) between each thermal logging.
1695e098bc96SEvan Quan  *
1696e098bc96SEvan Quan  * Writing an integer to the file, sets a new logging interval, in seconds.
1697e098bc96SEvan Quan  * The value should be between 1 and 3600. If the value is less than 1,
1698e098bc96SEvan Quan  * thermal logging is disabled. Values greater than 3600 are ignored.
1699e098bc96SEvan Quan  */
1700e098bc96SEvan Quan static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1701e098bc96SEvan Quan 						     struct device_attribute *attr,
1702e098bc96SEvan Quan 						     char *buf)
1703e098bc96SEvan Quan {
1704e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17051348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1706e098bc96SEvan Quan 
1707a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
17084a580877SLuben Tuikov 			  adev_to_drm(adev)->unique,
1709e098bc96SEvan Quan 			  atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1710e098bc96SEvan Quan 			  adev->throttling_logging_rs.interval / HZ + 1);
1711e098bc96SEvan Quan }
1712e098bc96SEvan Quan 
1713e098bc96SEvan Quan static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1714e098bc96SEvan Quan 						     struct device_attribute *attr,
1715e098bc96SEvan Quan 						     const char *buf,
1716e098bc96SEvan Quan 						     size_t count)
1717e098bc96SEvan Quan {
1718e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
17191348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1720e098bc96SEvan Quan 	long throttling_logging_interval;
1721e098bc96SEvan Quan 	unsigned long flags;
1722e098bc96SEvan Quan 	int ret = 0;
1723e098bc96SEvan Quan 
1724e098bc96SEvan Quan 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1725e098bc96SEvan Quan 	if (ret)
1726e098bc96SEvan Quan 		return ret;
1727e098bc96SEvan Quan 
1728e098bc96SEvan Quan 	if (throttling_logging_interval > 3600)
1729e098bc96SEvan Quan 		return -EINVAL;
1730e098bc96SEvan Quan 
1731e098bc96SEvan Quan 	if (throttling_logging_interval > 0) {
1732e098bc96SEvan Quan 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1733e098bc96SEvan Quan 		/*
1734e098bc96SEvan Quan 		 * Reset the ratelimit timer internals.
1735e098bc96SEvan Quan 		 * This can effectively restart the timer.
1736e098bc96SEvan Quan 		 */
1737e098bc96SEvan Quan 		adev->throttling_logging_rs.interval =
1738e098bc96SEvan Quan 			(throttling_logging_interval - 1) * HZ;
1739e098bc96SEvan Quan 		adev->throttling_logging_rs.begin = 0;
1740e098bc96SEvan Quan 		adev->throttling_logging_rs.printed = 0;
1741e098bc96SEvan Quan 		adev->throttling_logging_rs.missed = 0;
1742e098bc96SEvan Quan 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1743e098bc96SEvan Quan 
1744e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 1);
1745e098bc96SEvan Quan 	} else {
1746e098bc96SEvan Quan 		atomic_set(&adev->throttling_logging_enabled, 0);
1747e098bc96SEvan Quan 	}
1748e098bc96SEvan Quan 
1749e098bc96SEvan Quan 	return count;
1750e098bc96SEvan Quan }
1751e098bc96SEvan Quan 
1752e098bc96SEvan Quan /**
1753c3ed0e72SKun Liu  * DOC: apu_thermal_cap
1754c3ed0e72SKun Liu  *
1755c3ed0e72SKun Liu  * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1756c3ed0e72SKun Liu  * limit temperature in millidegrees Celsius
1757c3ed0e72SKun Liu  *
1758c3ed0e72SKun Liu  * Reading back the file shows you core limit value
1759c3ed0e72SKun Liu  *
1760c3ed0e72SKun Liu  * Writing an integer to the file, sets a new thermal limit. The value
1761c3ed0e72SKun Liu  * should be between 0 and 100. If the value is less than 0 or greater
1762c3ed0e72SKun Liu  * than 100, then the write request will be ignored.
1763c3ed0e72SKun Liu  */
1764c3ed0e72SKun Liu static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1765c3ed0e72SKun Liu 					 struct device_attribute *attr,
1766c3ed0e72SKun Liu 					 char *buf)
1767c3ed0e72SKun Liu {
1768c3ed0e72SKun Liu 	int ret, size;
1769c3ed0e72SKun Liu 	u32 limit;
1770c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1771c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1772c3ed0e72SKun Liu 
1773c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1774c3ed0e72SKun Liu 	if (ret < 0) {
1775c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1776c3ed0e72SKun Liu 		return ret;
1777c3ed0e72SKun Liu 	}
1778c3ed0e72SKun Liu 
1779c3ed0e72SKun Liu 	ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1780c3ed0e72SKun Liu 	if (!ret)
1781c3ed0e72SKun Liu 		size = sysfs_emit(buf, "%u\n", limit);
1782c3ed0e72SKun Liu 	else
1783c3ed0e72SKun Liu 		size = sysfs_emit(buf, "failed to get thermal limit\n");
1784c3ed0e72SKun Liu 
1785c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1786c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1787c3ed0e72SKun Liu 
1788c3ed0e72SKun Liu 	return size;
1789c3ed0e72SKun Liu }
1790c3ed0e72SKun Liu 
1791c3ed0e72SKun Liu static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1792c3ed0e72SKun Liu 					 struct device_attribute *attr,
1793c3ed0e72SKun Liu 					 const char *buf,
1794c3ed0e72SKun Liu 					 size_t count)
1795c3ed0e72SKun Liu {
1796c3ed0e72SKun Liu 	int ret;
1797c3ed0e72SKun Liu 	u32 value;
1798c3ed0e72SKun Liu 	struct drm_device *ddev = dev_get_drvdata(dev);
1799c3ed0e72SKun Liu 	struct amdgpu_device *adev = drm_to_adev(ddev);
1800c3ed0e72SKun Liu 
1801c3ed0e72SKun Liu 	ret = kstrtou32(buf, 10, &value);
1802c3ed0e72SKun Liu 	if (ret)
1803c3ed0e72SKun Liu 		return ret;
1804c3ed0e72SKun Liu 
18054d2c09d6SMuhammad Usama Anjum 	if (value > 100) {
1806c3ed0e72SKun Liu 		dev_err(dev, "Invalid argument !\n");
1807c3ed0e72SKun Liu 		return -EINVAL;
1808c3ed0e72SKun Liu 	}
1809c3ed0e72SKun Liu 
1810c3ed0e72SKun Liu 	ret = pm_runtime_get_sync(ddev->dev);
1811c3ed0e72SKun Liu 	if (ret < 0) {
1812c3ed0e72SKun Liu 		pm_runtime_put_autosuspend(ddev->dev);
1813c3ed0e72SKun Liu 		return ret;
1814c3ed0e72SKun Liu 	}
1815c3ed0e72SKun Liu 
1816c3ed0e72SKun Liu 	ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1817c3ed0e72SKun Liu 	if (ret) {
1818c3ed0e72SKun Liu 		dev_err(dev, "failed to update thermal limit\n");
1819c3ed0e72SKun Liu 		return ret;
1820c3ed0e72SKun Liu 	}
1821c3ed0e72SKun Liu 
1822c3ed0e72SKun Liu 	pm_runtime_mark_last_busy(ddev->dev);
1823c3ed0e72SKun Liu 	pm_runtime_put_autosuspend(ddev->dev);
1824c3ed0e72SKun Liu 
1825c3ed0e72SKun Liu 	return count;
1826c3ed0e72SKun Liu }
1827c3ed0e72SKun Liu 
1828223aad1bSLijo Lazar static int amdgpu_pm_metrics_attr_update(struct amdgpu_device *adev,
1829223aad1bSLijo Lazar 					 struct amdgpu_device_attr *attr,
1830223aad1bSLijo Lazar 					 uint32_t mask,
1831223aad1bSLijo Lazar 					 enum amdgpu_device_attr_states *states)
1832223aad1bSLijo Lazar {
1833223aad1bSLijo Lazar 	if (amdgpu_dpm_get_pm_metrics(adev, NULL, 0) == -EOPNOTSUPP)
1834223aad1bSLijo Lazar 		*states = ATTR_STATE_UNSUPPORTED;
1835223aad1bSLijo Lazar 
1836223aad1bSLijo Lazar 	return 0;
1837223aad1bSLijo Lazar }
1838223aad1bSLijo Lazar 
1839223aad1bSLijo Lazar static ssize_t amdgpu_get_pm_metrics(struct device *dev,
1840223aad1bSLijo Lazar 				     struct device_attribute *attr, char *buf)
1841223aad1bSLijo Lazar {
1842223aad1bSLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
1843223aad1bSLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
1844223aad1bSLijo Lazar 	ssize_t size = 0;
1845223aad1bSLijo Lazar 	int ret;
1846223aad1bSLijo Lazar 
1847223aad1bSLijo Lazar 	if (amdgpu_in_reset(adev))
1848223aad1bSLijo Lazar 		return -EPERM;
1849223aad1bSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
1850223aad1bSLijo Lazar 		return -EPERM;
1851223aad1bSLijo Lazar 
1852223aad1bSLijo Lazar 	ret = pm_runtime_get_sync(ddev->dev);
1853223aad1bSLijo Lazar 	if (ret < 0) {
1854223aad1bSLijo Lazar 		pm_runtime_put_autosuspend(ddev->dev);
1855223aad1bSLijo Lazar 		return ret;
1856223aad1bSLijo Lazar 	}
1857223aad1bSLijo Lazar 
1858223aad1bSLijo Lazar 	size = amdgpu_dpm_get_pm_metrics(adev, buf, PAGE_SIZE);
1859223aad1bSLijo Lazar 
1860223aad1bSLijo Lazar 	pm_runtime_mark_last_busy(ddev->dev);
1861223aad1bSLijo Lazar 	pm_runtime_put_autosuspend(ddev->dev);
1862223aad1bSLijo Lazar 
1863223aad1bSLijo Lazar 	return size;
1864223aad1bSLijo Lazar }
1865223aad1bSLijo Lazar 
1866c3ed0e72SKun Liu /**
1867e098bc96SEvan Quan  * DOC: gpu_metrics
1868e098bc96SEvan Quan  *
1869e098bc96SEvan Quan  * The amdgpu driver provides a sysfs API for retrieving current gpu
1870e098bc96SEvan Quan  * metrics data. The file gpu_metrics is used for this. Reading the
1871e098bc96SEvan Quan  * file will dump all the current gpu metrics data.
1872e098bc96SEvan Quan  *
1873e098bc96SEvan Quan  * These data include temperature, frequency, engines utilization,
1874e098bc96SEvan Quan  * power consume, throttler status, fan speed and cpu core statistics(
1875e098bc96SEvan Quan  * available for APU only). That's it will give a snapshot of all sensors
1876e098bc96SEvan Quan  * at the same time.
1877e098bc96SEvan Quan  */
1878e098bc96SEvan Quan static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1879e098bc96SEvan Quan 				      struct device_attribute *attr,
1880e098bc96SEvan Quan 				      char *buf)
1881e098bc96SEvan Quan {
1882e098bc96SEvan Quan 	struct drm_device *ddev = dev_get_drvdata(dev);
18831348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);
1884e098bc96SEvan Quan 	void *gpu_metrics;
1885e098bc96SEvan Quan 	ssize_t size = 0;
1886e098bc96SEvan Quan 	int ret;
1887e098bc96SEvan Quan 
188853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
1889e098bc96SEvan Quan 		return -EPERM;
1890d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
1891d2ae842dSAlex Deucher 		return -EPERM;
1892e098bc96SEvan Quan 
1893e098bc96SEvan Quan 	ret = pm_runtime_get_sync(ddev->dev);
1894e098bc96SEvan Quan 	if (ret < 0) {
1895e098bc96SEvan Quan 		pm_runtime_put_autosuspend(ddev->dev);
1896e098bc96SEvan Quan 		return ret;
1897e098bc96SEvan Quan 	}
1898e098bc96SEvan Quan 
1899e098bc96SEvan Quan 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1900e098bc96SEvan Quan 	if (size <= 0)
1901e098bc96SEvan Quan 		goto out;
1902e098bc96SEvan Quan 
1903e098bc96SEvan Quan 	if (size >= PAGE_SIZE)
1904e098bc96SEvan Quan 		size = PAGE_SIZE - 1;
1905e098bc96SEvan Quan 
1906e098bc96SEvan Quan 	memcpy(buf, gpu_metrics, size);
1907e098bc96SEvan Quan 
1908e098bc96SEvan Quan out:
1909e098bc96SEvan Quan 	pm_runtime_mark_last_busy(ddev->dev);
1910e098bc96SEvan Quan 	pm_runtime_put_autosuspend(ddev->dev);
1911e098bc96SEvan Quan 
1912e098bc96SEvan Quan 	return size;
1913e098bc96SEvan Quan }
1914e098bc96SEvan Quan 
1915494c1432SSathishkumar S static int amdgpu_show_powershift_percent(struct device *dev,
1916d78c227fSMario Limonciello 					char *buf, enum amd_pp_sensors sensor)
1917494c1432SSathishkumar S {
1918494c1432SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
1919494c1432SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
1920494c1432SSathishkumar S 	uint32_t ss_power;
1921494c1432SSathishkumar S 	int r = 0, i;
1922494c1432SSathishkumar S 
1923d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1924494c1432SSathishkumar S 	if (r == -EOPNOTSUPP) {
1925494c1432SSathishkumar S 		/* sensor not available on dGPU, try to read from APU */
1926494c1432SSathishkumar S 		adev = NULL;
1927494c1432SSathishkumar S 		mutex_lock(&mgpu_info.mutex);
1928494c1432SSathishkumar S 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1929494c1432SSathishkumar S 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1930494c1432SSathishkumar S 				adev = mgpu_info.gpu_ins[i].adev;
1931494c1432SSathishkumar S 				break;
1932494c1432SSathishkumar S 			}
1933494c1432SSathishkumar S 		}
1934494c1432SSathishkumar S 		mutex_unlock(&mgpu_info.mutex);
1935494c1432SSathishkumar S 		if (adev)
1936d78c227fSMario Limonciello 			r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1937494c1432SSathishkumar S 	}
1938494c1432SSathishkumar S 
1939d78c227fSMario Limonciello 	if (r)
1940494c1432SSathishkumar S 		return r;
1941d78c227fSMario Limonciello 
1942d78c227fSMario Limonciello 	return sysfs_emit(buf, "%u%%\n", ss_power);
1943494c1432SSathishkumar S }
1944d78c227fSMario Limonciello 
1945a7673a1cSSathishkumar S /**
1946a7673a1cSSathishkumar S  * DOC: smartshift_apu_power
1947a7673a1cSSathishkumar S  *
1948a7673a1cSSathishkumar S  * The amdgpu driver provides a sysfs API for reporting APU power
1949494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1950494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power
1951494c1432SSathishkumar S  * is shifted to APU, the percentage of boost is with respect to APU power
1952494c1432SSathishkumar S  * limit on the platform.
1953a7673a1cSSathishkumar S  */
1954a7673a1cSSathishkumar S 
1955a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1956a7673a1cSSathishkumar S 					       char *buf)
1957a7673a1cSSathishkumar S {
1958d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1959a7673a1cSSathishkumar S }
1960a7673a1cSSathishkumar S 
1961a7673a1cSSathishkumar S /**
1962a7673a1cSSathishkumar S  * DOC: smartshift_dgpu_power
1963a7673a1cSSathishkumar S  *
1964494c1432SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting dGPU power
1965494c1432SSathishkumar S  * shift in percentage if platform supports smartshift. Value 0 means that
1966494c1432SSathishkumar S  * there is no powershift and values between [1-100] means that the power is
1967494c1432SSathishkumar S  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1968494c1432SSathishkumar S  * limit on the platform.
1969a7673a1cSSathishkumar S  */
1970a7673a1cSSathishkumar S 
1971a7673a1cSSathishkumar S static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1972a7673a1cSSathishkumar S 						char *buf)
1973a7673a1cSSathishkumar S {
1974d78c227fSMario Limonciello 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1975a7673a1cSSathishkumar S }
1976a7673a1cSSathishkumar S 
197730d95a37SSathishkumar S /**
197830d95a37SSathishkumar S  * DOC: smartshift_bias
197930d95a37SSathishkumar S  *
198030d95a37SSathishkumar S  * The amdgpu driver provides a sysfs API for reporting the
198130d95a37SSathishkumar S  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
198230d95a37SSathishkumar S  * and the default is 0. -100 sets maximum preference to APU
198330d95a37SSathishkumar S  * and 100 sets max perference to dGPU.
198430d95a37SSathishkumar S  */
198530d95a37SSathishkumar S 
198630d95a37SSathishkumar S static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
198730d95a37SSathishkumar S 					  struct device_attribute *attr,
198830d95a37SSathishkumar S 					  char *buf)
198930d95a37SSathishkumar S {
199030d95a37SSathishkumar S 	int r = 0;
199130d95a37SSathishkumar S 
199230d95a37SSathishkumar S 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
199330d95a37SSathishkumar S 
199430d95a37SSathishkumar S 	return r;
199530d95a37SSathishkumar S }
199630d95a37SSathishkumar S 
199730d95a37SSathishkumar S static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
199830d95a37SSathishkumar S 					  struct device_attribute *attr,
199930d95a37SSathishkumar S 					  const char *buf, size_t count)
200030d95a37SSathishkumar S {
200130d95a37SSathishkumar S 	struct drm_device *ddev = dev_get_drvdata(dev);
200230d95a37SSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(ddev);
200330d95a37SSathishkumar S 	int r = 0;
200430d95a37SSathishkumar S 	int bias = 0;
200530d95a37SSathishkumar S 
200630d95a37SSathishkumar S 	if (amdgpu_in_reset(adev))
200730d95a37SSathishkumar S 		return -EPERM;
200830d95a37SSathishkumar S 	if (adev->in_suspend && !adev->in_runpm)
200930d95a37SSathishkumar S 		return -EPERM;
201030d95a37SSathishkumar S 
201130d95a37SSathishkumar S 	r = pm_runtime_get_sync(ddev->dev);
201230d95a37SSathishkumar S 	if (r < 0) {
201330d95a37SSathishkumar S 		pm_runtime_put_autosuspend(ddev->dev);
201430d95a37SSathishkumar S 		return r;
201530d95a37SSathishkumar S 	}
201630d95a37SSathishkumar S 
201730d95a37SSathishkumar S 	r = kstrtoint(buf, 10, &bias);
201830d95a37SSathishkumar S 	if (r)
201930d95a37SSathishkumar S 		goto out;
202030d95a37SSathishkumar S 
202130d95a37SSathishkumar S 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
202230d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
202330d95a37SSathishkumar S 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
202430d95a37SSathishkumar S 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
202530d95a37SSathishkumar S 
202630d95a37SSathishkumar S 	amdgpu_smartshift_bias = bias;
202730d95a37SSathishkumar S 	r = count;
202830d95a37SSathishkumar S 
2029bd4b9bb7SJulia Lawall 	/* TODO: update bias level with SMU message */
203030d95a37SSathishkumar S 
203130d95a37SSathishkumar S out:
203230d95a37SSathishkumar S 	pm_runtime_mark_last_busy(ddev->dev);
203330d95a37SSathishkumar S 	pm_runtime_put_autosuspend(ddev->dev);
203430d95a37SSathishkumar S 	return r;
203530d95a37SSathishkumar S }
203630d95a37SSathishkumar S 
2037a7673a1cSSathishkumar S static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2038a7673a1cSSathishkumar S 				uint32_t mask, enum amdgpu_device_attr_states *states)
2039a7673a1cSSathishkumar S {
2040494c1432SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
2041a7673a1cSSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
2042a7673a1cSSathishkumar S 
2043a7673a1cSSathishkumar S 	return 0;
2044a7673a1cSSathishkumar S }
2045a7673a1cSSathishkumar S 
204630d95a37SSathishkumar S static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
204730d95a37SSathishkumar S 			       uint32_t mask, enum amdgpu_device_attr_states *states)
204830d95a37SSathishkumar S {
2049d78c227fSMario Limonciello 	uint32_t ss_power;
205030d95a37SSathishkumar S 
205130d95a37SSathishkumar S 	if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
205230d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
2053d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
2054d78c227fSMario Limonciello 		 (void *)&ss_power))
205530d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
2056d78c227fSMario Limonciello 	else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
2057d78c227fSMario Limonciello 		 (void *)&ss_power))
205830d95a37SSathishkumar S 		*states = ATTR_STATE_UNSUPPORTED;
205930d95a37SSathishkumar S 
206030d95a37SSathishkumar S 	return 0;
206130d95a37SSathishkumar S }
206230d95a37SSathishkumar S 
206398a936c3SYang Wang static int pp_od_clk_voltage_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
206498a936c3SYang Wang 					 uint32_t mask, enum amdgpu_device_attr_states *states)
206598a936c3SYang Wang {
206698a936c3SYang Wang 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
206798a936c3SYang Wang 
206898a936c3SYang Wang 	*states = ATTR_STATE_SUPPORTED;
206998a936c3SYang Wang 
207098a936c3SYang Wang 	if (!amdgpu_dpm_is_overdrive_supported(adev)) {
207198a936c3SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
207298a936c3SYang Wang 		return 0;
207398a936c3SYang Wang 	}
207498a936c3SYang Wang 
207598a936c3SYang Wang 	/* Enable pp_od_clk_voltage node for gc 9.4.3 SRIOV/BM support */
207698a936c3SYang Wang 	if (gc_ver == IP_VERSION(9, 4, 3)) {
207798a936c3SYang Wang 		if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
207898a936c3SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
207998a936c3SYang Wang 		return 0;
208098a936c3SYang Wang 	}
208198a936c3SYang Wang 
208298a936c3SYang Wang 	if (!(attr->flags & mask))
208398a936c3SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
208498a936c3SYang Wang 
208598a936c3SYang Wang 	return 0;
208698a936c3SYang Wang }
208798a936c3SYang Wang 
2088190145f6SYang Wang static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2089190145f6SYang Wang 				      uint32_t mask, enum amdgpu_device_attr_states *states)
2090190145f6SYang Wang {
2091190145f6SYang Wang 	struct device_attribute *dev_attr = &attr->dev_attr;
2092190145f6SYang Wang 	uint32_t gc_ver;
2093190145f6SYang Wang 
2094190145f6SYang Wang 	*states = ATTR_STATE_SUPPORTED;
2095190145f6SYang Wang 
2096190145f6SYang Wang 	if (!(attr->flags & mask)) {
2097190145f6SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
2098190145f6SYang Wang 		return 0;
2099190145f6SYang Wang 	}
2100190145f6SYang Wang 
2101190145f6SYang Wang 	gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2102190145f6SYang Wang 	/* dcefclk node is not available on gfx 11.0.3 sriov */
2103190145f6SYang Wang 	if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) ||
2104190145f6SYang Wang 	    gc_ver < IP_VERSION(9, 0, 0) ||
2105190145f6SYang Wang 	    !amdgpu_device_has_display_hardware(adev))
2106190145f6SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
2107190145f6SYang Wang 
2108190145f6SYang Wang 	/* SMU MP1 does not support dcefclk level setting,
2109190145f6SYang Wang 	 * setting should not be allowed from VF if not in one VF mode.
2110190145f6SYang Wang 	 */
2111190145f6SYang Wang 	if (gc_ver >= IP_VERSION(10, 0, 0) ||
2112190145f6SYang Wang 	    (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))) {
2113190145f6SYang Wang 		dev_attr->attr.mode &= ~S_IWUGO;
2114190145f6SYang Wang 		dev_attr->store = NULL;
2115190145f6SYang Wang 	}
2116190145f6SYang Wang 
2117190145f6SYang Wang 	return 0;
2118190145f6SYang Wang }
2119190145f6SYang Wang 
2120166a3c73SYang Wang static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2121166a3c73SYang Wang 					  uint32_t mask, enum amdgpu_device_attr_states *states)
2122166a3c73SYang Wang {
2123166a3c73SYang Wang 	struct device_attribute *dev_attr = &attr->dev_attr;
2124166a3c73SYang Wang 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
2125166a3c73SYang Wang 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
2126166a3c73SYang Wang 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2127166a3c73SYang Wang 
2128166a3c73SYang Wang 	*states = ATTR_STATE_SUPPORTED;
2129166a3c73SYang Wang 
2130166a3c73SYang Wang 	if (!(attr->flags & mask)) {
2131166a3c73SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
2132166a3c73SYang Wang 		return 0;
2133166a3c73SYang Wang 	}
2134166a3c73SYang Wang 
2135166a3c73SYang Wang 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2136166a3c73SYang Wang 		if (gc_ver < IP_VERSION(9, 0, 0))
2137166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2138166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2139166a3c73SYang Wang 		if (mp1_ver < IP_VERSION(10, 0, 0))
2140166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2141166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2142166a3c73SYang Wang 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2143166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 3) ||
2144166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 6) ||
2145166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 7) ||
2146166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 0) ||
2147166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 1, 2) ||
2148166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 0) ||
2149166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 1) ||
2150166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 4) ||
2151166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 5, 0) ||
2152166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 2) ||
2153166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 3) ||
2154166a3c73SYang Wang 		      gc_ver == IP_VERSION(9, 4, 3)))
2155166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2156166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
2157166a3c73SYang Wang 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2158166a3c73SYang Wang 		       gc_ver == IP_VERSION(10, 3, 0) ||
2159166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 2) ||
2160166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2161166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2162166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2163166a3c73SYang Wang 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2164166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 3) ||
2165166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 6) ||
2166166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 7) ||
2167166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 0) ||
2168166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 1, 2) ||
2169166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 0) ||
2170166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 1) ||
2171166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 4) ||
2172166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 5, 0) ||
2173166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 2) ||
2174166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 3) ||
2175166a3c73SYang Wang 		      gc_ver == IP_VERSION(9, 4, 3)))
2176166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2177166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
2178166a3c73SYang Wang 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2179166a3c73SYang Wang 		       gc_ver == IP_VERSION(10, 3, 0) ||
2180166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 2) ||
2181166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2182166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2183166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
2184166a3c73SYang Wang 		if (gc_ver == IP_VERSION(9, 4, 2) ||
2185166a3c73SYang Wang 		    gc_ver == IP_VERSION(9, 4, 3))
2186166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2187166a3c73SYang Wang 	}
2188166a3c73SYang Wang 
2189166a3c73SYang Wang 	switch (gc_ver) {
2190166a3c73SYang Wang 	case IP_VERSION(9, 4, 1):
2191166a3c73SYang Wang 	case IP_VERSION(9, 4, 2):
2192166a3c73SYang Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2193166a3c73SYang Wang 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2194166a3c73SYang Wang 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2195166a3c73SYang Wang 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2196166a3c73SYang Wang 			dev_attr->attr.mode &= ~S_IWUGO;
2197166a3c73SYang Wang 			dev_attr->store = NULL;
2198166a3c73SYang Wang 		}
2199166a3c73SYang Wang 		break;
2200166a3c73SYang Wang 	default:
2201166a3c73SYang Wang 		break;
2202166a3c73SYang Wang 	}
2203166a3c73SYang Wang 
2204166a3c73SYang Wang 	/* setting should not be allowed from VF if not in one VF mode */
2205166a3c73SYang Wang 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) {
2206166a3c73SYang Wang 		dev_attr->attr.mode &= ~S_IWUGO;
2207166a3c73SYang Wang 		dev_attr->store = NULL;
2208166a3c73SYang Wang 	}
2209166a3c73SYang Wang 
2210166a3c73SYang Wang 	return 0;
2211166a3c73SYang Wang }
2212166a3c73SYang Wang 
221321e43386SLe Ma /* Following items will be read out to indicate current plpd policy:
221421e43386SLe Ma  *  - -1: none
221521e43386SLe Ma  *  - 0: disallow
221621e43386SLe Ma  *  - 1: default
221721e43386SLe Ma  *  - 2: optimized
221821e43386SLe Ma  */
221921e43386SLe Ma static ssize_t amdgpu_get_xgmi_plpd_policy(struct device *dev,
222021e43386SLe Ma 					   struct device_attribute *attr,
222121e43386SLe Ma 					   char *buf)
222221e43386SLe Ma {
222321e43386SLe Ma 	struct drm_device *ddev = dev_get_drvdata(dev);
222421e43386SLe Ma 	struct amdgpu_device *adev = drm_to_adev(ddev);
222521e43386SLe Ma 	char *mode_desc = "none";
222621e43386SLe Ma 	int mode;
222721e43386SLe Ma 
222821e43386SLe Ma 	if (amdgpu_in_reset(adev))
222921e43386SLe Ma 		return -EPERM;
223021e43386SLe Ma 	if (adev->in_suspend && !adev->in_runpm)
223121e43386SLe Ma 		return -EPERM;
223221e43386SLe Ma 
223321e43386SLe Ma 	mode = amdgpu_dpm_get_xgmi_plpd_mode(adev, &mode_desc);
223421e43386SLe Ma 
223521e43386SLe Ma 	return sysfs_emit(buf, "%d: %s\n", mode, mode_desc);
223621e43386SLe Ma }
223721e43386SLe Ma 
223821e43386SLe Ma /* Following argument value is expected from user to change plpd policy
223921e43386SLe Ma  *  - arg 0: disallow plpd
224021e43386SLe Ma  *  - arg 1: default policy
224121e43386SLe Ma  *  - arg 2: optimized policy
224221e43386SLe Ma  */
224321e43386SLe Ma static ssize_t amdgpu_set_xgmi_plpd_policy(struct device *dev,
224421e43386SLe Ma 					   struct device_attribute *attr,
224521e43386SLe Ma 					   const char *buf, size_t count)
224621e43386SLe Ma {
224721e43386SLe Ma 	struct drm_device *ddev = dev_get_drvdata(dev);
224821e43386SLe Ma 	struct amdgpu_device *adev = drm_to_adev(ddev);
224921e43386SLe Ma 	int mode, ret;
225021e43386SLe Ma 
225121e43386SLe Ma 	if (amdgpu_in_reset(adev))
225221e43386SLe Ma 		return -EPERM;
225321e43386SLe Ma 	if (adev->in_suspend && !adev->in_runpm)
225421e43386SLe Ma 		return -EPERM;
225521e43386SLe Ma 
225621e43386SLe Ma 	ret = kstrtos32(buf, 0, &mode);
225721e43386SLe Ma 	if (ret)
225821e43386SLe Ma 		return -EINVAL;
225921e43386SLe Ma 
226021e43386SLe Ma 	ret = pm_runtime_get_sync(ddev->dev);
226121e43386SLe Ma 	if (ret < 0) {
226221e43386SLe Ma 		pm_runtime_put_autosuspend(ddev->dev);
226321e43386SLe Ma 		return ret;
226421e43386SLe Ma 	}
226521e43386SLe Ma 
226621e43386SLe Ma 	ret = amdgpu_dpm_set_xgmi_plpd_mode(adev, mode);
226721e43386SLe Ma 
226821e43386SLe Ma 	pm_runtime_mark_last_busy(ddev->dev);
226921e43386SLe Ma 	pm_runtime_put_autosuspend(ddev->dev);
227021e43386SLe Ma 
227121e43386SLe Ma 	if (ret)
227221e43386SLe Ma 		return ret;
227321e43386SLe Ma 
227421e43386SLe Ma 	return count;
227521e43386SLe Ma }
227621e43386SLe Ma 
2277e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2278e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
22794215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
22807884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
22817884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
22827884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
22837884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2284166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2285166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2286166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2287166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2288166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2289166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2290166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2291166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2292166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2293166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2294166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2295166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2296166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2297166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2298166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2299166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2300190145f6SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2301190145f6SYang Wang 			      .attr_update = pp_dpm_dcefclk_attr_update),
2302166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2303166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2304e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2305e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2306ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
230798a936c3SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC,
230898a936c3SYang Wang 			      .attr_update = pp_od_clk_voltage_attr_update),
2309ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2310ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2311d1b2703cSXiaojian Du 	AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2312e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2313ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2314ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2315ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2316c3ed0e72SKun Liu 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2317ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2318a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2319a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
2320a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2321a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
232230d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
232330d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
232421e43386SLe Ma 	AMDGPU_DEVICE_ATTR_RW(xgmi_plpd_policy,				ATTR_FLAG_BASIC),
2325223aad1bSLijo Lazar 	AMDGPU_DEVICE_ATTR_RO(pm_metrics,				ATTR_FLAG_BASIC,
2326223aad1bSLijo Lazar 			      .attr_update = amdgpu_pm_metrics_attr_update),
2327e098bc96SEvan Quan };
2328e098bc96SEvan Quan 
2329e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2330e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2331e098bc96SEvan Quan {
2332e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
23332ea6f4d9SYang Wang 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
23344e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2335e098bc96SEvan Quan 
2336e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2337e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2338e098bc96SEvan Quan 		return 0;
2339e098bc96SEvan Quan 	}
2340e098bc96SEvan Quan 
2341166a3c73SYang Wang 	if (DEVICE_ATTR_IS(mem_busy_percent)) {
23425df0f0b3SAsad Kamal 		if ((adev->flags & AMD_IS_APU &&
23435df0f0b3SAsad Kamal 		     gc_ver != IP_VERSION(9, 4, 3)) ||
23445df0f0b3SAsad Kamal 		    gc_ver == IP_VERSION(9, 0, 1))
2345e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2346d1b2703cSXiaojian Du 	} else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
2347d1b2703cSXiaojian Du 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2348d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(10, 3, 3) ||
2349d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(10, 3, 6) ||
2350d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(10, 3, 7) ||
2351d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(11, 0, 1) ||
2352d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(11, 0, 4) ||
2353d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(11, 5, 0)))
2354d1b2703cSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
2355e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2356e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
23575fe4a8d3SAsad Kamal 		if (adev->flags & AMD_IS_APU ||
23585fe4a8d3SAsad Kamal 		    !adev->asic_funcs->get_pcie_usage)
2359e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2360e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
236160044748SKent Russell 		switch (gc_ver) {
236260044748SKent Russell 		case IP_VERSION(9, 0, 1):
236360044748SKent Russell 		case IP_VERSION(9, 4, 0):
236460044748SKent Russell 		case IP_VERSION(9, 4, 1):
236560044748SKent Russell 		case IP_VERSION(9, 4, 2):
2366baf65745SLijo Lazar 		case IP_VERSION(9, 4, 3):
2367ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
2368276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
236935e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
237035e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
2371d82758adSKenneth Feng 		case IP_VERSION(11, 0, 3):
237260044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
237360044748SKent Russell 			break;
237460044748SKent Russell 		default:
2375e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
237660044748SKent Russell 		}
2377e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2378fc8e84a2SLijo Lazar 		if ((adev->flags & AMD_IS_APU &&
2379fc8e84a2SLijo Lazar 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2380fc8e84a2SLijo Lazar 		    gc_ver < IP_VERSION(9, 0, 0))
2381e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2382e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
23838ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2384e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2385a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
238679c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2387a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
2388b57c4f1cSVictor Zhao 		else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2389b57c4f1cSVictor Zhao 			  gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
23901b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
239121e43386SLe Ma 	} else if (DEVICE_ATTR_IS(xgmi_plpd_policy)) {
239221e43386SLe Ma 		if (amdgpu_dpm_get_xgmi_plpd_mode(adev, NULL) == XGMI_PLPD_NONE)
239321e43386SLe Ma 			*states = ATTR_STATE_UNSUPPORTED;
2394df2a5f74SDmitrii Galantsev 	} else if (DEVICE_ATTR_IS(pp_mclk_od)) {
23958cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
23968cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
2397df2a5f74SDmitrii Galantsev 	} else if (DEVICE_ATTR_IS(pp_sclk_od)) {
23988cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
23998cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
24008cfd6a05SLijo Lazar 	} else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
24018cfd6a05SLijo Lazar 		u32 limit;
24028cfd6a05SLijo Lazar 
24038cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
24048cfd6a05SLijo Lazar 		    -EOPNOTSUPP)
24058cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
2406e098bc96SEvan Quan 	}
2407e098bc96SEvan Quan 
24088ecad8d6SLijo Lazar 	switch (gc_ver) {
24091b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
24101b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
24111b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
24121b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
24131b852572SDanijel Slivka 			dev_attr->store = NULL;
24141b852572SDanijel Slivka 		}
24151b852572SDanijel Slivka 		break;
24161d0e622fSKevin Wang 	default:
24171d0e622fSKevin Wang 		break;
2418e098bc96SEvan Quan 	}
2419e098bc96SEvan Quan 
2420e098bc96SEvan Quan 	return 0;
2421e098bc96SEvan Quan }
2422e098bc96SEvan Quan 
2423e098bc96SEvan Quan 
2424e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2425e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2426e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2427e098bc96SEvan Quan {
2428e098bc96SEvan Quan 	int ret = 0;
2429e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2430e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
243125e6373aSYang Wang 	struct device_attribute *dev_attr;
243225e6373aSYang Wang 	const char *name;
2433e098bc96SEvan Quan 
2434e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2435e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2436e098bc96SEvan Quan 
243725e6373aSYang Wang 	if (!attr)
243825e6373aSYang Wang 		return -EINVAL;
243925e6373aSYang Wang 
244025e6373aSYang Wang 	dev_attr = &attr->dev_attr;
244125e6373aSYang Wang 	name = dev_attr->attr.name;
2442e098bc96SEvan Quan 
24438a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2444e098bc96SEvan Quan 
2445e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2446e098bc96SEvan Quan 	if (ret) {
2447e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2448e098bc96SEvan Quan 			name, ret);
2449e098bc96SEvan Quan 		return ret;
2450e098bc96SEvan Quan 	}
2451e098bc96SEvan Quan 
2452e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2453e098bc96SEvan Quan 		return 0;
2454e098bc96SEvan Quan 
2455e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2456e098bc96SEvan Quan 	if (ret) {
2457e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2458e098bc96SEvan Quan 			name, ret);
2459e098bc96SEvan Quan 	}
2460e098bc96SEvan Quan 
2461e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2462e098bc96SEvan Quan 	if (!attr_entry)
2463e098bc96SEvan Quan 		return -ENOMEM;
2464e098bc96SEvan Quan 
2465e098bc96SEvan Quan 	attr_entry->attr = attr;
2466e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2467e098bc96SEvan Quan 
2468e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2469e098bc96SEvan Quan 
2470e098bc96SEvan Quan 	return ret;
2471e098bc96SEvan Quan }
2472e098bc96SEvan Quan 
2473e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2474e098bc96SEvan Quan {
2475e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2476e098bc96SEvan Quan 
2477e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2478e098bc96SEvan Quan }
2479e098bc96SEvan Quan 
2480e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2481e098bc96SEvan Quan 					     struct list_head *attr_list);
2482e098bc96SEvan Quan 
2483e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2484e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2485e098bc96SEvan Quan 					    uint32_t counts,
2486e098bc96SEvan Quan 					    uint32_t mask,
2487e098bc96SEvan Quan 					    struct list_head *attr_list)
2488e098bc96SEvan Quan {
2489e098bc96SEvan Quan 	int ret = 0;
2490e098bc96SEvan Quan 	uint32_t i = 0;
2491e098bc96SEvan Quan 
2492e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2493e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2494e098bc96SEvan Quan 		if (ret)
2495e098bc96SEvan Quan 			goto failed;
2496e098bc96SEvan Quan 	}
2497e098bc96SEvan Quan 
2498e098bc96SEvan Quan 	return 0;
2499e098bc96SEvan Quan 
2500e098bc96SEvan Quan failed:
2501e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2502e098bc96SEvan Quan 
2503e098bc96SEvan Quan 	return ret;
2504e098bc96SEvan Quan }
2505e098bc96SEvan Quan 
2506e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2507e098bc96SEvan Quan 					     struct list_head *attr_list)
2508e098bc96SEvan Quan {
2509e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2510e098bc96SEvan Quan 
2511e098bc96SEvan Quan 	if (list_empty(attr_list))
2512e098bc96SEvan Quan 		return ;
2513e098bc96SEvan Quan 
2514e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2515e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2516e098bc96SEvan Quan 		list_del(&entry->entry);
2517e098bc96SEvan Quan 		kfree(entry);
2518e098bc96SEvan Quan 	}
2519e098bc96SEvan Quan }
2520e098bc96SEvan Quan 
2521e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2522e098bc96SEvan Quan 				      struct device_attribute *attr,
2523e098bc96SEvan Quan 				      char *buf)
2524e098bc96SEvan Quan {
2525e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2526e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2527d78c227fSMario Limonciello 	int r, temp = 0;
2528e098bc96SEvan Quan 
2529e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2530e098bc96SEvan Quan 		return -EINVAL;
2531e098bc96SEvan Quan 
2532e098bc96SEvan Quan 	switch (channel) {
2533e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2534e098bc96SEvan Quan 		/* get current junction temperature */
2535d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2536d78c227fSMario Limonciello 					   (void *)&temp);
2537e098bc96SEvan Quan 		break;
2538e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2539e098bc96SEvan Quan 		/* get current edge temperature */
2540d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2541d78c227fSMario Limonciello 					   (void *)&temp);
2542e098bc96SEvan Quan 		break;
2543e098bc96SEvan Quan 	case PP_TEMP_MEM:
2544e098bc96SEvan Quan 		/* get current memory temperature */
2545d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2546d78c227fSMario Limonciello 					   (void *)&temp);
2547e098bc96SEvan Quan 		break;
2548e098bc96SEvan Quan 	default:
2549e098bc96SEvan Quan 		r = -EINVAL;
2550e098bc96SEvan Quan 		break;
2551e098bc96SEvan Quan 	}
2552e098bc96SEvan Quan 
2553e098bc96SEvan Quan 	if (r)
2554e098bc96SEvan Quan 		return r;
2555e098bc96SEvan Quan 
2556a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2557e098bc96SEvan Quan }
2558e098bc96SEvan Quan 
2559e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2560e098bc96SEvan Quan 					     struct device_attribute *attr,
2561e098bc96SEvan Quan 					     char *buf)
2562e098bc96SEvan Quan {
2563e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2564e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2565e098bc96SEvan Quan 	int temp;
2566e098bc96SEvan Quan 
2567e098bc96SEvan Quan 	if (hyst)
2568e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2569e098bc96SEvan Quan 	else
2570e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2571e098bc96SEvan Quan 
2572a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2573e098bc96SEvan Quan }
2574e098bc96SEvan Quan 
2575e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2576e098bc96SEvan Quan 					     struct device_attribute *attr,
2577e098bc96SEvan Quan 					     char *buf)
2578e098bc96SEvan Quan {
2579e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2580e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2581e098bc96SEvan Quan 	int temp;
2582e098bc96SEvan Quan 
2583e098bc96SEvan Quan 	if (hyst)
2584e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2585e098bc96SEvan Quan 	else
2586e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2587e098bc96SEvan Quan 
2588a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2589e098bc96SEvan Quan }
2590e098bc96SEvan Quan 
2591e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2592e098bc96SEvan Quan 					     struct device_attribute *attr,
2593e098bc96SEvan Quan 					     char *buf)
2594e098bc96SEvan Quan {
2595e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2596e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2597e098bc96SEvan Quan 	int temp;
2598e098bc96SEvan Quan 
2599e098bc96SEvan Quan 	if (hyst)
2600e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2601e098bc96SEvan Quan 	else
2602e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2603e098bc96SEvan Quan 
2604a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2605e098bc96SEvan Quan }
2606e098bc96SEvan Quan 
2607e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2608e098bc96SEvan Quan 					     struct device_attribute *attr,
2609e098bc96SEvan Quan 					     char *buf)
2610e098bc96SEvan Quan {
2611e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2612e098bc96SEvan Quan 
2613e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2614e098bc96SEvan Quan 		return -EINVAL;
2615e098bc96SEvan Quan 
2616a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2617e098bc96SEvan Quan }
2618e098bc96SEvan Quan 
2619e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2620e098bc96SEvan Quan 					     struct device_attribute *attr,
2621e098bc96SEvan Quan 					     char *buf)
2622e098bc96SEvan Quan {
2623e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2624e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2625e098bc96SEvan Quan 	int temp = 0;
2626e098bc96SEvan Quan 
2627e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2628e098bc96SEvan Quan 		return -EINVAL;
2629e098bc96SEvan Quan 
2630e098bc96SEvan Quan 	switch (channel) {
2631e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2632e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2633e098bc96SEvan Quan 		break;
2634e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2635e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2636e098bc96SEvan Quan 		break;
2637e098bc96SEvan Quan 	case PP_TEMP_MEM:
2638e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2639e098bc96SEvan Quan 		break;
2640e098bc96SEvan Quan 	}
2641e098bc96SEvan Quan 
2642a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2643e098bc96SEvan Quan }
2644e098bc96SEvan Quan 
2645e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2646e098bc96SEvan Quan 					    struct device_attribute *attr,
2647e098bc96SEvan Quan 					    char *buf)
2648e098bc96SEvan Quan {
2649e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2650e098bc96SEvan Quan 	u32 pwm_mode = 0;
2651e098bc96SEvan Quan 	int ret;
2652e098bc96SEvan Quan 
265353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2654e098bc96SEvan Quan 		return -EPERM;
2655d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2656d2ae842dSAlex Deucher 		return -EPERM;
2657e098bc96SEvan Quan 
26584a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2659e098bc96SEvan Quan 	if (ret < 0) {
26604a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2661e098bc96SEvan Quan 		return ret;
2662e098bc96SEvan Quan 	}
2663e098bc96SEvan Quan 
266479c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
266579c65f3fSEvan Quan 
26664a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
26674a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
266879c65f3fSEvan Quan 
266979c65f3fSEvan Quan 	if (ret)
2670e098bc96SEvan Quan 		return -EINVAL;
2671e098bc96SEvan Quan 
2672fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2673e098bc96SEvan Quan }
2674e098bc96SEvan Quan 
2675e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2676e098bc96SEvan Quan 					    struct device_attribute *attr,
2677e098bc96SEvan Quan 					    const char *buf,
2678e098bc96SEvan Quan 					    size_t count)
2679e098bc96SEvan Quan {
2680e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2681e098bc96SEvan Quan 	int err, ret;
2682f317c5e5SMa Jun 	u32 pwm_mode;
2683e098bc96SEvan Quan 	int value;
2684e098bc96SEvan Quan 
268553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2686e098bc96SEvan Quan 		return -EPERM;
2687d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2688d2ae842dSAlex Deucher 		return -EPERM;
2689e098bc96SEvan Quan 
2690e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2691e098bc96SEvan Quan 	if (err)
2692e098bc96SEvan Quan 		return err;
2693e098bc96SEvan Quan 
2694f317c5e5SMa Jun 	if (value == 0)
2695f317c5e5SMa Jun 		pwm_mode = AMD_FAN_CTRL_NONE;
2696f317c5e5SMa Jun 	else if (value == 1)
2697f317c5e5SMa Jun 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2698f317c5e5SMa Jun 	else if (value == 2)
2699f317c5e5SMa Jun 		pwm_mode = AMD_FAN_CTRL_AUTO;
2700f317c5e5SMa Jun 	else
2701f317c5e5SMa Jun 		return -EINVAL;
2702f317c5e5SMa Jun 
27034a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2704e098bc96SEvan Quan 	if (ret < 0) {
27054a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2706e098bc96SEvan Quan 		return ret;
2707e098bc96SEvan Quan 	}
2708e098bc96SEvan Quan 
2709f317c5e5SMa Jun 	ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
271079c65f3fSEvan Quan 
27114a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27124a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
271379c65f3fSEvan Quan 
271479c65f3fSEvan Quan 	if (ret)
2715e098bc96SEvan Quan 		return -EINVAL;
2716e098bc96SEvan Quan 
2717e098bc96SEvan Quan 	return count;
2718e098bc96SEvan Quan }
2719e098bc96SEvan Quan 
2720e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2721e098bc96SEvan Quan 					 struct device_attribute *attr,
2722e098bc96SEvan Quan 					 char *buf)
2723e098bc96SEvan Quan {
2724fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2725e098bc96SEvan Quan }
2726e098bc96SEvan Quan 
2727e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2728e098bc96SEvan Quan 					 struct device_attribute *attr,
2729e098bc96SEvan Quan 					 char *buf)
2730e098bc96SEvan Quan {
2731fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2732e098bc96SEvan Quan }
2733e098bc96SEvan Quan 
2734e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2735e098bc96SEvan Quan 				     struct device_attribute *attr,
2736e098bc96SEvan Quan 				     const char *buf, size_t count)
2737e098bc96SEvan Quan {
2738e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2739e098bc96SEvan Quan 	int err;
2740e098bc96SEvan Quan 	u32 value;
2741e098bc96SEvan Quan 	u32 pwm_mode;
2742e098bc96SEvan Quan 
274353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2744e098bc96SEvan Quan 		return -EPERM;
2745d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2746d2ae842dSAlex Deucher 		return -EPERM;
2747e098bc96SEvan Quan 
274879c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
274979c65f3fSEvan Quan 	if (err)
275079c65f3fSEvan Quan 		return err;
275179c65f3fSEvan Quan 
27524a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2753e098bc96SEvan Quan 	if (err < 0) {
27544a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2755e098bc96SEvan Quan 		return err;
2756e098bc96SEvan Quan 	}
2757e098bc96SEvan Quan 
275879c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
275979c65f3fSEvan Quan 	if (err)
276079c65f3fSEvan Quan 		goto out;
276179c65f3fSEvan Quan 
2762e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2763e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2764e098bc96SEvan Quan 		err = -EINVAL;
276579c65f3fSEvan Quan 		goto out;
276679c65f3fSEvan Quan 	}
2767e098bc96SEvan Quan 
276879c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
276979c65f3fSEvan Quan 
277079c65f3fSEvan Quan out:
27714a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27724a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2773e098bc96SEvan Quan 
2774e098bc96SEvan Quan 	if (err)
2775e098bc96SEvan Quan 		return err;
2776e098bc96SEvan Quan 
2777e098bc96SEvan Quan 	return count;
2778e098bc96SEvan Quan }
2779e098bc96SEvan Quan 
2780e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2781e098bc96SEvan Quan 				     struct device_attribute *attr,
2782e098bc96SEvan Quan 				     char *buf)
2783e098bc96SEvan Quan {
2784e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2785e098bc96SEvan Quan 	int err;
2786e098bc96SEvan Quan 	u32 speed = 0;
2787e098bc96SEvan Quan 
278853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2789e098bc96SEvan Quan 		return -EPERM;
2790d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2791d2ae842dSAlex Deucher 		return -EPERM;
2792e098bc96SEvan Quan 
27934a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2794e098bc96SEvan Quan 	if (err < 0) {
27954a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2796e098bc96SEvan Quan 		return err;
2797e098bc96SEvan Quan 	}
2798e098bc96SEvan Quan 
27990d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2800e098bc96SEvan Quan 
28014a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28024a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2803e098bc96SEvan Quan 
2804e098bc96SEvan Quan 	if (err)
2805e098bc96SEvan Quan 		return err;
2806e098bc96SEvan Quan 
2807fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2808e098bc96SEvan Quan }
2809e098bc96SEvan Quan 
2810e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2811e098bc96SEvan Quan 					   struct device_attribute *attr,
2812e098bc96SEvan Quan 					   char *buf)
2813e098bc96SEvan Quan {
2814e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2815e098bc96SEvan Quan 	int err;
2816e098bc96SEvan Quan 	u32 speed = 0;
2817e098bc96SEvan Quan 
281853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2819e098bc96SEvan Quan 		return -EPERM;
2820d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2821d2ae842dSAlex Deucher 		return -EPERM;
2822e098bc96SEvan Quan 
28234a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2824e098bc96SEvan Quan 	if (err < 0) {
28254a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2826e098bc96SEvan Quan 		return err;
2827e098bc96SEvan Quan 	}
2828e098bc96SEvan Quan 
2829e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2830e098bc96SEvan Quan 
28314a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28324a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2833e098bc96SEvan Quan 
2834e098bc96SEvan Quan 	if (err)
2835e098bc96SEvan Quan 		return err;
2836e098bc96SEvan Quan 
2837fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2838e098bc96SEvan Quan }
2839e098bc96SEvan Quan 
2840e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2841e098bc96SEvan Quan 					 struct device_attribute *attr,
2842e098bc96SEvan Quan 					 char *buf)
2843e098bc96SEvan Quan {
2844e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2845e098bc96SEvan Quan 	u32 min_rpm = 0;
2846e098bc96SEvan Quan 	int r;
2847e098bc96SEvan Quan 
2848d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2849d78c227fSMario Limonciello 				   (void *)&min_rpm);
2850e098bc96SEvan Quan 
2851e098bc96SEvan Quan 	if (r)
2852e098bc96SEvan Quan 		return r;
2853e098bc96SEvan Quan 
2854a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2855e098bc96SEvan Quan }
2856e098bc96SEvan Quan 
2857e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2858e098bc96SEvan Quan 					 struct device_attribute *attr,
2859e098bc96SEvan Quan 					 char *buf)
2860e098bc96SEvan Quan {
2861e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2862e098bc96SEvan Quan 	u32 max_rpm = 0;
2863e098bc96SEvan Quan 	int r;
2864e098bc96SEvan Quan 
2865d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2866d78c227fSMario Limonciello 				   (void *)&max_rpm);
2867e098bc96SEvan Quan 
2868e098bc96SEvan Quan 	if (r)
2869e098bc96SEvan Quan 		return r;
2870e098bc96SEvan Quan 
2871a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
2872e098bc96SEvan Quan }
2873e098bc96SEvan Quan 
2874e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2875e098bc96SEvan Quan 					   struct device_attribute *attr,
2876e098bc96SEvan Quan 					   char *buf)
2877e098bc96SEvan Quan {
2878e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2879e098bc96SEvan Quan 	int err;
2880e098bc96SEvan Quan 	u32 rpm = 0;
2881e098bc96SEvan Quan 
288253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2883e098bc96SEvan Quan 		return -EPERM;
2884d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2885d2ae842dSAlex Deucher 		return -EPERM;
2886e098bc96SEvan Quan 
28874a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2888e098bc96SEvan Quan 	if (err < 0) {
28894a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2890e098bc96SEvan Quan 		return err;
2891e098bc96SEvan Quan 	}
2892e098bc96SEvan Quan 
2893e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2894e098bc96SEvan Quan 
28954a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28964a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2897e098bc96SEvan Quan 
2898e098bc96SEvan Quan 	if (err)
2899e098bc96SEvan Quan 		return err;
2900e098bc96SEvan Quan 
2901fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
2902e098bc96SEvan Quan }
2903e098bc96SEvan Quan 
2904e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2905e098bc96SEvan Quan 				     struct device_attribute *attr,
2906e098bc96SEvan Quan 				     const char *buf, size_t count)
2907e098bc96SEvan Quan {
2908e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2909e098bc96SEvan Quan 	int err;
2910e098bc96SEvan Quan 	u32 value;
2911e098bc96SEvan Quan 	u32 pwm_mode;
2912e098bc96SEvan Quan 
291353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2914e098bc96SEvan Quan 		return -EPERM;
2915d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2916d2ae842dSAlex Deucher 		return -EPERM;
2917e098bc96SEvan Quan 
291879c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
291979c65f3fSEvan Quan 	if (err)
292079c65f3fSEvan Quan 		return err;
292179c65f3fSEvan Quan 
29224a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2923e098bc96SEvan Quan 	if (err < 0) {
29244a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2925e098bc96SEvan Quan 		return err;
2926e098bc96SEvan Quan 	}
2927e098bc96SEvan Quan 
292879c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
292979c65f3fSEvan Quan 	if (err)
293079c65f3fSEvan Quan 		goto out;
2931e098bc96SEvan Quan 
2932e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
293379c65f3fSEvan Quan 		err = -ENODATA;
293479c65f3fSEvan Quan 		goto out;
2935e098bc96SEvan Quan 	}
2936e098bc96SEvan Quan 
2937e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2938e098bc96SEvan Quan 
293979c65f3fSEvan Quan out:
29404a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29414a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2942e098bc96SEvan Quan 
2943e098bc96SEvan Quan 	if (err)
2944e098bc96SEvan Quan 		return err;
2945e098bc96SEvan Quan 
2946e098bc96SEvan Quan 	return count;
2947e098bc96SEvan Quan }
2948e098bc96SEvan Quan 
2949e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2950e098bc96SEvan Quan 					    struct device_attribute *attr,
2951e098bc96SEvan Quan 					    char *buf)
2952e098bc96SEvan Quan {
2953e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2954e098bc96SEvan Quan 	u32 pwm_mode = 0;
2955e098bc96SEvan Quan 	int ret;
2956e098bc96SEvan Quan 
295753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2958e098bc96SEvan Quan 		return -EPERM;
2959d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2960d2ae842dSAlex Deucher 		return -EPERM;
2961e098bc96SEvan Quan 
29624a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2963e098bc96SEvan Quan 	if (ret < 0) {
29644a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2965e098bc96SEvan Quan 		return ret;
2966e098bc96SEvan Quan 	}
2967e098bc96SEvan Quan 
296879c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
296979c65f3fSEvan Quan 
29704a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29714a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
297279c65f3fSEvan Quan 
297379c65f3fSEvan Quan 	if (ret)
2974e098bc96SEvan Quan 		return -EINVAL;
2975e098bc96SEvan Quan 
2976fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2977e098bc96SEvan Quan }
2978e098bc96SEvan Quan 
2979e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2980e098bc96SEvan Quan 					    struct device_attribute *attr,
2981e098bc96SEvan Quan 					    const char *buf,
2982e098bc96SEvan Quan 					    size_t count)
2983e098bc96SEvan Quan {
2984e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2985e098bc96SEvan Quan 	int err;
2986e098bc96SEvan Quan 	int value;
2987e098bc96SEvan Quan 	u32 pwm_mode;
2988e098bc96SEvan Quan 
298953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2990e098bc96SEvan Quan 		return -EPERM;
2991d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2992d2ae842dSAlex Deucher 		return -EPERM;
2993e098bc96SEvan Quan 
2994e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2995e098bc96SEvan Quan 	if (err)
2996e098bc96SEvan Quan 		return err;
2997e098bc96SEvan Quan 
2998e098bc96SEvan Quan 	if (value == 0)
2999e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
3000e098bc96SEvan Quan 	else if (value == 1)
3001e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
3002e098bc96SEvan Quan 	else
3003e098bc96SEvan Quan 		return -EINVAL;
3004e098bc96SEvan Quan 
30054a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3006e098bc96SEvan Quan 	if (err < 0) {
30074a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3008e098bc96SEvan Quan 		return err;
3009e098bc96SEvan Quan 	}
3010e098bc96SEvan Quan 
301179c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
3012e098bc96SEvan Quan 
30134a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30144a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3015e098bc96SEvan Quan 
301679c65f3fSEvan Quan 	if (err)
301779c65f3fSEvan Quan 		return -EINVAL;
301879c65f3fSEvan Quan 
3019e098bc96SEvan Quan 	return count;
3020e098bc96SEvan Quan }
3021e098bc96SEvan Quan 
3022e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
3023e098bc96SEvan Quan 					struct device_attribute *attr,
3024e098bc96SEvan Quan 					char *buf)
3025e098bc96SEvan Quan {
3026e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3027e098bc96SEvan Quan 	u32 vddgfx;
3028d78c227fSMario Limonciello 	int r;
3029e098bc96SEvan Quan 
3030e098bc96SEvan Quan 	/* get the voltage */
3031d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
3032d78c227fSMario Limonciello 				   (void *)&vddgfx);
3033e098bc96SEvan Quan 	if (r)
3034e098bc96SEvan Quan 		return r;
3035e098bc96SEvan Quan 
3036a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
3037e098bc96SEvan Quan }
3038e098bc96SEvan Quan 
3039e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
3040e098bc96SEvan Quan 					      struct device_attribute *attr,
3041e098bc96SEvan Quan 					      char *buf)
3042e098bc96SEvan Quan {
3043a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
3044e098bc96SEvan Quan }
3045e098bc96SEvan Quan 
3046e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
3047e098bc96SEvan Quan 				       struct device_attribute *attr,
3048e098bc96SEvan Quan 				       char *buf)
3049e098bc96SEvan Quan {
3050e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3051e098bc96SEvan Quan 	u32 vddnb;
3052d78c227fSMario Limonciello 	int r;
3053e098bc96SEvan Quan 
3054e098bc96SEvan Quan 	/* only APUs have vddnb */
3055e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
3056e098bc96SEvan Quan 		return -EINVAL;
3057e098bc96SEvan Quan 
3058e098bc96SEvan Quan 	/* get the voltage */
3059d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
3060d78c227fSMario Limonciello 				   (void *)&vddnb);
3061e098bc96SEvan Quan 	if (r)
3062e098bc96SEvan Quan 		return r;
3063e098bc96SEvan Quan 
3064a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
3065e098bc96SEvan Quan }
3066e098bc96SEvan Quan 
3067e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
3068e098bc96SEvan Quan 					      struct device_attribute *attr,
3069e098bc96SEvan Quan 					      char *buf)
3070e098bc96SEvan Quan {
3071a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
3072e098bc96SEvan Quan }
3073e098bc96SEvan Quan 
3074a5600853SAlex Deucher static int amdgpu_hwmon_get_power(struct device *dev,
3075d78c227fSMario Limonciello 				  enum amd_pp_sensors sensor)
3076e098bc96SEvan Quan {
3077e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3078d78c227fSMario Limonciello 	unsigned int uw;
3079e098bc96SEvan Quan 	u32 query = 0;
3080d78c227fSMario Limonciello 	int r;
3081e098bc96SEvan Quan 
3082d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
3083e098bc96SEvan Quan 	if (r)
3084e098bc96SEvan Quan 		return r;
3085e098bc96SEvan Quan 
3086e098bc96SEvan Quan 	/* convert to microwatts */
3087e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
3088e098bc96SEvan Quan 
3089d78c227fSMario Limonciello 	return uw;
3090d78c227fSMario Limonciello }
3091d78c227fSMario Limonciello 
3092d78c227fSMario Limonciello static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3093d78c227fSMario Limonciello 					   struct device_attribute *attr,
3094d78c227fSMario Limonciello 					   char *buf)
3095d78c227fSMario Limonciello {
3096d1090194SSrinivasan Shanmugam 	ssize_t val;
3097d78c227fSMario Limonciello 
30989366c2e8SMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
3099d78c227fSMario Limonciello 	if (val < 0)
3100d78c227fSMario Limonciello 		return val;
3101d78c227fSMario Limonciello 
3102d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
3103e098bc96SEvan Quan }
3104e098bc96SEvan Quan 
3105bb9f7b68SMario Limonciello static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
3106bb9f7b68SMario Limonciello 					     struct device_attribute *attr,
3107bb9f7b68SMario Limonciello 					     char *buf)
3108bb9f7b68SMario Limonciello {
3109d1090194SSrinivasan Shanmugam 	ssize_t val;
3110bb9f7b68SMario Limonciello 
311147f1724dSMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
3112bb9f7b68SMario Limonciello 	if (val < 0)
3113bb9f7b68SMario Limonciello 		return val;
3114bb9f7b68SMario Limonciello 
3115d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
3116bb9f7b68SMario Limonciello }
3117bb9f7b68SMario Limonciello 
311891161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
3119e098bc96SEvan Quan 					struct device_attribute *attr,
312091161b06SDarren Powell 					char *buf,
312191161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
3122e098bc96SEvan Quan {
3123e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3124a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
3125a40a020dSDarren Powell 	uint32_t limit;
3126e098bc96SEvan Quan 	ssize_t size;
3127e098bc96SEvan Quan 	int r;
3128e098bc96SEvan Quan 
312953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3130e098bc96SEvan Quan 		return -EPERM;
3131d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3132d2ae842dSAlex Deucher 		return -EPERM;
3133e098bc96SEvan Quan 
31344a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3135e098bc96SEvan Quan 	if (r < 0) {
31364a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3137e098bc96SEvan Quan 		return r;
3138e098bc96SEvan Quan 	}
3139e098bc96SEvan Quan 
314079c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
314104bec521SDarren Powell 				      pp_limit_level, power_type);
3142dc2a8240SDarren Powell 
3143dc2a8240SDarren Powell 	if (!r)
314409b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
3145dc2a8240SDarren Powell 	else
314609b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
3147e098bc96SEvan Quan 
31484a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31494a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3150e098bc96SEvan Quan 
3151e098bc96SEvan Quan 	return size;
3152e098bc96SEvan Quan }
3153e098bc96SEvan Quan 
315419589468SMa Jun static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
315519589468SMa Jun 					 struct device_attribute *attr,
315619589468SMa Jun 					 char *buf)
315719589468SMa Jun {
315819589468SMa Jun 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
315919589468SMa Jun }
316091161b06SDarren Powell 
316191161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
316291161b06SDarren Powell 					 struct device_attribute *attr,
316391161b06SDarren Powell 					 char *buf)
316491161b06SDarren Powell {
316591161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
316691161b06SDarren Powell 
316791161b06SDarren Powell }
316891161b06SDarren Powell 
3169e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3170e098bc96SEvan Quan 					 struct device_attribute *attr,
3171e098bc96SEvan Quan 					 char *buf)
3172e098bc96SEvan Quan {
317391161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
3174e098bc96SEvan Quan 
3175e098bc96SEvan Quan }
3176e098bc96SEvan Quan 
31776e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
31786e58941cSEric Huang 					 struct device_attribute *attr,
31796e58941cSEric Huang 					 char *buf)
31806e58941cSEric Huang {
318191161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
31826e58941cSEric Huang 
31836e58941cSEric Huang }
31846e58941cSEric Huang 
3185ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3186ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
3187ae07970aSXiaomeng Hou 					 char *buf)
3188ae07970aSXiaomeng Hou {
31893b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
31904e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3191ae07970aSXiaomeng Hou 
31928ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
3193a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
31943b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
31953b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
31963b99e8e3SYang Wang 	else
31973b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
3198ae07970aSXiaomeng Hou }
3199e098bc96SEvan Quan 
3200e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3201e098bc96SEvan Quan 		struct device_attribute *attr,
3202e098bc96SEvan Quan 		const char *buf,
3203e098bc96SEvan Quan 		size_t count)
3204e098bc96SEvan Quan {
3205e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3206ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
3207e098bc96SEvan Quan 	int err;
3208e098bc96SEvan Quan 	u32 value;
3209e098bc96SEvan Quan 
321053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3211e098bc96SEvan Quan 		return -EPERM;
3212d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3213d2ae842dSAlex Deucher 		return -EPERM;
3214e098bc96SEvan Quan 
3215e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
3216e098bc96SEvan Quan 		return -EINVAL;
3217e098bc96SEvan Quan 
3218e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
3219e098bc96SEvan Quan 	if (err)
3220e098bc96SEvan Quan 		return err;
3221e098bc96SEvan Quan 
3222e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
3223ae07970aSXiaomeng Hou 	value |= limit_type << 24;
3224e098bc96SEvan Quan 
32254a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3226e098bc96SEvan Quan 	if (err < 0) {
32274a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3228e098bc96SEvan Quan 		return err;
3229e098bc96SEvan Quan 	}
3230e098bc96SEvan Quan 
323179c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
3232e098bc96SEvan Quan 
32334a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
32344a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3235e098bc96SEvan Quan 
3236e098bc96SEvan Quan 	if (err)
3237e098bc96SEvan Quan 		return err;
3238e098bc96SEvan Quan 
3239e098bc96SEvan Quan 	return count;
3240e098bc96SEvan Quan }
3241e098bc96SEvan Quan 
3242e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3243e098bc96SEvan Quan 				      struct device_attribute *attr,
3244e098bc96SEvan Quan 				      char *buf)
3245e098bc96SEvan Quan {
3246e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3247e098bc96SEvan Quan 	uint32_t sclk;
3248d78c227fSMario Limonciello 	int r;
3249e098bc96SEvan Quan 
3250e098bc96SEvan Quan 	/* get the sclk */
3251d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3252d78c227fSMario Limonciello 				   (void *)&sclk);
3253e098bc96SEvan Quan 	if (r)
3254e098bc96SEvan Quan 		return r;
3255e098bc96SEvan Quan 
3256a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3257e098bc96SEvan Quan }
3258e098bc96SEvan Quan 
3259e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3260e098bc96SEvan Quan 					    struct device_attribute *attr,
3261e098bc96SEvan Quan 					    char *buf)
3262e098bc96SEvan Quan {
3263a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
3264e098bc96SEvan Quan }
3265e098bc96SEvan Quan 
3266e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3267e098bc96SEvan Quan 				      struct device_attribute *attr,
3268e098bc96SEvan Quan 				      char *buf)
3269e098bc96SEvan Quan {
3270e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3271e098bc96SEvan Quan 	uint32_t mclk;
3272d78c227fSMario Limonciello 	int r;
3273e098bc96SEvan Quan 
3274e098bc96SEvan Quan 	/* get the sclk */
3275d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3276d78c227fSMario Limonciello 				   (void *)&mclk);
3277e098bc96SEvan Quan 	if (r)
3278e098bc96SEvan Quan 		return r;
3279e098bc96SEvan Quan 
3280a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3281e098bc96SEvan Quan }
3282e098bc96SEvan Quan 
3283e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3284e098bc96SEvan Quan 					    struct device_attribute *attr,
3285e098bc96SEvan Quan 					    char *buf)
3286e098bc96SEvan Quan {
3287a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3288e098bc96SEvan Quan }
3289e098bc96SEvan Quan 
3290e098bc96SEvan Quan /**
3291e098bc96SEvan Quan  * DOC: hwmon
3292e098bc96SEvan Quan  *
3293e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3294e098bc96SEvan Quan  *
3295e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3296e098bc96SEvan Quan  *
3297e098bc96SEvan Quan  * - GPU voltage
3298e098bc96SEvan Quan  *
3299e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3300e098bc96SEvan Quan  *
3301e098bc96SEvan Quan  * - GPU power
3302e098bc96SEvan Quan  *
3303e098bc96SEvan Quan  * - GPU fan
3304e098bc96SEvan Quan  *
3305e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3306e098bc96SEvan Quan  *
3307e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3308e098bc96SEvan Quan  *
3309e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3310e098bc96SEvan Quan  *
3311e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3312e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3313e098bc96SEvan Quan  *
3314e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3315e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3316e098bc96SEvan Quan  *
3317e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3318e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3319e098bc96SEvan Quan  *
3320e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3321e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3322e098bc96SEvan Quan  *
3323e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3324e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3325e098bc96SEvan Quan  *
3326e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3327e098bc96SEvan Quan  *
3328e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3329e098bc96SEvan Quan  *
3330e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3331e098bc96SEvan Quan  *
3332e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3333e098bc96SEvan Quan  *
333429f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3335e098bc96SEvan Quan  *
3336bb9f7b68SMario Limonciello  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3337bb9f7b68SMario Limonciello  *
3338e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3339e098bc96SEvan Quan  *
3340e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3341e098bc96SEvan Quan  *
3342e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3343e098bc96SEvan Quan  *
3344e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3345e098bc96SEvan Quan  *
3346e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3347e098bc96SEvan Quan  *
3348e098bc96SEvan 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)
3349e098bc96SEvan Quan  *
3350e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3351e098bc96SEvan Quan  *
3352e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3353e098bc96SEvan Quan  *
3354e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3355e098bc96SEvan Quan  *
3356e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3357e098bc96SEvan Quan  *
3358e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3359e098bc96SEvan Quan  *
3360e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3361e098bc96SEvan Quan  *
3362e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3363e098bc96SEvan Quan  *
336496401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
336596401f7cSEvan Quan  *       That will get the former one overridden.
336696401f7cSEvan Quan  *
3367e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3368e098bc96SEvan Quan  *
3369e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3370e098bc96SEvan Quan  *
3371e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3372e098bc96SEvan Quan  *
3373e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3374e098bc96SEvan Quan  *
3375e098bc96SEvan Quan  */
3376e098bc96SEvan Quan 
3377e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3378e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3379e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3380e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3381e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3382e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3383e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3384e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3385e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3386e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3387e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3388e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3389e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3390e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3391e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3392e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3393e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3394e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3395e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3396e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3397e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3398e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3399e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3400e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3401e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3402e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3403e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3404e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3405e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3406bb9f7b68SMario Limonciello static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3407e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3408e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3409e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
34106e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3411ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3412ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3413ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3414ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3415ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
34166e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3417ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3418e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3419e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3420e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3421e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3422e098bc96SEvan Quan 
3423e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3424e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3425e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3426e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3427e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3428e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3429e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3430e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3431e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3432e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3433e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3434e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3435e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3436e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3437e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3438e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3439e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3440e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3441e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3442e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3443e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3444e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3445e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3446e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3447e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3448e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3449e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3450e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3451e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3452e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3453bb9f7b68SMario Limonciello 	&sensor_dev_attr_power1_input.dev_attr.attr,
3454e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3455e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3456e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
34576e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3458ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3459ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3460ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3461ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3462ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
34636e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3464ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3465e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3466e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3467e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3468e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3469e098bc96SEvan Quan 	NULL
3470e098bc96SEvan Quan };
3471e098bc96SEvan Quan 
3472e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3473e098bc96SEvan Quan 					struct attribute *attr, int index)
3474e098bc96SEvan Quan {
3475e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3476e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3477e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
34784e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
347915419813SMario Limonciello 	uint32_t tmp;
3480e098bc96SEvan Quan 
34814f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
34824f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
34834f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
34844f0f1b58SDanijel Slivka 
3485e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3486e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3487e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3488e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3489e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3490e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3491e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3492e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3493e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3494e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3495e098bc96SEvan Quan 		return 0;
3496e098bc96SEvan Quan 
3497e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3498e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3499e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3500e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3501e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3502e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3503e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3504e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3505e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3506e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3507e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3508e098bc96SEvan Quan 		return 0;
3509e098bc96SEvan Quan 
3510e098bc96SEvan Quan 	/* Skip crit temp on APU */
35118572fa2aSAsad Kamal 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
35128572fa2aSAsad Kamal 	    (gc_ver == IP_VERSION(9, 4, 3))) &&
3513e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3514e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3515e098bc96SEvan Quan 		return 0;
3516e098bc96SEvan Quan 
3517e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3518e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3519e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3520e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3521e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3522e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3523e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3524e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3525e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3526e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3527e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3528e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3529e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3530e098bc96SEvan Quan 		return 0;
3531e098bc96SEvan Quan 
3532e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3533685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3534e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3535685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3536e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3537e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3538e098bc96SEvan Quan 
3539685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3540e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3541685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3542e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3543e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3544e098bc96SEvan Quan 
35458572fa2aSAsad Kamal 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3546ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
35478572fa2aSAsad Kamal 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
35488572fa2aSAsad Kamal 	      (gc_ver != IP_VERSION(9, 4, 3)))) &&
3549367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3550e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
35516e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
35526e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3553e098bc96SEvan Quan 		return 0;
3554e098bc96SEvan Quan 
355589317d42SGuilherme G. Piccoli 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3556367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
35578ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3558367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3559367deb67SAlex Deucher 		return 0;
3560367deb67SAlex Deucher 
356115419813SMario Limonciello 	/* not all products support both average and instantaneous */
356215419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
356315419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
356415419813SMario Limonciello 		return 0;
356515419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
356615419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
356715419813SMario Limonciello 		return 0;
356815419813SMario Limonciello 
3569e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3570685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3571685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3572685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3573685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3574e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3575e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3576e098bc96SEvan Quan 		return 0;
3577e098bc96SEvan Quan 
3578685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3579685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3580e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3581e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3582e098bc96SEvan Quan 		return 0;
3583e098bc96SEvan Quan 
3584e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
35858572fa2aSAsad Kamal 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
35868572fa2aSAsad Kamal 	     (gc_ver == IP_VERSION(9, 4, 3))) &&
3587e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3588e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3589e098bc96SEvan Quan 		return 0;
3590e098bc96SEvan Quan 
35918572fa2aSAsad Kamal 	/* only APUs other than gc 9,4,3 have vddnb */
35928572fa2aSAsad Kamal 	if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) &&
3593e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3594e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3595e098bc96SEvan Quan 		return 0;
3596e098bc96SEvan Quan 
35978572fa2aSAsad Kamal 	/* no mclk on APUs other than gc 9,4,3*/
35988572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3599e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3600e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3601e098bc96SEvan Quan 		return 0;
3602e098bc96SEvan Quan 
36038ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
36048572fa2aSAsad Kamal 	    (gc_ver != IP_VERSION(9, 4, 3)) &&
36058572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3606bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
360707864911SAsad Kamal 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3608bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
360907864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
361007864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
36118572fa2aSAsad Kamal 		return 0;
36128572fa2aSAsad Kamal 
3613bfb4fd20SAsad Kamal 	/* hotspot temperature for gc 9,4,3*/
36149cff0879SLijo Lazar 	if (gc_ver == IP_VERSION(9, 4, 3)) {
36159cff0879SLijo Lazar 		if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
36169cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
36179cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
36188572fa2aSAsad Kamal 			return 0;
36198572fa2aSAsad Kamal 
36209cff0879SLijo Lazar 		if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
36219cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
36229cff0879SLijo Lazar 			return attr->mode;
36239cff0879SLijo Lazar 	}
36249cff0879SLijo Lazar 
36258572fa2aSAsad Kamal 	/* only SOC15 dGPUs support hotspot and mem temperatures */
36269cff0879SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
362707864911SAsad Kamal 	    (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3628e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3629e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3630e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3631bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3632e098bc96SEvan Quan 		return 0;
3633e098bc96SEvan Quan 
3634ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
36358ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3636ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3637ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3638ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3639ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
36406e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3641de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3642ae07970aSXiaomeng Hou 		return 0;
3643ae07970aSXiaomeng Hou 
3644e098bc96SEvan Quan 	return effective_mode;
3645e098bc96SEvan Quan }
3646e098bc96SEvan Quan 
3647e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3648e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3649e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3650e098bc96SEvan Quan };
3651e098bc96SEvan Quan 
3652e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3653e098bc96SEvan Quan 	&hwmon_attrgroup,
3654e098bc96SEvan Quan 	NULL
3655e098bc96SEvan Quan };
3656e098bc96SEvan Quan 
3657d7bf1b55SEvan Quan static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3658d7bf1b55SEvan Quan 				       enum pp_clock_type od_type,
3659d7bf1b55SEvan Quan 				       char *buf)
3660d7bf1b55SEvan Quan {
3661d7bf1b55SEvan Quan 	int size = 0;
3662d7bf1b55SEvan Quan 	int ret;
3663d7bf1b55SEvan Quan 
3664d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3665d7bf1b55SEvan Quan 		return -EPERM;
3666d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3667d7bf1b55SEvan Quan 		return -EPERM;
3668d7bf1b55SEvan Quan 
3669d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3670d7bf1b55SEvan Quan 	if (ret < 0) {
3671d7bf1b55SEvan Quan 		pm_runtime_put_autosuspend(adev->dev);
3672d7bf1b55SEvan Quan 		return ret;
3673d7bf1b55SEvan Quan 	}
3674d7bf1b55SEvan Quan 
3675d7bf1b55SEvan Quan 	size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3676d7bf1b55SEvan Quan 	if (size == 0)
3677d7bf1b55SEvan Quan 		size = sysfs_emit(buf, "\n");
3678d7bf1b55SEvan Quan 
3679d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3680d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3681d7bf1b55SEvan Quan 
3682d7bf1b55SEvan Quan 	return size;
3683d7bf1b55SEvan Quan }
3684d7bf1b55SEvan Quan 
3685d7bf1b55SEvan Quan static int parse_input_od_command_lines(const char *buf,
3686d7bf1b55SEvan Quan 					size_t count,
3687d7bf1b55SEvan Quan 					u32 *type,
3688d7bf1b55SEvan Quan 					long *params,
3689d7bf1b55SEvan Quan 					uint32_t *num_of_params)
3690d7bf1b55SEvan Quan {
3691d7bf1b55SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
3692d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3693d7bf1b55SEvan Quan 	char buf_cpy[128] = {0};
3694d7bf1b55SEvan Quan 	char *tmp_str, *sub_str;
3695d7bf1b55SEvan Quan 	int ret;
3696d7bf1b55SEvan Quan 
3697d7bf1b55SEvan Quan 	if (count > sizeof(buf_cpy) - 1)
3698d7bf1b55SEvan Quan 		return -EINVAL;
3699d7bf1b55SEvan Quan 
3700d7bf1b55SEvan Quan 	memcpy(buf_cpy, buf, count);
3701d7bf1b55SEvan Quan 	tmp_str = buf_cpy;
3702d7bf1b55SEvan Quan 
3703d7bf1b55SEvan Quan 	/* skip heading spaces */
3704d7bf1b55SEvan Quan 	while (isspace(*tmp_str))
3705d7bf1b55SEvan Quan 		tmp_str++;
3706d7bf1b55SEvan Quan 
3707d7bf1b55SEvan Quan 	switch (*tmp_str) {
3708d7bf1b55SEvan Quan 	case 'c':
3709d7bf1b55SEvan Quan 		*type = PP_OD_COMMIT_DPM_TABLE;
3710d7bf1b55SEvan Quan 		return 0;
3711f7f9e48fSMa Jun 	case 'r':
3712f7f9e48fSMa Jun 		params[parameter_size] = *type;
3713f7f9e48fSMa Jun 		*num_of_params = 1;
3714f7f9e48fSMa Jun 		*type = PP_OD_RESTORE_DEFAULT_TABLE;
3715f7f9e48fSMa Jun 		return 0;
3716d7bf1b55SEvan Quan 	default:
3717d7bf1b55SEvan Quan 		break;
3718d7bf1b55SEvan Quan 	}
3719d7bf1b55SEvan Quan 
3720d7bf1b55SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3721d7bf1b55SEvan Quan 		if (strlen(sub_str) == 0)
3722d7bf1b55SEvan Quan 			continue;
3723d7bf1b55SEvan Quan 
3724d7bf1b55SEvan Quan 		ret = kstrtol(sub_str, 0, &params[parameter_size]);
3725d7bf1b55SEvan Quan 		if (ret)
3726d7bf1b55SEvan Quan 			return -EINVAL;
3727d7bf1b55SEvan Quan 		parameter_size++;
3728d7bf1b55SEvan Quan 
3729d7bf1b55SEvan Quan 		while (isspace(*tmp_str))
3730d7bf1b55SEvan Quan 			tmp_str++;
3731d7bf1b55SEvan Quan 	}
3732d7bf1b55SEvan Quan 
3733d7bf1b55SEvan Quan 	*num_of_params = parameter_size;
3734d7bf1b55SEvan Quan 
3735d7bf1b55SEvan Quan 	return 0;
3736d7bf1b55SEvan Quan }
3737d7bf1b55SEvan Quan 
3738d7bf1b55SEvan Quan static int
3739d7bf1b55SEvan Quan amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3740d7bf1b55SEvan Quan 				     enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3741d7bf1b55SEvan Quan 				     const char *in_buf,
3742d7bf1b55SEvan Quan 				     size_t count)
3743d7bf1b55SEvan Quan {
3744d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3745d7bf1b55SEvan Quan 	long parameter[64];
3746d7bf1b55SEvan Quan 	int ret;
3747d7bf1b55SEvan Quan 
3748d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3749d7bf1b55SEvan Quan 		return -EPERM;
3750d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3751d7bf1b55SEvan Quan 		return -EPERM;
3752d7bf1b55SEvan Quan 
3753d7bf1b55SEvan Quan 	ret = parse_input_od_command_lines(in_buf,
3754d7bf1b55SEvan Quan 					   count,
3755d7bf1b55SEvan Quan 					   &cmd_type,
3756d7bf1b55SEvan Quan 					   parameter,
3757d7bf1b55SEvan Quan 					   &parameter_size);
3758d7bf1b55SEvan Quan 	if (ret)
3759d7bf1b55SEvan Quan 		return ret;
3760d7bf1b55SEvan Quan 
3761d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3762d7bf1b55SEvan Quan 	if (ret < 0)
3763d7bf1b55SEvan Quan 		goto err_out0;
3764d7bf1b55SEvan Quan 
3765d7bf1b55SEvan Quan 	ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3766d7bf1b55SEvan Quan 					    cmd_type,
3767d7bf1b55SEvan Quan 					    parameter,
3768d7bf1b55SEvan Quan 					    parameter_size);
3769d7bf1b55SEvan Quan 	if (ret)
3770d7bf1b55SEvan Quan 		goto err_out1;
3771d7bf1b55SEvan Quan 
3772d7bf1b55SEvan Quan 	if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3773d7bf1b55SEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
3774d7bf1b55SEvan Quan 					       AMD_PP_TASK_READJUST_POWER_STATE,
3775d7bf1b55SEvan Quan 					       NULL);
3776d7bf1b55SEvan Quan 		if (ret)
3777d7bf1b55SEvan Quan 			goto err_out1;
3778d7bf1b55SEvan Quan 	}
3779d7bf1b55SEvan Quan 
3780d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3781d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3782d7bf1b55SEvan Quan 
3783d7bf1b55SEvan Quan 	return count;
3784d7bf1b55SEvan Quan 
3785d7bf1b55SEvan Quan err_out1:
3786d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3787d7bf1b55SEvan Quan err_out0:
3788d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3789d7bf1b55SEvan Quan 
3790d7bf1b55SEvan Quan 	return ret;
3791d7bf1b55SEvan Quan }
3792d7bf1b55SEvan Quan 
3793d7bf1b55SEvan Quan /**
3794d7bf1b55SEvan Quan  * DOC: fan_curve
3795d7bf1b55SEvan Quan  *
3796d7bf1b55SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3797d7bf1b55SEvan Quan  * control curve line.
3798d7bf1b55SEvan Quan  *
3799d7bf1b55SEvan Quan  * Reading back the file shows you the current settings(temperature in Celsius
3800d7bf1b55SEvan Quan  * degree and fan speed in pwm) applied to every anchor point of the curve line
3801d7bf1b55SEvan Quan  * and their permitted ranges if changable.
3802d7bf1b55SEvan Quan  *
3803d7bf1b55SEvan Quan  * Writing a desired string(with the format like "anchor_point_index temperature
3804d7bf1b55SEvan Quan  * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3805d7bf1b55SEvan Quan  * point accordingly.
3806d7bf1b55SEvan Quan  *
3807d7bf1b55SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3808d7bf1b55SEvan Quan  * your changes.
3809d7bf1b55SEvan Quan  *
3810f7f9e48fSMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
3811f7f9e48fSMa Jun  * reset them
3812f7f9e48fSMa Jun  *
3813d7bf1b55SEvan Quan  * There are two fan control modes supported: auto and manual. With auto mode,
3814d7bf1b55SEvan Quan  * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3815d7bf1b55SEvan Quan  * While with manual mode, users can set their own fan curve line as what
3816d7bf1b55SEvan Quan  * described here. Normally the ASIC is booted up with auto mode. Any
3817d7bf1b55SEvan Quan  * settings via this interface will switch the fan control to manual mode
3818d7bf1b55SEvan Quan  * implicitly.
3819d7bf1b55SEvan Quan  */
3820d7bf1b55SEvan Quan static ssize_t fan_curve_show(struct kobject *kobj,
3821d7bf1b55SEvan Quan 			      struct kobj_attribute *attr,
3822d7bf1b55SEvan Quan 			      char *buf)
3823d7bf1b55SEvan Quan {
3824d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3825d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3826d7bf1b55SEvan Quan 
3827d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3828d7bf1b55SEvan Quan }
3829d7bf1b55SEvan Quan 
3830d7bf1b55SEvan Quan static ssize_t fan_curve_store(struct kobject *kobj,
3831d7bf1b55SEvan Quan 			       struct kobj_attribute *attr,
3832d7bf1b55SEvan Quan 			       const char *buf,
3833d7bf1b55SEvan Quan 			       size_t count)
3834d7bf1b55SEvan Quan {
3835d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3836d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3837d7bf1b55SEvan Quan 
3838d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3839d7bf1b55SEvan Quan 							     PP_OD_EDIT_FAN_CURVE,
3840d7bf1b55SEvan Quan 							     buf,
3841d7bf1b55SEvan Quan 							     count);
3842d7bf1b55SEvan Quan }
3843d7bf1b55SEvan Quan 
3844d7bf1b55SEvan Quan static umode_t fan_curve_visible(struct amdgpu_device *adev)
3845d7bf1b55SEvan Quan {
3846d7bf1b55SEvan Quan 	umode_t umode = 0000;
3847d7bf1b55SEvan Quan 
3848d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3849d7bf1b55SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3850d7bf1b55SEvan Quan 
3851d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3852d7bf1b55SEvan Quan 		umode |= S_IWUSR;
3853d7bf1b55SEvan Quan 
3854d7bf1b55SEvan Quan 	return umode;
3855d7bf1b55SEvan Quan }
3856d7bf1b55SEvan Quan 
3857548009adSEvan Quan /**
3858548009adSEvan Quan  * DOC: acoustic_limit_rpm_threshold
3859548009adSEvan Quan  *
3860548009adSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
3861548009adSEvan Quan  * acoustic limit in RPM for fan control.
3862548009adSEvan Quan  *
3863548009adSEvan Quan  * Reading back the file shows you the current setting and the permitted
3864548009adSEvan Quan  * ranges if changable.
3865548009adSEvan Quan  *
3866548009adSEvan Quan  * Writing an integer to the file, change the setting accordingly.
3867548009adSEvan Quan  *
3868548009adSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3869548009adSEvan Quan  * your changes.
3870548009adSEvan Quan  *
38711007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
38721007bc36SMa Jun  * reset them
38731007bc36SMa Jun  *
3874548009adSEvan Quan  * This setting works under auto fan control mode only. It adjusts the PMFW's
3875548009adSEvan Quan  * behavior about the maximum speed in RPM the fan can spin. Setting via this
3876548009adSEvan Quan  * interface will switch the fan control to auto mode implicitly.
3877548009adSEvan Quan  */
3878548009adSEvan Quan static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
3879548009adSEvan Quan 					     struct kobj_attribute *attr,
3880548009adSEvan Quan 					     char *buf)
3881548009adSEvan Quan {
3882548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3883548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3884548009adSEvan Quan 
3885548009adSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
3886548009adSEvan Quan }
3887548009adSEvan Quan 
3888548009adSEvan Quan static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
3889548009adSEvan Quan 					      struct kobj_attribute *attr,
3890548009adSEvan Quan 					      const char *buf,
3891548009adSEvan Quan 					      size_t count)
3892548009adSEvan Quan {
3893548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3894548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3895548009adSEvan Quan 
3896548009adSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3897548009adSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_LIMIT,
3898548009adSEvan Quan 							     buf,
3899548009adSEvan Quan 							     count);
3900548009adSEvan Quan }
3901548009adSEvan Quan 
3902548009adSEvan Quan static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
3903548009adSEvan Quan {
3904548009adSEvan Quan 	umode_t umode = 0000;
3905548009adSEvan Quan 
3906548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
3907548009adSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3908548009adSEvan Quan 
3909548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
3910548009adSEvan Quan 		umode |= S_IWUSR;
3911548009adSEvan Quan 
3912548009adSEvan Quan 	return umode;
3913548009adSEvan Quan }
3914548009adSEvan Quan 
391547cf6fcbSEvan Quan /**
391647cf6fcbSEvan Quan  * DOC: acoustic_target_rpm_threshold
391747cf6fcbSEvan Quan  *
391847cf6fcbSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
391947cf6fcbSEvan Quan  * acoustic target in RPM for fan control.
392047cf6fcbSEvan Quan  *
392147cf6fcbSEvan Quan  * Reading back the file shows you the current setting and the permitted
392247cf6fcbSEvan Quan  * ranges if changable.
392347cf6fcbSEvan Quan  *
392447cf6fcbSEvan Quan  * Writing an integer to the file, change the setting accordingly.
392547cf6fcbSEvan Quan  *
392647cf6fcbSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
392747cf6fcbSEvan Quan  * your changes.
392847cf6fcbSEvan Quan  *
39291007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
39301007bc36SMa Jun  * reset them
39311007bc36SMa Jun  *
393247cf6fcbSEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
393347cf6fcbSEvan Quan  * other settings which can work also under auto mode. It adjusts the PMFW's
393447cf6fcbSEvan Quan  * behavior about the maximum speed in RPM the fan can spin when ASIC
393547cf6fcbSEvan Quan  * temperature is not greater than target temperature. Setting via this
393647cf6fcbSEvan Quan  * interface will switch the fan control to auto mode implicitly.
393747cf6fcbSEvan Quan  */
393847cf6fcbSEvan Quan static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
393947cf6fcbSEvan Quan 					      struct kobj_attribute *attr,
394047cf6fcbSEvan Quan 					      char *buf)
394147cf6fcbSEvan Quan {
394247cf6fcbSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
394347cf6fcbSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
394447cf6fcbSEvan Quan 
394547cf6fcbSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
394647cf6fcbSEvan Quan }
394747cf6fcbSEvan Quan 
394847cf6fcbSEvan Quan static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
394947cf6fcbSEvan Quan 					       struct kobj_attribute *attr,
395047cf6fcbSEvan Quan 					       const char *buf,
395147cf6fcbSEvan Quan 					       size_t count)
395247cf6fcbSEvan Quan {
395347cf6fcbSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
395447cf6fcbSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
395547cf6fcbSEvan Quan 
395647cf6fcbSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
395747cf6fcbSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_TARGET,
395847cf6fcbSEvan Quan 							     buf,
395947cf6fcbSEvan Quan 							     count);
396047cf6fcbSEvan Quan }
396147cf6fcbSEvan Quan 
396247cf6fcbSEvan Quan static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
396347cf6fcbSEvan Quan {
396447cf6fcbSEvan Quan 	umode_t umode = 0000;
396547cf6fcbSEvan Quan 
396647cf6fcbSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
396747cf6fcbSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
396847cf6fcbSEvan Quan 
396947cf6fcbSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
397047cf6fcbSEvan Quan 		umode |= S_IWUSR;
397147cf6fcbSEvan Quan 
397247cf6fcbSEvan Quan 	return umode;
397347cf6fcbSEvan Quan }
397447cf6fcbSEvan Quan 
3975eedd5a34SEvan Quan /**
3976eedd5a34SEvan Quan  * DOC: fan_target_temperature
3977eedd5a34SEvan Quan  *
3978eedd5a34SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
3979eedd5a34SEvan Quan  * target tempeature in Celsius degree for fan control.
3980eedd5a34SEvan Quan  *
3981eedd5a34SEvan Quan  * Reading back the file shows you the current setting and the permitted
3982eedd5a34SEvan Quan  * ranges if changable.
3983eedd5a34SEvan Quan  *
3984eedd5a34SEvan Quan  * Writing an integer to the file, change the setting accordingly.
3985eedd5a34SEvan Quan  *
3986eedd5a34SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3987eedd5a34SEvan Quan  * your changes.
3988eedd5a34SEvan Quan  *
39891007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
39901007bc36SMa Jun  * reset them
39911007bc36SMa Jun  *
3992eedd5a34SEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
3993eedd5a34SEvan Quan  * other settings which can work also under auto mode. Paring with the
3994eedd5a34SEvan Quan  * acoustic_target_rpm_threshold setting, they define the maximum speed in
3995eedd5a34SEvan Quan  * RPM the fan can spin when ASIC temperature is not greater than target
3996eedd5a34SEvan Quan  * temperature. Setting via this interface will switch the fan control to
3997eedd5a34SEvan Quan  * auto mode implicitly.
3998eedd5a34SEvan Quan  */
3999eedd5a34SEvan Quan static ssize_t fan_target_temperature_show(struct kobject *kobj,
4000eedd5a34SEvan Quan 					   struct kobj_attribute *attr,
4001eedd5a34SEvan Quan 					   char *buf)
4002eedd5a34SEvan Quan {
4003eedd5a34SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4004eedd5a34SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4005eedd5a34SEvan Quan 
4006eedd5a34SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
4007eedd5a34SEvan Quan }
4008eedd5a34SEvan Quan 
4009eedd5a34SEvan Quan static ssize_t fan_target_temperature_store(struct kobject *kobj,
4010eedd5a34SEvan Quan 					    struct kobj_attribute *attr,
4011eedd5a34SEvan Quan 					    const char *buf,
4012eedd5a34SEvan Quan 					    size_t count)
4013eedd5a34SEvan Quan {
4014eedd5a34SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4015eedd5a34SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4016eedd5a34SEvan Quan 
4017eedd5a34SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4018eedd5a34SEvan Quan 							     PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
4019eedd5a34SEvan Quan 							     buf,
4020eedd5a34SEvan Quan 							     count);
4021eedd5a34SEvan Quan }
4022eedd5a34SEvan Quan 
4023eedd5a34SEvan Quan static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
4024eedd5a34SEvan Quan {
4025eedd5a34SEvan Quan 	umode_t umode = 0000;
4026eedd5a34SEvan Quan 
4027eedd5a34SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
4028eedd5a34SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4029eedd5a34SEvan Quan 
4030eedd5a34SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
4031eedd5a34SEvan Quan 		umode |= S_IWUSR;
4032eedd5a34SEvan Quan 
4033eedd5a34SEvan Quan 	return umode;
4034eedd5a34SEvan Quan }
4035eedd5a34SEvan Quan 
40369df5d008SEvan Quan /**
40379df5d008SEvan Quan  * DOC: fan_minimum_pwm
40389df5d008SEvan Quan  *
40399df5d008SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
40409df5d008SEvan Quan  * minimum fan speed in PWM.
40419df5d008SEvan Quan  *
40429df5d008SEvan Quan  * Reading back the file shows you the current setting and the permitted
40439df5d008SEvan Quan  * ranges if changable.
40449df5d008SEvan Quan  *
40459df5d008SEvan Quan  * Writing an integer to the file, change the setting accordingly.
40469df5d008SEvan Quan  *
40479df5d008SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
40489df5d008SEvan Quan  * your changes.
40499df5d008SEvan Quan  *
40501007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
40511007bc36SMa Jun  * reset them
40521007bc36SMa Jun  *
40539df5d008SEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
40549df5d008SEvan Quan  * other settings which can work also under auto mode. It adjusts the PMFW's
40559df5d008SEvan Quan  * behavior about the minimum fan speed in PWM the fan should spin. Setting
40569df5d008SEvan Quan  * via this interface will switch the fan control to auto mode implicitly.
40579df5d008SEvan Quan  */
40589df5d008SEvan Quan static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
40599df5d008SEvan Quan 				    struct kobj_attribute *attr,
40609df5d008SEvan Quan 				    char *buf)
40619df5d008SEvan Quan {
40629df5d008SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
40639df5d008SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
40649df5d008SEvan Quan 
40659df5d008SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
40669df5d008SEvan Quan }
40679df5d008SEvan Quan 
40689df5d008SEvan Quan static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
40699df5d008SEvan Quan 				     struct kobj_attribute *attr,
40709df5d008SEvan Quan 				     const char *buf,
40719df5d008SEvan Quan 				     size_t count)
40729df5d008SEvan Quan {
40739df5d008SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
40749df5d008SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
40759df5d008SEvan Quan 
40769df5d008SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
40779df5d008SEvan Quan 							     PP_OD_EDIT_FAN_MINIMUM_PWM,
40789df5d008SEvan Quan 							     buf,
40799df5d008SEvan Quan 							     count);
40809df5d008SEvan Quan }
40819df5d008SEvan Quan 
40829df5d008SEvan Quan static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
40839df5d008SEvan Quan {
40849df5d008SEvan Quan 	umode_t umode = 0000;
40859df5d008SEvan Quan 
40869df5d008SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
40879df5d008SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
40889df5d008SEvan Quan 
40899df5d008SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
40909df5d008SEvan Quan 		umode |= S_IWUSR;
40919df5d008SEvan Quan 
40929df5d008SEvan Quan 	return umode;
40939df5d008SEvan Quan }
40949df5d008SEvan Quan 
4095d7bf1b55SEvan Quan static struct od_feature_set amdgpu_od_set = {
4096d7bf1b55SEvan Quan 	.containers = {
4097d7bf1b55SEvan Quan 		[0] = {
4098d7bf1b55SEvan Quan 			.name = "fan_ctrl",
4099d7bf1b55SEvan Quan 			.sub_feature = {
4100d7bf1b55SEvan Quan 				[0] = {
4101d7bf1b55SEvan Quan 					.name = "fan_curve",
4102d7bf1b55SEvan Quan 					.ops = {
4103d7bf1b55SEvan Quan 						.is_visible = fan_curve_visible,
4104d7bf1b55SEvan Quan 						.show = fan_curve_show,
4105d7bf1b55SEvan Quan 						.store = fan_curve_store,
4106d7bf1b55SEvan Quan 					},
4107d7bf1b55SEvan Quan 				},
4108548009adSEvan Quan 				[1] = {
4109548009adSEvan Quan 					.name = "acoustic_limit_rpm_threshold",
4110548009adSEvan Quan 					.ops = {
4111548009adSEvan Quan 						.is_visible = acoustic_limit_threshold_visible,
4112548009adSEvan Quan 						.show = acoustic_limit_threshold_show,
4113548009adSEvan Quan 						.store = acoustic_limit_threshold_store,
4114548009adSEvan Quan 					},
4115548009adSEvan Quan 				},
411647cf6fcbSEvan Quan 				[2] = {
411747cf6fcbSEvan Quan 					.name = "acoustic_target_rpm_threshold",
411847cf6fcbSEvan Quan 					.ops = {
411947cf6fcbSEvan Quan 						.is_visible = acoustic_target_threshold_visible,
412047cf6fcbSEvan Quan 						.show = acoustic_target_threshold_show,
412147cf6fcbSEvan Quan 						.store = acoustic_target_threshold_store,
412247cf6fcbSEvan Quan 					},
412347cf6fcbSEvan Quan 				},
4124eedd5a34SEvan Quan 				[3] = {
4125eedd5a34SEvan Quan 					.name = "fan_target_temperature",
4126eedd5a34SEvan Quan 					.ops = {
4127eedd5a34SEvan Quan 						.is_visible = fan_target_temperature_visible,
4128eedd5a34SEvan Quan 						.show = fan_target_temperature_show,
4129eedd5a34SEvan Quan 						.store = fan_target_temperature_store,
4130eedd5a34SEvan Quan 					},
4131eedd5a34SEvan Quan 				},
41329df5d008SEvan Quan 				[4] = {
41339df5d008SEvan Quan 					.name = "fan_minimum_pwm",
41349df5d008SEvan Quan 					.ops = {
41359df5d008SEvan Quan 						.is_visible = fan_minimum_pwm_visible,
41369df5d008SEvan Quan 						.show = fan_minimum_pwm_show,
41379df5d008SEvan Quan 						.store = fan_minimum_pwm_store,
41389df5d008SEvan Quan 					},
41399df5d008SEvan Quan 				},
4140d7bf1b55SEvan Quan 			},
4141d7bf1b55SEvan Quan 		},
4142d7bf1b55SEvan Quan 	},
4143d7bf1b55SEvan Quan };
41443e38b634SEvan Quan 
41453e38b634SEvan Quan static void od_kobj_release(struct kobject *kobj)
41463e38b634SEvan Quan {
41473e38b634SEvan Quan 	struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
41483e38b634SEvan Quan 
41493e38b634SEvan Quan 	kfree(od_kobj);
41503e38b634SEvan Quan }
41513e38b634SEvan Quan 
41523e38b634SEvan Quan static const struct kobj_type od_ktype = {
41533e38b634SEvan Quan 	.release	= od_kobj_release,
41543e38b634SEvan Quan 	.sysfs_ops	= &kobj_sysfs_ops,
41553e38b634SEvan Quan };
41563e38b634SEvan Quan 
41573e38b634SEvan Quan static void amdgpu_od_set_fini(struct amdgpu_device *adev)
41583e38b634SEvan Quan {
41593e38b634SEvan Quan 	struct od_kobj *container, *container_next;
41603e38b634SEvan Quan 	struct od_attribute *attribute, *attribute_next;
41613e38b634SEvan Quan 
41623e38b634SEvan Quan 	if (list_empty(&adev->pm.od_kobj_list))
41633e38b634SEvan Quan 		return;
41643e38b634SEvan Quan 
41653e38b634SEvan Quan 	list_for_each_entry_safe(container, container_next,
41663e38b634SEvan Quan 				 &adev->pm.od_kobj_list, entry) {
41673e38b634SEvan Quan 		list_del(&container->entry);
41683e38b634SEvan Quan 
41693e38b634SEvan Quan 		list_for_each_entry_safe(attribute, attribute_next,
41703e38b634SEvan Quan 					 &container->attribute, entry) {
41713e38b634SEvan Quan 			list_del(&attribute->entry);
41723e38b634SEvan Quan 			sysfs_remove_file(&container->kobj,
41733e38b634SEvan Quan 					  &attribute->attribute.attr);
41743e38b634SEvan Quan 			kfree(attribute);
41753e38b634SEvan Quan 		}
41763e38b634SEvan Quan 
41773e38b634SEvan Quan 		kobject_put(&container->kobj);
41783e38b634SEvan Quan 	}
41793e38b634SEvan Quan }
41803e38b634SEvan Quan 
41813e38b634SEvan Quan static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
41823e38b634SEvan Quan 					   struct od_feature_ops *feature_ops)
41833e38b634SEvan Quan {
41843e38b634SEvan Quan 	umode_t mode;
41853e38b634SEvan Quan 
41863e38b634SEvan Quan 	if (!feature_ops->is_visible)
41873e38b634SEvan Quan 		return false;
41883e38b634SEvan Quan 
41893e38b634SEvan Quan 	/*
41903e38b634SEvan Quan 	 * If the feature has no user read and write mode set,
41913e38b634SEvan Quan 	 * we can assume the feature is actually not supported.(?)
41923e38b634SEvan Quan 	 * And the revelant sysfs interface should not be exposed.
41933e38b634SEvan Quan 	 */
41943e38b634SEvan Quan 	mode = feature_ops->is_visible(adev);
41953e38b634SEvan Quan 	if (mode & (S_IRUSR | S_IWUSR))
41963e38b634SEvan Quan 		return true;
41973e38b634SEvan Quan 
41983e38b634SEvan Quan 	return false;
41993e38b634SEvan Quan }
42003e38b634SEvan Quan 
42013e38b634SEvan Quan static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
42023e38b634SEvan Quan 					struct od_feature_container *container)
42033e38b634SEvan Quan {
42043e38b634SEvan Quan 	int i;
42053e38b634SEvan Quan 
42063e38b634SEvan Quan 	/*
42073e38b634SEvan Quan 	 * If there is no valid entry within the container, the container
42083e38b634SEvan Quan 	 * is recognized as a self contained container. And the valid entry
42093e38b634SEvan Quan 	 * here means it has a valid naming and it is visible/supported by
42103e38b634SEvan Quan 	 * the ASIC.
42113e38b634SEvan Quan 	 */
42123e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
42133e38b634SEvan Quan 		if (container->sub_feature[i].name &&
42143e38b634SEvan Quan 		    amdgpu_is_od_feature_supported(adev,
42153e38b634SEvan Quan 			&container->sub_feature[i].ops))
42163e38b634SEvan Quan 			return false;
42173e38b634SEvan Quan 	}
42183e38b634SEvan Quan 
42193e38b634SEvan Quan 	return true;
42203e38b634SEvan Quan }
42213e38b634SEvan Quan 
42223e38b634SEvan Quan static int amdgpu_od_set_init(struct amdgpu_device *adev)
42233e38b634SEvan Quan {
42243e38b634SEvan Quan 	struct od_kobj *top_set, *sub_set;
42253e38b634SEvan Quan 	struct od_attribute *attribute;
42263e38b634SEvan Quan 	struct od_feature_container *container;
42273e38b634SEvan Quan 	struct od_feature_item *feature;
42283e38b634SEvan Quan 	int i, j;
42293e38b634SEvan Quan 	int ret;
42303e38b634SEvan Quan 
42313e38b634SEvan Quan 	/* Setup the top `gpu_od` directory which holds all other OD interfaces */
42323e38b634SEvan Quan 	top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
42333e38b634SEvan Quan 	if (!top_set)
42343e38b634SEvan Quan 		return -ENOMEM;
42353e38b634SEvan Quan 	list_add(&top_set->entry, &adev->pm.od_kobj_list);
42363e38b634SEvan Quan 
42373e38b634SEvan Quan 	ret = kobject_init_and_add(&top_set->kobj,
42383e38b634SEvan Quan 				   &od_ktype,
42393e38b634SEvan Quan 				   &adev->dev->kobj,
42403e38b634SEvan Quan 				   "%s",
42413e38b634SEvan Quan 				   "gpu_od");
42423e38b634SEvan Quan 	if (ret)
42433e38b634SEvan Quan 		goto err_out;
42443e38b634SEvan Quan 	INIT_LIST_HEAD(&top_set->attribute);
42453e38b634SEvan Quan 	top_set->priv = adev;
42463e38b634SEvan Quan 
42473e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
42483e38b634SEvan Quan 		container = &amdgpu_od_set.containers[i];
42493e38b634SEvan Quan 
42503e38b634SEvan Quan 		if (!container->name)
42513e38b634SEvan Quan 			continue;
42523e38b634SEvan Quan 
42533e38b634SEvan Quan 		/*
42543e38b634SEvan Quan 		 * If there is valid entries within the container, the container
42553e38b634SEvan Quan 		 * will be presented as a sub directory and all its holding entries
42563e38b634SEvan Quan 		 * will be presented as plain files under it.
42573e38b634SEvan Quan 		 * While if there is no valid entry within the container, the container
42583e38b634SEvan Quan 		 * itself will be presented as a plain file under top `gpu_od` directory.
42593e38b634SEvan Quan 		 */
42603e38b634SEvan Quan 		if (amdgpu_od_is_self_contained(adev, container)) {
42613e38b634SEvan Quan 			if (!amdgpu_is_od_feature_supported(adev,
42623e38b634SEvan Quan 			     &container->ops))
42633e38b634SEvan Quan 				continue;
42643e38b634SEvan Quan 
42653e38b634SEvan Quan 			/*
42663e38b634SEvan Quan 			 * The container is presented as a plain file under top `gpu_od`
42673e38b634SEvan Quan 			 * directory.
42683e38b634SEvan Quan 			 */
42693e38b634SEvan Quan 			attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
42703e38b634SEvan Quan 			if (!attribute) {
42713e38b634SEvan Quan 				ret = -ENOMEM;
42723e38b634SEvan Quan 				goto err_out;
42733e38b634SEvan Quan 			}
42743e38b634SEvan Quan 			list_add(&attribute->entry, &top_set->attribute);
42753e38b634SEvan Quan 
42763e38b634SEvan Quan 			attribute->attribute.attr.mode =
42773e38b634SEvan Quan 					container->ops.is_visible(adev);
42783e38b634SEvan Quan 			attribute->attribute.attr.name = container->name;
42793e38b634SEvan Quan 			attribute->attribute.show =
42803e38b634SEvan Quan 					container->ops.show;
42813e38b634SEvan Quan 			attribute->attribute.store =
42823e38b634SEvan Quan 					container->ops.store;
42833e38b634SEvan Quan 			ret = sysfs_create_file(&top_set->kobj,
42843e38b634SEvan Quan 						&attribute->attribute.attr);
42853e38b634SEvan Quan 			if (ret)
42863e38b634SEvan Quan 				goto err_out;
42873e38b634SEvan Quan 		} else {
42883e38b634SEvan Quan 			/* The container is presented as a sub directory. */
42893e38b634SEvan Quan 			sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
42903e38b634SEvan Quan 			if (!sub_set) {
42913e38b634SEvan Quan 				ret = -ENOMEM;
42923e38b634SEvan Quan 				goto err_out;
42933e38b634SEvan Quan 			}
42943e38b634SEvan Quan 			list_add(&sub_set->entry, &adev->pm.od_kobj_list);
42953e38b634SEvan Quan 
42963e38b634SEvan Quan 			ret = kobject_init_and_add(&sub_set->kobj,
42973e38b634SEvan Quan 						   &od_ktype,
42983e38b634SEvan Quan 						   &top_set->kobj,
42993e38b634SEvan Quan 						   "%s",
43003e38b634SEvan Quan 						   container->name);
43013e38b634SEvan Quan 			if (ret)
43023e38b634SEvan Quan 				goto err_out;
43033e38b634SEvan Quan 			INIT_LIST_HEAD(&sub_set->attribute);
43043e38b634SEvan Quan 			sub_set->priv = adev;
43053e38b634SEvan Quan 
43063e38b634SEvan Quan 			for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
43073e38b634SEvan Quan 				feature = &container->sub_feature[j];
43083e38b634SEvan Quan 				if (!feature->name)
43093e38b634SEvan Quan 					continue;
43103e38b634SEvan Quan 
43113e38b634SEvan Quan 				if (!amdgpu_is_od_feature_supported(adev,
43123e38b634SEvan Quan 				     &feature->ops))
43133e38b634SEvan Quan 					continue;
43143e38b634SEvan Quan 
43153e38b634SEvan Quan 				/*
43163e38b634SEvan Quan 				 * With the container presented as a sub directory, the entry within
43173e38b634SEvan Quan 				 * it is presented as a plain file under the sub directory.
43183e38b634SEvan Quan 				 */
43193e38b634SEvan Quan 				attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
43203e38b634SEvan Quan 				if (!attribute) {
43213e38b634SEvan Quan 					ret = -ENOMEM;
43223e38b634SEvan Quan 					goto err_out;
43233e38b634SEvan Quan 				}
43243e38b634SEvan Quan 				list_add(&attribute->entry, &sub_set->attribute);
43253e38b634SEvan Quan 
43263e38b634SEvan Quan 				attribute->attribute.attr.mode =
43273e38b634SEvan Quan 						feature->ops.is_visible(adev);
43283e38b634SEvan Quan 				attribute->attribute.attr.name = feature->name;
43293e38b634SEvan Quan 				attribute->attribute.show =
43303e38b634SEvan Quan 						feature->ops.show;
43313e38b634SEvan Quan 				attribute->attribute.store =
43323e38b634SEvan Quan 						feature->ops.store;
43333e38b634SEvan Quan 				ret = sysfs_create_file(&sub_set->kobj,
43343e38b634SEvan Quan 							&attribute->attribute.attr);
43353e38b634SEvan Quan 				if (ret)
43363e38b634SEvan Quan 					goto err_out;
43373e38b634SEvan Quan 			}
43383e38b634SEvan Quan 		}
43393e38b634SEvan Quan 	}
43403e38b634SEvan Quan 
4341*69bc7a8aSMa Jun 	/*
4342*69bc7a8aSMa Jun 	 * If gpu_od is the only member in the list, that means gpu_od is an
4343*69bc7a8aSMa Jun 	 * empty directory, so remove it.
4344*69bc7a8aSMa Jun 	 */
4345*69bc7a8aSMa Jun 	if (list_is_singular(&adev->pm.od_kobj_list))
4346*69bc7a8aSMa Jun 		goto err_out;
4347*69bc7a8aSMa Jun 
43483e38b634SEvan Quan 	return 0;
43493e38b634SEvan Quan 
43503e38b634SEvan Quan err_out:
43513e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
43523e38b634SEvan Quan 
43533e38b634SEvan Quan 	return ret;
43543e38b634SEvan Quan }
43553e38b634SEvan Quan 
4356e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4357e098bc96SEvan Quan {
435888e5c8f8SMa Jun 	enum amdgpu_sriov_vf_mode mode;
4359e098bc96SEvan Quan 	uint32_t mask = 0;
43603e38b634SEvan Quan 	int ret;
4361e098bc96SEvan Quan 
4362e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
4363e098bc96SEvan Quan 		return 0;
4364e098bc96SEvan Quan 
43655fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
43665fa99373SZhenGuo Yin 
4367e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
4368e098bc96SEvan Quan 		return 0;
4369e098bc96SEvan Quan 
437088e5c8f8SMa Jun 	mode = amdgpu_virt_get_sriov_vf_mode(adev);
437188e5c8f8SMa Jun 
437288e5c8f8SMa Jun 	/* under multi-vf mode, the hwmon attributes are all not supported */
437388e5c8f8SMa Jun 	if (mode != SRIOV_VF_MODE_MULTI_VF) {
4374e098bc96SEvan Quan 		adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
4375e098bc96SEvan Quan 														DRIVER_NAME, adev,
4376e098bc96SEvan Quan 														hwmon_groups);
4377e098bc96SEvan Quan 		if (IS_ERR(adev->pm.int_hwmon_dev)) {
4378e098bc96SEvan Quan 			ret = PTR_ERR(adev->pm.int_hwmon_dev);
437988e5c8f8SMa Jun 			dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4380e098bc96SEvan Quan 			return ret;
4381e098bc96SEvan Quan 		}
438288e5c8f8SMa Jun 	}
4383e098bc96SEvan Quan 
438488e5c8f8SMa Jun 	switch (mode) {
4385e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
4386e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
4387e098bc96SEvan Quan 		break;
4388e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
4389e098bc96SEvan Quan 		mask = 0;
4390e098bc96SEvan Quan 		break;
4391e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
4392e098bc96SEvan Quan 	default:
4393e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
4394e098bc96SEvan Quan 		break;
4395e098bc96SEvan Quan 	}
4396e098bc96SEvan Quan 
4397e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
4398e098bc96SEvan Quan 					       amdgpu_device_attrs,
4399e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
4400e098bc96SEvan Quan 					       mask,
4401e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
4402e098bc96SEvan Quan 	if (ret)
44033e38b634SEvan Quan 		goto err_out0;
44043e38b634SEvan Quan 
44053e38b634SEvan Quan 	if (amdgpu_dpm_is_overdrive_supported(adev)) {
44063e38b634SEvan Quan 		ret = amdgpu_od_set_init(adev);
44073e38b634SEvan Quan 		if (ret)
44083e38b634SEvan Quan 			goto err_out1;
44093e38b634SEvan Quan 	}
4410e098bc96SEvan Quan 
4411e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
4412e098bc96SEvan Quan 
4413e098bc96SEvan Quan 	return 0;
44143e38b634SEvan Quan 
44153e38b634SEvan Quan err_out1:
44163e38b634SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
44173e38b634SEvan Quan err_out0:
44183e38b634SEvan Quan 	if (adev->pm.int_hwmon_dev)
44193e38b634SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
44203e38b634SEvan Quan 
44213e38b634SEvan Quan 	return ret;
4422e098bc96SEvan Quan }
4423e098bc96SEvan Quan 
4424e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4425e098bc96SEvan Quan {
44263e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
44273e38b634SEvan Quan 
4428e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
4429e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4430e098bc96SEvan Quan 
4431e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4432e098bc96SEvan Quan }
4433e098bc96SEvan Quan 
4434e098bc96SEvan Quan /*
4435e098bc96SEvan Quan  * Debugfs info
4436e098bc96SEvan Quan  */
4437e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4438e098bc96SEvan Quan 
4439517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
4440e1b3bcaaSRan Sun 					   struct amdgpu_device *adev)
4441e1b3bcaaSRan Sun {
4442517cb957SHuang Rui 	uint16_t *p_val;
4443517cb957SHuang Rui 	uint32_t size;
4444517cb957SHuang Rui 	int i;
444579c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
4446517cb957SHuang Rui 
444779c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
444879c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
4449517cb957SHuang Rui 				GFP_KERNEL);
4450517cb957SHuang Rui 
4451517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4452517cb957SHuang Rui 					    (void *)p_val, &size)) {
445379c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
4454517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
4455517cb957SHuang Rui 					   *(p_val + i), i);
4456517cb957SHuang Rui 		}
4457517cb957SHuang Rui 
4458517cb957SHuang Rui 		kfree(p_val);
4459517cb957SHuang Rui 	}
4460517cb957SHuang Rui }
4461517cb957SHuang Rui 
4462e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4463e098bc96SEvan Quan {
44644e8303cfSLijo Lazar 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
44654e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4466e098bc96SEvan Quan 	uint32_t value;
4467800c53d6SXiaojian Du 	uint64_t value64 = 0;
4468e098bc96SEvan Quan 	uint32_t query = 0;
4469e098bc96SEvan Quan 	int size;
4470e098bc96SEvan Quan 
4471e098bc96SEvan Quan 	/* GPU Clocks */
4472e098bc96SEvan Quan 	size = sizeof(value);
4473e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
4474517cb957SHuang Rui 
4475517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
4476517cb957SHuang Rui 
4477e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
4478e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
4479e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
4480e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
4481e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4482e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4483e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4484e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
4485e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
4486e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
4487e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
4488e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
4489e098bc96SEvan Quan 	size = sizeof(uint32_t);
44906127d7dfSAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
44916127d7dfSAlex Deucher 		if (adev->flags & AMD_IS_APU)
44926127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff);
44936127d7dfSAlex Deucher 		else
44946127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff);
44956127d7dfSAlex Deucher 	}
4496e0e1764aSAlex Deucher 	size = sizeof(uint32_t);
44976127d7dfSAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
44986127d7dfSAlex Deucher 		if (adev->flags & AMD_IS_APU)
44996127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff);
45006127d7dfSAlex Deucher 		else
45016127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff);
45026127d7dfSAlex Deucher 	}
4503e098bc96SEvan Quan 	size = sizeof(value);
4504e098bc96SEvan Quan 	seq_printf(m, "\n");
4505e098bc96SEvan Quan 
4506e098bc96SEvan Quan 	/* GPU Temp */
4507e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4508e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4509e098bc96SEvan Quan 
4510e098bc96SEvan Quan 	/* GPU Load */
4511e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4512e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
4513e098bc96SEvan Quan 	/* MEM Load */
4514e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4515e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
4516d1b2703cSXiaojian Du 	/* VCN Load */
4517d1b2703cSXiaojian Du 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
4518d1b2703cSXiaojian Du 		seq_printf(m, "VCN Load: %u %%\n", value);
4519e098bc96SEvan Quan 
4520e098bc96SEvan Quan 	seq_printf(m, "\n");
4521e098bc96SEvan Quan 
4522e098bc96SEvan Quan 	/* SMC feature mask */
4523e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4524e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4525e098bc96SEvan Quan 
45268ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
45278ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4528e098bc96SEvan Quan 		/* VCN clocks */
4529e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4530e098bc96SEvan Quan 			if (!value) {
45316127d7dfSAlex Deucher 				seq_printf(m, "VCN: Powered down\n");
4532e098bc96SEvan Quan 			} else {
45336127d7dfSAlex Deucher 				seq_printf(m, "VCN: Powered up\n");
4534e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4535e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4536e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4537e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4538e098bc96SEvan Quan 			}
4539e098bc96SEvan Quan 		}
4540e098bc96SEvan Quan 		seq_printf(m, "\n");
4541e098bc96SEvan Quan 	} else {
4542e098bc96SEvan Quan 		/* UVD clocks */
4543e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4544e098bc96SEvan Quan 			if (!value) {
45456127d7dfSAlex Deucher 				seq_printf(m, "UVD: Powered down\n");
4546e098bc96SEvan Quan 			} else {
45476127d7dfSAlex Deucher 				seq_printf(m, "UVD: Powered up\n");
4548e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4549e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4550e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4551e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4552e098bc96SEvan Quan 			}
4553e098bc96SEvan Quan 		}
4554e098bc96SEvan Quan 		seq_printf(m, "\n");
4555e098bc96SEvan Quan 
4556e098bc96SEvan Quan 		/* VCE clocks */
4557e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4558e098bc96SEvan Quan 			if (!value) {
45596127d7dfSAlex Deucher 				seq_printf(m, "VCE: Powered down\n");
4560e098bc96SEvan Quan 			} else {
45616127d7dfSAlex Deucher 				seq_printf(m, "VCE: Powered up\n");
4562e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4563e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4564e098bc96SEvan Quan 			}
4565e098bc96SEvan Quan 		}
4566e098bc96SEvan Quan 	}
4567e098bc96SEvan Quan 
4568e098bc96SEvan Quan 	return 0;
4569e098bc96SEvan Quan }
4570e098bc96SEvan Quan 
457144762718SNathan Chancellor static const struct cg_flag_name clocks[] = {
457244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
457344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
457444762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
457544762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
457644762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
457744762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
457844762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
457944762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
458044762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
458144762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
458244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
458344762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
458444762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
458544762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
458644762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
458744762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
458844762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
458944762718SNathan Chancellor 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
459044762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
459144762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
459244762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
459344762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
459444762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
459544762718SNathan Chancellor 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
459644762718SNathan Chancellor 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
459744762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
459844762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
459944762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
460044762718SNathan Chancellor 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
460144762718SNathan Chancellor 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
460244762718SNathan Chancellor 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
460344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
460444762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
460544762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
460644762718SNathan Chancellor 	{0, NULL},
460744762718SNathan Chancellor };
460844762718SNathan Chancellor 
460925faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
4610e098bc96SEvan Quan {
4611e098bc96SEvan Quan 	int i;
4612e098bc96SEvan Quan 
4613e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
4614e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
4615e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
4616e098bc96SEvan Quan }
4617e098bc96SEvan Quan 
4618373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
4619e098bc96SEvan Quan {
4620373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4621373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
462225faeddcSEvan Quan 	u64 flags = 0;
4623e098bc96SEvan Quan 	int r;
4624e098bc96SEvan Quan 
462553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
4626e098bc96SEvan Quan 		return -EPERM;
4627d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
4628d2ae842dSAlex Deucher 		return -EPERM;
4629e098bc96SEvan Quan 
4630e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
4631e098bc96SEvan Quan 	if (r < 0) {
4632e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
4633e098bc96SEvan Quan 		return r;
4634e098bc96SEvan Quan 	}
4635e098bc96SEvan Quan 
463679c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
4637e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
4638e098bc96SEvan Quan 		if (r)
4639e098bc96SEvan Quan 			goto out;
464079c65f3fSEvan Quan 	}
4641e098bc96SEvan Quan 
4642e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
4643e098bc96SEvan Quan 
464425faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
4645e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
4646e098bc96SEvan Quan 	seq_printf(m, "\n");
4647e098bc96SEvan Quan 
4648e098bc96SEvan Quan out:
4649e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
4650e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
4651e098bc96SEvan Quan 
4652e098bc96SEvan Quan 	return r;
4653e098bc96SEvan Quan }
4654e098bc96SEvan Quan 
4655373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4656373720f7SNirmoy Das 
465727ebf21fSLijo Lazar /*
465827ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
465927ebf21fSLijo Lazar  *
466027ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
466127ebf21fSLijo Lazar  */
466227ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
466327ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
466427ebf21fSLijo Lazar {
466527ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
466627ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
466727ebf21fSLijo Lazar 	void *smu_prv_buf;
466879c65f3fSEvan Quan 	int ret = 0;
466927ebf21fSLijo Lazar 
467027ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
467127ebf21fSLijo Lazar 		return -EPERM;
467227ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
467327ebf21fSLijo Lazar 		return -EPERM;
467427ebf21fSLijo Lazar 
467579c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
467679c65f3fSEvan Quan 	if (ret)
467779c65f3fSEvan Quan 		return ret;
467827ebf21fSLijo Lazar 
467927ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
468027ebf21fSLijo Lazar 		return -EINVAL;
468127ebf21fSLijo Lazar 
468227ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
468327ebf21fSLijo Lazar 				       smu_prv_buf_size);
468427ebf21fSLijo Lazar }
468527ebf21fSLijo Lazar 
468627ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
468727ebf21fSLijo Lazar 	.owner = THIS_MODULE,
468827ebf21fSLijo Lazar 	.open = simple_open,
468927ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
469027ebf21fSLijo Lazar 	.llseek = default_llseek,
469127ebf21fSLijo Lazar };
469227ebf21fSLijo Lazar 
4693e098bc96SEvan Quan #endif
4694e098bc96SEvan Quan 
4695373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
4696e098bc96SEvan Quan {
4697e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4698373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
4699373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
4700373720f7SNirmoy Das 
47011613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
47021613f346SFlora Cui 		return;
47031613f346SFlora Cui 
4704373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4705373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
4706373720f7SNirmoy Das 
470727ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
470827ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
470927ebf21fSLijo Lazar 					 adev,
471027ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
471127ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
47121f5fc7a5SAndrey Grodzovsky 
471379c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
4714e098bc96SEvan Quan #endif
4715e098bc96SEvan Quan }
4716